1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-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 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 contains for collecting and outputting cross-reference
29 with Einfo; use Einfo;
33 -------------------------------------------------------
34 -- Format of Cross-Reference Information in ALI File --
35 -------------------------------------------------------
37 -- Cross-reference sections follow the dependency section (D lines) in
38 -- an ALI file, so that they need not be read by gnatbind, gnatmake etc.
40 -- A cross reference section has a header of the form
42 -- X dependency-number filename
44 -- This header precedes xref information (entities/references from
45 -- the unit, identified by dependency number and file name. The
46 -- dependency number is the index into the generated D lines and
47 -- is ones origin (i.e. 2 = reference to second generated D line).
49 -- Note that the filename here will reflect the original name if
50 -- a Source_Reference pragma was encountered (since all line number
51 -- references will be with respect to the original file).
53 -- The lines following the header look like
55 -- line type col level entity renameref instref typeref overref ref ref
57 -- line is the line number of the referenced entity. The name of
58 -- the entity starts in column col. Columns are numbered from one,
59 -- and if horizontal tab characters are present, the column number
60 -- is computed assuming standard 1,9,17,.. tab stops. For example,
61 -- if the entity is the first token on the line, and is preceded
62 -- by space-HT-space, then the column would be column 10.
64 -- type is a single letter identifying the type of the entity.
65 -- See next section (Cross-Reference Entity Identifiers) for a
66 -- full list of the characters used).
68 -- col is the column number of the referenced entity
70 -- level is a single character that separates the col and
71 -- entity fields. It is an asterisk for a top level library
72 -- entity that is publicly visible, as well for an entity declared
73 -- in the visible part of a generic package, and space otherwise.
75 -- entity is the name of the referenced entity, with casing in
76 -- the canical casing for the source file where it is defined.
78 -- renameref provides information on renaming. If the entity is
79 -- a package, object or overloadable entity which is declared by
80 -- a renaming declaration, and the renaming refers to an entity
81 -- with a simple identifier or expanded name, then renameref has
86 -- Here line:col give the reference to the identifier that
87 -- appears in the renaming declaration. Note that we never need
88 -- a file entry, since this identifier is always in the current
89 -- file in which the entity is declared. Currently, renameref
90 -- appears only for the simple renaming case. If the renaming
91 -- reference is a complex expressions, then renameref is omitted.
92 -- Here line/col give line/column as defined above.
94 -- instref is only present for package and subprogram instances.
95 -- The information in instref is the location of the point of
96 -- declaration of the generic parent unit. This part has the form:
100 -- without column information, on the reasonable assumption that
101 -- there is only one unit per line (the same assumption is made
102 -- in references to entities that are declared within instances,
105 -- typeref is the reference for a related type. This part is
106 -- optional. It is present for the following cases:
108 -- derived types (points to the parent type) LR=<>
109 -- access types (points to designated type) LR=()
110 -- array types (points to component type) LR=()
111 -- subtypes (points to ancestor type) LR={}
112 -- functions (points to result type) LR={}
113 -- enumeration literals (points to enum type) LR={}
114 -- objects and components (points to type) LR={}
116 -- For a type that implements multiple interfaces, there is an
117 -- entry of the form LR=<> for each of the interfaces appearing
118 -- in the type declaration.
120 -- In the above list LR shows the brackets used in the output,
121 -- which has one of the two following forms:
123 -- L file | line type col R user entity
124 -- L name-in-lower-case R standard entity
126 -- For the form for a user entity, file is the dependency number
127 -- of the file containing the declaration of the related type.
128 -- This number and the following vertical bar are omitted if the
129 -- relevant type is defined in the same file as the current entity.
130 -- The line, type, col are defined as previously described, and
131 -- specify the location of the relevant type declaration in the
132 -- referenced file. For the standard entity form, the name between
133 -- the brackets is the normal name of the entity in lower case.
135 -- overref is present for overriding operations (procedures and
136 -- functions), and provides information on the operation that it
137 -- overrides. This information has the format:
139 -- '<' file | line 'o' col '>'
141 -- file is the dependency number of the file containing the
142 -- declaration of the overridden operation. It and the following
143 -- vertical bar are omitted if the file is the same as that of
144 -- the overriding operation.
146 -- There may be zero or more ref entries on each line
148 -- file | line type col [...]
150 -- file is the dependency number of the file with the reference.
151 -- It and the following vertical bar are omitted if the file is
152 -- the same as the previous ref, and the refs for the current
153 -- file are first (and do not need a bar).
155 -- line is the line number of the reference
157 -- col is the column number of the reference, as defined above
161 -- c = completion of private or incomplete type
162 -- d = discriminant of type
165 -- i = implicit reference
166 -- k = implicit reference to parent unit in child unit
167 -- l = label on END line
169 -- p = primitive operation
170 -- P = overriding primitive operation
174 -- x = type extension
175 -- z = generic formal parameter
176 -- > = subprogram IN parameter
177 -- = = subprogram IN OUT parameter
178 -- < = subprogram OUT parameter
179 -- > = subprogram ACCESS parameter
181 -- b is used for spec entities that are repeated in a body,
182 -- including the unit (subprogram, package, task, protected
183 -- body, protected entry) name itself, and in the case of a
184 -- subprogram, the formals. This letter is also used for the
185 -- occurrence of entry names in accept statements. Such entities
186 -- are not considered to be definitions for cross-referencing
187 -- purposes, but rather are considered to be references to the
188 -- corresponding spec entities, marked with this special type.
190 -- c is similar to b but is used to mark the completion of a
191 -- private or incomplete type. As with b, the completion is not
192 -- regarded as a separate definition, but rather a reference to
193 -- the initial declaration, marked with this special type.
195 -- d is used to identify a discriminant of a type. If this is
196 -- an incomplete or private type with discriminants, the entry
197 -- denotes the occurrence of the discriminant in the partial view
198 -- which is also the point of definition of the discriminant.
199 -- The occurrence of the same discriminant in the full view is
200 -- a regular reference to it.
202 -- e is used to identify the end of a construct in the following
205 -- Block Statement end [block_IDENTIFIER];
206 -- Loop Statement end loop [loop_IDENTIFIER];
207 -- Package Specification end [[PARENT_UNIT_NAME .] IDENTIFIER];
208 -- Task Definition end [task_IDENTIFIER];
209 -- Protected Definition end [protected_IDENTIFIER];
210 -- Record Definition end record;
211 -- Enumeration Definition );
213 -- Note that 'e' entries are special in that they appear even
214 -- in referencing units (normally xref entries appear only
215 -- for references in the extended main source unit (see Lib) to
216 -- which the ali applies. But 'e' entries are really structural
217 -- and simply indicate where packages end. This information can
218 -- be used to reconstruct scope information for any entities
219 -- referenced from within the package. The line/column values
220 -- for these entries point to the semicolon ending the construct.
222 -- i is used to identify a reference to the entity in a generic
223 -- actual or in a default in a call. The node that denotes the
224 -- entity does not come from source, but it has the Sloc of the
225 -- source node that generates the implicit reference, and it is
226 -- useful to record this one.
228 -- k is used to denote a reference to the parent unit, in the
229 -- cross-reference line for a child unit.
231 -- l is used to identify the occurrence in the source of the
232 -- name on an end line. This is just a syntactic reference
233 -- which can be ignored for semantic purposes (such as call
234 -- graph construction). Again, in the case of an accept there
235 -- can be multiple l lines.
237 -- p is used to mark a primitive operation of the given entity.
238 -- For example, if we have a type Tx, and a primitive operation
239 -- Pq of this type, then an entry in the list of references to
240 -- Tx will point to the declaration of Pq. Note that this entry
241 -- type is unusual because it an implicit rather than explicit,
242 -- and the name of the refrerence does not match the name of the
243 -- entity for which a reference is generated. These entries are
244 -- generated only for entities declared in the extended main
245 -- source unit (main unit itself, its separate spec (if any).
246 -- and all subunits (considered recursively).
248 -- If the primitive operation overrides an inherited primitive
249 -- operation of the parent type, the letter 'P' is used in the
250 -- corresponding entry.
252 -- t is similar to e. It identifies the end of a corresponding
253 -- body (such a reference always links up with a b reference)
255 -- Subprogram Body end [DESIGNATOR];
256 -- Package Body end [[PARENT_UNIT_NAME .] IDENTIFIER];
257 -- Task Body end [task_IDENTIFIER];
258 -- Entry Body end [entry_IDENTIFIER];
259 -- Protected Body end [protected_IDENTIFIER]
260 -- Accept Statement end [entry_IDENTIFIER]];
262 -- Note that in the case of accept statements, there can
263 -- be multiple b and t entries for the same entity.
265 -- x is used to identify the reference as the entity from which
266 -- a tagged type is extended. This allows immediate access to
267 -- the parent of a tagged type.
269 -- z is used on the cross-reference line for a generic unit, to
270 -- mark the definition of a generic formal of the unit.
271 -- This entry type is similar to 'k' and 'p' in that it is an
272 -- implicit reference for an entity with a different name.
274 -- The characters >, <. =, and ^ are used on the cross-reference
275 -- line for a subprogram, to denote formal parameters and their
276 -- modes. As with the 'z' and 'p' entries, each such entry is
277 -- an implicit reference to an entity with a different name.
279 -- [..] is used for generic instantiation references. These
280 -- references are present only if the entity in question is
281 -- a generic entity, and in that case the [..] contains the
282 -- reference for the instantiation. In the case of nested
283 -- instantiations, this can be nested [...[...[...]]] etc.
284 -- The reference is of the form [file|line] no column is
285 -- present since it is assumed that only one instantiation
286 -- appears on a single source line. Note that the appearence
287 -- of file numbers in such references follows the normal
288 -- rules (present only if needed, and resets the current
289 -- file for subsequent references).
293 -- 44B5*Flag_Type{boolean} 5r23 6m45 3|9r35 11r56
295 -- This line gives references for the publicly visible Boolean
296 -- type Flag_Type declared on line 44, column 5. There are four
299 -- a reference on line 5, column 23 of the current file
301 -- a modification on line 6, column 45 of the current file
303 -- a reference on line 9, column 35 of unit number 3
305 -- a reference on line 11, column 56 of unit number 3
307 -- 2U13 p3=2:35 5b13 8r4 12r13 12t15
309 -- This line gives references for the non-publicly visible
310 -- procedure p3 declared on line 2, column 13. This procedure
311 -- renames the procedure whose identifier reference is at
312 -- line 2 column 35. There are four references:
314 -- the corresponding body entity at line 5, column 13,
315 -- of the current file.
317 -- a reference (e.g. a call) at line 8 column 4 of the
318 -- of the current file.
320 -- the END line of the body has an explict reference to
321 -- the name of the procedure at line 12, column 13.
323 -- the body ends at line 12, column 15, just past this label
325 -- 16I9*My_Type<2|4I9> 18r8
327 -- This line gives references for the publicly visible Integer
328 -- derived type My_Type declared on line 16, column 9. It also
329 -- gives references to the parent type declared in the unit
330 -- number 2 on line 4, column 9. There is one reference:
332 -- a reference (e.g. a variable declaration) at line 18 column
333 -- 4 of the current file.
335 -- 10I3*Genv{integer} 3|4I10[6|12]
337 -- This line gives a reference for the entity Genv in a generic
338 -- package. The reference in file 3, line 4, col 10, refers to
339 -- an instance of the generic where the instantiation can be
340 -- found in file 6 at line 12.
342 -- Continuation lines are used if the reference list gets too long,
343 -- a continuation line starts with a period, and then has references
344 -- continuing from the previous line. The references are sorted first
345 -- by unit, then by position in the source.
347 -- Note on handling of generic entities. The cross-reference is oriented
348 -- towards source references, so the entities in a generic instantiation
349 -- are not considered distinct from the entities in the template. All
350 -- definitions and references from generic instantiations are suppressed,
351 -- since they will be generated from the template. Any references to
352 -- entities in a generic instantiation from outside the instantiation
353 -- are considered to be references to the original template entity.
355 ----------------------------------------
356 -- Cross-Reference Entity Identifiers --
357 ----------------------------------------
359 -- In the cross-reference section of the ali file, entity types are
360 -- identified by a single letter, indicating the entity type. The
361 -- following table indicates the letter. A space for an entry is
362 -- used for entities that do not appear in the cross-reference table.
364 -- For objects, the character * appears in this table. In the xref
365 -- listing, this character is replaced by the lower case letter that
366 -- corresponds to the type of the object. For example, if a variable
367 -- is of a Float type, then, since the type is represented by an
368 -- upper case F, the object would be represented by a lower case f.
370 -- A special exception is the case of booleans, whose entities are
371 -- normal E_Enumeration_Type or E_Enumeration_Subtype entities, but
372 -- which appear as B/b in the xref lines, rather than E/e.
374 -- For private types, the character + appears in the table. In this
375 -- case the kind of the underlying type is used, if available, to
376 -- determine the character to use in the xref listing. The listing
377 -- will still include a '+' for a generic private type, for example,
378 -- but will retain the '*' for an object or formal parameter of such
381 -- For subprograms, the characters 'U' and 'V' appear in the table,
382 -- indicating procedures and functions. If the operation is abstract,
383 -- these letters are replaced in the xref by 'x' and 'y' respectively.
385 Xref_Entity_Letters : array (Entity_Kind) of Character :=
390 E_Discriminant => '*',
392 E_Loop_Parameter => '*',
393 E_In_Parameter => '*',
394 E_Out_Parameter => '*',
395 E_In_Out_Parameter => '*',
396 E_Generic_In_Out_Parameter => '*',
398 E_Generic_In_Parameter => '*',
399 E_Named_Integer => 'N',
401 E_Enumeration_Type => 'E', -- B for boolean
402 E_Enumeration_Subtype => 'E', -- B for boolean
404 E_Signed_Integer_Type => 'I',
405 E_Signed_Integer_Subtype => 'I',
406 E_Modular_Integer_Type => 'M',
407 E_Modular_Integer_Subtype => 'M',
408 E_Ordinary_Fixed_Point_Type => 'O',
410 E_Ordinary_Fixed_Point_Subtype => 'O',
411 E_Decimal_Fixed_Point_Type => 'D',
412 E_Decimal_Fixed_Point_Subtype => 'D',
413 E_Floating_Point_Type => 'F',
414 E_Floating_Point_Subtype => 'F',
416 E_Access_Type => 'P',
417 E_Access_Subtype => 'P',
418 E_Access_Attribute_Type => 'P',
419 E_Allocator_Type => ' ',
420 E_General_Access_Type => 'P',
422 E_Access_Subprogram_Type => 'P',
423 E_Access_Protected_Subprogram_Type => 'P',
424 E_Anonymous_Access_Subprogram_Type => ' ',
425 E_Anonymous_Access_Protected_Subprogram_Type => ' ',
426 E_Anonymous_Access_Type => ' ',
429 E_Array_Subtype => 'A',
430 E_String_Type => 'S',
431 E_String_Subtype => 'S',
432 E_String_Literal_Subtype => ' ',
434 E_Class_Wide_Type => 'C',
435 E_Class_Wide_Subtype => 'C',
436 E_Record_Type => 'R',
437 E_Record_Subtype => 'R',
438 E_Record_Type_With_Private => 'R',
440 E_Record_Subtype_With_Private => 'R',
441 E_Private_Type => '+',
442 E_Private_Subtype => '+',
443 E_Limited_Private_Type => '+',
444 E_Limited_Private_Subtype => '+',
446 E_Incomplete_Type => '+',
447 E_Incomplete_Subtype => '+',
449 E_Task_Subtype => 'T',
450 E_Protected_Type => 'W',
452 E_Protected_Subtype => 'W',
453 E_Exception_Type => ' ',
454 E_Subprogram_Type => ' ',
455 E_Enumeration_Literal => 'n',
461 E_Entry_Family => 'Y',
464 E_Entry_Index_Parameter => '*',
466 E_Generic_Function => 'v',
467 E_Generic_Package => 'k',
468 E_Generic_Procedure => 'u',
472 E_Return_Statement => ' ',
475 -- The following entities are not ones to which we gather
476 -- cross-references, since it does not make sense to do so
477 -- (e.g. references to a package are to the spec, not the body)
478 -- Indeed the occurrence of the body entity is considered to
479 -- be a reference to the spec entity.
481 E_Package_Body => ' ',
482 E_Protected_Object => ' ',
483 E_Protected_Body => ' ',
485 E_Subprogram_Body => ' ');
487 -- The following table is for information purposes. It shows the
488 -- use of each character appearing as an entity type.
490 -- letter lower case usage UPPER CASE USAGE
492 -- a array object (except string) array type (except string)
493 -- b Boolean object Boolean type
494 -- c class-wide object class-wide type
495 -- d decimal fixed-point object decimal fixed-point type
496 -- e non-Boolean enumeration object non_Boolean enumeration type
497 -- f floating-point object floating-point type
498 -- g (unused) (unused)
499 -- h Interface (Ada 2005) Abstract type
500 -- i signed integer object signed integer type
501 -- j (unused) (unused)
502 -- k generic package package
503 -- l label on loop label on statement
504 -- m modular integer object modular integer type
505 -- n enumeration literal named number
506 -- o ordinary fixed-point object ordinary fixed-point type
507 -- p access object access type
508 -- q label on block (unused)
509 -- r record object record type
510 -- s string object string type
511 -- t task object task type
512 -- u generic procedure procedure
513 -- v generic function or operator function or operator
514 -- w protected object protected type
515 -- x abstract procedure exception
516 -- y abstract function entry or entry family
517 -- z generic formal parameter (unused)
519 --------------------------------------
520 -- Handling of Imported Subprograms --
521 --------------------------------------
523 -- If a pragma Import or Interface applies to a subprogram, the
524 -- pragma is the completion of the subprogram. This is noted in
525 -- the ALI file by making the occurrence of the subprogram in the
526 -- pragma into a body reference ('b') and by including the external
527 -- name of the subprogram and its language, bracketed by '<' and '>'
528 -- in that reference. For example:
530 -- 3U13*elsewhere 4b<c,there>21
532 -- indicates that procedure elsewhere, declared at line 3, has a
533 -- pragma Import at line 4, that its body is in C, and that the link
534 -- name as given in the pragma is "there".
540 procedure Generate_Definition (E : Entity_Id);
541 -- Records the definition of an entity
543 procedure Generate_Operator_Reference
546 -- Node N is an operator node, whose entity has been set. If this entity
547 -- is a user defined operator (i.e. an operator not defined in package
548 -- Standard), then a reference to the operator is recorded at node N.
549 -- T is the operand type of of the operator. A reference to the operator
550 -- is an implicit reference to the type, and that needs to be recorded
551 -- to avoid spurious warnings on unused entities, when the operator is
552 -- a renaming of a predefined operator.
554 procedure Generate_Reference
557 Typ : Character := 'r';
558 Set_Ref : Boolean := True;
559 Force : Boolean := False);
560 -- This procedure is called to record a reference. N is the location
561 -- of the reference and E is the referenced entity. Typ is one of:
564 -- 'c' completion of incomplete or private type (see below)
565 -- 'e' end of construct
566 -- 'i' implicit reference
567 -- 'l' label on end line
569 -- 'p' primitive operation
570 -- 'r' standard reference
572 -- 'x' type extension
573 -- ' ' dummy reference (see below)
575 -- Note: all references to incomplete or private types are to the
576 -- original (incomplete or private type) declaration. The full
577 -- declaration is treated as a reference with type 'c'.
579 -- Note: all references to packages or subprograms are to the entity
580 -- for the spec. The entity in the body is treated as a reference
581 -- with type 'b'. Similar handling for references to subprogram formals.
583 -- The call has no effect if N is not in the extended main source unit
584 -- This check is omitted for type 'e' references (where it is useful to
585 -- have structural scoping information for other than the main source),
586 -- and for 'p' (since we want to pick up inherited primitive operations
587 -- that are defined in other packages).
589 -- The call also has no effect if any of the following conditions hold:
591 -- cross-reference collection is disabled
592 -- entity does not come from source (and Force is False)
593 -- reference does not come from source (and Force is False)
594 -- the entity is not one for which xrefs are appropriate
595 -- the type letter is blank
596 -- the node N is not an identifier, defining identifier, or expanded name
597 -- the type is 'p' and the entity is not in the extended main source
599 -- If all these conditions are met, then the Is_Referenced flag of E
600 -- is set (unless Set_Ref is False) and a cross-reference entry is
601 -- recorded for later output when Output_References is called.
603 -- Note: the dummy space entry is for the convenience of some callers,
604 -- who find it easier to pass a space to suppress the entry than to do
605 -- a specific test. The call has no effect if the type is a space.
607 -- The parameter Set_Ref is normally True, and indicates that in
608 -- addition to generating a cross-reference, the Referenced flag
609 -- of the specified entity should be set. If this parameter is
610 -- False, then setting of the Referenced flag is inhibited.
612 -- The parameter Force is set to True to force a reference to be
613 -- generated even if Comes_From_Source is false. This is used for
614 -- certain implicit references, and also for end label references.
616 procedure Generate_Reference_To_Formals (E : Entity_Id);
617 -- Add a reference to the definition of each formal on the line for
620 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id);
621 -- Add a reference to the definition of each generic formal on the line
622 -- for a generic unit.
624 procedure Output_References;
625 -- Output references to the current ali file
627 procedure Initialize;
628 -- Initialize internal tables