1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-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 with Atree; use Atree;
27 with Csets; use Csets;
28 with Elists; use Elists;
29 with Errout; use Errout;
30 with Nlists; use Nlists;
32 with Restrict; use Restrict;
33 with Rident; use Rident;
35 with Sem_Aux; use Sem_Aux;
36 with Sem_Prag; use Sem_Prag;
37 with Sem_Util; use Sem_Util;
38 with Sem_Warn; use Sem_Warn;
39 with Sinfo; use Sinfo;
40 with Sinput; use Sinput;
41 with Snames; use Snames;
42 with Stringt; use Stringt;
43 with Stand; use Stand;
44 with Table; use Table;
46 with GNAT.Heap_Sort_G;
49 package body Lib.Xref is
55 -- The Xref table is used to record references. The Loc field is set
56 -- to No_Location for a definition entry.
58 subtype Xref_Entry_Number is Int;
60 type Xref_Key is record
61 -- These are the components of Xref_Entry that participate in hash
65 -- Entity referenced (E parameter to Generate_Reference)
68 -- Location of reference (Original_Location (Sloc field of N parameter
69 -- to Generate_Reference). Set to No_Location for the case of a
70 -- defining occurrence.
73 -- Reference type (Typ param to Generate_Reference)
75 Eun : Unit_Number_Type;
76 -- Unit number corresponding to Ent
78 Lun : Unit_Number_Type;
79 -- Unit number corresponding to Loc. Value is undefined and not
80 -- referenced if Loc is set to No_Location.
82 -- The following components are only used for Alfa cross-references
84 Ref_Scope : Entity_Id;
85 -- Entity of the closest subprogram or package enclosing the reference
87 Ent_Scope : Entity_Id;
88 -- Entity of the closest subprogram or package enclosing the definition,
89 -- which should be located in the same file as the definition itself.
92 type Xref_Entry is record
95 Ent_Scope_File : Unit_Number_Type;
96 -- File for entity Ent_Scope
99 -- Original source location for entity being referenced. Note that these
100 -- values are used only during the output process, they are not set when
101 -- the entries are originally built. This is because private entities
102 -- can be swapped when the initial call is made.
104 HTable_Next : Xref_Entry_Number;
105 -- For use only by Static_HTable
108 package Xrefs is new Table.Table (
109 Table_Component_Type => Xref_Entry,
110 Table_Index_Type => Xref_Entry_Number,
111 Table_Low_Bound => 1,
112 Table_Initial => Alloc.Xrefs_Initial,
113 Table_Increment => Alloc.Xrefs_Increment,
114 Table_Name => "Xrefs");
120 -- We keep a set of xref entries, in order to avoid inserting duplicate
121 -- entries into the above Xrefs table. An entry is in Xref_Set if and only
122 -- if it is in Xrefs.
124 Num_Buckets : constant := 2**16;
126 subtype Header_Num is Integer range 0 .. Num_Buckets - 1;
127 type Null_Type is null record;
128 pragma Unreferenced (Null_Type);
130 function Hash (F : Xref_Entry_Number) return Header_Num;
132 function Equal (F1, F2 : Xref_Entry_Number) return Boolean;
134 procedure HT_Set_Next (E : Xref_Entry_Number; Next : Xref_Entry_Number);
136 function HT_Next (E : Xref_Entry_Number) return Xref_Entry_Number;
138 function Get_Key (E : Xref_Entry_Number) return Xref_Entry_Number;
140 pragma Inline (Hash, Equal, HT_Set_Next, HT_Next, Get_Key);
142 package Xref_Set is new GNAT.HTable.Static_HTable (
144 Element => Xref_Entry,
145 Elmt_Ptr => Xref_Entry_Number,
147 Set_Next => HT_Set_Next,
149 Key => Xref_Entry_Number,
154 ----------------------
155 -- Alfa Information --
156 ----------------------
158 package body Alfa is separate;
160 ------------------------
161 -- Local Subprograms --
162 ------------------------
164 procedure Generate_Prim_Op_References (Typ : Entity_Id);
165 -- For a tagged type, generate implicit references to its primitive
166 -- operations, for source navigation. This is done right before emitting
167 -- cross-reference information rather than at the freeze point of the type
168 -- in order to handle late bodies that are primitive operations.
170 function Lt (T1, T2 : Xref_Entry) return Boolean;
171 -- Order cross-references
173 procedure Add_Entry (Key : Xref_Key; Ent_Scope_File : Unit_Number_Type);
174 -- Add an entry to the tables of Xref_Entries, avoiding duplicates
180 procedure Add_Entry (Key : Xref_Key; Ent_Scope_File : Unit_Number_Type) is
182 Xrefs.Increment_Last; -- tentative
183 Xrefs.Table (Xrefs.Last).Key := Key;
185 -- Set the entry in Xref_Set, and if newly set, keep the above
186 -- tentative increment.
188 if Xref_Set.Set_If_Not_Present (Xrefs.Last) then
189 Xrefs.Table (Xrefs.Last).Ent_Scope_File := Ent_Scope_File;
190 -- Leave Def and HTable_Next uninitialized
192 Set_Has_Xref_Entry (Key.Ent);
194 -- It was already in Xref_Set, so throw away the tentatively-added
198 Xrefs.Decrement_Last;
206 function Equal (F1, F2 : Xref_Entry_Number) return Boolean is
207 Result : constant Boolean :=
208 Xrefs.Table (F1).Key = Xrefs.Table (F2).Key;
213 -------------------------
214 -- Generate_Definition --
215 -------------------------
217 procedure Generate_Definition (E : Entity_Id) is
219 pragma Assert (Nkind (E) in N_Entity);
221 -- Note that we do not test Xref_Entity_Letters here. It is too early
222 -- to do so, since we are often called before the entity is fully
223 -- constructed, so that the Ekind is still E_Void.
227 -- Definition must come from source
229 -- We make an exception for subprogram child units that have no spec.
230 -- For these we generate a subprogram declaration for library use,
231 -- and the corresponding entity does not come from source.
232 -- Nevertheless, all references will be attached to it and we have
233 -- to treat is as coming from user code.
235 and then (Comes_From_Source (E) or else Is_Child_Unit (E))
237 -- And must have a reasonable source location that is not
238 -- within an instance (all entities in instances are ignored)
240 and then Sloc (E) > No_Location
241 and then Instantiation_Location (Sloc (E)) = No_Location
243 -- And must be a non-internal name from the main source unit
245 and then In_Extended_Main_Source_Unit (E)
246 and then not Is_Internal_Name (Chars (E))
252 Eun => Get_Source_Unit (Original_Location (Sloc (E))),
256 Ent_Scope_File => No_Unit);
258 if In_Inlined_Body then
262 end Generate_Definition;
264 ---------------------------------
265 -- Generate_Operator_Reference --
266 ---------------------------------
268 procedure Generate_Operator_Reference
273 if not In_Extended_Main_Source_Unit (N) then
277 -- If the operator is not a Standard operator, then we generate a real
278 -- reference to the user defined operator.
280 if Sloc (Entity (N)) /= Standard_Location then
281 Generate_Reference (Entity (N), N);
283 -- A reference to an implicit inequality operator is also a reference
284 -- to the user-defined equality.
286 if Nkind (N) = N_Op_Ne
287 and then not Comes_From_Source (Entity (N))
288 and then Present (Corresponding_Equality (Entity (N)))
290 Generate_Reference (Corresponding_Equality (Entity (N)), N);
293 -- For the case of Standard operators, we mark the result type as
294 -- referenced. This ensures that in the case where we are using a
295 -- derived operator, we mark an entity of the unit that implicitly
296 -- defines this operator as used. Otherwise we may think that no entity
297 -- of the unit is used. The actual entity marked as referenced is the
298 -- first subtype, which is the relevant user defined entity.
300 -- Note: we only do this for operators that come from source. The
301 -- generated code sometimes reaches for entities that do not need to be
302 -- explicitly visible (for example, when we expand the code for
303 -- comparing two record objects, the fields of the record may not be
306 elsif Comes_From_Source (N) then
307 Set_Referenced (First_Subtype (T));
309 end Generate_Operator_Reference;
311 ---------------------------------
312 -- Generate_Prim_Op_References --
313 ---------------------------------
315 procedure Generate_Prim_Op_References (Typ : Entity_Id) is
318 Prim_List : Elist_Id;
321 -- Handle subtypes of synchronized types
323 if Ekind (Typ) = E_Protected_Subtype
324 or else Ekind (Typ) = E_Task_Subtype
326 Base_T := Etype (Typ);
331 -- References to primitive operations are only relevant for tagged types
333 if not Is_Tagged_Type (Base_T)
334 or else Is_Class_Wide_Type (Base_T)
339 -- Ada 2005 (AI-345): For synchronized types generate reference to the
340 -- wrapper that allow us to dispatch calls through their implemented
341 -- abstract interface types.
343 -- The check for Present here is to protect against previously reported
346 Prim_List := Primitive_Operations (Base_T);
348 if No (Prim_List) then
352 Prim := First_Elmt (Prim_List);
353 while Present (Prim) loop
355 -- If the operation is derived, get the original for cross-reference
356 -- reference purposes (it is the original for which we want the xref
357 -- and for which the comes_from_source test must be performed).
360 (Typ, Ultimate_Alias (Node (Prim)), 'p', Set_Ref => False);
363 end Generate_Prim_Op_References;
365 ------------------------
366 -- Generate_Reference --
367 ------------------------
369 procedure Generate_Reference
372 Typ : Character := 'r';
373 Set_Ref : Boolean := True;
374 Force : Boolean := False)
381 Actual_Typ : Character := Typ;
383 Ref_Scope : Entity_Id;
384 Ent_Scope : Entity_Id;
385 Ent_Scope_File : Unit_Number_Type;
389 -- Used for call to Find_Actual
392 -- If Formal is non-Empty, then its Ekind, otherwise E_Void
394 function Get_Through_Renamings (E : Entity_Id) return Entity_Id;
395 -- Get the enclosing entity through renamings, which may come from
396 -- source or from the translation of generic instantiations.
398 function Is_On_LHS (Node : Node_Id) return Boolean;
399 -- Used to check if a node is on the left hand side of an assignment.
400 -- The following cases are handled:
402 -- Variable Node is a direct descendant of left hand side of an
403 -- assignment statement.
405 -- Prefix Of an indexed or selected component that is present in
406 -- a subtree rooted by an assignment statement. There is
407 -- no restriction of nesting of components, thus cases
408 -- such as A.B (C).D are handled properly. However a prefix
409 -- of a dereference (either implicit or explicit) is never
410 -- considered as on a LHS.
412 -- Out param Same as above cases, but OUT parameter
414 function OK_To_Set_Referenced return Boolean;
415 -- Returns True if the Referenced flag can be set. There are a few
416 -- exceptions where we do not want to set this flag, see body for
417 -- details of these exceptional cases.
419 ---------------------------
420 -- Get_Through_Renamings --
421 ---------------------------
423 function Get_Through_Renamings (E : Entity_Id) return Entity_Id is
424 Result : Entity_Id := E;
426 while Present (Result)
427 and then Is_Object (Result)
428 and then Present (Renamed_Object (Result))
430 Result := Get_Enclosing_Object (Renamed_Object (Result));
433 end Get_Through_Renamings;
439 -- ??? There are several routines here and there that perform a similar
440 -- (but subtly different) computation, which should be factored:
442 -- Sem_Util.May_Be_Lvalue
443 -- Sem_Util.Known_To_Be_Assigned
444 -- Exp_Ch2.Expand_Entry_Parameter.In_Assignment_Context
445 -- Exp_Smem.Is_Out_Actual
447 function Is_On_LHS (Node : Node_Id) return Boolean is
453 -- Only identifiers are considered, is this necessary???
455 if Nkind (Node) /= N_Identifier then
459 -- Immediate return if appeared as OUT parameter
461 if Kind = E_Out_Parameter then
465 -- Search for assignment statement subtree root
472 if K = N_Assignment_Statement then
475 -- Check whether the parent is a component and the current node is
476 -- its prefix, but return False if the current node has an access
477 -- type, as in that case the selected or indexed component is an
478 -- implicit dereference, and the LHS is the designated object, not
479 -- the access object.
481 -- ??? case of a slice assignment?
483 -- ??? Note that in some cases this is called too early
484 -- (see comments in Sem_Ch8.Find_Direct_Name), at a point where
485 -- the tree is not fully typed yet. In that case we may lack
486 -- an Etype for N, and we must disable the check for an implicit
487 -- dereference. If the dereference is on an LHS, this causes a
490 elsif (K = N_Selected_Component or else K = N_Indexed_Component)
491 and then Prefix (P) = N
492 and then not (Present (Etype (N))
494 Is_Access_Type (Etype (N)))
498 -- All other cases, definitely not on left side
506 ---------------------------
507 -- OK_To_Set_Referenced --
508 ---------------------------
510 function OK_To_Set_Referenced return Boolean is
514 -- A reference from a pragma Unreferenced or pragma Unmodified or
515 -- pragma Warnings does not cause the Referenced flag to be set.
516 -- This avoids silly warnings about things being referenced and
517 -- not assigned when the only reference is from the pragma.
519 if Nkind (N) = N_Identifier then
522 if Nkind (P) = N_Pragma_Argument_Association then
525 if Nkind (P) = N_Pragma then
526 if Pragma_Name (P) = Name_Warnings
528 Pragma_Name (P) = Name_Unmodified
530 Pragma_Name (P) = Name_Unreferenced
536 -- A reference to a formal in a named parameter association does
537 -- not make the formal referenced. Formals that are unused in the
538 -- subprogram body are properly flagged as such, even if calls
539 -- elsewhere use named notation.
541 elsif Nkind (P) = N_Parameter_Association
542 and then N = Selector_Name (P)
549 end OK_To_Set_Referenced;
551 -- Start of processing for Generate_Reference
554 pragma Assert (Nkind (E) in N_Entity);
555 Find_Actual (N, Formal, Call);
557 if Present (Formal) then
558 Kind := Ekind (Formal);
563 -- Check for obsolescent reference to package ASCII. GNAT treats this
564 -- element of annex J specially since in practice, programs make a lot
565 -- of use of this feature, so we don't include it in the set of features
566 -- diagnosed when Warn_On_Obsolescent_Features mode is set. However we
567 -- are required to note it as a violation of the RM defined restriction.
569 if E = Standard_ASCII then
570 Check_Restriction (No_Obsolescent_Features, N);
573 -- Check for reference to entity marked with Is_Obsolescent
575 -- Note that we always allow obsolescent references in the compiler
576 -- itself and the run time, since we assume that we know what we are
577 -- doing in such cases. For example the calls in Ada.Characters.Handling
578 -- to its own obsolescent subprograms are just fine.
580 -- In any case we only generate warnings if we are in the extended main
581 -- source unit, and the entity itself is not in the extended main source
582 -- unit, since we assume the source unit itself knows what is going on
583 -- (and for sure we do not want silly warnings, e.g. on the end line of
584 -- an obsolescent procedure body).
586 if Is_Obsolescent (E)
587 and then not GNAT_Mode
588 and then not In_Extended_Main_Source_Unit (E)
589 and then In_Extended_Main_Source_Unit (N)
591 Check_Restriction (No_Obsolescent_Features, N);
593 if Warn_On_Obsolescent_Feature then
594 Output_Obsolescent_Entity_Warnings (N, E);
598 -- Warn if reference to Ada 2005 entity not in Ada 2005 mode. We only
599 -- detect real explicit references (modifications and references).
601 if Comes_From_Source (N)
602 and then Is_Ada_2005_Only (E)
603 and then Ada_Version < Ada_2005
604 and then Warn_On_Ada_2005_Compatibility
605 and then (Typ = 'm' or else Typ = 'r' or else Typ = 's')
607 Error_Msg_NE ("& is only defined in Ada 2005?", N, E);
610 -- Warn if reference to Ada 2012 entity not in Ada 2012 mode. We only
611 -- detect real explicit references (modifications and references).
613 if Comes_From_Source (N)
614 and then Is_Ada_2012_Only (E)
615 and then Ada_Version < Ada_2012
616 and then Warn_On_Ada_2012_Compatibility
617 and then (Typ = 'm' or else Typ = 'r')
619 Error_Msg_NE ("& is only defined in Ada 2012?", N, E);
622 -- Never collect references if not in main source unit. However, we omit
623 -- this test if Typ is 'e' or 'k', since these entries are structural,
624 -- and it is useful to have them in units that reference packages as
625 -- well as units that define packages. We also omit the test for the
626 -- case of 'p' since we want to include inherited primitive operations
627 -- from other packages.
629 -- We also omit this test is this is a body reference for a subprogram
630 -- instantiation. In this case the reference is to the generic body,
631 -- which clearly need not be in the main unit containing the instance.
632 -- For the same reason we accept an implicit reference generated for
633 -- a default in an instance.
635 if not In_Extended_Main_Source_Unit (N) then
641 or else (Typ = 'b' and then Is_Generic_Instance (E))
649 -- For reference type p, the entity must be in main source unit
651 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
655 -- Unless the reference is forced, we ignore references where the
656 -- reference itself does not come from source.
658 if not Force and then not Comes_From_Source (N) then
662 -- Deal with setting entity as referenced, unless suppressed. Note that
663 -- we still do Set_Referenced on entities that do not come from source.
664 -- This situation arises when we have a source reference to a derived
665 -- operation, where the derived operation itself does not come from
666 -- source, but we still want to mark it as referenced, since we really
667 -- are referencing an entity in the corresponding package (this avoids
668 -- wrong complaints that the package contains no referenced entities).
672 -- Assignable object appearing on left side of assignment or as
676 and then Is_On_LHS (N)
677 and then Ekind (E) /= E_In_Out_Parameter
679 -- For objects that are renamings, just set as simply referenced
680 -- we do not try to do assignment type tracking in this case.
682 if Present (Renamed_Object (E)) then
685 -- Out parameter case
687 elsif Kind = E_Out_Parameter then
689 -- If warning mode for all out parameters is set, or this is
690 -- the only warning parameter, then we want to mark this for
691 -- later warning logic by setting Referenced_As_Out_Parameter
693 if Warn_On_Modified_As_Out_Parameter (Formal) then
694 Set_Referenced_As_Out_Parameter (E, True);
695 Set_Referenced_As_LHS (E, False);
697 -- For OUT parameter not covered by the above cases, we simply
698 -- regard it as a normal reference (in this case we do not
699 -- want any of the warning machinery for out parameters).
705 -- For the left hand of an assignment case, we do nothing here.
706 -- The processing for Analyze_Assignment_Statement will set the
707 -- Referenced_As_LHS flag.
713 -- Check for a reference in a pragma that should not count as a
714 -- making the variable referenced for warning purposes.
716 elsif Is_Non_Significant_Pragma_Reference (N) then
719 -- A reference in an attribute definition clause does not count as a
720 -- reference except for the case of Address. The reason that 'Address
721 -- is an exception is that it creates an alias through which the
722 -- variable may be referenced.
724 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
725 and then Chars (Parent (N)) /= Name_Address
726 and then N = Name (Parent (N))
730 -- Constant completion does not count as a reference
733 and then Ekind (E) = E_Constant
737 -- Record representation clause does not count as a reference
739 elsif Nkind (N) = N_Identifier
740 and then Nkind (Parent (N)) = N_Record_Representation_Clause
744 -- Discriminants do not need to produce a reference to record type
747 and then Nkind (Parent (N)) = N_Discriminant_Specification
754 -- Special processing for IN OUT parameters, where we have an
755 -- implicit assignment to a simple variable.
757 if Kind = E_In_Out_Parameter
758 and then Is_Assignable (E)
760 -- For sure this counts as a normal read reference
763 Set_Last_Assignment (E, Empty);
765 -- We count it as being referenced as an out parameter if the
766 -- option is set to warn on all out parameters, except that we
767 -- have a special exclusion for an intrinsic subprogram, which
768 -- is most likely an instantiation of Unchecked_Deallocation
769 -- which we do not want to consider as an assignment since it
770 -- generates false positives. We also exclude the case of an
771 -- IN OUT parameter if the name of the procedure is Free,
772 -- since we suspect similar semantics.
774 if Warn_On_All_Unread_Out_Parameters
775 and then Is_Entity_Name (Name (Call))
776 and then not Is_Intrinsic_Subprogram (Entity (Name (Call)))
777 and then Chars (Name (Call)) /= Name_Free
779 Set_Referenced_As_Out_Parameter (E, True);
780 Set_Referenced_As_LHS (E, False);
783 -- Don't count a recursive reference within a subprogram as a
784 -- reference (that allows detection of a recursive subprogram
785 -- whose only references are recursive calls as unreferenced).
787 elsif Is_Subprogram (E)
788 and then E = Nearest_Dynamic_Scope (Current_Scope)
792 -- Any other occurrence counts as referencing the entity
794 elsif OK_To_Set_Referenced then
797 -- If variable, this is an OK reference after an assignment
798 -- so we can clear the Last_Assignment indication.
800 if Is_Assignable (E) then
801 Set_Last_Assignment (E, Empty);
806 -- Check for pragma Unreferenced given and reference is within
807 -- this source unit (occasion for possible warning to be issued).
809 if Has_Unreferenced (E)
810 and then In_Same_Extended_Unit (E, N)
812 -- A reference as a named parameter in a call does not count
813 -- as a violation of pragma Unreferenced for this purpose...
815 if Nkind (N) = N_Identifier
816 and then Nkind (Parent (N)) = N_Parameter_Association
817 and then Selector_Name (Parent (N)) = N
821 -- ... Neither does a reference to a variable on the left side
824 elsif Is_On_LHS (N) then
827 -- For entry formals, we want to place the warning message on the
828 -- corresponding entity in the accept statement. The current scope
829 -- is the body of the accept, so we find the formal whose name
830 -- matches that of the entry formal (there is no link between the
831 -- two entities, and the one in the accept statement is only used
832 -- for conformance checking).
834 elsif Ekind (Scope (E)) = E_Entry then
839 BE := First_Entity (Current_Scope);
840 while Present (BE) loop
841 if Chars (BE) = Chars (E) then
842 Error_Msg_NE -- CODEFIX
843 ("?pragma Unreferenced given for&!", N, BE);
851 -- Here we issue the warning, since this is a real reference
854 Error_Msg_NE -- CODEFIX
855 ("?pragma Unreferenced given for&!", N, E);
859 -- If this is a subprogram instance, mark as well the internal
860 -- subprogram in the wrapper package, which may be a visible
863 if Is_Overloadable (E)
864 and then Is_Generic_Instance (E)
865 and then Present (Alias (E))
867 Set_Referenced (Alias (E));
871 -- Generate reference if all conditions are met:
874 -- Cross referencing must be active
878 -- The entity must be one for which we collect references
880 and then Xref_Entity_Letters (Ekind (E)) /= ' '
882 -- Both Sloc values must be set to something sensible
884 and then Sloc (E) > No_Location
885 and then Sloc (N) > No_Location
887 -- We ignore references from within an instance, except for default
888 -- subprograms, for which we generate an implicit reference.
891 (Instantiation_Location (Sloc (N)) = No_Location or else Typ = 'i')
893 -- Ignore dummy references
897 if Nkind (N) = N_Identifier
899 Nkind (N) = N_Defining_Identifier
903 Nkind (N) = N_Defining_Operator_Symbol
905 Nkind (N) = N_Operator_Symbol
907 (Nkind (N) = N_Character_Literal
908 and then Sloc (Entity (N)) /= Standard_Location)
910 Nkind (N) = N_Defining_Character_Literal
914 elsif Nkind (N) = N_Expanded_Name
916 Nkind (N) = N_Selected_Component
918 Nod := Selector_Name (N);
924 -- Normal case of source entity comes from source
926 if Comes_From_Source (E) then
929 -- Entity does not come from source, but is a derived subprogram and
930 -- the derived subprogram comes from source (after one or more
931 -- derivations) in which case the reference is to parent subprogram.
933 elsif Is_Overloadable (E)
934 and then Present (Alias (E))
937 while not Comes_From_Source (Ent) loop
938 if No (Alias (Ent)) then
945 -- The internally created defining entity for a child subprogram
946 -- that has no previous spec has valid references.
948 elsif Is_Overloadable (E)
949 and then Is_Child_Unit (E)
953 -- Record components of discriminated subtypes or derived types must
954 -- be treated as references to the original component.
956 elsif Ekind (E) = E_Component
957 and then Comes_From_Source (Original_Record_Component (E))
959 Ent := Original_Record_Component (E);
961 -- If this is an expanded reference to a discriminant, recover the
962 -- original discriminant, which gets the reference.
964 elsif Ekind (E) = E_In_Parameter
965 and then Present (Discriminal_Link (E))
967 Ent := Discriminal_Link (E);
968 Set_Referenced (Ent);
970 -- Ignore reference to any other entity that is not from source
976 -- In Alfa mode, consider the underlying entity renamed instead of
977 -- the renaming, which is needed to compute a valid set of effects
978 -- (reads, writes) for the enclosing subprogram.
981 Ent := Get_Through_Renamings (Ent);
983 -- If no enclosing object, then it could be a reference to any
984 -- location not tracked individually, like heap-allocated data.
985 -- Conservatively approximate this possibility by generating a
986 -- dereference, and return.
989 if Actual_Typ = 'w' then
990 Alfa.Generate_Dereference (Nod, 'r');
991 Alfa.Generate_Dereference (Nod, 'w');
993 Alfa.Generate_Dereference (Nod, 'r');
1000 -- Record reference to entity
1002 Ref := Original_Location (Sloc (Nod));
1003 Def := Original_Location (Sloc (Ent));
1006 and then Is_Subprogram (N)
1007 and then Present (Overridden_Operation (N))
1013 Ref_Scope := Alfa.Enclosing_Subprogram_Or_Package (N);
1014 Ent_Scope := Alfa.Enclosing_Subprogram_Or_Package (Ent);
1016 -- Since we are reaching through renamings in Alfa mode, we may
1017 -- end up with standard constants. Ignore those.
1019 if Sloc (Ent_Scope) <= Standard_Location
1020 or else Def <= Standard_Location
1025 Ent_Scope_File := Get_Source_Unit (Ent_Scope);
1029 Ent_Scope_File := No_Unit;
1036 Eun => Get_Source_Unit (Def),
1037 Lun => Get_Source_Unit (Ref),
1038 Ref_Scope => Ref_Scope,
1039 Ent_Scope => Ent_Scope),
1040 Ent_Scope_File => Ent_Scope_File);
1042 end Generate_Reference;
1044 -----------------------------------
1045 -- Generate_Reference_To_Formals --
1046 -----------------------------------
1048 procedure Generate_Reference_To_Formals (E : Entity_Id) is
1052 if Is_Generic_Subprogram (E) then
1053 Formal := First_Entity (E);
1055 while Present (Formal)
1056 and then not Is_Formal (Formal)
1058 Next_Entity (Formal);
1062 Formal := First_Formal (E);
1065 while Present (Formal) loop
1066 if Ekind (Formal) = E_In_Parameter then
1068 if Nkind (Parameter_Type (Parent (Formal)))
1069 = N_Access_Definition
1071 Generate_Reference (E, Formal, '^', False);
1073 Generate_Reference (E, Formal, '>', False);
1076 elsif Ekind (Formal) = E_In_Out_Parameter then
1077 Generate_Reference (E, Formal, '=', False);
1080 Generate_Reference (E, Formal, '<', False);
1083 Next_Formal (Formal);
1085 end Generate_Reference_To_Formals;
1087 -------------------------------------------
1088 -- Generate_Reference_To_Generic_Formals --
1089 -------------------------------------------
1091 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
1095 Formal := First_Entity (E);
1096 while Present (Formal) loop
1097 if Comes_From_Source (Formal) then
1098 Generate_Reference (E, Formal, 'z', False);
1101 Next_Entity (Formal);
1103 end Generate_Reference_To_Generic_Formals;
1109 function Get_Key (E : Xref_Entry_Number) return Xref_Entry_Number is
1118 function Hash (F : Xref_Entry_Number) return Header_Num is
1119 -- It is unlikely to have two references to the same entity at the same
1120 -- source location, so the hash function depends only on the Ent and Loc
1123 XE : Xref_Entry renames Xrefs.Table (F);
1124 type M is mod 2**32;
1126 H : constant M := M (XE.Key.Ent) + 2 ** 7 * M (abs XE.Key.Loc);
1127 -- It would be more natural to write:
1129 -- H : constant M := M'Mod (XE.Key.Ent) + 2**7 * M'Mod (XE.Key.Loc);
1131 -- But we can't use M'Mod, because it prevents bootstrapping with older
1132 -- compilers. Loc can be negative, so we do "abs" before converting.
1133 -- One day this can be cleaned up ???
1136 return Header_Num (H mod Num_Buckets);
1143 procedure HT_Set_Next (E : Xref_Entry_Number; Next : Xref_Entry_Number) is
1145 Xrefs.Table (E).HTable_Next := Next;
1152 function HT_Next (E : Xref_Entry_Number) return Xref_Entry_Number is
1154 return Xrefs.Table (E).HTable_Next;
1161 procedure Initialize is
1170 function Lt (T1, T2 : Xref_Entry) return Boolean is
1172 -- First test: if entity is in different unit, sort by unit
1174 if T1.Key.Eun /= T2.Key.Eun then
1175 return Dependency_Num (T1.Key.Eun) < Dependency_Num (T2.Key.Eun);
1177 -- Second test: within same unit, sort by entity Sloc
1179 elsif T1.Def /= T2.Def then
1180 return T1.Def < T2.Def;
1182 -- Third test: sort definitions ahead of references
1184 elsif T1.Key.Loc = No_Location then
1187 elsif T2.Key.Loc = No_Location then
1190 -- Fourth test: for same entity, sort by reference location unit
1192 elsif T1.Key.Lun /= T2.Key.Lun then
1193 return Dependency_Num (T1.Key.Lun) < Dependency_Num (T2.Key.Lun);
1195 -- Fifth test: order of location within referencing unit
1197 elsif T1.Key.Loc /= T2.Key.Loc then
1198 return T1.Key.Loc < T2.Key.Loc;
1200 -- Finally, for two locations at the same address, we prefer
1201 -- the one that does NOT have the type 'r' so that a modification
1202 -- or extension takes preference, when there are more than one
1203 -- reference at the same location. As a result, in the case of
1204 -- entities that are in-out actuals, the read reference follows
1205 -- the modify reference.
1208 return T2.Key.Typ = 'r';
1212 -----------------------
1213 -- Output_References --
1214 -----------------------
1216 procedure Output_References is
1218 procedure Get_Type_Reference
1220 Tref : out Entity_Id;
1221 Left : out Character;
1222 Right : out Character);
1223 -- Given an Entity_Id Ent, determines whether a type reference is
1224 -- required. If so, Tref is set to the entity for the type reference
1225 -- and Left and Right are set to the left/right brackets to be output
1226 -- for the reference. If no type reference is required, then Tref is
1227 -- set to Empty, and Left/Right are set to space.
1229 procedure Output_Import_Export_Info (Ent : Entity_Id);
1230 -- Output language and external name information for an interfaced
1231 -- entity, using the format <language, external_name>.
1233 ------------------------
1234 -- Get_Type_Reference --
1235 ------------------------
1237 procedure Get_Type_Reference
1239 Tref : out Entity_Id;
1240 Left : out Character;
1241 Right : out Character)
1246 -- See if we have a type reference
1255 -- Processing for types
1257 if Is_Type (Tref) then
1259 -- Case of base type
1261 if Base_Type (Tref) = Tref then
1263 -- If derived, then get first subtype
1265 if Tref /= Etype (Tref) then
1266 Tref := First_Subtype (Etype (Tref));
1268 -- Set brackets for derived type, but don't override
1269 -- pointer case since the fact that something is a
1270 -- pointer is more important.
1277 -- If non-derived ptr, get directly designated type.
1278 -- If the type has a full view, all references are on the
1279 -- partial view, that is seen first.
1281 elsif Is_Access_Type (Tref) then
1282 Tref := Directly_Designated_Type (Tref);
1286 elsif Is_Private_Type (Tref)
1287 and then Present (Full_View (Tref))
1289 if Is_Access_Type (Full_View (Tref)) then
1290 Tref := Directly_Designated_Type (Full_View (Tref));
1294 -- If the full view is an array type, we also retrieve
1295 -- the corresponding component type, because the ali
1296 -- entry already indicates that this is an array.
1298 elsif Is_Array_Type (Full_View (Tref)) then
1299 Tref := Component_Type (Full_View (Tref));
1304 -- If non-derived array, get component type. Skip component
1305 -- type for case of String or Wide_String, saves worthwhile
1308 elsif Is_Array_Type (Tref)
1309 and then Tref /= Standard_String
1310 and then Tref /= Standard_Wide_String
1312 Tref := Component_Type (Tref);
1316 -- For other non-derived base types, nothing
1322 -- For a subtype, go to ancestor subtype
1325 Tref := Ancestor_Subtype (Tref);
1327 -- If no ancestor subtype, go to base type
1330 Tref := Base_Type (Sav);
1334 -- For objects, functions, enum literals, just get type from
1337 elsif Is_Object (Tref)
1338 or else Ekind (Tref) = E_Enumeration_Literal
1339 or else Ekind (Tref) = E_Function
1340 or else Ekind (Tref) = E_Operator
1342 Tref := Etype (Tref);
1344 -- For anything else, exit
1350 -- Exit if no type reference, or we are stuck in some loop trying
1351 -- to find the type reference, or if the type is standard void
1352 -- type (the latter is an implementation artifact that should not
1353 -- show up in the generated cross-references).
1357 or else Tref = Standard_Void_Type;
1359 -- If we have a usable type reference, return, otherwise keep
1360 -- looking for something useful (we are looking for something
1361 -- that either comes from source or standard)
1363 if Sloc (Tref) = Standard_Location
1364 or else Comes_From_Source (Tref)
1366 -- If the reference is a subtype created for a generic actual,
1367 -- go actual directly, the inner subtype is not user visible.
1369 if Nkind (Parent (Tref)) = N_Subtype_Declaration
1370 and then not Comes_From_Source (Parent (Tref))
1372 (Is_Wrapper_Package (Scope (Tref))
1373 or else Is_Generic_Instance (Scope (Tref)))
1375 Tref := First_Subtype (Base_Type (Tref));
1382 -- If we fall through the loop, no type reference
1387 end Get_Type_Reference;
1389 -------------------------------
1390 -- Output_Import_Export_Info --
1391 -------------------------------
1393 procedure Output_Import_Export_Info (Ent : Entity_Id) is
1394 Language_Name : Name_Id;
1395 Conv : constant Convention_Id := Convention (Ent);
1398 -- Generate language name from convention
1400 if Conv = Convention_C then
1401 Language_Name := Name_C;
1403 elsif Conv = Convention_CPP then
1404 Language_Name := Name_CPP;
1406 elsif Conv = Convention_Ada then
1407 Language_Name := Name_Ada;
1410 -- For the moment we ignore all other cases ???
1415 Write_Info_Char ('<');
1416 Get_Unqualified_Name_String (Language_Name);
1418 for J in 1 .. Name_Len loop
1419 Write_Info_Char (Name_Buffer (J));
1422 if Present (Interface_Name (Ent)) then
1423 Write_Info_Char (',');
1424 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
1426 for J in 1 .. Name_Len loop
1427 Write_Info_Char (Name_Buffer (J));
1431 Write_Info_Char ('>');
1432 end Output_Import_Export_Info;
1434 -- Start of processing for Output_References
1437 -- First we add references to the primitive operations of tagged types
1438 -- declared in the main unit.
1440 Handle_Prim_Ops : declare
1444 for J in 1 .. Xrefs.Last loop
1445 Ent := Xrefs.Table (J).Key.Ent;
1448 and then Is_Tagged_Type (Ent)
1449 and then Is_Base_Type (Ent)
1450 and then In_Extended_Main_Source_Unit (Ent)
1452 Generate_Prim_Op_References (Ent);
1455 end Handle_Prim_Ops;
1457 -- Before we go ahead and output the references we have a problem
1458 -- that needs dealing with. So far we have captured things that are
1459 -- definitely referenced by the main unit, or defined in the main
1460 -- unit. That's because we don't want to clutter up the ali file
1461 -- for this unit with definition lines for entities in other units
1462 -- that are not referenced.
1464 -- But there is a glitch. We may reference an entity in another unit,
1465 -- and it may have a type reference to an entity that is not directly
1466 -- referenced in the main unit, which may mean that there is no xref
1467 -- entry for this entity yet in the list of references.
1469 -- If we don't do something about this, we will end with an orphan type
1470 -- reference, i.e. it will point to an entity that does not appear
1471 -- within the generated references in the ali file. That is not good for
1472 -- tools using the xref information.
1474 -- To fix this, we go through the references adding definition entries
1475 -- for any unreferenced entities that can be referenced in a type
1476 -- reference. There is a recursion problem here, and that is dealt with
1477 -- by making sure that this traversal also traverses any entries that
1478 -- get added by the traversal.
1480 Handle_Orphan_Type_References : declare
1486 pragma Warnings (Off, L);
1487 pragma Warnings (Off, R);
1489 procedure New_Entry (E : Entity_Id);
1490 -- Make an additional entry into the Xref table for a type entity
1491 -- that is related to the current entity (parent, type ancestor,
1492 -- progenitor, etc.).
1498 procedure New_Entry (E : Entity_Id) is
1500 pragma Assert (Present (E));
1502 if not Has_Xref_Entry (Implementation_Base_Type (E))
1503 and then Sloc (E) > No_Location
1508 Typ => Character'First,
1509 Eun => Get_Source_Unit (Original_Location (Sloc (E))),
1512 Ent_Scope => Empty),
1513 Ent_Scope_File => No_Unit);
1517 -- Start of processing for Handle_Orphan_Type_References
1520 -- Note that this is not a for loop for a very good reason. The
1521 -- processing of items in the table can add new items to the table,
1522 -- and they must be processed as well.
1525 while J <= Xrefs.Last loop
1526 Ent := Xrefs.Table (J).Key.Ent;
1527 Get_Type_Reference (Ent, Tref, L, R);
1530 and then not Has_Xref_Entry (Tref)
1531 and then Sloc (Tref) > No_Location
1535 if Is_Record_Type (Ent)
1536 and then Present (Interfaces (Ent))
1538 -- Add an entry for each one of the given interfaces
1539 -- implemented by type Ent.
1542 Elmt : Elmt_Id := First_Elmt (Interfaces (Ent));
1544 while Present (Elmt) loop
1545 New_Entry (Node (Elmt));
1552 -- Collect inherited primitive operations that may be declared in
1553 -- another unit and have no visible reference in the current one.
1556 and then Is_Tagged_Type (Ent)
1557 and then Is_Derived_Type (Ent)
1558 and then Is_Base_Type (Ent)
1559 and then In_Extended_Main_Source_Unit (Ent)
1562 Op_List : constant Elist_Id := Primitive_Operations (Ent);
1566 function Parent_Op (E : Entity_Id) return Entity_Id;
1567 -- Find original operation, which may be inherited through
1568 -- several derivations.
1570 function Parent_Op (E : Entity_Id) return Entity_Id is
1571 Orig_Op : constant Entity_Id := Alias (E);
1574 if No (Orig_Op) then
1577 elsif not Comes_From_Source (E)
1578 and then not Has_Xref_Entry (Orig_Op)
1579 and then Comes_From_Source (Orig_Op)
1583 return Parent_Op (Orig_Op);
1588 Op := First_Elmt (Op_List);
1589 while Present (Op) loop
1590 Prim := Parent_Op (Node (Op));
1592 if Present (Prim) then
1596 Typ => Character'First,
1597 Eun => Get_Source_Unit (Sloc (Prim)),
1600 Ent_Scope => Empty),
1601 Ent_Scope_File => No_Unit);
1611 end Handle_Orphan_Type_References;
1613 -- Now we have all the references, including those for any embedded
1614 -- type references, so we can sort them, and output them.
1616 Output_Refs : declare
1618 Nrefs : constant Nat := Xrefs.Last;
1619 -- Number of references in table
1621 Rnums : array (0 .. Nrefs) of Nat;
1622 -- This array contains numbers of references in the Xrefs table.
1623 -- This list is sorted in output order. The extra 0'th entry is
1624 -- convenient for the call to sort. When we sort the table, we
1625 -- move the entries in Rnums around, but we do not move the
1626 -- original table entries.
1628 Curxu : Unit_Number_Type;
1629 -- Current xref unit
1631 Curru : Unit_Number_Type;
1632 -- Current reference unit for one entity
1637 Curnam : String (1 .. Name_Buffer'Length);
1639 -- Simple name and length of current entity
1641 Curdef : Source_Ptr;
1642 -- Original source location for current entity
1645 -- Current reference location
1648 -- Entity type character
1651 -- reference kind of previous reference
1657 -- Renaming reference
1659 Trunit : Unit_Number_Type;
1660 -- Unit number for type reference
1662 function Lt (Op1, Op2 : Natural) return Boolean;
1663 -- Comparison function for Sort call
1665 function Name_Change (X : Entity_Id) return Boolean;
1666 -- Determines if entity X has a different simple name from Curent
1668 procedure Move (From : Natural; To : Natural);
1669 -- Move procedure for Sort call
1671 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
1677 function Lt (Op1, Op2 : Natural) return Boolean is
1678 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
1679 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
1689 procedure Move (From : Natural; To : Natural) is
1691 Rnums (Nat (To)) := Rnums (Nat (From));
1698 -- Why a string comparison here??? Why not compare Name_Id values???
1700 function Name_Change (X : Entity_Id) return Boolean is
1702 Get_Unqualified_Name_String (Chars (X));
1704 if Name_Len /= Curlen then
1707 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
1711 -- Start of processing for Output_Refs
1714 -- Capture the definition Sloc values. We delay doing this till now,
1715 -- since at the time the reference or definition is made, private
1716 -- types may be swapped, and the Sloc value may be incorrect. We
1717 -- also set up the pointer vector for the sort.
1719 for J in 1 .. Nrefs loop
1721 Xrefs.Table (J).Def :=
1722 Original_Location (Sloc (Xrefs.Table (J).Key.Ent));
1725 -- Sort the references
1727 Sorting.Sort (Integer (Nrefs));
1729 -- Initialize loop through references
1733 Curdef := No_Location;
1735 Crloc := No_Location;
1738 -- Loop to output references
1740 for Refno in 1 .. Nrefs loop
1741 Output_One_Ref : declare
1744 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
1745 -- The current entry to be accessed
1749 -- Used for {} or <> or () for type reference
1751 procedure Check_Type_Reference
1753 List_Interface : Boolean);
1754 -- Find whether there is a meaningful type reference for
1755 -- Ent, and display it accordingly. If List_Interface is
1756 -- true, then Ent is a progenitor interface of the current
1757 -- type entity being listed. In that case list it as is,
1758 -- without looking for a type reference for it.
1760 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
1761 -- Recursive procedure to output instantiation references for
1762 -- the given source ptr in [file|line[...]] form. No output
1763 -- if the given location is not a generic template reference.
1765 procedure Output_Overridden_Op (Old_E : Entity_Id);
1766 -- For a subprogram that is overriding, display information
1767 -- about the inherited operation that it overrides.
1769 --------------------------
1770 -- Check_Type_Reference --
1771 --------------------------
1773 procedure Check_Type_Reference
1775 List_Interface : Boolean)
1778 if List_Interface then
1780 -- This is a progenitor interface of the type for which
1781 -- xref information is being generated.
1788 Get_Type_Reference (Ent, Tref, Left, Right);
1791 if Present (Tref) then
1793 -- Case of standard entity, output name
1795 if Sloc (Tref) = Standard_Location then
1796 Write_Info_Char (Left);
1797 Write_Info_Name (Chars (Tref));
1798 Write_Info_Char (Right);
1800 -- Case of source entity, output location
1803 Write_Info_Char (Left);
1804 Trunit := Get_Source_Unit (Sloc (Tref));
1806 if Trunit /= Curxu then
1807 Write_Info_Nat (Dependency_Num (Trunit));
1808 Write_Info_Char ('|');
1812 (Int (Get_Logical_Line_Number (Sloc (Tref))));
1820 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1823 and then Present (Full_View (Ent))
1825 Ent := Underlying_Type (Ent);
1827 if Present (Ent) then
1828 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1832 Write_Info_Char (Ctyp);
1836 (Int (Get_Column_Number (Sloc (Tref))));
1838 -- If the type comes from an instantiation, add the
1839 -- corresponding info.
1841 Output_Instantiation_Refs (Sloc (Tref));
1842 Write_Info_Char (Right);
1845 end Check_Type_Reference;
1847 -------------------------------
1848 -- Output_Instantiation_Refs --
1849 -------------------------------
1851 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
1852 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
1853 Lun : Unit_Number_Type;
1854 Cu : constant Unit_Number_Type := Curru;
1857 -- Nothing to do if this is not an instantiation
1859 if Iloc = No_Location then
1863 -- Output instantiation reference
1865 Write_Info_Char ('[');
1866 Lun := Get_Source_Unit (Iloc);
1868 if Lun /= Curru then
1870 Write_Info_Nat (Dependency_Num (Curru));
1871 Write_Info_Char ('|');
1874 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
1876 -- Recursive call to get nested instantiations
1878 Output_Instantiation_Refs (Iloc);
1880 -- Output final ] after call to get proper nesting
1882 Write_Info_Char (']');
1885 end Output_Instantiation_Refs;
1887 --------------------------
1888 -- Output_Overridden_Op --
1889 --------------------------
1891 procedure Output_Overridden_Op (Old_E : Entity_Id) is
1895 -- The overridden operation has an implicit declaration
1896 -- at the point of derivation. What we want to display
1897 -- is the original operation, which has the actual body
1898 -- (or abstract declaration) that is being overridden.
1899 -- The overridden operation is not always set, e.g. when
1900 -- it is a predefined operator.
1905 -- Follow alias chain if one is present
1907 elsif Present (Alias (Old_E)) then
1909 -- The subprogram may have been implicitly inherited
1910 -- through several levels of derivation, so find the
1911 -- ultimate (source) ancestor.
1913 Op := Ultimate_Alias (Old_E);
1915 -- Normal case of no alias present. We omit generated
1916 -- primitives like tagged equality, that have no source
1924 and then Sloc (Op) /= Standard_Location
1925 and then Comes_From_Source (Op)
1928 Loc : constant Source_Ptr := Sloc (Op);
1929 Par_Unit : constant Unit_Number_Type :=
1930 Get_Source_Unit (Loc);
1933 Write_Info_Char ('<');
1935 if Par_Unit /= Curxu then
1936 Write_Info_Nat (Dependency_Num (Par_Unit));
1937 Write_Info_Char ('|');
1940 Write_Info_Nat (Int (Get_Logical_Line_Number (Loc)));
1941 Write_Info_Char ('p');
1942 Write_Info_Nat (Int (Get_Column_Number (Loc)));
1943 Write_Info_Char ('>');
1946 end Output_Overridden_Op;
1948 -- Start of processing for Output_One_Ref
1952 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1954 -- Skip reference if it is the only reference to an entity,
1955 -- and it is an END line reference, and the entity is not in
1956 -- the current extended source. This prevents junk entries
1957 -- consisting only of packages with END lines, where no
1958 -- entity from the package is actually referenced.
1961 and then Ent /= Curent
1962 and then (Refno = Nrefs
1964 Ent /= Xrefs.Table (Rnums (Refno + 1)).Key.Ent)
1965 and then not In_Extended_Main_Source_Unit (Ent)
1970 -- For private type, get full view type
1973 and then Present (Full_View (XE.Key.Ent))
1975 Ent := Underlying_Type (Ent);
1977 if Present (Ent) then
1978 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1982 -- Special exception for Boolean
1984 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
1988 -- For variable reference, get corresponding type
1991 Ent := Etype (XE.Key.Ent);
1992 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1994 -- If variable is private type, get full view type
1997 and then Present (Full_View (Etype (XE.Key.Ent)))
1999 Ent := Underlying_Type (Etype (XE.Key.Ent));
2001 if Present (Ent) then
2002 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
2005 elsif Is_Generic_Type (Ent) then
2007 -- If the type of the entity is a generic private type,
2008 -- there is no usable full view, so retain the indication
2009 -- that this is an object.
2014 -- Special handling for access parameters and objects of
2015 -- an anonymous access type.
2017 if Ekind_In (Etype (XE.Key.Ent),
2018 E_Anonymous_Access_Type,
2019 E_Anonymous_Access_Subprogram_Type,
2020 E_Anonymous_Access_Protected_Subprogram_Type)
2022 if Is_Formal (XE.Key.Ent)
2023 or else Ekind_In (XE.Key.Ent, E_Variable, E_Constant)
2028 -- Special handling for Boolean
2030 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
2035 -- Special handling for abstract types and operations
2037 if Is_Overloadable (XE.Key.Ent)
2038 and then Is_Abstract_Subprogram (XE.Key.Ent)
2041 Ctyp := 'x'; -- Abstract procedure
2043 elsif Ctyp = 'V' then
2044 Ctyp := 'y'; -- Abstract function
2047 elsif Is_Type (XE.Key.Ent)
2048 and then Is_Abstract_Type (XE.Key.Ent)
2050 if Is_Interface (XE.Key.Ent) then
2053 elsif Ctyp = 'R' then
2054 Ctyp := 'H'; -- Abstract type
2058 -- Only output reference if interesting type of entity
2062 -- Suppress references to object definitions, used for local
2065 or else XE.Key.Typ = 'D'
2066 or else XE.Key.Typ = 'I'
2068 -- Suppress self references, except for bodies that act as
2071 or else (XE.Key.Loc = XE.Def
2074 or else not Is_Subprogram (XE.Key.Ent)))
2076 -- Also suppress definitions of body formals (we only
2077 -- treat these as references, and the references were
2078 -- separately recorded).
2080 or else (Is_Formal (XE.Key.Ent)
2081 and then Present (Spec_Entity (XE.Key.Ent)))
2086 -- Start new Xref section if new xref unit
2088 if XE.Key.Eun /= Curxu then
2089 if Write_Info_Col > 1 then
2093 Curxu := XE.Key.Eun;
2095 Write_Info_Initiate ('X');
2096 Write_Info_Char (' ');
2097 Write_Info_Nat (Dependency_Num (XE.Key.Eun));
2098 Write_Info_Char (' ');
2100 (Reference_Name (Source_Index (XE.Key.Eun)));
2103 -- Start new Entity line if new entity. Note that we
2104 -- consider two entities the same if they have the same
2105 -- name and source location. This causes entities in
2106 -- instantiations to be treated as though they referred
2111 (XE.Key.Ent /= Curent
2113 (Name_Change (XE.Key.Ent) or else XE.Def /= Curdef))
2115 Curent := XE.Key.Ent;
2118 Get_Unqualified_Name_String (Chars (XE.Key.Ent));
2120 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
2122 if Write_Info_Col > 1 then
2126 -- Write column number information
2128 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
2129 Write_Info_Char (Ctyp);
2130 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
2132 -- Write level information
2134 Write_Level_Info : declare
2135 function Is_Visible_Generic_Entity
2136 (E : Entity_Id) return Boolean;
2137 -- Check whether E is declared in the visible part
2138 -- of a generic package. For source navigation
2139 -- purposes, treat this as a visible entity.
2141 function Is_Private_Record_Component
2142 (E : Entity_Id) return Boolean;
2143 -- Check whether E is a non-inherited component of a
2144 -- private extension. Even if the enclosing record is
2145 -- public, we want to treat the component as private
2146 -- for navigation purposes.
2148 ---------------------------------
2149 -- Is_Private_Record_Component --
2150 ---------------------------------
2152 function Is_Private_Record_Component
2153 (E : Entity_Id) return Boolean
2155 S : constant Entity_Id := Scope (E);
2158 Ekind (E) = E_Component
2159 and then Nkind (Declaration_Node (S)) =
2160 N_Private_Extension_Declaration
2161 and then Original_Record_Component (E) = E;
2162 end Is_Private_Record_Component;
2164 -------------------------------
2165 -- Is_Visible_Generic_Entity --
2166 -------------------------------
2168 function Is_Visible_Generic_Entity
2169 (E : Entity_Id) return Boolean
2174 -- The Present check here is an error defense
2176 if Present (Scope (E))
2177 and then Ekind (Scope (E)) /= E_Generic_Package
2183 while Present (Par) loop
2185 Nkind (Par) = N_Generic_Package_Declaration
2187 -- Entity is a generic formal
2192 Nkind (Parent (Par)) = N_Package_Specification
2195 Is_List_Member (Par)
2196 and then List_Containing (Par) =
2197 Visible_Declarations (Parent (Par));
2199 Par := Parent (Par);
2204 end Is_Visible_Generic_Entity;
2206 -- Start of processing for Write_Level_Info
2209 if Is_Hidden (Curent)
2210 or else Is_Private_Record_Component (Curent)
2212 Write_Info_Char (' ');
2216 or else Is_Visible_Generic_Entity (Curent)
2218 Write_Info_Char ('*');
2221 Write_Info_Char (' ');
2223 end Write_Level_Info;
2225 -- Output entity name. We use the occurrence from the
2226 -- actual source program at the definition point.
2229 Ent_Name : constant String :=
2230 Exact_Source_Name (Sloc (XE.Key.Ent));
2232 for C in Ent_Name'Range loop
2233 Write_Info_Char (Ent_Name (C));
2237 -- See if we have a renaming reference
2239 if Is_Object (XE.Key.Ent)
2240 and then Present (Renamed_Object (XE.Key.Ent))
2242 Rref := Renamed_Object (XE.Key.Ent);
2244 elsif Is_Overloadable (XE.Key.Ent)
2245 and then Nkind (Parent (Declaration_Node (XE.Key.Ent)))
2246 = N_Subprogram_Renaming_Declaration
2248 Rref := Name (Parent (Declaration_Node (XE.Key.Ent)));
2250 elsif Ekind (XE.Key.Ent) = E_Package
2251 and then Nkind (Declaration_Node (XE.Key.Ent)) =
2252 N_Package_Renaming_Declaration
2254 Rref := Name (Declaration_Node (XE.Key.Ent));
2260 if Present (Rref) then
2261 if Nkind (Rref) = N_Expanded_Name then
2262 Rref := Selector_Name (Rref);
2265 if Nkind (Rref) = N_Identifier
2266 or else Nkind (Rref) = N_Operator_Symbol
2270 -- For renamed array components, use the array name
2271 -- for the renamed entity, which reflect the fact that
2272 -- in general the whole array is aliased.
2274 elsif Nkind (Rref) = N_Indexed_Component then
2275 if Nkind (Prefix (Rref)) = N_Identifier then
2276 Rref := Prefix (Rref);
2277 elsif Nkind (Prefix (Rref)) = N_Expanded_Name then
2278 Rref := Selector_Name (Prefix (Rref));
2288 -- Write out renaming reference if we have one
2290 if Present (Rref) then
2291 Write_Info_Char ('=');
2293 (Int (Get_Logical_Line_Number (Sloc (Rref))));
2294 Write_Info_Char (':');
2296 (Int (Get_Column_Number (Sloc (Rref))));
2299 -- Indicate that the entity is in the unit of the current
2304 -- Write out information about generic parent, if entity
2307 if Is_Generic_Instance (XE.Key.Ent) then
2309 Gen_Par : constant Entity_Id :=
2312 (Unit_Declaration_Node
2314 Loc : constant Source_Ptr := Sloc (Gen_Par);
2315 Gen_U : constant Unit_Number_Type :=
2316 Get_Source_Unit (Loc);
2319 Write_Info_Char ('[');
2321 if Curru /= Gen_U then
2322 Write_Info_Nat (Dependency_Num (Gen_U));
2323 Write_Info_Char ('|');
2327 (Int (Get_Logical_Line_Number (Loc)));
2328 Write_Info_Char (']');
2332 -- See if we have a type reference and if so output
2334 Check_Type_Reference (XE.Key.Ent, False);
2336 -- Additional information for types with progenitors
2338 if Is_Record_Type (XE.Key.Ent)
2339 and then Present (Interfaces (XE.Key.Ent))
2343 First_Elmt (Interfaces (XE.Key.Ent));
2345 while Present (Elmt) loop
2346 Check_Type_Reference (Node (Elmt), True);
2351 -- For array types, list index types as well. (This is
2352 -- not C, indexes have distinct types).
2354 elsif Is_Array_Type (XE.Key.Ent) then
2358 Indx := First_Index (XE.Key.Ent);
2359 while Present (Indx) loop
2360 Check_Type_Reference
2361 (First_Subtype (Etype (Indx)), True);
2367 -- If the entity is an overriding operation, write info
2368 -- on operation that was overridden.
2370 if Is_Subprogram (XE.Key.Ent)
2371 and then Present (Overridden_Operation (XE.Key.Ent))
2373 Output_Overridden_Op
2374 (Overridden_Operation (XE.Key.Ent));
2377 -- End of processing for entity output
2379 Crloc := No_Location;
2382 -- Output the reference if it is not as the same location
2383 -- as the previous one, or it is a read-reference that
2384 -- indicates that the entity is an in-out actual in a call.
2386 if XE.Key.Loc /= No_Location
2388 (XE.Key.Loc /= Crloc
2389 or else (Prevt = 'm' and then XE.Key.Typ = 'r'))
2391 Crloc := XE.Key.Loc;
2392 Prevt := XE.Key.Typ;
2394 -- Start continuation if line full, else blank
2396 if Write_Info_Col > 72 then
2398 Write_Info_Initiate ('.');
2401 Write_Info_Char (' ');
2403 -- Output file number if changed
2405 if XE.Key.Lun /= Curru then
2406 Curru := XE.Key.Lun;
2407 Write_Info_Nat (Dependency_Num (Curru));
2408 Write_Info_Char ('|');
2412 (Int (Get_Logical_Line_Number (XE.Key.Loc)));
2413 Write_Info_Char (XE.Key.Typ);
2415 if Is_Overloadable (XE.Key.Ent)
2416 and then Is_Imported (XE.Key.Ent)
2417 and then XE.Key.Typ = 'b'
2419 Output_Import_Export_Info (XE.Key.Ent);
2422 Write_Info_Nat (Int (Get_Column_Number (XE.Key.Loc)));
2424 Output_Instantiation_Refs (Sloc (XE.Key.Ent));
2435 end Output_References;
2438 -- Reset is necessary because Elmt_Ptr does not default to Null_Ptr,
2439 -- because it's not an access type.