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