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