[multiple changes]
[gcc.git] / gcc / ada / lib-xref.adb
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 ------------------------------------------------------------------------------
25
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;
47
48 with GNAT.Heap_Sort_G;
49
50 package body Lib.Xref is
51
52 ------------------
53 -- Declarations --
54 ------------------
55
56 -- The Xref table is used to record references. The Loc field is set
57 -- to No_Location for a definition entry.
58
59 subtype Xref_Entry_Number is Int;
60
61 type Xref_Entry is record
62 Ent : Entity_Id;
63 -- Entity referenced (E parameter to Generate_Reference)
64
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.
70
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.
75
76 Typ : Character;
77 -- Reference type (Typ param to Generate_Reference)
78
79 Eun : Unit_Number_Type;
80 -- Unit number corresponding to Ent
81
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.
85
86 end record;
87
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");
95
96 ------------------------
97 -- Local Subprograms --
98 ------------------------
99
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.
105
106 -------------------------
107 -- Generate_Definition --
108 -------------------------
109
110 procedure Generate_Definition (E : Entity_Id) is
111 Loc : Source_Ptr;
112 Indx : Nat;
113
114 begin
115 pragma Assert (Nkind (E) in N_Entity);
116
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.
120
121 if Opt.Xref_Active
122
123 -- Definition must come from source
124
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.
130
131 and then (Comes_From_Source (E) or else Is_Child_Unit (E))
132
133 -- And must have a reasonable source location that is not
134 -- within an instance (all entities in instances are ignored)
135
136 and then Sloc (E) > No_Location
137 and then Instantiation_Location (Sloc (E)) = No_Location
138
139 -- And must be a non-internal name from the main source unit
140
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));
147
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);
155
156 if In_Inlined_Body then
157 Set_Referenced (E);
158 end if;
159 end if;
160 end Generate_Definition;
161
162 ---------------------------------
163 -- Generate_Operator_Reference --
164 ---------------------------------
165
166 procedure Generate_Operator_Reference
167 (N : Node_Id;
168 T : Entity_Id)
169 is
170 begin
171 if not In_Extended_Main_Source_Unit (N) then
172 return;
173 end if;
174
175 -- If the operator is not a Standard operator, then we generate a real
176 -- reference to the user defined operator.
177
178 if Sloc (Entity (N)) /= Standard_Location then
179 Generate_Reference (Entity (N), N);
180
181 -- A reference to an implicit inequality operator is also a reference
182 -- to the user-defined equality.
183
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;
190
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.
197
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).
203
204 elsif Comes_From_Source (N) then
205 Set_Referenced (First_Subtype (T));
206 end if;
207 end Generate_Operator_Reference;
208
209 ---------------------------------
210 -- Generate_Prim_Op_References --
211 ---------------------------------
212
213 procedure Generate_Prim_Op_References (Typ : Entity_Id) is
214 Base_T : Entity_Id;
215 Prim : Elmt_Id;
216 Prim_List : Elist_Id;
217
218 begin
219 -- Handle subtypes of synchronized types
220
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;
228
229 -- References to primitive operations are only relevant for tagged types
230
231 if not Is_Tagged_Type (Base_T)
232 or else Is_Class_Wide_Type (Base_T)
233 then
234 return;
235 end if;
236
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.
240
241 -- The check for Present here is to protect against previously
242 -- reported critical errors.
243
244 Prim_List := Primitive_Operations (Base_T);
245
246 if No (Prim_List) then
247 return;
248 end if;
249
250 Prim := First_Elmt (Prim_List);
251 while Present (Prim) loop
252
253 -- If the operation is derived, get the original for cross-reference
254 -- reference purposes (it is the original for which we want the xref
255 -- and for which the comes_from_source test must be performed).
256
257 Generate_Reference
258 (Typ, Ultimate_Alias (Node (Prim)), 'p', Set_Ref => False);
259 Next_Elmt (Prim);
260 end loop;
261 end Generate_Prim_Op_References;
262
263 ------------------------
264 -- Generate_Reference --
265 ------------------------
266
267 procedure Generate_Reference
268 (E : Entity_Id;
269 N : Node_Id;
270 Typ : Character := 'r';
271 Set_Ref : Boolean := True;
272 Force : Boolean := False)
273 is
274 Indx : Nat;
275 Nod : Node_Id;
276 Ref : Source_Ptr;
277 Def : Source_Ptr;
278 Ent : Entity_Id;
279
280 Call : Node_Id;
281 Formal : Entity_Id;
282 -- Used for call to Find_Actual
283
284 Kind : Entity_Kind;
285 -- If Formal is non-Empty, then its Ekind, otherwise E_Void
286
287 function Is_On_LHS (Node : Node_Id) return Boolean;
288 -- Used to check if a node is on the left hand side of an assignment.
289 -- The following cases are handled:
290 --
291 -- Variable Node is a direct descendant of left hand side of an
292 -- assignment statement.
293 --
294 -- Prefix Of an indexed or selected component that is present in
295 -- a subtree rooted by an assignment statement. There is
296 -- no restriction of nesting of components, thus cases
297 -- such as A.B (C).D are handled properly. However a prefix
298 -- of a dereference (either implicit or explicit) is never
299 -- considered as on a LHS.
300 --
301 -- Out param Same as above cases, but OUT parameter
302
303 function OK_To_Set_Referenced return Boolean;
304 -- Returns True if the Referenced flag can be set. There are a few
305 -- exceptions where we do not want to set this flag, see body for
306 -- details of these exceptional cases.
307
308 ---------------
309 -- Is_On_LHS --
310 ---------------
311
312 -- ??? There are several routines here and there that perform a similar
313 -- (but subtly different) computation, which should be factored:
314
315 -- Sem_Util.May_Be_Lvalue
316 -- Sem_Util.Known_To_Be_Assigned
317 -- Exp_Ch2.Expand_Entry_Parameter.In_Assignment_Context
318 -- Exp_Smem.Is_Out_Actual
319
320 function Is_On_LHS (Node : Node_Id) return Boolean is
321 N : Node_Id;
322 P : Node_Id;
323 K : Node_Kind;
324
325 begin
326 -- Only identifiers are considered, is this necessary???
327
328 if Nkind (Node) /= N_Identifier then
329 return False;
330 end if;
331
332 -- Immediate return if appeared as OUT parameter
333
334 if Kind = E_Out_Parameter then
335 return True;
336 end if;
337
338 -- Search for assignment statement subtree root
339
340 N := Node;
341 loop
342 P := Parent (N);
343 K := Nkind (P);
344
345 if K = N_Assignment_Statement then
346 return Name (P) = N;
347
348 -- Check whether the parent is a component and the current node is
349 -- its prefix, but return False if the current node has an access
350 -- type, as in that case the selected or indexed component is an
351 -- implicit dereference, and the LHS is the designated object, not
352 -- the access object.
353
354 -- ??? case of a slice assignment?
355
356 -- ??? Note that in some cases this is called too early
357 -- (see comments in Sem_Ch8.Find_Direct_Name), at a point where
358 -- the tree is not fully typed yet. In that case we may lack
359 -- an Etype for N, and we must disable the check for an implicit
360 -- dereference. If the dereference is on an LHS, this causes a
361 -- false positive.
362
363 elsif (K = N_Selected_Component or else K = N_Indexed_Component)
364 and then Prefix (P) = N
365 and then not (Present (Etype (N))
366 and then
367 Is_Access_Type (Etype (N)))
368 then
369 N := P;
370
371 -- All other cases, definitely not on left side
372
373 else
374 return False;
375 end if;
376 end loop;
377 end Is_On_LHS;
378
379 ---------------------------
380 -- OK_To_Set_Referenced --
381 ---------------------------
382
383 function OK_To_Set_Referenced return Boolean is
384 P : Node_Id;
385
386 begin
387 -- A reference from a pragma Unreferenced or pragma Unmodified or
388 -- pragma Warnings does not cause the Referenced flag to be set.
389 -- This avoids silly warnings about things being referenced and
390 -- not assigned when the only reference is from the pragma.
391
392 if Nkind (N) = N_Identifier then
393 P := Parent (N);
394
395 if Nkind (P) = N_Pragma_Argument_Association then
396 P := Parent (P);
397
398 if Nkind (P) = N_Pragma then
399 if Pragma_Name (P) = Name_Warnings
400 or else
401 Pragma_Name (P) = Name_Unmodified
402 or else
403 Pragma_Name (P) = Name_Unreferenced
404 then
405 return False;
406 end if;
407 end if;
408 end if;
409 end if;
410
411 return True;
412 end OK_To_Set_Referenced;
413
414 -- Start of processing for Generate_Reference
415
416 begin
417 pragma Assert (Nkind (E) in N_Entity);
418 Find_Actual (N, Formal, Call);
419
420 if Present (Formal) then
421 Kind := Ekind (Formal);
422 else
423 Kind := E_Void;
424 end if;
425
426 -- Check for obsolescent reference to package ASCII. GNAT treats this
427 -- element of annex J specially since in practice, programs make a lot
428 -- of use of this feature, so we don't include it in the set of features
429 -- diagnosed when Warn_On_Obsolescent_Features mode is set. However we
430 -- are required to note it as a violation of the RM defined restriction.
431
432 if E = Standard_ASCII then
433 Check_Restriction (No_Obsolescent_Features, N);
434 end if;
435
436 -- Check for reference to entity marked with Is_Obsolescent
437
438 -- Note that we always allow obsolescent references in the compiler
439 -- itself and the run time, since we assume that we know what we are
440 -- doing in such cases. For example the calls in Ada.Characters.Handling
441 -- to its own obsolescent subprograms are just fine.
442
443 -- In any case we do not generate warnings within the extended source
444 -- unit of the entity in question, since we assume the source unit
445 -- itself knows what is going on (and for sure we do not want silly
446 -- warnings, e.g. on the end line of an obsolescent procedure body).
447
448 if Is_Obsolescent (E)
449 and then not GNAT_Mode
450 and then not In_Extended_Main_Source_Unit (E)
451 then
452 Check_Restriction (No_Obsolescent_Features, N);
453
454 if Warn_On_Obsolescent_Feature then
455 Output_Obsolescent_Entity_Warnings (N, E);
456 end if;
457 end if;
458
459 -- Warn if reference to Ada 2005 entity not in Ada 2005 mode. We only
460 -- detect real explicit references (modifications and references).
461
462 if Comes_From_Source (N)
463 and then Is_Ada_2005_Only (E)
464 and then Ada_Version < Ada_2005
465 and then Warn_On_Ada_2005_Compatibility
466 and then (Typ = 'm' or else Typ = 'r' or else Typ = 's')
467 then
468 Error_Msg_NE ("& is only defined in Ada 2005?", N, E);
469 end if;
470
471 -- Warn if reference to Ada 2012 entity not in Ada 2012 mode. We only
472 -- detect real explicit references (modifications and references).
473
474 if Comes_From_Source (N)
475 and then Is_Ada_2012_Only (E)
476 and then Ada_Version < Ada_2012
477 and then Warn_On_Ada_2012_Compatibility
478 and then (Typ = 'm' or else Typ = 'r')
479 then
480 Error_Msg_NE ("& is only defined in Ada 2012?", N, E);
481 end if;
482
483 -- Never collect references if not in main source unit. However, we omit
484 -- this test if Typ is 'e' or 'k', since these entries are structural,
485 -- and it is useful to have them in units that reference packages as
486 -- well as units that define packages. We also omit the test for the
487 -- case of 'p' since we want to include inherited primitive operations
488 -- from other packages.
489
490 -- We also omit this test is this is a body reference for a subprogram
491 -- instantiation. In this case the reference is to the generic body,
492 -- which clearly need not be in the main unit containing the instance.
493 -- For the same reason we accept an implicit reference generated for
494 -- a default in an instance.
495
496 if not In_Extended_Main_Source_Unit (N) then
497 if Typ = 'e'
498 or else Typ = 'p'
499 or else Typ = 'i'
500 or else Typ = 'k'
501 or else (Typ = 'b' and then Is_Generic_Instance (E))
502 then
503 null;
504 else
505 return;
506 end if;
507 end if;
508
509 -- For reference type p, the entity must be in main source unit
510
511 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
512 return;
513 end if;
514
515 -- Unless the reference is forced, we ignore references where the
516 -- reference itself does not come from source.
517
518 if not Force and then not Comes_From_Source (N) then
519 return;
520 end if;
521
522 -- Deal with setting entity as referenced, unless suppressed. Note that
523 -- we still do Set_Referenced on entities that do not come from source.
524 -- This situation arises when we have a source reference to a derived
525 -- operation, where the derived operation itself does not come from
526 -- source, but we still want to mark it as referenced, since we really
527 -- are referencing an entity in the corresponding package (this avoids
528 -- wrong complaints that the package contains no referenced entities).
529
530 if Set_Ref then
531
532 -- Assignable object appearing on left side of assignment or as
533 -- an out parameter.
534
535 if Is_Assignable (E)
536 and then Is_On_LHS (N)
537 and then Ekind (E) /= E_In_Out_Parameter
538 then
539 -- For objects that are renamings, just set as simply referenced
540 -- we do not try to do assignment type tracking in this case.
541
542 if Present (Renamed_Object (E)) then
543 Set_Referenced (E);
544
545 -- Out parameter case
546
547 elsif Kind = E_Out_Parameter then
548
549 -- If warning mode for all out parameters is set, or this is
550 -- the only warning parameter, then we want to mark this for
551 -- later warning logic by setting Referenced_As_Out_Parameter
552
553 if Warn_On_Modified_As_Out_Parameter (Formal) then
554 Set_Referenced_As_Out_Parameter (E, True);
555 Set_Referenced_As_LHS (E, False);
556
557 -- For OUT parameter not covered by the above cases, we simply
558 -- regard it as a normal reference (in this case we do not
559 -- want any of the warning machinery for out parameters).
560
561 else
562 Set_Referenced (E);
563 end if;
564
565 -- For the left hand of an assignment case, we do nothing here.
566 -- The processing for Analyze_Assignment_Statement will set the
567 -- Referenced_As_LHS flag.
568
569 else
570 null;
571 end if;
572
573 -- Check for a reference in a pragma that should not count as a
574 -- making the variable referenced for warning purposes.
575
576 elsif Is_Non_Significant_Pragma_Reference (N) then
577 null;
578
579 -- A reference in an attribute definition clause does not count as a
580 -- reference except for the case of Address. The reason that 'Address
581 -- is an exception is that it creates an alias through which the
582 -- variable may be referenced.
583
584 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
585 and then Chars (Parent (N)) /= Name_Address
586 and then N = Name (Parent (N))
587 then
588 null;
589
590 -- Constant completion does not count as a reference
591
592 elsif Typ = 'c'
593 and then Ekind (E) = E_Constant
594 then
595 null;
596
597 -- Record representation clause does not count as a reference
598
599 elsif Nkind (N) = N_Identifier
600 and then Nkind (Parent (N)) = N_Record_Representation_Clause
601 then
602 null;
603
604 -- Discriminants do not need to produce a reference to record type
605
606 elsif Typ = 'd'
607 and then Nkind (Parent (N)) = N_Discriminant_Specification
608 then
609 null;
610
611 -- All other cases
612
613 else
614 -- Special processing for IN OUT parameters, where we have an
615 -- implicit assignment to a simple variable.
616
617 if Kind = E_In_Out_Parameter
618 and then Is_Assignable (E)
619 then
620 -- For sure this counts as a normal read reference
621
622 Set_Referenced (E);
623 Set_Last_Assignment (E, Empty);
624
625 -- We count it as being referenced as an out parameter if the
626 -- option is set to warn on all out parameters, except that we
627 -- have a special exclusion for an intrinsic subprogram, which
628 -- is most likely an instantiation of Unchecked_Deallocation
629 -- which we do not want to consider as an assignment since it
630 -- generates false positives. We also exclude the case of an
631 -- IN OUT parameter if the name of the procedure is Free,
632 -- since we suspect similar semantics.
633
634 if Warn_On_All_Unread_Out_Parameters
635 and then Is_Entity_Name (Name (Call))
636 and then not Is_Intrinsic_Subprogram (Entity (Name (Call)))
637 and then Chars (Name (Call)) /= Name_Free
638 then
639 Set_Referenced_As_Out_Parameter (E, True);
640 Set_Referenced_As_LHS (E, False);
641 end if;
642
643 -- Don't count a recursive reference within a subprogram as a
644 -- reference (that allows detection of a recursive subprogram
645 -- whose only references are recursive calls as unreferenced).
646
647 elsif Is_Subprogram (E)
648 and then E = Nearest_Dynamic_Scope (Current_Scope)
649 then
650 null;
651
652 -- Any other occurrence counts as referencing the entity
653
654 elsif OK_To_Set_Referenced then
655 Set_Referenced (E);
656
657 -- If variable, this is an OK reference after an assignment
658 -- so we can clear the Last_Assignment indication.
659
660 if Is_Assignable (E) then
661 Set_Last_Assignment (E, Empty);
662 end if;
663 end if;
664 end if;
665
666 -- Check for pragma Unreferenced given and reference is within
667 -- this source unit (occasion for possible warning to be issued).
668
669 if Has_Unreferenced (E)
670 and then In_Same_Extended_Unit (E, N)
671 then
672 -- A reference as a named parameter in a call does not count
673 -- as a violation of pragma Unreferenced for this purpose...
674
675 if Nkind (N) = N_Identifier
676 and then Nkind (Parent (N)) = N_Parameter_Association
677 and then Selector_Name (Parent (N)) = N
678 then
679 null;
680
681 -- ... Neither does a reference to a variable on the left side
682 -- of an assignment.
683
684 elsif Is_On_LHS (N) then
685 null;
686
687 -- For entry formals, we want to place the warning message on the
688 -- corresponding entity in the accept statement. The current scope
689 -- is the body of the accept, so we find the formal whose name
690 -- matches that of the entry formal (there is no link between the
691 -- two entities, and the one in the accept statement is only used
692 -- for conformance checking).
693
694 elsif Ekind (Scope (E)) = E_Entry then
695 declare
696 BE : Entity_Id;
697
698 begin
699 BE := First_Entity (Current_Scope);
700 while Present (BE) loop
701 if Chars (BE) = Chars (E) then
702 Error_Msg_NE -- CODEFIX
703 ("?pragma Unreferenced given for&!", N, BE);
704 exit;
705 end if;
706
707 Next_Entity (BE);
708 end loop;
709 end;
710
711 -- Here we issue the warning, since this is a real reference
712
713 else
714 Error_Msg_NE -- CODEFIX
715 ("?pragma Unreferenced given for&!", N, E);
716 end if;
717 end if;
718
719 -- If this is a subprogram instance, mark as well the internal
720 -- subprogram in the wrapper package, which may be a visible
721 -- compilation unit.
722
723 if Is_Overloadable (E)
724 and then Is_Generic_Instance (E)
725 and then Present (Alias (E))
726 then
727 Set_Referenced (Alias (E));
728 end if;
729 end if;
730
731 -- Generate reference if all conditions are met:
732
733 if
734 -- Cross referencing must be active
735
736 Opt.Xref_Active
737
738 -- The entity must be one for which we collect references
739
740 and then Xref_Entity_Letters (Ekind (E)) /= ' '
741
742 -- Both Sloc values must be set to something sensible
743
744 and then Sloc (E) > No_Location
745 and then Sloc (N) > No_Location
746
747 -- We ignore references from within an instance, except for default
748 -- subprograms, for which we generate an implicit reference.
749
750 and then
751 (Instantiation_Location (Sloc (N)) = No_Location or else Typ = 'i')
752
753 -- Ignore dummy references
754
755 and then Typ /= ' '
756 then
757 if Nkind (N) = N_Identifier
758 or else
759 Nkind (N) = N_Defining_Identifier
760 or else
761 Nkind (N) in N_Op
762 or else
763 Nkind (N) = N_Defining_Operator_Symbol
764 or else
765 Nkind (N) = N_Operator_Symbol
766 or else
767 (Nkind (N) = N_Character_Literal
768 and then Sloc (Entity (N)) /= Standard_Location)
769 or else
770 Nkind (N) = N_Defining_Character_Literal
771 then
772 Nod := N;
773
774 elsif Nkind (N) = N_Expanded_Name
775 or else
776 Nkind (N) = N_Selected_Component
777 then
778 Nod := Selector_Name (N);
779
780 else
781 return;
782 end if;
783
784 -- Normal case of source entity comes from source
785
786 if Comes_From_Source (E) then
787 Ent := E;
788
789 -- Entity does not come from source, but is a derived subprogram and
790 -- the derived subprogram comes from source (after one or more
791 -- derivations) in which case the reference is to parent subprogram.
792
793 elsif Is_Overloadable (E)
794 and then Present (Alias (E))
795 then
796 Ent := Alias (E);
797 while not Comes_From_Source (Ent) loop
798 if No (Alias (Ent)) then
799 return;
800 end if;
801
802 Ent := Alias (Ent);
803 end loop;
804
805 -- The internally created defining entity for a child subprogram
806 -- that has no previous spec has valid references.
807
808 elsif Is_Overloadable (E)
809 and then Is_Child_Unit (E)
810 then
811 Ent := E;
812
813 -- Record components of discriminated subtypes or derived types must
814 -- be treated as references to the original component.
815
816 elsif Ekind (E) = E_Component
817 and then Comes_From_Source (Original_Record_Component (E))
818 then
819 Ent := Original_Record_Component (E);
820
821 -- If this is an expanded reference to a discriminant, recover the
822 -- original discriminant, which gets the reference.
823
824 elsif Ekind (E) = E_In_Parameter
825 and then Present (Discriminal_Link (E))
826 then
827 Ent := Discriminal_Link (E);
828 Set_Referenced (Ent);
829
830 -- Ignore reference to any other entity that is not from source
831
832 else
833 return;
834 end if;
835
836 -- Record reference to entity
837
838 Ref := Original_Location (Sloc (Nod));
839 Def := Original_Location (Sloc (Ent));
840
841 Xrefs.Increment_Last;
842 Indx := Xrefs.Last;
843
844 Xrefs.Table (Indx).Loc := Ref;
845
846 -- Overriding operations are marked with 'P'
847
848 if Typ = 'p'
849 and then Is_Subprogram (N)
850 and then Present (Overridden_Operation (N))
851 then
852 Xrefs.Table (Indx).Typ := 'P';
853 else
854 Xrefs.Table (Indx).Typ := Typ;
855 end if;
856
857 Xrefs.Table (Indx).Eun := Get_Source_Unit (Def);
858 Xrefs.Table (Indx).Lun := Get_Source_Unit (Ref);
859 Xrefs.Table (Indx).Ent := Ent;
860 Set_Has_Xref_Entry (Ent);
861 end if;
862 end Generate_Reference;
863
864 -----------------------------------
865 -- Generate_Reference_To_Formals --
866 -----------------------------------
867
868 procedure Generate_Reference_To_Formals (E : Entity_Id) is
869 Formal : Entity_Id;
870
871 begin
872 if Is_Generic_Subprogram (E) then
873 Formal := First_Entity (E);
874
875 while Present (Formal)
876 and then not Is_Formal (Formal)
877 loop
878 Next_Entity (Formal);
879 end loop;
880
881 else
882 Formal := First_Formal (E);
883 end if;
884
885 while Present (Formal) loop
886 if Ekind (Formal) = E_In_Parameter then
887
888 if Nkind (Parameter_Type (Parent (Formal)))
889 = N_Access_Definition
890 then
891 Generate_Reference (E, Formal, '^', False);
892 else
893 Generate_Reference (E, Formal, '>', False);
894 end if;
895
896 elsif Ekind (Formal) = E_In_Out_Parameter then
897 Generate_Reference (E, Formal, '=', False);
898
899 else
900 Generate_Reference (E, Formal, '<', False);
901 end if;
902
903 Next_Formal (Formal);
904 end loop;
905 end Generate_Reference_To_Formals;
906
907 -------------------------------------------
908 -- Generate_Reference_To_Generic_Formals --
909 -------------------------------------------
910
911 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
912 Formal : Entity_Id;
913
914 begin
915 Formal := First_Entity (E);
916 while Present (Formal) loop
917 if Comes_From_Source (Formal) then
918 Generate_Reference (E, Formal, 'z', False);
919 end if;
920
921 Next_Entity (Formal);
922 end loop;
923 end Generate_Reference_To_Generic_Formals;
924
925 ----------------
926 -- Initialize --
927 ----------------
928
929 procedure Initialize is
930 begin
931 Xrefs.Init;
932 end Initialize;
933
934 -----------------------
935 -- Output_References --
936 -----------------------
937
938 procedure Output_References is
939
940 procedure Get_Type_Reference
941 (Ent : Entity_Id;
942 Tref : out Entity_Id;
943 Left : out Character;
944 Right : out Character);
945 -- Given an Entity_Id Ent, determines whether a type reference is
946 -- required. If so, Tref is set to the entity for the type reference
947 -- and Left and Right are set to the left/right brackets to be output
948 -- for the reference. If no type reference is required, then Tref is
949 -- set to Empty, and Left/Right are set to space.
950
951 procedure Output_Import_Export_Info (Ent : Entity_Id);
952 -- Output language and external name information for an interfaced
953 -- entity, using the format <language, external_name>,
954
955 ------------------------
956 -- Get_Type_Reference --
957 ------------------------
958
959 procedure Get_Type_Reference
960 (Ent : Entity_Id;
961 Tref : out Entity_Id;
962 Left : out Character;
963 Right : out Character)
964 is
965 Sav : Entity_Id;
966
967 begin
968 -- See if we have a type reference
969
970 Tref := Ent;
971 Left := '{';
972 Right := '}';
973
974 loop
975 Sav := Tref;
976
977 -- Processing for types
978
979 if Is_Type (Tref) then
980
981 -- Case of base type
982
983 if Base_Type (Tref) = Tref then
984
985 -- If derived, then get first subtype
986
987 if Tref /= Etype (Tref) then
988 Tref := First_Subtype (Etype (Tref));
989
990 -- Set brackets for derived type, but don't override
991 -- pointer case since the fact that something is a
992 -- pointer is more important.
993
994 if Left /= '(' then
995 Left := '<';
996 Right := '>';
997 end if;
998
999 -- If non-derived ptr, get directly designated type.
1000 -- If the type has a full view, all references are on the
1001 -- partial view, that is seen first.
1002
1003 elsif Is_Access_Type (Tref) then
1004 Tref := Directly_Designated_Type (Tref);
1005 Left := '(';
1006 Right := ')';
1007
1008 elsif Is_Private_Type (Tref)
1009 and then Present (Full_View (Tref))
1010 then
1011 if Is_Access_Type (Full_View (Tref)) then
1012 Tref := Directly_Designated_Type (Full_View (Tref));
1013 Left := '(';
1014 Right := ')';
1015
1016 -- If the full view is an array type, we also retrieve
1017 -- the corresponding component type, because the ali
1018 -- entry already indicates that this is an array.
1019
1020 elsif Is_Array_Type (Full_View (Tref)) then
1021 Tref := Component_Type (Full_View (Tref));
1022 Left := '(';
1023 Right := ')';
1024 end if;
1025
1026 -- If non-derived array, get component type. Skip component
1027 -- type for case of String or Wide_String, saves worthwhile
1028 -- space.
1029
1030 elsif Is_Array_Type (Tref)
1031 and then Tref /= Standard_String
1032 and then Tref /= Standard_Wide_String
1033 then
1034 Tref := Component_Type (Tref);
1035 Left := '(';
1036 Right := ')';
1037
1038 -- For other non-derived base types, nothing
1039
1040 else
1041 exit;
1042 end if;
1043
1044 -- For a subtype, go to ancestor subtype
1045
1046 else
1047 Tref := Ancestor_Subtype (Tref);
1048
1049 -- If no ancestor subtype, go to base type
1050
1051 if No (Tref) then
1052 Tref := Base_Type (Sav);
1053 end if;
1054 end if;
1055
1056 -- For objects, functions, enum literals, just get type from
1057 -- Etype field.
1058
1059 elsif Is_Object (Tref)
1060 or else Ekind (Tref) = E_Enumeration_Literal
1061 or else Ekind (Tref) = E_Function
1062 or else Ekind (Tref) = E_Operator
1063 then
1064 Tref := Etype (Tref);
1065
1066 -- For anything else, exit
1067
1068 else
1069 exit;
1070 end if;
1071
1072 -- Exit if no type reference, or we are stuck in some loop trying
1073 -- to find the type reference, or if the type is standard void
1074 -- type (the latter is an implementation artifact that should not
1075 -- show up in the generated cross-references).
1076
1077 exit when No (Tref)
1078 or else Tref = Sav
1079 or else Tref = Standard_Void_Type;
1080
1081 -- If we have a usable type reference, return, otherwise keep
1082 -- looking for something useful (we are looking for something
1083 -- that either comes from source or standard)
1084
1085 if Sloc (Tref) = Standard_Location
1086 or else Comes_From_Source (Tref)
1087 then
1088 -- If the reference is a subtype created for a generic actual,
1089 -- go actual directly, the inner subtype is not user visible.
1090
1091 if Nkind (Parent (Tref)) = N_Subtype_Declaration
1092 and then not Comes_From_Source (Parent (Tref))
1093 and then
1094 (Is_Wrapper_Package (Scope (Tref))
1095 or else Is_Generic_Instance (Scope (Tref)))
1096 then
1097 Tref := First_Subtype (Base_Type (Tref));
1098 end if;
1099
1100 return;
1101 end if;
1102 end loop;
1103
1104 -- If we fall through the loop, no type reference
1105
1106 Tref := Empty;
1107 Left := ' ';
1108 Right := ' ';
1109 end Get_Type_Reference;
1110
1111 -------------------------------
1112 -- Output_Import_Export_Info --
1113 -------------------------------
1114
1115 procedure Output_Import_Export_Info (Ent : Entity_Id) is
1116 Language_Name : Name_Id;
1117 Conv : constant Convention_Id := Convention (Ent);
1118
1119 begin
1120 -- Generate language name from convention
1121
1122 if Conv = Convention_C then
1123 Language_Name := Name_C;
1124
1125 elsif Conv = Convention_CPP then
1126 Language_Name := Name_CPP;
1127
1128 elsif Conv = Convention_Ada then
1129 Language_Name := Name_Ada;
1130
1131 else
1132 -- For the moment we ignore all other cases ???
1133
1134 return;
1135 end if;
1136
1137 Write_Info_Char ('<');
1138 Get_Unqualified_Name_String (Language_Name);
1139
1140 for J in 1 .. Name_Len loop
1141 Write_Info_Char (Name_Buffer (J));
1142 end loop;
1143
1144 if Present (Interface_Name (Ent)) then
1145 Write_Info_Char (',');
1146 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
1147
1148 for J in 1 .. Name_Len loop
1149 Write_Info_Char (Name_Buffer (J));
1150 end loop;
1151 end if;
1152
1153 Write_Info_Char ('>');
1154 end Output_Import_Export_Info;
1155
1156 -- Start of processing for Output_References
1157
1158 begin
1159 if not Opt.Xref_Active then
1160 return;
1161 end if;
1162
1163 -- First we add references to the primitive operations of tagged
1164 -- types declared in the main unit.
1165
1166 Handle_Prim_Ops : declare
1167 Ent : Entity_Id;
1168
1169 begin
1170 for J in 1 .. Xrefs.Last loop
1171 Ent := Xrefs.Table (J).Ent;
1172
1173 if Is_Type (Ent)
1174 and then Is_Tagged_Type (Ent)
1175 and then Is_Base_Type (Ent)
1176 and then In_Extended_Main_Source_Unit (Ent)
1177 then
1178 Generate_Prim_Op_References (Ent);
1179 end if;
1180 end loop;
1181 end Handle_Prim_Ops;
1182
1183 -- Before we go ahead and output the references we have a problem
1184 -- that needs dealing with. So far we have captured things that are
1185 -- definitely referenced by the main unit, or defined in the main
1186 -- unit. That's because we don't want to clutter up the ali file
1187 -- for this unit with definition lines for entities in other units
1188 -- that are not referenced.
1189
1190 -- But there is a glitch. We may reference an entity in another unit,
1191 -- and it may have a type reference to an entity that is not directly
1192 -- referenced in the main unit, which may mean that there is no xref
1193 -- entry for this entity yet in the list of references.
1194
1195 -- If we don't do something about this, we will end with an orphan type
1196 -- reference, i.e. it will point to an entity that does not appear
1197 -- within the generated references in the ali file. That is not good for
1198 -- tools using the xref information.
1199
1200 -- To fix this, we go through the references adding definition entries
1201 -- for any unreferenced entities that can be referenced in a type
1202 -- reference. There is a recursion problem here, and that is dealt with
1203 -- by making sure that this traversal also traverses any entries that
1204 -- get added by the traversal.
1205
1206 Handle_Orphan_Type_References : declare
1207 J : Nat;
1208 Tref : Entity_Id;
1209 Indx : Nat;
1210 Ent : Entity_Id;
1211 Loc : Source_Ptr;
1212
1213 L, R : Character;
1214 pragma Warnings (Off, L);
1215 pragma Warnings (Off, R);
1216
1217 procedure New_Entry (E : Entity_Id);
1218 -- Make an additional entry into the Xref table for a type entity
1219 -- that is related to the current entity (parent, type ancestor,
1220 -- progenitor, etc.).
1221
1222 ----------------
1223 -- New_Entry --
1224 ----------------
1225
1226 procedure New_Entry (E : Entity_Id) is
1227 begin
1228 if Present (E)
1229 and then not Has_Xref_Entry (E)
1230 and then Sloc (E) > No_Location
1231 then
1232 Xrefs.Increment_Last;
1233 Indx := Xrefs.Last;
1234 Loc := Original_Location (Sloc (E));
1235 Xrefs.Table (Indx).Ent := E;
1236 Xrefs.Table (Indx).Loc := No_Location;
1237 Xrefs.Table (Indx).Eun := Get_Source_Unit (Loc);
1238 Xrefs.Table (Indx).Lun := No_Unit;
1239 Set_Has_Xref_Entry (E);
1240 end if;
1241 end New_Entry;
1242
1243 -- Start of processing for Handle_Orphan_Type_References
1244
1245 begin
1246 -- Note that this is not a for loop for a very good reason. The
1247 -- processing of items in the table can add new items to the table,
1248 -- and they must be processed as well.
1249
1250 J := 1;
1251 while J <= Xrefs.Last loop
1252 Ent := Xrefs.Table (J).Ent;
1253 Get_Type_Reference (Ent, Tref, L, R);
1254
1255 if Present (Tref)
1256 and then not Has_Xref_Entry (Tref)
1257 and then Sloc (Tref) > No_Location
1258 then
1259 New_Entry (Tref);
1260
1261 if Is_Record_Type (Ent)
1262 and then Present (Interfaces (Ent))
1263 then
1264 -- Add an entry for each one of the given interfaces
1265 -- implemented by type Ent.
1266
1267 declare
1268 Elmt : Elmt_Id := First_Elmt (Interfaces (Ent));
1269 begin
1270 while Present (Elmt) loop
1271 New_Entry (Node (Elmt));
1272 Next_Elmt (Elmt);
1273 end loop;
1274 end;
1275 end if;
1276 end if;
1277
1278 -- Collect inherited primitive operations that may be declared in
1279 -- another unit and have no visible reference in the current one.
1280
1281 if Is_Type (Ent)
1282 and then Is_Tagged_Type (Ent)
1283 and then Is_Derived_Type (Ent)
1284 and then Is_Base_Type (Ent)
1285 and then In_Extended_Main_Source_Unit (Ent)
1286 then
1287 declare
1288 Op_List : constant Elist_Id := Primitive_Operations (Ent);
1289 Op : Elmt_Id;
1290 Prim : Entity_Id;
1291
1292 function Parent_Op (E : Entity_Id) return Entity_Id;
1293 -- Find original operation, which may be inherited through
1294 -- several derivations.
1295
1296 function Parent_Op (E : Entity_Id) return Entity_Id is
1297 Orig_Op : constant Entity_Id := Alias (E);
1298
1299 begin
1300 if No (Orig_Op) then
1301 return Empty;
1302
1303 elsif not Comes_From_Source (E)
1304 and then not Has_Xref_Entry (Orig_Op)
1305 and then Comes_From_Source (Orig_Op)
1306 then
1307 return Orig_Op;
1308 else
1309 return Parent_Op (Orig_Op);
1310 end if;
1311 end Parent_Op;
1312
1313 begin
1314 Op := First_Elmt (Op_List);
1315 while Present (Op) loop
1316 Prim := Parent_Op (Node (Op));
1317
1318 if Present (Prim) then
1319 Xrefs.Increment_Last;
1320 Indx := Xrefs.Last;
1321 Loc := Original_Location (Sloc (Prim));
1322 Xrefs.Table (Indx).Ent := Prim;
1323 Xrefs.Table (Indx).Loc := No_Location;
1324 Xrefs.Table (Indx).Eun :=
1325 Get_Source_Unit (Sloc (Prim));
1326 Xrefs.Table (Indx).Lun := No_Unit;
1327 Set_Has_Xref_Entry (Prim);
1328 end if;
1329
1330 Next_Elmt (Op);
1331 end loop;
1332 end;
1333 end if;
1334
1335 J := J + 1;
1336 end loop;
1337 end Handle_Orphan_Type_References;
1338
1339 -- Now we have all the references, including those for any embedded
1340 -- type references, so we can sort them, and output them.
1341
1342 Output_Refs : declare
1343
1344 Nrefs : Nat := Xrefs.Last;
1345 -- Number of references in table. This value may get reset (reduced)
1346 -- when we eliminate duplicate reference entries.
1347
1348 Rnums : array (0 .. Nrefs) of Nat;
1349 -- This array contains numbers of references in the Xrefs table.
1350 -- This list is sorted in output order. The extra 0'th entry is
1351 -- convenient for the call to sort. When we sort the table, we
1352 -- move the entries in Rnums around, but we do not move the
1353 -- original table entries.
1354
1355 Curxu : Unit_Number_Type;
1356 -- Current xref unit
1357
1358 Curru : Unit_Number_Type;
1359 -- Current reference unit for one entity
1360
1361 Cursrc : Source_Buffer_Ptr;
1362 -- Current xref unit source text
1363
1364 Curent : Entity_Id;
1365 -- Current entity
1366
1367 Curnam : String (1 .. Name_Buffer'Length);
1368 Curlen : Natural;
1369 -- Simple name and length of current entity
1370
1371 Curdef : Source_Ptr;
1372 -- Original source location for current entity
1373
1374 Crloc : Source_Ptr;
1375 -- Current reference location
1376
1377 Ctyp : Character;
1378 -- Entity type character
1379
1380 Prevt : Character;
1381 -- reference kind of previous reference
1382
1383 Tref : Entity_Id;
1384 -- Type reference
1385
1386 Rref : Node_Id;
1387 -- Renaming reference
1388
1389 Trunit : Unit_Number_Type;
1390 -- Unit number for type reference
1391
1392 function Lt (Op1, Op2 : Natural) return Boolean;
1393 -- Comparison function for Sort call
1394
1395 function Name_Change (X : Entity_Id) return Boolean;
1396 -- Determines if entity X has a different simple name from Curent
1397
1398 procedure Move (From : Natural; To : Natural);
1399 -- Move procedure for Sort call
1400
1401 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
1402
1403 --------
1404 -- Lt --
1405 --------
1406
1407 function Lt (Op1, Op2 : Natural) return Boolean is
1408 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
1409 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
1410
1411 begin
1412 -- First test: if entity is in different unit, sort by unit
1413
1414 if T1.Eun /= T2.Eun then
1415 return Dependency_Num (T1.Eun) < Dependency_Num (T2.Eun);
1416
1417 -- Second test: within same unit, sort by entity Sloc
1418
1419 elsif T1.Def /= T2.Def then
1420 return T1.Def < T2.Def;
1421
1422 -- Third test: sort definitions ahead of references
1423
1424 elsif T1.Loc = No_Location then
1425 return True;
1426
1427 elsif T2.Loc = No_Location then
1428 return False;
1429
1430 -- Fourth test: for same entity, sort by reference location unit
1431
1432 elsif T1.Lun /= T2.Lun then
1433 return Dependency_Num (T1.Lun) < Dependency_Num (T2.Lun);
1434
1435 -- Fifth test: order of location within referencing unit
1436
1437 elsif T1.Loc /= T2.Loc then
1438 return T1.Loc < T2.Loc;
1439
1440 -- Finally, for two locations at the same address, we prefer
1441 -- the one that does NOT have the type 'r' so that a modification
1442 -- or extension takes preference, when there are more than one
1443 -- reference at the same location. As a result, in the case of
1444 -- entities that are in-out actuals, the read reference follows
1445 -- the modify reference.
1446
1447 else
1448 return T2.Typ = 'r';
1449 end if;
1450 end Lt;
1451
1452 ----------
1453 -- Move --
1454 ----------
1455
1456 procedure Move (From : Natural; To : Natural) is
1457 begin
1458 Rnums (Nat (To)) := Rnums (Nat (From));
1459 end Move;
1460
1461 -----------------
1462 -- Name_Change --
1463 -----------------
1464
1465 -- Why a string comparison here??? Why not compare Name_Id values???
1466
1467 function Name_Change (X : Entity_Id) return Boolean is
1468 begin
1469 Get_Unqualified_Name_String (Chars (X));
1470
1471 if Name_Len /= Curlen then
1472 return True;
1473 else
1474 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
1475 end if;
1476 end Name_Change;
1477
1478 -- Start of processing for Output_Refs
1479
1480 begin
1481 -- Capture the definition Sloc values. We delay doing this till now,
1482 -- since at the time the reference or definition is made, private
1483 -- types may be swapped, and the Sloc value may be incorrect. We
1484 -- also set up the pointer vector for the sort.
1485
1486 for J in 1 .. Nrefs loop
1487 Rnums (J) := J;
1488 Xrefs.Table (J).Def :=
1489 Original_Location (Sloc (Xrefs.Table (J).Ent));
1490 end loop;
1491
1492 -- Sort the references
1493
1494 Sorting.Sort (Integer (Nrefs));
1495
1496 -- Eliminate duplicate entries
1497
1498 declare
1499 NR : constant Nat := Nrefs;
1500
1501 begin
1502 -- We need this test for NR because if we force ALI file
1503 -- generation in case of errors detected, it may be the case
1504 -- that Nrefs is 0, so we should not reset it here
1505
1506 if NR >= 2 then
1507 Nrefs := 1;
1508
1509 for J in 2 .. NR loop
1510 if Xrefs.Table (Rnums (J)) /=
1511 Xrefs.Table (Rnums (Nrefs))
1512 then
1513 Nrefs := Nrefs + 1;
1514 Rnums (Nrefs) := Rnums (J);
1515 end if;
1516 end loop;
1517 end if;
1518 end;
1519
1520 -- Initialize loop through references
1521
1522 Curxu := No_Unit;
1523 Curent := Empty;
1524 Curdef := No_Location;
1525 Curru := No_Unit;
1526 Crloc := No_Location;
1527 Prevt := 'm';
1528
1529 -- Loop to output references
1530
1531 for Refno in 1 .. Nrefs loop
1532 Output_One_Ref : declare
1533 P2 : Source_Ptr;
1534 Ent : Entity_Id;
1535
1536 WC : Char_Code;
1537 Err : Boolean;
1538 pragma Warnings (Off, WC);
1539 pragma Warnings (Off, Err);
1540
1541 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
1542 -- The current entry to be accessed
1543
1544 P : Source_Ptr;
1545 -- Used to index into source buffer to get entity name
1546
1547 Left : Character;
1548 Right : Character;
1549 -- Used for {} or <> or () for type reference
1550
1551 procedure Check_Type_Reference
1552 (Ent : Entity_Id;
1553 List_Interface : Boolean);
1554 -- Find whether there is a meaningful type reference for
1555 -- Ent, and display it accordingly. If List_Interface is
1556 -- true, then Ent is a progenitor interface of the current
1557 -- type entity being listed. In that case list it as is,
1558 -- without looking for a type reference for it.
1559
1560 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
1561 -- Recursive procedure to output instantiation references for
1562 -- the given source ptr in [file|line[...]] form. No output
1563 -- if the given location is not a generic template reference.
1564
1565 procedure Output_Overridden_Op (Old_E : Entity_Id);
1566 -- For a subprogram that is overriding, display information
1567 -- about the inherited operation that it overrides.
1568
1569 --------------------------
1570 -- Check_Type_Reference --
1571 --------------------------
1572
1573 procedure Check_Type_Reference
1574 (Ent : Entity_Id;
1575 List_Interface : Boolean)
1576 is
1577 begin
1578 if List_Interface then
1579
1580 -- This is a progenitor interface of the type for which
1581 -- xref information is being generated.
1582
1583 Tref := Ent;
1584 Left := '<';
1585 Right := '>';
1586
1587 else
1588 Get_Type_Reference (Ent, Tref, Left, Right);
1589 end if;
1590
1591 if Present (Tref) then
1592
1593 -- Case of standard entity, output name
1594
1595 if Sloc (Tref) = Standard_Location then
1596 Write_Info_Char (Left);
1597 Write_Info_Name (Chars (Tref));
1598 Write_Info_Char (Right);
1599
1600 -- Case of source entity, output location
1601
1602 else
1603 Write_Info_Char (Left);
1604 Trunit := Get_Source_Unit (Sloc (Tref));
1605
1606 if Trunit /= Curxu then
1607 Write_Info_Nat (Dependency_Num (Trunit));
1608 Write_Info_Char ('|');
1609 end if;
1610
1611 Write_Info_Nat
1612 (Int (Get_Logical_Line_Number (Sloc (Tref))));
1613
1614 declare
1615 Ent : Entity_Id;
1616 Ctyp : Character;
1617
1618 begin
1619 Ent := Tref;
1620 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1621
1622 if Ctyp = '+'
1623 and then Present (Full_View (Ent))
1624 then
1625 Ent := Underlying_Type (Ent);
1626
1627 if Present (Ent) then
1628 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1629 end if;
1630 end if;
1631
1632 Write_Info_Char (Ctyp);
1633 end;
1634
1635 Write_Info_Nat
1636 (Int (Get_Column_Number (Sloc (Tref))));
1637
1638 -- If the type comes from an instantiation, add the
1639 -- corresponding info.
1640
1641 Output_Instantiation_Refs (Sloc (Tref));
1642 Write_Info_Char (Right);
1643 end if;
1644 end if;
1645 end Check_Type_Reference;
1646
1647 -------------------------------
1648 -- Output_Instantiation_Refs --
1649 -------------------------------
1650
1651 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
1652 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
1653 Lun : Unit_Number_Type;
1654 Cu : constant Unit_Number_Type := Curru;
1655
1656 begin
1657 -- Nothing to do if this is not an instantiation
1658
1659 if Iloc = No_Location then
1660 return;
1661 end if;
1662
1663 -- Output instantiation reference
1664
1665 Write_Info_Char ('[');
1666 Lun := Get_Source_Unit (Iloc);
1667
1668 if Lun /= Curru then
1669 Curru := Lun;
1670 Write_Info_Nat (Dependency_Num (Curru));
1671 Write_Info_Char ('|');
1672 end if;
1673
1674 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
1675
1676 -- Recursive call to get nested instantiations
1677
1678 Output_Instantiation_Refs (Iloc);
1679
1680 -- Output final ] after call to get proper nesting
1681
1682 Write_Info_Char (']');
1683 Curru := Cu;
1684 return;
1685 end Output_Instantiation_Refs;
1686
1687 --------------------------
1688 -- Output_Overridden_Op --
1689 --------------------------
1690
1691 procedure Output_Overridden_Op (Old_E : Entity_Id) is
1692 Op : Entity_Id;
1693
1694 begin
1695 -- The overridden operation has an implicit declaration
1696 -- at the point of derivation. What we want to display
1697 -- is the original operation, which has the actual body
1698 -- (or abstract declaration) that is being overridden.
1699 -- The overridden operation is not always set, e.g. when
1700 -- it is a predefined operator.
1701
1702 if No (Old_E) then
1703 return;
1704
1705 -- Follow alias chain if one is present
1706
1707 elsif Present (Alias (Old_E)) then
1708
1709 -- The subprogram may have been implicitly inherited
1710 -- through several levels of derivation, so find the
1711 -- ultimate (source) ancestor.
1712
1713 Op := Ultimate_Alias (Old_E);
1714
1715 -- Normal case of no alias present
1716
1717 else
1718 Op := Old_E;
1719 end if;
1720
1721 if Present (Op)
1722 and then Sloc (Op) /= Standard_Location
1723 then
1724 declare
1725 Loc : constant Source_Ptr := Sloc (Op);
1726 Par_Unit : constant Unit_Number_Type :=
1727 Get_Source_Unit (Loc);
1728
1729 begin
1730 Write_Info_Char ('<');
1731
1732 if Par_Unit /= Curxu then
1733 Write_Info_Nat (Dependency_Num (Par_Unit));
1734 Write_Info_Char ('|');
1735 end if;
1736
1737 Write_Info_Nat (Int (Get_Logical_Line_Number (Loc)));
1738 Write_Info_Char ('p');
1739 Write_Info_Nat (Int (Get_Column_Number (Loc)));
1740 Write_Info_Char ('>');
1741 end;
1742 end if;
1743 end Output_Overridden_Op;
1744
1745 -- Start of processing for Output_One_Ref
1746
1747 begin
1748 Ent := XE.Ent;
1749 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1750
1751 -- Skip reference if it is the only reference to an entity,
1752 -- and it is an END line reference, and the entity is not in
1753 -- the current extended source. This prevents junk entries
1754 -- consisting only of packages with END lines, where no
1755 -- entity from the package is actually referenced.
1756
1757 if XE.Typ = 'e'
1758 and then Ent /= Curent
1759 and then (Refno = Nrefs or else
1760 Ent /= Xrefs.Table (Rnums (Refno + 1)).Ent)
1761 and then
1762 not In_Extended_Main_Source_Unit (Ent)
1763 then
1764 goto Continue;
1765 end if;
1766
1767 -- For private type, get full view type
1768
1769 if Ctyp = '+'
1770 and then Present (Full_View (XE.Ent))
1771 then
1772 Ent := Underlying_Type (Ent);
1773
1774 if Present (Ent) then
1775 Ctyp := Xref_Entity_Letters (Ekind (Ent));
1776 end if;
1777 end if;
1778
1779 -- Special exception for Boolean
1780
1781 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
1782 Ctyp := 'B';
1783 end if;
1784
1785 -- For variable reference, get corresponding type
1786
1787 if Ctyp = '*' then
1788 Ent := Etype (XE.Ent);
1789 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1790
1791 -- If variable is private type, get full view type
1792
1793 if Ctyp = '+'
1794 and then Present (Full_View (Etype (XE.Ent)))
1795 then
1796 Ent := Underlying_Type (Etype (XE.Ent));
1797
1798 if Present (Ent) then
1799 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
1800 end if;
1801
1802 elsif Is_Generic_Type (Ent) then
1803
1804 -- If the type of the entity is a generic private type,
1805 -- there is no usable full view, so retain the indication
1806 -- that this is an object.
1807
1808 Ctyp := '*';
1809 end if;
1810
1811 -- Special handling for access parameters and objects of
1812 -- an anonymous access type.
1813
1814 if Ekind_In (Etype (XE.Ent),
1815 E_Anonymous_Access_Type,
1816 E_Anonymous_Access_Subprogram_Type,
1817 E_Anonymous_Access_Protected_Subprogram_Type)
1818 then
1819 if Is_Formal (XE.Ent)
1820 or else Ekind_In (XE.Ent, E_Variable, E_Constant)
1821 then
1822 Ctyp := 'p';
1823 end if;
1824
1825 -- Special handling for Boolean
1826
1827 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
1828 Ctyp := 'b';
1829 end if;
1830 end if;
1831
1832 -- Special handling for abstract types and operations
1833
1834 if Is_Overloadable (XE.Ent)
1835 and then Is_Abstract_Subprogram (XE.Ent)
1836 then
1837 if Ctyp = 'U' then
1838 Ctyp := 'x'; -- Abstract procedure
1839
1840 elsif Ctyp = 'V' then
1841 Ctyp := 'y'; -- Abstract function
1842 end if;
1843
1844 elsif Is_Type (XE.Ent)
1845 and then Is_Abstract_Type (XE.Ent)
1846 then
1847 if Is_Interface (XE.Ent) then
1848 Ctyp := 'h';
1849
1850 elsif Ctyp = 'R' then
1851 Ctyp := 'H'; -- Abstract type
1852 end if;
1853 end if;
1854
1855 -- Only output reference if interesting type of entity, and
1856 -- suppress self references, except for bodies that act as
1857 -- specs. Also suppress definitions of body formals (we only
1858 -- treat these as references, and the references were
1859 -- separately recorded).
1860
1861 if Ctyp = ' '
1862 or else (XE.Loc = XE.Def
1863 and then
1864 (XE.Typ /= 'b'
1865 or else not Is_Subprogram (XE.Ent)))
1866 or else (Is_Formal (XE.Ent)
1867 and then Present (Spec_Entity (XE.Ent)))
1868 then
1869 null;
1870
1871 else
1872 -- Start new Xref section if new xref unit
1873
1874 if XE.Eun /= Curxu then
1875 if Write_Info_Col > 1 then
1876 Write_Info_EOL;
1877 end if;
1878
1879 Curxu := XE.Eun;
1880 Cursrc := Source_Text (Source_Index (Curxu));
1881
1882 Write_Info_Initiate ('X');
1883 Write_Info_Char (' ');
1884 Write_Info_Nat (Dependency_Num (XE.Eun));
1885 Write_Info_Char (' ');
1886 Write_Info_Name (Reference_Name (Source_Index (XE.Eun)));
1887 end if;
1888
1889 -- Start new Entity line if new entity. Note that we
1890 -- consider two entities the same if they have the same
1891 -- name and source location. This causes entities in
1892 -- instantiations to be treated as though they referred
1893 -- to the template.
1894
1895 if No (Curent)
1896 or else
1897 (XE.Ent /= Curent
1898 and then
1899 (Name_Change (XE.Ent) or else XE.Def /= Curdef))
1900 then
1901 Curent := XE.Ent;
1902 Curdef := XE.Def;
1903
1904 Get_Unqualified_Name_String (Chars (XE.Ent));
1905 Curlen := Name_Len;
1906 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
1907
1908 if Write_Info_Col > 1 then
1909 Write_Info_EOL;
1910 end if;
1911
1912 -- Write column number information
1913
1914 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
1915 Write_Info_Char (Ctyp);
1916 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
1917
1918 -- Write level information
1919
1920 Write_Level_Info : declare
1921 function Is_Visible_Generic_Entity
1922 (E : Entity_Id) return Boolean;
1923 -- Check whether E is declared in the visible part
1924 -- of a generic package. For source navigation
1925 -- purposes, treat this as a visible entity.
1926
1927 function Is_Private_Record_Component
1928 (E : Entity_Id) return Boolean;
1929 -- Check whether E is a non-inherited component of a
1930 -- private extension. Even if the enclosing record is
1931 -- public, we want to treat the component as private
1932 -- for navigation purposes.
1933
1934 ---------------------------------
1935 -- Is_Private_Record_Component --
1936 ---------------------------------
1937
1938 function Is_Private_Record_Component
1939 (E : Entity_Id) return Boolean
1940 is
1941 S : constant Entity_Id := Scope (E);
1942 begin
1943 return
1944 Ekind (E) = E_Component
1945 and then Nkind (Declaration_Node (S)) =
1946 N_Private_Extension_Declaration
1947 and then Original_Record_Component (E) = E;
1948 end Is_Private_Record_Component;
1949
1950 -------------------------------
1951 -- Is_Visible_Generic_Entity --
1952 -------------------------------
1953
1954 function Is_Visible_Generic_Entity
1955 (E : Entity_Id) return Boolean
1956 is
1957 Par : Node_Id;
1958
1959 begin
1960 -- The Present check here is an error defense
1961
1962 if Present (Scope (E))
1963 and then Ekind (Scope (E)) /= E_Generic_Package
1964 then
1965 return False;
1966 end if;
1967
1968 Par := Parent (E);
1969 while Present (Par) loop
1970 if
1971 Nkind (Par) = N_Generic_Package_Declaration
1972 then
1973 -- Entity is a generic formal
1974
1975 return False;
1976
1977 elsif
1978 Nkind (Parent (Par)) = N_Package_Specification
1979 then
1980 return
1981 Is_List_Member (Par)
1982 and then List_Containing (Par) =
1983 Visible_Declarations (Parent (Par));
1984 else
1985 Par := Parent (Par);
1986 end if;
1987 end loop;
1988
1989 return False;
1990 end Is_Visible_Generic_Entity;
1991
1992 -- Start of processing for Write_Level_Info
1993
1994 begin
1995 if Is_Hidden (Curent)
1996 or else Is_Private_Record_Component (Curent)
1997 then
1998 Write_Info_Char (' ');
1999
2000 elsif
2001 Is_Public (Curent)
2002 or else Is_Visible_Generic_Entity (Curent)
2003 then
2004 Write_Info_Char ('*');
2005
2006 else
2007 Write_Info_Char (' ');
2008 end if;
2009 end Write_Level_Info;
2010
2011 -- Output entity name. We use the occurrence from the
2012 -- actual source program at the definition point.
2013
2014 P := Original_Location (Sloc (XE.Ent));
2015
2016 -- Entity is character literal
2017
2018 if Cursrc (P) = ''' then
2019 Write_Info_Char (Cursrc (P));
2020 Write_Info_Char (Cursrc (P + 1));
2021 Write_Info_Char (Cursrc (P + 2));
2022
2023 -- Entity is operator symbol
2024
2025 elsif Cursrc (P) = '"' or else Cursrc (P) = '%' then
2026 Write_Info_Char (Cursrc (P));
2027
2028 P2 := P;
2029 loop
2030 P2 := P2 + 1;
2031 Write_Info_Char (Cursrc (P2));
2032 exit when Cursrc (P2) = Cursrc (P);
2033 end loop;
2034
2035 -- Entity is identifier
2036
2037 else
2038 loop
2039 if Is_Start_Of_Wide_Char (Cursrc, P) then
2040 Scan_Wide (Cursrc, P, WC, Err);
2041 elsif not Identifier_Char (Cursrc (P)) then
2042 exit;
2043 else
2044 P := P + 1;
2045 end if;
2046 end loop;
2047
2048 -- Write out the identifier by copying the exact
2049 -- source characters used in its declaration. Note
2050 -- that this means wide characters will be in their
2051 -- original encoded form.
2052
2053 for J in
2054 Original_Location (Sloc (XE.Ent)) .. P - 1
2055 loop
2056 Write_Info_Char (Cursrc (J));
2057 end loop;
2058 end if;
2059
2060 -- See if we have a renaming reference
2061
2062 if Is_Object (XE.Ent)
2063 and then Present (Renamed_Object (XE.Ent))
2064 then
2065 Rref := Renamed_Object (XE.Ent);
2066
2067 elsif Is_Overloadable (XE.Ent)
2068 and then Nkind (Parent (Declaration_Node (XE.Ent))) =
2069 N_Subprogram_Renaming_Declaration
2070 then
2071 Rref := Name (Parent (Declaration_Node (XE.Ent)));
2072
2073 elsif Ekind (XE.Ent) = E_Package
2074 and then Nkind (Declaration_Node (XE.Ent)) =
2075 N_Package_Renaming_Declaration
2076 then
2077 Rref := Name (Declaration_Node (XE.Ent));
2078
2079 else
2080 Rref := Empty;
2081 end if;
2082
2083 if Present (Rref) then
2084 if Nkind (Rref) = N_Expanded_Name then
2085 Rref := Selector_Name (Rref);
2086 end if;
2087
2088 if Nkind (Rref) = N_Identifier
2089 or else Nkind (Rref) = N_Operator_Symbol
2090 then
2091 null;
2092
2093 -- For renamed array components, use the array name
2094 -- for the renamed entity, which reflect the fact that
2095 -- in general the whole array is aliased.
2096
2097 elsif Nkind (Rref) = N_Indexed_Component then
2098 if Nkind (Prefix (Rref)) = N_Identifier then
2099 Rref := Prefix (Rref);
2100 elsif Nkind (Prefix (Rref)) = N_Expanded_Name then
2101 Rref := Selector_Name (Prefix (Rref));
2102 else
2103 Rref := Empty;
2104 end if;
2105
2106 else
2107 Rref := Empty;
2108 end if;
2109 end if;
2110
2111 -- Write out renaming reference if we have one
2112
2113 if Present (Rref) then
2114 Write_Info_Char ('=');
2115 Write_Info_Nat
2116 (Int (Get_Logical_Line_Number (Sloc (Rref))));
2117 Write_Info_Char (':');
2118 Write_Info_Nat
2119 (Int (Get_Column_Number (Sloc (Rref))));
2120 end if;
2121
2122 -- Indicate that the entity is in the unit of the current
2123 -- xref section.
2124
2125 Curru := Curxu;
2126
2127 -- Write out information about generic parent, if entity
2128 -- is an instance.
2129
2130 if Is_Generic_Instance (XE.Ent) then
2131 declare
2132 Gen_Par : constant Entity_Id :=
2133 Generic_Parent
2134 (Specification
2135 (Unit_Declaration_Node (XE.Ent)));
2136 Loc : constant Source_Ptr := Sloc (Gen_Par);
2137 Gen_U : constant Unit_Number_Type :=
2138 Get_Source_Unit (Loc);
2139
2140 begin
2141 Write_Info_Char ('[');
2142
2143 if Curru /= Gen_U then
2144 Write_Info_Nat (Dependency_Num (Gen_U));
2145 Write_Info_Char ('|');
2146 end if;
2147
2148 Write_Info_Nat
2149 (Int (Get_Logical_Line_Number (Loc)));
2150 Write_Info_Char (']');
2151 end;
2152 end if;
2153
2154 -- See if we have a type reference and if so output
2155
2156 Check_Type_Reference (XE.Ent, False);
2157
2158 -- Additional information for types with progenitors
2159
2160 if Is_Record_Type (XE.Ent)
2161 and then Present (Interfaces (XE.Ent))
2162 then
2163 declare
2164 Elmt : Elmt_Id := First_Elmt (Interfaces (XE.Ent));
2165 begin
2166 while Present (Elmt) loop
2167 Check_Type_Reference (Node (Elmt), True);
2168 Next_Elmt (Elmt);
2169 end loop;
2170 end;
2171
2172 -- For array types, list index types as well. (This is
2173 -- not C, indexes have distinct types).
2174
2175 elsif Is_Array_Type (XE.Ent) then
2176 declare
2177 Indx : Node_Id;
2178 begin
2179 Indx := First_Index (XE.Ent);
2180 while Present (Indx) loop
2181 Check_Type_Reference
2182 (First_Subtype (Etype (Indx)), True);
2183 Next_Index (Indx);
2184 end loop;
2185 end;
2186 end if;
2187
2188 -- If the entity is an overriding operation, write info
2189 -- on operation that was overridden.
2190
2191 if Is_Subprogram (XE.Ent)
2192 and then Present (Overridden_Operation (XE.Ent))
2193 then
2194 Output_Overridden_Op (Overridden_Operation (XE.Ent));
2195 end if;
2196
2197 -- End of processing for entity output
2198
2199 Crloc := No_Location;
2200 end if;
2201
2202 -- Output the reference if it is not as the same location
2203 -- as the previous one, or it is a read-reference that
2204 -- indicates that the entity is an in-out actual in a call.
2205
2206 if XE.Loc /= No_Location
2207 and then
2208 (XE.Loc /= Crloc
2209 or else (Prevt = 'm' and then XE.Typ = 'r'))
2210 then
2211 Crloc := XE.Loc;
2212 Prevt := XE.Typ;
2213
2214 -- Start continuation if line full, else blank
2215
2216 if Write_Info_Col > 72 then
2217 Write_Info_EOL;
2218 Write_Info_Initiate ('.');
2219 end if;
2220
2221 Write_Info_Char (' ');
2222
2223 -- Output file number if changed
2224
2225 if XE.Lun /= Curru then
2226 Curru := XE.Lun;
2227 Write_Info_Nat (Dependency_Num (Curru));
2228 Write_Info_Char ('|');
2229 end if;
2230
2231 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Loc)));
2232 Write_Info_Char (XE.Typ);
2233
2234 if Is_Overloadable (XE.Ent)
2235 and then Is_Imported (XE.Ent)
2236 and then XE.Typ = 'b'
2237 then
2238 Output_Import_Export_Info (XE.Ent);
2239 end if;
2240
2241 Write_Info_Nat (Int (Get_Column_Number (XE.Loc)));
2242
2243 Output_Instantiation_Refs (Sloc (XE.Ent));
2244 end if;
2245 end if;
2246 end Output_One_Ref;
2247
2248 <<Continue>>
2249 null;
2250 end loop;
2251
2252 Write_Info_EOL;
2253 end Output_Refs;
2254 end Output_References;
2255
2256 end Lib.Xref;