1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
36 with Hostparm; use Hostparm;
37 with System; use System;
38 with Types; use Types;
42 -- WARNING: There is a C version of this package. Any changes to this
43 -- source file must be properly reflected in the C header file namet.h
44 -- which is created manually from namet.ads and namet.adb.
46 -- This package contains routines for handling the names table. The table
47 -- is used to store character strings for identifiers and operator symbols,
48 -- as well as other string values such as unit names and file names.
50 -- The forms of the entries are as follows:
52 -- Identifiers Stored with upper case letters folded to lower case. Upper
53 -- half (16#80# bit set) and wide characters are stored
54 -- in an encoded form (Uhh for upper half char, Whhhh
55 -- for wide characters, WWhhhhhhhh as provided by the
56 -- routine Store_Encoded_Character, where hh are hex
57 -- digits for the character code using lower case a-f).
58 -- Normally the use of U or W in other internal names is
59 -- avoided, but these letters may be used in internal
60 -- names (without this special meaning), if they appear
61 -- as the last character of the name, or they are
62 -- followed by an upper case letter (other than the WW
63 -- sequence), or an underscore.
65 -- Operator symbols Stored with an initial letter O, and the remainder
66 -- of the name is the lower case characters XXX where
67 -- the name is Name_Op_XXX, see Snames spec for a full
68 -- list of the operator names. Normally the use of O
69 -- in other internal names is avoided, but it may be
70 -- used in internal names (without this special meaning)
71 -- if it is the last character of the name, or if it is
72 -- followed by an upper case letter or an underscore.
74 -- Character literals Character literals have names that are used only for
75 -- debugging and error message purposes. The form is a
76 -- upper case Q followed by a single lower case letter,
77 -- or by a Uxx/Wxxxx/WWxxxxxxx encoding as described for
78 -- identifiers. The Set_Character_Literal_Name procedure
79 -- should be used to construct these encodings. Normally
80 -- the use of O in other internal names is avoided, but
81 -- it may be used in internal names (without this special
82 -- meaning) if it is the last character of the name, or
83 -- if it is followed by an upper case letter or an
86 -- Unit names Stored with upper case letters folded to lower case,
87 -- using Uhh/Whhhh/WWhhhhhhhh encoding as described for
88 -- identifiers, and a %s or %b suffix for specs/bodies.
89 -- See package Uname for further details.
91 -- File names Are stored in the form provided by Osint. Typically
92 -- they may include wide character escape sequences and
93 -- upper case characters (in non-encoded form). Casing
94 -- is also derived from the external environment. Note
95 -- that file names provided by Osint must generally be
96 -- consistent with the names from Fname.Get_File_Name.
98 -- Other strings The names table is also used as a convenient storage
99 -- location for other variable length strings such as
100 -- error messages etc. There are no restrictions on what
101 -- characters may appear for such entries.
103 -- Note: the encodings Uhh (upper half characters), Whhhh (wide characters),
104 -- WWhhhhhhhh (wide wide characters) and Qx (character literal names) are
105 -- described in the spec, since they are visible throughout the system (e.g.
106 -- in debugging output). However, no code should depend on these particular
107 -- encodings, so it should be possible to change the encodings by making
108 -- changes only to the Namet specification (to change these comments) and the
109 -- body (which actually implements the encodings).
111 -- The names are hashed so that a given name appears only once in the table,
112 -- except that names entered with Name_Enter as opposed to Name_Find are
113 -- omitted from the hash table.
115 -- The first 26 entries in the names table (with Name_Id values in the range
116 -- First_Name_Id .. First_Name_Id + 25) represent names which are the one
117 -- character lower case letters in the range a-z, and these names are created
118 -- and initialized by the Initialize procedure.
120 -- Two values, one of type Int and one of type Byte, are stored with each
121 -- names table entry and subprograms are provided for setting and retrieving
122 -- these associated values. The usage of these values is up to the client. In
123 -- the compiler, the Int field is used to point to a chain of potentially
124 -- visible entities (see Sem.Ch8 for details), and the Byte field is used to
125 -- hold the Token_Type value for reserved words (see Sem for details). In the
126 -- binder, the Byte field is unused, and the Int field is used in various
127 -- ways depending on the name involved (see binder documentation).
129 Name_Buffer : String (1 .. 4 * Max_Line_Length);
130 -- This buffer is used to set the name to be stored in the table for the
131 -- Name_Find call, and to retrieve the name for the Get_Name_String call.
132 -- The limit here is intended to be an infinite value that ensures that we
133 -- never overflow the buffer (names this long are too absurd to worry!)
136 -- Length of name stored in Name_Buffer. Used as an input parameter for
137 -- Name_Find, and as an output value by Get_Name_String, or Write_Name.
139 -----------------------------
140 -- Types for Namet Package --
141 -----------------------------
143 -- Name_Id values are used to identify entries in the names table. Except
144 -- for the special values No_Name, and Error_Name, they are subscript
145 -- values for the Names table defined in package Namet.
147 -- Note that with only a few exceptions, which are clearly documented, the
148 -- type Name_Id should be regarded as a private type. In particular it is
149 -- never appropriate to perform arithmetic operations using this type.
151 type Name_Id is range Names_Low_Bound .. Names_High_Bound;
152 for Name_Id'Size use 32;
153 -- Type used to identify entries in the names table
155 No_Name : constant Name_Id := Names_Low_Bound;
156 -- The special Name_Id value No_Name is used in the parser to indicate
157 -- a situation where no name is present (e.g. on a loop or block).
159 Error_Name : constant Name_Id := Names_Low_Bound + 1;
160 -- The special Name_Id value Error_Name is used in the parser to
161 -- indicate that some kind of error was encountered in scanning out
162 -- the relevant name, so it does not have a representable label.
164 subtype Error_Name_Or_No_Name is Name_Id range No_Name .. Error_Name;
165 -- Used to test for either error name or no name
167 First_Name_Id : constant Name_Id := Names_Low_Bound + 2;
168 -- Subscript of first entry in names table
175 -- Called at the end of a use of the Namet package (before a subsequent
176 -- call to Initialize). Currently this routine is only used to generate
179 procedure Get_Name_String (Id : Name_Id);
180 -- Get_Name_String is used to retrieve the string associated with an entry
181 -- in the names table. The resulting string is stored in Name_Buffer and
182 -- Name_Len is set. It is an error to call Get_Name_String with one of the
183 -- special name Id values (No_Name or Error_Name).
185 function Get_Name_String (Id : Name_Id) return String;
186 -- This functional form returns the result as a string without affecting
187 -- the contents of either Name_Buffer or Name_Len. The lower bound is 1.
189 procedure Get_Unqualified_Name_String (Id : Name_Id);
190 -- Similar to the above except that qualification (as defined in unit
191 -- Exp_Dbug) is removed (including both preceding __ delimited names, and
192 -- also the suffixes used to indicate package body entities and to
193 -- distinguish between overloaded entities). Note that names are not
194 -- qualified until just before the call to gigi, so this routine is only
195 -- needed by processing that occurs after gigi has been called. This
196 -- includes all ASIS processing, since ASIS works on the tree written
197 -- after gigi has been called.
199 procedure Get_Name_String_And_Append (Id : Name_Id);
200 -- Like Get_Name_String but the resulting characters are appended to the
201 -- current contents of the entry stored in Name_Buffer, and Name_Len is
202 -- incremented to include the added characters.
204 procedure Get_Decoded_Name_String (Id : Name_Id);
205 -- Same calling sequence an interface as Get_Name_String, except that the
206 -- result is decoded, so that upper half characters and wide characters
207 -- appear as originally found in the source program text, operators have
208 -- their source forms (special characters and enclosed in quotes), and
209 -- character literals appear surrounded by apostrophes.
211 procedure Get_Unqualified_Decoded_Name_String (Id : Name_Id);
212 -- Similar to the above except that qualification (as defined in unit
213 -- Exp_Dbug) is removed (including both preceding __ delimited names, and
214 -- also the suffix used to indicate package body entities). Note that
215 -- names are not qualified until just before the call to gigi, so this
216 -- routine is only needed by processing that occurs after gigi has been
217 -- called. This includes all ASIS processing, since ASIS works on the tree
218 -- written after gigi has been called.
220 procedure Get_Decoded_Name_String_With_Brackets (Id : Name_Id);
221 -- This routine is similar to Decoded_Name, except that the brackets
222 -- notation (Uhh replaced by ["hh"], Whhhh replaced by ["hhhh"],
223 -- WWhhhhhhhh replaced by ["hhhhhhhh"]) is used for all non-lower half
224 -- characters, regardless of how Opt.Wide_Character_Encoding_Method is
225 -- set, and also in that characters in the range 16#80# .. 16#FF# are
226 -- converted to brackets notation in all cases. This routine can be used
227 -- when there is a requirement for a canonical representation not affected
228 -- by the character set options (e.g. in the binder generation of
231 function Get_Name_Table_Byte (Id : Name_Id) return Byte;
232 pragma Inline (Get_Name_Table_Byte);
233 -- Fetches the Byte value associated with the given name
235 function Get_Name_Table_Info (Id : Name_Id) return Int;
236 pragma Inline (Get_Name_Table_Info);
237 -- Fetches the Int value associated with the given name
239 function Is_Operator_Name (Id : Name_Id) return Boolean;
240 -- Returns True if name given is of the form of an operator (that
241 -- is, it starts with an upper case O).
243 procedure Initialize;
244 -- Initializes the names table, including initializing the first 26
245 -- entries in the table (for the 1-character lower case names a-z) Note
246 -- that Initialize must not be called if Tree_Read is used.
249 -- Lock name tables before calling back end. We reserve some extra space
250 -- before locking to avoid unnecessary inefficiencies when we unlock.
253 -- Unlocks the name table to allow use of the extra space reserved by the
254 -- call to Lock. See gnat1drv for details of the need for this.
256 function Length_Of_Name (Id : Name_Id) return Nat;
257 pragma Inline (Length_Of_Name);
258 -- Returns length of given name in characters. This is the length of the
259 -- encoded name, as stored in the names table, the result is equivalent to
260 -- calling Get_Name_String and reading Name_Len, except that a call to
261 -- Length_Of_Name does not affect the contents of Name_Len and Name_Buffer.
263 function Name_Chars_Address return System.Address;
264 -- Return starting address of name characters table (used in Back_End call
267 function Name_Find return Name_Id;
268 -- Name_Find is called with a string stored in Name_Buffer whose length is
269 -- in Name_Len (i.e. the characters of the name are in subscript positions
270 -- 1 to Name_Len in Name_Buffer). It searches the names table to see if
271 -- the string has already been stored. If so the Id of the existing entry
272 -- is returned. Otherwise a new entry is created with its Name_Table_Info
273 -- field set to zero. The contents of Name_Buffer and Name_Len are not
274 -- modified by this call. Note that it is permissible for Name_Len to be
275 -- set to zero to lookup the null name string.
277 function Name_Enter return Name_Id;
278 -- Name_Enter has the same calling interface as Name_Find. The difference
279 -- is that it does not search the table for an existing match, and also
280 -- subsequent Name_Find calls using the same name will not locate the
281 -- entry created by this call. Thus multiple calls to Name_Enter with the
282 -- same name will create multiple entries in the name table with different
283 -- Name_Id values. This is useful in the case of created names, which are
284 -- never expected to be looked up. Note: Name_Enter should never be used
285 -- for one character names, since these are efficiently located without
286 -- hashing by Name_Find in any case.
288 function Name_Entries_Address return System.Address;
289 -- Return starting address of Names table (used in Back_End call to Gigi)
291 function Name_Entries_Count return Nat;
292 -- Return current number of entries in the names table
294 function Is_OK_Internal_Letter (C : Character) return Boolean;
295 pragma Inline (Is_OK_Internal_Letter);
296 -- Returns true if C is a suitable character for using as a prefix or a
297 -- suffix of an internally generated name, i.e. it is an upper case letter
298 -- other than one of the ones used for encoding source names (currently
299 -- the set of reserved letters is O, Q, U, W) and also returns False for
300 -- the letter X, which is reserved for debug output (see Exp_Dbug).
302 function Is_Internal_Name (Id : Name_Id) return Boolean;
303 -- Returns True if the name is an internal name (i.e. contains a character
304 -- for which Is_OK_Internal_Letter is true, or if the name starts or ends
305 -- with an underscore. This call destroys the value of Name_Len and
306 -- Name_Buffer (it loads these as for Get_Name_String).
308 -- Note: if the name is qualified (has a double underscore), then only the
309 -- final entity name is considered, not the qualifying names. Consider for
310 -- example that the name:
314 -- is not an internal name, because the B comes from the internal name of
315 -- a qualifying block, but the xyz means that this was indeed a declared
316 -- identifier called "xyz" within this block and there is nothing internal
319 function Is_Internal_Name return Boolean;
320 -- Like the form with an Id argument, except that the name to be tested is
321 -- passed in Name_Buffer and Name_Len (which are not affected by the call).
322 -- Name_Buffer (it loads these as for Get_Name_String).
324 function Is_Valid_Name (Id : Name_Id) return Boolean;
325 -- True if Id is a valid name -- points to a valid entry in the
326 -- Name_Entries table.
328 procedure Reset_Name_Table;
329 -- This procedure is used when there are multiple source files to reset
330 -- the name table info entries associated with current entries in the
331 -- names table. There is no harm in keeping the names entries themselves
332 -- from one compilation to another, but we can't keep the entity info,
333 -- since this refers to tree nodes, which are destroyed between each main
336 procedure Add_Char_To_Name_Buffer (C : Character);
337 pragma Inline (Add_Char_To_Name_Buffer);
338 -- Add given character to the end of the string currently stored in the
339 -- Name_Buffer, incrementing Name_Len.
341 procedure Add_Nat_To_Name_Buffer (V : Nat);
342 -- Add decimal representation of given value to the end of the string
343 -- currently stored in Name_Buffer, incrementing Name_Len as required.
345 procedure Add_Str_To_Name_Buffer (S : String);
346 -- Add characters of string S to the end of the string currently stored
347 -- in the Name_Buffer, incrementing Name_Len by the length of the string.
349 procedure Set_Character_Literal_Name (C : Char_Code);
350 -- This procedure sets the proper encoded name for the character literal
351 -- for the given character code. On return Name_Buffer and Name_Len are
352 -- set to reflect the stored name.
354 procedure Set_Name_Table_Info (Id : Name_Id; Val : Int);
355 pragma Inline (Set_Name_Table_Info);
356 -- Sets the Int value associated with the given name
358 procedure Set_Name_Table_Byte (Id : Name_Id; Val : Byte);
359 pragma Inline (Set_Name_Table_Byte);
360 -- Sets the Byte value associated with the given name
362 procedure Store_Encoded_Character (C : Char_Code);
363 -- Stores given character code at the end of Name_Buffer, updating the
364 -- value in Name_Len appropriately. Lower case letters and digits are
365 -- stored unchanged. Other 8-bit characters are stored using the Uhh
366 -- encoding (hh = hex code), other 16-bit wide character values are stored
367 -- using the Whhhh (hhhh = hex code) encoding, and other 32-bit wide wide
368 -- character values are stored using the WWhhhhhhhh (hhhhhhhh = hex code).
369 -- Note that this procedure does not fold upper case letters (they are
370 -- stored using the Uhh encoding). If folding is required, it must be done
371 -- by the caller prior to the call.
374 -- Initializes internal tables from current tree file using the relevant
375 -- Table.Tree_Read routines. Note that Initialize should not be called if
376 -- Tree_Read is used. Tree_Read includes all necessary initialization.
378 procedure Tree_Write;
379 -- Writes out internal tables to current tree file using the relevant
380 -- Table.Tree_Write routines.
382 procedure Get_Last_Two_Chars (N : Name_Id; C1, C2 : out Character);
383 -- Obtains last two characters of a name. C1 is last but one character
384 -- and C2 is last character. If name is less than two characters long,
385 -- then both C1 and C2 are set to ASCII.NUL on return.
387 procedure Write_Name (Id : Name_Id);
388 -- Write_Name writes the characters of the specified name using the
389 -- standard output procedures in package Output. No end of line is
390 -- written, just the characters of the name. On return Name_Buffer and
391 -- Name_Len are set as for a call to Get_Name_String. The name is written
392 -- in encoded form (i.e. including Uhh, Whhh, Qx, _op as they appear in
393 -- the name table). If Id is Error_Name, or No_Name, no text is output.
395 procedure Write_Name_Decoded (Id : Name_Id);
396 -- Like Write_Name, except that the name written is the decoded name, as
397 -- described for Get_Decoded_Name_String, and the resulting value stored
398 -- in Name_Len and Name_Buffer is the decoded name.
400 ------------------------------
401 -- File and Unit Name Types --
402 ------------------------------
404 -- These are defined here in Namet rather than Fname and Uname to avoid
405 -- problems with dependencies, and to avoid dragging in Fname and Uname
406 -- into many more files, but it would be cleaner to move to Fname/Uname.
408 type File_Name_Type is new Name_Id;
409 -- File names are stored in the names table and this type is used to
410 -- indicate that a Name_Id value is being used to hold a simple file name
411 -- (which does not include any directory information).
413 No_File : constant File_Name_Type := File_Name_Type (No_Name);
414 -- Constant used to indicate no file is present (this is used for example
415 -- when a search for a file indicates that no file of the name exists).
417 Error_File_Name : constant File_Name_Type := File_Name_Type (Error_Name);
418 -- The special File_Name_Type value Error_File_Name is used to indicate
419 -- a unit name where some previous processing has found an error.
421 subtype Error_File_Name_Or_No_File is
422 File_Name_Type range No_File .. Error_File_Name;
423 -- Used to test for either error file name or no file
425 type Path_Name_Type is new Name_Id;
426 -- Path names are stored in the names table and this type is used to
427 -- indicate that a Name_Id value is being used to hold a path name (that
428 -- may contain directory information).
430 No_Path : constant Path_Name_Type := Path_Name_Type (No_Name);
431 -- Constant used to indicate no path name is present
433 type Unit_Name_Type is new Name_Id;
434 -- Unit names are stored in the names table and this type is used to
435 -- indicate that a Name_Id value is being used to hold a unit name, which
436 -- terminates in %b for a body or %s for a spec.
438 No_Unit_Name : constant Unit_Name_Type := Unit_Name_Type (No_Name);
439 -- Constant used to indicate no file name present
441 Error_Unit_Name : constant Unit_Name_Type := Unit_Name_Type (Error_Name);
442 -- The special Unit_Name_Type value Error_Unit_Name is used to indicate
443 -- a unit name where some previous processing has found an error.
445 subtype Error_Unit_Name_Or_No_Unit_Name is
446 Unit_Name_Type range No_Unit_Name .. Error_Unit_Name;
448 ------------------------
449 -- Debugging Routines --
450 ------------------------
452 procedure wn (Id : Name_Id);
453 pragma Export (Ada, wn);
454 -- This routine is intended for debugging use only (i.e. it is intended to
455 -- be called from the debugger). It writes the characters of the specified
456 -- name using the standard output procedures in package Output, followed by
457 -- a new line. The name is written in encoded form (i.e. including Uhh,
458 -- Whhh, Qx, _op as they appear in the name table). If Id is Error_Name,
459 -- No_Name, or invalid an appropriate string is written (<Error_Name>,
460 -- <No_Name>, <invalid name>). Unlike Write_Name, this call does not affect
461 -- the contents of Name_Buffer or Name_Len.
463 ---------------------------
464 -- Table Data Structures --
465 ---------------------------
467 -- The following declarations define the data structures used to store
468 -- names. The definitions are in the private part of the package spec,
469 -- rather than the body, since they are referenced directly by gigi.
473 -- This table stores the actual string names. Although logically there is
474 -- no need for a terminating character (since the length is stored in the
475 -- name entry table), we still store a NUL character at the end of every
476 -- name (for convenience in interfacing to the C world).
478 package Name_Chars is new Table.Table (
479 Table_Component_Type => Character,
480 Table_Index_Type => Int,
481 Table_Low_Bound => 0,
482 Table_Initial => Alloc.Name_Chars_Initial,
483 Table_Increment => Alloc.Name_Chars_Increment,
484 Table_Name => "Name_Chars");
486 type Name_Entry is record
487 Name_Chars_Index : Int;
488 -- Starting location of characters in the Name_Chars table minus one
489 -- (i.e. pointer to character just before first character). The reason
490 -- for the bias of one is that indexes in Name_Buffer are one's origin,
491 -- so this avoids unnecessary adds and subtracts of 1.
494 -- Length of this name in characters
497 -- Byte value associated with this name
499 Name_Has_No_Encodings : Boolean;
500 -- This flag is set True if the name entry is known not to contain any
501 -- special character encodings. This is used to speed up repeated calls
502 -- to Get_Decoded_Name_String. A value of False means that it is not
503 -- known whether the name contains any such encodings.
506 -- Link to next entry in names table for same hash code
509 -- Int Value associated with this name
512 for Name_Entry use record
513 Name_Chars_Index at 0 range 0 .. 31;
514 Name_Len at 4 range 0 .. 15;
515 Byte_Info at 6 range 0 .. 7;
516 Name_Has_No_Encodings at 7 range 0 .. 7;
517 Hash_Link at 8 range 0 .. 31;
518 Int_Info at 12 range 0 .. 31;
521 for Name_Entry'Size use 16 * 8;
522 -- This ensures that we did not leave out any fields
524 -- This is the table that is referenced by Name_Id entries.
525 -- It contains one entry for each unique name in the table.
527 package Name_Entries is new Table.Table (
528 Table_Component_Type => Name_Entry,
529 Table_Index_Type => Name_Id'Base,
530 Table_Low_Bound => First_Name_Id,
531 Table_Initial => Alloc.Names_Initial,
532 Table_Increment => Alloc.Names_Increment,
533 Table_Name => "Name_Entries");