OSDN Git Service

Update FSF address
[pf3gnuchains/gcc-fork.git] / gcc / ada / xr_tabls.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             X R  _ T A B L S                             --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --         Copyright (C) 1998-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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 --  We need comment here saying what this package is???
28
29 with GNAT.OS_Lib;
30
31 package Xr_Tabls is
32
33    -------------------
34    -- Project files --
35    -------------------
36
37    function ALI_File_Name (Ada_File_Name : String) return String;
38    --  Returns the ali file name corresponding to Ada_File_Name.
39
40    procedure Create_Project_File (Name : String);
41    --  Open and parse a new project file. If the file Name could not be
42    --  opened or is not a valid project file, then a project file associated
43    --  with the standard default directories is returned
44
45    function Next_Obj_Dir return String;
46    --  Returns the next directory to visit to find related ali files
47    --  If there are no more such directories, returns a null string.
48
49    function Current_Obj_Dir return String;
50    --  Returns the obj_dir which was returned by the last Next_Obj_Dir call
51
52    procedure Reset_Obj_Dir;
53    --  Reset the iterator for Obj_Dir
54
55    ------------
56    -- Tables --
57    ------------
58
59    type Declaration_Reference is private;
60    Empty_Declaration : constant Declaration_Reference;
61
62    type Declaration_Array is array (Natural range <>) of Declaration_Reference;
63    type Declaration_Array_Access is access Declaration_Array;
64
65    type File_Reference is private;
66    Empty_File : constant File_Reference;
67
68    type Reference is private;
69    Empty_Reference : constant Reference;
70
71    type Reference_Array is array (Natural range <>) of Reference;
72    type Reference_Array_Access is access Reference_Array;
73
74    procedure Free (Arr : in out Reference_Array_Access);
75
76    function Add_Declaration
77      (File_Ref     : File_Reference;
78       Symbol       : String;
79       Line         : Natural;
80       Column       : Natural;
81       Decl_Type    : Character;
82       Remove_Only  : Boolean := False;
83       Symbol_Match : Boolean := True)
84       return         Declaration_Reference;
85    --  Add a new declaration in the table and return the index to it.
86    --  Decl_Type is the type of the entity Any previous instance of this
87    --  entity in the htable is removed. If Remove_Only is True, then any
88    --  previous instance is removed, but the new entity is never inserted.
89    --  Symbol_Match should be set to False if the name of the symbol doesn't
90    --  match the pattern from the command line. In that case, the entity will
91    --  not be output by gnatfind. If Symbol_Match is True, the entity will only
92    --  be output if the file name itself matches.
93
94    procedure Add_Parent
95      (Declaration : in out Declaration_Reference;
96       Symbol      : String;
97       Line        : Natural;
98       Column      : Natural;
99       File_Ref    : File_Reference);
100    --  The parent declaration (Symbol in file File_Ref at position Line and
101    --  Column) information is added to Declaration.
102
103    function Add_To_Xref_File
104      (File_Name       : String;
105       Visited         : Boolean := True;
106       Emit_Warning    : Boolean := False;
107       Gnatchop_File   : String  := "";
108       Gnatchop_Offset : Integer := 0)
109       return            File_Reference;
110    --  Add a new reference to a file in the table. Ref is used to return the
111    --  index in the table where this file is stored. Visited is the value which
112    --  will be used in the table (if True, the file will not be returned by
113    --  Next_Unvisited_File). If Emit_Warning is True and the ali file does
114    --  not exist or does not have cross-referencing information, then a
115    --  warning will be emitted. Gnatchop_File is the name of the file that
116    --  File_Name was extracted from through a call to "gnatchop -r" (using
117    --  pragma Source_Reference). Gnatchop_Offset should be the index of the
118    --  first line of File_Name within the Gnatchop_File.
119
120    procedure Add_Line
121      (File   : File_Reference;
122       Line   : Natural;
123       Column : Natural);
124    --  Add a new reference in a file, which the user has provided on the
125    --  command line. This is used for an optimized matching algorithm.
126
127    procedure Add_Reference
128      (Declaration   : Declaration_Reference;
129       File_Ref      : File_Reference;
130       Line          : Natural;
131       Column        : Natural;
132       Ref_Type      : Character;
133       Labels_As_Ref : Boolean);
134    --  Add a new reference (Ref_Type = 'r'), body (Ref_Type = 'b') or
135    --  modification (Ref_Type = 'm') to an entity. If Labels_As_Ref is True,
136    --  then the references to the entity after the end statements ("end Foo")
137    --  are counted as actual references. This means that the entity will never
138    --  be reported as unreferenced (for instance in the case of gnatxref -u).
139
140    function Get_Declarations
141      (Sorted : Boolean := True)
142       return   Declaration_Array_Access;
143    --  Return a sorted list of all the declarations in the application.
144    --  Freeing this array is the responsability of the caller, however it
145    --  shouldn't free the actual contents of the array, which are pointers
146    --  to internal data
147
148    function References_Count
149      (Decl       : Declaration_Reference;
150       Get_Reads  : Boolean := False;
151       Get_Writes : Boolean := False;
152       Get_Bodies : Boolean := False)
153       return       Natural;
154    --  Return the number of references in Decl for the categories specified
155    --  by the Get_* parameters (read-only accesses, write accesses and bodies)
156
157    function Get_References
158      (Decl : Declaration_Reference;
159       Get_Reads  : Boolean := False;
160       Get_Writes : Boolean := False;
161       Get_Bodies : Boolean := False)
162       return       Reference_Array_Access;
163    --  Return a sorted list of all references to the entity in decl.
164    --  The parameters Get_* are used to specify what kind of references
165    --  should be merged and returned (read-only accesses, write accesses
166    --  and bodies).
167
168    function Get_Column (Decl : Declaration_Reference) return String;
169    function Get_Column (Ref : Reference) return String;
170
171    function Get_Declaration
172      (File_Ref : File_Reference;
173       Line     : Natural;
174       Column   : Natural)
175       return     Declaration_Reference;
176    --  Returns reference to the declaration found in file File_Ref at the
177    --  given Line and Column
178
179    function Get_Parent
180      (Decl : Declaration_Reference)
181       return Declaration_Reference;
182    --  Returns reference to Decl's parent declaration
183
184    function Get_Emit_Warning (File : File_Reference) return Boolean;
185    --  Returns the Emit_Warning field of the structure
186
187    function Get_Gnatchop_File
188      (File     : File_Reference;
189       With_Dir : Boolean := False)
190       return     String;
191    function Get_Gnatchop_File
192      (Ref      : Reference;
193       With_Dir : Boolean := False)
194       return     String;
195    function Get_Gnatchop_File
196      (Decl     : Declaration_Reference;
197       With_Dir : Boolean := False)
198       return     String;
199    --  Return the name of the file that File was extracted from through a
200    --  call to "gnatchop -r". The file name for File is returned if File
201    --  was not extracted from such a file. The directory will be given only
202    --  if With_Dir is True.
203
204    function Get_File
205      (Decl     : Declaration_Reference;
206       With_Dir : Boolean := False) return String;
207    pragma Inline (Get_File);
208    --  Extract column number or file name from reference
209
210    function Get_File
211      (Ref      : Reference;
212       With_Dir : Boolean := False) return String;
213    pragma Inline (Get_File);
214
215    function Get_File
216      (File     : File_Reference;
217       With_Dir : Boolean := False;
218       Strip    : Natural := 0) return String;
219    --  Returns the file name (and its directory if With_Dir is True or the
220    --  user has used the -f switch on the command line. If Strip is not 0,
221    --  then the last Strip-th "-..." substrings are removed first. For
222    --  instance, with Strip=2, a file name "parent-child1-child2-child3.ali"
223    --  would be returned as "parent-child1.ali". This is used when looking
224    --  for the ALI file to use for a package, since for separates with have
225    --  to use the parent's ALI. The null string is returned if there is no
226    --  such parent unit.
227    --
228    --  Note that this version of Get_File is not inlined
229
230    function Get_File_Ref (Ref : Reference)              return File_Reference;
231    function Get_Line     (Decl : Declaration_Reference) return String;
232    function Get_Line     (Ref : Reference)              return String;
233    function Get_Symbol   (Decl : Declaration_Reference) return String;
234    function Get_Type     (Decl : Declaration_Reference) return Character;
235    function Is_Parameter (Decl : Declaration_Reference) return Boolean;
236    --  Functions that return the contents of a declaration
237
238    function Get_Source_Line (Ref : Reference)              return String;
239    function Get_Source_Line (Decl : Declaration_Reference) return String;
240    --  Return the source line associated with the reference
241
242    procedure Grep_Source_Files;
243    --  Parse all the source files which have at least one reference,
244    --  and grep the appropriate source lines so that we'll be able to
245    --  display them.  This function should be called once all the .ali
246    --  files have been parsed, and only if the appropriate user switch
247    --  has been used (gnatfind -s).
248    --
249    --  Note: To save memory, the strings for the source lines are shared.
250    --  Thus it is no longer possible to free the references, or we would
251    --  free the same chunk multiple times. It doesn't matter, though, since
252    --  this is only called once, prior to exiting gnatfind.
253
254    function Longest_File_Name return Natural;
255    --  Returns the longest file name found
256
257    function Match (Decl : Declaration_Reference) return Boolean;
258    --  Return True if the declaration matches
259
260    function Match
261      (File   : File_Reference;
262       Line   : Natural;
263       Column : Natural)
264       return   Boolean;
265    --  Returns True if File:Line:Column was given on the command line
266    --  by the user
267
268    function Next_Unvisited_File return File_Reference;
269    --  Returns the next unvisited library file in the list
270    --  If there is no more unvisited file, return Empty_File.
271    --  Two calls to this subprogram will return different files.
272
273    procedure Set_Default_Match (Value : Boolean);
274    --  Set the default value for match in declarations.
275    --  This is used so that if no file was provided in the
276    --  command line, then every file match
277
278    procedure Reset_Directory (File : File_Reference);
279    --  Reset the cached directory for file. Next time Get_File is
280    --  called, the directory willl be recomputed.
281
282    procedure Set_Unvisited (File_Ref : File_Reference);
283    --  Set File_Ref as unvisited. So Next_Unvisited_File will return it.
284
285    procedure Read_File
286      (File_Name : String;
287       Contents  : out GNAT.OS_Lib.String_Access);
288    --  Reads File_Name into the newly allocated strig Contents. A
289    --  Types.EOF character will be added to the returned Contents to
290    --  simplify parsing. Name_Error is raised if the file was not found.
291    --  End_Error is raised if the file could not be read correctly. For
292    --  most systems correct reading means that the number of bytes read
293    --  is equal to the file size. The exception is OpenVMS where correct
294    --  reading means that the number of bytes read is less than or equal
295    --  to the file size.
296
297 private
298    type Project_File (Src_Dir_Length, Obj_Dir_Length : Natural) is record
299       Src_Dir : String (1 .. Src_Dir_Length);
300       Src_Dir_Index : Integer;
301
302       Obj_Dir            : String (1 .. Obj_Dir_Length);
303       Obj_Dir_Index      : Integer;
304       Last_Obj_Dir_Start : Natural;
305    end record;
306
307    type Project_File_Ptr is access all Project_File;
308    --  This is actually a list of all the directories to be searched,
309    --  either for source files or for library files
310
311    type Ref_In_File;
312    type Ref_In_File_Ptr is access all Ref_In_File;
313
314    type Ref_In_File is record
315       Line   : Natural;
316       Column : Natural;
317       Next   : Ref_In_File_Ptr := null;
318    end record;
319
320    type File_Record;
321    type File_Reference is access all File_Record;
322
323    Empty_File : constant File_Reference := null;
324    type Cst_String_Access is access constant String;
325
326    procedure Free (Str : in out Cst_String_Access);
327
328    type File_Record is record
329       File            : Cst_String_Access;
330       Dir             : GNAT.OS_Lib.String_Access;
331       Lines           : Ref_In_File_Ptr := null;
332       Visited         : Boolean         := False;
333       Emit_Warning    : Boolean         := False;
334       Gnatchop_File   : GNAT.OS_Lib.String_Access   := null;
335       Gnatchop_Offset : Integer         := 0;
336       Next            : File_Reference  := null;
337    end record;
338    --  Holds a reference to a source file, that was referenced in at least one
339    --  ALI file. Gnatchop_File will contain the name of the file that File was
340    --  extracted From. Gnatchop_Offset contains the index of the first line of
341    --  File within Gnatchop_File. These two fields are used to properly support
342    --  gnatchop files and pragma Source_Reference.
343    --
344    --  Lines is used for files that were given on the command line, to
345    --  memorize the lines and columns that the user specified.
346
347    type Reference_Record;
348    type Reference is access all Reference_Record;
349
350    Empty_Reference : constant Reference := null;
351
352    type Reference_Record is record
353       File        : File_Reference;
354       Line        : Natural;
355       Column      : Natural;
356       Source_Line : Cst_String_Access;
357       Next        : Reference := null;
358    end record;
359    --  File is a reference to the Ada source file
360    --  Source_Line is the Line as it appears in the source file. This
361    --  field is only used when the switch is set on the command line of
362    --  gnatfind.
363
364    type Declaration_Record;
365    type Declaration_Reference is access all Declaration_Record;
366
367    Empty_Declaration : constant Declaration_Reference := null;
368
369    type Declaration_Record (Symbol_Length : Natural) is record
370       Key          : Cst_String_Access;
371       Symbol       : String (1 .. Symbol_Length);
372       Decl         : Reference;
373       Is_Parameter : Boolean := False; -- True if entity is subprog param
374       Decl_Type    : Character;
375       Body_Ref     : Reference := null;
376       Ref_Ref      : Reference := null;
377       Modif_Ref    : Reference := null;
378       Match        : Boolean := False;
379       Par_Symbol   : Declaration_Reference := null;
380       Next         : Declaration_Reference := null;
381    end record;
382    --  The lists of referenced (Body_Ref, Ref_Ref and Modif_Ref) are
383    --  kept unsorted until the results needs to be printed. This saves
384    --  lots of time while the internal tables are created.
385
386    pragma Inline (Get_Column);
387    pragma Inline (Get_Emit_Warning);
388    pragma Inline (Get_File_Ref);
389    pragma Inline (Get_Line);
390    pragma Inline (Get_Symbol);
391    pragma Inline (Get_Type);
392    pragma Inline (Longest_File_Name);
393 end Xr_Tabls;