1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2020, 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;
47 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_Key is record
62 -- These are the components of Xref_Entry that participate in hash
66 -- Entity referenced (E parameter to Generate_Reference)
69 -- Location of reference (Original_Location (Sloc field of N parameter
70 -- to Generate_Reference)). Set to No_Location for the case of a
71 -- defining occurrence.
74 -- Reference type (Typ param to Generate_Reference)
76 Eun : Unit_Number_Type;
77 -- Unit number corresponding to Ent
79 Lun : Unit_Number_Type;
80 -- Unit number corresponding to Loc. Value is undefined and not
81 -- referenced if Loc is set to No_Location.
83 -- The following components are only used for SPARK cross-references
85 Ref_Scope : Entity_Id;
86 -- Entity of the closest subprogram or package enclosing the reference
88 Ent_Scope : Entity_Id;
89 -- Entity of the closest subprogram or package enclosing the definition,
90 -- which should be located in the same file as the definition itself.
93 type Xref_Entry is record
96 Ent_Scope_File : Unit_Number_Type;
97 -- File for entity Ent_Scope
100 -- Original source location for entity being referenced. Note that these
101 -- values are used only during the output process, they are not set when
102 -- the entries are originally built. This is because private entities
103 -- can be swapped when the initial call is made.
105 HTable_Next : Xref_Entry_Number;
106 -- For use only by Static_HTable
109 package Xrefs is new Table.Table (
110 Table_Component_Type => Xref_Entry,
111 Table_Index_Type => Xref_Entry_Number,
112 Table_Low_Bound => 1,
113 Table_Initial => Alloc.Xrefs_Initial,
114 Table_Increment => Alloc.Xrefs_Increment,
115 Table_Name => "Xrefs");
121 -- We keep a set of xref entries, in order to avoid inserting duplicate
122 -- entries into the above Xrefs table. An entry is in Xref_Set if and only
123 -- if it is in Xrefs.
125 Num_Buckets : constant := 2**16;
127 subtype Header_Num is Integer range 0 .. Num_Buckets - 1;
128 type Null_Type is null record;
129 pragma Unreferenced (Null_Type);
131 function Hash (F : Xref_Entry_Number) return Header_Num;
133 function Equal (F1, F2 : Xref_Entry_Number) return Boolean;
135 procedure HT_Set_Next (E : Xref_Entry_Number; Next : Xref_Entry_Number);
137 function HT_Next (E : Xref_Entry_Number) return Xref_Entry_Number;
139 function Get_Key (E : Xref_Entry_Number) return Xref_Entry_Number;
141 pragma Inline (Hash, Equal, HT_Set_Next, HT_Next, Get_Key);
143 package Xref_Set is new GNAT.HTable.Static_HTable (
145 Element => Xref_Entry,
146 Elmt_Ptr => Xref_Entry_Number,
148 Set_Next => HT_Set_Next,
150 Key => Xref_Entry_Number,
155 -----------------------------
156 -- SPARK Xrefs Information --
157 -----------------------------
159 package body SPARK_Specific is separate;
161 ------------------------
162 -- Local Subprograms --
163 ------------------------
165 procedure Add_Entry (Key : Xref_Key; Ent_Scope_File : Unit_Number_Type);
166 -- Add an entry to the tables of Xref_Entries, avoiding duplicates
168 procedure Generate_Prim_Op_References (Typ : Entity_Id);
169 -- For a tagged type, generate implicit references to its primitive
170 -- operations, for source navigation. This is done right before emitting
171 -- cross-reference information rather than at the freeze point of the type
172 -- in order to handle late bodies that are primitive operations.
174 function Lt (T1, T2 : Xref_Entry) return Boolean;
175 -- Order cross-references
181 procedure Add_Entry (Key : Xref_Key; Ent_Scope_File : Unit_Number_Type) is
183 Xrefs.Increment_Last; -- tentative
184 Xrefs.Table (Xrefs.Last).Key := Key;
186 -- Set the entry in Xref_Set, and if newly set, keep the above
187 -- tentative increment.
189 if Xref_Set.Set_If_Not_Present (Xrefs.Last) then
190 Xrefs.Table (Xrefs.Last).Ent_Scope_File := Ent_Scope_File;
191 -- Leave Def and HTable_Next uninitialized
193 Set_Has_Xref_Entry (Key.Ent);
195 -- It was already in Xref_Set, so throw away the tentatively-added entry
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;
385 Ref_Scope : Entity_Id;
387 function Get_Through_Renamings (E : Entity_Id) return Entity_Id;
388 -- Get the enclosing entity through renamings, which may come from
389 -- source or from the translation of generic instantiations.
391 function Is_On_LHS (Node : Node_Id) return Boolean;
392 -- Used to check if a node is on the left hand side of an assignment.
393 -- The following cases are handled:
395 -- Variable Node is a direct descendant of left hand side of an
396 -- assignment statement.
398 -- Prefix Of an indexed or selected component that is present in
399 -- a subtree rooted by an assignment statement. There is
400 -- no restriction of nesting of components, thus cases
401 -- such as A.B (C).D are handled properly. However a prefix
402 -- of a dereference (either implicit or explicit) is never
403 -- considered as on a LHS.
405 -- Out param Same as above cases, but OUT parameter
407 function OK_To_Set_Referenced return Boolean;
408 -- Returns True if the Referenced flag can be set. There are a few
409 -- exceptions where we do not want to set this flag, see body for
410 -- details of these exceptional cases.
412 ---------------------------
413 -- Get_Through_Renamings --
414 ---------------------------
416 function Get_Through_Renamings (E : Entity_Id) return Entity_Id is
420 -- For subprograms we just need to check once if they are have a
421 -- Renamed_Entity, because Renamed_Entity is set transitively.
423 when Subprogram_Kind =>
425 Renamed : constant Entity_Id := Renamed_Entity (E);
428 if Present (Renamed) then
435 -- For objects we need to repeatedly call Renamed_Object, because
436 -- it is not transitive.
440 Obj : Entity_Id := E;
444 pragma Assert (Present (Obj));
447 Renamed : constant Entity_Id := Renamed_Object (Obj);
450 if Present (Renamed) then
451 Obj := Get_Enclosing_Object (Renamed);
453 -- The renamed expression denotes a non-object,
454 -- e.g. function call, slicing of a function call,
455 -- pointer dereference, etc.
471 end Get_Through_Renamings;
477 -- ??? There are several routines here and there that perform a similar
478 -- (but subtly different) computation, which should be factored:
481 -- Sem_Util.May_Be_Lvalue
482 -- Sem_Util.Known_To_Be_Assigned
483 -- Exp_Ch2.Expand_Entry_Parameter.In_Assignment_Context
484 -- Exp_Smem.Is_Out_Actual
486 function Is_On_LHS (Node : Node_Id) return Boolean is
492 -- Only identifiers are considered, is this necessary???
494 if Nkind (Node) /= N_Identifier then
498 -- Immediate return if appeared as OUT parameter
500 if Kind = E_Out_Parameter then
504 -- Search for assignment statement subtree root
511 if K = N_Assignment_Statement then
514 -- Check whether the parent is a component and the current node is
515 -- its prefix, but return False if the current node has an access
516 -- type, as in that case the selected or indexed component is an
517 -- implicit dereference, and the LHS is the designated object, not
518 -- the access object.
520 -- ??? case of a slice assignment?
522 elsif (K = N_Selected_Component or else K = N_Indexed_Component)
523 and then Prefix (P) = N
525 -- Check for access type. First a special test, In some cases
526 -- this is called too early (see comments in Find_Direct_Name),
527 -- at a point where the tree is not fully typed yet. In that
528 -- case we may lack an Etype for N, and we can't check the
529 -- Etype. For now, we always return False in such a case,
530 -- but this is clearly not right in all cases ???
532 if No (Etype (N)) then
535 elsif Is_Access_Type (Etype (N)) then
538 -- Access type case dealt with, keep going
544 -- All other cases, definitely not on left side
552 ---------------------------
553 -- OK_To_Set_Referenced --
554 ---------------------------
556 function OK_To_Set_Referenced return Boolean is
560 -- A reference from a pragma Unreferenced or pragma Unmodified or
561 -- pragma Warnings does not cause the Referenced flag to be set.
562 -- This avoids silly warnings about things being referenced and
563 -- not assigned when the only reference is from the pragma.
565 if Nkind (N) = N_Identifier then
568 if Nkind (P) = N_Pragma_Argument_Association then
571 if Nkind (P) = N_Pragma then
572 if Pragma_Name_Unmapped (P) in Name_Warnings
580 -- A reference to a formal in a named parameter association does
581 -- not make the formal referenced. Formals that are unused in the
582 -- subprogram body are properly flagged as such, even if calls
583 -- elsewhere use named notation.
585 elsif Nkind (P) = N_Parameter_Association
586 and then N = Selector_Name (P)
593 end OK_To_Set_Referenced;
595 -- Start of processing for Generate_Reference
598 -- May happen in case of severe errors
600 if Nkind (E) not in N_Entity then
604 Find_Actual (N, Formal, Call);
606 if Present (Formal) then
607 Kind := Ekind (Formal);
612 -- Check for obsolescent reference to package ASCII. GNAT treats this
613 -- element of annex J specially since in practice, programs make a lot
614 -- of use of this feature, so we don't include it in the set of features
615 -- diagnosed when Warn_On_Obsolescent_Features mode is set. However we
616 -- are required to note it as a violation of the RM defined restriction.
618 if E = Standard_ASCII then
619 Check_Restriction (No_Obsolescent_Features, N);
622 -- Check for reference to entity marked with Is_Obsolescent
624 -- Note that we always allow obsolescent references in the compiler
625 -- itself and the run time, since we assume that we know what we are
626 -- doing in such cases. For example the calls in Ada.Characters.Handling
627 -- to its own obsolescent subprograms are just fine.
629 -- In any case we only generate warnings if we are in the extended main
630 -- source unit, and the entity itself is not in the extended main source
631 -- unit, since we assume the source unit itself knows what is going on
632 -- (and for sure we do not want silly warnings, e.g. on the end line of
633 -- an obsolescent procedure body).
635 if Is_Obsolescent (E)
636 and then not GNAT_Mode
637 and then not In_Extended_Main_Source_Unit (E)
638 and then In_Extended_Main_Source_Unit (N)
640 Check_Restriction (No_Obsolescent_Features, N);
642 if Warn_On_Obsolescent_Feature then
643 Output_Obsolescent_Entity_Warnings (N, E);
647 -- Warn if reference to Ada 2005 entity not in Ada 2005 mode. We only
648 -- detect real explicit references (modifications and references).
650 if Comes_From_Source (N)
651 and then Is_Ada_2005_Only (E)
652 and then Ada_Version < Ada_2005
653 and then Warn_On_Ada_2005_Compatibility
654 and then (Typ = 'm' or else Typ = 'r' or else Typ = 's')
656 Error_Msg_NE ("& is only defined in Ada 2005?y?", N, E);
659 -- Warn if reference to Ada 2012 entity not in Ada 2012 mode. We only
660 -- detect real explicit references (modifications and references).
662 if Comes_From_Source (N)
663 and then Is_Ada_2012_Only (E)
664 and then Ada_Version < Ada_2012
665 and then Warn_On_Ada_2012_Compatibility
666 and then (Typ = 'm' or else Typ = 'r')
668 Error_Msg_NE ("& is only defined in Ada 2012?y?", N, E);
671 -- Do not generate references if we are within a postcondition sub-
672 -- program, because the reference does not comes from source, and the
673 -- preanalysis of the aspect has already created an entry for the ALI
674 -- file at the proper source location.
676 if Chars (Current_Scope) = Name_uPostconditions then
680 -- Never collect references if not in main source unit. However, we omit
681 -- this test if Typ is 'e' or 'k', since these entries are structural,
682 -- and it is useful to have them in units that reference packages as
683 -- well as units that define packages. We also omit the test for the
684 -- case of 'p' since we want to include inherited primitive operations
685 -- from other packages.
687 -- We also omit this test is this is a body reference for a subprogram
688 -- instantiation. In this case the reference is to the generic body,
689 -- which clearly need not be in the main unit containing the instance.
690 -- For the same reason we accept an implicit reference generated for
691 -- a default in an instance.
693 -- We also set the referenced flag in a generic package that is not in
694 -- then main source unit, when the variable is of a formal private type,
695 -- to warn in the instance if the corresponding type is not a fully
698 if not In_Extended_Main_Source_Unit (N) then
704 or else (Typ = 'b' and then Is_Generic_Instance (E))
706 -- Allow the generation of references to reads, writes and calls
707 -- in SPARK mode when the related context comes from an instance.
711 and then In_Extended_Main_Code_Unit (N)
712 and then (Typ = 'm' or else Typ = 'r' or else Typ = 's'))
716 elsif In_Instance_Body
717 and then In_Extended_Main_Code_Unit (N)
718 and then Is_Generic_Type (Etype (E))
723 elsif Inside_A_Generic
724 and then Is_Generic_Type (Etype (E))
734 -- For reference type p, the entity must be in main source unit
736 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
740 -- Unless the reference is forced, we ignore references where the
741 -- reference itself does not come from source.
743 if not Force and then not Comes_From_Source (N) then
747 -- Deal with setting entity as referenced, unless suppressed. Note that
748 -- we still do Set_Referenced on entities that do not come from source.
749 -- This situation arises when we have a source reference to a derived
750 -- operation, where the derived operation itself does not come from
751 -- source, but we still want to mark it as referenced, since we really
752 -- are referencing an entity in the corresponding package (this avoids
753 -- wrong complaints that the package contains no referenced entities).
757 -- Assignable object appearing on left side of assignment or as
761 and then Is_On_LHS (N)
762 and then Ekind (E) /= E_In_Out_Parameter
764 -- For objects that are renamings, just set as simply referenced
765 -- we do not try to do assignment type tracking in this case.
767 if Present (Renamed_Object (E)) then
770 -- Out parameter case
772 elsif Kind = E_Out_Parameter then
774 -- If warning mode for all out parameters is set, or this is
775 -- the only warning parameter, then we want to mark this for
776 -- later warning logic by setting Referenced_As_Out_Parameter
778 if Warn_On_Modified_As_Out_Parameter (Formal) then
779 Set_Referenced_As_Out_Parameter (E, True);
780 Set_Referenced_As_LHS (E, False);
782 -- For OUT parameter not covered by the above cases, we simply
783 -- regard it as a normal reference (in this case we do not
784 -- want any of the warning machinery for out parameters).
790 -- For the left hand of an assignment case, we do nothing here.
791 -- The processing for Analyze_Assignment_Statement will set the
792 -- Referenced_As_LHS flag.
798 -- Check for a reference in a pragma that should not count as a
799 -- making the variable referenced for warning purposes.
801 elsif Is_Non_Significant_Pragma_Reference (N) then
804 -- A reference in an attribute definition clause does not count as a
805 -- reference except for the case of Address. The reason that 'Address
806 -- is an exception is that it creates an alias through which the
807 -- variable may be referenced.
809 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
810 and then Chars (Parent (N)) /= Name_Address
811 and then N = Name (Parent (N))
815 -- Constant completion does not count as a reference
818 and then Ekind (E) = E_Constant
822 -- Record representation clause does not count as a reference
824 elsif Nkind (N) = N_Identifier
825 and then Nkind (Parent (N)) = N_Record_Representation_Clause
829 -- Discriminants do not need to produce a reference to record type
832 and then Nkind (Parent (N)) = N_Discriminant_Specification
839 -- Special processing for IN OUT parameters, where we have an
840 -- implicit assignment to a simple variable.
842 if Kind = E_In_Out_Parameter
843 and then Is_Assignable (E)
845 -- For sure this counts as a normal read reference
848 Set_Last_Assignment (E, Empty);
850 -- We count it as being referenced as an out parameter if the
851 -- option is set to warn on all out parameters, except that we
852 -- have a special exclusion for an intrinsic subprogram, which
853 -- is most likely an instantiation of Unchecked_Deallocation
854 -- which we do not want to consider as an assignment since it
855 -- generates false positives. We also exclude the case of an
856 -- IN OUT parameter if the name of the procedure is Free,
857 -- since we suspect similar semantics.
859 if Warn_On_All_Unread_Out_Parameters
860 and then Is_Entity_Name (Name (Call))
861 and then not Is_Intrinsic_Subprogram (Entity (Name (Call)))
862 and then Chars (Name (Call)) /= Name_Free
864 Set_Referenced_As_Out_Parameter (E, True);
865 Set_Referenced_As_LHS (E, False);
868 -- Don't count a recursive reference within a subprogram as a
869 -- reference (that allows detection of a recursive subprogram
870 -- whose only references are recursive calls as unreferenced).
872 elsif Is_Subprogram (E)
873 and then E = Nearest_Dynamic_Scope (Current_Scope)
877 -- Any other occurrence counts as referencing the entity
879 elsif OK_To_Set_Referenced then
882 -- If variable, this is an OK reference after an assignment
883 -- so we can clear the Last_Assignment indication.
885 if Is_Assignable (E) then
886 Set_Last_Assignment (E, Empty);
891 -- Check for pragma Unreferenced given and reference is within
892 -- this source unit (occasion for possible warning to be issued).
893 -- Note that the entity may be marked as unreferenced by pragma
896 if Has_Unreferenced (E)
897 and then In_Same_Extended_Unit (E, N)
899 -- A reference as a named parameter in a call does not count as a
900 -- violation of pragma Unreferenced for this purpose...
902 if Nkind (N) = N_Identifier
903 and then Nkind (Parent (N)) = N_Parameter_Association
904 and then Selector_Name (Parent (N)) = N
908 -- ... Neither does a reference to a variable on the left side of
911 elsif Is_On_LHS (N) then
914 -- Do not consider F'Result as a violation of pragma Unreferenced
915 -- since the attribute acts as an anonymous alias of the function
916 -- result and not as a real reference to the function.
918 elsif Ekind (E) in E_Function | E_Generic_Function
919 and then Is_Entity_Name (N)
920 and then Is_Attribute_Result (Parent (N))
924 -- No warning if the reference is in a call that does not come
925 -- from source (e.g. a call to a controlled type primitive).
927 elsif not Comes_From_Source (Parent (N))
928 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
932 -- For entry formals, we want to place the warning message on the
933 -- corresponding entity in the accept statement. The current scope
934 -- is the body of the accept, so we find the formal whose name
935 -- matches that of the entry formal (there is no link between the
936 -- two entities, and the one in the accept statement is only used
937 -- for conformance checking).
939 elsif Ekind (Scope (E)) = E_Entry then
944 BE := First_Entity (Current_Scope);
945 while Present (BE) loop
946 if Chars (BE) = Chars (E) then
947 if Has_Pragma_Unused (E) then
948 Error_Msg_NE -- CODEFIX
949 ("??pragma Unused given for&!", N, BE);
951 Error_Msg_NE -- CODEFIX
952 ("??pragma Unreferenced given for&!", N, BE);
961 -- Here we issue the warning, since this is a real reference
963 elsif Has_Pragma_Unused (E) then
964 Error_Msg_NE -- CODEFIX
965 ("??pragma Unused given for&!", N, E);
967 Error_Msg_NE -- CODEFIX
968 ("??pragma Unreferenced given for&!", N, E);
972 -- If this is a subprogram instance, mark as well the internal
973 -- subprogram in the wrapper package, which may be a visible
976 if Is_Overloadable (E)
977 and then Is_Generic_Instance (E)
978 and then Present (Alias (E))
980 Set_Referenced (Alias (E));
984 -- Generate reference if all conditions are met:
987 -- Cross referencing must be active
991 -- The entity must be one for which we collect references
993 and then Xref_Entity_Letters (Ekind (E)) /= ' '
995 -- Both Sloc values must be set to something sensible
997 and then Sloc (E) > No_Location
998 and then Sloc (N) > No_Location
1000 -- Ignore references from within an instance. The only exceptions to
1001 -- this are default subprograms, for which we generate an implicit
1002 -- reference and compilations in SPARK mode.
1005 (Instantiation_Location (Sloc (N)) = No_Location
1007 or else GNATprove_Mode)
1009 -- Ignore dummy references
1013 if Nkind (N) in N_Identifier
1014 | N_Defining_Identifier
1015 | N_Defining_Operator_Symbol
1017 | N_Defining_Character_Literal
1019 or else (Nkind (N) = N_Character_Literal
1020 and then Sloc (Entity (N)) /= Standard_Location)
1024 elsif Nkind (N) in N_Expanded_Name | N_Selected_Component then
1025 Nod := Selector_Name (N);
1031 -- Normal case of source entity comes from source
1033 if Comes_From_Source (E) then
1036 -- Because a declaration may be generated for a subprogram body
1037 -- without declaration in GNATprove mode, for inlining, some
1038 -- parameters may end up being marked as not coming from source
1039 -- although they are. Take these into account specially.
1041 elsif GNATprove_Mode and then Is_Formal (E) then
1044 -- Entity does not come from source, but is a derived subprogram and
1045 -- the derived subprogram comes from source (after one or more
1046 -- derivations) in which case the reference is to parent subprogram.
1048 elsif Is_Overloadable (E)
1049 and then Present (Alias (E))
1052 while not Comes_From_Source (Ent) loop
1053 if No (Alias (Ent)) then
1060 -- The internally created defining entity for a child subprogram
1061 -- that has no previous spec has valid references.
1063 elsif Is_Overloadable (E)
1064 and then Is_Child_Unit (E)
1068 -- Ditto for the formals of such a subprogram
1070 elsif Is_Overloadable (Scope (E))
1071 and then Is_Child_Unit (Scope (E))
1075 -- Record components of discriminated subtypes or derived types must
1076 -- be treated as references to the original component.
1078 elsif Ekind (E) = E_Component
1079 and then Comes_From_Source (Original_Record_Component (E))
1081 Ent := Original_Record_Component (E);
1083 -- If this is an expanded reference to a discriminant, recover the
1084 -- original discriminant, which gets the reference.
1086 elsif Ekind (E) = E_In_Parameter
1087 and then Present (Discriminal_Link (E))
1089 Ent := Discriminal_Link (E);
1090 Set_Referenced (Ent);
1092 -- Ignore reference to any other entity that is not from source
1098 -- In SPARK mode, consider the underlying entity renamed instead of
1099 -- the renaming, which is needed to compute a valid set of effects
1100 -- (reads, writes) for the enclosing subprogram.
1102 if GNATprove_Mode then
1103 Ent := Get_Through_Renamings (Ent);
1105 -- If no enclosing object, then it could be a reference to any
1106 -- location not tracked individually, like heap-allocated data.
1107 -- Conservatively approximate this possibility by generating a
1108 -- dereference, and return.
1111 if Actual_Typ = 'w' then
1112 SPARK_Specific.Generate_Dereference (Nod, 'r');
1113 SPARK_Specific.Generate_Dereference (Nod, 'w');
1115 SPARK_Specific.Generate_Dereference (Nod, 'r');
1122 -- Record reference to entity
1125 and then Is_Subprogram (Nod)
1126 and then Present (Overridden_Operation (Nod))
1131 -- Comment needed here for special SPARK code ???
1133 if GNATprove_Mode then
1135 -- Ignore references to an entity which is a Part_Of single
1136 -- concurrent object. Ideally we would prefer to add it as a
1137 -- reference to the corresponding concurrent type, but it is quite
1138 -- difficult (as such references are not currently added even for)
1139 -- reads/writes of private protected components) and not worth the
1142 if Ekind (Ent) in E_Abstract_State | E_Constant | E_Variable
1143 and then Present (Encapsulating_State (Ent))
1144 and then Is_Single_Concurrent_Object (Encapsulating_State (Ent))
1153 SPARK_Specific.Enclosing_Subprogram_Or_Library_Package (Nod);
1155 SPARK_Specific.Enclosing_Subprogram_Or_Library_Package (Ent);
1157 -- Since we are reaching through renamings in SPARK mode, we may
1158 -- end up with standard constants. Ignore those.
1160 if Sloc (Ent_Scope) <= Standard_Location
1161 or else Def <= Standard_Location
1170 Eun => Get_Top_Level_Code_Unit (Def),
1171 Lun => Get_Top_Level_Code_Unit (Ref),
1172 Ref_Scope => Ref_Scope,
1173 Ent_Scope => Ent_Scope),
1174 Ent_Scope_File => Get_Top_Level_Code_Unit (Ent));
1177 Ref := Original_Location (Sloc (Nod));
1178 Def := Original_Location (Sloc (Ent));
1180 -- If this is an operator symbol, skip the initial quote for
1181 -- navigation purposes. This is not done for the end label,
1182 -- where we want the actual position after the closing quote.
1187 elsif Nkind (N) = N_Defining_Operator_Symbol
1188 or else Nkind (Nod) = N_Operator_Symbol
1197 Eun => Get_Source_Unit (Def),
1198 Lun => Get_Source_Unit (Ref),
1200 Ent_Scope => Empty),
1201 Ent_Scope_File => No_Unit);
1203 -- Generate reference to the first private entity
1206 and then Comes_From_Source (E)
1207 and then Nkind (Ent) = N_Defining_Identifier
1208 and then (Is_Package_Or_Generic_Package (Ent)
1209 or else Is_Concurrent_Type (Ent))
1210 and then Present (First_Private_Entity (E))
1211 and then In_Extended_Main_Source_Unit (N)
1213 -- Handle case in which the full-view and partial-view of the
1214 -- first private entity are swapped.
1217 First_Private : Entity_Id := First_Private_Entity (E);
1220 if Is_Private_Type (First_Private)
1221 and then Present (Full_View (First_Private))
1223 First_Private := Full_View (First_Private);
1228 Loc => Sloc (First_Private),
1230 Eun => Get_Source_Unit (Def),
1231 Lun => Get_Source_Unit (Ref),
1233 Ent_Scope => Empty),
1234 Ent_Scope_File => No_Unit);
1239 end Generate_Reference;
1241 -----------------------------------
1242 -- Generate_Reference_To_Formals --
1243 -----------------------------------
1245 procedure Generate_Reference_To_Formals (E : Entity_Id) is
1249 if Is_Generic_Subprogram (E) then
1250 Formal := First_Entity (E);
1252 while Present (Formal)
1253 and then not Is_Formal (Formal)
1255 Next_Entity (Formal);
1258 elsif Ekind (E) in Access_Subprogram_Kind then
1259 Formal := First_Formal (Designated_Type (E));
1262 Formal := First_Formal (E);
1265 while Present (Formal) loop
1266 if Ekind (Formal) = E_In_Parameter then
1268 if Nkind (Parameter_Type (Parent (Formal))) = N_Access_Definition
1270 Generate_Reference (E, Formal, '^', False);
1272 Generate_Reference (E, Formal, '>', False);
1275 elsif Ekind (Formal) = E_In_Out_Parameter then
1276 Generate_Reference (E, Formal, '=', False);
1279 Generate_Reference (E, Formal, '<', False);
1282 Next_Formal (Formal);
1284 end Generate_Reference_To_Formals;
1286 -------------------------------------------
1287 -- Generate_Reference_To_Generic_Formals --
1288 -------------------------------------------
1290 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
1294 Formal := First_Entity (E);
1295 while Present (Formal) loop
1296 if Comes_From_Source (Formal) then
1297 Generate_Reference (E, Formal, 'z', False);
1300 Next_Entity (Formal);
1302 end Generate_Reference_To_Generic_Formals;
1308 function Get_Key (E : Xref_Entry_Number) return Xref_Entry_Number is
1313 ----------------------------
1314 -- Has_Deferred_Reference --
1315 ----------------------------
1317 function Has_Deferred_Reference (Ent : Entity_Id) return Boolean is
1319 for J in Deferred_References.First .. Deferred_References.Last loop
1320 if Deferred_References.Table (J).E = Ent then
1326 end Has_Deferred_Reference;
1332 function Hash (F : Xref_Entry_Number) return Header_Num is
1333 -- It is unlikely to have two references to the same entity at the same
1334 -- source location, so the hash function depends only on the Ent and Loc
1337 XE : Xref_Entry renames Xrefs.Table (F);
1338 type M is mod 2**32;
1340 H : constant M := M (XE.Key.Ent) + 2 ** 7 * M (abs XE.Key.Loc);
1341 -- It would be more natural to write:
1343 -- H : constant M := M'Mod (XE.Key.Ent) + 2**7 * M'Mod (XE.Key.Loc);
1345 -- But we can't use M'Mod, because it prevents bootstrapping with older
1346 -- compilers. Loc can be negative, so we do "abs" before converting.
1347 -- One day this can be cleaned up ???
1350 return Header_Num (H mod Num_Buckets);
1357 procedure HT_Set_Next (E : Xref_Entry_Number; Next : Xref_Entry_Number) is
1359 Xrefs.Table (E).HTable_Next := Next;
1366 function HT_Next (E : Xref_Entry_Number) return Xref_Entry_Number is
1368 return Xrefs.Table (E).HTable_Next;
1375 procedure Initialize is
1384 function Lt (T1, T2 : Xref_Entry) return Boolean is
1386 -- First test: if entity is in different unit, sort by unit
1388 if T1.Key.Eun /= T2.Key.Eun then
1389 return Dependency_Num (T1.Key.Eun) < Dependency_Num (T2.Key.Eun);
1391 -- Second test: within same unit, sort by entity Sloc
1393 elsif T1.Def /= T2.Def then
1394 return T1.Def < T2.Def;
1396 -- Third test: sort definitions ahead of references
1398 elsif T1.Key.Loc = No_Location then
1401 elsif T2.Key.Loc = No_Location then
1404 -- Fourth test: for same entity, sort by reference location unit
1406 elsif T1.Key.Lun /= T2.Key.Lun then
1407 return Dependency_Num (T1.Key.Lun) < Dependency_Num (T2.Key.Lun);
1409 -- Fifth test: order of location within referencing unit
1411 elsif T1.Key.Loc /= T2.Key.Loc then
1412 return T1.Key.Loc < T2.Key.Loc;
1414 -- Finally, for two locations at the same address, we prefer
1415 -- the one that does NOT have the type 'r' so that a modification
1416 -- or extension takes preference, when there are more than one
1417 -- reference at the same location. As a result, in the case of
1418 -- entities that are in-out actuals, the read reference follows
1419 -- the modify reference.
1422 return T2.Key.Typ = 'r';
1426 -----------------------
1427 -- Output_References --
1428 -----------------------
1430 procedure Output_References is
1432 procedure Get_Type_Reference
1434 Tref : out Entity_Id;
1435 Left : out Character;
1436 Right : out Character);
1437 -- Given an Entity_Id Ent, determines whether a type reference is
1438 -- required. If so, Tref is set to the entity for the type reference
1439 -- and Left and Right are set to the left/right brackets to be output
1440 -- for the reference. If no type reference is required, then Tref is
1441 -- set to Empty, and Left/Right are set to space.
1443 procedure Output_Import_Export_Info (Ent : Entity_Id);
1444 -- Output language and external name information for an interfaced
1445 -- entity, using the format <language, external_name>.
1447 ------------------------
1448 -- Get_Type_Reference --
1449 ------------------------
1451 procedure Get_Type_Reference
1453 Tref : out Entity_Id;
1454 Left : out Character;
1455 Right : out Character)
1460 -- See if we have a type reference
1469 -- Processing for types
1471 if Is_Type (Tref) then
1473 -- Case of base type
1475 if Base_Type (Tref) = Tref then
1477 -- If derived, then get first subtype
1479 if Tref /= Etype (Tref) then
1480 Tref := First_Subtype (Etype (Tref));
1482 -- Set brackets for derived type, but don't override
1483 -- pointer case since the fact that something is a
1484 -- pointer is more important.
1491 -- If the completion of a private type is itself a derived
1492 -- type, we need the parent of the full view.
1494 elsif Is_Private_Type (Tref)
1495 and then Present (Full_View (Tref))
1496 and then Etype (Full_View (Tref)) /= Full_View (Tref)
1498 Tref := Etype (Full_View (Tref));
1505 -- If non-derived pointer, get directly designated type.
1506 -- If the type has a full view, all references are on the
1507 -- partial view that is seen first.
1509 elsif Is_Access_Type (Tref) then
1510 Tref := Directly_Designated_Type (Tref);
1514 elsif Is_Private_Type (Tref)
1515 and then Present (Full_View (Tref))
1517 if Is_Access_Type (Full_View (Tref)) then
1518 Tref := Directly_Designated_Type (Full_View (Tref));
1522 -- If the full view is an array type, we also retrieve
1523 -- the corresponding component type, because the ali
1524 -- entry already indicates that this is an array.
1526 elsif Is_Array_Type (Full_View (Tref)) then
1527 Tref := Component_Type (Full_View (Tref));
1532 -- If non-derived array, get component type. Skip component
1533 -- type for case of String or Wide_String, saves worthwhile
1536 elsif Is_Array_Type (Tref)
1537 and then Tref /= Standard_String
1538 and then Tref /= Standard_Wide_String
1540 Tref := Component_Type (Tref);
1544 -- For other non-derived base types, nothing
1550 -- For a subtype, go to ancestor subtype
1553 Tref := Ancestor_Subtype (Tref);
1555 -- If no ancestor subtype, go to base type
1558 Tref := Base_Type (Sav);
1562 -- For objects, functions, enum literals, just get type from
1565 elsif Is_Object (Tref)
1566 or else Ekind (Tref) = E_Enumeration_Literal
1567 or else Ekind (Tref) = E_Function
1568 or else Ekind (Tref) = E_Operator
1570 Tref := Etype (Tref);
1572 -- Another special case: an object of a classwide type
1573 -- initialized with a tag-indeterminate call gets a subtype
1574 -- of the classwide type during expansion. See if the original
1575 -- type in the declaration is named, and return it instead
1576 -- of going to the root type. The expression may be a class-
1577 -- wide function call whose result is on the secondary stack,
1578 -- which forces the declaration to be rewritten as a renaming,
1579 -- so examine the source declaration.
1581 if Ekind (Tref) = E_Class_Wide_Subtype then
1583 Decl : constant Node_Id := Original_Node (Parent (Ent));
1585 if Nkind (Decl) = N_Object_Declaration
1586 and then Is_Entity_Name
1587 (Original_Node (Object_Definition (Decl)))
1590 Entity (Original_Node (Object_Definition (Decl)));
1594 -- For a function that returns a class-wide type, Tref is
1597 elsif Is_Overloadable (Ent)
1598 and then Is_Class_Wide_Type (Tref)
1603 -- For anything else, exit
1609 -- Exit if no type reference, or we are stuck in some loop trying
1610 -- to find the type reference, or if the type is standard void
1611 -- type (the latter is an implementation artifact that should not
1612 -- show up in the generated cross-references).
1616 or else Tref = Standard_Void_Type;
1618 -- If we have a usable type reference, return, otherwise keep
1619 -- looking for something useful (we are looking for something
1620 -- that either comes from source or standard)
1622 if Sloc (Tref) = Standard_Location
1623 or else Comes_From_Source (Tref)
1625 -- If the reference is a subtype created for a generic actual,
1626 -- go actual directly, the inner subtype is not user visible.
1628 if Nkind (Parent (Tref)) = N_Subtype_Declaration
1629 and then not Comes_From_Source (Parent (Tref))
1631 (Is_Wrapper_Package (Scope (Tref))
1632 or else Is_Generic_Instance (Scope (Tref)))
1634 Tref := First_Subtype (Base_Type (Tref));
1641 -- If we fall through the loop, no type reference
1646 end Get_Type_Reference;
1648 -------------------------------
1649 -- Output_Import_Export_Info --
1650 -------------------------------
1652 procedure Output_Import_Export_Info (Ent : Entity_Id) is
1653 Language_Name : Name_Id;
1654 Conv : constant Convention_Id := Convention (Ent);
1657 -- Generate language name from convention
1659 if Conv = Convention_C or else Conv in Convention_C_Variadic then
1660 Language_Name := Name_C;
1662 elsif Conv = Convention_CPP then
1663 Language_Name := Name_CPP;
1665 elsif Conv = Convention_Ada then
1666 Language_Name := Name_Ada;
1669 -- For the moment we ignore all other cases ???
1674 Write_Info_Char ('<');
1675 Get_Unqualified_Name_String (Language_Name);
1677 for J in 1 .. Name_Len loop
1678 Write_Info_Char (Name_Buffer (J));
1681 if Present (Interface_Name (Ent)) then
1682 Write_Info_Char (',');
1683 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
1685 for J in 1 .. Name_Len loop
1686 Write_Info_Char (Name_Buffer (J));
1690 Write_Info_Char ('>');
1691 end Output_Import_Export_Info;
1693 -- Start of processing for Output_References
1696 -- First we add references to the primitive operations of tagged types
1697 -- declared in the main unit.
1699 Handle_Prim_Ops : declare
1703 for J in 1 .. Xrefs.Last loop
1704 Ent := Xrefs.Table (J).Key.Ent;
1707 and then Is_Tagged_Type (Ent)
1708 and then Is_Base_Type (Ent)
1709 and then In_Extended_Main_Source_Unit (Ent)
1711 Generate_Prim_Op_References (Ent);
1714 end Handle_Prim_Ops;
1716 -- Before we go ahead and output the references we have a problem
1717 -- that needs dealing with. So far we have captured things that are
1718 -- definitely referenced by the main unit, or defined in the main
1719 -- unit. That's because we don't want to clutter up the ali file
1720 -- for this unit with definition lines for entities in other units
1721 -- that are not referenced.
1723 -- But there is a glitch. We may reference an entity in another unit,
1724 -- and it may have a type reference to an entity that is not directly
1725 -- referenced in the main unit, which may mean that there is no xref
1726 -- entry for this entity yet in the list of references.
1728 -- If we don't do something about this, we will end with an orphan type
1729 -- reference, i.e. it will point to an entity that does not appear
1730 -- within the generated references in the ali file. That is not good for
1731 -- tools using the xref information.
1733 -- To fix this, we go through the references adding definition entries
1734 -- for any unreferenced entities that can be referenced in a type
1735 -- reference. There is a recursion problem here, and that is dealt with
1736 -- by making sure that this traversal also traverses any entries that
1737 -- get added by the traversal.
1739 Handle_Orphan_Type_References : declare
1745 pragma Warnings (Off, L);
1746 pragma Warnings (Off, R);
1748 procedure New_Entry (E : Entity_Id);
1749 -- Make an additional entry into the Xref table for a type entity
1750 -- that is related to the current entity (parent, type ancestor,
1751 -- progenitor, etc.).
1757 procedure New_Entry (E : Entity_Id) is
1759 pragma Assert (Present (E));
1761 if not Has_Xref_Entry (Implementation_Base_Type (E))
1762 and then Sloc (E) > No_Location
1767 Typ => Character'First,
1768 Eun => Get_Source_Unit (Original_Location (Sloc (E))),
1771 Ent_Scope => Empty),
1772 Ent_Scope_File => No_Unit);
1776 -- Start of processing for Handle_Orphan_Type_References
1779 -- Note that this is not a for loop for a very good reason. The
1780 -- processing of items in the table can add new items to the table,
1781 -- and they must be processed as well.
1784 while J <= Xrefs.Last loop
1785 Ent := Xrefs.Table (J).Key.Ent;
1787 -- Do not generate reference information for an ignored Ghost
1788 -- entity because neither the entity nor its references will
1789 -- appear in the final tree.
1791 if Is_Ignored_Ghost_Entity (Ent) then
1792 goto Orphan_Continue;
1795 Get_Type_Reference (Ent, Tref, L, R);
1798 and then not Has_Xref_Entry (Tref)
1799 and then Sloc (Tref) > No_Location
1803 if Is_Record_Type (Ent)
1804 and then Present (Interfaces (Ent))
1806 -- Add an entry for each one of the given interfaces
1807 -- implemented by type Ent.
1810 Elmt : Elmt_Id := First_Elmt (Interfaces (Ent));
1812 while Present (Elmt) loop
1813 New_Entry (Node (Elmt));
1820 -- Collect inherited primitive operations that may be declared in
1821 -- another unit and have no visible reference in the current one.
1824 and then Is_Tagged_Type (Ent)
1825 and then Is_Derived_Type (Ent)
1826 and then Is_Base_Type (Ent)
1827 and then In_Extended_Main_Source_Unit (Ent)
1830 Op_List : constant Elist_Id := Primitive_Operations (Ent);
1834 function Parent_Op (E : Entity_Id) return Entity_Id;
1835 -- Find original operation, which may be inherited through
1836 -- several derivations.
1838 function Parent_Op (E : Entity_Id) return Entity_Id is
1839 Orig_Op : constant Entity_Id := Alias (E);
1842 if No (Orig_Op) then
1845 elsif not Comes_From_Source (E)
1846 and then not Has_Xref_Entry (Orig_Op)
1847 and then Comes_From_Source (Orig_Op)
1851 return Parent_Op (Orig_Op);
1856 Op := First_Elmt (Op_List);
1857 while Present (Op) loop
1858 Prim := Parent_Op (Node (Op));
1860 if Present (Prim) then
1864 Typ => Character'First,
1865 Eun => Get_Source_Unit (Sloc (Prim)),
1868 Ent_Scope => Empty),
1869 Ent_Scope_File => No_Unit);
1880 end Handle_Orphan_Type_References;
1882 -- Now we have all the references, including those for any embedded type
1883 -- references, so we can sort them, and output them.
1885 Output_Refs : declare
1886 Nrefs : constant Nat := Xrefs.Last;
1887 -- Number of references in table
1889 Rnums : array (0 .. Nrefs) of Nat;
1890 -- This array contains numbers of references in the Xrefs table.
1891 -- This list is sorted in output order. The extra 0'th entry is
1892 -- convenient for the call to sort. When we sort the table, we
1893 -- move the entries in Rnums around, but we do not move the
1894 -- original table entries.
1896 Curxu : Unit_Number_Type;
1897 -- Current xref unit
1899 Curru : Unit_Number_Type;
1900 -- Current reference unit for one entity
1905 Curnam : String (1 .. Name_Buffer'Length);
1907 -- Simple name and length of current entity
1909 Curdef : Source_Ptr;
1910 -- Original source location for current entity
1913 -- Current reference location
1916 -- Entity type character
1919 -- reference kind of previous reference
1925 -- Renaming reference
1927 Trunit : Unit_Number_Type;
1928 -- Unit number for type reference
1930 function Lt (Op1, Op2 : Natural) return Boolean;
1931 -- Comparison function for Sort call
1933 function Name_Change (X : Entity_Id) return Boolean;
1934 -- Determines if entity X has a different simple name from Curent
1936 procedure Move (From : Natural; To : Natural);
1937 -- Move procedure for Sort call
1939 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
1945 function Lt (Op1, Op2 : Natural) return Boolean is
1946 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
1947 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
1957 procedure Move (From : Natural; To : Natural) is
1959 Rnums (Nat (To)) := Rnums (Nat (From));
1966 -- Why a string comparison here??? Why not compare Name_Id values???
1968 function Name_Change (X : Entity_Id) return Boolean is
1970 Get_Unqualified_Name_String (Chars (X));
1972 if Name_Len /= Curlen then
1975 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
1979 -- Start of processing for Output_Refs
1982 -- Capture the definition Sloc values. We delay doing this till now,
1983 -- since at the time the reference or definition is made, private
1984 -- types may be swapped, and the Sloc value may be incorrect. We
1985 -- also set up the pointer vector for the sort.
1987 -- For user-defined operators we need to skip the initial quote and
1988 -- point to the first character of the name, for navigation purposes.
1990 for J in 1 .. Nrefs loop
1992 E : constant Entity_Id := Xrefs.Table (J).Key.Ent;
1993 Loc : constant Source_Ptr := Original_Location (Sloc (E));
1998 if Nkind (E) = N_Defining_Operator_Symbol then
1999 Xrefs.Table (J).Def := Loc + 1;
2001 Xrefs.Table (J).Def := Loc;
2006 -- Sort the references
2008 Sorting.Sort (Integer (Nrefs));
2010 -- Initialize loop through references
2014 Curdef := No_Location;
2016 Crloc := No_Location;
2019 -- Loop to output references
2021 for Refno in 1 .. Nrefs loop
2022 Output_One_Ref : declare
2025 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
2026 -- The current entry to be accessed
2030 -- Used for {} or <> or () for type reference
2032 procedure Check_Type_Reference
2034 List_Interface : Boolean;
2035 Is_Component : Boolean := False);
2036 -- Find whether there is a meaningful type reference for
2037 -- Ent, and display it accordingly. If List_Interface is
2038 -- true, then Ent is a progenitor interface of the current
2039 -- type entity being listed. In that case list it as is,
2040 -- without looking for a type reference for it. Flag is also
2041 -- used for index types of an array type, where the caller
2042 -- supplies the intended type reference. Is_Component serves
2043 -- the same purpose, to display the component type of a
2044 -- derived array type, for which only the parent type has
2045 -- ben displayed so far.
2047 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
2048 -- Recursive procedure to output instantiation references for
2049 -- the given source ptr in [file|line[...]] form. No output
2050 -- if the given location is not a generic template reference.
2052 procedure Output_Overridden_Op (Old_E : Entity_Id);
2053 -- For a subprogram that is overriding, display information
2054 -- about the inherited operation that it overrides.
2056 --------------------------
2057 -- Check_Type_Reference --
2058 --------------------------
2060 procedure Check_Type_Reference
2062 List_Interface : Boolean;
2063 Is_Component : Boolean := False)
2066 if List_Interface then
2068 -- This is a progenitor interface of the type for which
2069 -- xref information is being generated.
2075 -- The following is not documented in lib-xref.ads ???
2077 elsif Is_Component then
2083 Get_Type_Reference (Ent, Tref, Left, Right);
2086 if Present (Tref) then
2088 -- Case of standard entity, output name
2090 if Sloc (Tref) = Standard_Location then
2091 Write_Info_Char (Left);
2092 Write_Info_Name (Chars (Tref));
2093 Write_Info_Char (Right);
2095 -- Case of source entity, output location
2098 Write_Info_Char (Left);
2099 Trunit := Get_Source_Unit (Sloc (Tref));
2101 if Trunit /= Curxu then
2102 Write_Info_Nat (Dependency_Num (Trunit));
2103 Write_Info_Char ('|');
2107 (Int (Get_Logical_Line_Number (Sloc (Tref))));
2115 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2118 and then Present (Full_View (Ent))
2120 Ent := Underlying_Type (Ent);
2122 if Present (Ent) then
2123 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2127 Write_Info_Char (Ctyp);
2131 (Int (Get_Column_Number (Sloc (Tref))));
2133 -- If the type comes from an instantiation, add the
2134 -- corresponding info.
2136 Output_Instantiation_Refs (Sloc (Tref));
2137 Write_Info_Char (Right);
2140 end Check_Type_Reference;
2142 -------------------------------
2143 -- Output_Instantiation_Refs --
2144 -------------------------------
2146 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
2147 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
2148 Lun : Unit_Number_Type;
2149 Cu : constant Unit_Number_Type := Curru;
2152 -- Nothing to do if this is not an instantiation
2154 if Iloc = No_Location then
2158 -- Output instantiation reference
2160 Write_Info_Char ('[');
2161 Lun := Get_Source_Unit (Iloc);
2163 if Lun /= Curru then
2165 Write_Info_Nat (Dependency_Num (Curru));
2166 Write_Info_Char ('|');
2169 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
2171 -- Recursive call to get nested instantiations
2173 Output_Instantiation_Refs (Iloc);
2175 -- Output final ] after call to get proper nesting
2177 Write_Info_Char (']');
2180 end Output_Instantiation_Refs;
2182 --------------------------
2183 -- Output_Overridden_Op --
2184 --------------------------
2186 procedure Output_Overridden_Op (Old_E : Entity_Id) is
2190 -- The overridden operation has an implicit declaration
2191 -- at the point of derivation. What we want to display
2192 -- is the original operation, which has the actual body
2193 -- (or abstract declaration) that is being overridden.
2194 -- The overridden operation is not always set, e.g. when
2195 -- it is a predefined operator.
2200 -- Follow alias chain if one is present
2202 elsif Present (Alias (Old_E)) then
2204 -- The subprogram may have been implicitly inherited
2205 -- through several levels of derivation, so find the
2206 -- ultimate (source) ancestor.
2208 Op := Ultimate_Alias (Old_E);
2210 -- Normal case of no alias present. We omit generated
2211 -- primitives like tagged equality, that have no source
2219 and then Sloc (Op) /= Standard_Location
2220 and then Comes_From_Source (Op)
2223 Loc : constant Source_Ptr := Sloc (Op);
2224 Par_Unit : constant Unit_Number_Type :=
2225 Get_Source_Unit (Loc);
2228 Write_Info_Char ('<');
2230 if Par_Unit /= Curxu then
2231 Write_Info_Nat (Dependency_Num (Par_Unit));
2232 Write_Info_Char ('|');
2235 Write_Info_Nat (Int (Get_Logical_Line_Number (Loc)));
2236 Write_Info_Char ('p');
2237 Write_Info_Nat (Int (Get_Column_Number (Loc)));
2238 Write_Info_Char ('>');
2241 end Output_Overridden_Op;
2243 -- Start of processing for Output_One_Ref
2248 -- Do not generate reference information for an ignored Ghost
2249 -- entity because neither the entity nor its references will
2250 -- appear in the final tree.
2252 if Is_Ignored_Ghost_Entity (Ent) then
2256 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2258 -- Skip reference if it is the only reference to an entity,
2259 -- and it is an END line reference, and the entity is not in
2260 -- the current extended source. This prevents junk entries
2261 -- consisting only of packages with END lines, where no
2262 -- entity from the package is actually referenced.
2265 and then Ent /= Curent
2266 and then (Refno = Nrefs
2268 Ent /= Xrefs.Table (Rnums (Refno + 1)).Key.Ent)
2269 and then not In_Extended_Main_Source_Unit (Ent)
2274 -- For private type, get full view type
2277 and then Present (Full_View (XE.Key.Ent))
2279 Ent := Underlying_Type (Ent);
2281 if Present (Ent) then
2282 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2286 -- Special exception for Boolean
2288 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
2292 -- For variable reference, get corresponding type
2295 Ent := Etype (XE.Key.Ent);
2296 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
2298 -- If variable is private type, get full view type
2301 and then Present (Full_View (Etype (XE.Key.Ent)))
2303 Ent := Underlying_Type (Etype (XE.Key.Ent));
2305 if Present (Ent) then
2306 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
2309 elsif Is_Generic_Type (Ent) then
2311 -- If the type of the entity is a generic private type,
2312 -- there is no usable full view, so retain the indication
2313 -- that this is an object.
2318 -- Special handling for access parameters and objects and
2319 -- components of an anonymous access type.
2321 if Ekind (Etype (XE.Key.Ent)) in
2322 E_Anonymous_Access_Type
2323 | E_Anonymous_Access_Subprogram_Type
2324 | E_Anonymous_Access_Protected_Subprogram_Type
2326 if Is_Formal (XE.Key.Ent)
2328 Ekind (XE.Key.Ent) in
2329 E_Variable | E_Constant | E_Component
2334 -- Special handling for Boolean
2336 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
2341 -- Special handling for abstract types and operations
2343 if Is_Overloadable (XE.Key.Ent)
2344 and then Is_Abstract_Subprogram (XE.Key.Ent)
2347 Ctyp := 'x'; -- Abstract procedure
2349 elsif Ctyp = 'V' then
2350 Ctyp := 'y'; -- Abstract function
2353 elsif Is_Type (XE.Key.Ent)
2354 and then Is_Abstract_Type (XE.Key.Ent)
2356 if Is_Interface (XE.Key.Ent) then
2359 elsif Ctyp = 'R' then
2360 Ctyp := 'H'; -- Abstract type
2364 -- Only output reference if interesting type of entity
2368 -- Suppress references to object definitions, used for local
2371 or else XE.Key.Typ = 'D'
2372 or else XE.Key.Typ = 'I'
2374 -- Suppress self references, except for bodies that act as
2377 or else (XE.Key.Loc = XE.Def
2380 or else not Is_Subprogram (XE.Key.Ent)))
2382 -- Also suppress definitions of body formals (we only
2383 -- treat these as references, and the references were
2384 -- separately recorded).
2386 or else (Is_Formal (XE.Key.Ent)
2387 and then Present (Spec_Entity (XE.Key.Ent)))
2392 -- Start new Xref section if new xref unit
2394 if XE.Key.Eun /= Curxu then
2395 if Write_Info_Col > 1 then
2399 Curxu := XE.Key.Eun;
2401 Write_Info_Initiate ('X');
2402 Write_Info_Char (' ');
2403 Write_Info_Nat (Dependency_Num (XE.Key.Eun));
2404 Write_Info_Char (' ');
2406 (Reference_Name (Source_Index (XE.Key.Eun)));
2409 -- Start new Entity line if new entity. Note that we
2410 -- consider two entities the same if they have the same
2411 -- name and source location. This causes entities in
2412 -- instantiations to be treated as though they referred
2417 (XE.Key.Ent /= Curent
2419 (Name_Change (XE.Key.Ent) or else XE.Def /= Curdef))
2421 Curent := XE.Key.Ent;
2424 Get_Unqualified_Name_String (Chars (XE.Key.Ent));
2426 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
2428 if Write_Info_Col > 1 then
2432 -- Write column number information
2434 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
2435 Write_Info_Char (Ctyp);
2436 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
2438 -- Write level information
2440 Write_Level_Info : declare
2441 function Is_Visible_Generic_Entity
2442 (E : Entity_Id) return Boolean;
2443 -- Check whether E is declared in the visible part
2444 -- of a generic package. For source navigation
2445 -- purposes, treat this as a visible entity.
2447 function Is_Private_Record_Component
2448 (E : Entity_Id) return Boolean;
2449 -- Check whether E is a non-inherited component of a
2450 -- private extension. Even if the enclosing record is
2451 -- public, we want to treat the component as private
2452 -- for navigation purposes.
2454 ---------------------------------
2455 -- Is_Private_Record_Component --
2456 ---------------------------------
2458 function Is_Private_Record_Component
2459 (E : Entity_Id) return Boolean
2461 S : constant Entity_Id := Scope (E);
2464 Ekind (E) = E_Component
2465 and then Nkind (Declaration_Node (S)) =
2466 N_Private_Extension_Declaration
2467 and then Original_Record_Component (E) = E;
2468 end Is_Private_Record_Component;
2470 -------------------------------
2471 -- Is_Visible_Generic_Entity --
2472 -------------------------------
2474 function Is_Visible_Generic_Entity
2475 (E : Entity_Id) return Boolean
2480 -- The Present check here is an error defense
2482 if Present (Scope (E))
2483 and then Ekind (Scope (E)) /= E_Generic_Package
2489 while Present (Par) loop
2491 Nkind (Par) = N_Generic_Package_Declaration
2493 -- Entity is a generic formal
2498 Nkind (Parent (Par)) = N_Package_Specification
2501 Is_List_Member (Par)
2502 and then List_Containing (Par) =
2503 Visible_Declarations (Parent (Par));
2505 Par := Parent (Par);
2510 end Is_Visible_Generic_Entity;
2512 -- Start of processing for Write_Level_Info
2515 if Is_Hidden (Curent)
2516 or else Is_Private_Record_Component (Curent)
2518 Write_Info_Char (' ');
2522 or else Is_Visible_Generic_Entity (Curent)
2524 Write_Info_Char ('*');
2527 Write_Info_Char (' ');
2529 end Write_Level_Info;
2531 -- Output entity name. We use the occurrence from the
2532 -- actual source program at the definition point.
2535 Ent_Name : constant String :=
2536 Exact_Source_Name (Sloc (XE.Key.Ent));
2538 for C in Ent_Name'Range loop
2539 Write_Info_Char (Ent_Name (C));
2543 -- See if we have a renaming reference
2545 if Is_Object (XE.Key.Ent)
2546 and then Present (Renamed_Object (XE.Key.Ent))
2548 Rref := Renamed_Object (XE.Key.Ent);
2550 elsif Is_Overloadable (XE.Key.Ent)
2551 and then Nkind (Parent (Declaration_Node (XE.Key.Ent)))
2552 = N_Subprogram_Renaming_Declaration
2554 Rref := Name (Parent (Declaration_Node (XE.Key.Ent)));
2556 elsif Ekind (XE.Key.Ent) = E_Package
2557 and then Nkind (Declaration_Node (XE.Key.Ent)) =
2558 N_Package_Renaming_Declaration
2560 Rref := Name (Declaration_Node (XE.Key.Ent));
2566 if Present (Rref) then
2567 if Nkind (Rref) = N_Expanded_Name then
2568 Rref := Selector_Name (Rref);
2571 if Nkind (Rref) = N_Identifier
2572 or else Nkind (Rref) = N_Operator_Symbol
2576 -- For renamed array components, use the array name
2577 -- for the renamed entity, which reflect the fact that
2578 -- in general the whole array is aliased.
2580 elsif Nkind (Rref) = N_Indexed_Component then
2581 if Nkind (Prefix (Rref)) = N_Identifier then
2582 Rref := Prefix (Rref);
2583 elsif Nkind (Prefix (Rref)) = N_Expanded_Name then
2584 Rref := Selector_Name (Prefix (Rref));
2594 -- Write out renaming reference if we have one
2596 if Present (Rref) then
2597 Write_Info_Char ('=');
2599 (Int (Get_Logical_Line_Number (Sloc (Rref))));
2600 Write_Info_Char (':');
2602 (Int (Get_Column_Number (Sloc (Rref))));
2605 -- Indicate that the entity is in the unit of the current
2610 -- Write out information about generic parent, if entity
2613 if Is_Generic_Instance (XE.Key.Ent) then
2615 Gen_Par : constant Entity_Id :=
2618 (Unit_Declaration_Node
2620 Loc : constant Source_Ptr := Sloc (Gen_Par);
2621 Gen_U : constant Unit_Number_Type :=
2622 Get_Source_Unit (Loc);
2625 Write_Info_Char ('[');
2627 if Curru /= Gen_U then
2628 Write_Info_Nat (Dependency_Num (Gen_U));
2629 Write_Info_Char ('|');
2633 (Int (Get_Logical_Line_Number (Loc)));
2634 Write_Info_Char (']');
2638 -- See if we have a type reference and if so output
2640 Check_Type_Reference (XE.Key.Ent, False);
2642 -- Additional information for types with progenitors,
2643 -- including synchronized tagged types.
2646 Typ : constant Entity_Id := XE.Key.Ent;
2650 if Is_Record_Type (Typ)
2651 and then Present (Interfaces (Typ))
2653 Elmt := First_Elmt (Interfaces (Typ));
2655 elsif Is_Concurrent_Type (Typ)
2656 and then Present (Corresponding_Record_Type (Typ))
2658 Interfaces (Corresponding_Record_Type (Typ)))
2662 Interfaces (Corresponding_Record_Type (Typ)));
2668 while Present (Elmt) loop
2669 Check_Type_Reference (Node (Elmt), True);
2674 -- For array types, list index types as well. (This is
2675 -- not C, indexes have distinct types).
2677 if Is_Array_Type (XE.Key.Ent) then
2679 A_Typ : constant Entity_Id := XE.Key.Ent;
2683 -- If this is a derived array type, we have
2684 -- output the parent type, so add the component
2687 if Is_Derived_Type (A_Typ) then
2688 Check_Type_Reference
2689 (Component_Type (A_Typ), False, True);
2692 -- Add references to index types.
2694 Indx := First_Index (XE.Key.Ent);
2695 while Present (Indx) loop
2696 Check_Type_Reference
2697 (First_Subtype (Etype (Indx)), True);
2703 -- If the entity is an overriding operation, write info
2704 -- on operation that was overridden.
2706 if Is_Subprogram (XE.Key.Ent)
2707 and then Present (Overridden_Operation (XE.Key.Ent))
2709 Output_Overridden_Op
2710 (Overridden_Operation (XE.Key.Ent));
2713 -- End of processing for entity output
2715 Crloc := No_Location;
2718 -- Output the reference if it is not as the same location
2719 -- as the previous one, or it is a read-reference that
2720 -- indicates that the entity is an in-out actual in a call.
2722 if XE.Key.Loc /= No_Location
2724 (XE.Key.Loc /= Crloc
2725 or else (Prevt = 'm' and then XE.Key.Typ = 'r'))
2727 Crloc := XE.Key.Loc;
2728 Prevt := XE.Key.Typ;
2730 -- Start continuation if line full, else blank
2732 if Write_Info_Col > 72 then
2734 Write_Info_Initiate ('.');
2737 Write_Info_Char (' ');
2739 -- Output file number if changed
2741 if XE.Key.Lun /= Curru then
2742 Curru := XE.Key.Lun;
2743 Write_Info_Nat (Dependency_Num (Curru));
2744 Write_Info_Char ('|');
2748 (Int (Get_Logical_Line_Number (XE.Key.Loc)));
2749 Write_Info_Char (XE.Key.Typ);
2751 if Is_Overloadable (XE.Key.Ent) then
2752 if (Is_Imported (XE.Key.Ent) and then XE.Key.Typ = 'b')
2754 (Is_Exported (XE.Key.Ent) and then XE.Key.Typ = 'i')
2756 Output_Import_Export_Info (XE.Key.Ent);
2760 Write_Info_Nat (Int (Get_Column_Number (XE.Key.Loc)));
2762 Output_Instantiation_Refs (Sloc (XE.Key.Ent));
2773 end Output_References;
2775 ---------------------------------
2776 -- Process_Deferred_References --
2777 ---------------------------------
2779 procedure Process_Deferred_References is
2781 for J in Deferred_References.First .. Deferred_References.Last loop
2783 D : Deferred_Reference_Entry renames Deferred_References.Table (J);
2786 case Is_LHS (D.N) is
2788 Generate_Reference (D.E, D.N, 'm');
2791 Generate_Reference (D.E, D.N, 'r');
2793 -- Not clear if Unknown can occur at this stage, but if it
2794 -- does we will treat it as a normal reference.
2797 Generate_Reference (D.E, D.N, 'r');
2802 -- Clear processed entries from table
2804 Deferred_References.Init;
2805 end Process_Deferred_References;
2807 -- Start of elaboration for Lib.Xref
2810 -- Reset is necessary because Elmt_Ptr does not default to Null_Ptr,
2811 -- because it's not an access type.