1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1998-2001, Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Csets; use Csets;
31 with Debug; use Debug;
32 with Lib.Util; use Lib.Util;
33 with Namet; use Namet;
35 with Sinfo; use Sinfo;
36 with Sinput; use Sinput;
37 with Table; use Table;
38 with Widechar; use Widechar;
40 with GNAT.Heap_Sort_A;
42 package body Lib.Xref is
48 -- The Xref table is used to record references. The Loc field is set
49 -- to No_Location for a definition entry.
51 subtype Xref_Entry_Number is Int;
53 type Xref_Entry is record
55 -- Entity referenced (E parameter to Generate_Reference)
58 -- Original source location for entity being referenced. Note that
59 -- these values are used only during the output process, they are
60 -- not set when the entries are originally built. This is because
61 -- private entities can be swapped when the initial call is made.
64 -- Location of reference (Original_Location (Sloc field of N parameter
65 -- to Generate_Reference). Set to No_Location for the case of a
66 -- defining occurrence.
69 -- Reference type (Typ param to Generate_Reference)
71 Eun : Unit_Number_Type;
72 -- Unit number corresponding to Ent
74 Lun : Unit_Number_Type;
75 -- Unit number corresponding to Loc. Value is undefined and not
76 -- referenced if Loc is set to No_Location.
80 package Xrefs is new Table.Table (
81 Table_Component_Type => Xref_Entry,
82 Table_Index_Type => Int,
84 Table_Initial => Alloc.Xrefs_Initial,
85 Table_Increment => Alloc.Xrefs_Increment,
86 Table_Name => "Xrefs");
88 -------------------------
89 -- Generate_Definition --
90 -------------------------
92 procedure Generate_Definition (E : Entity_Id) is
97 pragma Assert (Nkind (E) in N_Entity);
99 -- Note that we do not test Xref_Entity_Letters here. It is too
100 -- early to do so, since we are often called before the entity
101 -- is fully constructed, so that the Ekind is still E_Void.
105 -- Definition must come from source
107 and then Comes_From_Source (E)
109 -- And must have a reasonable source location that is not
110 -- within an instance (all entities in instances are ignored)
112 and then Sloc (E) > No_Location
113 and then Instantiation_Location (Sloc (E)) = No_Location
115 -- And must be a non-internal name from the main source unit
117 and then In_Extended_Main_Source_Unit (E)
118 and then not Is_Internal_Name (Chars (E))
120 Xrefs.Increment_Last;
122 Loc := Original_Location (Sloc (E));
124 Xrefs.Table (Indx).Ent := E;
125 Xrefs.Table (Indx).Loc := No_Location;
126 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
127 Xrefs.Table (Indx).Lun := No_Unit;
129 end Generate_Definition;
131 ---------------------------------
132 -- Generate_Operator_Reference --
133 ---------------------------------
135 procedure Generate_Operator_Reference (N : Node_Id) is
137 if not In_Extended_Main_Source_Unit (N) then
141 -- If the operator is not a Standard operator, then we generate
142 -- a real reference to the user defined operator.
144 if Sloc (Entity (N)) /= Standard_Location then
145 Generate_Reference (Entity (N), N);
147 -- A reference to an implicit inequality operator is a also a
148 -- reference to the user-defined equality.
150 if Nkind (N) = N_Op_Ne
151 and then not Comes_From_Source (Entity (N))
152 and then Present (Corresponding_Equality (Entity (N)))
154 Generate_Reference (Corresponding_Equality (Entity (N)), N);
157 -- For the case of Standard operators, we mark the result type
158 -- as referenced. This ensures that in the case where we are
159 -- using a derived operator, we mark an entity of the unit that
160 -- implicitly defines this operator as used. Otherwise we may
161 -- think that no entity of the unit is used. The actual entity
162 -- marked as referenced is the first subtype, which is the user
163 -- defined entity that is relevant.
166 if Nkind (N) = N_Op_Eq
167 or else Nkind (N) = N_Op_Ne
168 or else Nkind (N) = N_Op_Le
169 or else Nkind (N) = N_Op_Lt
170 or else Nkind (N) = N_Op_Ge
171 or else Nkind (N) = N_Op_Gt
173 Set_Referenced (First_Subtype (Etype (Right_Opnd (N))));
175 Set_Referenced (First_Subtype (Etype (N)));
178 end Generate_Operator_Reference;
180 ------------------------
181 -- Generate_Reference --
182 ------------------------
184 procedure Generate_Reference
187 Typ : Character := 'r';
188 Set_Ref : Boolean := True;
189 Force : Boolean := False)
198 pragma Assert (Nkind (E) in N_Entity);
200 -- Never collect references if not in main source unit. However,
201 -- we omit this test if Typ is 'e', since these entries are
202 -- really structural, and it is useful to have them in units
203 -- that reference packages as well as units that define packages.
205 if not In_Extended_Main_Source_Unit (N)
211 -- Unless the reference is forced, we ignore references where
212 -- the reference itself does not come from Source.
214 if not Force and then not Comes_From_Source (N) then
218 -- Deal with setting entity as referenced, unless suppressed.
219 -- Note that we still do Set_Referenced on entities that do not
220 -- come from source. This situation arises when we have a source
221 -- reference to a derived operation, where the derived operation
222 -- itself does not come from source, but we still want to mark it
223 -- as referenced, since we really are referencing an entity in the
224 -- corresponding package (this avoids incorrect complaints that the
225 -- package contains no referenced entities).
230 -- If this is a subprogram instance, mark as well the internal
231 -- subprogram in the wrapper package, which may be a visible
234 if Is_Overloadable (E)
235 and then Is_Generic_Instance (E)
236 and then Present (Alias (E))
238 Set_Referenced (Alias (E));
242 -- Generate reference if all conditions are met:
245 -- Cross referencing must be active
249 -- The entity must be one for which we collect references
251 and then Xref_Entity_Letters (Ekind (E)) /= ' '
253 -- Both Sloc values must be set to something sensible
255 and then Sloc (E) > No_Location
256 and then Sloc (N) > No_Location
258 -- We ignore references from within an instance
260 and then Instantiation_Location (Sloc (N)) = No_Location
262 -- Ignore dummy references
266 if Nkind (N) = N_Identifier
268 Nkind (N) = N_Defining_Identifier
272 Nkind (N) = N_Defining_Operator_Symbol
274 (Nkind (N) = N_Character_Literal
275 and then Sloc (Entity (N)) /= Standard_Location)
277 Nkind (N) = N_Defining_Character_Literal
281 elsif Nkind (N) = N_Expanded_Name
283 Nkind (N) = N_Selected_Component
285 Nod := Selector_Name (N);
291 -- Normal case of source entity comes from source
293 if Comes_From_Source (E) then
296 -- Entity does not come from source, but is a derived subprogram
297 -- and the derived subprogram comes from source, in which case
298 -- the reference is to this parent subprogram.
300 elsif Is_Overloadable (E)
301 and then Present (Alias (E))
302 and then Comes_From_Source (Alias (E))
306 -- Ignore reference to any other source that is not from source
312 -- Record reference to entity
314 Ref := Original_Location (Sloc (Nod));
315 Def := Original_Location (Sloc (Ent));
317 Xrefs.Increment_Last;
320 Xrefs.Table (Indx).Loc := Ref;
321 Xrefs.Table (Indx).Typ := Typ;
322 Xrefs.Table (Indx).Eun := Get_Source_Unit (Def);
323 Xrefs.Table (Indx).Lun := Get_Source_Unit (Ref);
324 Xrefs.Table (Indx).Ent := Ent;
326 end Generate_Reference;
328 -----------------------
329 -- Output_References --
330 -----------------------
332 procedure Output_References is
333 Nrefs : constant Nat := Xrefs.Last;
335 Rnums : array (0 .. Nrefs) of Nat;
336 -- This array contains numbers of references in the Xrefs table. This
337 -- list is sorted in output order. The extra 0'th entry is convenient
338 -- for the call to sort. When we sort the table, we move these entries
339 -- around, but we do not move the original table entries.
341 function Lt (Op1, Op2 : Natural) return Boolean;
342 -- Comparison function for Sort call
344 procedure Move (From : Natural; To : Natural);
345 -- Move procedure for Sort call
347 function Lt (Op1, Op2 : Natural) return Boolean is
348 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
349 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
352 -- First test. If entity is in different unit, sort by unit
354 if T1.Eun /= T2.Eun then
355 return Dependency_Num (T1.Eun) < Dependency_Num (T2.Eun);
357 -- Second test, within same unit, sort by entity Sloc
359 elsif T1.Def /= T2.Def then
360 return T1.Def < T2.Def;
362 -- Third test, sort definitions ahead of references
364 elsif T1.Loc = No_Location then
367 elsif T2.Loc = No_Location then
370 -- Fourth test, for same entity, sort by reference location unit
372 elsif T1.Lun /= T2.Lun then
373 return Dependency_Num (T1.Lun) < Dependency_Num (T2.Lun);
375 -- Fifth test order of location within referencing unit
377 elsif T1.Loc /= T2.Loc then
378 return T1.Loc < T2.Loc;
380 -- Finally, for two locations at the same address, we prefer
381 -- the one that does NOT have the type 'r' so that a modification
382 -- or extension takes preference, when there are more than one
383 -- reference at the same location.
390 procedure Move (From : Natural; To : Natural) is
392 Rnums (Nat (To)) := Rnums (Nat (From));
395 -- Start of processing for Output_References
398 if not Opt.Xref_Active then
402 -- Capture the definition Sloc values. We delay doing this till now,
403 -- since at the time the reference or definition is made, private
404 -- types may be swapped, and the Sloc value may be incorrect. We
405 -- also set up the pointer vector for the sort.
407 for J in 1 .. Nrefs loop
409 Xrefs.Table (J).Def :=
410 Original_Location (Sloc (Xrefs.Table (J).Ent));
413 -- Sort the references
415 GNAT.Heap_Sort_A.Sort
417 Move'Unrestricted_Access,
418 Lt'Unrestricted_Access);
420 -- Now output the references
422 Output_Refs : declare
424 Curxu : Unit_Number_Type;
427 Curru : Unit_Number_Type;
428 -- Current reference unit for one entity
430 Cursrc : Source_Buffer_Ptr;
431 -- Current xref unit source text
436 Curnam : String (1 .. Name_Buffer'Length);
438 -- Simple name and length of current entity
441 -- Original source location for current entity
444 -- Current reference location
447 -- Entity type character
453 -- Renaming reference
455 Trunit : Unit_Number_Type;
456 -- Unit number for type reference
458 function Name_Change (X : Entity_Id) return Boolean;
459 -- Determines if entity X has a different simple name from Curent
465 function Name_Change (X : Entity_Id) return Boolean is
467 Get_Unqualified_Name_String (Chars (X));
469 if Name_Len /= Curlen then
473 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
477 -- Start of processing for Output_Refs
482 Curdef := No_Location;
484 Crloc := No_Location;
486 for Refno in 1 .. Nrefs loop
488 Output_One_Ref : declare
490 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
491 -- The current entry to be accessed
494 -- Used to index into source buffer to get entity name
504 -- Used for {} or <> for type reference
506 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
507 -- Recursive procedure to output instantiation references for
508 -- the given source ptr in [file|line[...]] form. No output
509 -- if the given location is not a generic template reference.
511 -------------------------------
512 -- Output_Instantiation_Refs --
513 -------------------------------
515 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
516 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
517 Lun : Unit_Number_Type;
520 -- Nothing to do if this is not an instantiation
522 if Iloc = No_Location then
526 -- For now, nothing to do unless special debug flag set
528 if not Debug_Flag_MM then
532 -- Output instantiation reference
534 Write_Info_Char ('[');
535 Lun := Get_Source_Unit (Iloc);
539 Write_Info_Nat (Dependency_Num (Curru));
540 Write_Info_Char ('|');
543 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
545 -- Recursive call to get nested instantiations
547 Output_Instantiation_Refs (Iloc);
549 -- Output final ] after call to get proper nesting
551 Write_Info_Char (']');
553 end Output_Instantiation_Refs;
555 -- Start of processing for Output_One_Ref
559 Ctyp := Xref_Entity_Letters (Ekind (Ent));
561 -- Skip reference if it is the only reference to an entity,
562 -- and it is an end-line reference, and the entity is not in
563 -- the current extended source. This prevents junk entries
564 -- consisting only of packages with end lines, where no
565 -- entity from the package is actually referenced.
568 and then Ent /= Curent
569 and then (Refno = Nrefs or else
570 Ent /= Xrefs.Table (Rnums (Refno + 1)).Ent)
572 not In_Extended_Main_Source_Unit (Ent)
577 -- For private type, get full view type
580 and then Present (Full_View (XE.Ent))
582 Ent := Underlying_Type (Ent);
584 if Present (Ent) then
585 Ctyp := Xref_Entity_Letters (Ekind (Ent));
589 -- Special exception for Boolean
591 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
595 -- For variable reference, get corresponding type
598 Ent := Etype (XE.Ent);
599 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
601 -- If variable is private type, get full view type
604 and then Present (Full_View (Etype (XE.Ent)))
606 Ent := Underlying_Type (Etype (XE.Ent));
608 if Present (Ent) then
609 Ctyp := Xref_Entity_Letters (Ekind (Ent));
613 -- Special handling for access parameter
615 if Ekind (Etype (XE.Ent)) = E_Anonymous_Access_Type
616 and then Is_Formal (XE.Ent)
620 -- Special handling for Boolean
622 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
627 -- Only output reference if interesting type of entity,
628 -- and suppress self references. Also suppress definitions
629 -- of body formals (we only treat these as references, and
630 -- the references were separately recorded).
633 and then XE.Loc /= XE.Def
634 and then (not Is_Formal (XE.Ent)
635 or else No (Spec_Entity (XE.Ent)))
637 -- Start new Xref section if new xref unit
639 if XE.Eun /= Curxu then
641 if Write_Info_Col > 1 then
646 Cursrc := Source_Text (Source_Index (Curxu));
648 Write_Info_Initiate ('X');
649 Write_Info_Char (' ');
650 Write_Info_Nat (Dependency_Num (XE.Eun));
651 Write_Info_Char (' ');
652 Write_Info_Name (Reference_Name (Source_Index (XE.Eun)));
655 -- Start new Entity line if new entity. Note that we
656 -- consider two entities the same if they have the same
657 -- name and source location. This causes entities in
658 -- instantiations to be treated as though they referred
665 (Name_Change (XE.Ent) or else XE.Def /= Curdef))
670 Get_Unqualified_Name_String (Chars (XE.Ent));
672 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
674 if Write_Info_Col > 1 then
678 -- Write column number information
680 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
681 Write_Info_Char (Ctyp);
682 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
684 -- Write level information
686 if Is_Public (Curent) and then not Is_Hidden (Curent) then
687 Write_Info_Char ('*');
689 Write_Info_Char (' ');
692 -- Output entity name. We use the occurrence from the
693 -- actual source program at the definition point
695 P := Original_Location (Sloc (XE.Ent));
697 -- Entity is character literal
699 if Cursrc (P) = ''' then
700 Write_Info_Char (Cursrc (P));
701 Write_Info_Char (Cursrc (P + 1));
702 Write_Info_Char (Cursrc (P + 2));
704 -- Entity is operator symbol
706 elsif Cursrc (P) = '"' or else Cursrc (P) = '%' then
707 Write_Info_Char (Cursrc (P));
712 Write_Info_Char (Cursrc (P2));
713 exit when Cursrc (P2) = Cursrc (P);
716 -- Entity is identifier
720 if Is_Start_Of_Wide_Char (Cursrc, P) then
721 Scan_Wide (Cursrc, P, WC, Err);
722 elsif not Identifier_Char (Cursrc (P)) then
730 Original_Location (Sloc (XE.Ent)) .. P - 1
732 Write_Info_Char (Cursrc (J));
736 -- See if we have a renaming reference
738 if Is_Object (XE.Ent)
739 and then Present (Renamed_Object (XE.Ent))
741 Rref := Renamed_Object (XE.Ent);
743 elsif Is_Overloadable (XE.Ent)
744 and then Nkind (Parent (Declaration_Node (XE.Ent))) =
745 N_Subprogram_Renaming_Declaration
747 Rref := Name (Parent (Declaration_Node (XE.Ent)));
749 elsif Ekind (XE.Ent) = E_Package
750 and then Nkind (Declaration_Node (XE.Ent)) =
751 N_Package_Renaming_Declaration
753 Rref := Name (Declaration_Node (XE.Ent));
759 if Present (Rref) then
760 if Nkind (Rref) = N_Expanded_Name then
761 Rref := Selector_Name (Rref);
764 if Nkind (Rref) /= N_Identifier then
769 -- Write out renaming reference if we have one
771 if Debug_Flag_MM and then Present (Rref) then
772 Write_Info_Char ('=');
774 (Int (Get_Logical_Line_Number (Sloc (Rref))));
775 Write_Info_Char (':');
777 (Int (Get_Column_Number (Sloc (Rref))));
780 -- See if we have a type reference
789 -- Processing for types
791 if Is_Type (Tref) then
795 if Base_Type (Tref) = Tref then
797 -- If derived, then get first subtype
799 if Tref /= Etype (Tref) then
800 Tref := First_Subtype (Etype (Tref));
802 -- Set brackets for derived type, but don't
803 -- override pointer case since the fact that
804 -- something is a pointer is more important
811 -- If non-derived ptr, get designated type
813 elsif Is_Access_Type (Tref) then
814 Tref := Designated_Type (Tref);
818 -- For other non-derived base types, nothing
824 -- For a subtype, go to ancestor subtype
827 Tref := Ancestor_Subtype (Tref);
829 -- If no ancestor subtype, go to base type
832 Tref := Base_Type (Sav);
836 -- For objects, functions, enum literals,
837 -- just get type from Etype field.
839 elsif Is_Object (Tref)
840 or else Ekind (Tref) = E_Enumeration_Literal
841 or else Ekind (Tref) = E_Function
842 or else Ekind (Tref) = E_Operator
844 Tref := Etype (Tref);
846 -- For anything else, exit
852 -- Exit if no type reference, or we are stuck in
853 -- some loop trying to find the type reference.
855 exit when No (Tref) or else Tref = Sav;
857 -- Here we have a type reference to output
859 -- Case of standard entity, output name
861 if Sloc (Tref) = Standard_Location then
863 -- For now, output only if special -gnatdM flag set
865 exit when not Debug_Flag_MM;
867 Write_Info_Char (Left);
868 Write_Info_Name (Chars (Tref));
869 Write_Info_Char (Right);
872 -- Case of source entity, output location
874 elsif Comes_From_Source (Tref) then
876 -- For now, output only derived type entries
877 -- unless we have special debug flag -gnatdM
879 exit when not (Debug_Flag_MM or else Left = '<');
881 -- Do not output type reference if referenced
882 -- entity is not in the main unit and is itself
883 -- not referenced, since otherwise the reference
886 exit when not Referenced (Tref)
887 and then not In_Extended_Main_Source_Unit (Tref);
889 -- Output the reference
891 Write_Info_Char (Left);
892 Trunit := Get_Source_Unit (Sloc (Tref));
894 if Trunit /= Curxu then
895 Write_Info_Nat (Dependency_Num (Trunit));
896 Write_Info_Char ('|');
900 (Int (Get_Logical_Line_Number (Sloc (Tref))));
902 (Xref_Entity_Letters (Ekind (Tref)));
904 (Int (Get_Column_Number (Sloc (Tref))));
905 Write_Info_Char (Right);
908 -- If non-standard, non-source entity, keep looking
915 -- End of processing for entity output
918 Crloc := No_Location;
921 -- Output the reference
923 if XE.Loc /= No_Location
924 and then XE.Loc /= Crloc
928 -- Start continuation if line full, else blank
930 if Write_Info_Col > 72 then
932 Write_Info_Initiate ('.');
935 Write_Info_Char (' ');
937 -- Output file number if changed
939 if XE.Lun /= Curru then
941 Write_Info_Nat (Dependency_Num (Curru));
942 Write_Info_Char ('|');
945 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Loc)));
946 Write_Info_Char (XE.Typ);
947 Write_Info_Nat (Int (Get_Column_Number (XE.Loc)));
949 Output_Instantiation_Refs (Sloc (XE.Ent));
960 end Output_References;