[Ada] Assert failure on incorrect code
[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-2020, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Csets; use Csets;
28 with Elists; use Elists;
29 with Errout; use Errout;
30 with Lib.Util; use Lib.Util;
31 with Nlists; use Nlists;
32 with Opt; use Opt;
33 with Restrict; use Restrict;
34 with Rident; use Rident;
35 with Sem; use Sem;
36 with Sem_Aux; use Sem_Aux;
37 with Sem_Prag; use Sem_Prag;
38 with Sem_Util; use Sem_Util;
39 with Sem_Warn; use Sem_Warn;
40 with Sinfo; use Sinfo;
41 with Sinput; use Sinput;
42 with Snames; use Snames;
43 with Stringt; use Stringt;
44 with Stand; use Stand;
45 with Table; use Table;
46
47 with GNAT.Heap_Sort_G;
48 with GNAT.HTable;
49
50 package body Lib.Xref is
51
52 ------------------
53 -- Declarations --
54 ------------------
55
56 -- The Xref table is used to record references. The Loc field is set
57 -- to No_Location for a definition entry.
58
59 subtype Xref_Entry_Number is Int;
60
61 type Xref_Key is record
62 -- These are the components of Xref_Entry that participate in hash
63 -- lookups.
64
65 Ent : Entity_Id;
66 -- Entity referenced (E parameter to Generate_Reference)
67
68 Loc : Source_Ptr;
69 -- Location of reference (Original_Location (Sloc field of N parameter
70 -- to Generate_Reference)). Set to No_Location for the case of a
71 -- defining occurrence.
72
73 Typ : Character;
74 -- Reference type (Typ param to Generate_Reference)
75
76 Eun : Unit_Number_Type;
77 -- Unit number corresponding to Ent
78
79 Lun : Unit_Number_Type;
80 -- Unit number corresponding to Loc. Value is undefined and not
81 -- referenced if Loc is set to No_Location.
82
83 -- The following components are only used for SPARK cross-references
84
85 Ref_Scope : Entity_Id;
86 -- Entity of the closest subprogram or package enclosing the reference
87
88 Ent_Scope : Entity_Id;
89 -- Entity of the closest subprogram or package enclosing the definition,
90 -- which should be located in the same file as the definition itself.
91 end record;
92
93 type Xref_Entry is record
94 Key : Xref_Key;
95
96 Ent_Scope_File : Unit_Number_Type;
97 -- File for entity Ent_Scope
98
99 Def : Source_Ptr;
100 -- Original source location for entity being referenced. Note that these
101 -- values are used only during the output process, they are not set when
102 -- the entries are originally built. This is because private entities
103 -- can be swapped when the initial call is made.
104
105 HTable_Next : Xref_Entry_Number;
106 -- For use only by Static_HTable
107 end record;
108
109 package Xrefs is new Table.Table (
110 Table_Component_Type => Xref_Entry,
111 Table_Index_Type => Xref_Entry_Number,
112 Table_Low_Bound => 1,
113 Table_Initial => Alloc.Xrefs_Initial,
114 Table_Increment => Alloc.Xrefs_Increment,
115 Table_Name => "Xrefs");
116
117 --------------
118 -- Xref_Set --
119 --------------
120
121 -- We keep a set of xref entries, in order to avoid inserting duplicate
122 -- entries into the above Xrefs table. An entry is in Xref_Set if and only
123 -- if it is in Xrefs.
124
125 Num_Buckets : constant := 2**16;
126
127 subtype Header_Num is Integer range 0 .. Num_Buckets - 1;
128 type Null_Type is null record;
129 pragma Unreferenced (Null_Type);
130
131 function Hash (F : Xref_Entry_Number) return Header_Num;
132
133 function Equal (F1, F2 : Xref_Entry_Number) return Boolean;
134
135 procedure HT_Set_Next (E : Xref_Entry_Number; Next : Xref_Entry_Number);
136
137 function HT_Next (E : Xref_Entry_Number) return Xref_Entry_Number;
138
139 function Get_Key (E : Xref_Entry_Number) return Xref_Entry_Number;
140
141 pragma Inline (Hash, Equal, HT_Set_Next, HT_Next, Get_Key);
142
143 package Xref_Set is new GNAT.HTable.Static_HTable (
144 Header_Num,
145 Element => Xref_Entry,
146 Elmt_Ptr => Xref_Entry_Number,
147 Null_Ptr => 0,
148 Set_Next => HT_Set_Next,
149 Next => HT_Next,
150 Key => Xref_Entry_Number,
151 Get_Key => Get_Key,
152 Hash => Hash,
153 Equal => Equal);
154
155 -----------------------------
156 -- SPARK Xrefs Information --
157 -----------------------------
158
159 package body SPARK_Specific is separate;
160
161 ------------------------
162 -- Local Subprograms --
163 ------------------------
164
165 procedure Add_Entry (Key : Xref_Key; Ent_Scope_File : Unit_Number_Type);
166 -- Add an entry to the tables of Xref_Entries, avoiding duplicates
167
168 procedure Generate_Prim_Op_References (Typ : Entity_Id);
169 -- For a tagged type, generate implicit references to its primitive
170 -- operations, for source navigation. This is done right before emitting
171 -- cross-reference information rather than at the freeze point of the type
172 -- in order to handle late bodies that are primitive operations.
173
174 function Lt (T1, T2 : Xref_Entry) return Boolean;
175 -- Order cross-references
176
177 ---------------
178 -- Add_Entry --
179 ---------------
180
181 procedure Add_Entry (Key : Xref_Key; Ent_Scope_File : Unit_Number_Type) is
182 begin
183 Xrefs.Increment_Last; -- tentative
184 Xrefs.Table (Xrefs.Last).Key := Key;
185
186 -- Set the entry in Xref_Set, and if newly set, keep the above
187 -- tentative increment.
188
189 if Xref_Set.Set_If_Not_Present (Xrefs.Last) then
190 Xrefs.Table (Xrefs.Last).Ent_Scope_File := Ent_Scope_File;
191 -- Leave Def and HTable_Next uninitialized
192
193 Set_Has_Xref_Entry (Key.Ent);
194
195 -- It was already in Xref_Set, so throw away the tentatively-added entry
196
197 else
198 Xrefs.Decrement_Last;
199 end if;
200 end Add_Entry;
201
202 -----------
203 -- Equal --
204 -----------
205
206 function Equal (F1, F2 : Xref_Entry_Number) return Boolean is
207 Result : constant Boolean :=
208 Xrefs.Table (F1).Key = Xrefs.Table (F2).Key;
209 begin
210 return Result;
211 end Equal;
212
213 -------------------------
214 -- Generate_Definition --
215 -------------------------
216
217 procedure Generate_Definition (E : Entity_Id) is
218 begin
219 pragma Assert (Nkind (E) in N_Entity);
220
221 -- Note that we do not test Xref_Entity_Letters here. It is too early
222 -- to do so, since we are often called before the entity is fully
223 -- constructed, so that the Ekind is still E_Void.
224
225 if Opt.Xref_Active
226
227 -- Definition must come from source
228
229 -- We make an exception for subprogram child units that have no spec.
230 -- For these we generate a subprogram declaration for library use,
231 -- and the corresponding entity does not come from source.
232 -- Nevertheless, all references will be attached to it and we have
233 -- to treat is as coming from user code.
234
235 and then (Comes_From_Source (E) or else Is_Child_Unit (E))
236
237 -- And must have a reasonable source location that is not
238 -- within an instance (all entities in instances are ignored)
239
240 and then Sloc (E) > No_Location
241 and then Instantiation_Location (Sloc (E)) = No_Location
242
243 -- And must be a non-internal name from the main source unit
244
245 and then In_Extended_Main_Source_Unit (E)
246 and then not Is_Internal_Name (Chars (E))
247 then
248 Add_Entry
249 ((Ent => E,
250 Loc => No_Location,
251 Typ => ' ',
252 Eun => Get_Source_Unit (Original_Location (Sloc (E))),
253 Lun => No_Unit,
254 Ref_Scope => Empty,
255 Ent_Scope => Empty),
256 Ent_Scope_File => No_Unit);
257
258 if In_Inlined_Body then
259 Set_Referenced (E);
260 end if;
261 end if;
262 end Generate_Definition;
263
264 ---------------------------------
265 -- Generate_Operator_Reference --
266 ---------------------------------
267
268 procedure Generate_Operator_Reference
269 (N : Node_Id;
270 T : Entity_Id)
271 is
272 begin
273 if not In_Extended_Main_Source_Unit (N) then
274 return;
275 end if;
276
277 -- If the operator is not a Standard operator, then we generate a real
278 -- reference to the user defined operator.
279
280 if Sloc (Entity (N)) /= Standard_Location then
281 Generate_Reference (Entity (N), N);
282
283 -- A reference to an implicit inequality operator is also a reference
284 -- to the user-defined equality.
285
286 if Nkind (N) = N_Op_Ne
287 and then not Comes_From_Source (Entity (N))
288 and then Present (Corresponding_Equality (Entity (N)))
289 then
290 Generate_Reference (Corresponding_Equality (Entity (N)), N);
291 end if;
292
293 -- For the case of Standard operators, we mark the result type as
294 -- referenced. This ensures that in the case where we are using a
295 -- derived operator, we mark an entity of the unit that implicitly
296 -- defines this operator as used. Otherwise we may think that no entity
297 -- of the unit is used. The actual entity marked as referenced is the
298 -- first subtype, which is the relevant user defined entity.
299
300 -- Note: we only do this for operators that come from source. The
301 -- generated code sometimes reaches for entities that do not need to be
302 -- explicitly visible (for example, when we expand the code for
303 -- comparing two record objects, the fields of the record may not be
304 -- visible).
305
306 elsif Comes_From_Source (N) then
307 Set_Referenced (First_Subtype (T));
308 end if;
309 end Generate_Operator_Reference;
310
311 ---------------------------------
312 -- Generate_Prim_Op_References --
313 ---------------------------------
314
315 procedure Generate_Prim_Op_References (Typ : Entity_Id) is
316 Base_T : Entity_Id;
317 Prim : Elmt_Id;
318 Prim_List : Elist_Id;
319
320 begin
321 -- Handle subtypes of synchronized types
322
323 if Ekind (Typ) = E_Protected_Subtype
324 or else Ekind (Typ) = E_Task_Subtype
325 then
326 Base_T := Etype (Typ);
327 else
328 Base_T := Typ;
329 end if;
330
331 -- References to primitive operations are only relevant for tagged types
332
333 if not Is_Tagged_Type (Base_T)
334 or else Is_Class_Wide_Type (Base_T)
335 then
336 return;
337 end if;
338
339 -- Ada 2005 (AI-345): For synchronized types generate reference to the
340 -- wrapper that allow us to dispatch calls through their implemented
341 -- abstract interface types.
342
343 -- The check for Present here is to protect against previously reported
344 -- critical errors.
345
346 Prim_List := Primitive_Operations (Base_T);
347
348 if No (Prim_List) then
349 return;
350 end if;
351
352 Prim := First_Elmt (Prim_List);
353 while Present (Prim) loop
354
355 -- If the operation is derived, get the original for cross-reference
356 -- reference purposes (it is the original for which we want the xref
357 -- and for which the comes_from_source test must be performed).
358
359 Generate_Reference
360 (Typ, Ultimate_Alias (Node (Prim)), 'p', Set_Ref => False);
361 Next_Elmt (Prim);
362 end loop;
363 end Generate_Prim_Op_References;
364
365 ------------------------
366 -- Generate_Reference --
367 ------------------------
368
369 procedure Generate_Reference
370 (E : Entity_Id;
371 N : Node_Id;
372 Typ : Character := 'r';
373 Set_Ref : Boolean := True;
374 Force : Boolean := False)
375 is
376 Actual_Typ : Character := Typ;
377 Call : Node_Id;
378 Def : Source_Ptr;
379 Ent : Entity_Id;
380 Ent_Scope : Entity_Id;
381 Formal : Entity_Id;
382 Kind : Entity_Kind;
383 Nod : Node_Id;
384 Ref : Source_Ptr;
385 Ref_Scope : Entity_Id;
386
387 function Get_Through_Renamings (E : Entity_Id) return Entity_Id;
388 -- Get the enclosing entity through renamings, which may come from
389 -- source or from the translation of generic instantiations.
390
391 function Is_On_LHS (Node : Node_Id) return Boolean;
392 -- Used to check if a node is on the left hand side of an assignment.
393 -- The following cases are handled:
394 --
395 -- Variable Node is a direct descendant of left hand side of an
396 -- assignment statement.
397 --
398 -- Prefix Of an indexed or selected component that is present in
399 -- a subtree rooted by an assignment statement. There is
400 -- no restriction of nesting of components, thus cases
401 -- such as A.B (C).D are handled properly. However a prefix
402 -- of a dereference (either implicit or explicit) is never
403 -- considered as on a LHS.
404 --
405 -- Out param Same as above cases, but OUT parameter
406
407 function OK_To_Set_Referenced return Boolean;
408 -- Returns True if the Referenced flag can be set. There are a few
409 -- exceptions where we do not want to set this flag, see body for
410 -- details of these exceptional cases.
411
412 ---------------------------
413 -- Get_Through_Renamings --
414 ---------------------------
415
416 function Get_Through_Renamings (E : Entity_Id) return Entity_Id is
417 begin
418 case Ekind (E) is
419
420 -- For subprograms we just need to check once if they are have a
421 -- Renamed_Entity, because Renamed_Entity is set transitively.
422
423 when Subprogram_Kind =>
424 declare
425 Renamed : constant Entity_Id := Renamed_Entity (E);
426
427 begin
428 if Present (Renamed) then
429 return Renamed;
430 else
431 return E;
432 end if;
433 end;
434
435 -- For objects we need to repeatedly call Renamed_Object, because
436 -- it is not transitive.
437
438 when Object_Kind =>
439 declare
440 Obj : Entity_Id := E;
441
442 begin
443 loop
444 pragma Assert (Present (Obj));
445
446 declare
447 Renamed : constant Entity_Id := Renamed_Object (Obj);
448
449 begin
450 if Present (Renamed) then
451 Obj := Get_Enclosing_Object (Renamed);
452
453 -- The renamed expression denotes a non-object,
454 -- e.g. function call, slicing of a function call,
455 -- pointer dereference, etc.
456
457 if No (Obj) then
458 return Empty;
459 end if;
460 else
461 return Obj;
462 end if;
463 end;
464 end loop;
465 end;
466
467 when others =>
468 return E;
469
470 end case;
471 end Get_Through_Renamings;
472
473 ---------------
474 -- Is_On_LHS --
475 ---------------
476
477 -- ??? There are several routines here and there that perform a similar
478 -- (but subtly different) computation, which should be factored:
479
480 -- Sem_Util.Is_LHS
481 -- Sem_Util.May_Be_Lvalue
482 -- Sem_Util.Known_To_Be_Assigned
483 -- Exp_Ch2.Expand_Entry_Parameter.In_Assignment_Context
484 -- Exp_Smem.Is_Out_Actual
485
486 function Is_On_LHS (Node : Node_Id) return Boolean is
487 N : Node_Id;
488 P : Node_Id;
489 K : Node_Kind;
490
491 begin
492 -- Only identifiers are considered, is this necessary???
493
494 if Nkind (Node) /= N_Identifier then
495 return False;
496 end if;
497
498 -- Immediate return if appeared as OUT parameter
499
500 if Kind = E_Out_Parameter then
501 return True;
502 end if;
503
504 -- Search for assignment statement subtree root
505
506 N := Node;
507 loop
508 P := Parent (N);
509 K := Nkind (P);
510
511 if K = N_Assignment_Statement then
512 return Name (P) = N;
513
514 -- Check whether the parent is a component and the current node is
515 -- its prefix, but return False if the current node has an access
516 -- type, as in that case the selected or indexed component is an
517 -- implicit dereference, and the LHS is the designated object, not
518 -- the access object.
519
520 -- ??? case of a slice assignment?
521
522 elsif (K = N_Selected_Component or else K = N_Indexed_Component)
523 and then Prefix (P) = N
524 then
525 -- Check for access type. First a special test, In some cases
526 -- this is called too early (see comments in Find_Direct_Name),
527 -- at a point where the tree is not fully typed yet. In that
528 -- case we may lack an Etype for N, and we can't check the
529 -- Etype. For now, we always return False in such a case,
530 -- but this is clearly not right in all cases ???
531
532 if No (Etype (N)) then
533 return False;
534
535 elsif Is_Access_Type (Etype (N)) then
536 return False;
537
538 -- Access type case dealt with, keep going
539
540 else
541 N := P;
542 end if;
543
544 -- All other cases, definitely not on left side
545
546 else
547 return False;
548 end if;
549 end loop;
550 end Is_On_LHS;
551
552 ---------------------------
553 -- OK_To_Set_Referenced --
554 ---------------------------
555
556 function OK_To_Set_Referenced return Boolean is
557 P : Node_Id;
558
559 begin
560 -- A reference from a pragma Unreferenced or pragma Unmodified or
561 -- pragma Warnings does not cause the Referenced flag to be set.
562 -- This avoids silly warnings about things being referenced and
563 -- not assigned when the only reference is from the pragma.
564
565 if Nkind (N) = N_Identifier then
566 P := Parent (N);
567
568 if Nkind (P) = N_Pragma_Argument_Association then
569 P := Parent (P);
570
571 if Nkind (P) = N_Pragma then
572 if Pragma_Name_Unmapped (P) in Name_Warnings
573 | Name_Unmodified
574 | Name_Unreferenced
575 then
576 return False;
577 end if;
578 end if;
579
580 -- A reference to a formal in a named parameter association does
581 -- not make the formal referenced. Formals that are unused in the
582 -- subprogram body are properly flagged as such, even if calls
583 -- elsewhere use named notation.
584
585 elsif Nkind (P) = N_Parameter_Association
586 and then N = Selector_Name (P)
587 then
588 return False;
589 end if;
590 end if;
591
592 return True;
593 end OK_To_Set_Referenced;
594
595 -- Start of processing for Generate_Reference
596
597 begin
598 -- May happen in case of severe errors
599
600 if Nkind (E) not in N_Entity then
601 return;
602 end if;
603
604 Find_Actual (N, Formal, Call);
605
606 if Present (Formal) then
607 Kind := Ekind (Formal);
608 else
609 Kind := E_Void;
610 end if;
611
612 -- Check for obsolescent reference to package ASCII. GNAT treats this
613 -- element of annex J specially since in practice, programs make a lot
614 -- of use of this feature, so we don't include it in the set of features
615 -- diagnosed when Warn_On_Obsolescent_Features mode is set. However we
616 -- are required to note it as a violation of the RM defined restriction.
617
618 if E = Standard_ASCII then
619 Check_Restriction (No_Obsolescent_Features, N);
620 end if;
621
622 -- Check for reference to entity marked with Is_Obsolescent
623
624 -- Note that we always allow obsolescent references in the compiler
625 -- itself and the run time, since we assume that we know what we are
626 -- doing in such cases. For example the calls in Ada.Characters.Handling
627 -- to its own obsolescent subprograms are just fine.
628
629 -- In any case we only generate warnings if we are in the extended main
630 -- source unit, and the entity itself is not in the extended main source
631 -- unit, since we assume the source unit itself knows what is going on
632 -- (and for sure we do not want silly warnings, e.g. on the end line of
633 -- an obsolescent procedure body).
634
635 if Is_Obsolescent (E)
636 and then not GNAT_Mode
637 and then not In_Extended_Main_Source_Unit (E)
638 and then In_Extended_Main_Source_Unit (N)
639 then
640 Check_Restriction (No_Obsolescent_Features, N);
641
642 if Warn_On_Obsolescent_Feature then
643 Output_Obsolescent_Entity_Warnings (N, E);
644 end if;
645 end if;
646
647 -- Warn if reference to Ada 2005 entity not in Ada 2005 mode. We only
648 -- detect real explicit references (modifications and references).
649
650 if Comes_From_Source (N)
651 and then Is_Ada_2005_Only (E)
652 and then Ada_Version < Ada_2005
653 and then Warn_On_Ada_2005_Compatibility
654 and then (Typ = 'm' or else Typ = 'r' or else Typ = 's')
655 then
656 Error_Msg_NE ("& is only defined in Ada 2005?y?", N, E);
657 end if;
658
659 -- Warn if reference to Ada 2012 entity not in Ada 2012 mode. We only
660 -- detect real explicit references (modifications and references).
661
662 if Comes_From_Source (N)
663 and then Is_Ada_2012_Only (E)
664 and then Ada_Version < Ada_2012
665 and then Warn_On_Ada_2012_Compatibility
666 and then (Typ = 'm' or else Typ = 'r')
667 then
668 Error_Msg_NE ("& is only defined in Ada 2012?y?", N, E);
669 end if;
670
671 -- Do not generate references if we are within a postcondition sub-
672 -- program, because the reference does not comes from source, and the
673 -- preanalysis of the aspect has already created an entry for the ALI
674 -- file at the proper source location.
675
676 if Chars (Current_Scope) = Name_uPostconditions then
677 return;
678 end if;
679
680 -- Never collect references if not in main source unit. However, we omit
681 -- this test if Typ is 'e' or 'k', since these entries are structural,
682 -- and it is useful to have them in units that reference packages as
683 -- well as units that define packages. We also omit the test for the
684 -- case of 'p' since we want to include inherited primitive operations
685 -- from other packages.
686
687 -- We also omit this test is this is a body reference for a subprogram
688 -- instantiation. In this case the reference is to the generic body,
689 -- which clearly need not be in the main unit containing the instance.
690 -- For the same reason we accept an implicit reference generated for
691 -- a default in an instance.
692
693 -- We also set the referenced flag in a generic package that is not in
694 -- then main source unit, when the variable is of a formal private type,
695 -- to warn in the instance if the corresponding type is not a fully
696 -- initialized type.
697
698 if not In_Extended_Main_Source_Unit (N) then
699 if Typ = 'e' or else
700 Typ = 'I' or else
701 Typ = 'p' or else
702 Typ = 'i' or else
703 Typ = 'k'
704 or else (Typ = 'b' and then Is_Generic_Instance (E))
705
706 -- Allow the generation of references to reads, writes and calls
707 -- in SPARK mode when the related context comes from an instance.
708
709 or else
710 (GNATprove_Mode
711 and then In_Extended_Main_Code_Unit (N)
712 and then (Typ = 'm' or else Typ = 'r' or else Typ = 's'))
713 then
714 null;
715
716 elsif In_Instance_Body
717 and then In_Extended_Main_Code_Unit (N)
718 and then Is_Generic_Type (Etype (E))
719 then
720 Set_Referenced (E);
721 return;
722
723 elsif Inside_A_Generic
724 and then Is_Generic_Type (Etype (E))
725 then
726 Set_Referenced (E);
727 return;
728
729 else
730 return;
731 end if;
732 end if;
733
734 -- For reference type p, the entity must be in main source unit
735
736 if Typ = 'p' and then not In_Extended_Main_Source_Unit (E) then
737 return;
738 end if;
739
740 -- Unless the reference is forced, we ignore references where the
741 -- reference itself does not come from source.
742
743 if not Force and then not Comes_From_Source (N) then
744 return;
745 end if;
746
747 -- Deal with setting entity as referenced, unless suppressed. Note that
748 -- we still do Set_Referenced on entities that do not come from source.
749 -- This situation arises when we have a source reference to a derived
750 -- operation, where the derived operation itself does not come from
751 -- source, but we still want to mark it as referenced, since we really
752 -- are referencing an entity in the corresponding package (this avoids
753 -- wrong complaints that the package contains no referenced entities).
754
755 if Set_Ref then
756
757 -- Assignable object appearing on left side of assignment or as
758 -- an out parameter.
759
760 if Is_Assignable (E)
761 and then Is_On_LHS (N)
762 and then Ekind (E) /= E_In_Out_Parameter
763 then
764 -- For objects that are renamings, just set as simply referenced
765 -- we do not try to do assignment type tracking in this case.
766
767 if Present (Renamed_Object (E)) then
768 Set_Referenced (E);
769
770 -- Out parameter case
771
772 elsif Kind = E_Out_Parameter then
773
774 -- If warning mode for all out parameters is set, or this is
775 -- the only warning parameter, then we want to mark this for
776 -- later warning logic by setting Referenced_As_Out_Parameter
777
778 if Warn_On_Modified_As_Out_Parameter (Formal) then
779 Set_Referenced_As_Out_Parameter (E, True);
780 Set_Referenced_As_LHS (E, False);
781
782 -- For OUT parameter not covered by the above cases, we simply
783 -- regard it as a normal reference (in this case we do not
784 -- want any of the warning machinery for out parameters).
785
786 else
787 Set_Referenced (E);
788 end if;
789
790 -- For the left hand of an assignment case, we do nothing here.
791 -- The processing for Analyze_Assignment_Statement will set the
792 -- Referenced_As_LHS flag.
793
794 else
795 null;
796 end if;
797
798 -- Check for a reference in a pragma that should not count as a
799 -- making the variable referenced for warning purposes.
800
801 elsif Is_Non_Significant_Pragma_Reference (N) then
802 null;
803
804 -- A reference in an attribute definition clause does not count as a
805 -- reference except for the case of Address. The reason that 'Address
806 -- is an exception is that it creates an alias through which the
807 -- variable may be referenced.
808
809 elsif Nkind (Parent (N)) = N_Attribute_Definition_Clause
810 and then Chars (Parent (N)) /= Name_Address
811 and then N = Name (Parent (N))
812 then
813 null;
814
815 -- Constant completion does not count as a reference
816
817 elsif Typ = 'c'
818 and then Ekind (E) = E_Constant
819 then
820 null;
821
822 -- Record representation clause does not count as a reference
823
824 elsif Nkind (N) = N_Identifier
825 and then Nkind (Parent (N)) = N_Record_Representation_Clause
826 then
827 null;
828
829 -- Discriminants do not need to produce a reference to record type
830
831 elsif Typ = 'd'
832 and then Nkind (Parent (N)) = N_Discriminant_Specification
833 then
834 null;
835
836 -- All other cases
837
838 else
839 -- Special processing for IN OUT parameters, where we have an
840 -- implicit assignment to a simple variable.
841
842 if Kind = E_In_Out_Parameter
843 and then Is_Assignable (E)
844 then
845 -- For sure this counts as a normal read reference
846
847 Set_Referenced (E);
848 Set_Last_Assignment (E, Empty);
849
850 -- We count it as being referenced as an out parameter if the
851 -- option is set to warn on all out parameters, except that we
852 -- have a special exclusion for an intrinsic subprogram, which
853 -- is most likely an instantiation of Unchecked_Deallocation
854 -- which we do not want to consider as an assignment since it
855 -- generates false positives. We also exclude the case of an
856 -- IN OUT parameter if the name of the procedure is Free,
857 -- since we suspect similar semantics.
858
859 if Warn_On_All_Unread_Out_Parameters
860 and then Is_Entity_Name (Name (Call))
861 and then not Is_Intrinsic_Subprogram (Entity (Name (Call)))
862 and then Chars (Name (Call)) /= Name_Free
863 then
864 Set_Referenced_As_Out_Parameter (E, True);
865 Set_Referenced_As_LHS (E, False);
866 end if;
867
868 -- Don't count a recursive reference within a subprogram as a
869 -- reference (that allows detection of a recursive subprogram
870 -- whose only references are recursive calls as unreferenced).
871
872 elsif Is_Subprogram (E)
873 and then E = Nearest_Dynamic_Scope (Current_Scope)
874 then
875 null;
876
877 -- Any other occurrence counts as referencing the entity
878
879 elsif OK_To_Set_Referenced then
880 Set_Referenced (E);
881
882 -- If variable, this is an OK reference after an assignment
883 -- so we can clear the Last_Assignment indication.
884
885 if Is_Assignable (E) then
886 Set_Last_Assignment (E, Empty);
887 end if;
888 end if;
889 end if;
890
891 -- Check for pragma Unreferenced given and reference is within
892 -- this source unit (occasion for possible warning to be issued).
893 -- Note that the entity may be marked as unreferenced by pragma
894 -- Unused.
895
896 if Has_Unreferenced (E)
897 and then In_Same_Extended_Unit (E, N)
898 then
899 -- A reference as a named parameter in a call does not count as a
900 -- violation of pragma Unreferenced for this purpose...
901
902 if Nkind (N) = N_Identifier
903 and then Nkind (Parent (N)) = N_Parameter_Association
904 and then Selector_Name (Parent (N)) = N
905 then
906 null;
907
908 -- ... Neither does a reference to a variable on the left side of
909 -- an assignment.
910
911 elsif Is_On_LHS (N) then
912 null;
913
914 -- Do not consider F'Result as a violation of pragma Unreferenced
915 -- since the attribute acts as an anonymous alias of the function
916 -- result and not as a real reference to the function.
917
918 elsif Ekind (E) in E_Function | E_Generic_Function
919 and then Is_Entity_Name (N)
920 and then Is_Attribute_Result (Parent (N))
921 then
922 null;
923
924 -- No warning if the reference is in a call that does not come
925 -- from source (e.g. a call to a controlled type primitive).
926
927 elsif not Comes_From_Source (Parent (N))
928 and then Nkind (Parent (N)) = N_Procedure_Call_Statement
929 then
930 null;
931
932 -- For entry formals, we want to place the warning message on the
933 -- corresponding entity in the accept statement. The current scope
934 -- is the body of the accept, so we find the formal whose name
935 -- matches that of the entry formal (there is no link between the
936 -- two entities, and the one in the accept statement is only used
937 -- for conformance checking).
938
939 elsif Ekind (Scope (E)) = E_Entry then
940 declare
941 BE : Entity_Id;
942
943 begin
944 BE := First_Entity (Current_Scope);
945 while Present (BE) loop
946 if Chars (BE) = Chars (E) then
947 if Has_Pragma_Unused (E) then
948 Error_Msg_NE -- CODEFIX
949 ("??pragma Unused given for&!", N, BE);
950 else
951 Error_Msg_NE -- CODEFIX
952 ("??pragma Unreferenced given for&!", N, BE);
953 end if;
954 exit;
955 end if;
956
957 Next_Entity (BE);
958 end loop;
959 end;
960
961 -- Here we issue the warning, since this is a real reference
962
963 elsif Has_Pragma_Unused (E) then
964 Error_Msg_NE -- CODEFIX
965 ("??pragma Unused given for&!", N, E);
966 else
967 Error_Msg_NE -- CODEFIX
968 ("??pragma Unreferenced given for&!", N, E);
969 end if;
970 end if;
971
972 -- If this is a subprogram instance, mark as well the internal
973 -- subprogram in the wrapper package, which may be a visible
974 -- compilation unit.
975
976 if Is_Overloadable (E)
977 and then Is_Generic_Instance (E)
978 and then Present (Alias (E))
979 then
980 Set_Referenced (Alias (E));
981 end if;
982 end if;
983
984 -- Generate reference if all conditions are met:
985
986 if
987 -- Cross referencing must be active
988
989 Opt.Xref_Active
990
991 -- The entity must be one for which we collect references
992
993 and then Xref_Entity_Letters (Ekind (E)) /= ' '
994
995 -- Both Sloc values must be set to something sensible
996
997 and then Sloc (E) > No_Location
998 and then Sloc (N) > No_Location
999
1000 -- Ignore references from within an instance. The only exceptions to
1001 -- this are default subprograms, for which we generate an implicit
1002 -- reference and compilations in SPARK mode.
1003
1004 and then
1005 (Instantiation_Location (Sloc (N)) = No_Location
1006 or else Typ = 'i'
1007 or else GNATprove_Mode)
1008
1009 -- Ignore dummy references
1010
1011 and then Typ /= ' '
1012 then
1013 if Nkind (N) in N_Identifier
1014 | N_Defining_Identifier
1015 | N_Defining_Operator_Symbol
1016 | N_Operator_Symbol
1017 | N_Defining_Character_Literal
1018 | N_Op
1019 or else (Nkind (N) = N_Character_Literal
1020 and then Sloc (Entity (N)) /= Standard_Location)
1021 then
1022 Nod := N;
1023
1024 elsif Nkind (N) in N_Expanded_Name | N_Selected_Component then
1025 Nod := Selector_Name (N);
1026
1027 else
1028 return;
1029 end if;
1030
1031 -- Normal case of source entity comes from source
1032
1033 if Comes_From_Source (E) then
1034 Ent := E;
1035
1036 -- Because a declaration may be generated for a subprogram body
1037 -- without declaration in GNATprove mode, for inlining, some
1038 -- parameters may end up being marked as not coming from source
1039 -- although they are. Take these into account specially.
1040
1041 elsif GNATprove_Mode and then Is_Formal (E) then
1042 Ent := E;
1043
1044 -- Entity does not come from source, but is a derived subprogram and
1045 -- the derived subprogram comes from source (after one or more
1046 -- derivations) in which case the reference is to parent subprogram.
1047
1048 elsif Is_Overloadable (E)
1049 and then Present (Alias (E))
1050 then
1051 Ent := Alias (E);
1052 while not Comes_From_Source (Ent) loop
1053 if No (Alias (Ent)) then
1054 return;
1055 end if;
1056
1057 Ent := Alias (Ent);
1058 end loop;
1059
1060 -- The internally created defining entity for a child subprogram
1061 -- that has no previous spec has valid references.
1062
1063 elsif Is_Overloadable (E)
1064 and then Is_Child_Unit (E)
1065 then
1066 Ent := E;
1067
1068 -- Ditto for the formals of such a subprogram
1069
1070 elsif Is_Overloadable (Scope (E))
1071 and then Is_Child_Unit (Scope (E))
1072 then
1073 Ent := E;
1074
1075 -- Record components of discriminated subtypes or derived types must
1076 -- be treated as references to the original component.
1077
1078 elsif Ekind (E) = E_Component
1079 and then Comes_From_Source (Original_Record_Component (E))
1080 then
1081 Ent := Original_Record_Component (E);
1082
1083 -- If this is an expanded reference to a discriminant, recover the
1084 -- original discriminant, which gets the reference.
1085
1086 elsif Ekind (E) = E_In_Parameter
1087 and then Present (Discriminal_Link (E))
1088 then
1089 Ent := Discriminal_Link (E);
1090 Set_Referenced (Ent);
1091
1092 -- Ignore reference to any other entity that is not from source
1093
1094 else
1095 return;
1096 end if;
1097
1098 -- In SPARK mode, consider the underlying entity renamed instead of
1099 -- the renaming, which is needed to compute a valid set of effects
1100 -- (reads, writes) for the enclosing subprogram.
1101
1102 if GNATprove_Mode then
1103 Ent := Get_Through_Renamings (Ent);
1104
1105 -- If no enclosing object, then it could be a reference to any
1106 -- location not tracked individually, like heap-allocated data.
1107 -- Conservatively approximate this possibility by generating a
1108 -- dereference, and return.
1109
1110 if No (Ent) then
1111 if Actual_Typ = 'w' then
1112 SPARK_Specific.Generate_Dereference (Nod, 'r');
1113 SPARK_Specific.Generate_Dereference (Nod, 'w');
1114 else
1115 SPARK_Specific.Generate_Dereference (Nod, 'r');
1116 end if;
1117
1118 return;
1119 end if;
1120 end if;
1121
1122 -- Record reference to entity
1123
1124 if Actual_Typ = 'p'
1125 and then Is_Subprogram (Nod)
1126 and then Present (Overridden_Operation (Nod))
1127 then
1128 Actual_Typ := 'P';
1129 end if;
1130
1131 -- Comment needed here for special SPARK code ???
1132
1133 if GNATprove_Mode then
1134
1135 -- Ignore references to an entity which is a Part_Of single
1136 -- concurrent object. Ideally we would prefer to add it as a
1137 -- reference to the corresponding concurrent type, but it is quite
1138 -- difficult (as such references are not currently added even for)
1139 -- reads/writes of private protected components) and not worth the
1140 -- effort.
1141
1142 if Ekind (Ent) in E_Abstract_State | E_Constant | E_Variable
1143 and then Present (Encapsulating_State (Ent))
1144 and then Is_Single_Concurrent_Object (Encapsulating_State (Ent))
1145 then
1146 return;
1147 end if;
1148
1149 Ref := Sloc (Nod);
1150 Def := Sloc (Ent);
1151
1152 Ref_Scope :=
1153 SPARK_Specific.Enclosing_Subprogram_Or_Library_Package (Nod);
1154 Ent_Scope :=
1155 SPARK_Specific.Enclosing_Subprogram_Or_Library_Package (Ent);
1156
1157 -- Since we are reaching through renamings in SPARK mode, we may
1158 -- end up with standard constants. Ignore those.
1159
1160 if Sloc (Ent_Scope) <= Standard_Location
1161 or else Def <= Standard_Location
1162 then
1163 return;
1164 end if;
1165
1166 Add_Entry
1167 ((Ent => Ent,
1168 Loc => Ref,
1169 Typ => Actual_Typ,
1170 Eun => Get_Top_Level_Code_Unit (Def),
1171 Lun => Get_Top_Level_Code_Unit (Ref),
1172 Ref_Scope => Ref_Scope,
1173 Ent_Scope => Ent_Scope),
1174 Ent_Scope_File => Get_Top_Level_Code_Unit (Ent));
1175
1176 else
1177 Ref := Original_Location (Sloc (Nod));
1178 Def := Original_Location (Sloc (Ent));
1179
1180 -- If this is an operator symbol, skip the initial quote for
1181 -- navigation purposes. This is not done for the end label,
1182 -- where we want the actual position after the closing quote.
1183
1184 if Typ = 't' then
1185 null;
1186
1187 elsif Nkind (N) = N_Defining_Operator_Symbol
1188 or else Nkind (Nod) = N_Operator_Symbol
1189 then
1190 Ref := Ref + 1;
1191 end if;
1192
1193 Add_Entry
1194 ((Ent => Ent,
1195 Loc => Ref,
1196 Typ => Actual_Typ,
1197 Eun => Get_Source_Unit (Def),
1198 Lun => Get_Source_Unit (Ref),
1199 Ref_Scope => Empty,
1200 Ent_Scope => Empty),
1201 Ent_Scope_File => No_Unit);
1202
1203 -- Generate reference to the first private entity
1204
1205 if Typ = 'e'
1206 and then Comes_From_Source (E)
1207 and then Nkind (Ent) = N_Defining_Identifier
1208 and then (Is_Package_Or_Generic_Package (Ent)
1209 or else Is_Concurrent_Type (Ent))
1210 and then Present (First_Private_Entity (E))
1211 and then In_Extended_Main_Source_Unit (N)
1212 then
1213 -- Handle case in which the full-view and partial-view of the
1214 -- first private entity are swapped.
1215
1216 declare
1217 First_Private : Entity_Id := First_Private_Entity (E);
1218
1219 begin
1220 if Is_Private_Type (First_Private)
1221 and then Present (Full_View (First_Private))
1222 then
1223 First_Private := Full_View (First_Private);
1224 end if;
1225
1226 Add_Entry
1227 ((Ent => Ent,
1228 Loc => Sloc (First_Private),
1229 Typ => 'E',
1230 Eun => Get_Source_Unit (Def),
1231 Lun => Get_Source_Unit (Ref),
1232 Ref_Scope => Empty,
1233 Ent_Scope => Empty),
1234 Ent_Scope_File => No_Unit);
1235 end;
1236 end if;
1237 end if;
1238 end if;
1239 end Generate_Reference;
1240
1241 -----------------------------------
1242 -- Generate_Reference_To_Formals --
1243 -----------------------------------
1244
1245 procedure Generate_Reference_To_Formals (E : Entity_Id) is
1246 Formal : Entity_Id;
1247
1248 begin
1249 if Is_Generic_Subprogram (E) then
1250 Formal := First_Entity (E);
1251
1252 while Present (Formal)
1253 and then not Is_Formal (Formal)
1254 loop
1255 Next_Entity (Formal);
1256 end loop;
1257
1258 elsif Ekind (E) in Access_Subprogram_Kind then
1259 Formal := First_Formal (Designated_Type (E));
1260
1261 else
1262 Formal := First_Formal (E);
1263 end if;
1264
1265 while Present (Formal) loop
1266 if Ekind (Formal) = E_In_Parameter then
1267
1268 if Nkind (Parameter_Type (Parent (Formal))) = N_Access_Definition
1269 then
1270 Generate_Reference (E, Formal, '^', False);
1271 else
1272 Generate_Reference (E, Formal, '>', False);
1273 end if;
1274
1275 elsif Ekind (Formal) = E_In_Out_Parameter then
1276 Generate_Reference (E, Formal, '=', False);
1277
1278 else
1279 Generate_Reference (E, Formal, '<', False);
1280 end if;
1281
1282 Next_Formal (Formal);
1283 end loop;
1284 end Generate_Reference_To_Formals;
1285
1286 -------------------------------------------
1287 -- Generate_Reference_To_Generic_Formals --
1288 -------------------------------------------
1289
1290 procedure Generate_Reference_To_Generic_Formals (E : Entity_Id) is
1291 Formal : Entity_Id;
1292
1293 begin
1294 Formal := First_Entity (E);
1295 while Present (Formal) loop
1296 if Comes_From_Source (Formal) then
1297 Generate_Reference (E, Formal, 'z', False);
1298 end if;
1299
1300 Next_Entity (Formal);
1301 end loop;
1302 end Generate_Reference_To_Generic_Formals;
1303
1304 -------------
1305 -- Get_Key --
1306 -------------
1307
1308 function Get_Key (E : Xref_Entry_Number) return Xref_Entry_Number is
1309 begin
1310 return E;
1311 end Get_Key;
1312
1313 ----------------------------
1314 -- Has_Deferred_Reference --
1315 ----------------------------
1316
1317 function Has_Deferred_Reference (Ent : Entity_Id) return Boolean is
1318 begin
1319 for J in Deferred_References.First .. Deferred_References.Last loop
1320 if Deferred_References.Table (J).E = Ent then
1321 return True;
1322 end if;
1323 end loop;
1324
1325 return False;
1326 end Has_Deferred_Reference;
1327
1328 ----------
1329 -- Hash --
1330 ----------
1331
1332 function Hash (F : Xref_Entry_Number) return Header_Num is
1333 -- It is unlikely to have two references to the same entity at the same
1334 -- source location, so the hash function depends only on the Ent and Loc
1335 -- fields.
1336
1337 XE : Xref_Entry renames Xrefs.Table (F);
1338 type M is mod 2**32;
1339
1340 H : constant M := M (XE.Key.Ent) + 2 ** 7 * M (abs XE.Key.Loc);
1341 -- It would be more natural to write:
1342 --
1343 -- H : constant M := M'Mod (XE.Key.Ent) + 2**7 * M'Mod (XE.Key.Loc);
1344 --
1345 -- But we can't use M'Mod, because it prevents bootstrapping with older
1346 -- compilers. Loc can be negative, so we do "abs" before converting.
1347 -- One day this can be cleaned up ???
1348
1349 begin
1350 return Header_Num (H mod Num_Buckets);
1351 end Hash;
1352
1353 -----------------
1354 -- HT_Set_Next --
1355 -----------------
1356
1357 procedure HT_Set_Next (E : Xref_Entry_Number; Next : Xref_Entry_Number) is
1358 begin
1359 Xrefs.Table (E).HTable_Next := Next;
1360 end HT_Set_Next;
1361
1362 -------------
1363 -- HT_Next --
1364 -------------
1365
1366 function HT_Next (E : Xref_Entry_Number) return Xref_Entry_Number is
1367 begin
1368 return Xrefs.Table (E).HTable_Next;
1369 end HT_Next;
1370
1371 ----------------
1372 -- Initialize --
1373 ----------------
1374
1375 procedure Initialize is
1376 begin
1377 Xrefs.Init;
1378 end Initialize;
1379
1380 --------
1381 -- Lt --
1382 --------
1383
1384 function Lt (T1, T2 : Xref_Entry) return Boolean is
1385 begin
1386 -- First test: if entity is in different unit, sort by unit
1387
1388 if T1.Key.Eun /= T2.Key.Eun then
1389 return Dependency_Num (T1.Key.Eun) < Dependency_Num (T2.Key.Eun);
1390
1391 -- Second test: within same unit, sort by entity Sloc
1392
1393 elsif T1.Def /= T2.Def then
1394 return T1.Def < T2.Def;
1395
1396 -- Third test: sort definitions ahead of references
1397
1398 elsif T1.Key.Loc = No_Location then
1399 return True;
1400
1401 elsif T2.Key.Loc = No_Location then
1402 return False;
1403
1404 -- Fourth test: for same entity, sort by reference location unit
1405
1406 elsif T1.Key.Lun /= T2.Key.Lun then
1407 return Dependency_Num (T1.Key.Lun) < Dependency_Num (T2.Key.Lun);
1408
1409 -- Fifth test: order of location within referencing unit
1410
1411 elsif T1.Key.Loc /= T2.Key.Loc then
1412 return T1.Key.Loc < T2.Key.Loc;
1413
1414 -- Finally, for two locations at the same address, we prefer
1415 -- the one that does NOT have the type 'r' so that a modification
1416 -- or extension takes preference, when there are more than one
1417 -- reference at the same location. As a result, in the case of
1418 -- entities that are in-out actuals, the read reference follows
1419 -- the modify reference.
1420
1421 else
1422 return T2.Key.Typ = 'r';
1423 end if;
1424 end Lt;
1425
1426 -----------------------
1427 -- Output_References --
1428 -----------------------
1429
1430 procedure Output_References is
1431
1432 procedure Get_Type_Reference
1433 (Ent : Entity_Id;
1434 Tref : out Entity_Id;
1435 Left : out Character;
1436 Right : out Character);
1437 -- Given an Entity_Id Ent, determines whether a type reference is
1438 -- required. If so, Tref is set to the entity for the type reference
1439 -- and Left and Right are set to the left/right brackets to be output
1440 -- for the reference. If no type reference is required, then Tref is
1441 -- set to Empty, and Left/Right are set to space.
1442
1443 procedure Output_Import_Export_Info (Ent : Entity_Id);
1444 -- Output language and external name information for an interfaced
1445 -- entity, using the format <language, external_name>.
1446
1447 ------------------------
1448 -- Get_Type_Reference --
1449 ------------------------
1450
1451 procedure Get_Type_Reference
1452 (Ent : Entity_Id;
1453 Tref : out Entity_Id;
1454 Left : out Character;
1455 Right : out Character)
1456 is
1457 Sav : Entity_Id;
1458
1459 begin
1460 -- See if we have a type reference
1461
1462 Tref := Ent;
1463 Left := '{';
1464 Right := '}';
1465
1466 loop
1467 Sav := Tref;
1468
1469 -- Processing for types
1470
1471 if Is_Type (Tref) then
1472
1473 -- Case of base type
1474
1475 if Base_Type (Tref) = Tref then
1476
1477 -- If derived, then get first subtype
1478
1479 if Tref /= Etype (Tref) then
1480 Tref := First_Subtype (Etype (Tref));
1481
1482 -- Set brackets for derived type, but don't override
1483 -- pointer case since the fact that something is a
1484 -- pointer is more important.
1485
1486 if Left /= '(' then
1487 Left := '<';
1488 Right := '>';
1489 end if;
1490
1491 -- If the completion of a private type is itself a derived
1492 -- type, we need the parent of the full view.
1493
1494 elsif Is_Private_Type (Tref)
1495 and then Present (Full_View (Tref))
1496 and then Etype (Full_View (Tref)) /= Full_View (Tref)
1497 then
1498 Tref := Etype (Full_View (Tref));
1499
1500 if Left /= '(' then
1501 Left := '<';
1502 Right := '>';
1503 end if;
1504
1505 -- If non-derived pointer, get directly designated type.
1506 -- If the type has a full view, all references are on the
1507 -- partial view that is seen first.
1508
1509 elsif Is_Access_Type (Tref) then
1510 Tref := Directly_Designated_Type (Tref);
1511 Left := '(';
1512 Right := ')';
1513
1514 elsif Is_Private_Type (Tref)
1515 and then Present (Full_View (Tref))
1516 then
1517 if Is_Access_Type (Full_View (Tref)) then
1518 Tref := Directly_Designated_Type (Full_View (Tref));
1519 Left := '(';
1520 Right := ')';
1521
1522 -- If the full view is an array type, we also retrieve
1523 -- the corresponding component type, because the ali
1524 -- entry already indicates that this is an array.
1525
1526 elsif Is_Array_Type (Full_View (Tref)) then
1527 Tref := Component_Type (Full_View (Tref));
1528 Left := '(';
1529 Right := ')';
1530 end if;
1531
1532 -- If non-derived array, get component type. Skip component
1533 -- type for case of String or Wide_String, saves worthwhile
1534 -- space.
1535
1536 elsif Is_Array_Type (Tref)
1537 and then Tref /= Standard_String
1538 and then Tref /= Standard_Wide_String
1539 then
1540 Tref := Component_Type (Tref);
1541 Left := '(';
1542 Right := ')';
1543
1544 -- For other non-derived base types, nothing
1545
1546 else
1547 exit;
1548 end if;
1549
1550 -- For a subtype, go to ancestor subtype
1551
1552 else
1553 Tref := Ancestor_Subtype (Tref);
1554
1555 -- If no ancestor subtype, go to base type
1556
1557 if No (Tref) then
1558 Tref := Base_Type (Sav);
1559 end if;
1560 end if;
1561
1562 -- For objects, functions, enum literals, just get type from
1563 -- Etype field.
1564
1565 elsif Is_Object (Tref)
1566 or else Ekind (Tref) = E_Enumeration_Literal
1567 or else Ekind (Tref) = E_Function
1568 or else Ekind (Tref) = E_Operator
1569 then
1570 Tref := Etype (Tref);
1571
1572 -- Another special case: an object of a classwide type
1573 -- initialized with a tag-indeterminate call gets a subtype
1574 -- of the classwide type during expansion. See if the original
1575 -- type in the declaration is named, and return it instead
1576 -- of going to the root type. The expression may be a class-
1577 -- wide function call whose result is on the secondary stack,
1578 -- which forces the declaration to be rewritten as a renaming,
1579 -- so examine the source declaration.
1580
1581 if Ekind (Tref) = E_Class_Wide_Subtype then
1582 declare
1583 Decl : constant Node_Id := Original_Node (Parent (Ent));
1584 begin
1585 if Nkind (Decl) = N_Object_Declaration
1586 and then Is_Entity_Name
1587 (Original_Node (Object_Definition (Decl)))
1588 then
1589 Tref :=
1590 Entity (Original_Node (Object_Definition (Decl)));
1591 end if;
1592 end;
1593
1594 -- For a function that returns a class-wide type, Tref is
1595 -- already correct.
1596
1597 elsif Is_Overloadable (Ent)
1598 and then Is_Class_Wide_Type (Tref)
1599 then
1600 return;
1601 end if;
1602
1603 -- For anything else, exit
1604
1605 else
1606 exit;
1607 end if;
1608
1609 -- Exit if no type reference, or we are stuck in some loop trying
1610 -- to find the type reference, or if the type is standard void
1611 -- type (the latter is an implementation artifact that should not
1612 -- show up in the generated cross-references).
1613
1614 exit when No (Tref)
1615 or else Tref = Sav
1616 or else Tref = Standard_Void_Type;
1617
1618 -- If we have a usable type reference, return, otherwise keep
1619 -- looking for something useful (we are looking for something
1620 -- that either comes from source or standard)
1621
1622 if Sloc (Tref) = Standard_Location
1623 or else Comes_From_Source (Tref)
1624 then
1625 -- If the reference is a subtype created for a generic actual,
1626 -- go actual directly, the inner subtype is not user visible.
1627
1628 if Nkind (Parent (Tref)) = N_Subtype_Declaration
1629 and then not Comes_From_Source (Parent (Tref))
1630 and then
1631 (Is_Wrapper_Package (Scope (Tref))
1632 or else Is_Generic_Instance (Scope (Tref)))
1633 then
1634 Tref := First_Subtype (Base_Type (Tref));
1635 end if;
1636
1637 return;
1638 end if;
1639 end loop;
1640
1641 -- If we fall through the loop, no type reference
1642
1643 Tref := Empty;
1644 Left := ' ';
1645 Right := ' ';
1646 end Get_Type_Reference;
1647
1648 -------------------------------
1649 -- Output_Import_Export_Info --
1650 -------------------------------
1651
1652 procedure Output_Import_Export_Info (Ent : Entity_Id) is
1653 Language_Name : Name_Id;
1654 Conv : constant Convention_Id := Convention (Ent);
1655
1656 begin
1657 -- Generate language name from convention
1658
1659 if Conv = Convention_C or else Conv in Convention_C_Variadic then
1660 Language_Name := Name_C;
1661
1662 elsif Conv = Convention_CPP then
1663 Language_Name := Name_CPP;
1664
1665 elsif Conv = Convention_Ada then
1666 Language_Name := Name_Ada;
1667
1668 else
1669 -- For the moment we ignore all other cases ???
1670
1671 return;
1672 end if;
1673
1674 Write_Info_Char ('<');
1675 Get_Unqualified_Name_String (Language_Name);
1676
1677 for J in 1 .. Name_Len loop
1678 Write_Info_Char (Name_Buffer (J));
1679 end loop;
1680
1681 if Present (Interface_Name (Ent)) then
1682 Write_Info_Char (',');
1683 String_To_Name_Buffer (Strval (Interface_Name (Ent)));
1684
1685 for J in 1 .. Name_Len loop
1686 Write_Info_Char (Name_Buffer (J));
1687 end loop;
1688 end if;
1689
1690 Write_Info_Char ('>');
1691 end Output_Import_Export_Info;
1692
1693 -- Start of processing for Output_References
1694
1695 begin
1696 -- First we add references to the primitive operations of tagged types
1697 -- declared in the main unit.
1698
1699 Handle_Prim_Ops : declare
1700 Ent : Entity_Id;
1701
1702 begin
1703 for J in 1 .. Xrefs.Last loop
1704 Ent := Xrefs.Table (J).Key.Ent;
1705
1706 if Is_Type (Ent)
1707 and then Is_Tagged_Type (Ent)
1708 and then Is_Base_Type (Ent)
1709 and then In_Extended_Main_Source_Unit (Ent)
1710 then
1711 Generate_Prim_Op_References (Ent);
1712 end if;
1713 end loop;
1714 end Handle_Prim_Ops;
1715
1716 -- Before we go ahead and output the references we have a problem
1717 -- that needs dealing with. So far we have captured things that are
1718 -- definitely referenced by the main unit, or defined in the main
1719 -- unit. That's because we don't want to clutter up the ali file
1720 -- for this unit with definition lines for entities in other units
1721 -- that are not referenced.
1722
1723 -- But there is a glitch. We may reference an entity in another unit,
1724 -- and it may have a type reference to an entity that is not directly
1725 -- referenced in the main unit, which may mean that there is no xref
1726 -- entry for this entity yet in the list of references.
1727
1728 -- If we don't do something about this, we will end with an orphan type
1729 -- reference, i.e. it will point to an entity that does not appear
1730 -- within the generated references in the ali file. That is not good for
1731 -- tools using the xref information.
1732
1733 -- To fix this, we go through the references adding definition entries
1734 -- for any unreferenced entities that can be referenced in a type
1735 -- reference. There is a recursion problem here, and that is dealt with
1736 -- by making sure that this traversal also traverses any entries that
1737 -- get added by the traversal.
1738
1739 Handle_Orphan_Type_References : declare
1740 J : Nat;
1741 Tref : Entity_Id;
1742 Ent : Entity_Id;
1743
1744 L, R : Character;
1745 pragma Warnings (Off, L);
1746 pragma Warnings (Off, R);
1747
1748 procedure New_Entry (E : Entity_Id);
1749 -- Make an additional entry into the Xref table for a type entity
1750 -- that is related to the current entity (parent, type ancestor,
1751 -- progenitor, etc.).
1752
1753 ----------------
1754 -- New_Entry --
1755 ----------------
1756
1757 procedure New_Entry (E : Entity_Id) is
1758 begin
1759 pragma Assert (Present (E));
1760
1761 if not Has_Xref_Entry (Implementation_Base_Type (E))
1762 and then Sloc (E) > No_Location
1763 then
1764 Add_Entry
1765 ((Ent => E,
1766 Loc => No_Location,
1767 Typ => Character'First,
1768 Eun => Get_Source_Unit (Original_Location (Sloc (E))),
1769 Lun => No_Unit,
1770 Ref_Scope => Empty,
1771 Ent_Scope => Empty),
1772 Ent_Scope_File => No_Unit);
1773 end if;
1774 end New_Entry;
1775
1776 -- Start of processing for Handle_Orphan_Type_References
1777
1778 begin
1779 -- Note that this is not a for loop for a very good reason. The
1780 -- processing of items in the table can add new items to the table,
1781 -- and they must be processed as well.
1782
1783 J := 1;
1784 while J <= Xrefs.Last loop
1785 Ent := Xrefs.Table (J).Key.Ent;
1786
1787 -- Do not generate reference information for an ignored Ghost
1788 -- entity because neither the entity nor its references will
1789 -- appear in the final tree.
1790
1791 if Is_Ignored_Ghost_Entity (Ent) then
1792 goto Orphan_Continue;
1793 end if;
1794
1795 Get_Type_Reference (Ent, Tref, L, R);
1796
1797 if Present (Tref)
1798 and then not Has_Xref_Entry (Tref)
1799 and then Sloc (Tref) > No_Location
1800 then
1801 New_Entry (Tref);
1802
1803 if Is_Record_Type (Ent)
1804 and then Present (Interfaces (Ent))
1805 then
1806 -- Add an entry for each one of the given interfaces
1807 -- implemented by type Ent.
1808
1809 declare
1810 Elmt : Elmt_Id := First_Elmt (Interfaces (Ent));
1811 begin
1812 while Present (Elmt) loop
1813 New_Entry (Node (Elmt));
1814 Next_Elmt (Elmt);
1815 end loop;
1816 end;
1817 end if;
1818 end if;
1819
1820 -- Collect inherited primitive operations that may be declared in
1821 -- another unit and have no visible reference in the current one.
1822
1823 if Is_Type (Ent)
1824 and then Is_Tagged_Type (Ent)
1825 and then Is_Derived_Type (Ent)
1826 and then Is_Base_Type (Ent)
1827 and then In_Extended_Main_Source_Unit (Ent)
1828 then
1829 declare
1830 Op_List : constant Elist_Id := Primitive_Operations (Ent);
1831 Op : Elmt_Id;
1832 Prim : Entity_Id;
1833
1834 function Parent_Op (E : Entity_Id) return Entity_Id;
1835 -- Find original operation, which may be inherited through
1836 -- several derivations.
1837
1838 function Parent_Op (E : Entity_Id) return Entity_Id is
1839 Orig_Op : constant Entity_Id := Alias (E);
1840
1841 begin
1842 if No (Orig_Op) then
1843 return Empty;
1844
1845 elsif not Comes_From_Source (E)
1846 and then not Has_Xref_Entry (Orig_Op)
1847 and then Comes_From_Source (Orig_Op)
1848 then
1849 return Orig_Op;
1850 else
1851 return Parent_Op (Orig_Op);
1852 end if;
1853 end Parent_Op;
1854
1855 begin
1856 Op := First_Elmt (Op_List);
1857 while Present (Op) loop
1858 Prim := Parent_Op (Node (Op));
1859
1860 if Present (Prim) then
1861 Add_Entry
1862 ((Ent => Prim,
1863 Loc => No_Location,
1864 Typ => Character'First,
1865 Eun => Get_Source_Unit (Sloc (Prim)),
1866 Lun => No_Unit,
1867 Ref_Scope => Empty,
1868 Ent_Scope => Empty),
1869 Ent_Scope_File => No_Unit);
1870 end if;
1871
1872 Next_Elmt (Op);
1873 end loop;
1874 end;
1875 end if;
1876
1877 <<Orphan_Continue>>
1878 J := J + 1;
1879 end loop;
1880 end Handle_Orphan_Type_References;
1881
1882 -- Now we have all the references, including those for any embedded type
1883 -- references, so we can sort them, and output them.
1884
1885 Output_Refs : declare
1886 Nrefs : constant Nat := Xrefs.Last;
1887 -- Number of references in table
1888
1889 Rnums : array (0 .. Nrefs) of Nat;
1890 -- This array contains numbers of references in the Xrefs table.
1891 -- This list is sorted in output order. The extra 0'th entry is
1892 -- convenient for the call to sort. When we sort the table, we
1893 -- move the entries in Rnums around, but we do not move the
1894 -- original table entries.
1895
1896 Curxu : Unit_Number_Type;
1897 -- Current xref unit
1898
1899 Curru : Unit_Number_Type;
1900 -- Current reference unit for one entity
1901
1902 Curent : Entity_Id;
1903 -- Current entity
1904
1905 Curnam : String (1 .. Name_Buffer'Length);
1906 Curlen : Natural;
1907 -- Simple name and length of current entity
1908
1909 Curdef : Source_Ptr;
1910 -- Original source location for current entity
1911
1912 Crloc : Source_Ptr;
1913 -- Current reference location
1914
1915 Ctyp : Character;
1916 -- Entity type character
1917
1918 Prevt : Character;
1919 -- reference kind of previous reference
1920
1921 Tref : Entity_Id;
1922 -- Type reference
1923
1924 Rref : Node_Id;
1925 -- Renaming reference
1926
1927 Trunit : Unit_Number_Type;
1928 -- Unit number for type reference
1929
1930 function Lt (Op1, Op2 : Natural) return Boolean;
1931 -- Comparison function for Sort call
1932
1933 function Name_Change (X : Entity_Id) return Boolean;
1934 -- Determines if entity X has a different simple name from Curent
1935
1936 procedure Move (From : Natural; To : Natural);
1937 -- Move procedure for Sort call
1938
1939 package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
1940
1941 --------
1942 -- Lt --
1943 --------
1944
1945 function Lt (Op1, Op2 : Natural) return Boolean is
1946 T1 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op1)));
1947 T2 : Xref_Entry renames Xrefs.Table (Rnums (Nat (Op2)));
1948
1949 begin
1950 return Lt (T1, T2);
1951 end Lt;
1952
1953 ----------
1954 -- Move --
1955 ----------
1956
1957 procedure Move (From : Natural; To : Natural) is
1958 begin
1959 Rnums (Nat (To)) := Rnums (Nat (From));
1960 end Move;
1961
1962 -----------------
1963 -- Name_Change --
1964 -----------------
1965
1966 -- Why a string comparison here??? Why not compare Name_Id values???
1967
1968 function Name_Change (X : Entity_Id) return Boolean is
1969 begin
1970 Get_Unqualified_Name_String (Chars (X));
1971
1972 if Name_Len /= Curlen then
1973 return True;
1974 else
1975 return Name_Buffer (1 .. Curlen) /= Curnam (1 .. Curlen);
1976 end if;
1977 end Name_Change;
1978
1979 -- Start of processing for Output_Refs
1980
1981 begin
1982 -- Capture the definition Sloc values. We delay doing this till now,
1983 -- since at the time the reference or definition is made, private
1984 -- types may be swapped, and the Sloc value may be incorrect. We
1985 -- also set up the pointer vector for the sort.
1986
1987 -- For user-defined operators we need to skip the initial quote and
1988 -- point to the first character of the name, for navigation purposes.
1989
1990 for J in 1 .. Nrefs loop
1991 declare
1992 E : constant Entity_Id := Xrefs.Table (J).Key.Ent;
1993 Loc : constant Source_Ptr := Original_Location (Sloc (E));
1994
1995 begin
1996 Rnums (J) := J;
1997
1998 if Nkind (E) = N_Defining_Operator_Symbol then
1999 Xrefs.Table (J).Def := Loc + 1;
2000 else
2001 Xrefs.Table (J).Def := Loc;
2002 end if;
2003 end;
2004 end loop;
2005
2006 -- Sort the references
2007
2008 Sorting.Sort (Integer (Nrefs));
2009
2010 -- Initialize loop through references
2011
2012 Curxu := No_Unit;
2013 Curent := Empty;
2014 Curdef := No_Location;
2015 Curru := No_Unit;
2016 Crloc := No_Location;
2017 Prevt := 'm';
2018
2019 -- Loop to output references
2020
2021 for Refno in 1 .. Nrefs loop
2022 Output_One_Ref : declare
2023 Ent : Entity_Id;
2024
2025 XE : Xref_Entry renames Xrefs.Table (Rnums (Refno));
2026 -- The current entry to be accessed
2027
2028 Left : Character;
2029 Right : Character;
2030 -- Used for {} or <> or () for type reference
2031
2032 procedure Check_Type_Reference
2033 (Ent : Entity_Id;
2034 List_Interface : Boolean;
2035 Is_Component : Boolean := False);
2036 -- Find whether there is a meaningful type reference for
2037 -- Ent, and display it accordingly. If List_Interface is
2038 -- true, then Ent is a progenitor interface of the current
2039 -- type entity being listed. In that case list it as is,
2040 -- without looking for a type reference for it. Flag is also
2041 -- used for index types of an array type, where the caller
2042 -- supplies the intended type reference. Is_Component serves
2043 -- the same purpose, to display the component type of a
2044 -- derived array type, for which only the parent type has
2045 -- ben displayed so far.
2046
2047 procedure Output_Instantiation_Refs (Loc : Source_Ptr);
2048 -- Recursive procedure to output instantiation references for
2049 -- the given source ptr in [file|line[...]] form. No output
2050 -- if the given location is not a generic template reference.
2051
2052 procedure Output_Overridden_Op (Old_E : Entity_Id);
2053 -- For a subprogram that is overriding, display information
2054 -- about the inherited operation that it overrides.
2055
2056 --------------------------
2057 -- Check_Type_Reference --
2058 --------------------------
2059
2060 procedure Check_Type_Reference
2061 (Ent : Entity_Id;
2062 List_Interface : Boolean;
2063 Is_Component : Boolean := False)
2064 is
2065 begin
2066 if List_Interface then
2067
2068 -- This is a progenitor interface of the type for which
2069 -- xref information is being generated.
2070
2071 Tref := Ent;
2072 Left := '<';
2073 Right := '>';
2074
2075 -- The following is not documented in lib-xref.ads ???
2076
2077 elsif Is_Component then
2078 Tref := Ent;
2079 Left := '(';
2080 Right := ')';
2081
2082 else
2083 Get_Type_Reference (Ent, Tref, Left, Right);
2084 end if;
2085
2086 if Present (Tref) then
2087
2088 -- Case of standard entity, output name
2089
2090 if Sloc (Tref) = Standard_Location then
2091 Write_Info_Char (Left);
2092 Write_Info_Name (Chars (Tref));
2093 Write_Info_Char (Right);
2094
2095 -- Case of source entity, output location
2096
2097 else
2098 Write_Info_Char (Left);
2099 Trunit := Get_Source_Unit (Sloc (Tref));
2100
2101 if Trunit /= Curxu then
2102 Write_Info_Nat (Dependency_Num (Trunit));
2103 Write_Info_Char ('|');
2104 end if;
2105
2106 Write_Info_Nat
2107 (Int (Get_Logical_Line_Number (Sloc (Tref))));
2108
2109 declare
2110 Ent : Entity_Id;
2111 Ctyp : Character;
2112
2113 begin
2114 Ent := Tref;
2115 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2116
2117 if Ctyp = '+'
2118 and then Present (Full_View (Ent))
2119 then
2120 Ent := Underlying_Type (Ent);
2121
2122 if Present (Ent) then
2123 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2124 end if;
2125 end if;
2126
2127 Write_Info_Char (Ctyp);
2128 end;
2129
2130 Write_Info_Nat
2131 (Int (Get_Column_Number (Sloc (Tref))));
2132
2133 -- If the type comes from an instantiation, add the
2134 -- corresponding info.
2135
2136 Output_Instantiation_Refs (Sloc (Tref));
2137 Write_Info_Char (Right);
2138 end if;
2139 end if;
2140 end Check_Type_Reference;
2141
2142 -------------------------------
2143 -- Output_Instantiation_Refs --
2144 -------------------------------
2145
2146 procedure Output_Instantiation_Refs (Loc : Source_Ptr) is
2147 Iloc : constant Source_Ptr := Instantiation_Location (Loc);
2148 Lun : Unit_Number_Type;
2149 Cu : constant Unit_Number_Type := Curru;
2150
2151 begin
2152 -- Nothing to do if this is not an instantiation
2153
2154 if Iloc = No_Location then
2155 return;
2156 end if;
2157
2158 -- Output instantiation reference
2159
2160 Write_Info_Char ('[');
2161 Lun := Get_Source_Unit (Iloc);
2162
2163 if Lun /= Curru then
2164 Curru := Lun;
2165 Write_Info_Nat (Dependency_Num (Curru));
2166 Write_Info_Char ('|');
2167 end if;
2168
2169 Write_Info_Nat (Int (Get_Logical_Line_Number (Iloc)));
2170
2171 -- Recursive call to get nested instantiations
2172
2173 Output_Instantiation_Refs (Iloc);
2174
2175 -- Output final ] after call to get proper nesting
2176
2177 Write_Info_Char (']');
2178 Curru := Cu;
2179 return;
2180 end Output_Instantiation_Refs;
2181
2182 --------------------------
2183 -- Output_Overridden_Op --
2184 --------------------------
2185
2186 procedure Output_Overridden_Op (Old_E : Entity_Id) is
2187 Op : Entity_Id;
2188
2189 begin
2190 -- The overridden operation has an implicit declaration
2191 -- at the point of derivation. What we want to display
2192 -- is the original operation, which has the actual body
2193 -- (or abstract declaration) that is being overridden.
2194 -- The overridden operation is not always set, e.g. when
2195 -- it is a predefined operator.
2196
2197 if No (Old_E) then
2198 return;
2199
2200 -- Follow alias chain if one is present
2201
2202 elsif Present (Alias (Old_E)) then
2203
2204 -- The subprogram may have been implicitly inherited
2205 -- through several levels of derivation, so find the
2206 -- ultimate (source) ancestor.
2207
2208 Op := Ultimate_Alias (Old_E);
2209
2210 -- Normal case of no alias present. We omit generated
2211 -- primitives like tagged equality, that have no source
2212 -- representation.
2213
2214 else
2215 Op := Old_E;
2216 end if;
2217
2218 if Present (Op)
2219 and then Sloc (Op) /= Standard_Location
2220 and then Comes_From_Source (Op)
2221 then
2222 declare
2223 Loc : constant Source_Ptr := Sloc (Op);
2224 Par_Unit : constant Unit_Number_Type :=
2225 Get_Source_Unit (Loc);
2226
2227 begin
2228 Write_Info_Char ('<');
2229
2230 if Par_Unit /= Curxu then
2231 Write_Info_Nat (Dependency_Num (Par_Unit));
2232 Write_Info_Char ('|');
2233 end if;
2234
2235 Write_Info_Nat (Int (Get_Logical_Line_Number (Loc)));
2236 Write_Info_Char ('p');
2237 Write_Info_Nat (Int (Get_Column_Number (Loc)));
2238 Write_Info_Char ('>');
2239 end;
2240 end if;
2241 end Output_Overridden_Op;
2242
2243 -- Start of processing for Output_One_Ref
2244
2245 begin
2246 Ent := XE.Key.Ent;
2247
2248 -- Do not generate reference information for an ignored Ghost
2249 -- entity because neither the entity nor its references will
2250 -- appear in the final tree.
2251
2252 if Is_Ignored_Ghost_Entity (Ent) then
2253 goto Continue;
2254 end if;
2255
2256 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2257
2258 -- Skip reference if it is the only reference to an entity,
2259 -- and it is an END line reference, and the entity is not in
2260 -- the current extended source. This prevents junk entries
2261 -- consisting only of packages with END lines, where no
2262 -- entity from the package is actually referenced.
2263
2264 if XE.Key.Typ = 'e'
2265 and then Ent /= Curent
2266 and then (Refno = Nrefs
2267 or else
2268 Ent /= Xrefs.Table (Rnums (Refno + 1)).Key.Ent)
2269 and then not In_Extended_Main_Source_Unit (Ent)
2270 then
2271 goto Continue;
2272 end if;
2273
2274 -- For private type, get full view type
2275
2276 if Ctyp = '+'
2277 and then Present (Full_View (XE.Key.Ent))
2278 then
2279 Ent := Underlying_Type (Ent);
2280
2281 if Present (Ent) then
2282 Ctyp := Xref_Entity_Letters (Ekind (Ent));
2283 end if;
2284 end if;
2285
2286 -- Special exception for Boolean
2287
2288 if Ctyp = 'E' and then Is_Boolean_Type (Ent) then
2289 Ctyp := 'B';
2290 end if;
2291
2292 -- For variable reference, get corresponding type
2293
2294 if Ctyp = '*' then
2295 Ent := Etype (XE.Key.Ent);
2296 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
2297
2298 -- If variable is private type, get full view type
2299
2300 if Ctyp = '+'
2301 and then Present (Full_View (Etype (XE.Key.Ent)))
2302 then
2303 Ent := Underlying_Type (Etype (XE.Key.Ent));
2304
2305 if Present (Ent) then
2306 Ctyp := Fold_Lower (Xref_Entity_Letters (Ekind (Ent)));
2307 end if;
2308
2309 elsif Is_Generic_Type (Ent) then
2310
2311 -- If the type of the entity is a generic private type,
2312 -- there is no usable full view, so retain the indication
2313 -- that this is an object.
2314
2315 Ctyp := '*';
2316 end if;
2317
2318 -- Special handling for access parameters and objects and
2319 -- components of an anonymous access type.
2320
2321 if Ekind (Etype (XE.Key.Ent)) in
2322 E_Anonymous_Access_Type
2323 | E_Anonymous_Access_Subprogram_Type
2324 | E_Anonymous_Access_Protected_Subprogram_Type
2325 then
2326 if Is_Formal (XE.Key.Ent)
2327 or else
2328 Ekind (XE.Key.Ent) in
2329 E_Variable | E_Constant | E_Component
2330 then
2331 Ctyp := 'p';
2332 end if;
2333
2334 -- Special handling for Boolean
2335
2336 elsif Ctyp = 'e' and then Is_Boolean_Type (Ent) then
2337 Ctyp := 'b';
2338 end if;
2339 end if;
2340
2341 -- Special handling for abstract types and operations
2342
2343 if Is_Overloadable (XE.Key.Ent)
2344 and then Is_Abstract_Subprogram (XE.Key.Ent)
2345 then
2346 if Ctyp = 'U' then
2347 Ctyp := 'x'; -- Abstract procedure
2348
2349 elsif Ctyp = 'V' then
2350 Ctyp := 'y'; -- Abstract function
2351 end if;
2352
2353 elsif Is_Type (XE.Key.Ent)
2354 and then Is_Abstract_Type (XE.Key.Ent)
2355 then
2356 if Is_Interface (XE.Key.Ent) then
2357 Ctyp := 'h';
2358
2359 elsif Ctyp = 'R' then
2360 Ctyp := 'H'; -- Abstract type
2361 end if;
2362 end if;
2363
2364 -- Only output reference if interesting type of entity
2365
2366 if Ctyp = ' '
2367
2368 -- Suppress references to object definitions, used for local
2369 -- references.
2370
2371 or else XE.Key.Typ = 'D'
2372 or else XE.Key.Typ = 'I'
2373
2374 -- Suppress self references, except for bodies that act as
2375 -- specs.
2376
2377 or else (XE.Key.Loc = XE.Def
2378 and then
2379 (XE.Key.Typ /= 'b'
2380 or else not Is_Subprogram (XE.Key.Ent)))
2381
2382 -- Also suppress definitions of body formals (we only
2383 -- treat these as references, and the references were
2384 -- separately recorded).
2385
2386 or else (Is_Formal (XE.Key.Ent)
2387 and then Present (Spec_Entity (XE.Key.Ent)))
2388 then
2389 null;
2390
2391 else
2392 -- Start new Xref section if new xref unit
2393
2394 if XE.Key.Eun /= Curxu then
2395 if Write_Info_Col > 1 then
2396 Write_Info_EOL;
2397 end if;
2398
2399 Curxu := XE.Key.Eun;
2400
2401 Write_Info_Initiate ('X');
2402 Write_Info_Char (' ');
2403 Write_Info_Nat (Dependency_Num (XE.Key.Eun));
2404 Write_Info_Char (' ');
2405 Write_Info_Name
2406 (Reference_Name (Source_Index (XE.Key.Eun)));
2407 end if;
2408
2409 -- Start new Entity line if new entity. Note that we
2410 -- consider two entities the same if they have the same
2411 -- name and source location. This causes entities in
2412 -- instantiations to be treated as though they referred
2413 -- to the template.
2414
2415 if No (Curent)
2416 or else
2417 (XE.Key.Ent /= Curent
2418 and then
2419 (Name_Change (XE.Key.Ent) or else XE.Def /= Curdef))
2420 then
2421 Curent := XE.Key.Ent;
2422 Curdef := XE.Def;
2423
2424 Get_Unqualified_Name_String (Chars (XE.Key.Ent));
2425 Curlen := Name_Len;
2426 Curnam (1 .. Curlen) := Name_Buffer (1 .. Curlen);
2427
2428 if Write_Info_Col > 1 then
2429 Write_Info_EOL;
2430 end if;
2431
2432 -- Write column number information
2433
2434 Write_Info_Nat (Int (Get_Logical_Line_Number (XE.Def)));
2435 Write_Info_Char (Ctyp);
2436 Write_Info_Nat (Int (Get_Column_Number (XE.Def)));
2437
2438 -- Write level information
2439
2440 Write_Level_Info : declare
2441 function Is_Visible_Generic_Entity
2442 (E : Entity_Id) return Boolean;
2443 -- Check whether E is declared in the visible part
2444 -- of a generic package. For source navigation
2445 -- purposes, treat this as a visible entity.
2446
2447 function Is_Private_Record_Component
2448 (E : Entity_Id) return Boolean;
2449 -- Check whether E is a non-inherited component of a
2450 -- private extension. Even if the enclosing record is
2451 -- public, we want to treat the component as private
2452 -- for navigation purposes.
2453
2454 ---------------------------------
2455 -- Is_Private_Record_Component --
2456 ---------------------------------
2457
2458 function Is_Private_Record_Component
2459 (E : Entity_Id) return Boolean
2460 is
2461 S : constant Entity_Id := Scope (E);
2462 begin
2463 return
2464 Ekind (E) = E_Component
2465 and then Nkind (Declaration_Node (S)) =
2466 N_Private_Extension_Declaration
2467 and then Original_Record_Component (E) = E;
2468 end Is_Private_Record_Component;
2469
2470 -------------------------------
2471 -- Is_Visible_Generic_Entity --
2472 -------------------------------
2473
2474 function Is_Visible_Generic_Entity
2475 (E : Entity_Id) return Boolean
2476 is
2477 Par : Node_Id;
2478
2479 begin
2480 -- The Present check here is an error defense
2481
2482 if Present (Scope (E))
2483 and then Ekind (Scope (E)) /= E_Generic_Package
2484 then
2485 return False;
2486 end if;
2487
2488 Par := Parent (E);
2489 while Present (Par) loop
2490 if
2491 Nkind (Par) = N_Generic_Package_Declaration
2492 then
2493 -- Entity is a generic formal
2494
2495 return False;
2496
2497 elsif
2498 Nkind (Parent (Par)) = N_Package_Specification
2499 then
2500 return
2501 Is_List_Member (Par)
2502 and then List_Containing (Par) =
2503 Visible_Declarations (Parent (Par));
2504 else
2505 Par := Parent (Par);
2506 end if;
2507 end loop;
2508
2509 return False;
2510 end Is_Visible_Generic_Entity;
2511
2512 -- Start of processing for Write_Level_Info
2513
2514 begin
2515 if Is_Hidden (Curent)
2516 or else Is_Private_Record_Component (Curent)
2517 then
2518 Write_Info_Char (' ');
2519
2520 elsif
2521 Is_Public (Curent)
2522 or else Is_Visible_Generic_Entity (Curent)
2523 then
2524 Write_Info_Char ('*');
2525
2526 else
2527 Write_Info_Char (' ');
2528 end if;
2529 end Write_Level_Info;
2530
2531 -- Output entity name. We use the occurrence from the
2532 -- actual source program at the definition point.
2533
2534 declare
2535 Ent_Name : constant String :=
2536 Exact_Source_Name (Sloc (XE.Key.Ent));
2537 begin
2538 for C in Ent_Name'Range loop
2539 Write_Info_Char (Ent_Name (C));
2540 end loop;
2541 end;
2542
2543 -- See if we have a renaming reference
2544
2545 if Is_Object (XE.Key.Ent)
2546 and then Present (Renamed_Object (XE.Key.Ent))
2547 then
2548 Rref := Renamed_Object (XE.Key.Ent);
2549
2550 elsif Is_Overloadable (XE.Key.Ent)
2551 and then Nkind (Parent (Declaration_Node (XE.Key.Ent)))
2552 = N_Subprogram_Renaming_Declaration
2553 then
2554 Rref := Name (Parent (Declaration_Node (XE.Key.Ent)));
2555
2556 elsif Ekind (XE.Key.Ent) = E_Package
2557 and then Nkind (Declaration_Node (XE.Key.Ent)) =
2558 N_Package_Renaming_Declaration
2559 then
2560 Rref := Name (Declaration_Node (XE.Key.Ent));
2561
2562 else
2563 Rref := Empty;
2564 end if;
2565
2566 if Present (Rref) then
2567 if Nkind (Rref) = N_Expanded_Name then
2568 Rref := Selector_Name (Rref);
2569 end if;
2570
2571 if Nkind (Rref) = N_Identifier
2572 or else Nkind (Rref) = N_Operator_Symbol
2573 then
2574 null;
2575
2576 -- For renamed array components, use the array name
2577 -- for the renamed entity, which reflect the fact that
2578 -- in general the whole array is aliased.
2579
2580 elsif Nkind (Rref) = N_Indexed_Component then
2581 if Nkind (Prefix (Rref)) = N_Identifier then
2582 Rref := Prefix (Rref);
2583 elsif Nkind (Prefix (Rref)) = N_Expanded_Name then
2584 Rref := Selector_Name (Prefix (Rref));
2585 else
2586 Rref := Empty;
2587 end if;
2588
2589 else
2590 Rref := Empty;
2591 end if;
2592 end if;
2593
2594 -- Write out renaming reference if we have one
2595
2596 if Present (Rref) then
2597 Write_Info_Char ('=');
2598 Write_Info_Nat
2599 (Int (Get_Logical_Line_Number (Sloc (Rref))));
2600 Write_Info_Char (':');
2601 Write_Info_Nat
2602 (Int (Get_Column_Number (Sloc (Rref))));
2603 end if;
2604
2605 -- Indicate that the entity is in the unit of the current
2606 -- xref section.
2607
2608 Curru := Curxu;
2609
2610 -- Write out information about generic parent, if entity
2611 -- is an instance.
2612
2613 if Is_Generic_Instance (XE.Key.Ent) then
2614 declare
2615 Gen_Par : constant Entity_Id :=
2616 Generic_Parent
2617 (Specification
2618 (Unit_Declaration_Node
2619 (XE.Key.Ent)));
2620 Loc : constant Source_Ptr := Sloc (Gen_Par);
2621 Gen_U : constant Unit_Number_Type :=
2622 Get_Source_Unit (Loc);
2623
2624 begin
2625 Write_Info_Char ('[');
2626
2627 if Curru /= Gen_U then
2628 Write_Info_Nat (Dependency_Num (Gen_U));
2629 Write_Info_Char ('|');
2630 end if;
2631
2632 Write_Info_Nat
2633 (Int (Get_Logical_Line_Number (Loc)));
2634 Write_Info_Char (']');
2635 end;
2636 end if;
2637
2638 -- See if we have a type reference and if so output
2639
2640 Check_Type_Reference (XE.Key.Ent, False);
2641
2642 -- Additional information for types with progenitors,
2643 -- including synchronized tagged types.
2644
2645 declare
2646 Typ : constant Entity_Id := XE.Key.Ent;
2647 Elmt : Elmt_Id;
2648
2649 begin
2650 if Is_Record_Type (Typ)
2651 and then Present (Interfaces (Typ))
2652 then
2653 Elmt := First_Elmt (Interfaces (Typ));
2654
2655 elsif Is_Concurrent_Type (Typ)
2656 and then Present (Corresponding_Record_Type (Typ))
2657 and then Present (
2658 Interfaces (Corresponding_Record_Type (Typ)))
2659 then
2660 Elmt :=
2661 First_Elmt (
2662 Interfaces (Corresponding_Record_Type (Typ)));
2663
2664 else
2665 Elmt := No_Elmt;
2666 end if;
2667
2668 while Present (Elmt) loop
2669 Check_Type_Reference (Node (Elmt), True);
2670 Next_Elmt (Elmt);
2671 end loop;
2672 end;
2673
2674 -- For array types, list index types as well. (This is
2675 -- not C, indexes have distinct types).
2676
2677 if Is_Array_Type (XE.Key.Ent) then
2678 declare
2679 A_Typ : constant Entity_Id := XE.Key.Ent;
2680 Indx : Node_Id;
2681
2682 begin
2683 -- If this is a derived array type, we have
2684 -- output the parent type, so add the component
2685 -- type now.
2686
2687 if Is_Derived_Type (A_Typ) then
2688 Check_Type_Reference
2689 (Component_Type (A_Typ), False, True);
2690 end if;
2691
2692 -- Add references to index types.
2693
2694 Indx := First_Index (XE.Key.Ent);
2695 while Present (Indx) loop
2696 Check_Type_Reference
2697 (First_Subtype (Etype (Indx)), True);
2698 Next_Index (Indx);
2699 end loop;
2700 end;
2701 end if;
2702
2703 -- If the entity is an overriding operation, write info
2704 -- on operation that was overridden.
2705
2706 if Is_Subprogram (XE.Key.Ent)
2707 and then Present (Overridden_Operation (XE.Key.Ent))
2708 then
2709 Output_Overridden_Op
2710 (Overridden_Operation (XE.Key.Ent));
2711 end if;
2712
2713 -- End of processing for entity output
2714
2715 Crloc := No_Location;
2716 end if;
2717
2718 -- Output the reference if it is not as the same location
2719 -- as the previous one, or it is a read-reference that
2720 -- indicates that the entity is an in-out actual in a call.
2721
2722 if XE.Key.Loc /= No_Location
2723 and then
2724 (XE.Key.Loc /= Crloc
2725 or else (Prevt = 'm' and then XE.Key.Typ = 'r'))
2726 then
2727 Crloc := XE.Key.Loc;
2728 Prevt := XE.Key.Typ;
2729
2730 -- Start continuation if line full, else blank
2731
2732 if Write_Info_Col > 72 then
2733 Write_Info_EOL;
2734 Write_Info_Initiate ('.');
2735 end if;
2736
2737 Write_Info_Char (' ');
2738
2739 -- Output file number if changed
2740
2741 if XE.Key.Lun /= Curru then
2742 Curru := XE.Key.Lun;
2743 Write_Info_Nat (Dependency_Num (Curru));
2744 Write_Info_Char ('|');
2745 end if;
2746
2747 Write_Info_Nat
2748 (Int (Get_Logical_Line_Number (XE.Key.Loc)));
2749 Write_Info_Char (XE.Key.Typ);
2750
2751 if Is_Overloadable (XE.Key.Ent) then
2752 if (Is_Imported (XE.Key.Ent) and then XE.Key.Typ = 'b')
2753 or else
2754 (Is_Exported (XE.Key.Ent) and then XE.Key.Typ = 'i')
2755 then
2756 Output_Import_Export_Info (XE.Key.Ent);
2757 end if;
2758 end if;
2759
2760 Write_Info_Nat (Int (Get_Column_Number (XE.Key.Loc)));
2761
2762 Output_Instantiation_Refs (Sloc (XE.Key.Ent));
2763 end if;
2764 end if;
2765 end Output_One_Ref;
2766
2767 <<Continue>>
2768 null;
2769 end loop;
2770
2771 Write_Info_EOL;
2772 end Output_Refs;
2773 end Output_References;
2774
2775 ---------------------------------
2776 -- Process_Deferred_References --
2777 ---------------------------------
2778
2779 procedure Process_Deferred_References is
2780 begin
2781 for J in Deferred_References.First .. Deferred_References.Last loop
2782 declare
2783 D : Deferred_Reference_Entry renames Deferred_References.Table (J);
2784
2785 begin
2786 case Is_LHS (D.N) is
2787 when Yes =>
2788 Generate_Reference (D.E, D.N, 'm');
2789
2790 when No =>
2791 Generate_Reference (D.E, D.N, 'r');
2792
2793 -- Not clear if Unknown can occur at this stage, but if it
2794 -- does we will treat it as a normal reference.
2795
2796 when Unknown =>
2797 Generate_Reference (D.E, D.N, 'r');
2798 end case;
2799 end;
2800 end loop;
2801
2802 -- Clear processed entries from table
2803
2804 Deferred_References.Init;
2805 end Process_Deferred_References;
2806
2807 -- Start of elaboration for Lib.Xref
2808
2809 begin
2810 -- Reset is necessary because Elmt_Ptr does not default to Null_Ptr,
2811 -- because it's not an access type.
2812
2813 Xref_Set.Reset;
2814 end Lib.Xref;