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