OSDN Git Service

2010-10-25 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_util.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             S E M _ U T I L                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2010, 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 Casing;   use Casing;
28 with Checks;   use Checks;
29 with Debug;    use Debug;
30 with Errout;   use Errout;
31 with Elists;   use Elists;
32 with Exp_Ch11; use Exp_Ch11;
33 with Exp_Disp; use Exp_Disp;
34 with Exp_Tss;  use Exp_Tss;
35 with Exp_Util; use Exp_Util;
36 with Fname;    use Fname;
37 with Freeze;   use Freeze;
38 with Lib;      use Lib;
39 with Lib.Xref; use Lib.Xref;
40 with Nlists;   use Nlists;
41 with Output;   use Output;
42 with Opt;      use Opt;
43 with Rtsfind;  use Rtsfind;
44 with Sem;      use Sem;
45 with Sem_Aux;  use Sem_Aux;
46 with Sem_Attr; use Sem_Attr;
47 with Sem_Ch8;  use Sem_Ch8;
48 with Sem_Disp; use Sem_Disp;
49 with Sem_Eval; use Sem_Eval;
50 with Sem_Res;  use Sem_Res;
51 with Sem_Type; use Sem_Type;
52 with Sinfo;    use Sinfo;
53 with Sinput;   use Sinput;
54 with Stand;    use Stand;
55 with Style;
56 with Stringt;  use Stringt;
57 with Table;
58 with Targparm; use Targparm;
59 with Tbuild;   use Tbuild;
60 with Ttypes;   use Ttypes;
61 with Uname;    use Uname;
62
63 with GNAT.HTable; use GNAT.HTable;
64
65 package body Sem_Util is
66
67    ----------------------------------------
68    -- Global_Variables for New_Copy_Tree --
69    ----------------------------------------
70
71    --  These global variables are used by New_Copy_Tree. See description
72    --  of the body of this subprogram for details. Global variables can be
73    --  safely used by New_Copy_Tree, since there is no case of a recursive
74    --  call from the processing inside New_Copy_Tree.
75
76    NCT_Hash_Threshhold : constant := 20;
77    --  If there are more than this number of pairs of entries in the
78    --  map, then Hash_Tables_Used will be set, and the hash tables will
79    --  be initialized and used for the searches.
80
81    NCT_Hash_Tables_Used : Boolean := False;
82    --  Set to True if hash tables are in use
83
84    NCT_Table_Entries : Nat;
85    --  Count entries in table to see if threshhold is reached
86
87    NCT_Hash_Table_Setup : Boolean := False;
88    --  Set to True if hash table contains data. We set this True if we
89    --  setup the hash table with data, and leave it set permanently
90    --  from then on, this is a signal that second and subsequent users
91    --  of the hash table must clear the old entries before reuse.
92
93    subtype NCT_Header_Num is Int range 0 .. 511;
94    --  Defines range of headers in hash tables (512 headers)
95
96    ----------------------------------
97    -- Order Dependence (AI05-0144) --
98    ----------------------------------
99
100    --  Each actual in a call is entered into the table below. A flag indicates
101    --  whether the corresponding formal is OUT or IN OUT. Each top-level call
102    --  (procedure call, condition, assignment) examines all the actuals for a
103    --  possible order dependence. The table is reset after each such check.
104    --  The actuals to be checked in a call to Check_Order_Dependence are at
105    --  positions 1 .. Last.
106
107    type Actual_Name is record
108       Act         : Node_Id;
109       Is_Writable : Boolean;
110    end record;
111
112    package Actuals_In_Call is new Table.Table (
113       Table_Component_Type => Actual_Name,
114       Table_Index_Type     => Int,
115       Table_Low_Bound      => 0,
116       Table_Initial        => 10,
117       Table_Increment      => 100,
118       Table_Name           => "Actuals");
119
120    -----------------------
121    -- Local Subprograms --
122    -----------------------
123
124    function Build_Component_Subtype
125      (C   : List_Id;
126       Loc : Source_Ptr;
127       T   : Entity_Id) return Node_Id;
128    --  This function builds the subtype for Build_Actual_Subtype_Of_Component
129    --  and Build_Discriminal_Subtype_Of_Component. C is a list of constraints,
130    --  Loc is the source location, T is the original subtype.
131
132    function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean;
133    --  Subsidiary to Is_Fully_Initialized_Type. For an unconstrained type
134    --  with discriminants whose default values are static, examine only the
135    --  components in the selected variant to determine whether all of them
136    --  have a default.
137
138    function Has_Null_Extension (T : Entity_Id) return Boolean;
139    --  T is a derived tagged type. Check whether the type extension is null.
140    --  If the parent type is fully initialized, T can be treated as such.
141
142    ------------------------------
143    --  Abstract_Interface_List --
144    ------------------------------
145
146    function Abstract_Interface_List (Typ : Entity_Id) return List_Id is
147       Nod : Node_Id;
148
149    begin
150       if Is_Concurrent_Type (Typ) then
151
152          --  If we are dealing with a synchronized subtype, go to the base
153          --  type, whose declaration has the interface list.
154
155          --  Shouldn't this be Declaration_Node???
156
157          Nod := Parent (Base_Type (Typ));
158
159          if Nkind (Nod) = N_Full_Type_Declaration then
160             return Empty_List;
161          end if;
162
163       elsif Ekind (Typ) = E_Record_Type_With_Private then
164          if Nkind (Parent (Typ)) = N_Full_Type_Declaration then
165             Nod := Type_Definition (Parent (Typ));
166
167          elsif Nkind (Parent (Typ)) = N_Private_Type_Declaration then
168             if Present (Full_View (Typ)) then
169                Nod := Type_Definition (Parent (Full_View (Typ)));
170
171             --  If the full-view is not available we cannot do anything else
172             --  here (the source has errors).
173
174             else
175                return Empty_List;
176             end if;
177
178          --  Support for generic formals with interfaces is still missing ???
179
180          elsif Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
181             return Empty_List;
182
183          else
184             pragma Assert
185               (Nkind (Parent (Typ)) = N_Private_Extension_Declaration);
186             Nod := Parent (Typ);
187          end if;
188
189       elsif Ekind (Typ) = E_Record_Subtype then
190          Nod := Type_Definition (Parent (Etype (Typ)));
191
192       elsif Ekind (Typ) = E_Record_Subtype_With_Private then
193
194          --  Recurse, because parent may still be a private extension. Also
195          --  note that the full view of the subtype or the full view of its
196          --  base type may (both) be unavailable.
197
198          return Abstract_Interface_List (Etype (Typ));
199
200       else pragma Assert ((Ekind (Typ)) = E_Record_Type);
201          if Nkind (Parent (Typ)) = N_Formal_Type_Declaration then
202             Nod := Formal_Type_Definition (Parent (Typ));
203          else
204             Nod := Type_Definition (Parent (Typ));
205          end if;
206       end if;
207
208       return Interface_List (Nod);
209    end Abstract_Interface_List;
210
211    --------------------------------
212    -- Add_Access_Type_To_Process --
213    --------------------------------
214
215    procedure Add_Access_Type_To_Process (E : Entity_Id; A : Entity_Id) is
216       L : Elist_Id;
217
218    begin
219       Ensure_Freeze_Node (E);
220       L := Access_Types_To_Process (Freeze_Node (E));
221
222       if No (L) then
223          L := New_Elmt_List;
224          Set_Access_Types_To_Process (Freeze_Node (E), L);
225       end if;
226
227       Append_Elmt (A, L);
228    end Add_Access_Type_To_Process;
229
230    ----------------------------
231    -- Add_Global_Declaration --
232    ----------------------------
233
234    procedure Add_Global_Declaration (N : Node_Id) is
235       Aux_Node : constant Node_Id := Aux_Decls_Node (Cunit (Current_Sem_Unit));
236
237    begin
238       if No (Declarations (Aux_Node)) then
239          Set_Declarations (Aux_Node, New_List);
240       end if;
241
242       Append_To (Declarations (Aux_Node), N);
243       Analyze (N);
244    end Add_Global_Declaration;
245
246    -----------------
247    -- Addressable --
248    -----------------
249
250    --  For now, just 8/16/32/64. but analyze later if AAMP is special???
251
252    function Addressable (V : Uint) return Boolean is
253    begin
254       return V = Uint_8  or else
255              V = Uint_16 or else
256              V = Uint_32 or else
257              V = Uint_64;
258    end Addressable;
259
260    function Addressable (V : Int) return Boolean is
261    begin
262       return V = 8  or else
263              V = 16 or else
264              V = 32 or else
265              V = 64;
266    end Addressable;
267
268    -----------------------
269    -- Alignment_In_Bits --
270    -----------------------
271
272    function Alignment_In_Bits (E : Entity_Id) return Uint is
273    begin
274       return Alignment (E) * System_Storage_Unit;
275    end Alignment_In_Bits;
276
277    -----------------------------------------
278    -- Apply_Compile_Time_Constraint_Error --
279    -----------------------------------------
280
281    procedure Apply_Compile_Time_Constraint_Error
282      (N      : Node_Id;
283       Msg    : String;
284       Reason : RT_Exception_Code;
285       Ent    : Entity_Id  := Empty;
286       Typ    : Entity_Id  := Empty;
287       Loc    : Source_Ptr := No_Location;
288       Rep    : Boolean    := True;
289       Warn   : Boolean    := False)
290    is
291       Stat   : constant Boolean := Is_Static_Expression (N);
292       R_Stat : constant Node_Id :=
293                  Make_Raise_Constraint_Error (Sloc (N), Reason => Reason);
294       Rtyp   : Entity_Id;
295
296    begin
297       if No (Typ) then
298          Rtyp := Etype (N);
299       else
300          Rtyp := Typ;
301       end if;
302
303       Discard_Node
304         (Compile_Time_Constraint_Error (N, Msg, Ent, Loc, Warn => Warn));
305
306       if not Rep then
307          return;
308       end if;
309
310       --  Now we replace the node by an N_Raise_Constraint_Error node
311       --  This does not need reanalyzing, so set it as analyzed now.
312
313       Rewrite (N, R_Stat);
314       Set_Analyzed (N, True);
315
316       Set_Etype (N, Rtyp);
317       Set_Raises_Constraint_Error (N);
318
319       --  Now deal with possible local raise handling
320
321       Possible_Local_Raise (N, Standard_Constraint_Error);
322
323       --  If the original expression was marked as static, the result is
324       --  still marked as static, but the Raises_Constraint_Error flag is
325       --  always set so that further static evaluation is not attempted.
326
327       if Stat then
328          Set_Is_Static_Expression (N);
329       end if;
330    end Apply_Compile_Time_Constraint_Error;
331
332    --------------------------------
333    -- Bad_Predicated_Subtype_Use --
334    --------------------------------
335
336    procedure Bad_Predicated_Subtype_Use
337      (Msg : String;
338       N   : Node_Id;
339       Typ : Entity_Id)
340    is
341    begin
342       if Has_Predicates (Typ) then
343          if Is_Generic_Actual_Type (Typ) then
344             Error_Msg_FE (Msg & '?', N, Typ);
345             Error_Msg_F ("\Program_Error will be raised at run time?", N);
346             Insert_Action (N,
347               Make_Raise_Program_Error (Sloc (N),
348                 Reason => PE_Bad_Predicated_Generic_Type));
349
350          else
351             Error_Msg_FE (Msg, N, Typ);
352          end if;
353       end if;
354    end Bad_Predicated_Subtype_Use;
355
356    --------------------------
357    -- Build_Actual_Subtype --
358    --------------------------
359
360    function Build_Actual_Subtype
361      (T : Entity_Id;
362       N : Node_Or_Entity_Id) return Node_Id
363    is
364       Loc : Source_Ptr;
365       --  Normally Sloc (N), but may point to corresponding body in some cases
366
367       Constraints : List_Id;
368       Decl        : Node_Id;
369       Discr       : Entity_Id;
370       Hi          : Node_Id;
371       Lo          : Node_Id;
372       Subt        : Entity_Id;
373       Disc_Type   : Entity_Id;
374       Obj         : Node_Id;
375
376    begin
377       Loc := Sloc (N);
378
379       if Nkind (N) = N_Defining_Identifier then
380          Obj := New_Reference_To (N, Loc);
381
382          --  If this is a formal parameter of a subprogram declaration, and
383          --  we are compiling the body, we want the declaration for the
384          --  actual subtype to carry the source position of the body, to
385          --  prevent anomalies in gdb when stepping through the code.
386
387          if Is_Formal (N) then
388             declare
389                Decl : constant Node_Id := Unit_Declaration_Node (Scope (N));
390             begin
391                if Nkind (Decl) = N_Subprogram_Declaration
392                  and then Present (Corresponding_Body (Decl))
393                then
394                   Loc := Sloc (Corresponding_Body (Decl));
395                end if;
396             end;
397          end if;
398
399       else
400          Obj := N;
401       end if;
402
403       if Is_Array_Type (T) then
404          Constraints := New_List;
405          for J in 1 .. Number_Dimensions (T) loop
406
407             --  Build an array subtype declaration with the nominal subtype and
408             --  the bounds of the actual. Add the declaration in front of the
409             --  local declarations for the subprogram, for analysis before any
410             --  reference to the formal in the body.
411
412             Lo :=
413               Make_Attribute_Reference (Loc,
414                 Prefix         =>
415                   Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
416                 Attribute_Name => Name_First,
417                 Expressions    => New_List (
418                   Make_Integer_Literal (Loc, J)));
419
420             Hi :=
421               Make_Attribute_Reference (Loc,
422                 Prefix         =>
423                   Duplicate_Subexpr_No_Checks (Obj, Name_Req => True),
424                 Attribute_Name => Name_Last,
425                 Expressions    => New_List (
426                   Make_Integer_Literal (Loc, J)));
427
428             Append (Make_Range (Loc, Lo, Hi), Constraints);
429          end loop;
430
431       --  If the type has unknown discriminants there is no constrained
432       --  subtype to build. This is never called for a formal or for a
433       --  lhs, so returning the type is ok ???
434
435       elsif Has_Unknown_Discriminants (T) then
436          return T;
437
438       else
439          Constraints := New_List;
440
441          --  Type T is a generic derived type, inherit the discriminants from
442          --  the parent type.
443
444          if Is_Private_Type (T)
445            and then No (Full_View (T))
446
447             --  T was flagged as an error if it was declared as a formal
448             --  derived type with known discriminants. In this case there
449             --  is no need to look at the parent type since T already carries
450             --  its own discriminants.
451
452            and then not Error_Posted (T)
453          then
454             Disc_Type := Etype (Base_Type (T));
455          else
456             Disc_Type := T;
457          end if;
458
459          Discr := First_Discriminant (Disc_Type);
460          while Present (Discr) loop
461             Append_To (Constraints,
462               Make_Selected_Component (Loc,
463                 Prefix =>
464                   Duplicate_Subexpr_No_Checks (Obj),
465                 Selector_Name => New_Occurrence_Of (Discr, Loc)));
466             Next_Discriminant (Discr);
467          end loop;
468       end if;
469
470       Subt := Make_Temporary (Loc, 'S', Related_Node => N);
471       Set_Is_Internal (Subt);
472
473       Decl :=
474         Make_Subtype_Declaration (Loc,
475           Defining_Identifier => Subt,
476           Subtype_Indication =>
477             Make_Subtype_Indication (Loc,
478               Subtype_Mark => New_Reference_To (T,  Loc),
479               Constraint  =>
480                 Make_Index_Or_Discriminant_Constraint (Loc,
481                   Constraints => Constraints)));
482
483       Mark_Rewrite_Insertion (Decl);
484       return Decl;
485    end Build_Actual_Subtype;
486
487    ---------------------------------------
488    -- Build_Actual_Subtype_Of_Component --
489    ---------------------------------------
490
491    function Build_Actual_Subtype_Of_Component
492      (T : Entity_Id;
493       N : Node_Id) return Node_Id
494    is
495       Loc       : constant Source_Ptr := Sloc (N);
496       P         : constant Node_Id    := Prefix (N);
497       D         : Elmt_Id;
498       Id        : Node_Id;
499       Indx_Type : Entity_Id;
500
501       Deaccessed_T : Entity_Id;
502       --  This is either a copy of T, or if T is an access type, then it is
503       --  the directly designated type of this access type.
504
505       function Build_Actual_Array_Constraint return List_Id;
506       --  If one or more of the bounds of the component depends on
507       --  discriminants, build  actual constraint using the discriminants
508       --  of the prefix.
509
510       function Build_Actual_Record_Constraint return List_Id;
511       --  Similar to previous one, for discriminated components constrained
512       --  by the discriminant of the enclosing object.
513
514       -----------------------------------
515       -- Build_Actual_Array_Constraint --
516       -----------------------------------
517
518       function Build_Actual_Array_Constraint return List_Id is
519          Constraints : constant List_Id := New_List;
520          Indx        : Node_Id;
521          Hi          : Node_Id;
522          Lo          : Node_Id;
523          Old_Hi      : Node_Id;
524          Old_Lo      : Node_Id;
525
526       begin
527          Indx := First_Index (Deaccessed_T);
528          while Present (Indx) loop
529             Old_Lo := Type_Low_Bound  (Etype (Indx));
530             Old_Hi := Type_High_Bound (Etype (Indx));
531
532             if Denotes_Discriminant (Old_Lo) then
533                Lo :=
534                  Make_Selected_Component (Loc,
535                    Prefix => New_Copy_Tree (P),
536                    Selector_Name => New_Occurrence_Of (Entity (Old_Lo), Loc));
537
538             else
539                Lo := New_Copy_Tree (Old_Lo);
540
541                --  The new bound will be reanalyzed in the enclosing
542                --  declaration. For literal bounds that come from a type
543                --  declaration, the type of the context must be imposed, so
544                --  insure that analysis will take place. For non-universal
545                --  types this is not strictly necessary.
546
547                Set_Analyzed (Lo, False);
548             end if;
549
550             if Denotes_Discriminant (Old_Hi) then
551                Hi :=
552                  Make_Selected_Component (Loc,
553                    Prefix => New_Copy_Tree (P),
554                    Selector_Name => New_Occurrence_Of (Entity (Old_Hi), Loc));
555
556             else
557                Hi := New_Copy_Tree (Old_Hi);
558                Set_Analyzed (Hi, False);
559             end if;
560
561             Append (Make_Range (Loc, Lo, Hi), Constraints);
562             Next_Index (Indx);
563          end loop;
564
565          return Constraints;
566       end Build_Actual_Array_Constraint;
567
568       ------------------------------------
569       -- Build_Actual_Record_Constraint --
570       ------------------------------------
571
572       function Build_Actual_Record_Constraint return List_Id is
573          Constraints : constant List_Id := New_List;
574          D           : Elmt_Id;
575          D_Val       : Node_Id;
576
577       begin
578          D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
579          while Present (D) loop
580             if Denotes_Discriminant (Node (D)) then
581                D_Val :=  Make_Selected_Component (Loc,
582                  Prefix => New_Copy_Tree (P),
583                 Selector_Name => New_Occurrence_Of (Entity (Node (D)), Loc));
584
585             else
586                D_Val := New_Copy_Tree (Node (D));
587             end if;
588
589             Append (D_Val, Constraints);
590             Next_Elmt (D);
591          end loop;
592
593          return Constraints;
594       end Build_Actual_Record_Constraint;
595
596    --  Start of processing for Build_Actual_Subtype_Of_Component
597
598    begin
599       --  Why the test for Spec_Expression mode here???
600
601       if In_Spec_Expression then
602          return Empty;
603
604       --  More comments for the rest of this body would be good ???
605
606       elsif Nkind (N) = N_Explicit_Dereference then
607          if Is_Composite_Type (T)
608            and then not Is_Constrained (T)
609            and then not (Is_Class_Wide_Type (T)
610                           and then Is_Constrained (Root_Type (T)))
611            and then not Has_Unknown_Discriminants (T)
612          then
613             --  If the type of the dereference is already constrained, it is an
614             --  actual subtype.
615
616             if Is_Array_Type (Etype (N))
617               and then Is_Constrained (Etype (N))
618             then
619                return Empty;
620             else
621                Remove_Side_Effects (P);
622                return Build_Actual_Subtype (T, N);
623             end if;
624          else
625             return Empty;
626          end if;
627       end if;
628
629       if Ekind (T) = E_Access_Subtype then
630          Deaccessed_T := Designated_Type (T);
631       else
632          Deaccessed_T := T;
633       end if;
634
635       if Ekind (Deaccessed_T) = E_Array_Subtype then
636          Id := First_Index (Deaccessed_T);
637          while Present (Id) loop
638             Indx_Type := Underlying_Type (Etype (Id));
639
640             if Denotes_Discriminant (Type_Low_Bound  (Indx_Type))
641                  or else
642                Denotes_Discriminant (Type_High_Bound (Indx_Type))
643             then
644                Remove_Side_Effects (P);
645                return
646                  Build_Component_Subtype
647                    (Build_Actual_Array_Constraint, Loc, Base_Type (T));
648             end if;
649
650             Next_Index (Id);
651          end loop;
652
653       elsif Is_Composite_Type (Deaccessed_T)
654         and then Has_Discriminants (Deaccessed_T)
655         and then not Has_Unknown_Discriminants (Deaccessed_T)
656       then
657          D := First_Elmt (Discriminant_Constraint (Deaccessed_T));
658          while Present (D) loop
659             if Denotes_Discriminant (Node (D)) then
660                Remove_Side_Effects (P);
661                return
662                  Build_Component_Subtype (
663                    Build_Actual_Record_Constraint, Loc, Base_Type (T));
664             end if;
665
666             Next_Elmt (D);
667          end loop;
668       end if;
669
670       --  If none of the above, the actual and nominal subtypes are the same
671
672       return Empty;
673    end Build_Actual_Subtype_Of_Component;
674
675    -----------------------------
676    -- Build_Component_Subtype --
677    -----------------------------
678
679    function Build_Component_Subtype
680      (C   : List_Id;
681       Loc : Source_Ptr;
682       T   : Entity_Id) return Node_Id
683    is
684       Subt : Entity_Id;
685       Decl : Node_Id;
686
687    begin
688       --  Unchecked_Union components do not require component subtypes
689
690       if Is_Unchecked_Union (T) then
691          return Empty;
692       end if;
693
694       Subt := Make_Temporary (Loc, 'S');
695       Set_Is_Internal (Subt);
696
697       Decl :=
698         Make_Subtype_Declaration (Loc,
699           Defining_Identifier => Subt,
700           Subtype_Indication =>
701             Make_Subtype_Indication (Loc,
702               Subtype_Mark => New_Reference_To (Base_Type (T),  Loc),
703               Constraint  =>
704                 Make_Index_Or_Discriminant_Constraint (Loc,
705                   Constraints => C)));
706
707       Mark_Rewrite_Insertion (Decl);
708       return Decl;
709    end Build_Component_Subtype;
710
711    ---------------------------
712    -- Build_Default_Subtype --
713    ---------------------------
714
715    function Build_Default_Subtype
716      (T : Entity_Id;
717       N : Node_Id) return Entity_Id
718    is
719       Loc  : constant Source_Ptr := Sloc (N);
720       Disc : Entity_Id;
721
722    begin
723       if not Has_Discriminants (T) or else Is_Constrained (T) then
724          return T;
725       end if;
726
727       Disc := First_Discriminant (T);
728
729       if No (Discriminant_Default_Value (Disc)) then
730          return T;
731       end if;
732
733       declare
734          Act         : constant Entity_Id := Make_Temporary (Loc, 'S');
735          Constraints : constant List_Id := New_List;
736          Decl        : Node_Id;
737
738       begin
739          while Present (Disc) loop
740             Append_To (Constraints,
741               New_Copy_Tree (Discriminant_Default_Value (Disc)));
742             Next_Discriminant (Disc);
743          end loop;
744
745          Decl :=
746            Make_Subtype_Declaration (Loc,
747              Defining_Identifier => Act,
748              Subtype_Indication =>
749                Make_Subtype_Indication (Loc,
750                  Subtype_Mark => New_Occurrence_Of (T, Loc),
751                  Constraint =>
752                    Make_Index_Or_Discriminant_Constraint (Loc,
753                      Constraints => Constraints)));
754
755          Insert_Action (N, Decl);
756          Analyze (Decl);
757          return Act;
758       end;
759    end Build_Default_Subtype;
760
761    --------------------------------------------
762    -- Build_Discriminal_Subtype_Of_Component --
763    --------------------------------------------
764
765    function Build_Discriminal_Subtype_Of_Component
766      (T : Entity_Id) return Node_Id
767    is
768       Loc : constant Source_Ptr := Sloc (T);
769       D   : Elmt_Id;
770       Id  : Node_Id;
771
772       function Build_Discriminal_Array_Constraint return List_Id;
773       --  If one or more of the bounds of the component depends on
774       --  discriminants, build  actual constraint using the discriminants
775       --  of the prefix.
776
777       function Build_Discriminal_Record_Constraint return List_Id;
778       --  Similar to previous one, for discriminated components constrained
779       --  by the discriminant of the enclosing object.
780
781       ----------------------------------------
782       -- Build_Discriminal_Array_Constraint --
783       ----------------------------------------
784
785       function Build_Discriminal_Array_Constraint return List_Id is
786          Constraints : constant List_Id := New_List;
787          Indx        : Node_Id;
788          Hi          : Node_Id;
789          Lo          : Node_Id;
790          Old_Hi      : Node_Id;
791          Old_Lo      : Node_Id;
792
793       begin
794          Indx := First_Index (T);
795          while Present (Indx) loop
796             Old_Lo := Type_Low_Bound  (Etype (Indx));
797             Old_Hi := Type_High_Bound (Etype (Indx));
798
799             if Denotes_Discriminant (Old_Lo) then
800                Lo := New_Occurrence_Of (Discriminal (Entity (Old_Lo)), Loc);
801
802             else
803                Lo := New_Copy_Tree (Old_Lo);
804             end if;
805
806             if Denotes_Discriminant (Old_Hi) then
807                Hi := New_Occurrence_Of (Discriminal (Entity (Old_Hi)), Loc);
808
809             else
810                Hi := New_Copy_Tree (Old_Hi);
811             end if;
812
813             Append (Make_Range (Loc, Lo, Hi), Constraints);
814             Next_Index (Indx);
815          end loop;
816
817          return Constraints;
818       end Build_Discriminal_Array_Constraint;
819
820       -----------------------------------------
821       -- Build_Discriminal_Record_Constraint --
822       -----------------------------------------
823
824       function Build_Discriminal_Record_Constraint return List_Id is
825          Constraints : constant List_Id := New_List;
826          D           : Elmt_Id;
827          D_Val       : Node_Id;
828
829       begin
830          D := First_Elmt (Discriminant_Constraint (T));
831          while Present (D) loop
832             if Denotes_Discriminant (Node (D)) then
833                D_Val :=
834                  New_Occurrence_Of (Discriminal (Entity (Node (D))), Loc);
835
836             else
837                D_Val := New_Copy_Tree (Node (D));
838             end if;
839
840             Append (D_Val, Constraints);
841             Next_Elmt (D);
842          end loop;
843
844          return Constraints;
845       end Build_Discriminal_Record_Constraint;
846
847    --  Start of processing for Build_Discriminal_Subtype_Of_Component
848
849    begin
850       if Ekind (T) = E_Array_Subtype then
851          Id := First_Index (T);
852          while Present (Id) loop
853             if Denotes_Discriminant (Type_Low_Bound  (Etype (Id))) or else
854                Denotes_Discriminant (Type_High_Bound (Etype (Id)))
855             then
856                return Build_Component_Subtype
857                  (Build_Discriminal_Array_Constraint, Loc, T);
858             end if;
859
860             Next_Index (Id);
861          end loop;
862
863       elsif Ekind (T) = E_Record_Subtype
864         and then Has_Discriminants (T)
865         and then not Has_Unknown_Discriminants (T)
866       then
867          D := First_Elmt (Discriminant_Constraint (T));
868          while Present (D) loop
869             if Denotes_Discriminant (Node (D)) then
870                return Build_Component_Subtype
871                  (Build_Discriminal_Record_Constraint, Loc, T);
872             end if;
873
874             Next_Elmt (D);
875          end loop;
876       end if;
877
878       --  If none of the above, the actual and nominal subtypes are the same
879
880       return Empty;
881    end Build_Discriminal_Subtype_Of_Component;
882
883    ------------------------------
884    -- Build_Elaboration_Entity --
885    ------------------------------
886
887    procedure Build_Elaboration_Entity (N : Node_Id; Spec_Id : Entity_Id) is
888       Loc      : constant Source_Ptr := Sloc (N);
889       Decl     : Node_Id;
890       Elab_Ent : Entity_Id;
891
892       procedure Set_Package_Name (Ent : Entity_Id);
893       --  Given an entity, sets the fully qualified name of the entity in
894       --  Name_Buffer, with components separated by double underscores. This
895       --  is a recursive routine that climbs the scope chain to Standard.
896
897       ----------------------
898       -- Set_Package_Name --
899       ----------------------
900
901       procedure Set_Package_Name (Ent : Entity_Id) is
902       begin
903          if Scope (Ent) /= Standard_Standard then
904             Set_Package_Name (Scope (Ent));
905
906             declare
907                Nam : constant String := Get_Name_String (Chars (Ent));
908             begin
909                Name_Buffer (Name_Len + 1) := '_';
910                Name_Buffer (Name_Len + 2) := '_';
911                Name_Buffer (Name_Len + 3 .. Name_Len + Nam'Length + 2) := Nam;
912                Name_Len := Name_Len + Nam'Length + 2;
913             end;
914
915          else
916             Get_Name_String (Chars (Ent));
917          end if;
918       end Set_Package_Name;
919
920    --  Start of processing for Build_Elaboration_Entity
921
922    begin
923       --  Ignore if already constructed
924
925       if Present (Elaboration_Entity (Spec_Id)) then
926          return;
927       end if;
928
929       --  Construct name of elaboration entity as xxx_E, where xxx is the unit
930       --  name with dots replaced by double underscore. We have to manually
931       --  construct this name, since it will be elaborated in the outer scope,
932       --  and thus will not have the unit name automatically prepended.
933
934       Set_Package_Name (Spec_Id);
935
936       --  Append _E
937
938       Name_Buffer (Name_Len + 1) := '_';
939       Name_Buffer (Name_Len + 2) := 'E';
940       Name_Len := Name_Len + 2;
941
942       --  Create elaboration flag
943
944       Elab_Ent :=
945         Make_Defining_Identifier (Loc, Chars => Name_Find);
946       Set_Elaboration_Entity (Spec_Id, Elab_Ent);
947
948       Decl :=
949          Make_Object_Declaration (Loc,
950            Defining_Identifier => Elab_Ent,
951            Object_Definition   =>
952              New_Occurrence_Of (Standard_Boolean, Loc),
953            Expression          =>
954              New_Occurrence_Of (Standard_False, Loc));
955
956       Push_Scope (Standard_Standard);
957       Add_Global_Declaration (Decl);
958       Pop_Scope;
959
960       --  Reset True_Constant indication, since we will indeed assign a value
961       --  to the variable in the binder main. We also kill the Current_Value
962       --  and Last_Assignment fields for the same reason.
963
964       Set_Is_True_Constant (Elab_Ent, False);
965       Set_Current_Value    (Elab_Ent, Empty);
966       Set_Last_Assignment  (Elab_Ent, Empty);
967
968       --  We do not want any further qualification of the name (if we did
969       --  not do this, we would pick up the name of the generic package
970       --  in the case of a library level generic instantiation).
971
972       Set_Has_Qualified_Name       (Elab_Ent);
973       Set_Has_Fully_Qualified_Name (Elab_Ent);
974    end Build_Elaboration_Entity;
975
976    -----------------------------------
977    -- Cannot_Raise_Constraint_Error --
978    -----------------------------------
979
980    function Cannot_Raise_Constraint_Error (Expr : Node_Id) return Boolean is
981    begin
982       if Compile_Time_Known_Value (Expr) then
983          return True;
984
985       elsif Do_Range_Check (Expr) then
986          return False;
987
988       elsif Raises_Constraint_Error (Expr) then
989          return False;
990
991       else
992          case Nkind (Expr) is
993             when N_Identifier =>
994                return True;
995
996             when N_Expanded_Name =>
997                return True;
998
999             when N_Selected_Component =>
1000                return not Do_Discriminant_Check (Expr);
1001
1002             when N_Attribute_Reference =>
1003                if Do_Overflow_Check (Expr) then
1004                   return False;
1005
1006                elsif No (Expressions (Expr)) then
1007                   return True;
1008
1009                else
1010                   declare
1011                      N : Node_Id;
1012
1013                   begin
1014                      N := First (Expressions (Expr));
1015                      while Present (N) loop
1016                         if Cannot_Raise_Constraint_Error (N) then
1017                            Next (N);
1018                         else
1019                            return False;
1020                         end if;
1021                      end loop;
1022
1023                      return True;
1024                   end;
1025                end if;
1026
1027             when N_Type_Conversion =>
1028                if Do_Overflow_Check (Expr)
1029                  or else Do_Length_Check (Expr)
1030                  or else Do_Tag_Check (Expr)
1031                then
1032                   return False;
1033                else
1034                   return
1035                     Cannot_Raise_Constraint_Error (Expression (Expr));
1036                end if;
1037
1038             when N_Unchecked_Type_Conversion =>
1039                return Cannot_Raise_Constraint_Error (Expression (Expr));
1040
1041             when N_Unary_Op =>
1042                if Do_Overflow_Check (Expr) then
1043                   return False;
1044                else
1045                   return
1046                     Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1047                end if;
1048
1049             when N_Op_Divide |
1050                  N_Op_Mod    |
1051                  N_Op_Rem
1052             =>
1053                if Do_Division_Check (Expr)
1054                  or else Do_Overflow_Check (Expr)
1055                then
1056                   return False;
1057                else
1058                   return
1059                     Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1060                       and then
1061                     Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1062                end if;
1063
1064             when N_Op_Add                    |
1065                  N_Op_And                    |
1066                  N_Op_Concat                 |
1067                  N_Op_Eq                     |
1068                  N_Op_Expon                  |
1069                  N_Op_Ge                     |
1070                  N_Op_Gt                     |
1071                  N_Op_Le                     |
1072                  N_Op_Lt                     |
1073                  N_Op_Multiply               |
1074                  N_Op_Ne                     |
1075                  N_Op_Or                     |
1076                  N_Op_Rotate_Left            |
1077                  N_Op_Rotate_Right           |
1078                  N_Op_Shift_Left             |
1079                  N_Op_Shift_Right            |
1080                  N_Op_Shift_Right_Arithmetic |
1081                  N_Op_Subtract               |
1082                  N_Op_Xor
1083             =>
1084                if Do_Overflow_Check (Expr) then
1085                   return False;
1086                else
1087                   return
1088                     Cannot_Raise_Constraint_Error (Left_Opnd (Expr))
1089                       and then
1090                     Cannot_Raise_Constraint_Error (Right_Opnd (Expr));
1091                end if;
1092
1093             when others =>
1094                return False;
1095          end case;
1096       end if;
1097    end Cannot_Raise_Constraint_Error;
1098
1099    -----------------------------------------
1100    -- Check_Dynamically_Tagged_Expression --
1101    -----------------------------------------
1102
1103    procedure Check_Dynamically_Tagged_Expression
1104      (Expr        : Node_Id;
1105       Typ         : Entity_Id;
1106       Related_Nod : Node_Id)
1107    is
1108    begin
1109       pragma Assert (Is_Tagged_Type (Typ));
1110
1111       --  In order to avoid spurious errors when analyzing the expanded code,
1112       --  this check is done only for nodes that come from source and for
1113       --  actuals of generic instantiations.
1114
1115       if (Comes_From_Source (Related_Nod)
1116            or else In_Generic_Actual (Expr))
1117         and then (Is_Class_Wide_Type (Etype (Expr))
1118                    or else Is_Dynamically_Tagged (Expr))
1119         and then Is_Tagged_Type (Typ)
1120         and then not Is_Class_Wide_Type (Typ)
1121       then
1122          Error_Msg_N ("dynamically tagged expression not allowed!", Expr);
1123       end if;
1124    end Check_Dynamically_Tagged_Expression;
1125
1126    --------------------------
1127    -- Check_Fully_Declared --
1128    --------------------------
1129
1130    procedure Check_Fully_Declared (T : Entity_Id; N : Node_Id) is
1131    begin
1132       if Ekind (T) = E_Incomplete_Type then
1133
1134          --  Ada 2005 (AI-50217): If the type is available through a limited
1135          --  with_clause, verify that its full view has been analyzed.
1136
1137          if From_With_Type (T)
1138            and then Present (Non_Limited_View (T))
1139            and then Ekind (Non_Limited_View (T)) /= E_Incomplete_Type
1140          then
1141             --  The non-limited view is fully declared
1142             null;
1143
1144          else
1145             Error_Msg_NE
1146               ("premature usage of incomplete}", N, First_Subtype (T));
1147          end if;
1148
1149       --  Need comments for these tests ???
1150
1151       elsif Has_Private_Component (T)
1152         and then not Is_Generic_Type (Root_Type (T))
1153         and then not In_Spec_Expression
1154       then
1155          --  Special case: if T is the anonymous type created for a single
1156          --  task or protected object, use the name of the source object.
1157
1158          if Is_Concurrent_Type (T)
1159            and then not Comes_From_Source (T)
1160            and then Nkind (N) = N_Object_Declaration
1161          then
1162             Error_Msg_NE ("type of& has incomplete component", N,
1163               Defining_Identifier (N));
1164
1165          else
1166             Error_Msg_NE
1167               ("premature usage of incomplete}", N, First_Subtype (T));
1168          end if;
1169       end if;
1170    end Check_Fully_Declared;
1171
1172    -------------------------
1173    -- Check_Nested_Access --
1174    -------------------------
1175
1176    procedure Check_Nested_Access (Ent : Entity_Id) is
1177       Scop         : constant Entity_Id := Current_Scope;
1178       Current_Subp : Entity_Id;
1179       Enclosing    : Entity_Id;
1180
1181    begin
1182       --  Currently only enabled for VM back-ends for efficiency, should we
1183       --  enable it more systematically ???
1184
1185       --  Check for Is_Imported needs commenting below ???
1186
1187       if VM_Target /= No_VM
1188         and then (Ekind (Ent) = E_Variable
1189                     or else
1190                   Ekind (Ent) = E_Constant
1191                     or else
1192                   Ekind (Ent) = E_Loop_Parameter)
1193         and then Scope (Ent) /= Empty
1194         and then not Is_Library_Level_Entity (Ent)
1195         and then not Is_Imported (Ent)
1196       then
1197          if Is_Subprogram (Scop)
1198            or else Is_Generic_Subprogram (Scop)
1199            or else Is_Entry (Scop)
1200          then
1201             Current_Subp := Scop;
1202          else
1203             Current_Subp := Current_Subprogram;
1204          end if;
1205
1206          Enclosing := Enclosing_Subprogram (Ent);
1207
1208          if Enclosing /= Empty
1209            and then Enclosing /= Current_Subp
1210          then
1211             Set_Has_Up_Level_Access (Ent, True);
1212          end if;
1213       end if;
1214    end Check_Nested_Access;
1215
1216    ----------------------------
1217    -- Check_Order_Dependence --
1218    ----------------------------
1219
1220    procedure Check_Order_Dependence is
1221       Act1 : Node_Id;
1222       Act2 : Node_Id;
1223
1224    begin
1225       if Ada_Version < Ada_2012 then
1226          return;
1227       end if;
1228
1229       --  Ada 2012 AI04-0144-2: Dangerous order dependence. Actuals in nested
1230       --  calls within a construct have been collected. If one of them is
1231       --  writable and overlaps with another one, evaluation of the enclosing
1232       --  construct is nondeterministic. This is illegal in Ada 2012, but is
1233       --  treated as a warning for now.
1234
1235       for J in 1 .. Actuals_In_Call.Last loop
1236          if Actuals_In_Call.Table (J).Is_Writable then
1237             Act1 := Actuals_In_Call.Table (J).Act;
1238
1239             if Nkind (Act1) = N_Attribute_Reference then
1240                Act1 := Prefix (Act1);
1241             end if;
1242
1243             for K in 1 .. Actuals_In_Call.Last loop
1244                if K /= J then
1245                   Act2 := Actuals_In_Call.Table (K).Act;
1246
1247                   if Nkind (Act2) = N_Attribute_Reference then
1248                      Act2 := Prefix (Act2);
1249                   end if;
1250
1251                   if Actuals_In_Call.Table (K).Is_Writable
1252                     and then K < J
1253                   then
1254                      --  Already checked
1255
1256                      null;
1257
1258                   elsif Denotes_Same_Object (Act1, Act2)
1259                     and then Parent (Act1) /= Parent (Act2)
1260                   then
1261                      Error_Msg_N
1262                        ("result may differ if evaluated "
1263                         & "after other actual in expression?", Act1);
1264                   end if;
1265                end if;
1266             end loop;
1267          end if;
1268       end loop;
1269
1270       --  Remove checked actuals from table
1271
1272       Actuals_In_Call.Set_Last (0);
1273    end Check_Order_Dependence;
1274
1275    ------------------------------------------
1276    -- Check_Potentially_Blocking_Operation --
1277    ------------------------------------------
1278
1279    procedure Check_Potentially_Blocking_Operation (N : Node_Id) is
1280       S : Entity_Id;
1281
1282    begin
1283       --  N is one of the potentially blocking operations listed in 9.5.1(8).
1284       --  When pragma Detect_Blocking is active, the run time will raise
1285       --  Program_Error. Here we only issue a warning, since we generally
1286       --  support the use of potentially blocking operations in the absence
1287       --  of the pragma.
1288
1289       --  Indirect blocking through a subprogram call cannot be diagnosed
1290       --  statically without interprocedural analysis, so we do not attempt
1291       --  to do it here.
1292
1293       S := Scope (Current_Scope);
1294       while Present (S) and then S /= Standard_Standard loop
1295          if Is_Protected_Type (S) then
1296             Error_Msg_N
1297               ("potentially blocking operation in protected operation?", N);
1298             return;
1299          end if;
1300
1301          S := Scope (S);
1302       end loop;
1303    end Check_Potentially_Blocking_Operation;
1304
1305    ------------------------------
1306    -- Check_Unprotected_Access --
1307    ------------------------------
1308
1309    procedure Check_Unprotected_Access
1310      (Context : Node_Id;
1311       Expr    : Node_Id)
1312    is
1313       Cont_Encl_Typ : Entity_Id;
1314       Pref_Encl_Typ : Entity_Id;
1315
1316       function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id;
1317       --  Check whether Obj is a private component of a protected object.
1318       --  Return the protected type where the component resides, Empty
1319       --  otherwise.
1320
1321       function Is_Public_Operation return Boolean;
1322       --  Verify that the enclosing operation is callable from outside the
1323       --  protected object, to minimize false positives.
1324
1325       ------------------------------
1326       -- Enclosing_Protected_Type --
1327       ------------------------------
1328
1329       function Enclosing_Protected_Type (Obj : Node_Id) return Entity_Id is
1330       begin
1331          if Is_Entity_Name (Obj) then
1332             declare
1333                Ent : Entity_Id := Entity (Obj);
1334
1335             begin
1336                --  The object can be a renaming of a private component, use
1337                --  the original record component.
1338
1339                if Is_Prival (Ent) then
1340                   Ent := Prival_Link (Ent);
1341                end if;
1342
1343                if Is_Protected_Type (Scope (Ent)) then
1344                   return Scope (Ent);
1345                end if;
1346             end;
1347          end if;
1348
1349          --  For indexed and selected components, recursively check the prefix
1350
1351          if Nkind_In (Obj, N_Indexed_Component, N_Selected_Component) then
1352             return Enclosing_Protected_Type (Prefix (Obj));
1353
1354          --  The object does not denote a protected component
1355
1356          else
1357             return Empty;
1358          end if;
1359       end Enclosing_Protected_Type;
1360
1361       -------------------------
1362       -- Is_Public_Operation --
1363       -------------------------
1364
1365       function Is_Public_Operation return Boolean is
1366          S : Entity_Id;
1367          E : Entity_Id;
1368
1369       begin
1370          S := Current_Scope;
1371          while Present (S)
1372            and then S /= Pref_Encl_Typ
1373          loop
1374             if Scope (S) = Pref_Encl_Typ then
1375                E := First_Entity (Pref_Encl_Typ);
1376                while Present (E)
1377                  and then E /= First_Private_Entity (Pref_Encl_Typ)
1378                loop
1379                   if E = S then
1380                      return True;
1381                   end if;
1382                   Next_Entity (E);
1383                end loop;
1384             end if;
1385
1386             S := Scope (S);
1387          end loop;
1388
1389          return False;
1390       end Is_Public_Operation;
1391
1392    --  Start of processing for Check_Unprotected_Access
1393
1394    begin
1395       if Nkind (Expr) = N_Attribute_Reference
1396         and then Attribute_Name (Expr) = Name_Unchecked_Access
1397       then
1398          Cont_Encl_Typ := Enclosing_Protected_Type (Context);
1399          Pref_Encl_Typ := Enclosing_Protected_Type (Prefix (Expr));
1400
1401          --  Check whether we are trying to export a protected component to a
1402          --  context with an equal or lower access level.
1403
1404          if Present (Pref_Encl_Typ)
1405            and then No (Cont_Encl_Typ)
1406            and then Is_Public_Operation
1407            and then Scope_Depth (Pref_Encl_Typ) >=
1408                       Object_Access_Level (Context)
1409          then
1410             Error_Msg_N
1411               ("?possible unprotected access to protected data", Expr);
1412          end if;
1413       end if;
1414    end Check_Unprotected_Access;
1415
1416    ---------------
1417    -- Check_VMS --
1418    ---------------
1419
1420    procedure Check_VMS (Construct : Node_Id) is
1421    begin
1422       if not OpenVMS_On_Target then
1423          Error_Msg_N
1424            ("this construct is allowed only in Open'V'M'S", Construct);
1425       end if;
1426    end Check_VMS;
1427
1428    ------------------------
1429    -- Collect_Interfaces --
1430    ------------------------
1431
1432    procedure Collect_Interfaces
1433      (T               : Entity_Id;
1434       Ifaces_List     : out Elist_Id;
1435       Exclude_Parents : Boolean := False;
1436       Use_Full_View   : Boolean := True)
1437    is
1438       procedure Collect (Typ : Entity_Id);
1439       --  Subsidiary subprogram used to traverse the whole list
1440       --  of directly and indirectly implemented interfaces
1441
1442       -------------
1443       -- Collect --
1444       -------------
1445
1446       procedure Collect (Typ : Entity_Id) is
1447          Ancestor   : Entity_Id;
1448          Full_T     : Entity_Id;
1449          Id         : Node_Id;
1450          Iface      : Entity_Id;
1451
1452       begin
1453          Full_T := Typ;
1454
1455          --  Handle private types
1456
1457          if Use_Full_View
1458            and then Is_Private_Type (Typ)
1459            and then Present (Full_View (Typ))
1460          then
1461             Full_T := Full_View (Typ);
1462          end if;
1463
1464          --  Include the ancestor if we are generating the whole list of
1465          --  abstract interfaces.
1466
1467          if Etype (Full_T) /= Typ
1468
1469             --  Protect the frontend against wrong sources. For example:
1470
1471             --    package P is
1472             --      type A is tagged null record;
1473             --      type B is new A with private;
1474             --      type C is new A with private;
1475             --    private
1476             --      type B is new C with null record;
1477             --      type C is new B with null record;
1478             --    end P;
1479
1480            and then Etype (Full_T) /= T
1481          then
1482             Ancestor := Etype (Full_T);
1483             Collect (Ancestor);
1484
1485             if Is_Interface (Ancestor)
1486               and then not Exclude_Parents
1487             then
1488                Append_Unique_Elmt (Ancestor, Ifaces_List);
1489             end if;
1490          end if;
1491
1492          --  Traverse the graph of ancestor interfaces
1493
1494          if Is_Non_Empty_List (Abstract_Interface_List (Full_T)) then
1495             Id := First (Abstract_Interface_List (Full_T));
1496             while Present (Id) loop
1497                Iface := Etype (Id);
1498
1499                --  Protect against wrong uses. For example:
1500                --    type I is interface;
1501                --    type O is tagged null record;
1502                --    type Wrong is new I and O with null record; -- ERROR
1503
1504                if Is_Interface (Iface) then
1505                   if Exclude_Parents
1506                     and then Etype (T) /= T
1507                     and then Interface_Present_In_Ancestor (Etype (T), Iface)
1508                   then
1509                      null;
1510                   else
1511                      Collect (Iface);
1512                      Append_Unique_Elmt (Iface, Ifaces_List);
1513                   end if;
1514                end if;
1515
1516                Next (Id);
1517             end loop;
1518          end if;
1519       end Collect;
1520
1521    --  Start of processing for Collect_Interfaces
1522
1523    begin
1524       pragma Assert (Is_Tagged_Type (T) or else Is_Concurrent_Type (T));
1525       Ifaces_List := New_Elmt_List;
1526       Collect (T);
1527    end Collect_Interfaces;
1528
1529    ----------------------------------
1530    -- Collect_Interface_Components --
1531    ----------------------------------
1532
1533    procedure Collect_Interface_Components
1534      (Tagged_Type     : Entity_Id;
1535       Components_List : out Elist_Id)
1536    is
1537       procedure Collect (Typ : Entity_Id);
1538       --  Subsidiary subprogram used to climb to the parents
1539
1540       -------------
1541       -- Collect --
1542       -------------
1543
1544       procedure Collect (Typ : Entity_Id) is
1545          Tag_Comp   : Entity_Id;
1546          Parent_Typ : Entity_Id;
1547
1548       begin
1549          --  Handle private types
1550
1551          if Present (Full_View (Etype (Typ))) then
1552             Parent_Typ := Full_View (Etype (Typ));
1553          else
1554             Parent_Typ := Etype (Typ);
1555          end if;
1556
1557          if Parent_Typ /= Typ
1558
1559             --  Protect the frontend against wrong sources. For example:
1560
1561             --    package P is
1562             --      type A is tagged null record;
1563             --      type B is new A with private;
1564             --      type C is new A with private;
1565             --    private
1566             --      type B is new C with null record;
1567             --      type C is new B with null record;
1568             --    end P;
1569
1570            and then Parent_Typ /= Tagged_Type
1571          then
1572             Collect (Parent_Typ);
1573          end if;
1574
1575          --  Collect the components containing tags of secondary dispatch
1576          --  tables.
1577
1578          Tag_Comp := Next_Tag_Component (First_Tag_Component (Typ));
1579          while Present (Tag_Comp) loop
1580             pragma Assert (Present (Related_Type (Tag_Comp)));
1581             Append_Elmt (Tag_Comp, Components_List);
1582
1583             Tag_Comp := Next_Tag_Component (Tag_Comp);
1584          end loop;
1585       end Collect;
1586
1587    --  Start of processing for Collect_Interface_Components
1588
1589    begin
1590       pragma Assert (Ekind (Tagged_Type) = E_Record_Type
1591         and then Is_Tagged_Type (Tagged_Type));
1592
1593       Components_List := New_Elmt_List;
1594       Collect (Tagged_Type);
1595    end Collect_Interface_Components;
1596
1597    -----------------------------
1598    -- Collect_Interfaces_Info --
1599    -----------------------------
1600
1601    procedure Collect_Interfaces_Info
1602      (T               : Entity_Id;
1603       Ifaces_List     : out Elist_Id;
1604       Components_List : out Elist_Id;
1605       Tags_List       : out Elist_Id)
1606    is
1607       Comps_List : Elist_Id;
1608       Comp_Elmt  : Elmt_Id;
1609       Comp_Iface : Entity_Id;
1610       Iface_Elmt : Elmt_Id;
1611       Iface      : Entity_Id;
1612
1613       function Search_Tag (Iface : Entity_Id) return Entity_Id;
1614       --  Search for the secondary tag associated with the interface type
1615       --  Iface that is implemented by T.
1616
1617       ----------------
1618       -- Search_Tag --
1619       ----------------
1620
1621       function Search_Tag (Iface : Entity_Id) return Entity_Id is
1622          ADT : Elmt_Id;
1623       begin
1624          if not Is_CPP_Class (T) then
1625             ADT := Next_Elmt (Next_Elmt (First_Elmt (Access_Disp_Table (T))));
1626          else
1627             ADT := Next_Elmt (First_Elmt (Access_Disp_Table (T)));
1628          end if;
1629
1630          while Present (ADT)
1631             and then Is_Tag (Node (ADT))
1632             and then Related_Type (Node (ADT)) /= Iface
1633          loop
1634             --  Skip secondary dispatch table referencing thunks to user
1635             --  defined primitives covered by this interface.
1636
1637             pragma Assert (Has_Suffix (Node (ADT), 'P'));
1638             Next_Elmt (ADT);
1639
1640             --  Skip secondary dispatch tables of Ada types
1641
1642             if not Is_CPP_Class (T) then
1643
1644                --  Skip secondary dispatch table referencing thunks to
1645                --  predefined primitives.
1646
1647                pragma Assert (Has_Suffix (Node (ADT), 'Y'));
1648                Next_Elmt (ADT);
1649
1650                --  Skip secondary dispatch table referencing user-defined
1651                --  primitives covered by this interface.
1652
1653                pragma Assert (Has_Suffix (Node (ADT), 'D'));
1654                Next_Elmt (ADT);
1655
1656                --  Skip secondary dispatch table referencing predefined
1657                --  primitives.
1658
1659                pragma Assert (Has_Suffix (Node (ADT), 'Z'));
1660                Next_Elmt (ADT);
1661             end if;
1662          end loop;
1663
1664          pragma Assert (Is_Tag (Node (ADT)));
1665          return Node (ADT);
1666       end Search_Tag;
1667
1668    --  Start of processing for Collect_Interfaces_Info
1669
1670    begin
1671       Collect_Interfaces (T, Ifaces_List);
1672       Collect_Interface_Components (T, Comps_List);
1673
1674       --  Search for the record component and tag associated with each
1675       --  interface type of T.
1676
1677       Components_List := New_Elmt_List;
1678       Tags_List       := New_Elmt_List;
1679
1680       Iface_Elmt := First_Elmt (Ifaces_List);
1681       while Present (Iface_Elmt) loop
1682          Iface := Node (Iface_Elmt);
1683
1684          --  Associate the primary tag component and the primary dispatch table
1685          --  with all the interfaces that are parents of T
1686
1687          if Is_Ancestor (Iface, T) then
1688             Append_Elmt (First_Tag_Component (T), Components_List);
1689             Append_Elmt (Node (First_Elmt (Access_Disp_Table (T))), Tags_List);
1690
1691          --  Otherwise search for the tag component and secondary dispatch
1692          --  table of Iface
1693
1694          else
1695             Comp_Elmt := First_Elmt (Comps_List);
1696             while Present (Comp_Elmt) loop
1697                Comp_Iface := Related_Type (Node (Comp_Elmt));
1698
1699                if Comp_Iface = Iface
1700                  or else Is_Ancestor (Iface, Comp_Iface)
1701                then
1702                   Append_Elmt (Node (Comp_Elmt), Components_List);
1703                   Append_Elmt (Search_Tag (Comp_Iface), Tags_List);
1704                   exit;
1705                end if;
1706
1707                Next_Elmt (Comp_Elmt);
1708             end loop;
1709             pragma Assert (Present (Comp_Elmt));
1710          end if;
1711
1712          Next_Elmt (Iface_Elmt);
1713       end loop;
1714    end Collect_Interfaces_Info;
1715
1716    ---------------------
1717    -- Collect_Parents --
1718    ---------------------
1719
1720    procedure Collect_Parents
1721      (T             : Entity_Id;
1722       List          : out Elist_Id;
1723       Use_Full_View : Boolean := True)
1724    is
1725       Current_Typ : Entity_Id := T;
1726       Parent_Typ  : Entity_Id;
1727
1728    begin
1729       List := New_Elmt_List;
1730
1731       --  No action if the if the type has no parents
1732
1733       if T = Etype (T) then
1734          return;
1735       end if;
1736
1737       loop
1738          Parent_Typ := Etype (Current_Typ);
1739
1740          if Is_Private_Type (Parent_Typ)
1741            and then Present (Full_View (Parent_Typ))
1742            and then Use_Full_View
1743          then
1744             Parent_Typ := Full_View (Base_Type (Parent_Typ));
1745          end if;
1746
1747          Append_Elmt (Parent_Typ, List);
1748
1749          exit when Parent_Typ = Current_Typ;
1750          Current_Typ := Parent_Typ;
1751       end loop;
1752    end Collect_Parents;
1753
1754    ----------------------------------
1755    -- Collect_Primitive_Operations --
1756    ----------------------------------
1757
1758    function Collect_Primitive_Operations (T : Entity_Id) return Elist_Id is
1759       B_Type         : constant Entity_Id := Base_Type (T);
1760       B_Decl         : constant Node_Id   := Original_Node (Parent (B_Type));
1761       B_Scope        : Entity_Id          := Scope (B_Type);
1762       Op_List        : Elist_Id;
1763       Formal         : Entity_Id;
1764       Is_Prim        : Boolean;
1765       Formal_Derived : Boolean := False;
1766       Id             : Entity_Id;
1767
1768       function Match (E : Entity_Id) return Boolean;
1769       --  True if E's base type is B_Type, or E is of an anonymous access type
1770       --  and the base type of its designated type is B_Type.
1771
1772       -----------
1773       -- Match --
1774       -----------
1775
1776       function Match (E : Entity_Id) return Boolean is
1777          Etyp : Entity_Id := Etype (E);
1778
1779       begin
1780          if Ekind (Etyp) = E_Anonymous_Access_Type then
1781             Etyp := Designated_Type (Etyp);
1782          end if;
1783
1784          return Base_Type (Etyp) = B_Type;
1785       end Match;
1786
1787    --  Start of processing for Collect_Primitive_Operations
1788
1789    begin
1790       --  For tagged types, the primitive operations are collected as they
1791       --  are declared, and held in an explicit list which is simply returned.
1792
1793       if Is_Tagged_Type (B_Type) then
1794          return Primitive_Operations (B_Type);
1795
1796       --  An untagged generic type that is a derived type inherits the
1797       --  primitive operations of its parent type. Other formal types only
1798       --  have predefined operators, which are not explicitly represented.
1799
1800       elsif Is_Generic_Type (B_Type) then
1801          if Nkind (B_Decl) = N_Formal_Type_Declaration
1802            and then Nkind (Formal_Type_Definition (B_Decl))
1803              = N_Formal_Derived_Type_Definition
1804          then
1805             Formal_Derived := True;
1806          else
1807             return New_Elmt_List;
1808          end if;
1809       end if;
1810
1811       Op_List := New_Elmt_List;
1812
1813       if B_Scope = Standard_Standard then
1814          if B_Type = Standard_String then
1815             Append_Elmt (Standard_Op_Concat, Op_List);
1816
1817          elsif B_Type = Standard_Wide_String then
1818             Append_Elmt (Standard_Op_Concatw, Op_List);
1819
1820          else
1821             null;
1822          end if;
1823
1824       elsif (Is_Package_Or_Generic_Package (B_Scope)
1825               and then
1826                 Nkind (Parent (Declaration_Node (First_Subtype (T)))) /=
1827                                                             N_Package_Body)
1828         or else Is_Derived_Type (B_Type)
1829       then
1830          --  The primitive operations appear after the base type, except
1831          --  if the derivation happens within the private part of B_Scope
1832          --  and the type is a private type, in which case both the type
1833          --  and some primitive operations may appear before the base
1834          --  type, and the list of candidates starts after the type.
1835
1836          if In_Open_Scopes (B_Scope)
1837            and then Scope (T) = B_Scope
1838            and then In_Private_Part (B_Scope)
1839          then
1840             Id := Next_Entity (T);
1841          else
1842             Id := Next_Entity (B_Type);
1843          end if;
1844
1845          while Present (Id) loop
1846
1847             --  Note that generic formal subprograms are not
1848             --  considered to be primitive operations and thus
1849             --  are never inherited.
1850
1851             if Is_Overloadable (Id)
1852               and then Nkind (Parent (Parent (Id)))
1853                          not in N_Formal_Subprogram_Declaration
1854             then
1855                Is_Prim := False;
1856
1857                if Match (Id) then
1858                   Is_Prim := True;
1859
1860                else
1861                   Formal := First_Formal (Id);
1862                   while Present (Formal) loop
1863                      if Match (Formal) then
1864                         Is_Prim := True;
1865                         exit;
1866                      end if;
1867
1868                      Next_Formal (Formal);
1869                   end loop;
1870                end if;
1871
1872                --  For a formal derived type, the only primitives are the
1873                --  ones inherited from the parent type. Operations appearing
1874                --  in the package declaration are not primitive for it.
1875
1876                if Is_Prim
1877                  and then (not Formal_Derived
1878                             or else Present (Alias (Id)))
1879                then
1880                   --  In the special case of an equality operator aliased to
1881                   --  an overriding dispatching equality belonging to the same
1882                   --  type, we don't include it in the list of primitives.
1883                   --  This avoids inheriting multiple equality operators when
1884                   --  deriving from untagged private types whose full type is
1885                   --  tagged, which can otherwise cause ambiguities. Note that
1886                   --  this should only happen for this kind of untagged parent
1887                   --  type, since normally dispatching operations are inherited
1888                   --  using the type's Primitive_Operations list.
1889
1890                   if Chars (Id) = Name_Op_Eq
1891                     and then Is_Dispatching_Operation (Id)
1892                     and then Present (Alias (Id))
1893                     and then Is_Overriding_Operation (Alias (Id))
1894                     and then Base_Type (Etype (First_Entity (Id))) =
1895                                Base_Type (Etype (First_Entity (Alias (Id))))
1896                   then
1897                      null;
1898
1899                   --  Include the subprogram in the list of primitives
1900
1901                   else
1902                      Append_Elmt (Id, Op_List);
1903                   end if;
1904                end if;
1905             end if;
1906
1907             Next_Entity (Id);
1908
1909             --  For a type declared in System, some of its operations may
1910             --  appear in the target-specific extension to System.
1911
1912             if No (Id)
1913               and then B_Scope = RTU_Entity (System)
1914               and then Present_System_Aux
1915             then
1916                B_Scope := System_Aux_Id;
1917                Id := First_Entity (System_Aux_Id);
1918             end if;
1919          end loop;
1920       end if;
1921
1922       return Op_List;
1923    end Collect_Primitive_Operations;
1924
1925    -----------------------------------
1926    -- Compile_Time_Constraint_Error --
1927    -----------------------------------
1928
1929    function Compile_Time_Constraint_Error
1930      (N    : Node_Id;
1931       Msg  : String;
1932       Ent  : Entity_Id  := Empty;
1933       Loc  : Source_Ptr := No_Location;
1934       Warn : Boolean    := False) return Node_Id
1935    is
1936       Msgc : String (1 .. Msg'Length + 2);
1937       --  Copy of message, with room for possible ? and ! at end
1938
1939       Msgl : Natural;
1940       Wmsg : Boolean;
1941       P    : Node_Id;
1942       OldP : Node_Id;
1943       Msgs : Boolean;
1944       Eloc : Source_Ptr;
1945
1946    begin
1947       --  A static constraint error in an instance body is not a fatal error.
1948       --  we choose to inhibit the message altogether, because there is no
1949       --  obvious node (for now) on which to post it. On the other hand the
1950       --  offending node must be replaced with a constraint_error in any case.
1951
1952       --  No messages are generated if we already posted an error on this node
1953
1954       if not Error_Posted (N) then
1955          if Loc /= No_Location then
1956             Eloc := Loc;
1957          else
1958             Eloc := Sloc (N);
1959          end if;
1960
1961          Msgc (1 .. Msg'Length) := Msg;
1962          Msgl := Msg'Length;
1963
1964          --  Message is a warning, even in Ada 95 case
1965
1966          if Msg (Msg'Last) = '?' then
1967             Wmsg := True;
1968
1969          --  In Ada 83, all messages are warnings. In the private part and
1970          --  the body of an instance, constraint_checks are only warnings.
1971          --  We also make this a warning if the Warn parameter is set.
1972
1973          elsif Warn
1974            or else (Ada_Version = Ada_83 and then Comes_From_Source (N))
1975          then
1976             Msgl := Msgl + 1;
1977             Msgc (Msgl) := '?';
1978             Wmsg := True;
1979
1980          elsif In_Instance_Not_Visible then
1981             Msgl := Msgl + 1;
1982             Msgc (Msgl) := '?';
1983             Wmsg := True;
1984
1985          --  Otherwise we have a real error message (Ada 95 static case)
1986          --  and we make this an unconditional message. Note that in the
1987          --  warning case we do not make the message unconditional, it seems
1988          --  quite reasonable to delete messages like this (about exceptions
1989          --  that will be raised) in dead code.
1990
1991          else
1992             Wmsg := False;
1993             Msgl := Msgl + 1;
1994             Msgc (Msgl) := '!';
1995          end if;
1996
1997          --  Should we generate a warning? The answer is not quite yes. The
1998          --  very annoying exception occurs in the case of a short circuit
1999          --  operator where the left operand is static and decisive. Climb
2000          --  parents to see if that is the case we have here. Conditional
2001          --  expressions with decisive conditions are a similar situation.
2002
2003          Msgs := True;
2004          P := N;
2005          loop
2006             OldP := P;
2007             P := Parent (P);
2008
2009             --  And then with False as left operand
2010
2011             if Nkind (P) = N_And_Then
2012               and then Compile_Time_Known_Value (Left_Opnd (P))
2013               and then Is_False (Expr_Value (Left_Opnd (P)))
2014             then
2015                Msgs := False;
2016                exit;
2017
2018             --  OR ELSE with True as left operand
2019
2020             elsif Nkind (P) = N_Or_Else
2021               and then Compile_Time_Known_Value (Left_Opnd (P))
2022               and then Is_True (Expr_Value (Left_Opnd (P)))
2023             then
2024                Msgs := False;
2025                exit;
2026
2027             --  Conditional expression
2028
2029             elsif Nkind (P) = N_Conditional_Expression then
2030                declare
2031                   Cond : constant Node_Id := First (Expressions (P));
2032                   Texp : constant Node_Id := Next (Cond);
2033                   Fexp : constant Node_Id := Next (Texp);
2034
2035                begin
2036                   if Compile_Time_Known_Value (Cond) then
2037
2038                      --  Condition is True and we are in the right operand
2039
2040                      if Is_True (Expr_Value (Cond))
2041                        and then OldP = Fexp
2042                      then
2043                         Msgs := False;
2044                         exit;
2045
2046                      --  Condition is False and we are in the left operand
2047
2048                      elsif Is_False (Expr_Value (Cond))
2049                        and then OldP = Texp
2050                      then
2051                         Msgs := False;
2052                         exit;
2053                      end if;
2054                   end if;
2055                end;
2056
2057             --  Special case for component association in aggregates, where
2058             --  we want to keep climbing up to the parent aggregate.
2059
2060             elsif Nkind (P) = N_Component_Association
2061               and then Nkind (Parent (P)) = N_Aggregate
2062             then
2063                null;
2064
2065             --  Keep going if within subexpression
2066
2067             else
2068                exit when Nkind (P) not in N_Subexpr;
2069             end if;
2070          end loop;
2071
2072          if Msgs then
2073             if Present (Ent) then
2074                Error_Msg_NEL (Msgc (1 .. Msgl), N, Ent, Eloc);
2075             else
2076                Error_Msg_NEL (Msgc (1 .. Msgl), N, Etype (N), Eloc);
2077             end if;
2078
2079             if Wmsg then
2080                if Inside_Init_Proc then
2081                   Error_Msg_NEL
2082                     ("\?& will be raised for objects of this type",
2083                      N, Standard_Constraint_Error, Eloc);
2084                else
2085                   Error_Msg_NEL
2086                     ("\?& will be raised at run time",
2087                      N, Standard_Constraint_Error, Eloc);
2088                end if;
2089
2090             else
2091                Error_Msg
2092                  ("\static expression fails Constraint_Check", Eloc);
2093                Set_Error_Posted (N);
2094             end if;
2095          end if;
2096       end if;
2097
2098       return N;
2099    end Compile_Time_Constraint_Error;
2100
2101    -----------------------
2102    -- Conditional_Delay --
2103    -----------------------
2104
2105    procedure Conditional_Delay (New_Ent, Old_Ent : Entity_Id) is
2106    begin
2107       if Has_Delayed_Freeze (Old_Ent) and then not Is_Frozen (Old_Ent) then
2108          Set_Has_Delayed_Freeze (New_Ent);
2109       end if;
2110    end Conditional_Delay;
2111
2112    -------------------------
2113    -- Copy_Parameter_List --
2114    -------------------------
2115
2116    function Copy_Parameter_List (Subp_Id : Entity_Id) return List_Id is
2117       Loc    : constant Source_Ptr := Sloc (Subp_Id);
2118       Plist  : List_Id;
2119       Formal : Entity_Id;
2120
2121    begin
2122       if No (First_Formal (Subp_Id)) then
2123          return No_List;
2124       else
2125          Plist := New_List;
2126          Formal := First_Formal (Subp_Id);
2127          while Present (Formal) loop
2128             Append
2129               (Make_Parameter_Specification (Loc,
2130                 Defining_Identifier =>
2131                   Make_Defining_Identifier (Sloc (Formal),
2132                     Chars => Chars (Formal)),
2133                 In_Present  => In_Present (Parent (Formal)),
2134                 Out_Present => Out_Present (Parent (Formal)),
2135              Parameter_Type =>
2136                   New_Reference_To (Etype (Formal), Loc),
2137                 Expression =>
2138                   New_Copy_Tree (Expression (Parent (Formal)))),
2139               Plist);
2140
2141             Next_Formal (Formal);
2142          end loop;
2143       end if;
2144
2145       return Plist;
2146    end Copy_Parameter_List;
2147
2148    --------------------
2149    -- Current_Entity --
2150    --------------------
2151
2152    --  The currently visible definition for a given identifier is the
2153    --  one most chained at the start of the visibility chain, i.e. the
2154    --  one that is referenced by the Node_Id value of the name of the
2155    --  given identifier.
2156
2157    function Current_Entity (N : Node_Id) return Entity_Id is
2158    begin
2159       return Get_Name_Entity_Id (Chars (N));
2160    end Current_Entity;
2161
2162    -----------------------------
2163    -- Current_Entity_In_Scope --
2164    -----------------------------
2165
2166    function Current_Entity_In_Scope (N : Node_Id) return Entity_Id is
2167       E  : Entity_Id;
2168       CS : constant Entity_Id := Current_Scope;
2169
2170       Transient_Case : constant Boolean := Scope_Is_Transient;
2171
2172    begin
2173       E := Get_Name_Entity_Id (Chars (N));
2174       while Present (E)
2175         and then Scope (E) /= CS
2176         and then (not Transient_Case or else Scope (E) /= Scope (CS))
2177       loop
2178          E := Homonym (E);
2179       end loop;
2180
2181       return E;
2182    end Current_Entity_In_Scope;
2183
2184    -------------------
2185    -- Current_Scope --
2186    -------------------
2187
2188    function Current_Scope return Entity_Id is
2189    begin
2190       if Scope_Stack.Last = -1 then
2191          return Standard_Standard;
2192       else
2193          declare
2194             C : constant Entity_Id :=
2195                   Scope_Stack.Table (Scope_Stack.Last).Entity;
2196          begin
2197             if Present (C) then
2198                return C;
2199             else
2200                return Standard_Standard;
2201             end if;
2202          end;
2203       end if;
2204    end Current_Scope;
2205
2206    ------------------------
2207    -- Current_Subprogram --
2208    ------------------------
2209
2210    function Current_Subprogram return Entity_Id is
2211       Scop : constant Entity_Id := Current_Scope;
2212    begin
2213       if Is_Subprogram (Scop) or else Is_Generic_Subprogram (Scop) then
2214          return Scop;
2215       else
2216          return Enclosing_Subprogram (Scop);
2217       end if;
2218    end Current_Subprogram;
2219
2220    ---------------------
2221    -- Defining_Entity --
2222    ---------------------
2223
2224    function Defining_Entity (N : Node_Id) return Entity_Id is
2225       K   : constant Node_Kind := Nkind (N);
2226       Err : Entity_Id := Empty;
2227
2228    begin
2229       case K is
2230          when
2231            N_Subprogram_Declaration                 |
2232            N_Abstract_Subprogram_Declaration        |
2233            N_Subprogram_Body                        |
2234            N_Package_Declaration                    |
2235            N_Subprogram_Renaming_Declaration        |
2236            N_Subprogram_Body_Stub                   |
2237            N_Generic_Subprogram_Declaration         |
2238            N_Generic_Package_Declaration            |
2239            N_Formal_Subprogram_Declaration
2240          =>
2241             return Defining_Entity (Specification (N));
2242
2243          when
2244            N_Component_Declaration                  |
2245            N_Defining_Program_Unit_Name             |
2246            N_Discriminant_Specification             |
2247            N_Entry_Body                             |
2248            N_Entry_Declaration                      |
2249            N_Entry_Index_Specification              |
2250            N_Exception_Declaration                  |
2251            N_Exception_Renaming_Declaration         |
2252            N_Formal_Object_Declaration              |
2253            N_Formal_Package_Declaration             |
2254            N_Formal_Type_Declaration                |
2255            N_Full_Type_Declaration                  |
2256            N_Implicit_Label_Declaration             |
2257            N_Incomplete_Type_Declaration            |
2258            N_Loop_Parameter_Specification           |
2259            N_Number_Declaration                     |
2260            N_Object_Declaration                     |
2261            N_Object_Renaming_Declaration            |
2262            N_Package_Body_Stub                      |
2263            N_Parameter_Specification                |
2264            N_Private_Extension_Declaration          |
2265            N_Private_Type_Declaration               |
2266            N_Protected_Body                         |
2267            N_Protected_Body_Stub                    |
2268            N_Protected_Type_Declaration             |
2269            N_Single_Protected_Declaration           |
2270            N_Single_Task_Declaration                |
2271            N_Subtype_Declaration                    |
2272            N_Task_Body                              |
2273            N_Task_Body_Stub                         |
2274            N_Task_Type_Declaration
2275          =>
2276             return Defining_Identifier (N);
2277
2278          when N_Subunit =>
2279             return Defining_Entity (Proper_Body (N));
2280
2281          when
2282            N_Function_Instantiation                 |
2283            N_Function_Specification                 |
2284            N_Generic_Function_Renaming_Declaration  |
2285            N_Generic_Package_Renaming_Declaration   |
2286            N_Generic_Procedure_Renaming_Declaration |
2287            N_Package_Body                           |
2288            N_Package_Instantiation                  |
2289            N_Package_Renaming_Declaration           |
2290            N_Package_Specification                  |
2291            N_Procedure_Instantiation                |
2292            N_Procedure_Specification
2293          =>
2294             declare
2295                Nam : constant Node_Id := Defining_Unit_Name (N);
2296
2297             begin
2298                if Nkind (Nam) in N_Entity then
2299                   return Nam;
2300
2301                --  For Error, make up a name and attach to declaration
2302                --  so we can continue semantic analysis
2303
2304                elsif Nam = Error then
2305                   Err := Make_Temporary (Sloc (N), 'T');
2306                   Set_Defining_Unit_Name (N, Err);
2307
2308                   return Err;
2309                --  If not an entity, get defining identifier
2310
2311                else
2312                   return Defining_Identifier (Nam);
2313                end if;
2314             end;
2315
2316          when N_Block_Statement =>
2317             return Entity (Identifier (N));
2318
2319          when others =>
2320             raise Program_Error;
2321
2322       end case;
2323    end Defining_Entity;
2324
2325    --------------------------
2326    -- Denotes_Discriminant --
2327    --------------------------
2328
2329    function Denotes_Discriminant
2330      (N                : Node_Id;
2331       Check_Concurrent : Boolean := False) return Boolean
2332    is
2333       E : Entity_Id;
2334    begin
2335       if not Is_Entity_Name (N)
2336         or else No (Entity (N))
2337       then
2338          return False;
2339       else
2340          E := Entity (N);
2341       end if;
2342
2343       --  If we are checking for a protected type, the discriminant may have
2344       --  been rewritten as the corresponding discriminal of the original type
2345       --  or of the corresponding concurrent record, depending on whether we
2346       --  are in the spec or body of the protected type.
2347
2348       return Ekind (E) = E_Discriminant
2349         or else
2350           (Check_Concurrent
2351             and then Ekind (E) = E_In_Parameter
2352             and then Present (Discriminal_Link (E))
2353             and then
2354               (Is_Concurrent_Type (Scope (Discriminal_Link (E)))
2355                 or else
2356                   Is_Concurrent_Record_Type (Scope (Discriminal_Link (E)))));
2357
2358    end Denotes_Discriminant;
2359
2360    -------------------------
2361    -- Denotes_Same_Object --
2362    -------------------------
2363
2364    function Denotes_Same_Object (A1, A2 : Node_Id) return Boolean is
2365       Obj1 : Node_Id := A1;
2366       Obj2 : Node_Id := A2;
2367
2368       procedure Check_Renaming (Obj : in out Node_Id);
2369       --  If an object is a renaming, examine renamed object. If it is a
2370       --  dereference of a variable, or an indexed expression with non-constant
2371       --  indexes, no overlap check can be reported.
2372
2373       --------------------
2374       -- Check_Renaming --
2375       --------------------
2376
2377       procedure Check_Renaming (Obj : in out Node_Id) is
2378       begin
2379          if Is_Entity_Name (Obj)
2380            and then Present (Renamed_Entity (Entity (Obj)))
2381          then
2382             Obj := Renamed_Entity (Entity (Obj));
2383             if Nkind (Obj) = N_Explicit_Dereference
2384               and then Is_Variable (Prefix (Obj))
2385             then
2386                Obj := Empty;
2387
2388             elsif Nkind (Obj) = N_Indexed_Component then
2389                declare
2390                   Indx : Node_Id;
2391
2392                begin
2393                   Indx := First (Expressions (Obj));
2394                   while Present (Indx) loop
2395                      if not Is_OK_Static_Expression (Indx) then
2396                         Obj := Empty;
2397                         exit;
2398                      end if;
2399
2400                      Next_Index (Indx);
2401                   end loop;
2402                end;
2403             end if;
2404          end if;
2405       end Check_Renaming;
2406
2407    --  Start of processing for Denotes_Same_Object
2408
2409    begin
2410       Check_Renaming (Obj1);
2411       Check_Renaming (Obj2);
2412
2413       if No (Obj1)
2414         or else No (Obj2)
2415       then
2416          return False;
2417       end if;
2418
2419       --  If we have entity names, then must be same entity
2420
2421       if Is_Entity_Name (Obj1) then
2422          if Is_Entity_Name (Obj2) then
2423             return Entity (Obj1) = Entity (Obj2);
2424          else
2425             return False;
2426          end if;
2427
2428       --  No match if not same node kind
2429
2430       elsif Nkind (Obj1) /= Nkind (Obj2) then
2431          return False;
2432
2433       --  For selected components, must have same prefix and selector
2434
2435       elsif Nkind (Obj1) = N_Selected_Component then
2436          return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
2437            and then
2438          Entity (Selector_Name (Obj1)) = Entity (Selector_Name (Obj2));
2439
2440       --  For explicit dereferences, prefixes must be same
2441
2442       elsif Nkind (Obj1) = N_Explicit_Dereference then
2443          return Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2));
2444
2445       --  For indexed components, prefixes and all subscripts must be the same
2446
2447       elsif Nkind (Obj1) = N_Indexed_Component then
2448          if Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2)) then
2449             declare
2450                Indx1 : Node_Id;
2451                Indx2 : Node_Id;
2452
2453             begin
2454                Indx1 := First (Expressions (Obj1));
2455                Indx2 := First (Expressions (Obj2));
2456                while Present (Indx1) loop
2457
2458                   --  Indexes must denote the same static value or same object
2459
2460                   if Is_OK_Static_Expression (Indx1) then
2461                      if not Is_OK_Static_Expression (Indx2) then
2462                         return False;
2463
2464                      elsif Expr_Value (Indx1) /= Expr_Value (Indx2) then
2465                         return False;
2466                      end if;
2467
2468                   elsif not Denotes_Same_Object (Indx1, Indx2) then
2469                      return False;
2470                   end if;
2471
2472                   Next (Indx1);
2473                   Next (Indx2);
2474                end loop;
2475
2476                return True;
2477             end;
2478          else
2479             return False;
2480          end if;
2481
2482       --  For slices, prefixes must match and bounds must match
2483
2484       elsif Nkind (Obj1) = N_Slice
2485         and then Denotes_Same_Object (Prefix (Obj1), Prefix (Obj2))
2486       then
2487          declare
2488             Lo1, Lo2, Hi1, Hi2 : Node_Id;
2489
2490          begin
2491             Get_Index_Bounds (Etype (Obj1), Lo1, Hi1);
2492             Get_Index_Bounds (Etype (Obj2), Lo2, Hi2);
2493
2494             --  Check whether bounds are statically identical. There is no
2495             --  attempt to detect partial overlap of slices.
2496
2497             return Denotes_Same_Object (Lo1, Lo2)
2498               and then Denotes_Same_Object (Hi1, Hi2);
2499          end;
2500
2501          --  Literals will appear as indexes. Isn't this where we should check
2502          --  Known_At_Compile_Time at least if we are generating warnings ???
2503
2504       elsif Nkind (Obj1) = N_Integer_Literal then
2505          return Intval (Obj1) = Intval (Obj2);
2506
2507       else
2508          return False;
2509       end if;
2510    end Denotes_Same_Object;
2511
2512    -------------------------
2513    -- Denotes_Same_Prefix --
2514    -------------------------
2515
2516    function Denotes_Same_Prefix (A1, A2 : Node_Id) return Boolean is
2517
2518    begin
2519       if Is_Entity_Name (A1) then
2520          if Nkind_In (A2, N_Selected_Component, N_Indexed_Component)
2521            and then not Is_Access_Type (Etype (A1))
2522          then
2523             return Denotes_Same_Object (A1, Prefix (A2))
2524               or else Denotes_Same_Prefix (A1, Prefix (A2));
2525          else
2526             return False;
2527          end if;
2528
2529       elsif Is_Entity_Name (A2) then
2530          return Denotes_Same_Prefix (A2, A1);
2531
2532       elsif Nkind_In (A1, N_Selected_Component, N_Indexed_Component, N_Slice)
2533               and then
2534             Nkind_In (A2, N_Selected_Component, N_Indexed_Component, N_Slice)
2535       then
2536          declare
2537             Root1, Root2 : Node_Id;
2538             Depth1, Depth2 : Int := 0;
2539
2540          begin
2541             Root1 := Prefix (A1);
2542             while not Is_Entity_Name (Root1) loop
2543                if not Nkind_In
2544                  (Root1, N_Selected_Component, N_Indexed_Component)
2545                then
2546                   return False;
2547                else
2548                   Root1 := Prefix (Root1);
2549                end if;
2550
2551                Depth1 := Depth1 + 1;
2552             end loop;
2553
2554             Root2 := Prefix (A2);
2555             while not Is_Entity_Name (Root2) loop
2556                if not Nkind_In
2557                  (Root2, N_Selected_Component, N_Indexed_Component)
2558                then
2559                   return False;
2560                else
2561                   Root2 := Prefix (Root2);
2562                end if;
2563
2564                Depth2 := Depth2 + 1;
2565             end loop;
2566
2567             --  If both have the same depth and they do not denote the same
2568             --  object, they are disjoint and not warning is needed.
2569
2570             if Depth1 = Depth2 then
2571                return False;
2572
2573             elsif Depth1 > Depth2 then
2574                Root1 := Prefix (A1);
2575                for I in 1 .. Depth1 - Depth2 - 1 loop
2576                   Root1 := Prefix (Root1);
2577                end loop;
2578
2579                return Denotes_Same_Object (Root1, A2);
2580
2581             else
2582                Root2 := Prefix (A2);
2583                for I in 1 .. Depth2 - Depth1 - 1 loop
2584                   Root2 := Prefix (Root2);
2585                end loop;
2586
2587                return Denotes_Same_Object (A1, Root2);
2588             end if;
2589          end;
2590
2591       else
2592          return False;
2593       end if;
2594    end Denotes_Same_Prefix;
2595
2596    ----------------------
2597    -- Denotes_Variable --
2598    ----------------------
2599
2600    function Denotes_Variable (N : Node_Id) return Boolean is
2601    begin
2602       return Is_Variable (N) and then Paren_Count (N) = 0;
2603    end Denotes_Variable;
2604
2605    -----------------------------
2606    -- Depends_On_Discriminant --
2607    -----------------------------
2608
2609    function Depends_On_Discriminant (N : Node_Id) return Boolean is
2610       L : Node_Id;
2611       H : Node_Id;
2612
2613    begin
2614       Get_Index_Bounds (N, L, H);
2615       return Denotes_Discriminant (L) or else Denotes_Discriminant (H);
2616    end Depends_On_Discriminant;
2617
2618    -------------------------
2619    -- Designate_Same_Unit --
2620    -------------------------
2621
2622    function Designate_Same_Unit
2623      (Name1 : Node_Id;
2624       Name2 : Node_Id) return Boolean
2625    is
2626       K1 : constant Node_Kind := Nkind (Name1);
2627       K2 : constant Node_Kind := Nkind (Name2);
2628
2629       function Prefix_Node (N : Node_Id) return Node_Id;
2630       --  Returns the parent unit name node of a defining program unit name
2631       --  or the prefix if N is a selected component or an expanded name.
2632
2633       function Select_Node (N : Node_Id) return Node_Id;
2634       --  Returns the defining identifier node of a defining program unit
2635       --  name or  the selector node if N is a selected component or an
2636       --  expanded name.
2637
2638       -----------------
2639       -- Prefix_Node --
2640       -----------------
2641
2642       function Prefix_Node (N : Node_Id) return Node_Id is
2643       begin
2644          if Nkind (N) = N_Defining_Program_Unit_Name then
2645             return Name (N);
2646
2647          else
2648             return Prefix (N);
2649          end if;
2650       end Prefix_Node;
2651
2652       -----------------
2653       -- Select_Node --
2654       -----------------
2655
2656       function Select_Node (N : Node_Id) return Node_Id is
2657       begin
2658          if Nkind (N) = N_Defining_Program_Unit_Name then
2659             return Defining_Identifier (N);
2660
2661          else
2662             return Selector_Name (N);
2663          end if;
2664       end Select_Node;
2665
2666    --  Start of processing for Designate_Next_Unit
2667
2668    begin
2669       if (K1 = N_Identifier or else
2670           K1 = N_Defining_Identifier)
2671         and then
2672          (K2 = N_Identifier or else
2673           K2 = N_Defining_Identifier)
2674       then
2675          return Chars (Name1) = Chars (Name2);
2676
2677       elsif
2678          (K1 = N_Expanded_Name      or else
2679           K1 = N_Selected_Component or else
2680           K1 = N_Defining_Program_Unit_Name)
2681         and then
2682          (K2 = N_Expanded_Name      or else
2683           K2 = N_Selected_Component or else
2684           K2 = N_Defining_Program_Unit_Name)
2685       then
2686          return
2687            (Chars (Select_Node (Name1)) = Chars (Select_Node (Name2)))
2688              and then
2689                Designate_Same_Unit (Prefix_Node (Name1), Prefix_Node (Name2));
2690
2691       else
2692          return False;
2693       end if;
2694    end Designate_Same_Unit;
2695
2696    --------------------------
2697    -- Enclosing_CPP_Parent --
2698    --------------------------
2699
2700    function Enclosing_CPP_Parent (Typ : Entity_Id) return Entity_Id is
2701       Parent_Typ : Entity_Id := Typ;
2702
2703    begin
2704       while not Is_CPP_Class (Parent_Typ)
2705          and then Etype (Parent_Typ) /= Parent_Typ
2706       loop
2707          Parent_Typ := Etype (Parent_Typ);
2708
2709          if Is_Private_Type (Parent_Typ) then
2710             Parent_Typ := Full_View (Base_Type (Parent_Typ));
2711          end if;
2712       end loop;
2713
2714       pragma Assert (Is_CPP_Class (Parent_Typ));
2715       return Parent_Typ;
2716    end Enclosing_CPP_Parent;
2717
2718    ----------------------------
2719    -- Enclosing_Generic_Body --
2720    ----------------------------
2721
2722    function Enclosing_Generic_Body
2723      (N : Node_Id) return Node_Id
2724    is
2725       P    : Node_Id;
2726       Decl : Node_Id;
2727       Spec : Node_Id;
2728
2729    begin
2730       P := Parent (N);
2731       while Present (P) loop
2732          if Nkind (P) = N_Package_Body
2733            or else Nkind (P) = N_Subprogram_Body
2734          then
2735             Spec := Corresponding_Spec (P);
2736
2737             if Present (Spec) then
2738                Decl := Unit_Declaration_Node (Spec);
2739
2740                if Nkind (Decl) = N_Generic_Package_Declaration
2741                  or else Nkind (Decl) = N_Generic_Subprogram_Declaration
2742                then
2743                   return P;
2744                end if;
2745             end if;
2746          end if;
2747
2748          P := Parent (P);
2749       end loop;
2750
2751       return Empty;
2752    end Enclosing_Generic_Body;
2753
2754    ----------------------------
2755    -- Enclosing_Generic_Unit --
2756    ----------------------------
2757
2758    function Enclosing_Generic_Unit
2759      (N : Node_Id) return Node_Id
2760    is
2761       P    : Node_Id;
2762       Decl : Node_Id;
2763       Spec : Node_Id;
2764
2765    begin
2766       P := Parent (N);
2767       while Present (P) loop
2768          if Nkind (P) = N_Generic_Package_Declaration
2769            or else Nkind (P) = N_Generic_Subprogram_Declaration
2770          then
2771             return P;
2772
2773          elsif Nkind (P) = N_Package_Body
2774            or else Nkind (P) = N_Subprogram_Body
2775          then
2776             Spec := Corresponding_Spec (P);
2777
2778             if Present (Spec) then
2779                Decl := Unit_Declaration_Node (Spec);
2780
2781                if Nkind (Decl) = N_Generic_Package_Declaration
2782                  or else Nkind (Decl) = N_Generic_Subprogram_Declaration
2783                then
2784                   return Decl;
2785                end if;
2786             end if;
2787          end if;
2788
2789          P := Parent (P);
2790       end loop;
2791
2792       return Empty;
2793    end Enclosing_Generic_Unit;
2794
2795    -------------------------------
2796    -- Enclosing_Lib_Unit_Entity --
2797    -------------------------------
2798
2799    function Enclosing_Lib_Unit_Entity return Entity_Id is
2800       Unit_Entity : Entity_Id;
2801
2802    begin
2803       --  Look for enclosing library unit entity by following scope links.
2804       --  Equivalent to, but faster than indexing through the scope stack.
2805
2806       Unit_Entity := Current_Scope;
2807       while (Present (Scope (Unit_Entity))
2808         and then Scope (Unit_Entity) /= Standard_Standard)
2809         and not Is_Child_Unit (Unit_Entity)
2810       loop
2811          Unit_Entity := Scope (Unit_Entity);
2812       end loop;
2813
2814       return Unit_Entity;
2815    end Enclosing_Lib_Unit_Entity;
2816
2817    -----------------------------
2818    -- Enclosing_Lib_Unit_Node --
2819    -----------------------------
2820
2821    function Enclosing_Lib_Unit_Node (N : Node_Id) return Node_Id is
2822       Current_Node : Node_Id;
2823
2824    begin
2825       Current_Node := N;
2826       while Present (Current_Node)
2827         and then Nkind (Current_Node) /= N_Compilation_Unit
2828       loop
2829          Current_Node := Parent (Current_Node);
2830       end loop;
2831
2832       if Nkind (Current_Node) /= N_Compilation_Unit then
2833          return Empty;
2834       end if;
2835
2836       return Current_Node;
2837    end Enclosing_Lib_Unit_Node;
2838
2839    --------------------------
2840    -- Enclosing_Subprogram --
2841    --------------------------
2842
2843    function Enclosing_Subprogram (E : Entity_Id) return Entity_Id is
2844       Dynamic_Scope : constant Entity_Id := Enclosing_Dynamic_Scope (E);
2845
2846    begin
2847       if Dynamic_Scope = Standard_Standard then
2848          return Empty;
2849
2850       elsif Dynamic_Scope = Empty then
2851          return Empty;
2852
2853       elsif Ekind (Dynamic_Scope) = E_Subprogram_Body then
2854          return Corresponding_Spec (Parent (Parent (Dynamic_Scope)));
2855
2856       elsif Ekind (Dynamic_Scope) = E_Block
2857         or else Ekind (Dynamic_Scope) = E_Return_Statement
2858       then
2859          return Enclosing_Subprogram (Dynamic_Scope);
2860
2861       elsif Ekind (Dynamic_Scope) = E_Task_Type then
2862          return Get_Task_Body_Procedure (Dynamic_Scope);
2863
2864       elsif Ekind (Dynamic_Scope) = E_Limited_Private_Type
2865         and then Present (Full_View (Dynamic_Scope))
2866         and then Ekind (Full_View (Dynamic_Scope)) = E_Task_Type
2867       then
2868          return Get_Task_Body_Procedure (Full_View (Dynamic_Scope));
2869
2870       --  No body is generated if the protected operation is eliminated
2871
2872       elsif Convention (Dynamic_Scope) = Convention_Protected
2873         and then not Is_Eliminated (Dynamic_Scope)
2874         and then Present (Protected_Body_Subprogram (Dynamic_Scope))
2875       then
2876          return Protected_Body_Subprogram (Dynamic_Scope);
2877
2878       else
2879          return Dynamic_Scope;
2880       end if;
2881    end Enclosing_Subprogram;
2882
2883    ------------------------
2884    -- Ensure_Freeze_Node --
2885    ------------------------
2886
2887    procedure Ensure_Freeze_Node (E : Entity_Id) is
2888       FN : Node_Id;
2889
2890    begin
2891       if No (Freeze_Node (E)) then
2892          FN := Make_Freeze_Entity (Sloc (E));
2893          Set_Has_Delayed_Freeze (E);
2894          Set_Freeze_Node (E, FN);
2895          Set_Access_Types_To_Process (FN, No_Elist);
2896          Set_TSS_Elist (FN, No_Elist);
2897          Set_Entity (FN, E);
2898       end if;
2899    end Ensure_Freeze_Node;
2900
2901    ----------------
2902    -- Enter_Name --
2903    ----------------
2904
2905    procedure Enter_Name (Def_Id : Entity_Id) is
2906       C : constant Entity_Id := Current_Entity (Def_Id);
2907       E : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
2908       S : constant Entity_Id := Current_Scope;
2909
2910    begin
2911       Generate_Definition (Def_Id);
2912
2913       --  Add new name to current scope declarations. Check for duplicate
2914       --  declaration, which may or may not be a genuine error.
2915
2916       if Present (E) then
2917
2918          --  Case of previous entity entered because of a missing declaration
2919          --  or else a bad subtype indication. Best is to use the new entity,
2920          --  and make the previous one invisible.
2921
2922          if Etype (E) = Any_Type then
2923             Set_Is_Immediately_Visible (E, False);
2924
2925          --  Case of renaming declaration constructed for package instances.
2926          --  if there is an explicit declaration with the same identifier,
2927          --  the renaming is not immediately visible any longer, but remains
2928          --  visible through selected component notation.
2929
2930          elsif Nkind (Parent (E)) = N_Package_Renaming_Declaration
2931            and then not Comes_From_Source (E)
2932          then
2933             Set_Is_Immediately_Visible (E, False);
2934
2935          --  The new entity may be the package renaming, which has the same
2936          --  same name as a generic formal which has been seen already.
2937
2938          elsif Nkind (Parent (Def_Id)) = N_Package_Renaming_Declaration
2939             and then not Comes_From_Source (Def_Id)
2940          then
2941             Set_Is_Immediately_Visible (E, False);
2942
2943          --  For a fat pointer corresponding to a remote access to subprogram,
2944          --  we use the same identifier as the RAS type, so that the proper
2945          --  name appears in the stub. This type is only retrieved through
2946          --  the RAS type and never by visibility, and is not added to the
2947          --  visibility list (see below).
2948
2949          elsif Nkind (Parent (Def_Id)) = N_Full_Type_Declaration
2950            and then Present (Corresponding_Remote_Type (Def_Id))
2951          then
2952             null;
2953
2954          --  A controller component for a type extension overrides the
2955          --  inherited component.
2956
2957          elsif Chars (E) = Name_uController then
2958             null;
2959
2960          --  Case of an implicit operation or derived literal. The new entity
2961          --  hides the implicit one,  which is removed from all visibility,
2962          --  i.e. the entity list of its scope, and homonym chain of its name.
2963
2964          elsif (Is_Overloadable (E) and then Is_Inherited_Operation (E))
2965            or else Is_Internal (E)
2966          then
2967             declare
2968                Prev     : Entity_Id;
2969                Prev_Vis : Entity_Id;
2970                Decl     : constant Node_Id := Parent (E);
2971
2972             begin
2973                --  If E is an implicit declaration, it cannot be the first
2974                --  entity in the scope.
2975
2976                Prev := First_Entity (Current_Scope);
2977                while Present (Prev)
2978                  and then Next_Entity (Prev) /= E
2979                loop
2980                   Next_Entity (Prev);
2981                end loop;
2982
2983                if No (Prev) then
2984
2985                   --  If E is not on the entity chain of the current scope,
2986                   --  it is an implicit declaration in the generic formal
2987                   --  part of a generic subprogram. When analyzing the body,
2988                   --  the generic formals are visible but not on the entity
2989                   --  chain of the subprogram. The new entity will become
2990                   --  the visible one in the body.
2991
2992                   pragma Assert
2993                     (Nkind (Parent (Decl)) = N_Generic_Subprogram_Declaration);
2994                   null;
2995
2996                else
2997                   Set_Next_Entity (Prev, Next_Entity (E));
2998
2999                   if No (Next_Entity (Prev)) then
3000                      Set_Last_Entity (Current_Scope, Prev);
3001                   end if;
3002
3003                   if E = Current_Entity (E) then
3004                      Prev_Vis := Empty;
3005
3006                   else
3007                      Prev_Vis := Current_Entity (E);
3008                      while Homonym (Prev_Vis) /= E loop
3009                         Prev_Vis := Homonym (Prev_Vis);
3010                      end loop;
3011                   end if;
3012
3013                   if Present (Prev_Vis)  then
3014
3015                      --  Skip E in the visibility chain
3016
3017                      Set_Homonym (Prev_Vis, Homonym (E));
3018
3019                   else
3020                      Set_Name_Entity_Id (Chars (E), Homonym (E));
3021                   end if;
3022                end if;
3023             end;
3024
3025          --  This section of code could use a comment ???
3026
3027          elsif Present (Etype (E))
3028            and then Is_Concurrent_Type (Etype (E))
3029            and then E = Def_Id
3030          then
3031             return;
3032
3033          --  If the homograph is a protected component renaming, it should not
3034          --  be hiding the current entity. Such renamings are treated as weak
3035          --  declarations.
3036
3037          elsif Is_Prival (E) then
3038             Set_Is_Immediately_Visible (E, False);
3039
3040          --  In this case the current entity is a protected component renaming.
3041          --  Perform minimal decoration by setting the scope and return since
3042          --  the prival should not be hiding other visible entities.
3043
3044          elsif Is_Prival (Def_Id) then
3045             Set_Scope (Def_Id, Current_Scope);
3046             return;
3047
3048          --  Analogous to privals, the discriminal generated for an entry
3049          --  index parameter acts as a weak declaration. Perform minimal
3050          --  decoration to avoid bogus errors.
3051
3052          elsif Is_Discriminal (Def_Id)
3053            and then Ekind (Discriminal_Link (Def_Id)) = E_Entry_Index_Parameter
3054          then
3055             Set_Scope (Def_Id, Current_Scope);
3056             return;
3057
3058          --  In the body or private part of an instance, a type extension
3059          --  may introduce a component with the same name as that of an
3060          --  actual. The legality rule is not enforced, but the semantics
3061          --  of the full type with two components of the same name are not
3062          --  clear at this point ???
3063
3064          elsif In_Instance_Not_Visible then
3065             null;
3066
3067          --  When compiling a package body, some child units may have become
3068          --  visible. They cannot conflict with local entities that hide them.
3069
3070          elsif Is_Child_Unit (E)
3071            and then In_Open_Scopes (Scope (E))
3072            and then not Is_Immediately_Visible (E)
3073          then
3074             null;
3075
3076          --  Conversely, with front-end inlining we may compile the parent
3077          --  body first, and a child unit subsequently. The context is now
3078          --  the parent spec, and body entities are not visible.
3079
3080          elsif Is_Child_Unit (Def_Id)
3081            and then Is_Package_Body_Entity (E)
3082            and then not In_Package_Body (Current_Scope)
3083          then
3084             null;
3085
3086          --  Case of genuine duplicate declaration
3087
3088          else
3089             Error_Msg_Sloc := Sloc (E);
3090
3091             --  If the previous declaration is an incomplete type declaration
3092             --  this may be an attempt to complete it with a private type.
3093             --  The following avoids confusing cascaded errors.
3094
3095             if Nkind (Parent (E)) = N_Incomplete_Type_Declaration
3096               and then Nkind (Parent (Def_Id)) = N_Private_Type_Declaration
3097             then
3098                Error_Msg_N
3099                  ("incomplete type cannot be completed with a private " &
3100                   "declaration", Parent (Def_Id));
3101                Set_Is_Immediately_Visible (E, False);
3102                Set_Full_View (E, Def_Id);
3103
3104             --  An inherited component of a record conflicts with a new
3105             --  discriminant. The discriminant is inserted first in the scope,
3106             --  but the error should be posted on it, not on the component.
3107
3108             elsif Ekind (E) = E_Discriminant
3109               and then Present (Scope (Def_Id))
3110               and then Scope (Def_Id) /= Current_Scope
3111             then
3112                Error_Msg_Sloc := Sloc (Def_Id);
3113                Error_Msg_N ("& conflicts with declaration#", E);
3114                return;
3115
3116             --  If the name of the unit appears in its own context clause,
3117             --  a dummy package with the name has already been created, and
3118             --  the error emitted. Try to continue quietly.
3119
3120             elsif Error_Posted (E)
3121               and then Sloc (E) = No_Location
3122               and then Nkind (Parent (E)) = N_Package_Specification
3123               and then Current_Scope = Standard_Standard
3124             then
3125                Set_Scope (Def_Id, Current_Scope);
3126                return;
3127
3128             else
3129                Error_Msg_N ("& conflicts with declaration#", Def_Id);
3130
3131                --  Avoid cascaded messages with duplicate components in
3132                --  derived types.
3133
3134                if Ekind_In (E, E_Component, E_Discriminant) then
3135                   return;
3136                end if;
3137             end if;
3138
3139             if Nkind (Parent (Parent (Def_Id))) =
3140                 N_Generic_Subprogram_Declaration
3141               and then Def_Id =
3142                 Defining_Entity (Specification (Parent (Parent (Def_Id))))
3143             then
3144                Error_Msg_N ("\generic units cannot be overloaded", Def_Id);
3145             end if;
3146
3147             --  If entity is in standard, then we are in trouble, because
3148             --  it means that we have a library package with a duplicated
3149             --  name. That's hard to recover from, so abort!
3150
3151             if S = Standard_Standard then
3152                raise Unrecoverable_Error;
3153
3154             --  Otherwise we continue with the declaration. Having two
3155             --  identical declarations should not cause us too much trouble!
3156
3157             else
3158                null;
3159             end if;
3160          end if;
3161       end if;
3162
3163       --  If we fall through, declaration is OK , or OK enough to continue
3164
3165       --  If Def_Id is a discriminant or a record component we are in the
3166       --  midst of inheriting components in a derived record definition.
3167       --  Preserve their Ekind and Etype.
3168
3169       if Ekind_In (Def_Id, E_Discriminant, E_Component) then
3170          null;
3171
3172       --  If a type is already set, leave it alone (happens whey a type
3173       --  declaration is reanalyzed following a call to the optimizer)
3174
3175       elsif Present (Etype (Def_Id)) then
3176          null;
3177
3178       --  Otherwise, the kind E_Void insures that premature uses of the entity
3179       --  will be detected. Any_Type insures that no cascaded errors will occur
3180
3181       else
3182          Set_Ekind (Def_Id, E_Void);
3183          Set_Etype (Def_Id, Any_Type);
3184       end if;
3185
3186       --  Inherited discriminants and components in derived record types are
3187       --  immediately visible. Itypes are not.
3188
3189       if Ekind_In (Def_Id, E_Discriminant, E_Component)
3190         or else (No (Corresponding_Remote_Type (Def_Id))
3191                  and then not Is_Itype (Def_Id))
3192       then
3193          Set_Is_Immediately_Visible (Def_Id);
3194          Set_Current_Entity         (Def_Id);
3195       end if;
3196
3197       Set_Homonym       (Def_Id, C);
3198       Append_Entity     (Def_Id, S);
3199       Set_Public_Status (Def_Id);
3200
3201       --  Warn if new entity hides an old one
3202
3203       if Warn_On_Hiding and then Present (C)
3204
3205          --  Don't warn for record components since they always have a well
3206          --  defined scope which does not confuse other uses. Note that in
3207          --  some cases, Ekind has not been set yet.
3208
3209          and then Ekind (C) /= E_Component
3210          and then Ekind (C) /= E_Discriminant
3211          and then Nkind (Parent (C)) /= N_Component_Declaration
3212          and then Ekind (Def_Id) /= E_Component
3213          and then Ekind (Def_Id) /= E_Discriminant
3214          and then Nkind (Parent (Def_Id)) /= N_Component_Declaration
3215
3216          --  Don't warn for one character variables. It is too common to use
3217          --  such variables as locals and will just cause too many false hits.
3218
3219          and then Length_Of_Name (Chars (C)) /= 1
3220
3221          --  Don't warn for non-source entities
3222
3223          and then Comes_From_Source (C)
3224          and then Comes_From_Source (Def_Id)
3225
3226          --  Don't warn unless entity in question is in extended main source
3227
3228          and then In_Extended_Main_Source_Unit (Def_Id)
3229
3230          --  Finally, the hidden entity must be either immediately visible
3231          --  or use visible (from a used package)
3232
3233          and then
3234            (Is_Immediately_Visible (C)
3235               or else
3236             Is_Potentially_Use_Visible (C))
3237       then
3238          Error_Msg_Sloc := Sloc (C);
3239          Error_Msg_N ("declaration hides &#?", Def_Id);
3240       end if;
3241    end Enter_Name;
3242
3243    --------------------------
3244    -- Explain_Limited_Type --
3245    --------------------------
3246
3247    procedure Explain_Limited_Type (T : Entity_Id; N : Node_Id) is
3248       C : Entity_Id;
3249
3250    begin
3251       --  For array, component type must be limited
3252
3253       if Is_Array_Type (T) then
3254          Error_Msg_Node_2 := T;
3255          Error_Msg_NE
3256            ("\component type& of type& is limited", N, Component_Type (T));
3257          Explain_Limited_Type (Component_Type (T), N);
3258
3259       elsif Is_Record_Type (T) then
3260
3261          --  No need for extra messages if explicit limited record
3262
3263          if Is_Limited_Record (Base_Type (T)) then
3264             return;
3265          end if;
3266
3267          --  Otherwise find a limited component. Check only components that
3268          --  come from source, or inherited components that appear in the
3269          --  source of the ancestor.
3270
3271          C := First_Component (T);
3272          while Present (C) loop
3273             if Is_Limited_Type (Etype (C))
3274               and then
3275                 (Comes_From_Source (C)
3276                    or else
3277                      (Present (Original_Record_Component (C))
3278                        and then
3279                          Comes_From_Source (Original_Record_Component (C))))
3280             then
3281                Error_Msg_Node_2 := T;
3282                Error_Msg_NE ("\component& of type& has limited type", N, C);
3283                Explain_Limited_Type (Etype (C), N);
3284                return;
3285             end if;
3286
3287             Next_Component (C);
3288          end loop;
3289
3290          --  The type may be declared explicitly limited, even if no component
3291          --  of it is limited, in which case we fall out of the loop.
3292          return;
3293       end if;
3294    end Explain_Limited_Type;
3295
3296    -----------------
3297    -- Find_Actual --
3298    -----------------
3299
3300    procedure Find_Actual
3301      (N        : Node_Id;
3302       Formal   : out Entity_Id;
3303       Call     : out Node_Id)
3304    is
3305       Parnt  : constant Node_Id := Parent (N);
3306       Actual : Node_Id;
3307
3308    begin
3309       if (Nkind (Parnt) = N_Indexed_Component
3310             or else
3311           Nkind (Parnt) = N_Selected_Component)
3312         and then N = Prefix (Parnt)
3313       then
3314          Find_Actual (Parnt, Formal, Call);
3315          return;
3316
3317       elsif Nkind (Parnt) = N_Parameter_Association
3318         and then N = Explicit_Actual_Parameter (Parnt)
3319       then
3320          Call := Parent (Parnt);
3321
3322       elsif Nkind (Parnt) = N_Procedure_Call_Statement then
3323          Call := Parnt;
3324
3325       else
3326          Formal := Empty;
3327          Call   := Empty;
3328          return;
3329       end if;
3330
3331       --  If we have a call to a subprogram look for the parameter. Note that
3332       --  we exclude overloaded calls, since we don't know enough to be sure
3333       --  of giving the right answer in this case.
3334
3335       if Is_Entity_Name (Name (Call))
3336         and then Present (Entity (Name (Call)))
3337         and then Is_Overloadable (Entity (Name (Call)))
3338         and then not Is_Overloaded (Name (Call))
3339       then
3340          --  Fall here if we are definitely a parameter
3341
3342          Actual := First_Actual (Call);
3343          Formal := First_Formal (Entity (Name (Call)));
3344          while Present (Formal) and then Present (Actual) loop
3345             if Actual = N then
3346                return;
3347             else
3348                Actual := Next_Actual (Actual);
3349                Formal := Next_Formal (Formal);
3350             end if;
3351          end loop;
3352       end if;
3353
3354       --  Fall through here if we did not find matching actual
3355
3356       Formal := Empty;
3357       Call   := Empty;
3358    end Find_Actual;
3359
3360    ---------------------------
3361    -- Find_Body_Discriminal --
3362    ---------------------------
3363
3364    function Find_Body_Discriminal
3365      (Spec_Discriminant : Entity_Id) return Entity_Id
3366    is
3367       pragma Assert (Is_Concurrent_Record_Type (Scope (Spec_Discriminant)));
3368
3369       Tsk  : constant Entity_Id :=
3370                Corresponding_Concurrent_Type (Scope (Spec_Discriminant));
3371       Disc : Entity_Id;
3372
3373    begin
3374       --  Find discriminant of original concurrent type, and use its current
3375       --  discriminal, which is the renaming within the task/protected body.
3376
3377       Disc := First_Discriminant (Tsk);
3378       while Present (Disc) loop
3379          if Chars (Disc) = Chars (Spec_Discriminant) then
3380             return Discriminal (Disc);
3381          end if;
3382
3383          Next_Discriminant (Disc);
3384       end loop;
3385
3386       --  That loop should always succeed in finding a matching entry and
3387       --  returning. Fatal error if not.
3388
3389       raise Program_Error;
3390    end Find_Body_Discriminal;
3391
3392    -------------------------------------
3393    -- Find_Corresponding_Discriminant --
3394    -------------------------------------
3395
3396    function Find_Corresponding_Discriminant
3397      (Id  : Node_Id;
3398       Typ : Entity_Id) return Entity_Id
3399    is
3400       Par_Disc : Entity_Id;
3401       Old_Disc : Entity_Id;
3402       New_Disc : Entity_Id;
3403
3404    begin
3405       Par_Disc := Original_Record_Component (Original_Discriminant (Id));
3406
3407       --  The original type may currently be private, and the discriminant
3408       --  only appear on its full view.
3409
3410       if Is_Private_Type (Scope (Par_Disc))
3411         and then not Has_Discriminants (Scope (Par_Disc))
3412         and then Present (Full_View (Scope (Par_Disc)))
3413       then
3414          Old_Disc := First_Discriminant (Full_View (Scope (Par_Disc)));
3415       else
3416          Old_Disc := First_Discriminant (Scope (Par_Disc));
3417       end if;
3418
3419       if Is_Class_Wide_Type (Typ) then
3420          New_Disc := First_Discriminant (Root_Type (Typ));
3421       else
3422          New_Disc := First_Discriminant (Typ);
3423       end if;
3424
3425       while Present (Old_Disc) and then Present (New_Disc) loop
3426          if Old_Disc = Par_Disc  then
3427             return New_Disc;
3428          else
3429             Next_Discriminant (Old_Disc);
3430             Next_Discriminant (New_Disc);
3431          end if;
3432       end loop;
3433
3434       --  Should always find it
3435
3436       raise Program_Error;
3437    end Find_Corresponding_Discriminant;
3438
3439    --------------------------
3440    -- Find_Overlaid_Entity --
3441    --------------------------
3442
3443    procedure Find_Overlaid_Entity
3444      (N   : Node_Id;
3445       Ent : out Entity_Id;
3446       Off : out Boolean)
3447    is
3448       Expr : Node_Id;
3449
3450    begin
3451       --  We are looking for one of the two following forms:
3452
3453       --    for X'Address use Y'Address
3454
3455       --  or
3456
3457       --    Const : constant Address := expr;
3458       --    ...
3459       --    for X'Address use Const;
3460
3461       --  In the second case, the expr is either Y'Address, or recursively a
3462       --  constant that eventually references Y'Address.
3463
3464       Ent := Empty;
3465       Off := False;
3466
3467       if Nkind (N) = N_Attribute_Definition_Clause
3468         and then Chars (N) = Name_Address
3469       then
3470          Expr := Expression (N);
3471
3472          --  This loop checks the form of the expression for Y'Address,
3473          --  using recursion to deal with intermediate constants.
3474
3475          loop
3476             --  Check for Y'Address
3477
3478             if Nkind (Expr) = N_Attribute_Reference
3479               and then Attribute_Name (Expr) = Name_Address
3480             then
3481                Expr := Prefix (Expr);
3482                exit;
3483
3484                --  Check for Const where Const is a constant entity
3485
3486             elsif Is_Entity_Name (Expr)
3487               and then Ekind (Entity (Expr)) = E_Constant
3488             then
3489                Expr := Constant_Value (Entity (Expr));
3490
3491             --  Anything else does not need checking
3492
3493             else
3494                return;
3495             end if;
3496          end loop;
3497
3498          --  This loop checks the form of the prefix for an entity,
3499          --  using recursion to deal with intermediate components.
3500
3501          loop
3502             --  Check for Y where Y is an entity
3503
3504             if Is_Entity_Name (Expr) then
3505                Ent := Entity (Expr);
3506                return;
3507
3508             --  Check for components
3509
3510             elsif
3511                Nkind_In (Expr, N_Selected_Component, N_Indexed_Component) then
3512
3513                Expr := Prefix (Expr);
3514                Off := True;
3515
3516             --  Anything else does not need checking
3517
3518             else
3519                return;
3520             end if;
3521          end loop;
3522       end if;
3523    end Find_Overlaid_Entity;
3524
3525    -------------------------
3526    -- Find_Parameter_Type --
3527    -------------------------
3528
3529    function Find_Parameter_Type (Param : Node_Id) return Entity_Id is
3530    begin
3531       if Nkind (Param) /= N_Parameter_Specification then
3532          return Empty;
3533
3534       --  For an access parameter, obtain the type from the formal entity
3535       --  itself, because access to subprogram nodes do not carry a type.
3536       --  Shouldn't we always use the formal entity ???
3537
3538       elsif Nkind (Parameter_Type (Param)) = N_Access_Definition then
3539          return Etype (Defining_Identifier (Param));
3540
3541       else
3542          return Etype (Parameter_Type (Param));
3543       end if;
3544    end Find_Parameter_Type;
3545
3546    -----------------------------
3547    -- Find_Static_Alternative --
3548    -----------------------------
3549
3550    function Find_Static_Alternative (N : Node_Id) return Node_Id is
3551       Expr   : constant Node_Id := Expression (N);
3552       Val    : constant Uint    := Expr_Value (Expr);
3553       Alt    : Node_Id;
3554       Choice : Node_Id;
3555
3556    begin
3557       Alt := First (Alternatives (N));
3558
3559       Search : loop
3560          if Nkind (Alt) /= N_Pragma then
3561             Choice := First (Discrete_Choices (Alt));
3562             while Present (Choice) loop
3563
3564                --  Others choice, always matches
3565
3566                if Nkind (Choice) = N_Others_Choice then
3567                   exit Search;
3568
3569                --  Range, check if value is in the range
3570
3571                elsif Nkind (Choice) = N_Range then
3572                   exit Search when
3573                     Val >= Expr_Value (Low_Bound (Choice))
3574                       and then
3575                     Val <= Expr_Value (High_Bound (Choice));
3576
3577                --  Choice is a subtype name. Note that we know it must
3578                --  be a static subtype, since otherwise it would have
3579                --  been diagnosed as illegal.
3580
3581                elsif Is_Entity_Name (Choice)
3582                  and then Is_Type (Entity (Choice))
3583                then
3584                   exit Search when Is_In_Range (Expr, Etype (Choice),
3585                                                 Assume_Valid => False);
3586
3587                --  Choice is a subtype indication
3588
3589                elsif Nkind (Choice) = N_Subtype_Indication then
3590                   declare
3591                      C : constant Node_Id := Constraint (Choice);
3592                      R : constant Node_Id := Range_Expression (C);
3593
3594                   begin
3595                      exit Search when
3596                        Val >= Expr_Value (Low_Bound (R))
3597                          and then
3598                        Val <= Expr_Value (High_Bound (R));
3599                   end;
3600
3601                --  Choice is a simple expression
3602
3603                else
3604                   exit Search when Val = Expr_Value (Choice);
3605                end if;
3606
3607                Next (Choice);
3608             end loop;
3609          end if;
3610
3611          Next (Alt);
3612          pragma Assert (Present (Alt));
3613       end loop Search;
3614
3615       --  The above loop *must* terminate by finding a match, since
3616       --  we know the case statement is valid, and the value of the
3617       --  expression is known at compile time. When we fall out of
3618       --  the loop, Alt points to the alternative that we know will
3619       --  be selected at run time.
3620
3621       return Alt;
3622    end Find_Static_Alternative;
3623
3624    ------------------
3625    -- First_Actual --
3626    ------------------
3627
3628    function First_Actual (Node : Node_Id) return Node_Id is
3629       N : Node_Id;
3630
3631    begin
3632       if No (Parameter_Associations (Node)) then
3633          return Empty;
3634       end if;
3635
3636       N := First (Parameter_Associations (Node));
3637
3638       if Nkind (N) = N_Parameter_Association then
3639          return First_Named_Actual (Node);
3640       else
3641          return N;
3642       end if;
3643    end First_Actual;
3644
3645    -----------------------
3646    -- Gather_Components --
3647    -----------------------
3648
3649    procedure Gather_Components
3650      (Typ           : Entity_Id;
3651       Comp_List     : Node_Id;
3652       Governed_By   : List_Id;
3653       Into          : Elist_Id;
3654       Report_Errors : out Boolean)
3655    is
3656       Assoc           : Node_Id;
3657       Variant         : Node_Id;
3658       Discrete_Choice : Node_Id;
3659       Comp_Item       : Node_Id;
3660
3661       Discrim       : Entity_Id;
3662       Discrim_Name  : Node_Id;
3663       Discrim_Value : Node_Id;
3664
3665    begin
3666       Report_Errors := False;
3667
3668       if No (Comp_List) or else Null_Present (Comp_List) then
3669          return;
3670
3671       elsif Present (Component_Items (Comp_List)) then
3672          Comp_Item := First (Component_Items (Comp_List));
3673
3674       else
3675          Comp_Item := Empty;
3676       end if;
3677
3678       while Present (Comp_Item) loop
3679
3680          --  Skip the tag of a tagged record, the interface tags, as well
3681          --  as all items that are not user components (anonymous types,
3682          --  rep clauses, Parent field, controller field).
3683
3684          if Nkind (Comp_Item) = N_Component_Declaration then
3685             declare
3686                Comp : constant Entity_Id := Defining_Identifier (Comp_Item);
3687             begin
3688                if not Is_Tag (Comp)
3689                  and then Chars (Comp) /= Name_uParent
3690                  and then Chars (Comp) /= Name_uController
3691                then
3692                   Append_Elmt (Comp, Into);
3693                end if;
3694             end;
3695          end if;
3696
3697          Next (Comp_Item);
3698       end loop;
3699
3700       if No (Variant_Part (Comp_List)) then
3701          return;
3702       else
3703          Discrim_Name := Name (Variant_Part (Comp_List));
3704          Variant := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
3705       end if;
3706
3707       --  Look for the discriminant that governs this variant part.
3708       --  The discriminant *must* be in the Governed_By List
3709
3710       Assoc := First (Governed_By);
3711       Find_Constraint : loop
3712          Discrim := First (Choices (Assoc));
3713          exit Find_Constraint when Chars (Discrim_Name) = Chars (Discrim)
3714            or else (Present (Corresponding_Discriminant (Entity (Discrim)))
3715                       and then
3716                     Chars (Corresponding_Discriminant (Entity (Discrim)))
3717                          = Chars  (Discrim_Name))
3718            or else Chars (Original_Record_Component (Entity (Discrim)))
3719                          = Chars (Discrim_Name);
3720
3721          if No (Next (Assoc)) then
3722             if not Is_Constrained (Typ)
3723               and then Is_Derived_Type (Typ)
3724               and then Present (Stored_Constraint (Typ))
3725             then
3726                --  If the type is a tagged type with inherited discriminants,
3727                --  use the stored constraint on the parent in order to find
3728                --  the values of discriminants that are otherwise hidden by an
3729                --  explicit constraint. Renamed discriminants are handled in
3730                --  the code above.
3731
3732                --  If several parent discriminants are renamed by a single
3733                --  discriminant of the derived type, the call to obtain the
3734                --  Corresponding_Discriminant field only retrieves the last
3735                --  of them. We recover the constraint on the others from the
3736                --  Stored_Constraint as well.
3737
3738                declare
3739                   D : Entity_Id;
3740                   C : Elmt_Id;
3741
3742                begin
3743                   D := First_Discriminant (Etype (Typ));
3744                   C := First_Elmt (Stored_Constraint (Typ));
3745                   while Present (D) and then Present (C) loop
3746                      if Chars (Discrim_Name) = Chars (D) then
3747                         if Is_Entity_Name (Node (C))
3748                           and then Entity (Node (C)) = Entity (Discrim)
3749                         then
3750                            --  D is renamed by Discrim, whose value is given in
3751                            --  Assoc.
3752
3753                            null;
3754
3755                         else
3756                            Assoc :=
3757                              Make_Component_Association (Sloc (Typ),
3758                                New_List
3759                                  (New_Occurrence_Of (D, Sloc (Typ))),
3760                                   Duplicate_Subexpr_No_Checks (Node (C)));
3761                         end if;
3762                         exit Find_Constraint;
3763                      end if;
3764
3765                      Next_Discriminant (D);
3766                      Next_Elmt (C);
3767                   end loop;
3768                end;
3769             end if;
3770          end if;
3771
3772          if No (Next (Assoc)) then
3773             Error_Msg_NE (" missing value for discriminant&",
3774               First (Governed_By), Discrim_Name);
3775             Report_Errors := True;
3776             return;
3777          end if;
3778
3779          Next (Assoc);
3780       end loop Find_Constraint;
3781
3782       Discrim_Value := Expression (Assoc);
3783
3784       if not Is_OK_Static_Expression (Discrim_Value) then
3785          Error_Msg_FE
3786            ("value for discriminant & must be static!",
3787             Discrim_Value, Discrim);
3788          Why_Not_Static (Discrim_Value);
3789          Report_Errors := True;
3790          return;
3791       end if;
3792
3793       Search_For_Discriminant_Value : declare
3794          Low  : Node_Id;
3795          High : Node_Id;
3796
3797          UI_High          : Uint;
3798          UI_Low           : Uint;
3799          UI_Discrim_Value : constant Uint := Expr_Value (Discrim_Value);
3800
3801       begin
3802          Find_Discrete_Value : while Present (Variant) loop
3803             Discrete_Choice := First (Discrete_Choices (Variant));
3804             while Present (Discrete_Choice) loop
3805
3806                exit Find_Discrete_Value when
3807                  Nkind (Discrete_Choice) = N_Others_Choice;
3808
3809                Get_Index_Bounds (Discrete_Choice, Low, High);
3810
3811                UI_Low  := Expr_Value (Low);
3812                UI_High := Expr_Value (High);
3813
3814                exit Find_Discrete_Value when
3815                  UI_Low <= UI_Discrim_Value
3816                    and then
3817                  UI_High >= UI_Discrim_Value;
3818
3819                Next (Discrete_Choice);
3820             end loop;
3821
3822             Next_Non_Pragma (Variant);
3823          end loop Find_Discrete_Value;
3824       end Search_For_Discriminant_Value;
3825
3826       if No (Variant) then
3827          Error_Msg_NE
3828            ("value of discriminant & is out of range", Discrim_Value, Discrim);
3829          Report_Errors := True;
3830          return;
3831       end  if;
3832
3833       --  If we have found the corresponding choice, recursively add its
3834       --  components to the Into list.
3835
3836       Gather_Components (Empty,
3837         Component_List (Variant), Governed_By, Into, Report_Errors);
3838    end Gather_Components;
3839
3840    ------------------------
3841    -- Get_Actual_Subtype --
3842    ------------------------
3843
3844    function Get_Actual_Subtype (N : Node_Id) return Entity_Id is
3845       Typ  : constant Entity_Id := Etype (N);
3846       Utyp : Entity_Id := Underlying_Type (Typ);
3847       Decl : Node_Id;
3848       Atyp : Entity_Id;
3849
3850    begin
3851       if No (Utyp) then
3852          Utyp := Typ;
3853       end if;
3854
3855       --  If what we have is an identifier that references a subprogram
3856       --  formal, or a variable or constant object, then we get the actual
3857       --  subtype from the referenced entity if one has been built.
3858
3859       if Nkind (N) = N_Identifier
3860         and then
3861           (Is_Formal (Entity (N))
3862             or else Ekind (Entity (N)) = E_Constant
3863             or else Ekind (Entity (N)) = E_Variable)
3864         and then Present (Actual_Subtype (Entity (N)))
3865       then
3866          return Actual_Subtype (Entity (N));
3867
3868       --  Actual subtype of unchecked union is always itself. We never need
3869       --  the "real" actual subtype. If we did, we couldn't get it anyway
3870       --  because the discriminant is not available. The restrictions on
3871       --  Unchecked_Union are designed to make sure that this is OK.
3872
3873       elsif Is_Unchecked_Union (Base_Type (Utyp)) then
3874          return Typ;
3875
3876       --  Here for the unconstrained case, we must find actual subtype
3877       --  No actual subtype is available, so we must build it on the fly.
3878
3879       --  Checking the type, not the underlying type, for constrainedness
3880       --  seems to be necessary. Maybe all the tests should be on the type???
3881
3882       elsif (not Is_Constrained (Typ))
3883            and then (Is_Array_Type (Utyp)
3884                       or else (Is_Record_Type (Utyp)
3885                                 and then Has_Discriminants (Utyp)))
3886            and then not Has_Unknown_Discriminants (Utyp)
3887            and then not (Ekind (Utyp) = E_String_Literal_Subtype)
3888       then
3889          --  Nothing to do if in spec expression (why not???)
3890
3891          if In_Spec_Expression then
3892             return Typ;
3893
3894          elsif Is_Private_Type (Typ)
3895            and then not Has_Discriminants (Typ)
3896          then
3897             --  If the type has no discriminants, there is no subtype to
3898             --  build, even if the underlying type is discriminated.
3899
3900             return Typ;
3901
3902          --  Else build the actual subtype
3903
3904          else
3905             Decl := Build_Actual_Subtype (Typ, N);
3906             Atyp := Defining_Identifier (Decl);
3907
3908             --  If Build_Actual_Subtype generated a new declaration then use it
3909
3910             if Atyp /= Typ then
3911
3912                --  The actual subtype is an Itype, so analyze the declaration,
3913                --  but do not attach it to the tree, to get the type defined.
3914
3915                Set_Parent (Decl, N);
3916                Set_Is_Itype (Atyp);
3917                Analyze (Decl, Suppress => All_Checks);
3918                Set_Associated_Node_For_Itype (Atyp, N);
3919                Set_Has_Delayed_Freeze (Atyp, False);
3920
3921                --  We need to freeze the actual subtype immediately. This is
3922                --  needed, because otherwise this Itype will not get frozen
3923                --  at all, and it is always safe to freeze on creation because
3924                --  any associated types must be frozen at this point.
3925
3926                Freeze_Itype (Atyp, N);
3927                return Atyp;
3928
3929             --  Otherwise we did not build a declaration, so return original
3930
3931             else
3932                return Typ;
3933             end if;
3934          end if;
3935
3936       --  For all remaining cases, the actual subtype is the same as
3937       --  the nominal type.
3938
3939       else
3940          return Typ;
3941       end if;
3942    end Get_Actual_Subtype;
3943
3944    -------------------------------------
3945    -- Get_Actual_Subtype_If_Available --
3946    -------------------------------------
3947
3948    function Get_Actual_Subtype_If_Available (N : Node_Id) return Entity_Id is
3949       Typ  : constant Entity_Id := Etype (N);
3950
3951    begin
3952       --  If what we have is an identifier that references a subprogram
3953       --  formal, or a variable or constant object, then we get the actual
3954       --  subtype from the referenced entity if one has been built.
3955
3956       if Nkind (N) = N_Identifier
3957         and then
3958           (Is_Formal (Entity (N))
3959             or else Ekind (Entity (N)) = E_Constant
3960             or else Ekind (Entity (N)) = E_Variable)
3961         and then Present (Actual_Subtype (Entity (N)))
3962       then
3963          return Actual_Subtype (Entity (N));
3964
3965       --  Otherwise the Etype of N is returned unchanged
3966
3967       else
3968          return Typ;
3969       end if;
3970    end Get_Actual_Subtype_If_Available;
3971
3972    -------------------------------
3973    -- Get_Default_External_Name --
3974    -------------------------------
3975
3976    function Get_Default_External_Name (E : Node_Or_Entity_Id) return Node_Id is
3977    begin
3978       Get_Decoded_Name_String (Chars (E));
3979
3980       if Opt.External_Name_Imp_Casing = Uppercase then
3981          Set_Casing (All_Upper_Case);
3982       else
3983          Set_Casing (All_Lower_Case);
3984       end if;
3985
3986       return
3987         Make_String_Literal (Sloc (E),
3988           Strval => String_From_Name_Buffer);
3989    end Get_Default_External_Name;
3990
3991    ---------------------------
3992    -- Get_Enum_Lit_From_Pos --
3993    ---------------------------
3994
3995    function Get_Enum_Lit_From_Pos
3996      (T   : Entity_Id;
3997       Pos : Uint;
3998       Loc : Source_Ptr) return Node_Id
3999    is
4000       Lit : Node_Id;
4001
4002    begin
4003       --  In the case where the literal is of type Character, Wide_Character
4004       --  or Wide_Wide_Character or of a type derived from them, there needs
4005       --  to be some special handling since there is no explicit chain of
4006       --  literals to search. Instead, an N_Character_Literal node is created
4007       --  with the appropriate Char_Code and Chars fields.
4008
4009       if Is_Standard_Character_Type (T) then
4010          Set_Character_Literal_Name (UI_To_CC (Pos));
4011          return
4012            Make_Character_Literal (Loc,
4013              Chars              => Name_Find,
4014              Char_Literal_Value => Pos);
4015
4016       --  For all other cases, we have a complete table of literals, and
4017       --  we simply iterate through the chain of literal until the one
4018       --  with the desired position value is found.
4019       --
4020
4021       else
4022          Lit := First_Literal (Base_Type (T));
4023          for J in 1 .. UI_To_Int (Pos) loop
4024             Next_Literal (Lit);
4025          end loop;
4026
4027          return New_Occurrence_Of (Lit, Loc);
4028       end if;
4029    end Get_Enum_Lit_From_Pos;
4030
4031    ------------------------
4032    -- Get_Generic_Entity --
4033    ------------------------
4034
4035    function Get_Generic_Entity (N : Node_Id) return Entity_Id is
4036       Ent : constant Entity_Id := Entity (Name (N));
4037    begin
4038       if Present (Renamed_Object (Ent)) then
4039          return Renamed_Object (Ent);
4040       else
4041          return Ent;
4042       end if;
4043    end Get_Generic_Entity;
4044
4045    ----------------------
4046    -- Get_Index_Bounds --
4047    ----------------------
4048
4049    procedure Get_Index_Bounds (N : Node_Id; L, H : out Node_Id) is
4050       Kind : constant Node_Kind := Nkind (N);
4051       R    : Node_Id;
4052
4053    begin
4054       if Kind = N_Range then
4055          L := Low_Bound (N);
4056          H := High_Bound (N);
4057
4058       elsif Kind = N_Subtype_Indication then
4059          R := Range_Expression (Constraint (N));
4060
4061          if R = Error then
4062             L := Error;
4063             H := Error;
4064             return;
4065
4066          else
4067             L := Low_Bound  (Range_Expression (Constraint (N)));
4068             H := High_Bound (Range_Expression (Constraint (N)));
4069          end if;
4070
4071       elsif Is_Entity_Name (N) and then Is_Type (Entity (N)) then
4072          if Error_Posted (Scalar_Range (Entity (N))) then
4073             L := Error;
4074             H := Error;
4075
4076          elsif Nkind (Scalar_Range (Entity (N))) = N_Subtype_Indication then
4077             Get_Index_Bounds (Scalar_Range (Entity (N)), L, H);
4078
4079          else
4080             L := Low_Bound  (Scalar_Range (Entity (N)));
4081             H := High_Bound (Scalar_Range (Entity (N)));
4082          end if;
4083
4084       else
4085          --  N is an expression, indicating a range with one value
4086
4087          L := N;
4088          H := N;
4089       end if;
4090    end Get_Index_Bounds;
4091
4092    ----------------------------------
4093    -- Get_Library_Unit_Name_string --
4094    ----------------------------------
4095
4096    procedure Get_Library_Unit_Name_String (Decl_Node : Node_Id) is
4097       Unit_Name_Id : constant Unit_Name_Type := Get_Unit_Name (Decl_Node);
4098
4099    begin
4100       Get_Unit_Name_String (Unit_Name_Id);
4101
4102       --  Remove seven last character (" (spec)" or " (body)")
4103
4104       Name_Len := Name_Len - 7;
4105       pragma Assert (Name_Buffer (Name_Len + 1) = ' ');
4106    end Get_Library_Unit_Name_String;
4107
4108    ------------------------
4109    -- Get_Name_Entity_Id --
4110    ------------------------
4111
4112    function Get_Name_Entity_Id (Id : Name_Id) return Entity_Id is
4113    begin
4114       return Entity_Id (Get_Name_Table_Info (Id));
4115    end Get_Name_Entity_Id;
4116
4117    -------------------
4118    -- Get_Pragma_Id --
4119    -------------------
4120
4121    function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
4122    begin
4123       return Get_Pragma_Id (Pragma_Name (N));
4124    end Get_Pragma_Id;
4125
4126    ---------------------------
4127    -- Get_Referenced_Object --
4128    ---------------------------
4129
4130    function Get_Referenced_Object (N : Node_Id) return Node_Id is
4131       R : Node_Id;
4132
4133    begin
4134       R := N;
4135       while Is_Entity_Name (R)
4136         and then Present (Renamed_Object (Entity (R)))
4137       loop
4138          R := Renamed_Object (Entity (R));
4139       end loop;
4140
4141       return R;
4142    end Get_Referenced_Object;
4143
4144    ------------------------
4145    -- Get_Renamed_Entity --
4146    ------------------------
4147
4148    function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
4149       R : Entity_Id;
4150
4151    begin
4152       R := E;
4153       while Present (Renamed_Entity (R)) loop
4154          R := Renamed_Entity (R);
4155       end loop;
4156
4157       return R;
4158    end Get_Renamed_Entity;
4159
4160    -------------------------
4161    -- Get_Subprogram_Body --
4162    -------------------------
4163
4164    function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
4165       Decl : Node_Id;
4166
4167    begin
4168       Decl := Unit_Declaration_Node (E);
4169
4170       if Nkind (Decl) = N_Subprogram_Body then
4171          return Decl;
4172
4173       --  The below comment is bad, because it is possible for
4174       --  Nkind (Decl) to be an N_Subprogram_Body_Stub ???
4175
4176       else           --  Nkind (Decl) = N_Subprogram_Declaration
4177
4178          if Present (Corresponding_Body (Decl)) then
4179             return Unit_Declaration_Node (Corresponding_Body (Decl));
4180
4181          --  Imported subprogram case
4182
4183          else
4184             return Empty;
4185          end if;
4186       end if;
4187    end Get_Subprogram_Body;
4188
4189    ---------------------------
4190    -- Get_Subprogram_Entity --
4191    ---------------------------
4192
4193    function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
4194       Nam  : Node_Id;
4195       Proc : Entity_Id;
4196
4197    begin
4198       if Nkind (Nod) = N_Accept_Statement then
4199          Nam := Entry_Direct_Name (Nod);
4200
4201       --  For an entry call, the prefix of the call is a selected component.
4202       --  Need additional code for internal calls ???
4203
4204       elsif Nkind (Nod) = N_Entry_Call_Statement then
4205          if Nkind (Name (Nod)) = N_Selected_Component then
4206             Nam := Entity (Selector_Name (Name (Nod)));
4207          else
4208             Nam := Empty;
4209          end if;
4210
4211       else
4212          Nam := Name (Nod);
4213       end if;
4214
4215       if Nkind (Nam) = N_Explicit_Dereference then
4216          Proc := Etype (Prefix (Nam));
4217       elsif Is_Entity_Name (Nam) then
4218          Proc := Entity (Nam);
4219       else
4220          return Empty;
4221       end if;
4222
4223       if Is_Object (Proc) then
4224          Proc := Etype (Proc);
4225       end if;
4226
4227       if Ekind (Proc) = E_Access_Subprogram_Type then
4228          Proc := Directly_Designated_Type (Proc);
4229       end if;
4230
4231       if not Is_Subprogram (Proc)
4232         and then Ekind (Proc) /= E_Subprogram_Type
4233       then
4234          return Empty;
4235       else
4236          return Proc;
4237       end if;
4238    end Get_Subprogram_Entity;
4239
4240    -----------------------------
4241    -- Get_Task_Body_Procedure --
4242    -----------------------------
4243
4244    function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
4245    begin
4246       --  Note: A task type may be the completion of a private type with
4247       --  discriminants. When performing elaboration checks on a task
4248       --  declaration, the current view of the type may be the private one,
4249       --  and the procedure that holds the body of the task is held in its
4250       --  underlying type.
4251
4252       --  This is an odd function, why not have Task_Body_Procedure do
4253       --  the following digging???
4254
4255       return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
4256    end Get_Task_Body_Procedure;
4257
4258    -----------------------
4259    -- Has_Access_Values --
4260    -----------------------
4261
4262    function Has_Access_Values (T : Entity_Id) return Boolean is
4263       Typ : constant Entity_Id := Underlying_Type (T);
4264
4265    begin
4266       --  Case of a private type which is not completed yet. This can only
4267       --  happen in the case of a generic format type appearing directly, or
4268       --  as a component of the type to which this function is being applied
4269       --  at the top level. Return False in this case, since we certainly do
4270       --  not know that the type contains access types.
4271
4272       if No (Typ) then
4273          return False;
4274
4275       elsif Is_Access_Type (Typ) then
4276          return True;
4277
4278       elsif Is_Array_Type (Typ) then
4279          return Has_Access_Values (Component_Type (Typ));
4280
4281       elsif Is_Record_Type (Typ) then
4282          declare
4283             Comp : Entity_Id;
4284
4285          begin
4286             --  Loop to Check components
4287
4288             Comp := First_Component_Or_Discriminant (Typ);
4289             while Present (Comp) loop
4290
4291                --  Check for access component, tag field does not count, even
4292                --  though it is implemented internally using an access type.
4293
4294                if Has_Access_Values (Etype (Comp))
4295                  and then Chars (Comp) /= Name_uTag
4296                then
4297                   return True;
4298                end if;
4299
4300                Next_Component_Or_Discriminant (Comp);
4301             end loop;
4302          end;
4303
4304          return False;
4305
4306       else
4307          return False;
4308       end if;
4309    end Has_Access_Values;
4310
4311    ------------------------------
4312    -- Has_Compatible_Alignment --
4313    ------------------------------
4314
4315    function Has_Compatible_Alignment
4316      (Obj  : Entity_Id;
4317       Expr : Node_Id) return Alignment_Result
4318    is
4319       function Has_Compatible_Alignment_Internal
4320         (Obj     : Entity_Id;
4321          Expr    : Node_Id;
4322          Default : Alignment_Result) return Alignment_Result;
4323       --  This is the internal recursive function that actually does the work.
4324       --  There is one additional parameter, which says what the result should
4325       --  be if no alignment information is found, and there is no definite
4326       --  indication of compatible alignments. At the outer level, this is set
4327       --  to Unknown, but for internal recursive calls in the case where types
4328       --  are known to be correct, it is set to Known_Compatible.
4329
4330       ---------------------------------------
4331       -- Has_Compatible_Alignment_Internal --
4332       ---------------------------------------
4333
4334       function Has_Compatible_Alignment_Internal
4335         (Obj     : Entity_Id;
4336          Expr    : Node_Id;
4337          Default : Alignment_Result) return Alignment_Result
4338       is
4339          Result : Alignment_Result := Known_Compatible;
4340          --  Holds the current status of the result. Note that once a value of
4341          --  Known_Incompatible is set, it is sticky and does not get changed
4342          --  to Unknown (the value in Result only gets worse as we go along,
4343          --  never better).
4344
4345          Offs : Uint := No_Uint;
4346          --  Set to a factor of the offset from the base object when Expr is a
4347          --  selected or indexed component, based on Component_Bit_Offset and
4348          --  Component_Size respectively. A negative value is used to represent
4349          --  a value which is not known at compile time.
4350
4351          procedure Check_Prefix;
4352          --  Checks the prefix recursively in the case where the expression
4353          --  is an indexed or selected component.
4354
4355          procedure Set_Result (R : Alignment_Result);
4356          --  If R represents a worse outcome (unknown instead of known
4357          --  compatible, or known incompatible), then set Result to R.
4358
4359          ------------------
4360          -- Check_Prefix --
4361          ------------------
4362
4363          procedure Check_Prefix is
4364          begin
4365             --  The subtlety here is that in doing a recursive call to check
4366             --  the prefix, we have to decide what to do in the case where we
4367             --  don't find any specific indication of an alignment problem.
4368
4369             --  At the outer level, we normally set Unknown as the result in
4370             --  this case, since we can only set Known_Compatible if we really
4371             --  know that the alignment value is OK, but for the recursive
4372             --  call, in the case where the types match, and we have not
4373             --  specified a peculiar alignment for the object, we are only
4374             --  concerned about suspicious rep clauses, the default case does
4375             --  not affect us, since the compiler will, in the absence of such
4376             --  rep clauses, ensure that the alignment is correct.
4377
4378             if Default = Known_Compatible
4379               or else
4380                 (Etype (Obj) = Etype (Expr)
4381                   and then (Unknown_Alignment (Obj)
4382                              or else
4383                                Alignment (Obj) = Alignment (Etype (Obj))))
4384             then
4385                Set_Result
4386                  (Has_Compatible_Alignment_Internal
4387                     (Obj, Prefix (Expr), Known_Compatible));
4388
4389             --  In all other cases, we need a full check on the prefix
4390
4391             else
4392                Set_Result
4393                  (Has_Compatible_Alignment_Internal
4394                     (Obj, Prefix (Expr), Unknown));
4395             end if;
4396          end Check_Prefix;
4397
4398          ----------------
4399          -- Set_Result --
4400          ----------------
4401
4402          procedure Set_Result (R : Alignment_Result) is
4403          begin
4404             if R > Result then
4405                Result := R;
4406             end if;
4407          end Set_Result;
4408
4409       --  Start of processing for Has_Compatible_Alignment_Internal
4410
4411       begin
4412          --  If Expr is a selected component, we must make sure there is no
4413          --  potentially troublesome component clause, and that the record is
4414          --  not packed.
4415
4416          if Nkind (Expr) = N_Selected_Component then
4417
4418             --  Packed record always generate unknown alignment
4419
4420             if Is_Packed (Etype (Prefix (Expr))) then
4421                Set_Result (Unknown);
4422             end if;
4423
4424             --  Check prefix and component offset
4425
4426             Check_Prefix;
4427             Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
4428
4429          --  If Expr is an indexed component, we must make sure there is no
4430          --  potentially troublesome Component_Size clause and that the array
4431          --  is not bit-packed.
4432
4433          elsif Nkind (Expr) = N_Indexed_Component then
4434             declare
4435                Typ : constant Entity_Id := Etype (Prefix (Expr));
4436                Ind : constant Node_Id   := First_Index (Typ);
4437
4438             begin
4439                --  Bit packed array always generates unknown alignment
4440
4441                if Is_Bit_Packed_Array (Typ) then
4442                   Set_Result (Unknown);
4443                end if;
4444
4445                --  Check prefix and component offset
4446
4447                Check_Prefix;
4448                Offs := Component_Size (Typ);
4449
4450                --  Small optimization: compute the full offset when possible
4451
4452                if Offs /= No_Uint
4453                  and then Offs > Uint_0
4454                  and then Present (Ind)
4455                  and then Nkind (Ind) = N_Range
4456                  and then Compile_Time_Known_Value (Low_Bound (Ind))
4457                  and then Compile_Time_Known_Value (First (Expressions (Expr)))
4458                then
4459                   Offs := Offs * (Expr_Value (First (Expressions (Expr)))
4460                                     - Expr_Value (Low_Bound ((Ind))));
4461                end if;
4462             end;
4463          end if;
4464
4465          --  If we have a null offset, the result is entirely determined by
4466          --  the base object and has already been computed recursively.
4467
4468          if Offs = Uint_0 then
4469             null;
4470
4471          --  Case where we know the alignment of the object
4472
4473          elsif Known_Alignment (Obj) then
4474             declare
4475                ObjA : constant Uint := Alignment (Obj);
4476                ExpA : Uint          := No_Uint;
4477                SizA : Uint          := No_Uint;
4478
4479             begin
4480                --  If alignment of Obj is 1, then we are always OK
4481
4482                if ObjA = 1 then
4483                   Set_Result (Known_Compatible);
4484
4485                --  Alignment of Obj is greater than 1, so we need to check
4486
4487                else
4488                   --  If we have an offset, see if it is compatible
4489
4490                   if Offs /= No_Uint and Offs > Uint_0 then
4491                      if Offs mod (System_Storage_Unit * ObjA) /= 0 then
4492                         Set_Result (Known_Incompatible);
4493                      end if;
4494
4495                      --  See if Expr is an object with known alignment
4496
4497                   elsif Is_Entity_Name (Expr)
4498                     and then Known_Alignment (Entity (Expr))
4499                   then
4500                      ExpA := Alignment (Entity (Expr));
4501
4502                      --  Otherwise, we can use the alignment of the type of
4503                      --  Expr given that we already checked for
4504                      --  discombobulating rep clauses for the cases of indexed
4505                      --  and selected components above.
4506
4507                   elsif Known_Alignment (Etype (Expr)) then
4508                      ExpA := Alignment (Etype (Expr));
4509
4510                      --  Otherwise the alignment is unknown
4511
4512                   else
4513                      Set_Result (Default);
4514                   end if;
4515
4516                   --  If we got an alignment, see if it is acceptable
4517
4518                   if ExpA /= No_Uint and then ExpA < ObjA then
4519                      Set_Result (Known_Incompatible);
4520                   end if;
4521
4522                   --  If Expr is not a piece of a larger object, see if size
4523                   --  is given. If so, check that it is not too small for the
4524                   --  required alignment.
4525
4526                   if Offs /= No_Uint then
4527                      null;
4528
4529                      --  See if Expr is an object with known size
4530
4531                   elsif Is_Entity_Name (Expr)
4532                     and then Known_Static_Esize (Entity (Expr))
4533                   then
4534                      SizA := Esize (Entity (Expr));
4535
4536                      --  Otherwise, we check the object size of the Expr type
4537
4538                   elsif Known_Static_Esize (Etype (Expr)) then
4539                      SizA := Esize (Etype (Expr));
4540                   end if;
4541
4542                   --  If we got a size, see if it is a multiple of the Obj
4543                   --  alignment, if not, then the alignment cannot be
4544                   --  acceptable, since the size is always a multiple of the
4545                   --  alignment.
4546
4547                   if SizA /= No_Uint then
4548                      if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
4549                         Set_Result (Known_Incompatible);
4550                      end if;
4551                   end if;
4552                end if;
4553             end;
4554
4555          --  If we do not know required alignment, any non-zero offset is a
4556          --  potential problem (but certainly may be OK, so result is unknown).
4557
4558          elsif Offs /= No_Uint then
4559             Set_Result (Unknown);
4560
4561          --  If we can't find the result by direct comparison of alignment
4562          --  values, then there is still one case that we can determine known
4563          --  result, and that is when we can determine that the types are the
4564          --  same, and no alignments are specified. Then we known that the
4565          --  alignments are compatible, even if we don't know the alignment
4566          --  value in the front end.
4567
4568          elsif Etype (Obj) = Etype (Expr) then
4569
4570             --  Types are the same, but we have to check for possible size
4571             --  and alignments on the Expr object that may make the alignment
4572             --  different, even though the types are the same.
4573
4574             if Is_Entity_Name (Expr) then
4575
4576                --  First check alignment of the Expr object. Any alignment less
4577                --  than Maximum_Alignment is worrisome since this is the case
4578                --  where we do not know the alignment of Obj.
4579
4580                if Known_Alignment (Entity (Expr))
4581                  and then
4582                    UI_To_Int (Alignment (Entity (Expr))) <
4583                                                     Ttypes.Maximum_Alignment
4584                then
4585                   Set_Result (Unknown);
4586
4587                   --  Now check size of Expr object. Any size that is not an
4588                   --  even multiple of Maximum_Alignment is also worrisome
4589                   --  since it may cause the alignment of the object to be less
4590                   --  than the alignment of the type.
4591
4592                elsif Known_Static_Esize (Entity (Expr))
4593                  and then
4594                    (UI_To_Int (Esize (Entity (Expr))) mod
4595                      (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
4596                                                                         /= 0
4597                then
4598                   Set_Result (Unknown);
4599
4600                   --  Otherwise same type is decisive
4601
4602                else
4603                   Set_Result (Known_Compatible);
4604                end if;
4605             end if;
4606
4607          --  Another case to deal with is when there is an explicit size or
4608          --  alignment clause when the types are not the same. If so, then the
4609          --  result is Unknown. We don't need to do this test if the Default is
4610          --  Unknown, since that result will be set in any case.
4611
4612          elsif Default /= Unknown
4613            and then (Has_Size_Clause      (Etype (Expr))
4614                       or else
4615                      Has_Alignment_Clause (Etype (Expr)))
4616          then
4617             Set_Result (Unknown);
4618
4619          --  If no indication found, set default
4620
4621          else
4622             Set_Result (Default);
4623          end if;
4624
4625          --  Return worst result found
4626
4627          return Result;
4628       end Has_Compatible_Alignment_Internal;
4629
4630    --  Start of processing for Has_Compatible_Alignment
4631
4632    begin
4633       --  If Obj has no specified alignment, then set alignment from the type
4634       --  alignment. Perhaps we should always do this, but for sure we should
4635       --  do it when there is an address clause since we can do more if the
4636       --  alignment is known.
4637
4638       if Unknown_Alignment (Obj) then
4639          Set_Alignment (Obj, Alignment (Etype (Obj)));
4640       end if;
4641
4642       --  Now do the internal call that does all the work
4643
4644       return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
4645    end Has_Compatible_Alignment;
4646
4647    ----------------------
4648    -- Has_Declarations --
4649    ----------------------
4650
4651    function Has_Declarations (N : Node_Id) return Boolean is
4652    begin
4653       return Nkind_In (Nkind (N), N_Accept_Statement,
4654                                   N_Block_Statement,
4655                                   N_Compilation_Unit_Aux,
4656                                   N_Entry_Body,
4657                                   N_Package_Body,
4658                                   N_Protected_Body,
4659                                   N_Subprogram_Body,
4660                                   N_Task_Body,
4661                                   N_Package_Specification);
4662    end Has_Declarations;
4663
4664    -------------------------------------------
4665    -- Has_Discriminant_Dependent_Constraint --
4666    -------------------------------------------
4667
4668    function Has_Discriminant_Dependent_Constraint
4669      (Comp : Entity_Id) return Boolean
4670    is
4671       Comp_Decl  : constant Node_Id := Parent (Comp);
4672       Subt_Indic : constant Node_Id :=
4673                      Subtype_Indication (Component_Definition (Comp_Decl));
4674       Constr     : Node_Id;
4675       Assn       : Node_Id;
4676
4677    begin
4678       if Nkind (Subt_Indic) = N_Subtype_Indication then
4679          Constr := Constraint (Subt_Indic);
4680
4681          if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
4682             Assn := First (Constraints (Constr));
4683             while Present (Assn) loop
4684                case Nkind (Assn) is
4685                   when N_Subtype_Indication |
4686                        N_Range              |
4687                        N_Identifier
4688                   =>
4689                      if Depends_On_Discriminant (Assn) then
4690                         return True;
4691                      end if;
4692
4693                   when N_Discriminant_Association =>
4694                      if Depends_On_Discriminant (Expression (Assn)) then
4695                         return True;
4696                      end if;
4697
4698                   when others =>
4699                      null;
4700
4701                end case;
4702
4703                Next (Assn);
4704             end loop;
4705          end if;
4706       end if;
4707
4708       return False;
4709    end Has_Discriminant_Dependent_Constraint;
4710
4711    --------------------
4712    -- Has_Infinities --
4713    --------------------
4714
4715    function Has_Infinities (E : Entity_Id) return Boolean is
4716    begin
4717       return
4718         Is_Floating_Point_Type (E)
4719           and then Nkind (Scalar_Range (E)) = N_Range
4720           and then Includes_Infinities (Scalar_Range (E));
4721    end Has_Infinities;
4722
4723    --------------------
4724    -- Has_Interfaces --
4725    --------------------
4726
4727    function Has_Interfaces
4728      (T             : Entity_Id;
4729       Use_Full_View : Boolean := True) return Boolean
4730    is
4731       Typ : Entity_Id := Base_Type (T);
4732
4733    begin
4734       --  Handle concurrent types
4735
4736       if Is_Concurrent_Type (Typ) then
4737          Typ := Corresponding_Record_Type (Typ);
4738       end if;
4739
4740       if not Present (Typ)
4741         or else not Is_Record_Type (Typ)
4742         or else not Is_Tagged_Type (Typ)
4743       then
4744          return False;
4745       end if;
4746
4747       --  Handle private types
4748
4749       if Use_Full_View
4750         and then Present (Full_View (Typ))
4751       then
4752          Typ := Full_View (Typ);
4753       end if;
4754
4755       --  Handle concurrent record types
4756
4757       if Is_Concurrent_Record_Type (Typ)
4758         and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
4759       then
4760          return True;
4761       end if;
4762
4763       loop
4764          if Is_Interface (Typ)
4765            or else
4766              (Is_Record_Type (Typ)
4767                and then Present (Interfaces (Typ))
4768                and then not Is_Empty_Elmt_List (Interfaces (Typ)))
4769          then
4770             return True;
4771          end if;
4772
4773          exit when Etype (Typ) = Typ
4774
4775             --  Handle private types
4776
4777             or else (Present (Full_View (Etype (Typ)))
4778                        and then Full_View (Etype (Typ)) = Typ)
4779
4780             --  Protect the frontend against wrong source with cyclic
4781             --  derivations
4782
4783             or else Etype (Typ) = T;
4784
4785          --  Climb to the ancestor type handling private types
4786
4787          if Present (Full_View (Etype (Typ))) then
4788             Typ := Full_View (Etype (Typ));
4789          else
4790             Typ := Etype (Typ);
4791          end if;
4792       end loop;
4793
4794       return False;
4795    end Has_Interfaces;
4796
4797    ------------------------
4798    -- Has_Null_Exclusion --
4799    ------------------------
4800
4801    function Has_Null_Exclusion (N : Node_Id) return Boolean is
4802    begin
4803       case Nkind (N) is
4804          when N_Access_Definition               |
4805               N_Access_Function_Definition      |
4806               N_Access_Procedure_Definition     |
4807               N_Access_To_Object_Definition     |
4808               N_Allocator                       |
4809               N_Derived_Type_Definition         |
4810               N_Function_Specification          |
4811               N_Subtype_Declaration             =>
4812             return Null_Exclusion_Present (N);
4813
4814          when N_Component_Definition            |
4815               N_Formal_Object_Declaration       |
4816               N_Object_Renaming_Declaration     =>
4817             if Present (Subtype_Mark (N)) then
4818                return Null_Exclusion_Present (N);
4819             else pragma Assert (Present (Access_Definition (N)));
4820                return Null_Exclusion_Present (Access_Definition (N));
4821             end if;
4822
4823          when N_Discriminant_Specification =>
4824             if Nkind (Discriminant_Type (N)) = N_Access_Definition then
4825                return Null_Exclusion_Present (Discriminant_Type (N));
4826             else
4827                return Null_Exclusion_Present (N);
4828             end if;
4829
4830          when N_Object_Declaration =>
4831             if Nkind (Object_Definition (N)) = N_Access_Definition then
4832                return Null_Exclusion_Present (Object_Definition (N));
4833             else
4834                return Null_Exclusion_Present (N);
4835             end if;
4836
4837          when N_Parameter_Specification =>
4838             if Nkind (Parameter_Type (N)) = N_Access_Definition then
4839                return Null_Exclusion_Present (Parameter_Type (N));
4840             else
4841                return Null_Exclusion_Present (N);
4842             end if;
4843
4844          when others =>
4845             return False;
4846
4847       end case;
4848    end Has_Null_Exclusion;
4849
4850    ------------------------
4851    -- Has_Null_Extension --
4852    ------------------------
4853
4854    function Has_Null_Extension (T : Entity_Id) return Boolean is
4855       B     : constant Entity_Id := Base_Type (T);
4856       Comps : Node_Id;
4857       Ext   : Node_Id;
4858
4859    begin
4860       if Nkind (Parent (B)) = N_Full_Type_Declaration
4861         and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
4862       then
4863          Ext := Record_Extension_Part (Type_Definition (Parent (B)));
4864
4865          if Present (Ext) then
4866             if Null_Present (Ext) then
4867                return True;
4868             else
4869                Comps := Component_List (Ext);
4870
4871                --  The null component list is rewritten during analysis to
4872                --  include the parent component. Any other component indicates
4873                --  that the extension was not originally null.
4874
4875                return Null_Present (Comps)
4876                  or else No (Next (First (Component_Items (Comps))));
4877             end if;
4878          else
4879             return False;
4880          end if;
4881
4882       else
4883          return False;
4884       end if;
4885    end Has_Null_Extension;
4886
4887    -------------------------------
4888    -- Has_Overriding_Initialize --
4889    -------------------------------
4890
4891    function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
4892       BT   : constant Entity_Id := Base_Type (T);
4893       Comp : Entity_Id;
4894       P    : Elmt_Id;
4895
4896    begin
4897       if Is_Controlled (BT) then
4898
4899          --  For derived types, check immediate ancestor, excluding
4900          --  Controlled itself.
4901
4902          if Is_Derived_Type (BT)
4903            and then not In_Predefined_Unit (Etype (BT))
4904            and then Has_Overriding_Initialize (Etype (BT))
4905          then
4906             return True;
4907
4908          elsif Present (Primitive_Operations (BT)) then
4909             P := First_Elmt (Primitive_Operations (BT));
4910             while Present (P) loop
4911                if Chars (Node (P)) = Name_Initialize
4912                  and then Comes_From_Source (Node (P))
4913                then
4914                   return True;
4915                end if;
4916
4917                Next_Elmt (P);
4918             end loop;
4919          end if;
4920
4921          return False;
4922
4923       elsif Has_Controlled_Component (BT) then
4924          Comp := First_Component (BT);
4925          while Present (Comp) loop
4926             if Has_Overriding_Initialize (Etype (Comp)) then
4927                return True;
4928             end if;
4929
4930             Next_Component (Comp);
4931          end loop;
4932
4933          return False;
4934
4935       else
4936          return False;
4937       end if;
4938    end Has_Overriding_Initialize;
4939
4940    --------------------------------------
4941    -- Has_Preelaborable_Initialization --
4942    --------------------------------------
4943
4944    function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
4945       Has_PE : Boolean;
4946
4947       procedure Check_Components (E : Entity_Id);
4948       --  Check component/discriminant chain, sets Has_PE False if a component
4949       --  or discriminant does not meet the preelaborable initialization rules.
4950
4951       ----------------------
4952       -- Check_Components --
4953       ----------------------
4954
4955       procedure Check_Components (E : Entity_Id) is
4956          Ent : Entity_Id;
4957          Exp : Node_Id;
4958
4959          function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
4960          --  Returns True if and only if the expression denoted by N does not
4961          --  violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
4962
4963          ---------------------------------
4964          -- Is_Preelaborable_Expression --
4965          ---------------------------------
4966
4967          function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
4968             Exp           : Node_Id;
4969             Assn          : Node_Id;
4970             Choice        : Node_Id;
4971             Comp_Type     : Entity_Id;
4972             Is_Array_Aggr : Boolean;
4973
4974          begin
4975             if Is_Static_Expression (N) then
4976                return True;
4977
4978             elsif Nkind (N) = N_Null then
4979                return True;
4980
4981             --  Attributes are allowed in general, even if their prefix is a
4982             --  formal type. (It seems that certain attributes known not to be
4983             --  static might not be allowed, but there are no rules to prevent
4984             --  them.)
4985
4986             elsif Nkind (N) = N_Attribute_Reference then
4987                return True;
4988
4989             --  The name of a discriminant evaluated within its parent type is
4990             --  defined to be preelaborable (10.2.1(8)). Note that we test for
4991             --  names that denote discriminals as well as discriminants to
4992             --  catch references occurring within init procs.
4993
4994             elsif Is_Entity_Name (N)
4995               and then
4996                 (Ekind (Entity (N)) = E_Discriminant
4997                   or else
4998                     ((Ekind (Entity (N)) = E_Constant
4999                        or else Ekind (Entity (N)) = E_In_Parameter)
5000                      and then Present (Discriminal_Link (Entity (N)))))
5001             then
5002                return True;
5003
5004             elsif Nkind (N) = N_Qualified_Expression then
5005                return Is_Preelaborable_Expression (Expression (N));
5006
5007             --  For aggregates we have to check that each of the associations
5008             --  is preelaborable.
5009
5010             elsif Nkind (N) = N_Aggregate
5011               or else Nkind (N) = N_Extension_Aggregate
5012             then
5013                Is_Array_Aggr := Is_Array_Type (Etype (N));
5014
5015                if Is_Array_Aggr then
5016                   Comp_Type := Component_Type (Etype (N));
5017                end if;
5018
5019                --  Check the ancestor part of extension aggregates, which must
5020                --  be either the name of a type that has preelaborable init or
5021                --  an expression that is preelaborable.
5022
5023                if Nkind (N) = N_Extension_Aggregate then
5024                   declare
5025                      Anc_Part : constant Node_Id := Ancestor_Part (N);
5026
5027                   begin
5028                      if Is_Entity_Name (Anc_Part)
5029                        and then Is_Type (Entity (Anc_Part))
5030                      then
5031                         if not Has_Preelaborable_Initialization
5032                                  (Entity (Anc_Part))
5033                         then
5034                            return False;
5035                         end if;
5036
5037                      elsif not Is_Preelaborable_Expression (Anc_Part) then
5038                         return False;
5039                      end if;
5040                   end;
5041                end if;
5042
5043                --  Check positional associations
5044
5045                Exp := First (Expressions (N));
5046                while Present (Exp) loop
5047                   if not Is_Preelaborable_Expression (Exp) then
5048                      return False;
5049                   end if;
5050
5051                   Next (Exp);
5052                end loop;
5053
5054                --  Check named associations
5055
5056                Assn := First (Component_Associations (N));
5057                while Present (Assn) loop
5058                   Choice := First (Choices (Assn));
5059                   while Present (Choice) loop
5060                      if Is_Array_Aggr then
5061                         if Nkind (Choice) = N_Others_Choice then
5062                            null;
5063
5064                         elsif Nkind (Choice) = N_Range then
5065                            if not Is_Static_Range (Choice) then
5066                               return False;
5067                            end if;
5068
5069                         elsif not Is_Static_Expression (Choice) then
5070                            return False;
5071                         end if;
5072
5073                      else
5074                         Comp_Type := Etype (Choice);
5075                      end if;
5076
5077                      Next (Choice);
5078                   end loop;
5079
5080                   --  If the association has a <> at this point, then we have
5081                   --  to check whether the component's type has preelaborable
5082                   --  initialization. Note that this only occurs when the
5083                   --  association's corresponding component does not have a
5084                   --  default expression, the latter case having already been
5085                   --  expanded as an expression for the association.
5086
5087                   if Box_Present (Assn) then
5088                      if not Has_Preelaborable_Initialization (Comp_Type) then
5089                         return False;
5090                      end if;
5091
5092                   --  In the expression case we check whether the expression
5093                   --  is preelaborable.
5094
5095                   elsif
5096                     not Is_Preelaborable_Expression (Expression (Assn))
5097                   then
5098                      return False;
5099                   end if;
5100
5101                   Next (Assn);
5102                end loop;
5103
5104                --  If we get here then aggregate as a whole is preelaborable
5105
5106                return True;
5107
5108             --  All other cases are not preelaborable
5109
5110             else
5111                return False;
5112             end if;
5113          end Is_Preelaborable_Expression;
5114
5115       --  Start of processing for Check_Components
5116
5117       begin
5118          --  Loop through entities of record or protected type
5119
5120          Ent := E;
5121          while Present (Ent) loop
5122
5123             --  We are interested only in components and discriminants
5124
5125             if Ekind_In (Ent, E_Component, E_Discriminant) then
5126
5127                --  Get default expression if any. If there is no declaration
5128                --  node, it means we have an internal entity. The parent and
5129                --  tag fields are examples of such entities. For these cases,
5130                --  we just test the type of the entity.
5131
5132                if Present (Declaration_Node (Ent)) then
5133                   Exp := Expression (Declaration_Node (Ent));
5134                else
5135                   Exp := Empty;
5136                end if;
5137
5138                --  A component has PI if it has no default expression and the
5139                --  component type has PI.
5140
5141                if No (Exp) then
5142                   if not Has_Preelaborable_Initialization (Etype (Ent)) then
5143                      Has_PE := False;
5144                      exit;
5145                   end if;
5146
5147                --  Require the default expression to be preelaborable
5148
5149                elsif not Is_Preelaborable_Expression (Exp) then
5150                   Has_PE := False;
5151                   exit;
5152                end if;
5153             end if;
5154
5155             Next_Entity (Ent);
5156          end loop;
5157       end Check_Components;
5158
5159    --  Start of processing for Has_Preelaborable_Initialization
5160
5161    begin
5162       --  Immediate return if already marked as known preelaborable init. This
5163       --  covers types for which this function has already been called once
5164       --  and returned True (in which case the result is cached), and also
5165       --  types to which a pragma Preelaborable_Initialization applies.
5166
5167       if Known_To_Have_Preelab_Init (E) then
5168          return True;
5169       end if;
5170
5171       --  If the type is a subtype representing a generic actual type, then
5172       --  test whether its base type has preelaborable initialization since
5173       --  the subtype representing the actual does not inherit this attribute
5174       --  from the actual or formal. (but maybe it should???)
5175
5176       if Is_Generic_Actual_Type (E) then
5177          return Has_Preelaborable_Initialization (Base_Type (E));
5178       end if;
5179
5180       --  All elementary types have preelaborable initialization
5181
5182       if Is_Elementary_Type (E) then
5183          Has_PE := True;
5184
5185       --  Array types have PI if the component type has PI
5186
5187       elsif Is_Array_Type (E) then
5188          Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
5189
5190       --  A derived type has preelaborable initialization if its parent type
5191       --  has preelaborable initialization and (in the case of a derived record
5192       --  extension) if the non-inherited components all have preelaborable
5193       --  initialization. However, a user-defined controlled type with an
5194       --  overriding Initialize procedure does not have preelaborable
5195       --  initialization.
5196
5197       elsif Is_Derived_Type (E) then
5198
5199          --  If the derived type is a private extension then it doesn't have
5200          --  preelaborable initialization.
5201
5202          if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
5203             return False;
5204          end if;
5205
5206          --  First check whether ancestor type has preelaborable initialization
5207
5208          Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
5209
5210          --  If OK, check extension components (if any)
5211
5212          if Has_PE and then Is_Record_Type (E) then
5213             Check_Components (First_Entity (E));
5214          end if;
5215
5216          --  Check specifically for 10.2.1(11.4/2) exception: a controlled type
5217          --  with a user defined Initialize procedure does not have PI.
5218
5219          if Has_PE
5220            and then Is_Controlled (E)
5221            and then Has_Overriding_Initialize (E)
5222          then
5223             Has_PE := False;
5224          end if;
5225
5226       --  Private types not derived from a type having preelaborable init and
5227       --  that are not marked with pragma Preelaborable_Initialization do not
5228       --  have preelaborable initialization.
5229
5230       elsif Is_Private_Type (E) then
5231          return False;
5232
5233       --  Record type has PI if it is non private and all components have PI
5234
5235       elsif Is_Record_Type (E) then
5236          Has_PE := True;
5237          Check_Components (First_Entity (E));
5238
5239       --  Protected types must not have entries, and components must meet
5240       --  same set of rules as for record components.
5241
5242       elsif Is_Protected_Type (E) then
5243          if Has_Entries (E) then
5244             Has_PE := False;
5245          else
5246             Has_PE := True;
5247             Check_Components (First_Entity (E));
5248             Check_Components (First_Private_Entity (E));
5249          end if;
5250
5251       --  Type System.Address always has preelaborable initialization
5252
5253       elsif Is_RTE (E, RE_Address) then
5254          Has_PE := True;
5255
5256       --  In all other cases, type does not have preelaborable initialization
5257
5258       else
5259          return False;
5260       end if;
5261
5262       --  If type has preelaborable initialization, cache result
5263
5264       if Has_PE then
5265          Set_Known_To_Have_Preelab_Init (E);
5266       end if;
5267
5268       return Has_PE;
5269    end Has_Preelaborable_Initialization;
5270
5271    ---------------------------
5272    -- Has_Private_Component --
5273    ---------------------------
5274
5275    function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
5276       Btype     : Entity_Id := Base_Type (Type_Id);
5277       Component : Entity_Id;
5278
5279    begin
5280       if Error_Posted (Type_Id)
5281         or else Error_Posted (Btype)
5282       then
5283          return False;
5284       end if;
5285
5286       if Is_Class_Wide_Type (Btype) then
5287          Btype := Root_Type (Btype);
5288       end if;
5289
5290       if Is_Private_Type (Btype) then
5291          declare
5292             UT : constant Entity_Id := Underlying_Type (Btype);
5293          begin
5294             if No (UT) then
5295                if No (Full_View (Btype)) then
5296                   return not Is_Generic_Type (Btype)
5297                     and then not Is_Generic_Type (Root_Type (Btype));
5298                else
5299                   return not Is_Generic_Type (Root_Type (Full_View (Btype)));
5300                end if;
5301             else
5302                return not Is_Frozen (UT) and then Has_Private_Component (UT);
5303             end if;
5304          end;
5305
5306       elsif Is_Array_Type (Btype) then
5307          return Has_Private_Component (Component_Type (Btype));
5308
5309       elsif Is_Record_Type (Btype) then
5310          Component := First_Component (Btype);
5311          while Present (Component) loop
5312             if Has_Private_Component (Etype (Component)) then
5313                return True;
5314             end if;
5315
5316             Next_Component (Component);
5317          end loop;
5318
5319          return False;
5320
5321       elsif Is_Protected_Type (Btype)
5322         and then Present (Corresponding_Record_Type (Btype))
5323       then
5324          return Has_Private_Component (Corresponding_Record_Type (Btype));
5325
5326       else
5327          return False;
5328       end if;
5329    end Has_Private_Component;
5330
5331    ----------------
5332    -- Has_Stream --
5333    ----------------
5334
5335    function Has_Stream (T : Entity_Id) return Boolean is
5336       E : Entity_Id;
5337
5338    begin
5339       if No (T) then
5340          return False;
5341
5342       elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
5343          return True;
5344
5345       elsif Is_Array_Type (T) then
5346          return Has_Stream (Component_Type (T));
5347
5348       elsif Is_Record_Type (T) then
5349          E := First_Component (T);
5350          while Present (E) loop
5351             if Has_Stream (Etype (E)) then
5352                return True;
5353             else
5354                Next_Component (E);
5355             end if;
5356          end loop;
5357
5358          return False;
5359
5360       elsif Is_Private_Type (T) then
5361          return Has_Stream (Underlying_Type (T));
5362
5363       else
5364          return False;
5365       end if;
5366    end Has_Stream;
5367
5368    ----------------
5369    -- Has_Suffix --
5370    ----------------
5371
5372    function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
5373    begin
5374       Get_Name_String (Chars (E));
5375       return Name_Buffer (Name_Len) = Suffix;
5376    end Has_Suffix;
5377
5378    --------------------------
5379    -- Has_Tagged_Component --
5380    --------------------------
5381
5382    function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
5383       Comp : Entity_Id;
5384
5385    begin
5386       if Is_Private_Type (Typ)
5387         and then Present (Underlying_Type (Typ))
5388       then
5389          return Has_Tagged_Component (Underlying_Type (Typ));
5390
5391       elsif Is_Array_Type (Typ) then
5392          return Has_Tagged_Component (Component_Type (Typ));
5393
5394       elsif Is_Tagged_Type (Typ) then
5395          return True;
5396
5397       elsif Is_Record_Type (Typ) then
5398          Comp := First_Component (Typ);
5399          while Present (Comp) loop
5400             if Has_Tagged_Component (Etype (Comp)) then
5401                return True;
5402             end if;
5403
5404             Next_Component (Comp);
5405          end loop;
5406
5407          return False;
5408
5409       else
5410          return False;
5411       end if;
5412    end Has_Tagged_Component;
5413
5414    -------------------------
5415    -- Implementation_Kind --
5416    -------------------------
5417
5418    function Implementation_Kind (Subp : Entity_Id) return Name_Id is
5419       Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
5420    begin
5421       pragma Assert (Present (Impl_Prag));
5422       return
5423         Chars (Expression (Last (Pragma_Argument_Associations (Impl_Prag))));
5424    end Implementation_Kind;
5425
5426    --------------------------
5427    -- Implements_Interface --
5428    --------------------------
5429
5430    function Implements_Interface
5431      (Typ_Ent         : Entity_Id;
5432       Iface_Ent       : Entity_Id;
5433       Exclude_Parents : Boolean := False) return Boolean
5434    is
5435       Ifaces_List : Elist_Id;
5436       Elmt        : Elmt_Id;
5437       Iface       : Entity_Id := Base_Type (Iface_Ent);
5438       Typ         : Entity_Id := Base_Type (Typ_Ent);
5439
5440    begin
5441       if Is_Class_Wide_Type (Typ) then
5442          Typ := Root_Type (Typ);
5443       end if;
5444
5445       if not Has_Interfaces (Typ) then
5446          return False;
5447       end if;
5448
5449       if Is_Class_Wide_Type (Iface) then
5450          Iface := Root_Type (Iface);
5451       end if;
5452
5453       Collect_Interfaces (Typ, Ifaces_List);
5454
5455       Elmt := First_Elmt (Ifaces_List);
5456       while Present (Elmt) loop
5457          if Is_Ancestor (Node (Elmt), Typ)
5458            and then Exclude_Parents
5459          then
5460             null;
5461
5462          elsif Node (Elmt) = Iface then
5463             return True;
5464          end if;
5465
5466          Next_Elmt (Elmt);
5467       end loop;
5468
5469       return False;
5470    end Implements_Interface;
5471
5472    -----------------
5473    -- In_Instance --
5474    -----------------
5475
5476    function In_Instance return Boolean is
5477       Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5478       S         : Entity_Id;
5479
5480    begin
5481       S := Current_Scope;
5482       while Present (S)
5483         and then S /= Standard_Standard
5484       loop
5485          if (Ekind (S) = E_Function
5486               or else Ekind (S) = E_Package
5487               or else Ekind (S) = E_Procedure)
5488            and then Is_Generic_Instance (S)
5489          then
5490             --  A child instance is always compiled in the context of a parent
5491             --  instance. Nevertheless, the actuals are not analyzed in an
5492             --  instance context. We detect this case by examining the current
5493             --  compilation unit, which must be a child instance, and checking
5494             --  that it is not currently on the scope stack.
5495
5496             if Is_Child_Unit (Curr_Unit)
5497               and then
5498                 Nkind (Unit (Cunit (Current_Sem_Unit)))
5499                   = N_Package_Instantiation
5500               and then not In_Open_Scopes (Curr_Unit)
5501             then
5502                return False;
5503             else
5504                return True;
5505             end if;
5506          end if;
5507
5508          S := Scope (S);
5509       end loop;
5510
5511       return False;
5512    end In_Instance;
5513
5514    ----------------------
5515    -- In_Instance_Body --
5516    ----------------------
5517
5518    function In_Instance_Body return Boolean is
5519       S : Entity_Id;
5520
5521    begin
5522       S := Current_Scope;
5523       while Present (S)
5524         and then S /= Standard_Standard
5525       loop
5526          if (Ekind (S) = E_Function
5527               or else Ekind (S) = E_Procedure)
5528            and then Is_Generic_Instance (S)
5529          then
5530             return True;
5531
5532          elsif Ekind (S) = E_Package
5533            and then In_Package_Body (S)
5534            and then Is_Generic_Instance (S)
5535          then
5536             return True;
5537          end if;
5538
5539          S := Scope (S);
5540       end loop;
5541
5542       return False;
5543    end In_Instance_Body;
5544
5545    -----------------------------
5546    -- In_Instance_Not_Visible --
5547    -----------------------------
5548
5549    function In_Instance_Not_Visible return Boolean is
5550       S : Entity_Id;
5551
5552    begin
5553       S := Current_Scope;
5554       while Present (S)
5555         and then S /= Standard_Standard
5556       loop
5557          if (Ekind (S) = E_Function
5558               or else Ekind (S) = E_Procedure)
5559            and then Is_Generic_Instance (S)
5560          then
5561             return True;
5562
5563          elsif Ekind (S) = E_Package
5564            and then (In_Package_Body (S) or else In_Private_Part (S))
5565            and then Is_Generic_Instance (S)
5566          then
5567             return True;
5568          end if;
5569
5570          S := Scope (S);
5571       end loop;
5572
5573       return False;
5574    end In_Instance_Not_Visible;
5575
5576    ------------------------------
5577    -- In_Instance_Visible_Part --
5578    ------------------------------
5579
5580    function In_Instance_Visible_Part return Boolean is
5581       S : Entity_Id;
5582
5583    begin
5584       S := Current_Scope;
5585       while Present (S)
5586         and then S /= Standard_Standard
5587       loop
5588          if Ekind (S) = E_Package
5589            and then Is_Generic_Instance (S)
5590            and then not In_Package_Body (S)
5591            and then not In_Private_Part (S)
5592          then
5593             return True;
5594          end if;
5595
5596          S := Scope (S);
5597       end loop;
5598
5599       return False;
5600    end In_Instance_Visible_Part;
5601
5602    ---------------------
5603    -- In_Package_Body --
5604    ---------------------
5605
5606    function In_Package_Body return Boolean is
5607       S : Entity_Id;
5608
5609    begin
5610       S := Current_Scope;
5611       while Present (S)
5612         and then S /= Standard_Standard
5613       loop
5614          if Ekind (S) = E_Package
5615            and then In_Package_Body (S)
5616          then
5617             return True;
5618          else
5619             S := Scope (S);
5620          end if;
5621       end loop;
5622
5623       return False;
5624    end In_Package_Body;
5625
5626    --------------------------------
5627    -- In_Parameter_Specification --
5628    --------------------------------
5629
5630    function In_Parameter_Specification (N : Node_Id) return Boolean is
5631       PN : Node_Id;
5632
5633    begin
5634       PN := Parent (N);
5635       while Present (PN) loop
5636          if Nkind (PN) = N_Parameter_Specification then
5637             return True;
5638          end if;
5639
5640          PN := Parent (PN);
5641       end loop;
5642
5643       return False;
5644    end In_Parameter_Specification;
5645
5646    --------------------------------------
5647    -- In_Subprogram_Or_Concurrent_Unit --
5648    --------------------------------------
5649
5650    function In_Subprogram_Or_Concurrent_Unit return Boolean is
5651       E : Entity_Id;
5652       K : Entity_Kind;
5653
5654    begin
5655       --  Use scope chain to check successively outer scopes
5656
5657       E := Current_Scope;
5658       loop
5659          K := Ekind (E);
5660
5661          if K in Subprogram_Kind
5662            or else K in Concurrent_Kind
5663            or else K in Generic_Subprogram_Kind
5664          then
5665             return True;
5666
5667          elsif E = Standard_Standard then
5668             return False;
5669          end if;
5670
5671          E := Scope (E);
5672       end loop;
5673    end In_Subprogram_Or_Concurrent_Unit;
5674
5675    ---------------------
5676    -- In_Visible_Part --
5677    ---------------------
5678
5679    function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
5680    begin
5681       return
5682         Is_Package_Or_Generic_Package (Scope_Id)
5683           and then In_Open_Scopes (Scope_Id)
5684           and then not In_Package_Body (Scope_Id)
5685           and then not In_Private_Part (Scope_Id);
5686    end In_Visible_Part;
5687
5688    ---------------------------------
5689    -- Insert_Explicit_Dereference --
5690    ---------------------------------
5691
5692    procedure Insert_Explicit_Dereference (N : Node_Id) is
5693       New_Prefix : constant Node_Id := Relocate_Node (N);
5694       Ent        : Entity_Id := Empty;
5695       Pref       : Node_Id;
5696       I          : Interp_Index;
5697       It         : Interp;
5698       T          : Entity_Id;
5699
5700    begin
5701       Save_Interps (N, New_Prefix);
5702
5703       Rewrite (N,
5704         Make_Explicit_Dereference (Sloc (Parent (N)),
5705           Prefix => New_Prefix));
5706
5707       Set_Etype (N, Designated_Type (Etype (New_Prefix)));
5708
5709       if Is_Overloaded (New_Prefix) then
5710
5711          --  The dereference is also overloaded, and its interpretations are
5712          --  the designated types of the interpretations of the original node.
5713
5714          Set_Etype (N, Any_Type);
5715
5716          Get_First_Interp (New_Prefix, I, It);
5717          while Present (It.Nam) loop
5718             T := It.Typ;
5719
5720             if Is_Access_Type (T) then
5721                Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
5722             end if;
5723
5724             Get_Next_Interp (I, It);
5725          end loop;
5726
5727          End_Interp_List;
5728
5729       else
5730          --  Prefix is unambiguous: mark the original prefix (which might
5731          --  Come_From_Source) as a reference, since the new (relocated) one
5732          --  won't be taken into account.
5733
5734          if Is_Entity_Name (New_Prefix) then
5735             Ent := Entity (New_Prefix);
5736             Pref := New_Prefix;
5737
5738          --  For a retrieval of a subcomponent of some composite object,
5739          --  retrieve the ultimate entity if there is one.
5740
5741          elsif Nkind (New_Prefix) = N_Selected_Component
5742            or else Nkind (New_Prefix) = N_Indexed_Component
5743          then
5744             Pref := Prefix (New_Prefix);
5745             while Present (Pref)
5746               and then
5747                 (Nkind (Pref) = N_Selected_Component
5748                   or else Nkind (Pref) = N_Indexed_Component)
5749             loop
5750                Pref := Prefix (Pref);
5751             end loop;
5752
5753             if Present (Pref) and then Is_Entity_Name (Pref) then
5754                Ent := Entity (Pref);
5755             end if;
5756          end if;
5757
5758          --  Place the reference on the entity node
5759
5760          if Present (Ent) then
5761             Generate_Reference (Ent, Pref);
5762          end if;
5763       end if;
5764    end Insert_Explicit_Dereference;
5765
5766    ------------------------------------------
5767    -- Inspect_Deferred_Constant_Completion --
5768    ------------------------------------------
5769
5770    procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
5771       Decl   : Node_Id;
5772
5773    begin
5774       Decl := First (Decls);
5775       while Present (Decl) loop
5776
5777          --  Deferred constant signature
5778
5779          if Nkind (Decl) = N_Object_Declaration
5780            and then Constant_Present (Decl)
5781            and then No (Expression (Decl))
5782
5783             --  No need to check internally generated constants
5784
5785            and then Comes_From_Source (Decl)
5786
5787             --  The constant is not completed. A full object declaration or a
5788             --  pragma Import complete a deferred constant.
5789
5790            and then not Has_Completion (Defining_Identifier (Decl))
5791          then
5792             Error_Msg_N
5793               ("constant declaration requires initialization expression",
5794               Defining_Identifier (Decl));
5795          end if;
5796
5797          Decl := Next (Decl);
5798       end loop;
5799    end Inspect_Deferred_Constant_Completion;
5800
5801    -----------------------------
5802    -- Is_Actual_Out_Parameter --
5803    -----------------------------
5804
5805    function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
5806       Formal : Entity_Id;
5807       Call   : Node_Id;
5808    begin
5809       Find_Actual (N, Formal, Call);
5810       return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
5811    end Is_Actual_Out_Parameter;
5812
5813    -------------------------
5814    -- Is_Actual_Parameter --
5815    -------------------------
5816
5817    function Is_Actual_Parameter (N : Node_Id) return Boolean is
5818       PK : constant Node_Kind := Nkind (Parent (N));
5819
5820    begin
5821       case PK is
5822          when N_Parameter_Association =>
5823             return N = Explicit_Actual_Parameter (Parent (N));
5824
5825          when N_Function_Call | N_Procedure_Call_Statement =>
5826             return Is_List_Member (N)
5827               and then
5828                 List_Containing (N) = Parameter_Associations (Parent (N));
5829
5830          when others =>
5831             return False;
5832       end case;
5833    end Is_Actual_Parameter;
5834
5835    ---------------------
5836    -- Is_Aliased_View --
5837    ---------------------
5838
5839    function Is_Aliased_View (Obj : Node_Id) return Boolean is
5840       E : Entity_Id;
5841
5842    begin
5843       if Is_Entity_Name (Obj) then
5844
5845          E := Entity (Obj);
5846
5847          return
5848            (Is_Object (E)
5849              and then
5850                (Is_Aliased (E)
5851                   or else (Present (Renamed_Object (E))
5852                              and then Is_Aliased_View (Renamed_Object (E)))))
5853
5854            or else ((Is_Formal (E)
5855                       or else Ekind (E) = E_Generic_In_Out_Parameter
5856                       or else Ekind (E) = E_Generic_In_Parameter)
5857                     and then Is_Tagged_Type (Etype (E)))
5858
5859            or else (Is_Concurrent_Type (E)
5860                      and then In_Open_Scopes (E))
5861
5862             --  Current instance of type, either directly or as rewritten
5863             --  reference to the current object.
5864
5865            or else (Is_Entity_Name (Original_Node (Obj))
5866                      and then Present (Entity (Original_Node (Obj)))
5867                      and then Is_Type (Entity (Original_Node (Obj))))
5868
5869            or else (Is_Type (E) and then E = Current_Scope)
5870
5871            or else (Is_Incomplete_Or_Private_Type (E)
5872                      and then Full_View (E) = Current_Scope);
5873
5874       elsif Nkind (Obj) = N_Selected_Component then
5875          return Is_Aliased (Entity (Selector_Name (Obj)));
5876
5877       elsif Nkind (Obj) = N_Indexed_Component then
5878          return Has_Aliased_Components (Etype (Prefix (Obj)))
5879            or else
5880              (Is_Access_Type (Etype (Prefix (Obj)))
5881                and then
5882               Has_Aliased_Components
5883                 (Designated_Type (Etype (Prefix (Obj)))));
5884
5885       elsif Nkind (Obj) = N_Unchecked_Type_Conversion
5886         or else Nkind (Obj) = N_Type_Conversion
5887       then
5888          return Is_Tagged_Type (Etype (Obj))
5889            and then Is_Aliased_View (Expression (Obj));
5890
5891       elsif Nkind (Obj) = N_Explicit_Dereference then
5892          return Nkind (Original_Node (Obj)) /= N_Function_Call;
5893
5894       else
5895          return False;
5896       end if;
5897    end Is_Aliased_View;
5898
5899    -------------------------
5900    -- Is_Ancestor_Package --
5901    -------------------------
5902
5903    function Is_Ancestor_Package
5904      (E1 : Entity_Id;
5905       E2 : Entity_Id) return Boolean
5906    is
5907       Par : Entity_Id;
5908
5909    begin
5910       Par := E2;
5911       while Present (Par)
5912         and then Par /= Standard_Standard
5913       loop
5914          if Par = E1 then
5915             return True;
5916          end if;
5917
5918          Par := Scope (Par);
5919       end loop;
5920
5921       return False;
5922    end Is_Ancestor_Package;
5923
5924    ----------------------
5925    -- Is_Atomic_Object --
5926    ----------------------
5927
5928    function Is_Atomic_Object (N : Node_Id) return Boolean is
5929
5930       function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
5931       --  Determines if given object has atomic components
5932
5933       function Is_Atomic_Prefix (N : Node_Id) return Boolean;
5934       --  If prefix is an implicit dereference, examine designated type
5935
5936       ----------------------
5937       -- Is_Atomic_Prefix --
5938       ----------------------
5939
5940       function Is_Atomic_Prefix (N : Node_Id) return Boolean is
5941       begin
5942          if Is_Access_Type (Etype (N)) then
5943             return
5944               Has_Atomic_Components (Designated_Type (Etype (N)));
5945          else
5946             return Object_Has_Atomic_Components (N);
5947          end if;
5948       end Is_Atomic_Prefix;
5949
5950       ----------------------------------
5951       -- Object_Has_Atomic_Components --
5952       ----------------------------------
5953
5954       function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
5955       begin
5956          if Has_Atomic_Components (Etype (N))
5957            or else Is_Atomic (Etype (N))
5958          then
5959             return True;
5960
5961          elsif Is_Entity_Name (N)
5962            and then (Has_Atomic_Components (Entity (N))
5963                       or else Is_Atomic (Entity (N)))
5964          then
5965             return True;
5966
5967          elsif Nkind (N) = N_Indexed_Component
5968            or else Nkind (N) = N_Selected_Component
5969          then
5970             return Is_Atomic_Prefix (Prefix (N));
5971
5972          else
5973             return False;
5974          end if;
5975       end Object_Has_Atomic_Components;
5976
5977    --  Start of processing for Is_Atomic_Object
5978
5979    begin
5980       --  Predicate is not relevant to subprograms
5981
5982       if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
5983          return False;
5984
5985       elsif Is_Atomic (Etype (N))
5986         or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
5987       then
5988          return True;
5989
5990       elsif Nkind (N) = N_Indexed_Component
5991         or else Nkind (N) = N_Selected_Component
5992       then
5993          return Is_Atomic_Prefix (Prefix (N));
5994
5995       else
5996          return False;
5997       end if;
5998    end Is_Atomic_Object;
5999
6000    -------------------------
6001    -- Is_Coextension_Root --
6002    -------------------------
6003
6004    function Is_Coextension_Root (N : Node_Id) return Boolean is
6005    begin
6006       return
6007         Nkind (N) = N_Allocator
6008           and then Present (Coextensions (N))
6009
6010          --  Anonymous access discriminants carry a list of all nested
6011          --  controlled coextensions.
6012
6013           and then not Is_Dynamic_Coextension (N)
6014           and then not Is_Static_Coextension (N);
6015    end Is_Coextension_Root;
6016
6017    -----------------------------
6018    -- Is_Concurrent_Interface --
6019    -----------------------------
6020
6021    function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
6022    begin
6023       return
6024         Is_Interface (T)
6025           and then
6026             (Is_Protected_Interface (T)
6027                or else Is_Synchronized_Interface (T)
6028                or else Is_Task_Interface (T));
6029    end Is_Concurrent_Interface;
6030
6031    --------------------------------------
6032    -- Is_Controlling_Limited_Procedure --
6033    --------------------------------------
6034
6035    function Is_Controlling_Limited_Procedure
6036      (Proc_Nam : Entity_Id) return Boolean
6037    is
6038       Param_Typ : Entity_Id := Empty;
6039
6040    begin
6041       if Ekind (Proc_Nam) = E_Procedure
6042         and then Present (Parameter_Specifications (Parent (Proc_Nam)))
6043       then
6044          Param_Typ := Etype (Parameter_Type (First (
6045                         Parameter_Specifications (Parent (Proc_Nam)))));
6046
6047       --  In this case where an Itype was created, the procedure call has been
6048       --  rewritten.
6049
6050       elsif Present (Associated_Node_For_Itype (Proc_Nam))
6051         and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
6052         and then
6053           Present (Parameter_Associations
6054                      (Associated_Node_For_Itype (Proc_Nam)))
6055       then
6056          Param_Typ :=
6057            Etype (First (Parameter_Associations
6058                           (Associated_Node_For_Itype (Proc_Nam))));
6059       end if;
6060
6061       if Present (Param_Typ) then
6062          return
6063            Is_Interface (Param_Typ)
6064              and then Is_Limited_Record (Param_Typ);
6065       end if;
6066
6067       return False;
6068    end Is_Controlling_Limited_Procedure;
6069
6070    -----------------------------
6071    -- Is_CPP_Constructor_Call --
6072    -----------------------------
6073
6074    function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
6075    begin
6076       return Nkind (N) = N_Function_Call
6077         and then Is_CPP_Class (Etype (Etype (N)))
6078         and then Is_Constructor (Entity (Name (N)))
6079         and then Is_Imported (Entity (Name (N)));
6080    end Is_CPP_Constructor_Call;
6081
6082    -----------------
6083    -- Is_Delegate --
6084    -----------------
6085
6086    function Is_Delegate (T : Entity_Id) return Boolean is
6087       Desig_Type : Entity_Id;
6088
6089    begin
6090       if VM_Target /= CLI_Target then
6091          return False;
6092       end if;
6093
6094       --  Access-to-subprograms are delegates in CIL
6095
6096       if Ekind (T) = E_Access_Subprogram_Type then
6097          return True;
6098       end if;
6099
6100       if Ekind (T) not in Access_Kind then
6101
6102          --  A delegate is a managed pointer. If no designated type is defined
6103          --  it means that it's not a delegate.
6104
6105          return False;
6106       end if;
6107
6108       Desig_Type := Etype (Directly_Designated_Type (T));
6109
6110       if not Is_Tagged_Type (Desig_Type) then
6111          return False;
6112       end if;
6113
6114       --  Test if the type is inherited from [mscorlib]System.Delegate
6115
6116       while Etype (Desig_Type) /= Desig_Type loop
6117          if Chars (Scope (Desig_Type)) /= No_Name
6118            and then Is_Imported (Scope (Desig_Type))
6119            and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
6120          then
6121             return True;
6122          end if;
6123
6124          Desig_Type := Etype (Desig_Type);
6125       end loop;
6126
6127       return False;
6128    end Is_Delegate;
6129
6130    ----------------------------------------------
6131    -- Is_Dependent_Component_Of_Mutable_Object --
6132    ----------------------------------------------
6133
6134    function Is_Dependent_Component_Of_Mutable_Object
6135      (Object : Node_Id) return Boolean
6136    is
6137       P           : Node_Id;
6138       Prefix_Type : Entity_Id;
6139       P_Aliased   : Boolean := False;
6140       Comp        : Entity_Id;
6141
6142       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
6143       --  Returns True if and only if Comp is declared within a variant part
6144
6145       --------------------------------
6146       -- Is_Declared_Within_Variant --
6147       --------------------------------
6148
6149       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
6150          Comp_Decl : constant Node_Id   := Parent (Comp);
6151          Comp_List : constant Node_Id   := Parent (Comp_Decl);
6152       begin
6153          return Nkind (Parent (Comp_List)) = N_Variant;
6154       end Is_Declared_Within_Variant;
6155
6156    --  Start of processing for Is_Dependent_Component_Of_Mutable_Object
6157
6158    begin
6159       if Is_Variable (Object) then
6160
6161          if Nkind (Object) = N_Selected_Component then
6162             P := Prefix (Object);
6163             Prefix_Type := Etype (P);
6164
6165             if Is_Entity_Name (P) then
6166
6167                if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
6168                   Prefix_Type := Base_Type (Prefix_Type);
6169                end if;
6170
6171                if Is_Aliased (Entity (P)) then
6172                   P_Aliased := True;
6173                end if;
6174
6175             --  A discriminant check on a selected component may be expanded
6176             --  into a dereference when removing side-effects. Recover the
6177             --  original node and its type, which may be unconstrained.
6178
6179             elsif Nkind (P) = N_Explicit_Dereference
6180               and then not (Comes_From_Source (P))
6181             then
6182                P := Original_Node (P);
6183                Prefix_Type := Etype (P);
6184
6185             else
6186                --  Check for prefix being an aliased component???
6187
6188                null;
6189
6190             end if;
6191
6192             --  A heap object is constrained by its initial value
6193
6194             --  Ada 2005 (AI-363): Always assume the object could be mutable in
6195             --  the dereferenced case, since the access value might denote an
6196             --  unconstrained aliased object, whereas in Ada 95 the designated
6197             --  object is guaranteed to be constrained. A worst-case assumption
6198             --  has to apply in Ada 2005 because we can't tell at compile time
6199             --  whether the object is "constrained by its initial value"
6200             --  (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are
6201             --  semantic rules -- these rules are acknowledged to need fixing).
6202
6203             if Ada_Version < Ada_2005 then
6204                if Is_Access_Type (Prefix_Type)
6205                  or else Nkind (P) = N_Explicit_Dereference
6206                then
6207                   return False;
6208                end if;
6209
6210             elsif Ada_Version >= Ada_2005 then
6211                if Is_Access_Type (Prefix_Type) then
6212
6213                   --  If the access type is pool-specific, and there is no
6214                   --  constrained partial view of the designated type, then the
6215                   --  designated object is known to be constrained.
6216
6217                   if Ekind (Prefix_Type) = E_Access_Type
6218                     and then not Has_Constrained_Partial_View
6219                                    (Designated_Type (Prefix_Type))
6220                   then
6221                      return False;
6222
6223                   --  Otherwise (general access type, or there is a constrained
6224                   --  partial view of the designated type), we need to check
6225                   --  based on the designated type.
6226
6227                   else
6228                      Prefix_Type := Designated_Type (Prefix_Type);
6229                   end if;
6230                end if;
6231             end if;
6232
6233             Comp :=
6234               Original_Record_Component (Entity (Selector_Name (Object)));
6235
6236             --  As per AI-0017, the renaming is illegal in a generic body, even
6237             --  if the subtype is indefinite.
6238
6239             --  Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
6240
6241             if not Is_Constrained (Prefix_Type)
6242               and then (not Is_Indefinite_Subtype (Prefix_Type)
6243                          or else
6244                           (Is_Generic_Type (Prefix_Type)
6245                             and then Ekind (Current_Scope) = E_Generic_Package
6246                             and then In_Package_Body (Current_Scope)))
6247
6248               and then (Is_Declared_Within_Variant (Comp)
6249                           or else Has_Discriminant_Dependent_Constraint (Comp))
6250               and then (not P_Aliased or else Ada_Version >= Ada_2005)
6251             then
6252                return True;
6253
6254             else
6255                return
6256                  Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
6257
6258             end if;
6259
6260          elsif Nkind (Object) = N_Indexed_Component
6261            or else Nkind (Object) = N_Slice
6262          then
6263             return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
6264
6265          --  A type conversion that Is_Variable is a view conversion:
6266          --  go back to the denoted object.
6267
6268          elsif Nkind (Object) = N_Type_Conversion then
6269             return
6270               Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
6271          end if;
6272       end if;
6273
6274       return False;
6275    end Is_Dependent_Component_Of_Mutable_Object;
6276
6277    ---------------------
6278    -- Is_Dereferenced --
6279    ---------------------
6280
6281    function Is_Dereferenced (N : Node_Id) return Boolean is
6282       P : constant Node_Id := Parent (N);
6283    begin
6284       return
6285          (Nkind (P) = N_Selected_Component
6286             or else
6287           Nkind (P) = N_Explicit_Dereference
6288             or else
6289           Nkind (P) = N_Indexed_Component
6290             or else
6291           Nkind (P) = N_Slice)
6292         and then Prefix (P) = N;
6293    end Is_Dereferenced;
6294
6295    ----------------------
6296    -- Is_Descendent_Of --
6297    ----------------------
6298
6299    function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
6300       T    : Entity_Id;
6301       Etyp : Entity_Id;
6302
6303    begin
6304       pragma Assert (Nkind (T1) in N_Entity);
6305       pragma Assert (Nkind (T2) in N_Entity);
6306
6307       T := Base_Type (T1);
6308
6309       --  Immediate return if the types match
6310
6311       if T = T2 then
6312          return True;
6313
6314       --  Comment needed here ???
6315
6316       elsif Ekind (T) = E_Class_Wide_Type then
6317          return Etype (T) = T2;
6318
6319       --  All other cases
6320
6321       else
6322          loop
6323             Etyp := Etype (T);
6324
6325             --  Done if we found the type we are looking for
6326
6327             if Etyp = T2 then
6328                return True;
6329
6330             --  Done if no more derivations to check
6331
6332             elsif T = T1
6333               or else T = Etyp
6334             then
6335                return False;
6336
6337             --  Following test catches error cases resulting from prev errors
6338
6339             elsif No (Etyp) then
6340                return False;
6341
6342             elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
6343                return False;
6344
6345             elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
6346                return False;
6347             end if;
6348
6349             T := Base_Type (Etyp);
6350          end loop;
6351       end if;
6352    end Is_Descendent_Of;
6353
6354    --------------
6355    -- Is_False --
6356    --------------
6357
6358    function Is_False (U : Uint) return Boolean is
6359    begin
6360       return (U = 0);
6361    end Is_False;
6362
6363    ---------------------------
6364    -- Is_Fixed_Model_Number --
6365    ---------------------------
6366
6367    function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
6368       S : constant Ureal := Small_Value (T);
6369       M : Urealp.Save_Mark;
6370       R : Boolean;
6371    begin
6372       M := Urealp.Mark;
6373       R := (U = UR_Trunc (U / S) * S);
6374       Urealp.Release (M);
6375       return R;
6376    end Is_Fixed_Model_Number;
6377
6378    -------------------------------
6379    -- Is_Fully_Initialized_Type --
6380    -------------------------------
6381
6382    function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
6383    begin
6384       if Is_Scalar_Type (Typ) then
6385          return False;
6386
6387       elsif Is_Access_Type (Typ) then
6388          return True;
6389
6390       elsif Is_Array_Type (Typ) then
6391          if Is_Fully_Initialized_Type (Component_Type (Typ)) then
6392             return True;
6393          end if;
6394
6395          --  An interesting case, if we have a constrained type one of whose
6396          --  bounds is known to be null, then there are no elements to be
6397          --  initialized, so all the elements are initialized!
6398
6399          if Is_Constrained (Typ) then
6400             declare
6401                Indx     : Node_Id;
6402                Indx_Typ : Entity_Id;
6403                Lbd, Hbd : Node_Id;
6404
6405             begin
6406                Indx := First_Index (Typ);
6407                while Present (Indx) loop
6408                   if Etype (Indx) = Any_Type then
6409                      return False;
6410
6411                   --  If index is a range, use directly
6412
6413                   elsif Nkind (Indx) = N_Range then
6414                      Lbd := Low_Bound  (Indx);
6415                      Hbd := High_Bound (Indx);
6416
6417                   else
6418                      Indx_Typ := Etype (Indx);
6419
6420                      if Is_Private_Type (Indx_Typ)  then
6421                         Indx_Typ := Full_View (Indx_Typ);
6422                      end if;
6423
6424                      if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
6425                         return False;
6426                      else
6427                         Lbd := Type_Low_Bound  (Indx_Typ);
6428                         Hbd := Type_High_Bound (Indx_Typ);
6429                      end if;
6430                   end if;
6431
6432                   if Compile_Time_Known_Value (Lbd)
6433                     and then Compile_Time_Known_Value (Hbd)
6434                   then
6435                      if Expr_Value (Hbd) < Expr_Value (Lbd) then
6436                         return True;
6437                      end if;
6438                   end if;
6439
6440                   Next_Index (Indx);
6441                end loop;
6442             end;
6443          end if;
6444
6445          --  If no null indexes, then type is not fully initialized
6446
6447          return False;
6448
6449       --  Record types
6450
6451       elsif Is_Record_Type (Typ) then
6452          if Has_Discriminants (Typ)
6453            and then
6454              Present (Discriminant_Default_Value (First_Discriminant (Typ)))
6455            and then Is_Fully_Initialized_Variant (Typ)
6456          then
6457             return True;
6458          end if;
6459
6460          --  Controlled records are considered to be fully initialized if
6461          --  there is a user defined Initialize routine. This may not be
6462          --  entirely correct, but as the spec notes, we are guessing here
6463          --  what is best from the point of view of issuing warnings.
6464
6465          if Is_Controlled (Typ) then
6466             declare
6467                Utyp : constant Entity_Id := Underlying_Type (Typ);
6468
6469             begin
6470                if Present (Utyp) then
6471                   declare
6472                      Init : constant Entity_Id :=
6473                               (Find_Prim_Op
6474                                  (Underlying_Type (Typ), Name_Initialize));
6475
6476                   begin
6477                      if Present (Init)
6478                        and then Comes_From_Source (Init)
6479                        and then not
6480                          Is_Predefined_File_Name
6481                            (File_Name (Get_Source_File_Index (Sloc (Init))))
6482                      then
6483                         return True;
6484
6485                      elsif Has_Null_Extension (Typ)
6486                         and then
6487                           Is_Fully_Initialized_Type
6488                             (Etype (Base_Type (Typ)))
6489                      then
6490                         return True;
6491                      end if;
6492                   end;
6493                end if;
6494             end;
6495          end if;
6496
6497          --  Otherwise see if all record components are initialized
6498
6499          declare
6500             Ent : Entity_Id;
6501
6502          begin
6503             Ent := First_Entity (Typ);
6504             while Present (Ent) loop
6505                if Chars (Ent) = Name_uController then
6506                   null;
6507
6508                elsif Ekind (Ent) = E_Component
6509                  and then (No (Parent (Ent))
6510                              or else No (Expression (Parent (Ent))))
6511                  and then not Is_Fully_Initialized_Type (Etype (Ent))
6512
6513                   --  Special VM case for tag components, which need to be
6514                   --  defined in this case, but are never initialized as VMs
6515                   --  are using other dispatching mechanisms. Ignore this
6516                   --  uninitialized case. Note that this applies both to the
6517                   --  uTag entry and the main vtable pointer (CPP_Class case).
6518
6519                  and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
6520                then
6521                   return False;
6522                end if;
6523
6524                Next_Entity (Ent);
6525             end loop;
6526          end;
6527
6528          --  No uninitialized components, so type is fully initialized.
6529          --  Note that this catches the case of no components as well.
6530
6531          return True;
6532
6533       elsif Is_Concurrent_Type (Typ) then
6534          return True;
6535
6536       elsif Is_Private_Type (Typ) then
6537          declare
6538             U : constant Entity_Id := Underlying_Type (Typ);
6539
6540          begin
6541             if No (U) then
6542                return False;
6543             else
6544                return Is_Fully_Initialized_Type (U);
6545             end if;
6546          end;
6547
6548       else
6549          return False;
6550       end if;
6551    end Is_Fully_Initialized_Type;
6552
6553    ----------------------------------
6554    -- Is_Fully_Initialized_Variant --
6555    ----------------------------------
6556
6557    function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
6558       Loc           : constant Source_Ptr := Sloc (Typ);
6559       Constraints   : constant List_Id    := New_List;
6560       Components    : constant Elist_Id   := New_Elmt_List;
6561       Comp_Elmt     : Elmt_Id;
6562       Comp_Id       : Node_Id;
6563       Comp_List     : Node_Id;
6564       Discr         : Entity_Id;
6565       Discr_Val     : Node_Id;
6566
6567       Report_Errors : Boolean;
6568       pragma Warnings (Off, Report_Errors);
6569
6570    begin
6571       if Serious_Errors_Detected > 0 then
6572          return False;
6573       end if;
6574
6575       if Is_Record_Type (Typ)
6576         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
6577         and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
6578       then
6579          Comp_List := Component_List (Type_Definition (Parent (Typ)));
6580
6581          Discr := First_Discriminant (Typ);
6582          while Present (Discr) loop
6583             if Nkind (Parent (Discr)) = N_Discriminant_Specification then
6584                Discr_Val := Expression (Parent (Discr));
6585
6586                if Present (Discr_Val)
6587                  and then Is_OK_Static_Expression (Discr_Val)
6588                then
6589                   Append_To (Constraints,
6590                     Make_Component_Association (Loc,
6591                       Choices    => New_List (New_Occurrence_Of (Discr, Loc)),
6592                       Expression => New_Copy (Discr_Val)));
6593                else
6594                   return False;
6595                end if;
6596             else
6597                return False;
6598             end if;
6599
6600             Next_Discriminant (Discr);
6601          end loop;
6602
6603          Gather_Components
6604            (Typ           => Typ,
6605             Comp_List     => Comp_List,
6606             Governed_By   => Constraints,
6607             Into          => Components,
6608             Report_Errors => Report_Errors);
6609
6610          --  Check that each component present is fully initialized
6611
6612          Comp_Elmt := First_Elmt (Components);
6613          while Present (Comp_Elmt) loop
6614             Comp_Id := Node (Comp_Elmt);
6615
6616             if Ekind (Comp_Id) = E_Component
6617               and then (No (Parent (Comp_Id))
6618                          or else No (Expression (Parent (Comp_Id))))
6619               and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
6620             then
6621                return False;
6622             end if;
6623
6624             Next_Elmt (Comp_Elmt);
6625          end loop;
6626
6627          return True;
6628
6629       elsif Is_Private_Type (Typ) then
6630          declare
6631             U : constant Entity_Id := Underlying_Type (Typ);
6632
6633          begin
6634             if No (U) then
6635                return False;
6636             else
6637                return Is_Fully_Initialized_Variant (U);
6638             end if;
6639          end;
6640       else
6641          return False;
6642       end if;
6643    end Is_Fully_Initialized_Variant;
6644
6645    ------------
6646    -- Is_LHS --
6647    ------------
6648
6649    --  We seem to have a lot of overlapping functions that do similar things
6650    --  (testing for left hand sides or lvalues???). Anyway, since this one is
6651    --  purely syntactic, it should be in Sem_Aux I would think???
6652
6653    function Is_LHS (N : Node_Id) return Boolean is
6654       P : constant Node_Id := Parent (N);
6655    begin
6656       return Nkind (P) = N_Assignment_Statement
6657         and then Name (P) = N;
6658    end Is_LHS;
6659
6660    ----------------------------
6661    -- Is_Inherited_Operation --
6662    ----------------------------
6663
6664    function Is_Inherited_Operation (E : Entity_Id) return Boolean is
6665       Kind : constant Node_Kind := Nkind (Parent (E));
6666    begin
6667       pragma Assert (Is_Overloadable (E));
6668       return Kind = N_Full_Type_Declaration
6669         or else Kind = N_Private_Extension_Declaration
6670         or else Kind = N_Subtype_Declaration
6671         or else (Ekind (E) = E_Enumeration_Literal
6672                   and then Is_Derived_Type (Etype (E)));
6673    end Is_Inherited_Operation;
6674
6675    -----------------------------
6676    -- Is_Library_Level_Entity --
6677    -----------------------------
6678
6679    function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
6680    begin
6681       --  The following is a small optimization, and it also properly handles
6682       --  discriminals, which in task bodies might appear in expressions before
6683       --  the corresponding procedure has been created, and which therefore do
6684       --  not have an assigned scope.
6685
6686       if Is_Formal (E) then
6687          return False;
6688       end if;
6689
6690       --  Normal test is simply that the enclosing dynamic scope is Standard
6691
6692       return Enclosing_Dynamic_Scope (E) = Standard_Standard;
6693    end Is_Library_Level_Entity;
6694
6695    ---------------------------------
6696    -- Is_Local_Variable_Reference --
6697    ---------------------------------
6698
6699    function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
6700    begin
6701       if not Is_Entity_Name (Expr) then
6702          return False;
6703
6704       else
6705          declare
6706             Ent : constant Entity_Id := Entity (Expr);
6707             Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
6708          begin
6709             if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
6710                return False;
6711             else
6712                return Present (Sub) and then Sub = Current_Subprogram;
6713             end if;
6714          end;
6715       end if;
6716    end Is_Local_Variable_Reference;
6717
6718    -------------------------
6719    -- Is_Object_Reference --
6720    -------------------------
6721
6722    function Is_Object_Reference (N : Node_Id) return Boolean is
6723    begin
6724       if Is_Entity_Name (N) then
6725          return Present (Entity (N)) and then Is_Object (Entity (N));
6726
6727       else
6728          case Nkind (N) is
6729             when N_Indexed_Component | N_Slice =>
6730                return
6731                  Is_Object_Reference (Prefix (N))
6732                    or else Is_Access_Type (Etype (Prefix (N)));
6733
6734             --  In Ada95, a function call is a constant object; a procedure
6735             --  call is not.
6736
6737             when N_Function_Call =>
6738                return Etype (N) /= Standard_Void_Type;
6739
6740             --  A reference to the stream attribute Input is a function call
6741
6742             when N_Attribute_Reference =>
6743                return Attribute_Name (N) = Name_Input;
6744
6745             when N_Selected_Component =>
6746                return
6747                  Is_Object_Reference (Selector_Name (N))
6748                    and then
6749                      (Is_Object_Reference (Prefix (N))
6750                         or else Is_Access_Type (Etype (Prefix (N))));
6751
6752             when N_Explicit_Dereference =>
6753                return True;
6754
6755             --  A view conversion of a tagged object is an object reference
6756
6757             when N_Type_Conversion =>
6758                return Is_Tagged_Type (Etype (Subtype_Mark (N)))
6759                  and then Is_Tagged_Type (Etype (Expression (N)))
6760                  and then Is_Object_Reference (Expression (N));
6761
6762             --  An unchecked type conversion is considered to be an object if
6763             --  the operand is an object (this construction arises only as a
6764             --  result of expansion activities).
6765
6766             when N_Unchecked_Type_Conversion =>
6767                return True;
6768
6769             when others =>
6770                return False;
6771          end case;
6772       end if;
6773    end Is_Object_Reference;
6774
6775    -----------------------------------
6776    -- Is_OK_Variable_For_Out_Formal --
6777    -----------------------------------
6778
6779    function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
6780    begin
6781       Note_Possible_Modification (AV, Sure => True);
6782
6783       --  We must reject parenthesized variable names. The check for
6784       --  Comes_From_Source is present because there are currently
6785       --  cases where the compiler violates this rule (e.g. passing
6786       --  a task object to its controlled Initialize routine).
6787
6788       if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
6789          return False;
6790
6791       --  A variable is always allowed
6792
6793       elsif Is_Variable (AV) then
6794          return True;
6795
6796       --  Unchecked conversions are allowed only if they come from the
6797       --  generated code, which sometimes uses unchecked conversions for out
6798       --  parameters in cases where code generation is unaffected. We tell
6799       --  source unchecked conversions by seeing if they are rewrites of an
6800       --  original Unchecked_Conversion function call, or of an explicit
6801       --  conversion of a function call.
6802
6803       elsif Nkind (AV) = N_Unchecked_Type_Conversion then
6804          if Nkind (Original_Node (AV)) = N_Function_Call then
6805             return False;
6806
6807          elsif Comes_From_Source (AV)
6808            and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
6809          then
6810             return False;
6811
6812          elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
6813             return Is_OK_Variable_For_Out_Formal (Expression (AV));
6814
6815          else
6816             return True;
6817          end if;
6818
6819       --  Normal type conversions are allowed if argument is a variable
6820
6821       elsif Nkind (AV) = N_Type_Conversion then
6822          if Is_Variable (Expression (AV))
6823            and then Paren_Count (Expression (AV)) = 0
6824          then
6825             Note_Possible_Modification (Expression (AV), Sure => True);
6826             return True;
6827
6828          --  We also allow a non-parenthesized expression that raises
6829          --  constraint error if it rewrites what used to be a variable
6830
6831          elsif Raises_Constraint_Error (Expression (AV))
6832             and then Paren_Count (Expression (AV)) = 0
6833             and then Is_Variable (Original_Node (Expression (AV)))
6834          then
6835             return True;
6836
6837          --  Type conversion of something other than a variable
6838
6839          else
6840             return False;
6841          end if;
6842
6843       --  If this node is rewritten, then test the original form, if that is
6844       --  OK, then we consider the rewritten node OK (for example, if the
6845       --  original node is a conversion, then Is_Variable will not be true
6846       --  but we still want to allow the conversion if it converts a variable).
6847
6848       elsif Original_Node (AV) /= AV then
6849          return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
6850
6851       --  All other non-variables are rejected
6852
6853       else
6854          return False;
6855       end if;
6856    end Is_OK_Variable_For_Out_Formal;
6857
6858    -----------------------------------
6859    -- Is_Partially_Initialized_Type --
6860    -----------------------------------
6861
6862    function Is_Partially_Initialized_Type
6863      (Typ          : Entity_Id;
6864       Include_Null : Boolean := True) return Boolean
6865    is
6866    begin
6867       if Is_Scalar_Type (Typ) then
6868          return False;
6869
6870       elsif Is_Access_Type (Typ) then
6871          return Include_Null;
6872
6873       elsif Is_Array_Type (Typ) then
6874
6875          --  If component type is partially initialized, so is array type
6876
6877          if Is_Partially_Initialized_Type
6878               (Component_Type (Typ), Include_Null)
6879          then
6880             return True;
6881
6882          --  Otherwise we are only partially initialized if we are fully
6883          --  initialized (this is the empty array case, no point in us
6884          --  duplicating that code here).
6885
6886          else
6887             return Is_Fully_Initialized_Type (Typ);
6888          end if;
6889
6890       elsif Is_Record_Type (Typ) then
6891
6892          --  A discriminated type is always partially initialized
6893
6894          if Has_Discriminants (Typ) then
6895             return True;
6896
6897          --  A tagged type is always partially initialized
6898
6899          elsif Is_Tagged_Type (Typ) then
6900             return True;
6901
6902          --  Case of non-discriminated record
6903
6904          else
6905             declare
6906                Ent : Entity_Id;
6907
6908                Component_Present : Boolean := False;
6909                --  Set True if at least one component is present. If no
6910                --  components are present, then record type is fully
6911                --  initialized (another odd case, like the null array).
6912
6913             begin
6914                --  Loop through components
6915
6916                Ent := First_Entity (Typ);
6917                while Present (Ent) loop
6918                   if Ekind (Ent) = E_Component then
6919                      Component_Present := True;
6920
6921                      --  If a component has an initialization expression then
6922                      --  the enclosing record type is partially initialized
6923
6924                      if Present (Parent (Ent))
6925                        and then Present (Expression (Parent (Ent)))
6926                      then
6927                         return True;
6928
6929                      --  If a component is of a type which is itself partially
6930                      --  initialized, then the enclosing record type is also.
6931
6932                      elsif Is_Partially_Initialized_Type
6933                              (Etype (Ent), Include_Null)
6934                      then
6935                         return True;
6936                      end if;
6937                   end if;
6938
6939                   Next_Entity (Ent);
6940                end loop;
6941
6942                --  No initialized components found. If we found any components
6943                --  they were all uninitialized so the result is false.
6944
6945                if Component_Present then
6946                   return False;
6947
6948                --  But if we found no components, then all the components are
6949                --  initialized so we consider the type to be initialized.
6950
6951                else
6952                   return True;
6953                end if;
6954             end;
6955          end if;
6956
6957       --  Concurrent types are always fully initialized
6958
6959       elsif Is_Concurrent_Type (Typ) then
6960          return True;
6961
6962       --  For a private type, go to underlying type. If there is no underlying
6963       --  type then just assume this partially initialized. Not clear if this
6964       --  can happen in a non-error case, but no harm in testing for this.
6965
6966       elsif Is_Private_Type (Typ) then
6967          declare
6968             U : constant Entity_Id := Underlying_Type (Typ);
6969          begin
6970             if No (U) then
6971                return True;
6972             else
6973                return Is_Partially_Initialized_Type (U, Include_Null);
6974             end if;
6975          end;
6976
6977       --  For any other type (are there any?) assume partially initialized
6978
6979       else
6980          return True;
6981       end if;
6982    end Is_Partially_Initialized_Type;
6983
6984    ------------------------------------
6985    -- Is_Potentially_Persistent_Type --
6986    ------------------------------------
6987
6988    function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
6989       Comp : Entity_Id;
6990       Indx : Node_Id;
6991
6992    begin
6993       --  For private type, test corresponding full type
6994
6995       if Is_Private_Type (T) then
6996          return Is_Potentially_Persistent_Type (Full_View (T));
6997
6998       --  Scalar types are potentially persistent
6999
7000       elsif Is_Scalar_Type (T) then
7001          return True;
7002
7003       --  Record type is potentially persistent if not tagged and the types of
7004       --  all it components are potentially persistent, and no component has
7005       --  an initialization expression.
7006
7007       elsif Is_Record_Type (T)
7008         and then not Is_Tagged_Type (T)
7009         and then not Is_Partially_Initialized_Type (T)
7010       then
7011          Comp := First_Component (T);
7012          while Present (Comp) loop
7013             if not Is_Potentially_Persistent_Type (Etype (Comp)) then
7014                return False;
7015             else
7016                Next_Entity (Comp);
7017             end if;
7018          end loop;
7019
7020          return True;
7021
7022       --  Array type is potentially persistent if its component type is
7023       --  potentially persistent and if all its constraints are static.
7024
7025       elsif Is_Array_Type (T) then
7026          if not Is_Potentially_Persistent_Type (Component_Type (T)) then
7027             return False;
7028          end if;
7029
7030          Indx := First_Index (T);
7031          while Present (Indx) loop
7032             if not Is_OK_Static_Subtype (Etype (Indx)) then
7033                return False;
7034             else
7035                Next_Index (Indx);
7036             end if;
7037          end loop;
7038
7039          return True;
7040
7041       --  All other types are not potentially persistent
7042
7043       else
7044          return False;
7045       end if;
7046    end Is_Potentially_Persistent_Type;
7047
7048    ---------------------------------
7049    -- Is_Protected_Self_Reference --
7050    ---------------------------------
7051
7052    function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
7053
7054       function In_Access_Definition (N : Node_Id) return Boolean;
7055       --  Returns true if N belongs to an access definition
7056
7057       --------------------------
7058       -- In_Access_Definition --
7059       --------------------------
7060
7061       function In_Access_Definition (N : Node_Id) return Boolean is
7062          P : Node_Id;
7063
7064       begin
7065          P := Parent (N);
7066          while Present (P) loop
7067             if Nkind (P) = N_Access_Definition then
7068                return True;
7069             end if;
7070
7071             P := Parent (P);
7072          end loop;
7073
7074          return False;
7075       end In_Access_Definition;
7076
7077    --  Start of processing for Is_Protected_Self_Reference
7078
7079    begin
7080       --  Verify that prefix is analyzed and has the proper form. Note that
7081       --  the attributes Elab_Spec, Elab_Body, and UET_Address, which also
7082       --  produce the address of an entity, do not analyze their prefix
7083       --  because they denote entities that are not necessarily visible.
7084       --  Neither of them can apply to a protected type.
7085
7086       return Ada_Version >= Ada_2005
7087         and then Is_Entity_Name (N)
7088         and then Present (Entity (N))
7089         and then Is_Protected_Type (Entity (N))
7090         and then In_Open_Scopes (Entity (N))
7091         and then not In_Access_Definition (N);
7092    end Is_Protected_Self_Reference;
7093
7094    -----------------------------
7095    -- Is_RCI_Pkg_Spec_Or_Body --
7096    -----------------------------
7097
7098    function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
7099
7100       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
7101       --  Return True if the unit of Cunit is an RCI package declaration
7102
7103       ---------------------------
7104       -- Is_RCI_Pkg_Decl_Cunit --
7105       ---------------------------
7106
7107       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
7108          The_Unit : constant Node_Id := Unit (Cunit);
7109
7110       begin
7111          if Nkind (The_Unit) /= N_Package_Declaration then
7112             return False;
7113          end if;
7114
7115          return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
7116       end Is_RCI_Pkg_Decl_Cunit;
7117
7118    --  Start of processing for Is_RCI_Pkg_Spec_Or_Body
7119
7120    begin
7121       return Is_RCI_Pkg_Decl_Cunit (Cunit)
7122         or else
7123          (Nkind (Unit (Cunit)) = N_Package_Body
7124            and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
7125    end Is_RCI_Pkg_Spec_Or_Body;
7126
7127    -----------------------------------------
7128    -- Is_Remote_Access_To_Class_Wide_Type --
7129    -----------------------------------------
7130
7131    function Is_Remote_Access_To_Class_Wide_Type
7132      (E : Entity_Id) return Boolean
7133    is
7134    begin
7135       --  A remote access to class-wide type is a general access to object type
7136       --  declared in the visible part of a Remote_Types or Remote_Call_
7137       --  Interface unit.
7138
7139       return Ekind (E) = E_General_Access_Type
7140         and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
7141    end Is_Remote_Access_To_Class_Wide_Type;
7142
7143    -----------------------------------------
7144    -- Is_Remote_Access_To_Subprogram_Type --
7145    -----------------------------------------
7146
7147    function Is_Remote_Access_To_Subprogram_Type
7148      (E : Entity_Id) return Boolean
7149    is
7150    begin
7151       return (Ekind (E) = E_Access_Subprogram_Type
7152                 or else (Ekind (E) = E_Record_Type
7153                            and then Present (Corresponding_Remote_Type (E))))
7154         and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
7155    end Is_Remote_Access_To_Subprogram_Type;
7156
7157    --------------------
7158    -- Is_Remote_Call --
7159    --------------------
7160
7161    function Is_Remote_Call (N : Node_Id) return Boolean is
7162    begin
7163       if Nkind (N) /= N_Procedure_Call_Statement
7164         and then Nkind (N) /= N_Function_Call
7165       then
7166          --  An entry call cannot be remote
7167
7168          return False;
7169
7170       elsif Nkind (Name (N)) in N_Has_Entity
7171         and then Is_Remote_Call_Interface (Entity (Name (N)))
7172       then
7173          --  A subprogram declared in the spec of a RCI package is remote
7174
7175          return True;
7176
7177       elsif Nkind (Name (N)) = N_Explicit_Dereference
7178         and then Is_Remote_Access_To_Subprogram_Type
7179                    (Etype (Prefix (Name (N))))
7180       then
7181          --  The dereference of a RAS is a remote call
7182
7183          return True;
7184
7185       elsif Present (Controlling_Argument (N))
7186         and then Is_Remote_Access_To_Class_Wide_Type
7187           (Etype (Controlling_Argument (N)))
7188       then
7189          --  Any primitive operation call with a controlling argument of
7190          --  a RACW type is a remote call.
7191
7192          return True;
7193       end if;
7194
7195       --  All other calls are local calls
7196
7197       return False;
7198    end Is_Remote_Call;
7199
7200    ----------------------
7201    -- Is_Renamed_Entry --
7202    ----------------------
7203
7204    function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
7205       Orig_Node : Node_Id := Empty;
7206       Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
7207
7208       function Is_Entry (Nam : Node_Id) return Boolean;
7209       --  Determine whether Nam is an entry. Traverse selectors if there are
7210       --  nested selected components.
7211
7212       --------------
7213       -- Is_Entry --
7214       --------------
7215
7216       function Is_Entry (Nam : Node_Id) return Boolean is
7217       begin
7218          if Nkind (Nam) = N_Selected_Component then
7219             return Is_Entry (Selector_Name (Nam));
7220          end if;
7221
7222          return Ekind (Entity (Nam)) = E_Entry;
7223       end Is_Entry;
7224
7225    --  Start of processing for Is_Renamed_Entry
7226
7227    begin
7228       if Present (Alias (Proc_Nam)) then
7229          Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
7230       end if;
7231
7232       --  Look for a rewritten subprogram renaming declaration
7233
7234       if Nkind (Subp_Decl) = N_Subprogram_Declaration
7235         and then Present (Original_Node (Subp_Decl))
7236       then
7237          Orig_Node := Original_Node (Subp_Decl);
7238       end if;
7239
7240       --  The rewritten subprogram is actually an entry
7241
7242       if Present (Orig_Node)
7243         and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
7244         and then Is_Entry (Name (Orig_Node))
7245       then
7246          return True;
7247       end if;
7248
7249       return False;
7250    end Is_Renamed_Entry;
7251
7252    ----------------------
7253    -- Is_Selector_Name --
7254    ----------------------
7255
7256    function Is_Selector_Name (N : Node_Id) return Boolean is
7257    begin
7258       if not Is_List_Member (N) then
7259          declare
7260             P : constant Node_Id   := Parent (N);
7261             K : constant Node_Kind := Nkind (P);
7262          begin
7263             return
7264               (K = N_Expanded_Name          or else
7265                K = N_Generic_Association    or else
7266                K = N_Parameter_Association  or else
7267                K = N_Selected_Component)
7268               and then Selector_Name (P) = N;
7269          end;
7270
7271       else
7272          declare
7273             L : constant List_Id := List_Containing (N);
7274             P : constant Node_Id := Parent (L);
7275          begin
7276             return (Nkind (P) = N_Discriminant_Association
7277                      and then Selector_Names (P) = L)
7278               or else
7279                    (Nkind (P) = N_Component_Association
7280                      and then Choices (P) = L);
7281          end;
7282       end if;
7283    end Is_Selector_Name;
7284
7285    ------------------
7286    -- Is_Statement --
7287    ------------------
7288
7289    function Is_Statement (N : Node_Id) return Boolean is
7290    begin
7291       return
7292         Nkind (N) in N_Statement_Other_Than_Procedure_Call
7293           or else Nkind (N) = N_Procedure_Call_Statement;
7294    end Is_Statement;
7295
7296    ---------------------------------
7297    -- Is_Synchronized_Tagged_Type --
7298    ---------------------------------
7299
7300    function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
7301       Kind : constant Entity_Kind := Ekind (Base_Type (E));
7302
7303    begin
7304       --  A task or protected type derived from an interface is a tagged type.
7305       --  Such a tagged type is called a synchronized tagged type, as are
7306       --  synchronized interfaces and private extensions whose declaration
7307       --  includes the reserved word synchronized.
7308
7309       return (Is_Tagged_Type (E)
7310                 and then (Kind = E_Task_Type
7311                            or else Kind = E_Protected_Type))
7312             or else
7313              (Is_Interface (E)
7314                 and then Is_Synchronized_Interface (E))
7315             or else
7316              (Ekind (E) = E_Record_Type_With_Private
7317                 and then (Synchronized_Present (Parent (E))
7318                            or else Is_Synchronized_Interface (Etype (E))));
7319    end Is_Synchronized_Tagged_Type;
7320
7321    -----------------
7322    -- Is_Transfer --
7323    -----------------
7324
7325    function Is_Transfer (N : Node_Id) return Boolean is
7326       Kind : constant Node_Kind := Nkind (N);
7327
7328    begin
7329       if Kind = N_Simple_Return_Statement
7330            or else
7331          Kind = N_Extended_Return_Statement
7332            or else
7333          Kind = N_Goto_Statement
7334            or else
7335          Kind = N_Raise_Statement
7336            or else
7337          Kind = N_Requeue_Statement
7338       then
7339          return True;
7340
7341       elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
7342         and then No (Condition (N))
7343       then
7344          return True;
7345
7346       elsif Kind = N_Procedure_Call_Statement
7347         and then Is_Entity_Name (Name (N))
7348         and then Present (Entity (Name (N)))
7349         and then No_Return (Entity (Name (N)))
7350       then
7351          return True;
7352
7353       elsif Nkind (Original_Node (N)) = N_Raise_Statement then
7354          return True;
7355
7356       else
7357          return False;
7358       end if;
7359    end Is_Transfer;
7360
7361    -------------
7362    -- Is_True --
7363    -------------
7364
7365    function Is_True (U : Uint) return Boolean is
7366    begin
7367       return (U /= 0);
7368    end Is_True;
7369
7370    -------------------------------
7371    -- Is_Universal_Numeric_Type --
7372    -------------------------------
7373
7374    function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
7375    begin
7376       return T = Universal_Integer or else T = Universal_Real;
7377    end Is_Universal_Numeric_Type;
7378
7379    -------------------
7380    -- Is_Value_Type --
7381    -------------------
7382
7383    function Is_Value_Type (T : Entity_Id) return Boolean is
7384    begin
7385       return VM_Target = CLI_Target
7386         and then Nkind (T) in N_Has_Chars
7387         and then Chars (T) /= No_Name
7388         and then Get_Name_String (Chars (T)) = "valuetype";
7389    end Is_Value_Type;
7390
7391    ---------------------
7392    -- Is_VMS_Operator --
7393    ---------------------
7394
7395    function Is_VMS_Operator (Op : Entity_Id) return Boolean is
7396    begin
7397       --  The VMS operators are declared in a child of System that is loaded
7398       --  through pragma Extend_System. In some rare cases a program is run
7399       --  with this extension but without indicating that the target is VMS.
7400
7401       return Ekind (Op) = E_Function
7402         and then Is_Intrinsic_Subprogram (Op)
7403         and then
7404           ((Present_System_Aux
7405             and then Scope (Op) = System_Aux_Id)
7406            or else
7407            (True_VMS_Target
7408              and then Scope (Scope (Op)) = RTU_Entity (System)));
7409    end Is_VMS_Operator;
7410
7411    -----------------
7412    -- Is_Variable --
7413    -----------------
7414
7415    function Is_Variable (N : Node_Id) return Boolean is
7416
7417       Orig_Node : constant Node_Id := Original_Node (N);
7418       --  We do the test on the original node, since this is basically a test
7419       --  of syntactic categories, so it must not be disturbed by whatever
7420       --  rewriting might have occurred. For example, an aggregate, which is
7421       --  certainly NOT a variable, could be turned into a variable by
7422       --  expansion.
7423
7424       function In_Protected_Function (E : Entity_Id) return Boolean;
7425       --  Within a protected function, the private components of the enclosing
7426       --  protected type are constants. A function nested within a (protected)
7427       --  procedure is not itself protected.
7428
7429       function Is_Variable_Prefix (P : Node_Id) return Boolean;
7430       --  Prefixes can involve implicit dereferences, in which case we must
7431       --  test for the case of a reference of a constant access type, which can
7432       --  can never be a variable.
7433
7434       ---------------------------
7435       -- In_Protected_Function --
7436       ---------------------------
7437
7438       function In_Protected_Function (E : Entity_Id) return Boolean is
7439          Prot : constant Entity_Id := Scope (E);
7440          S    : Entity_Id;
7441
7442       begin
7443          if not Is_Protected_Type (Prot) then
7444             return False;
7445          else
7446             S := Current_Scope;
7447             while Present (S) and then S /= Prot loop
7448                if Ekind (S) = E_Function and then Scope (S) = Prot then
7449                   return True;
7450                end if;
7451
7452                S := Scope (S);
7453             end loop;
7454
7455             return False;
7456          end if;
7457       end In_Protected_Function;
7458
7459       ------------------------
7460       -- Is_Variable_Prefix --
7461       ------------------------
7462
7463       function Is_Variable_Prefix (P : Node_Id) return Boolean is
7464       begin
7465          if Is_Access_Type (Etype (P)) then
7466             return not Is_Access_Constant (Root_Type (Etype (P)));
7467
7468          --  For the case of an indexed component whose prefix has a packed
7469          --  array type, the prefix has been rewritten into a type conversion.
7470          --  Determine variable-ness from the converted expression.
7471
7472          elsif Nkind (P) = N_Type_Conversion
7473            and then not Comes_From_Source (P)
7474            and then Is_Array_Type (Etype (P))
7475            and then Is_Packed (Etype (P))
7476          then
7477             return Is_Variable (Expression (P));
7478
7479          else
7480             return Is_Variable (P);
7481          end if;
7482       end Is_Variable_Prefix;
7483
7484    --  Start of processing for Is_Variable
7485
7486    begin
7487       --  Definitely OK if Assignment_OK is set. Since this is something that
7488       --  only gets set for expanded nodes, the test is on N, not Orig_Node.
7489
7490       if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
7491          return True;
7492
7493       --  Normally we go to the original node, but there is one exception where
7494       --  we use the rewritten node, namely when it is an explicit dereference.
7495       --  The generated code may rewrite a prefix which is an access type with
7496       --  an explicit dereference. The dereference is a variable, even though
7497       --  the original node may not be (since it could be a constant of the
7498       --  access type).
7499
7500       --  In Ada 2005 we have a further case to consider: the prefix may be a
7501       --  function call given in prefix notation. The original node appears to
7502       --  be a selected component, but we need to examine the call.
7503
7504       elsif Nkind (N) = N_Explicit_Dereference
7505         and then Nkind (Orig_Node) /= N_Explicit_Dereference
7506         and then Present (Etype (Orig_Node))
7507         and then Is_Access_Type (Etype (Orig_Node))
7508       then
7509          --  Note that if the prefix is an explicit dereference that does not
7510          --  come from source, we must check for a rewritten function call in
7511          --  prefixed notation before other forms of rewriting, to prevent a
7512          --  compiler crash.
7513
7514          return
7515            (Nkind (Orig_Node) = N_Function_Call
7516              and then not Is_Access_Constant (Etype (Prefix (N))))
7517            or else
7518              Is_Variable_Prefix (Original_Node (Prefix (N)));
7519
7520       --  A function call is never a variable
7521
7522       elsif Nkind (N) = N_Function_Call then
7523          return False;
7524
7525       --  All remaining checks use the original node
7526
7527       elsif Is_Entity_Name (Orig_Node)
7528         and then Present (Entity (Orig_Node))
7529       then
7530          declare
7531             E : constant Entity_Id := Entity (Orig_Node);
7532             K : constant Entity_Kind := Ekind (E);
7533
7534          begin
7535             return (K = E_Variable
7536                       and then Nkind (Parent (E)) /= N_Exception_Handler)
7537               or else  (K = E_Component
7538                           and then not In_Protected_Function (E))
7539               or else  K = E_Out_Parameter
7540               or else  K = E_In_Out_Parameter
7541               or else  K = E_Generic_In_Out_Parameter
7542
7543                --  Current instance of type:
7544
7545               or else (Is_Type (E) and then In_Open_Scopes (E))
7546               or else (Is_Incomplete_Or_Private_Type (E)
7547                         and then In_Open_Scopes (Full_View (E)));
7548          end;
7549
7550       else
7551          case Nkind (Orig_Node) is
7552             when N_Indexed_Component | N_Slice =>
7553                return Is_Variable_Prefix (Prefix (Orig_Node));
7554
7555             when N_Selected_Component =>
7556                return Is_Variable_Prefix (Prefix (Orig_Node))
7557                  and then Is_Variable (Selector_Name (Orig_Node));
7558
7559             --  For an explicit dereference, the type of the prefix cannot
7560             --  be an access to constant or an access to subprogram.
7561
7562             when N_Explicit_Dereference =>
7563                declare
7564                   Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
7565                begin
7566                   return Is_Access_Type (Typ)
7567                     and then not Is_Access_Constant (Root_Type (Typ))
7568                     and then Ekind (Typ) /= E_Access_Subprogram_Type;
7569                end;
7570
7571             --  The type conversion is the case where we do not deal with the
7572             --  context dependent special case of an actual parameter. Thus
7573             --  the type conversion is only considered a variable for the
7574             --  purposes of this routine if the target type is tagged. However,
7575             --  a type conversion is considered to be a variable if it does not
7576             --  come from source (this deals for example with the conversions
7577             --  of expressions to their actual subtypes).
7578
7579             when N_Type_Conversion =>
7580                return Is_Variable (Expression (Orig_Node))
7581                  and then
7582                    (not Comes_From_Source (Orig_Node)
7583                       or else
7584                         (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
7585                           and then
7586                          Is_Tagged_Type (Etype (Expression (Orig_Node)))));
7587
7588             --  GNAT allows an unchecked type conversion as a variable. This
7589             --  only affects the generation of internal expanded code, since
7590             --  calls to instantiations of Unchecked_Conversion are never
7591             --  considered variables (since they are function calls).
7592             --  This is also true for expression actions.
7593
7594             when N_Unchecked_Type_Conversion =>
7595                return Is_Variable (Expression (Orig_Node));
7596
7597             when others =>
7598                return False;
7599          end case;
7600       end if;
7601    end Is_Variable;
7602
7603    ---------------------------
7604    -- Is_Visibly_Controlled --
7605    ---------------------------
7606
7607    function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
7608       Root : constant Entity_Id := Root_Type (T);
7609    begin
7610       return Chars (Scope (Root)) = Name_Finalization
7611         and then Chars (Scope (Scope (Root))) = Name_Ada
7612         and then Scope (Scope (Scope (Root))) = Standard_Standard;
7613    end Is_Visibly_Controlled;
7614
7615    ------------------------
7616    -- Is_Volatile_Object --
7617    ------------------------
7618
7619    function Is_Volatile_Object (N : Node_Id) return Boolean is
7620
7621       function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
7622       --  Determines if given object has volatile components
7623
7624       function Is_Volatile_Prefix (N : Node_Id) return Boolean;
7625       --  If prefix is an implicit dereference, examine designated type
7626
7627       ------------------------
7628       -- Is_Volatile_Prefix --
7629       ------------------------
7630
7631       function Is_Volatile_Prefix (N : Node_Id) return Boolean is
7632          Typ  : constant Entity_Id := Etype (N);
7633
7634       begin
7635          if Is_Access_Type (Typ) then
7636             declare
7637                Dtyp : constant Entity_Id := Designated_Type (Typ);
7638
7639             begin
7640                return Is_Volatile (Dtyp)
7641                  or else Has_Volatile_Components (Dtyp);
7642             end;
7643
7644          else
7645             return Object_Has_Volatile_Components (N);
7646          end if;
7647       end Is_Volatile_Prefix;
7648
7649       ------------------------------------
7650       -- Object_Has_Volatile_Components --
7651       ------------------------------------
7652
7653       function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
7654          Typ : constant Entity_Id := Etype (N);
7655
7656       begin
7657          if Is_Volatile (Typ)
7658            or else Has_Volatile_Components (Typ)
7659          then
7660             return True;
7661
7662          elsif Is_Entity_Name (N)
7663            and then (Has_Volatile_Components (Entity (N))
7664                       or else Is_Volatile (Entity (N)))
7665          then
7666             return True;
7667
7668          elsif Nkind (N) = N_Indexed_Component
7669            or else Nkind (N) = N_Selected_Component
7670          then
7671             return Is_Volatile_Prefix (Prefix (N));
7672
7673          else
7674             return False;
7675          end if;
7676       end Object_Has_Volatile_Components;
7677
7678    --  Start of processing for Is_Volatile_Object
7679
7680    begin
7681       if Is_Volatile (Etype (N))
7682         or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
7683       then
7684          return True;
7685
7686       elsif Nkind (N) = N_Indexed_Component
7687         or else Nkind (N) = N_Selected_Component
7688       then
7689          return Is_Volatile_Prefix (Prefix (N));
7690
7691       else
7692          return False;
7693       end if;
7694    end Is_Volatile_Object;
7695
7696    -------------------------
7697    -- Kill_Current_Values --
7698    -------------------------
7699
7700    procedure Kill_Current_Values
7701      (Ent                  : Entity_Id;
7702       Last_Assignment_Only : Boolean := False)
7703    is
7704    begin
7705       --  ??? do we have to worry about clearing cached checks?
7706
7707       if Is_Assignable (Ent) then
7708          Set_Last_Assignment (Ent, Empty);
7709       end if;
7710
7711       if Is_Object (Ent) then
7712          if not Last_Assignment_Only then
7713             Kill_Checks (Ent);
7714             Set_Current_Value (Ent, Empty);
7715
7716             if not Can_Never_Be_Null (Ent) then
7717                Set_Is_Known_Non_Null (Ent, False);
7718             end if;
7719
7720             Set_Is_Known_Null (Ent, False);
7721
7722             --  Reset Is_Known_Valid unless type is always valid, or if we have
7723             --  a loop parameter (loop parameters are always valid, since their
7724             --  bounds are defined by the bounds given in the loop header).
7725
7726             if not Is_Known_Valid (Etype (Ent))
7727               and then Ekind (Ent) /= E_Loop_Parameter
7728             then
7729                Set_Is_Known_Valid (Ent, False);
7730             end if;
7731          end if;
7732       end if;
7733    end Kill_Current_Values;
7734
7735    procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
7736       S : Entity_Id;
7737
7738       procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
7739       --  Clear current value for entity E and all entities chained to E
7740
7741       ------------------------------------------
7742       -- Kill_Current_Values_For_Entity_Chain --
7743       ------------------------------------------
7744
7745       procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
7746          Ent : Entity_Id;
7747       begin
7748          Ent := E;
7749          while Present (Ent) loop
7750             Kill_Current_Values (Ent, Last_Assignment_Only);
7751             Next_Entity (Ent);
7752          end loop;
7753       end Kill_Current_Values_For_Entity_Chain;
7754
7755    --  Start of processing for Kill_Current_Values
7756
7757    begin
7758       --  Kill all saved checks, a special case of killing saved values
7759
7760       if not Last_Assignment_Only then
7761          Kill_All_Checks;
7762       end if;
7763
7764       --  Loop through relevant scopes, which includes the current scope and
7765       --  any parent scopes if the current scope is a block or a package.
7766
7767       S := Current_Scope;
7768       Scope_Loop : loop
7769
7770          --  Clear current values of all entities in current scope
7771
7772          Kill_Current_Values_For_Entity_Chain (First_Entity (S));
7773
7774          --  If scope is a package, also clear current values of all
7775          --  private entities in the scope.
7776
7777          if Is_Package_Or_Generic_Package (S)
7778            or else Is_Concurrent_Type (S)
7779          then
7780             Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
7781          end if;
7782
7783          --  If this is a not a subprogram, deal with parents
7784
7785          if not Is_Subprogram (S) then
7786             S := Scope (S);
7787             exit Scope_Loop when S = Standard_Standard;
7788          else
7789             exit Scope_Loop;
7790          end if;
7791       end loop Scope_Loop;
7792    end Kill_Current_Values;
7793
7794    --------------------------
7795    -- Kill_Size_Check_Code --
7796    --------------------------
7797
7798    procedure Kill_Size_Check_Code (E : Entity_Id) is
7799    begin
7800       if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
7801         and then Present (Size_Check_Code (E))
7802       then
7803          Remove (Size_Check_Code (E));
7804          Set_Size_Check_Code (E, Empty);
7805       end if;
7806    end Kill_Size_Check_Code;
7807
7808    --------------------------
7809    -- Known_To_Be_Assigned --
7810    --------------------------
7811
7812    function Known_To_Be_Assigned (N : Node_Id) return Boolean is
7813       P : constant Node_Id := Parent (N);
7814
7815    begin
7816       case Nkind (P) is
7817
7818          --  Test left side of assignment
7819
7820          when N_Assignment_Statement =>
7821             return N = Name (P);
7822
7823             --  Function call arguments are never lvalues
7824
7825          when N_Function_Call =>
7826             return False;
7827
7828          --  Positional parameter for procedure or accept call
7829
7830          when N_Procedure_Call_Statement |
7831               N_Accept_Statement
7832           =>
7833             declare
7834                Proc : Entity_Id;
7835                Form : Entity_Id;
7836                Act  : Node_Id;
7837
7838             begin
7839                Proc := Get_Subprogram_Entity (P);
7840
7841                if No (Proc) then
7842                   return False;
7843                end if;
7844
7845                --  If we are not a list member, something is strange, so
7846                --  be conservative and return False.
7847
7848                if not Is_List_Member (N) then
7849                   return False;
7850                end if;
7851
7852                --  We are going to find the right formal by stepping forward
7853                --  through the formals, as we step backwards in the actuals.
7854
7855                Form := First_Formal (Proc);
7856                Act  := N;
7857                loop
7858                   --  If no formal, something is weird, so be conservative
7859                   --  and return False.
7860
7861                   if No (Form) then
7862                      return False;
7863                   end if;
7864
7865                   Prev (Act);
7866                   exit when No (Act);
7867                   Next_Formal (Form);
7868                end loop;
7869
7870                return Ekind (Form) /= E_In_Parameter;
7871             end;
7872
7873          --  Named parameter for procedure or accept call
7874
7875          when N_Parameter_Association =>
7876             declare
7877                Proc : Entity_Id;
7878                Form : Entity_Id;
7879
7880             begin
7881                Proc := Get_Subprogram_Entity (Parent (P));
7882
7883                if No (Proc) then
7884                   return False;
7885                end if;
7886
7887                --  Loop through formals to find the one that matches
7888
7889                Form := First_Formal (Proc);
7890                loop
7891                   --  If no matching formal, that's peculiar, some kind of
7892                   --  previous error, so return False to be conservative.
7893
7894                   if No (Form) then
7895                      return False;
7896                   end if;
7897
7898                   --  Else test for match
7899
7900                   if Chars (Form) = Chars (Selector_Name (P)) then
7901                      return Ekind (Form) /= E_In_Parameter;
7902                   end if;
7903
7904                   Next_Formal (Form);
7905                end loop;
7906             end;
7907
7908          --  Test for appearing in a conversion that itself appears
7909          --  in an lvalue context, since this should be an lvalue.
7910
7911          when N_Type_Conversion =>
7912             return Known_To_Be_Assigned (P);
7913
7914          --  All other references are definitely not known to be modifications
7915
7916          when others =>
7917             return False;
7918
7919       end case;
7920    end Known_To_Be_Assigned;
7921
7922    -------------------
7923    -- May_Be_Lvalue --
7924    -------------------
7925
7926    function May_Be_Lvalue (N : Node_Id) return Boolean is
7927       P : constant Node_Id := Parent (N);
7928
7929    begin
7930       case Nkind (P) is
7931
7932          --  Test left side of assignment
7933
7934          when N_Assignment_Statement =>
7935             return N = Name (P);
7936
7937          --  Test prefix of component or attribute. Note that the prefix of an
7938          --  explicit or implicit dereference cannot be an l-value.
7939
7940          when N_Attribute_Reference =>
7941             return N = Prefix (P)
7942               and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
7943
7944          --  For an expanded name, the name is an lvalue if the expanded name
7945          --  is an lvalue, but the prefix is never an lvalue, since it is just
7946          --  the scope where the name is found.
7947
7948          when N_Expanded_Name        =>
7949             if N = Prefix (P) then
7950                return May_Be_Lvalue (P);
7951             else
7952                return False;
7953             end if;
7954
7955          --  For a selected component A.B, A is certainly an lvalue if A.B is.
7956          --  B is a little interesting, if we have A.B := 3, there is some
7957          --  discussion as to whether B is an lvalue or not, we choose to say
7958          --  it is. Note however that A is not an lvalue if it is of an access
7959          --  type since this is an implicit dereference.
7960
7961          when N_Selected_Component   =>
7962             if N = Prefix (P)
7963               and then Present (Etype (N))
7964               and then Is_Access_Type (Etype (N))
7965             then
7966                return False;
7967             else
7968                return May_Be_Lvalue (P);
7969             end if;
7970
7971          --  For an indexed component or slice, the index or slice bounds is
7972          --  never an lvalue. The prefix is an lvalue if the indexed component
7973          --  or slice is an lvalue, except if it is an access type, where we
7974          --  have an implicit dereference.
7975
7976          when N_Indexed_Component    =>
7977             if N /= Prefix (P)
7978               or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
7979             then
7980                return False;
7981             else
7982                return May_Be_Lvalue (P);
7983             end if;
7984
7985          --  Prefix of a reference is an lvalue if the reference is an lvalue
7986
7987          when N_Reference            =>
7988             return May_Be_Lvalue (P);
7989
7990          --  Prefix of explicit dereference is never an lvalue
7991
7992          when N_Explicit_Dereference =>
7993             return False;
7994
7995          --  Positional parameter for subprogram, entry, or accept call.
7996          --  In older versions of Ada function call arguments are never
7997          --  lvalues. In Ada 2012 functions can have in-out parameters.
7998
7999          when N_Function_Call            |
8000               N_Procedure_Call_Statement |
8001               N_Entry_Call_Statement     |
8002               N_Accept_Statement
8003          =>
8004             if Nkind (P) = N_Function_Call
8005               and then Ada_Version < Ada_2012
8006             then
8007                return False;
8008             end if;
8009
8010             --  The following mechanism is clumsy and fragile. A single
8011             --  flag set in Resolve_Actuals would be preferable ???
8012
8013             declare
8014                Proc : Entity_Id;
8015                Form : Entity_Id;
8016                Act  : Node_Id;
8017
8018             begin
8019                Proc := Get_Subprogram_Entity (P);
8020
8021                if No (Proc) then
8022                   return True;
8023                end if;
8024
8025                --  If we are not a list member, something is strange, so
8026                --  be conservative and return True.
8027
8028                if not Is_List_Member (N) then
8029                   return True;
8030                end if;
8031
8032                --  We are going to find the right formal by stepping forward
8033                --  through the formals, as we step backwards in the actuals.
8034
8035                Form := First_Formal (Proc);
8036                Act  := N;
8037                loop
8038                   --  If no formal, something is weird, so be conservative
8039                   --  and return True.
8040
8041                   if No (Form) then
8042                      return True;
8043                   end if;
8044
8045                   Prev (Act);
8046                   exit when No (Act);
8047                   Next_Formal (Form);
8048                end loop;
8049
8050                return Ekind (Form) /= E_In_Parameter;
8051             end;
8052
8053          --  Named parameter for procedure or accept call
8054
8055          when N_Parameter_Association =>
8056             declare
8057                Proc : Entity_Id;
8058                Form : Entity_Id;
8059
8060             begin
8061                Proc := Get_Subprogram_Entity (Parent (P));
8062
8063                if No (Proc) then
8064                   return True;
8065                end if;
8066
8067                --  Loop through formals to find the one that matches
8068
8069                Form := First_Formal (Proc);
8070                loop
8071                   --  If no matching formal, that's peculiar, some kind of
8072                   --  previous error, so return True to be conservative.
8073
8074                   if No (Form) then
8075                      return True;
8076                   end if;
8077
8078                   --  Else test for match
8079
8080                   if Chars (Form) = Chars (Selector_Name (P)) then
8081                      return Ekind (Form) /= E_In_Parameter;
8082                   end if;
8083
8084                   Next_Formal (Form);
8085                end loop;
8086             end;
8087
8088          --  Test for appearing in a conversion that itself appears in an
8089          --  lvalue context, since this should be an lvalue.
8090
8091          when N_Type_Conversion =>
8092             return May_Be_Lvalue (P);
8093
8094          --  Test for appearance in object renaming declaration
8095
8096          when N_Object_Renaming_Declaration =>
8097             return True;
8098
8099          --  All other references are definitely not lvalues
8100
8101          when others =>
8102             return False;
8103
8104       end case;
8105    end May_Be_Lvalue;
8106
8107    -----------------------
8108    -- Mark_Coextensions --
8109    -----------------------
8110
8111    procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
8112       Is_Dynamic : Boolean;
8113       --  Indicates whether the context causes nested coextensions to be
8114       --  dynamic or static
8115
8116       function Mark_Allocator (N : Node_Id) return Traverse_Result;
8117       --  Recognize an allocator node and label it as a dynamic coextension
8118
8119       --------------------
8120       -- Mark_Allocator --
8121       --------------------
8122
8123       function Mark_Allocator (N : Node_Id) return Traverse_Result is
8124       begin
8125          if Nkind (N) = N_Allocator then
8126             if Is_Dynamic then
8127                Set_Is_Dynamic_Coextension (N);
8128
8129             --  If the allocator expression is potentially dynamic, it may
8130             --  be expanded out of order and require dynamic allocation
8131             --  anyway, so we treat the coextension itself as dynamic.
8132             --  Potential optimization ???
8133
8134             elsif Nkind (Expression (N)) = N_Qualified_Expression
8135               and then Nkind (Expression (Expression (N))) = N_Op_Concat
8136             then
8137                Set_Is_Dynamic_Coextension (N);
8138
8139             else
8140                Set_Is_Static_Coextension (N);
8141             end if;
8142          end if;
8143
8144          return OK;
8145       end Mark_Allocator;
8146
8147       procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
8148
8149    --  Start of processing Mark_Coextensions
8150
8151    begin
8152       case Nkind (Context_Nod) is
8153          when N_Assignment_Statement    |
8154               N_Simple_Return_Statement =>
8155             Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
8156
8157          when N_Object_Declaration =>
8158             Is_Dynamic := Nkind (Root_Nod) = N_Allocator;
8159
8160          --  This routine should not be called for constructs which may not
8161          --  contain coextensions.
8162
8163          when others =>
8164             raise Program_Error;
8165       end case;
8166
8167       Mark_Allocators (Root_Nod);
8168    end Mark_Coextensions;
8169
8170    ----------------------
8171    -- Needs_One_Actual --
8172    ----------------------
8173
8174    function Needs_One_Actual (E : Entity_Id) return Boolean is
8175       Formal : Entity_Id;
8176
8177    begin
8178       if Ada_Version >= Ada_2005
8179         and then Present (First_Formal (E))
8180       then
8181          Formal := Next_Formal (First_Formal (E));
8182          while Present (Formal) loop
8183             if No (Default_Value (Formal)) then
8184                return False;
8185             end if;
8186
8187             Next_Formal (Formal);
8188          end loop;
8189
8190          return True;
8191
8192       else
8193          return False;
8194       end if;
8195    end Needs_One_Actual;
8196
8197    ------------------------
8198    -- New_Copy_List_Tree --
8199    ------------------------
8200
8201    function New_Copy_List_Tree (List : List_Id) return List_Id is
8202       NL : List_Id;
8203       E  : Node_Id;
8204
8205    begin
8206       if List = No_List then
8207          return No_List;
8208
8209       else
8210          NL := New_List;
8211          E := First (List);
8212
8213          while Present (E) loop
8214             Append (New_Copy_Tree (E), NL);
8215             E := Next (E);
8216          end loop;
8217
8218          return NL;
8219       end if;
8220    end New_Copy_List_Tree;
8221
8222    -------------------
8223    -- New_Copy_Tree --
8224    -------------------
8225
8226    use Atree.Unchecked_Access;
8227    use Atree_Private_Part;
8228
8229    --  Our approach here requires a two pass traversal of the tree. The
8230    --  first pass visits all nodes that eventually will be copied looking
8231    --  for defining Itypes. If any defining Itypes are found, then they are
8232    --  copied, and an entry is added to the replacement map. In the second
8233    --  phase, the tree is copied, using the replacement map to replace any
8234    --  Itype references within the copied tree.
8235
8236    --  The following hash tables are used if the Map supplied has more
8237    --  than hash threshhold entries to speed up access to the map. If
8238    --  there are fewer entries, then the map is searched sequentially
8239    --  (because setting up a hash table for only a few entries takes
8240    --  more time than it saves.
8241
8242    function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
8243    --  Hash function used for hash operations
8244
8245    -------------------
8246    -- New_Copy_Hash --
8247    -------------------
8248
8249    function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
8250    begin
8251       return Nat (E) mod (NCT_Header_Num'Last + 1);
8252    end New_Copy_Hash;
8253
8254    ---------------
8255    -- NCT_Assoc --
8256    ---------------
8257
8258    --  The hash table NCT_Assoc associates old entities in the table
8259    --  with their corresponding new entities (i.e. the pairs of entries
8260    --  presented in the original Map argument are Key-Element pairs).
8261
8262    package NCT_Assoc is new Simple_HTable (
8263      Header_Num => NCT_Header_Num,
8264      Element    => Entity_Id,
8265      No_Element => Empty,
8266      Key        => Entity_Id,
8267      Hash       => New_Copy_Hash,
8268      Equal      => Types."=");
8269
8270    ---------------------
8271    -- NCT_Itype_Assoc --
8272    ---------------------
8273
8274    --  The hash table NCT_Itype_Assoc contains entries only for those
8275    --  old nodes which have a non-empty Associated_Node_For_Itype set.
8276    --  The key is the associated node, and the element is the new node
8277    --  itself (NOT the associated node for the new node).
8278
8279    package NCT_Itype_Assoc is new Simple_HTable (
8280      Header_Num => NCT_Header_Num,
8281      Element    => Entity_Id,
8282      No_Element => Empty,
8283      Key        => Entity_Id,
8284      Hash       => New_Copy_Hash,
8285      Equal      => Types."=");
8286
8287    --  Start of processing for New_Copy_Tree function
8288
8289    function New_Copy_Tree
8290      (Source    : Node_Id;
8291       Map       : Elist_Id := No_Elist;
8292       New_Sloc  : Source_Ptr := No_Location;
8293       New_Scope : Entity_Id := Empty) return Node_Id
8294    is
8295       Actual_Map : Elist_Id := Map;
8296       --  This is the actual map for the copy. It is initialized with the
8297       --  given elements, and then enlarged as required for Itypes that are
8298       --  copied during the first phase of the copy operation. The visit
8299       --  procedures add elements to this map as Itypes are encountered.
8300       --  The reason we cannot use Map directly, is that it may well be
8301       --  (and normally is) initialized to No_Elist, and if we have mapped
8302       --  entities, we have to reset it to point to a real Elist.
8303
8304       function Assoc (N : Node_Or_Entity_Id) return Node_Id;
8305       --  Called during second phase to map entities into their corresponding
8306       --  copies using Actual_Map. If the argument is not an entity, or is not
8307       --  in Actual_Map, then it is returned unchanged.
8308
8309       procedure Build_NCT_Hash_Tables;
8310       --  Builds hash tables (number of elements >= threshold value)
8311
8312       function Copy_Elist_With_Replacement
8313         (Old_Elist : Elist_Id) return Elist_Id;
8314       --  Called during second phase to copy element list doing replacements
8315
8316       procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
8317       --  Called during the second phase to process a copied Itype. The actual
8318       --  copy happened during the first phase (so that we could make the entry
8319       --  in the mapping), but we still have to deal with the descendents of
8320       --  the copied Itype and copy them where necessary.
8321
8322       function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
8323       --  Called during second phase to copy list doing replacements
8324
8325       function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
8326       --  Called during second phase to copy node doing replacements
8327
8328       procedure Visit_Elist (E : Elist_Id);
8329       --  Called during first phase to visit all elements of an Elist
8330
8331       procedure Visit_Field (F : Union_Id; N : Node_Id);
8332       --  Visit a single field, recursing to call Visit_Node or Visit_List
8333       --  if the field is a syntactic descendent of the current node (i.e.
8334       --  its parent is Node N).
8335
8336       procedure Visit_Itype (Old_Itype : Entity_Id);
8337       --  Called during first phase to visit subsidiary fields of a defining
8338       --  Itype, and also create a copy and make an entry in the replacement
8339       --  map for the new copy.
8340
8341       procedure Visit_List (L : List_Id);
8342       --  Called during first phase to visit all elements of a List
8343
8344       procedure Visit_Node (N : Node_Or_Entity_Id);
8345       --  Called during first phase to visit a node and all its subtrees
8346
8347       -----------
8348       -- Assoc --
8349       -----------
8350
8351       function Assoc (N : Node_Or_Entity_Id) return Node_Id is
8352          E   : Elmt_Id;
8353          Ent : Entity_Id;
8354
8355       begin
8356          if not Has_Extension (N) or else No (Actual_Map) then
8357             return N;
8358
8359          elsif NCT_Hash_Tables_Used then
8360             Ent := NCT_Assoc.Get (Entity_Id (N));
8361
8362             if Present (Ent) then
8363                return Ent;
8364             else
8365                return N;
8366             end if;
8367
8368          --  No hash table used, do serial search
8369
8370          else
8371             E := First_Elmt (Actual_Map);
8372             while Present (E) loop
8373                if Node (E) = N then
8374                   return Node (Next_Elmt (E));
8375                else
8376                   E := Next_Elmt (Next_Elmt (E));
8377                end if;
8378             end loop;
8379          end if;
8380
8381          return N;
8382       end Assoc;
8383
8384       ---------------------------
8385       -- Build_NCT_Hash_Tables --
8386       ---------------------------
8387
8388       procedure Build_NCT_Hash_Tables is
8389          Elmt : Elmt_Id;
8390          Ent  : Entity_Id;
8391       begin
8392          if NCT_Hash_Table_Setup then
8393             NCT_Assoc.Reset;
8394             NCT_Itype_Assoc.Reset;
8395          end if;
8396
8397          Elmt := First_Elmt (Actual_Map);
8398          while Present (Elmt) loop
8399             Ent := Node (Elmt);
8400
8401             --  Get new entity, and associate old and new
8402
8403             Next_Elmt (Elmt);
8404             NCT_Assoc.Set (Ent, Node (Elmt));
8405
8406             if Is_Type (Ent) then
8407                declare
8408                   Anode : constant Entity_Id :=
8409                             Associated_Node_For_Itype (Ent);
8410
8411                begin
8412                   if Present (Anode) then
8413
8414                      --  Enter a link between the associated node of the
8415                      --  old Itype and the new Itype, for updating later
8416                      --  when node is copied.
8417
8418                      NCT_Itype_Assoc.Set (Anode, Node (Elmt));
8419                   end if;
8420                end;
8421             end if;
8422
8423             Next_Elmt (Elmt);
8424          end loop;
8425
8426          NCT_Hash_Tables_Used := True;
8427          NCT_Hash_Table_Setup := True;
8428       end Build_NCT_Hash_Tables;
8429
8430       ---------------------------------
8431       -- Copy_Elist_With_Replacement --
8432       ---------------------------------
8433
8434       function Copy_Elist_With_Replacement
8435         (Old_Elist : Elist_Id) return Elist_Id
8436       is
8437          M         : Elmt_Id;
8438          New_Elist : Elist_Id;
8439
8440       begin
8441          if No (Old_Elist) then
8442             return No_Elist;
8443
8444          else
8445             New_Elist := New_Elmt_List;
8446
8447             M := First_Elmt (Old_Elist);
8448             while Present (M) loop
8449                Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
8450                Next_Elmt (M);
8451             end loop;
8452          end if;
8453
8454          return New_Elist;
8455       end Copy_Elist_With_Replacement;
8456
8457       ---------------------------------
8458       -- Copy_Itype_With_Replacement --
8459       ---------------------------------
8460
8461       --  This routine exactly parallels its phase one analog Visit_Itype,
8462
8463       procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
8464       begin
8465          --  Translate Next_Entity, Scope and Etype fields, in case they
8466          --  reference entities that have been mapped into copies.
8467
8468          Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
8469          Set_Etype       (New_Itype, Assoc (Etype       (New_Itype)));
8470
8471          if Present (New_Scope) then
8472             Set_Scope    (New_Itype, New_Scope);
8473          else
8474             Set_Scope    (New_Itype, Assoc (Scope       (New_Itype)));
8475          end if;
8476
8477          --  Copy referenced fields
8478
8479          if Is_Discrete_Type (New_Itype) then
8480             Set_Scalar_Range (New_Itype,
8481               Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
8482
8483          elsif Has_Discriminants (Base_Type (New_Itype)) then
8484             Set_Discriminant_Constraint (New_Itype,
8485               Copy_Elist_With_Replacement
8486                 (Discriminant_Constraint (New_Itype)));
8487
8488          elsif Is_Array_Type (New_Itype) then
8489             if Present (First_Index (New_Itype)) then
8490                Set_First_Index (New_Itype,
8491                  First (Copy_List_With_Replacement
8492                          (List_Containing (First_Index (New_Itype)))));
8493             end if;
8494
8495             if Is_Packed (New_Itype) then
8496                Set_Packed_Array_Type (New_Itype,
8497                  Copy_Node_With_Replacement
8498                    (Packed_Array_Type (New_Itype)));
8499             end if;
8500          end if;
8501       end Copy_Itype_With_Replacement;
8502
8503       --------------------------------
8504       -- Copy_List_With_Replacement --
8505       --------------------------------
8506
8507       function Copy_List_With_Replacement
8508         (Old_List : List_Id) return List_Id
8509       is
8510          New_List : List_Id;
8511          E        : Node_Id;
8512
8513       begin
8514          if Old_List = No_List then
8515             return No_List;
8516
8517          else
8518             New_List := Empty_List;
8519
8520             E := First (Old_List);
8521             while Present (E) loop
8522                Append (Copy_Node_With_Replacement (E), New_List);
8523                Next (E);
8524             end loop;
8525
8526             return New_List;
8527          end if;
8528       end Copy_List_With_Replacement;
8529
8530       --------------------------------
8531       -- Copy_Node_With_Replacement --
8532       --------------------------------
8533
8534       function Copy_Node_With_Replacement
8535         (Old_Node : Node_Id) return Node_Id
8536       is
8537          New_Node : Node_Id;
8538
8539          procedure Adjust_Named_Associations
8540            (Old_Node : Node_Id;
8541             New_Node : Node_Id);
8542          --  If a call node has named associations, these are chained through
8543          --  the First_Named_Actual, Next_Named_Actual links. These must be
8544          --  propagated separately to the new parameter list, because these
8545          --  are not syntactic fields.
8546
8547          function Copy_Field_With_Replacement
8548            (Field : Union_Id) return Union_Id;
8549          --  Given Field, which is a field of Old_Node, return a copy of it
8550          --  if it is a syntactic field (i.e. its parent is Node), setting
8551          --  the parent of the copy to poit to New_Node. Otherwise returns
8552          --  the field (possibly mapped if it is an entity).
8553
8554          -------------------------------
8555          -- Adjust_Named_Associations --
8556          -------------------------------
8557
8558          procedure Adjust_Named_Associations
8559            (Old_Node : Node_Id;
8560             New_Node : Node_Id)
8561          is
8562             Old_E : Node_Id;
8563             New_E : Node_Id;
8564
8565             Old_Next : Node_Id;
8566             New_Next : Node_Id;
8567
8568          begin
8569             Old_E := First (Parameter_Associations (Old_Node));
8570             New_E := First (Parameter_Associations (New_Node));
8571             while Present (Old_E) loop
8572                if Nkind (Old_E) = N_Parameter_Association
8573                  and then Present (Next_Named_Actual (Old_E))
8574                then
8575                   if First_Named_Actual (Old_Node)
8576                     =  Explicit_Actual_Parameter (Old_E)
8577                   then
8578                      Set_First_Named_Actual
8579                        (New_Node, Explicit_Actual_Parameter (New_E));
8580                   end if;
8581
8582                   --  Now scan parameter list from the beginning,to locate
8583                   --  next named actual, which can be out of order.
8584
8585                   Old_Next := First (Parameter_Associations (Old_Node));
8586                   New_Next := First (Parameter_Associations (New_Node));
8587
8588                   while Nkind (Old_Next) /= N_Parameter_Association
8589                     or else  Explicit_Actual_Parameter (Old_Next)
8590                       /= Next_Named_Actual (Old_E)
8591                   loop
8592                      Next (Old_Next);
8593                      Next (New_Next);
8594                   end loop;
8595
8596                   Set_Next_Named_Actual
8597                     (New_E, Explicit_Actual_Parameter (New_Next));
8598                end if;
8599
8600                Next (Old_E);
8601                Next (New_E);
8602             end loop;
8603          end Adjust_Named_Associations;
8604
8605          ---------------------------------
8606          -- Copy_Field_With_Replacement --
8607          ---------------------------------
8608
8609          function Copy_Field_With_Replacement
8610            (Field : Union_Id) return Union_Id
8611          is
8612          begin
8613             if Field = Union_Id (Empty) then
8614                return Field;
8615
8616             elsif Field in Node_Range then
8617                declare
8618                   Old_N : constant Node_Id := Node_Id (Field);
8619                   New_N : Node_Id;
8620
8621                begin
8622                   --  If syntactic field, as indicated by the parent pointer
8623                   --  being set, then copy the referenced node recursively.
8624
8625                   if Parent (Old_N) = Old_Node then
8626                      New_N := Copy_Node_With_Replacement (Old_N);
8627
8628                      if New_N /= Old_N then
8629                         Set_Parent (New_N, New_Node);
8630                      end if;
8631
8632                   --  For semantic fields, update possible entity reference
8633                   --  from the replacement map.
8634
8635                   else
8636                      New_N := Assoc (Old_N);
8637                   end if;
8638
8639                   return Union_Id (New_N);
8640                end;
8641
8642             elsif Field in List_Range then
8643                declare
8644                   Old_L : constant List_Id := List_Id (Field);
8645                   New_L : List_Id;
8646
8647                begin
8648                   --  If syntactic field, as indicated by the parent pointer,
8649                   --  then recursively copy the entire referenced list.
8650
8651                   if Parent (Old_L) = Old_Node then
8652                      New_L := Copy_List_With_Replacement (Old_L);
8653                      Set_Parent (New_L, New_Node);
8654
8655                   --  For semantic list, just returned unchanged
8656
8657                   else
8658                      New_L := Old_L;
8659                   end if;
8660
8661                   return Union_Id (New_L);
8662                end;
8663
8664             --  Anything other than a list or a node is returned unchanged
8665
8666             else
8667                return Field;
8668             end if;
8669          end Copy_Field_With_Replacement;
8670
8671       --  Start of processing for Copy_Node_With_Replacement
8672
8673       begin
8674          if Old_Node <= Empty_Or_Error then
8675             return Old_Node;
8676
8677          elsif Has_Extension (Old_Node) then
8678             return Assoc (Old_Node);
8679
8680          else
8681             New_Node := New_Copy (Old_Node);
8682
8683             --  If the node we are copying is the associated node of a
8684             --  previously copied Itype, then adjust the associated node
8685             --  of the copy of that Itype accordingly.
8686
8687             if Present (Actual_Map) then
8688                declare
8689                   E   : Elmt_Id;
8690                   Ent : Entity_Id;
8691
8692                begin
8693                   --  Case of hash table used
8694
8695                   if NCT_Hash_Tables_Used then
8696                      Ent := NCT_Itype_Assoc.Get (Old_Node);
8697
8698                      if Present (Ent) then
8699                         Set_Associated_Node_For_Itype (Ent, New_Node);
8700                      end if;
8701
8702                   --  Case of no hash table used
8703
8704                   else
8705                      E := First_Elmt (Actual_Map);
8706                      while Present (E) loop
8707                         if Is_Itype (Node (E))
8708                           and then
8709                             Old_Node = Associated_Node_For_Itype (Node (E))
8710                         then
8711                            Set_Associated_Node_For_Itype
8712                              (Node (Next_Elmt (E)), New_Node);
8713                         end if;
8714
8715                         E := Next_Elmt (Next_Elmt (E));
8716                      end loop;
8717                   end if;
8718                end;
8719             end if;
8720
8721             --  Recursively copy descendents
8722
8723             Set_Field1
8724               (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
8725             Set_Field2
8726               (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
8727             Set_Field3
8728               (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
8729             Set_Field4
8730               (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
8731             Set_Field5
8732               (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
8733
8734             --  Adjust Sloc of new node if necessary
8735
8736             if New_Sloc /= No_Location then
8737                Set_Sloc (New_Node, New_Sloc);
8738
8739                --  If we adjust the Sloc, then we are essentially making
8740                --  a completely new node, so the Comes_From_Source flag
8741                --  should be reset to the proper default value.
8742
8743                Nodes.Table (New_Node).Comes_From_Source :=
8744                  Default_Node.Comes_From_Source;
8745             end if;
8746
8747             --  If the node is call and has named associations,
8748             --  set the corresponding links in the copy.
8749
8750             if (Nkind (Old_Node) = N_Function_Call
8751                  or else Nkind (Old_Node) = N_Entry_Call_Statement
8752                  or else
8753                    Nkind (Old_Node) = N_Procedure_Call_Statement)
8754               and then Present (First_Named_Actual (Old_Node))
8755             then
8756                Adjust_Named_Associations (Old_Node, New_Node);
8757             end if;
8758
8759             --  Reset First_Real_Statement for Handled_Sequence_Of_Statements.
8760             --  The replacement mechanism applies to entities, and is not used
8761             --  here. Eventually we may need a more general graph-copying
8762             --  routine. For now, do a sequential search to find desired node.
8763
8764             if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
8765               and then Present (First_Real_Statement (Old_Node))
8766             then
8767                declare
8768                   Old_F  : constant Node_Id := First_Real_Statement (Old_Node);
8769                   N1, N2 : Node_Id;
8770
8771                begin
8772                   N1 := First (Statements (Old_Node));
8773                   N2 := First (Statements (New_Node));
8774
8775                   while N1 /= Old_F loop
8776                      Next (N1);
8777                      Next (N2);
8778                   end loop;
8779
8780                   Set_First_Real_Statement (New_Node, N2);
8781                end;
8782             end if;
8783          end if;
8784
8785          --  All done, return copied node
8786
8787          return New_Node;
8788       end Copy_Node_With_Replacement;
8789
8790       -----------------
8791       -- Visit_Elist --
8792       -----------------
8793
8794       procedure Visit_Elist (E : Elist_Id) is
8795          Elmt : Elmt_Id;
8796       begin
8797          if Present (E) then
8798             Elmt := First_Elmt (E);
8799
8800             while Elmt /= No_Elmt loop
8801                Visit_Node (Node (Elmt));
8802                Next_Elmt (Elmt);
8803             end loop;
8804          end if;
8805       end Visit_Elist;
8806
8807       -----------------
8808       -- Visit_Field --
8809       -----------------
8810
8811       procedure Visit_Field (F : Union_Id; N : Node_Id) is
8812       begin
8813          if F = Union_Id (Empty) then
8814             return;
8815
8816          elsif F in Node_Range then
8817
8818             --  Copy node if it is syntactic, i.e. its parent pointer is
8819             --  set to point to the field that referenced it (certain
8820             --  Itypes will also meet this criterion, which is fine, since
8821             --  these are clearly Itypes that do need to be copied, since
8822             --  we are copying their parent.)
8823
8824             if Parent (Node_Id (F)) = N then
8825                Visit_Node (Node_Id (F));
8826                return;
8827
8828             --  Another case, if we are pointing to an Itype, then we want
8829             --  to copy it if its associated node is somewhere in the tree
8830             --  being copied.
8831
8832             --  Note: the exclusion of self-referential copies is just an
8833             --  optimization, since the search of the already copied list
8834             --  would catch it, but it is a common case (Etype pointing
8835             --  to itself for an Itype that is a base type).
8836
8837             elsif Has_Extension (Node_Id (F))
8838               and then Is_Itype (Entity_Id (F))
8839               and then Node_Id (F) /= N
8840             then
8841                declare
8842                   P : Node_Id;
8843
8844                begin
8845                   P := Associated_Node_For_Itype (Node_Id (F));
8846                   while Present (P) loop
8847                      if P = Source then
8848                         Visit_Node (Node_Id (F));
8849                         return;
8850                      else
8851                         P := Parent (P);
8852                      end if;
8853                   end loop;
8854
8855                   --  An Itype whose parent is not being copied definitely
8856                   --  should NOT be copied, since it does not belong in any
8857                   --  sense to the copied subtree.
8858
8859                   return;
8860                end;
8861             end if;
8862
8863          elsif F in List_Range
8864            and then Parent (List_Id (F)) = N
8865          then
8866             Visit_List (List_Id (F));
8867             return;
8868          end if;
8869       end Visit_Field;
8870
8871       -----------------
8872       -- Visit_Itype --
8873       -----------------
8874
8875       procedure Visit_Itype (Old_Itype : Entity_Id) is
8876          New_Itype : Entity_Id;
8877          E         : Elmt_Id;
8878          Ent       : Entity_Id;
8879
8880       begin
8881          --  Itypes that describe the designated type of access to subprograms
8882          --  have the structure of subprogram declarations, with signatures,
8883          --  etc. Either we duplicate the signatures completely, or choose to
8884          --  share such itypes, which is fine because their elaboration will
8885          --  have no side effects.
8886
8887          if Ekind (Old_Itype) = E_Subprogram_Type then
8888             return;
8889          end if;
8890
8891          New_Itype := New_Copy (Old_Itype);
8892
8893          --  The new Itype has all the attributes of the old one, and
8894          --  we just copy the contents of the entity. However, the back-end
8895          --  needs different names for debugging purposes, so we create a
8896          --  new internal name for it in all cases.
8897
8898          Set_Chars (New_Itype, New_Internal_Name ('T'));
8899
8900          --  If our associated node is an entity that has already been copied,
8901          --  then set the associated node of the copy to point to the right
8902          --  copy. If we have copied an Itype that is itself the associated
8903          --  node of some previously copied Itype, then we set the right
8904          --  pointer in the other direction.
8905
8906          if Present (Actual_Map) then
8907
8908             --  Case of hash tables used
8909
8910             if NCT_Hash_Tables_Used then
8911
8912                Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
8913
8914                if Present (Ent) then
8915                   Set_Associated_Node_For_Itype (New_Itype, Ent);
8916                end if;
8917
8918                Ent := NCT_Itype_Assoc.Get (Old_Itype);
8919                if Present (Ent) then
8920                   Set_Associated_Node_For_Itype (Ent, New_Itype);
8921
8922                --  If the hash table has no association for this Itype and
8923                --  its associated node, enter one now.
8924
8925                else
8926                   NCT_Itype_Assoc.Set
8927                     (Associated_Node_For_Itype (Old_Itype), New_Itype);
8928                end if;
8929
8930             --  Case of hash tables not used
8931
8932             else
8933                E := First_Elmt (Actual_Map);
8934                while Present (E) loop
8935                   if Associated_Node_For_Itype (Old_Itype) = Node (E) then
8936                      Set_Associated_Node_For_Itype
8937                        (New_Itype, Node (Next_Elmt (E)));
8938                   end if;
8939
8940                   if Is_Type (Node (E))
8941                     and then
8942                       Old_Itype = Associated_Node_For_Itype (Node (E))
8943                   then
8944                      Set_Associated_Node_For_Itype
8945                        (Node (Next_Elmt (E)), New_Itype);
8946                   end if;
8947
8948                   E := Next_Elmt (Next_Elmt (E));
8949                end loop;
8950             end if;
8951          end if;
8952
8953          if Present (Freeze_Node (New_Itype)) then
8954             Set_Is_Frozen (New_Itype, False);
8955             Set_Freeze_Node (New_Itype, Empty);
8956          end if;
8957
8958          --  Add new association to map
8959
8960          if No (Actual_Map) then
8961             Actual_Map := New_Elmt_List;
8962          end if;
8963
8964          Append_Elmt (Old_Itype, Actual_Map);
8965          Append_Elmt (New_Itype, Actual_Map);
8966
8967          if NCT_Hash_Tables_Used then
8968             NCT_Assoc.Set (Old_Itype, New_Itype);
8969
8970          else
8971             NCT_Table_Entries := NCT_Table_Entries + 1;
8972
8973             if NCT_Table_Entries > NCT_Hash_Threshhold then
8974                Build_NCT_Hash_Tables;
8975             end if;
8976          end if;
8977
8978          --  If a record subtype is simply copied, the entity list will be
8979          --  shared. Thus cloned_Subtype must be set to indicate the sharing.
8980
8981          if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
8982             Set_Cloned_Subtype (New_Itype, Old_Itype);
8983          end if;
8984
8985          --  Visit descendents that eventually get copied
8986
8987          Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
8988
8989          if Is_Discrete_Type (Old_Itype) then
8990             Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
8991
8992          elsif Has_Discriminants (Base_Type (Old_Itype)) then
8993             --  ??? This should involve call to Visit_Field
8994             Visit_Elist (Discriminant_Constraint (Old_Itype));
8995
8996          elsif Is_Array_Type (Old_Itype) then
8997             if Present (First_Index (Old_Itype)) then
8998                Visit_Field (Union_Id (List_Containing
8999                                 (First_Index (Old_Itype))),
9000                             Old_Itype);
9001             end if;
9002
9003             if Is_Packed (Old_Itype) then
9004                Visit_Field (Union_Id (Packed_Array_Type (Old_Itype)),
9005                             Old_Itype);
9006             end if;
9007          end if;
9008       end Visit_Itype;
9009
9010       ----------------
9011       -- Visit_List --
9012       ----------------
9013
9014       procedure Visit_List (L : List_Id) is
9015          N : Node_Id;
9016       begin
9017          if L /= No_List then
9018             N := First (L);
9019
9020             while Present (N) loop
9021                Visit_Node (N);
9022                Next (N);
9023             end loop;
9024          end if;
9025       end Visit_List;
9026
9027       ----------------
9028       -- Visit_Node --
9029       ----------------
9030
9031       procedure Visit_Node (N : Node_Or_Entity_Id) is
9032
9033       --  Start of processing for Visit_Node
9034
9035       begin
9036          --  Handle case of an Itype, which must be copied
9037
9038          if Has_Extension (N)
9039            and then Is_Itype (N)
9040          then
9041             --  Nothing to do if already in the list. This can happen with an
9042             --  Itype entity that appears more than once in the tree.
9043             --  Note that we do not want to visit descendents in this case.
9044
9045             --  Test for already in list when hash table is used
9046
9047             if NCT_Hash_Tables_Used then
9048                if Present (NCT_Assoc.Get (Entity_Id (N))) then
9049                   return;
9050                end if;
9051
9052             --  Test for already in list when hash table not used
9053
9054             else
9055                declare
9056                   E : Elmt_Id;
9057                begin
9058                   if Present (Actual_Map) then
9059                      E := First_Elmt (Actual_Map);
9060                      while Present (E) loop
9061                         if Node (E) = N then
9062                            return;
9063                         else
9064                            E := Next_Elmt (Next_Elmt (E));
9065                         end if;
9066                      end loop;
9067                   end if;
9068                end;
9069             end if;
9070
9071             Visit_Itype (N);
9072          end if;
9073
9074          --  Visit descendents
9075
9076          Visit_Field (Field1 (N), N);
9077          Visit_Field (Field2 (N), N);
9078          Visit_Field (Field3 (N), N);
9079          Visit_Field (Field4 (N), N);
9080          Visit_Field (Field5 (N), N);
9081       end Visit_Node;
9082
9083    --  Start of processing for New_Copy_Tree
9084
9085    begin
9086       Actual_Map := Map;
9087
9088       --  See if we should use hash table
9089
9090       if No (Actual_Map) then
9091          NCT_Hash_Tables_Used := False;
9092
9093       else
9094          declare
9095             Elmt : Elmt_Id;
9096
9097          begin
9098             NCT_Table_Entries := 0;
9099
9100             Elmt := First_Elmt (Actual_Map);
9101             while Present (Elmt) loop
9102                NCT_Table_Entries := NCT_Table_Entries + 1;
9103                Next_Elmt (Elmt);
9104                Next_Elmt (Elmt);
9105             end loop;
9106
9107             if NCT_Table_Entries > NCT_Hash_Threshhold then
9108                Build_NCT_Hash_Tables;
9109             else
9110                NCT_Hash_Tables_Used := False;
9111             end if;
9112          end;
9113       end if;
9114
9115       --  Hash table set up if required, now start phase one by visiting
9116       --  top node (we will recursively visit the descendents).
9117
9118       Visit_Node (Source);
9119
9120       --  Now the second phase of the copy can start. First we process
9121       --  all the mapped entities, copying their descendents.
9122
9123       if Present (Actual_Map) then
9124          declare
9125             Elmt      : Elmt_Id;
9126             New_Itype : Entity_Id;
9127          begin
9128             Elmt := First_Elmt (Actual_Map);
9129             while Present (Elmt) loop
9130                Next_Elmt (Elmt);
9131                New_Itype := Node (Elmt);
9132                Copy_Itype_With_Replacement (New_Itype);
9133                Next_Elmt (Elmt);
9134             end loop;
9135          end;
9136       end if;
9137
9138       --  Now we can copy the actual tree
9139
9140       return Copy_Node_With_Replacement (Source);
9141    end New_Copy_Tree;
9142
9143    -------------------------
9144    -- New_External_Entity --
9145    -------------------------
9146
9147    function New_External_Entity
9148      (Kind         : Entity_Kind;
9149       Scope_Id     : Entity_Id;
9150       Sloc_Value   : Source_Ptr;
9151       Related_Id   : Entity_Id;
9152       Suffix       : Character;
9153       Suffix_Index : Nat := 0;
9154       Prefix       : Character := ' ') return Entity_Id
9155    is
9156       N : constant Entity_Id :=
9157             Make_Defining_Identifier (Sloc_Value,
9158               New_External_Name
9159                 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
9160
9161    begin
9162       Set_Ekind          (N, Kind);
9163       Set_Is_Internal    (N, True);
9164       Append_Entity      (N, Scope_Id);
9165       Set_Public_Status  (N);
9166
9167       if Kind in Type_Kind then
9168          Init_Size_Align (N);
9169       end if;
9170
9171       return N;
9172    end New_External_Entity;
9173
9174    -------------------------
9175    -- New_Internal_Entity --
9176    -------------------------
9177
9178    function New_Internal_Entity
9179      (Kind       : Entity_Kind;
9180       Scope_Id   : Entity_Id;
9181       Sloc_Value : Source_Ptr;
9182       Id_Char    : Character) return Entity_Id
9183    is
9184       N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
9185
9186    begin
9187       Set_Ekind          (N, Kind);
9188       Set_Is_Internal    (N, True);
9189       Append_Entity      (N, Scope_Id);
9190
9191       if Kind in Type_Kind then
9192          Init_Size_Align (N);
9193       end if;
9194
9195       return N;
9196    end New_Internal_Entity;
9197
9198    -----------------
9199    -- Next_Actual --
9200    -----------------
9201
9202    function Next_Actual (Actual_Id : Node_Id) return Node_Id is
9203       N  : Node_Id;
9204
9205    begin
9206       --  If we are pointing at a positional parameter, it is a member of a
9207       --  node list (the list of parameters), and the next parameter is the
9208       --  next node on the list, unless we hit a parameter association, then
9209       --  we shift to using the chain whose head is the First_Named_Actual in
9210       --  the parent, and then is threaded using the Next_Named_Actual of the
9211       --  Parameter_Association. All this fiddling is because the original node
9212       --  list is in the textual call order, and what we need is the
9213       --  declaration order.
9214
9215       if Is_List_Member (Actual_Id) then
9216          N := Next (Actual_Id);
9217
9218          if Nkind (N) = N_Parameter_Association then
9219             return First_Named_Actual (Parent (Actual_Id));
9220          else
9221             return N;
9222          end if;
9223
9224       else
9225          return Next_Named_Actual (Parent (Actual_Id));
9226       end if;
9227    end Next_Actual;
9228
9229    procedure Next_Actual (Actual_Id : in out Node_Id) is
9230    begin
9231       Actual_Id := Next_Actual (Actual_Id);
9232    end Next_Actual;
9233
9234    -----------------------
9235    -- Normalize_Actuals --
9236    -----------------------
9237
9238    --  Chain actuals according to formals of subprogram. If there are no named
9239    --  associations, the chain is simply the list of Parameter Associations,
9240    --  since the order is the same as the declaration order. If there are named
9241    --  associations, then the First_Named_Actual field in the N_Function_Call
9242    --  or N_Procedure_Call_Statement node points to the Parameter_Association
9243    --  node for the parameter that comes first in declaration order. The
9244    --  remaining named parameters are then chained in declaration order using
9245    --  Next_Named_Actual.
9246
9247    --  This routine also verifies that the number of actuals is compatible with
9248    --  the number and default values of formals, but performs no type checking
9249    --  (type checking is done by the caller).
9250
9251    --  If the matching succeeds, Success is set to True and the caller proceeds
9252    --  with type-checking. If the match is unsuccessful, then Success is set to
9253    --  False, and the caller attempts a different interpretation, if there is
9254    --  one.
9255
9256    --  If the flag Report is on, the call is not overloaded, and a failure to
9257    --  match can be reported here, rather than in the caller.
9258
9259    procedure Normalize_Actuals
9260      (N       : Node_Id;
9261       S       : Entity_Id;
9262       Report  : Boolean;
9263       Success : out Boolean)
9264    is
9265       Actuals     : constant List_Id := Parameter_Associations (N);
9266       Actual      : Node_Id := Empty;
9267       Formal      : Entity_Id;
9268       Last        : Node_Id := Empty;
9269       First_Named : Node_Id := Empty;
9270       Found       : Boolean;
9271
9272       Formals_To_Match : Integer := 0;
9273       Actuals_To_Match : Integer := 0;
9274
9275       procedure Chain (A : Node_Id);
9276       --  Add named actual at the proper place in the list, using the
9277       --  Next_Named_Actual link.
9278
9279       function Reporting return Boolean;
9280       --  Determines if an error is to be reported. To report an error, we
9281       --  need Report to be True, and also we do not report errors caused
9282       --  by calls to init procs that occur within other init procs. Such
9283       --  errors must always be cascaded errors, since if all the types are
9284       --  declared correctly, the compiler will certainly build decent calls!
9285
9286       -----------
9287       -- Chain --
9288       -----------
9289
9290       procedure Chain (A : Node_Id) is
9291       begin
9292          if No (Last) then
9293
9294             --  Call node points to first actual in list
9295
9296             Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
9297
9298          else
9299             Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
9300          end if;
9301
9302          Last := A;
9303          Set_Next_Named_Actual (Last, Empty);
9304       end Chain;
9305
9306       ---------------
9307       -- Reporting --
9308       ---------------
9309
9310       function Reporting return Boolean is
9311       begin
9312          if not Report then
9313             return False;
9314
9315          elsif not Within_Init_Proc then
9316             return True;
9317
9318          elsif Is_Init_Proc (Entity (Name (N))) then
9319             return False;
9320
9321          else
9322             return True;
9323          end if;
9324       end Reporting;
9325
9326    --  Start of processing for Normalize_Actuals
9327
9328    begin
9329       if Is_Access_Type (S) then
9330
9331          --  The name in the call is a function call that returns an access
9332          --  to subprogram. The designated type has the list of formals.
9333
9334          Formal := First_Formal (Designated_Type (S));
9335       else
9336          Formal := First_Formal (S);
9337       end if;
9338
9339       while Present (Formal) loop
9340          Formals_To_Match := Formals_To_Match + 1;
9341          Next_Formal (Formal);
9342       end loop;
9343
9344       --  Find if there is a named association, and verify that no positional
9345       --  associations appear after named ones.
9346
9347       if Present (Actuals) then
9348          Actual := First (Actuals);
9349       end if;
9350
9351       while Present (Actual)
9352         and then Nkind (Actual) /= N_Parameter_Association
9353       loop
9354          Actuals_To_Match := Actuals_To_Match + 1;
9355          Next (Actual);
9356       end loop;
9357
9358       if No (Actual) and Actuals_To_Match = Formals_To_Match then
9359
9360          --  Most common case: positional notation, no defaults
9361
9362          Success := True;
9363          return;
9364
9365       elsif Actuals_To_Match > Formals_To_Match then
9366
9367          --  Too many actuals: will not work
9368
9369          if Reporting then
9370             if Is_Entity_Name (Name (N)) then
9371                Error_Msg_N ("too many arguments in call to&", Name (N));
9372             else
9373                Error_Msg_N ("too many arguments in call", N);
9374             end if;
9375          end if;
9376
9377          Success := False;
9378          return;
9379       end if;
9380
9381       First_Named := Actual;
9382
9383       while Present (Actual) loop
9384          if Nkind (Actual) /= N_Parameter_Association then
9385             Error_Msg_N
9386               ("positional parameters not allowed after named ones", Actual);
9387             Success := False;
9388             return;
9389
9390          else
9391             Actuals_To_Match := Actuals_To_Match + 1;
9392          end if;
9393
9394          Next (Actual);
9395       end loop;
9396
9397       if Present (Actuals) then
9398          Actual := First (Actuals);
9399       end if;
9400
9401       Formal := First_Formal (S);
9402       while Present (Formal) loop
9403
9404          --  Match the formals in order. If the corresponding actual is
9405          --  positional, nothing to do. Else scan the list of named actuals
9406          --  to find the one with the right name.
9407
9408          if Present (Actual)
9409            and then Nkind (Actual) /= N_Parameter_Association
9410          then
9411             Next (Actual);
9412             Actuals_To_Match := Actuals_To_Match - 1;
9413             Formals_To_Match := Formals_To_Match - 1;
9414
9415          else
9416             --  For named parameters, search the list of actuals to find
9417             --  one that matches the next formal name.
9418
9419             Actual := First_Named;
9420             Found  := False;
9421             while Present (Actual) loop
9422                if Chars (Selector_Name (Actual)) = Chars (Formal) then
9423                   Found := True;
9424                   Chain (Actual);
9425                   Actuals_To_Match := Actuals_To_Match - 1;
9426                   Formals_To_Match := Formals_To_Match - 1;
9427                   exit;
9428                end if;
9429
9430                Next (Actual);
9431             end loop;
9432
9433             if not Found then
9434                if Ekind (Formal) /= E_In_Parameter
9435                  or else No (Default_Value (Formal))
9436                then
9437                   if Reporting then
9438                      if (Comes_From_Source (S)
9439                           or else Sloc (S) = Standard_Location)
9440                        and then Is_Overloadable (S)
9441                      then
9442                         if No (Actuals)
9443                           and then
9444                            (Nkind (Parent (N)) = N_Procedure_Call_Statement
9445                              or else
9446                            (Nkind (Parent (N)) = N_Function_Call
9447                              or else
9448                             Nkind (Parent (N)) = N_Parameter_Association))
9449                           and then Ekind (S) /= E_Function
9450                         then
9451                            Set_Etype (N, Etype (S));
9452                         else
9453                            Error_Msg_Name_1 := Chars (S);
9454                            Error_Msg_Sloc := Sloc (S);
9455                            Error_Msg_NE
9456                              ("missing argument for parameter & " &
9457                                 "in call to % declared #", N, Formal);
9458                         end if;
9459
9460                      elsif Is_Overloadable (S) then
9461                         Error_Msg_Name_1 := Chars (S);
9462
9463                         --  Point to type derivation that generated the
9464                         --  operation.
9465
9466                         Error_Msg_Sloc := Sloc (Parent (S));
9467
9468                         Error_Msg_NE
9469                           ("missing argument for parameter & " &
9470                              "in call to % (inherited) #", N, Formal);
9471
9472                      else
9473                         Error_Msg_NE
9474                           ("missing argument for parameter &", N, Formal);
9475                      end if;
9476                   end if;
9477
9478                   Success := False;
9479                   return;
9480
9481                else
9482                   Formals_To_Match := Formals_To_Match - 1;
9483                end if;
9484             end if;
9485          end if;
9486
9487          Next_Formal (Formal);
9488       end loop;
9489
9490       if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
9491          Success := True;
9492          return;
9493
9494       else
9495          if Reporting then
9496
9497             --  Find some superfluous named actual that did not get
9498             --  attached to the list of associations.
9499
9500             Actual := First (Actuals);
9501             while Present (Actual) loop
9502                if Nkind (Actual) = N_Parameter_Association
9503                  and then Actual /= Last
9504                  and then No (Next_Named_Actual (Actual))
9505                then
9506                   Error_Msg_N ("unmatched actual & in call",
9507                     Selector_Name (Actual));
9508                   exit;
9509                end if;
9510
9511                Next (Actual);
9512             end loop;
9513          end if;
9514
9515          Success := False;
9516          return;
9517       end if;
9518    end Normalize_Actuals;
9519
9520    --------------------------------
9521    -- Note_Possible_Modification --
9522    --------------------------------
9523
9524    procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
9525       Modification_Comes_From_Source : constant Boolean :=
9526                                          Comes_From_Source (Parent (N));
9527
9528       Ent : Entity_Id;
9529       Exp : Node_Id;
9530
9531    begin
9532       --  Loop to find referenced entity, if there is one
9533
9534       Exp := N;
9535       loop
9536          <<Continue>>
9537          Ent := Empty;
9538
9539          if Is_Entity_Name (Exp) then
9540             Ent := Entity (Exp);
9541
9542             --  If the entity is missing, it is an undeclared identifier,
9543             --  and there is nothing to annotate.
9544
9545             if No (Ent) then
9546                return;
9547             end if;
9548
9549          elsif Nkind (Exp) = N_Explicit_Dereference then
9550             declare
9551                P : constant Node_Id := Prefix (Exp);
9552
9553             begin
9554                if Nkind (P) = N_Selected_Component
9555                  and then Present (
9556                    Entry_Formal (Entity (Selector_Name (P))))
9557                then
9558                   --  Case of a reference to an entry formal
9559
9560                   Ent := Entry_Formal (Entity (Selector_Name (P)));
9561
9562                elsif Nkind (P) = N_Identifier
9563                  and then Nkind (Parent (Entity (P))) = N_Object_Declaration
9564                  and then Present (Expression (Parent (Entity (P))))
9565                  and then Nkind (Expression (Parent (Entity (P))))
9566                    = N_Reference
9567                then
9568                   --  Case of a reference to a value on which side effects have
9569                   --  been removed.
9570
9571                   Exp := Prefix (Expression (Parent (Entity (P))));
9572                   goto Continue;
9573
9574                else
9575                   return;
9576
9577                end if;
9578             end;
9579
9580          elsif     Nkind (Exp) = N_Type_Conversion
9581            or else Nkind (Exp) = N_Unchecked_Type_Conversion
9582          then
9583             Exp := Expression (Exp);
9584             goto Continue;
9585
9586          elsif     Nkind (Exp) = N_Slice
9587            or else Nkind (Exp) = N_Indexed_Component
9588            or else Nkind (Exp) = N_Selected_Component
9589          then
9590             Exp := Prefix (Exp);
9591             goto Continue;
9592
9593          else
9594             return;
9595          end if;
9596
9597          --  Now look for entity being referenced
9598
9599          if Present (Ent) then
9600             if Is_Object (Ent) then
9601                if Comes_From_Source (Exp)
9602                  or else Modification_Comes_From_Source
9603                then
9604                   --  Give warning if pragma unmodified given and we are
9605                   --  sure this is a modification.
9606
9607                   if Has_Pragma_Unmodified (Ent) and then Sure then
9608                      Error_Msg_NE ("?pragma Unmodified given for &!", N, Ent);
9609                   end if;
9610
9611                   Set_Never_Set_In_Source (Ent, False);
9612                end if;
9613
9614                Set_Is_True_Constant (Ent, False);
9615                Set_Current_Value    (Ent, Empty);
9616                Set_Is_Known_Null    (Ent, False);
9617
9618                if not Can_Never_Be_Null (Ent) then
9619                   Set_Is_Known_Non_Null (Ent, False);
9620                end if;
9621
9622                --  Follow renaming chain
9623
9624                if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
9625                  and then Present (Renamed_Object (Ent))
9626                then
9627                   Exp := Renamed_Object (Ent);
9628                   goto Continue;
9629                end if;
9630
9631                --  Generate a reference only if the assignment comes from
9632                --  source. This excludes, for example, calls to a dispatching
9633                --  assignment operation when the left-hand side is tagged.
9634
9635                if Modification_Comes_From_Source then
9636                   Generate_Reference (Ent, Exp, 'm');
9637                end if;
9638
9639                Check_Nested_Access (Ent);
9640             end if;
9641
9642             Kill_Checks (Ent);
9643
9644             --  If we are sure this is a modification from source, and we know
9645             --  this modifies a constant, then give an appropriate warning.
9646
9647             if Overlays_Constant (Ent)
9648               and then Modification_Comes_From_Source
9649               and then Sure
9650             then
9651                declare
9652                   A : constant Node_Id := Address_Clause (Ent);
9653                begin
9654                   if Present (A) then
9655                      declare
9656                         Exp : constant Node_Id := Expression (A);
9657                      begin
9658                         if Nkind (Exp) = N_Attribute_Reference
9659                           and then Attribute_Name (Exp) = Name_Address
9660                           and then Is_Entity_Name (Prefix (Exp))
9661                         then
9662                            Error_Msg_Sloc := Sloc (A);
9663                            Error_Msg_NE
9664                              ("constant& may be modified via address clause#?",
9665                               N, Entity (Prefix (Exp)));
9666                         end if;
9667                      end;
9668                   end if;
9669                end;
9670             end if;
9671
9672             return;
9673          end if;
9674       end loop;
9675    end Note_Possible_Modification;
9676
9677    -------------------------
9678    -- Object_Access_Level --
9679    -------------------------
9680
9681    function Object_Access_Level (Obj : Node_Id) return Uint is
9682       E : Entity_Id;
9683
9684    --  Returns the static accessibility level of the view denoted by Obj. Note
9685    --  that the value returned is the result of a call to Scope_Depth. Only
9686    --  scope depths associated with dynamic scopes can actually be returned.
9687    --  Since only relative levels matter for accessibility checking, the fact
9688    --  that the distance between successive levels of accessibility is not
9689    --  always one is immaterial (invariant: if level(E2) is deeper than
9690    --  level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
9691
9692       function Reference_To (Obj : Node_Id) return Node_Id;
9693       --  An explicit dereference is created when removing side-effects from
9694       --  expressions for constraint checking purposes. In this case a local
9695       --  access type is created for it. The correct access level is that of
9696       --  the original source node. We detect this case by noting that the
9697       --  prefix of the dereference is created by an object declaration whose
9698       --  initial expression is a reference.
9699
9700       ------------------
9701       -- Reference_To --
9702       ------------------
9703
9704       function Reference_To (Obj : Node_Id) return Node_Id is
9705          Pref : constant Node_Id := Prefix (Obj);
9706       begin
9707          if Is_Entity_Name (Pref)
9708            and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
9709            and then Present (Expression (Parent (Entity (Pref))))
9710            and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
9711          then
9712             return (Prefix (Expression (Parent (Entity (Pref)))));
9713          else
9714             return Empty;
9715          end if;
9716       end Reference_To;
9717
9718    --  Start of processing for Object_Access_Level
9719
9720    begin
9721       if Is_Entity_Name (Obj) then
9722          E := Entity (Obj);
9723
9724          if Is_Prival (E) then
9725             E := Prival_Link (E);
9726          end if;
9727
9728          --  If E is a type then it denotes a current instance. For this case
9729          --  we add one to the normal accessibility level of the type to ensure
9730          --  that current instances are treated as always being deeper than
9731          --  than the level of any visible named access type (see 3.10.2(21)).
9732
9733          if Is_Type (E) then
9734             return Type_Access_Level (E) +  1;
9735
9736          elsif Present (Renamed_Object (E)) then
9737             return Object_Access_Level (Renamed_Object (E));
9738
9739          --  Similarly, if E is a component of the current instance of a
9740          --  protected type, any instance of it is assumed to be at a deeper
9741          --  level than the type. For a protected object (whose type is an
9742          --  anonymous protected type) its components are at the same level
9743          --  as the type itself.
9744
9745          elsif not Is_Overloadable (E)
9746            and then Ekind (Scope (E)) = E_Protected_Type
9747            and then Comes_From_Source (Scope (E))
9748          then
9749             return Type_Access_Level (Scope (E)) + 1;
9750
9751          else
9752             return Scope_Depth (Enclosing_Dynamic_Scope (E));
9753          end if;
9754
9755       elsif Nkind (Obj) = N_Selected_Component then
9756          if Is_Access_Type (Etype (Prefix (Obj))) then
9757             return Type_Access_Level (Etype (Prefix (Obj)));
9758          else
9759             return Object_Access_Level (Prefix (Obj));
9760          end if;
9761
9762       elsif Nkind (Obj) = N_Indexed_Component then
9763          if Is_Access_Type (Etype (Prefix (Obj))) then
9764             return Type_Access_Level (Etype (Prefix (Obj)));
9765          else
9766             return Object_Access_Level (Prefix (Obj));
9767          end if;
9768
9769       elsif Nkind (Obj) = N_Explicit_Dereference then
9770
9771          --  If the prefix is a selected access discriminant then we make a
9772          --  recursive call on the prefix, which will in turn check the level
9773          --  of the prefix object of the selected discriminant.
9774
9775          if Nkind (Prefix (Obj)) = N_Selected_Component
9776            and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
9777            and then
9778              Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
9779          then
9780             return Object_Access_Level (Prefix (Obj));
9781
9782          elsif not (Comes_From_Source (Obj)) then
9783             declare
9784                Ref : constant Node_Id := Reference_To (Obj);
9785             begin
9786                if Present (Ref) then
9787                   return Object_Access_Level (Ref);
9788                else
9789                   return Type_Access_Level (Etype (Prefix (Obj)));
9790                end if;
9791             end;
9792
9793          else
9794             return Type_Access_Level (Etype (Prefix (Obj)));
9795          end if;
9796
9797       elsif Nkind (Obj) = N_Type_Conversion
9798         or else Nkind (Obj) = N_Unchecked_Type_Conversion
9799       then
9800          return Object_Access_Level (Expression (Obj));
9801
9802       elsif Nkind (Obj) = N_Function_Call then
9803
9804          --  Function results are objects, so we get either the access level of
9805          --  the function or, in the case of an indirect call, the level of the
9806          --  access-to-subprogram type. (This code is used for Ada 95, but it
9807          --  looks wrong, because it seems that we should be checking the level
9808          --  of the call itself, even for Ada 95. However, using the Ada 2005
9809          --  version of the code causes regressions in several tests that are
9810          --  compiled with -gnat95. ???)
9811
9812          if Ada_Version < Ada_2005 then
9813             if Is_Entity_Name (Name (Obj)) then
9814                return Subprogram_Access_Level (Entity (Name (Obj)));
9815             else
9816                return Type_Access_Level (Etype (Prefix (Name (Obj))));
9817             end if;
9818
9819          --  For Ada 2005, the level of the result object of a function call is
9820          --  defined to be the level of the call's innermost enclosing master.
9821          --  We determine that by querying the depth of the innermost enclosing
9822          --  dynamic scope.
9823
9824          else
9825             Return_Master_Scope_Depth_Of_Call : declare
9826
9827                function Innermost_Master_Scope_Depth
9828                  (N : Node_Id) return Uint;
9829                --  Returns the scope depth of the given node's innermost
9830                --  enclosing dynamic scope (effectively the accessibility
9831                --  level of the innermost enclosing master).
9832
9833                ----------------------------------
9834                -- Innermost_Master_Scope_Depth --
9835                ----------------------------------
9836
9837                function Innermost_Master_Scope_Depth
9838                  (N : Node_Id) return Uint
9839                is
9840                   Node_Par : Node_Id := Parent (N);
9841
9842                begin
9843                   --  Locate the nearest enclosing node (by traversing Parents)
9844                   --  that Defining_Entity can be applied to, and return the
9845                   --  depth of that entity's nearest enclosing dynamic scope.
9846
9847                   while Present (Node_Par) loop
9848                      case Nkind (Node_Par) is
9849                         when N_Component_Declaration           |
9850                              N_Entry_Declaration               |
9851                              N_Formal_Object_Declaration       |
9852                              N_Formal_Type_Declaration         |
9853                              N_Full_Type_Declaration           |
9854                              N_Incomplete_Type_Declaration     |
9855                              N_Loop_Parameter_Specification    |
9856                              N_Object_Declaration              |
9857                              N_Protected_Type_Declaration      |
9858                              N_Private_Extension_Declaration   |
9859                              N_Private_Type_Declaration        |
9860                              N_Subtype_Declaration             |
9861                              N_Function_Specification          |
9862                              N_Procedure_Specification         |
9863                              N_Task_Type_Declaration           |
9864                              N_Body_Stub                       |
9865                              N_Generic_Instantiation           |
9866                              N_Proper_Body                     |
9867                              N_Implicit_Label_Declaration      |
9868                              N_Package_Declaration             |
9869                              N_Single_Task_Declaration         |
9870                              N_Subprogram_Declaration          |
9871                              N_Generic_Declaration             |
9872                              N_Renaming_Declaration            |
9873                              N_Block_Statement                 |
9874                              N_Formal_Subprogram_Declaration   |
9875                              N_Abstract_Subprogram_Declaration |
9876                              N_Entry_Body                      |
9877                              N_Exception_Declaration           |
9878                              N_Formal_Package_Declaration      |
9879                              N_Number_Declaration              |
9880                              N_Package_Specification           |
9881                              N_Parameter_Specification         |
9882                              N_Single_Protected_Declaration    |
9883                              N_Subunit                         =>
9884
9885                            return Scope_Depth
9886                                     (Nearest_Dynamic_Scope
9887                                        (Defining_Entity (Node_Par)));
9888
9889                         when others =>
9890                            null;
9891                      end case;
9892
9893                      Node_Par := Parent (Node_Par);
9894                   end loop;
9895
9896                   pragma Assert (False);
9897
9898                   --  Should never reach the following return
9899
9900                   return Scope_Depth (Current_Scope) + 1;
9901                end Innermost_Master_Scope_Depth;
9902
9903             --  Start of processing for Return_Master_Scope_Depth_Of_Call
9904
9905             begin
9906                return Innermost_Master_Scope_Depth (Obj);
9907             end Return_Master_Scope_Depth_Of_Call;
9908          end if;
9909
9910       --  For convenience we handle qualified expressions, even though
9911       --  they aren't technically object names.
9912
9913       elsif Nkind (Obj) = N_Qualified_Expression then
9914          return Object_Access_Level (Expression (Obj));
9915
9916       --  Otherwise return the scope level of Standard.
9917       --  (If there are cases that fall through
9918       --  to this point they will be treated as
9919       --  having global accessibility for now. ???)
9920
9921       else
9922          return Scope_Depth (Standard_Standard);
9923       end if;
9924    end Object_Access_Level;
9925
9926    --------------------------------------
9927    -- Original_Corresponding_Operation --
9928    --------------------------------------
9929
9930    function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
9931    is
9932       Typ : constant Entity_Id := Find_Dispatching_Type (S);
9933
9934    begin
9935       --  If S is an inherited primitive S2 the original corresponding
9936       --  operation of S is the original corresponding operation of S2
9937
9938       if Present (Alias (S))
9939         and then Find_Dispatching_Type (Alias (S)) /= Typ
9940       then
9941          return Original_Corresponding_Operation (Alias (S));
9942
9943       --  If S overrides an inherted subprogram S2 the original corresponding
9944       --  operation of S is the original corresponding operation of S2
9945
9946       elsif Is_Overriding_Operation (S)
9947         and then Present (Overridden_Operation (S))
9948       then
9949          return Original_Corresponding_Operation (Overridden_Operation (S));
9950
9951       --  otherwise it is S itself
9952
9953       else
9954          return S;
9955       end if;
9956    end Original_Corresponding_Operation;
9957
9958    -----------------------
9959    -- Private_Component --
9960    -----------------------
9961
9962    function Private_Component (Type_Id : Entity_Id) return Entity_Id is
9963       Ancestor  : constant Entity_Id := Base_Type (Type_Id);
9964
9965       function Trace_Components
9966         (T     : Entity_Id;
9967          Check : Boolean) return Entity_Id;
9968       --  Recursive function that does the work, and checks against circular
9969       --  definition for each subcomponent type.
9970
9971       ----------------------
9972       -- Trace_Components --
9973       ----------------------
9974
9975       function Trace_Components
9976          (T     : Entity_Id;
9977           Check : Boolean) return Entity_Id
9978        is
9979          Btype     : constant Entity_Id := Base_Type (T);
9980          Component : Entity_Id;
9981          P         : Entity_Id;
9982          Candidate : Entity_Id := Empty;
9983
9984       begin
9985          if Check and then Btype = Ancestor then
9986             Error_Msg_N ("circular type definition", Type_Id);
9987             return Any_Type;
9988          end if;
9989
9990          if Is_Private_Type (Btype)
9991            and then not Is_Generic_Type (Btype)
9992          then
9993             if Present (Full_View (Btype))
9994               and then Is_Record_Type (Full_View (Btype))
9995               and then not Is_Frozen (Btype)
9996             then
9997                --  To indicate that the ancestor depends on a private type, the
9998                --  current Btype is sufficient. However, to check for circular
9999                --  definition we must recurse on the full view.
10000
10001                Candidate := Trace_Components (Full_View (Btype), True);
10002
10003                if Candidate = Any_Type then
10004                   return Any_Type;
10005                else
10006                   return Btype;
10007                end if;
10008
10009             else
10010                return Btype;
10011             end if;
10012
10013          elsif Is_Array_Type (Btype) then
10014             return Trace_Components (Component_Type (Btype), True);
10015
10016          elsif Is_Record_Type (Btype) then
10017             Component := First_Entity (Btype);
10018             while Present (Component) loop
10019
10020                --  Skip anonymous types generated by constrained components
10021
10022                if not Is_Type (Component) then
10023                   P := Trace_Components (Etype (Component), True);
10024
10025                   if Present (P) then
10026                      if P = Any_Type then
10027                         return P;
10028                      else
10029                         Candidate := P;
10030                      end if;
10031                   end if;
10032                end if;
10033
10034                Next_Entity (Component);
10035             end loop;
10036
10037             return Candidate;
10038
10039          else
10040             return Empty;
10041          end if;
10042       end Trace_Components;
10043
10044    --  Start of processing for Private_Component
10045
10046    begin
10047       return Trace_Components (Type_Id, False);
10048    end Private_Component;
10049
10050    ---------------------------
10051    -- Primitive_Names_Match --
10052    ---------------------------
10053
10054    function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
10055
10056       function Non_Internal_Name (E : Entity_Id) return Name_Id;
10057       --  Given an internal name, returns the corresponding non-internal name
10058
10059       ------------------------
10060       --  Non_Internal_Name --
10061       ------------------------
10062
10063       function Non_Internal_Name (E : Entity_Id) return Name_Id is
10064       begin
10065          Get_Name_String (Chars (E));
10066          Name_Len := Name_Len - 1;
10067          return Name_Find;
10068       end Non_Internal_Name;
10069
10070    --  Start of processing for Primitive_Names_Match
10071
10072    begin
10073       pragma Assert (Present (E1) and then Present (E2));
10074
10075       return Chars (E1) = Chars (E2)
10076         or else
10077            (not Is_Internal_Name (Chars (E1))
10078               and then Is_Internal_Name (Chars (E2))
10079               and then Non_Internal_Name (E2) = Chars (E1))
10080         or else
10081            (not Is_Internal_Name (Chars (E2))
10082               and then Is_Internal_Name (Chars (E1))
10083               and then Non_Internal_Name (E1) = Chars (E2))
10084         or else
10085            (Is_Predefined_Dispatching_Operation (E1)
10086               and then Is_Predefined_Dispatching_Operation (E2)
10087               and then Same_TSS (E1, E2))
10088         or else
10089            (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
10090    end Primitive_Names_Match;
10091
10092    -----------------------
10093    -- Process_End_Label --
10094    -----------------------
10095
10096    procedure Process_End_Label
10097      (N   : Node_Id;
10098       Typ : Character;
10099       Ent  : Entity_Id)
10100    is
10101       Loc  : Source_Ptr;
10102       Nam  : Node_Id;
10103       Scop : Entity_Id;
10104
10105       Label_Ref : Boolean;
10106       --  Set True if reference to end label itself is required
10107
10108       Endl : Node_Id;
10109       --  Gets set to the operator symbol or identifier that references the
10110       --  entity Ent. For the child unit case, this is the identifier from the
10111       --  designator. For other cases, this is simply Endl.
10112
10113       procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
10114       --  N is an identifier node that appears as a parent unit reference in
10115       --  the case where Ent is a child unit. This procedure generates an
10116       --  appropriate cross-reference entry. E is the corresponding entity.
10117
10118       -------------------------
10119       -- Generate_Parent_Ref --
10120       -------------------------
10121
10122       procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
10123       begin
10124          --  If names do not match, something weird, skip reference
10125
10126          if Chars (E) = Chars (N) then
10127
10128             --  Generate the reference. We do NOT consider this as a reference
10129             --  for unreferenced symbol purposes.
10130
10131             Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
10132
10133             if Style_Check then
10134                Style.Check_Identifier (N, E);
10135             end if;
10136          end if;
10137       end Generate_Parent_Ref;
10138
10139    --  Start of processing for Process_End_Label
10140
10141    begin
10142       --  If no node, ignore. This happens in some error situations, and
10143       --  also for some internally generated structures where no end label
10144       --  references are required in any case.
10145
10146       if No (N) then
10147          return;
10148       end if;
10149
10150       --  Nothing to do if no End_Label, happens for internally generated
10151       --  constructs where we don't want an end label reference anyway. Also
10152       --  nothing to do if Endl is a string literal, which means there was
10153       --  some prior error (bad operator symbol)
10154
10155       Endl := End_Label (N);
10156
10157       if No (Endl) or else Nkind (Endl) = N_String_Literal then
10158          return;
10159       end if;
10160
10161       --  Reference node is not in extended main source unit
10162
10163       if not In_Extended_Main_Source_Unit (N) then
10164
10165          --  Generally we do not collect references except for the extended
10166          --  main source unit. The one exception is the 'e' entry for a
10167          --  package spec, where it is useful for a client to have the
10168          --  ending information to define scopes.
10169
10170          if Typ /= 'e' then
10171             return;
10172
10173          else
10174             Label_Ref := False;
10175
10176             --  For this case, we can ignore any parent references, but we
10177             --  need the package name itself for the 'e' entry.
10178
10179             if Nkind (Endl) = N_Designator then
10180                Endl := Identifier (Endl);
10181             end if;
10182          end if;
10183
10184       --  Reference is in extended main source unit
10185
10186       else
10187          Label_Ref := True;
10188
10189          --  For designator, generate references for the parent entries
10190
10191          if Nkind (Endl) = N_Designator then
10192
10193             --  Generate references for the prefix if the END line comes from
10194             --  source (otherwise we do not need these references) We climb the
10195             --  scope stack to find the expected entities.
10196
10197             if Comes_From_Source (Endl) then
10198                Nam  := Name (Endl);
10199                Scop := Current_Scope;
10200                while Nkind (Nam) = N_Selected_Component loop
10201                   Scop := Scope (Scop);
10202                   exit when No (Scop);
10203                   Generate_Parent_Ref (Selector_Name (Nam), Scop);
10204                   Nam := Prefix (Nam);
10205                end loop;
10206
10207                if Present (Scop) then
10208                   Generate_Parent_Ref (Nam, Scope (Scop));
10209                end if;
10210             end if;
10211
10212             Endl := Identifier (Endl);
10213          end if;
10214       end if;
10215
10216       --  If the end label is not for the given entity, then either we have
10217       --  some previous error, or this is a generic instantiation for which
10218       --  we do not need to make a cross-reference in this case anyway. In
10219       --  either case we simply ignore the call.
10220
10221       if Chars (Ent) /= Chars (Endl) then
10222          return;
10223       end if;
10224
10225       --  If label was really there, then generate a normal reference and then
10226       --  adjust the location in the end label to point past the name (which
10227       --  should almost always be the semicolon).
10228
10229       Loc := Sloc (Endl);
10230
10231       if Comes_From_Source (Endl) then
10232
10233          --  If a label reference is required, then do the style check and
10234          --  generate an l-type cross-reference entry for the label
10235
10236          if Label_Ref then
10237             if Style_Check then
10238                Style.Check_Identifier (Endl, Ent);
10239             end if;
10240
10241             Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
10242          end if;
10243
10244          --  Set the location to point past the label (normally this will
10245          --  mean the semicolon immediately following the label). This is
10246          --  done for the sake of the 'e' or 't' entry generated below.
10247
10248          Get_Decoded_Name_String (Chars (Endl));
10249          Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
10250       end if;
10251
10252       --  Now generate the e/t reference
10253
10254       Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
10255
10256       --  Restore Sloc, in case modified above, since we have an identifier
10257       --  and the normal Sloc should be left set in the tree.
10258
10259       Set_Sloc (Endl, Loc);
10260    end Process_End_Label;
10261
10262    ------------------------------------
10263    -- References_Generic_Formal_Type --
10264    ------------------------------------
10265
10266    function References_Generic_Formal_Type (N : Node_Id) return Boolean is
10267
10268       function Process (N : Node_Id) return Traverse_Result;
10269       --  Process one node in search for generic formal type
10270
10271       -------------
10272       -- Process --
10273       -------------
10274
10275       function Process (N : Node_Id) return Traverse_Result is
10276       begin
10277          if Nkind (N) in N_Has_Entity then
10278             declare
10279                E : constant Entity_Id := Entity (N);
10280             begin
10281                if Present (E) then
10282                   if Is_Generic_Type (E) then
10283                      return Abandon;
10284                   elsif Present (Etype (E))
10285                     and then Is_Generic_Type (Etype (E))
10286                   then
10287                      return Abandon;
10288                   end if;
10289                end if;
10290             end;
10291          end if;
10292
10293          return Atree.OK;
10294       end Process;
10295
10296       function Traverse is new Traverse_Func (Process);
10297       --  Traverse tree to look for generic type
10298
10299    begin
10300       if Inside_A_Generic then
10301          return Traverse (N) = Abandon;
10302       else
10303          return False;
10304       end if;
10305    end References_Generic_Formal_Type;
10306
10307    --------------------
10308    -- Remove_Homonym --
10309    --------------------
10310
10311    procedure Remove_Homonym (E : Entity_Id) is
10312       Prev  : Entity_Id := Empty;
10313       H     : Entity_Id;
10314
10315    begin
10316       if E = Current_Entity (E) then
10317          if Present (Homonym (E)) then
10318             Set_Current_Entity (Homonym (E));
10319          else
10320             Set_Name_Entity_Id (Chars (E), Empty);
10321          end if;
10322       else
10323          H := Current_Entity (E);
10324          while Present (H) and then H /= E loop
10325             Prev := H;
10326             H    := Homonym (H);
10327          end loop;
10328
10329          Set_Homonym (Prev, Homonym (E));
10330       end if;
10331    end Remove_Homonym;
10332
10333    ---------------------
10334    -- Rep_To_Pos_Flag --
10335    ---------------------
10336
10337    function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
10338    begin
10339       return New_Occurrence_Of
10340                (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
10341    end Rep_To_Pos_Flag;
10342
10343    --------------------
10344    -- Require_Entity --
10345    --------------------
10346
10347    procedure Require_Entity (N : Node_Id) is
10348    begin
10349       if Is_Entity_Name (N) and then No (Entity (N)) then
10350          if Total_Errors_Detected /= 0 then
10351             Set_Entity (N, Any_Id);
10352          else
10353             raise Program_Error;
10354          end if;
10355       end if;
10356    end Require_Entity;
10357
10358    ------------------------------
10359    -- Requires_Transient_Scope --
10360    ------------------------------
10361
10362    --  A transient scope is required when variable-sized temporaries are
10363    --  allocated in the primary or secondary stack, or when finalization
10364    --  actions must be generated before the next instruction.
10365
10366    function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
10367       Typ : constant Entity_Id := Underlying_Type (Id);
10368
10369    --  Start of processing for Requires_Transient_Scope
10370
10371    begin
10372       --  This is a private type which is not completed yet. This can only
10373       --  happen in a default expression (of a formal parameter or of a
10374       --  record component). Do not expand transient scope in this case
10375
10376       if No (Typ) then
10377          return False;
10378
10379       --  Do not expand transient scope for non-existent procedure return
10380
10381       elsif Typ = Standard_Void_Type then
10382          return False;
10383
10384       --  Elementary types do not require a transient scope
10385
10386       elsif Is_Elementary_Type (Typ) then
10387          return False;
10388
10389       --  Generally, indefinite subtypes require a transient scope, since the
10390       --  back end cannot generate temporaries, since this is not a valid type
10391       --  for declaring an object. It might be possible to relax this in the
10392       --  future, e.g. by declaring the maximum possible space for the type.
10393
10394       elsif Is_Indefinite_Subtype (Typ) then
10395          return True;
10396
10397       --  Functions returning tagged types may dispatch on result so their
10398       --  returned value is allocated on the secondary stack. Controlled
10399       --  type temporaries need finalization.
10400
10401       elsif Is_Tagged_Type (Typ)
10402         or else Has_Controlled_Component (Typ)
10403       then
10404          return not Is_Value_Type (Typ);
10405
10406       --  Record type
10407
10408       elsif Is_Record_Type (Typ) then
10409          declare
10410             Comp : Entity_Id;
10411          begin
10412             Comp := First_Entity (Typ);
10413             while Present (Comp) loop
10414                if Ekind (Comp) = E_Component
10415                   and then Requires_Transient_Scope (Etype (Comp))
10416                then
10417                   return True;
10418                else
10419                   Next_Entity (Comp);
10420                end if;
10421             end loop;
10422          end;
10423
10424          return False;
10425
10426       --  String literal types never require transient scope
10427
10428       elsif Ekind (Typ) = E_String_Literal_Subtype then
10429          return False;
10430
10431       --  Array type. Note that we already know that this is a constrained
10432       --  array, since unconstrained arrays will fail the indefinite test.
10433
10434       elsif Is_Array_Type (Typ) then
10435
10436          --  If component type requires a transient scope, the array does too
10437
10438          if Requires_Transient_Scope (Component_Type (Typ)) then
10439             return True;
10440
10441          --  Otherwise, we only need a transient scope if the size is not
10442          --  known at compile time.
10443
10444          else
10445             return not Size_Known_At_Compile_Time (Typ);
10446          end if;
10447
10448       --  All other cases do not require a transient scope
10449
10450       else
10451          return False;
10452       end if;
10453    end Requires_Transient_Scope;
10454
10455    --------------------------
10456    -- Reset_Analyzed_Flags --
10457    --------------------------
10458
10459    procedure Reset_Analyzed_Flags (N : Node_Id) is
10460
10461       function Clear_Analyzed (N : Node_Id) return Traverse_Result;
10462       --  Function used to reset Analyzed flags in tree. Note that we do
10463       --  not reset Analyzed flags in entities, since there is no need to
10464       --  reanalyze entities, and indeed, it is wrong to do so, since it
10465       --  can result in generating auxiliary stuff more than once.
10466
10467       --------------------
10468       -- Clear_Analyzed --
10469       --------------------
10470
10471       function Clear_Analyzed (N : Node_Id) return Traverse_Result is
10472       begin
10473          if not Has_Extension (N) then
10474             Set_Analyzed (N, False);
10475          end if;
10476
10477          return OK;
10478       end Clear_Analyzed;
10479
10480       procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
10481
10482    --  Start of processing for Reset_Analyzed_Flags
10483
10484    begin
10485       Reset_Analyzed (N);
10486    end Reset_Analyzed_Flags;
10487
10488    ---------------------------
10489    -- Safe_To_Capture_Value --
10490    ---------------------------
10491
10492    function Safe_To_Capture_Value
10493      (N    : Node_Id;
10494       Ent  : Entity_Id;
10495       Cond : Boolean := False) return Boolean
10496    is
10497    begin
10498       --  The only entities for which we track constant values are variables
10499       --  which are not renamings, constants, out parameters, and in out
10500       --  parameters, so check if we have this case.
10501
10502       --  Note: it may seem odd to track constant values for constants, but in
10503       --  fact this routine is used for other purposes than simply capturing
10504       --  the value. In particular, the setting of Known[_Non]_Null.
10505
10506       if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
10507             or else
10508           Ekind (Ent) = E_Constant
10509             or else
10510           Ekind (Ent) = E_Out_Parameter
10511             or else
10512           Ekind (Ent) = E_In_Out_Parameter
10513       then
10514          null;
10515
10516       --  For conditionals, we also allow loop parameters and all formals,
10517       --  including in parameters.
10518
10519       elsif Cond
10520         and then
10521           (Ekind (Ent) = E_Loop_Parameter
10522              or else
10523            Ekind (Ent) = E_In_Parameter)
10524       then
10525          null;
10526
10527       --  For all other cases, not just unsafe, but impossible to capture
10528       --  Current_Value, since the above are the only entities which have
10529       --  Current_Value fields.
10530
10531       else
10532          return False;
10533       end if;
10534
10535       --  Skip if volatile or aliased, since funny things might be going on in
10536       --  these cases which we cannot necessarily track. Also skip any variable
10537       --  for which an address clause is given, or whose address is taken. Also
10538       --  never capture value of library level variables (an attempt to do so
10539       --  can occur in the case of package elaboration code).
10540
10541       if Treat_As_Volatile (Ent)
10542         or else Is_Aliased (Ent)
10543         or else Present (Address_Clause (Ent))
10544         or else Address_Taken (Ent)
10545         or else (Is_Library_Level_Entity (Ent)
10546                    and then Ekind (Ent) = E_Variable)
10547       then
10548          return False;
10549       end if;
10550
10551       --  OK, all above conditions are met. We also require that the scope of
10552       --  the reference be the same as the scope of the entity, not counting
10553       --  packages and blocks and loops.
10554
10555       declare
10556          E_Scope : constant Entity_Id := Scope (Ent);
10557          R_Scope : Entity_Id;
10558
10559       begin
10560          R_Scope := Current_Scope;
10561          while R_Scope /= Standard_Standard loop
10562             exit when R_Scope = E_Scope;
10563
10564             if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
10565                return False;
10566             else
10567                R_Scope := Scope (R_Scope);
10568             end if;
10569          end loop;
10570       end;
10571
10572       --  We also require that the reference does not appear in a context
10573       --  where it is not sure to be executed (i.e. a conditional context
10574       --  or an exception handler). We skip this if Cond is True, since the
10575       --  capturing of values from conditional tests handles this ok.
10576
10577       if Cond then
10578          return True;
10579       end if;
10580
10581       declare
10582          Desc : Node_Id;
10583          P    : Node_Id;
10584
10585       begin
10586          Desc := N;
10587
10588          P := Parent (N);
10589          while Present (P) loop
10590             if         Nkind (P) = N_If_Statement
10591               or else  Nkind (P) = N_Case_Statement
10592               or else (Nkind (P) in N_Short_Circuit
10593                          and then Desc = Right_Opnd (P))
10594               or else (Nkind (P) = N_Conditional_Expression
10595                          and then Desc /= First (Expressions (P)))
10596               or else  Nkind (P) = N_Exception_Handler
10597               or else  Nkind (P) = N_Selective_Accept
10598               or else  Nkind (P) = N_Conditional_Entry_Call
10599               or else  Nkind (P) = N_Timed_Entry_Call
10600               or else  Nkind (P) = N_Asynchronous_Select
10601             then
10602                return False;
10603             else
10604                Desc := P;
10605                P    := Parent (P);
10606             end if;
10607          end loop;
10608       end;
10609
10610       --  OK, looks safe to set value
10611
10612       return True;
10613    end Safe_To_Capture_Value;
10614
10615    ---------------
10616    -- Same_Name --
10617    ---------------
10618
10619    function Same_Name (N1, N2 : Node_Id) return Boolean is
10620       K1 : constant Node_Kind := Nkind (N1);
10621       K2 : constant Node_Kind := Nkind (N2);
10622
10623    begin
10624       if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
10625         and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
10626       then
10627          return Chars (N1) = Chars (N2);
10628
10629       elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
10630         and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
10631       then
10632          return Same_Name (Selector_Name (N1), Selector_Name (N2))
10633            and then Same_Name (Prefix (N1), Prefix (N2));
10634
10635       else
10636          return False;
10637       end if;
10638    end Same_Name;
10639
10640    -----------------
10641    -- Same_Object --
10642    -----------------
10643
10644    function Same_Object (Node1, Node2 : Node_Id) return Boolean is
10645       N1 : constant Node_Id := Original_Node (Node1);
10646       N2 : constant Node_Id := Original_Node (Node2);
10647       --  We do the tests on original nodes, since we are most interested
10648       --  in the original source, not any expansion that got in the way.
10649
10650       K1 : constant Node_Kind := Nkind (N1);
10651       K2 : constant Node_Kind := Nkind (N2);
10652
10653    begin
10654       --  First case, both are entities with same entity
10655
10656       if K1 in N_Has_Entity and then K2 in N_Has_Entity then
10657          declare
10658             EN1 : constant Entity_Id := Entity (N1);
10659             EN2 : constant Entity_Id := Entity (N2);
10660          begin
10661             if Present (EN1) and then Present (EN2)
10662               and then (Ekind_In (EN1, E_Variable, E_Constant)
10663                          or else Is_Formal (EN1))
10664               and then EN1 = EN2
10665             then
10666                return True;
10667             end if;
10668          end;
10669       end if;
10670
10671       --  Second case, selected component with same selector, same record
10672
10673       if K1 = N_Selected_Component
10674         and then K2 = N_Selected_Component
10675         and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
10676       then
10677          return Same_Object (Prefix (N1), Prefix (N2));
10678
10679       --  Third case, indexed component with same subscripts, same array
10680
10681       elsif K1 = N_Indexed_Component
10682         and then K2 = N_Indexed_Component
10683         and then Same_Object (Prefix (N1), Prefix (N2))
10684       then
10685          declare
10686             E1, E2 : Node_Id;
10687          begin
10688             E1 := First (Expressions (N1));
10689             E2 := First (Expressions (N2));
10690             while Present (E1) loop
10691                if not Same_Value (E1, E2) then
10692                   return False;
10693                else
10694                   Next (E1);
10695                   Next (E2);
10696                end if;
10697             end loop;
10698
10699             return True;
10700          end;
10701
10702       --  Fourth case, slice of same array with same bounds
10703
10704       elsif K1 = N_Slice
10705         and then K2 = N_Slice
10706         and then Nkind (Discrete_Range (N1)) = N_Range
10707         and then Nkind (Discrete_Range (N2)) = N_Range
10708         and then Same_Value (Low_Bound (Discrete_Range (N1)),
10709                              Low_Bound (Discrete_Range (N2)))
10710         and then Same_Value (High_Bound (Discrete_Range (N1)),
10711                              High_Bound (Discrete_Range (N2)))
10712       then
10713          return Same_Name (Prefix (N1), Prefix (N2));
10714
10715       --  All other cases, not clearly the same object
10716
10717       else
10718          return False;
10719       end if;
10720    end Same_Object;
10721
10722    ---------------
10723    -- Same_Type --
10724    ---------------
10725
10726    function Same_Type (T1, T2 : Entity_Id) return Boolean is
10727    begin
10728       if T1 = T2 then
10729          return True;
10730
10731       elsif not Is_Constrained (T1)
10732         and then not Is_Constrained (T2)
10733         and then Base_Type (T1) = Base_Type (T2)
10734       then
10735          return True;
10736
10737       --  For now don't bother with case of identical constraints, to be
10738       --  fiddled with later on perhaps (this is only used for optimization
10739       --  purposes, so it is not critical to do a best possible job)
10740
10741       else
10742          return False;
10743       end if;
10744    end Same_Type;
10745
10746    ----------------
10747    -- Same_Value --
10748    ----------------
10749
10750    function Same_Value (Node1, Node2 : Node_Id) return Boolean is
10751    begin
10752       if Compile_Time_Known_Value (Node1)
10753         and then Compile_Time_Known_Value (Node2)
10754         and then Expr_Value (Node1) = Expr_Value (Node2)
10755       then
10756          return True;
10757       elsif Same_Object (Node1, Node2) then
10758          return True;
10759       else
10760          return False;
10761       end if;
10762    end Same_Value;
10763
10764    -----------------
10765    -- Save_Actual --
10766    -----------------
10767
10768    procedure Save_Actual (N : Node_Id;  Writable : Boolean := False) is
10769    begin
10770       if Ada_Version < Ada_2012 then
10771          return;
10772
10773       elsif Is_Entity_Name (N)
10774         or else
10775           Nkind_In (N, N_Indexed_Component, N_Selected_Component, N_Slice)
10776         or else
10777           (Nkind (N) = N_Attribute_Reference
10778             and then Attribute_Name (N) = Name_Access)
10779
10780       then
10781          --  We are only interested in IN OUT parameters of inner calls
10782
10783          if not Writable
10784            or else Nkind (Parent (N)) = N_Function_Call
10785            or else Nkind (Parent (N)) in N_Op
10786          then
10787             Actuals_In_Call.Increment_Last;
10788             Actuals_In_Call.Table (Actuals_In_Call.Last) := (N, Writable);
10789          end if;
10790       end if;
10791    end Save_Actual;
10792
10793    ------------------------
10794    -- Scope_Is_Transient --
10795    ------------------------
10796
10797    function Scope_Is_Transient return Boolean is
10798    begin
10799       return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
10800    end Scope_Is_Transient;
10801
10802    ------------------
10803    -- Scope_Within --
10804    ------------------
10805
10806    function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
10807       Scop : Entity_Id;
10808
10809    begin
10810       Scop := Scope1;
10811       while Scop /= Standard_Standard loop
10812          Scop := Scope (Scop);
10813
10814          if Scop = Scope2 then
10815             return True;
10816          end if;
10817       end loop;
10818
10819       return False;
10820    end Scope_Within;
10821
10822    --------------------------
10823    -- Scope_Within_Or_Same --
10824    --------------------------
10825
10826    function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
10827       Scop : Entity_Id;
10828
10829    begin
10830       Scop := Scope1;
10831       while Scop /= Standard_Standard loop
10832          if Scop = Scope2 then
10833             return True;
10834          else
10835             Scop := Scope (Scop);
10836          end if;
10837       end loop;
10838
10839       return False;
10840    end Scope_Within_Or_Same;
10841
10842    --------------------
10843    -- Set_Convention --
10844    --------------------
10845
10846    procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
10847    begin
10848       Basic_Set_Convention (E, Val);
10849
10850       if Is_Type (E)
10851         and then Is_Access_Subprogram_Type (Base_Type (E))
10852         and then Has_Foreign_Convention (E)
10853       then
10854          Set_Can_Use_Internal_Rep (E, False);
10855       end if;
10856    end Set_Convention;
10857
10858    ------------------------
10859    -- Set_Current_Entity --
10860    ------------------------
10861
10862    --  The given entity is to be set as the currently visible definition
10863    --  of its associated name (i.e. the Node_Id associated with its name).
10864    --  All we have to do is to get the name from the identifier, and
10865    --  then set the associated Node_Id to point to the given entity.
10866
10867    procedure Set_Current_Entity (E : Entity_Id) is
10868    begin
10869       Set_Name_Entity_Id (Chars (E), E);
10870    end Set_Current_Entity;
10871
10872    ---------------------------
10873    -- Set_Debug_Info_Needed --
10874    ---------------------------
10875
10876    procedure Set_Debug_Info_Needed (T : Entity_Id) is
10877
10878       procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
10879       pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
10880       --  Used to set debug info in a related node if not set already
10881
10882       --------------------------------------
10883       -- Set_Debug_Info_Needed_If_Not_Set --
10884       --------------------------------------
10885
10886       procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
10887       begin
10888          if Present (E)
10889            and then not Needs_Debug_Info (E)
10890          then
10891             Set_Debug_Info_Needed (E);
10892
10893             --  For a private type, indicate that the full view also needs
10894             --  debug information.
10895
10896             if Is_Type (E)
10897               and then Is_Private_Type (E)
10898               and then Present (Full_View (E))
10899             then
10900                Set_Debug_Info_Needed (Full_View (E));
10901             end if;
10902          end if;
10903       end Set_Debug_Info_Needed_If_Not_Set;
10904
10905    --  Start of processing for Set_Debug_Info_Needed
10906
10907    begin
10908       --  Nothing to do if argument is Empty or has Debug_Info_Off set, which
10909       --  indicates that Debug_Info_Needed is never required for the entity.
10910
10911       if No (T)
10912         or else Debug_Info_Off (T)
10913       then
10914          return;
10915       end if;
10916
10917       --  Set flag in entity itself. Note that we will go through the following
10918       --  circuitry even if the flag is already set on T. That's intentional,
10919       --  it makes sure that the flag will be set in subsidiary entities.
10920
10921       Set_Needs_Debug_Info (T);
10922
10923       --  Set flag on subsidiary entities if not set already
10924
10925       if Is_Object (T) then
10926          Set_Debug_Info_Needed_If_Not_Set (Etype (T));
10927
10928       elsif Is_Type (T) then
10929          Set_Debug_Info_Needed_If_Not_Set (Etype (T));
10930
10931          if Is_Record_Type (T) then
10932             declare
10933                Ent : Entity_Id := First_Entity (T);
10934             begin
10935                while Present (Ent) loop
10936                   Set_Debug_Info_Needed_If_Not_Set (Ent);
10937                   Next_Entity (Ent);
10938                end loop;
10939             end;
10940
10941             --  For a class wide subtype, we also need debug information
10942             --  for the equivalent type.
10943
10944             if Ekind (T) = E_Class_Wide_Subtype then
10945                Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
10946             end if;
10947
10948          elsif Is_Array_Type (T) then
10949             Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
10950
10951             declare
10952                Indx : Node_Id := First_Index (T);
10953             begin
10954                while Present (Indx) loop
10955                   Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
10956                   Indx := Next_Index (Indx);
10957                end loop;
10958             end;
10959
10960             if Is_Packed (T) then
10961                Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Type (T));
10962             end if;
10963
10964          elsif Is_Access_Type (T) then
10965             Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
10966
10967          elsif Is_Private_Type (T) then
10968             Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
10969
10970          elsif Is_Protected_Type (T) then
10971             Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
10972          end if;
10973       end if;
10974    end Set_Debug_Info_Needed;
10975
10976    ---------------------------------
10977    -- Set_Entity_With_Style_Check --
10978    ---------------------------------
10979
10980    procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
10981       Val_Actual : Entity_Id;
10982       Nod        : Node_Id;
10983
10984    begin
10985       Set_Entity (N, Val);
10986
10987       if Style_Check
10988         and then not Suppress_Style_Checks (Val)
10989         and then not In_Instance
10990       then
10991          if Nkind (N) = N_Identifier then
10992             Nod := N;
10993          elsif Nkind (N) = N_Expanded_Name then
10994             Nod := Selector_Name (N);
10995          else
10996             return;
10997          end if;
10998
10999          --  A special situation arises for derived operations, where we want
11000          --  to do the check against the parent (since the Sloc of the derived
11001          --  operation points to the derived type declaration itself).
11002
11003          Val_Actual := Val;
11004          while not Comes_From_Source (Val_Actual)
11005            and then Nkind (Val_Actual) in N_Entity
11006            and then (Ekind (Val_Actual) = E_Enumeration_Literal
11007                       or else Is_Subprogram (Val_Actual)
11008                       or else Is_Generic_Subprogram (Val_Actual))
11009            and then Present (Alias (Val_Actual))
11010          loop
11011             Val_Actual := Alias (Val_Actual);
11012          end loop;
11013
11014          --  Renaming declarations for generic actuals do not come from source,
11015          --  and have a different name from that of the entity they rename, so
11016          --  there is no style check to perform here.
11017
11018          if Chars (Nod) = Chars (Val_Actual) then
11019             Style.Check_Identifier (Nod, Val_Actual);
11020          end if;
11021       end if;
11022
11023       Set_Entity (N, Val);
11024    end Set_Entity_With_Style_Check;
11025
11026    ------------------------
11027    -- Set_Name_Entity_Id --
11028    ------------------------
11029
11030    procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
11031    begin
11032       Set_Name_Table_Info (Id, Int (Val));
11033    end Set_Name_Entity_Id;
11034
11035    ---------------------
11036    -- Set_Next_Actual --
11037    ---------------------
11038
11039    procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
11040    begin
11041       if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
11042          Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
11043       end if;
11044    end Set_Next_Actual;
11045
11046    ----------------------------------
11047    -- Set_Optimize_Alignment_Flags --
11048    ----------------------------------
11049
11050    procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
11051    begin
11052       if Optimize_Alignment = 'S' then
11053          Set_Optimize_Alignment_Space (E);
11054       elsif Optimize_Alignment = 'T' then
11055          Set_Optimize_Alignment_Time (E);
11056       end if;
11057    end Set_Optimize_Alignment_Flags;
11058
11059    -----------------------
11060    -- Set_Public_Status --
11061    -----------------------
11062
11063    procedure Set_Public_Status (Id : Entity_Id) is
11064       S : constant Entity_Id := Current_Scope;
11065
11066       function Within_HSS_Or_If (E : Entity_Id) return Boolean;
11067       --  Determines if E is defined within handled statement sequence or
11068       --  an if statement, returns True if so, False otherwise.
11069
11070       ----------------------
11071       -- Within_HSS_Or_If --
11072       ----------------------
11073
11074       function Within_HSS_Or_If (E : Entity_Id) return Boolean is
11075          N : Node_Id;
11076       begin
11077          N := Declaration_Node (E);
11078          loop
11079             N := Parent (N);
11080
11081             if No (N) then
11082                return False;
11083
11084             elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
11085                                N_If_Statement)
11086             then
11087                return True;
11088             end if;
11089          end loop;
11090       end Within_HSS_Or_If;
11091
11092    --  Start of processing for Set_Public_Status
11093
11094    begin
11095       --  Everything in the scope of Standard is public
11096
11097       if S = Standard_Standard then
11098          Set_Is_Public (Id);
11099
11100       --  Entity is definitely not public if enclosing scope is not public
11101
11102       elsif not Is_Public (S) then
11103          return;
11104
11105       --  An object or function declaration that occurs in a handled sequence
11106       --  of statements or within an if statement is the declaration for a
11107       --  temporary object or local subprogram generated by the expander. It
11108       --  never needs to be made public and furthermore, making it public can
11109       --  cause back end problems.
11110
11111       elsif Nkind_In (Parent (Id), N_Object_Declaration,
11112                                    N_Function_Specification)
11113         and then Within_HSS_Or_If (Id)
11114       then
11115          return;
11116
11117       --  Entities in public packages or records are public
11118
11119       elsif Ekind (S) = E_Package or Is_Record_Type (S) then
11120          Set_Is_Public (Id);
11121
11122       --  The bounds of an entry family declaration can generate object
11123       --  declarations that are visible to the back-end, e.g. in the
11124       --  the declaration of a composite type that contains tasks.
11125
11126       elsif Is_Concurrent_Type (S)
11127         and then not Has_Completion (S)
11128         and then Nkind (Parent (Id)) = N_Object_Declaration
11129       then
11130          Set_Is_Public (Id);
11131       end if;
11132    end Set_Public_Status;
11133
11134    -----------------------------
11135    -- Set_Referenced_Modified --
11136    -----------------------------
11137
11138    procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
11139       Pref : Node_Id;
11140
11141    begin
11142       --  Deal with indexed or selected component where prefix is modified
11143
11144       if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
11145          Pref := Prefix (N);
11146
11147          --  If prefix is access type, then it is the designated object that is
11148          --  being modified, which means we have no entity to set the flag on.
11149
11150          if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
11151             return;
11152
11153             --  Otherwise chase the prefix
11154
11155          else
11156             Set_Referenced_Modified (Pref, Out_Param);
11157          end if;
11158
11159       --  Otherwise see if we have an entity name (only other case to process)
11160
11161       elsif Is_Entity_Name (N) and then Present (Entity (N)) then
11162          Set_Referenced_As_LHS           (Entity (N), not Out_Param);
11163          Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
11164       end if;
11165    end Set_Referenced_Modified;
11166
11167    ----------------------------
11168    -- Set_Scope_Is_Transient --
11169    ----------------------------
11170
11171    procedure Set_Scope_Is_Transient (V : Boolean := True) is
11172    begin
11173       Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
11174    end Set_Scope_Is_Transient;
11175
11176    -------------------
11177    -- Set_Size_Info --
11178    -------------------
11179
11180    procedure Set_Size_Info (T1, T2 : Entity_Id) is
11181    begin
11182       --  We copy Esize, but not RM_Size, since in general RM_Size is
11183       --  subtype specific and does not get inherited by all subtypes.
11184
11185       Set_Esize                     (T1, Esize                     (T2));
11186       Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
11187
11188       if Is_Discrete_Or_Fixed_Point_Type (T1)
11189            and then
11190          Is_Discrete_Or_Fixed_Point_Type (T2)
11191       then
11192          Set_Is_Unsigned_Type       (T1, Is_Unsigned_Type          (T2));
11193       end if;
11194
11195       Set_Alignment                 (T1, Alignment                 (T2));
11196    end Set_Size_Info;
11197
11198    --------------------
11199    -- Static_Integer --
11200    --------------------
11201
11202    function Static_Integer (N : Node_Id) return Uint is
11203    begin
11204       Analyze_And_Resolve (N, Any_Integer);
11205
11206       if N = Error
11207         or else Error_Posted (N)
11208         or else Etype (N) = Any_Type
11209       then
11210          return No_Uint;
11211       end if;
11212
11213       if Is_Static_Expression (N) then
11214          if not Raises_Constraint_Error (N) then
11215             return Expr_Value (N);
11216          else
11217             return No_Uint;
11218          end if;
11219
11220       elsif Etype (N) = Any_Type then
11221          return No_Uint;
11222
11223       else
11224          Flag_Non_Static_Expr
11225            ("static integer expression required here", N);
11226          return No_Uint;
11227       end if;
11228    end Static_Integer;
11229
11230    --------------------------
11231    -- Statically_Different --
11232    --------------------------
11233
11234    function Statically_Different (E1, E2 : Node_Id) return Boolean is
11235       R1 : constant Node_Id := Get_Referenced_Object (E1);
11236       R2 : constant Node_Id := Get_Referenced_Object (E2);
11237    begin
11238       return     Is_Entity_Name (R1)
11239         and then Is_Entity_Name (R2)
11240         and then Entity (R1) /= Entity (R2)
11241         and then not Is_Formal (Entity (R1))
11242         and then not Is_Formal (Entity (R2));
11243    end Statically_Different;
11244
11245    -----------------------------
11246    -- Subprogram_Access_Level --
11247    -----------------------------
11248
11249    function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
11250    begin
11251       if Present (Alias (Subp)) then
11252          return Subprogram_Access_Level (Alias (Subp));
11253       else
11254          return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
11255       end if;
11256    end Subprogram_Access_Level;
11257
11258    -----------------
11259    -- Trace_Scope --
11260    -----------------
11261
11262    procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
11263    begin
11264       if Debug_Flag_W then
11265          for J in 0 .. Scope_Stack.Last loop
11266             Write_Str ("  ");
11267          end loop;
11268
11269          Write_Str (Msg);
11270          Write_Name (Chars (E));
11271          Write_Str (" from ");
11272          Write_Location (Sloc (N));
11273          Write_Eol;
11274       end if;
11275    end Trace_Scope;
11276
11277    -----------------------
11278    -- Transfer_Entities --
11279    -----------------------
11280
11281    procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
11282       Ent : Entity_Id := First_Entity (From);
11283
11284    begin
11285       if No (Ent) then
11286          return;
11287       end if;
11288
11289       if (Last_Entity (To)) = Empty then
11290          Set_First_Entity (To, Ent);
11291       else
11292          Set_Next_Entity (Last_Entity (To), Ent);
11293       end if;
11294
11295       Set_Last_Entity (To, Last_Entity (From));
11296
11297       while Present (Ent) loop
11298          Set_Scope (Ent, To);
11299
11300          if not Is_Public (Ent) then
11301             Set_Public_Status (Ent);
11302
11303             if Is_Public (Ent)
11304               and then Ekind (Ent) = E_Record_Subtype
11305
11306             then
11307                --  The components of the propagated Itype must be public
11308                --  as well.
11309
11310                declare
11311                   Comp : Entity_Id;
11312                begin
11313                   Comp := First_Entity (Ent);
11314                   while Present (Comp) loop
11315                      Set_Is_Public (Comp);
11316                      Next_Entity (Comp);
11317                   end loop;
11318                end;
11319             end if;
11320          end if;
11321
11322          Next_Entity (Ent);
11323       end loop;
11324
11325       Set_First_Entity (From, Empty);
11326       Set_Last_Entity (From, Empty);
11327    end Transfer_Entities;
11328
11329    -----------------------
11330    -- Type_Access_Level --
11331    -----------------------
11332
11333    function Type_Access_Level (Typ : Entity_Id) return Uint is
11334       Btyp : Entity_Id;
11335
11336    begin
11337       Btyp := Base_Type (Typ);
11338
11339       --  Ada 2005 (AI-230): For most cases of anonymous access types, we
11340       --  simply use the level where the type is declared. This is true for
11341       --  stand-alone object declarations, and for anonymous access types
11342       --  associated with components the level is the same as that of the
11343       --  enclosing composite type. However, special treatment is needed for
11344       --  the cases of access parameters, return objects of an anonymous access
11345       --  type, and, in Ada 95, access discriminants of limited types.
11346
11347       if Ekind (Btyp) in Access_Kind then
11348          if Ekind (Btyp) = E_Anonymous_Access_Type then
11349
11350             --  If the type is a nonlocal anonymous access type (such as for
11351             --  an access parameter) we treat it as being declared at the
11352             --  library level to ensure that names such as X.all'access don't
11353             --  fail static accessibility checks.
11354
11355             if not Is_Local_Anonymous_Access (Typ) then
11356                return Scope_Depth (Standard_Standard);
11357
11358             --  If this is a return object, the accessibility level is that of
11359             --  the result subtype of the enclosing function. The test here is
11360             --  little complicated, because we have to account for extended
11361             --  return statements that have been rewritten as blocks, in which
11362             --  case we have to find and the Is_Return_Object attribute of the
11363             --  itype's associated object. It would be nice to find a way to
11364             --  simplify this test, but it doesn't seem worthwhile to add a new
11365             --  flag just for purposes of this test. ???
11366
11367             elsif Ekind (Scope (Btyp)) = E_Return_Statement
11368               or else
11369                 (Is_Itype (Btyp)
11370                   and then Nkind (Associated_Node_For_Itype (Btyp)) =
11371                              N_Object_Declaration
11372                   and then Is_Return_Object
11373                              (Defining_Identifier
11374                                 (Associated_Node_For_Itype (Btyp))))
11375             then
11376                declare
11377                   Scop : Entity_Id;
11378
11379                begin
11380                   Scop := Scope (Scope (Btyp));
11381                   while Present (Scop) loop
11382                      exit when Ekind (Scop) = E_Function;
11383                      Scop := Scope (Scop);
11384                   end loop;
11385
11386                   --  Treat the return object's type as having the level of the
11387                   --  function's result subtype (as per RM05-6.5(5.3/2)).
11388
11389                   return Type_Access_Level (Etype (Scop));
11390                end;
11391             end if;
11392          end if;
11393
11394          Btyp := Root_Type (Btyp);
11395
11396          --  The accessibility level of anonymous access types associated with
11397          --  discriminants is that of the current instance of the type, and
11398          --  that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
11399
11400          --  AI-402: access discriminants have accessibility based on the
11401          --  object rather than the type in Ada 2005, so the above paragraph
11402          --  doesn't apply.
11403
11404          --  ??? Needs completion with rules from AI-416
11405
11406          if Ada_Version <= Ada_95
11407            and then Ekind (Typ) = E_Anonymous_Access_Type
11408            and then Present (Associated_Node_For_Itype (Typ))
11409            and then Nkind (Associated_Node_For_Itype (Typ)) =
11410                                                  N_Discriminant_Specification
11411          then
11412             return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
11413          end if;
11414       end if;
11415
11416       return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
11417    end Type_Access_Level;
11418
11419    --------------------------
11420    -- Unit_Declaration_Node --
11421    --------------------------
11422
11423    function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
11424       N : Node_Id := Parent (Unit_Id);
11425
11426    begin
11427       --  Predefined operators do not have a full function declaration
11428
11429       if Ekind (Unit_Id) = E_Operator then
11430          return N;
11431       end if;
11432
11433       --  Isn't there some better way to express the following ???
11434
11435       while Nkind (N) /= N_Abstract_Subprogram_Declaration
11436         and then Nkind (N) /= N_Formal_Package_Declaration
11437         and then Nkind (N) /= N_Function_Instantiation
11438         and then Nkind (N) /= N_Generic_Package_Declaration
11439         and then Nkind (N) /= N_Generic_Subprogram_Declaration
11440         and then Nkind (N) /= N_Package_Declaration
11441         and then Nkind (N) /= N_Package_Body
11442         and then Nkind (N) /= N_Package_Instantiation
11443         and then Nkind (N) /= N_Package_Renaming_Declaration
11444         and then Nkind (N) /= N_Procedure_Instantiation
11445         and then Nkind (N) /= N_Protected_Body
11446         and then Nkind (N) /= N_Subprogram_Declaration
11447         and then Nkind (N) /= N_Subprogram_Body
11448         and then Nkind (N) /= N_Subprogram_Body_Stub
11449         and then Nkind (N) /= N_Subprogram_Renaming_Declaration
11450         and then Nkind (N) /= N_Task_Body
11451         and then Nkind (N) /= N_Task_Type_Declaration
11452         and then Nkind (N) not in N_Formal_Subprogram_Declaration
11453         and then Nkind (N) not in N_Generic_Renaming_Declaration
11454       loop
11455          N := Parent (N);
11456          pragma Assert (Present (N));
11457       end loop;
11458
11459       return N;
11460    end Unit_Declaration_Node;
11461
11462    ------------------------------
11463    -- Universal_Interpretation --
11464    ------------------------------
11465
11466    function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
11467       Index : Interp_Index;
11468       It    : Interp;
11469
11470    begin
11471       --  The argument may be a formal parameter of an operator or subprogram
11472       --  with multiple interpretations, or else an expression for an actual.
11473
11474       if Nkind (Opnd) = N_Defining_Identifier
11475         or else not Is_Overloaded (Opnd)
11476       then
11477          if Etype (Opnd) = Universal_Integer
11478            or else Etype (Opnd) = Universal_Real
11479          then
11480             return Etype (Opnd);
11481          else
11482             return Empty;
11483          end if;
11484
11485       else
11486          Get_First_Interp (Opnd, Index, It);
11487          while Present (It.Typ) loop
11488             if It.Typ = Universal_Integer
11489               or else It.Typ = Universal_Real
11490             then
11491                return It.Typ;
11492             end if;
11493
11494             Get_Next_Interp (Index, It);
11495          end loop;
11496
11497          return Empty;
11498       end if;
11499    end Universal_Interpretation;
11500
11501    ---------------
11502    -- Unqualify --
11503    ---------------
11504
11505    function Unqualify (Expr : Node_Id) return Node_Id is
11506    begin
11507       --  Recurse to handle unlikely case of multiple levels of qualification
11508
11509       if Nkind (Expr) = N_Qualified_Expression then
11510          return Unqualify (Expression (Expr));
11511
11512       --  Normal case, not a qualified expression
11513
11514       else
11515          return Expr;
11516       end if;
11517    end Unqualify;
11518
11519    -----------------------
11520    -- Visible_Ancestors --
11521    -----------------------
11522
11523    function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
11524       List_1 : Elist_Id;
11525       List_2 : Elist_Id;
11526       Elmt   : Elmt_Id;
11527
11528    begin
11529       pragma Assert (Is_Record_Type (Typ)
11530         and then Is_Tagged_Type (Typ));
11531
11532       --  Collect all the parents and progenitors of Typ. If the full-view of
11533       --  private parents and progenitors is available then it is used to
11534       --  generate the list of visible ancestors; otherwise their partial
11535       --  view is added to the resulting list.
11536
11537       Collect_Parents
11538         (T               => Typ,
11539          List            => List_1,
11540          Use_Full_View   => True);
11541
11542       Collect_Interfaces
11543         (T               => Typ,
11544          Ifaces_List     => List_2,
11545          Exclude_Parents => True,
11546          Use_Full_View   => True);
11547
11548       --  Join the two lists. Avoid duplications because an interface may
11549       --  simultaneously be parent and progenitor of a type.
11550
11551       Elmt := First_Elmt (List_2);
11552       while Present (Elmt) loop
11553          Append_Unique_Elmt (Node (Elmt), List_1);
11554          Next_Elmt (Elmt);
11555       end loop;
11556
11557       return List_1;
11558    end Visible_Ancestors;
11559
11560    ----------------------
11561    -- Within_Init_Proc --
11562    ----------------------
11563
11564    function Within_Init_Proc return Boolean is
11565       S : Entity_Id;
11566
11567    begin
11568       S := Current_Scope;
11569       while not Is_Overloadable (S) loop
11570          if S = Standard_Standard then
11571             return False;
11572          else
11573             S := Scope (S);
11574          end if;
11575       end loop;
11576
11577       return Is_Init_Proc (S);
11578    end Within_Init_Proc;
11579
11580    ----------------
11581    -- Wrong_Type --
11582    ----------------
11583
11584    procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
11585       Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
11586       Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
11587
11588       function Has_One_Matching_Field return Boolean;
11589       --  Determines if Expec_Type is a record type with a single component or
11590       --  discriminant whose type matches the found type or is one dimensional
11591       --  array whose component type matches the found type.
11592
11593       ----------------------------
11594       -- Has_One_Matching_Field --
11595       ----------------------------
11596
11597       function Has_One_Matching_Field return Boolean is
11598          E : Entity_Id;
11599
11600       begin
11601          if Is_Array_Type (Expec_Type)
11602            and then Number_Dimensions (Expec_Type) = 1
11603            and then
11604              Covers (Etype (Component_Type (Expec_Type)), Found_Type)
11605          then
11606             return True;
11607
11608          elsif not Is_Record_Type (Expec_Type) then
11609             return False;
11610
11611          else
11612             E := First_Entity (Expec_Type);
11613             loop
11614                if No (E) then
11615                   return False;
11616
11617                elsif (Ekind (E) /= E_Discriminant
11618                        and then Ekind (E) /= E_Component)
11619                  or else (Chars (E) = Name_uTag
11620                            or else Chars (E) = Name_uParent)
11621                then
11622                   Next_Entity (E);
11623
11624                else
11625                   exit;
11626                end if;
11627             end loop;
11628
11629             if not Covers (Etype (E), Found_Type) then
11630                return False;
11631
11632             elsif Present (Next_Entity (E)) then
11633                return False;
11634
11635             else
11636                return True;
11637             end if;
11638          end if;
11639       end Has_One_Matching_Field;
11640
11641    --  Start of processing for Wrong_Type
11642
11643    begin
11644       --  Don't output message if either type is Any_Type, or if a message
11645       --  has already been posted for this node. We need to do the latter
11646       --  check explicitly (it is ordinarily done in Errout), because we
11647       --  are using ! to force the output of the error messages.
11648
11649       if Expec_Type = Any_Type
11650         or else Found_Type = Any_Type
11651         or else Error_Posted (Expr)
11652       then
11653          return;
11654
11655       --  In  an instance, there is an ongoing problem with completion of
11656       --  type derived from private types. Their structure is what Gigi
11657       --  expects, but the  Etype is the parent type rather than the
11658       --  derived private type itself. Do not flag error in this case. The
11659       --  private completion is an entity without a parent, like an Itype.
11660       --  Similarly, full and partial views may be incorrect in the instance.
11661       --  There is no simple way to insure that it is consistent ???
11662
11663       elsif In_Instance then
11664          if Etype (Etype (Expr)) = Etype (Expected_Type)
11665            and then
11666              (Has_Private_Declaration (Expected_Type)
11667                or else Has_Private_Declaration (Etype (Expr)))
11668            and then No (Parent (Expected_Type))
11669          then
11670             return;
11671          end if;
11672       end if;
11673
11674       --  An interesting special check. If the expression is parenthesized
11675       --  and its type corresponds to the type of the sole component of the
11676       --  expected record type, or to the component type of the expected one
11677       --  dimensional array type, then assume we have a bad aggregate attempt.
11678
11679       if Nkind (Expr) in N_Subexpr
11680         and then Paren_Count (Expr) /= 0
11681         and then Has_One_Matching_Field
11682       then
11683          Error_Msg_N ("positional aggregate cannot have one component", Expr);
11684
11685       --  Another special check, if we are looking for a pool-specific access
11686       --  type and we found an E_Access_Attribute_Type, then we have the case
11687       --  of an Access attribute being used in a context which needs a pool-
11688       --  specific type, which is never allowed. The one extra check we make
11689       --  is that the expected designated type covers the Found_Type.
11690
11691       elsif Is_Access_Type (Expec_Type)
11692         and then Ekind (Found_Type) = E_Access_Attribute_Type
11693         and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
11694         and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
11695         and then Covers
11696           (Designated_Type (Expec_Type), Designated_Type (Found_Type))
11697       then
11698          Error_Msg_N -- CODEFIX
11699            ("result must be general access type!", Expr);
11700          Error_Msg_NE -- CODEFIX
11701            ("add ALL to }!", Expr, Expec_Type);
11702
11703       --  Another special check, if the expected type is an integer type,
11704       --  but the expression is of type System.Address, and the parent is
11705       --  an addition or subtraction operation whose left operand is the
11706       --  expression in question and whose right operand is of an integral
11707       --  type, then this is an attempt at address arithmetic, so give
11708       --  appropriate message.
11709
11710       elsif Is_Integer_Type (Expec_Type)
11711         and then Is_RTE (Found_Type, RE_Address)
11712         and then (Nkind (Parent (Expr)) = N_Op_Add
11713                     or else
11714                   Nkind (Parent (Expr)) = N_Op_Subtract)
11715         and then Expr = Left_Opnd (Parent (Expr))
11716         and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
11717       then
11718          Error_Msg_N
11719            ("address arithmetic not predefined in package System",
11720             Parent (Expr));
11721          Error_Msg_N
11722            ("\possible missing with/use of System.Storage_Elements",
11723             Parent (Expr));
11724          return;
11725
11726       --  If the expected type is an anonymous access type, as for access
11727       --  parameters and discriminants, the error is on the designated types.
11728
11729       elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
11730          if Comes_From_Source (Expec_Type) then
11731             Error_Msg_NE ("expected}!", Expr, Expec_Type);
11732          else
11733             Error_Msg_NE
11734               ("expected an access type with designated}",
11735                  Expr, Designated_Type (Expec_Type));
11736          end if;
11737
11738          if Is_Access_Type (Found_Type)
11739            and then not Comes_From_Source (Found_Type)
11740          then
11741             Error_Msg_NE
11742               ("\\found an access type with designated}!",
11743                 Expr, Designated_Type (Found_Type));
11744          else
11745             if From_With_Type (Found_Type) then
11746                Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
11747                Error_Msg_Qual_Level := 99;
11748                Error_Msg_NE -- CODEFIX
11749                  ("\\missing `WITH &;", Expr, Scope (Found_Type));
11750                Error_Msg_Qual_Level := 0;
11751             else
11752                Error_Msg_NE ("found}!", Expr, Found_Type);
11753             end if;
11754          end if;
11755
11756       --  Normal case of one type found, some other type expected
11757
11758       else
11759          --  If the names of the two types are the same, see if some number
11760          --  of levels of qualification will help. Don't try more than three
11761          --  levels, and if we get to standard, it's no use (and probably
11762          --  represents an error in the compiler) Also do not bother with
11763          --  internal scope names.
11764
11765          declare
11766             Expec_Scope : Entity_Id;
11767             Found_Scope : Entity_Id;
11768
11769          begin
11770             Expec_Scope := Expec_Type;
11771             Found_Scope := Found_Type;
11772
11773             for Levels in Int range 0 .. 3 loop
11774                if Chars (Expec_Scope) /= Chars (Found_Scope) then
11775                   Error_Msg_Qual_Level := Levels;
11776                   exit;
11777                end if;
11778
11779                Expec_Scope := Scope (Expec_Scope);
11780                Found_Scope := Scope (Found_Scope);
11781
11782                exit when Expec_Scope = Standard_Standard
11783                  or else Found_Scope = Standard_Standard
11784                  or else not Comes_From_Source (Expec_Scope)
11785                  or else not Comes_From_Source (Found_Scope);
11786             end loop;
11787          end;
11788
11789          if Is_Record_Type (Expec_Type)
11790            and then Present (Corresponding_Remote_Type (Expec_Type))
11791          then
11792             Error_Msg_NE ("expected}!", Expr,
11793                           Corresponding_Remote_Type (Expec_Type));
11794          else
11795             Error_Msg_NE ("expected}!", Expr, Expec_Type);
11796          end if;
11797
11798          if Is_Entity_Name (Expr)
11799            and then Is_Package_Or_Generic_Package (Entity (Expr))
11800          then
11801             Error_Msg_N ("\\found package name!", Expr);
11802
11803          elsif Is_Entity_Name (Expr)
11804            and then
11805              (Ekind (Entity (Expr)) = E_Procedure
11806                 or else
11807               Ekind (Entity (Expr)) = E_Generic_Procedure)
11808          then
11809             if Ekind (Expec_Type) = E_Access_Subprogram_Type then
11810                Error_Msg_N
11811                  ("found procedure name, possibly missing Access attribute!",
11812                    Expr);
11813             else
11814                Error_Msg_N
11815                  ("\\found procedure name instead of function!", Expr);
11816             end if;
11817
11818          elsif Nkind (Expr) = N_Function_Call
11819            and then Ekind (Expec_Type) = E_Access_Subprogram_Type
11820            and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
11821            and then No (Parameter_Associations (Expr))
11822          then
11823             Error_Msg_N
11824               ("found function name, possibly missing Access attribute!",
11825                Expr);
11826
11827          --  Catch common error: a prefix or infix operator which is not
11828          --  directly visible because the type isn't.
11829
11830          elsif Nkind (Expr) in N_Op
11831             and then Is_Overloaded (Expr)
11832             and then not Is_Immediately_Visible (Expec_Type)
11833             and then not Is_Potentially_Use_Visible (Expec_Type)
11834             and then not In_Use (Expec_Type)
11835             and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
11836          then
11837             Error_Msg_N
11838               ("operator of the type is not directly visible!", Expr);
11839
11840          elsif Ekind (Found_Type) = E_Void
11841            and then Present (Parent (Found_Type))
11842            and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
11843          then
11844             Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
11845
11846          else
11847             Error_Msg_NE ("\\found}!", Expr, Found_Type);
11848          end if;
11849
11850          --  A special check for cases like M1 and M2 = 0 where M1 and M2 are
11851          --  of the same modular type, and (M1 and M2) = 0 was intended.
11852
11853          if Expec_Type = Standard_Boolean
11854            and then Is_Modular_Integer_Type (Found_Type)
11855            and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
11856            and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
11857          then
11858             declare
11859                Op : constant Node_Id := Right_Opnd (Parent (Expr));
11860                L  : constant Node_Id := Left_Opnd (Op);
11861                R  : constant Node_Id := Right_Opnd (Op);
11862             begin
11863                --  The case for the message is when the left operand of the
11864                --  comparison is the same modular type, or when it is an
11865                --  integer literal (or other universal integer expression),
11866                --  which would have been typed as the modular type if the
11867                --  parens had been there.
11868
11869                if (Etype (L) = Found_Type
11870                      or else
11871                    Etype (L) = Universal_Integer)
11872                  and then Is_Integer_Type (Etype (R))
11873                then
11874                   Error_Msg_N
11875                     ("\\possible missing parens for modular operation", Expr);
11876                end if;
11877             end;
11878          end if;
11879
11880          --  Reset error message qualification indication
11881
11882          Error_Msg_Qual_Level := 0;
11883       end if;
11884    end Wrong_Type;
11885
11886 end Sem_Util;