1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2012, 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 Add_Entry (Key : Xref_Key; Ent_Scope_File : Unit_Number_Type);
165 -- Add an entry to the tables of Xref_Entries, avoiding duplicates
167 procedure Generate_Prim_Op_References (Typ : Entity_Id);
168 -- For a tagged type, generate implicit references to its primitive
169 -- operations, for source navigation. This is done right before emitting
170 -- cross-reference information rather than at the freeze point of the type
171 -- in order to handle late bodies that are primitive operations.
173 function Lt (T1, T2 : Xref_Entry) return Boolean;
174 -- Order cross-references
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)
376 Actual_Typ : Character := Typ;
380 Ent_Scope : Entity_Id;
381 Ent_Scope_File : Unit_Number_Type;
386 Ref_Scope : Entity_Id;
388 function Get_Through_Renamings (E : Entity_Id) return Entity_Id;
389 -- Get the enclosing entity through renamings, which may come from
390 -- source or from the translation of generic instantiations.
392 function Is_On_LHS (Node : Node_Id) return Boolean;
393 -- Used to check if a node is on the left hand side of an assignment.
394 -- The following cases are handled:
396 -- Variable Node is a direct descendant of left hand side of an
397 -- assignment statement.
399 -- Prefix Of an indexed or selected component that is present in
400 -- a subtree rooted by an assignment statement. There is
401 -- no restriction of nesting of components, thus cases
402 -- such as A.B (C).D are handled properly. However a prefix
403 -- of a dereference (either implicit or explicit) is never
404 -- considered as on a LHS.
406 -- Out param Same as above cases, but OUT parameter
408 function OK_To_Set_Referenced return Boolean;
409 -- Returns True if the Referenced flag can be set. There are a few
410 -- exceptions where we do not want to set this flag, see body for
411 -- details of these exceptional cases.
413 ---------------------------
414 -- Get_Through_Renamings --
415 ---------------------------
417 function Get_Through_Renamings (E : Entity_Id) return Entity_Id is
418 Result : Entity_Id := E;
420 while Present (Result)
421 and then Is_Object (Result)
422 and then Present (Renamed_Object (Result))
424 Result := Get_Enclosing_Object (Renamed_Object (Result));
427 end Get_Through_Renamings;
433 -- ??? There are several routines here and there that perform a similar
434 -- (but subtly different) computation, which should be factored:
436 -- Sem_Util.May_Be_Lvalue
437 -- Sem_Util.Known_To_Be_Assigned
438 -- Exp_Ch2.Expand_Entry_Parameter.In_Assignment_Context
439 -- Exp_Smem.Is_Out_Actual
441 function Is_On_LHS (Node : Node_Id) return Boolean is
447 -- Only identifiers are considered, is this necessary???
449 if Nkind (Node) /= N_Identifier then
453 -- Immediate return if appeared as OUT parameter
455 if Kind = E_Out_Parameter then
459 -- Search for assignment statement subtree root
466 if K = N_Assignment_Statement then
469 -- Check whether the parent is a component and the current node is
470 -- its prefix, but return False if the current node has an access
471 -- type, as in that case the selected or indexed component is an
472 -- implicit dereference, and the LHS is the designated object, not
473 -- the access object.
475 -- ??? case of a slice assignment?
477 -- ??? Note that in some cases this is called too early
478 -- (see comments in Sem_Ch8.Find_Direct_Name), at a point where
479 -- the tree is not fully typed yet. In that case we may lack
480 -- an Etype for N, and we must disable the check for an implicit
481 -- dereference. If the dereference is on an LHS, this causes a
484 elsif (K = N_Selected_Component or else K = N_Indexed_Component)
485 and then Prefix (P) = N
486 and then not (Present (Etype (N))
488 Is_Access_Type (Etype (N)))
492 -- All other cases, definitely not on left side
500 ---------------------------
501 -- OK_To_Set_Referenced --
502 ---------------------------
504 function OK_To_Set_Referenced return Boolean is
508 -- A reference from a pragma Unreferenced or pragma Unmodified or
509 -- pragma Warnings does not cause the Referenced flag to be set.
510 -- This avoids silly warnings about things being referenced and
511 -- not assigned when the only reference is from the pragma.
513 if Nkind (N) = N_Identifier then
516 if Nkind (P) = N_Pragma_Argument_Association then
519 if Nkind (P) = N_Pragma then
520 if Pragma_Name (P) = Name_Warnings
522 Pragma_Name (P) = Name_Unmodified
524 Pragma_Name (P) = Name_Unreferenced
530 -- A reference to a formal in a named parameter association does
531 -- not make the formal referenced. Formals that are unused in the
532 -- subprogram body are properly flagged as such, even if calls
533 -- elsewhere use named notation.
535 elsif Nkind (P) = N_Parameter_Association
536 and then N = Selector_Name (P)
543 end OK_To_Set_Referenced;
545 -- Start of processing for Generate_Reference
548 pragma Assert (Nkind (E) in N_Entity);
549 Find_Actual (N, Formal, Call);
551 if Present (Formal) then
552 Kind := Ekind (Formal);
557 -- Check for obsolescent reference to package ASCII. GNAT treats this
558 -- element of annex J specially since in practice, programs make a lot
559 -- of use of this feature, so we don't include it in the set of features
560 -- diagnosed when Warn_On_Obsolescent_Features mode is set. However we
561 -- are required to note it as a violation of the RM defined restriction.
563 if E = Standard_ASCII then
564 Check_Restriction (No_Obsolescent_Features, N);
567 -- Check for reference to entity marked with Is_Obsolescent
569 -- Note that we always allow obsolescent references in the compiler
570 -- itself and the run time, since we assume that we know what we are
571 -- doing in such cases. For example the calls in Ada.Characters.Handling
572 -- to its own obsolescent subprograms are just fine.
574 -- In any case we only generate warnings if we are in the extended main
575 -- source unit, and the entity itself is not in the extended main source
576 -- unit, since we assume the source unit itself knows what is going on
577 -- (and for sure we do not want silly warnings, e.g. on the end line of
578 -- an obsolescent procedure body).
580 if Is_Obsolescent (E)
581 and then not GNAT_Mode
582 and then not In_Extended_Main_Source_Unit (E)
583 and then In_Extended_Main_Source_Unit (N)
585 Check_Restriction (No_Obsolescent_Features, N);
587 if Warn_On_Obsolescent_Feature then
588 Output_Obsolescent_Entity_Warnings (N, E);
592 -- Warn if reference to Ada 2005 entity not in Ada 2005 mode. We only
593 -- detect real explicit references (modifications and references).
595 if Comes_From_Source (N)
596 and then Is_Ada_2005_Only (E)
597 and then Ada_Version < Ada_2005
598 and then Warn_On_Ada_2005_Compatibility
599 and then (Typ = 'm' or else Typ = 'r' or else Typ = 's')
601 Error_Msg_NE ("& is only defined in Ada 2005?", N, E);
604 -- Warn if reference to Ada 2012 entity not in Ada 2012 mode. We only
605 -- detect real explicit references (modifications and references).
607 if Comes_From_Source (N)
608 and then Is_Ada_2012_Only (E)
609 and then Ada_Version < Ada_2012
610 and then Warn_On_Ada_2012_Compatibility
611 and then (Typ = 'm' or else Typ = 'r')
613 Error_Msg_NE ("& is only defined in Ada 2012?", N, E);
616 -- Never collect references if not in main source unit. However, we omit
617 -- this test if Typ is 'e' or 'k', since these entries are structural,
618 -- and it is useful to have them in units that reference packages as
619 -- well as units that define packages. We also omit the test for the
620 -- case of 'p' since we want to include inherited primitive operations
621 -- from other packages.
623 -- We also omit this test is this is a body reference for a subprogram
624 -- instantiation. In this case the reference is to the generic body,
625 -- which clearly need not be in the main unit containing the instance.
626 -- For the same reason we accept an implicit reference generated for
627 -- a default in an instance.
629 if not In_Extended_Main_Source_Unit (N) then
635 or else (Typ = 'b' and then Is_Generic_Instance (E))
643 -- For reference type p, the entity must be in main source unit
645 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
649 -- Unless the reference is forced, we ignore references where the
650 -- reference itself does not come from source.
652 if not Force and then not Comes_From_Source (N) then
656 -- Deal with setting entity as referenced, unless suppressed. Note that
657 -- we still do Set_Referenced on entities that do not come from source.
658 -- This situation arises when we have a source reference to a derived
659 -- operation, where the derived operation itself does not come from
660 -- source, but we still want to mark it as referenced, since we really
661 -- are referencing an entity in the corresponding package (this avoids
662 -- wrong complaints that the package contains no referenced entities).
666 -- Assignable object appearing on left side of assignment or as
670 and then Is_On_LHS (N)
671 and then Ekind (E) /= E_In_Out_Parameter
673 -- For objects that are renamings, just set as simply referenced
674 -- we do not try to do assignment type tracking in this case.
676 if Present (Renamed_Object (E)) then
679 -- Out parameter case
681 elsif Kind = E_Out_Parameter then
683 -- If warning mode for all out parameters is set, or this is
684 -- the only warning parameter, then we want to mark this for
685 -- later warning logic by setting Referenced_As_Out_Parameter
687 if Warn_On_Modified_As_Out_Parameter (Formal) then
688 Set_Referenced_As_Out_Parameter (E, True);
689 Set_Referenced_As_LHS (E, False);
691 -- For OUT parameter not covered by the above cases, we simply
692 -- regard it as a normal reference (in this case we do not
693 -- want any of the warning machinery for out parameters).
699 -- For the left hand of an assignment case, we do nothing here.
700 -- The processing for Analyze_Assignment_Statement will set the
701 -- Referenced_As_LHS flag.
707 -- Check for a reference in a pragma that should not count as a
708 -- making the variable referenced for warning purposes.
710 elsif Is_Non_Significant_Pragma_Reference (N) then
713 -- A reference in an attribute definition clause does not count as a
714 -- reference except for the case of Address. The reason that 'Address
715 -- is an exception is that it creates an alias through which the
716 -- variable may be referenced.
718 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
719 and then Chars (Parent (N)) /= Name_Address
720 and then N = Name (Parent (N))
724 -- Constant completion does not count as a reference
727 and then Ekind (E) = E_Constant
731 -- Record representation clause does not count as a reference
733 elsif Nkind (N) = N_Identifier
734 and then Nkind (Parent (N)) = N_Record_Representation_Clause
738 -- Discriminants do not need to produce a reference to record type
741 and then Nkind (Parent (N)) = N_Discriminant_Specification
748 -- Special processing for IN OUT parameters, where we have an
749 -- implicit assignment to a simple variable.
751 if Kind = E_In_Out_Parameter
752 and then Is_Assignable (E)
754 -- For sure this counts as a normal read reference
757 Set_Last_Assignment (E, Empty);
759 -- We count it as being referenced as an out parameter if the
760 -- option is set to warn on all out parameters, except that we
761 -- have a special exclusion for an intrinsic subprogram, which
762 -- is most likely an instantiation of Unchecked_Deallocation
763 -- which we do not want to consider as an assignment since it
764 -- generates false positives. We also exclude the case of an
765 -- IN OUT parameter if the name of the procedure is Free,
766 -- since we suspect similar semantics.
768 if Warn_On_All_Unread_Out_Parameters
769 and then Is_Entity_Name (Name (Call))
770 and then not Is_Intrinsic_Subprogram (Entity (Name (Call)))
771 and then Chars (Name (Call)) /= Name_Free
773 Set_Referenced_As_Out_Parameter (E, True);
774 Set_Referenced_As_LHS (E, False);
777 -- Don't count a recursive reference within a subprogram as a
778 -- reference (that allows detection of a recursive subprogram
779 -- whose only references are recursive calls as unreferenced).
781 elsif Is_Subprogram (E)
782 and then E = Nearest_Dynamic_Scope (Current_Scope)
786 -- Any other occurrence counts as referencing the entity
788 elsif OK_To_Set_Referenced then
791 -- If variable, this is an OK reference after an assignment
792 -- so we can clear the Last_Assignment indication.
794 if Is_Assignable (E) then
795 Set_Last_Assignment (E, Empty);
800 -- Check for pragma Unreferenced given and reference is within
801 -- this source unit (occasion for possible warning to be issued).
803 if Has_Unreferenced (E)
804 and then In_Same_Extended_Unit (E, N)
806 -- A reference as a named parameter in a call does not count
807 -- as a violation of pragma Unreferenced for this purpose...
809 if Nkind (N) = N_Identifier
810 and then Nkind (Parent (N)) = N_Parameter_Association
811 and then Selector_Name (Parent (N)) = N
815 -- ... Neither does a reference to a variable on the left side
818 elsif Is_On_LHS (N) then
821 -- For entry formals, we want to place the warning message on the
822 -- corresponding entity in the accept statement. The current scope
823 -- is the body of the accept, so we find the formal whose name
824 -- matches that of the entry formal (there is no link between the
825 -- two entities, and the one in the accept statement is only used
826 -- for conformance checking).
828 elsif Ekind (Scope (E)) = E_Entry then
833 BE := First_Entity (Current_Scope);
834 while Present (BE) loop
835 if Chars (BE) = Chars (E) then
836 Error_Msg_NE -- CODEFIX
837 ("?pragma Unreferenced given for&!", N, BE);
845 -- Here we issue the warning, since this is a real reference
848 Error_Msg_NE -- CODEFIX
849 ("?pragma Unreferenced given for&!", N, E);
853 -- If this is a subprogram instance, mark as well the internal
854 -- subprogram in the wrapper package, which may be a visible
857 if Is_Overloadable (E)
858 and then Is_Generic_Instance (E)
859 and then Present (Alias (E))
861 Set_Referenced (Alias (E));
865 -- Generate reference if all conditions are met:
868 -- Cross referencing must be active
872 -- The entity must be one for which we collect references
874 and then Xref_Entity_Letters (Ekind (E)) /= ' '
876 -- Both Sloc values must be set to something sensible
878 and then Sloc (E) > No_Location
879 and then Sloc (N) > No_Location
881 -- Ignore references from within an instance. The only exceptions to
882 -- this are default subprograms, for which we generate an implicit
886 (Instantiation_Location (Sloc (N)) = No_Location
889 -- Ignore dummy references
893 if Nkind (N) = N_Identifier
895 Nkind (N) = N_Defining_Identifier
899 Nkind (N) = N_Defining_Operator_Symbol
901 Nkind (N) = N_Operator_Symbol
903 (Nkind (N) = N_Character_Literal
904 and then Sloc (Entity (N)) /= Standard_Location)
906 Nkind (N) = N_Defining_Character_Literal
910 elsif Nkind (N) = N_Expanded_Name
912 Nkind (N) = N_Selected_Component
914 Nod := Selector_Name (N);
920 -- Normal case of source entity comes from source
922 if Comes_From_Source (E) then
925 -- Entity does not come from source, but is a derived subprogram and
926 -- the derived subprogram comes from source (after one or more
927 -- derivations) in which case the reference is to parent subprogram.
929 elsif Is_Overloadable (E)
930 and then Present (Alias (E))
933 while not Comes_From_Source (Ent) loop
934 if No (Alias (Ent)) then
941 -- The internally created defining entity for a child subprogram
942 -- that has no previous spec has valid references.
944 elsif Is_Overloadable (E)
945 and then Is_Child_Unit (E)
949 -- Record components of discriminated subtypes or derived types must
950 -- be treated as references to the original component.
952 elsif Ekind (E) = E_Component
953 and then Comes_From_Source (Original_Record_Component (E))
955 Ent := Original_Record_Component (E);
957 -- If this is an expanded reference to a discriminant, recover the
958 -- original discriminant, which gets the reference.
960 elsif Ekind (E) = E_In_Parameter
961 and then Present (Discriminal_Link (E))
963 Ent := Discriminal_Link (E);
964 Set_Referenced (Ent);
966 -- Ignore reference to any other entity that is not from source
972 -- In Alfa mode, consider the underlying entity renamed instead of
973 -- the renaming, which is needed to compute a valid set of effects
974 -- (reads, writes) for the enclosing subprogram.
977 Ent := Get_Through_Renamings (Ent);
979 -- If no enclosing object, then it could be a reference to any
980 -- location not tracked individually, like heap-allocated data.
981 -- Conservatively approximate this possibility by generating a
982 -- dereference, and return.
985 if Actual_Typ = 'w' then
986 Alfa.Generate_Dereference (Nod, 'r');
987 Alfa.Generate_Dereference (Nod, 'w');
989 Alfa.Generate_Dereference (Nod, 'r');
996 -- Record reference to entity
998 Ref := Original_Location (Sloc (Nod));
999 Def := Original_Location (Sloc (Ent));
1002 and then Is_Subprogram (Nod)
1003 and then Present (Overridden_Operation (Nod))
1009 Ref_Scope := Alfa.Enclosing_Subprogram_Or_Package (Nod);
1010 Ent_Scope := Alfa.Enclosing_Subprogram_Or_Package (Ent);
1012 -- Since we are reaching through renamings in Alfa mode, we may
1013 -- end up with standard constants. Ignore those.
1015 if Sloc (Ent_Scope) <= Standard_Location
1016 or else Def <= Standard_Location
1021 Ent_Scope_File := Get_Source_Unit (Ent_Scope);
1025 Ent_Scope_File := No_Unit;
1032 Eun => Get_Source_Unit (Def),
1033 Lun => Get_Source_Unit (Ref),
1034 Ref_Scope => Ref_Scope,
1035 Ent_Scope => Ent_Scope),
1036 Ent_Scope_File => Ent_Scope_File);
1038 end Generate_Reference;
1040 -----------------------------------
1041 -- Generate_Reference_To_Formals --
1042 -----------------------------------
1044 procedure Generate_Reference_To_Formals (E : Entity_Id) is
1048 if Is_Generic_Subprogram (E) then
1049 Formal := First_Entity (E);
1051 while Present (Formal)
1052 and then not Is_Formal (Formal)
1054 Next_Entity (Formal);
1058 Formal := First_Formal (E);
1061 while Present (Formal) loop
1062 if Ekind (Formal) = E_In_Parameter then
1064 if Nkind (Parameter_Type (Parent (Formal)))
1065 = N_Access_Definition
1067 Generate_Reference (E, Formal, '^', False);
1069 Generate_Reference (E, Formal, '>', False);
1072 elsif Ekind (Formal) = E_In_Out_Parameter then
1073 Generate_Reference (E, Formal, '=', False);
1076 Generate_Reference (E, Formal, '<', False);
1079 Next_Formal (Formal);
1081 end Generate_Reference_To_Formals;
1083 -------------------------------------------
1084 -- Generate_Reference_To_Generic_Formals --
1085 -------------------------------------------
1087 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
1091 Formal := First_Entity (E);
1092 while Present (Formal) loop
1093 if Comes_From_Source (Formal) then
1094 Generate_Reference (E, Formal, 'z', False);
1097 Next_Entity (Formal);
1099 end Generate_Reference_To_Generic_Formals;
1105 function Get_Key (E : Xref_Entry_Number) return Xref_Entry_Number is
1114 function Hash (F : Xref_Entry_Number) return Header_Num is
1115 -- It is unlikely to have two references to the same entity at the same
1116 -- source location, so the hash function depends only on the Ent and Loc
1119 XE : Xref_Entry renames Xrefs.Table (F);
1120 type M is mod 2**32;
1122 H : constant M := M (XE.Key.Ent) + 2 ** 7 * M (abs XE.Key.Loc);
1123 -- It would be more natural to write:
1125 -- H : constant M := M'Mod (XE.Key.Ent) + 2**7 * M'Mod (XE.Key.Loc);
1127 -- But we can't use M'Mod, because it prevents bootstrapping with older
1128 -- compilers. Loc can be negative, so we do "abs" before converting.
1129 -- One day this can be cleaned up ???
1132 return Header_Num (H mod Num_Buckets);
1139 procedure HT_Set_Next (E : Xref_Entry_Number; Next : Xref_Entry_Number) is
1141 Xrefs.Table (E).HTable_Next := Next;
1148 function HT_Next (E : Xref_Entry_Number) return Xref_Entry_Number is
1150 return Xrefs.Table (E).HTable_Next;
1157 procedure Initialize is
1166 function Lt (T1, T2 : Xref_Entry) return Boolean is
1168 -- First test: if entity is in different unit, sort by unit
1170 if T1.Key.Eun /= T2.Key.Eun then
1171 return Dependency_Num (T1.Key.Eun) < Dependency_Num (T2.Key.Eun);
1173 -- Second test: within same unit, sort by entity Sloc
1175 elsif T1.Def /= T2.Def then
1176 return T1.Def < T2.Def;
1178 -- Third test: sort definitions ahead of references
1180 elsif T1.Key.Loc = No_Location then
1183 elsif T2.Key.Loc = No_Location then
1186 -- Fourth test: for same entity, sort by reference location unit
1188 elsif T1.Key.Lun /= T2.Key.Lun then
1189 return Dependency_Num (T1.Key.Lun) < Dependency_Num (T2.Key.Lun);
1191 -- Fifth test: order of location within referencing unit
1193 elsif T1.Key.Loc /= T2.Key.Loc then
1194 return T1.Key.Loc < T2.Key.Loc;
1196 -- Finally, for two locations at the same address, we prefer
1197 -- the one that does NOT have the type 'r' so that a modification
1198 -- or extension takes preference, when there are more than one
1199 -- reference at the same location. As a result, in the case of
1200 -- entities that are in-out actuals, the read reference follows
1201 -- the modify reference.
1204 return T2.Key.Typ = 'r';
1208 -----------------------
1209 -- Output_References --
1210 -----------------------
1212 procedure Output_References is
1214 procedure Get_Type_Reference
1216 Tref : out Entity_Id;
1217 Left : out Character;
1218 Right : out Character);
1219 -- Given an Entity_Id Ent, determines whether a type reference is
1220 -- required. If so, Tref is set to the entity for the type reference
1221 -- and Left and Right are set to the left/right brackets to be output
1222 -- for the reference. If no type reference is required, then Tref is
1223 -- set to Empty, and Left/Right are set to space.
1225 procedure Output_Import_Export_Info (Ent : Entity_Id);
1226 -- Output language and external name information for an interfaced
1227 -- entity, using the format <language, external_name>.
1229 ------------------------
1230 -- Get_Type_Reference --
1231 ------------------------
1233 procedure Get_Type_Reference
1235 Tref : out Entity_Id;
1236 Left : out Character;
1237 Right : out Character)
1242 -- See if we have a type reference
1251 -- Processing for types
1253 if Is_Type (Tref) then
1255 -- Case of base type
1257 if Base_Type (Tref) = Tref then
1259 -- If derived, then get first subtype
1261 if Tref /= Etype (Tref) then
1262 Tref := First_Subtype (Etype (Tref));
1264 -- Set brackets for derived type, but don't override
1265 -- pointer case since the fact that something is a
1266 -- pointer is more important.
1273 -- If non-derived ptr, get directly designated type.
1274 -- If the type has a full view, all references are on the
1275 -- partial view, that is seen first.
1277 elsif Is_Access_Type (Tref) then
1278 Tref := Directly_Designated_Type (Tref);
1282 elsif Is_Private_Type (Tref)
1283 and then Present (Full_View (Tref))
1285 if Is_Access_Type (Full_View (Tref)) then
1286 Tref := Directly_Designated_Type (Full_View (Tref));
1290 -- If the full view is an array type, we also retrieve
1291 -- the corresponding component type, because the ali
1292 -- entry already indicates that this is an array.
1294 elsif Is_Array_Type (Full_View (Tref)) then
1295 Tref := Component_Type (Full_View (Tref));
1300 -- If non-derived array, get component type. Skip component
1301 -- type for case of String or Wide_String, saves worthwhile
1304 elsif Is_Array_Type (Tref)
1305 and then Tref /= Standard_String
1306 and then Tref /= Standard_Wide_String
1308 Tref := Component_Type (Tref);
1312 -- For other non-derived base types, nothing
1318 -- For a subtype, go to ancestor subtype
1321 Tref := Ancestor_Subtype (Tref);
1323 -- If no ancestor subtype, go to base type
1326 Tref := Base_Type (Sav);
1330 -- For objects, functions, enum literals, just get type from
1333 elsif Is_Object (Tref)
1334 or else Ekind (Tref) = E_Enumeration_Literal
1335 or else Ekind (Tref) = E_Function
1336 or else Ekind (Tref) = E_Operator
1338 Tref := Etype (Tref);
1340 -- For anything else, exit
1346 -- Exit if no type reference, or we are stuck in some loop trying
1347 -- to find the type reference, or if the type is standard void
1348 -- type (the latter is an implementation artifact that should not
1349 -- show up in the generated cross-references).
1353 or else Tref = Standard_Void_Type;
1355 -- If we have a usable type reference, return, otherwise keep
1356 -- looking for something useful (we are looking for something
1357 -- that either comes from source or standard)
1359 if Sloc (Tref) = Standard_Location
1360 or else Comes_From_Source (Tref)
1362 -- If the reference is a subtype created for a generic actual,
1363 -- go actual directly, the inner subtype is not user visible.
1365 if Nkind (Parent (Tref)) = N_Subtype_Declaration
1366 and then not Comes_From_Source (Parent (Tref))
1368 (Is_Wrapper_Package (Scope (Tref))
1369 or else Is_Generic_Instance (Scope (Tref)))
1371 Tref := First_Subtype (Base_Type (Tref));
1378 -- If we fall through the loop, no type reference
1383 end Get_Type_Reference;
1385 -------------------------------
1386 -- Output_Import_Export_Info --
1387 -------------------------------
1389 procedure Output_Import_Export_Info (Ent : Entity_Id) is
1390 Language_Name : Name_Id;
1391 Conv : constant Convention_Id := Convention (Ent);
1394 -- Generate language name from convention
1396 if Conv = Convention_C then
1397 Language_Name := Name_C;
1399 elsif Conv = Convention_CPP then
1400 Language_Name := Name_CPP;
1402 elsif Conv = Convention_Ada then
1403 Language_Name := Name_Ada;
1406 -- For the moment we ignore all other cases ???
1411 Write_Info_Char ('<');
1412 Get_Unqualified_Name_String (Language_Name);
1414 for J in 1 .. Name_Len loop
1415 Write_Info_Char (Name_Buffer (J));
1418 if Present (Interface_Name (Ent)) then
1419 Write_Info_Char (',');
1420 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
1422 for J in 1 .. Name_Len loop
1423 Write_Info_Char (Name_Buffer (J));
1427 Write_Info_Char ('>');
1428 end Output_Import_Export_Info;
1430 -- Start of processing for Output_References
1433 -- First we add references to the primitive operations of tagged types
1434 -- declared in the main unit.
1436 Handle_Prim_Ops : declare
1440 for J in 1 .. Xrefs.Last loop
1441 Ent := Xrefs.Table (J).Key.Ent;
1444 and then Is_Tagged_Type (Ent)
1445 and then Is_Base_Type (Ent)
1446 and then In_Extended_Main_Source_Unit (Ent)
1448 Generate_Prim_Op_References (Ent);
1451 end Handle_Prim_Ops;
1453 -- Before we go ahead and output the references we have a problem
1454 -- that needs dealing with. So far we have captured things that are
1455 -- definitely referenced by the main unit, or defined in the main
1456 -- unit. That's because we don't want to clutter up the ali file
1457 -- for this unit with definition lines for entities in other units
1458 -- that are not referenced.
1460 -- But there is a glitch. We may reference an entity in another unit,
1461 -- and it may have a type reference to an entity that is not directly
1462 -- referenced in the main unit, which may mean that there is no xref
1463 -- entry for this entity yet in the list of references.
1465 -- If we don't do something about this, we will end with an orphan type
1466 -- reference, i.e. it will point to an entity that does not appear
1467 -- within the generated references in the ali file. That is not good for
1468 -- tools using the xref information.
1470 -- To fix this, we go through the references adding definition entries
1471 -- for any unreferenced entities that can be referenced in a type
1472 -- reference. There is a recursion problem here, and that is dealt with
1473 -- by making sure that this traversal also traverses any entries that
1474 -- get added by the traversal.
1476 Handle_Orphan_Type_References : declare
1482 pragma Warnings (Off, L);
1483 pragma Warnings (Off, R);
1485 procedure New_Entry (E : Entity_Id);
1486 -- Make an additional entry into the Xref table for a type entity
1487 -- that is related to the current entity (parent, type ancestor,
1488 -- progenitor, etc.).
1494 procedure New_Entry (E : Entity_Id) is
1496 pragma Assert (Present (E));
1498 if not Has_Xref_Entry (Implementation_Base_Type (E))
1499 and then Sloc (E) > No_Location
1504 Typ => Character'First,
1505 Eun => Get_Source_Unit (Original_Location (Sloc (E))),
1508 Ent_Scope => Empty),
1509 Ent_Scope_File => No_Unit);
1513 -- Start of processing for Handle_Orphan_Type_References
1516 -- Note that this is not a for loop for a very good reason. The
1517 -- processing of items in the table can add new items to the table,
1518 -- and they must be processed as well.
1521 while J <= Xrefs.Last loop
1522 Ent := Xrefs.Table (J).Key.Ent;
1523 Get_Type_Reference (Ent, Tref, L, R);
1526 and then not Has_Xref_Entry (Tref)
1527 and then Sloc (Tref) > No_Location
1531 if Is_Record_Type (Ent)
1532 and then Present (Interfaces (Ent))
1534 -- Add an entry for each one of the given interfaces
1535 -- implemented by type Ent.
1538 Elmt : Elmt_Id := First_Elmt (Interfaces (Ent));
1540 while Present (Elmt) loop
1541 New_Entry (Node (Elmt));
1548 -- Collect inherited primitive operations that may be declared in
1549 -- another unit and have no visible reference in the current one.
1552 and then Is_Tagged_Type (Ent)
1553 and then Is_Derived_Type (Ent)
1554 and then Is_Base_Type (Ent)
1555 and then In_Extended_Main_Source_Unit (Ent)
1558 Op_List : constant Elist_Id := Primitive_Operations (Ent);
1562 function Parent_Op (E : Entity_Id) return Entity_Id;
1563 -- Find original operation, which may be inherited through
1564 -- several derivations.
1566 function Parent_Op (E : Entity_Id) return Entity_Id is
1567 Orig_Op : constant Entity_Id := Alias (E);
1570 if No (Orig_Op) then
1573 elsif not Comes_From_Source (E)
1574 and then not Has_Xref_Entry (Orig_Op)
1575 and then Comes_From_Source (Orig_Op)
1579 return Parent_Op (Orig_Op);
1584 Op := First_Elmt (Op_List);
1585 while Present (Op) loop
1586 Prim := Parent_Op (Node (Op));
1588 if Present (Prim) then
1592 Typ => Character'First,
1593 Eun => Get_Source_Unit (Sloc (Prim)),
1596 Ent_Scope => Empty),
1597 Ent_Scope_File => No_Unit);
1607 end Handle_Orphan_Type_References;
1609 -- Now we have all the references, including those for any embedded
1610 -- type references, so we can sort them, and output them.
1612 Output_Refs : declare
1614 Nrefs : constant Nat := Xrefs.Last;
1615 -- Number of references in table
1617 Rnums : array (0 .. Nrefs) of Nat;
1618 -- This array contains numbers of references in the Xrefs table.
1619 -- This list is sorted in output order. The extra 0'th entry is
1620 -- convenient for the call to sort. When we sort the table, we
1621 -- move the entries in Rnums around, but we do not move the
1622 -- original table entries.
1624 Curxu : Unit_Number_Type;
1625 -- Current xref unit
1627 Curru : Unit_Number_Type;
1628 -- Current reference unit for one entity
1633 Curnam : String (1 .. Name_Buffer'Length);
1635 -- Simple name and length of current entity
1637 Curdef : Source_Ptr;
1638 -- Original source location for current entity
1641 -- Current reference location
1644 -- Entity type character
1647 -- reference kind of previous reference
1653 -- Renaming reference
1655 Trunit : Unit_Number_Type;
1656 -- Unit number for type reference
1658 function Lt (Op1, Op2 : Natural) return Boolean;
1659 -- Comparison function for Sort call
1661 function Name_Change (X : Entity_Id) return Boolean;
1662 -- Determines if entity X has a different simple name from Curent
1664 procedure Move (From : Natural; To : Natural);
1665 -- Move procedure for Sort call
1667 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
1673 function Lt (Op1, Op2 : Natural) return Boolean is
1674 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
1675 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
1685 procedure Move (From : Natural; To : Natural) is
1687 Rnums (Nat (To)) := Rnums (Nat (From));
1694 -- Why a string comparison here??? Why not compare Name_Id values???
1696 function Name_Change (X : Entity_Id) return Boolean is
1698 Get_Unqualified_Name_String (Chars (X));
1700 if Name_Len /= Curlen then
1703 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
1707 -- Start of processing for Output_Refs
1710 -- Capture the definition Sloc values. We delay doing this till now,
1711 -- since at the time the reference or definition is made, private
1712 -- types may be swapped, and the Sloc value may be incorrect. We
1713 -- also set up the pointer vector for the sort.
1715 for J in 1 .. Nrefs loop
1717 Xrefs.Table (J).Def :=
1718 Original_Location (Sloc (Xrefs.Table (J).Key.Ent));
1721 -- Sort the references
1723 Sorting.Sort (Integer (Nrefs));
1725 -- Initialize loop through references
1729 Curdef := No_Location;
1731 Crloc := No_Location;
1734 -- Loop to output references
1736 for Refno in 1 .. Nrefs loop
1737 Output_One_Ref : declare
1740 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
1741 -- The current entry to be accessed
1745 -- Used for {} or <> or () for type reference
1747 procedure Check_Type_Reference
1749 List_Interface : Boolean);
1750 -- Find whether there is a meaningful type reference for
1751 -- Ent, and display it accordingly. If List_Interface is
1752 -- true, then Ent is a progenitor interface of the current
1753 -- type entity being listed. In that case list it as is,
1754 -- without looking for a type reference for it.
1756 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
1757 -- Recursive procedure to output instantiation references for
1758 -- the given source ptr in [file|line[...]] form. No output
1759 -- if the given location is not a generic template reference.
1761 procedure Output_Overridden_Op (Old_E : Entity_Id);
1762 -- For a subprogram that is overriding, display information
1763 -- about the inherited operation that it overrides.
1765 --------------------------
1766 -- Check_Type_Reference --
1767 --------------------------
1769 procedure Check_Type_Reference
1771 List_Interface : Boolean)
1774 if List_Interface then
1776 -- This is a progenitor interface of the type for which
1777 -- xref information is being generated.
1784 Get_Type_Reference (Ent, Tref, Left, Right);
1787 if Present (Tref) then
1789 -- Case of standard entity, output name
1791 if Sloc (Tref) = Standard_Location then
1792 Write_Info_Char (Left);
1793 Write_Info_Name (Chars (Tref));
1794 Write_Info_Char (Right);
1796 -- Case of source entity, output location
1799 Write_Info_Char (Left);
1800 Trunit := Get_Source_Unit (Sloc (Tref));
1802 if Trunit /= Curxu then
1803 Write_Info_Nat (Dependency_Num (Trunit));
1804 Write_Info_Char ('|');
1808 (Int (Get_Logical_Line_Number (Sloc (Tref))));
1816 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1819 and then Present (Full_View (Ent))
1821 Ent := Underlying_Type (Ent);
1823 if Present (Ent) then
1824 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1828 Write_Info_Char (Ctyp);
1832 (Int (Get_Column_Number (Sloc (Tref))));
1834 -- If the type comes from an instantiation, add the
1835 -- corresponding info.
1837 Output_Instantiation_Refs (Sloc (Tref));
1838 Write_Info_Char (Right);
1841 end Check_Type_Reference;
1843 -------------------------------
1844 -- Output_Instantiation_Refs --
1845 -------------------------------
1847 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
1848 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
1849 Lun : Unit_Number_Type;
1850 Cu : constant Unit_Number_Type := Curru;
1853 -- Nothing to do if this is not an instantiation
1855 if Iloc = No_Location then
1859 -- Output instantiation reference
1861 Write_Info_Char ('[');
1862 Lun := Get_Source_Unit (Iloc);
1864 if Lun /= Curru then
1866 Write_Info_Nat (Dependency_Num (Curru));
1867 Write_Info_Char ('|');
1870 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
1872 -- Recursive call to get nested instantiations
1874 Output_Instantiation_Refs (Iloc);
1876 -- Output final ] after call to get proper nesting
1878 Write_Info_Char (']');
1881 end Output_Instantiation_Refs;
1883 --------------------------
1884 -- Output_Overridden_Op --
1885 --------------------------
1887 procedure Output_Overridden_Op (Old_E : Entity_Id) is
1891 -- The overridden operation has an implicit declaration
1892 -- at the point of derivation. What we want to display
1893 -- is the original operation, which has the actual body
1894 -- (or abstract declaration) that is being overridden.
1895 -- The overridden operation is not always set, e.g. when
1896 -- it is a predefined operator.
1901 -- Follow alias chain if one is present
1903 elsif Present (Alias (Old_E)) then
1905 -- The subprogram may have been implicitly inherited
1906 -- through several levels of derivation, so find the
1907 -- ultimate (source) ancestor.
1909 Op := Ultimate_Alias (Old_E);
1911 -- Normal case of no alias present. We omit generated
1912 -- primitives like tagged equality, that have no source
1920 and then Sloc (Op) /= Standard_Location
1921 and then Comes_From_Source (Op)
1924 Loc : constant Source_Ptr := Sloc (Op);
1925 Par_Unit : constant Unit_Number_Type :=
1926 Get_Source_Unit (Loc);
1929 Write_Info_Char ('<');
1931 if Par_Unit /= Curxu then
1932 Write_Info_Nat (Dependency_Num (Par_Unit));
1933 Write_Info_Char ('|');
1936 Write_Info_Nat (Int (Get_Logical_Line_Number (Loc)));
1937 Write_Info_Char ('p');
1938 Write_Info_Nat (Int (Get_Column_Number (Loc)));
1939 Write_Info_Char ('>');
1942 end Output_Overridden_Op;
1944 -- Start of processing for Output_One_Ref
1948 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1950 -- Skip reference if it is the only reference to an entity,
1951 -- and it is an END line reference, and the entity is not in
1952 -- the current extended source. This prevents junk entries
1953 -- consisting only of packages with END lines, where no
1954 -- entity from the package is actually referenced.
1957 and then Ent /= Curent
1958 and then (Refno = Nrefs
1960 Ent /= Xrefs.Table (Rnums (Refno + 1)).Key.Ent)
1961 and then not In_Extended_Main_Source_Unit (Ent)
1966 -- For private type, get full view type
1969 and then Present (Full_View (XE.Key.Ent))
1971 Ent := Underlying_Type (Ent);
1973 if Present (Ent) then
1974 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1978 -- Special exception for Boolean
1980 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
1984 -- For variable reference, get corresponding type
1987 Ent := Etype (XE.Key.Ent);
1988 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1990 -- If variable is private type, get full view type
1993 and then Present (Full_View (Etype (XE.Key.Ent)))
1995 Ent := Underlying_Type (Etype (XE.Key.Ent));
1997 if Present (Ent) then
1998 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
2001 elsif Is_Generic_Type (Ent) then
2003 -- If the type of the entity is a generic private type,
2004 -- there is no usable full view, so retain the indication
2005 -- that this is an object.
2010 -- Special handling for access parameters and objects of
2011 -- an anonymous access type.
2013 if Ekind_In (Etype (XE.Key.Ent),
2014 E_Anonymous_Access_Type,
2015 E_Anonymous_Access_Subprogram_Type,
2016 E_Anonymous_Access_Protected_Subprogram_Type)
2018 if Is_Formal (XE.Key.Ent)
2019 or else Ekind_In (XE.Key.Ent, E_Variable, E_Constant)
2024 -- Special handling for Boolean
2026 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
2031 -- Special handling for abstract types and operations
2033 if Is_Overloadable (XE.Key.Ent)
2034 and then Is_Abstract_Subprogram (XE.Key.Ent)
2037 Ctyp := 'x'; -- Abstract procedure
2039 elsif Ctyp = 'V' then
2040 Ctyp := 'y'; -- Abstract function
2043 elsif Is_Type (XE.Key.Ent)
2044 and then Is_Abstract_Type (XE.Key.Ent)
2046 if Is_Interface (XE.Key.Ent) then
2049 elsif Ctyp = 'R' then
2050 Ctyp := 'H'; -- Abstract type
2054 -- Only output reference if interesting type of entity
2058 -- Suppress references to object definitions, used for local
2061 or else XE.Key.Typ = 'D'
2062 or else XE.Key.Typ = 'I'
2064 -- Suppress self references, except for bodies that act as
2067 or else (XE.Key.Loc = XE.Def
2070 or else not Is_Subprogram (XE.Key.Ent)))
2072 -- Also suppress definitions of body formals (we only
2073 -- treat these as references, and the references were
2074 -- separately recorded).
2076 or else (Is_Formal (XE.Key.Ent)
2077 and then Present (Spec_Entity (XE.Key.Ent)))
2082 -- Start new Xref section if new xref unit
2084 if XE.Key.Eun /= Curxu then
2085 if Write_Info_Col > 1 then
2089 Curxu := XE.Key.Eun;
2091 Write_Info_Initiate ('X');
2092 Write_Info_Char (' ');
2093 Write_Info_Nat (Dependency_Num (XE.Key.Eun));
2094 Write_Info_Char (' ');
2096 (Reference_Name (Source_Index (XE.Key.Eun)));
2099 -- Start new Entity line if new entity. Note that we
2100 -- consider two entities the same if they have the same
2101 -- name and source location. This causes entities in
2102 -- instantiations to be treated as though they referred
2107 (XE.Key.Ent /= Curent
2109 (Name_Change (XE.Key.Ent) or else XE.Def /= Curdef))
2111 Curent := XE.Key.Ent;
2114 Get_Unqualified_Name_String (Chars (XE.Key.Ent));
2116 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
2118 if Write_Info_Col > 1 then
2122 -- Write column number information
2124 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
2125 Write_Info_Char (Ctyp);
2126 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
2128 -- Write level information
2130 Write_Level_Info : declare
2131 function Is_Visible_Generic_Entity
2132 (E : Entity_Id) return Boolean;
2133 -- Check whether E is declared in the visible part
2134 -- of a generic package. For source navigation
2135 -- purposes, treat this as a visible entity.
2137 function Is_Private_Record_Component
2138 (E : Entity_Id) return Boolean;
2139 -- Check whether E is a non-inherited component of a
2140 -- private extension. Even if the enclosing record is
2141 -- public, we want to treat the component as private
2142 -- for navigation purposes.
2144 ---------------------------------
2145 -- Is_Private_Record_Component --
2146 ---------------------------------
2148 function Is_Private_Record_Component
2149 (E : Entity_Id) return Boolean
2151 S : constant Entity_Id := Scope (E);
2154 Ekind (E) = E_Component
2155 and then Nkind (Declaration_Node (S)) =
2156 N_Private_Extension_Declaration
2157 and then Original_Record_Component (E) = E;
2158 end Is_Private_Record_Component;
2160 -------------------------------
2161 -- Is_Visible_Generic_Entity --
2162 -------------------------------
2164 function Is_Visible_Generic_Entity
2165 (E : Entity_Id) return Boolean
2170 -- The Present check here is an error defense
2172 if Present (Scope (E))
2173 and then Ekind (Scope (E)) /= E_Generic_Package
2179 while Present (Par) loop
2181 Nkind (Par) = N_Generic_Package_Declaration
2183 -- Entity is a generic formal
2188 Nkind (Parent (Par)) = N_Package_Specification
2191 Is_List_Member (Par)
2192 and then List_Containing (Par) =
2193 Visible_Declarations (Parent (Par));
2195 Par := Parent (Par);
2200 end Is_Visible_Generic_Entity;
2202 -- Start of processing for Write_Level_Info
2205 if Is_Hidden (Curent)
2206 or else Is_Private_Record_Component (Curent)
2208 Write_Info_Char (' ');
2212 or else Is_Visible_Generic_Entity (Curent)
2214 Write_Info_Char ('*');
2217 Write_Info_Char (' ');
2219 end Write_Level_Info;
2221 -- Output entity name. We use the occurrence from the
2222 -- actual source program at the definition point.
2225 Ent_Name : constant String :=
2226 Exact_Source_Name (Sloc (XE.Key.Ent));
2228 for C in Ent_Name'Range loop
2229 Write_Info_Char (Ent_Name (C));
2233 -- See if we have a renaming reference
2235 if Is_Object (XE.Key.Ent)
2236 and then Present (Renamed_Object (XE.Key.Ent))
2238 Rref := Renamed_Object (XE.Key.Ent);
2240 elsif Is_Overloadable (XE.Key.Ent)
2241 and then Nkind (Parent (Declaration_Node (XE.Key.Ent)))
2242 = N_Subprogram_Renaming_Declaration
2244 Rref := Name (Parent (Declaration_Node (XE.Key.Ent)));
2246 elsif Ekind (XE.Key.Ent) = E_Package
2247 and then Nkind (Declaration_Node (XE.Key.Ent)) =
2248 N_Package_Renaming_Declaration
2250 Rref := Name (Declaration_Node (XE.Key.Ent));
2256 if Present (Rref) then
2257 if Nkind (Rref) = N_Expanded_Name then
2258 Rref := Selector_Name (Rref);
2261 if Nkind (Rref) = N_Identifier
2262 or else Nkind (Rref) = N_Operator_Symbol
2266 -- For renamed array components, use the array name
2267 -- for the renamed entity, which reflect the fact that
2268 -- in general the whole array is aliased.
2270 elsif Nkind (Rref) = N_Indexed_Component then
2271 if Nkind (Prefix (Rref)) = N_Identifier then
2272 Rref := Prefix (Rref);
2273 elsif Nkind (Prefix (Rref)) = N_Expanded_Name then
2274 Rref := Selector_Name (Prefix (Rref));
2284 -- Write out renaming reference if we have one
2286 if Present (Rref) then
2287 Write_Info_Char ('=');
2289 (Int (Get_Logical_Line_Number (Sloc (Rref))));
2290 Write_Info_Char (':');
2292 (Int (Get_Column_Number (Sloc (Rref))));
2295 -- Indicate that the entity is in the unit of the current
2300 -- Write out information about generic parent, if entity
2303 if Is_Generic_Instance (XE.Key.Ent) then
2305 Gen_Par : constant Entity_Id :=
2308 (Unit_Declaration_Node
2310 Loc : constant Source_Ptr := Sloc (Gen_Par);
2311 Gen_U : constant Unit_Number_Type :=
2312 Get_Source_Unit (Loc);
2315 Write_Info_Char ('[');
2317 if Curru /= Gen_U then
2318 Write_Info_Nat (Dependency_Num (Gen_U));
2319 Write_Info_Char ('|');
2323 (Int (Get_Logical_Line_Number (Loc)));
2324 Write_Info_Char (']');
2328 -- See if we have a type reference and if so output
2330 Check_Type_Reference (XE.Key.Ent, False);
2332 -- Additional information for types with progenitors
2334 if Is_Record_Type (XE.Key.Ent)
2335 and then Present (Interfaces (XE.Key.Ent))
2339 First_Elmt (Interfaces (XE.Key.Ent));
2341 while Present (Elmt) loop
2342 Check_Type_Reference (Node (Elmt), True);
2347 -- For array types, list index types as well. (This is
2348 -- not C, indexes have distinct types).
2350 elsif Is_Array_Type (XE.Key.Ent) then
2354 Indx := First_Index (XE.Key.Ent);
2355 while Present (Indx) loop
2356 Check_Type_Reference
2357 (First_Subtype (Etype (Indx)), True);
2363 -- If the entity is an overriding operation, write info
2364 -- on operation that was overridden.
2366 if Is_Subprogram (XE.Key.Ent)
2367 and then Present (Overridden_Operation (XE.Key.Ent))
2369 Output_Overridden_Op
2370 (Overridden_Operation (XE.Key.Ent));
2373 -- End of processing for entity output
2375 Crloc := No_Location;
2378 -- Output the reference if it is not as the same location
2379 -- as the previous one, or it is a read-reference that
2380 -- indicates that the entity is an in-out actual in a call.
2382 if XE.Key.Loc /= No_Location
2384 (XE.Key.Loc /= Crloc
2385 or else (Prevt = 'm' and then XE.Key.Typ = 'r'))
2387 Crloc := XE.Key.Loc;
2388 Prevt := XE.Key.Typ;
2390 -- Start continuation if line full, else blank
2392 if Write_Info_Col > 72 then
2394 Write_Info_Initiate ('.');
2397 Write_Info_Char (' ');
2399 -- Output file number if changed
2401 if XE.Key.Lun /= Curru then
2402 Curru := XE.Key.Lun;
2403 Write_Info_Nat (Dependency_Num (Curru));
2404 Write_Info_Char ('|');
2408 (Int (Get_Logical_Line_Number (XE.Key.Loc)));
2409 Write_Info_Char (XE.Key.Typ);
2411 if Is_Overloadable (XE.Key.Ent)
2412 and then Is_Imported (XE.Key.Ent)
2413 and then XE.Key.Typ = 'b'
2415 Output_Import_Export_Info (XE.Key.Ent);
2418 Write_Info_Nat (Int (Get_Column_Number (XE.Key.Loc)));
2420 Output_Instantiation_Refs (Sloc (XE.Key.Ent));
2431 end Output_References;
2433 -- Start of elaboration for Lib.Xref
2436 -- Reset is necessary because Elmt_Ptr does not default to Null_Ptr,
2437 -- because it's not an access type.