1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2011, 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 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This package defines tables used to store information needed for the Alfa
27 -- mode. It is used by procedures in Lib.Xref.Alfa to build the Alfa
28 -- information before writing it out to the ALI file, and by Get_Alfa/Put_Alfa
29 -- to read and write the text form that is used in the ALI file.
31 with Types; use Types;
36 -- Alfa information can exist in one of two forms. In the ALI file, it is
37 -- represented using a text format that is described in this specification.
38 -- Internally it is stored using three tables Alfa_Xref_Table,
39 -- Alfa_Scope_Table and Alfa_File_Table, which are also defined in this
42 -- Lib.Xref.Alfa is part of the compiler. It extracts Alfa information from
43 -- the complete set of cross-references generated during compilation.
45 -- Get_Alfa reads the text lines in ALI format and populates the internal
46 -- tables with corresponding information.
48 -- Put_Alfa reads the internal tables and generates text lines in the ALI
55 -- Alfa information is generated on a unit-by-unit basis in the ALI file,
56 -- using lines that start with the identifying character F ("Formal").
57 -- These lines are generated if one of the -gnatd.E (SPARK generation mode)
58 -- or gnatd.F (Why generation mode) switches is set.
60 -- The Alfa information follows the cross-reference information, so it
61 -- needs not be read by tools like gnatbind, gnatmake etc.
63 -- -------------------
64 -- -- Scope Section --
65 -- -------------------
67 -- A first section defines the scopes in which entities are defined and
68 -- referenced. A scope is a package/subprogram declaration/body. Note that
69 -- a package declaration and body define two different scopes. Similarly, a
70 -- subprogram declaration and body, when both present, define two different
73 -- FD dependency-number filename
75 -- This header precedes scope information for the unit identified by
76 -- dependency number and file name. The dependency number is the index
77 -- into the generated D lines and is ones-origin (e.g. 2 = reference to
78 -- second generated D line).
80 -- The list of FD lines should match the list of D lines defined in the
81 -- ALI file, in the same order.
83 -- Note that the filename here will reflect the original name if a
84 -- Source_Reference pragma was encountered (since all line number
85 -- references will be with respect to the original file).
87 -- Note: the filename is redundant in that it could be deduced from the
88 -- corresponding D line, but it is convenient at least for human
89 -- reading of the Alfa information, and means that the Alfa information
90 -- can stand on its own without needing other parts of the ALI file.
92 -- FS . scope line type col entity (-> spec-file . spec-scope)?
94 -- (The ? mark stands for an optional entry in the syntax)
96 -- scope is the ones-origin scope number for the current file (e.g. 2 =
97 -- reference to the second FS line in this FD block).
99 -- line is the line number of the scope entity. The name of the entity
100 -- starts in column col. Columns are numbered from one, and if
101 -- horizontal tab characters are present, the column number is computed
102 -- assuming standard 1,9,17,.. tab stops. For example, if the entity is
103 -- the first token on the line, and is preceded by space-HT-space, then
104 -- the column would be column 10.
106 -- type is a single letter identifying the type of the entity, using
107 -- the same code as in cross-references:
113 -- col is the column number of the scope entity
115 -- entity is the name of the scope entity, with casing in the canonical
116 -- casing for the source file where it is defined.
118 -- spec-file and spec-scope are respectively the file and scope for the
119 -- spec corresponding to the current body scope, when they differ.
121 -- ------------------
122 -- -- Xref Section --
123 -- ------------------
125 -- A second section defines cross-references useful for computing the set
126 -- of global variables read/written in each subprogram/package.
128 -- FX dependency-number filename . entity-number entity
130 -- dependency-number and filename identity a file in FD lines
132 -- entity-number and identity identify a scope entity in FS lines for
133 -- the file previously identified.
135 -- line typ col entity ref*
137 -- line is the line number of the referenced entity
139 -- typ is the type of the referenced entity, using a code similar to
140 -- the one used for cross-references:
144 -- = = IN OUT parameter
145 -- * = all other cases
147 -- col is the column number of the referenced entity
149 -- entity is the name of the referenced entity as written in the source
150 -- file where it is defined.
152 -- There may be zero or more ref entries on each line
154 -- (file |)? ((. scope :)? line type col)*
156 -- file is the dependency number of the file with the reference. It and
157 -- the following vertical bar are omitted if the file is the same as
158 -- the previous ref, and the refs for the current file are first (and
159 -- do not need a bar).
161 -- scope is the scope number of the scope with the reference. It and
162 -- the following colon are omitted if the scope is the same as the
163 -- previous ref, and the refs for the current scope are first (and do
164 -- not need a colon).
166 -- line is the line number of the reference
168 -- col is the column number of the reference
170 -- type is one of the following, using the same code as in
175 -- s = subprogram reference in a static call
177 -- Special entries for reads and writes to memory reference a special
178 -- variable called "__HEAP". These special entries are present in every
179 -- scope where reads and writes to memory are present. Line and column for
180 -- this special variable are always 0.
182 -- Examples: ??? add examples here
188 -- The following table records Alfa cross-references
190 type Xref_Index is new Int;
191 -- Used to index values in this table. Values start at 1 and are assigned
192 -- sequentially as entries are constructed.
194 type Alfa_Xref_Record is record
195 Entity_Name : String_Ptr;
196 -- Pointer to entity name in ALI file
199 -- Line number for the entity referenced
202 -- Indicates type of entity, using code used in ALI file:
205 -- = = IN OUT parameter
206 -- * = all other cases
209 -- Column number for the entity referenced
212 -- Set to the file dependency number for the cross-reference. Note
213 -- that if no file entry is present explicitly, this is just a copy
214 -- of the reference for the current cross-reference section.
217 -- Set to the scope number for the cross-reference. Note that if no
218 -- scope entry is present explicitly, this is just a copy of the
219 -- reference for the current cross-reference section.
222 -- Line number for the reference
225 -- Indicates type of reference, using code used in ALI file:
231 -- Column number for the reference
234 package Alfa_Xref_Table is new GNAT.Table (
235 Table_Component_Type => Alfa_Xref_Record,
236 Table_Index_Type => Xref_Index,
237 Table_Low_Bound => 1,
238 Table_Initial => 2000,
239 Table_Increment => 300);
245 -- This table keeps track of the scopes and the corresponding starting and
246 -- ending indexes (From, To) in the Xref table.
248 type Scope_Index is new Int;
249 -- Used to index values in this table. Values start at 1 and are assigned
250 -- sequentially as entries are constructed.
252 type Alfa_Scope_Record is record
253 Scope_Name : String_Ptr;
254 -- Pointer to scope name in ALI file
257 -- Set to the file dependency number for the scope
260 -- Set to the scope number for the scope
263 -- Set to the file dependency number for the scope corresponding to the
264 -- spec of the current scope entity, if different, or else 0.
266 Spec_Scope_Num : Nat;
267 -- Set to the scope number for the scope corresponding to the spec of
268 -- the current scope entity, if different, or else 0.
271 -- Line number for the scope
274 -- Indicates type of scope, using code used in ALI file:
280 -- Column number for the scope
282 From_Xref : Xref_Index;
283 -- Starting index in Xref table for this scope
285 To_Xref : Xref_Index;
286 -- Ending index in Xref table for this scope
288 -- The following component is only used in-memory, not printed out in
291 Scope_Entity : Entity_Id := Empty;
292 -- Entity (subprogram or package) for the scope
295 package Alfa_Scope_Table is new GNAT.Table (
296 Table_Component_Type => Alfa_Scope_Record,
297 Table_Index_Type => Scope_Index,
298 Table_Low_Bound => 1,
299 Table_Initial => 200,
300 Table_Increment => 300);
306 -- This table keeps track of the units and the corresponding starting and
307 -- ending indexes (From, To) in the Scope table.
309 type File_Index is new Int;
310 -- Used to index values in this table. Values start at 1 and are assigned
311 -- sequentially as entries are constructed.
313 type Alfa_File_Record is record
314 File_Name : String_Ptr;
315 -- Pointer to file name in ALI file
318 -- Dependency number in ALI file
320 From_Scope : Scope_Index;
321 -- Starting index in Scope table for this unit
323 To_Scope : Scope_Index;
324 -- Ending index in Scope table for this unit
327 package Alfa_File_Table is new GNAT.Table (
328 Table_Component_Type => Alfa_File_Record,
329 Table_Index_Type => File_Index,
330 Table_Low_Bound => 1,
332 Table_Increment => 200);
338 Name_Of_Heap_Variable : constant String := "__HEAP";
339 -- Name of special variable used in effects to denote reads and writes
340 -- through explicit dereference.
346 procedure Initialize_Alfa_Tables;
347 -- Reset tables for a new compilation
350 -- Debug routine to dump internal Alfa tables. This is a raw format dump
351 -- showing exactly what the tables contain.
354 -- Debugging procedure to output contents of Alfa binary tables in the
355 -- format in which they appear in an ALI file.