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