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