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
452 Trunit : Unit_Number_Type;
453 -- Unit number for type reference
455 function Name_Change (X : Entity_Id) return Boolean;
456 -- Determines if entity X has a different simple name from Curent
462 function Name_Change (X : Entity_Id) return Boolean is
464 Get_Unqualified_Name_String (Chars (X));
466 if Name_Len /= Curlen then
470 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
474 -- Start of processing for Output_Refs
479 Curdef := No_Location;
481 Crloc := No_Location;
483 for Refno in 1 .. Nrefs loop
485 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
486 -- The current entry to be accessed
489 -- Used to index into source buffer to get entity name
499 -- Used for {} or <> for type reference
503 Ctyp := Xref_Entity_Letters (Ekind (Ent));
505 -- Skip reference if it is the only reference to an entity,
506 -- and it is an end-line reference, and the entity is not in
507 -- the current extended source. This prevents junk entries
508 -- consisting only of packages with end lines, where no
509 -- entity from the package is actually referenced.
512 and then Ent /= Curent
513 and then (Refno = Nrefs or else
514 Ent /= Xrefs.Table (Rnums (Refno + 1)).Ent)
516 not In_Extended_Main_Source_Unit (Ent)
521 -- For private type, get full view type
524 and then Present (Full_View (XE.Ent))
526 Ent := Underlying_Type (Ent);
528 if Present (Ent) then
529 Ctyp := Xref_Entity_Letters (Ekind (Ent));
533 -- Special exception for Boolean
535 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
539 -- For variable reference, get corresponding type
542 Ent := Etype (XE.Ent);
543 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
545 -- If variable is private type, get full view type
548 and then Present (Full_View (Etype (XE.Ent)))
550 Ent := Underlying_Type (Etype (XE.Ent));
552 if Present (Ent) then
553 Ctyp := Xref_Entity_Letters (Ekind (Ent));
557 -- Special handling for access parameter
559 if Ekind (Etype (XE.Ent)) = E_Anonymous_Access_Type
560 and then Is_Formal (XE.Ent)
564 -- Special handling for Boolean
566 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
571 -- Only output reference if interesting type of entity,
572 -- and suppress self references. Also suppress definitions
573 -- of body formals (we only treat these as references, and
574 -- the references were separately recorded).
577 and then XE.Loc /= XE.Def
578 and then (not Is_Formal (XE.Ent)
579 or else No (Spec_Entity (XE.Ent)))
581 -- Start new Xref section if new xref unit
583 if XE.Eun /= Curxu then
585 if Write_Info_Col > 1 then
590 Cursrc := Source_Text (Source_Index (Curxu));
592 Write_Info_Initiate ('X');
593 Write_Info_Char (' ');
594 Write_Info_Nat (Dependency_Num (XE.Eun));
595 Write_Info_Char (' ');
596 Write_Info_Name (Reference_Name (Source_Index (XE.Eun)));
599 -- Start new Entity line if new entity. Note that we
600 -- consider two entities the same if they have the same
601 -- name and source location. This causes entities in
602 -- instantiations to be treated as though they referred
609 (Name_Change (XE.Ent) or else XE.Def /= Curdef))
614 Get_Unqualified_Name_String (Chars (XE.Ent));
616 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
618 if Write_Info_Col > 1 then
622 -- Write column number information
624 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
625 Write_Info_Char (Ctyp);
626 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
628 -- Write level information
630 if Is_Public (Curent) and then not Is_Hidden (Curent) then
631 Write_Info_Char ('*');
633 Write_Info_Char (' ');
636 -- Output entity name. We use the occurrence from the
637 -- actual source program at the definition point
639 P := Original_Location (Sloc (XE.Ent));
641 -- Entity is character literal
643 if Cursrc (P) = ''' then
644 Write_Info_Char (Cursrc (P));
645 Write_Info_Char (Cursrc (P + 1));
646 Write_Info_Char (Cursrc (P + 2));
648 -- Entity is operator symbol
650 elsif Cursrc (P) = '"' or else Cursrc (P) = '%' then
651 Write_Info_Char (Cursrc (P));
656 Write_Info_Char (Cursrc (P2));
657 exit when Cursrc (P2) = Cursrc (P);
660 -- Entity is identifier
664 if Is_Start_Of_Wide_Char (Cursrc, P) then
665 Scan_Wide (Cursrc, P, WC, Err);
666 elsif not Identifier_Char (Cursrc (P)) then
674 Original_Location (Sloc (XE.Ent)) .. P - 1
676 Write_Info_Char (Cursrc (J));
680 -- Output type reference if any
689 -- Processing for types
691 if Is_Type (Tref) then
695 if Base_Type (Tref) = Tref then
697 -- If derived, then get first subtype
699 if Tref /= Etype (Tref) then
700 Tref := First_Subtype (Etype (Tref));
702 -- Set brackets for derived type, but don't
703 -- override pointer case since the fact that
704 -- something is a pointer is more important
711 -- If non-derived ptr, get designated type
713 elsif Is_Access_Type (Tref) then
714 Tref := Designated_Type (Tref);
718 -- For other non-derived base types, nothing
724 -- For a subtype, go to ancestor subtype
727 Tref := Ancestor_Subtype (Tref);
729 -- If no ancestor subtype, go to base type
732 Tref := Base_Type (Sav);
736 -- For objects, functions, enum literals,
737 -- just get type from Etype field.
739 elsif Is_Object (Tref)
740 or else Ekind (Tref) = E_Enumeration_Literal
741 or else Ekind (Tref) = E_Function
742 or else Ekind (Tref) = E_Operator
744 Tref := Etype (Tref);
746 -- For anything else, exit
752 -- Exit if no type reference, or we are stuck in
753 -- some loop trying to find the type reference.
755 exit when No (Tref) or else Tref = Sav;
757 -- Case of standard entity, output name
759 if Sloc (Tref) = Standard_Location then
761 -- For now, output only if special -gnatdM flag set
763 exit when not Debug_Flag_MM;
765 Write_Info_Char (Left);
766 Write_Info_Name (Chars (Tref));
767 Write_Info_Char (Right);
770 -- Case of source entity, output location
772 elsif Comes_From_Source (Tref) then
774 -- For now, output only derived type entries
775 -- unless we have special debug flag -gnatdM
777 exit when not (Debug_Flag_MM or else Left = '<');
779 -- Do not output type reference if referenced
780 -- entity is not in the main unit and is itself
781 -- not referenced, since otherwise the reference
784 exit when not Referenced (Tref)
785 and then not In_Extended_Main_Source_Unit (Tref);
787 -- Output the reference
789 Write_Info_Char (Left);
790 Trunit := Get_Source_Unit (Sloc (Tref));
792 if Trunit /= Curxu then
793 Write_Info_Nat (Dependency_Num (Trunit));
794 Write_Info_Char ('|');
798 (Int (Get_Logical_Line_Number (Sloc (Tref))));
800 (Xref_Entity_Letters (Ekind (Tref)));
802 (Int (Get_Column_Number (Sloc (Tref))));
803 Write_Info_Char (Right);
806 -- If non-standard, non-source entity, keep looking
814 Crloc := No_Location;
817 -- Output the reference
819 if XE.Loc /= No_Location
820 and then XE.Loc /= Crloc
824 -- Start continuation if line full, else blank
826 if Write_Info_Col > 72 then
828 Write_Info_Initiate ('.');
831 Write_Info_Char (' ');
833 -- Output file number if changed
835 if XE.Lun /= Curru then
837 Write_Info_Nat (Dependency_Num (Curru));
838 Write_Info_Char ('|');
841 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Loc)));
842 Write_Info_Char (XE.Typ);
843 Write_Info_Nat (Int (Get_Column_Number (XE.Loc)));
854 end Output_References;