PR testsuite/44195
[official-gcc.git] / gcc / ada / lib-xref.adb
blob5283023a8561b57e8472bae31cfb825faa6e4240
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- L I B . X R E F --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2010, Free Software Foundation, Inc. --
10 -- --
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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Csets; use Csets;
28 with Elists; use Elists;
29 with Errout; use Errout;
30 with Lib.Util; use Lib.Util;
31 with Nlists; use Nlists;
32 with Opt; use Opt;
33 with Restrict; use Restrict;
34 with Rident; use Rident;
35 with Sem; use Sem;
36 with Sem_Aux; use Sem_Aux;
37 with Sem_Prag; use Sem_Prag;
38 with Sem_Util; use Sem_Util;
39 with Sem_Warn; use Sem_Warn;
40 with Sinfo; use Sinfo;
41 with Sinput; use Sinput;
42 with Snames; use Snames;
43 with Stringt; use Stringt;
44 with Stand; use Stand;
45 with Table; use Table;
46 with Widechar; use Widechar;
48 with GNAT.Heap_Sort_G;
50 package body Lib.Xref is
52 ------------------
53 -- Declarations --
54 ------------------
56 -- The Xref table is used to record references. The Loc field is set
57 -- to No_Location for a definition entry.
59 subtype Xref_Entry_Number is Int;
61 type Xref_Entry is record
62 Ent : Entity_Id;
63 -- Entity referenced (E parameter to Generate_Reference)
65 Def : Source_Ptr;
66 -- Original source location for entity being referenced. Note that these
67 -- values are used only during the output process, they are not set when
68 -- the entries are originally built. This is because private entities
69 -- can be swapped when the initial call is made.
71 Loc : Source_Ptr;
72 -- Location of reference (Original_Location (Sloc field of N parameter
73 -- to Generate_Reference). Set to No_Location for the case of a
74 -- defining occurrence.
76 Typ : Character;
77 -- Reference type (Typ param to Generate_Reference)
79 Eun : Unit_Number_Type;
80 -- Unit number corresponding to Ent
82 Lun : Unit_Number_Type;
83 -- Unit number corresponding to Loc. Value is undefined and not
84 -- referenced if Loc is set to No_Location.
86 end record;
88 package Xrefs is new Table.Table (
89 Table_Component_Type => Xref_Entry,
90 Table_Index_Type => Xref_Entry_Number,
91 Table_Low_Bound => 1,
92 Table_Initial => Alloc.Xrefs_Initial,
93 Table_Increment => Alloc.Xrefs_Increment,
94 Table_Name => "Xrefs");
96 ------------------------
97 -- Local Subprograms --
98 ------------------------
100 procedure Generate_Prim_Op_References (Typ : Entity_Id);
101 -- For a tagged type, generate implicit references to its primitive
102 -- operations, for source navigation. This is done right before emitting
103 -- cross-reference information rather than at the freeze point of the type
104 -- in order to handle late bodies that are primitive operations.
106 -------------------------
107 -- Generate_Definition --
108 -------------------------
110 procedure Generate_Definition (E : Entity_Id) is
111 Loc : Source_Ptr;
112 Indx : Nat;
114 begin
115 pragma Assert (Nkind (E) in N_Entity);
117 -- Note that we do not test Xref_Entity_Letters here. It is too early
118 -- to do so, since we are often called before the entity is fully
119 -- constructed, so that the Ekind is still E_Void.
121 if Opt.Xref_Active
123 -- Definition must come from source
125 -- We make an exception for subprogram child units that have no spec.
126 -- For these we generate a subprogram declaration for library use,
127 -- and the corresponding entity does not come from source.
128 -- Nevertheless, all references will be attached to it and we have
129 -- to treat is as coming from user code.
131 and then (Comes_From_Source (E) or else Is_Child_Unit (E))
133 -- And must have a reasonable source location that is not
134 -- within an instance (all entities in instances are ignored)
136 and then Sloc (E) > No_Location
137 and then Instantiation_Location (Sloc (E)) = No_Location
139 -- And must be a non-internal name from the main source unit
141 and then In_Extended_Main_Source_Unit (E)
142 and then not Is_Internal_Name (Chars (E))
143 then
144 Xrefs.Increment_Last;
145 Indx := Xrefs.Last;
146 Loc := Original_Location (Sloc (E));
148 Xrefs.Table (Indx).Ent := E;
149 Xrefs.Table (Indx).Def := No_Location;
150 Xrefs.Table (Indx).Loc := No_Location;
151 Xrefs.Table (Indx).Typ := ' ';
152 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
153 Xrefs.Table (Indx).Lun := No_Unit;
154 Set_Has_Xref_Entry (E);
156 if In_Inlined_Body then
157 Set_Referenced (E);
158 end if;
159 end if;
160 end Generate_Definition;
162 ---------------------------------
163 -- Generate_Operator_Reference --
164 ---------------------------------
166 procedure Generate_Operator_Reference
167 (N : Node_Id;
168 T : Entity_Id)
170 begin
171 if not In_Extended_Main_Source_Unit (N) then
172 return;
173 end if;
175 -- If the operator is not a Standard operator, then we generate a real
176 -- reference to the user defined operator.
178 if Sloc (Entity (N)) /= Standard_Location then
179 Generate_Reference (Entity (N), N);
181 -- A reference to an implicit inequality operator is also a reference
182 -- to the user-defined equality.
184 if Nkind (N) = N_Op_Ne
185 and then not Comes_From_Source (Entity (N))
186 and then Present (Corresponding_Equality (Entity (N)))
187 then
188 Generate_Reference (Corresponding_Equality (Entity (N)), N);
189 end if;
191 -- For the case of Standard operators, we mark the result type as
192 -- referenced. This ensures that in the case where we are using a
193 -- derived operator, we mark an entity of the unit that implicitly
194 -- defines this operator as used. Otherwise we may think that no entity
195 -- of the unit is used. The actual entity marked as referenced is the
196 -- first subtype, which is the relevant user defined entity.
198 -- Note: we only do this for operators that come from source. The
199 -- generated code sometimes reaches for entities that do not need to be
200 -- explicitly visible (for example, when we expand the code for
201 -- comparing two record objects, the fields of the record may not be
202 -- visible).
204 elsif Comes_From_Source (N) then
205 Set_Referenced (First_Subtype (T));
206 end if;
207 end Generate_Operator_Reference;
209 ---------------------------------
210 -- Generate_Prim_Op_References --
211 ---------------------------------
213 procedure Generate_Prim_Op_References (Typ : Entity_Id) is
214 Base_T : Entity_Id;
215 Prim : Elmt_Id;
216 Prim_List : Elist_Id;
218 begin
219 -- Handle subtypes of synchronized types
221 if Ekind (Typ) = E_Protected_Subtype
222 or else Ekind (Typ) = E_Task_Subtype
223 then
224 Base_T := Etype (Typ);
225 else
226 Base_T := Typ;
227 end if;
229 -- References to primitive operations are only relevant for tagged types
231 if not Is_Tagged_Type (Base_T)
232 or else Is_Class_Wide_Type (Base_T)
233 then
234 return;
235 end if;
237 -- Ada 2005 (AI-345): For synchronized types generate reference
238 -- to the wrapper that allow us to dispatch calls through their
239 -- implemented abstract interface types.
241 -- The check for Present here is to protect against previously
242 -- reported critical errors.
244 if Is_Concurrent_Type (Base_T)
245 and then Present (Corresponding_Record_Type (Base_T))
246 then
247 Prim_List := Primitive_Operations
248 (Corresponding_Record_Type (Base_T));
249 else
250 Prim_List := Primitive_Operations (Base_T);
251 end if;
253 if No (Prim_List) then
254 return;
255 end if;
257 Prim := First_Elmt (Prim_List);
258 while Present (Prim) loop
260 -- If the operation is derived, get the original for cross-reference
261 -- reference purposes (it is the original for which we want the xref
262 -- and for which the comes_from_source test must be performed).
264 Generate_Reference
265 (Typ, Ultimate_Alias (Node (Prim)), 'p', Set_Ref => False);
266 Next_Elmt (Prim);
267 end loop;
268 end Generate_Prim_Op_References;
270 ------------------------
271 -- Generate_Reference --
272 ------------------------
274 procedure Generate_Reference
275 (E : Entity_Id;
276 N : Node_Id;
277 Typ : Character := 'r';
278 Set_Ref : Boolean := True;
279 Force : Boolean := False)
281 Indx : Nat;
282 Nod : Node_Id;
283 Ref : Source_Ptr;
284 Def : Source_Ptr;
285 Ent : Entity_Id;
287 Call : Node_Id;
288 Formal : Entity_Id;
289 -- Used for call to Find_Actual
291 Kind : Entity_Kind;
292 -- If Formal is non-Empty, then its Ekind, otherwise E_Void
294 function Is_On_LHS (Node : Node_Id) return Boolean;
295 -- Used to check if a node is on the left hand side of an assignment.
296 -- The following cases are handled:
298 -- Variable Node is a direct descendant of left hand side of an
299 -- assignment statement.
301 -- Prefix Of an indexed or selected component that is present in
302 -- a subtree rooted by an assignment statement. There is
303 -- no restriction of nesting of components, thus cases
304 -- such as A.B (C).D are handled properly. However a prefix
305 -- of a dereference (either implicit or explicit) is never
306 -- considered as on a LHS.
308 -- Out param Same as above cases, but OUT parameter
310 function OK_To_Set_Referenced return Boolean;
311 -- Returns True if the Referenced flag can be set. There are a few
312 -- exceptions where we do not want to set this flag, see body for
313 -- details of these exceptional cases.
315 ---------------
316 -- Is_On_LHS --
317 ---------------
319 -- ??? There are several routines here and there that perform a similar
320 -- (but subtly different) computation, which should be factored:
322 -- Sem_Util.May_Be_Lvalue
323 -- Sem_Util.Known_To_Be_Assigned
324 -- Exp_Ch2.Expand_Entry_Parameter.In_Assignment_Context
325 -- Exp_Smem.Is_Out_Actual
327 function Is_On_LHS (Node : Node_Id) return Boolean is
328 N : Node_Id;
329 P : Node_Id;
330 K : Node_Kind;
332 begin
333 -- Only identifiers are considered, is this necessary???
335 if Nkind (Node) /= N_Identifier then
336 return False;
337 end if;
339 -- Immediate return if appeared as OUT parameter
341 if Kind = E_Out_Parameter then
342 return True;
343 end if;
345 -- Search for assignment statement subtree root
347 N := Node;
348 loop
349 P := Parent (N);
350 K := Nkind (P);
352 if K = N_Assignment_Statement then
353 return Name (P) = N;
355 -- Check whether the parent is a component and the current node is
356 -- its prefix, but return False if the current node has an access
357 -- type, as in that case the selected or indexed component is an
358 -- implicit dereference, and the LHS is the designated object, not
359 -- the access object.
361 -- ??? case of a slice assignment?
363 -- ??? Note that in some cases this is called too early
364 -- (see comments in Sem_Ch8.Find_Direct_Name), at a point where
365 -- the tree is not fully typed yet. In that case we may lack
366 -- an Etype for N, and we must disable the check for an implicit
367 -- dereference. If the dereference is on an LHS, this causes a
368 -- false positive.
370 elsif (K = N_Selected_Component or else K = N_Indexed_Component)
371 and then Prefix (P) = N
372 and then not (Present (Etype (N))
373 and then
374 Is_Access_Type (Etype (N)))
375 then
376 N := P;
378 -- All other cases, definitely not on left side
380 else
381 return False;
382 end if;
383 end loop;
384 end Is_On_LHS;
386 ---------------------------
387 -- OK_To_Set_Referenced --
388 ---------------------------
390 function OK_To_Set_Referenced return Boolean is
391 P : Node_Id;
393 begin
394 -- A reference from a pragma Unreferenced or pragma Unmodified or
395 -- pragma Warnings does not cause the Referenced flag to be set.
396 -- This avoids silly warnings about things being referenced and
397 -- not assigned when the only reference is from the pragma.
399 if Nkind (N) = N_Identifier then
400 P := Parent (N);
402 if Nkind (P) = N_Pragma_Argument_Association then
403 P := Parent (P);
405 if Nkind (P) = N_Pragma then
406 if Pragma_Name (P) = Name_Warnings
407 or else
408 Pragma_Name (P) = Name_Unmodified
409 or else
410 Pragma_Name (P) = Name_Unreferenced
411 then
412 return False;
413 end if;
414 end if;
415 end if;
416 end if;
418 return True;
419 end OK_To_Set_Referenced;
421 -- Start of processing for Generate_Reference
423 begin
424 pragma Assert (Nkind (E) in N_Entity);
425 Find_Actual (N, Formal, Call);
427 if Present (Formal) then
428 Kind := Ekind (Formal);
429 else
430 Kind := E_Void;
431 end if;
433 -- Check for obsolescent reference to package ASCII. GNAT treats this
434 -- element of annex J specially since in practice, programs make a lot
435 -- of use of this feature, so we don't include it in the set of features
436 -- diagnosed when Warn_On_Obsolescent_Features mode is set. However we
437 -- are required to note it as a violation of the RM defined restriction.
439 if E = Standard_ASCII then
440 Check_Restriction (No_Obsolescent_Features, N);
441 end if;
443 -- Check for reference to entity marked with Is_Obsolescent
445 -- Note that we always allow obsolescent references in the compiler
446 -- itself and the run time, since we assume that we know what we are
447 -- doing in such cases. For example the calls in Ada.Characters.Handling
448 -- to its own obsolescent subprograms are just fine.
450 -- In any case we do not generate warnings within the extended source
451 -- unit of the entity in question, since we assume the source unit
452 -- itself knows what is going on (and for sure we do not want silly
453 -- warnings, e.g. on the end line of an obsolescent procedure body).
455 if Is_Obsolescent (E)
456 and then not GNAT_Mode
457 and then not In_Extended_Main_Source_Unit (E)
458 then
459 Check_Restriction (No_Obsolescent_Features, N);
461 if Warn_On_Obsolescent_Feature then
462 Output_Obsolescent_Entity_Warnings (N, E);
463 end if;
464 end if;
466 -- Warn if reference to Ada 2005 entity not in Ada 2005 mode. We only
467 -- detect real explicit references (modifications and references).
469 if Comes_From_Source (N)
470 and then Is_Ada_2005_Only (E)
471 and then Ada_Version < Ada_05
472 and then Warn_On_Ada_2005_Compatibility
473 and then (Typ = 'm' or else Typ = 'r')
474 then
475 Error_Msg_NE ("& is only defined in Ada 2005?", N, E);
476 end if;
478 -- Never collect references if not in main source unit. However, we omit
479 -- this test if Typ is 'e' or 'k', since these entries are structural,
480 -- and it is useful to have them in units that reference packages as
481 -- well as units that define packages. We also omit the test for the
482 -- case of 'p' since we want to include inherited primitive operations
483 -- from other packages.
485 -- We also omit this test is this is a body reference for a subprogram
486 -- instantiation. In this case the reference is to the generic body,
487 -- which clearly need not be in the main unit containing the instance.
488 -- For the same reason we accept an implicit reference generated for
489 -- a default in an instance.
491 if not In_Extended_Main_Source_Unit (N) then
492 if Typ = 'e'
493 or else Typ = 'p'
494 or else Typ = 'i'
495 or else Typ = 'k'
496 or else (Typ = 'b' and then Is_Generic_Instance (E))
497 then
498 null;
499 else
500 return;
501 end if;
502 end if;
504 -- For reference type p, the entity must be in main source unit
506 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
507 return;
508 end if;
510 -- Unless the reference is forced, we ignore references where the
511 -- reference itself does not come from source.
513 if not Force and then not Comes_From_Source (N) then
514 return;
515 end if;
517 -- Deal with setting entity as referenced, unless suppressed. Note that
518 -- we still do Set_Referenced on entities that do not come from source.
519 -- This situation arises when we have a source reference to a derived
520 -- operation, where the derived operation itself does not come from
521 -- source, but we still want to mark it as referenced, since we really
522 -- are referencing an entity in the corresponding package (this avoids
523 -- wrong complaints that the package contains no referenced entities).
525 if Set_Ref then
527 -- Assignable object appearing on left side of assignment or as
528 -- an out parameter.
530 if Is_Assignable (E)
531 and then Is_On_LHS (N)
532 and then Ekind (E) /= E_In_Out_Parameter
533 then
534 -- For objects that are renamings, just set as simply referenced
535 -- we do not try to do assignment type tracking in this case.
537 if Present (Renamed_Object (E)) then
538 Set_Referenced (E);
540 -- Out parameter case
542 elsif Kind = E_Out_Parameter then
544 -- If warning mode for all out parameters is set, or this is
545 -- the only warning parameter, then we want to mark this for
546 -- later warning logic by setting Referenced_As_Out_Parameter
548 if Warn_On_Modified_As_Out_Parameter (Formal) then
549 Set_Referenced_As_Out_Parameter (E, True);
550 Set_Referenced_As_LHS (E, False);
552 -- For OUT parameter not covered by the above cases, we simply
553 -- regard it as a normal reference (in this case we do not
554 -- want any of the warning machinery for out parameters).
556 else
557 Set_Referenced (E);
558 end if;
560 -- For the left hand of an assignment case, we do nothing here.
561 -- The processing for Analyze_Assignment_Statement will set the
562 -- Referenced_As_LHS flag.
564 else
565 null;
566 end if;
568 -- Check for a reference in a pragma that should not count as a
569 -- making the variable referenced for warning purposes.
571 elsif Is_Non_Significant_Pragma_Reference (N) then
572 null;
574 -- A reference in an attribute definition clause does not count as a
575 -- reference except for the case of Address. The reason that 'Address
576 -- is an exception is that it creates an alias through which the
577 -- variable may be referenced.
579 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
580 and then Chars (Parent (N)) /= Name_Address
581 and then N = Name (Parent (N))
582 then
583 null;
585 -- Constant completion does not count as a reference
587 elsif Typ = 'c'
588 and then Ekind (E) = E_Constant
589 then
590 null;
592 -- Record representation clause does not count as a reference
594 elsif Nkind (N) = N_Identifier
595 and then Nkind (Parent (N)) = N_Record_Representation_Clause
596 then
597 null;
599 -- Discriminants do not need to produce a reference to record type
601 elsif Typ = 'd'
602 and then Nkind (Parent (N)) = N_Discriminant_Specification
603 then
604 null;
606 -- All other cases
608 else
609 -- Special processing for IN OUT parameters, where we have an
610 -- implicit assignment to a simple variable.
612 if Kind = E_In_Out_Parameter
613 and then Is_Assignable (E)
614 then
615 -- For sure this counts as a normal read reference
617 Set_Referenced (E);
618 Set_Last_Assignment (E, Empty);
620 -- We count it as being referenced as an out parameter if the
621 -- option is set to warn on all out parameters, except that we
622 -- have a special exclusion for an intrinsic subprogram, which
623 -- is most likely an instantiation of Unchecked_Deallocation
624 -- which we do not want to consider as an assignment since it
625 -- generates false positives. We also exclude the case of an
626 -- IN OUT parameter if the name of the procedure is Free,
627 -- since we suspect similar semantics.
629 if Warn_On_All_Unread_Out_Parameters
630 and then Is_Entity_Name (Name (Call))
631 and then not Is_Intrinsic_Subprogram (Entity (Name (Call)))
632 and then Chars (Name (Call)) /= Name_Free
633 then
634 Set_Referenced_As_Out_Parameter (E, True);
635 Set_Referenced_As_LHS (E, False);
636 end if;
638 -- Don't count a recursive reference within a subprogram as a
639 -- reference (that allows detection of a recursive subprogram
640 -- whose only references are recursive calls as unreferenced).
642 elsif Is_Subprogram (E)
643 and then E = Nearest_Dynamic_Scope (Current_Scope)
644 then
645 null;
647 -- Any other occurrence counts as referencing the entity
649 elsif OK_To_Set_Referenced then
650 Set_Referenced (E);
652 -- If variable, this is an OK reference after an assignment
653 -- so we can clear the Last_Assignment indication.
655 if Is_Assignable (E) then
656 Set_Last_Assignment (E, Empty);
657 end if;
658 end if;
659 end if;
661 -- Check for pragma Unreferenced given and reference is within
662 -- this source unit (occasion for possible warning to be issued).
664 if Has_Unreferenced (E)
665 and then In_Same_Extended_Unit (E, N)
666 then
667 -- A reference as a named parameter in a call does not count
668 -- as a violation of pragma Unreferenced for this purpose...
670 if Nkind (N) = N_Identifier
671 and then Nkind (Parent (N)) = N_Parameter_Association
672 and then Selector_Name (Parent (N)) = N
673 then
674 null;
676 -- ... Neither does a reference to a variable on the left side
677 -- of an assignment.
679 elsif Is_On_LHS (N) then
680 null;
682 -- For entry formals, we want to place the warning message on the
683 -- corresponding entity in the accept statement. The current scope
684 -- is the body of the accept, so we find the formal whose name
685 -- matches that of the entry formal (there is no link between the
686 -- two entities, and the one in the accept statement is only used
687 -- for conformance checking).
689 elsif Ekind (Scope (E)) = E_Entry then
690 declare
691 BE : Entity_Id;
693 begin
694 BE := First_Entity (Current_Scope);
695 while Present (BE) loop
696 if Chars (BE) = Chars (E) then
697 Error_Msg_NE -- CODEFIX
698 ("?pragma Unreferenced given for&!", N, BE);
699 exit;
700 end if;
702 Next_Entity (BE);
703 end loop;
704 end;
706 -- Here we issue the warning, since this is a real reference
708 else
709 Error_Msg_NE -- CODEFIX
710 ("?pragma Unreferenced given for&!", N, E);
711 end if;
712 end if;
714 -- If this is a subprogram instance, mark as well the internal
715 -- subprogram in the wrapper package, which may be a visible
716 -- compilation unit.
718 if Is_Overloadable (E)
719 and then Is_Generic_Instance (E)
720 and then Present (Alias (E))
721 then
722 Set_Referenced (Alias (E));
723 end if;
724 end if;
726 -- Generate reference if all conditions are met:
729 -- Cross referencing must be active
731 Opt.Xref_Active
733 -- The entity must be one for which we collect references
735 and then Xref_Entity_Letters (Ekind (E)) /= ' '
737 -- Both Sloc values must be set to something sensible
739 and then Sloc (E) > No_Location
740 and then Sloc (N) > No_Location
742 -- We ignore references from within an instance, except for default
743 -- subprograms, for which we generate an implicit reference.
745 and then
746 (Instantiation_Location (Sloc (N)) = No_Location or else Typ = 'i')
748 -- Ignore dummy references
750 and then Typ /= ' '
751 then
752 if Nkind (N) = N_Identifier
753 or else
754 Nkind (N) = N_Defining_Identifier
755 or else
756 Nkind (N) in N_Op
757 or else
758 Nkind (N) = N_Defining_Operator_Symbol
759 or else
760 Nkind (N) = N_Operator_Symbol
761 or else
762 (Nkind (N) = N_Character_Literal
763 and then Sloc (Entity (N)) /= Standard_Location)
764 or else
765 Nkind (N) = N_Defining_Character_Literal
766 then
767 Nod := N;
769 elsif Nkind (N) = N_Expanded_Name
770 or else
771 Nkind (N) = N_Selected_Component
772 then
773 Nod := Selector_Name (N);
775 else
776 return;
777 end if;
779 -- Normal case of source entity comes from source
781 if Comes_From_Source (E) then
782 Ent := E;
784 -- Entity does not come from source, but is a derived subprogram and
785 -- the derived subprogram comes from source (after one or more
786 -- derivations) in which case the reference is to parent subprogram.
788 elsif Is_Overloadable (E)
789 and then Present (Alias (E))
790 then
791 Ent := Alias (E);
792 while not Comes_From_Source (Ent) loop
793 if No (Alias (Ent)) then
794 return;
795 end if;
797 Ent := Alias (Ent);
798 end loop;
800 -- The internally created defining entity for a child subprogram
801 -- that has no previous spec has valid references.
803 elsif Is_Overloadable (E)
804 and then Is_Child_Unit (E)
805 then
806 Ent := E;
808 -- Record components of discriminated subtypes or derived types must
809 -- be treated as references to the original component.
811 elsif Ekind (E) = E_Component
812 and then Comes_From_Source (Original_Record_Component (E))
813 then
814 Ent := Original_Record_Component (E);
816 -- If this is an expanded reference to a discriminant, recover the
817 -- original discriminant, which gets the reference.
819 elsif Ekind (E) = E_In_Parameter
820 and then Present (Discriminal_Link (E))
821 then
822 Ent := Discriminal_Link (E);
823 Set_Referenced (Ent);
825 -- Ignore reference to any other entity that is not from source
827 else
828 return;
829 end if;
831 -- Record reference to entity
833 Ref := Original_Location (Sloc (Nod));
834 Def := Original_Location (Sloc (Ent));
836 Xrefs.Increment_Last;
837 Indx := Xrefs.Last;
839 Xrefs.Table (Indx).Loc := Ref;
841 -- Overriding operations are marked with 'P'
843 if Typ = 'p'
844 and then Is_Subprogram (N)
845 and then Is_Overriding_Operation (N)
846 then
847 Xrefs.Table (Indx).Typ := 'P';
848 else
849 Xrefs.Table (Indx).Typ := Typ;
850 end if;
852 Xrefs.Table (Indx).Eun := Get_Source_Unit (Def);
853 Xrefs.Table (Indx).Lun := Get_Source_Unit (Ref);
854 Xrefs.Table (Indx).Ent := Ent;
855 Set_Has_Xref_Entry (Ent);
856 end if;
857 end Generate_Reference;
859 -----------------------------------
860 -- Generate_Reference_To_Formals --
861 -----------------------------------
863 procedure Generate_Reference_To_Formals (E : Entity_Id) is
864 Formal : Entity_Id;
866 begin
867 if Is_Generic_Subprogram (E) then
868 Formal := First_Entity (E);
870 while Present (Formal)
871 and then not Is_Formal (Formal)
872 loop
873 Next_Entity (Formal);
874 end loop;
876 else
877 Formal := First_Formal (E);
878 end if;
880 while Present (Formal) loop
881 if Ekind (Formal) = E_In_Parameter then
883 if Nkind (Parameter_Type (Parent (Formal)))
884 = N_Access_Definition
885 then
886 Generate_Reference (E, Formal, '^', False);
887 else
888 Generate_Reference (E, Formal, '>', False);
889 end if;
891 elsif Ekind (Formal) = E_In_Out_Parameter then
892 Generate_Reference (E, Formal, '=', False);
894 else
895 Generate_Reference (E, Formal, '<', False);
896 end if;
898 Next_Formal (Formal);
899 end loop;
900 end Generate_Reference_To_Formals;
902 -------------------------------------------
903 -- Generate_Reference_To_Generic_Formals --
904 -------------------------------------------
906 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
907 Formal : Entity_Id;
909 begin
910 Formal := First_Entity (E);
911 while Present (Formal) loop
912 if Comes_From_Source (Formal) then
913 Generate_Reference (E, Formal, 'z', False);
914 end if;
916 Next_Entity (Formal);
917 end loop;
918 end Generate_Reference_To_Generic_Formals;
920 ----------------
921 -- Initialize --
922 ----------------
924 procedure Initialize is
925 begin
926 Xrefs.Init;
927 end Initialize;
929 -----------------------
930 -- Output_References --
931 -----------------------
933 procedure Output_References is
935 procedure Get_Type_Reference
936 (Ent : Entity_Id;
937 Tref : out Entity_Id;
938 Left : out Character;
939 Right : out Character);
940 -- Given an Entity_Id Ent, determines whether a type reference is
941 -- required. If so, Tref is set to the entity for the type reference
942 -- and Left and Right are set to the left/right brackets to be output
943 -- for the reference. If no type reference is required, then Tref is
944 -- set to Empty, and Left/Right are set to space.
946 procedure Output_Import_Export_Info (Ent : Entity_Id);
947 -- Output language and external name information for an interfaced
948 -- entity, using the format <language, external_name>,
950 ------------------------
951 -- Get_Type_Reference --
952 ------------------------
954 procedure Get_Type_Reference
955 (Ent : Entity_Id;
956 Tref : out Entity_Id;
957 Left : out Character;
958 Right : out Character)
960 Sav : Entity_Id;
962 begin
963 -- See if we have a type reference
965 Tref := Ent;
966 Left := '{';
967 Right := '}';
969 loop
970 Sav := Tref;
972 -- Processing for types
974 if Is_Type (Tref) then
976 -- Case of base type
978 if Base_Type (Tref) = Tref then
980 -- If derived, then get first subtype
982 if Tref /= Etype (Tref) then
983 Tref := First_Subtype (Etype (Tref));
985 -- Set brackets for derived type, but don't override
986 -- pointer case since the fact that something is a
987 -- pointer is more important.
989 if Left /= '(' then
990 Left := '<';
991 Right := '>';
992 end if;
994 -- If non-derived ptr, get directly designated type.
995 -- If the type has a full view, all references are on the
996 -- partial view, that is seen first.
998 elsif Is_Access_Type (Tref) then
999 Tref := Directly_Designated_Type (Tref);
1000 Left := '(';
1001 Right := ')';
1003 elsif Is_Private_Type (Tref)
1004 and then Present (Full_View (Tref))
1005 then
1006 if Is_Access_Type (Full_View (Tref)) then
1007 Tref := Directly_Designated_Type (Full_View (Tref));
1008 Left := '(';
1009 Right := ')';
1011 -- If the full view is an array type, we also retrieve
1012 -- the corresponding component type, because the ali
1013 -- entry already indicates that this is an array.
1015 elsif Is_Array_Type (Full_View (Tref)) then
1016 Tref := Component_Type (Full_View (Tref));
1017 Left := '(';
1018 Right := ')';
1019 end if;
1021 -- If non-derived array, get component type. Skip component
1022 -- type for case of String or Wide_String, saves worthwhile
1023 -- space.
1025 elsif Is_Array_Type (Tref)
1026 and then Tref /= Standard_String
1027 and then Tref /= Standard_Wide_String
1028 then
1029 Tref := Component_Type (Tref);
1030 Left := '(';
1031 Right := ')';
1033 -- For other non-derived base types, nothing
1035 else
1036 exit;
1037 end if;
1039 -- For a subtype, go to ancestor subtype
1041 else
1042 Tref := Ancestor_Subtype (Tref);
1044 -- If no ancestor subtype, go to base type
1046 if No (Tref) then
1047 Tref := Base_Type (Sav);
1048 end if;
1049 end if;
1051 -- For objects, functions, enum literals, just get type from
1052 -- Etype field.
1054 elsif Is_Object (Tref)
1055 or else Ekind (Tref) = E_Enumeration_Literal
1056 or else Ekind (Tref) = E_Function
1057 or else Ekind (Tref) = E_Operator
1058 then
1059 Tref := Etype (Tref);
1061 -- For anything else, exit
1063 else
1064 exit;
1065 end if;
1067 -- Exit if no type reference, or we are stuck in some loop trying
1068 -- to find the type reference, or if the type is standard void
1069 -- type (the latter is an implementation artifact that should not
1070 -- show up in the generated cross-references).
1072 exit when No (Tref)
1073 or else Tref = Sav
1074 or else Tref = Standard_Void_Type;
1076 -- If we have a usable type reference, return, otherwise keep
1077 -- looking for something useful (we are looking for something
1078 -- that either comes from source or standard)
1080 if Sloc (Tref) = Standard_Location
1081 or else Comes_From_Source (Tref)
1082 then
1083 -- If the reference is a subtype created for a generic actual,
1084 -- go actual directly, the inner subtype is not user visible.
1086 if Nkind (Parent (Tref)) = N_Subtype_Declaration
1087 and then not Comes_From_Source (Parent (Tref))
1088 and then
1089 (Is_Wrapper_Package (Scope (Tref))
1090 or else Is_Generic_Instance (Scope (Tref)))
1091 then
1092 Tref := First_Subtype (Base_Type (Tref));
1093 end if;
1095 return;
1096 end if;
1097 end loop;
1099 -- If we fall through the loop, no type reference
1101 Tref := Empty;
1102 Left := ' ';
1103 Right := ' ';
1104 end Get_Type_Reference;
1106 -------------------------------
1107 -- Output_Import_Export_Info --
1108 -------------------------------
1110 procedure Output_Import_Export_Info (Ent : Entity_Id) is
1111 Language_Name : Name_Id;
1112 Conv : constant Convention_Id := Convention (Ent);
1114 begin
1115 -- Generate language name from convention
1117 if Conv = Convention_C then
1118 Language_Name := Name_C;
1120 elsif Conv = Convention_CPP then
1121 Language_Name := Name_CPP;
1123 elsif Conv = Convention_Ada then
1124 Language_Name := Name_Ada;
1126 else
1127 -- For the moment we ignore all other cases ???
1129 return;
1130 end if;
1132 Write_Info_Char ('<');
1133 Get_Unqualified_Name_String (Language_Name);
1135 for J in 1 .. Name_Len loop
1136 Write_Info_Char (Name_Buffer (J));
1137 end loop;
1139 if Present (Interface_Name (Ent)) then
1140 Write_Info_Char (',');
1141 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
1143 for J in 1 .. Name_Len loop
1144 Write_Info_Char (Name_Buffer (J));
1145 end loop;
1146 end if;
1148 Write_Info_Char ('>');
1149 end Output_Import_Export_Info;
1151 -- Start of processing for Output_References
1153 begin
1154 if not Opt.Xref_Active then
1155 return;
1156 end if;
1158 -- First we add references to the primitive operations of tagged
1159 -- types declared in the main unit.
1161 Handle_Prim_Ops : declare
1162 Ent : Entity_Id;
1164 begin
1165 for J in 1 .. Xrefs.Last loop
1166 Ent := Xrefs.Table (J).Ent;
1168 if Is_Type (Ent)
1169 and then Is_Tagged_Type (Ent)
1170 and then Ent = Base_Type (Ent)
1171 and then In_Extended_Main_Source_Unit (Ent)
1172 then
1173 Generate_Prim_Op_References (Ent);
1174 end if;
1175 end loop;
1176 end Handle_Prim_Ops;
1178 -- Before we go ahead and output the references we have a problem
1179 -- that needs dealing with. So far we have captured things that are
1180 -- definitely referenced by the main unit, or defined in the main
1181 -- unit. That's because we don't want to clutter up the ali file
1182 -- for this unit with definition lines for entities in other units
1183 -- that are not referenced.
1185 -- But there is a glitch. We may reference an entity in another unit,
1186 -- and it may have a type reference to an entity that is not directly
1187 -- referenced in the main unit, which may mean that there is no xref
1188 -- entry for this entity yet in the list of references.
1190 -- If we don't do something about this, we will end with an orphan type
1191 -- reference, i.e. it will point to an entity that does not appear
1192 -- within the generated references in the ali file. That is not good for
1193 -- tools using the xref information.
1195 -- To fix this, we go through the references adding definition entries
1196 -- for any unreferenced entities that can be referenced in a type
1197 -- reference. There is a recursion problem here, and that is dealt with
1198 -- by making sure that this traversal also traverses any entries that
1199 -- get added by the traversal.
1201 Handle_Orphan_Type_References : declare
1202 J : Nat;
1203 Tref : Entity_Id;
1204 Indx : Nat;
1205 Ent : Entity_Id;
1206 Loc : Source_Ptr;
1208 L, R : Character;
1209 pragma Warnings (Off, L);
1210 pragma Warnings (Off, R);
1212 procedure New_Entry (E : Entity_Id);
1213 -- Make an additional entry into the Xref table for a type entity
1214 -- that is related to the current entity (parent, type ancestor,
1215 -- progenitor, etc.).
1217 ----------------
1218 -- New_Entry --
1219 ----------------
1221 procedure New_Entry (E : Entity_Id) is
1222 begin
1223 if Present (E)
1224 and then not Has_Xref_Entry (E)
1225 and then Sloc (E) > No_Location
1226 then
1227 Xrefs.Increment_Last;
1228 Indx := Xrefs.Last;
1229 Loc := Original_Location (Sloc (E));
1230 Xrefs.Table (Indx).Ent := E;
1231 Xrefs.Table (Indx).Loc := No_Location;
1232 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
1233 Xrefs.Table (Indx).Lun := No_Unit;
1234 Set_Has_Xref_Entry (E);
1235 end if;
1236 end New_Entry;
1238 -- Start of processing for Handle_Orphan_Type_References
1240 begin
1241 -- Note that this is not a for loop for a very good reason. The
1242 -- processing of items in the table can add new items to the table,
1243 -- and they must be processed as well.
1245 J := 1;
1246 while J <= Xrefs.Last loop
1247 Ent := Xrefs.Table (J).Ent;
1248 Get_Type_Reference (Ent, Tref, L, R);
1250 if Present (Tref)
1251 and then not Has_Xref_Entry (Tref)
1252 and then Sloc (Tref) > No_Location
1253 then
1254 New_Entry (Tref);
1256 if Is_Record_Type (Ent)
1257 and then Present (Interfaces (Ent))
1258 then
1259 -- Add an entry for each one of the given interfaces
1260 -- implemented by type Ent.
1262 declare
1263 Elmt : Elmt_Id := First_Elmt (Interfaces (Ent));
1264 begin
1265 while Present (Elmt) loop
1266 New_Entry (Node (Elmt));
1267 Next_Elmt (Elmt);
1268 end loop;
1269 end;
1270 end if;
1271 end if;
1273 -- Collect inherited primitive operations that may be declared in
1274 -- another unit and have no visible reference in the current one.
1276 if Is_Type (Ent)
1277 and then Is_Tagged_Type (Ent)
1278 and then Is_Derived_Type (Ent)
1279 and then Ent = Base_Type (Ent)
1280 and then In_Extended_Main_Source_Unit (Ent)
1281 then
1282 declare
1283 Op_List : constant Elist_Id := Primitive_Operations (Ent);
1284 Op : Elmt_Id;
1285 Prim : Entity_Id;
1287 function Parent_Op (E : Entity_Id) return Entity_Id;
1288 -- Find original operation, which may be inherited through
1289 -- several derivations.
1291 function Parent_Op (E : Entity_Id) return Entity_Id is
1292 Orig_Op : constant Entity_Id := Alias (E);
1294 begin
1295 if No (Orig_Op) then
1296 return Empty;
1298 elsif not Comes_From_Source (E)
1299 and then not Has_Xref_Entry (Orig_Op)
1300 and then Comes_From_Source (Orig_Op)
1301 then
1302 return Orig_Op;
1303 else
1304 return Parent_Op (Orig_Op);
1305 end if;
1306 end Parent_Op;
1308 begin
1309 Op := First_Elmt (Op_List);
1310 while Present (Op) loop
1311 Prim := Parent_Op (Node (Op));
1313 if Present (Prim) then
1314 Xrefs.Increment_Last;
1315 Indx := Xrefs.Last;
1316 Loc := Original_Location (Sloc (Prim));
1317 Xrefs.Table (Indx).Ent := Prim;
1318 Xrefs.Table (Indx).Loc := No_Location;
1319 Xrefs.Table (Indx).Eun :=
1320 Get_Source_Unit (Sloc (Prim));
1321 Xrefs.Table (Indx).Lun := No_Unit;
1322 Set_Has_Xref_Entry (Prim);
1323 end if;
1325 Next_Elmt (Op);
1326 end loop;
1327 end;
1328 end if;
1330 J := J + 1;
1331 end loop;
1332 end Handle_Orphan_Type_References;
1334 -- Now we have all the references, including those for any embedded
1335 -- type references, so we can sort them, and output them.
1337 Output_Refs : declare
1339 Nrefs : Nat := Xrefs.Last;
1340 -- Number of references in table. This value may get reset (reduced)
1341 -- when we eliminate duplicate reference entries.
1343 Rnums : array (0 .. Nrefs) of Nat;
1344 -- This array contains numbers of references in the Xrefs table.
1345 -- This list is sorted in output order. The extra 0'th entry is
1346 -- convenient for the call to sort. When we sort the table, we
1347 -- move the entries in Rnums around, but we do not move the
1348 -- original table entries.
1350 Curxu : Unit_Number_Type;
1351 -- Current xref unit
1353 Curru : Unit_Number_Type;
1354 -- Current reference unit for one entity
1356 Cursrc : Source_Buffer_Ptr;
1357 -- Current xref unit source text
1359 Curent : Entity_Id;
1360 -- Current entity
1362 Curnam : String (1 .. Name_Buffer'Length);
1363 Curlen : Natural;
1364 -- Simple name and length of current entity
1366 Curdef : Source_Ptr;
1367 -- Original source location for current entity
1369 Crloc : Source_Ptr;
1370 -- Current reference location
1372 Ctyp : Character;
1373 -- Entity type character
1375 Tref : Entity_Id;
1376 -- Type reference
1378 Rref : Node_Id;
1379 -- Renaming reference
1381 Trunit : Unit_Number_Type;
1382 -- Unit number for type reference
1384 function Lt (Op1, Op2 : Natural) return Boolean;
1385 -- Comparison function for Sort call
1387 function Name_Change (X : Entity_Id) return Boolean;
1388 -- Determines if entity X has a different simple name from Curent
1390 procedure Move (From : Natural; To : Natural);
1391 -- Move procedure for Sort call
1393 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
1395 --------
1396 -- Lt --
1397 --------
1399 function Lt (Op1, Op2 : Natural) return Boolean is
1400 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
1401 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
1403 begin
1404 -- First test: if entity is in different unit, sort by unit
1406 if T1.Eun /= T2.Eun then
1407 return Dependency_Num (T1.Eun) < Dependency_Num (T2.Eun);
1409 -- Second test: within same unit, sort by entity Sloc
1411 elsif T1.Def /= T2.Def then
1412 return T1.Def < T2.Def;
1414 -- Third test: sort definitions ahead of references
1416 elsif T1.Loc = No_Location then
1417 return True;
1419 elsif T2.Loc = No_Location then
1420 return False;
1422 -- Fourth test: for same entity, sort by reference location unit
1424 elsif T1.Lun /= T2.Lun then
1425 return Dependency_Num (T1.Lun) < Dependency_Num (T2.Lun);
1427 -- Fifth test: order of location within referencing unit
1429 elsif T1.Loc /= T2.Loc then
1430 return T1.Loc < T2.Loc;
1432 -- Finally, for two locations at the same address, we prefer
1433 -- the one that does NOT have the type 'r' so that a modification
1434 -- or extension takes preference, when there are more than one
1435 -- reference at the same location.
1437 else
1438 return T2.Typ = 'r';
1439 end if;
1440 end Lt;
1442 ----------
1443 -- Move --
1444 ----------
1446 procedure Move (From : Natural; To : Natural) is
1447 begin
1448 Rnums (Nat (To)) := Rnums (Nat (From));
1449 end Move;
1451 -----------------
1452 -- Name_Change --
1453 -----------------
1455 -- Why a string comparison here??? Why not compare Name_Id values???
1457 function Name_Change (X : Entity_Id) return Boolean is
1458 begin
1459 Get_Unqualified_Name_String (Chars (X));
1461 if Name_Len /= Curlen then
1462 return True;
1463 else
1464 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
1465 end if;
1466 end Name_Change;
1468 -- Start of processing for Output_Refs
1470 begin
1471 -- Capture the definition Sloc values. We delay doing this till now,
1472 -- since at the time the reference or definition is made, private
1473 -- types may be swapped, and the Sloc value may be incorrect. We
1474 -- also set up the pointer vector for the sort.
1476 for J in 1 .. Nrefs loop
1477 Rnums (J) := J;
1478 Xrefs.Table (J).Def :=
1479 Original_Location (Sloc (Xrefs.Table (J).Ent));
1480 end loop;
1482 -- Sort the references
1484 Sorting.Sort (Integer (Nrefs));
1486 -- Eliminate duplicate entries
1488 declare
1489 NR : constant Nat := Nrefs;
1491 begin
1492 -- We need this test for NR because if we force ALI file
1493 -- generation in case of errors detected, it may be the case
1494 -- that Nrefs is 0, so we should not reset it here
1496 if NR >= 2 then
1497 Nrefs := 1;
1499 for J in 2 .. NR loop
1500 if Xrefs.Table (Rnums (J)) /=
1501 Xrefs.Table (Rnums (Nrefs))
1502 then
1503 Nrefs := Nrefs + 1;
1504 Rnums (Nrefs) := Rnums (J);
1505 end if;
1506 end loop;
1507 end if;
1508 end;
1510 -- Initialize loop through references
1512 Curxu := No_Unit;
1513 Curent := Empty;
1514 Curdef := No_Location;
1515 Curru := No_Unit;
1516 Crloc := No_Location;
1518 -- Loop to output references
1520 for Refno in 1 .. Nrefs loop
1521 Output_One_Ref : declare
1522 P2 : Source_Ptr;
1523 Ent : Entity_Id;
1525 WC : Char_Code;
1526 Err : Boolean;
1527 pragma Warnings (Off, WC);
1528 pragma Warnings (Off, Err);
1530 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
1531 -- The current entry to be accessed
1533 P : Source_Ptr;
1534 -- Used to index into source buffer to get entity name
1536 Left : Character;
1537 Right : Character;
1538 -- Used for {} or <> or () for type reference
1540 procedure Check_Type_Reference
1541 (Ent : Entity_Id;
1542 List_Interface : Boolean);
1543 -- Find whether there is a meaningful type reference for
1544 -- Ent, and display it accordingly. If List_Interface is
1545 -- true, then Ent is a progenitor interface of the current
1546 -- type entity being listed. In that case list it as is,
1547 -- without looking for a type reference for it.
1549 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
1550 -- Recursive procedure to output instantiation references for
1551 -- the given source ptr in [file|line[...]] form. No output
1552 -- if the given location is not a generic template reference.
1554 procedure Output_Overridden_Op (Old_E : Entity_Id);
1555 -- For a subprogram that is overriding, display information
1556 -- about the inherited operation that it overrides.
1558 --------------------------
1559 -- Check_Type_Reference --
1560 --------------------------
1562 procedure Check_Type_Reference
1563 (Ent : Entity_Id;
1564 List_Interface : Boolean)
1566 begin
1567 if List_Interface then
1569 -- This is a progenitor interface of the type for which
1570 -- xref information is being generated.
1572 Tref := Ent;
1573 Left := '<';
1574 Right := '>';
1576 else
1577 Get_Type_Reference (Ent, Tref, Left, Right);
1578 end if;
1580 if Present (Tref) then
1582 -- Case of standard entity, output name
1584 if Sloc (Tref) = Standard_Location then
1585 Write_Info_Char (Left);
1586 Write_Info_Name (Chars (Tref));
1587 Write_Info_Char (Right);
1589 -- Case of source entity, output location
1591 else
1592 Write_Info_Char (Left);
1593 Trunit := Get_Source_Unit (Sloc (Tref));
1595 if Trunit /= Curxu then
1596 Write_Info_Nat (Dependency_Num (Trunit));
1597 Write_Info_Char ('|');
1598 end if;
1600 Write_Info_Nat
1601 (Int (Get_Logical_Line_Number (Sloc (Tref))));
1603 declare
1604 Ent : Entity_Id;
1605 Ctyp : Character;
1607 begin
1608 Ent := Tref;
1609 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1611 if Ctyp = '+'
1612 and then Present (Full_View (Ent))
1613 then
1614 Ent := Underlying_Type (Ent);
1616 if Present (Ent) then
1617 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1618 end if;
1619 end if;
1621 Write_Info_Char (Ctyp);
1622 end;
1624 Write_Info_Nat
1625 (Int (Get_Column_Number (Sloc (Tref))));
1627 -- If the type comes from an instantiation, add the
1628 -- corresponding info.
1630 Output_Instantiation_Refs (Sloc (Tref));
1631 Write_Info_Char (Right);
1632 end if;
1633 end if;
1634 end Check_Type_Reference;
1636 -------------------------------
1637 -- Output_Instantiation_Refs --
1638 -------------------------------
1640 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
1641 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
1642 Lun : Unit_Number_Type;
1643 Cu : constant Unit_Number_Type := Curru;
1645 begin
1646 -- Nothing to do if this is not an instantiation
1648 if Iloc = No_Location then
1649 return;
1650 end if;
1652 -- Output instantiation reference
1654 Write_Info_Char ('[');
1655 Lun := Get_Source_Unit (Iloc);
1657 if Lun /= Curru then
1658 Curru := Lun;
1659 Write_Info_Nat (Dependency_Num (Curru));
1660 Write_Info_Char ('|');
1661 end if;
1663 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
1665 -- Recursive call to get nested instantiations
1667 Output_Instantiation_Refs (Iloc);
1669 -- Output final ] after call to get proper nesting
1671 Write_Info_Char (']');
1672 Curru := Cu;
1673 return;
1674 end Output_Instantiation_Refs;
1676 --------------------------
1677 -- Output_Overridden_Op --
1678 --------------------------
1680 procedure Output_Overridden_Op (Old_E : Entity_Id) is
1681 Op : Entity_Id;
1683 begin
1684 -- The overridden operation has an implicit declaration
1685 -- at the point of derivation. What we want to display
1686 -- is the original operation, which has the actual body
1687 -- (or abstract declaration) that is being overridden.
1688 -- The overridden operation is not always set, e.g. when
1689 -- it is a predefined operator.
1691 if No (Old_E) then
1692 return;
1694 -- Follow alias chain if one is present
1696 elsif Present (Alias (Old_E)) then
1698 -- The subprogram may have been implicitly inherited
1699 -- through several levels of derivation, so find the
1700 -- ultimate (source) ancestor.
1702 Op := Ultimate_Alias (Old_E);
1704 -- Normal case of no alias present
1706 else
1707 Op := Old_E;
1708 end if;
1710 if Present (Op)
1711 and then Sloc (Op) /= Standard_Location
1712 then
1713 declare
1714 Loc : constant Source_Ptr := Sloc (Op);
1715 Par_Unit : constant Unit_Number_Type :=
1716 Get_Source_Unit (Loc);
1718 begin
1719 Write_Info_Char ('<');
1721 if Par_Unit /= Curxu then
1722 Write_Info_Nat (Dependency_Num (Par_Unit));
1723 Write_Info_Char ('|');
1724 end if;
1726 Write_Info_Nat (Int (Get_Logical_Line_Number (Loc)));
1727 Write_Info_Char ('p');
1728 Write_Info_Nat (Int (Get_Column_Number (Loc)));
1729 Write_Info_Char ('>');
1730 end;
1731 end if;
1732 end Output_Overridden_Op;
1734 -- Start of processing for Output_One_Ref
1736 begin
1737 Ent := XE.Ent;
1738 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1740 -- Skip reference if it is the only reference to an entity,
1741 -- and it is an END line reference, and the entity is not in
1742 -- the current extended source. This prevents junk entries
1743 -- consisting only of packages with END lines, where no
1744 -- entity from the package is actually referenced.
1746 if XE.Typ = 'e'
1747 and then Ent /= Curent
1748 and then (Refno = Nrefs or else
1749 Ent /= Xrefs.Table (Rnums (Refno + 1)).Ent)
1750 and then
1751 not In_Extended_Main_Source_Unit (Ent)
1752 then
1753 goto Continue;
1754 end if;
1756 -- For private type, get full view type
1758 if Ctyp = '+'
1759 and then Present (Full_View (XE.Ent))
1760 then
1761 Ent := Underlying_Type (Ent);
1763 if Present (Ent) then
1764 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1765 end if;
1766 end if;
1768 -- Special exception for Boolean
1770 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
1771 Ctyp := 'B';
1772 end if;
1774 -- For variable reference, get corresponding type
1776 if Ctyp = '*' then
1777 Ent := Etype (XE.Ent);
1778 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1780 -- If variable is private type, get full view type
1782 if Ctyp = '+'
1783 and then Present (Full_View (Etype (XE.Ent)))
1784 then
1785 Ent := Underlying_Type (Etype (XE.Ent));
1787 if Present (Ent) then
1788 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1789 end if;
1791 elsif Is_Generic_Type (Ent) then
1793 -- If the type of the entity is a generic private type,
1794 -- there is no usable full view, so retain the indication
1795 -- that this is an object.
1797 Ctyp := '*';
1798 end if;
1800 -- Special handling for access parameter
1802 declare
1803 K : constant Entity_Kind := Ekind (Etype (XE.Ent));
1805 begin
1806 if (K = E_Anonymous_Access_Type
1807 or else
1808 K = E_Anonymous_Access_Subprogram_Type
1809 or else K =
1810 E_Anonymous_Access_Protected_Subprogram_Type)
1811 and then Is_Formal (XE.Ent)
1812 then
1813 Ctyp := 'p';
1815 -- Special handling for Boolean
1817 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
1818 Ctyp := 'b';
1819 end if;
1820 end;
1821 end if;
1823 -- Special handling for abstract types and operations
1825 if Is_Overloadable (XE.Ent)
1826 and then Is_Abstract_Subprogram (XE.Ent)
1827 then
1828 if Ctyp = 'U' then
1829 Ctyp := 'x'; -- Abstract procedure
1831 elsif Ctyp = 'V' then
1832 Ctyp := 'y'; -- Abstract function
1833 end if;
1835 elsif Is_Type (XE.Ent)
1836 and then Is_Abstract_Type (XE.Ent)
1837 then
1838 if Is_Interface (XE.Ent) then
1839 Ctyp := 'h';
1841 elsif Ctyp = 'R' then
1842 Ctyp := 'H'; -- Abstract type
1843 end if;
1844 end if;
1846 -- Only output reference if interesting type of entity, and
1847 -- suppress self references, except for bodies that act as
1848 -- specs. Also suppress definitions of body formals (we only
1849 -- treat these as references, and the references were
1850 -- separately recorded).
1852 if Ctyp = ' '
1853 or else (XE.Loc = XE.Def
1854 and then
1855 (XE.Typ /= 'b'
1856 or else not Is_Subprogram (XE.Ent)))
1857 or else (Is_Formal (XE.Ent)
1858 and then Present (Spec_Entity (XE.Ent)))
1859 then
1860 null;
1862 else
1863 -- Start new Xref section if new xref unit
1865 if XE.Eun /= Curxu then
1866 if Write_Info_Col > 1 then
1867 Write_Info_EOL;
1868 end if;
1870 Curxu := XE.Eun;
1871 Cursrc := Source_Text (Source_Index (Curxu));
1873 Write_Info_Initiate ('X');
1874 Write_Info_Char (' ');
1875 Write_Info_Nat (Dependency_Num (XE.Eun));
1876 Write_Info_Char (' ');
1877 Write_Info_Name (Reference_Name (Source_Index (XE.Eun)));
1878 end if;
1880 -- Start new Entity line if new entity. Note that we
1881 -- consider two entities the same if they have the same
1882 -- name and source location. This causes entities in
1883 -- instantiations to be treated as though they referred
1884 -- to the template.
1886 if No (Curent)
1887 or else
1888 (XE.Ent /= Curent
1889 and then
1890 (Name_Change (XE.Ent) or else XE.Def /= Curdef))
1891 then
1892 Curent := XE.Ent;
1893 Curdef := XE.Def;
1895 Get_Unqualified_Name_String (Chars (XE.Ent));
1896 Curlen := Name_Len;
1897 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
1899 if Write_Info_Col > 1 then
1900 Write_Info_EOL;
1901 end if;
1903 -- Write column number information
1905 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
1906 Write_Info_Char (Ctyp);
1907 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
1909 -- Write level information
1911 Write_Level_Info : declare
1912 function Is_Visible_Generic_Entity
1913 (E : Entity_Id) return Boolean;
1914 -- Check whether E is declared in the visible part
1915 -- of a generic package. For source navigation
1916 -- purposes, treat this as a visible entity.
1918 function Is_Private_Record_Component
1919 (E : Entity_Id) return Boolean;
1920 -- Check whether E is a non-inherited component of a
1921 -- private extension. Even if the enclosing record is
1922 -- public, we want to treat the component as private
1923 -- for navigation purposes.
1925 ---------------------------------
1926 -- Is_Private_Record_Component --
1927 ---------------------------------
1929 function Is_Private_Record_Component
1930 (E : Entity_Id) return Boolean
1932 S : constant Entity_Id := Scope (E);
1933 begin
1934 return
1935 Ekind (E) = E_Component
1936 and then Nkind (Declaration_Node (S)) =
1937 N_Private_Extension_Declaration
1938 and then Original_Record_Component (E) = E;
1939 end Is_Private_Record_Component;
1941 -------------------------------
1942 -- Is_Visible_Generic_Entity --
1943 -------------------------------
1945 function Is_Visible_Generic_Entity
1946 (E : Entity_Id) return Boolean
1948 Par : Node_Id;
1950 begin
1951 -- The Present check here is an error defense
1953 if Present (Scope (E))
1954 and then Ekind (Scope (E)) /= E_Generic_Package
1955 then
1956 return False;
1957 end if;
1959 Par := Parent (E);
1960 while Present (Par) loop
1962 Nkind (Par) = N_Generic_Package_Declaration
1963 then
1964 -- Entity is a generic formal
1966 return False;
1968 elsif
1969 Nkind (Parent (Par)) = N_Package_Specification
1970 then
1971 return
1972 Is_List_Member (Par)
1973 and then List_Containing (Par) =
1974 Visible_Declarations (Parent (Par));
1975 else
1976 Par := Parent (Par);
1977 end if;
1978 end loop;
1980 return False;
1981 end Is_Visible_Generic_Entity;
1983 -- Start of processing for Write_Level_Info
1985 begin
1986 if Is_Hidden (Curent)
1987 or else Is_Private_Record_Component (Curent)
1988 then
1989 Write_Info_Char (' ');
1991 elsif
1992 Is_Public (Curent)
1993 or else Is_Visible_Generic_Entity (Curent)
1994 then
1995 Write_Info_Char ('*');
1997 else
1998 Write_Info_Char (' ');
1999 end if;
2000 end Write_Level_Info;
2002 -- Output entity name. We use the occurrence from the
2003 -- actual source program at the definition point.
2005 P := Original_Location (Sloc (XE.Ent));
2007 -- Entity is character literal
2009 if Cursrc (P) = ''' then
2010 Write_Info_Char (Cursrc (P));
2011 Write_Info_Char (Cursrc (P + 1));
2012 Write_Info_Char (Cursrc (P + 2));
2014 -- Entity is operator symbol
2016 elsif Cursrc (P) = '"' or else Cursrc (P) = '%' then
2017 Write_Info_Char (Cursrc (P));
2019 P2 := P;
2020 loop
2021 P2 := P2 + 1;
2022 Write_Info_Char (Cursrc (P2));
2023 exit when Cursrc (P2) = Cursrc (P);
2024 end loop;
2026 -- Entity is identifier
2028 else
2029 loop
2030 if Is_Start_Of_Wide_Char (Cursrc, P) then
2031 Scan_Wide (Cursrc, P, WC, Err);
2032 elsif not Identifier_Char (Cursrc (P)) then
2033 exit;
2034 else
2035 P := P + 1;
2036 end if;
2037 end loop;
2039 -- Write out the identifier by copying the exact
2040 -- source characters used in its declaration. Note
2041 -- that this means wide characters will be in their
2042 -- original encoded form.
2044 for J in
2045 Original_Location (Sloc (XE.Ent)) .. P - 1
2046 loop
2047 Write_Info_Char (Cursrc (J));
2048 end loop;
2049 end if;
2051 -- See if we have a renaming reference
2053 if Is_Object (XE.Ent)
2054 and then Present (Renamed_Object (XE.Ent))
2055 then
2056 Rref := Renamed_Object (XE.Ent);
2058 elsif Is_Overloadable (XE.Ent)
2059 and then Nkind (Parent (Declaration_Node (XE.Ent))) =
2060 N_Subprogram_Renaming_Declaration
2061 then
2062 Rref := Name (Parent (Declaration_Node (XE.Ent)));
2064 elsif Ekind (XE.Ent) = E_Package
2065 and then Nkind (Declaration_Node (XE.Ent)) =
2066 N_Package_Renaming_Declaration
2067 then
2068 Rref := Name (Declaration_Node (XE.Ent));
2070 else
2071 Rref := Empty;
2072 end if;
2074 if Present (Rref) then
2075 if Nkind (Rref) = N_Expanded_Name then
2076 Rref := Selector_Name (Rref);
2077 end if;
2079 if Nkind (Rref) = N_Identifier
2080 or else Nkind (Rref) = N_Operator_Symbol
2081 then
2082 null;
2084 -- For renamed array components, use the array name
2085 -- for the renamed entity, which reflect the fact that
2086 -- in general the whole array is aliased.
2088 elsif Nkind (Rref) = N_Indexed_Component then
2089 if Nkind (Prefix (Rref)) = N_Identifier then
2090 Rref := Prefix (Rref);
2091 elsif Nkind (Prefix (Rref)) = N_Expanded_Name then
2092 Rref := Selector_Name (Prefix (Rref));
2093 else
2094 Rref := Empty;
2095 end if;
2097 else
2098 Rref := Empty;
2099 end if;
2100 end if;
2102 -- Write out renaming reference if we have one
2104 if Present (Rref) then
2105 Write_Info_Char ('=');
2106 Write_Info_Nat
2107 (Int (Get_Logical_Line_Number (Sloc (Rref))));
2108 Write_Info_Char (':');
2109 Write_Info_Nat
2110 (Int (Get_Column_Number (Sloc (Rref))));
2111 end if;
2113 -- Indicate that the entity is in the unit of the current
2114 -- xref section.
2116 Curru := Curxu;
2118 -- Write out information about generic parent, if entity
2119 -- is an instance.
2121 if Is_Generic_Instance (XE.Ent) then
2122 declare
2123 Gen_Par : constant Entity_Id :=
2124 Generic_Parent
2125 (Specification
2126 (Unit_Declaration_Node (XE.Ent)));
2127 Loc : constant Source_Ptr := Sloc (Gen_Par);
2128 Gen_U : constant Unit_Number_Type :=
2129 Get_Source_Unit (Loc);
2131 begin
2132 Write_Info_Char ('[');
2134 if Curru /= Gen_U then
2135 Write_Info_Nat (Dependency_Num (Gen_U));
2136 Write_Info_Char ('|');
2137 end if;
2139 Write_Info_Nat
2140 (Int (Get_Logical_Line_Number (Loc)));
2141 Write_Info_Char (']');
2142 end;
2143 end if;
2145 -- See if we have a type reference and if so output
2147 Check_Type_Reference (XE.Ent, False);
2149 -- Additional information for types with progenitors
2151 if Is_Record_Type (XE.Ent)
2152 and then Present (Interfaces (XE.Ent))
2153 then
2154 declare
2155 Elmt : Elmt_Id := First_Elmt (Interfaces (XE.Ent));
2156 begin
2157 while Present (Elmt) loop
2158 Check_Type_Reference (Node (Elmt), True);
2159 Next_Elmt (Elmt);
2160 end loop;
2161 end;
2163 -- For array types, list index types as well.
2164 -- (This is not C, indices have distinct types).
2166 elsif Is_Array_Type (XE.Ent) then
2167 declare
2168 Indx : Node_Id;
2169 begin
2170 Indx := First_Index (XE.Ent);
2171 while Present (Indx) loop
2172 Check_Type_Reference
2173 (First_Subtype (Etype (Indx)), True);
2174 Next_Index (Indx);
2175 end loop;
2176 end;
2177 end if;
2179 -- If the entity is an overriding operation, write info
2180 -- on operation that was overridden.
2182 if Is_Subprogram (XE.Ent)
2183 and then Is_Overriding_Operation (XE.Ent)
2184 then
2185 Output_Overridden_Op (Overridden_Operation (XE.Ent));
2186 end if;
2188 -- End of processing for entity output
2190 Crloc := No_Location;
2191 end if;
2193 -- Output the reference
2195 if XE.Loc /= No_Location
2196 and then XE.Loc /= Crloc
2197 then
2198 Crloc := XE.Loc;
2200 -- Start continuation if line full, else blank
2202 if Write_Info_Col > 72 then
2203 Write_Info_EOL;
2204 Write_Info_Initiate ('.');
2205 end if;
2207 Write_Info_Char (' ');
2209 -- Output file number if changed
2211 if XE.Lun /= Curru then
2212 Curru := XE.Lun;
2213 Write_Info_Nat (Dependency_Num (Curru));
2214 Write_Info_Char ('|');
2215 end if;
2217 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Loc)));
2218 Write_Info_Char (XE.Typ);
2220 if Is_Overloadable (XE.Ent)
2221 and then Is_Imported (XE.Ent)
2222 and then XE.Typ = 'b'
2223 then
2224 Output_Import_Export_Info (XE.Ent);
2225 end if;
2227 Write_Info_Nat (Int (Get_Column_Number (XE.Loc)));
2229 Output_Instantiation_Refs (Sloc (XE.Ent));
2230 end if;
2231 end if;
2232 end Output_One_Ref;
2234 <<Continue>>
2235 null;
2236 end loop;
2238 Write_Info_EOL;
2239 end Output_Refs;
2240 end Output_References;
2242 end Lib.Xref;