OSDN Git Service

2005-02-09 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / namet.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                N A M E T                                 --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005 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 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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
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.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with Alloc;
35 with Table;
36 with System;   use System;
37 with Types;    use Types;
38
39 package Namet is
40
41 --  WARNING: There is a C version of this package. Any changes to this
42 --  source file must be properly reflected in the C header file namet.h
43 --  which is created manually from namet.ads and namet.adb.
44
45 --  This package contains routines for handling the names table. The table
46 --  is used to store character strings for identifiers and operator symbols,
47 --  as well as other string values such as unit names and file names.
48
49 --  The forms of the entries are as follows:
50
51 --    Identifiers Stored with upper case letters folded to lower case. Upper
52 --                       half (16#80# bit set) and wide characters are stored
53 --                       in an encoded form (Uhh for upper half char, Whhhh
54 --                       for wide characters, WWhhhhhhhh as provided by the
55 --                       routine Store_Encoded_Character, where hh are hex
56 --                       digits for the character code using lower case a-f).
57 --                       Normally the use of U or W in other internal names is
58 --                       avoided, but these letters may be used in internal
59 --                       names (without this special meaning), if they appear
60 --                       as the last character of the name, or they are
61 --                       followed by an upper case letter (other than the WW
62 --                       sequence), or an underscore.
63
64
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.
73
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
84 --                       underscore.
85
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.
90
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.
97
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.
102
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).
110
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.
114
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.
119
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).
128
129    Name_Buffer : String (1 .. 16*1024);
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 plus 1 in the length allows for cases of adding ASCII.NUL. The 16K
133    --  here is intended to be an infinite value that ensures that we never
134    --  overflow the buffer (names this long are too absurd to worry!)
135
136    Name_Len : Natural;
137    --  Length of name stored in Name_Buffer. Used as an input parameter for
138    --  Name_Find, and as an output value by Get_Name_String, or Write_Name.
139
140    -----------------
141    -- Subprograms --
142    -----------------
143
144    procedure Finalize;
145    --  Called at the end of a use of the Namet package (before a subsequent
146    --  call to Initialize). Currently this routine is only used to generate
147    --  debugging output.
148
149    procedure Get_Name_String (Id : Name_Id);
150    --  Get_Name_String is used to retrieve the string associated with an entry
151    --  in the names table. The resulting string is stored in Name_Buffer and
152    --  Name_Len is set. It is an error to call Get_Name_String with one of the
153    --  special name Id values (No_Name or Error_Name).
154
155    function Get_Name_String (Id : Name_Id) return String;
156    --  This functional form returns the result as a string without affecting
157    --  the contents of either Name_Buffer or Name_Len.
158
159    procedure Get_Unqualified_Name_String (Id : Name_Id);
160    --  Similar to the above except that qualification (as defined in unit
161    --  Exp_Dbug) is removed (including both preceding __ delimited names, and
162    --  also the suffixes used to indicate package body entities and to
163    --  distinguish between overloaded entities). Note that names are not
164    --  qualified until just before the call to gigi, so this routine is only
165    --  needed by processing that occurs after gigi has been called. This
166    --  includes all ASIS processing, since ASIS works on the tree written
167    --  after gigi has been called.
168
169    procedure Get_Name_String_And_Append (Id : Name_Id);
170    --  Like Get_Name_String but the resulting characters are appended to the
171    --  current contents of the entry stored in Name_Buffer, and Name_Len is
172    --  incremented to include the added characters.
173
174    procedure Get_Decoded_Name_String (Id : Name_Id);
175    --  Same calling sequence an interface as Get_Name_String, except that the
176    --  result is decoded, so that upper half characters and wide characters
177    --  appear as originally found in the source program text, operators have
178    --  their source forms (special characters and enclosed in quotes), and
179    --  character literals appear surrounded by apostrophes.
180
181    procedure Get_Unqualified_Decoded_Name_String (Id : Name_Id);
182    --  Similar to the above except that qualification (as defined in unit
183    --  Exp_Dbug) is removed (including both preceding __ delimited names, and
184    --  also the suffix used to indicate package body entities). Note that
185    --  names are not qualified until just before the call to gigi, so this
186    --  routine is only needed by processing that occurs after gigi has been
187    --  called. This includes all ASIS processing, since ASIS works on the tree
188    --  written after gigi has been called.
189
190    procedure Get_Decoded_Name_String_With_Brackets (Id : Name_Id);
191    --  This routine is similar to Decoded_Name, except that the brackets
192    --  notation (Uhh replaced by ["hh"], Whhhh replaced by ["hhhh"],
193    --  WWhhhhhhhh replaced by ["hhhhhhhh"]) is used for all non-lower half
194    --  characters, regardless of how Opt.Wide_Character_Encoding_Method is
195    --  set, and also in that characters in the range 16#80# .. 16#FF# are
196    --  converted to brackets notation in all cases. This routine can be used
197    --  when there is a requirement for a canonical representation not affected
198    --  by the character set options (e.g. in the binder generation of
199    --  symbols).
200
201    function Get_Name_Table_Byte (Id : Name_Id) return Byte;
202    pragma Inline (Get_Name_Table_Byte);
203    --  Fetches the Byte value associated with the given name
204
205    function Get_Name_Table_Info (Id : Name_Id) return Int;
206    pragma Inline (Get_Name_Table_Info);
207    --  Fetches the Int value associated with the given name
208
209    function Is_Operator_Name (Id : Name_Id) return Boolean;
210    --  Returns True if name given is of the form of an operator (that
211    --  is, it starts with an upper case O).
212
213    procedure Initialize;
214    --  Initializes the names table, including initializing the first 26
215    --  entries in the table (for the 1-character lower case names a-z) Note
216    --  that Initialize must not be called if Tree_Read is used.
217
218    procedure Lock;
219    --  Lock name table before calling back end. Space for up to 10 extra
220    --  names and 1000 extra characters is reserved before the table is locked.
221
222    procedure Unlock;
223    --  Unlocks the name table to allow use of the 10 extra names and 1000
224    --  extra characters reserved by the Lock call. See gnat1drv for details of
225    --  the need for this.
226
227    function Length_Of_Name (Id : Name_Id) return Nat;
228    pragma Inline (Length_Of_Name);
229    --  Returns length of given name in characters. This is the length of the
230    --  encoded name, as stored in the names table, the result is equivalent to
231    --  calling Get_Name_String and reading Name_Len, except that a call to
232    --  Length_Of_Name does not affect the contents of Name_Len and Name_Buffer.
233
234    function Name_Chars_Address return System.Address;
235    --  Return starting address of name characters table (used in Back_End call
236    --  to Gigi).
237
238    function Name_Find return Name_Id;
239    --  Name_Find is called with a string stored in Name_Buffer whose length is
240    --  in Name_Len (i.e. the characters of the name are in subscript positions
241    --  1 to Name_Len in Name_Buffer). It searches the names table to see if
242    --  the string has already been stored. If so the Id of the existing entry
243    --  is returned. Otherwise a new entry is created with its Name_Table_Info
244    --  field set to zero. The contents of Name_Buffer and Name_Len are not
245    --  modified by this call. Note that it is permissible for Name_Len to be
246    --  set to zero to lookup the null name string.
247
248    function Name_Enter return Name_Id;
249    --  Name_Enter has the same calling interface as Name_Find. The difference
250    --  is that it does not search the table for an existing match, and also
251    --  subsequent Name_Find calls using the same name will not locate the
252    --  entry created by this call. Thus multiple calls to Name_Enter with the
253    --  same name will create multiple entries in the name table with different
254    --  Name_Id values. This is useful in the case of created names, which are
255    --  never expected to be looked up. Note: Name_Enter should never be used
256    --  for one character names, since these are efficiently located without
257    --  hashing by Name_Find in any case.
258
259    function Name_Entries_Address return System.Address;
260    --  Return starting address of Names table (used in Back_End call to Gigi)
261
262    function Name_Entries_Count return Nat;
263    --  Return current number of entries in the names table
264
265    function Is_OK_Internal_Letter (C : Character) return Boolean;
266    pragma Inline (Is_OK_Internal_Letter);
267    --  Returns true if C is a suitable character for using as a prefix or a
268    --  suffix of an internally generated name, i.e. it is an upper case letter
269    --  other than one of the ones used for encoding source names (currently
270    --  the set of reserved letters is O, Q, U, W) and also returns False for
271    --  the letter X, which is reserved for debug output (see Exp_Dbug).
272
273    function Is_Internal_Name (Id : Name_Id) return Boolean;
274    --  Returns True if the name is an internal name (i.e. contains a character
275    --  for which Is_OK_Internal_Letter is true, or if the name starts or ends
276    --  with an underscore. This call destroys the value of Name_Len and
277    --  Name_Buffer (it loads these as for Get_Name_String).
278    --
279    --  Note: if the name is qualified (has a double underscore), then only the
280    --  final entity name is considered, not the qualifying names. Consider for
281    --  example that the name:
282    --
283    --    pkg__B_1__xyz
284    --
285    --  is not an internal name, because the B comes from the internal name of
286    --  a qualifying block, but the xyz means that this was indeed a declared
287    --  identifier called "xyz" within this block and there is nothing internal
288    --  about that name.
289
290    function Is_Internal_Name return Boolean;
291    --  Like the form with an Id argument, except that the name to be tested is
292    --  passed in Name_Buffer and Name_Len (which are not affected by the call).
293    --  Name_Buffer (it loads these as for Get_Name_String).
294
295    procedure Reset_Name_Table;
296    --  This procedure is used when there are multiple source files to reset
297    --  the name table info entries associated with current entries in the
298    --  names table. There is no harm in keeping the names entries themselves
299    --  from one compilation to another, but we can't keep the entity info,
300    --  since this refers to tree nodes, which are destroyed between each main
301    --  source file.
302
303    procedure Add_Char_To_Name_Buffer (C : Character);
304    pragma Inline (Add_Char_To_Name_Buffer);
305    --  Add given character to the end of the string currently stored in the
306    --  Name_Buffer, incrementing Name_Len.
307
308    procedure Add_Nat_To_Name_Buffer (V : Nat);
309    --  Add decimal representation of given value to the end of the string
310    --  currently stored in Name_Buffer, incrementing Name_Len as required.
311
312    procedure Add_Str_To_Name_Buffer (S : String);
313    --  Add characters of string S to the end of the string currently stored
314    --  in the Name_Buffer, incrementing Name_Len by the length of the string.
315
316    procedure Set_Character_Literal_Name (C : Char_Code);
317    --  This procedure sets the proper encoded name for the character literal
318    --  for the given character code. On return Name_Buffer and Name_Len are
319    --  set to reflect the stored name.
320
321    procedure Set_Name_Table_Info (Id : Name_Id; Val : Int);
322    pragma Inline (Set_Name_Table_Info);
323    --  Sets the Int value associated with the given name
324
325    procedure Set_Name_Table_Byte (Id : Name_Id; Val : Byte);
326    pragma Inline (Set_Name_Table_Byte);
327    --  Sets the Byte value associated with the given name
328
329    procedure Store_Encoded_Character (C : Char_Code);
330    --  Stores given character code at the end of Name_Buffer, updating the
331    --  value in Name_Len appropriately. Lower case letters and digits are
332    --  stored unchanged. Other 8-bit characters are stored using the Uhh
333    --  encoding (hh = hex code), other 16-bit wide character values are stored
334    --  using the Whhhh (hhhh = hex code) encoding, and other 32-bit wide wide
335    --  character values are stored using the WWhhhhhhhh (hhhhhhhh = hex code).
336    --  Note that this procedure does not fold upper case letters (they are
337    --  stored using the Uhh encoding). If folding is required, it must be done
338    --  by the caller prior to the call.
339
340    procedure Tree_Read;
341    --  Initializes internal tables from current tree file using the relevant
342    --  Table.Tree_Read routines. Note that Initialize should not be called if
343    --  Tree_Read is used. Tree_Read includes all necessary initialization.
344
345    procedure Tree_Write;
346    --  Writes out internal tables to current tree file using the relevant
347    --  Table.Tree_Write routines.
348
349    procedure Get_Last_Two_Chars (N : Name_Id; C1, C2 : out Character);
350    --  Obtains last two characters of a name. C1 is last but one character
351    --  and C2 is last character. If name is less than two characters long,
352    --  then both C1 and C2 are set to ASCII.NUL on return.
353
354    procedure Write_Name (Id : Name_Id);
355    --  Write_Name writes the characters of the specified name using the
356    --  standard output procedures in package Output. No end of line is
357    --  written, just the characters of the name. On return Name_Buffer and
358    --  Name_Len are set as for a call to Get_Name_String. The name is written
359    --  in encoded form (i.e. including Uhh, Whhh, Qx, _op as they appear in
360    --  the name table). If Id is Error_Name, or No_Name, no text is output.
361
362    procedure wn (Id : Name_Id);
363    pragma Export (Ada, wn);
364    --  Like Write_Name, but includes new line at end. Intended for use
365    --  from the debugger only.
366
367    procedure Write_Name_Decoded (Id : Name_Id);
368    --  Like Write_Name, except that the name written is the decoded name, as
369    --  described for Get_Decoded_Name_String, and the resulting value stored
370    --  in Name_Len and Name_Buffer is the decoded name.
371
372    ---------------------------
373    -- Table Data Structures --
374    ---------------------------
375
376    --  The following declarations define the data structures used to store
377    --  names. The definitions are in the private part of the package spec,
378    --  rather than the body, since they are referenced directly by gigi.
379
380 private
381
382    --  This table stores the actual string names. Although logically there is
383    --  no need for a terminating character (since the length is stored in the
384    --  name entry table), we still store a NUL character at the end of every
385    --  name (for convenience in interfacing to the C world).
386
387    package Name_Chars is new Table.Table (
388      Table_Component_Type => Character,
389      Table_Index_Type     => Int,
390      Table_Low_Bound      => 0,
391      Table_Initial        => Alloc.Name_Chars_Initial,
392      Table_Increment      => Alloc.Name_Chars_Increment,
393      Table_Name           => "Name_Chars");
394
395    type Name_Entry is record
396       Name_Chars_Index : Int;
397       --  Starting location of characters in the Name_Chars table minus one
398       --  (i.e. pointer to character just before first character). The reason
399       --  for the bias of one is that indexes in Name_Buffer are one's origin,
400       --  so this avoids unnecessary adds and subtracts of 1.
401
402       Name_Len : Short;
403       --  Length of this name in characters
404
405       Byte_Info : Byte;
406       --  Byte value associated with this name
407
408       Hash_Link : Name_Id;
409       --  Link to next entry in names table for same hash code
410
411       Int_Info : Int;
412       --  Int Value associated with this name
413    end record;
414
415    --  This is the table that is referenced by Name_Id entries.
416    --  It contains one entry for each unique name in the table.
417
418    package Name_Entries is new Table.Table (
419      Table_Component_Type => Name_Entry,
420      Table_Index_Type     => Name_Id,
421      Table_Low_Bound      => First_Name_Id,
422      Table_Initial        => Alloc.Names_Initial,
423      Table_Increment      => Alloc.Names_Increment,
424      Table_Name           => "Name_Entries");
425
426 end Namet;