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 Lib.Util; use Lib.Util;
31 with Nlists; use Nlists;
33 with Restrict; use Restrict;
34 with Rident; use Rident;
36 with Sem_Aux; use Sem_Aux;
37 with Sem_Prag; use Sem_Prag;
38 with Sem_Util; use Sem_Util;
39 with Sem_Warn; use Sem_Warn;
40 with Sinfo; use Sinfo;
41 with Sinput; use Sinput;
42 with Snames; use Snames;
43 with Stringt; use Stringt;
44 with Stand; use Stand;
45 with Table; use Table;
46 with Widechar; use Widechar;
48 with GNAT.Heap_Sort_G;
50 package body Lib.Xref is
56 -- The Xref table is used to record references. The Loc field is set
57 -- to No_Location for a definition entry.
59 subtype Xref_Entry_Number is Int;
61 type Xref_Entry is record
63 -- Entity referenced (E parameter to Generate_Reference)
66 -- Entity of the closest enclosing subprogram or package
69 -- Original source location for entity being referenced. Note that these
70 -- values are used only during the output process, they are not set when
71 -- the entries are originally built. This is because private entities
72 -- can be swapped when the initial call is made.
75 -- Location of reference (Original_Location (Sloc field of N parameter
76 -- to Generate_Reference). Set to No_Location for the case of a
77 -- defining occurrence.
80 -- Original source location for entity Sub
83 -- Reference type (Typ param to Generate_Reference)
85 Eun : Unit_Number_Type;
86 -- Unit number corresponding to Ent
88 Sun : Unit_Number_Type;
89 -- Unit number corresponding to Sub
91 Lun : Unit_Number_Type;
92 -- Unit number corresponding to Loc. Value is undefined and not
93 -- referenced if Loc is set to No_Location.
97 package Xrefs is new Table.Table (
98 Table_Component_Type => Xref_Entry,
99 Table_Index_Type => Xref_Entry_Number,
100 Table_Low_Bound => 1,
101 Table_Initial => Alloc.Xrefs_Initial,
102 Table_Increment => Alloc.Xrefs_Increment,
103 Table_Name => "Xrefs");
105 ------------------------
106 -- Local Subprograms --
107 ------------------------
109 function Enclosing_Subprogram_Or_Package (N : Node_Id) return Entity_Id;
110 -- Return the closest enclosing subprogram of package
112 function Is_Local_Reference_Type (Typ : Character) return Boolean;
113 -- Return whether Typ is a suitable reference type for a local reference
115 procedure Generate_Prim_Op_References (Typ : Entity_Id);
116 -- For a tagged type, generate implicit references to its primitive
117 -- operations, for source navigation. This is done right before emitting
118 -- cross-reference information rather than at the freeze point of the type
119 -- in order to handle late bodies that are primitive operations.
121 function Lt (T1, T2 : Xref_Entry) return Boolean;
122 -- Order cross-references
124 procedure Write_Entity_Name (E : Entity_Id; Cursrc : Source_Buffer_Ptr);
125 -- Output entity name for E. We use the occurrence from the actual
126 -- source program at the definition point.
128 -------------------------------------
129 -- Enclosing_Subprogram_Or_Package --
130 -------------------------------------
132 function Enclosing_Subprogram_Or_Package (N : Node_Id) return Entity_Id
139 exit when No (Result);
141 case Nkind (Result) is
142 when N_Package_Specification =>
143 Result := Defining_Unit_Name (Result);
146 when N_Package_Body =>
147 Result := Corresponding_Spec (Result);
150 when N_Subprogram_Specification =>
151 Result := Defining_Unit_Name (Result);
154 when N_Subprogram_Declaration =>
155 Result := Defining_Unit_Name (Specification (Result));
158 when N_Subprogram_Body =>
159 Result := Defining_Unit_Name (Specification (Result));
163 Result := Parent (Result);
167 if Nkind (Result) = N_Defining_Program_Unit_Name then
168 Result := Defining_Identifier (Result);
172 end Enclosing_Subprogram_Or_Package;
174 -------------------------
175 -- Generate_Definition --
176 -------------------------
178 procedure Generate_Definition (E : Entity_Id) is
183 pragma Assert (Nkind (E) in N_Entity);
185 -- Note that we do not test Xref_Entity_Letters here. It is too early
186 -- to do so, since we are often called before the entity is fully
187 -- constructed, so that the Ekind is still E_Void.
191 -- Definition must come from source
193 -- We make an exception for subprogram child units that have no spec.
194 -- For these we generate a subprogram declaration for library use,
195 -- and the corresponding entity does not come from source.
196 -- Nevertheless, all references will be attached to it and we have
197 -- to treat is as coming from user code.
199 and then (Comes_From_Source (E) or else Is_Child_Unit (E))
201 -- And must have a reasonable source location that is not
202 -- within an instance (all entities in instances are ignored)
204 and then Sloc (E) > No_Location
205 and then Instantiation_Location (Sloc (E)) = No_Location
207 -- And must be a non-internal name from the main source unit
209 and then In_Extended_Main_Source_Unit (E)
210 and then not Is_Internal_Name (Chars (E))
212 Xrefs.Increment_Last;
214 Loc := Original_Location (Sloc (E));
216 Xrefs.Table (Indx).Ent := E;
219 and then Nkind_In (Parent (E),
220 N_Object_Declaration,
221 N_Parameter_Specification)
223 -- In ALFA mode, define precise 'D' references for object
227 Sub : constant Entity_Id := Enclosing_Subprogram_Or_Package (E);
228 Slc : constant Source_Ptr := Original_Location (Sloc (Sub));
229 Sun : constant Unit_Number_Type := Get_Source_Unit (Slc);
231 Xrefs.Table (Indx).Typ := 'D';
232 Xrefs.Table (Indx).Sub := Sub;
233 Xrefs.Table (Indx).Def := Loc;
234 Xrefs.Table (Indx).Loc := Loc;
235 Xrefs.Table (Indx).Slc := Slc;
236 Xrefs.Table (Indx).Lun := Get_Source_Unit (Loc);
237 Xrefs.Table (Indx).Sun := Sun;
240 Xrefs.Table (Indx).Typ := ' ';
241 Xrefs.Table (Indx).Sub := Empty;
242 Xrefs.Table (Indx).Def := No_Location;
243 Xrefs.Table (Indx).Loc := No_Location;
244 Xrefs.Table (Indx).Slc := No_Location;
245 Xrefs.Table (Indx).Lun := No_Unit;
246 Xrefs.Table (Indx).Sun := No_Unit;
249 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
250 Set_Has_Xref_Entry (E);
252 if In_Inlined_Body then
256 end Generate_Definition;
258 ---------------------------------
259 -- Generate_Operator_Reference --
260 ---------------------------------
262 procedure Generate_Operator_Reference
267 if not In_Extended_Main_Source_Unit (N) then
271 -- If the operator is not a Standard operator, then we generate a real
272 -- reference to the user defined operator.
274 if Sloc (Entity (N)) /= Standard_Location then
275 Generate_Reference (Entity (N), N);
277 -- A reference to an implicit inequality operator is also a reference
278 -- to the user-defined equality.
280 if Nkind (N) = N_Op_Ne
281 and then not Comes_From_Source (Entity (N))
282 and then Present (Corresponding_Equality (Entity (N)))
284 Generate_Reference (Corresponding_Equality (Entity (N)), N);
287 -- For the case of Standard operators, we mark the result type as
288 -- referenced. This ensures that in the case where we are using a
289 -- derived operator, we mark an entity of the unit that implicitly
290 -- defines this operator as used. Otherwise we may think that no entity
291 -- of the unit is used. The actual entity marked as referenced is the
292 -- first subtype, which is the relevant user defined entity.
294 -- Note: we only do this for operators that come from source. The
295 -- generated code sometimes reaches for entities that do not need to be
296 -- explicitly visible (for example, when we expand the code for
297 -- comparing two record objects, the fields of the record may not be
300 elsif Comes_From_Source (N) then
301 Set_Referenced (First_Subtype (T));
303 end Generate_Operator_Reference;
305 ---------------------------------
306 -- Generate_Prim_Op_References --
307 ---------------------------------
309 procedure Generate_Prim_Op_References (Typ : Entity_Id) is
312 Prim_List : Elist_Id;
315 -- Handle subtypes of synchronized types
317 if Ekind (Typ) = E_Protected_Subtype
318 or else Ekind (Typ) = E_Task_Subtype
320 Base_T := Etype (Typ);
325 -- References to primitive operations are only relevant for tagged types
327 if not Is_Tagged_Type (Base_T)
328 or else Is_Class_Wide_Type (Base_T)
333 -- Ada 2005 (AI-345): For synchronized types generate reference to the
334 -- wrapper that allow us to dispatch calls through their implemented
335 -- abstract interface types.
337 -- The check for Present here is to protect against previously reported
340 Prim_List := Primitive_Operations (Base_T);
342 if No (Prim_List) then
346 Prim := First_Elmt (Prim_List);
347 while Present (Prim) loop
349 -- If the operation is derived, get the original for cross-reference
350 -- reference purposes (it is the original for which we want the xref
351 -- and for which the comes_from_source test must be performed).
354 (Typ, Ultimate_Alias (Node (Prim)), 'p', Set_Ref => False);
357 end Generate_Prim_Op_References;
359 ------------------------
360 -- Generate_Reference --
361 ------------------------
363 procedure Generate_Reference
366 Typ : Character := 'r';
367 Set_Ref : Boolean := True;
368 Force : Boolean := False)
380 -- Used for call to Find_Actual
383 -- If Formal is non-Empty, then its Ekind, otherwise E_Void
385 function Is_On_LHS (Node : Node_Id) return Boolean;
386 -- Used to check if a node is on the left hand side of an assignment.
387 -- The following cases are handled:
389 -- Variable Node is a direct descendant of left hand side of an
390 -- assignment statement.
392 -- Prefix Of an indexed or selected component that is present in
393 -- a subtree rooted by an assignment statement. There is
394 -- no restriction of nesting of components, thus cases
395 -- such as A.B (C).D are handled properly. However a prefix
396 -- of a dereference (either implicit or explicit) is never
397 -- considered as on a LHS.
399 -- Out param Same as above cases, but OUT parameter
401 function OK_To_Set_Referenced return Boolean;
402 -- Returns True if the Referenced flag can be set. There are a few
403 -- exceptions where we do not want to set this flag, see body for
404 -- details of these exceptional cases.
410 -- ??? There are several routines here and there that perform a similar
411 -- (but subtly different) computation, which should be factored:
413 -- Sem_Util.May_Be_Lvalue
414 -- Sem_Util.Known_To_Be_Assigned
415 -- Exp_Ch2.Expand_Entry_Parameter.In_Assignment_Context
416 -- Exp_Smem.Is_Out_Actual
418 function Is_On_LHS (Node : Node_Id) return Boolean is
424 -- Only identifiers are considered, is this necessary???
426 if Nkind (Node) /= N_Identifier then
430 -- Immediate return if appeared as OUT parameter
432 if Kind = E_Out_Parameter then
436 -- Search for assignment statement subtree root
443 if K = N_Assignment_Statement then
446 -- Check whether the parent is a component and the current node is
447 -- its prefix, but return False if the current node has an access
448 -- type, as in that case the selected or indexed component is an
449 -- implicit dereference, and the LHS is the designated object, not
450 -- the access object.
452 -- ??? case of a slice assignment?
454 -- ??? Note that in some cases this is called too early
455 -- (see comments in Sem_Ch8.Find_Direct_Name), at a point where
456 -- the tree is not fully typed yet. In that case we may lack
457 -- an Etype for N, and we must disable the check for an implicit
458 -- dereference. If the dereference is on an LHS, this causes a
461 elsif (K = N_Selected_Component or else K = N_Indexed_Component)
462 and then Prefix (P) = N
463 and then not (Present (Etype (N))
465 Is_Access_Type (Etype (N)))
469 -- All other cases, definitely not on left side
477 ---------------------------
478 -- OK_To_Set_Referenced --
479 ---------------------------
481 function OK_To_Set_Referenced return Boolean is
485 -- A reference from a pragma Unreferenced or pragma Unmodified or
486 -- pragma Warnings does not cause the Referenced flag to be set.
487 -- This avoids silly warnings about things being referenced and
488 -- not assigned when the only reference is from the pragma.
490 if Nkind (N) = N_Identifier then
493 if Nkind (P) = N_Pragma_Argument_Association then
496 if Nkind (P) = N_Pragma then
497 if Pragma_Name (P) = Name_Warnings
499 Pragma_Name (P) = Name_Unmodified
501 Pragma_Name (P) = Name_Unreferenced
510 end OK_To_Set_Referenced;
512 -- Start of processing for Generate_Reference
515 pragma Assert (Nkind (E) in N_Entity);
516 Find_Actual (N, Formal, Call);
518 if Present (Formal) then
519 Kind := Ekind (Formal);
524 -- Check for obsolescent reference to package ASCII. GNAT treats this
525 -- element of annex J specially since in practice, programs make a lot
526 -- of use of this feature, so we don't include it in the set of features
527 -- diagnosed when Warn_On_Obsolescent_Features mode is set. However we
528 -- are required to note it as a violation of the RM defined restriction.
530 if E = Standard_ASCII then
531 Check_Restriction (No_Obsolescent_Features, N);
534 -- Check for reference to entity marked with Is_Obsolescent
536 -- Note that we always allow obsolescent references in the compiler
537 -- itself and the run time, since we assume that we know what we are
538 -- doing in such cases. For example the calls in Ada.Characters.Handling
539 -- to its own obsolescent subprograms are just fine.
541 -- In any case we do not generate warnings within the extended source
542 -- unit of the entity in question, since we assume the source unit
543 -- itself knows what is going on (and for sure we do not want silly
544 -- warnings, e.g. on the end line of an obsolescent procedure body).
546 if Is_Obsolescent (E)
547 and then not GNAT_Mode
548 and then not In_Extended_Main_Source_Unit (E)
550 Check_Restriction (No_Obsolescent_Features, N);
552 if Warn_On_Obsolescent_Feature then
553 Output_Obsolescent_Entity_Warnings (N, E);
557 -- Warn if reference to Ada 2005 entity not in Ada 2005 mode. We only
558 -- detect real explicit references (modifications and references).
560 if Comes_From_Source (N)
561 and then Is_Ada_2005_Only (E)
562 and then Ada_Version < Ada_2005
563 and then Warn_On_Ada_2005_Compatibility
564 and then (Typ = 'm' or else Typ = 'r' or else Typ = 's')
566 Error_Msg_NE ("& is only defined in Ada 2005?", N, E);
569 -- Warn if reference to Ada 2012 entity not in Ada 2012 mode. We only
570 -- detect real explicit references (modifications and references).
572 if Comes_From_Source (N)
573 and then Is_Ada_2012_Only (E)
574 and then Ada_Version < Ada_2012
575 and then Warn_On_Ada_2012_Compatibility
576 and then (Typ = 'm' or else Typ = 'r')
578 Error_Msg_NE ("& is only defined in Ada 2012?", N, E);
581 -- Never collect references if not in main source unit. However, we omit
582 -- this test if Typ is 'e' or 'k', since these entries are structural,
583 -- and it is useful to have them in units that reference packages as
584 -- well as units that define packages. We also omit the test for the
585 -- case of 'p' since we want to include inherited primitive operations
586 -- from other packages.
588 -- We also omit this test is this is a body reference for a subprogram
589 -- instantiation. In this case the reference is to the generic body,
590 -- which clearly need not be in the main unit containing the instance.
591 -- For the same reason we accept an implicit reference generated for
592 -- a default in an instance.
594 if not In_Extended_Main_Source_Unit (N) then
600 or else (Typ = 'b' and then Is_Generic_Instance (E))
608 -- For reference type p, the entity must be in main source unit
610 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
614 -- Unless the reference is forced, we ignore references where the
615 -- reference itself does not come from source.
617 if not Force and then not Comes_From_Source (N) then
621 -- Deal with setting entity as referenced, unless suppressed. Note that
622 -- we still do Set_Referenced on entities that do not come from source.
623 -- This situation arises when we have a source reference to a derived
624 -- operation, where the derived operation itself does not come from
625 -- source, but we still want to mark it as referenced, since we really
626 -- are referencing an entity in the corresponding package (this avoids
627 -- wrong complaints that the package contains no referenced entities).
631 -- Assignable object appearing on left side of assignment or as
635 and then Is_On_LHS (N)
636 and then Ekind (E) /= E_In_Out_Parameter
638 -- For objects that are renamings, just set as simply referenced
639 -- we do not try to do assignment type tracking in this case.
641 if Present (Renamed_Object (E)) then
644 -- Out parameter case
646 elsif Kind = E_Out_Parameter then
648 -- If warning mode for all out parameters is set, or this is
649 -- the only warning parameter, then we want to mark this for
650 -- later warning logic by setting Referenced_As_Out_Parameter
652 if Warn_On_Modified_As_Out_Parameter (Formal) then
653 Set_Referenced_As_Out_Parameter (E, True);
654 Set_Referenced_As_LHS (E, False);
656 -- For OUT parameter not covered by the above cases, we simply
657 -- regard it as a normal reference (in this case we do not
658 -- want any of the warning machinery for out parameters).
664 -- For the left hand of an assignment case, we do nothing here.
665 -- The processing for Analyze_Assignment_Statement will set the
666 -- Referenced_As_LHS flag.
672 -- Check for a reference in a pragma that should not count as a
673 -- making the variable referenced for warning purposes.
675 elsif Is_Non_Significant_Pragma_Reference (N) then
678 -- A reference in an attribute definition clause does not count as a
679 -- reference except for the case of Address. The reason that 'Address
680 -- is an exception is that it creates an alias through which the
681 -- variable may be referenced.
683 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
684 and then Chars (Parent (N)) /= Name_Address
685 and then N = Name (Parent (N))
689 -- Constant completion does not count as a reference
692 and then Ekind (E) = E_Constant
696 -- Record representation clause does not count as a reference
698 elsif Nkind (N) = N_Identifier
699 and then Nkind (Parent (N)) = N_Record_Representation_Clause
703 -- Discriminants do not need to produce a reference to record type
706 and then Nkind (Parent (N)) = N_Discriminant_Specification
713 -- Special processing for IN OUT parameters, where we have an
714 -- implicit assignment to a simple variable.
716 if Kind = E_In_Out_Parameter
717 and then Is_Assignable (E)
719 -- For sure this counts as a normal read reference
722 Set_Last_Assignment (E, Empty);
724 -- We count it as being referenced as an out parameter if the
725 -- option is set to warn on all out parameters, except that we
726 -- have a special exclusion for an intrinsic subprogram, which
727 -- is most likely an instantiation of Unchecked_Deallocation
728 -- which we do not want to consider as an assignment since it
729 -- generates false positives. We also exclude the case of an
730 -- IN OUT parameter if the name of the procedure is Free,
731 -- since we suspect similar semantics.
733 if Warn_On_All_Unread_Out_Parameters
734 and then Is_Entity_Name (Name (Call))
735 and then not Is_Intrinsic_Subprogram (Entity (Name (Call)))
736 and then Chars (Name (Call)) /= Name_Free
738 Set_Referenced_As_Out_Parameter (E, True);
739 Set_Referenced_As_LHS (E, False);
742 -- Don't count a recursive reference within a subprogram as a
743 -- reference (that allows detection of a recursive subprogram
744 -- whose only references are recursive calls as unreferenced).
746 elsif Is_Subprogram (E)
747 and then E = Nearest_Dynamic_Scope (Current_Scope)
751 -- Any other occurrence counts as referencing the entity
753 elsif OK_To_Set_Referenced then
756 -- If variable, this is an OK reference after an assignment
757 -- so we can clear the Last_Assignment indication.
759 if Is_Assignable (E) then
760 Set_Last_Assignment (E, Empty);
765 -- Check for pragma Unreferenced given and reference is within
766 -- this source unit (occasion for possible warning to be issued).
768 if Has_Unreferenced (E)
769 and then In_Same_Extended_Unit (E, N)
771 -- A reference as a named parameter in a call does not count
772 -- as a violation of pragma Unreferenced for this purpose...
774 if Nkind (N) = N_Identifier
775 and then Nkind (Parent (N)) = N_Parameter_Association
776 and then Selector_Name (Parent (N)) = N
780 -- ... Neither does a reference to a variable on the left side
783 elsif Is_On_LHS (N) then
786 -- For entry formals, we want to place the warning message on the
787 -- corresponding entity in the accept statement. The current scope
788 -- is the body of the accept, so we find the formal whose name
789 -- matches that of the entry formal (there is no link between the
790 -- two entities, and the one in the accept statement is only used
791 -- for conformance checking).
793 elsif Ekind (Scope (E)) = E_Entry then
798 BE := First_Entity (Current_Scope);
799 while Present (BE) loop
800 if Chars (BE) = Chars (E) then
801 Error_Msg_NE -- CODEFIX
802 ("?pragma Unreferenced given for&!", N, BE);
810 -- Here we issue the warning, since this is a real reference
813 Error_Msg_NE -- CODEFIX
814 ("?pragma Unreferenced given for&!", N, E);
818 -- If this is a subprogram instance, mark as well the internal
819 -- subprogram in the wrapper package, which may be a visible
822 if Is_Overloadable (E)
823 and then Is_Generic_Instance (E)
824 and then Present (Alias (E))
826 Set_Referenced (Alias (E));
830 -- Generate reference if all conditions are met:
833 -- Cross referencing must be active
837 -- The entity must be one for which we collect references
839 and then Xref_Entity_Letters (Ekind (E)) /= ' '
841 -- Both Sloc values must be set to something sensible
843 and then Sloc (E) > No_Location
844 and then Sloc (N) > No_Location
846 -- We ignore references from within an instance, except for default
847 -- subprograms, for which we generate an implicit reference.
850 (Instantiation_Location (Sloc (N)) = No_Location or else Typ = 'i')
852 -- Ignore dummy references
856 if Nkind (N) = N_Identifier
858 Nkind (N) = N_Defining_Identifier
862 Nkind (N) = N_Defining_Operator_Symbol
864 Nkind (N) = N_Operator_Symbol
866 (Nkind (N) = N_Character_Literal
867 and then Sloc (Entity (N)) /= Standard_Location)
869 Nkind (N) = N_Defining_Character_Literal
873 elsif Nkind (N) = N_Expanded_Name
875 Nkind (N) = N_Selected_Component
877 Nod := Selector_Name (N);
883 -- Normal case of source entity comes from source
885 if Comes_From_Source (E) then
888 -- Entity does not come from source, but is a derived subprogram and
889 -- the derived subprogram comes from source (after one or more
890 -- derivations) in which case the reference is to parent subprogram.
892 elsif Is_Overloadable (E)
893 and then Present (Alias (E))
896 while not Comes_From_Source (Ent) loop
897 if No (Alias (Ent)) then
904 -- The internally created defining entity for a child subprogram
905 -- that has no previous spec has valid references.
907 elsif Is_Overloadable (E)
908 and then Is_Child_Unit (E)
912 -- Record components of discriminated subtypes or derived types must
913 -- be treated as references to the original component.
915 elsif Ekind (E) = E_Component
916 and then Comes_From_Source (Original_Record_Component (E))
918 Ent := Original_Record_Component (E);
920 -- If this is an expanded reference to a discriminant, recover the
921 -- original discriminant, which gets the reference.
923 elsif Ekind (E) = E_In_Parameter
924 and then Present (Discriminal_Link (E))
926 Ent := Discriminal_Link (E);
927 Set_Referenced (Ent);
929 -- Ignore reference to any other entity that is not from source
935 -- Record reference to entity
937 Sub := Enclosing_Subprogram_Or_Package (N);
939 Ref := Original_Location (Sloc (Nod));
940 Def := Original_Location (Sloc (Ent));
941 Slc := Original_Location (Sloc (Sub));
943 Xrefs.Increment_Last;
946 Xrefs.Table (Indx).Loc := Ref;
947 Xrefs.Table (Indx).Slc := Slc;
949 -- Overriding operations are marked with 'P'
952 and then Is_Subprogram (N)
953 and then Present (Overridden_Operation (N))
955 Xrefs.Table (Indx).Typ := 'P';
957 Xrefs.Table (Indx).Typ := Typ;
960 Xrefs.Table (Indx).Eun := Get_Source_Unit (Def);
961 Xrefs.Table (Indx).Lun := Get_Source_Unit (Ref);
962 Xrefs.Table (Indx).Sun := Get_Source_Unit (Slc);
963 Xrefs.Table (Indx).Ent := Ent;
964 Xrefs.Table (Indx).Sub := Sub;
965 Set_Has_Xref_Entry (Ent);
967 end Generate_Reference;
969 -----------------------------------
970 -- Generate_Reference_To_Formals --
971 -----------------------------------
973 procedure Generate_Reference_To_Formals (E : Entity_Id) is
977 if Is_Generic_Subprogram (E) then
978 Formal := First_Entity (E);
980 while Present (Formal)
981 and then not Is_Formal (Formal)
983 Next_Entity (Formal);
987 Formal := First_Formal (E);
990 while Present (Formal) loop
991 if Ekind (Formal) = E_In_Parameter then
993 if Nkind (Parameter_Type (Parent (Formal)))
994 = N_Access_Definition
996 Generate_Reference (E, Formal, '^', False);
998 Generate_Reference (E, Formal, '>', False);
1001 elsif Ekind (Formal) = E_In_Out_Parameter then
1002 Generate_Reference (E, Formal, '=', False);
1005 Generate_Reference (E, Formal, '<', False);
1008 Next_Formal (Formal);
1010 end Generate_Reference_To_Formals;
1012 -------------------------------------------
1013 -- Generate_Reference_To_Generic_Formals --
1014 -------------------------------------------
1016 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
1020 Formal := First_Entity (E);
1021 while Present (Formal) loop
1022 if Comes_From_Source (Formal) then
1023 Generate_Reference (E, Formal, 'z', False);
1026 Next_Entity (Formal);
1028 end Generate_Reference_To_Generic_Formals;
1034 procedure Initialize is
1039 -----------------------------
1040 -- Is_Local_Reference_Type --
1041 -----------------------------
1043 function Is_Local_Reference_Type (Typ : Character) return Boolean is
1045 return Typ = 'r' or else
1050 end Is_Local_Reference_Type;
1056 function Lt (T1, T2 : Xref_Entry) return Boolean is
1058 -- First test: if entity is in different unit, sort by unit
1060 if T1.Eun /= T2.Eun then
1061 return Dependency_Num (T1.Eun) < Dependency_Num (T2.Eun);
1063 -- Second test: within same unit, sort by entity Sloc
1065 elsif T1.Def /= T2.Def then
1066 return T1.Def < T2.Def;
1068 -- Third test: sort definitions ahead of references
1070 elsif T1.Loc = No_Location then
1073 elsif T2.Loc = No_Location then
1076 -- Fourth test: for same entity, sort by reference location unit
1078 elsif T1.Lun /= T2.Lun then
1079 return Dependency_Num (T1.Lun) < Dependency_Num (T2.Lun);
1081 -- Fifth test: order of location within referencing unit
1083 elsif T1.Loc /= T2.Loc then
1084 return T1.Loc < T2.Loc;
1086 -- Finally, for two locations at the same address, we prefer
1087 -- the one that does NOT have the type 'r' so that a modification
1088 -- or extension takes preference, when there are more than one
1089 -- reference at the same location. As a result, in the case of
1090 -- entities that are in-out actuals, the read reference follows
1091 -- the modify reference.
1094 return T2.Typ = 'r';
1098 -----------------------
1099 -- Output_References --
1100 -----------------------
1102 procedure Output_References is
1104 procedure Get_Type_Reference
1106 Tref : out Entity_Id;
1107 Left : out Character;
1108 Right : out Character);
1109 -- Given an Entity_Id Ent, determines whether a type reference is
1110 -- required. If so, Tref is set to the entity for the type reference
1111 -- and Left and Right are set to the left/right brackets to be output
1112 -- for the reference. If no type reference is required, then Tref is
1113 -- set to Empty, and Left/Right are set to space.
1115 procedure Output_Import_Export_Info (Ent : Entity_Id);
1116 -- Output language and external name information for an interfaced
1117 -- entity, using the format <language, external_name>,
1119 ------------------------
1120 -- Get_Type_Reference --
1121 ------------------------
1123 procedure Get_Type_Reference
1125 Tref : out Entity_Id;
1126 Left : out Character;
1127 Right : out Character)
1132 -- See if we have a type reference
1141 -- Processing for types
1143 if Is_Type (Tref) then
1145 -- Case of base type
1147 if Base_Type (Tref) = Tref then
1149 -- If derived, then get first subtype
1151 if Tref /= Etype (Tref) then
1152 Tref := First_Subtype (Etype (Tref));
1154 -- Set brackets for derived type, but don't override
1155 -- pointer case since the fact that something is a
1156 -- pointer is more important.
1163 -- If non-derived ptr, get directly designated type.
1164 -- If the type has a full view, all references are on the
1165 -- partial view, that is seen first.
1167 elsif Is_Access_Type (Tref) then
1168 Tref := Directly_Designated_Type (Tref);
1172 elsif Is_Private_Type (Tref)
1173 and then Present (Full_View (Tref))
1175 if Is_Access_Type (Full_View (Tref)) then
1176 Tref := Directly_Designated_Type (Full_View (Tref));
1180 -- If the full view is an array type, we also retrieve
1181 -- the corresponding component type, because the ali
1182 -- entry already indicates that this is an array.
1184 elsif Is_Array_Type (Full_View (Tref)) then
1185 Tref := Component_Type (Full_View (Tref));
1190 -- If non-derived array, get component type. Skip component
1191 -- type for case of String or Wide_String, saves worthwhile
1194 elsif Is_Array_Type (Tref)
1195 and then Tref /= Standard_String
1196 and then Tref /= Standard_Wide_String
1198 Tref := Component_Type (Tref);
1202 -- For other non-derived base types, nothing
1208 -- For a subtype, go to ancestor subtype
1211 Tref := Ancestor_Subtype (Tref);
1213 -- If no ancestor subtype, go to base type
1216 Tref := Base_Type (Sav);
1220 -- For objects, functions, enum literals, just get type from
1223 elsif Is_Object (Tref)
1224 or else Ekind (Tref) = E_Enumeration_Literal
1225 or else Ekind (Tref) = E_Function
1226 or else Ekind (Tref) = E_Operator
1228 Tref := Etype (Tref);
1230 -- For anything else, exit
1236 -- Exit if no type reference, or we are stuck in some loop trying
1237 -- to find the type reference, or if the type is standard void
1238 -- type (the latter is an implementation artifact that should not
1239 -- show up in the generated cross-references).
1243 or else Tref = Standard_Void_Type;
1245 -- If we have a usable type reference, return, otherwise keep
1246 -- looking for something useful (we are looking for something
1247 -- that either comes from source or standard)
1249 if Sloc (Tref) = Standard_Location
1250 or else Comes_From_Source (Tref)
1252 -- If the reference is a subtype created for a generic actual,
1253 -- go actual directly, the inner subtype is not user visible.
1255 if Nkind (Parent (Tref)) = N_Subtype_Declaration
1256 and then not Comes_From_Source (Parent (Tref))
1258 (Is_Wrapper_Package (Scope (Tref))
1259 or else Is_Generic_Instance (Scope (Tref)))
1261 Tref := First_Subtype (Base_Type (Tref));
1268 -- If we fall through the loop, no type reference
1273 end Get_Type_Reference;
1275 -------------------------------
1276 -- Output_Import_Export_Info --
1277 -------------------------------
1279 procedure Output_Import_Export_Info (Ent : Entity_Id) is
1280 Language_Name : Name_Id;
1281 Conv : constant Convention_Id := Convention (Ent);
1284 -- Generate language name from convention
1286 if Conv = Convention_C then
1287 Language_Name := Name_C;
1289 elsif Conv = Convention_CPP then
1290 Language_Name := Name_CPP;
1292 elsif Conv = Convention_Ada then
1293 Language_Name := Name_Ada;
1296 -- For the moment we ignore all other cases ???
1301 Write_Info_Char ('<');
1302 Get_Unqualified_Name_String (Language_Name);
1304 for J in 1 .. Name_Len loop
1305 Write_Info_Char (Name_Buffer (J));
1308 if Present (Interface_Name (Ent)) then
1309 Write_Info_Char (',');
1310 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
1312 for J in 1 .. Name_Len loop
1313 Write_Info_Char (Name_Buffer (J));
1317 Write_Info_Char ('>');
1318 end Output_Import_Export_Info;
1320 -- Start of processing for Output_References
1323 if not Opt.Xref_Active then
1327 -- First we add references to the primitive operations of tagged types
1328 -- declared in the main unit.
1330 Handle_Prim_Ops : declare
1334 for J in 1 .. Xrefs.Last loop
1335 Ent := Xrefs.Table (J).Ent;
1338 and then Is_Tagged_Type (Ent)
1339 and then Is_Base_Type (Ent)
1340 and then In_Extended_Main_Source_Unit (Ent)
1342 Generate_Prim_Op_References (Ent);
1345 end Handle_Prim_Ops;
1347 -- Before we go ahead and output the references we have a problem
1348 -- that needs dealing with. So far we have captured things that are
1349 -- definitely referenced by the main unit, or defined in the main
1350 -- unit. That's because we don't want to clutter up the ali file
1351 -- for this unit with definition lines for entities in other units
1352 -- that are not referenced.
1354 -- But there is a glitch. We may reference an entity in another unit,
1355 -- and it may have a type reference to an entity that is not directly
1356 -- referenced in the main unit, which may mean that there is no xref
1357 -- entry for this entity yet in the list of references.
1359 -- If we don't do something about this, we will end with an orphan type
1360 -- reference, i.e. it will point to an entity that does not appear
1361 -- within the generated references in the ali file. That is not good for
1362 -- tools using the xref information.
1364 -- To fix this, we go through the references adding definition entries
1365 -- for any unreferenced entities that can be referenced in a type
1366 -- reference. There is a recursion problem here, and that is dealt with
1367 -- by making sure that this traversal also traverses any entries that
1368 -- get added by the traversal.
1370 Handle_Orphan_Type_References : declare
1378 pragma Warnings (Off, L);
1379 pragma Warnings (Off, R);
1381 procedure New_Entry (E : Entity_Id);
1382 -- Make an additional entry into the Xref table for a type entity
1383 -- that is related to the current entity (parent, type ancestor,
1384 -- progenitor, etc.).
1390 procedure New_Entry (E : Entity_Id) is
1393 and then not Has_Xref_Entry (E)
1394 and then Sloc (E) > No_Location
1396 Xrefs.Increment_Last;
1398 Loc := Original_Location (Sloc (E));
1399 Xrefs.Table (Indx).Ent := E;
1400 Xrefs.Table (Indx).Loc := No_Location;
1401 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
1402 Xrefs.Table (Indx).Lun := No_Unit;
1403 Set_Has_Xref_Entry (E);
1407 -- Start of processing for Handle_Orphan_Type_References
1410 -- Note that this is not a for loop for a very good reason. The
1411 -- processing of items in the table can add new items to the table,
1412 -- and they must be processed as well.
1415 while J <= Xrefs.Last loop
1416 Ent := Xrefs.Table (J).Ent;
1417 Get_Type_Reference (Ent, Tref, L, R);
1420 and then not Has_Xref_Entry (Tref)
1421 and then Sloc (Tref) > No_Location
1425 if Is_Record_Type (Ent)
1426 and then Present (Interfaces (Ent))
1428 -- Add an entry for each one of the given interfaces
1429 -- implemented by type Ent.
1432 Elmt : Elmt_Id := First_Elmt (Interfaces (Ent));
1434 while Present (Elmt) loop
1435 New_Entry (Node (Elmt));
1442 -- Collect inherited primitive operations that may be declared in
1443 -- another unit and have no visible reference in the current one.
1446 and then Is_Tagged_Type (Ent)
1447 and then Is_Derived_Type (Ent)
1448 and then Is_Base_Type (Ent)
1449 and then In_Extended_Main_Source_Unit (Ent)
1452 Op_List : constant Elist_Id := Primitive_Operations (Ent);
1456 function Parent_Op (E : Entity_Id) return Entity_Id;
1457 -- Find original operation, which may be inherited through
1458 -- several derivations.
1460 function Parent_Op (E : Entity_Id) return Entity_Id is
1461 Orig_Op : constant Entity_Id := Alias (E);
1464 if No (Orig_Op) then
1467 elsif not Comes_From_Source (E)
1468 and then not Has_Xref_Entry (Orig_Op)
1469 and then Comes_From_Source (Orig_Op)
1473 return Parent_Op (Orig_Op);
1478 Op := First_Elmt (Op_List);
1479 while Present (Op) loop
1480 Prim := Parent_Op (Node (Op));
1482 if Present (Prim) then
1483 Xrefs.Increment_Last;
1485 Loc := Original_Location (Sloc (Prim));
1486 Xrefs.Table (Indx).Ent := Prim;
1487 Xrefs.Table (Indx).Loc := No_Location;
1488 Xrefs.Table (Indx).Eun :=
1489 Get_Source_Unit (Sloc (Prim));
1490 Xrefs.Table (Indx).Lun := No_Unit;
1491 Set_Has_Xref_Entry (Prim);
1501 end Handle_Orphan_Type_References;
1503 -- Now we have all the references, including those for any embedded
1504 -- type references, so we can sort them, and output them.
1506 Output_Refs : declare
1508 Nrefs : Nat := Xrefs.Last;
1509 -- Number of references in table. This value may get reset (reduced)
1510 -- when we eliminate duplicate reference entries.
1512 Rnums : array (0 .. Nrefs) of Nat;
1513 -- This array contains numbers of references in the Xrefs table.
1514 -- This list is sorted in output order. The extra 0'th entry is
1515 -- convenient for the call to sort. When we sort the table, we
1516 -- move the entries in Rnums around, but we do not move the
1517 -- original table entries.
1519 Curxu : Unit_Number_Type;
1520 -- Current xref unit
1522 Curru : Unit_Number_Type;
1523 -- Current reference unit for one entity
1525 Cursrc : Source_Buffer_Ptr;
1526 -- Current xref unit source text
1531 Curnam : String (1 .. Name_Buffer'Length);
1533 -- Simple name and length of current entity
1535 Curdef : Source_Ptr;
1536 -- Original source location for current entity
1539 -- Current reference location
1542 -- Entity type character
1545 -- reference kind of previous reference
1551 -- Renaming reference
1553 Trunit : Unit_Number_Type;
1554 -- Unit number for type reference
1556 function Lt (Op1, Op2 : Natural) return Boolean;
1557 -- Comparison function for Sort call
1559 function Name_Change (X : Entity_Id) return Boolean;
1560 -- Determines if entity X has a different simple name from Curent
1562 procedure Move (From : Natural; To : Natural);
1563 -- Move procedure for Sort call
1565 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
1571 function Lt (Op1, Op2 : Natural) return Boolean is
1572 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
1573 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
1583 procedure Move (From : Natural; To : Natural) is
1585 Rnums (Nat (To)) := Rnums (Nat (From));
1592 -- Why a string comparison here??? Why not compare Name_Id values???
1594 function Name_Change (X : Entity_Id) return Boolean is
1596 Get_Unqualified_Name_String (Chars (X));
1598 if Name_Len /= Curlen then
1601 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
1605 -- Start of processing for Output_Refs
1608 -- Capture the definition Sloc values. We delay doing this till now,
1609 -- since at the time the reference or definition is made, private
1610 -- types may be swapped, and the Sloc value may be incorrect. We
1611 -- also set up the pointer vector for the sort.
1613 for J in 1 .. Nrefs loop
1615 Xrefs.Table (J).Def :=
1616 Original_Location (Sloc (Xrefs.Table (J).Ent));
1619 -- Sort the references
1621 Sorting.Sort (Integer (Nrefs));
1623 -- Eliminate duplicate entries
1626 NR : constant Nat := Nrefs;
1629 -- We need this test for NR because if we force ALI file
1630 -- generation in case of errors detected, it may be the case
1631 -- that Nrefs is 0, so we should not reset it here
1636 for J in 2 .. NR loop
1637 if Xrefs.Table (Rnums (J)) /=
1638 Xrefs.Table (Rnums (Nrefs))
1641 Rnums (Nrefs) := Rnums (J);
1647 -- Initialize loop through references
1651 Curdef := No_Location;
1653 Crloc := No_Location;
1656 -- Loop to output references
1658 for Refno in 1 .. Nrefs loop
1659 Output_One_Ref : declare
1665 pragma Warnings (Off, WC);
1666 pragma Warnings (Off, Err);
1668 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
1669 -- The current entry to be accessed
1672 -- Used to index into source buffer to get entity name
1676 -- Used for {} or <> or () for type reference
1678 procedure Check_Type_Reference
1680 List_Interface : Boolean);
1681 -- Find whether there is a meaningful type reference for
1682 -- Ent, and display it accordingly. If List_Interface is
1683 -- true, then Ent is a progenitor interface of the current
1684 -- type entity being listed. In that case list it as is,
1685 -- without looking for a type reference for it.
1687 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
1688 -- Recursive procedure to output instantiation references for
1689 -- the given source ptr in [file|line[...]] form. No output
1690 -- if the given location is not a generic template reference.
1692 procedure Output_Overridden_Op (Old_E : Entity_Id);
1693 -- For a subprogram that is overriding, display information
1694 -- about the inherited operation that it overrides.
1696 --------------------------
1697 -- Check_Type_Reference --
1698 --------------------------
1700 procedure Check_Type_Reference
1702 List_Interface : Boolean)
1705 if List_Interface then
1707 -- This is a progenitor interface of the type for which
1708 -- xref information is being generated.
1715 Get_Type_Reference (Ent, Tref, Left, Right);
1718 if Present (Tref) then
1720 -- Case of standard entity, output name
1722 if Sloc (Tref) = Standard_Location then
1723 Write_Info_Char (Left);
1724 Write_Info_Name (Chars (Tref));
1725 Write_Info_Char (Right);
1727 -- Case of source entity, output location
1730 Write_Info_Char (Left);
1731 Trunit := Get_Source_Unit (Sloc (Tref));
1733 if Trunit /= Curxu then
1734 Write_Info_Nat (Dependency_Num (Trunit));
1735 Write_Info_Char ('|');
1739 (Int (Get_Logical_Line_Number (Sloc (Tref))));
1747 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1750 and then Present (Full_View (Ent))
1752 Ent := Underlying_Type (Ent);
1754 if Present (Ent) then
1755 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1759 Write_Info_Char (Ctyp);
1763 (Int (Get_Column_Number (Sloc (Tref))));
1765 -- If the type comes from an instantiation, add the
1766 -- corresponding info.
1768 Output_Instantiation_Refs (Sloc (Tref));
1769 Write_Info_Char (Right);
1772 end Check_Type_Reference;
1774 -------------------------------
1775 -- Output_Instantiation_Refs --
1776 -------------------------------
1778 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
1779 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
1780 Lun : Unit_Number_Type;
1781 Cu : constant Unit_Number_Type := Curru;
1784 -- Nothing to do if this is not an instantiation
1786 if Iloc = No_Location then
1790 -- Output instantiation reference
1792 Write_Info_Char ('[');
1793 Lun := Get_Source_Unit (Iloc);
1795 if Lun /= Curru then
1797 Write_Info_Nat (Dependency_Num (Curru));
1798 Write_Info_Char ('|');
1801 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
1803 -- Recursive call to get nested instantiations
1805 Output_Instantiation_Refs (Iloc);
1807 -- Output final ] after call to get proper nesting
1809 Write_Info_Char (']');
1812 end Output_Instantiation_Refs;
1814 --------------------------
1815 -- Output_Overridden_Op --
1816 --------------------------
1818 procedure Output_Overridden_Op (Old_E : Entity_Id) is
1822 -- The overridden operation has an implicit declaration
1823 -- at the point of derivation. What we want to display
1824 -- is the original operation, which has the actual body
1825 -- (or abstract declaration) that is being overridden.
1826 -- The overridden operation is not always set, e.g. when
1827 -- it is a predefined operator.
1832 -- Follow alias chain if one is present
1834 elsif Present (Alias (Old_E)) then
1836 -- The subprogram may have been implicitly inherited
1837 -- through several levels of derivation, so find the
1838 -- ultimate (source) ancestor.
1840 Op := Ultimate_Alias (Old_E);
1842 -- Normal case of no alias present
1849 and then Sloc (Op) /= Standard_Location
1852 Loc : constant Source_Ptr := Sloc (Op);
1853 Par_Unit : constant Unit_Number_Type :=
1854 Get_Source_Unit (Loc);
1857 Write_Info_Char ('<');
1859 if Par_Unit /= Curxu then
1860 Write_Info_Nat (Dependency_Num (Par_Unit));
1861 Write_Info_Char ('|');
1864 Write_Info_Nat (Int (Get_Logical_Line_Number (Loc)));
1865 Write_Info_Char ('p');
1866 Write_Info_Nat (Int (Get_Column_Number (Loc)));
1867 Write_Info_Char ('>');
1870 end Output_Overridden_Op;
1872 -- Start of processing for Output_One_Ref
1876 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1878 -- Skip reference if it is the only reference to an entity,
1879 -- and it is an END line reference, and the entity is not in
1880 -- the current extended source. This prevents junk entries
1881 -- consisting only of packages with END lines, where no
1882 -- entity from the package is actually referenced.
1885 and then Ent /= Curent
1886 and then (Refno = Nrefs or else
1887 Ent /= Xrefs.Table (Rnums (Refno + 1)).Ent)
1889 not In_Extended_Main_Source_Unit (Ent)
1894 -- For private type, get full view type
1897 and then Present (Full_View (XE.Ent))
1899 Ent := Underlying_Type (Ent);
1901 if Present (Ent) then
1902 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1906 -- Special exception for Boolean
1908 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
1912 -- For variable reference, get corresponding type
1915 Ent := Etype (XE.Ent);
1916 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1918 -- If variable is private type, get full view type
1921 and then Present (Full_View (Etype (XE.Ent)))
1923 Ent := Underlying_Type (Etype (XE.Ent));
1925 if Present (Ent) then
1926 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1929 elsif Is_Generic_Type (Ent) then
1931 -- If the type of the entity is a generic private type,
1932 -- there is no usable full view, so retain the indication
1933 -- that this is an object.
1938 -- Special handling for access parameters and objects of
1939 -- an anonymous access type.
1941 if Ekind_In (Etype (XE.Ent),
1942 E_Anonymous_Access_Type,
1943 E_Anonymous_Access_Subprogram_Type,
1944 E_Anonymous_Access_Protected_Subprogram_Type)
1946 if Is_Formal (XE.Ent)
1947 or else Ekind_In (XE.Ent, E_Variable, E_Constant)
1952 -- Special handling for Boolean
1954 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
1959 -- Special handling for abstract types and operations
1961 if Is_Overloadable (XE.Ent)
1962 and then Is_Abstract_Subprogram (XE.Ent)
1965 Ctyp := 'x'; -- Abstract procedure
1967 elsif Ctyp = 'V' then
1968 Ctyp := 'y'; -- Abstract function
1971 elsif Is_Type (XE.Ent)
1972 and then Is_Abstract_Type (XE.Ent)
1974 if Is_Interface (XE.Ent) then
1977 elsif Ctyp = 'R' then
1978 Ctyp := 'H'; -- Abstract type
1982 -- Only output reference if interesting type of entity
1986 -- Suppress references to object definitions, used for local
1989 or else XE.Typ = 'D'
1990 or else XE.Typ = 'I'
1992 -- Suppress self references, except for bodies that act as
1995 or else (XE.Loc = XE.Def
1998 or else not Is_Subprogram (XE.Ent)))
2000 -- Also suppress definitions of body formals (we only
2001 -- treat these as references, and the references were
2002 -- separately recorded).
2004 or else (Is_Formal (XE.Ent)
2005 and then Present (Spec_Entity (XE.Ent)))
2010 -- Start new Xref section if new xref unit
2012 if XE.Eun /= Curxu then
2013 if Write_Info_Col > 1 then
2018 Cursrc := Source_Text (Source_Index (Curxu));
2020 Write_Info_Initiate ('X');
2021 Write_Info_Char (' ');
2022 Write_Info_Nat (Dependency_Num (XE.Eun));
2023 Write_Info_Char (' ');
2024 Write_Info_Name (Reference_Name (Source_Index (XE.Eun)));
2027 -- Start new Entity line if new entity. Note that we
2028 -- consider two entities the same if they have the same
2029 -- name and source location. This causes entities in
2030 -- instantiations to be treated as though they referred
2037 (Name_Change (XE.Ent) or else XE.Def /= Curdef))
2042 Get_Unqualified_Name_String (Chars (XE.Ent));
2044 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
2046 if Write_Info_Col > 1 then
2050 -- Write column number information
2052 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
2053 Write_Info_Char (Ctyp);
2054 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
2056 -- Write level information
2058 Write_Level_Info : declare
2059 function Is_Visible_Generic_Entity
2060 (E : Entity_Id) return Boolean;
2061 -- Check whether E is declared in the visible part
2062 -- of a generic package. For source navigation
2063 -- purposes, treat this as a visible entity.
2065 function Is_Private_Record_Component
2066 (E : Entity_Id) return Boolean;
2067 -- Check whether E is a non-inherited component of a
2068 -- private extension. Even if the enclosing record is
2069 -- public, we want to treat the component as private
2070 -- for navigation purposes.
2072 ---------------------------------
2073 -- Is_Private_Record_Component --
2074 ---------------------------------
2076 function Is_Private_Record_Component
2077 (E : Entity_Id) return Boolean
2079 S : constant Entity_Id := Scope (E);
2082 Ekind (E) = E_Component
2083 and then Nkind (Declaration_Node (S)) =
2084 N_Private_Extension_Declaration
2085 and then Original_Record_Component (E) = E;
2086 end Is_Private_Record_Component;
2088 -------------------------------
2089 -- Is_Visible_Generic_Entity --
2090 -------------------------------
2092 function Is_Visible_Generic_Entity
2093 (E : Entity_Id) return Boolean
2098 -- The Present check here is an error defense
2100 if Present (Scope (E))
2101 and then Ekind (Scope (E)) /= E_Generic_Package
2107 while Present (Par) loop
2109 Nkind (Par) = N_Generic_Package_Declaration
2111 -- Entity is a generic formal
2116 Nkind (Parent (Par)) = N_Package_Specification
2119 Is_List_Member (Par)
2120 and then List_Containing (Par) =
2121 Visible_Declarations (Parent (Par));
2123 Par := Parent (Par);
2128 end Is_Visible_Generic_Entity;
2130 -- Start of processing for Write_Level_Info
2133 if Is_Hidden (Curent)
2134 or else Is_Private_Record_Component (Curent)
2136 Write_Info_Char (' ');
2140 or else Is_Visible_Generic_Entity (Curent)
2142 Write_Info_Char ('*');
2145 Write_Info_Char (' ');
2147 end Write_Level_Info;
2149 -- Output entity name. We use the occurrence from the
2150 -- actual source program at the definition point.
2152 P := Original_Location (Sloc (XE.Ent));
2154 -- Entity is character literal
2156 if Cursrc (P) = ''' then
2157 Write_Info_Char (Cursrc (P));
2158 Write_Info_Char (Cursrc (P + 1));
2159 Write_Info_Char (Cursrc (P + 2));
2161 -- Entity is operator symbol
2163 elsif Cursrc (P) = '"' or else Cursrc (P) = '%' then
2164 Write_Info_Char (Cursrc (P));
2169 Write_Info_Char (Cursrc (P2));
2170 exit when Cursrc (P2) = Cursrc (P);
2173 -- Entity is identifier
2177 if Is_Start_Of_Wide_Char (Cursrc, P) then
2178 Scan_Wide (Cursrc, P, WC, Err);
2179 elsif not Identifier_Char (Cursrc (P)) then
2186 -- Write out the identifier by copying the exact
2187 -- source characters used in its declaration. Note
2188 -- that this means wide characters will be in their
2189 -- original encoded form.
2192 Original_Location (Sloc (XE.Ent)) .. P - 1
2194 Write_Info_Char (Cursrc (J));
2198 -- See if we have a renaming reference
2200 if Is_Object (XE.Ent)
2201 and then Present (Renamed_Object (XE.Ent))
2203 Rref := Renamed_Object (XE.Ent);
2205 elsif Is_Overloadable (XE.Ent)
2206 and then Nkind (Parent (Declaration_Node (XE.Ent))) =
2207 N_Subprogram_Renaming_Declaration
2209 Rref := Name (Parent (Declaration_Node (XE.Ent)));
2211 elsif Ekind (XE.Ent) = E_Package
2212 and then Nkind (Declaration_Node (XE.Ent)) =
2213 N_Package_Renaming_Declaration
2215 Rref := Name (Declaration_Node (XE.Ent));
2221 if Present (Rref) then
2222 if Nkind (Rref) = N_Expanded_Name then
2223 Rref := Selector_Name (Rref);
2226 if Nkind (Rref) = N_Identifier
2227 or else Nkind (Rref) = N_Operator_Symbol
2231 -- For renamed array components, use the array name
2232 -- for the renamed entity, which reflect the fact that
2233 -- in general the whole array is aliased.
2235 elsif Nkind (Rref) = N_Indexed_Component then
2236 if Nkind (Prefix (Rref)) = N_Identifier then
2237 Rref := Prefix (Rref);
2238 elsif Nkind (Prefix (Rref)) = N_Expanded_Name then
2239 Rref := Selector_Name (Prefix (Rref));
2249 -- Write out renaming reference if we have one
2251 if Present (Rref) then
2252 Write_Info_Char ('=');
2254 (Int (Get_Logical_Line_Number (Sloc (Rref))));
2255 Write_Info_Char (':');
2257 (Int (Get_Column_Number (Sloc (Rref))));
2260 -- Indicate that the entity is in the unit of the current
2265 -- Write out information about generic parent, if entity
2268 if Is_Generic_Instance (XE.Ent) then
2270 Gen_Par : constant Entity_Id :=
2273 (Unit_Declaration_Node (XE.Ent)));
2274 Loc : constant Source_Ptr := Sloc (Gen_Par);
2275 Gen_U : constant Unit_Number_Type :=
2276 Get_Source_Unit (Loc);
2279 Write_Info_Char ('[');
2281 if Curru /= Gen_U then
2282 Write_Info_Nat (Dependency_Num (Gen_U));
2283 Write_Info_Char ('|');
2287 (Int (Get_Logical_Line_Number (Loc)));
2288 Write_Info_Char (']');
2292 -- See if we have a type reference and if so output
2294 Check_Type_Reference (XE.Ent, False);
2296 -- Additional information for types with progenitors
2298 if Is_Record_Type (XE.Ent)
2299 and then Present (Interfaces (XE.Ent))
2302 Elmt : Elmt_Id := First_Elmt (Interfaces (XE.Ent));
2304 while Present (Elmt) loop
2305 Check_Type_Reference (Node (Elmt), True);
2310 -- For array types, list index types as well. (This is
2311 -- not C, indexes have distinct types).
2313 elsif Is_Array_Type (XE.Ent) then
2317 Indx := First_Index (XE.Ent);
2318 while Present (Indx) loop
2319 Check_Type_Reference
2320 (First_Subtype (Etype (Indx)), True);
2326 -- If the entity is an overriding operation, write info
2327 -- on operation that was overridden.
2329 if Is_Subprogram (XE.Ent)
2330 and then Present (Overridden_Operation (XE.Ent))
2332 Output_Overridden_Op (Overridden_Operation (XE.Ent));
2335 -- End of processing for entity output
2337 Crloc := No_Location;
2340 -- Output the reference if it is not as the same location
2341 -- as the previous one, or it is a read-reference that
2342 -- indicates that the entity is an in-out actual in a call.
2344 if XE.Loc /= No_Location
2347 or else (Prevt = 'm' and then XE.Typ = 'r'))
2352 -- Start continuation if line full, else blank
2354 if Write_Info_Col > 72 then
2356 Write_Info_Initiate ('.');
2359 Write_Info_Char (' ');
2361 -- Output file number if changed
2363 if XE.Lun /= Curru then
2365 Write_Info_Nat (Dependency_Num (Curru));
2366 Write_Info_Char ('|');
2369 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Loc)));
2370 Write_Info_Char (XE.Typ);
2372 if Is_Overloadable (XE.Ent)
2373 and then Is_Imported (XE.Ent)
2374 and then XE.Typ = 'b'
2376 Output_Import_Export_Info (XE.Ent);
2379 Write_Info_Nat (Int (Get_Column_Number (XE.Loc)));
2381 Output_Instantiation_Refs (Sloc (XE.Ent));
2392 end Output_References;
2394 -----------------------------
2395 -- Output_Local_References --
2396 -----------------------------
2398 procedure Output_Local_References is
2400 Nrefs : Nat := Xrefs.Last;
2401 -- Number of references in table. This value may get reset (reduced)
2402 -- when we eliminate duplicate reference entries as well as references
2403 -- not suitable for local cross-references.
2405 Rnums : array (0 .. Nrefs) of Nat;
2406 -- This array contains numbers of references in the Xrefs table. This
2407 -- list is sorted in output order. The extra 0'th entry is convenient
2408 -- for the call to sort. When we sort the table, we move the entries in
2409 -- Rnums around, but we do not move the original table entries.
2411 Curxu : Unit_Number_Type;
2412 -- Current xref unit
2414 Curru : Unit_Number_Type;
2415 -- Current reference unit for one entity
2417 Cursu : Unit_Number_Type;
2418 -- Current reference unit for one enclosing subprogram
2420 Cursrc : Source_Buffer_Ptr;
2421 -- Current xref unit source text
2424 -- Current enclosing subprogram
2429 Curnam : String (1 .. Name_Buffer'Length);
2431 -- Simple name and length of current entity
2433 Curdef : Source_Ptr;
2434 -- Original source location for current entity
2437 -- Current reference location
2440 -- Entity type character
2443 -- Reference kind of previous reference
2445 function Lt (Op1, Op2 : Natural) return Boolean;
2446 -- Comparison function for Sort call
2448 function Name_Change (X : Entity_Id) return Boolean;
2449 -- Determines if entity X has a different simple name from Curent
2451 procedure Move (From : Natural; To : Natural);
2452 -- Move procedure for Sort call
2454 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
2460 function Lt (Op1, Op2 : Natural) return Boolean is
2461 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
2462 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
2465 if T1.Slc = No_Location then
2468 elsif T2.Slc = No_Location then
2471 elsif T1.Sun /= T2.Sun then
2472 return Dependency_Num (T1.Sun) < Dependency_Num (T2.Sun);
2474 elsif T1.Slc /= T2.Slc then
2475 return T1.Slc < T2.Slc;
2486 procedure Move (From : Natural; To : Natural) is
2488 Rnums (Nat (To)) := Rnums (Nat (From));
2495 -- Why a string comparison here??? Why not compare Name_Id values???
2497 function Name_Change (X : Entity_Id) return Boolean is
2499 Get_Unqualified_Name_String (Chars (X));
2501 if Name_Len /= Curlen then
2504 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
2508 -- Start of processing for Output_Subprogram_References
2511 -- Replace enclosing subprogram pointer by corresponding specification
2512 -- when appropriate. This could not be done before as the information
2513 -- was not always available when registering references.
2515 for J in 1 .. Xrefs.Last loop
2516 if Present (Xrefs.Table (J).Sub) then
2518 N : constant Node_Id :=
2519 Parent (Parent (Xrefs.Table (J).Sub));
2522 Sun : Unit_Number_Type;
2524 if Nkind (N) = N_Subprogram_Body
2525 and then not Acts_As_Spec (N)
2527 Sub := Corresponding_Spec (N);
2529 if Nkind (Sub) = N_Defining_Program_Unit_Name then
2530 Sub := Defining_Identifier (Sub);
2533 Slc := Original_Location (Sloc (Sub));
2534 Sun := Get_Source_Unit (Slc);
2536 Xrefs.Table (J).Sub := Sub;
2537 Xrefs.Table (J).Slc := Slc;
2538 Xrefs.Table (J).Sun := Sun;
2544 -- Set up the pointer vector for the sort
2546 for J in 1 .. Nrefs loop
2550 -- Sort the references
2552 Sorting.Sort (Integer (Nrefs));
2558 -- Eliminate duplicate entries
2560 -- We need this test for NR because if we force ALI file generation
2561 -- in case of errors detected, it may be the case that Nrefs is zero,
2562 -- so we should not reset it here.
2568 for J in 2 .. NR loop
2569 if Xrefs.Table (Rnums (J)) /= Xrefs.Table (Rnums (Nrefs)) then
2571 Rnums (Nrefs) := Rnums (J);
2576 -- Eliminate entries not appropriate for local references
2581 for J in 1 .. NR loop
2582 if Lref_Entity_Status (Ekind (Xrefs.Table (Rnums (J)).Ent))
2583 and then Is_Local_Reference_Type (Xrefs.Table (Rnums (J)).Typ)
2586 Rnums (Nrefs) := Rnums (J);
2591 -- Initialize loop through references
2596 Curdef := No_Location;
2599 Crloc := No_Location;
2602 -- Loop to output references
2604 for Refno in 1 .. Nrefs loop
2605 Output_One_Ref : declare
2608 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
2609 -- The current entry to be accessed
2613 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2615 -- Start new Unit section if subprogram in new unit
2617 if XE.Sun /= Cursu then
2618 if Write_Info_Col > 1 then
2624 Write_Info_Initiate ('F');
2625 Write_Info_Char (' ');
2626 Write_Info_Nat (Dependency_Num (XE.Sun));
2627 Write_Info_Char (' ');
2628 Write_Info_Name (Reference_Name (Source_Index (XE.Sun)));
2632 -- Start new Subprogram section if new subprogram
2634 if XE.Sub /= Cursub then
2635 if Write_Info_Col > 1 then
2640 Cursrc := Source_Text (Source_Index (Cursu));
2642 Write_Info_Initiate ('S');
2643 Write_Info_Char (' ');
2644 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Slc)));
2645 Write_Info_Char (Xref_Entity_Letters (Ekind (XE.Sub)));
2646 Write_Info_Nat (Int (Get_Column_Number (XE.Slc)));
2647 Write_Info_Char (' ');
2648 Write_Entity_Name (XE.Sub, Cursrc);
2650 -- Indicate that the entity is in the unit of the current local
2655 -- End of processing for subprogram output
2661 -- Start new Xref section if new xref unit
2663 if XE.Eun /= Curxu then
2664 if Write_Info_Col > 1 then
2669 Cursrc := Source_Text (Source_Index (Curxu));
2671 Write_Info_Initiate ('X');
2672 Write_Info_Char (' ');
2673 Write_Info_Nat (Dependency_Num (XE.Eun));
2674 Write_Info_Char (' ');
2675 Write_Info_Name (Reference_Name (Source_Index (XE.Eun)));
2677 -- End of processing for Xref section output
2682 -- Start new Entity line if new entity. Note that we consider two
2683 -- entities the same if they have the same name and source
2684 -- location. This causes entities in instantiations to be treated
2685 -- as though they referred to the template.
2690 and then (Name_Change (XE.Ent) or else XE.Def /= Curdef))
2695 Get_Unqualified_Name_String (Chars (XE.Ent));
2697 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
2699 if Write_Info_Col > 1 then
2703 -- Write line and column number information
2705 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
2706 Write_Info_Char (Ctyp);
2707 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
2708 Write_Info_Char (' ');
2710 -- Output entity name
2712 Write_Entity_Name (XE.Ent, Cursrc);
2714 -- End of processing for entity output
2716 Crloc := No_Location;
2719 -- Output the reference if it is not as the same location as the
2720 -- previous one, or it is a read-reference that indicates that the
2721 -- entity is an in-out actual in a call.
2723 if XE.Loc /= No_Location
2726 or else (Prevt = 'm' and then XE.Typ = 'r'))
2731 -- Start continuation if line full, else blank
2733 if Write_Info_Col > 72 then
2735 Write_Info_Initiate ('.');
2738 Write_Info_Char (' ');
2740 -- Output file number if changed
2742 if XE.Lun /= Curru then
2744 Write_Info_Nat (Dependency_Num (Curru));
2745 Write_Info_Char ('|');
2748 -- Write line and column number information
2750 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Loc)));
2751 Write_Info_Char (XE.Typ);
2752 Write_Info_Nat (Int (Get_Column_Number (XE.Loc)));
2758 end Output_Local_References;
2760 -----------------------
2761 -- Write_Entity_Name --
2762 -----------------------
2764 procedure Write_Entity_Name (E : Entity_Id; Cursrc : Source_Buffer_Ptr) is
2766 -- Used to index into source buffer to get entity name
2770 pragma Warnings (Off, WC);
2771 pragma Warnings (Off, Err);
2774 P := Original_Location (Sloc (E));
2776 -- Entity is character literal
2778 if Cursrc (P) = ''' then
2779 Write_Info_Char (Cursrc (P));
2780 Write_Info_Char (Cursrc (P + 1));
2781 Write_Info_Char (Cursrc (P + 2));
2783 -- Entity is operator symbol
2785 elsif Cursrc (P) = '"' or else Cursrc (P) = '%' then
2786 Write_Info_Char (Cursrc (P));
2791 Write_Info_Char (Cursrc (P2));
2792 exit when Cursrc (P2) = Cursrc (P);
2795 -- Entity is identifier
2799 if Is_Start_Of_Wide_Char (Cursrc, P) then
2800 Scan_Wide (Cursrc, P, WC, Err);
2801 elsif not Identifier_Char (Cursrc (P)) then
2808 -- Write out the identifier by copying the exact source characters
2809 -- used in its declaration. Note that this means wide characters will
2810 -- be in their original encoded form.
2813 Original_Location (Sloc (E)) .. P - 1
2815 Write_Info_Char (Cursrc (J));
2818 end Write_Entity_Name;