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