OSDN Git Service

* 41intnam.ads, 42intnam.ads, 4aintnam.ads, 4cintnam.ads,
[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 --                            $Revision$
10 --                                                                          --
11 --          Copyright (C) 1992-2002 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNAT was originally developed  by the GNAT team at  New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 --                                                                          --
34 ------------------------------------------------------------------------------
35
36 with Alloc;
37 with Table;
38 with System;   use System;
39 with Types;    use Types;
40
41 package Namet is
42
43 --  WARNING: There is a C version of this package. Any changes to this
44 --  source file must be properly reflected in the C header file namet.h
45 --  which is created manually from namet.ads and namet.adb.
46
47 --  This package contains routines for handling the names table. The table
48 --  is used to store character strings for identifiers and operator symbols,
49 --  as well as other string values such as unit names and file names.
50
51 --  The forms of the entries are as follows:
52
53 --    Identifiers        Stored with upper case letters folded to lower case.
54 --                       Upper half (16#80# bit set) and wide characters are
55 --                       stored in an encoded form (Uhh for upper half and
56 --                       Whhhh for wide characters, as provided by the routine
57 --                       Store_Encoded_Character, where hh are hex digits for
58 --                       the character code using lower case a-f). Other
59 --                       internally generated names use upper case letters
60 --                       (other than O,Q,U,W) to ensure that they do not clash
61 --                       with identifier names in the source program.
62
63 --    Operator symbols   Stored with an initial letter O, and the remainder
64 --                       of the name is the lower case characters XXX where
65 --                       the name is Name_Op_XXX, see Snames spec for a full
66 --                       list of the operator names.
67
68 --    Character literals Character literals have names that are used only for
69 --                       debugging and error message purposes. The form is a
70 --                       upper case Q followed by a single letter, or by a Uxx
71 --                       or Wxxxx encoding as described for identifiers. The
72 --                       Set_Character_Literal_Name procedure should be used
73 --                       to construct these encodings.
74
75 --    Unit names         Stored with upper case letters folded to lower case,
76 --                       using Uhh/Whhhh encoding as described for identifiers,
77 --                       and a %s or %b suffix for specs/bodies. See package
78 --                       Uname for further details.
79
80 --    File names         Are stored in the form provided by Osint. Typically
81 --                       they may include wide character escape sequences and
82 --                       upper case characters (in non-encoded form). Casing
83 --                       is also derived from the external environment. Note
84 --                       that file names provided by Osint must generally be
85 --                       consistent with the names from Fname.Get_File_Name.
86
87 --    Other strings      The names table is also used as a convenient storage
88 --                       location for other variable length strings such as
89 --                       error messages etc. There are no restrictions on what
90 --                       characters may appear for such entries.
91
92 --  Note: the encodings Uhh (upper half characters), Whhhh (wide characters),
93 --  and Qx (character literal names) are described in the spec, since they
94 --  are visible throughout the system (e.g. in debugging output). However,
95 --  no code should depend on these particular encodings, so it should be
96 --  possible to change the encodings by making changes only to the Namet
97 --  specification (to change these comments) and the body (which actually
98 --  implements the encodings).
99
100 --  The names are hashed so that a given name appears only once in the table,
101 --  except that names entered with Name_Enter as opposed to Name_Find are
102 --  omitted from the hash table.
103
104 --  The first 26 entries in the names table (with Name_Id values in the range
105 --  First_Name_Id .. First_Name_Id + 25) represent names which are the one
106 --  character lower case letters in the range a-z, and these names are created
107 --  and initialized by the Initialize procedure.
108
109 --  Two values, one of type Int and one of type Byte, are stored with each
110 --  names table entry and subprograms are provided for setting and retrieving
111 --  these associated values. The usage of these values is up to the client.
112 --  In the compiler, the Int field is used to point to a chain of potentially
113 --  visible entities (see Sem.Ch8 for details), and the Byte field is used
114 --  to hold the Token_Type value for reserved words (see Sem for details).
115 --  In the binder, the Byte field is unused, and the Int field is used in
116 --  various ways depending on the name involved (see binder documentation).
117
118    Name_Buffer : String (1 .. 16*1024);
119    --  This buffer is used to set the name to be stored in the table for the
120    --  Name_Find call, and to retrieve the name for the Get_Name_String call.
121    --  The plus 1 in the length allows for cases of adding ASCII.NUL. The
122    --  16K here is intended to be an infinite value that ensures that we
123    --  never overflow the buffer (names this long are too absurd to worry!)
124
125    Name_Len : Natural;
126    --  Length of name stored in Name_Buffer. Used as an input parameter for
127    --  Name_Find, and as an output value by Get_Name_String, or Write_Name.
128
129    -----------------
130    -- Subprograms --
131    -----------------
132
133    procedure Finalize;
134    --  Called at the end of a use of the Namet package (before a subsequent
135    --  call to Initialize). Currently this routine is only used to generate
136    --  debugging output.
137
138    procedure Get_Name_String (Id : Name_Id);
139    --  Get_Name_String is used to retrieve the string associated with an entry
140    --  in the names table. The resulting string is stored in Name_Buffer
141    --  and Name_Len is set. It is an error to call Get_Name_String with one
142    --  of the special name Id values (No_Name or Error_Name).
143
144    function Get_Name_String (Id : Name_Id) return String;
145    --  This functional form returns the result as a string without affecting
146    --  the contents of either Name_Buffer or Name_Len.
147
148    procedure Get_Unqualified_Name_String (Id : Name_Id);
149    --  Similar to the above except that qualification (as defined in unit
150    --  Exp_Dbug) is removed (including both preceding __ delimited names,
151    --  and also the suffixes used to indicate package body entities and to
152    --  distinguish between overloaded entities). Note that names are not
153    --  qualified until just before the call to gigi, so this routine is
154    --  only needed by processing that occurs after gigi has been called.
155    --  This includes all ASIS processing, since ASIS works on the tree
156    --  written after gigi has been called.
157
158    procedure Get_Name_String_And_Append (Id : Name_Id);
159    --  Like Get_Name_String but the resulting characters are appended to
160    --  the current contents of the entry stored in Name_Buffer, and Name_Len
161    --  is incremented to include the added characters.
162
163    procedure Get_Decoded_Name_String (Id : Name_Id);
164    --  Same calling sequence an interface as Get_Name_String, except that the
165    --  result is decoded, so that upper half characters and wide characters
166    --  appear as originally found in the source program text, operators have
167    --  their source forms (special characters and enclosed in quotes), and
168    --  character literals appear surrounded by apostrophes.
169
170    procedure Get_Unqualified_Decoded_Name_String (Id : Name_Id);
171    --  Similar to the above except that qualification (as defined in unit
172    --  Exp_Dbug) is removed (including both preceding __ delimited names,
173    --  and also the suffix used to indicate package body entities). Note
174    --  that names are not qualified until just before the call to gigi, so
175    --  this routine is only needed by processing that occurs after gigi has
176    --  been called. This includes all ASIS processing, since ASIS works on
177    --  the tree written after gigi has been called.
178
179    procedure Get_Decoded_Name_String_With_Brackets (Id : Name_Id);
180    --  This routine is similar to Decoded_Name, except that the brackets
181    --  notation (Uhh replaced by ["hh"], Whhhh replaced by ["hhhh"]) is
182    --  used for all non-lower half characters, regardless of the setting
183    --  of Opt.Wide_Character_Encoding_Method, and also in that characters
184    --  in the range 16#80# .. 16#FF# are converted to brackets notation
185    --  in all cases. This routine can be used when there is a requirement
186    --  for a canonical representation not affected by the character set
187    --  options (e.g. in the binder generation of symbols).
188
189    function Get_Name_Table_Byte (Id : Name_Id) return Byte;
190    pragma Inline (Get_Name_Table_Byte);
191    --  Fetches the Byte value associated with the given name
192
193    function Get_Name_Table_Info (Id : Name_Id) return Int;
194    pragma Inline (Get_Name_Table_Info);
195    --  Fetches the Int value associated with the given name
196
197    procedure Initialize;
198    --  Initializes the names table, including initializing the first 26
199    --  entries in the table (for the 1-character lower case names a-z)
200    --  Note that Initialize must not be called if Tree_Read is used.
201
202    procedure Lock;
203    --  Lock name table before calling back end. Space for up to 10 extra
204    --  names and 1000 extra characters is reserved before the table is locked.
205
206    procedure Unlock;
207    --  Unlocks the name table to allow use of the 10 extra names and 1000
208    --  extra characters reserved by the Lock call. See gnat1drv for details
209    --  of the need for this.
210
211    function Length_Of_Name (Id : Name_Id) return Nat;
212    pragma Inline (Length_Of_Name);
213    --  Returns length of given name in characters. This is the length of the
214    --  encoded name, as stored in the names table, the result is equivalent to
215    --  calling Get_Name_String and reading Name_Len, except that a call to
216    --  Length_Of_Name does not affect the contents of Name_Len and Name_Buffer.
217
218    function Name_Chars_Address return System.Address;
219    --  Return starting address of name characters table (used in Back_End
220    --  call to Gigi).
221
222    function Name_Find return Name_Id;
223    --  Name_Find is called with a string stored in Name_Buffer whose length
224    --  is in Name_Len (i.e. the characters of the name are in subscript
225    --  positions 1 to Name_Len in Name_Buffer). It searches the names
226    --  table to see if the string has already been stored. If so the Id of
227    --  the existing entry is returned. Otherwise a new entry is created with
228    --  its Name_Table_Info field set to zero. The contents of Name_Buffer
229    --  and Name_Len are not modified by this call.
230
231    function Name_Enter return Name_Id;
232    --  Name_Enter has the same calling interface as Name_Find. The difference
233    --  is that it does not search the table for an existing match, and also
234    --  subsequent Name_Find calls using the same name will not locate the
235    --  entry created by this call. Thus multiple calls to Name_Enter with the
236    --  same name will create multiple entries in the name table with different
237    --  Name_Id values. This is useful in the case of created names, which are
238    --  never expected to be looked up. Note: Name_Enter should never be used
239    --  for one character names, since these are efficiently located without
240    --  hashing by Name_Find in any case.
241
242    function Name_Entries_Address return System.Address;
243    --  Return starting address of Names table. Used in Back_End call to Gigi.
244
245    function Name_Entries_Count return Nat;
246    --  Return current number of entries in the names table
247
248    function Is_OK_Internal_Letter (C : Character) return Boolean;
249    pragma Inline (Is_OK_Internal_Letter);
250    --  Returns true if C is a suitable character for using as a prefix or a
251    --  suffix of an internally generated name, i.e. it is an upper case letter
252    --  other than one of the ones used for encoding source names (currently
253    --  the set of reserved letters is O, Q, U, W) and also returns False for
254    --  the letter X, which is reserved for debug output (see Exp_Dbug).
255
256    function Is_Internal_Name (Id : Name_Id) return Boolean;
257    --  Returns True if the name is an internal name (i.e. contains a character
258    --  for which Is_OK_Internal_Letter is true, or if the name starts or ends
259    --  with an underscore. This call destroys the value of Name_Len and
260    --  Name_Buffer (it loads these as for Get_Name_String).
261    --
262    --  Note: if the name is qualified (has a double underscore), then
263    --  only the final entity name is considered, not the qualifying
264    --  names. Consider for example that the name:
265    --
266    --    pkg__B_1__xyz
267    --
268    --  is not an internal name, because the B comes from the internal
269    --  name of a qualifying block, but the xyz means that this was
270    --  indeed a declared identifier called "xyz" within this block
271    --  and there is nothing internal about that name.
272
273    function Is_Internal_Name return Boolean;
274    --  Like the form with an Id argument, except that the name to be tested is
275    --  passed in Name_Buffer and Name_Len (which are not affected by the call).
276    --  Name_Buffer (it loads these as for Get_Name_String).
277
278    procedure Reset_Name_Table;
279    --  This procedure is used when there are multiple source files to reset
280    --  the name table info entries associated with current entries in the
281    --  names table. There is no harm in keeping the names entries themselves
282    --  from one compilation to another, but we can't keep the entity info,
283    --  since this refers to tree nodes, which are destroyed between each
284    --  main source file.
285
286    procedure Add_Char_To_Name_Buffer (C : Character);
287    pragma Inline (Add_Char_To_Name_Buffer);
288    --  Add given character to the end of the string currently stored in the
289    --  Name_Buffer, incrementing Name_Len.
290
291    procedure Add_Nat_To_Name_Buffer (V : Nat);
292    --  Add decimal representation of given value to the end of the string
293    --  currently stored in Name_Buffer, incrementing Name_Len as required.
294
295    procedure Add_Str_To_Name_Buffer (S : String);
296    --  Add characters of string S to the end of the string currently stored
297    --  in the Name_Buffer, incrementing Name_Len by the length of the string.
298
299    procedure Set_Character_Literal_Name (C : Char_Code);
300    --  This procedure sets the proper encoded name for the character literal
301    --  for the given character code. On return Name_Buffer and Name_Len are
302    --  set to reflect the stored name.
303
304    procedure Set_Name_Table_Info (Id : Name_Id; Val : Int);
305    pragma Inline (Set_Name_Table_Info);
306    --  Sets the Int value associated with the given name
307
308    procedure Set_Name_Table_Byte (Id : Name_Id; Val : Byte);
309    pragma Inline (Set_Name_Table_Byte);
310    --  Sets the Byte value associated with the given name
311
312    procedure Store_Encoded_Character (C : Char_Code);
313    --  Stores given character code at the end of Name_Buffer, updating the
314    --  value in Name_Len appropriately. Lower case letters and digits are
315    --  stored unchanged. Other 8-bit characters are stored using the Uhh
316    --  encoding (hh = hex code), and other 16-bit wide-character values
317    --  are stored using the Whhhh (hhhh = hex code) encoding. Note that
318    --  this procedure does not fold upper case letters (they are stored
319    --  using the Uhh encoding). If folding is required, it must be done
320    --  by the caller prior to the call.
321
322    procedure Tree_Read;
323    --  Initializes internal tables from current tree file using Tree_Read.
324    --  Note that Initialize should not be called if Tree_Read is used.
325    --  Tree_Read includes all necessary initialization.
326
327    procedure Tree_Write;
328    --  Writes out internal tables to current tree file using Tree_Write
329
330    procedure Write_Name (Id : Name_Id);
331    --  Write_Name writes the characters of the specified name using the
332    --  standard output procedures in package Output. No end of line is
333    --  written, just the characters of the name. On return Name_Buffer and
334    --  Name_Len are set as for a call to Get_Name_String. The name is written
335    --  in encoded form (i.e. including Uhh, Whhh, Qx, _op as they appear in
336    --  the name table). If Id is Error_Name, or No_Name, no text is output.
337
338    procedure wn (Id : Name_Id);
339    pragma Export (Ada, wn);
340    --  Like Write_Name, but includes new line at end. Intended for use
341    --  from the debugger only.
342
343    procedure Write_Name_Decoded (Id : Name_Id);
344    --  Like Write_Name, except that the name written is the decoded name, as
345    --  described for Get_Name_Decoded, and the resulting value stored in
346    --  Name_Len and Name_Buffer is the decoded name.
347
348    ---------------------------
349    -- Table Data Structures --
350    ---------------------------
351
352    --  The following declarations define the data structures used to store
353    --  names. The definitions are in the private part of the package spec,
354    --  rather than the body, since they are referenced directly by gigi.
355
356 private
357
358    --  This table stores the actual string names. Although logically there
359    --  is no need for a terminating character (since the length is stored
360    --  in the name entry table), we still store a NUL character at the end
361    --  of every name (for convenience in interfacing to the C world).
362
363    package Name_Chars is new Table.Table (
364      Table_Component_Type => Character,
365      Table_Index_Type     => Int,
366      Table_Low_Bound      => 0,
367      Table_Initial        => Alloc.Name_Chars_Initial,
368      Table_Increment      => Alloc.Name_Chars_Increment,
369      Table_Name           => "Name_Chars");
370
371    type Name_Entry is record
372       Name_Chars_Index : Int;
373       --  Starting location of characters in the Name_Chars table minus
374       --  one (i.e. pointer to character just before first character). The
375       --  reason for the bias of one is that indexes in Name_Buffer are
376       --  one's origin, so this avoids unnecessary adds and subtracts of 1.
377
378       Name_Len : Short;
379       --  Length of this name in characters
380
381       Byte_Info : Byte;
382       --  Byte value associated with this name
383
384       Hash_Link : Name_Id;
385       --  Link to next entry in names table for same hash code
386
387       Int_Info : Int;
388       --  Int Value associated with this name
389    end record;
390
391    --  This is the table that is referenced by Name_Id entries.
392    --  It contains one entry for each unique name in the table.
393
394    package Name_Entries is new Table.Table (
395      Table_Component_Type => Name_Entry,
396      Table_Index_Type     => Name_Id,
397      Table_Low_Bound      => First_Name_Id,
398      Table_Initial        => Alloc.Names_Initial,
399      Table_Increment      => Alloc.Names_Increment,
400      Table_Name           => "Name_Entries");
401
402 end Namet;