sem_ch8.adb: Minor code reorganization, comment updates.
[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    -- Matching_Static_Array_Bounds --
8003    ----------------------------------
8004
8005    function Matching_Static_Array_Bounds
8006      (L_Typ : Node_Id;
8007       R_Typ : Node_Id) return Boolean
8008    is
8009       L_Ndims : constant Nat := Number_Dimensions (L_Typ);
8010       R_Ndims : constant Nat := Number_Dimensions (R_Typ);
8011
8012       L_Index : Node_Id;
8013       R_Index : Node_Id;
8014       L_Low   : Node_Id;
8015       L_High  : Node_Id;
8016       R_Low   : Node_Id;
8017       R_High  : Node_Id;
8018
8019    begin
8020       if L_Ndims /= R_Ndims then
8021          return False;
8022       end if;
8023
8024       --  Unconstrained types do not have static bounds
8025
8026       if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
8027          return False;
8028       end if;
8029
8030       L_Index := First_Index (L_Typ);
8031       R_Index := First_Index (R_Typ);
8032
8033       for Indx in 1 .. L_Ndims loop
8034          Get_Index_Bounds (L_Index, L_Low, L_High);
8035          Get_Index_Bounds (R_Index, R_Low, R_High);
8036
8037          if         Is_OK_Static_Expression (L_Low)
8038            and then Is_OK_Static_Expression (L_High)
8039            and then Is_OK_Static_Expression (R_Low)
8040            and then Is_OK_Static_Expression (R_High)
8041            and then Expr_Value (L_Low)  = Expr_Value (R_Low)
8042            and then Expr_Value (L_High) = Expr_Value (R_High)
8043          then
8044             Next (L_Index);
8045             Next (R_Index);
8046
8047          else
8048             return False;
8049          end if;
8050       end loop;
8051
8052       --  If we fall through the loop, all indexes matched
8053
8054       return True;
8055    end Matching_Static_Array_Bounds;
8056
8057    -------------------
8058    -- May_Be_Lvalue --
8059    -------------------
8060
8061    function May_Be_Lvalue (N : Node_Id) return Boolean is
8062       P : constant Node_Id := Parent (N);
8063
8064    begin
8065       case Nkind (P) is
8066
8067          --  Test left side of assignment
8068
8069          when N_Assignment_Statement =>
8070             return N = Name (P);
8071
8072          --  Test prefix of component or attribute. Note that the prefix of an
8073          --  explicit or implicit dereference cannot be an l-value.
8074
8075          when N_Attribute_Reference =>
8076             return N = Prefix (P)
8077               and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
8078
8079          --  For an expanded name, the name is an lvalue if the expanded name
8080          --  is an lvalue, but the prefix is never an lvalue, since it is just
8081          --  the scope where the name is found.
8082
8083          when N_Expanded_Name        =>
8084             if N = Prefix (P) then
8085                return May_Be_Lvalue (P);
8086             else
8087                return False;
8088             end if;
8089
8090          --  For a selected component A.B, A is certainly an lvalue if A.B is.
8091          --  B is a little interesting, if we have A.B := 3, there is some
8092          --  discussion as to whether B is an lvalue or not, we choose to say
8093          --  it is. Note however that A is not an lvalue if it is of an access
8094          --  type since this is an implicit dereference.
8095
8096          when N_Selected_Component   =>
8097             if N = Prefix (P)
8098               and then Present (Etype (N))
8099               and then Is_Access_Type (Etype (N))
8100             then
8101                return False;
8102             else
8103                return May_Be_Lvalue (P);
8104             end if;
8105
8106          --  For an indexed component or slice, the index or slice bounds is
8107          --  never an lvalue. The prefix is an lvalue if the indexed component
8108          --  or slice is an lvalue, except if it is an access type, where we
8109          --  have an implicit dereference.
8110
8111          when N_Indexed_Component    =>
8112             if N /= Prefix (P)
8113               or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
8114             then
8115                return False;
8116             else
8117                return May_Be_Lvalue (P);
8118             end if;
8119
8120          --  Prefix of a reference is an lvalue if the reference is an lvalue
8121
8122          when N_Reference            =>
8123             return May_Be_Lvalue (P);
8124
8125          --  Prefix of explicit dereference is never an lvalue
8126
8127          when N_Explicit_Dereference =>
8128             return False;
8129
8130          --  Positional parameter for subprogram, entry, or accept call.
8131          --  In older versions of Ada function call arguments are never
8132          --  lvalues. In Ada 2012 functions can have in-out parameters.
8133
8134          when N_Function_Call            |
8135               N_Procedure_Call_Statement |
8136               N_Entry_Call_Statement     |
8137               N_Accept_Statement
8138          =>
8139             if Nkind (P) = N_Function_Call
8140               and then Ada_Version < Ada_2012
8141             then
8142                return False;
8143             end if;
8144
8145             --  The following mechanism is clumsy and fragile. A single
8146             --  flag set in Resolve_Actuals would be preferable ???
8147
8148             declare
8149                Proc : Entity_Id;
8150                Form : Entity_Id;
8151                Act  : Node_Id;
8152
8153             begin
8154                Proc := Get_Subprogram_Entity (P);
8155
8156                if No (Proc) then
8157                   return True;
8158                end if;
8159
8160                --  If we are not a list member, something is strange, so
8161                --  be conservative and return True.
8162
8163                if not Is_List_Member (N) then
8164                   return True;
8165                end if;
8166
8167                --  We are going to find the right formal by stepping forward
8168                --  through the formals, as we step backwards in the actuals.
8169
8170                Form := First_Formal (Proc);
8171                Act  := N;
8172                loop
8173                   --  If no formal, something is weird, so be conservative
8174                   --  and return True.
8175
8176                   if No (Form) then
8177                      return True;
8178                   end if;
8179
8180                   Prev (Act);
8181                   exit when No (Act);
8182                   Next_Formal (Form);
8183                end loop;
8184
8185                return Ekind (Form) /= E_In_Parameter;
8186             end;
8187
8188          --  Named parameter for procedure or accept call
8189
8190          when N_Parameter_Association =>
8191             declare
8192                Proc : Entity_Id;
8193                Form : Entity_Id;
8194
8195             begin
8196                Proc := Get_Subprogram_Entity (Parent (P));
8197
8198                if No (Proc) then
8199                   return True;
8200                end if;
8201
8202                --  Loop through formals to find the one that matches
8203
8204                Form := First_Formal (Proc);
8205                loop
8206                   --  If no matching formal, that's peculiar, some kind of
8207                   --  previous error, so return True to be conservative.
8208
8209                   if No (Form) then
8210                      return True;
8211                   end if;
8212
8213                   --  Else test for match
8214
8215                   if Chars (Form) = Chars (Selector_Name (P)) then
8216                      return Ekind (Form) /= E_In_Parameter;
8217                   end if;
8218
8219                   Next_Formal (Form);
8220                end loop;
8221             end;
8222
8223          --  Test for appearing in a conversion that itself appears in an
8224          --  lvalue context, since this should be an lvalue.
8225
8226          when N_Type_Conversion =>
8227             return May_Be_Lvalue (P);
8228
8229          --  Test for appearance in object renaming declaration
8230
8231          when N_Object_Renaming_Declaration =>
8232             return True;
8233
8234          --  All other references are definitely not lvalues
8235
8236          when others =>
8237             return False;
8238
8239       end case;
8240    end May_Be_Lvalue;
8241
8242    -----------------------
8243    -- Mark_Coextensions --
8244    -----------------------
8245
8246    procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
8247       Is_Dynamic : Boolean;
8248       --  Indicates whether the context causes nested coextensions to be
8249       --  dynamic or static
8250
8251       function Mark_Allocator (N : Node_Id) return Traverse_Result;
8252       --  Recognize an allocator node and label it as a dynamic coextension
8253
8254       --------------------
8255       -- Mark_Allocator --
8256       --------------------
8257
8258       function Mark_Allocator (N : Node_Id) return Traverse_Result is
8259       begin
8260          if Nkind (N) = N_Allocator then
8261             if Is_Dynamic then
8262                Set_Is_Dynamic_Coextension (N);
8263
8264             --  If the allocator expression is potentially dynamic, it may
8265             --  be expanded out of order and require dynamic allocation
8266             --  anyway, so we treat the coextension itself as dynamic.
8267             --  Potential optimization ???
8268
8269             elsif Nkind (Expression (N)) = N_Qualified_Expression
8270               and then Nkind (Expression (Expression (N))) = N_Op_Concat
8271             then
8272                Set_Is_Dynamic_Coextension (N);
8273
8274             else
8275                Set_Is_Static_Coextension (N);
8276             end if;
8277          end if;
8278
8279          return OK;
8280       end Mark_Allocator;
8281
8282       procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
8283
8284    --  Start of processing Mark_Coextensions
8285
8286    begin
8287       case Nkind (Context_Nod) is
8288          when N_Assignment_Statement    |
8289               N_Simple_Return_Statement =>
8290             Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
8291
8292          when N_Object_Declaration =>
8293             Is_Dynamic := Nkind (Root_Nod) = N_Allocator;
8294
8295          --  This routine should not be called for constructs which may not
8296          --  contain coextensions.
8297
8298          when others =>
8299             raise Program_Error;
8300       end case;
8301
8302       Mark_Allocators (Root_Nod);
8303    end Mark_Coextensions;
8304
8305    ----------------------
8306    -- Needs_One_Actual --
8307    ----------------------
8308
8309    function Needs_One_Actual (E : Entity_Id) return Boolean is
8310       Formal : Entity_Id;
8311
8312    begin
8313       if Ada_Version >= Ada_2005
8314         and then Present (First_Formal (E))
8315       then
8316          Formal := Next_Formal (First_Formal (E));
8317          while Present (Formal) loop
8318             if No (Default_Value (Formal)) then
8319                return False;
8320             end if;
8321
8322             Next_Formal (Formal);
8323          end loop;
8324
8325          return True;
8326
8327       else
8328          return False;
8329       end if;
8330    end Needs_One_Actual;
8331
8332    ------------------------
8333    -- New_Copy_List_Tree --
8334    ------------------------
8335
8336    function New_Copy_List_Tree (List : List_Id) return List_Id is
8337       NL : List_Id;
8338       E  : Node_Id;
8339
8340    begin
8341       if List = No_List then
8342          return No_List;
8343
8344       else
8345          NL := New_List;
8346          E := First (List);
8347
8348          while Present (E) loop
8349             Append (New_Copy_Tree (E), NL);
8350             E := Next (E);
8351          end loop;
8352
8353          return NL;
8354       end if;
8355    end New_Copy_List_Tree;
8356
8357    -------------------
8358    -- New_Copy_Tree --
8359    -------------------
8360
8361    use Atree.Unchecked_Access;
8362    use Atree_Private_Part;
8363
8364    --  Our approach here requires a two pass traversal of the tree. The
8365    --  first pass visits all nodes that eventually will be copied looking
8366    --  for defining Itypes. If any defining Itypes are found, then they are
8367    --  copied, and an entry is added to the replacement map. In the second
8368    --  phase, the tree is copied, using the replacement map to replace any
8369    --  Itype references within the copied tree.
8370
8371    --  The following hash tables are used if the Map supplied has more
8372    --  than hash threshold entries to speed up access to the map. If
8373    --  there are fewer entries, then the map is searched sequentially
8374    --  (because setting up a hash table for only a few entries takes
8375    --  more time than it saves.
8376
8377    function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
8378    --  Hash function used for hash operations
8379
8380    -------------------
8381    -- New_Copy_Hash --
8382    -------------------
8383
8384    function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
8385    begin
8386       return Nat (E) mod (NCT_Header_Num'Last + 1);
8387    end New_Copy_Hash;
8388
8389    ---------------
8390    -- NCT_Assoc --
8391    ---------------
8392
8393    --  The hash table NCT_Assoc associates old entities in the table
8394    --  with their corresponding new entities (i.e. the pairs of entries
8395    --  presented in the original Map argument are Key-Element pairs).
8396
8397    package NCT_Assoc is new Simple_HTable (
8398      Header_Num => NCT_Header_Num,
8399      Element    => Entity_Id,
8400      No_Element => Empty,
8401      Key        => Entity_Id,
8402      Hash       => New_Copy_Hash,
8403      Equal      => Types."=");
8404
8405    ---------------------
8406    -- NCT_Itype_Assoc --
8407    ---------------------
8408
8409    --  The hash table NCT_Itype_Assoc contains entries only for those
8410    --  old nodes which have a non-empty Associated_Node_For_Itype set.
8411    --  The key is the associated node, and the element is the new node
8412    --  itself (NOT the associated node for the new node).
8413
8414    package NCT_Itype_Assoc is new Simple_HTable (
8415      Header_Num => NCT_Header_Num,
8416      Element    => Entity_Id,
8417      No_Element => Empty,
8418      Key        => Entity_Id,
8419      Hash       => New_Copy_Hash,
8420      Equal      => Types."=");
8421
8422    --  Start of processing for New_Copy_Tree function
8423
8424    function New_Copy_Tree
8425      (Source    : Node_Id;
8426       Map       : Elist_Id := No_Elist;
8427       New_Sloc  : Source_Ptr := No_Location;
8428       New_Scope : Entity_Id := Empty) return Node_Id
8429    is
8430       Actual_Map : Elist_Id := Map;
8431       --  This is the actual map for the copy. It is initialized with the
8432       --  given elements, and then enlarged as required for Itypes that are
8433       --  copied during the first phase of the copy operation. The visit
8434       --  procedures add elements to this map as Itypes are encountered.
8435       --  The reason we cannot use Map directly, is that it may well be
8436       --  (and normally is) initialized to No_Elist, and if we have mapped
8437       --  entities, we have to reset it to point to a real Elist.
8438
8439       function Assoc (N : Node_Or_Entity_Id) return Node_Id;
8440       --  Called during second phase to map entities into their corresponding
8441       --  copies using Actual_Map. If the argument is not an entity, or is not
8442       --  in Actual_Map, then it is returned unchanged.
8443
8444       procedure Build_NCT_Hash_Tables;
8445       --  Builds hash tables (number of elements >= threshold value)
8446
8447       function Copy_Elist_With_Replacement
8448         (Old_Elist : Elist_Id) return Elist_Id;
8449       --  Called during second phase to copy element list doing replacements
8450
8451       procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
8452       --  Called during the second phase to process a copied Itype. The actual
8453       --  copy happened during the first phase (so that we could make the entry
8454       --  in the mapping), but we still have to deal with the descendents of
8455       --  the copied Itype and copy them where necessary.
8456
8457       function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
8458       --  Called during second phase to copy list doing replacements
8459
8460       function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
8461       --  Called during second phase to copy node doing replacements
8462
8463       procedure Visit_Elist (E : Elist_Id);
8464       --  Called during first phase to visit all elements of an Elist
8465
8466       procedure Visit_Field (F : Union_Id; N : Node_Id);
8467       --  Visit a single field, recursing to call Visit_Node or Visit_List
8468       --  if the field is a syntactic descendent of the current node (i.e.
8469       --  its parent is Node N).
8470
8471       procedure Visit_Itype (Old_Itype : Entity_Id);
8472       --  Called during first phase to visit subsidiary fields of a defining
8473       --  Itype, and also create a copy and make an entry in the replacement
8474       --  map for the new copy.
8475
8476       procedure Visit_List (L : List_Id);
8477       --  Called during first phase to visit all elements of a List
8478
8479       procedure Visit_Node (N : Node_Or_Entity_Id);
8480       --  Called during first phase to visit a node and all its subtrees
8481
8482       -----------
8483       -- Assoc --
8484       -----------
8485
8486       function Assoc (N : Node_Or_Entity_Id) return Node_Id is
8487          E   : Elmt_Id;
8488          Ent : Entity_Id;
8489
8490       begin
8491          if not Has_Extension (N) or else No (Actual_Map) then
8492             return N;
8493
8494          elsif NCT_Hash_Tables_Used then
8495             Ent := NCT_Assoc.Get (Entity_Id (N));
8496
8497             if Present (Ent) then
8498                return Ent;
8499             else
8500                return N;
8501             end if;
8502
8503          --  No hash table used, do serial search
8504
8505          else
8506             E := First_Elmt (Actual_Map);
8507             while Present (E) loop
8508                if Node (E) = N then
8509                   return Node (Next_Elmt (E));
8510                else
8511                   E := Next_Elmt (Next_Elmt (E));
8512                end if;
8513             end loop;
8514          end if;
8515
8516          return N;
8517       end Assoc;
8518
8519       ---------------------------
8520       -- Build_NCT_Hash_Tables --
8521       ---------------------------
8522
8523       procedure Build_NCT_Hash_Tables is
8524          Elmt : Elmt_Id;
8525          Ent  : Entity_Id;
8526       begin
8527          if NCT_Hash_Table_Setup then
8528             NCT_Assoc.Reset;
8529             NCT_Itype_Assoc.Reset;
8530          end if;
8531
8532          Elmt := First_Elmt (Actual_Map);
8533          while Present (Elmt) loop
8534             Ent := Node (Elmt);
8535
8536             --  Get new entity, and associate old and new
8537
8538             Next_Elmt (Elmt);
8539             NCT_Assoc.Set (Ent, Node (Elmt));
8540
8541             if Is_Type (Ent) then
8542                declare
8543                   Anode : constant Entity_Id :=
8544                             Associated_Node_For_Itype (Ent);
8545
8546                begin
8547                   if Present (Anode) then
8548
8549                      --  Enter a link between the associated node of the
8550                      --  old Itype and the new Itype, for updating later
8551                      --  when node is copied.
8552
8553                      NCT_Itype_Assoc.Set (Anode, Node (Elmt));
8554                   end if;
8555                end;
8556             end if;
8557
8558             Next_Elmt (Elmt);
8559          end loop;
8560
8561          NCT_Hash_Tables_Used := True;
8562          NCT_Hash_Table_Setup := True;
8563       end Build_NCT_Hash_Tables;
8564
8565       ---------------------------------
8566       -- Copy_Elist_With_Replacement --
8567       ---------------------------------
8568
8569       function Copy_Elist_With_Replacement
8570         (Old_Elist : Elist_Id) return Elist_Id
8571       is
8572          M         : Elmt_Id;
8573          New_Elist : Elist_Id;
8574
8575       begin
8576          if No (Old_Elist) then
8577             return No_Elist;
8578
8579          else
8580             New_Elist := New_Elmt_List;
8581
8582             M := First_Elmt (Old_Elist);
8583             while Present (M) loop
8584                Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
8585                Next_Elmt (M);
8586             end loop;
8587          end if;
8588
8589          return New_Elist;
8590       end Copy_Elist_With_Replacement;
8591
8592       ---------------------------------
8593       -- Copy_Itype_With_Replacement --
8594       ---------------------------------
8595
8596       --  This routine exactly parallels its phase one analog Visit_Itype,
8597
8598       procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
8599       begin
8600          --  Translate Next_Entity, Scope and Etype fields, in case they
8601          --  reference entities that have been mapped into copies.
8602
8603          Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
8604          Set_Etype       (New_Itype, Assoc (Etype       (New_Itype)));
8605
8606          if Present (New_Scope) then
8607             Set_Scope    (New_Itype, New_Scope);
8608          else
8609             Set_Scope    (New_Itype, Assoc (Scope       (New_Itype)));
8610          end if;
8611
8612          --  Copy referenced fields
8613
8614          if Is_Discrete_Type (New_Itype) then
8615             Set_Scalar_Range (New_Itype,
8616               Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
8617
8618          elsif Has_Discriminants (Base_Type (New_Itype)) then
8619             Set_Discriminant_Constraint (New_Itype,
8620               Copy_Elist_With_Replacement
8621                 (Discriminant_Constraint (New_Itype)));
8622
8623          elsif Is_Array_Type (New_Itype) then
8624             if Present (First_Index (New_Itype)) then
8625                Set_First_Index (New_Itype,
8626                  First (Copy_List_With_Replacement
8627                          (List_Containing (First_Index (New_Itype)))));
8628             end if;
8629
8630             if Is_Packed (New_Itype) then
8631                Set_Packed_Array_Type (New_Itype,
8632                  Copy_Node_With_Replacement
8633                    (Packed_Array_Type (New_Itype)));
8634             end if;
8635          end if;
8636       end Copy_Itype_With_Replacement;
8637
8638       --------------------------------
8639       -- Copy_List_With_Replacement --
8640       --------------------------------
8641
8642       function Copy_List_With_Replacement
8643         (Old_List : List_Id) return List_Id
8644       is
8645          New_List : List_Id;
8646          E        : Node_Id;
8647
8648       begin
8649          if Old_List = No_List then
8650             return No_List;
8651
8652          else
8653             New_List := Empty_List;
8654
8655             E := First (Old_List);
8656             while Present (E) loop
8657                Append (Copy_Node_With_Replacement (E), New_List);
8658                Next (E);
8659             end loop;
8660
8661             return New_List;
8662          end if;
8663       end Copy_List_With_Replacement;
8664
8665       --------------------------------
8666       -- Copy_Node_With_Replacement --
8667       --------------------------------
8668
8669       function Copy_Node_With_Replacement
8670         (Old_Node : Node_Id) return Node_Id
8671       is
8672          New_Node : Node_Id;
8673
8674          procedure Adjust_Named_Associations
8675            (Old_Node : Node_Id;
8676             New_Node : Node_Id);
8677          --  If a call node has named associations, these are chained through
8678          --  the First_Named_Actual, Next_Named_Actual links. These must be
8679          --  propagated separately to the new parameter list, because these
8680          --  are not syntactic fields.
8681
8682          function Copy_Field_With_Replacement
8683            (Field : Union_Id) return Union_Id;
8684          --  Given Field, which is a field of Old_Node, return a copy of it
8685          --  if it is a syntactic field (i.e. its parent is Node), setting
8686          --  the parent of the copy to poit to New_Node. Otherwise returns
8687          --  the field (possibly mapped if it is an entity).
8688
8689          -------------------------------
8690          -- Adjust_Named_Associations --
8691          -------------------------------
8692
8693          procedure Adjust_Named_Associations
8694            (Old_Node : Node_Id;
8695             New_Node : Node_Id)
8696          is
8697             Old_E : Node_Id;
8698             New_E : Node_Id;
8699
8700             Old_Next : Node_Id;
8701             New_Next : Node_Id;
8702
8703          begin
8704             Old_E := First (Parameter_Associations (Old_Node));
8705             New_E := First (Parameter_Associations (New_Node));
8706             while Present (Old_E) loop
8707                if Nkind (Old_E) = N_Parameter_Association
8708                  and then Present (Next_Named_Actual (Old_E))
8709                then
8710                   if First_Named_Actual (Old_Node)
8711                     =  Explicit_Actual_Parameter (Old_E)
8712                   then
8713                      Set_First_Named_Actual
8714                        (New_Node, Explicit_Actual_Parameter (New_E));
8715                   end if;
8716
8717                   --  Now scan parameter list from the beginning,to locate
8718                   --  next named actual, which can be out of order.
8719
8720                   Old_Next := First (Parameter_Associations (Old_Node));
8721                   New_Next := First (Parameter_Associations (New_Node));
8722
8723                   while Nkind (Old_Next) /= N_Parameter_Association
8724                     or else  Explicit_Actual_Parameter (Old_Next)
8725                       /= Next_Named_Actual (Old_E)
8726                   loop
8727                      Next (Old_Next);
8728                      Next (New_Next);
8729                   end loop;
8730
8731                   Set_Next_Named_Actual
8732                     (New_E, Explicit_Actual_Parameter (New_Next));
8733                end if;
8734
8735                Next (Old_E);
8736                Next (New_E);
8737             end loop;
8738          end Adjust_Named_Associations;
8739
8740          ---------------------------------
8741          -- Copy_Field_With_Replacement --
8742          ---------------------------------
8743
8744          function Copy_Field_With_Replacement
8745            (Field : Union_Id) return Union_Id
8746          is
8747          begin
8748             if Field = Union_Id (Empty) then
8749                return Field;
8750
8751             elsif Field in Node_Range then
8752                declare
8753                   Old_N : constant Node_Id := Node_Id (Field);
8754                   New_N : Node_Id;
8755
8756                begin
8757                   --  If syntactic field, as indicated by the parent pointer
8758                   --  being set, then copy the referenced node recursively.
8759
8760                   if Parent (Old_N) = Old_Node then
8761                      New_N := Copy_Node_With_Replacement (Old_N);
8762
8763                      if New_N /= Old_N then
8764                         Set_Parent (New_N, New_Node);
8765                      end if;
8766
8767                   --  For semantic fields, update possible entity reference
8768                   --  from the replacement map.
8769
8770                   else
8771                      New_N := Assoc (Old_N);
8772                   end if;
8773
8774                   return Union_Id (New_N);
8775                end;
8776
8777             elsif Field in List_Range then
8778                declare
8779                   Old_L : constant List_Id := List_Id (Field);
8780                   New_L : List_Id;
8781
8782                begin
8783                   --  If syntactic field, as indicated by the parent pointer,
8784                   --  then recursively copy the entire referenced list.
8785
8786                   if Parent (Old_L) = Old_Node then
8787                      New_L := Copy_List_With_Replacement (Old_L);
8788                      Set_Parent (New_L, New_Node);
8789
8790                   --  For semantic list, just returned unchanged
8791
8792                   else
8793                      New_L := Old_L;
8794                   end if;
8795
8796                   return Union_Id (New_L);
8797                end;
8798
8799             --  Anything other than a list or a node is returned unchanged
8800
8801             else
8802                return Field;
8803             end if;
8804          end Copy_Field_With_Replacement;
8805
8806       --  Start of processing for Copy_Node_With_Replacement
8807
8808       begin
8809          if Old_Node <= Empty_Or_Error then
8810             return Old_Node;
8811
8812          elsif Has_Extension (Old_Node) then
8813             return Assoc (Old_Node);
8814
8815          else
8816             New_Node := New_Copy (Old_Node);
8817
8818             --  If the node we are copying is the associated node of a
8819             --  previously copied Itype, then adjust the associated node
8820             --  of the copy of that Itype accordingly.
8821
8822             if Present (Actual_Map) then
8823                declare
8824                   E   : Elmt_Id;
8825                   Ent : Entity_Id;
8826
8827                begin
8828                   --  Case of hash table used
8829
8830                   if NCT_Hash_Tables_Used then
8831                      Ent := NCT_Itype_Assoc.Get (Old_Node);
8832
8833                      if Present (Ent) then
8834                         Set_Associated_Node_For_Itype (Ent, New_Node);
8835                      end if;
8836
8837                   --  Case of no hash table used
8838
8839                   else
8840                      E := First_Elmt (Actual_Map);
8841                      while Present (E) loop
8842                         if Is_Itype (Node (E))
8843                           and then
8844                             Old_Node = Associated_Node_For_Itype (Node (E))
8845                         then
8846                            Set_Associated_Node_For_Itype
8847                              (Node (Next_Elmt (E)), New_Node);
8848                         end if;
8849
8850                         E := Next_Elmt (Next_Elmt (E));
8851                      end loop;
8852                   end if;
8853                end;
8854             end if;
8855
8856             --  Recursively copy descendents
8857
8858             Set_Field1
8859               (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
8860             Set_Field2
8861               (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
8862             Set_Field3
8863               (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
8864             Set_Field4
8865               (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
8866             Set_Field5
8867               (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
8868
8869             --  Adjust Sloc of new node if necessary
8870
8871             if New_Sloc /= No_Location then
8872                Set_Sloc (New_Node, New_Sloc);
8873
8874                --  If we adjust the Sloc, then we are essentially making
8875                --  a completely new node, so the Comes_From_Source flag
8876                --  should be reset to the proper default value.
8877
8878                Nodes.Table (New_Node).Comes_From_Source :=
8879                  Default_Node.Comes_From_Source;
8880             end if;
8881
8882             --  If the node is call and has named associations,
8883             --  set the corresponding links in the copy.
8884
8885             if (Nkind (Old_Node) = N_Function_Call
8886                  or else Nkind (Old_Node) = N_Entry_Call_Statement
8887                  or else
8888                    Nkind (Old_Node) = N_Procedure_Call_Statement)
8889               and then Present (First_Named_Actual (Old_Node))
8890             then
8891                Adjust_Named_Associations (Old_Node, New_Node);
8892             end if;
8893
8894             --  Reset First_Real_Statement for Handled_Sequence_Of_Statements.
8895             --  The replacement mechanism applies to entities, and is not used
8896             --  here. Eventually we may need a more general graph-copying
8897             --  routine. For now, do a sequential search to find desired node.
8898
8899             if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
8900               and then Present (First_Real_Statement (Old_Node))
8901             then
8902                declare
8903                   Old_F  : constant Node_Id := First_Real_Statement (Old_Node);
8904                   N1, N2 : Node_Id;
8905
8906                begin
8907                   N1 := First (Statements (Old_Node));
8908                   N2 := First (Statements (New_Node));
8909
8910                   while N1 /= Old_F loop
8911                      Next (N1);
8912                      Next (N2);
8913                   end loop;
8914
8915                   Set_First_Real_Statement (New_Node, N2);
8916                end;
8917             end if;
8918          end if;
8919
8920          --  All done, return copied node
8921
8922          return New_Node;
8923       end Copy_Node_With_Replacement;
8924
8925       -----------------
8926       -- Visit_Elist --
8927       -----------------
8928
8929       procedure Visit_Elist (E : Elist_Id) is
8930          Elmt : Elmt_Id;
8931       begin
8932          if Present (E) then
8933             Elmt := First_Elmt (E);
8934
8935             while Elmt /= No_Elmt loop
8936                Visit_Node (Node (Elmt));
8937                Next_Elmt (Elmt);
8938             end loop;
8939          end if;
8940       end Visit_Elist;
8941
8942       -----------------
8943       -- Visit_Field --
8944       -----------------
8945
8946       procedure Visit_Field (F : Union_Id; N : Node_Id) is
8947       begin
8948          if F = Union_Id (Empty) then
8949             return;
8950
8951          elsif F in Node_Range then
8952
8953             --  Copy node if it is syntactic, i.e. its parent pointer is
8954             --  set to point to the field that referenced it (certain
8955             --  Itypes will also meet this criterion, which is fine, since
8956             --  these are clearly Itypes that do need to be copied, since
8957             --  we are copying their parent.)
8958
8959             if Parent (Node_Id (F)) = N then
8960                Visit_Node (Node_Id (F));
8961                return;
8962
8963             --  Another case, if we are pointing to an Itype, then we want
8964             --  to copy it if its associated node is somewhere in the tree
8965             --  being copied.
8966
8967             --  Note: the exclusion of self-referential copies is just an
8968             --  optimization, since the search of the already copied list
8969             --  would catch it, but it is a common case (Etype pointing
8970             --  to itself for an Itype that is a base type).
8971
8972             elsif Has_Extension (Node_Id (F))
8973               and then Is_Itype (Entity_Id (F))
8974               and then Node_Id (F) /= N
8975             then
8976                declare
8977                   P : Node_Id;
8978
8979                begin
8980                   P := Associated_Node_For_Itype (Node_Id (F));
8981                   while Present (P) loop
8982                      if P = Source then
8983                         Visit_Node (Node_Id (F));
8984                         return;
8985                      else
8986                         P := Parent (P);
8987                      end if;
8988                   end loop;
8989
8990                   --  An Itype whose parent is not being copied definitely
8991                   --  should NOT be copied, since it does not belong in any
8992                   --  sense to the copied subtree.
8993
8994                   return;
8995                end;
8996             end if;
8997
8998          elsif F in List_Range
8999            and then Parent (List_Id (F)) = N
9000          then
9001             Visit_List (List_Id (F));
9002             return;
9003          end if;
9004       end Visit_Field;
9005
9006       -----------------
9007       -- Visit_Itype --
9008       -----------------
9009
9010       procedure Visit_Itype (Old_Itype : Entity_Id) is
9011          New_Itype : Entity_Id;
9012          E         : Elmt_Id;
9013          Ent       : Entity_Id;
9014
9015       begin
9016          --  Itypes that describe the designated type of access to subprograms
9017          --  have the structure of subprogram declarations, with signatures,
9018          --  etc. Either we duplicate the signatures completely, or choose to
9019          --  share such itypes, which is fine because their elaboration will
9020          --  have no side effects.
9021
9022          if Ekind (Old_Itype) = E_Subprogram_Type then
9023             return;
9024          end if;
9025
9026          New_Itype := New_Copy (Old_Itype);
9027
9028          --  The new Itype has all the attributes of the old one, and
9029          --  we just copy the contents of the entity. However, the back-end
9030          --  needs different names for debugging purposes, so we create a
9031          --  new internal name for it in all cases.
9032
9033          Set_Chars (New_Itype, New_Internal_Name ('T'));
9034
9035          --  If our associated node is an entity that has already been copied,
9036          --  then set the associated node of the copy to point to the right
9037          --  copy. If we have copied an Itype that is itself the associated
9038          --  node of some previously copied Itype, then we set the right
9039          --  pointer in the other direction.
9040
9041          if Present (Actual_Map) then
9042
9043             --  Case of hash tables used
9044
9045             if NCT_Hash_Tables_Used then
9046
9047                Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
9048
9049                if Present (Ent) then
9050                   Set_Associated_Node_For_Itype (New_Itype, Ent);
9051                end if;
9052
9053                Ent := NCT_Itype_Assoc.Get (Old_Itype);
9054                if Present (Ent) then
9055                   Set_Associated_Node_For_Itype (Ent, New_Itype);
9056
9057                --  If the hash table has no association for this Itype and
9058                --  its associated node, enter one now.
9059
9060                else
9061                   NCT_Itype_Assoc.Set
9062                     (Associated_Node_For_Itype (Old_Itype), New_Itype);
9063                end if;
9064
9065             --  Case of hash tables not used
9066
9067             else
9068                E := First_Elmt (Actual_Map);
9069                while Present (E) loop
9070                   if Associated_Node_For_Itype (Old_Itype) = Node (E) then
9071                      Set_Associated_Node_For_Itype
9072                        (New_Itype, Node (Next_Elmt (E)));
9073                   end if;
9074
9075                   if Is_Type (Node (E))
9076                     and then
9077                       Old_Itype = Associated_Node_For_Itype (Node (E))
9078                   then
9079                      Set_Associated_Node_For_Itype
9080                        (Node (Next_Elmt (E)), New_Itype);
9081                   end if;
9082
9083                   E := Next_Elmt (Next_Elmt (E));
9084                end loop;
9085             end if;
9086          end if;
9087
9088          if Present (Freeze_Node (New_Itype)) then
9089             Set_Is_Frozen (New_Itype, False);
9090             Set_Freeze_Node (New_Itype, Empty);
9091          end if;
9092
9093          --  Add new association to map
9094
9095          if No (Actual_Map) then
9096             Actual_Map := New_Elmt_List;
9097          end if;
9098
9099          Append_Elmt (Old_Itype, Actual_Map);
9100          Append_Elmt (New_Itype, Actual_Map);
9101
9102          if NCT_Hash_Tables_Used then
9103             NCT_Assoc.Set (Old_Itype, New_Itype);
9104
9105          else
9106             NCT_Table_Entries := NCT_Table_Entries + 1;
9107
9108             if NCT_Table_Entries > NCT_Hash_Threshold then
9109                Build_NCT_Hash_Tables;
9110             end if;
9111          end if;
9112
9113          --  If a record subtype is simply copied, the entity list will be
9114          --  shared. Thus cloned_Subtype must be set to indicate the sharing.
9115
9116          if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
9117             Set_Cloned_Subtype (New_Itype, Old_Itype);
9118          end if;
9119
9120          --  Visit descendents that eventually get copied
9121
9122          Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
9123
9124          if Is_Discrete_Type (Old_Itype) then
9125             Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
9126
9127          elsif Has_Discriminants (Base_Type (Old_Itype)) then
9128             --  ??? This should involve call to Visit_Field
9129             Visit_Elist (Discriminant_Constraint (Old_Itype));
9130
9131          elsif Is_Array_Type (Old_Itype) then
9132             if Present (First_Index (Old_Itype)) then
9133                Visit_Field (Union_Id (List_Containing
9134                                 (First_Index (Old_Itype))),
9135                             Old_Itype);
9136             end if;
9137
9138             if Is_Packed (Old_Itype) then
9139                Visit_Field (Union_Id (Packed_Array_Type (Old_Itype)),
9140                             Old_Itype);
9141             end if;
9142          end if;
9143       end Visit_Itype;
9144
9145       ----------------
9146       -- Visit_List --
9147       ----------------
9148
9149       procedure Visit_List (L : List_Id) is
9150          N : Node_Id;
9151       begin
9152          if L /= No_List then
9153             N := First (L);
9154
9155             while Present (N) loop
9156                Visit_Node (N);
9157                Next (N);
9158             end loop;
9159          end if;
9160       end Visit_List;
9161
9162       ----------------
9163       -- Visit_Node --
9164       ----------------
9165
9166       procedure Visit_Node (N : Node_Or_Entity_Id) is
9167
9168       --  Start of processing for Visit_Node
9169
9170       begin
9171          --  Handle case of an Itype, which must be copied
9172
9173          if Has_Extension (N)
9174            and then Is_Itype (N)
9175          then
9176             --  Nothing to do if already in the list. This can happen with an
9177             --  Itype entity that appears more than once in the tree.
9178             --  Note that we do not want to visit descendents in this case.
9179
9180             --  Test for already in list when hash table is used
9181
9182             if NCT_Hash_Tables_Used then
9183                if Present (NCT_Assoc.Get (Entity_Id (N))) then
9184                   return;
9185                end if;
9186
9187             --  Test for already in list when hash table not used
9188
9189             else
9190                declare
9191                   E : Elmt_Id;
9192                begin
9193                   if Present (Actual_Map) then
9194                      E := First_Elmt (Actual_Map);
9195                      while Present (E) loop
9196                         if Node (E) = N then
9197                            return;
9198                         else
9199                            E := Next_Elmt (Next_Elmt (E));
9200                         end if;
9201                      end loop;
9202                   end if;
9203                end;
9204             end if;
9205
9206             Visit_Itype (N);
9207          end if;
9208
9209          --  Visit descendents
9210
9211          Visit_Field (Field1 (N), N);
9212          Visit_Field (Field2 (N), N);
9213          Visit_Field (Field3 (N), N);
9214          Visit_Field (Field4 (N), N);
9215          Visit_Field (Field5 (N), N);
9216       end Visit_Node;
9217
9218    --  Start of processing for New_Copy_Tree
9219
9220    begin
9221       Actual_Map := Map;
9222
9223       --  See if we should use hash table
9224
9225       if No (Actual_Map) then
9226          NCT_Hash_Tables_Used := False;
9227
9228       else
9229          declare
9230             Elmt : Elmt_Id;
9231
9232          begin
9233             NCT_Table_Entries := 0;
9234
9235             Elmt := First_Elmt (Actual_Map);
9236             while Present (Elmt) loop
9237                NCT_Table_Entries := NCT_Table_Entries + 1;
9238                Next_Elmt (Elmt);
9239                Next_Elmt (Elmt);
9240             end loop;
9241
9242             if NCT_Table_Entries > NCT_Hash_Threshold then
9243                Build_NCT_Hash_Tables;
9244             else
9245                NCT_Hash_Tables_Used := False;
9246             end if;
9247          end;
9248       end if;
9249
9250       --  Hash table set up if required, now start phase one by visiting
9251       --  top node (we will recursively visit the descendents).
9252
9253       Visit_Node (Source);
9254
9255       --  Now the second phase of the copy can start. First we process
9256       --  all the mapped entities, copying their descendents.
9257
9258       if Present (Actual_Map) then
9259          declare
9260             Elmt      : Elmt_Id;
9261             New_Itype : Entity_Id;
9262          begin
9263             Elmt := First_Elmt (Actual_Map);
9264             while Present (Elmt) loop
9265                Next_Elmt (Elmt);
9266                New_Itype := Node (Elmt);
9267                Copy_Itype_With_Replacement (New_Itype);
9268                Next_Elmt (Elmt);
9269             end loop;
9270          end;
9271       end if;
9272
9273       --  Now we can copy the actual tree
9274
9275       return Copy_Node_With_Replacement (Source);
9276    end New_Copy_Tree;
9277
9278    -------------------------
9279    -- New_External_Entity --
9280    -------------------------
9281
9282    function New_External_Entity
9283      (Kind         : Entity_Kind;
9284       Scope_Id     : Entity_Id;
9285       Sloc_Value   : Source_Ptr;
9286       Related_Id   : Entity_Id;
9287       Suffix       : Character;
9288       Suffix_Index : Nat := 0;
9289       Prefix       : Character := ' ') return Entity_Id
9290    is
9291       N : constant Entity_Id :=
9292             Make_Defining_Identifier (Sloc_Value,
9293               New_External_Name
9294                 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
9295
9296    begin
9297       Set_Ekind          (N, Kind);
9298       Set_Is_Internal    (N, True);
9299       Append_Entity      (N, Scope_Id);
9300       Set_Public_Status  (N);
9301
9302       if Kind in Type_Kind then
9303          Init_Size_Align (N);
9304       end if;
9305
9306       return N;
9307    end New_External_Entity;
9308
9309    -------------------------
9310    -- New_Internal_Entity --
9311    -------------------------
9312
9313    function New_Internal_Entity
9314      (Kind       : Entity_Kind;
9315       Scope_Id   : Entity_Id;
9316       Sloc_Value : Source_Ptr;
9317       Id_Char    : Character) return Entity_Id
9318    is
9319       N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
9320
9321    begin
9322       Set_Ekind          (N, Kind);
9323       Set_Is_Internal    (N, True);
9324       Append_Entity      (N, Scope_Id);
9325
9326       if Kind in Type_Kind then
9327          Init_Size_Align (N);
9328       end if;
9329
9330       return N;
9331    end New_Internal_Entity;
9332
9333    -----------------
9334    -- Next_Actual --
9335    -----------------
9336
9337    function Next_Actual (Actual_Id : Node_Id) return Node_Id is
9338       N  : Node_Id;
9339
9340    begin
9341       --  If we are pointing at a positional parameter, it is a member of a
9342       --  node list (the list of parameters), and the next parameter is the
9343       --  next node on the list, unless we hit a parameter association, then
9344       --  we shift to using the chain whose head is the First_Named_Actual in
9345       --  the parent, and then is threaded using the Next_Named_Actual of the
9346       --  Parameter_Association. All this fiddling is because the original node
9347       --  list is in the textual call order, and what we need is the
9348       --  declaration order.
9349
9350       if Is_List_Member (Actual_Id) then
9351          N := Next (Actual_Id);
9352
9353          if Nkind (N) = N_Parameter_Association then
9354             return First_Named_Actual (Parent (Actual_Id));
9355          else
9356             return N;
9357          end if;
9358
9359       else
9360          return Next_Named_Actual (Parent (Actual_Id));
9361       end if;
9362    end Next_Actual;
9363
9364    procedure Next_Actual (Actual_Id : in out Node_Id) is
9365    begin
9366       Actual_Id := Next_Actual (Actual_Id);
9367    end Next_Actual;
9368
9369    -----------------------
9370    -- Normalize_Actuals --
9371    -----------------------
9372
9373    --  Chain actuals according to formals of subprogram. If there are no named
9374    --  associations, the chain is simply the list of Parameter Associations,
9375    --  since the order is the same as the declaration order. If there are named
9376    --  associations, then the First_Named_Actual field in the N_Function_Call
9377    --  or N_Procedure_Call_Statement node points to the Parameter_Association
9378    --  node for the parameter that comes first in declaration order. The
9379    --  remaining named parameters are then chained in declaration order using
9380    --  Next_Named_Actual.
9381
9382    --  This routine also verifies that the number of actuals is compatible with
9383    --  the number and default values of formals, but performs no type checking
9384    --  (type checking is done by the caller).
9385
9386    --  If the matching succeeds, Success is set to True and the caller proceeds
9387    --  with type-checking. If the match is unsuccessful, then Success is set to
9388    --  False, and the caller attempts a different interpretation, if there is
9389    --  one.
9390
9391    --  If the flag Report is on, the call is not overloaded, and a failure to
9392    --  match can be reported here, rather than in the caller.
9393
9394    procedure Normalize_Actuals
9395      (N       : Node_Id;
9396       S       : Entity_Id;
9397       Report  : Boolean;
9398       Success : out Boolean)
9399    is
9400       Actuals     : constant List_Id := Parameter_Associations (N);
9401       Actual      : Node_Id := Empty;
9402       Formal      : Entity_Id;
9403       Last        : Node_Id := Empty;
9404       First_Named : Node_Id := Empty;
9405       Found       : Boolean;
9406
9407       Formals_To_Match : Integer := 0;
9408       Actuals_To_Match : Integer := 0;
9409
9410       procedure Chain (A : Node_Id);
9411       --  Add named actual at the proper place in the list, using the
9412       --  Next_Named_Actual link.
9413
9414       function Reporting return Boolean;
9415       --  Determines if an error is to be reported. To report an error, we
9416       --  need Report to be True, and also we do not report errors caused
9417       --  by calls to init procs that occur within other init procs. Such
9418       --  errors must always be cascaded errors, since if all the types are
9419       --  declared correctly, the compiler will certainly build decent calls!
9420
9421       -----------
9422       -- Chain --
9423       -----------
9424
9425       procedure Chain (A : Node_Id) is
9426       begin
9427          if No (Last) then
9428
9429             --  Call node points to first actual in list
9430
9431             Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
9432
9433          else
9434             Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
9435          end if;
9436
9437          Last := A;
9438          Set_Next_Named_Actual (Last, Empty);
9439       end Chain;
9440
9441       ---------------
9442       -- Reporting --
9443       ---------------
9444
9445       function Reporting return Boolean is
9446       begin
9447          if not Report then
9448             return False;
9449
9450          elsif not Within_Init_Proc then
9451             return True;
9452
9453          elsif Is_Init_Proc (Entity (Name (N))) then
9454             return False;
9455
9456          else
9457             return True;
9458          end if;
9459       end Reporting;
9460
9461    --  Start of processing for Normalize_Actuals
9462
9463    begin
9464       if Is_Access_Type (S) then
9465
9466          --  The name in the call is a function call that returns an access
9467          --  to subprogram. The designated type has the list of formals.
9468
9469          Formal := First_Formal (Designated_Type (S));
9470       else
9471          Formal := First_Formal (S);
9472       end if;
9473
9474       while Present (Formal) loop
9475          Formals_To_Match := Formals_To_Match + 1;
9476          Next_Formal (Formal);
9477       end loop;
9478
9479       --  Find if there is a named association, and verify that no positional
9480       --  associations appear after named ones.
9481
9482       if Present (Actuals) then
9483          Actual := First (Actuals);
9484       end if;
9485
9486       while Present (Actual)
9487         and then Nkind (Actual) /= N_Parameter_Association
9488       loop
9489          Actuals_To_Match := Actuals_To_Match + 1;
9490          Next (Actual);
9491       end loop;
9492
9493       if No (Actual) and Actuals_To_Match = Formals_To_Match then
9494
9495          --  Most common case: positional notation, no defaults
9496
9497          Success := True;
9498          return;
9499
9500       elsif Actuals_To_Match > Formals_To_Match then
9501
9502          --  Too many actuals: will not work
9503
9504          if Reporting then
9505             if Is_Entity_Name (Name (N)) then
9506                Error_Msg_N ("too many arguments in call to&", Name (N));
9507             else
9508                Error_Msg_N ("too many arguments in call", N);
9509             end if;
9510          end if;
9511
9512          Success := False;
9513          return;
9514       end if;
9515
9516       First_Named := Actual;
9517
9518       while Present (Actual) loop
9519          if Nkind (Actual) /= N_Parameter_Association then
9520             Error_Msg_N
9521               ("positional parameters not allowed after named ones", Actual);
9522             Success := False;
9523             return;
9524
9525          else
9526             Actuals_To_Match := Actuals_To_Match + 1;
9527          end if;
9528
9529          Next (Actual);
9530       end loop;
9531
9532       if Present (Actuals) then
9533          Actual := First (Actuals);
9534       end if;
9535
9536       Formal := First_Formal (S);
9537       while Present (Formal) loop
9538
9539          --  Match the formals in order. If the corresponding actual is
9540          --  positional, nothing to do. Else scan the list of named actuals
9541          --  to find the one with the right name.
9542
9543          if Present (Actual)
9544            and then Nkind (Actual) /= N_Parameter_Association
9545          then
9546             Next (Actual);
9547             Actuals_To_Match := Actuals_To_Match - 1;
9548             Formals_To_Match := Formals_To_Match - 1;
9549
9550          else
9551             --  For named parameters, search the list of actuals to find
9552             --  one that matches the next formal name.
9553
9554             Actual := First_Named;
9555             Found  := False;
9556             while Present (Actual) loop
9557                if Chars (Selector_Name (Actual)) = Chars (Formal) then
9558                   Found := True;
9559                   Chain (Actual);
9560                   Actuals_To_Match := Actuals_To_Match - 1;
9561                   Formals_To_Match := Formals_To_Match - 1;
9562                   exit;
9563                end if;
9564
9565                Next (Actual);
9566             end loop;
9567
9568             if not Found then
9569                if Ekind (Formal) /= E_In_Parameter
9570                  or else No (Default_Value (Formal))
9571                then
9572                   if Reporting then
9573                      if (Comes_From_Source (S)
9574                           or else Sloc (S) = Standard_Location)
9575                        and then Is_Overloadable (S)
9576                      then
9577                         if No (Actuals)
9578                           and then
9579                            (Nkind (Parent (N)) = N_Procedure_Call_Statement
9580                              or else
9581                            (Nkind (Parent (N)) = N_Function_Call
9582                              or else
9583                             Nkind (Parent (N)) = N_Parameter_Association))
9584                           and then Ekind (S) /= E_Function
9585                         then
9586                            Set_Etype (N, Etype (S));
9587                         else
9588                            Error_Msg_Name_1 := Chars (S);
9589                            Error_Msg_Sloc := Sloc (S);
9590                            Error_Msg_NE
9591                              ("missing argument for parameter & " &
9592                                 "in call to % declared #", N, Formal);
9593                         end if;
9594
9595                      elsif Is_Overloadable (S) then
9596                         Error_Msg_Name_1 := Chars (S);
9597
9598                         --  Point to type derivation that generated the
9599                         --  operation.
9600
9601                         Error_Msg_Sloc := Sloc (Parent (S));
9602
9603                         Error_Msg_NE
9604                           ("missing argument for parameter & " &
9605                              "in call to % (inherited) #", N, Formal);
9606
9607                      else
9608                         Error_Msg_NE
9609                           ("missing argument for parameter &", N, Formal);
9610                      end if;
9611                   end if;
9612
9613                   Success := False;
9614                   return;
9615
9616                else
9617                   Formals_To_Match := Formals_To_Match - 1;
9618                end if;
9619             end if;
9620          end if;
9621
9622          Next_Formal (Formal);
9623       end loop;
9624
9625       if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
9626          Success := True;
9627          return;
9628
9629       else
9630          if Reporting then
9631
9632             --  Find some superfluous named actual that did not get
9633             --  attached to the list of associations.
9634
9635             Actual := First (Actuals);
9636             while Present (Actual) loop
9637                if Nkind (Actual) = N_Parameter_Association
9638                  and then Actual /= Last
9639                  and then No (Next_Named_Actual (Actual))
9640                then
9641                   Error_Msg_N ("unmatched actual & in call",
9642                     Selector_Name (Actual));
9643                   exit;
9644                end if;
9645
9646                Next (Actual);
9647             end loop;
9648          end if;
9649
9650          Success := False;
9651          return;
9652       end if;
9653    end Normalize_Actuals;
9654
9655    --------------------------------
9656    -- Note_Possible_Modification --
9657    --------------------------------
9658
9659    procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
9660       Modification_Comes_From_Source : constant Boolean :=
9661                                          Comes_From_Source (Parent (N));
9662
9663       Ent : Entity_Id;
9664       Exp : Node_Id;
9665
9666    begin
9667       --  Loop to find referenced entity, if there is one
9668
9669       Exp := N;
9670       loop
9671          <<Continue>>
9672          Ent := Empty;
9673
9674          if Is_Entity_Name (Exp) then
9675             Ent := Entity (Exp);
9676
9677             --  If the entity is missing, it is an undeclared identifier,
9678             --  and there is nothing to annotate.
9679
9680             if No (Ent) then
9681                return;
9682             end if;
9683
9684          elsif Nkind (Exp) = N_Explicit_Dereference then
9685             declare
9686                P : constant Node_Id := Prefix (Exp);
9687
9688             begin
9689                if Nkind (P) = N_Selected_Component
9690                  and then Present (
9691                    Entry_Formal (Entity (Selector_Name (P))))
9692                then
9693                   --  Case of a reference to an entry formal
9694
9695                   Ent := Entry_Formal (Entity (Selector_Name (P)));
9696
9697                elsif Nkind (P) = N_Identifier
9698                  and then Nkind (Parent (Entity (P))) = N_Object_Declaration
9699                  and then Present (Expression (Parent (Entity (P))))
9700                  and then Nkind (Expression (Parent (Entity (P))))
9701                    = N_Reference
9702                then
9703                   --  Case of a reference to a value on which side effects have
9704                   --  been removed.
9705
9706                   Exp := Prefix (Expression (Parent (Entity (P))));
9707                   goto Continue;
9708
9709                else
9710                   return;
9711
9712                end if;
9713             end;
9714
9715          elsif     Nkind (Exp) = N_Type_Conversion
9716            or else Nkind (Exp) = N_Unchecked_Type_Conversion
9717          then
9718             Exp := Expression (Exp);
9719             goto Continue;
9720
9721          elsif     Nkind (Exp) = N_Slice
9722            or else Nkind (Exp) = N_Indexed_Component
9723            or else Nkind (Exp) = N_Selected_Component
9724          then
9725             Exp := Prefix (Exp);
9726             goto Continue;
9727
9728          else
9729             return;
9730          end if;
9731
9732          --  Now look for entity being referenced
9733
9734          if Present (Ent) then
9735             if Is_Object (Ent) then
9736                if Comes_From_Source (Exp)
9737                  or else Modification_Comes_From_Source
9738                then
9739                   --  Give warning if pragma unmodified given and we are
9740                   --  sure this is a modification.
9741
9742                   if Has_Pragma_Unmodified (Ent) and then Sure then
9743                      Error_Msg_NE ("?pragma Unmodified given for &!", N, Ent);
9744                   end if;
9745
9746                   Set_Never_Set_In_Source (Ent, False);
9747                end if;
9748
9749                Set_Is_True_Constant (Ent, False);
9750                Set_Current_Value    (Ent, Empty);
9751                Set_Is_Known_Null    (Ent, False);
9752
9753                if not Can_Never_Be_Null (Ent) then
9754                   Set_Is_Known_Non_Null (Ent, False);
9755                end if;
9756
9757                --  Follow renaming chain
9758
9759                if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
9760                  and then Present (Renamed_Object (Ent))
9761                then
9762                   Exp := Renamed_Object (Ent);
9763                   goto Continue;
9764                end if;
9765
9766                --  Generate a reference only if the assignment comes from
9767                --  source. This excludes, for example, calls to a dispatching
9768                --  assignment operation when the left-hand side is tagged.
9769
9770                if Modification_Comes_From_Source then
9771                   Generate_Reference (Ent, Exp, 'm');
9772
9773                   --  If the target of the assignment is the bound variable
9774                   --  in an iterator, indicate that the corresponding array
9775                   --  or container is also modified.
9776
9777                   if Ada_Version >= Ada_2012
9778                     and then
9779                       Nkind (Parent (Ent)) = N_Iterator_Specification
9780                   then
9781                      declare
9782                         Domain : constant Node_Id := Name (Parent (Ent));
9783
9784                      begin
9785                         --  TBD : in the full version of the construct, the
9786                         --  domain of iteration can be given by an expression.
9787
9788                         if Is_Entity_Name (Domain) then
9789                            Generate_Reference      (Entity (Domain), Exp, 'm');
9790                            Set_Is_True_Constant    (Entity (Domain), False);
9791                            Set_Never_Set_In_Source (Entity (Domain), False);
9792                         end if;
9793                      end;
9794                   end if;
9795                end if;
9796
9797                Check_Nested_Access (Ent);
9798             end if;
9799
9800             Kill_Checks (Ent);
9801
9802             --  If we are sure this is a modification from source, and we know
9803             --  this modifies a constant, then give an appropriate warning.
9804
9805             if Overlays_Constant (Ent)
9806               and then Modification_Comes_From_Source
9807               and then Sure
9808             then
9809                declare
9810                   A : constant Node_Id := Address_Clause (Ent);
9811                begin
9812                   if Present (A) then
9813                      declare
9814                         Exp : constant Node_Id := Expression (A);
9815                      begin
9816                         if Nkind (Exp) = N_Attribute_Reference
9817                           and then Attribute_Name (Exp) = Name_Address
9818                           and then Is_Entity_Name (Prefix (Exp))
9819                         then
9820                            Error_Msg_Sloc := Sloc (A);
9821                            Error_Msg_NE
9822                              ("constant& may be modified via address clause#?",
9823                               N, Entity (Prefix (Exp)));
9824                         end if;
9825                      end;
9826                   end if;
9827                end;
9828             end if;
9829
9830             return;
9831          end if;
9832       end loop;
9833    end Note_Possible_Modification;
9834
9835    -------------------------
9836    -- Object_Access_Level --
9837    -------------------------
9838
9839    function Object_Access_Level (Obj : Node_Id) return Uint is
9840       E : Entity_Id;
9841
9842    --  Returns the static accessibility level of the view denoted by Obj. Note
9843    --  that the value returned is the result of a call to Scope_Depth. Only
9844    --  scope depths associated with dynamic scopes can actually be returned.
9845    --  Since only relative levels matter for accessibility checking, the fact
9846    --  that the distance between successive levels of accessibility is not
9847    --  always one is immaterial (invariant: if level(E2) is deeper than
9848    --  level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
9849
9850       function Reference_To (Obj : Node_Id) return Node_Id;
9851       --  An explicit dereference is created when removing side-effects from
9852       --  expressions for constraint checking purposes. In this case a local
9853       --  access type is created for it. The correct access level is that of
9854       --  the original source node. We detect this case by noting that the
9855       --  prefix of the dereference is created by an object declaration whose
9856       --  initial expression is a reference.
9857
9858       ------------------
9859       -- Reference_To --
9860       ------------------
9861
9862       function Reference_To (Obj : Node_Id) return Node_Id is
9863          Pref : constant Node_Id := Prefix (Obj);
9864       begin
9865          if Is_Entity_Name (Pref)
9866            and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
9867            and then Present (Expression (Parent (Entity (Pref))))
9868            and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
9869          then
9870             return (Prefix (Expression (Parent (Entity (Pref)))));
9871          else
9872             return Empty;
9873          end if;
9874       end Reference_To;
9875
9876    --  Start of processing for Object_Access_Level
9877
9878    begin
9879       if Is_Entity_Name (Obj) then
9880          E := Entity (Obj);
9881
9882          if Is_Prival (E) then
9883             E := Prival_Link (E);
9884          end if;
9885
9886          --  If E is a type then it denotes a current instance. For this case
9887          --  we add one to the normal accessibility level of the type to ensure
9888          --  that current instances are treated as always being deeper than
9889          --  than the level of any visible named access type (see 3.10.2(21)).
9890
9891          if Is_Type (E) then
9892             return Type_Access_Level (E) +  1;
9893
9894          elsif Present (Renamed_Object (E)) then
9895             return Object_Access_Level (Renamed_Object (E));
9896
9897          --  Similarly, if E is a component of the current instance of a
9898          --  protected type, any instance of it is assumed to be at a deeper
9899          --  level than the type. For a protected object (whose type is an
9900          --  anonymous protected type) its components are at the same level
9901          --  as the type itself.
9902
9903          elsif not Is_Overloadable (E)
9904            and then Ekind (Scope (E)) = E_Protected_Type
9905            and then Comes_From_Source (Scope (E))
9906          then
9907             return Type_Access_Level (Scope (E)) + 1;
9908
9909          else
9910             return Scope_Depth (Enclosing_Dynamic_Scope (E));
9911          end if;
9912
9913       elsif Nkind (Obj) = N_Selected_Component then
9914          if Is_Access_Type (Etype (Prefix (Obj))) then
9915             return Type_Access_Level (Etype (Prefix (Obj)));
9916          else
9917             return Object_Access_Level (Prefix (Obj));
9918          end if;
9919
9920       elsif Nkind (Obj) = N_Indexed_Component then
9921          if Is_Access_Type (Etype (Prefix (Obj))) then
9922             return Type_Access_Level (Etype (Prefix (Obj)));
9923          else
9924             return Object_Access_Level (Prefix (Obj));
9925          end if;
9926
9927       elsif Nkind (Obj) = N_Explicit_Dereference then
9928
9929          --  If the prefix is a selected access discriminant then we make a
9930          --  recursive call on the prefix, which will in turn check the level
9931          --  of the prefix object of the selected discriminant.
9932
9933          if Nkind (Prefix (Obj)) = N_Selected_Component
9934            and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
9935            and then
9936              Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
9937          then
9938             return Object_Access_Level (Prefix (Obj));
9939
9940          elsif not (Comes_From_Source (Obj)) then
9941             declare
9942                Ref : constant Node_Id := Reference_To (Obj);
9943             begin
9944                if Present (Ref) then
9945                   return Object_Access_Level (Ref);
9946                else
9947                   return Type_Access_Level (Etype (Prefix (Obj)));
9948                end if;
9949             end;
9950
9951          else
9952             return Type_Access_Level (Etype (Prefix (Obj)));
9953          end if;
9954
9955       elsif Nkind (Obj) = N_Type_Conversion
9956         or else Nkind (Obj) = N_Unchecked_Type_Conversion
9957       then
9958          return Object_Access_Level (Expression (Obj));
9959
9960       elsif Nkind (Obj) = N_Function_Call then
9961
9962          --  Function results are objects, so we get either the access level of
9963          --  the function or, in the case of an indirect call, the level of the
9964          --  access-to-subprogram type. (This code is used for Ada 95, but it
9965          --  looks wrong, because it seems that we should be checking the level
9966          --  of the call itself, even for Ada 95. However, using the Ada 2005
9967          --  version of the code causes regressions in several tests that are
9968          --  compiled with -gnat95. ???)
9969
9970          if Ada_Version < Ada_2005 then
9971             if Is_Entity_Name (Name (Obj)) then
9972                return Subprogram_Access_Level (Entity (Name (Obj)));
9973             else
9974                return Type_Access_Level (Etype (Prefix (Name (Obj))));
9975             end if;
9976
9977          --  For Ada 2005, the level of the result object of a function call is
9978          --  defined to be the level of the call's innermost enclosing master.
9979          --  We determine that by querying the depth of the innermost enclosing
9980          --  dynamic scope.
9981
9982          else
9983             Return_Master_Scope_Depth_Of_Call : declare
9984
9985                function Innermost_Master_Scope_Depth
9986                  (N : Node_Id) return Uint;
9987                --  Returns the scope depth of the given node's innermost
9988                --  enclosing dynamic scope (effectively the accessibility
9989                --  level of the innermost enclosing master).
9990
9991                ----------------------------------
9992                -- Innermost_Master_Scope_Depth --
9993                ----------------------------------
9994
9995                function Innermost_Master_Scope_Depth
9996                  (N : Node_Id) return Uint
9997                is
9998                   Node_Par : Node_Id := Parent (N);
9999
10000                begin
10001                   --  Locate the nearest enclosing node (by traversing Parents)
10002                   --  that Defining_Entity can be applied to, and return the
10003                   --  depth of that entity's nearest enclosing dynamic scope.
10004
10005                   while Present (Node_Par) loop
10006                      case Nkind (Node_Par) is
10007                         when N_Component_Declaration           |
10008                              N_Entry_Declaration               |
10009                              N_Formal_Object_Declaration       |
10010                              N_Formal_Type_Declaration         |
10011                              N_Full_Type_Declaration           |
10012                              N_Incomplete_Type_Declaration     |
10013                              N_Loop_Parameter_Specification    |
10014                              N_Object_Declaration              |
10015                              N_Protected_Type_Declaration      |
10016                              N_Private_Extension_Declaration   |
10017                              N_Private_Type_Declaration        |
10018                              N_Subtype_Declaration             |
10019                              N_Function_Specification          |
10020                              N_Procedure_Specification         |
10021                              N_Task_Type_Declaration           |
10022                              N_Body_Stub                       |
10023                              N_Generic_Instantiation           |
10024                              N_Proper_Body                     |
10025                              N_Implicit_Label_Declaration      |
10026                              N_Package_Declaration             |
10027                              N_Single_Task_Declaration         |
10028                              N_Subprogram_Declaration          |
10029                              N_Generic_Declaration             |
10030                              N_Renaming_Declaration            |
10031                              N_Block_Statement                 |
10032                              N_Formal_Subprogram_Declaration   |
10033                              N_Abstract_Subprogram_Declaration |
10034                              N_Entry_Body                      |
10035                              N_Exception_Declaration           |
10036                              N_Formal_Package_Declaration      |
10037                              N_Number_Declaration              |
10038                              N_Package_Specification           |
10039                              N_Parameter_Specification         |
10040                              N_Single_Protected_Declaration    |
10041                              N_Subunit                         =>
10042
10043                            return Scope_Depth
10044                                     (Nearest_Dynamic_Scope
10045                                        (Defining_Entity (Node_Par)));
10046
10047                         when others =>
10048                            null;
10049                      end case;
10050
10051                      Node_Par := Parent (Node_Par);
10052                   end loop;
10053
10054                   pragma Assert (False);
10055
10056                   --  Should never reach the following return
10057
10058                   return Scope_Depth (Current_Scope) + 1;
10059                end Innermost_Master_Scope_Depth;
10060
10061             --  Start of processing for Return_Master_Scope_Depth_Of_Call
10062
10063             begin
10064                return Innermost_Master_Scope_Depth (Obj);
10065             end Return_Master_Scope_Depth_Of_Call;
10066          end if;
10067
10068       --  For convenience we handle qualified expressions, even though
10069       --  they aren't technically object names.
10070
10071       elsif Nkind (Obj) = N_Qualified_Expression then
10072          return Object_Access_Level (Expression (Obj));
10073
10074       --  Otherwise return the scope level of Standard.
10075       --  (If there are cases that fall through
10076       --  to this point they will be treated as
10077       --  having global accessibility for now. ???)
10078
10079       else
10080          return Scope_Depth (Standard_Standard);
10081       end if;
10082    end Object_Access_Level;
10083
10084    --------------------------------------
10085    -- Original_Corresponding_Operation --
10086    --------------------------------------
10087
10088    function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
10089    is
10090       Typ : constant Entity_Id := Find_Dispatching_Type (S);
10091
10092    begin
10093       --  If S is an inherited primitive S2 the original corresponding
10094       --  operation of S is the original corresponding operation of S2
10095
10096       if Present (Alias (S))
10097         and then Find_Dispatching_Type (Alias (S)) /= Typ
10098       then
10099          return Original_Corresponding_Operation (Alias (S));
10100
10101       --  If S overrides an inherited subprogram S2 the original corresponding
10102       --  operation of S is the original corresponding operation of S2
10103
10104       elsif Present (Overridden_Operation (S)) then
10105          return Original_Corresponding_Operation (Overridden_Operation (S));
10106
10107       --  otherwise it is S itself
10108
10109       else
10110          return S;
10111       end if;
10112    end Original_Corresponding_Operation;
10113
10114    -----------------------
10115    -- Private_Component --
10116    -----------------------
10117
10118    function Private_Component (Type_Id : Entity_Id) return Entity_Id is
10119       Ancestor  : constant Entity_Id := Base_Type (Type_Id);
10120
10121       function Trace_Components
10122         (T     : Entity_Id;
10123          Check : Boolean) return Entity_Id;
10124       --  Recursive function that does the work, and checks against circular
10125       --  definition for each subcomponent type.
10126
10127       ----------------------
10128       -- Trace_Components --
10129       ----------------------
10130
10131       function Trace_Components
10132          (T     : Entity_Id;
10133           Check : Boolean) return Entity_Id
10134        is
10135          Btype     : constant Entity_Id := Base_Type (T);
10136          Component : Entity_Id;
10137          P         : Entity_Id;
10138          Candidate : Entity_Id := Empty;
10139
10140       begin
10141          if Check and then Btype = Ancestor then
10142             Error_Msg_N ("circular type definition", Type_Id);
10143             return Any_Type;
10144          end if;
10145
10146          if Is_Private_Type (Btype)
10147            and then not Is_Generic_Type (Btype)
10148          then
10149             if Present (Full_View (Btype))
10150               and then Is_Record_Type (Full_View (Btype))
10151               and then not Is_Frozen (Btype)
10152             then
10153                --  To indicate that the ancestor depends on a private type, the
10154                --  current Btype is sufficient. However, to check for circular
10155                --  definition we must recurse on the full view.
10156
10157                Candidate := Trace_Components (Full_View (Btype), True);
10158
10159                if Candidate = Any_Type then
10160                   return Any_Type;
10161                else
10162                   return Btype;
10163                end if;
10164
10165             else
10166                return Btype;
10167             end if;
10168
10169          elsif Is_Array_Type (Btype) then
10170             return Trace_Components (Component_Type (Btype), True);
10171
10172          elsif Is_Record_Type (Btype) then
10173             Component := First_Entity (Btype);
10174             while Present (Component) loop
10175
10176                --  Skip anonymous types generated by constrained components
10177
10178                if not Is_Type (Component) then
10179                   P := Trace_Components (Etype (Component), True);
10180
10181                   if Present (P) then
10182                      if P = Any_Type then
10183                         return P;
10184                      else
10185                         Candidate := P;
10186                      end if;
10187                   end if;
10188                end if;
10189
10190                Next_Entity (Component);
10191             end loop;
10192
10193             return Candidate;
10194
10195          else
10196             return Empty;
10197          end if;
10198       end Trace_Components;
10199
10200    --  Start of processing for Private_Component
10201
10202    begin
10203       return Trace_Components (Type_Id, False);
10204    end Private_Component;
10205
10206    ---------------------------
10207    -- Primitive_Names_Match --
10208    ---------------------------
10209
10210    function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
10211
10212       function Non_Internal_Name (E : Entity_Id) return Name_Id;
10213       --  Given an internal name, returns the corresponding non-internal name
10214
10215       ------------------------
10216       --  Non_Internal_Name --
10217       ------------------------
10218
10219       function Non_Internal_Name (E : Entity_Id) return Name_Id is
10220       begin
10221          Get_Name_String (Chars (E));
10222          Name_Len := Name_Len - 1;
10223          return Name_Find;
10224       end Non_Internal_Name;
10225
10226    --  Start of processing for Primitive_Names_Match
10227
10228    begin
10229       pragma Assert (Present (E1) and then Present (E2));
10230
10231       return Chars (E1) = Chars (E2)
10232         or else
10233            (not Is_Internal_Name (Chars (E1))
10234               and then Is_Internal_Name (Chars (E2))
10235               and then Non_Internal_Name (E2) = Chars (E1))
10236         or else
10237            (not Is_Internal_Name (Chars (E2))
10238               and then Is_Internal_Name (Chars (E1))
10239               and then Non_Internal_Name (E1) = Chars (E2))
10240         or else
10241            (Is_Predefined_Dispatching_Operation (E1)
10242               and then Is_Predefined_Dispatching_Operation (E2)
10243               and then Same_TSS (E1, E2))
10244         or else
10245            (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
10246    end Primitive_Names_Match;
10247
10248    -----------------------
10249    -- Process_End_Label --
10250    -----------------------
10251
10252    procedure Process_End_Label
10253      (N   : Node_Id;
10254       Typ : Character;
10255       Ent  : Entity_Id)
10256    is
10257       Loc  : Source_Ptr;
10258       Nam  : Node_Id;
10259       Scop : Entity_Id;
10260
10261       Label_Ref : Boolean;
10262       --  Set True if reference to end label itself is required
10263
10264       Endl : Node_Id;
10265       --  Gets set to the operator symbol or identifier that references the
10266       --  entity Ent. For the child unit case, this is the identifier from the
10267       --  designator. For other cases, this is simply Endl.
10268
10269       procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
10270       --  N is an identifier node that appears as a parent unit reference in
10271       --  the case where Ent is a child unit. This procedure generates an
10272       --  appropriate cross-reference entry. E is the corresponding entity.
10273
10274       -------------------------
10275       -- Generate_Parent_Ref --
10276       -------------------------
10277
10278       procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
10279       begin
10280          --  If names do not match, something weird, skip reference
10281
10282          if Chars (E) = Chars (N) then
10283
10284             --  Generate the reference. We do NOT consider this as a reference
10285             --  for unreferenced symbol purposes.
10286
10287             Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
10288
10289             if Style_Check then
10290                Style.Check_Identifier (N, E);
10291             end if;
10292          end if;
10293       end Generate_Parent_Ref;
10294
10295    --  Start of processing for Process_End_Label
10296
10297    begin
10298       --  If no node, ignore. This happens in some error situations, and
10299       --  also for some internally generated structures where no end label
10300       --  references are required in any case.
10301
10302       if No (N) then
10303          return;
10304       end if;
10305
10306       --  Nothing to do if no End_Label, happens for internally generated
10307       --  constructs where we don't want an end label reference anyway. Also
10308       --  nothing to do if Endl is a string literal, which means there was
10309       --  some prior error (bad operator symbol)
10310
10311       Endl := End_Label (N);
10312
10313       if No (Endl) or else Nkind (Endl) = N_String_Literal then
10314          return;
10315       end if;
10316
10317       --  Reference node is not in extended main source unit
10318
10319       if not In_Extended_Main_Source_Unit (N) then
10320
10321          --  Generally we do not collect references except for the extended
10322          --  main source unit. The one exception is the 'e' entry for a
10323          --  package spec, where it is useful for a client to have the
10324          --  ending information to define scopes.
10325
10326          if Typ /= 'e' then
10327             return;
10328
10329          else
10330             Label_Ref := False;
10331
10332             --  For this case, we can ignore any parent references, but we
10333             --  need the package name itself for the 'e' entry.
10334
10335             if Nkind (Endl) = N_Designator then
10336                Endl := Identifier (Endl);
10337             end if;
10338          end if;
10339
10340       --  Reference is in extended main source unit
10341
10342       else
10343          Label_Ref := True;
10344
10345          --  For designator, generate references for the parent entries
10346
10347          if Nkind (Endl) = N_Designator then
10348
10349             --  Generate references for the prefix if the END line comes from
10350             --  source (otherwise we do not need these references) We climb the
10351             --  scope stack to find the expected entities.
10352
10353             if Comes_From_Source (Endl) then
10354                Nam  := Name (Endl);
10355                Scop := Current_Scope;
10356                while Nkind (Nam) = N_Selected_Component loop
10357                   Scop := Scope (Scop);
10358                   exit when No (Scop);
10359                   Generate_Parent_Ref (Selector_Name (Nam), Scop);
10360                   Nam := Prefix (Nam);
10361                end loop;
10362
10363                if Present (Scop) then
10364                   Generate_Parent_Ref (Nam, Scope (Scop));
10365                end if;
10366             end if;
10367
10368             Endl := Identifier (Endl);
10369          end if;
10370       end if;
10371
10372       --  If the end label is not for the given entity, then either we have
10373       --  some previous error, or this is a generic instantiation for which
10374       --  we do not need to make a cross-reference in this case anyway. In
10375       --  either case we simply ignore the call.
10376
10377       if Chars (Ent) /= Chars (Endl) then
10378          return;
10379       end if;
10380
10381       --  If label was really there, then generate a normal reference and then
10382       --  adjust the location in the end label to point past the name (which
10383       --  should almost always be the semicolon).
10384
10385       Loc := Sloc (Endl);
10386
10387       if Comes_From_Source (Endl) then
10388
10389          --  If a label reference is required, then do the style check and
10390          --  generate an l-type cross-reference entry for the label
10391
10392          if Label_Ref then
10393             if Style_Check then
10394                Style.Check_Identifier (Endl, Ent);
10395             end if;
10396
10397             Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
10398          end if;
10399
10400          --  Set the location to point past the label (normally this will
10401          --  mean the semicolon immediately following the label). This is
10402          --  done for the sake of the 'e' or 't' entry generated below.
10403
10404          Get_Decoded_Name_String (Chars (Endl));
10405          Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
10406       end if;
10407
10408       --  Now generate the e/t reference
10409
10410       Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
10411
10412       --  Restore Sloc, in case modified above, since we have an identifier
10413       --  and the normal Sloc should be left set in the tree.
10414
10415       Set_Sloc (Endl, Loc);
10416    end Process_End_Label;
10417
10418    ------------------------------------
10419    -- References_Generic_Formal_Type --
10420    ------------------------------------
10421
10422    function References_Generic_Formal_Type (N : Node_Id) return Boolean is
10423
10424       function Process (N : Node_Id) return Traverse_Result;
10425       --  Process one node in search for generic formal type
10426
10427       -------------
10428       -- Process --
10429       -------------
10430
10431       function Process (N : Node_Id) return Traverse_Result is
10432       begin
10433          if Nkind (N) in N_Has_Entity then
10434             declare
10435                E : constant Entity_Id := Entity (N);
10436             begin
10437                if Present (E) then
10438                   if Is_Generic_Type (E) then
10439                      return Abandon;
10440                   elsif Present (Etype (E))
10441                     and then Is_Generic_Type (Etype (E))
10442                   then
10443                      return Abandon;
10444                   end if;
10445                end if;
10446             end;
10447          end if;
10448
10449          return Atree.OK;
10450       end Process;
10451
10452       function Traverse is new Traverse_Func (Process);
10453       --  Traverse tree to look for generic type
10454
10455    begin
10456       if Inside_A_Generic then
10457          return Traverse (N) = Abandon;
10458       else
10459          return False;
10460       end if;
10461    end References_Generic_Formal_Type;
10462
10463    --------------------
10464    -- Remove_Homonym --
10465    --------------------
10466
10467    procedure Remove_Homonym (E : Entity_Id) is
10468       Prev  : Entity_Id := Empty;
10469       H     : Entity_Id;
10470
10471    begin
10472       if E = Current_Entity (E) then
10473          if Present (Homonym (E)) then
10474             Set_Current_Entity (Homonym (E));
10475          else
10476             Set_Name_Entity_Id (Chars (E), Empty);
10477          end if;
10478       else
10479          H := Current_Entity (E);
10480          while Present (H) and then H /= E loop
10481             Prev := H;
10482             H    := Homonym (H);
10483          end loop;
10484
10485          Set_Homonym (Prev, Homonym (E));
10486       end if;
10487    end Remove_Homonym;
10488
10489    ---------------------
10490    -- Rep_To_Pos_Flag --
10491    ---------------------
10492
10493    function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
10494    begin
10495       return New_Occurrence_Of
10496                (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
10497    end Rep_To_Pos_Flag;
10498
10499    --------------------
10500    -- Require_Entity --
10501    --------------------
10502
10503    procedure Require_Entity (N : Node_Id) is
10504    begin
10505       if Is_Entity_Name (N) and then No (Entity (N)) then
10506          if Total_Errors_Detected /= 0 then
10507             Set_Entity (N, Any_Id);
10508          else
10509             raise Program_Error;
10510          end if;
10511       end if;
10512    end Require_Entity;
10513
10514    ------------------------------
10515    -- Requires_Transient_Scope --
10516    ------------------------------
10517
10518    --  A transient scope is required when variable-sized temporaries are
10519    --  allocated in the primary or secondary stack, or when finalization
10520    --  actions must be generated before the next instruction.
10521
10522    function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
10523       Typ : constant Entity_Id := Underlying_Type (Id);
10524
10525    --  Start of processing for Requires_Transient_Scope
10526
10527    begin
10528       --  This is a private type which is not completed yet. This can only
10529       --  happen in a default expression (of a formal parameter or of a
10530       --  record component). Do not expand transient scope in this case
10531
10532       if No (Typ) then
10533          return False;
10534
10535       --  Do not expand transient scope for non-existent procedure return
10536
10537       elsif Typ = Standard_Void_Type then
10538          return False;
10539
10540       --  Elementary types do not require a transient scope
10541
10542       elsif Is_Elementary_Type (Typ) then
10543          return False;
10544
10545       --  Generally, indefinite subtypes require a transient scope, since the
10546       --  back end cannot generate temporaries, since this is not a valid type
10547       --  for declaring an object. It might be possible to relax this in the
10548       --  future, e.g. by declaring the maximum possible space for the type.
10549
10550       elsif Is_Indefinite_Subtype (Typ) then
10551          return True;
10552
10553       --  Functions returning tagged types may dispatch on result so their
10554       --  returned value is allocated on the secondary stack. Controlled
10555       --  type temporaries need finalization.
10556
10557       elsif Is_Tagged_Type (Typ)
10558         or else Has_Controlled_Component (Typ)
10559       then
10560          return not Is_Value_Type (Typ);
10561
10562       --  Record type
10563
10564       elsif Is_Record_Type (Typ) then
10565          declare
10566             Comp : Entity_Id;
10567          begin
10568             Comp := First_Entity (Typ);
10569             while Present (Comp) loop
10570                if Ekind (Comp) = E_Component
10571                   and then Requires_Transient_Scope (Etype (Comp))
10572                then
10573                   return True;
10574                else
10575                   Next_Entity (Comp);
10576                end if;
10577             end loop;
10578          end;
10579
10580          return False;
10581
10582       --  String literal types never require transient scope
10583
10584       elsif Ekind (Typ) = E_String_Literal_Subtype then
10585          return False;
10586
10587       --  Array type. Note that we already know that this is a constrained
10588       --  array, since unconstrained arrays will fail the indefinite test.
10589
10590       elsif Is_Array_Type (Typ) then
10591
10592          --  If component type requires a transient scope, the array does too
10593
10594          if Requires_Transient_Scope (Component_Type (Typ)) then
10595             return True;
10596
10597          --  Otherwise, we only need a transient scope if the size depends on
10598          --  the value of one or more discriminants.
10599
10600          else
10601             return Size_Depends_On_Discriminant (Typ);
10602          end if;
10603
10604       --  All other cases do not require a transient scope
10605
10606       else
10607          return False;
10608       end if;
10609    end Requires_Transient_Scope;
10610
10611    --------------------------
10612    -- Reset_Analyzed_Flags --
10613    --------------------------
10614
10615    procedure Reset_Analyzed_Flags (N : Node_Id) is
10616
10617       function Clear_Analyzed (N : Node_Id) return Traverse_Result;
10618       --  Function used to reset Analyzed flags in tree. Note that we do
10619       --  not reset Analyzed flags in entities, since there is no need to
10620       --  reanalyze entities, and indeed, it is wrong to do so, since it
10621       --  can result in generating auxiliary stuff more than once.
10622
10623       --------------------
10624       -- Clear_Analyzed --
10625       --------------------
10626
10627       function Clear_Analyzed (N : Node_Id) return Traverse_Result is
10628       begin
10629          if not Has_Extension (N) then
10630             Set_Analyzed (N, False);
10631          end if;
10632
10633          return OK;
10634       end Clear_Analyzed;
10635
10636       procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
10637
10638    --  Start of processing for Reset_Analyzed_Flags
10639
10640    begin
10641       Reset_Analyzed (N);
10642    end Reset_Analyzed_Flags;
10643
10644    ---------------------------
10645    -- Safe_To_Capture_Value --
10646    ---------------------------
10647
10648    function Safe_To_Capture_Value
10649      (N    : Node_Id;
10650       Ent  : Entity_Id;
10651       Cond : Boolean := False) return Boolean
10652    is
10653    begin
10654       --  The only entities for which we track constant values are variables
10655       --  which are not renamings, constants, out parameters, and in out
10656       --  parameters, so check if we have this case.
10657
10658       --  Note: it may seem odd to track constant values for constants, but in
10659       --  fact this routine is used for other purposes than simply capturing
10660       --  the value. In particular, the setting of Known[_Non]_Null.
10661
10662       if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
10663             or else
10664           Ekind (Ent) = E_Constant
10665             or else
10666           Ekind (Ent) = E_Out_Parameter
10667             or else
10668           Ekind (Ent) = E_In_Out_Parameter
10669       then
10670          null;
10671
10672       --  For conditionals, we also allow loop parameters and all formals,
10673       --  including in parameters.
10674
10675       elsif Cond
10676         and then
10677           (Ekind (Ent) = E_Loop_Parameter
10678              or else
10679            Ekind (Ent) = E_In_Parameter)
10680       then
10681          null;
10682
10683       --  For all other cases, not just unsafe, but impossible to capture
10684       --  Current_Value, since the above are the only entities which have
10685       --  Current_Value fields.
10686
10687       else
10688          return False;
10689       end if;
10690
10691       --  Skip if volatile or aliased, since funny things might be going on in
10692       --  these cases which we cannot necessarily track. Also skip any variable
10693       --  for which an address clause is given, or whose address is taken. Also
10694       --  never capture value of library level variables (an attempt to do so
10695       --  can occur in the case of package elaboration code).
10696
10697       if Treat_As_Volatile (Ent)
10698         or else Is_Aliased (Ent)
10699         or else Present (Address_Clause (Ent))
10700         or else Address_Taken (Ent)
10701         or else (Is_Library_Level_Entity (Ent)
10702                    and then Ekind (Ent) = E_Variable)
10703       then
10704          return False;
10705       end if;
10706
10707       --  OK, all above conditions are met. We also require that the scope of
10708       --  the reference be the same as the scope of the entity, not counting
10709       --  packages and blocks and loops.
10710
10711       declare
10712          E_Scope : constant Entity_Id := Scope (Ent);
10713          R_Scope : Entity_Id;
10714
10715       begin
10716          R_Scope := Current_Scope;
10717          while R_Scope /= Standard_Standard loop
10718             exit when R_Scope = E_Scope;
10719
10720             if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
10721                return False;
10722             else
10723                R_Scope := Scope (R_Scope);
10724             end if;
10725          end loop;
10726       end;
10727
10728       --  We also require that the reference does not appear in a context
10729       --  where it is not sure to be executed (i.e. a conditional context
10730       --  or an exception handler). We skip this if Cond is True, since the
10731       --  capturing of values from conditional tests handles this ok.
10732
10733       if Cond then
10734          return True;
10735       end if;
10736
10737       declare
10738          Desc : Node_Id;
10739          P    : Node_Id;
10740
10741       begin
10742          Desc := N;
10743
10744          P := Parent (N);
10745          while Present (P) loop
10746             if         Nkind (P) = N_If_Statement
10747               or else  Nkind (P) = N_Case_Statement
10748               or else (Nkind (P) in N_Short_Circuit
10749                          and then Desc = Right_Opnd (P))
10750               or else (Nkind (P) = N_Conditional_Expression
10751                          and then Desc /= First (Expressions (P)))
10752               or else  Nkind (P) = N_Exception_Handler
10753               or else  Nkind (P) = N_Selective_Accept
10754               or else  Nkind (P) = N_Conditional_Entry_Call
10755               or else  Nkind (P) = N_Timed_Entry_Call
10756               or else  Nkind (P) = N_Asynchronous_Select
10757             then
10758                return False;
10759             else
10760                Desc := P;
10761                P    := Parent (P);
10762             end if;
10763          end loop;
10764       end;
10765
10766       --  OK, looks safe to set value
10767
10768       return True;
10769    end Safe_To_Capture_Value;
10770
10771    ---------------
10772    -- Same_Name --
10773    ---------------
10774
10775    function Same_Name (N1, N2 : Node_Id) return Boolean is
10776       K1 : constant Node_Kind := Nkind (N1);
10777       K2 : constant Node_Kind := Nkind (N2);
10778
10779    begin
10780       if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
10781         and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
10782       then
10783          return Chars (N1) = Chars (N2);
10784
10785       elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
10786         and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
10787       then
10788          return Same_Name (Selector_Name (N1), Selector_Name (N2))
10789            and then Same_Name (Prefix (N1), Prefix (N2));
10790
10791       else
10792          return False;
10793       end if;
10794    end Same_Name;
10795
10796    -----------------
10797    -- Same_Object --
10798    -----------------
10799
10800    function Same_Object (Node1, Node2 : Node_Id) return Boolean is
10801       N1 : constant Node_Id := Original_Node (Node1);
10802       N2 : constant Node_Id := Original_Node (Node2);
10803       --  We do the tests on original nodes, since we are most interested
10804       --  in the original source, not any expansion that got in the way.
10805
10806       K1 : constant Node_Kind := Nkind (N1);
10807       K2 : constant Node_Kind := Nkind (N2);
10808
10809    begin
10810       --  First case, both are entities with same entity
10811
10812       if K1 in N_Has_Entity and then K2 in N_Has_Entity then
10813          declare
10814             EN1 : constant Entity_Id := Entity (N1);
10815             EN2 : constant Entity_Id := Entity (N2);
10816          begin
10817             if Present (EN1) and then Present (EN2)
10818               and then (Ekind_In (EN1, E_Variable, E_Constant)
10819                          or else Is_Formal (EN1))
10820               and then EN1 = EN2
10821             then
10822                return True;
10823             end if;
10824          end;
10825       end if;
10826
10827       --  Second case, selected component with same selector, same record
10828
10829       if K1 = N_Selected_Component
10830         and then K2 = N_Selected_Component
10831         and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
10832       then
10833          return Same_Object (Prefix (N1), Prefix (N2));
10834
10835       --  Third case, indexed component with same subscripts, same array
10836
10837       elsif K1 = N_Indexed_Component
10838         and then K2 = N_Indexed_Component
10839         and then Same_Object (Prefix (N1), Prefix (N2))
10840       then
10841          declare
10842             E1, E2 : Node_Id;
10843          begin
10844             E1 := First (Expressions (N1));
10845             E2 := First (Expressions (N2));
10846             while Present (E1) loop
10847                if not Same_Value (E1, E2) then
10848                   return False;
10849                else
10850                   Next (E1);
10851                   Next (E2);
10852                end if;
10853             end loop;
10854
10855             return True;
10856          end;
10857
10858       --  Fourth case, slice of same array with same bounds
10859
10860       elsif K1 = N_Slice
10861         and then K2 = N_Slice
10862         and then Nkind (Discrete_Range (N1)) = N_Range
10863         and then Nkind (Discrete_Range (N2)) = N_Range
10864         and then Same_Value (Low_Bound (Discrete_Range (N1)),
10865                              Low_Bound (Discrete_Range (N2)))
10866         and then Same_Value (High_Bound (Discrete_Range (N1)),
10867                              High_Bound (Discrete_Range (N2)))
10868       then
10869          return Same_Name (Prefix (N1), Prefix (N2));
10870
10871       --  All other cases, not clearly the same object
10872
10873       else
10874          return False;
10875       end if;
10876    end Same_Object;
10877
10878    ---------------
10879    -- Same_Type --
10880    ---------------
10881
10882    function Same_Type (T1, T2 : Entity_Id) return Boolean is
10883    begin
10884       if T1 = T2 then
10885          return True;
10886
10887       elsif not Is_Constrained (T1)
10888         and then not Is_Constrained (T2)
10889         and then Base_Type (T1) = Base_Type (T2)
10890       then
10891          return True;
10892
10893       --  For now don't bother with case of identical constraints, to be
10894       --  fiddled with later on perhaps (this is only used for optimization
10895       --  purposes, so it is not critical to do a best possible job)
10896
10897       else
10898          return False;
10899       end if;
10900    end Same_Type;
10901
10902    ----------------
10903    -- Same_Value --
10904    ----------------
10905
10906    function Same_Value (Node1, Node2 : Node_Id) return Boolean is
10907    begin
10908       if Compile_Time_Known_Value (Node1)
10909         and then Compile_Time_Known_Value (Node2)
10910         and then Expr_Value (Node1) = Expr_Value (Node2)
10911       then
10912          return True;
10913       elsif Same_Object (Node1, Node2) then
10914          return True;
10915       else
10916          return False;
10917       end if;
10918    end Same_Value;
10919
10920    -----------------
10921    -- Save_Actual --
10922    -----------------
10923
10924    procedure Save_Actual (N : Node_Id;  Writable : Boolean := False) is
10925    begin
10926       if Ada_Version < Ada_2012 then
10927          return;
10928
10929       elsif Is_Entity_Name (N)
10930         or else
10931           Nkind_In (N, N_Indexed_Component, N_Selected_Component, N_Slice)
10932         or else
10933           (Nkind (N) = N_Attribute_Reference
10934             and then Attribute_Name (N) = Name_Access)
10935
10936       then
10937          --  We are only interested in IN OUT parameters of inner calls
10938
10939          if not Writable
10940            or else Nkind (Parent (N)) = N_Function_Call
10941            or else Nkind (Parent (N)) in N_Op
10942          then
10943             Actuals_In_Call.Increment_Last;
10944             Actuals_In_Call.Table (Actuals_In_Call.Last) := (N, Writable);
10945          end if;
10946       end if;
10947    end Save_Actual;
10948
10949    ------------------------
10950    -- Scope_Is_Transient --
10951    ------------------------
10952
10953    function Scope_Is_Transient return Boolean is
10954    begin
10955       return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
10956    end Scope_Is_Transient;
10957
10958    ------------------
10959    -- Scope_Within --
10960    ------------------
10961
10962    function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
10963       Scop : Entity_Id;
10964
10965    begin
10966       Scop := Scope1;
10967       while Scop /= Standard_Standard loop
10968          Scop := Scope (Scop);
10969
10970          if Scop = Scope2 then
10971             return True;
10972          end if;
10973       end loop;
10974
10975       return False;
10976    end Scope_Within;
10977
10978    --------------------------
10979    -- Scope_Within_Or_Same --
10980    --------------------------
10981
10982    function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
10983       Scop : Entity_Id;
10984
10985    begin
10986       Scop := Scope1;
10987       while Scop /= Standard_Standard loop
10988          if Scop = Scope2 then
10989             return True;
10990          else
10991             Scop := Scope (Scop);
10992          end if;
10993       end loop;
10994
10995       return False;
10996    end Scope_Within_Or_Same;
10997
10998    --------------------
10999    -- Set_Convention --
11000    --------------------
11001
11002    procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
11003    begin
11004       Basic_Set_Convention (E, Val);
11005
11006       if Is_Type (E)
11007         and then Is_Access_Subprogram_Type (Base_Type (E))
11008         and then Has_Foreign_Convention (E)
11009       then
11010          Set_Can_Use_Internal_Rep (E, False);
11011       end if;
11012    end Set_Convention;
11013
11014    ------------------------
11015    -- Set_Current_Entity --
11016    ------------------------
11017
11018    --  The given entity is to be set as the currently visible definition
11019    --  of its associated name (i.e. the Node_Id associated with its name).
11020    --  All we have to do is to get the name from the identifier, and
11021    --  then set the associated Node_Id to point to the given entity.
11022
11023    procedure Set_Current_Entity (E : Entity_Id) is
11024    begin
11025       Set_Name_Entity_Id (Chars (E), E);
11026    end Set_Current_Entity;
11027
11028    ---------------------------
11029    -- Set_Debug_Info_Needed --
11030    ---------------------------
11031
11032    procedure Set_Debug_Info_Needed (T : Entity_Id) is
11033
11034       procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
11035       pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
11036       --  Used to set debug info in a related node if not set already
11037
11038       --------------------------------------
11039       -- Set_Debug_Info_Needed_If_Not_Set --
11040       --------------------------------------
11041
11042       procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
11043       begin
11044          if Present (E)
11045            and then not Needs_Debug_Info (E)
11046          then
11047             Set_Debug_Info_Needed (E);
11048
11049             --  For a private type, indicate that the full view also needs
11050             --  debug information.
11051
11052             if Is_Type (E)
11053               and then Is_Private_Type (E)
11054               and then Present (Full_View (E))
11055             then
11056                Set_Debug_Info_Needed (Full_View (E));
11057             end if;
11058          end if;
11059       end Set_Debug_Info_Needed_If_Not_Set;
11060
11061    --  Start of processing for Set_Debug_Info_Needed
11062
11063    begin
11064       --  Nothing to do if argument is Empty or has Debug_Info_Off set, which
11065       --  indicates that Debug_Info_Needed is never required for the entity.
11066
11067       if No (T)
11068         or else Debug_Info_Off (T)
11069       then
11070          return;
11071       end if;
11072
11073       --  Set flag in entity itself. Note that we will go through the following
11074       --  circuitry even if the flag is already set on T. That's intentional,
11075       --  it makes sure that the flag will be set in subsidiary entities.
11076
11077       Set_Needs_Debug_Info (T);
11078
11079       --  Set flag on subsidiary entities if not set already
11080
11081       if Is_Object (T) then
11082          Set_Debug_Info_Needed_If_Not_Set (Etype (T));
11083
11084       elsif Is_Type (T) then
11085          Set_Debug_Info_Needed_If_Not_Set (Etype (T));
11086
11087          if Is_Record_Type (T) then
11088             declare
11089                Ent : Entity_Id := First_Entity (T);
11090             begin
11091                while Present (Ent) loop
11092                   Set_Debug_Info_Needed_If_Not_Set (Ent);
11093                   Next_Entity (Ent);
11094                end loop;
11095             end;
11096
11097             --  For a class wide subtype, we also need debug information
11098             --  for the equivalent type.
11099
11100             if Ekind (T) = E_Class_Wide_Subtype then
11101                Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
11102             end if;
11103
11104          elsif Is_Array_Type (T) then
11105             Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
11106
11107             declare
11108                Indx : Node_Id := First_Index (T);
11109             begin
11110                while Present (Indx) loop
11111                   Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
11112                   Indx := Next_Index (Indx);
11113                end loop;
11114             end;
11115
11116             if Is_Packed (T) then
11117                Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Type (T));
11118             end if;
11119
11120          elsif Is_Access_Type (T) then
11121             Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
11122
11123          elsif Is_Private_Type (T) then
11124             Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
11125
11126          elsif Is_Protected_Type (T) then
11127             Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
11128          end if;
11129       end if;
11130    end Set_Debug_Info_Needed;
11131
11132    ---------------------------------
11133    -- Set_Entity_With_Style_Check --
11134    ---------------------------------
11135
11136    procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
11137       Val_Actual : Entity_Id;
11138       Nod        : Node_Id;
11139
11140    begin
11141       Set_Entity (N, Val);
11142
11143       if Style_Check
11144         and then not Suppress_Style_Checks (Val)
11145         and then not In_Instance
11146       then
11147          if Nkind (N) = N_Identifier then
11148             Nod := N;
11149          elsif Nkind (N) = N_Expanded_Name then
11150             Nod := Selector_Name (N);
11151          else
11152             return;
11153          end if;
11154
11155          --  A special situation arises for derived operations, where we want
11156          --  to do the check against the parent (since the Sloc of the derived
11157          --  operation points to the derived type declaration itself).
11158
11159          Val_Actual := Val;
11160          while not Comes_From_Source (Val_Actual)
11161            and then Nkind (Val_Actual) in N_Entity
11162            and then (Ekind (Val_Actual) = E_Enumeration_Literal
11163                       or else Is_Subprogram (Val_Actual)
11164                       or else Is_Generic_Subprogram (Val_Actual))
11165            and then Present (Alias (Val_Actual))
11166          loop
11167             Val_Actual := Alias (Val_Actual);
11168          end loop;
11169
11170          --  Renaming declarations for generic actuals do not come from source,
11171          --  and have a different name from that of the entity they rename, so
11172          --  there is no style check to perform here.
11173
11174          if Chars (Nod) = Chars (Val_Actual) then
11175             Style.Check_Identifier (Nod, Val_Actual);
11176          end if;
11177       end if;
11178
11179       Set_Entity (N, Val);
11180    end Set_Entity_With_Style_Check;
11181
11182    ------------------------
11183    -- Set_Name_Entity_Id --
11184    ------------------------
11185
11186    procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
11187    begin
11188       Set_Name_Table_Info (Id, Int (Val));
11189    end Set_Name_Entity_Id;
11190
11191    ---------------------
11192    -- Set_Next_Actual --
11193    ---------------------
11194
11195    procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
11196    begin
11197       if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
11198          Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
11199       end if;
11200    end Set_Next_Actual;
11201
11202    ----------------------------------
11203    -- Set_Optimize_Alignment_Flags --
11204    ----------------------------------
11205
11206    procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
11207    begin
11208       if Optimize_Alignment = 'S' then
11209          Set_Optimize_Alignment_Space (E);
11210       elsif Optimize_Alignment = 'T' then
11211          Set_Optimize_Alignment_Time (E);
11212       end if;
11213    end Set_Optimize_Alignment_Flags;
11214
11215    -----------------------
11216    -- Set_Public_Status --
11217    -----------------------
11218
11219    procedure Set_Public_Status (Id : Entity_Id) is
11220       S : constant Entity_Id := Current_Scope;
11221
11222       function Within_HSS_Or_If (E : Entity_Id) return Boolean;
11223       --  Determines if E is defined within handled statement sequence or
11224       --  an if statement, returns True if so, False otherwise.
11225
11226       ----------------------
11227       -- Within_HSS_Or_If --
11228       ----------------------
11229
11230       function Within_HSS_Or_If (E : Entity_Id) return Boolean is
11231          N : Node_Id;
11232       begin
11233          N := Declaration_Node (E);
11234          loop
11235             N := Parent (N);
11236
11237             if No (N) then
11238                return False;
11239
11240             elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
11241                                N_If_Statement)
11242             then
11243                return True;
11244             end if;
11245          end loop;
11246       end Within_HSS_Or_If;
11247
11248    --  Start of processing for Set_Public_Status
11249
11250    begin
11251       --  Everything in the scope of Standard is public
11252
11253       if S = Standard_Standard then
11254          Set_Is_Public (Id);
11255
11256       --  Entity is definitely not public if enclosing scope is not public
11257
11258       elsif not Is_Public (S) then
11259          return;
11260
11261       --  An object or function declaration that occurs in a handled sequence
11262       --  of statements or within an if statement is the declaration for a
11263       --  temporary object or local subprogram generated by the expander. It
11264       --  never needs to be made public and furthermore, making it public can
11265       --  cause back end problems.
11266
11267       elsif Nkind_In (Parent (Id), N_Object_Declaration,
11268                                    N_Function_Specification)
11269         and then Within_HSS_Or_If (Id)
11270       then
11271          return;
11272
11273       --  Entities in public packages or records are public
11274
11275       elsif Ekind (S) = E_Package or Is_Record_Type (S) then
11276          Set_Is_Public (Id);
11277
11278       --  The bounds of an entry family declaration can generate object
11279       --  declarations that are visible to the back-end, e.g. in the
11280       --  the declaration of a composite type that contains tasks.
11281
11282       elsif Is_Concurrent_Type (S)
11283         and then not Has_Completion (S)
11284         and then Nkind (Parent (Id)) = N_Object_Declaration
11285       then
11286          Set_Is_Public (Id);
11287       end if;
11288    end Set_Public_Status;
11289
11290    -----------------------------
11291    -- Set_Referenced_Modified --
11292    -----------------------------
11293
11294    procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
11295       Pref : Node_Id;
11296
11297    begin
11298       --  Deal with indexed or selected component where prefix is modified
11299
11300       if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
11301          Pref := Prefix (N);
11302
11303          --  If prefix is access type, then it is the designated object that is
11304          --  being modified, which means we have no entity to set the flag on.
11305
11306          if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
11307             return;
11308
11309             --  Otherwise chase the prefix
11310
11311          else
11312             Set_Referenced_Modified (Pref, Out_Param);
11313          end if;
11314
11315       --  Otherwise see if we have an entity name (only other case to process)
11316
11317       elsif Is_Entity_Name (N) and then Present (Entity (N)) then
11318          Set_Referenced_As_LHS           (Entity (N), not Out_Param);
11319          Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
11320       end if;
11321    end Set_Referenced_Modified;
11322
11323    ----------------------------
11324    -- Set_Scope_Is_Transient --
11325    ----------------------------
11326
11327    procedure Set_Scope_Is_Transient (V : Boolean := True) is
11328    begin
11329       Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
11330    end Set_Scope_Is_Transient;
11331
11332    -------------------
11333    -- Set_Size_Info --
11334    -------------------
11335
11336    procedure Set_Size_Info (T1, T2 : Entity_Id) is
11337    begin
11338       --  We copy Esize, but not RM_Size, since in general RM_Size is
11339       --  subtype specific and does not get inherited by all subtypes.
11340
11341       Set_Esize                     (T1, Esize                     (T2));
11342       Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
11343
11344       if Is_Discrete_Or_Fixed_Point_Type (T1)
11345            and then
11346          Is_Discrete_Or_Fixed_Point_Type (T2)
11347       then
11348          Set_Is_Unsigned_Type       (T1, Is_Unsigned_Type          (T2));
11349       end if;
11350
11351       Set_Alignment                 (T1, Alignment                 (T2));
11352    end Set_Size_Info;
11353
11354    --------------------
11355    -- Static_Boolean --
11356    --------------------
11357
11358    function Static_Boolean (N : Node_Id) return Uint is
11359    begin
11360       Analyze_And_Resolve (N, Standard_Boolean);
11361
11362       if N = Error
11363         or else Error_Posted (N)
11364         or else Etype (N) = Any_Type
11365       then
11366          return No_Uint;
11367       end if;
11368
11369       if Is_Static_Expression (N) then
11370          if not Raises_Constraint_Error (N) then
11371             return Expr_Value (N);
11372          else
11373             return No_Uint;
11374          end if;
11375
11376       elsif Etype (N) = Any_Type then
11377          return No_Uint;
11378
11379       else
11380          Flag_Non_Static_Expr
11381            ("static boolean expression required here", N);
11382          return No_Uint;
11383       end if;
11384    end Static_Boolean;
11385
11386    --------------------
11387    -- Static_Integer --
11388    --------------------
11389
11390    function Static_Integer (N : Node_Id) return Uint is
11391    begin
11392       Analyze_And_Resolve (N, Any_Integer);
11393
11394       if N = Error
11395         or else Error_Posted (N)
11396         or else Etype (N) = Any_Type
11397       then
11398          return No_Uint;
11399       end if;
11400
11401       if Is_Static_Expression (N) then
11402          if not Raises_Constraint_Error (N) then
11403             return Expr_Value (N);
11404          else
11405             return No_Uint;
11406          end if;
11407
11408       elsif Etype (N) = Any_Type then
11409          return No_Uint;
11410
11411       else
11412          Flag_Non_Static_Expr
11413            ("static integer expression required here", N);
11414          return No_Uint;
11415       end if;
11416    end Static_Integer;
11417
11418    --------------------------
11419    -- Statically_Different --
11420    --------------------------
11421
11422    function Statically_Different (E1, E2 : Node_Id) return Boolean is
11423       R1 : constant Node_Id := Get_Referenced_Object (E1);
11424       R2 : constant Node_Id := Get_Referenced_Object (E2);
11425    begin
11426       return     Is_Entity_Name (R1)
11427         and then Is_Entity_Name (R2)
11428         and then Entity (R1) /= Entity (R2)
11429         and then not Is_Formal (Entity (R1))
11430         and then not Is_Formal (Entity (R2));
11431    end Statically_Different;
11432
11433    -----------------------------
11434    -- Subprogram_Access_Level --
11435    -----------------------------
11436
11437    function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
11438    begin
11439       if Present (Alias (Subp)) then
11440          return Subprogram_Access_Level (Alias (Subp));
11441       else
11442          return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
11443       end if;
11444    end Subprogram_Access_Level;
11445
11446    -----------------
11447    -- Trace_Scope --
11448    -----------------
11449
11450    procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
11451    begin
11452       if Debug_Flag_W then
11453          for J in 0 .. Scope_Stack.Last loop
11454             Write_Str ("  ");
11455          end loop;
11456
11457          Write_Str (Msg);
11458          Write_Name (Chars (E));
11459          Write_Str (" from ");
11460          Write_Location (Sloc (N));
11461          Write_Eol;
11462       end if;
11463    end Trace_Scope;
11464
11465    -----------------------
11466    -- Transfer_Entities --
11467    -----------------------
11468
11469    procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
11470       Ent : Entity_Id := First_Entity (From);
11471
11472    begin
11473       if No (Ent) then
11474          return;
11475       end if;
11476
11477       if (Last_Entity (To)) = Empty then
11478          Set_First_Entity (To, Ent);
11479       else
11480          Set_Next_Entity (Last_Entity (To), Ent);
11481       end if;
11482
11483       Set_Last_Entity (To, Last_Entity (From));
11484
11485       while Present (Ent) loop
11486          Set_Scope (Ent, To);
11487
11488          if not Is_Public (Ent) then
11489             Set_Public_Status (Ent);
11490
11491             if Is_Public (Ent)
11492               and then Ekind (Ent) = E_Record_Subtype
11493
11494             then
11495                --  The components of the propagated Itype must be public
11496                --  as well.
11497
11498                declare
11499                   Comp : Entity_Id;
11500                begin
11501                   Comp := First_Entity (Ent);
11502                   while Present (Comp) loop
11503                      Set_Is_Public (Comp);
11504                      Next_Entity (Comp);
11505                   end loop;
11506                end;
11507             end if;
11508          end if;
11509
11510          Next_Entity (Ent);
11511       end loop;
11512
11513       Set_First_Entity (From, Empty);
11514       Set_Last_Entity (From, Empty);
11515    end Transfer_Entities;
11516
11517    -----------------------
11518    -- Type_Access_Level --
11519    -----------------------
11520
11521    function Type_Access_Level (Typ : Entity_Id) return Uint is
11522       Btyp : Entity_Id;
11523
11524    begin
11525       Btyp := Base_Type (Typ);
11526
11527       --  Ada 2005 (AI-230): For most cases of anonymous access types, we
11528       --  simply use the level where the type is declared. This is true for
11529       --  stand-alone object declarations, and for anonymous access types
11530       --  associated with components the level is the same as that of the
11531       --  enclosing composite type. However, special treatment is needed for
11532       --  the cases of access parameters, return objects of an anonymous access
11533       --  type, and, in Ada 95, access discriminants of limited types.
11534
11535       if Ekind (Btyp) in Access_Kind then
11536          if Ekind (Btyp) = E_Anonymous_Access_Type then
11537
11538             --  If the type is a nonlocal anonymous access type (such as for
11539             --  an access parameter) we treat it as being declared at the
11540             --  library level to ensure that names such as X.all'access don't
11541             --  fail static accessibility checks.
11542
11543             if not Is_Local_Anonymous_Access (Typ) then
11544                return Scope_Depth (Standard_Standard);
11545
11546             --  If this is a return object, the accessibility level is that of
11547             --  the result subtype of the enclosing function. The test here is
11548             --  little complicated, because we have to account for extended
11549             --  return statements that have been rewritten as blocks, in which
11550             --  case we have to find and the Is_Return_Object attribute of the
11551             --  itype's associated object. It would be nice to find a way to
11552             --  simplify this test, but it doesn't seem worthwhile to add a new
11553             --  flag just for purposes of this test. ???
11554
11555             elsif Ekind (Scope (Btyp)) = E_Return_Statement
11556               or else
11557                 (Is_Itype (Btyp)
11558                   and then Nkind (Associated_Node_For_Itype (Btyp)) =
11559                              N_Object_Declaration
11560                   and then Is_Return_Object
11561                              (Defining_Identifier
11562                                 (Associated_Node_For_Itype (Btyp))))
11563             then
11564                declare
11565                   Scop : Entity_Id;
11566
11567                begin
11568                   Scop := Scope (Scope (Btyp));
11569                   while Present (Scop) loop
11570                      exit when Ekind (Scop) = E_Function;
11571                      Scop := Scope (Scop);
11572                   end loop;
11573
11574                   --  Treat the return object's type as having the level of the
11575                   --  function's result subtype (as per RM05-6.5(5.3/2)).
11576
11577                   return Type_Access_Level (Etype (Scop));
11578                end;
11579             end if;
11580          end if;
11581
11582          Btyp := Root_Type (Btyp);
11583
11584          --  The accessibility level of anonymous access types associated with
11585          --  discriminants is that of the current instance of the type, and
11586          --  that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
11587
11588          --  AI-402: access discriminants have accessibility based on the
11589          --  object rather than the type in Ada 2005, so the above paragraph
11590          --  doesn't apply.
11591
11592          --  ??? Needs completion with rules from AI-416
11593
11594          if Ada_Version <= Ada_95
11595            and then Ekind (Typ) = E_Anonymous_Access_Type
11596            and then Present (Associated_Node_For_Itype (Typ))
11597            and then Nkind (Associated_Node_For_Itype (Typ)) =
11598                                                  N_Discriminant_Specification
11599          then
11600             return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
11601          end if;
11602       end if;
11603
11604       return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
11605    end Type_Access_Level;
11606
11607    --------------------------
11608    -- Unit_Declaration_Node --
11609    --------------------------
11610
11611    function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
11612       N : Node_Id := Parent (Unit_Id);
11613
11614    begin
11615       --  Predefined operators do not have a full function declaration
11616
11617       if Ekind (Unit_Id) = E_Operator then
11618          return N;
11619       end if;
11620
11621       --  Isn't there some better way to express the following ???
11622
11623       while Nkind (N) /= N_Abstract_Subprogram_Declaration
11624         and then Nkind (N) /= N_Formal_Package_Declaration
11625         and then Nkind (N) /= N_Function_Instantiation
11626         and then Nkind (N) /= N_Generic_Package_Declaration
11627         and then Nkind (N) /= N_Generic_Subprogram_Declaration
11628         and then Nkind (N) /= N_Package_Declaration
11629         and then Nkind (N) /= N_Package_Body
11630         and then Nkind (N) /= N_Package_Instantiation
11631         and then Nkind (N) /= N_Package_Renaming_Declaration
11632         and then Nkind (N) /= N_Procedure_Instantiation
11633         and then Nkind (N) /= N_Protected_Body
11634         and then Nkind (N) /= N_Subprogram_Declaration
11635         and then Nkind (N) /= N_Subprogram_Body
11636         and then Nkind (N) /= N_Subprogram_Body_Stub
11637         and then Nkind (N) /= N_Subprogram_Renaming_Declaration
11638         and then Nkind (N) /= N_Task_Body
11639         and then Nkind (N) /= N_Task_Type_Declaration
11640         and then Nkind (N) not in N_Formal_Subprogram_Declaration
11641         and then Nkind (N) not in N_Generic_Renaming_Declaration
11642       loop
11643          N := Parent (N);
11644          pragma Assert (Present (N));
11645       end loop;
11646
11647       return N;
11648    end Unit_Declaration_Node;
11649
11650    ---------------------
11651    -- Unit_Is_Visible --
11652    ---------------------
11653
11654    function Unit_Is_Visible (U : Entity_Id) return Boolean is
11655       Curr        : constant Node_Id   := Cunit (Current_Sem_Unit);
11656       Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
11657
11658       function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
11659       --  For a child unit, check whether unit appears in a with_clause
11660       --  of a parent.
11661
11662       function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
11663       --  Scan the context clause of one compilation unit looking for a
11664       --  with_clause for the unit in question.
11665
11666       ----------------------------
11667       -- Unit_In_Parent_Context --
11668       ----------------------------
11669
11670       function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
11671       begin
11672          if Unit_In_Context (Par_Unit) then
11673             return True;
11674
11675          elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
11676             return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
11677
11678          else
11679             return False;
11680          end if;
11681       end Unit_In_Parent_Context;
11682
11683       ---------------------
11684       -- Unit_In_Context --
11685       ---------------------
11686
11687       function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
11688          Clause : Node_Id;
11689
11690       begin
11691          Clause := First (Context_Items (Comp_Unit));
11692          while Present (Clause) loop
11693             if Nkind (Clause) = N_With_Clause then
11694                if Library_Unit (Clause) = U then
11695                   return True;
11696
11697                --  The with_clause may denote a renaming of the unit we are
11698                --  looking for, eg. Text_IO which renames Ada.Text_IO.
11699
11700                elsif
11701                  Renamed_Entity (Entity (Name (Clause))) =
11702                                                 Defining_Entity (Unit (U))
11703                then
11704                   return True;
11705                end if;
11706             end if;
11707
11708             Next (Clause);
11709          end loop;
11710
11711          return False;
11712       end Unit_In_Context;
11713
11714    --  Start of processing for Unit_Is_Visible
11715
11716    begin
11717       --  The currrent unit is directly visible.
11718
11719       if Curr = U then
11720          return True;
11721
11722       elsif Unit_In_Context (Curr) then
11723          return True;
11724
11725       --  If the current unit is a body, check the context of the spec.
11726
11727       elsif Nkind (Unit (Curr)) = N_Package_Body
11728         or else
11729           (Nkind (Unit (Curr)) = N_Subprogram_Body
11730             and then not Acts_As_Spec (Unit (Curr)))
11731       then
11732          if Unit_In_Context (Library_Unit (Curr)) then
11733             return True;
11734          end if;
11735       end if;
11736
11737       --  If the spec is a child unit, examine the parents.
11738
11739       if Is_Child_Unit (Curr_Entity) then
11740          if Nkind (Unit (Curr)) in N_Unit_Body then
11741             return
11742               Unit_In_Parent_Context
11743                 (Parent_Spec (Unit (Library_Unit (Curr))));
11744          else
11745             return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
11746          end if;
11747
11748       else
11749          return False;
11750       end if;
11751    end Unit_Is_Visible;
11752
11753    ------------------------------
11754    -- Universal_Interpretation --
11755    ------------------------------
11756
11757    function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
11758       Index : Interp_Index;
11759       It    : Interp;
11760
11761    begin
11762       --  The argument may be a formal parameter of an operator or subprogram
11763       --  with multiple interpretations, or else an expression for an actual.
11764
11765       if Nkind (Opnd) = N_Defining_Identifier
11766         or else not Is_Overloaded (Opnd)
11767       then
11768          if Etype (Opnd) = Universal_Integer
11769            or else Etype (Opnd) = Universal_Real
11770          then
11771             return Etype (Opnd);
11772          else
11773             return Empty;
11774          end if;
11775
11776       else
11777          Get_First_Interp (Opnd, Index, It);
11778          while Present (It.Typ) loop
11779             if It.Typ = Universal_Integer
11780               or else It.Typ = Universal_Real
11781             then
11782                return It.Typ;
11783             end if;
11784
11785             Get_Next_Interp (Index, It);
11786          end loop;
11787
11788          return Empty;
11789       end if;
11790    end Universal_Interpretation;
11791
11792    ---------------
11793    -- Unqualify --
11794    ---------------
11795
11796    function Unqualify (Expr : Node_Id) return Node_Id is
11797    begin
11798       --  Recurse to handle unlikely case of multiple levels of qualification
11799
11800       if Nkind (Expr) = N_Qualified_Expression then
11801          return Unqualify (Expression (Expr));
11802
11803       --  Normal case, not a qualified expression
11804
11805       else
11806          return Expr;
11807       end if;
11808    end Unqualify;
11809
11810    -----------------------
11811    -- Visible_Ancestors --
11812    -----------------------
11813
11814    function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
11815       List_1 : Elist_Id;
11816       List_2 : Elist_Id;
11817       Elmt   : Elmt_Id;
11818
11819    begin
11820       pragma Assert (Is_Record_Type (Typ)
11821         and then Is_Tagged_Type (Typ));
11822
11823       --  Collect all the parents and progenitors of Typ. If the full-view of
11824       --  private parents and progenitors is available then it is used to
11825       --  generate the list of visible ancestors; otherwise their partial
11826       --  view is added to the resulting list.
11827
11828       Collect_Parents
11829         (T               => Typ,
11830          List            => List_1,
11831          Use_Full_View   => True);
11832
11833       Collect_Interfaces
11834         (T               => Typ,
11835          Ifaces_List     => List_2,
11836          Exclude_Parents => True,
11837          Use_Full_View   => True);
11838
11839       --  Join the two lists. Avoid duplications because an interface may
11840       --  simultaneously be parent and progenitor of a type.
11841
11842       Elmt := First_Elmt (List_2);
11843       while Present (Elmt) loop
11844          Append_Unique_Elmt (Node (Elmt), List_1);
11845          Next_Elmt (Elmt);
11846       end loop;
11847
11848       return List_1;
11849    end Visible_Ancestors;
11850
11851    ----------------------
11852    -- Within_Init_Proc --
11853    ----------------------
11854
11855    function Within_Init_Proc return Boolean is
11856       S : Entity_Id;
11857
11858    begin
11859       S := Current_Scope;
11860       while not Is_Overloadable (S) loop
11861          if S = Standard_Standard then
11862             return False;
11863          else
11864             S := Scope (S);
11865          end if;
11866       end loop;
11867
11868       return Is_Init_Proc (S);
11869    end Within_Init_Proc;
11870
11871    ----------------
11872    -- Wrong_Type --
11873    ----------------
11874
11875    procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
11876       Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
11877       Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
11878
11879       function Has_One_Matching_Field return Boolean;
11880       --  Determines if Expec_Type is a record type with a single component or
11881       --  discriminant whose type matches the found type or is one dimensional
11882       --  array whose component type matches the found type.
11883
11884       ----------------------------
11885       -- Has_One_Matching_Field --
11886       ----------------------------
11887
11888       function Has_One_Matching_Field return Boolean is
11889          E : Entity_Id;
11890
11891       begin
11892          if Is_Array_Type (Expec_Type)
11893            and then Number_Dimensions (Expec_Type) = 1
11894            and then
11895              Covers (Etype (Component_Type (Expec_Type)), Found_Type)
11896          then
11897             return True;
11898
11899          elsif not Is_Record_Type (Expec_Type) then
11900             return False;
11901
11902          else
11903             E := First_Entity (Expec_Type);
11904             loop
11905                if No (E) then
11906                   return False;
11907
11908                elsif (Ekind (E) /= E_Discriminant
11909                        and then Ekind (E) /= E_Component)
11910                  or else (Chars (E) = Name_uTag
11911                            or else Chars (E) = Name_uParent)
11912                then
11913                   Next_Entity (E);
11914
11915                else
11916                   exit;
11917                end if;
11918             end loop;
11919
11920             if not Covers (Etype (E), Found_Type) then
11921                return False;
11922
11923             elsif Present (Next_Entity (E)) then
11924                return False;
11925
11926             else
11927                return True;
11928             end if;
11929          end if;
11930       end Has_One_Matching_Field;
11931
11932    --  Start of processing for Wrong_Type
11933
11934    begin
11935       --  Don't output message if either type is Any_Type, or if a message
11936       --  has already been posted for this node. We need to do the latter
11937       --  check explicitly (it is ordinarily done in Errout), because we
11938       --  are using ! to force the output of the error messages.
11939
11940       if Expec_Type = Any_Type
11941         or else Found_Type = Any_Type
11942         or else Error_Posted (Expr)
11943       then
11944          return;
11945
11946       --  In  an instance, there is an ongoing problem with completion of
11947       --  type derived from private types. Their structure is what Gigi
11948       --  expects, but the  Etype is the parent type rather than the
11949       --  derived private type itself. Do not flag error in this case. The
11950       --  private completion is an entity without a parent, like an Itype.
11951       --  Similarly, full and partial views may be incorrect in the instance.
11952       --  There is no simple way to insure that it is consistent ???
11953
11954       elsif In_Instance then
11955          if Etype (Etype (Expr)) = Etype (Expected_Type)
11956            and then
11957              (Has_Private_Declaration (Expected_Type)
11958                or else Has_Private_Declaration (Etype (Expr)))
11959            and then No (Parent (Expected_Type))
11960          then
11961             return;
11962          end if;
11963       end if;
11964
11965       --  An interesting special check. If the expression is parenthesized
11966       --  and its type corresponds to the type of the sole component of the
11967       --  expected record type, or to the component type of the expected one
11968       --  dimensional array type, then assume we have a bad aggregate attempt.
11969
11970       if Nkind (Expr) in N_Subexpr
11971         and then Paren_Count (Expr) /= 0
11972         and then Has_One_Matching_Field
11973       then
11974          Error_Msg_N ("positional aggregate cannot have one component", Expr);
11975
11976       --  Another special check, if we are looking for a pool-specific access
11977       --  type and we found an E_Access_Attribute_Type, then we have the case
11978       --  of an Access attribute being used in a context which needs a pool-
11979       --  specific type, which is never allowed. The one extra check we make
11980       --  is that the expected designated type covers the Found_Type.
11981
11982       elsif Is_Access_Type (Expec_Type)
11983         and then Ekind (Found_Type) = E_Access_Attribute_Type
11984         and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
11985         and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
11986         and then Covers
11987           (Designated_Type (Expec_Type), Designated_Type (Found_Type))
11988       then
11989          Error_Msg_N -- CODEFIX
11990            ("result must be general access type!", Expr);
11991          Error_Msg_NE -- CODEFIX
11992            ("add ALL to }!", Expr, Expec_Type);
11993
11994       --  Another special check, if the expected type is an integer type,
11995       --  but the expression is of type System.Address, and the parent is
11996       --  an addition or subtraction operation whose left operand is the
11997       --  expression in question and whose right operand is of an integral
11998       --  type, then this is an attempt at address arithmetic, so give
11999       --  appropriate message.
12000
12001       elsif Is_Integer_Type (Expec_Type)
12002         and then Is_RTE (Found_Type, RE_Address)
12003         and then (Nkind (Parent (Expr)) = N_Op_Add
12004                     or else
12005                   Nkind (Parent (Expr)) = N_Op_Subtract)
12006         and then Expr = Left_Opnd (Parent (Expr))
12007         and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
12008       then
12009          Error_Msg_N
12010            ("address arithmetic not predefined in package System",
12011             Parent (Expr));
12012          Error_Msg_N
12013            ("\possible missing with/use of System.Storage_Elements",
12014             Parent (Expr));
12015          return;
12016
12017       --  If the expected type is an anonymous access type, as for access
12018       --  parameters and discriminants, the error is on the designated types.
12019
12020       elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
12021          if Comes_From_Source (Expec_Type) then
12022             Error_Msg_NE ("expected}!", Expr, Expec_Type);
12023          else
12024             Error_Msg_NE
12025               ("expected an access type with designated}",
12026                  Expr, Designated_Type (Expec_Type));
12027          end if;
12028
12029          if Is_Access_Type (Found_Type)
12030            and then not Comes_From_Source (Found_Type)
12031          then
12032             Error_Msg_NE
12033               ("\\found an access type with designated}!",
12034                 Expr, Designated_Type (Found_Type));
12035          else
12036             if From_With_Type (Found_Type) then
12037                Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
12038                Error_Msg_Qual_Level := 99;
12039                Error_Msg_NE -- CODEFIX
12040                  ("\\missing `WITH &;", Expr, Scope (Found_Type));
12041                Error_Msg_Qual_Level := 0;
12042             else
12043                Error_Msg_NE ("found}!", Expr, Found_Type);
12044             end if;
12045          end if;
12046
12047       --  Normal case of one type found, some other type expected
12048
12049       else
12050          --  If the names of the two types are the same, see if some number
12051          --  of levels of qualification will help. Don't try more than three
12052          --  levels, and if we get to standard, it's no use (and probably
12053          --  represents an error in the compiler) Also do not bother with
12054          --  internal scope names.
12055
12056          declare
12057             Expec_Scope : Entity_Id;
12058             Found_Scope : Entity_Id;
12059
12060          begin
12061             Expec_Scope := Expec_Type;
12062             Found_Scope := Found_Type;
12063
12064             for Levels in Int range 0 .. 3 loop
12065                if Chars (Expec_Scope) /= Chars (Found_Scope) then
12066                   Error_Msg_Qual_Level := Levels;
12067                   exit;
12068                end if;
12069
12070                Expec_Scope := Scope (Expec_Scope);
12071                Found_Scope := Scope (Found_Scope);
12072
12073                exit when Expec_Scope = Standard_Standard
12074                  or else Found_Scope = Standard_Standard
12075                  or else not Comes_From_Source (Expec_Scope)
12076                  or else not Comes_From_Source (Found_Scope);
12077             end loop;
12078          end;
12079
12080          if Is_Record_Type (Expec_Type)
12081            and then Present (Corresponding_Remote_Type (Expec_Type))
12082          then
12083             Error_Msg_NE ("expected}!", Expr,
12084                           Corresponding_Remote_Type (Expec_Type));
12085          else
12086             Error_Msg_NE ("expected}!", Expr, Expec_Type);
12087          end if;
12088
12089          if Is_Entity_Name (Expr)
12090            and then Is_Package_Or_Generic_Package (Entity (Expr))
12091          then
12092             Error_Msg_N ("\\found package name!", Expr);
12093
12094          elsif Is_Entity_Name (Expr)
12095            and then
12096              (Ekind (Entity (Expr)) = E_Procedure
12097                 or else
12098               Ekind (Entity (Expr)) = E_Generic_Procedure)
12099          then
12100             if Ekind (Expec_Type) = E_Access_Subprogram_Type then
12101                Error_Msg_N
12102                  ("found procedure name, possibly missing Access attribute!",
12103                    Expr);
12104             else
12105                Error_Msg_N
12106                  ("\\found procedure name instead of function!", Expr);
12107             end if;
12108
12109          elsif Nkind (Expr) = N_Function_Call
12110            and then Ekind (Expec_Type) = E_Access_Subprogram_Type
12111            and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
12112            and then No (Parameter_Associations (Expr))
12113          then
12114             Error_Msg_N
12115               ("found function name, possibly missing Access attribute!",
12116                Expr);
12117
12118          --  Catch common error: a prefix or infix operator which is not
12119          --  directly visible because the type isn't.
12120
12121          elsif Nkind (Expr) in N_Op
12122             and then Is_Overloaded (Expr)
12123             and then not Is_Immediately_Visible (Expec_Type)
12124             and then not Is_Potentially_Use_Visible (Expec_Type)
12125             and then not In_Use (Expec_Type)
12126             and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
12127          then
12128             Error_Msg_N
12129               ("operator of the type is not directly visible!", Expr);
12130
12131          elsif Ekind (Found_Type) = E_Void
12132            and then Present (Parent (Found_Type))
12133            and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
12134          then
12135             Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
12136
12137          else
12138             Error_Msg_NE ("\\found}!", Expr, Found_Type);
12139          end if;
12140
12141          --  A special check for cases like M1 and M2 = 0 where M1 and M2 are
12142          --  of the same modular type, and (M1 and M2) = 0 was intended.
12143
12144          if Expec_Type = Standard_Boolean
12145            and then Is_Modular_Integer_Type (Found_Type)
12146            and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
12147            and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
12148          then
12149             declare
12150                Op : constant Node_Id := Right_Opnd (Parent (Expr));
12151                L  : constant Node_Id := Left_Opnd (Op);
12152                R  : constant Node_Id := Right_Opnd (Op);
12153             begin
12154                --  The case for the message is when the left operand of the
12155                --  comparison is the same modular type, or when it is an
12156                --  integer literal (or other universal integer expression),
12157                --  which would have been typed as the modular type if the
12158                --  parens had been there.
12159
12160                if (Etype (L) = Found_Type
12161                      or else
12162                    Etype (L) = Universal_Integer)
12163                  and then Is_Integer_Type (Etype (R))
12164                then
12165                   Error_Msg_N
12166                     ("\\possible missing parens for modular operation", Expr);
12167                end if;
12168             end;
12169          end if;
12170
12171          --  Reset error message qualification indication
12172
12173          Error_Msg_Qual_Level := 0;
12174       end if;
12175    end Wrong_Type;
12176
12177 end Sem_Util;