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)
202 pragma Assert (Nkind (E) in N_Entity);
204 -- Never collect references if not in main source unit. However,
205 -- we omit this test if Typ is 'e' or 'k', since these entries are
206 -- really structural, and it is useful to have them in units
207 -- that reference packages as well as units that define packages.
208 -- We also omit the test for the case of 'p' since we want to
209 -- include inherited primitive operations from other packages.
211 if not In_Extended_Main_Source_Unit (N)
219 -- For reference type p, the entity must be in main source unit
221 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
225 -- Unless the reference is forced, we ignore references where
226 -- the reference itself does not come from Source.
228 if not Force and then not Comes_From_Source (N) then
232 -- Deal with setting entity as referenced, unless suppressed.
233 -- Note that we still do Set_Referenced on entities that do not
234 -- come from source. This situation arises when we have a source
235 -- reference to a derived operation, where the derived operation
236 -- itself does not come from source, but we still want to mark it
237 -- as referenced, since we really are referencing an entity in the
238 -- corresponding package (this avoids incorrect complaints that the
239 -- package contains no referenced entities).
243 -- For a variable that appears on the left side of an
244 -- assignment statement, we set the Referenced_As_LHS
245 -- flag since this is indeed a left hand side.
247 if Ekind (E) = E_Variable
248 and then Nkind (Parent (N)) = N_Assignment_Statement
249 and then Name (Parent (N)) = N
250 and then No (Renamed_Object (E))
252 Set_Referenced_As_LHS (E);
254 -- Check for a reference in a pragma that should not count as a
255 -- making the variable referenced for warning purposes.
257 elsif Is_Non_Significant_Pragma_Reference (N) then
260 -- A reference in an attribute definition clause does not
261 -- count as a reference except for the case of Address.
262 -- The reason that 'Address is an exception is that it
263 -- creates an alias through which the variable may be
266 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
267 and then Chars (Parent (N)) /= Name_Address
268 and then N = Name (Parent (N))
272 -- Constant completion does not count as a reference
275 and then Ekind (E) = E_Constant
279 -- Record representation clause does not count as a reference
281 elsif Nkind (N) = N_Identifier
282 and then Nkind (Parent (N)) = N_Record_Representation_Clause
286 -- Discriminants do not need to produce a reference to record type
289 and then Nkind (Parent (N)) = N_Discriminant_Specification
293 -- Any other occurrence counts as referencing the entity
299 -- Check for pragma Unreferenced given and reference is within
300 -- this source unit (occasion for possible warning to be issued)
302 if Has_Pragma_Unreferenced (E)
303 and then In_Same_Extended_Unit (Sloc (E), Sloc (N))
305 -- A reference as a named parameter in a call does not count
306 -- as a violation of pragma Unreferenced for this purpose.
308 if Nkind (N) = N_Identifier
309 and then Nkind (Parent (N)) = N_Parameter_Association
310 and then Selector_Name (Parent (N)) = N
314 -- Neither does a reference to a variable on the left side
317 elsif Ekind (E) = E_Variable
318 and then Nkind (Parent (N)) = N_Assignment_Statement
319 and then Name (Parent (N)) = N
323 -- Here we issue the warning, since this is a real reference
326 Error_Msg_NE ("?pragma Unreferenced given for&", N, E);
330 -- If this is a subprogram instance, mark as well the internal
331 -- subprogram in the wrapper package, which may be a visible
334 if Is_Overloadable (E)
335 and then Is_Generic_Instance (E)
336 and then Present (Alias (E))
338 Set_Referenced (Alias (E));
342 -- Generate reference if all conditions are met:
345 -- Cross referencing must be active
349 -- The entity must be one for which we collect references
351 and then Xref_Entity_Letters (Ekind (E)) /= ' '
353 -- Both Sloc values must be set to something sensible
355 and then Sloc (E) > No_Location
356 and then Sloc (N) > No_Location
358 -- We ignore references from within an instance
360 and then Instantiation_Location (Sloc (N)) = No_Location
362 -- Ignore dummy references
366 if Nkind (N) = N_Identifier
368 Nkind (N) = N_Defining_Identifier
372 Nkind (N) = N_Defining_Operator_Symbol
374 Nkind (N) = N_Operator_Symbol
376 (Nkind (N) = N_Character_Literal
377 and then Sloc (Entity (N)) /= Standard_Location)
379 Nkind (N) = N_Defining_Character_Literal
383 elsif Nkind (N) = N_Expanded_Name
385 Nkind (N) = N_Selected_Component
387 Nod := Selector_Name (N);
393 -- Normal case of source entity comes from source
395 if Comes_From_Source (E) then
398 -- Entity does not come from source, but is a derived subprogram
399 -- and the derived subprogram comes from source (after one or more
400 -- derivations) in which case the reference is to parent subprogram.
402 elsif Is_Overloadable (E)
403 and then Present (Alias (E))
408 if Comes_From_Source (Ent) then
410 elsif No (Alias (Ent)) then
417 -- Record components of discriminated subtypes or derived types
418 -- must be treated as references to the original component.
420 elsif Ekind (E) = E_Component
421 and then Comes_From_Source (Original_Record_Component (E))
423 Ent := Original_Record_Component (E);
425 -- Ignore reference to any other entity that is not from source
431 -- Record reference to entity
433 Ref := Original_Location (Sloc (Nod));
434 Def := Original_Location (Sloc (Ent));
436 Xrefs.Increment_Last;
439 Xrefs.Table (Indx).Loc := Ref;
441 -- Overriding operations are marked with 'P'.
444 and then Is_Subprogram (N)
445 and then Is_Overriding_Operation (N)
447 Xrefs.Table (Indx).Typ := 'P';
449 Xrefs.Table (Indx).Typ := Typ;
452 Xrefs.Table (Indx).Eun := Get_Source_Unit (Def);
453 Xrefs.Table (Indx).Lun := Get_Source_Unit (Ref);
454 Xrefs.Table (Indx).Ent := Ent;
455 Set_Has_Xref_Entry (Ent);
457 end Generate_Reference;
459 -----------------------------------
460 -- Generate_Reference_To_Formals --
461 -----------------------------------
463 procedure Generate_Reference_To_Formals (E : Entity_Id) is
467 if Is_Generic_Subprogram (E) then
468 Formal := First_Entity (E);
470 while Present (Formal)
471 and then not Is_Formal (Formal)
473 Next_Entity (Formal);
477 Formal := First_Formal (E);
480 while Present (Formal) loop
481 if Ekind (Formal) = E_In_Parameter then
483 if Nkind (Parameter_Type (Parent (Formal)))
484 = N_Access_Definition
486 Generate_Reference (E, Formal, '^', False);
488 Generate_Reference (E, Formal, '>', False);
491 elsif Ekind (Formal) = E_In_Out_Parameter then
492 Generate_Reference (E, Formal, '=', False);
495 Generate_Reference (E, Formal, '<', False);
498 Next_Formal (Formal);
500 end Generate_Reference_To_Formals;
502 -------------------------------------------
503 -- Generate_Reference_To_Generic_Formals --
504 -------------------------------------------
506 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
510 Formal := First_Entity (E);
512 while Present (Formal) loop
513 if Comes_From_Source (Formal) then
514 Generate_Reference (E, Formal, 'z', False);
517 Next_Entity (Formal);
519 end Generate_Reference_To_Generic_Formals;
525 procedure Initialize is
530 -----------------------
531 -- Output_References --
532 -----------------------
534 procedure Output_References is
536 procedure Get_Type_Reference
538 Tref : out Entity_Id;
539 Left : out Character;
540 Right : out Character);
541 -- Given an entity id Ent, determines whether a type reference is
542 -- required. If so, Tref is set to the entity for the type reference
543 -- and Left and Right are set to the left/right brackets to be
544 -- output for the reference. If no type reference is required, then
545 -- Tref is set to Empty, and Left/Right are set to space.
547 procedure Output_Import_Export_Info (Ent : Entity_Id);
548 -- Ouput language and external name information for an interfaced
549 -- entity, using the format <language, external_name>,
551 ------------------------
552 -- Get_Type_Reference --
553 ------------------------
555 procedure Get_Type_Reference
557 Tref : out Entity_Id;
558 Left : out Character;
559 Right : out Character)
564 -- See if we have a type reference
573 -- Processing for types
575 if Is_Type (Tref) then
579 if Base_Type (Tref) = Tref then
581 -- If derived, then get first subtype
583 if Tref /= Etype (Tref) then
584 Tref := First_Subtype (Etype (Tref));
586 -- Set brackets for derived type, but don't
587 -- override pointer case since the fact that
588 -- something is a pointer is more important
595 -- If non-derived ptr, get directly designated type.
596 -- If the type has a full view, all references are
597 -- on the partial view, that is seen first.
599 elsif Is_Access_Type (Tref) then
600 Tref := Directly_Designated_Type (Tref);
604 elsif Is_Private_Type (Tref)
605 and then Present (Full_View (Tref))
606 and then Is_Access_Type (Full_View (Tref))
608 Tref := Directly_Designated_Type (Full_View (Tref));
612 -- If non-derived array, get component type.
613 -- Skip component type for case of String
614 -- or Wide_String, saves worthwhile space.
616 elsif Is_Array_Type (Tref)
617 and then Tref /= Standard_String
618 and then Tref /= Standard_Wide_String
620 Tref := Component_Type (Tref);
624 -- For other non-derived base types, nothing
630 -- For a subtype, go to ancestor subtype.
633 Tref := Ancestor_Subtype (Tref);
635 -- If no ancestor subtype, go to base type
638 Tref := Base_Type (Sav);
642 -- For objects, functions, enum literals,
643 -- just get type from Etype field.
645 elsif Is_Object (Tref)
646 or else Ekind (Tref) = E_Enumeration_Literal
647 or else Ekind (Tref) = E_Function
648 or else Ekind (Tref) = E_Operator
650 Tref := Etype (Tref);
652 -- For anything else, exit
658 -- Exit if no type reference, or we are stuck in
659 -- some loop trying to find the type reference, or
660 -- if the type is standard void type (the latter is
661 -- an implementation artifact that should not show
662 -- up in the generated cross-references).
666 or else Tref = Standard_Void_Type;
668 -- If we have a usable type reference, return, otherwise
669 -- keep looking for something useful (we are looking for
670 -- something that either comes from source or standard)
672 if Sloc (Tref) = Standard_Location
673 or else Comes_From_Source (Tref)
675 -- If the reference is a subtype created for a generic
676 -- actual, go to actual directly, the inner subtype is
679 if Nkind (Parent (Tref)) = N_Subtype_Declaration
680 and then not Comes_From_Source (Parent (Tref))
682 (Is_Wrapper_Package (Scope (Tref))
683 or else Is_Generic_Instance (Scope (Tref)))
685 Tref := Base_Type (Tref);
692 -- If we fall through the loop, no type reference
697 end Get_Type_Reference;
699 -------------------------------
700 -- Output_Import_Export_Info --
701 -------------------------------
703 procedure Output_Import_Export_Info (Ent : Entity_Id) is
704 Language_Name : Name_Id;
705 Conv : constant Convention_Id := Convention (Ent);
707 if Conv = Convention_C then
708 Language_Name := Name_C;
710 elsif Conv = Convention_CPP then
711 Language_Name := Name_CPP;
713 elsif Conv = Convention_Ada then
714 Language_Name := Name_Ada;
717 -- These are the only languages that GPS knows about.
722 Write_Info_Char ('<');
723 Get_Unqualified_Name_String (Language_Name);
725 for J in 1 .. Name_Len loop
726 Write_Info_Char (Name_Buffer (J));
729 if Present (Interface_Name (Ent)) then
730 Write_Info_Char (',');
731 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
733 for J in 1 .. Name_Len loop
734 Write_Info_Char (Name_Buffer (J));
738 Write_Info_Char ('>');
739 end Output_Import_Export_Info;
741 -- Start of processing for Output_References
744 if not Opt.Xref_Active then
748 -- Before we go ahead and output the references we have a problem
749 -- that needs dealing with. So far we have captured things that are
750 -- definitely referenced by the main unit, or defined in the main
751 -- unit. That's because we don't want to clutter up the ali file
752 -- for this unit with definition lines for entities in other units
753 -- that are not referenced.
755 -- But there is a glitch. We may reference an entity in another unit,
756 -- and it may have a type reference to an entity that is not directly
757 -- referenced in the main unit, which may mean that there is no xref
758 -- entry for this entity yet in the list of references.
760 -- If we don't do something about this, we will end with an orphan
761 -- type reference, i.e. it will point to an entity that does not
762 -- appear within the generated references in the ali file. That is
763 -- not good for tools using the xref information.
765 -- To fix this, we go through the references adding definition
766 -- entries for any unreferenced entities that can be referenced
767 -- in a type reference. There is a recursion problem here, and
768 -- that is dealt with by making sure that this traversal also
769 -- traverses any entries that get added by the traversal.
780 -- Note that this is not a for loop for a very good reason. The
781 -- processing of items in the table can add new items to the
782 -- table, and they must be processed as well
785 while J <= Xrefs.Last loop
786 Ent := Xrefs.Table (J).Ent;
787 Get_Type_Reference (Ent, Tref, L, R);
790 and then not Has_Xref_Entry (Tref)
791 and then Sloc (Tref) > No_Location
793 Xrefs.Increment_Last;
795 Loc := Original_Location (Sloc (Tref));
796 Xrefs.Table (Indx).Ent := Tref;
797 Xrefs.Table (Indx).Loc := No_Location;
798 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
799 Xrefs.Table (Indx).Lun := No_Unit;
800 Set_Has_Xref_Entry (Tref);
803 -- Collect inherited primitive operations that may be
804 -- declared in another unit and have no visible reference
805 -- in the current one.
808 and then Is_Tagged_Type (Ent)
809 and then Is_Derived_Type (Ent)
810 and then Ent = Base_Type (Ent)
811 and then In_Extended_Main_Source_Unit (Ent)
814 Op_List : constant Elist_Id := Primitive_Operations (Ent);
818 function Parent_Op (E : Entity_Id) return Entity_Id;
819 -- Find original operation, which may be inherited
820 -- through several derivations.
822 function Parent_Op (E : Entity_Id) return Entity_Id is
823 Orig_Op : constant Entity_Id := Alias (E);
827 elsif not Comes_From_Source (E)
828 and then not Has_Xref_Entry (Orig_Op)
829 and then Comes_From_Source (Orig_Op)
833 return Parent_Op (Orig_Op);
838 Op := First_Elmt (Op_List);
839 while Present (Op) loop
840 Prim := Parent_Op (Node (Op));
842 if Present (Prim) then
843 Xrefs.Increment_Last;
845 Loc := Original_Location (Sloc (Prim));
846 Xrefs.Table (Indx).Ent := Prim;
847 Xrefs.Table (Indx).Loc := No_Location;
848 Xrefs.Table (Indx).Eun :=
849 Get_Source_Unit (Sloc (Prim));
850 Xrefs.Table (Indx).Lun := No_Unit;
851 Set_Has_Xref_Entry (Prim);
863 -- Now we have all the references, including those for any embedded
864 -- type references, so we can sort them, and output them.
866 Output_Refs : declare
868 Nrefs : Nat := Xrefs.Last;
869 -- Number of references in table. This value may get reset
870 -- (reduced) when we eliminate duplicate reference entries.
872 Rnums : array (0 .. Nrefs) of Nat;
873 -- This array contains numbers of references in the Xrefs table.
874 -- This list is sorted in output order. The extra 0'th entry is
875 -- convenient for the call to sort. When we sort the table, we
876 -- move the entries in Rnums around, but we do not move the
877 -- original table entries.
879 Curxu : Unit_Number_Type;
882 Curru : Unit_Number_Type;
883 -- Current reference unit for one entity
885 Cursrc : Source_Buffer_Ptr;
886 -- Current xref unit source text
891 Curnam : String (1 .. Name_Buffer'Length);
893 -- Simple name and length of current entity
896 -- Original source location for current entity
899 -- Current reference location
902 -- Entity type character
908 -- Renaming reference
910 Trunit : Unit_Number_Type;
911 -- Unit number for type reference
913 function Lt (Op1, Op2 : Natural) return Boolean;
914 -- Comparison function for Sort call
916 function Name_Change (X : Entity_Id) return Boolean;
917 -- Determines if entity X has a different simple name from Curent
919 procedure Move (From : Natural; To : Natural);
920 -- Move procedure for Sort call
926 function Lt (Op1, Op2 : Natural) return Boolean is
927 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
928 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
931 -- First test. If entity is in different unit, sort by unit
933 if T1.Eun /= T2.Eun then
934 return Dependency_Num (T1.Eun) < Dependency_Num (T2.Eun);
936 -- Second test, within same unit, sort by entity Sloc
938 elsif T1.Def /= T2.Def then
939 return T1.Def < T2.Def;
941 -- Third test, sort definitions ahead of references
943 elsif T1.Loc = No_Location then
946 elsif T2.Loc = No_Location then
949 -- Fourth test, for same entity, sort by reference location unit
951 elsif T1.Lun /= T2.Lun then
952 return Dependency_Num (T1.Lun) < Dependency_Num (T2.Lun);
954 -- Fifth test order of location within referencing unit
956 elsif T1.Loc /= T2.Loc then
957 return T1.Loc < T2.Loc;
959 -- Finally, for two locations at the same address, we prefer
960 -- the one that does NOT have the type 'r' so that a modification
961 -- or extension takes preference, when there are more than one
962 -- reference at the same location.
973 procedure Move (From : Natural; To : Natural) is
975 Rnums (Nat (To)) := Rnums (Nat (From));
982 function Name_Change (X : Entity_Id) return Boolean is
984 Get_Unqualified_Name_String (Chars (X));
986 if Name_Len /= Curlen then
990 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
994 -- Start of processing for Output_Refs
997 -- Capture the definition Sloc values. We delay doing this till now,
998 -- since at the time the reference or definition is made, private
999 -- types may be swapped, and the Sloc value may be incorrect. We
1000 -- also set up the pointer vector for the sort.
1002 for J in 1 .. Nrefs loop
1004 Xrefs.Table (J).Def :=
1005 Original_Location (Sloc (Xrefs.Table (J).Ent));
1008 -- Sort the references
1010 GNAT.Heap_Sort_A.Sort
1012 Move'Unrestricted_Access,
1013 Lt'Unrestricted_Access);
1015 -- Eliminate duplicate entries
1018 NR : constant Nat := Nrefs;
1021 -- We need this test for NR because if we force ALI file
1022 -- generation in case of errors detected, it may be the case
1023 -- that Nrefs is 0, so we should not reset it here
1028 for J in 2 .. NR loop
1029 if Xrefs.Table (Rnums (J)) /=
1030 Xrefs.Table (Rnums (Nrefs))
1033 Rnums (Nrefs) := Rnums (J);
1039 -- Initialize loop through references
1043 Curdef := No_Location;
1045 Crloc := No_Location;
1047 -- Loop to output references
1049 for Refno in 1 .. Nrefs loop
1050 Output_One_Ref : declare
1056 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
1057 -- The current entry to be accessed
1060 -- Used to index into source buffer to get entity name
1064 -- Used for {} or <> or () for type reference
1066 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
1067 -- Recursive procedure to output instantiation references for
1068 -- the given source ptr in [file|line[...]] form. No output
1069 -- if the given location is not a generic template reference.
1071 -------------------------------
1072 -- Output_Instantiation_Refs --
1073 -------------------------------
1075 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
1076 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
1077 Lun : Unit_Number_Type;
1078 Cu : constant Unit_Number_Type := Curru;
1081 -- Nothing to do if this is not an instantiation
1083 if Iloc = No_Location then
1087 -- Output instantiation reference
1089 Write_Info_Char ('[');
1090 Lun := Get_Source_Unit (Iloc);
1092 if Lun /= Curru then
1094 Write_Info_Nat (Dependency_Num (Curru));
1095 Write_Info_Char ('|');
1098 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
1100 -- Recursive call to get nested instantiations
1102 Output_Instantiation_Refs (Iloc);
1104 -- Output final ] after call to get proper nesting
1106 Write_Info_Char (']');
1109 end Output_Instantiation_Refs;
1111 -- Start of processing for Output_One_Ref
1115 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1117 -- Skip reference if it is the only reference to an entity,
1118 -- and it is an end-line reference, and the entity is not in
1119 -- the current extended source. This prevents junk entries
1120 -- consisting only of packages with end lines, where no
1121 -- entity from the package is actually referenced.
1124 and then Ent /= Curent
1125 and then (Refno = Nrefs or else
1126 Ent /= Xrefs.Table (Rnums (Refno + 1)).Ent)
1128 not In_Extended_Main_Source_Unit (Ent)
1133 -- For private type, get full view type
1136 and then Present (Full_View (XE.Ent))
1138 Ent := Underlying_Type (Ent);
1140 if Present (Ent) then
1141 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1145 -- Special exception for Boolean
1147 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
1151 -- For variable reference, get corresponding type
1154 Ent := Etype (XE.Ent);
1155 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1157 -- If variable is private type, get full view type
1160 and then Present (Full_View (Etype (XE.Ent)))
1162 Ent := Underlying_Type (Etype (XE.Ent));
1164 if Present (Ent) then
1165 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1169 -- Special handling for access parameter
1172 K : constant Entity_Kind := Ekind (Etype (XE.Ent));
1175 if (K = E_Anonymous_Access_Type
1177 K = E_Anonymous_Access_Subprogram_Type
1179 E_Anonymous_Access_Protected_Subprogram_Type)
1180 and then Is_Formal (XE.Ent)
1184 -- Special handling for Boolean
1186 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
1192 -- Special handling for abstract types and operations.
1194 if Is_Abstract (XE.Ent) then
1197 Ctyp := 'x'; -- abstract procedure
1199 elsif Ctyp = 'V' then
1200 Ctyp := 'y'; -- abstract function
1202 elsif Ctyp = 'R' then
1203 Ctyp := 'H'; -- abstract type
1207 -- Only output reference if interesting type of entity,
1208 -- and suppress self references, except for bodies that
1209 -- act as specs. Also suppress definitions of body formals
1210 -- (we only treat these as references, and the references
1211 -- were separately recorded).
1214 or else (XE.Loc = XE.Def
1217 or else not Is_Subprogram (XE.Ent)))
1218 or else (Is_Formal (XE.Ent)
1219 and then Present (Spec_Entity (XE.Ent)))
1224 -- Start new Xref section if new xref unit
1226 if XE.Eun /= Curxu then
1227 if Write_Info_Col > 1 then
1232 Cursrc := Source_Text (Source_Index (Curxu));
1234 Write_Info_Initiate ('X');
1235 Write_Info_Char (' ');
1236 Write_Info_Nat (Dependency_Num (XE.Eun));
1237 Write_Info_Char (' ');
1238 Write_Info_Name (Reference_Name (Source_Index (XE.Eun)));
1241 -- Start new Entity line if new entity. Note that we
1242 -- consider two entities the same if they have the same
1243 -- name and source location. This causes entities in
1244 -- instantiations to be treated as though they referred
1251 (Name_Change (XE.Ent) or else XE.Def /= Curdef))
1256 Get_Unqualified_Name_String (Chars (XE.Ent));
1258 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
1260 if Write_Info_Col > 1 then
1264 -- Write column number information
1266 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
1267 Write_Info_Char (Ctyp);
1268 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
1270 -- Write level information
1272 Write_Level_Info : declare
1273 function Is_Visible_Generic_Entity
1274 (E : Entity_Id) return Boolean;
1275 -- Check whether E is declared in the visible part
1276 -- of a generic package. For source navigation
1277 -- purposes, treat this as a visible entity.
1279 function Is_Private_Record_Component
1280 (E : Entity_Id) return Boolean;
1281 -- Check whether E is a non-inherited component of a
1282 -- private extension. Even if the enclosing record is
1283 -- public, we want to treat the component as private
1284 -- for navigation purposes.
1286 ---------------------------------
1287 -- Is_Private_Record_Component --
1288 ---------------------------------
1290 function Is_Private_Record_Component
1291 (E : Entity_Id) return Boolean
1293 S : constant Entity_Id := Scope (E);
1296 Ekind (E) = E_Component
1297 and then Nkind (Declaration_Node (S)) =
1298 N_Private_Extension_Declaration
1299 and then Original_Record_Component (E) = E;
1300 end Is_Private_Record_Component;
1302 -------------------------------
1303 -- Is_Visible_Generic_Entity --
1304 -------------------------------
1306 function Is_Visible_Generic_Entity
1307 (E : Entity_Id) return Boolean
1312 if Ekind (Scope (E)) /= E_Generic_Package then
1317 while Present (Par) loop
1319 Nkind (Par) = N_Generic_Package_Declaration
1321 -- Entity is a generic formal
1326 Nkind (Parent (Par)) = N_Package_Specification
1329 Is_List_Member (Par)
1330 and then List_Containing (Par) =
1331 Visible_Declarations (Parent (Par));
1333 Par := Parent (Par);
1338 end Is_Visible_Generic_Entity;
1340 -- Start of processing for Write_Level_Info
1343 if Is_Hidden (Curent)
1344 or else Is_Private_Record_Component (Curent)
1346 Write_Info_Char (' ');
1350 or else Is_Visible_Generic_Entity (Curent)
1352 Write_Info_Char ('*');
1355 Write_Info_Char (' ');
1357 end Write_Level_Info;
1359 -- Output entity name. We use the occurrence from the
1360 -- actual source program at the definition point
1362 P := Original_Location (Sloc (XE.Ent));
1364 -- Entity is character literal
1366 if Cursrc (P) = ''' then
1367 Write_Info_Char (Cursrc (P));
1368 Write_Info_Char (Cursrc (P + 1));
1369 Write_Info_Char (Cursrc (P + 2));
1371 -- Entity is operator symbol
1373 elsif Cursrc (P) = '"' or else Cursrc (P) = '%' then
1374 Write_Info_Char (Cursrc (P));
1379 Write_Info_Char (Cursrc (P2));
1380 exit when Cursrc (P2) = Cursrc (P);
1383 -- Entity is identifier
1387 if Is_Start_Of_Wide_Char (Cursrc, P) then
1388 Scan_Wide (Cursrc, P, WC, Err);
1389 elsif not Identifier_Char (Cursrc (P)) then
1397 Original_Location (Sloc (XE.Ent)) .. P - 1
1399 Write_Info_Char (Cursrc (J));
1403 -- See if we have a renaming reference
1405 if Is_Object (XE.Ent)
1406 and then Present (Renamed_Object (XE.Ent))
1408 Rref := Renamed_Object (XE.Ent);
1410 elsif Is_Overloadable (XE.Ent)
1411 and then Nkind (Parent (Declaration_Node (XE.Ent))) =
1412 N_Subprogram_Renaming_Declaration
1414 Rref := Name (Parent (Declaration_Node (XE.Ent)));
1416 elsif Ekind (XE.Ent) = E_Package
1417 and then Nkind (Declaration_Node (XE.Ent)) =
1418 N_Package_Renaming_Declaration
1420 Rref := Name (Declaration_Node (XE.Ent));
1426 if Present (Rref) then
1427 if Nkind (Rref) = N_Expanded_Name then
1428 Rref := Selector_Name (Rref);
1431 if Nkind (Rref) /= N_Identifier then
1436 -- Write out renaming reference if we have one
1438 if Present (Rref) then
1439 Write_Info_Char ('=');
1441 (Int (Get_Logical_Line_Number (Sloc (Rref))));
1442 Write_Info_Char (':');
1444 (Int (Get_Column_Number (Sloc (Rref))));
1447 -- Indicate that the entity is in the unit
1448 -- of the current xref xection.
1452 -- See if we have a type reference and if so output
1454 Get_Type_Reference (XE.Ent, Tref, Left, Right);
1456 if Present (Tref) then
1458 -- Case of standard entity, output name
1460 if Sloc (Tref) = Standard_Location then
1461 Write_Info_Char (Left);
1462 Write_Info_Name (Chars (Tref));
1463 Write_Info_Char (Right);
1465 -- Case of source entity, output location
1468 Write_Info_Char (Left);
1469 Trunit := Get_Source_Unit (Sloc (Tref));
1471 if Trunit /= Curxu then
1472 Write_Info_Nat (Dependency_Num (Trunit));
1473 Write_Info_Char ('|');
1477 (Int (Get_Logical_Line_Number (Sloc (Tref))));
1480 Ent : Entity_Id := Tref;
1481 Kind : constant Entity_Kind := Ekind (Ent);
1482 Ctyp : Character := Xref_Entity_Letters (Kind);
1486 and then Present (Full_View (Ent))
1488 Ent := Underlying_Type (Ent);
1490 if Present (Ent) then
1491 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1495 Write_Info_Char (Ctyp);
1499 (Int (Get_Column_Number (Sloc (Tref))));
1501 -- If the type comes from an instantiation,
1502 -- add the corresponding info.
1504 Output_Instantiation_Refs (Sloc (Tref));
1505 Write_Info_Char (Right);
1509 -- End of processing for entity output
1511 Crloc := No_Location;
1514 -- Output the reference
1516 if XE.Loc /= No_Location
1517 and then XE.Loc /= Crloc
1521 -- Start continuation if line full, else blank
1523 if Write_Info_Col > 72 then
1525 Write_Info_Initiate ('.');
1528 Write_Info_Char (' ');
1530 -- Output file number if changed
1532 if XE.Lun /= Curru then
1534 Write_Info_Nat (Dependency_Num (Curru));
1535 Write_Info_Char ('|');
1538 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Loc)));
1539 Write_Info_Char (XE.Typ);
1541 if Is_Overloadable (XE.Ent)
1542 and then Is_Imported (XE.Ent)
1543 and then XE.Typ = 'b'
1545 Output_Import_Export_Info (XE.Ent);
1548 Write_Info_Nat (Int (Get_Column_Number (XE.Loc)));
1550 Output_Instantiation_Refs (Sloc (XE.Ent));
1561 end Output_References;