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 Lib
.Util
; use Lib
.Util
;
32 with Namet
; use Namet
;
34 with Sinfo
; use Sinfo
;
35 with Sinput
; use Sinput
;
36 with Table
; use Table
;
37 with Widechar
; use Widechar
;
39 with GNAT
.Heap_Sort_A
;
41 package body Lib
.Xref
is
47 -- The Xref table is used to record references. The Loc field is set
48 -- to No_Location for a definition entry.
50 subtype Xref_Entry_Number
is Int
;
52 type Xref_Entry
is record
54 -- Entity referenced (E parameter to Generate_Reference)
57 -- Original source location for entity being referenced. Note that
58 -- these values are used only during the output process, they are
59 -- not set when the entries are originally built. This is because
60 -- private entities can be swapped when the initial call is made.
63 -- Location of reference (Original_Location (Sloc field of N parameter
64 -- to Generate_Reference). Set to No_Location for the case of a
65 -- defining occurrence.
68 -- Reference type (Typ param to Generate_Reference)
70 Eun
: Unit_Number_Type
;
71 -- Unit number corresponding to Ent
73 Lun
: Unit_Number_Type
;
74 -- Unit number corresponding to Loc. Value is undefined and not
75 -- referenced if Loc is set to No_Location.
79 package Xrefs
is new Table
.Table
(
80 Table_Component_Type
=> Xref_Entry
,
81 Table_Index_Type
=> Int
,
83 Table_Initial
=> Alloc
.Xrefs_Initial
,
84 Table_Increment
=> Alloc
.Xrefs_Increment
,
85 Table_Name
=> "Xrefs");
87 function Get_Xref_Index
(E
: Entity_Id
) return Xref_Entry_Number
;
88 -- Returns the Xref entry table index for entity E.
89 -- So : Xrefs.Table (Get_Xref_Index (E)).Ent = E
91 -------------------------
92 -- Generate_Definition --
93 -------------------------
95 procedure Generate_Definition
(E
: Entity_Id
) is
100 pragma Assert
(Nkind
(E
) in N_Entity
);
102 -- Note that we do not test Xref_Entity_Letters here. It is too
103 -- early to do so, since we are often called before the entity
104 -- is fully constructed, so that the Ekind is still E_Void.
108 -- Definition must come from source
110 and then Comes_From_Source
(E
)
112 -- And must have a reasonable source location that is not
113 -- within an instance (all entities in instances are ignored)
115 and then Sloc
(E
) > No_Location
116 and then Instantiation_Location
(Sloc
(E
)) = No_Location
118 -- And must be a non-internal name from the main source unit
120 and then In_Extended_Main_Source_Unit
(E
)
121 and then not Is_Internal_Name
(Chars
(E
))
123 Xrefs
.Increment_Last
;
125 Loc
:= Original_Location
(Sloc
(E
));
127 Xrefs
.Table
(Indx
).Ent
:= E
;
128 Xrefs
.Table
(Indx
).Loc
:= No_Location
;
129 Xrefs
.Table
(Indx
).Eun
:= Get_Source_Unit
(Loc
);
130 Xrefs
.Table
(Indx
).Lun
:= No_Unit
;
132 end Generate_Definition
;
134 ---------------------------------
135 -- Generate_Operator_Reference --
136 ---------------------------------
138 procedure Generate_Operator_Reference
(N
: Node_Id
) is
140 if not In_Extended_Main_Source_Unit
(N
) then
144 -- If the operator is not a Standard operator, then we generate
145 -- a real reference to the user defined operator.
147 if Sloc
(Entity
(N
)) /= Standard_Location
then
148 Generate_Reference
(Entity
(N
), N
);
150 -- A reference to an implicit inequality operator is a also a
151 -- reference to the user-defined equality.
153 if Nkind
(N
) = N_Op_Ne
154 and then not Comes_From_Source
(Entity
(N
))
155 and then Present
(Corresponding_Equality
(Entity
(N
)))
157 Generate_Reference
(Corresponding_Equality
(Entity
(N
)), N
);
160 -- For the case of Standard operators, we mark the result type
161 -- as referenced. This ensures that in the case where we are
162 -- using a derived operator, we mark an entity of the unit that
163 -- implicitly defines this operator as used. Otherwise we may
164 -- think that no entity of the unit is used. The actual entity
165 -- marked as referenced is the first subtype, which is the user
166 -- defined entity that is relevant.
169 if Nkind
(N
) = N_Op_Eq
170 or else Nkind
(N
) = N_Op_Ne
171 or else Nkind
(N
) = N_Op_Le
172 or else Nkind
(N
) = N_Op_Lt
173 or else Nkind
(N
) = N_Op_Ge
174 or else Nkind
(N
) = N_Op_Gt
176 Set_Referenced
(First_Subtype
(Etype
(Right_Opnd
(N
))));
178 Set_Referenced
(First_Subtype
(Etype
(N
)));
181 end Generate_Operator_Reference
;
183 ------------------------
184 -- Generate_Reference --
185 ------------------------
187 procedure Generate_Reference
190 Typ
: Character := 'r';
191 Set_Ref
: Boolean := True;
192 Force
: Boolean := False)
201 pragma Assert
(Nkind
(E
) in N_Entity
);
203 -- Never collect references if not in main source unit. However,
204 -- we omit this test if Typ is 'e', since these entries are
205 -- really structural, and it is useful to have them in units
206 -- that reference packages as well as units that define packages.
208 if not In_Extended_Main_Source_Unit
(N
)
214 -- Unless the reference is forced, we ignore references where
215 -- the reference itself does not come from Source.
217 if not Force
and then not Comes_From_Source
(N
) then
221 -- Deal with setting entity as referenced, unless suppressed.
222 -- Note that we still do Set_Referenced on entities that do not
223 -- come from source. This situation arises when we have a source
224 -- reference to a derived operation, where the derived operation
225 -- itself does not come from source, but we still want to mark it
226 -- as referenced, since we really are referencing an entity in the
227 -- corresponding package (this avoids incorrect complaints that the
228 -- package contains no referenced entities).
233 -- If this is a subprogram instance, mark as well the internal
234 -- subprogram in the wrapper package, which may be a visible
237 if Is_Overloadable
(E
)
238 and then Is_Generic_Instance
(E
)
239 and then Present
(Alias
(E
))
241 Set_Referenced
(Alias
(E
));
245 -- Generate reference if all conditions are met:
248 -- Cross referencing must be active
252 -- The entity must be one for which we collect references
254 and then Xref_Entity_Letters
(Ekind
(E
)) /= ' '
256 -- Both Sloc values must be set to something sensible
258 and then Sloc
(E
) > No_Location
259 and then Sloc
(N
) > No_Location
261 -- We ignore references from within an instance
263 and then Instantiation_Location
(Sloc
(N
)) = No_Location
265 -- Ignore dummy references
269 if Nkind
(N
) = N_Identifier
271 Nkind
(N
) = N_Defining_Identifier
275 Nkind
(N
) = N_Defining_Operator_Symbol
277 (Nkind
(N
) = N_Character_Literal
278 and then Sloc
(Entity
(N
)) /= Standard_Location
)
280 Nkind
(N
) = N_Defining_Character_Literal
284 elsif Nkind
(N
) = N_Expanded_Name
286 Nkind
(N
) = N_Selected_Component
288 Nod
:= Selector_Name
(N
);
294 -- Normal case of source entity comes from source
296 if Comes_From_Source
(E
) then
299 -- Entity does not come from source, but is a derived subprogram
300 -- and the derived subprogram comes from source, in which case
301 -- the reference is to this parent subprogram.
303 elsif Is_Overloadable
(E
)
304 and then Present
(Alias
(E
))
305 and then Comes_From_Source
(Alias
(E
))
309 -- Ignore reference to any other source that is not from source
315 -- Record reference to entity
317 Ref
:= Original_Location
(Sloc
(Nod
));
318 Def
:= Original_Location
(Sloc
(Ent
));
320 Xrefs
.Increment_Last
;
323 Xrefs
.Table
(Indx
).Loc
:= Ref
;
324 Xrefs
.Table
(Indx
).Typ
:= Typ
;
325 Xrefs
.Table
(Indx
).Eun
:= Get_Source_Unit
(Def
);
326 Xrefs
.Table
(Indx
).Lun
:= Get_Source_Unit
(Ref
);
327 Xrefs
.Table
(Indx
).Ent
:= Ent
;
329 end Generate_Reference
;
335 function Get_Xref_Index
(E
: Entity_Id
) return Xref_Entry_Number
is
337 for K
in 1 .. Xrefs
.Last
loop
338 if Xrefs
.Table
(K
).Ent
= E
then
343 -- not found, this happend if the entity is not in the compiled unit.
348 -----------------------
349 -- Output_References --
350 -----------------------
352 procedure Output_References
is
353 Nrefs
: constant Nat
:= Xrefs
.Last
;
355 Rnums
: array (0 .. Nrefs
) of Nat
;
356 -- This array contains numbers of references in the Xrefs table. This
357 -- list is sorted in output order. The extra 0'th entry is convenient
358 -- for the call to sort. When we sort the table, we move these entries
359 -- around, but we do not move the original table entries.
361 function Lt
(Op1
, Op2
: Natural) return Boolean;
362 -- Comparison function for Sort call
364 procedure Move
(From
: Natural; To
: Natural);
365 -- Move procedure for Sort call
367 function Lt
(Op1
, Op2
: Natural) return Boolean is
368 T1
: Xref_Entry
renames Xrefs
.Table
(Rnums
(Nat
(Op1
)));
369 T2
: Xref_Entry
renames Xrefs
.Table
(Rnums
(Nat
(Op2
)));
372 -- First test. If entity is in different unit, sort by unit
374 if T1
.Eun
/= T2
.Eun
then
375 return Dependency_Num
(T1
.Eun
) < Dependency_Num
(T2
.Eun
);
377 -- Second test, within same unit, sort by entity Sloc
379 elsif T1
.Def
/= T2
.Def
then
380 return T1
.Def
< T2
.Def
;
382 -- Third test, sort definitions ahead of references
384 elsif T1
.Loc
= No_Location
then
387 elsif T2
.Loc
= No_Location
then
390 -- Fourth test, for same entity, sort by reference location unit
392 elsif T1
.Lun
/= T2
.Lun
then
393 return Dependency_Num
(T1
.Lun
) < Dependency_Num
(T2
.Lun
);
395 -- Fifth test order of location within referencing unit
397 elsif T1
.Loc
/= T2
.Loc
then
398 return T1
.Loc
< T2
.Loc
;
400 -- Finally, for two locations at the same address, we prefer
401 -- the one that does NOT have the type 'r' so that a modification
402 -- or extension takes preference, when there are more than one
403 -- reference at the same location.
410 procedure Move
(From
: Natural; To
: Natural) is
412 Rnums
(Nat
(To
)) := Rnums
(Nat
(From
));
415 -- Start of processing for Output_References
418 if not Opt
.Xref_Active
then
422 -- Capture the definition Sloc values. We delay doing this till now,
423 -- since at the time the reference or definition is made, private
424 -- types may be swapped, and the Sloc value may be incorrect. We
425 -- also set up the pointer vector for the sort.
427 for J
in 1 .. Nrefs
loop
429 Xrefs
.Table
(J
).Def
:=
430 Original_Location
(Sloc
(Xrefs
.Table
(J
).Ent
));
433 -- Sort the references
435 GNAT
.Heap_Sort_A
.Sort
437 Move
'Unrestricted_Access,
438 Lt
'Unrestricted_Access);
440 -- Now output the references
442 Output_Refs
: declare
444 Curxu
: Unit_Number_Type
;
447 Curru
: Unit_Number_Type
;
448 -- Current reference unit for one entity
450 Cursrc
: Source_Buffer_Ptr
;
451 -- Current xref unit source text
456 Curnam
: String (1 .. Name_Buffer
'Length);
458 -- Simple name and length of current entity
461 -- Original source location for current entity
464 -- Current reference location
467 -- Entity type character
470 -- entry for parent of derived type.
472 function Name_Change
(X
: Entity_Id
) return Boolean;
473 -- Determines if entity X has a different simple name from Curent
475 function Get_Parent_Entry
(X
: Entity_Id
) return Int
;
476 -- For a derived type, locate entry of parent type, if defined in
477 -- in the current unit.
479 function Get_Parent_Entry
(X
: Entity_Id
) return Int
is
480 Parent_Type
: Entity_Id
;
484 or else not Is_Derived_Type
(X
)
488 Parent_Type
:= First_Subtype
(Etype
(Base_Type
(X
)));
490 if Comes_From_Source
(Parent_Type
) then
491 return Get_Xref_Index
(Parent_Type
);
497 end Get_Parent_Entry
;
499 function Name_Change
(X
: Entity_Id
) return Boolean is
501 Get_Unqualified_Name_String
(Chars
(X
));
503 if Name_Len
/= Curlen
then
507 return Name_Buffer
(1 .. Curlen
) /= Curnam
(1 .. Curlen
);
511 -- Start of processing for Output_Refs
516 Curdef
:= No_Location
;
518 Crloc
:= No_Location
;
520 for Refno
in 1 .. Nrefs
loop
522 XE
: Xref_Entry
renames Xrefs
.Table
(Rnums
(Refno
));
523 -- The current entry to be accessed
526 -- Used to index into source buffer to get entity name
535 Ctyp
:= Xref_Entity_Letters
(Ekind
(Ent
));
537 -- Skip reference if it is the only reference to an entity,
538 -- and it is an end-line reference, and the entity is not in
539 -- the current extended source. This prevents junk entries
540 -- consisting only of packages with end lines, where no
541 -- entity from the package is actually referenced.
544 and then Ent
/= Curent
545 and then (Refno
= Nrefs
or else
546 Ent
/= Xrefs
.Table
(Rnums
(Refno
+ 1)).Ent
)
548 not In_Extended_Main_Source_Unit
(Ent
)
553 -- For private type, get full view type
556 and then Present
(Full_View
(XE
.Ent
))
558 Ent
:= Underlying_Type
(Ent
);
560 if Present
(Ent
) then
561 Ctyp
:= Xref_Entity_Letters
(Ekind
(Ent
));
565 -- Special exception for Boolean
567 if Ctyp
= 'E' and then Is_Boolean_Type
(Ent
) then
571 -- For variable reference, get corresponding type
574 Ent
:= Etype
(XE
.Ent
);
575 Ctyp
:= Fold_Lower
(Xref_Entity_Letters
(Ekind
(Ent
)));
577 -- If variable is private type, get full view type
580 and then Present
(Full_View
(Etype
(XE
.Ent
)))
582 Ent
:= Underlying_Type
(Etype
(XE
.Ent
));
584 if Present
(Ent
) then
585 Ctyp
:= Xref_Entity_Letters
(Ekind
(Ent
));
589 -- Special handling for access parameter
591 if Ekind
(Etype
(XE
.Ent
)) = E_Anonymous_Access_Type
592 and then Is_Formal
(XE
.Ent
)
596 -- Special handling for Boolean
598 elsif Ctyp
= 'e' and then Is_Boolean_Type
(Ent
) then
603 -- Only output reference if interesting type of entity,
604 -- and suppress self references. Also suppress definitions
605 -- of body formals (we only treat these as references, and
606 -- the references were separately recorded).
609 and then XE
.Loc
/= XE
.Def
610 and then (not Is_Formal
(XE
.Ent
)
611 or else No
(Spec_Entity
(XE
.Ent
)))
613 -- Start new Xref section if new xref unit
615 if XE
.Eun
/= Curxu
then
617 if Write_Info_Col
> 1 then
622 Cursrc
:= Source_Text
(Source_Index
(Curxu
));
624 Write_Info_Initiate
('X');
625 Write_Info_Char
(' ');
626 Write_Info_Nat
(Dependency_Num
(XE
.Eun
));
627 Write_Info_Char
(' ');
628 Write_Info_Name
(Reference_Name
(Source_Index
(XE
.Eun
)));
631 -- Start new Entity line if new entity. Note that we
632 -- consider two entities the same if they have the same
633 -- name and source location. This causes entities in
634 -- instantiations to be treated as though they referred
641 (Name_Change
(XE
.Ent
) or else XE
.Def
/= Curdef
))
646 Get_Unqualified_Name_String
(Chars
(XE
.Ent
));
648 Curnam
(1 .. Curlen
) := Name_Buffer
(1 .. Curlen
);
650 if Write_Info_Col
> 1 then
654 -- Write column number information
656 Write_Info_Nat
(Int
(Get_Logical_Line_Number
(XE
.Def
)));
657 Write_Info_Char
(Ctyp
);
658 Write_Info_Nat
(Int
(Get_Column_Number
(XE
.Def
)));
660 -- Write level information
662 if Is_Public
(Curent
) and then not Is_Hidden
(Curent
) then
663 Write_Info_Char
('*');
665 Write_Info_Char
(' ');
668 -- Output entity name. We use the occurrence from the
669 -- actual source program at the definition point
671 P
:= Original_Location
(Sloc
(XE
.Ent
));
673 -- Entity is character literal
675 if Cursrc
(P
) = ''' then
676 Write_Info_Char
(Cursrc
(P
));
677 Write_Info_Char
(Cursrc
(P
+ 1));
678 Write_Info_Char
(Cursrc
(P
+ 2));
680 -- Entity is operator symbol
682 elsif Cursrc
(P
) = '"' or else Cursrc
(P
) = '%' then
683 Write_Info_Char
(Cursrc
(P
));
688 Write_Info_Char
(Cursrc
(P2
));
689 exit when Cursrc
(P2
) = Cursrc
(P
);
692 -- Entity is identifier
696 if Is_Start_Of_Wide_Char
(Cursrc
, P
) then
697 Scan_Wide
(Cursrc
, P
, WC
, Err
);
698 elsif not Identifier_Char
(Cursrc
(P
)) then
706 Original_Location
(Sloc
(XE
.Ent
)) .. P
- 1
708 Write_Info_Char
(Cursrc
(J
));
712 -- Output derived entity name if it is available
714 Parent_Entry
:= Get_Parent_Entry
(XE
.Ent
);
716 if Parent_Entry
/= 0 then
718 XD
: Xref_Entry
renames Xrefs
.Table
(Parent_Entry
);
721 Write_Info_Char
('<');
723 -- Write unit number only if different from the
726 if XE
.Eun
/= XD
.Eun
then
727 Write_Info_Nat
(Dependency_Num
(XD
.Eun
));
728 Write_Info_Char
('|');
732 (Int
(Get_Logical_Line_Number
(XD
.Def
)));
734 (Xref_Entity_Letters
(Ekind
(XD
.Ent
)));
735 Write_Info_Nat
(Int
(Get_Column_Number
(XD
.Def
)));
737 Write_Info_Char
('>');
742 Crloc
:= No_Location
;
745 -- Output the reference
747 if XE
.Loc
/= No_Location
748 and then XE
.Loc
/= Crloc
752 -- Start continuation if line full, else blank
754 if Write_Info_Col
> 72 then
756 Write_Info_Initiate
('.');
759 Write_Info_Char
(' ');
761 -- Output file number if changed
763 if XE
.Lun
/= Curru
then
765 Write_Info_Nat
(Dependency_Num
(Curru
));
766 Write_Info_Char
('|');
769 Write_Info_Nat
(Int
(Get_Logical_Line_Number
(XE
.Loc
)));
770 Write_Info_Char
(XE
.Typ
);
771 Write_Info_Nat
(Int
(Get_Column_Number
(XE
.Loc
)));
782 end Output_References
;