1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2004, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Csets; use Csets;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Lib.Util; use Lib.Util;
32 with Namet; use Namet;
33 with Nlists; use Nlists;
35 with Sem_Prag; use Sem_Prag;
36 with Sinfo; use Sinfo;
37 with Sinput; use Sinput;
38 with Snames; use Snames;
39 with Stringt; use Stringt;
40 with Stand; use Stand;
41 with Table; use Table;
42 with Widechar; use Widechar;
44 with GNAT.Heap_Sort_A;
46 package body Lib.Xref is
52 -- The Xref table is used to record references. The Loc field is set
53 -- to No_Location for a definition entry.
55 subtype Xref_Entry_Number is Int;
57 type Xref_Entry is record
59 -- Entity referenced (E parameter to Generate_Reference)
62 -- Original source location for entity being referenced. Note that
63 -- these values are used only during the output process, they are
64 -- not set when the entries are originally built. This is because
65 -- private entities can be swapped when the initial call is made.
68 -- Location of reference (Original_Location (Sloc field of N parameter
69 -- to Generate_Reference). Set to No_Location for the case of a
70 -- defining occurrence.
73 -- Reference type (Typ param to Generate_Reference)
75 Eun : Unit_Number_Type;
76 -- Unit number corresponding to Ent
78 Lun : Unit_Number_Type;
79 -- Unit number corresponding to Loc. Value is undefined and not
80 -- referenced if Loc is set to No_Location.
84 package Xrefs is new Table.Table (
85 Table_Component_Type => Xref_Entry,
86 Table_Index_Type => Xref_Entry_Number,
88 Table_Initial => Alloc.Xrefs_Initial,
89 Table_Increment => Alloc.Xrefs_Increment,
90 Table_Name => "Xrefs");
92 -------------------------
93 -- Generate_Definition --
94 -------------------------
96 procedure Generate_Definition (E : Entity_Id) is
101 pragma Assert (Nkind (E) in N_Entity);
103 -- Note that we do not test Xref_Entity_Letters here. It is too
104 -- early to do so, since we are often called before the entity
105 -- is fully constructed, so that the Ekind is still E_Void.
109 -- Definition must come from source
111 and then Comes_From_Source (E)
113 -- And must have a reasonable source location that is not
114 -- within an instance (all entities in instances are ignored)
116 and then Sloc (E) > No_Location
117 and then Instantiation_Location (Sloc (E)) = No_Location
119 -- And must be a non-internal name from the main source unit
121 and then In_Extended_Main_Source_Unit (E)
122 and then not Is_Internal_Name (Chars (E))
124 Xrefs.Increment_Last;
126 Loc := Original_Location (Sloc (E));
128 Xrefs.Table (Indx).Ent := E;
129 Xrefs.Table (Indx).Loc := No_Location;
130 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
131 Xrefs.Table (Indx).Lun := No_Unit;
132 Set_Has_Xref_Entry (E);
134 end Generate_Definition;
136 ---------------------------------
137 -- Generate_Operator_Reference --
138 ---------------------------------
140 procedure Generate_Operator_Reference
145 if not In_Extended_Main_Source_Unit (N) then
149 -- If the operator is not a Standard operator, then we generate
150 -- a real reference to the user defined operator.
152 if Sloc (Entity (N)) /= Standard_Location then
153 Generate_Reference (Entity (N), N);
155 -- A reference to an implicit inequality operator is a also a
156 -- reference to the user-defined equality.
158 if Nkind (N) = N_Op_Ne
159 and then not Comes_From_Source (Entity (N))
160 and then Present (Corresponding_Equality (Entity (N)))
162 Generate_Reference (Corresponding_Equality (Entity (N)), N);
165 -- For the case of Standard operators, we mark the result type
166 -- as referenced. This ensures that in the case where we are
167 -- using a derived operator, we mark an entity of the unit that
168 -- implicitly defines this operator as used. Otherwise we may
169 -- think that no entity of the unit is used. The actual entity
170 -- marked as referenced is the first subtype, which is the user
171 -- defined entity that is relevant.
173 -- Note: we only do this for operators that come from source.
174 -- The generated code sometimes reaches for entities that do
175 -- not need to be explicitly visible (for example, when we
176 -- expand the code for comparing two record types, the fields
177 -- of the record may not be visible).
179 elsif Comes_From_Source (N) then
180 Set_Referenced (First_Subtype (T));
182 end Generate_Operator_Reference;
184 ------------------------
185 -- Generate_Reference --
186 ------------------------
188 procedure Generate_Reference
191 Typ : Character := 'r';
192 Set_Ref : Boolean := True;
193 Force : Boolean := False)
201 function Is_On_LHS (Node : Node_Id) return Boolean;
202 -- Used to check if a node is on the left hand side of an
203 -- assignment. The following cases are handled:
205 -- Variable Node is a direct descendant of an assignment
208 -- Prefix Of an indexed or selected component that is
209 -- present in a subtree rooted by an assignment
210 -- statement. There is no restriction of nesting
211 -- of components, thus cases such as A.B(C).D are
218 -- Couldn't we use Is_Lvalue or whatever it is called ???
220 function Is_On_LHS (Node : Node_Id) return Boolean is
224 -- Only identifiers are considered, is this necessary???
226 if Nkind (N) /= N_Identifier then
230 -- Reach the assignment statement subtree root. In the
231 -- case of a variable being a direct descendant of an
232 -- assignment statement, the loop is skiped.
234 while Nkind (Parent (N)) /= N_Assignment_Statement loop
236 -- Check whether the parent is a component and the
237 -- current node is its prefix.
239 if (Nkind (Parent (N)) = N_Selected_Component
241 Nkind (Parent (N)) = N_Indexed_Component)
242 and then Prefix (Parent (N)) = N
250 -- Parent (N) is an assignment statement, check whether
253 return Name (Parent (N)) = N;
256 -- Start of processing for Generate_Reference
259 pragma Assert (Nkind (E) in N_Entity);
261 -- Never collect references if not in main source unit. However,
262 -- we omit this test if Typ is 'e' or 'k', since these entries are
263 -- really structural, and it is useful to have them in units
264 -- that reference packages as well as units that define packages.
265 -- We also omit the test for the case of 'p' since we want to
266 -- include inherited primitive operations from other packages.
268 if not In_Extended_Main_Source_Unit (N)
276 -- For reference type p, the entity must be in main source unit
278 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
282 -- Unless the reference is forced, we ignore references where
283 -- the reference itself does not come from Source.
285 if not Force and then not Comes_From_Source (N) then
289 -- Deal with setting entity as referenced, unless suppressed.
290 -- Note that we still do Set_Referenced on entities that do not
291 -- come from source. This situation arises when we have a source
292 -- reference to a derived operation, where the derived operation
293 -- itself does not come from source, but we still want to mark it
294 -- as referenced, since we really are referencing an entity in the
295 -- corresponding package (this avoids incorrect complaints that the
296 -- package contains no referenced entities).
300 -- For a variable that appears on the left side of an
301 -- assignment statement, we set the Referenced_As_LHS
302 -- flag since this is indeed a left hand side.
303 -- We also set the Referenced_As_LHS flag of a prefix
304 -- of selected or indexed component.
306 if Ekind (E) = E_Variable
307 and then Is_On_LHS (N)
309 Set_Referenced_As_LHS (E);
311 -- Check for a reference in a pragma that should not count as a
312 -- making the variable referenced for warning purposes.
314 elsif Is_Non_Significant_Pragma_Reference (N) then
317 -- A reference in an attribute definition clause does not
318 -- count as a reference except for the case of Address.
319 -- The reason that 'Address is an exception is that it
320 -- creates an alias through which the variable may be
323 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
324 and then Chars (Parent (N)) /= Name_Address
325 and then N = Name (Parent (N))
329 -- Constant completion does not count as a reference
332 and then Ekind (E) = E_Constant
336 -- Record representation clause does not count as a reference
338 elsif Nkind (N) = N_Identifier
339 and then Nkind (Parent (N)) = N_Record_Representation_Clause
343 -- Discriminants do not need to produce a reference to record type
346 and then Nkind (Parent (N)) = N_Discriminant_Specification
350 -- Any other occurrence counts as referencing the entity
356 -- Check for pragma Unreferenced given and reference is within
357 -- this source unit (occasion for possible warning to be issued)
359 if Has_Pragma_Unreferenced (E)
360 and then In_Same_Extended_Unit (Sloc (E), Sloc (N))
362 -- A reference as a named parameter in a call does not count
363 -- as a violation of pragma Unreferenced for this purpose.
365 if Nkind (N) = N_Identifier
366 and then Nkind (Parent (N)) = N_Parameter_Association
367 and then Selector_Name (Parent (N)) = N
371 -- Neither does a reference to a variable on the left side
374 elsif Ekind (E) = E_Variable
375 and then Nkind (Parent (N)) = N_Assignment_Statement
376 and then Name (Parent (N)) = N
380 -- Here we issue the warning, since this is a real reference
383 Error_Msg_NE ("?pragma Unreferenced given for&", N, E);
387 -- If this is a subprogram instance, mark as well the internal
388 -- subprogram in the wrapper package, which may be a visible
391 if Is_Overloadable (E)
392 and then Is_Generic_Instance (E)
393 and then Present (Alias (E))
395 Set_Referenced (Alias (E));
399 -- Generate reference if all conditions are met:
402 -- Cross referencing must be active
406 -- The entity must be one for which we collect references
408 and then Xref_Entity_Letters (Ekind (E)) /= ' '
410 -- Both Sloc values must be set to something sensible
412 and then Sloc (E) > No_Location
413 and then Sloc (N) > No_Location
415 -- We ignore references from within an instance
417 and then Instantiation_Location (Sloc (N)) = No_Location
419 -- Ignore dummy references
423 if Nkind (N) = N_Identifier
425 Nkind (N) = N_Defining_Identifier
429 Nkind (N) = N_Defining_Operator_Symbol
431 Nkind (N) = N_Operator_Symbol
433 (Nkind (N) = N_Character_Literal
434 and then Sloc (Entity (N)) /= Standard_Location)
436 Nkind (N) = N_Defining_Character_Literal
440 elsif Nkind (N) = N_Expanded_Name
442 Nkind (N) = N_Selected_Component
444 Nod := Selector_Name (N);
450 -- Normal case of source entity comes from source
452 if Comes_From_Source (E) then
455 -- Entity does not come from source, but is a derived subprogram
456 -- and the derived subprogram comes from source (after one or more
457 -- derivations) in which case the reference is to parent subprogram.
459 elsif Is_Overloadable (E)
460 and then Present (Alias (E))
465 if Comes_From_Source (Ent) then
467 elsif No (Alias (Ent)) then
474 -- Record components of discriminated subtypes or derived types
475 -- must be treated as references to the original component.
477 elsif Ekind (E) = E_Component
478 and then Comes_From_Source (Original_Record_Component (E))
480 Ent := Original_Record_Component (E);
482 -- Ignore reference to any other entity that is not from source
488 -- Record reference to entity
490 Ref := Original_Location (Sloc (Nod));
491 Def := Original_Location (Sloc (Ent));
493 Xrefs.Increment_Last;
496 Xrefs.Table (Indx).Loc := Ref;
498 -- Overriding operations are marked with 'P'.
501 and then Is_Subprogram (N)
502 and then Is_Overriding_Operation (N)
504 Xrefs.Table (Indx).Typ := 'P';
506 Xrefs.Table (Indx).Typ := Typ;
509 Xrefs.Table (Indx).Eun := Get_Source_Unit (Def);
510 Xrefs.Table (Indx).Lun := Get_Source_Unit (Ref);
511 Xrefs.Table (Indx).Ent := Ent;
512 Set_Has_Xref_Entry (Ent);
514 end Generate_Reference;
516 -----------------------------------
517 -- Generate_Reference_To_Formals --
518 -----------------------------------
520 procedure Generate_Reference_To_Formals (E : Entity_Id) is
524 if Is_Generic_Subprogram (E) then
525 Formal := First_Entity (E);
527 while Present (Formal)
528 and then not Is_Formal (Formal)
530 Next_Entity (Formal);
534 Formal := First_Formal (E);
537 while Present (Formal) loop
538 if Ekind (Formal) = E_In_Parameter then
540 if Nkind (Parameter_Type (Parent (Formal)))
541 = N_Access_Definition
543 Generate_Reference (E, Formal, '^', False);
545 Generate_Reference (E, Formal, '>', False);
548 elsif Ekind (Formal) = E_In_Out_Parameter then
549 Generate_Reference (E, Formal, '=', False);
552 Generate_Reference (E, Formal, '<', False);
555 Next_Formal (Formal);
557 end Generate_Reference_To_Formals;
559 -------------------------------------------
560 -- Generate_Reference_To_Generic_Formals --
561 -------------------------------------------
563 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
567 Formal := First_Entity (E);
569 while Present (Formal) loop
570 if Comes_From_Source (Formal) then
571 Generate_Reference (E, Formal, 'z', False);
574 Next_Entity (Formal);
576 end Generate_Reference_To_Generic_Formals;
582 procedure Initialize is
587 -----------------------
588 -- Output_References --
589 -----------------------
591 procedure Output_References is
593 procedure Get_Type_Reference
595 Tref : out Entity_Id;
596 Left : out Character;
597 Right : out Character);
598 -- Given an entity id Ent, determines whether a type reference is
599 -- required. If so, Tref is set to the entity for the type reference
600 -- and Left and Right are set to the left/right brackets to be
601 -- output for the reference. If no type reference is required, then
602 -- Tref is set to Empty, and Left/Right are set to space.
604 procedure Output_Import_Export_Info (Ent : Entity_Id);
605 -- Ouput language and external name information for an interfaced
606 -- entity, using the format <language, external_name>,
608 ------------------------
609 -- Get_Type_Reference --
610 ------------------------
612 procedure Get_Type_Reference
614 Tref : out Entity_Id;
615 Left : out Character;
616 Right : out Character)
621 -- See if we have a type reference
630 -- Processing for types
632 if Is_Type (Tref) then
636 if Base_Type (Tref) = Tref then
638 -- If derived, then get first subtype
640 if Tref /= Etype (Tref) then
641 Tref := First_Subtype (Etype (Tref));
643 -- Set brackets for derived type, but don't
644 -- override pointer case since the fact that
645 -- something is a pointer is more important
652 -- If non-derived ptr, get directly designated type.
653 -- If the type has a full view, all references are
654 -- on the partial view, that is seen first.
656 elsif Is_Access_Type (Tref) then
657 Tref := Directly_Designated_Type (Tref);
661 elsif Is_Private_Type (Tref)
662 and then Present (Full_View (Tref))
663 and then Is_Access_Type (Full_View (Tref))
665 Tref := Directly_Designated_Type (Full_View (Tref));
669 -- If non-derived array, get component type.
670 -- Skip component type for case of String
671 -- or Wide_String, saves worthwhile space.
673 elsif Is_Array_Type (Tref)
674 and then Tref /= Standard_String
675 and then Tref /= Standard_Wide_String
677 Tref := Component_Type (Tref);
681 -- For other non-derived base types, nothing
687 -- For a subtype, go to ancestor subtype.
690 Tref := Ancestor_Subtype (Tref);
692 -- If no ancestor subtype, go to base type
695 Tref := Base_Type (Sav);
699 -- For objects, functions, enum literals,
700 -- just get type from Etype field.
702 elsif Is_Object (Tref)
703 or else Ekind (Tref) = E_Enumeration_Literal
704 or else Ekind (Tref) = E_Function
705 or else Ekind (Tref) = E_Operator
707 Tref := Etype (Tref);
709 -- For anything else, exit
715 -- Exit if no type reference, or we are stuck in
716 -- some loop trying to find the type reference, or
717 -- if the type is standard void type (the latter is
718 -- an implementation artifact that should not show
719 -- up in the generated cross-references).
723 or else Tref = Standard_Void_Type;
725 -- If we have a usable type reference, return, otherwise
726 -- keep looking for something useful (we are looking for
727 -- something that either comes from source or standard)
729 if Sloc (Tref) = Standard_Location
730 or else Comes_From_Source (Tref)
732 -- If the reference is a subtype created for a generic
733 -- actual, go to actual directly, the inner subtype is
736 if Nkind (Parent (Tref)) = N_Subtype_Declaration
737 and then not Comes_From_Source (Parent (Tref))
739 (Is_Wrapper_Package (Scope (Tref))
740 or else Is_Generic_Instance (Scope (Tref)))
742 Tref := Base_Type (Tref);
749 -- If we fall through the loop, no type reference
754 end Get_Type_Reference;
756 -------------------------------
757 -- Output_Import_Export_Info --
758 -------------------------------
760 procedure Output_Import_Export_Info (Ent : Entity_Id) is
761 Language_Name : Name_Id;
762 Conv : constant Convention_Id := Convention (Ent);
764 if Conv = Convention_C then
765 Language_Name := Name_C;
767 elsif Conv = Convention_CPP then
768 Language_Name := Name_CPP;
770 elsif Conv = Convention_Ada then
771 Language_Name := Name_Ada;
774 -- These are the only languages that GPS knows about.
779 Write_Info_Char ('<');
780 Get_Unqualified_Name_String (Language_Name);
782 for J in 1 .. Name_Len loop
783 Write_Info_Char (Name_Buffer (J));
786 if Present (Interface_Name (Ent)) then
787 Write_Info_Char (',');
788 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
790 for J in 1 .. Name_Len loop
791 Write_Info_Char (Name_Buffer (J));
795 Write_Info_Char ('>');
796 end Output_Import_Export_Info;
798 -- Start of processing for Output_References
801 if not Opt.Xref_Active then
805 -- Before we go ahead and output the references we have a problem
806 -- that needs dealing with. So far we have captured things that are
807 -- definitely referenced by the main unit, or defined in the main
808 -- unit. That's because we don't want to clutter up the ali file
809 -- for this unit with definition lines for entities in other units
810 -- that are not referenced.
812 -- But there is a glitch. We may reference an entity in another unit,
813 -- and it may have a type reference to an entity that is not directly
814 -- referenced in the main unit, which may mean that there is no xref
815 -- entry for this entity yet in the list of references.
817 -- If we don't do something about this, we will end with an orphan
818 -- type reference, i.e. it will point to an entity that does not
819 -- appear within the generated references in the ali file. That is
820 -- not good for tools using the xref information.
822 -- To fix this, we go through the references adding definition
823 -- entries for any unreferenced entities that can be referenced
824 -- in a type reference. There is a recursion problem here, and
825 -- that is dealt with by making sure that this traversal also
826 -- traverses any entries that get added by the traversal.
837 -- Note that this is not a for loop for a very good reason. The
838 -- processing of items in the table can add new items to the
839 -- table, and they must be processed as well
842 while J <= Xrefs.Last loop
843 Ent := Xrefs.Table (J).Ent;
844 Get_Type_Reference (Ent, Tref, L, R);
847 and then not Has_Xref_Entry (Tref)
848 and then Sloc (Tref) > No_Location
850 Xrefs.Increment_Last;
852 Loc := Original_Location (Sloc (Tref));
853 Xrefs.Table (Indx).Ent := Tref;
854 Xrefs.Table (Indx).Loc := No_Location;
855 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
856 Xrefs.Table (Indx).Lun := No_Unit;
857 Set_Has_Xref_Entry (Tref);
860 -- Collect inherited primitive operations that may be
861 -- declared in another unit and have no visible reference
862 -- in the current one.
865 and then Is_Tagged_Type (Ent)
866 and then Is_Derived_Type (Ent)
867 and then Ent = Base_Type (Ent)
868 and then In_Extended_Main_Source_Unit (Ent)
871 Op_List : constant Elist_Id := Primitive_Operations (Ent);
875 function Parent_Op (E : Entity_Id) return Entity_Id;
876 -- Find original operation, which may be inherited
877 -- through several derivations.
879 function Parent_Op (E : Entity_Id) return Entity_Id is
880 Orig_Op : constant Entity_Id := Alias (E);
884 elsif not Comes_From_Source (E)
885 and then not Has_Xref_Entry (Orig_Op)
886 and then Comes_From_Source (Orig_Op)
890 return Parent_Op (Orig_Op);
895 Op := First_Elmt (Op_List);
896 while Present (Op) loop
897 Prim := Parent_Op (Node (Op));
899 if Present (Prim) then
900 Xrefs.Increment_Last;
902 Loc := Original_Location (Sloc (Prim));
903 Xrefs.Table (Indx).Ent := Prim;
904 Xrefs.Table (Indx).Loc := No_Location;
905 Xrefs.Table (Indx).Eun :=
906 Get_Source_Unit (Sloc (Prim));
907 Xrefs.Table (Indx).Lun := No_Unit;
908 Set_Has_Xref_Entry (Prim);
920 -- Now we have all the references, including those for any embedded
921 -- type references, so we can sort them, and output them.
923 Output_Refs : declare
925 Nrefs : Nat := Xrefs.Last;
926 -- Number of references in table. This value may get reset
927 -- (reduced) when we eliminate duplicate reference entries.
929 Rnums : array (0 .. Nrefs) of Nat;
930 -- This array contains numbers of references in the Xrefs table.
931 -- This list is sorted in output order. The extra 0'th entry is
932 -- convenient for the call to sort. When we sort the table, we
933 -- move the entries in Rnums around, but we do not move the
934 -- original table entries.
936 Curxu : Unit_Number_Type;
939 Curru : Unit_Number_Type;
940 -- Current reference unit for one entity
942 Cursrc : Source_Buffer_Ptr;
943 -- Current xref unit source text
948 Curnam : String (1 .. Name_Buffer'Length);
950 -- Simple name and length of current entity
953 -- Original source location for current entity
956 -- Current reference location
959 -- Entity type character
965 -- Renaming reference
967 Trunit : Unit_Number_Type;
968 -- Unit number for type reference
970 function Lt (Op1, Op2 : Natural) return Boolean;
971 -- Comparison function for Sort call
973 function Name_Change (X : Entity_Id) return Boolean;
974 -- Determines if entity X has a different simple name from Curent
976 procedure Move (From : Natural; To : Natural);
977 -- Move procedure for Sort call
983 function Lt (Op1, Op2 : Natural) return Boolean is
984 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
985 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
988 -- First test. If entity is in different unit, sort by unit
990 if T1.Eun /= T2.Eun then
991 return Dependency_Num (T1.Eun) < Dependency_Num (T2.Eun);
993 -- Second test, within same unit, sort by entity Sloc
995 elsif T1.Def /= T2.Def then
996 return T1.Def < T2.Def;
998 -- Third test, sort definitions ahead of references
1000 elsif T1.Loc = No_Location then
1003 elsif T2.Loc = No_Location then
1006 -- Fourth test, for same entity, sort by reference location unit
1008 elsif T1.Lun /= T2.Lun then
1009 return Dependency_Num (T1.Lun) < Dependency_Num (T2.Lun);
1011 -- Fifth test order of location within referencing unit
1013 elsif T1.Loc /= T2.Loc then
1014 return T1.Loc < T2.Loc;
1016 -- Finally, for two locations at the same address, we prefer
1017 -- the one that does NOT have the type 'r' so that a modification
1018 -- or extension takes preference, when there are more than one
1019 -- reference at the same location.
1022 return T2.Typ = 'r';
1030 procedure Move (From : Natural; To : Natural) is
1032 Rnums (Nat (To)) := Rnums (Nat (From));
1039 function Name_Change (X : Entity_Id) return Boolean is
1041 Get_Unqualified_Name_String (Chars (X));
1043 if Name_Len /= Curlen then
1047 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
1051 -- Start of processing for Output_Refs
1054 -- Capture the definition Sloc values. We delay doing this till now,
1055 -- since at the time the reference or definition is made, private
1056 -- types may be swapped, and the Sloc value may be incorrect. We
1057 -- also set up the pointer vector for the sort.
1059 for J in 1 .. Nrefs loop
1061 Xrefs.Table (J).Def :=
1062 Original_Location (Sloc (Xrefs.Table (J).Ent));
1065 -- Sort the references
1067 GNAT.Heap_Sort_A.Sort
1069 Move'Unrestricted_Access,
1070 Lt'Unrestricted_Access);
1072 -- Eliminate duplicate entries
1075 NR : constant Nat := Nrefs;
1078 -- We need this test for NR because if we force ALI file
1079 -- generation in case of errors detected, it may be the case
1080 -- that Nrefs is 0, so we should not reset it here
1085 for J in 2 .. NR loop
1086 if Xrefs.Table (Rnums (J)) /=
1087 Xrefs.Table (Rnums (Nrefs))
1090 Rnums (Nrefs) := Rnums (J);
1096 -- Initialize loop through references
1100 Curdef := No_Location;
1102 Crloc := No_Location;
1104 -- Loop to output references
1106 for Refno in 1 .. Nrefs loop
1107 Output_One_Ref : declare
1113 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
1114 -- The current entry to be accessed
1117 -- Used to index into source buffer to get entity name
1121 -- Used for {} or <> or () for type reference
1123 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
1124 -- Recursive procedure to output instantiation references for
1125 -- the given source ptr in [file|line[...]] form. No output
1126 -- if the given location is not a generic template reference.
1128 -------------------------------
1129 -- Output_Instantiation_Refs --
1130 -------------------------------
1132 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
1133 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
1134 Lun : Unit_Number_Type;
1135 Cu : constant Unit_Number_Type := Curru;
1138 -- Nothing to do if this is not an instantiation
1140 if Iloc = No_Location then
1144 -- Output instantiation reference
1146 Write_Info_Char ('[');
1147 Lun := Get_Source_Unit (Iloc);
1149 if Lun /= Curru then
1151 Write_Info_Nat (Dependency_Num (Curru));
1152 Write_Info_Char ('|');
1155 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
1157 -- Recursive call to get nested instantiations
1159 Output_Instantiation_Refs (Iloc);
1161 -- Output final ] after call to get proper nesting
1163 Write_Info_Char (']');
1166 end Output_Instantiation_Refs;
1168 -- Start of processing for Output_One_Ref
1172 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1174 -- Skip reference if it is the only reference to an entity,
1175 -- and it is an end-line reference, and the entity is not in
1176 -- the current extended source. This prevents junk entries
1177 -- consisting only of packages with end lines, where no
1178 -- entity from the package is actually referenced.
1181 and then Ent /= Curent
1182 and then (Refno = Nrefs or else
1183 Ent /= Xrefs.Table (Rnums (Refno + 1)).Ent)
1185 not In_Extended_Main_Source_Unit (Ent)
1190 -- For private type, get full view type
1193 and then Present (Full_View (XE.Ent))
1195 Ent := Underlying_Type (Ent);
1197 if Present (Ent) then
1198 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1202 -- Special exception for Boolean
1204 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
1208 -- For variable reference, get corresponding type
1211 Ent := Etype (XE.Ent);
1212 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1214 -- If variable is private type, get full view type
1217 and then Present (Full_View (Etype (XE.Ent)))
1219 Ent := Underlying_Type (Etype (XE.Ent));
1221 if Present (Ent) then
1222 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1226 -- Special handling for access parameter
1229 K : constant Entity_Kind := Ekind (Etype (XE.Ent));
1232 if (K = E_Anonymous_Access_Type
1234 K = E_Anonymous_Access_Subprogram_Type
1236 E_Anonymous_Access_Protected_Subprogram_Type)
1237 and then Is_Formal (XE.Ent)
1241 -- Special handling for Boolean
1243 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
1249 -- Special handling for abstract types and operations.
1251 if Is_Abstract (XE.Ent) then
1254 Ctyp := 'x'; -- abstract procedure
1256 elsif Ctyp = 'V' then
1257 Ctyp := 'y'; -- abstract function
1259 elsif Ctyp = 'R' then
1260 Ctyp := 'H'; -- abstract type
1264 -- Only output reference if interesting type of entity,
1265 -- and suppress self references, except for bodies that
1266 -- act as specs. Also suppress definitions of body formals
1267 -- (we only treat these as references, and the references
1268 -- were separately recorded).
1271 or else (XE.Loc = XE.Def
1274 or else not Is_Subprogram (XE.Ent)))
1275 or else (Is_Formal (XE.Ent)
1276 and then Present (Spec_Entity (XE.Ent)))
1281 -- Start new Xref section if new xref unit
1283 if XE.Eun /= Curxu then
1284 if Write_Info_Col > 1 then
1289 Cursrc := Source_Text (Source_Index (Curxu));
1291 Write_Info_Initiate ('X');
1292 Write_Info_Char (' ');
1293 Write_Info_Nat (Dependency_Num (XE.Eun));
1294 Write_Info_Char (' ');
1295 Write_Info_Name (Reference_Name (Source_Index (XE.Eun)));
1298 -- Start new Entity line if new entity. Note that we
1299 -- consider two entities the same if they have the same
1300 -- name and source location. This causes entities in
1301 -- instantiations to be treated as though they referred
1308 (Name_Change (XE.Ent) or else XE.Def /= Curdef))
1313 Get_Unqualified_Name_String (Chars (XE.Ent));
1315 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
1317 if Write_Info_Col > 1 then
1321 -- Write column number information
1323 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
1324 Write_Info_Char (Ctyp);
1325 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
1327 -- Write level information
1329 Write_Level_Info : declare
1330 function Is_Visible_Generic_Entity
1331 (E : Entity_Id) return Boolean;
1332 -- Check whether E is declared in the visible part
1333 -- of a generic package. For source navigation
1334 -- purposes, treat this as a visible entity.
1336 function Is_Private_Record_Component
1337 (E : Entity_Id) return Boolean;
1338 -- Check whether E is a non-inherited component of a
1339 -- private extension. Even if the enclosing record is
1340 -- public, we want to treat the component as private
1341 -- for navigation purposes.
1343 ---------------------------------
1344 -- Is_Private_Record_Component --
1345 ---------------------------------
1347 function Is_Private_Record_Component
1348 (E : Entity_Id) return Boolean
1350 S : constant Entity_Id := Scope (E);
1353 Ekind (E) = E_Component
1354 and then Nkind (Declaration_Node (S)) =
1355 N_Private_Extension_Declaration
1356 and then Original_Record_Component (E) = E;
1357 end Is_Private_Record_Component;
1359 -------------------------------
1360 -- Is_Visible_Generic_Entity --
1361 -------------------------------
1363 function Is_Visible_Generic_Entity
1364 (E : Entity_Id) return Boolean
1369 if Ekind (Scope (E)) /= E_Generic_Package then
1374 while Present (Par) loop
1376 Nkind (Par) = N_Generic_Package_Declaration
1378 -- Entity is a generic formal
1383 Nkind (Parent (Par)) = N_Package_Specification
1386 Is_List_Member (Par)
1387 and then List_Containing (Par) =
1388 Visible_Declarations (Parent (Par));
1390 Par := Parent (Par);
1395 end Is_Visible_Generic_Entity;
1397 -- Start of processing for Write_Level_Info
1400 if Is_Hidden (Curent)
1401 or else Is_Private_Record_Component (Curent)
1403 Write_Info_Char (' ');
1407 or else Is_Visible_Generic_Entity (Curent)
1409 Write_Info_Char ('*');
1412 Write_Info_Char (' ');
1414 end Write_Level_Info;
1416 -- Output entity name. We use the occurrence from the
1417 -- actual source program at the definition point
1419 P := Original_Location (Sloc (XE.Ent));
1421 -- Entity is character literal
1423 if Cursrc (P) = ''' then
1424 Write_Info_Char (Cursrc (P));
1425 Write_Info_Char (Cursrc (P + 1));
1426 Write_Info_Char (Cursrc (P + 2));
1428 -- Entity is operator symbol
1430 elsif Cursrc (P) = '"' or else Cursrc (P) = '%' then
1431 Write_Info_Char (Cursrc (P));
1436 Write_Info_Char (Cursrc (P2));
1437 exit when Cursrc (P2) = Cursrc (P);
1440 -- Entity is identifier
1444 if Is_Start_Of_Wide_Char (Cursrc, P) then
1445 Scan_Wide (Cursrc, P, WC, Err);
1446 elsif not Identifier_Char (Cursrc (P)) then
1454 Original_Location (Sloc (XE.Ent)) .. P - 1
1456 Write_Info_Char (Cursrc (J));
1460 -- See if we have a renaming reference
1462 if Is_Object (XE.Ent)
1463 and then Present (Renamed_Object (XE.Ent))
1465 Rref := Renamed_Object (XE.Ent);
1467 elsif Is_Overloadable (XE.Ent)
1468 and then Nkind (Parent (Declaration_Node (XE.Ent))) =
1469 N_Subprogram_Renaming_Declaration
1471 Rref := Name (Parent (Declaration_Node (XE.Ent)));
1473 elsif Ekind (XE.Ent) = E_Package
1474 and then Nkind (Declaration_Node (XE.Ent)) =
1475 N_Package_Renaming_Declaration
1477 Rref := Name (Declaration_Node (XE.Ent));
1483 if Present (Rref) then
1484 if Nkind (Rref) = N_Expanded_Name then
1485 Rref := Selector_Name (Rref);
1488 if Nkind (Rref) /= N_Identifier then
1493 -- Write out renaming reference if we have one
1495 if Present (Rref) then
1496 Write_Info_Char ('=');
1498 (Int (Get_Logical_Line_Number (Sloc (Rref))));
1499 Write_Info_Char (':');
1501 (Int (Get_Column_Number (Sloc (Rref))));
1504 -- Indicate that the entity is in the unit
1505 -- of the current xref xection.
1509 -- See if we have a type reference and if so output
1511 Get_Type_Reference (XE.Ent, Tref, Left, Right);
1513 if Present (Tref) then
1515 -- Case of standard entity, output name
1517 if Sloc (Tref) = Standard_Location then
1518 Write_Info_Char (Left);
1519 Write_Info_Name (Chars (Tref));
1520 Write_Info_Char (Right);
1522 -- Case of source entity, output location
1525 Write_Info_Char (Left);
1526 Trunit := Get_Source_Unit (Sloc (Tref));
1528 if Trunit /= Curxu then
1529 Write_Info_Nat (Dependency_Num (Trunit));
1530 Write_Info_Char ('|');
1534 (Int (Get_Logical_Line_Number (Sloc (Tref))));
1537 Ent : Entity_Id := Tref;
1538 Kind : constant Entity_Kind := Ekind (Ent);
1539 Ctyp : Character := Xref_Entity_Letters (Kind);
1543 and then Present (Full_View (Ent))
1545 Ent := Underlying_Type (Ent);
1547 if Present (Ent) then
1548 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1552 Write_Info_Char (Ctyp);
1556 (Int (Get_Column_Number (Sloc (Tref))));
1558 -- If the type comes from an instantiation,
1559 -- add the corresponding info.
1561 Output_Instantiation_Refs (Sloc (Tref));
1562 Write_Info_Char (Right);
1566 -- End of processing for entity output
1568 Crloc := No_Location;
1571 -- Output the reference
1573 if XE.Loc /= No_Location
1574 and then XE.Loc /= Crloc
1578 -- Start continuation if line full, else blank
1580 if Write_Info_Col > 72 then
1582 Write_Info_Initiate ('.');
1585 Write_Info_Char (' ');
1587 -- Output file number if changed
1589 if XE.Lun /= Curru then
1591 Write_Info_Nat (Dependency_Num (Curru));
1592 Write_Info_Char ('|');
1595 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Loc)));
1596 Write_Info_Char (XE.Typ);
1598 if Is_Overloadable (XE.Ent)
1599 and then Is_Imported (XE.Ent)
1600 and then XE.Typ = 'b'
1602 Output_Import_Export_Info (XE.Ent);
1605 Write_Info_Nat (Int (Get_Column_Number (XE.Loc)));
1607 Output_Instantiation_Refs (Sloc (XE.Ent));
1618 end Output_References;