| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- N A M E T -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- |
| 17 |
-- for more details. You should have received a copy of the GNU General -- |
| 18 |
-- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
| 19 |
-- http://www.gnu.org/licenses for a complete copy of the license. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
-- WARNING: There is a C version of this package. Any changes to this |
| 27 |
-- source file must be properly reflected in the C header file namet.h |
| 28 |
-- which is created manually from namet.ads and namet.adb. |
| 29 |
|
| 30 |
with Debug; use Debug; |
| 31 |
with Opt; use Opt; |
| 32 |
with Output; use Output; |
| 33 |
with Widechar; |
| 34 |
|
| 35 |
with Interfaces; use Interfaces; |
| 36 |
|
| 37 |
package body Namet is |
| 38 |
|
| 39 |
Name_Chars_Reserve : constant := 5000; |
| 40 |
Name_Entries_Reserve : constant := 100; |
| 41 |
-- The names table is locked during gigi processing, since gigi assumes |
| 42 |
-- that the table does not move. After returning from gigi, the names |
| 43 |
-- table is unlocked again, since writing library file information needs |
| 44 |
-- to generate some extra names. To avoid the inefficiency of always |
| 45 |
-- reallocating during this second unlocked phase, we reserve a bit of |
| 46 |
-- extra space before doing the release call. |
| 47 |
|
| 48 |
Hash_Num : constant Int := 2**16; |
| 49 |
-- Number of headers in the hash table. Current hash algorithm is closely |
| 50 |
-- tailored to this choice, so it can only be changed if a corresponding |
| 51 |
-- change is made to the hash algorithm. |
| 52 |
|
| 53 |
Hash_Max : constant Int := Hash_Num - 1; |
| 54 |
-- Indexes in the hash header table run from 0 to Hash_Num - 1 |
| 55 |
|
| 56 |
subtype Hash_Index_Type is Int range 0 .. Hash_Max; |
| 57 |
-- Range of hash index values |
| 58 |
|
| 59 |
Hash_Table : array (Hash_Index_Type) of Name_Id; |
| 60 |
-- The hash table is used to locate existing entries in the names table. |
| 61 |
-- The entries point to the first names table entry whose hash value |
| 62 |
-- matches the hash code. Then subsequent names table entries with the |
| 63 |
-- same hash code value are linked through the Hash_Link fields. |
| 64 |
|
| 65 |
----------------------- |
| 66 |
-- Local Subprograms -- |
| 67 |
----------------------- |
| 68 |
|
| 69 |
function Hash (Buf : Bounded_String) return Hash_Index_Type; |
| 70 |
pragma Inline (Hash); |
| 71 |
-- Compute hash code for name stored in Buf |
| 72 |
|
| 73 |
procedure Strip_Qualification_And_Suffixes (Buf : in out Bounded_String); |
| 74 |
-- Given an encoded entity name in Buf, remove package body |
| 75 |
-- suffix as described for Strip_Package_Body_Suffix, and also remove |
| 76 |
-- all qualification, i.e. names followed by two underscores. |
| 77 |
|
| 78 |
----------------------------- |
| 79 |
-- Add_Char_To_Name_Buffer -- |
| 80 |
----------------------------- |
| 81 |
|
| 82 |
procedure Add_Char_To_Name_Buffer (C : Character) is |
| 83 |
begin |
| 84 |
Append (Global_Name_Buffer, C); |
| 85 |
end Add_Char_To_Name_Buffer; |
| 86 |
|
| 87 |
---------------------------- |
| 88 |
-- Add_Nat_To_Name_Buffer -- |
| 89 |
---------------------------- |
| 90 |
|
| 91 |
procedure Add_Nat_To_Name_Buffer (V : Nat) is |
| 92 |
begin |
| 93 |
Append (Global_Name_Buffer, V); |
| 94 |
end Add_Nat_To_Name_Buffer; |
| 95 |
|
| 96 |
---------------------------- |
| 97 |
-- Add_Str_To_Name_Buffer -- |
| 98 |
---------------------------- |
| 99 |
|
| 100 |
procedure Add_Str_To_Name_Buffer (S : String) is |
| 101 |
begin |
| 102 |
Append (Global_Name_Buffer, S); |
| 103 |
end Add_Str_To_Name_Buffer; |
| 104 |
|
| 105 |
------------ |
| 106 |
-- Append -- |
| 107 |
------------ |
| 108 |
|
| 109 |
procedure Append (Buf : in out Bounded_String; C : Character) is |
| 110 |
begin |
| 111 |
Buf.Length := Buf.Length + 1; |
| 112 |
|
| 113 |
if Buf.Length > Buf.Chars'Last then |
| 114 |
Write_Str ("Name buffer overflow; Max_Length = "); |
| 115 |
Write_Int (Int (Buf.Max_Length)); |
| 116 |
Write_Line (""); |
| 117 |
raise Program_Error; |
| 118 |
end if; |
| 119 |
|
| 120 |
Buf.Chars (Buf.Length) := C; |
| 121 |
end Append; |
| 122 |
|
| 123 |
procedure Append (Buf : in out Bounded_String; V : Nat) is |
| 124 |
begin |
| 125 |
if V >= 10 then |
| 126 |
Append (Buf, V / 10); |
| 127 |
end if; |
| 128 |
|
| 129 |
Append (Buf, Character'Val (Character'Pos ('0') + V rem 10)); |
| 130 |
end Append; |
| 131 |
|
| 132 |
procedure Append (Buf : in out Bounded_String; S : String) is |
| 133 |
First : constant Natural := Buf.Length + 1; |
| 134 |
begin |
| 135 |
Buf.Length := Buf.Length + S'Length; |
| 136 |
|
| 137 |
if Buf.Length > Buf.Chars'Last then |
| 138 |
Write_Str ("Name buffer overflow; Max_Length = "); |
| 139 |
Write_Int (Int (Buf.Max_Length)); |
| 140 |
Write_Line (""); |
| 141 |
raise Program_Error; |
| 142 |
end if; |
| 143 |
|
| 144 |
Buf.Chars (First .. Buf.Length) := S; |
| 145 |
-- A loop calling Append(Character) would be cleaner, but this slice |
| 146 |
-- assignment is substantially faster. |
| 147 |
end Append; |
| 148 |
|
| 149 |
procedure Append (Buf : in out Bounded_String; Buf2 : Bounded_String) is |
| 150 |
begin |
| 151 |
Append (Buf, Buf2.Chars (1 .. Buf2.Length)); |
| 152 |
end Append; |
| 153 |
|
| 154 |
procedure Append (Buf : in out Bounded_String; Id : Valid_Name_Id) is |
| 155 |
pragma Assert (Is_Valid_Name (Id)); |
| 156 |
|
| 157 |
Index : constant Int := Name_Entries.Table (Id).Name_Chars_Index; |
| 158 |
Len : constant Short := Name_Entries.Table (Id).Name_Len; |
| 159 |
Chars : Name_Chars.Table_Type renames |
| 160 |
Name_Chars.Table (Index + 1 .. Index + Int (Len)); |
| 161 |
begin |
| 162 |
Append (Buf, String (Chars)); |
| 163 |
end Append; |
| 164 |
|
| 165 |
-------------------- |
| 166 |
-- Append_Decoded -- |
| 167 |
-------------------- |
| 168 |
|
| 169 |
procedure Append_Decoded |
| 170 |
(Buf : in out Bounded_String; |
| 171 |
Id : Valid_Name_Id) |
| 172 |
is |
| 173 |
Temp : Bounded_String; |
| 174 |
|
| 175 |
function Has_Encodings (Temp : Bounded_String) return Boolean; |
| 176 |
-- True if Temp contains encoded characters. If not, we can set |
| 177 |
-- Name_Has_No_Encodings to True below, and never call this again |
| 178 |
-- on the same Name_Id. |
| 179 |
|
| 180 |
function Has_Encodings (Temp : Bounded_String) return Boolean is |
| 181 |
begin |
| 182 |
for J in 1 .. Temp.Length loop |
| 183 |
if Temp.Chars (J) in 'U' | 'W' | 'Q' | 'O' then |
| 184 |
return True; |
| 185 |
end if; |
| 186 |
end loop; |
| 187 |
|
| 188 |
return False; |
| 189 |
end Has_Encodings; |
| 190 |
|
| 191 |
begin |
| 192 |
Append (Temp, Id); |
| 193 |
|
| 194 |
-- Skip scan if we already know there are no encodings (i.e. the first |
| 195 |
-- time this was called on Id, the Has_Encodings call below returned |
| 196 |
-- False). |
| 197 |
|
| 198 |
if Name_Entries.Table (Id).Name_Has_No_Encodings then |
| 199 |
goto Done; |
| 200 |
end if; |
| 201 |
|
| 202 |
if not Has_Encodings (Temp) then |
| 203 |
Name_Entries.Table (Id).Name_Has_No_Encodings := True; |
| 204 |
goto Done; |
| 205 |
end if; |
| 206 |
|
| 207 |
-- Here we have at least some encoding that we must decode |
| 208 |
|
| 209 |
Decode : declare |
| 210 |
New_Len : Natural; |
| 211 |
Old : Positive; |
| 212 |
New_Buf : String (1 .. Temp.Chars'Last); |
| 213 |
|
| 214 |
procedure Copy_One_Character; |
| 215 |
-- Copy a character from Temp.Chars to New_Buf. Includes case |
| 216 |
-- of copying a Uhh,Whhhh,WWhhhhhhhh sequence and decoding it. |
| 217 |
|
| 218 |
function Hex (N : Natural) return Word; |
| 219 |
-- Scans past N digits using Old pointer and returns hex value |
| 220 |
|
| 221 |
procedure Insert_Character (C : Character); |
| 222 |
-- Insert a new character into output decoded name |
| 223 |
|
| 224 |
------------------------ |
| 225 |
-- Copy_One_Character -- |
| 226 |
------------------------ |
| 227 |
|
| 228 |
procedure Copy_One_Character is |
| 229 |
C : Character; |
| 230 |
|
| 231 |
begin |
| 232 |
C := Temp.Chars (Old); |
| 233 |
|
| 234 |
-- U (upper half insertion case) |
| 235 |
|
| 236 |
if C = 'U' |
| 237 |
and then Old < Temp.Length |
| 238 |
and then Temp.Chars (Old + 1) not in 'A' .. 'Z' | '_' |
| 239 |
then |
| 240 |
Old := Old + 1; |
| 241 |
|
| 242 |
-- If we have upper half encoding, then we have to set an |
| 243 |
-- appropriate wide character sequence for this character. |
| 244 |
|
| 245 |
if Upper_Half_Encoding then |
| 246 |
Widechar.Set_Wide (Char_Code (Hex (2)), New_Buf, New_Len); |
| 247 |
|
| 248 |
-- For other encoding methods, upper half characters can |
| 249 |
-- simply use their normal representation. |
| 250 |
|
| 251 |
else |
| 252 |
declare |
| 253 |
W2 : constant Word := Hex (2); |
| 254 |
begin |
| 255 |
pragma Assert (W2 <= 255); |
| 256 |
-- Add assumption to facilitate static analysis. Note |
| 257 |
-- that we cannot use pragma Assume for bootstrap |
| 258 |
-- reasons. |
| 259 |
Insert_Character (Character'Val (W2)); |
| 260 |
end; |
| 261 |
end if; |
| 262 |
|
| 263 |
-- WW (wide wide character insertion) |
| 264 |
|
| 265 |
elsif C = 'W' |
| 266 |
and then Old < Temp.Length |
| 267 |
and then Temp.Chars (Old + 1) = 'W' |
| 268 |
then |
| 269 |
Old := Old + 2; |
| 270 |
Widechar.Set_Wide (Char_Code (Hex (8)), New_Buf, New_Len); |
| 271 |
|
| 272 |
-- W (wide character insertion) |
| 273 |
|
| 274 |
elsif C = 'W' |
| 275 |
and then Old < Temp.Length |
| 276 |
and then Temp.Chars (Old + 1) not in 'A' .. 'Z' | '_' |
| 277 |
then |
| 278 |
Old := Old + 1; |
| 279 |
Widechar.Set_Wide (Char_Code (Hex (4)), New_Buf, New_Len); |
| 280 |
|
| 281 |
-- Any other character is copied unchanged |
| 282 |
|
| 283 |
else |
| 284 |
Insert_Character (C); |
| 285 |
Old := Old + 1; |
| 286 |
end if; |
| 287 |
end Copy_One_Character; |
| 288 |
|
| 289 |
--------- |
| 290 |
-- Hex -- |
| 291 |
--------- |
| 292 |
|
| 293 |
function Hex (N : Natural) return Word is |
| 294 |
T : Word := 0; |
| 295 |
C : Character; |
| 296 |
|
| 297 |
begin |
| 298 |
for J in 1 .. N loop |
| 299 |
C := Temp.Chars (Old); |
| 300 |
Old := Old + 1; |
| 301 |
|
| 302 |
pragma Assert (C in '0' .. '9' | 'a' .. 'f'); |
| 303 |
|
| 304 |
if C <= '9' then |
| 305 |
T := 16 * T + Character'Pos (C) - Character'Pos ('0'); |
| 306 |
else -- C in 'a' .. 'f' |
| 307 |
T := 16 * T + Character'Pos (C) - (Character'Pos ('a') - 10); |
| 308 |
end if; |
| 309 |
end loop; |
| 310 |
|
| 311 |
return T; |
| 312 |
end Hex; |
| 313 |
|
| 314 |
---------------------- |
| 315 |
-- Insert_Character -- |
| 316 |
---------------------- |
| 317 |
|
| 318 |
procedure Insert_Character (C : Character) is |
| 319 |
begin |
| 320 |
New_Len := New_Len + 1; |
| 321 |
New_Buf (New_Len) := C; |
| 322 |
end Insert_Character; |
| 323 |
|
| 324 |
-- Start of processing for Decode |
| 325 |
|
| 326 |
begin |
| 327 |
New_Len := 0; |
| 328 |
Old := 1; |
| 329 |
|
| 330 |
-- Loop through characters of name |
| 331 |
|
| 332 |
while Old <= Temp.Length loop |
| 333 |
|
| 334 |
-- Case of character literal, put apostrophes around character |
| 335 |
|
| 336 |
if Temp.Chars (Old) = 'Q' |
| 337 |
and then Old < Temp.Length |
| 338 |
then |
| 339 |
Old := Old + 1; |
| 340 |
Insert_Character ('''); |
| 341 |
Copy_One_Character; |
| 342 |
Insert_Character ('''); |
| 343 |
|
| 344 |
-- Case of operator name |
| 345 |
|
| 346 |
elsif Temp.Chars (Old) = 'O' |
| 347 |
and then Old < Temp.Length |
| 348 |
and then Temp.Chars (Old + 1) not in 'A' .. 'Z' | '_' |
| 349 |
then |
| 350 |
Old := Old + 1; |
| 351 |
|
| 352 |
declare |
| 353 |
-- This table maps the 2nd and 3rd characters of the name |
| 354 |
-- into the required output. Two blanks means leave the |
| 355 |
-- name alone |
| 356 |
|
| 357 |
Map : constant String := |
| 358 |
"ab " & -- Oabs => "abs" |
| 359 |
"ad+ " & -- Oadd => "+" |
| 360 |
"an " & -- Oand => "and" |
| 361 |
"co& " & -- Oconcat => "&" |
| 362 |
"di/ " & -- Odivide => "/" |
| 363 |
"eq= " & -- Oeq => "=" |
| 364 |
"ex**" & -- Oexpon => "**" |
| 365 |
"gt> " & -- Ogt => ">" |
| 366 |
"ge>=" & -- Oge => ">=" |
| 367 |
"le<=" & -- Ole => "<=" |
| 368 |
"lt< " & -- Olt => "<" |
| 369 |
"mo " & -- Omod => "mod" |
| 370 |
"mu* " & -- Omutliply => "*" |
| 371 |
"ne/=" & -- One => "/=" |
| 372 |
"no " & -- Onot => "not" |
| 373 |
"or " & -- Oor => "or" |
| 374 |
"re " & -- Orem => "rem" |
| 375 |
"su- " & -- Osubtract => "-" |
| 376 |
"xo "; -- Oxor => "xor" |
| 377 |
|
| 378 |
J : Integer; |
| 379 |
|
| 380 |
begin |
| 381 |
Insert_Character ('"'); |
| 382 |
|
| 383 |
-- Search the map. Note that this loop must terminate, if |
| 384 |
-- not we have some kind of internal error, and a constraint |
| 385 |
-- error may be raised. |
| 386 |
|
| 387 |
J := Map'First; |
| 388 |
loop |
| 389 |
exit when Temp.Chars (Old) = Map (J) |
| 390 |
and then Temp.Chars (Old + 1) = Map (J + 1); |
| 391 |
J := J + 4; |
| 392 |
end loop; |
| 393 |
|
| 394 |
-- Special operator name |
| 395 |
|
| 396 |
if Map (J + 2) /= ' ' then |
| 397 |
Insert_Character (Map (J + 2)); |
| 398 |
|
| 399 |
if Map (J + 3) /= ' ' then |
| 400 |
Insert_Character (Map (J + 3)); |
| 401 |
end if; |
| 402 |
|
| 403 |
Insert_Character ('"'); |
| 404 |
|
| 405 |
-- Skip past original operator name in input |
| 406 |
|
| 407 |
while Old <= Temp.Length |
| 408 |
and then Temp.Chars (Old) in 'a' .. 'z' |
| 409 |
loop |
| 410 |
Old := Old + 1; |
| 411 |
end loop; |
| 412 |
|
| 413 |
-- For other operator names, leave them in lower case, |
| 414 |
-- surrounded by apostrophes |
| 415 |
|
| 416 |
else |
| 417 |
-- Copy original operator name from input to output |
| 418 |
|
| 419 |
while Old <= Temp.Length |
| 420 |
and then Temp.Chars (Old) in 'a' .. 'z' |
| 421 |
loop |
| 422 |
Copy_One_Character; |
| 423 |
end loop; |
| 424 |
|
| 425 |
Insert_Character ('"'); |
| 426 |
end if; |
| 427 |
end; |
| 428 |
|
| 429 |
-- Else copy one character and keep going |
| 430 |
|
| 431 |
else |
| 432 |
Copy_One_Character; |
| 433 |
end if; |
| 434 |
end loop; |
| 435 |
|
| 436 |
-- Copy new buffer as result |
| 437 |
|
| 438 |
Temp.Length := New_Len; |
| 439 |
Temp.Chars (1 .. New_Len) := New_Buf (1 .. New_Len); |
| 440 |
end Decode; |
| 441 |
|
| 442 |
<<Done>> |
| 443 |
Append (Buf, Temp); |
| 444 |
end Append_Decoded; |
| 445 |
|
| 446 |
---------------------------------- |
| 447 |
-- Append_Decoded_With_Brackets -- |
| 448 |
---------------------------------- |
| 449 |
|
| 450 |
procedure Append_Decoded_With_Brackets |
| 451 |
(Buf : in out Bounded_String; |
| 452 |
Id : Valid_Name_Id) |
| 453 |
is |
| 454 |
P : Natural; |
| 455 |
|
| 456 |
begin |
| 457 |
-- Case of operator name, normal decoding is fine |
| 458 |
|
| 459 |
if Buf.Chars (1) = 'O' then |
| 460 |
Append_Decoded (Buf, Id); |
| 461 |
|
| 462 |
-- For character literals, normal decoding is fine |
| 463 |
|
| 464 |
elsif Buf.Chars (1) = 'Q' then |
| 465 |
Append_Decoded (Buf, Id); |
| 466 |
|
| 467 |
-- Only remaining issue is U/W/WW sequences |
| 468 |
|
| 469 |
else |
| 470 |
declare |
| 471 |
Temp : Bounded_String; |
| 472 |
begin |
| 473 |
Append (Temp, Id); |
| 474 |
|
| 475 |
P := 1; |
| 476 |
while P < Temp.Length loop |
| 477 |
-- Cheap test for the common case of no encoding |
| 478 |
|
| 479 |
if Temp.Chars (P + 1) in 'A' .. 'Z' |
| 480 |
and then Temp.Chars (P + 1) /= 'W' |
| 481 |
then |
| 482 |
P := P + 1; |
| 483 |
|
| 484 |
-- Uhh encoding |
| 485 |
|
| 486 |
elsif Temp.Chars (P) = 'U' then |
| 487 |
for J in reverse P + 3 .. P + Temp.Length loop |
| 488 |
Temp.Chars (J + 3) := Temp.Chars (J); |
| 489 |
end loop; |
| 490 |
|
| 491 |
Temp.Length := Temp.Length + 3; |
| 492 |
Temp.Chars (P + 3) := Temp.Chars (P + 2); |
| 493 |
Temp.Chars (P + 2) := Temp.Chars (P + 1); |
| 494 |
Temp.Chars (P) := '['; |
| 495 |
Temp.Chars (P + 1) := '"'; |
| 496 |
Temp.Chars (P + 4) := '"'; |
| 497 |
Temp.Chars (P + 5) := ']'; |
| 498 |
P := P + 6; |
| 499 |
|
| 500 |
-- WWhhhhhhhh encoding |
| 501 |
|
| 502 |
elsif Temp.Chars (P) = 'W' |
| 503 |
and then P + 9 <= Temp.Length |
| 504 |
and then Temp.Chars (P + 1) = 'W' |
| 505 |
and then Temp.Chars (P + 2) not in 'A' .. 'Z' | '_' |
| 506 |
then |
| 507 |
Temp.Chars (P + 12 .. Temp.Length + 2) := |
| 508 |
Temp.Chars (P + 10 .. Temp.Length); |
| 509 |
Temp.Chars (P) := '['; |
| 510 |
Temp.Chars (P + 1) := '"'; |
| 511 |
Temp.Chars (P + 10) := '"'; |
| 512 |
Temp.Chars (P + 11) := ']'; |
| 513 |
Temp.Length := Temp.Length + 2; |
| 514 |
P := P + 12; |
| 515 |
|
| 516 |
-- Whhhh encoding |
| 517 |
|
| 518 |
elsif Temp.Chars (P) = 'W' |
| 519 |
and then P < Temp.Length |
| 520 |
and then Temp.Chars (P + 1) not in 'A' .. 'Z' | '_' |
| 521 |
then |
| 522 |
Temp.Chars (P + 8 .. P + Temp.Length + 3) := |
| 523 |
Temp.Chars (P + 5 .. Temp.Length); |
| 524 |
Temp.Chars (P + 2 .. P + 5) := Temp.Chars (P + 1 .. P + 4); |
| 525 |
Temp.Chars (P) := '['; |
| 526 |
Temp.Chars (P + 1) := '"'; |
| 527 |
Temp.Chars (P + 6) := '"'; |
| 528 |
Temp.Chars (P + 7) := ']'; |
| 529 |
Temp.Length := Temp.Length + 3; |
| 530 |
P := P + 8; |
| 531 |
|
| 532 |
else |
| 533 |
P := P + 1; |
| 534 |
end if; |
| 535 |
end loop; |
| 536 |
|
| 537 |
Append (Buf, Temp); |
| 538 |
end; |
| 539 |
end if; |
| 540 |
end Append_Decoded_With_Brackets; |
| 541 |
|
| 542 |
-------------------- |
| 543 |
-- Append_Encoded -- |
| 544 |
-------------------- |
| 545 |
|
| 546 |
procedure Append_Encoded (Buf : in out Bounded_String; C : Char_Code) is |
| 547 |
procedure Set_Hex_Chars (C : Char_Code); |
| 548 |
-- Stores given value, which is in the range 0 .. 255, as two hex |
| 549 |
-- digits (using lower case a-f) in Buf.Chars, incrementing Buf.Length. |
| 550 |
|
| 551 |
------------------- |
| 552 |
-- Set_Hex_Chars -- |
| 553 |
------------------- |
| 554 |
|
| 555 |
procedure Set_Hex_Chars (C : Char_Code) is |
| 556 |
Hexd : constant String := "0123456789abcdef"; |
| 557 |
N : constant Natural := Natural (C); |
| 558 |
begin |
| 559 |
Buf.Chars (Buf.Length + 1) := Hexd (N / 16 + 1); |
| 560 |
Buf.Chars (Buf.Length + 2) := Hexd (N mod 16 + 1); |
| 561 |
Buf.Length := Buf.Length + 2; |
| 562 |
end Set_Hex_Chars; |
| 563 |
|
| 564 |
-- Start of processing for Append_Encoded |
| 565 |
|
| 566 |
begin |
| 567 |
Buf.Length := Buf.Length + 1; |
| 568 |
|
| 569 |
if In_Character_Range (C) then |
| 570 |
declare |
| 571 |
CC : constant Character := Get_Character (C); |
| 572 |
begin |
| 573 |
if CC in 'a' .. 'z' | '0' .. '9' then |
| 574 |
Buf.Chars (Buf.Length) := CC; |
| 575 |
else |
| 576 |
Buf.Chars (Buf.Length) := 'U'; |
| 577 |
Set_Hex_Chars (C); |
| 578 |
end if; |
| 579 |
end; |
| 580 |
|
| 581 |
elsif In_Wide_Character_Range (C) then |
| 582 |
Buf.Chars (Buf.Length) := 'W'; |
| 583 |
Set_Hex_Chars (C / 256); |
| 584 |
Set_Hex_Chars (C mod 256); |
| 585 |
|
| 586 |
else |
| 587 |
Buf.Chars (Buf.Length) := 'W'; |
| 588 |
Buf.Length := Buf.Length + 1; |
| 589 |
Buf.Chars (Buf.Length) := 'W'; |
| 590 |
Set_Hex_Chars (C / 2 ** 24); |
| 591 |
Set_Hex_Chars ((C / 2 ** 16) mod 256); |
| 592 |
Set_Hex_Chars ((C / 256) mod 256); |
| 593 |
Set_Hex_Chars (C mod 256); |
| 594 |
end if; |
| 595 |
end Append_Encoded; |
| 596 |
|
| 597 |
------------------------ |
| 598 |
-- Append_Unqualified -- |
| 599 |
------------------------ |
| 600 |
|
| 601 |
procedure Append_Unqualified |
| 602 |
(Buf : in out Bounded_String; |
| 603 |
Id : Valid_Name_Id) |
| 604 |
is |
| 605 |
Temp : Bounded_String; |
| 606 |
begin |
| 607 |
Append (Temp, Id); |
| 608 |
Strip_Qualification_And_Suffixes (Temp); |
| 609 |
Append (Buf, Temp); |
| 610 |
end Append_Unqualified; |
| 611 |
|
| 612 |
-------------------------------- |
| 613 |
-- Append_Unqualified_Decoded -- |
| 614 |
-------------------------------- |
| 615 |
|
| 616 |
procedure Append_Unqualified_Decoded |
| 617 |
(Buf : in out Bounded_String; |
| 618 |
Id : Valid_Name_Id) |
| 619 |
is |
| 620 |
Temp : Bounded_String; |
| 621 |
begin |
| 622 |
Append_Decoded (Temp, Id); |
| 623 |
Strip_Qualification_And_Suffixes (Temp); |
| 624 |
Append (Buf, Temp); |
| 625 |
end Append_Unqualified_Decoded; |
| 626 |
|
| 627 |
-------------------------------- |
| 628 |
-- Destroy_Global_Name_Buffer -- |
| 629 |
-------------------------------- |
| 630 |
|
| 631 |
procedure Destroy_Global_Name_Buffer is |
| 632 |
procedure Do_It; |
| 633 |
-- Do the work. Needed only for "pragma Debug" below, so we don't do |
| 634 |
-- anything in production mode. |
| 635 |
|
| 636 |
procedure Do_It is |
| 637 |
begin |
| 638 |
Global_Name_Buffer.Length := Global_Name_Buffer.Max_Length; |
| 639 |
Global_Name_Buffer.Chars := (others => '!'); |
| 640 |
end Do_It; |
| 641 |
pragma Debug (Do_It); |
| 642 |
begin |
| 643 |
null; |
| 644 |
end Destroy_Global_Name_Buffer; |
| 645 |
|
| 646 |
-------------- |
| 647 |
-- Finalize -- |
| 648 |
-------------- |
| 649 |
|
| 650 |
procedure Finalize is |
| 651 |
F : array (Int range 0 .. 50) of Int; |
| 652 |
-- N'th entry is the number of chains of length N, except last entry, |
| 653 |
-- which is the number of chains of length F'Last or more. |
| 654 |
|
| 655 |
Max_Chain_Length : Nat := 0; |
| 656 |
-- Maximum length of all chains |
| 657 |
|
| 658 |
Probes : Nat := 0; |
| 659 |
-- Used to compute average number of probes |
| 660 |
|
| 661 |
Nsyms : Nat := 0; |
| 662 |
-- Number of symbols in table |
| 663 |
|
| 664 |
Verbosity : constant Int range 1 .. 3 := 1; |
| 665 |
pragma Warnings (Off, Verbosity); |
| 666 |
-- This constant indicates the level of verbosity in the output from |
| 667 |
-- this procedure. Currently this can only be changed by editing the |
| 668 |
-- declaration above and recompiling. That's good enough in practice, |
| 669 |
-- since we very rarely need to use this debug option. Settings are: |
| 670 |
-- |
| 671 |
-- 1 => print basic summary information |
| 672 |
-- 2 => in addition print number of entries per hash chain |
| 673 |
-- 3 => in addition print content of entries |
| 674 |
|
| 675 |
Zero : constant Int := Character'Pos ('0'); |
| 676 |
|
| 677 |
begin |
| 678 |
if not Debug_Flag_H then |
| 679 |
return; |
| 680 |
end if; |
| 681 |
|
| 682 |
for J in F'Range loop |
| 683 |
F (J) := 0; |
| 684 |
end loop; |
| 685 |
|
| 686 |
for J in Hash_Index_Type loop |
| 687 |
if Hash_Table (J) = No_Name then |
| 688 |
F (0) := F (0) + 1; |
| 689 |
|
| 690 |
else |
| 691 |
declare |
| 692 |
C : Nat; |
| 693 |
N : Name_Id; |
| 694 |
S : Int; |
| 695 |
|
| 696 |
begin |
| 697 |
C := 0; |
| 698 |
N := Hash_Table (J); |
| 699 |
|
| 700 |
while N /= No_Name loop |
| 701 |
N := Name_Entries.Table (N).Hash_Link; |
| 702 |
C := C + 1; |
| 703 |
end loop; |
| 704 |
|
| 705 |
Nsyms := Nsyms + 1; |
| 706 |
Probes := Probes + (1 + C) * 100; |
| 707 |
|
| 708 |
if C > Max_Chain_Length then |
| 709 |
Max_Chain_Length := C; |
| 710 |
end if; |
| 711 |
|
| 712 |
if Verbosity >= 2 then |
| 713 |
Write_Str ("Hash_Table ("); |
| 714 |
Write_Int (J); |
| 715 |
Write_Str (") has "); |
| 716 |
Write_Int (C); |
| 717 |
Write_Str (" entries"); |
| 718 |
Write_Eol; |
| 719 |
end if; |
| 720 |
|
| 721 |
if C < F'Last then |
| 722 |
F (C) := F (C) + 1; |
| 723 |
else |
| 724 |
F (F'Last) := F (F'Last) + 1; |
| 725 |
end if; |
| 726 |
|
| 727 |
if Verbosity >= 3 then |
| 728 |
N := Hash_Table (J); |
| 729 |
while N /= No_Name loop |
| 730 |
S := Name_Entries.Table (N).Name_Chars_Index; |
| 731 |
|
| 732 |
Write_Str (" "); |
| 733 |
|
| 734 |
for J in 1 .. Name_Entries.Table (N).Name_Len loop |
| 735 |
Write_Char (Name_Chars.Table (S + Int (J))); |
| 736 |
end loop; |
| 737 |
|
| 738 |
Write_Eol; |
| 739 |
|
| 740 |
N := Name_Entries.Table (N).Hash_Link; |
| 741 |
end loop; |
| 742 |
end if; |
| 743 |
end; |
| 744 |
end if; |
| 745 |
end loop; |
| 746 |
|
| 747 |
Write_Eol; |
| 748 |
|
| 749 |
for J in F'Range loop |
| 750 |
if F (J) /= 0 then |
| 751 |
Write_Str ("Number of hash chains of length "); |
| 752 |
|
| 753 |
if J < 10 then |
| 754 |
Write_Char (' '); |
| 755 |
end if; |
| 756 |
|
| 757 |
Write_Int (J); |
| 758 |
|
| 759 |
if J = F'Last then |
| 760 |
Write_Str (" or greater"); |
| 761 |
end if; |
| 762 |
|
| 763 |
Write_Str (" = "); |
| 764 |
Write_Int (F (J)); |
| 765 |
Write_Eol; |
| 766 |
end if; |
| 767 |
end loop; |
| 768 |
|
| 769 |
-- Print out average number of probes, in the case where Name_Find is |
| 770 |
-- called for a string that is already in the table. |
| 771 |
|
| 772 |
Write_Eol; |
| 773 |
Write_Str ("Average number of probes for lookup = "); |
| 774 |
pragma Assert (Nsyms /= 0); |
| 775 |
-- Add assumption to facilitate static analysis. Here Nsyms cannot be |
| 776 |
-- zero because many symbols are added to the table by default. |
| 777 |
Probes := Probes / Nsyms; |
| 778 |
Write_Int (Probes / 200); |
| 779 |
Write_Char ('.'); |
| 780 |
Probes := (Probes mod 200) / 2; |
| 781 |
Write_Char (Character'Val (Zero + Probes / 10)); |
| 782 |
Write_Char (Character'Val (Zero + Probes mod 10)); |
| 783 |
Write_Eol; |
| 784 |
|
| 785 |
Write_Str ("Max_Chain_Length = "); |
| 786 |
Write_Int (Max_Chain_Length); |
| 787 |
Write_Eol; |
| 788 |
Write_Str ("Name_Chars'Length = "); |
| 789 |
Write_Int (Name_Chars.Last - Name_Chars.First + 1); |
| 790 |
Write_Eol; |
| 791 |
Write_Str ("Name_Entries'Length = "); |
| 792 |
Write_Int (Int (Name_Entries.Last - Name_Entries.First + 1)); |
| 793 |
Write_Eol; |
| 794 |
Write_Str ("Nsyms = "); |
| 795 |
Write_Int (Nsyms); |
| 796 |
Write_Eol; |
| 797 |
end Finalize; |
| 798 |
|
| 799 |
----------------------------- |
| 800 |
-- Get_Decoded_Name_String -- |
| 801 |
----------------------------- |
| 802 |
|
| 803 |
procedure Get_Decoded_Name_String (Id : Valid_Name_Id) is |
| 804 |
begin |
| 805 |
Global_Name_Buffer.Length := 0; |
| 806 |
Append_Decoded (Global_Name_Buffer, Id); |
| 807 |
end Get_Decoded_Name_String; |
| 808 |
|
| 809 |
------------------------------------------- |
| 810 |
-- Get_Decoded_Name_String_With_Brackets -- |
| 811 |
------------------------------------------- |
| 812 |
|
| 813 |
procedure Get_Decoded_Name_String_With_Brackets (Id : Valid_Name_Id) is |
| 814 |
begin |
| 815 |
Global_Name_Buffer.Length := 0; |
| 816 |
Append_Decoded_With_Brackets (Global_Name_Buffer, Id); |
| 817 |
end Get_Decoded_Name_String_With_Brackets; |
| 818 |
|
| 819 |
------------------------ |
| 820 |
-- Get_Last_Two_Chars -- |
| 821 |
------------------------ |
| 822 |
|
| 823 |
procedure Get_Last_Two_Chars |
| 824 |
(N : Valid_Name_Id; |
| 825 |
C1 : out Character; |
| 826 |
C2 : out Character) |
| 827 |
is |
| 828 |
NE : Name_Entry renames Name_Entries.Table (N); |
| 829 |
NEL : constant Int := Int (NE.Name_Len); |
| 830 |
|
| 831 |
begin |
| 832 |
if NEL >= 2 then |
| 833 |
C1 := Name_Chars.Table (NE.Name_Chars_Index + NEL - 1); |
| 834 |
C2 := Name_Chars.Table (NE.Name_Chars_Index + NEL - 0); |
| 835 |
else |
| 836 |
C1 := ASCII.NUL; |
| 837 |
C2 := ASCII.NUL; |
| 838 |
end if; |
| 839 |
end Get_Last_Two_Chars; |
| 840 |
|
| 841 |
--------------------- |
| 842 |
-- Get_Name_String -- |
| 843 |
--------------------- |
| 844 |
|
| 845 |
procedure Get_Name_String (Id : Valid_Name_Id) is |
| 846 |
begin |
| 847 |
Global_Name_Buffer.Length := 0; |
| 848 |
Append (Global_Name_Buffer, Id); |
| 849 |
end Get_Name_String; |
| 850 |
|
| 851 |
function Get_Name_String (Id : Valid_Name_Id) return String is |
| 852 |
Buf : Bounded_String (Max_Length => Natural (Length_Of_Name (Id))); |
| 853 |
begin |
| 854 |
Append (Buf, Id); |
| 855 |
return +Buf; |
| 856 |
end Get_Name_String; |
| 857 |
|
| 858 |
-------------------------------- |
| 859 |
-- Get_Name_String_And_Append -- |
| 860 |
-------------------------------- |
| 861 |
|
| 862 |
procedure Get_Name_String_And_Append (Id : Valid_Name_Id) is |
| 863 |
begin |
| 864 |
Append (Global_Name_Buffer, Id); |
| 865 |
end Get_Name_String_And_Append; |
| 866 |
|
| 867 |
----------------------------- |
| 868 |
-- Get_Name_Table_Boolean1 -- |
| 869 |
----------------------------- |
| 870 |
|
| 871 |
function Get_Name_Table_Boolean1 (Id : Valid_Name_Id) return Boolean is |
| 872 |
begin |
| 873 |
pragma Assert (Is_Valid_Name (Id)); |
| 874 |
return Name_Entries.Table (Id).Boolean1_Info; |
| 875 |
end Get_Name_Table_Boolean1; |
| 876 |
|
| 877 |
----------------------------- |
| 878 |
-- Get_Name_Table_Boolean2 -- |
| 879 |
----------------------------- |
| 880 |
|
| 881 |
function Get_Name_Table_Boolean2 (Id : Valid_Name_Id) return Boolean is |
| 882 |
begin |
| 883 |
pragma Assert (Is_Valid_Name (Id)); |
| 884 |
return Name_Entries.Table (Id).Boolean2_Info; |
| 885 |
end Get_Name_Table_Boolean2; |
| 886 |
|
| 887 |
----------------------------- |
| 888 |
-- Get_Name_Table_Boolean3 -- |
| 889 |
----------------------------- |
| 890 |
|
| 891 |
function Get_Name_Table_Boolean3 (Id : Valid_Name_Id) return Boolean is |
| 892 |
begin |
| 893 |
pragma Assert (Is_Valid_Name (Id)); |
| 894 |
return Name_Entries.Table (Id).Boolean3_Info; |
| 895 |
end Get_Name_Table_Boolean3; |
| 896 |
|
| 897 |
------------------------- |
| 898 |
-- Get_Name_Table_Byte -- |
| 899 |
------------------------- |
| 900 |
|
| 901 |
function Get_Name_Table_Byte (Id : Valid_Name_Id) return Byte is |
| 902 |
begin |
| 903 |
pragma Assert (Is_Valid_Name (Id)); |
| 904 |
return Name_Entries.Table (Id).Byte_Info; |
| 905 |
end Get_Name_Table_Byte; |
| 906 |
|
| 907 |
------------------------- |
| 908 |
-- Get_Name_Table_Int -- |
| 909 |
------------------------- |
| 910 |
|
| 911 |
function Get_Name_Table_Int (Id : Valid_Name_Id) return Int is |
| 912 |
begin |
| 913 |
pragma Assert (Is_Valid_Name (Id)); |
| 914 |
return Name_Entries.Table (Id).Int_Info; |
| 915 |
end Get_Name_Table_Int; |
| 916 |
|
| 917 |
----------------------------------------- |
| 918 |
-- Get_Unqualified_Decoded_Name_String -- |
| 919 |
----------------------------------------- |
| 920 |
|
| 921 |
procedure Get_Unqualified_Decoded_Name_String (Id : Valid_Name_Id) is |
| 922 |
begin |
| 923 |
Global_Name_Buffer.Length := 0; |
| 924 |
Append_Unqualified_Decoded (Global_Name_Buffer, Id); |
| 925 |
end Get_Unqualified_Decoded_Name_String; |
| 926 |
|
| 927 |
--------------------------------- |
| 928 |
-- Get_Unqualified_Name_String -- |
| 929 |
--------------------------------- |
| 930 |
|
| 931 |
procedure Get_Unqualified_Name_String (Id : Valid_Name_Id) is |
| 932 |
begin |
| 933 |
Global_Name_Buffer.Length := 0; |
| 934 |
Append_Unqualified (Global_Name_Buffer, Id); |
| 935 |
end Get_Unqualified_Name_String; |
| 936 |
|
| 937 |
---------- |
| 938 |
-- Hash -- |
| 939 |
---------- |
| 940 |
|
| 941 |
function Hash (Buf : Bounded_String) return Hash_Index_Type is |
| 942 |
|
| 943 |
-- This hash function looks at every character, in order to make it |
| 944 |
-- likely that similar strings get different hash values. The rotate by |
| 945 |
-- 7 bits has been determined empirically to be good, and it doesn't |
| 946 |
-- lose bits like a shift would. The final conversion can't overflow, |
| 947 |
-- because the table is 2**16 in size. This function probably needs to |
| 948 |
-- be changed if the hash table size is changed. |
| 949 |
|
| 950 |
-- Note that we could get some speed improvement by aligning the string |
| 951 |
-- to 32 or 64 bits, and doing word-wise xor's. We could also implement |
| 952 |
-- a growable table. It doesn't seem worth the trouble to do those |
| 953 |
-- things, for now. |
| 954 |
|
| 955 |
Result : Unsigned_16 := 0; |
| 956 |
|
| 957 |
begin |
| 958 |
for J in 1 .. Buf.Length loop |
| 959 |
Result := Rotate_Left (Result, 7) xor Character'Pos (Buf.Chars (J)); |
| 960 |
end loop; |
| 961 |
|
| 962 |
return Hash_Index_Type (Result); |
| 963 |
end Hash; |
| 964 |
|
| 965 |
---------------- |
| 966 |
-- Initialize -- |
| 967 |
---------------- |
| 968 |
|
| 969 |
procedure Initialize is |
| 970 |
begin |
| 971 |
null; |
| 972 |
end Initialize; |
| 973 |
|
| 974 |
---------------- |
| 975 |
-- Insert_Str -- |
| 976 |
---------------- |
| 977 |
|
| 978 |
procedure Insert_Str |
| 979 |
(Buf : in out Bounded_String; |
| 980 |
S : String; |
| 981 |
Index : Positive) |
| 982 |
is |
| 983 |
SL : constant Natural := S'Length; |
| 984 |
|
| 985 |
begin |
| 986 |
Buf.Chars (Index + SL .. Buf.Length + SL) := |
| 987 |
Buf.Chars (Index .. Buf.Length); |
| 988 |
Buf.Chars (Index .. Index + SL - 1) := S; |
| 989 |
Buf.Length := Buf.Length + SL; |
| 990 |
end Insert_Str; |
| 991 |
|
| 992 |
------------------------------- |
| 993 |
-- Insert_Str_In_Name_Buffer -- |
| 994 |
------------------------------- |
| 995 |
|
| 996 |
procedure Insert_Str_In_Name_Buffer (S : String; Index : Positive) is |
| 997 |
begin |
| 998 |
Insert_Str (Global_Name_Buffer, S, Index); |
| 999 |
end Insert_Str_In_Name_Buffer; |
| 1000 |
|
| 1001 |
---------------------- |
| 1002 |
-- Is_Internal_Name -- |
| 1003 |
---------------------- |
| 1004 |
|
| 1005 |
function Is_Internal_Name (Buf : Bounded_String) return Boolean is |
| 1006 |
J : Natural; |
| 1007 |
|
| 1008 |
begin |
| 1009 |
-- Any name starting or ending with underscore is internal |
| 1010 |
|
| 1011 |
if Buf.Chars (1) = '_' or else Buf.Chars (Buf.Length) = '_' then |
| 1012 |
return True; |
| 1013 |
|
| 1014 |
-- Allow quoted character |
| 1015 |
|
| 1016 |
elsif Buf.Chars (1) = ''' then |
| 1017 |
return False; |
| 1018 |
|
| 1019 |
-- All other cases, scan name |
| 1020 |
|
| 1021 |
else |
| 1022 |
-- Test backwards, because we only want to test the last entity |
| 1023 |
-- name if the name we have is qualified with other entities. |
| 1024 |
|
| 1025 |
J := Buf.Length; |
| 1026 |
while J /= 0 loop |
| 1027 |
|
| 1028 |
-- Skip stuff between brackets (A-F OK there) |
| 1029 |
|
| 1030 |
if Buf.Chars (J) = ']' then |
| 1031 |
loop |
| 1032 |
J := J - 1; |
| 1033 |
exit when J = 1 or else Buf.Chars (J) = '['; |
| 1034 |
end loop; |
| 1035 |
|
| 1036 |
-- Test for internal letter |
| 1037 |
|
| 1038 |
elsif Is_OK_Internal_Letter (Buf.Chars (J)) then |
| 1039 |
return True; |
| 1040 |
|
| 1041 |
-- Quit if we come to terminating double underscore (note that |
| 1042 |
-- if the current character is an underscore, we know that |
| 1043 |
-- there is a previous character present, since we already |
| 1044 |
-- filtered out the case of Buf.Chars (1) = '_' above. |
| 1045 |
|
| 1046 |
elsif Buf.Chars (J) = '_' |
| 1047 |
and then Buf.Chars (J - 1) = '_' |
| 1048 |
and then Buf.Chars (J - 2) /= '_' |
| 1049 |
then |
| 1050 |
return False; |
| 1051 |
end if; |
| 1052 |
|
| 1053 |
J := J - 1; |
| 1054 |
end loop; |
| 1055 |
end if; |
| 1056 |
|
| 1057 |
return False; |
| 1058 |
end Is_Internal_Name; |
| 1059 |
|
| 1060 |
function Is_Internal_Name (Id : Valid_Name_Id) return Boolean is |
| 1061 |
Buf : Bounded_String (Max_Length => Natural (Length_Of_Name (Id))); |
| 1062 |
begin |
| 1063 |
Append (Buf, Id); |
| 1064 |
return Is_Internal_Name (Buf); |
| 1065 |
end Is_Internal_Name; |
| 1066 |
|
| 1067 |
function Is_Internal_Name return Boolean is |
| 1068 |
begin |
| 1069 |
return Is_Internal_Name (Global_Name_Buffer); |
| 1070 |
end Is_Internal_Name; |
| 1071 |
|
| 1072 |
--------------------------- |
| 1073 |
-- Is_OK_Internal_Letter -- |
| 1074 |
--------------------------- |
| 1075 |
|
| 1076 |
function Is_OK_Internal_Letter (C : Character) return Boolean is |
| 1077 |
begin |
| 1078 |
return C in 'A' .. 'Z' and then C not in 'O' | 'Q' | 'U' | 'W' | 'X'; |
| 1079 |
end Is_OK_Internal_Letter; |
| 1080 |
|
| 1081 |
---------------------- |
| 1082 |
-- Is_Operator_Name -- |
| 1083 |
---------------------- |
| 1084 |
|
| 1085 |
function Is_Operator_Name (Id : Valid_Name_Id) return Boolean is |
| 1086 |
S : Int; |
| 1087 |
begin |
| 1088 |
pragma Assert (Is_Valid_Name (Id)); |
| 1089 |
S := Name_Entries.Table (Id).Name_Chars_Index; |
| 1090 |
return Name_Chars.Table (S + 1) = 'O'; |
| 1091 |
end Is_Operator_Name; |
| 1092 |
|
| 1093 |
------------------- |
| 1094 |
-- Is_Valid_Name -- |
| 1095 |
------------------- |
| 1096 |
|
| 1097 |
function Is_Valid_Name (Id : Name_Id) return Boolean is |
| 1098 |
begin |
| 1099 |
return Id in Name_Entries.First .. Name_Entries.Last; |
| 1100 |
end Is_Valid_Name; |
| 1101 |
|
| 1102 |
------------------ |
| 1103 |
-- Last_Name_Id -- |
| 1104 |
------------------ |
| 1105 |
|
| 1106 |
function Last_Name_Id return Name_Id is |
| 1107 |
begin |
| 1108 |
return Name_Id (Int (First_Name_Id) + Name_Entries_Count - 1); |
| 1109 |
end Last_Name_Id; |
| 1110 |
|
| 1111 |
-------------------- |
| 1112 |
-- Length_Of_Name -- |
| 1113 |
-------------------- |
| 1114 |
|
| 1115 |
function Length_Of_Name (Id : Valid_Name_Id) return Nat is |
| 1116 |
begin |
| 1117 |
return Int (Name_Entries.Table (Id).Name_Len); |
| 1118 |
end Length_Of_Name; |
| 1119 |
|
| 1120 |
---------- |
| 1121 |
-- Lock -- |
| 1122 |
---------- |
| 1123 |
|
| 1124 |
procedure Lock is |
| 1125 |
begin |
| 1126 |
Name_Chars.Set_Last (Name_Chars.Last + Name_Chars_Reserve); |
| 1127 |
Name_Entries.Set_Last (Name_Entries.Last + Name_Entries_Reserve); |
| 1128 |
Name_Chars.Release; |
| 1129 |
Name_Chars.Locked := True; |
| 1130 |
Name_Entries.Release; |
| 1131 |
Name_Entries.Locked := True; |
| 1132 |
end Lock; |
| 1133 |
|
| 1134 |
---------------- |
| 1135 |
-- Name_Enter -- |
| 1136 |
---------------- |
| 1137 |
|
| 1138 |
function Name_Enter |
| 1139 |
(Buf : Bounded_String := Global_Name_Buffer) return Valid_Name_Id |
| 1140 |
is |
| 1141 |
begin |
| 1142 |
Name_Entries.Append |
| 1143 |
((Name_Chars_Index => Name_Chars.Last, |
| 1144 |
Name_Len => Short (Buf.Length), |
| 1145 |
Byte_Info => 0, |
| 1146 |
Int_Info => 0, |
| 1147 |
Hash_Link => No_Name, |
| 1148 |
Name_Has_No_Encodings => False, |
| 1149 |
Boolean1_Info => False, |
| 1150 |
Boolean2_Info => False, |
| 1151 |
Boolean3_Info => False, |
| 1152 |
Spare => False)); |
| 1153 |
|
| 1154 |
-- Set corresponding string entry in the Name_Chars table |
| 1155 |
|
| 1156 |
for J in 1 .. Buf.Length loop |
| 1157 |
Name_Chars.Append (Buf.Chars (J)); |
| 1158 |
end loop; |
| 1159 |
|
| 1160 |
Name_Chars.Append (ASCII.NUL); |
| 1161 |
|
| 1162 |
return Name_Entries.Last; |
| 1163 |
end Name_Enter; |
| 1164 |
|
| 1165 |
function Name_Enter (S : String) return Valid_Name_Id is |
| 1166 |
Buf : Bounded_String (Max_Length => S'Length); |
| 1167 |
begin |
| 1168 |
Append (Buf, S); |
| 1169 |
return Name_Enter (Buf); |
| 1170 |
end Name_Enter; |
| 1171 |
|
| 1172 |
------------------------ |
| 1173 |
-- Name_Entries_Count -- |
| 1174 |
------------------------ |
| 1175 |
|
| 1176 |
function Name_Entries_Count return Nat is |
| 1177 |
begin |
| 1178 |
return Int (Name_Entries.Last - Name_Entries.First + 1); |
| 1179 |
end Name_Entries_Count; |
| 1180 |
|
| 1181 |
--------------- |
| 1182 |
-- Name_Find -- |
| 1183 |
--------------- |
| 1184 |
|
| 1185 |
function Name_Find |
| 1186 |
(Buf : Bounded_String := Global_Name_Buffer) return Valid_Name_Id |
| 1187 |
is |
| 1188 |
New_Id : Name_Id; |
| 1189 |
-- Id of entry in hash search, and value to be returned |
| 1190 |
|
| 1191 |
S : Int; |
| 1192 |
-- Pointer into string table |
| 1193 |
|
| 1194 |
Hash_Index : Hash_Index_Type; |
| 1195 |
-- Computed hash index |
| 1196 |
|
| 1197 |
Result : Valid_Name_Id; |
| 1198 |
|
| 1199 |
begin |
| 1200 |
-- Quick handling for one character names |
| 1201 |
|
| 1202 |
if Buf.Length = 1 then |
| 1203 |
Result := First_Name_Id + Character'Pos (Buf.Chars (1)); |
| 1204 |
|
| 1205 |
-- Otherwise search hash table for existing matching entry |
| 1206 |
|
| 1207 |
else |
| 1208 |
Hash_Index := Namet.Hash (Buf); |
| 1209 |
New_Id := Hash_Table (Hash_Index); |
| 1210 |
|
| 1211 |
if New_Id = No_Name then |
| 1212 |
Hash_Table (Hash_Index) := Name_Entries.Last + 1; |
| 1213 |
|
| 1214 |
else |
| 1215 |
Search : loop |
| 1216 |
if Buf.Length /= |
| 1217 |
Integer (Name_Entries.Table (New_Id).Name_Len) |
| 1218 |
then |
| 1219 |
goto No_Match; |
| 1220 |
end if; |
| 1221 |
|
| 1222 |
S := Name_Entries.Table (New_Id).Name_Chars_Index; |
| 1223 |
|
| 1224 |
for J in 1 .. Buf.Length loop |
| 1225 |
if Name_Chars.Table (S + Int (J)) /= Buf.Chars (J) then |
| 1226 |
goto No_Match; |
| 1227 |
end if; |
| 1228 |
end loop; |
| 1229 |
|
| 1230 |
Result := New_Id; |
| 1231 |
goto Done; |
| 1232 |
|
| 1233 |
-- Current entry in hash chain does not match |
| 1234 |
|
| 1235 |
<<No_Match>> |
| 1236 |
if Name_Entries.Table (New_Id).Hash_Link /= No_Name then |
| 1237 |
New_Id := Name_Entries.Table (New_Id).Hash_Link; |
| 1238 |
else |
| 1239 |
Name_Entries.Table (New_Id).Hash_Link := |
| 1240 |
Name_Entries.Last + 1; |
| 1241 |
exit Search; |
| 1242 |
end if; |
| 1243 |
end loop Search; |
| 1244 |
end if; |
| 1245 |
|
| 1246 |
-- We fall through here only if a matching entry was not found in the |
| 1247 |
-- hash table. We now create a new entry in the names table. The hash |
| 1248 |
-- link pointing to the new entry (Name_Entries.Last+1) has been set. |
| 1249 |
|
| 1250 |
Name_Entries.Append |
| 1251 |
((Name_Chars_Index => Name_Chars.Last, |
| 1252 |
Name_Len => Short (Buf.Length), |
| 1253 |
Hash_Link => No_Name, |
| 1254 |
Int_Info => 0, |
| 1255 |
Byte_Info => 0, |
| 1256 |
Name_Has_No_Encodings => False, |
| 1257 |
Boolean1_Info => False, |
| 1258 |
Boolean2_Info => False, |
| 1259 |
Boolean3_Info => False, |
| 1260 |
Spare => False)); |
| 1261 |
|
| 1262 |
-- Set corresponding string entry in the Name_Chars table |
| 1263 |
|
| 1264 |
for J in 1 .. Buf.Length loop |
| 1265 |
Name_Chars.Append (Buf.Chars (J)); |
| 1266 |
end loop; |
| 1267 |
|
| 1268 |
Name_Chars.Append (ASCII.NUL); |
| 1269 |
|
| 1270 |
Result := Name_Entries.Last; |
| 1271 |
end if; |
| 1272 |
|
| 1273 |
<<Done>> |
| 1274 |
return Result; |
| 1275 |
end Name_Find; |
| 1276 |
|
| 1277 |
function Name_Find (S : String) return Valid_Name_Id is |
| 1278 |
Buf : Bounded_String (Max_Length => S'Length); |
| 1279 |
begin |
| 1280 |
Append (Buf, S); |
| 1281 |
return Name_Find (Buf); |
| 1282 |
end Name_Find; |
| 1283 |
|
| 1284 |
----------------- |
| 1285 |
-- Name_Equals -- |
| 1286 |
----------------- |
| 1287 |
|
| 1288 |
function Name_Equals |
| 1289 |
(N1 : Valid_Name_Id; |
| 1290 |
N2 : Valid_Name_Id) return Boolean |
| 1291 |
is |
| 1292 |
begin |
| 1293 |
return N1 = N2 or else Get_Name_String (N1) = Get_Name_String (N2); |
| 1294 |
end Name_Equals; |
| 1295 |
|
| 1296 |
------------- |
| 1297 |
-- Present -- |
| 1298 |
------------- |
| 1299 |
|
| 1300 |
function Present (Nam : File_Name_Type) return Boolean is |
| 1301 |
begin |
| 1302 |
return Nam /= No_File; |
| 1303 |
end Present; |
| 1304 |
|
| 1305 |
------------- |
| 1306 |
-- Present -- |
| 1307 |
------------- |
| 1308 |
|
| 1309 |
function Present (Nam : Name_Id) return Boolean is |
| 1310 |
begin |
| 1311 |
return Nam /= No_Name; |
| 1312 |
end Present; |
| 1313 |
|
| 1314 |
------------- |
| 1315 |
-- Present -- |
| 1316 |
------------- |
| 1317 |
|
| 1318 |
function Present (Nam : Unit_Name_Type) return Boolean is |
| 1319 |
begin |
| 1320 |
return Nam /= No_Unit_Name; |
| 1321 |
end Present; |
| 1322 |
|
| 1323 |
------------------ |
| 1324 |
-- Reinitialize -- |
| 1325 |
------------------ |
| 1326 |
|
| 1327 |
procedure Reinitialize is |
| 1328 |
begin |
| 1329 |
Name_Chars.Init; |
| 1330 |
Name_Entries.Init; |
| 1331 |
|
| 1332 |
-- Initialize entries for one character names |
| 1333 |
|
| 1334 |
for C in Character loop |
| 1335 |
Name_Entries.Append |
| 1336 |
((Name_Chars_Index => Name_Chars.Last, |
| 1337 |
Name_Len => 1, |
| 1338 |
Byte_Info => 0, |
| 1339 |
Int_Info => 0, |
| 1340 |
Hash_Link => No_Name, |
| 1341 |
Name_Has_No_Encodings => True, |
| 1342 |
Boolean1_Info => False, |
| 1343 |
Boolean2_Info => False, |
| 1344 |
Boolean3_Info => False, |
| 1345 |
Spare => False)); |
| 1346 |
|
| 1347 |
Name_Chars.Append (C); |
| 1348 |
Name_Chars.Append (ASCII.NUL); |
| 1349 |
end loop; |
| 1350 |
|
| 1351 |
-- Clear hash table |
| 1352 |
|
| 1353 |
for J in Hash_Index_Type loop |
| 1354 |
Hash_Table (J) := No_Name; |
| 1355 |
end loop; |
| 1356 |
end Reinitialize; |
| 1357 |
|
| 1358 |
---------------------- |
| 1359 |
-- Reset_Name_Table -- |
| 1360 |
---------------------- |
| 1361 |
|
| 1362 |
procedure Reset_Name_Table is |
| 1363 |
begin |
| 1364 |
for J in First_Name_Id .. Name_Entries.Last loop |
| 1365 |
Name_Entries.Table (J).Int_Info := 0; |
| 1366 |
Name_Entries.Table (J).Byte_Info := 0; |
| 1367 |
end loop; |
| 1368 |
end Reset_Name_Table; |
| 1369 |
|
| 1370 |
-------------------------------- |
| 1371 |
-- Set_Character_Literal_Name -- |
| 1372 |
-------------------------------- |
| 1373 |
|
| 1374 |
procedure Set_Character_Literal_Name |
| 1375 |
(Buf : in out Bounded_String; |
| 1376 |
C : Char_Code) |
| 1377 |
is |
| 1378 |
begin |
| 1379 |
Buf.Length := 0; |
| 1380 |
Append (Buf, 'Q'); |
| 1381 |
Append_Encoded (Buf, C); |
| 1382 |
end Set_Character_Literal_Name; |
| 1383 |
|
| 1384 |
procedure Set_Character_Literal_Name (C : Char_Code) is |
| 1385 |
begin |
| 1386 |
Set_Character_Literal_Name (Global_Name_Buffer, C); |
| 1387 |
end Set_Character_Literal_Name; |
| 1388 |
|
| 1389 |
----------------------------- |
| 1390 |
-- Set_Name_Table_Boolean1 -- |
| 1391 |
----------------------------- |
| 1392 |
|
| 1393 |
procedure Set_Name_Table_Boolean1 (Id : Valid_Name_Id; Val : Boolean) is |
| 1394 |
begin |
| 1395 |
pragma Assert (Is_Valid_Name (Id)); |
| 1396 |
Name_Entries.Table (Id).Boolean1_Info := Val; |
| 1397 |
end Set_Name_Table_Boolean1; |
| 1398 |
|
| 1399 |
----------------------------- |
| 1400 |
-- Set_Name_Table_Boolean2 -- |
| 1401 |
----------------------------- |
| 1402 |
|
| 1403 |
procedure Set_Name_Table_Boolean2 (Id : Valid_Name_Id; Val : Boolean) is |
| 1404 |
begin |
| 1405 |
pragma Assert (Is_Valid_Name (Id)); |
| 1406 |
Name_Entries.Table (Id).Boolean2_Info := Val; |
| 1407 |
end Set_Name_Table_Boolean2; |
| 1408 |
|
| 1409 |
----------------------------- |
| 1410 |
-- Set_Name_Table_Boolean3 -- |
| 1411 |
----------------------------- |
| 1412 |
|
| 1413 |
procedure Set_Name_Table_Boolean3 (Id : Valid_Name_Id; Val : Boolean) is |
| 1414 |
begin |
| 1415 |
pragma Assert (Is_Valid_Name (Id)); |
| 1416 |
Name_Entries.Table (Id).Boolean3_Info := Val; |
| 1417 |
end Set_Name_Table_Boolean3; |
| 1418 |
|
| 1419 |
------------------------- |
| 1420 |
-- Set_Name_Table_Byte -- |
| 1421 |
------------------------- |
| 1422 |
|
| 1423 |
procedure Set_Name_Table_Byte (Id : Valid_Name_Id; Val : Byte) is |
| 1424 |
begin |
| 1425 |
pragma Assert (Is_Valid_Name (Id)); |
| 1426 |
Name_Entries.Table (Id).Byte_Info := Val; |
| 1427 |
end Set_Name_Table_Byte; |
| 1428 |
|
| 1429 |
------------------------- |
| 1430 |
-- Set_Name_Table_Int -- |
| 1431 |
------------------------- |
| 1432 |
|
| 1433 |
procedure Set_Name_Table_Int (Id : Valid_Name_Id; Val : Int) is |
| 1434 |
begin |
| 1435 |
pragma Assert (Is_Valid_Name (Id)); |
| 1436 |
Name_Entries.Table (Id).Int_Info := Val; |
| 1437 |
end Set_Name_Table_Int; |
| 1438 |
|
| 1439 |
----------------------------- |
| 1440 |
-- Store_Encoded_Character -- |
| 1441 |
----------------------------- |
| 1442 |
|
| 1443 |
procedure Store_Encoded_Character (C : Char_Code) is |
| 1444 |
begin |
| 1445 |
Append_Encoded (Global_Name_Buffer, C); |
| 1446 |
end Store_Encoded_Character; |
| 1447 |
|
| 1448 |
-------------------------------------- |
| 1449 |
-- Strip_Qualification_And_Suffixes -- |
| 1450 |
-------------------------------------- |
| 1451 |
|
| 1452 |
procedure Strip_Qualification_And_Suffixes (Buf : in out Bounded_String) is |
| 1453 |
J : Integer; |
| 1454 |
|
| 1455 |
begin |
| 1456 |
-- Strip package body qualification string off end |
| 1457 |
|
| 1458 |
for J in reverse 2 .. Buf.Length loop |
| 1459 |
if Buf.Chars (J) = 'X' then |
| 1460 |
Buf.Length := J - 1; |
| 1461 |
exit; |
| 1462 |
end if; |
| 1463 |
|
| 1464 |
exit when Buf.Chars (J) not in 'b' | 'n' | 'p'; |
| 1465 |
end loop; |
| 1466 |
|
| 1467 |
-- Find rightmost __ or $ separator if one exists. First we position |
| 1468 |
-- to start the search. If we have a character constant, position |
| 1469 |
-- just before it, otherwise position to last character but one |
| 1470 |
|
| 1471 |
if Buf.Chars (Buf.Length) = ''' then |
| 1472 |
J := Buf.Length - 2; |
| 1473 |
while J > 0 and then Buf.Chars (J) /= ''' loop |
| 1474 |
J := J - 1; |
| 1475 |
end loop; |
| 1476 |
|
| 1477 |
else |
| 1478 |
J := Buf.Length - 1; |
| 1479 |
end if; |
| 1480 |
|
| 1481 |
-- Loop to search for rightmost __ or $ (homonym) separator |
| 1482 |
|
| 1483 |
while J > 1 loop |
| 1484 |
|
| 1485 |
-- If $ separator, homonym separator, so strip it and keep looking |
| 1486 |
|
| 1487 |
if Buf.Chars (J) = '$' then |
| 1488 |
Buf.Length := J - 1; |
| 1489 |
J := Buf.Length - 1; |
| 1490 |
|
| 1491 |
-- Else check for __ found |
| 1492 |
|
| 1493 |
elsif Buf.Chars (J) = '_' and then Buf.Chars (J + 1) = '_' then |
| 1494 |
|
| 1495 |
-- Found __ so see if digit follows, and if so, this is a |
| 1496 |
-- homonym separator, so strip it and keep looking. |
| 1497 |
|
| 1498 |
if Buf.Chars (J + 2) in '0' .. '9' then |
| 1499 |
Buf.Length := J - 1; |
| 1500 |
J := Buf.Length - 1; |
| 1501 |
|
| 1502 |
-- If not a homonym separator, then we simply strip the |
| 1503 |
-- separator and everything that precedes it, and we are done |
| 1504 |
|
| 1505 |
else |
| 1506 |
Buf.Chars (1 .. Buf.Length - J - 1) := |
| 1507 |
Buf.Chars (J + 2 .. Buf.Length); |
| 1508 |
Buf.Length := Buf.Length - J - 1; |
| 1509 |
exit; |
| 1510 |
end if; |
| 1511 |
|
| 1512 |
else |
| 1513 |
J := J - 1; |
| 1514 |
end if; |
| 1515 |
end loop; |
| 1516 |
end Strip_Qualification_And_Suffixes; |
| 1517 |
|
| 1518 |
--------------- |
| 1519 |
-- To_String -- |
| 1520 |
--------------- |
| 1521 |
|
| 1522 |
function To_String (Buf : Bounded_String) return String is |
| 1523 |
begin |
| 1524 |
return Buf.Chars (1 .. Buf.Length); |
| 1525 |
end To_String; |
| 1526 |
|
| 1527 |
---------------------- |
| 1528 |
-- Unlock_If_Locked -- |
| 1529 |
---------------------- |
| 1530 |
|
| 1531 |
procedure Unlock_If_Locked is |
| 1532 |
begin |
| 1533 |
if Name_Chars.Locked then |
| 1534 |
Unlock; |
| 1535 |
end if; |
| 1536 |
end Unlock_If_Locked; |
| 1537 |
|
| 1538 |
------------ |
| 1539 |
-- Unlock -- |
| 1540 |
------------ |
| 1541 |
|
| 1542 |
procedure Unlock is |
| 1543 |
begin |
| 1544 |
Name_Chars.Locked := False; |
| 1545 |
Name_Chars.Set_Last (Name_Chars.Last - Name_Chars_Reserve); |
| 1546 |
Name_Chars.Release; |
| 1547 |
Name_Entries.Locked := False; |
| 1548 |
Name_Entries.Set_Last (Name_Entries.Last - Name_Entries_Reserve); |
| 1549 |
Name_Entries.Release; |
| 1550 |
end Unlock; |
| 1551 |
|
| 1552 |
-------- |
| 1553 |
-- wn -- |
| 1554 |
-------- |
| 1555 |
|
| 1556 |
procedure wn (Id : Name_Id) is |
| 1557 |
begin |
| 1558 |
Write_Name_For_Debug (Id); |
| 1559 |
Write_Eol; |
| 1560 |
end wn; |
| 1561 |
|
| 1562 |
---------------- |
| 1563 |
-- Write_Name -- |
| 1564 |
---------------- |
| 1565 |
|
| 1566 |
procedure Write_Name (Id : Valid_Name_Id) is |
| 1567 |
Buf : Bounded_String (Max_Length => Natural (Length_Of_Name (Id))); |
| 1568 |
begin |
| 1569 |
Append (Buf, Id); |
| 1570 |
Write_Str (Buf.Chars (1 .. Buf.Length)); |
| 1571 |
end Write_Name; |
| 1572 |
|
| 1573 |
------------------------ |
| 1574 |
-- Write_Name_Decoded -- |
| 1575 |
------------------------ |
| 1576 |
|
| 1577 |
procedure Write_Name_Decoded (Id : Valid_Name_Id) is |
| 1578 |
Buf : Bounded_String; |
| 1579 |
begin |
| 1580 |
Append_Decoded (Buf, Id); |
| 1581 |
Write_Str (Buf.Chars (1 .. Buf.Length)); |
| 1582 |
end Write_Name_Decoded; |
| 1583 |
|
| 1584 |
-------------------------- |
| 1585 |
-- Write_Name_For_Debug -- |
| 1586 |
-------------------------- |
| 1587 |
|
| 1588 |
procedure Write_Name_For_Debug (Id : Name_Id; Quote : String := "") is |
| 1589 |
begin |
| 1590 |
if Is_Valid_Name (Id) then |
| 1591 |
Write_Str (Quote); |
| 1592 |
|
| 1593 |
declare |
| 1594 |
Buf : Bounded_String (Max_Length => Natural (Length_Of_Name (Id))); |
| 1595 |
begin |
| 1596 |
Append (Buf, Id); |
| 1597 |
Write_Str (Buf.Chars (1 .. Buf.Length)); |
| 1598 |
end; |
| 1599 |
|
| 1600 |
Write_Str (Quote); |
| 1601 |
|
| 1602 |
elsif Id = No_Name then |
| 1603 |
Write_Str ("<No_Name>"); |
| 1604 |
|
| 1605 |
elsif Id = Error_Name then |
| 1606 |
Write_Str ("<Error_Name>"); |
| 1607 |
|
| 1608 |
else |
| 1609 |
Write_Str ("<invalid name "); |
| 1610 |
Write_Int (Int (Id)); |
| 1611 |
Write_Str (">"); |
| 1612 |
end if; |
| 1613 |
end Write_Name_For_Debug; |
| 1614 |
|
| 1615 |
-- Package initialization, initialize tables |
| 1616 |
|
| 1617 |
begin |
| 1618 |
Reinitialize; |
| 1619 |
end Namet; |