[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       Arg : constant Node_Id :=
4340               Get_Pragma_Arg (First (Pragma_Argument_Associations (N)));
4341    begin
4342       return Strval (Expr_Value_S (Arg));
4343    end Get_Name_From_Test_Case_Pragma;
4344
4345    -------------------
4346    -- Get_Pragma_Id --
4347    -------------------
4348
4349    function Get_Pragma_Id (N : Node_Id) return Pragma_Id is
4350    begin
4351       return Get_Pragma_Id (Pragma_Name (N));
4352    end Get_Pragma_Id;
4353
4354    ---------------------------
4355    -- Get_Referenced_Object --
4356    ---------------------------
4357
4358    function Get_Referenced_Object (N : Node_Id) return Node_Id is
4359       R : Node_Id;
4360
4361    begin
4362       R := N;
4363       while Is_Entity_Name (R)
4364         and then Present (Renamed_Object (Entity (R)))
4365       loop
4366          R := Renamed_Object (Entity (R));
4367       end loop;
4368
4369       return R;
4370    end Get_Referenced_Object;
4371
4372    ------------------------
4373    -- Get_Renamed_Entity --
4374    ------------------------
4375
4376    function Get_Renamed_Entity (E : Entity_Id) return Entity_Id is
4377       R : Entity_Id;
4378
4379    begin
4380       R := E;
4381       while Present (Renamed_Entity (R)) loop
4382          R := Renamed_Entity (R);
4383       end loop;
4384
4385       return R;
4386    end Get_Renamed_Entity;
4387
4388    ----------------------------------------
4389    -- Get_Requires_From_Test_Case_Pragma --
4390    ----------------------------------------
4391
4392    function Get_Requires_From_Test_Case_Pragma (N : Node_Id) return Node_Id is
4393       Args : constant List_Id := Pragma_Argument_Associations (N);
4394       Res  : Node_Id;
4395
4396    begin
4397       Res := Pick (Args, 3);
4398       if Chars (Res) /= Name_Requires then
4399          Res := Empty;
4400       end if;
4401
4402       return Res;
4403    end Get_Requires_From_Test_Case_Pragma;
4404
4405    -------------------------
4406    -- Get_Subprogram_Body --
4407    -------------------------
4408
4409    function Get_Subprogram_Body (E : Entity_Id) return Node_Id is
4410       Decl : Node_Id;
4411
4412    begin
4413       Decl := Unit_Declaration_Node (E);
4414
4415       if Nkind (Decl) = N_Subprogram_Body then
4416          return Decl;
4417
4418       --  The below comment is bad, because it is possible for
4419       --  Nkind (Decl) to be an N_Subprogram_Body_Stub ???
4420
4421       else           --  Nkind (Decl) = N_Subprogram_Declaration
4422
4423          if Present (Corresponding_Body (Decl)) then
4424             return Unit_Declaration_Node (Corresponding_Body (Decl));
4425
4426          --  Imported subprogram case
4427
4428          else
4429             return Empty;
4430          end if;
4431       end if;
4432    end Get_Subprogram_Body;
4433
4434    ---------------------------
4435    -- Get_Subprogram_Entity --
4436    ---------------------------
4437
4438    function Get_Subprogram_Entity (Nod : Node_Id) return Entity_Id is
4439       Nam  : Node_Id;
4440       Proc : Entity_Id;
4441
4442    begin
4443       if Nkind (Nod) = N_Accept_Statement then
4444          Nam := Entry_Direct_Name (Nod);
4445
4446       --  For an entry call, the prefix of the call is a selected component.
4447       --  Need additional code for internal calls ???
4448
4449       elsif Nkind (Nod) = N_Entry_Call_Statement then
4450          if Nkind (Name (Nod)) = N_Selected_Component then
4451             Nam := Entity (Selector_Name (Name (Nod)));
4452          else
4453             Nam := Empty;
4454          end if;
4455
4456       else
4457          Nam := Name (Nod);
4458       end if;
4459
4460       if Nkind (Nam) = N_Explicit_Dereference then
4461          Proc := Etype (Prefix (Nam));
4462       elsif Is_Entity_Name (Nam) then
4463          Proc := Entity (Nam);
4464       else
4465          return Empty;
4466       end if;
4467
4468       if Is_Object (Proc) then
4469          Proc := Etype (Proc);
4470       end if;
4471
4472       if Ekind (Proc) = E_Access_Subprogram_Type then
4473          Proc := Directly_Designated_Type (Proc);
4474       end if;
4475
4476       if not Is_Subprogram (Proc)
4477         and then Ekind (Proc) /= E_Subprogram_Type
4478       then
4479          return Empty;
4480       else
4481          return Proc;
4482       end if;
4483    end Get_Subprogram_Entity;
4484
4485    -----------------------------
4486    -- Get_Task_Body_Procedure --
4487    -----------------------------
4488
4489    function Get_Task_Body_Procedure (E : Entity_Id) return Node_Id is
4490    begin
4491       --  Note: A task type may be the completion of a private type with
4492       --  discriminants. When performing elaboration checks on a task
4493       --  declaration, the current view of the type may be the private one,
4494       --  and the procedure that holds the body of the task is held in its
4495       --  underlying type.
4496
4497       --  This is an odd function, why not have Task_Body_Procedure do
4498       --  the following digging???
4499
4500       return Task_Body_Procedure (Underlying_Type (Root_Type (E)));
4501    end Get_Task_Body_Procedure;
4502
4503    -----------------------
4504    -- Has_Access_Values --
4505    -----------------------
4506
4507    function Has_Access_Values (T : Entity_Id) return Boolean is
4508       Typ : constant Entity_Id := Underlying_Type (T);
4509
4510    begin
4511       --  Case of a private type which is not completed yet. This can only
4512       --  happen in the case of a generic format type appearing directly, or
4513       --  as a component of the type to which this function is being applied
4514       --  at the top level. Return False in this case, since we certainly do
4515       --  not know that the type contains access types.
4516
4517       if No (Typ) then
4518          return False;
4519
4520       elsif Is_Access_Type (Typ) then
4521          return True;
4522
4523       elsif Is_Array_Type (Typ) then
4524          return Has_Access_Values (Component_Type (Typ));
4525
4526       elsif Is_Record_Type (Typ) then
4527          declare
4528             Comp : Entity_Id;
4529
4530          begin
4531             --  Loop to Check components
4532
4533             Comp := First_Component_Or_Discriminant (Typ);
4534             while Present (Comp) loop
4535
4536                --  Check for access component, tag field does not count, even
4537                --  though it is implemented internally using an access type.
4538
4539                if Has_Access_Values (Etype (Comp))
4540                  and then Chars (Comp) /= Name_uTag
4541                then
4542                   return True;
4543                end if;
4544
4545                Next_Component_Or_Discriminant (Comp);
4546             end loop;
4547          end;
4548
4549          return False;
4550
4551       else
4552          return False;
4553       end if;
4554    end Has_Access_Values;
4555
4556    ------------------------------
4557    -- Has_Compatible_Alignment --
4558    ------------------------------
4559
4560    function Has_Compatible_Alignment
4561      (Obj  : Entity_Id;
4562       Expr : Node_Id) return Alignment_Result
4563    is
4564       function Has_Compatible_Alignment_Internal
4565         (Obj     : Entity_Id;
4566          Expr    : Node_Id;
4567          Default : Alignment_Result) return Alignment_Result;
4568       --  This is the internal recursive function that actually does the work.
4569       --  There is one additional parameter, which says what the result should
4570       --  be if no alignment information is found, and there is no definite
4571       --  indication of compatible alignments. At the outer level, this is set
4572       --  to Unknown, but for internal recursive calls in the case where types
4573       --  are known to be correct, it is set to Known_Compatible.
4574
4575       ---------------------------------------
4576       -- Has_Compatible_Alignment_Internal --
4577       ---------------------------------------
4578
4579       function Has_Compatible_Alignment_Internal
4580         (Obj     : Entity_Id;
4581          Expr    : Node_Id;
4582          Default : Alignment_Result) return Alignment_Result
4583       is
4584          Result : Alignment_Result := Known_Compatible;
4585          --  Holds the current status of the result. Note that once a value of
4586          --  Known_Incompatible is set, it is sticky and does not get changed
4587          --  to Unknown (the value in Result only gets worse as we go along,
4588          --  never better).
4589
4590          Offs : Uint := No_Uint;
4591          --  Set to a factor of the offset from the base object when Expr is a
4592          --  selected or indexed component, based on Component_Bit_Offset and
4593          --  Component_Size respectively. A negative value is used to represent
4594          --  a value which is not known at compile time.
4595
4596          procedure Check_Prefix;
4597          --  Checks the prefix recursively in the case where the expression
4598          --  is an indexed or selected component.
4599
4600          procedure Set_Result (R : Alignment_Result);
4601          --  If R represents a worse outcome (unknown instead of known
4602          --  compatible, or known incompatible), then set Result to R.
4603
4604          ------------------
4605          -- Check_Prefix --
4606          ------------------
4607
4608          procedure Check_Prefix is
4609          begin
4610             --  The subtlety here is that in doing a recursive call to check
4611             --  the prefix, we have to decide what to do in the case where we
4612             --  don't find any specific indication of an alignment problem.
4613
4614             --  At the outer level, we normally set Unknown as the result in
4615             --  this case, since we can only set Known_Compatible if we really
4616             --  know that the alignment value is OK, but for the recursive
4617             --  call, in the case where the types match, and we have not
4618             --  specified a peculiar alignment for the object, we are only
4619             --  concerned about suspicious rep clauses, the default case does
4620             --  not affect us, since the compiler will, in the absence of such
4621             --  rep clauses, ensure that the alignment is correct.
4622
4623             if Default = Known_Compatible
4624               or else
4625                 (Etype (Obj) = Etype (Expr)
4626                   and then (Unknown_Alignment (Obj)
4627                              or else
4628                                Alignment (Obj) = Alignment (Etype (Obj))))
4629             then
4630                Set_Result
4631                  (Has_Compatible_Alignment_Internal
4632                     (Obj, Prefix (Expr), Known_Compatible));
4633
4634             --  In all other cases, we need a full check on the prefix
4635
4636             else
4637                Set_Result
4638                  (Has_Compatible_Alignment_Internal
4639                     (Obj, Prefix (Expr), Unknown));
4640             end if;
4641          end Check_Prefix;
4642
4643          ----------------
4644          -- Set_Result --
4645          ----------------
4646
4647          procedure Set_Result (R : Alignment_Result) is
4648          begin
4649             if R > Result then
4650                Result := R;
4651             end if;
4652          end Set_Result;
4653
4654       --  Start of processing for Has_Compatible_Alignment_Internal
4655
4656       begin
4657          --  If Expr is a selected component, we must make sure there is no
4658          --  potentially troublesome component clause, and that the record is
4659          --  not packed.
4660
4661          if Nkind (Expr) = N_Selected_Component then
4662
4663             --  Packed record always generate unknown alignment
4664
4665             if Is_Packed (Etype (Prefix (Expr))) then
4666                Set_Result (Unknown);
4667             end if;
4668
4669             --  Check prefix and component offset
4670
4671             Check_Prefix;
4672             Offs := Component_Bit_Offset (Entity (Selector_Name (Expr)));
4673
4674          --  If Expr is an indexed component, we must make sure there is no
4675          --  potentially troublesome Component_Size clause and that the array
4676          --  is not bit-packed.
4677
4678          elsif Nkind (Expr) = N_Indexed_Component then
4679             declare
4680                Typ : constant Entity_Id := Etype (Prefix (Expr));
4681                Ind : constant Node_Id   := First_Index (Typ);
4682
4683             begin
4684                --  Bit packed array always generates unknown alignment
4685
4686                if Is_Bit_Packed_Array (Typ) then
4687                   Set_Result (Unknown);
4688                end if;
4689
4690                --  Check prefix and component offset
4691
4692                Check_Prefix;
4693                Offs := Component_Size (Typ);
4694
4695                --  Small optimization: compute the full offset when possible
4696
4697                if Offs /= No_Uint
4698                  and then Offs > Uint_0
4699                  and then Present (Ind)
4700                  and then Nkind (Ind) = N_Range
4701                  and then Compile_Time_Known_Value (Low_Bound (Ind))
4702                  and then Compile_Time_Known_Value (First (Expressions (Expr)))
4703                then
4704                   Offs := Offs * (Expr_Value (First (Expressions (Expr)))
4705                                     - Expr_Value (Low_Bound ((Ind))));
4706                end if;
4707             end;
4708          end if;
4709
4710          --  If we have a null offset, the result is entirely determined by
4711          --  the base object and has already been computed recursively.
4712
4713          if Offs = Uint_0 then
4714             null;
4715
4716          --  Case where we know the alignment of the object
4717
4718          elsif Known_Alignment (Obj) then
4719             declare
4720                ObjA : constant Uint := Alignment (Obj);
4721                ExpA : Uint          := No_Uint;
4722                SizA : Uint          := No_Uint;
4723
4724             begin
4725                --  If alignment of Obj is 1, then we are always OK
4726
4727                if ObjA = 1 then
4728                   Set_Result (Known_Compatible);
4729
4730                --  Alignment of Obj is greater than 1, so we need to check
4731
4732                else
4733                   --  If we have an offset, see if it is compatible
4734
4735                   if Offs /= No_Uint and Offs > Uint_0 then
4736                      if Offs mod (System_Storage_Unit * ObjA) /= 0 then
4737                         Set_Result (Known_Incompatible);
4738                      end if;
4739
4740                      --  See if Expr is an object with known alignment
4741
4742                   elsif Is_Entity_Name (Expr)
4743                     and then Known_Alignment (Entity (Expr))
4744                   then
4745                      ExpA := Alignment (Entity (Expr));
4746
4747                      --  Otherwise, we can use the alignment of the type of
4748                      --  Expr given that we already checked for
4749                      --  discombobulating rep clauses for the cases of indexed
4750                      --  and selected components above.
4751
4752                   elsif Known_Alignment (Etype (Expr)) then
4753                      ExpA := Alignment (Etype (Expr));
4754
4755                      --  Otherwise the alignment is unknown
4756
4757                   else
4758                      Set_Result (Default);
4759                   end if;
4760
4761                   --  If we got an alignment, see if it is acceptable
4762
4763                   if ExpA /= No_Uint and then ExpA < ObjA then
4764                      Set_Result (Known_Incompatible);
4765                   end if;
4766
4767                   --  If Expr is not a piece of a larger object, see if size
4768                   --  is given. If so, check that it is not too small for the
4769                   --  required alignment.
4770
4771                   if Offs /= No_Uint then
4772                      null;
4773
4774                      --  See if Expr is an object with known size
4775
4776                   elsif Is_Entity_Name (Expr)
4777                     and then Known_Static_Esize (Entity (Expr))
4778                   then
4779                      SizA := Esize (Entity (Expr));
4780
4781                      --  Otherwise, we check the object size of the Expr type
4782
4783                   elsif Known_Static_Esize (Etype (Expr)) then
4784                      SizA := Esize (Etype (Expr));
4785                   end if;
4786
4787                   --  If we got a size, see if it is a multiple of the Obj
4788                   --  alignment, if not, then the alignment cannot be
4789                   --  acceptable, since the size is always a multiple of the
4790                   --  alignment.
4791
4792                   if SizA /= No_Uint then
4793                      if SizA mod (ObjA * Ttypes.System_Storage_Unit) /= 0 then
4794                         Set_Result (Known_Incompatible);
4795                      end if;
4796                   end if;
4797                end if;
4798             end;
4799
4800          --  If we do not know required alignment, any non-zero offset is a
4801          --  potential problem (but certainly may be OK, so result is unknown).
4802
4803          elsif Offs /= No_Uint then
4804             Set_Result (Unknown);
4805
4806          --  If we can't find the result by direct comparison of alignment
4807          --  values, then there is still one case that we can determine known
4808          --  result, and that is when we can determine that the types are the
4809          --  same, and no alignments are specified. Then we known that the
4810          --  alignments are compatible, even if we don't know the alignment
4811          --  value in the front end.
4812
4813          elsif Etype (Obj) = Etype (Expr) then
4814
4815             --  Types are the same, but we have to check for possible size
4816             --  and alignments on the Expr object that may make the alignment
4817             --  different, even though the types are the same.
4818
4819             if Is_Entity_Name (Expr) then
4820
4821                --  First check alignment of the Expr object. Any alignment less
4822                --  than Maximum_Alignment is worrisome since this is the case
4823                --  where we do not know the alignment of Obj.
4824
4825                if Known_Alignment (Entity (Expr))
4826                  and then
4827                    UI_To_Int (Alignment (Entity (Expr))) <
4828                                                     Ttypes.Maximum_Alignment
4829                then
4830                   Set_Result (Unknown);
4831
4832                   --  Now check size of Expr object. Any size that is not an
4833                   --  even multiple of Maximum_Alignment is also worrisome
4834                   --  since it may cause the alignment of the object to be less
4835                   --  than the alignment of the type.
4836
4837                elsif Known_Static_Esize (Entity (Expr))
4838                  and then
4839                    (UI_To_Int (Esize (Entity (Expr))) mod
4840                      (Ttypes.Maximum_Alignment * Ttypes.System_Storage_Unit))
4841                                                                         /= 0
4842                then
4843                   Set_Result (Unknown);
4844
4845                   --  Otherwise same type is decisive
4846
4847                else
4848                   Set_Result (Known_Compatible);
4849                end if;
4850             end if;
4851
4852          --  Another case to deal with is when there is an explicit size or
4853          --  alignment clause when the types are not the same. If so, then the
4854          --  result is Unknown. We don't need to do this test if the Default is
4855          --  Unknown, since that result will be set in any case.
4856
4857          elsif Default /= Unknown
4858            and then (Has_Size_Clause      (Etype (Expr))
4859                       or else
4860                      Has_Alignment_Clause (Etype (Expr)))
4861          then
4862             Set_Result (Unknown);
4863
4864          --  If no indication found, set default
4865
4866          else
4867             Set_Result (Default);
4868          end if;
4869
4870          --  Return worst result found
4871
4872          return Result;
4873       end Has_Compatible_Alignment_Internal;
4874
4875    --  Start of processing for Has_Compatible_Alignment
4876
4877    begin
4878       --  If Obj has no specified alignment, then set alignment from the type
4879       --  alignment. Perhaps we should always do this, but for sure we should
4880       --  do it when there is an address clause since we can do more if the
4881       --  alignment is known.
4882
4883       if Unknown_Alignment (Obj) then
4884          Set_Alignment (Obj, Alignment (Etype (Obj)));
4885       end if;
4886
4887       --  Now do the internal call that does all the work
4888
4889       return Has_Compatible_Alignment_Internal (Obj, Expr, Unknown);
4890    end Has_Compatible_Alignment;
4891
4892    ----------------------
4893    -- Has_Declarations --
4894    ----------------------
4895
4896    function Has_Declarations (N : Node_Id) return Boolean is
4897    begin
4898       return Nkind_In (Nkind (N), N_Accept_Statement,
4899                                   N_Block_Statement,
4900                                   N_Compilation_Unit_Aux,
4901                                   N_Entry_Body,
4902                                   N_Package_Body,
4903                                   N_Protected_Body,
4904                                   N_Subprogram_Body,
4905                                   N_Task_Body,
4906                                   N_Package_Specification);
4907    end Has_Declarations;
4908
4909    -------------------------------------------
4910    -- Has_Discriminant_Dependent_Constraint --
4911    -------------------------------------------
4912
4913    function Has_Discriminant_Dependent_Constraint
4914      (Comp : Entity_Id) return Boolean
4915    is
4916       Comp_Decl  : constant Node_Id := Parent (Comp);
4917       Subt_Indic : constant Node_Id :=
4918                      Subtype_Indication (Component_Definition (Comp_Decl));
4919       Constr     : Node_Id;
4920       Assn       : Node_Id;
4921
4922    begin
4923       if Nkind (Subt_Indic) = N_Subtype_Indication then
4924          Constr := Constraint (Subt_Indic);
4925
4926          if Nkind (Constr) = N_Index_Or_Discriminant_Constraint then
4927             Assn := First (Constraints (Constr));
4928             while Present (Assn) loop
4929                case Nkind (Assn) is
4930                   when N_Subtype_Indication |
4931                        N_Range              |
4932                        N_Identifier
4933                   =>
4934                      if Depends_On_Discriminant (Assn) then
4935                         return True;
4936                      end if;
4937
4938                   when N_Discriminant_Association =>
4939                      if Depends_On_Discriminant (Expression (Assn)) then
4940                         return True;
4941                      end if;
4942
4943                   when others =>
4944                      null;
4945
4946                end case;
4947
4948                Next (Assn);
4949             end loop;
4950          end if;
4951       end if;
4952
4953       return False;
4954    end Has_Discriminant_Dependent_Constraint;
4955
4956    --------------------
4957    -- Has_Infinities --
4958    --------------------
4959
4960    function Has_Infinities (E : Entity_Id) return Boolean is
4961    begin
4962       return
4963         Is_Floating_Point_Type (E)
4964           and then Nkind (Scalar_Range (E)) = N_Range
4965           and then Includes_Infinities (Scalar_Range (E));
4966    end Has_Infinities;
4967
4968    --------------------
4969    -- Has_Interfaces --
4970    --------------------
4971
4972    function Has_Interfaces
4973      (T             : Entity_Id;
4974       Use_Full_View : Boolean := True) return Boolean
4975    is
4976       Typ : Entity_Id := Base_Type (T);
4977
4978    begin
4979       --  Handle concurrent types
4980
4981       if Is_Concurrent_Type (Typ) then
4982          Typ := Corresponding_Record_Type (Typ);
4983       end if;
4984
4985       if not Present (Typ)
4986         or else not Is_Record_Type (Typ)
4987         or else not Is_Tagged_Type (Typ)
4988       then
4989          return False;
4990       end if;
4991
4992       --  Handle private types
4993
4994       if Use_Full_View
4995         and then Present (Full_View (Typ))
4996       then
4997          Typ := Full_View (Typ);
4998       end if;
4999
5000       --  Handle concurrent record types
5001
5002       if Is_Concurrent_Record_Type (Typ)
5003         and then Is_Non_Empty_List (Abstract_Interface_List (Typ))
5004       then
5005          return True;
5006       end if;
5007
5008       loop
5009          if Is_Interface (Typ)
5010            or else
5011              (Is_Record_Type (Typ)
5012                and then Present (Interfaces (Typ))
5013                and then not Is_Empty_Elmt_List (Interfaces (Typ)))
5014          then
5015             return True;
5016          end if;
5017
5018          exit when Etype (Typ) = Typ
5019
5020             --  Handle private types
5021
5022             or else (Present (Full_View (Etype (Typ)))
5023                        and then Full_View (Etype (Typ)) = Typ)
5024
5025             --  Protect the frontend against wrong source with cyclic
5026             --  derivations
5027
5028             or else Etype (Typ) = T;
5029
5030          --  Climb to the ancestor type handling private types
5031
5032          if Present (Full_View (Etype (Typ))) then
5033             Typ := Full_View (Etype (Typ));
5034          else
5035             Typ := Etype (Typ);
5036          end if;
5037       end loop;
5038
5039       return False;
5040    end Has_Interfaces;
5041
5042    ------------------------
5043    -- Has_Null_Exclusion --
5044    ------------------------
5045
5046    function Has_Null_Exclusion (N : Node_Id) return Boolean is
5047    begin
5048       case Nkind (N) is
5049          when N_Access_Definition               |
5050               N_Access_Function_Definition      |
5051               N_Access_Procedure_Definition     |
5052               N_Access_To_Object_Definition     |
5053               N_Allocator                       |
5054               N_Derived_Type_Definition         |
5055               N_Function_Specification          |
5056               N_Subtype_Declaration             =>
5057             return Null_Exclusion_Present (N);
5058
5059          when N_Component_Definition            |
5060               N_Formal_Object_Declaration       |
5061               N_Object_Renaming_Declaration     =>
5062             if Present (Subtype_Mark (N)) then
5063                return Null_Exclusion_Present (N);
5064             else pragma Assert (Present (Access_Definition (N)));
5065                return Null_Exclusion_Present (Access_Definition (N));
5066             end if;
5067
5068          when N_Discriminant_Specification =>
5069             if Nkind (Discriminant_Type (N)) = N_Access_Definition then
5070                return Null_Exclusion_Present (Discriminant_Type (N));
5071             else
5072                return Null_Exclusion_Present (N);
5073             end if;
5074
5075          when N_Object_Declaration =>
5076             if Nkind (Object_Definition (N)) = N_Access_Definition then
5077                return Null_Exclusion_Present (Object_Definition (N));
5078             else
5079                return Null_Exclusion_Present (N);
5080             end if;
5081
5082          when N_Parameter_Specification =>
5083             if Nkind (Parameter_Type (N)) = N_Access_Definition then
5084                return Null_Exclusion_Present (Parameter_Type (N));
5085             else
5086                return Null_Exclusion_Present (N);
5087             end if;
5088
5089          when others =>
5090             return False;
5091
5092       end case;
5093    end Has_Null_Exclusion;
5094
5095    ------------------------
5096    -- Has_Null_Extension --
5097    ------------------------
5098
5099    function Has_Null_Extension (T : Entity_Id) return Boolean is
5100       B     : constant Entity_Id := Base_Type (T);
5101       Comps : Node_Id;
5102       Ext   : Node_Id;
5103
5104    begin
5105       if Nkind (Parent (B)) = N_Full_Type_Declaration
5106         and then Present (Record_Extension_Part (Type_Definition (Parent (B))))
5107       then
5108          Ext := Record_Extension_Part (Type_Definition (Parent (B)));
5109
5110          if Present (Ext) then
5111             if Null_Present (Ext) then
5112                return True;
5113             else
5114                Comps := Component_List (Ext);
5115
5116                --  The null component list is rewritten during analysis to
5117                --  include the parent component. Any other component indicates
5118                --  that the extension was not originally null.
5119
5120                return Null_Present (Comps)
5121                  or else No (Next (First (Component_Items (Comps))));
5122             end if;
5123          else
5124             return False;
5125          end if;
5126
5127       else
5128          return False;
5129       end if;
5130    end Has_Null_Extension;
5131
5132    -------------------------------
5133    -- Has_Overriding_Initialize --
5134    -------------------------------
5135
5136    function Has_Overriding_Initialize (T : Entity_Id) return Boolean is
5137       BT   : constant Entity_Id := Base_Type (T);
5138       P    : Elmt_Id;
5139
5140    begin
5141       if Is_Controlled (BT) then
5142          if Is_RTU (Scope (BT), Ada_Finalization) then
5143             return False;
5144
5145          elsif Present (Primitive_Operations (BT)) then
5146             P := First_Elmt (Primitive_Operations (BT));
5147             while Present (P) loop
5148                declare
5149                   Init : constant Entity_Id := Node (P);
5150                   Formal : constant Entity_Id := First_Formal (Init);
5151                begin
5152                   if Ekind (Init) = E_Procedure
5153                        and then Chars (Init) = Name_Initialize
5154                        and then Comes_From_Source (Init)
5155                        and then Present (Formal)
5156                        and then Etype (Formal) = BT
5157                        and then No (Next_Formal (Formal))
5158                        and then (Ada_Version < Ada_2012
5159                                    or else not Null_Present (Parent (Init)))
5160                   then
5161                      return True;
5162                   end if;
5163                end;
5164
5165                Next_Elmt (P);
5166             end loop;
5167          end if;
5168
5169          --  Here if type itself does not have a non-null Initialize operation:
5170          --  check immediate ancestor.
5171
5172          if Is_Derived_Type (BT)
5173            and then Has_Overriding_Initialize (Etype (BT))
5174          then
5175             return True;
5176          end if;
5177       end if;
5178
5179       return False;
5180    end Has_Overriding_Initialize;
5181
5182    --------------------------------------
5183    -- Has_Preelaborable_Initialization --
5184    --------------------------------------
5185
5186    function Has_Preelaborable_Initialization (E : Entity_Id) return Boolean is
5187       Has_PE : Boolean;
5188
5189       procedure Check_Components (E : Entity_Id);
5190       --  Check component/discriminant chain, sets Has_PE False if a component
5191       --  or discriminant does not meet the preelaborable initialization rules.
5192
5193       ----------------------
5194       -- Check_Components --
5195       ----------------------
5196
5197       procedure Check_Components (E : Entity_Id) is
5198          Ent : Entity_Id;
5199          Exp : Node_Id;
5200
5201          function Is_Preelaborable_Expression (N : Node_Id) return Boolean;
5202          --  Returns True if and only if the expression denoted by N does not
5203          --  violate restrictions on preelaborable constructs (RM-10.2.1(5-9)).
5204
5205          ---------------------------------
5206          -- Is_Preelaborable_Expression --
5207          ---------------------------------
5208
5209          function Is_Preelaborable_Expression (N : Node_Id) return Boolean is
5210             Exp           : Node_Id;
5211             Assn          : Node_Id;
5212             Choice        : Node_Id;
5213             Comp_Type     : Entity_Id;
5214             Is_Array_Aggr : Boolean;
5215
5216          begin
5217             if Is_Static_Expression (N) then
5218                return True;
5219
5220             elsif Nkind (N) = N_Null then
5221                return True;
5222
5223             --  Attributes are allowed in general, even if their prefix is a
5224             --  formal type. (It seems that certain attributes known not to be
5225             --  static might not be allowed, but there are no rules to prevent
5226             --  them.)
5227
5228             elsif Nkind (N) = N_Attribute_Reference then
5229                return True;
5230
5231             --  The name of a discriminant evaluated within its parent type is
5232             --  defined to be preelaborable (10.2.1(8)). Note that we test for
5233             --  names that denote discriminals as well as discriminants to
5234             --  catch references occurring within init procs.
5235
5236             elsif Is_Entity_Name (N)
5237               and then
5238                 (Ekind (Entity (N)) = E_Discriminant
5239                   or else
5240                     ((Ekind (Entity (N)) = E_Constant
5241                        or else Ekind (Entity (N)) = E_In_Parameter)
5242                      and then Present (Discriminal_Link (Entity (N)))))
5243             then
5244                return True;
5245
5246             elsif Nkind (N) = N_Qualified_Expression then
5247                return Is_Preelaborable_Expression (Expression (N));
5248
5249             --  For aggregates we have to check that each of the associations
5250             --  is preelaborable.
5251
5252             elsif Nkind (N) = N_Aggregate
5253               or else Nkind (N) = N_Extension_Aggregate
5254             then
5255                Is_Array_Aggr := Is_Array_Type (Etype (N));
5256
5257                if Is_Array_Aggr then
5258                   Comp_Type := Component_Type (Etype (N));
5259                end if;
5260
5261                --  Check the ancestor part of extension aggregates, which must
5262                --  be either the name of a type that has preelaborable init or
5263                --  an expression that is preelaborable.
5264
5265                if Nkind (N) = N_Extension_Aggregate then
5266                   declare
5267                      Anc_Part : constant Node_Id := Ancestor_Part (N);
5268
5269                   begin
5270                      if Is_Entity_Name (Anc_Part)
5271                        and then Is_Type (Entity (Anc_Part))
5272                      then
5273                         if not Has_Preelaborable_Initialization
5274                                  (Entity (Anc_Part))
5275                         then
5276                            return False;
5277                         end if;
5278
5279                      elsif not Is_Preelaborable_Expression (Anc_Part) then
5280                         return False;
5281                      end if;
5282                   end;
5283                end if;
5284
5285                --  Check positional associations
5286
5287                Exp := First (Expressions (N));
5288                while Present (Exp) loop
5289                   if not Is_Preelaborable_Expression (Exp) then
5290                      return False;
5291                   end if;
5292
5293                   Next (Exp);
5294                end loop;
5295
5296                --  Check named associations
5297
5298                Assn := First (Component_Associations (N));
5299                while Present (Assn) loop
5300                   Choice := First (Choices (Assn));
5301                   while Present (Choice) loop
5302                      if Is_Array_Aggr then
5303                         if Nkind (Choice) = N_Others_Choice then
5304                            null;
5305
5306                         elsif Nkind (Choice) = N_Range then
5307                            if not Is_Static_Range (Choice) then
5308                               return False;
5309                            end if;
5310
5311                         elsif not Is_Static_Expression (Choice) then
5312                            return False;
5313                         end if;
5314
5315                      else
5316                         Comp_Type := Etype (Choice);
5317                      end if;
5318
5319                      Next (Choice);
5320                   end loop;
5321
5322                   --  If the association has a <> at this point, then we have
5323                   --  to check whether the component's type has preelaborable
5324                   --  initialization. Note that this only occurs when the
5325                   --  association's corresponding component does not have a
5326                   --  default expression, the latter case having already been
5327                   --  expanded as an expression for the association.
5328
5329                   if Box_Present (Assn) then
5330                      if not Has_Preelaborable_Initialization (Comp_Type) then
5331                         return False;
5332                      end if;
5333
5334                   --  In the expression case we check whether the expression
5335                   --  is preelaborable.
5336
5337                   elsif
5338                     not Is_Preelaborable_Expression (Expression (Assn))
5339                   then
5340                      return False;
5341                   end if;
5342
5343                   Next (Assn);
5344                end loop;
5345
5346                --  If we get here then aggregate as a whole is preelaborable
5347
5348                return True;
5349
5350             --  All other cases are not preelaborable
5351
5352             else
5353                return False;
5354             end if;
5355          end Is_Preelaborable_Expression;
5356
5357       --  Start of processing for Check_Components
5358
5359       begin
5360          --  Loop through entities of record or protected type
5361
5362          Ent := E;
5363          while Present (Ent) loop
5364
5365             --  We are interested only in components and discriminants
5366
5367             Exp := Empty;
5368
5369             case Ekind (Ent) is
5370                when E_Component =>
5371
5372                   --  Get default expression if any. If there is no declaration
5373                   --  node, it means we have an internal entity. The parent and
5374                   --  tag fields are examples of such entities. For such cases,
5375                   --  we just test the type of the entity.
5376
5377                   if Present (Declaration_Node (Ent)) then
5378                      Exp := Expression (Declaration_Node (Ent));
5379                   end if;
5380
5381                when E_Discriminant =>
5382
5383                   --  Note: for a renamed discriminant, the Declaration_Node
5384                   --  may point to the one from the ancestor, and have a
5385                   --  different expression, so use the proper attribute to
5386                   --  retrieve the expression from the derived constraint.
5387
5388                   Exp := Discriminant_Default_Value (Ent);
5389
5390                when others =>
5391                   goto Check_Next_Entity;
5392             end case;
5393
5394             --  A component has PI if it has no default expression and the
5395             --  component type has PI.
5396
5397             if No (Exp) then
5398                if not Has_Preelaborable_Initialization (Etype (Ent)) then
5399                   Has_PE := False;
5400                   exit;
5401                end if;
5402
5403             --  Require the default expression to be preelaborable
5404
5405             elsif not Is_Preelaborable_Expression (Exp) then
5406                Has_PE := False;
5407                exit;
5408             end if;
5409
5410          <<Check_Next_Entity>>
5411             Next_Entity (Ent);
5412          end loop;
5413       end Check_Components;
5414
5415    --  Start of processing for Has_Preelaborable_Initialization
5416
5417    begin
5418       --  Immediate return if already marked as known preelaborable init. This
5419       --  covers types for which this function has already been called once
5420       --  and returned True (in which case the result is cached), and also
5421       --  types to which a pragma Preelaborable_Initialization applies.
5422
5423       if Known_To_Have_Preelab_Init (E) then
5424          return True;
5425       end if;
5426
5427       --  If the type is a subtype representing a generic actual type, then
5428       --  test whether its base type has preelaborable initialization since
5429       --  the subtype representing the actual does not inherit this attribute
5430       --  from the actual or formal. (but maybe it should???)
5431
5432       if Is_Generic_Actual_Type (E) then
5433          return Has_Preelaborable_Initialization (Base_Type (E));
5434       end if;
5435
5436       --  All elementary types have preelaborable initialization
5437
5438       if Is_Elementary_Type (E) then
5439          Has_PE := True;
5440
5441       --  Array types have PI if the component type has PI
5442
5443       elsif Is_Array_Type (E) then
5444          Has_PE := Has_Preelaborable_Initialization (Component_Type (E));
5445
5446       --  A derived type has preelaborable initialization if its parent type
5447       --  has preelaborable initialization and (in the case of a derived record
5448       --  extension) if the non-inherited components all have preelaborable
5449       --  initialization. However, a user-defined controlled type with an
5450       --  overriding Initialize procedure does not have preelaborable
5451       --  initialization.
5452
5453       elsif Is_Derived_Type (E) then
5454
5455          --  If the derived type is a private extension then it doesn't have
5456          --  preelaborable initialization.
5457
5458          if Ekind (Base_Type (E)) = E_Record_Type_With_Private then
5459             return False;
5460          end if;
5461
5462          --  First check whether ancestor type has preelaborable initialization
5463
5464          Has_PE := Has_Preelaborable_Initialization (Etype (Base_Type (E)));
5465
5466          --  If OK, check extension components (if any)
5467
5468          if Has_PE and then Is_Record_Type (E) then
5469             Check_Components (First_Entity (E));
5470          end if;
5471
5472          --  Check specifically for 10.2.1(11.4/2) exception: a controlled type
5473          --  with a user defined Initialize procedure does not have PI.
5474
5475          if Has_PE
5476            and then Is_Controlled (E)
5477            and then Has_Overriding_Initialize (E)
5478          then
5479             Has_PE := False;
5480          end if;
5481
5482       --  Private types not derived from a type having preelaborable init and
5483       --  that are not marked with pragma Preelaborable_Initialization do not
5484       --  have preelaborable initialization.
5485
5486       elsif Is_Private_Type (E) then
5487          return False;
5488
5489       --  Record type has PI if it is non private and all components have PI
5490
5491       elsif Is_Record_Type (E) then
5492          Has_PE := True;
5493          Check_Components (First_Entity (E));
5494
5495       --  Protected types must not have entries, and components must meet
5496       --  same set of rules as for record components.
5497
5498       elsif Is_Protected_Type (E) then
5499          if Has_Entries (E) then
5500             Has_PE := False;
5501          else
5502             Has_PE := True;
5503             Check_Components (First_Entity (E));
5504             Check_Components (First_Private_Entity (E));
5505          end if;
5506
5507       --  Type System.Address always has preelaborable initialization
5508
5509       elsif Is_RTE (E, RE_Address) then
5510          Has_PE := True;
5511
5512       --  In all other cases, type does not have preelaborable initialization
5513
5514       else
5515          return False;
5516       end if;
5517
5518       --  If type has preelaborable initialization, cache result
5519
5520       if Has_PE then
5521          Set_Known_To_Have_Preelab_Init (E);
5522       end if;
5523
5524       return Has_PE;
5525    end Has_Preelaborable_Initialization;
5526
5527    ---------------------------
5528    -- Has_Private_Component --
5529    ---------------------------
5530
5531    function Has_Private_Component (Type_Id : Entity_Id) return Boolean is
5532       Btype     : Entity_Id := Base_Type (Type_Id);
5533       Component : Entity_Id;
5534
5535    begin
5536       if Error_Posted (Type_Id)
5537         or else Error_Posted (Btype)
5538       then
5539          return False;
5540       end if;
5541
5542       if Is_Class_Wide_Type (Btype) then
5543          Btype := Root_Type (Btype);
5544       end if;
5545
5546       if Is_Private_Type (Btype) then
5547          declare
5548             UT : constant Entity_Id := Underlying_Type (Btype);
5549          begin
5550             if No (UT) then
5551                if No (Full_View (Btype)) then
5552                   return not Is_Generic_Type (Btype)
5553                     and then not Is_Generic_Type (Root_Type (Btype));
5554                else
5555                   return not Is_Generic_Type (Root_Type (Full_View (Btype)));
5556                end if;
5557             else
5558                return not Is_Frozen (UT) and then Has_Private_Component (UT);
5559             end if;
5560          end;
5561
5562       elsif Is_Array_Type (Btype) then
5563          return Has_Private_Component (Component_Type (Btype));
5564
5565       elsif Is_Record_Type (Btype) then
5566          Component := First_Component (Btype);
5567          while Present (Component) loop
5568             if Has_Private_Component (Etype (Component)) then
5569                return True;
5570             end if;
5571
5572             Next_Component (Component);
5573          end loop;
5574
5575          return False;
5576
5577       elsif Is_Protected_Type (Btype)
5578         and then Present (Corresponding_Record_Type (Btype))
5579       then
5580          return Has_Private_Component (Corresponding_Record_Type (Btype));
5581
5582       else
5583          return False;
5584       end if;
5585    end Has_Private_Component;
5586
5587    -----------------------------
5588    -- Has_Static_Array_Bounds --
5589    -----------------------------
5590
5591    function Has_Static_Array_Bounds (Typ : Node_Id) return Boolean is
5592       Ndims : constant Nat := Number_Dimensions (Typ);
5593
5594       Index : Node_Id;
5595       Low   : Node_Id;
5596       High  : Node_Id;
5597
5598    begin
5599       --  Unconstrained types do not have static bounds
5600
5601       if not Is_Constrained (Typ) then
5602          return False;
5603       end if;
5604
5605       --  First treat string literals specially, as the lower bound and length
5606       --  of string literals are not stored like those of arrays.
5607
5608       --  A string literal always has static bounds
5609
5610       if Ekind (Typ) = E_String_Literal_Subtype then
5611          return True;
5612       end if;
5613
5614       --  Treat all dimensions in turn
5615
5616       Index := First_Index (Typ);
5617       for Indx in 1 .. Ndims loop
5618
5619          --  In case of an erroneous index which is not a discrete type, return
5620          --  that the type is not static.
5621
5622          if not Is_Discrete_Type (Etype (Index))
5623            or else Etype (Index) = Any_Type
5624          then
5625             return False;
5626          end if;
5627
5628          Get_Index_Bounds (Index, Low, High);
5629
5630          if Error_Posted (Low) or else Error_Posted (High) then
5631             return False;
5632          end if;
5633
5634          if Is_OK_Static_Expression (Low)
5635               and then
5636             Is_OK_Static_Expression (High)
5637          then
5638             null;
5639          else
5640             return False;
5641          end if;
5642
5643          Next (Index);
5644       end loop;
5645
5646       --  If we fall through the loop, all indexes matched
5647
5648       return True;
5649    end Has_Static_Array_Bounds;
5650
5651    ----------------
5652    -- Has_Stream --
5653    ----------------
5654
5655    function Has_Stream (T : Entity_Id) return Boolean is
5656       E : Entity_Id;
5657
5658    begin
5659       if No (T) then
5660          return False;
5661
5662       elsif Is_RTE (Root_Type (T), RE_Root_Stream_Type) then
5663          return True;
5664
5665       elsif Is_Array_Type (T) then
5666          return Has_Stream (Component_Type (T));
5667
5668       elsif Is_Record_Type (T) then
5669          E := First_Component (T);
5670          while Present (E) loop
5671             if Has_Stream (Etype (E)) then
5672                return True;
5673             else
5674                Next_Component (E);
5675             end if;
5676          end loop;
5677
5678          return False;
5679
5680       elsif Is_Private_Type (T) then
5681          return Has_Stream (Underlying_Type (T));
5682
5683       else
5684          return False;
5685       end if;
5686    end Has_Stream;
5687
5688    ----------------
5689    -- Has_Suffix --
5690    ----------------
5691
5692    function Has_Suffix (E : Entity_Id; Suffix : Character) return Boolean is
5693    begin
5694       Get_Name_String (Chars (E));
5695       return Name_Buffer (Name_Len) = Suffix;
5696    end Has_Suffix;
5697
5698    --------------------------
5699    -- Has_Tagged_Component --
5700    --------------------------
5701
5702    function Has_Tagged_Component (Typ : Entity_Id) return Boolean is
5703       Comp : Entity_Id;
5704
5705    begin
5706       if Is_Private_Type (Typ)
5707         and then Present (Underlying_Type (Typ))
5708       then
5709          return Has_Tagged_Component (Underlying_Type (Typ));
5710
5711       elsif Is_Array_Type (Typ) then
5712          return Has_Tagged_Component (Component_Type (Typ));
5713
5714       elsif Is_Tagged_Type (Typ) then
5715          return True;
5716
5717       elsif Is_Record_Type (Typ) then
5718          Comp := First_Component (Typ);
5719          while Present (Comp) loop
5720             if Has_Tagged_Component (Etype (Comp)) then
5721                return True;
5722             end if;
5723
5724             Next_Component (Comp);
5725          end loop;
5726
5727          return False;
5728
5729       else
5730          return False;
5731       end if;
5732    end Has_Tagged_Component;
5733
5734    -------------------------
5735    -- Implementation_Kind --
5736    -------------------------
5737
5738    function Implementation_Kind (Subp : Entity_Id) return Name_Id is
5739       Impl_Prag : constant Node_Id := Get_Rep_Pragma (Subp, Name_Implemented);
5740    begin
5741       pragma Assert (Present (Impl_Prag));
5742       return
5743         Chars (Expression (Last (Pragma_Argument_Associations (Impl_Prag))));
5744    end Implementation_Kind;
5745
5746    --------------------------
5747    -- Implements_Interface --
5748    --------------------------
5749
5750    function Implements_Interface
5751      (Typ_Ent         : Entity_Id;
5752       Iface_Ent       : Entity_Id;
5753       Exclude_Parents : Boolean := False) return Boolean
5754    is
5755       Ifaces_List : Elist_Id;
5756       Elmt        : Elmt_Id;
5757       Iface       : Entity_Id := Base_Type (Iface_Ent);
5758       Typ         : Entity_Id := Base_Type (Typ_Ent);
5759
5760    begin
5761       if Is_Class_Wide_Type (Typ) then
5762          Typ := Root_Type (Typ);
5763       end if;
5764
5765       if not Has_Interfaces (Typ) then
5766          return False;
5767       end if;
5768
5769       if Is_Class_Wide_Type (Iface) then
5770          Iface := Root_Type (Iface);
5771       end if;
5772
5773       Collect_Interfaces (Typ, Ifaces_List);
5774
5775       Elmt := First_Elmt (Ifaces_List);
5776       while Present (Elmt) loop
5777          if Is_Ancestor (Node (Elmt), Typ, Use_Full_View => True)
5778            and then Exclude_Parents
5779          then
5780             null;
5781
5782          elsif Node (Elmt) = Iface then
5783             return True;
5784          end if;
5785
5786          Next_Elmt (Elmt);
5787       end loop;
5788
5789       return False;
5790    end Implements_Interface;
5791
5792    -----------------
5793    -- In_Instance --
5794    -----------------
5795
5796    function In_Instance return Boolean is
5797       Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5798       S         : Entity_Id;
5799
5800    begin
5801       S := Current_Scope;
5802       while Present (S)
5803         and then S /= Standard_Standard
5804       loop
5805          if (Ekind (S) = E_Function
5806               or else Ekind (S) = E_Package
5807               or else Ekind (S) = E_Procedure)
5808            and then Is_Generic_Instance (S)
5809          then
5810             --  A child instance is always compiled in the context of a parent
5811             --  instance. Nevertheless, the actuals are not analyzed in an
5812             --  instance context. We detect this case by examining the current
5813             --  compilation unit, which must be a child instance, and checking
5814             --  that it is not currently on the scope stack.
5815
5816             if Is_Child_Unit (Curr_Unit)
5817               and then
5818                 Nkind (Unit (Cunit (Current_Sem_Unit)))
5819                   = N_Package_Instantiation
5820               and then not In_Open_Scopes (Curr_Unit)
5821             then
5822                return False;
5823             else
5824                return True;
5825             end if;
5826          end if;
5827
5828          S := Scope (S);
5829       end loop;
5830
5831       return False;
5832    end In_Instance;
5833
5834    ----------------------
5835    -- In_Instance_Body --
5836    ----------------------
5837
5838    function In_Instance_Body return Boolean is
5839       S : Entity_Id;
5840
5841    begin
5842       S := Current_Scope;
5843       while Present (S)
5844         and then S /= Standard_Standard
5845       loop
5846          if (Ekind (S) = E_Function
5847               or else Ekind (S) = E_Procedure)
5848            and then Is_Generic_Instance (S)
5849          then
5850             return True;
5851
5852          elsif Ekind (S) = E_Package
5853            and then In_Package_Body (S)
5854            and then Is_Generic_Instance (S)
5855          then
5856             return True;
5857          end if;
5858
5859          S := Scope (S);
5860       end loop;
5861
5862       return False;
5863    end In_Instance_Body;
5864
5865    -----------------------------
5866    -- In_Instance_Not_Visible --
5867    -----------------------------
5868
5869    function In_Instance_Not_Visible return Boolean is
5870       S : Entity_Id;
5871
5872    begin
5873       S := Current_Scope;
5874       while Present (S)
5875         and then S /= Standard_Standard
5876       loop
5877          if (Ekind (S) = E_Function
5878               or else Ekind (S) = E_Procedure)
5879            and then Is_Generic_Instance (S)
5880          then
5881             return True;
5882
5883          elsif Ekind (S) = E_Package
5884            and then (In_Package_Body (S) or else In_Private_Part (S))
5885            and then Is_Generic_Instance (S)
5886          then
5887             return True;
5888          end if;
5889
5890          S := Scope (S);
5891       end loop;
5892
5893       return False;
5894    end In_Instance_Not_Visible;
5895
5896    ------------------------------
5897    -- In_Instance_Visible_Part --
5898    ------------------------------
5899
5900    function In_Instance_Visible_Part return Boolean is
5901       S : Entity_Id;
5902
5903    begin
5904       S := Current_Scope;
5905       while Present (S)
5906         and then S /= Standard_Standard
5907       loop
5908          if Ekind (S) = E_Package
5909            and then Is_Generic_Instance (S)
5910            and then not In_Package_Body (S)
5911            and then not In_Private_Part (S)
5912          then
5913             return True;
5914          end if;
5915
5916          S := Scope (S);
5917       end loop;
5918
5919       return False;
5920    end In_Instance_Visible_Part;
5921
5922    ---------------------
5923    -- In_Package_Body --
5924    ---------------------
5925
5926    function In_Package_Body return Boolean is
5927       S : Entity_Id;
5928
5929    begin
5930       S := Current_Scope;
5931       while Present (S)
5932         and then S /= Standard_Standard
5933       loop
5934          if Ekind (S) = E_Package
5935            and then In_Package_Body (S)
5936          then
5937             return True;
5938          else
5939             S := Scope (S);
5940          end if;
5941       end loop;
5942
5943       return False;
5944    end In_Package_Body;
5945
5946    --------------------------------
5947    -- In_Parameter_Specification --
5948    --------------------------------
5949
5950    function In_Parameter_Specification (N : Node_Id) return Boolean is
5951       PN : Node_Id;
5952
5953    begin
5954       PN := Parent (N);
5955       while Present (PN) loop
5956          if Nkind (PN) = N_Parameter_Specification then
5957             return True;
5958          end if;
5959
5960          PN := Parent (PN);
5961       end loop;
5962
5963       return False;
5964    end In_Parameter_Specification;
5965
5966    --------------------------------------
5967    -- In_Subprogram_Or_Concurrent_Unit --
5968    --------------------------------------
5969
5970    function In_Subprogram_Or_Concurrent_Unit return Boolean is
5971       E : Entity_Id;
5972       K : Entity_Kind;
5973
5974    begin
5975       --  Use scope chain to check successively outer scopes
5976
5977       E := Current_Scope;
5978       loop
5979          K := Ekind (E);
5980
5981          if K in Subprogram_Kind
5982            or else K in Concurrent_Kind
5983            or else K in Generic_Subprogram_Kind
5984          then
5985             return True;
5986
5987          elsif E = Standard_Standard then
5988             return False;
5989          end if;
5990
5991          E := Scope (E);
5992       end loop;
5993    end In_Subprogram_Or_Concurrent_Unit;
5994
5995    ---------------------
5996    -- In_Visible_Part --
5997    ---------------------
5998
5999    function In_Visible_Part (Scope_Id : Entity_Id) return Boolean is
6000    begin
6001       return
6002         Is_Package_Or_Generic_Package (Scope_Id)
6003           and then In_Open_Scopes (Scope_Id)
6004           and then not In_Package_Body (Scope_Id)
6005           and then not In_Private_Part (Scope_Id);
6006    end In_Visible_Part;
6007
6008    --------------------------------
6009    -- Incomplete_Or_Private_View --
6010    --------------------------------
6011
6012    function Incomplete_Or_Private_View (Typ : Entity_Id) return Entity_Id is
6013       function Inspect_Decls
6014         (Decls : List_Id;
6015          Taft  : Boolean := False) return Entity_Id;
6016       --  Check whether a declarative region contains the incomplete or private
6017       --  view of Typ.
6018
6019       -------------------
6020       -- Inspect_Decls --
6021       -------------------
6022
6023       function Inspect_Decls
6024         (Decls : List_Id;
6025          Taft  : Boolean := False) return Entity_Id
6026       is
6027          Decl  : Node_Id;
6028          Match : Node_Id;
6029
6030       begin
6031          Decl := First (Decls);
6032          while Present (Decl) loop
6033             Match := Empty;
6034
6035             if Taft then
6036                if Nkind (Decl) = N_Incomplete_Type_Declaration then
6037                   Match := Defining_Identifier (Decl);
6038                end if;
6039
6040             else
6041                if Nkind_In (Decl, N_Private_Extension_Declaration,
6042                                   N_Private_Type_Declaration)
6043                then
6044                   Match := Defining_Identifier (Decl);
6045                end if;
6046             end if;
6047
6048             if Present (Match)
6049               and then Present (Full_View (Match))
6050               and then Full_View (Match) = Typ
6051             then
6052                return Match;
6053             end if;
6054
6055             Next (Decl);
6056          end loop;
6057
6058          return Empty;
6059       end Inspect_Decls;
6060
6061       --  Local variables
6062
6063       Prev : Entity_Id;
6064
6065    --  Start of processing for Incomplete_Or_Partial_View
6066
6067    begin
6068       --  Incomplete type case
6069
6070       Prev := Current_Entity_In_Scope (Typ);
6071
6072       if Present (Prev)
6073         and then Is_Incomplete_Type (Prev)
6074         and then Present (Full_View (Prev))
6075         and then Full_View (Prev) = Typ
6076       then
6077          return Prev;
6078       end if;
6079
6080       --  Private or Taft amendment type case
6081
6082       declare
6083          Pkg      : constant Entity_Id := Scope (Typ);
6084          Pkg_Decl : Node_Id := Pkg;
6085
6086       begin
6087          if Ekind (Pkg) = E_Package then
6088             while Nkind (Pkg_Decl) /= N_Package_Specification loop
6089                Pkg_Decl := Parent (Pkg_Decl);
6090             end loop;
6091
6092             --  It is knows that Typ has a private view, look for it in the
6093             --  visible declarations of the enclosing scope. A special case
6094             --  of this is when the two views have been exchanged - the full
6095             --  appears earlier than the private.
6096
6097             if Has_Private_Declaration (Typ) then
6098                Prev := Inspect_Decls (Visible_Declarations (Pkg_Decl));
6099
6100                --  Exchanged view case, look in the private declarations
6101
6102                if No (Prev) then
6103                   Prev := Inspect_Decls (Private_Declarations (Pkg_Decl));
6104                end if;
6105
6106                return Prev;
6107
6108             --  Otherwise if this is the package body, then Typ is a potential
6109             --  Taft amendment type. The incomplete view should be located in
6110             --  the private declarations of the enclosing scope.
6111
6112             elsif In_Package_Body (Pkg) then
6113                return Inspect_Decls (Private_Declarations (Pkg_Decl), True);
6114             end if;
6115          end if;
6116       end;
6117
6118       --  The type has no incomplete or private view
6119
6120       return Empty;
6121    end Incomplete_Or_Private_View;
6122
6123    ---------------------------------
6124    -- Insert_Explicit_Dereference --
6125    ---------------------------------
6126
6127    procedure Insert_Explicit_Dereference (N : Node_Id) is
6128       New_Prefix : constant Node_Id := Relocate_Node (N);
6129       Ent        : Entity_Id := Empty;
6130       Pref       : Node_Id;
6131       I          : Interp_Index;
6132       It         : Interp;
6133       T          : Entity_Id;
6134
6135    begin
6136       Save_Interps (N, New_Prefix);
6137
6138       Rewrite (N,
6139         Make_Explicit_Dereference (Sloc (Parent (N)),
6140           Prefix => New_Prefix));
6141
6142       Set_Etype (N, Designated_Type (Etype (New_Prefix)));
6143
6144       if Is_Overloaded (New_Prefix) then
6145
6146          --  The dereference is also overloaded, and its interpretations are
6147          --  the designated types of the interpretations of the original node.
6148
6149          Set_Etype (N, Any_Type);
6150
6151          Get_First_Interp (New_Prefix, I, It);
6152          while Present (It.Nam) loop
6153             T := It.Typ;
6154
6155             if Is_Access_Type (T) then
6156                Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
6157             end if;
6158
6159             Get_Next_Interp (I, It);
6160          end loop;
6161
6162          End_Interp_List;
6163
6164       else
6165          --  Prefix is unambiguous: mark the original prefix (which might
6166          --  Come_From_Source) as a reference, since the new (relocated) one
6167          --  won't be taken into account.
6168
6169          if Is_Entity_Name (New_Prefix) then
6170             Ent := Entity (New_Prefix);
6171             Pref := New_Prefix;
6172
6173          --  For a retrieval of a subcomponent of some composite object,
6174          --  retrieve the ultimate entity if there is one.
6175
6176          elsif Nkind (New_Prefix) = N_Selected_Component
6177            or else Nkind (New_Prefix) = N_Indexed_Component
6178          then
6179             Pref := Prefix (New_Prefix);
6180             while Present (Pref)
6181               and then
6182                 (Nkind (Pref) = N_Selected_Component
6183                   or else Nkind (Pref) = N_Indexed_Component)
6184             loop
6185                Pref := Prefix (Pref);
6186             end loop;
6187
6188             if Present (Pref) and then Is_Entity_Name (Pref) then
6189                Ent := Entity (Pref);
6190             end if;
6191          end if;
6192
6193          --  Place the reference on the entity node
6194
6195          if Present (Ent) then
6196             Generate_Reference (Ent, Pref);
6197          end if;
6198       end if;
6199    end Insert_Explicit_Dereference;
6200
6201    ------------------------------------------
6202    -- Inspect_Deferred_Constant_Completion --
6203    ------------------------------------------
6204
6205    procedure Inspect_Deferred_Constant_Completion (Decls : List_Id) is
6206       Decl   : Node_Id;
6207
6208    begin
6209       Decl := First (Decls);
6210       while Present (Decl) loop
6211
6212          --  Deferred constant signature
6213
6214          if Nkind (Decl) = N_Object_Declaration
6215            and then Constant_Present (Decl)
6216            and then No (Expression (Decl))
6217
6218             --  No need to check internally generated constants
6219
6220            and then Comes_From_Source (Decl)
6221
6222             --  The constant is not completed. A full object declaration or a
6223             --  pragma Import complete a deferred constant.
6224
6225            and then not Has_Completion (Defining_Identifier (Decl))
6226          then
6227             Error_Msg_N
6228               ("constant declaration requires initialization expression",
6229               Defining_Identifier (Decl));
6230          end if;
6231
6232          Decl := Next (Decl);
6233       end loop;
6234    end Inspect_Deferred_Constant_Completion;
6235
6236    -----------------------------
6237    -- Is_Actual_Out_Parameter --
6238    -----------------------------
6239
6240    function Is_Actual_Out_Parameter (N : Node_Id) return Boolean is
6241       Formal : Entity_Id;
6242       Call   : Node_Id;
6243    begin
6244       Find_Actual (N, Formal, Call);
6245       return Present (Formal) and then Ekind (Formal) = E_Out_Parameter;
6246    end Is_Actual_Out_Parameter;
6247
6248    -------------------------
6249    -- Is_Actual_Parameter --
6250    -------------------------
6251
6252    function Is_Actual_Parameter (N : Node_Id) return Boolean is
6253       PK : constant Node_Kind := Nkind (Parent (N));
6254
6255    begin
6256       case PK is
6257          when N_Parameter_Association =>
6258             return N = Explicit_Actual_Parameter (Parent (N));
6259
6260          when N_Function_Call | N_Procedure_Call_Statement =>
6261             return Is_List_Member (N)
6262               and then
6263                 List_Containing (N) = Parameter_Associations (Parent (N));
6264
6265          when others =>
6266             return False;
6267       end case;
6268    end Is_Actual_Parameter;
6269
6270    --------------------------------
6271    -- Is_Actual_Tagged_Parameter --
6272    --------------------------------
6273
6274    function Is_Actual_Tagged_Parameter (N : Node_Id) return Boolean is
6275       Formal : Entity_Id;
6276       Call   : Node_Id;
6277    begin
6278       Find_Actual (N, Formal, Call);
6279       return Present (Formal) and then Is_Tagged_Type (Etype (Formal));
6280    end Is_Actual_Tagged_Parameter;
6281
6282    ---------------------
6283    -- Is_Aliased_View --
6284    ---------------------
6285
6286    function Is_Aliased_View (Obj : Node_Id) return Boolean is
6287       E : Entity_Id;
6288
6289    begin
6290       if Is_Entity_Name (Obj) then
6291
6292          E := Entity (Obj);
6293
6294          return
6295            (Is_Object (E)
6296              and then
6297                (Is_Aliased (E)
6298                   or else (Present (Renamed_Object (E))
6299                              and then Is_Aliased_View (Renamed_Object (E)))))
6300
6301            or else ((Is_Formal (E)
6302                       or else Ekind (E) = E_Generic_In_Out_Parameter
6303                       or else Ekind (E) = E_Generic_In_Parameter)
6304                     and then Is_Tagged_Type (Etype (E)))
6305
6306            or else (Is_Concurrent_Type (E)
6307                      and then In_Open_Scopes (E))
6308
6309             --  Current instance of type, either directly or as rewritten
6310             --  reference to the current object.
6311
6312            or else (Is_Entity_Name (Original_Node (Obj))
6313                      and then Present (Entity (Original_Node (Obj)))
6314                      and then Is_Type (Entity (Original_Node (Obj))))
6315
6316            or else (Is_Type (E) and then E = Current_Scope)
6317
6318            or else (Is_Incomplete_Or_Private_Type (E)
6319                      and then Full_View (E) = Current_Scope);
6320
6321       elsif Nkind (Obj) = N_Selected_Component then
6322          return Is_Aliased (Entity (Selector_Name (Obj)));
6323
6324       elsif Nkind (Obj) = N_Indexed_Component then
6325          return Has_Aliased_Components (Etype (Prefix (Obj)))
6326            or else
6327              (Is_Access_Type (Etype (Prefix (Obj)))
6328                and then
6329               Has_Aliased_Components
6330                 (Designated_Type (Etype (Prefix (Obj)))));
6331
6332       elsif Nkind (Obj) = N_Unchecked_Type_Conversion
6333         or else Nkind (Obj) = N_Type_Conversion
6334       then
6335          return Is_Tagged_Type (Etype (Obj))
6336            and then Is_Aliased_View (Expression (Obj));
6337
6338       elsif Nkind (Obj) = N_Explicit_Dereference then
6339          return Nkind (Original_Node (Obj)) /= N_Function_Call;
6340
6341       else
6342          return False;
6343       end if;
6344    end Is_Aliased_View;
6345
6346    -------------------------
6347    -- Is_Ancestor_Package --
6348    -------------------------
6349
6350    function Is_Ancestor_Package
6351      (E1 : Entity_Id;
6352       E2 : Entity_Id) return Boolean
6353    is
6354       Par : Entity_Id;
6355
6356    begin
6357       Par := E2;
6358       while Present (Par)
6359         and then Par /= Standard_Standard
6360       loop
6361          if Par = E1 then
6362             return True;
6363          end if;
6364
6365          Par := Scope (Par);
6366       end loop;
6367
6368       return False;
6369    end Is_Ancestor_Package;
6370
6371    ----------------------
6372    -- Is_Atomic_Object --
6373    ----------------------
6374
6375    function Is_Atomic_Object (N : Node_Id) return Boolean is
6376
6377       function Object_Has_Atomic_Components (N : Node_Id) return Boolean;
6378       --  Determines if given object has atomic components
6379
6380       function Is_Atomic_Prefix (N : Node_Id) return Boolean;
6381       --  If prefix is an implicit dereference, examine designated type
6382
6383       ----------------------
6384       -- Is_Atomic_Prefix --
6385       ----------------------
6386
6387       function Is_Atomic_Prefix (N : Node_Id) return Boolean is
6388       begin
6389          if Is_Access_Type (Etype (N)) then
6390             return
6391               Has_Atomic_Components (Designated_Type (Etype (N)));
6392          else
6393             return Object_Has_Atomic_Components (N);
6394          end if;
6395       end Is_Atomic_Prefix;
6396
6397       ----------------------------------
6398       -- Object_Has_Atomic_Components --
6399       ----------------------------------
6400
6401       function Object_Has_Atomic_Components (N : Node_Id) return Boolean is
6402       begin
6403          if Has_Atomic_Components (Etype (N))
6404            or else Is_Atomic (Etype (N))
6405          then
6406             return True;
6407
6408          elsif Is_Entity_Name (N)
6409            and then (Has_Atomic_Components (Entity (N))
6410                       or else Is_Atomic (Entity (N)))
6411          then
6412             return True;
6413
6414          elsif Nkind (N) = N_Indexed_Component
6415            or else Nkind (N) = N_Selected_Component
6416          then
6417             return Is_Atomic_Prefix (Prefix (N));
6418
6419          else
6420             return False;
6421          end if;
6422       end Object_Has_Atomic_Components;
6423
6424    --  Start of processing for Is_Atomic_Object
6425
6426    begin
6427       --  Predicate is not relevant to subprograms
6428
6429       if Is_Entity_Name (N) and then Is_Overloadable (Entity (N)) then
6430          return False;
6431
6432       elsif Is_Atomic (Etype (N))
6433         or else (Is_Entity_Name (N) and then Is_Atomic (Entity (N)))
6434       then
6435          return True;
6436
6437       elsif Nkind (N) = N_Indexed_Component
6438         or else Nkind (N) = N_Selected_Component
6439       then
6440          return Is_Atomic_Prefix (Prefix (N));
6441
6442       else
6443          return False;
6444       end if;
6445    end Is_Atomic_Object;
6446
6447    -----------------------------
6448    -- Is_Concurrent_Interface --
6449    -----------------------------
6450
6451    function Is_Concurrent_Interface (T : Entity_Id) return Boolean is
6452    begin
6453       return
6454         Is_Interface (T)
6455           and then
6456             (Is_Protected_Interface (T)
6457                or else Is_Synchronized_Interface (T)
6458                or else Is_Task_Interface (T));
6459    end Is_Concurrent_Interface;
6460
6461    --------------------------------------
6462    -- Is_Controlling_Limited_Procedure --
6463    --------------------------------------
6464
6465    function Is_Controlling_Limited_Procedure
6466      (Proc_Nam : Entity_Id) return Boolean
6467    is
6468       Param_Typ : Entity_Id := Empty;
6469
6470    begin
6471       if Ekind (Proc_Nam) = E_Procedure
6472         and then Present (Parameter_Specifications (Parent (Proc_Nam)))
6473       then
6474          Param_Typ := Etype (Parameter_Type (First (
6475                         Parameter_Specifications (Parent (Proc_Nam)))));
6476
6477       --  In this case where an Itype was created, the procedure call has been
6478       --  rewritten.
6479
6480       elsif Present (Associated_Node_For_Itype (Proc_Nam))
6481         and then Present (Original_Node (Associated_Node_For_Itype (Proc_Nam)))
6482         and then
6483           Present (Parameter_Associations
6484                      (Associated_Node_For_Itype (Proc_Nam)))
6485       then
6486          Param_Typ :=
6487            Etype (First (Parameter_Associations
6488                           (Associated_Node_For_Itype (Proc_Nam))));
6489       end if;
6490
6491       if Present (Param_Typ) then
6492          return
6493            Is_Interface (Param_Typ)
6494              and then Is_Limited_Record (Param_Typ);
6495       end if;
6496
6497       return False;
6498    end Is_Controlling_Limited_Procedure;
6499
6500    -----------------------------
6501    -- Is_CPP_Constructor_Call --
6502    -----------------------------
6503
6504    function Is_CPP_Constructor_Call (N : Node_Id) return Boolean is
6505    begin
6506       return Nkind (N) = N_Function_Call
6507         and then Is_CPP_Class (Etype (Etype (N)))
6508         and then Is_Constructor (Entity (Name (N)))
6509         and then Is_Imported (Entity (Name (N)));
6510    end Is_CPP_Constructor_Call;
6511
6512    -----------------
6513    -- Is_Delegate --
6514    -----------------
6515
6516    function Is_Delegate (T : Entity_Id) return Boolean is
6517       Desig_Type : Entity_Id;
6518
6519    begin
6520       if VM_Target /= CLI_Target then
6521          return False;
6522       end if;
6523
6524       --  Access-to-subprograms are delegates in CIL
6525
6526       if Ekind (T) = E_Access_Subprogram_Type then
6527          return True;
6528       end if;
6529
6530       if Ekind (T) not in Access_Kind then
6531
6532          --  A delegate is a managed pointer. If no designated type is defined
6533          --  it means that it's not a delegate.
6534
6535          return False;
6536       end if;
6537
6538       Desig_Type := Etype (Directly_Designated_Type (T));
6539
6540       if not Is_Tagged_Type (Desig_Type) then
6541          return False;
6542       end if;
6543
6544       --  Test if the type is inherited from [mscorlib]System.Delegate
6545
6546       while Etype (Desig_Type) /= Desig_Type loop
6547          if Chars (Scope (Desig_Type)) /= No_Name
6548            and then Is_Imported (Scope (Desig_Type))
6549            and then Get_Name_String (Chars (Scope (Desig_Type))) = "delegate"
6550          then
6551             return True;
6552          end if;
6553
6554          Desig_Type := Etype (Desig_Type);
6555       end loop;
6556
6557       return False;
6558    end Is_Delegate;
6559
6560    ----------------------------------------------
6561    -- Is_Dependent_Component_Of_Mutable_Object --
6562    ----------------------------------------------
6563
6564    function Is_Dependent_Component_Of_Mutable_Object
6565      (Object : Node_Id) return Boolean
6566    is
6567       P           : Node_Id;
6568       Prefix_Type : Entity_Id;
6569       P_Aliased   : Boolean := False;
6570       Comp        : Entity_Id;
6571
6572       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean;
6573       --  Returns True if and only if Comp is declared within a variant part
6574
6575       --------------------------------
6576       -- Is_Declared_Within_Variant --
6577       --------------------------------
6578
6579       function Is_Declared_Within_Variant (Comp : Entity_Id) return Boolean is
6580          Comp_Decl : constant Node_Id   := Parent (Comp);
6581          Comp_List : constant Node_Id   := Parent (Comp_Decl);
6582       begin
6583          return Nkind (Parent (Comp_List)) = N_Variant;
6584       end Is_Declared_Within_Variant;
6585
6586    --  Start of processing for Is_Dependent_Component_Of_Mutable_Object
6587
6588    begin
6589       if Is_Variable (Object) then
6590
6591          if Nkind (Object) = N_Selected_Component then
6592             P := Prefix (Object);
6593             Prefix_Type := Etype (P);
6594
6595             if Is_Entity_Name (P) then
6596
6597                if Ekind (Entity (P)) = E_Generic_In_Out_Parameter then
6598                   Prefix_Type := Base_Type (Prefix_Type);
6599                end if;
6600
6601                if Is_Aliased (Entity (P)) then
6602                   P_Aliased := True;
6603                end if;
6604
6605             --  A discriminant check on a selected component may be expanded
6606             --  into a dereference when removing side-effects. Recover the
6607             --  original node and its type, which may be unconstrained.
6608
6609             elsif Nkind (P) = N_Explicit_Dereference
6610               and then not (Comes_From_Source (P))
6611             then
6612                P := Original_Node (P);
6613                Prefix_Type := Etype (P);
6614
6615             else
6616                --  Check for prefix being an aliased component???
6617
6618                null;
6619
6620             end if;
6621
6622             --  A heap object is constrained by its initial value
6623
6624             --  Ada 2005 (AI-363): Always assume the object could be mutable in
6625             --  the dereferenced case, since the access value might denote an
6626             --  unconstrained aliased object, whereas in Ada 95 the designated
6627             --  object is guaranteed to be constrained. A worst-case assumption
6628             --  has to apply in Ada 2005 because we can't tell at compile time
6629             --  whether the object is "constrained by its initial value"
6630             --  (despite the fact that 3.10.2(26/2) and 8.5.1(5/2) are
6631             --  semantic rules -- these rules are acknowledged to need fixing).
6632
6633             if Ada_Version < Ada_2005 then
6634                if Is_Access_Type (Prefix_Type)
6635                  or else Nkind (P) = N_Explicit_Dereference
6636                then
6637                   return False;
6638                end if;
6639
6640             elsif Ada_Version >= Ada_2005 then
6641                if Is_Access_Type (Prefix_Type) then
6642
6643                   --  If the access type is pool-specific, and there is no
6644                   --  constrained partial view of the designated type, then the
6645                   --  designated object is known to be constrained.
6646
6647                   if Ekind (Prefix_Type) = E_Access_Type
6648                     and then not Has_Constrained_Partial_View
6649                                    (Designated_Type (Prefix_Type))
6650                   then
6651                      return False;
6652
6653                   --  Otherwise (general access type, or there is a constrained
6654                   --  partial view of the designated type), we need to check
6655                   --  based on the designated type.
6656
6657                   else
6658                      Prefix_Type := Designated_Type (Prefix_Type);
6659                   end if;
6660                end if;
6661             end if;
6662
6663             Comp :=
6664               Original_Record_Component (Entity (Selector_Name (Object)));
6665
6666             --  As per AI-0017, the renaming is illegal in a generic body, even
6667             --  if the subtype is indefinite.
6668
6669             --  Ada 2005 (AI-363): In Ada 2005 an aliased object can be mutable
6670
6671             if not Is_Constrained (Prefix_Type)
6672               and then (not Is_Indefinite_Subtype (Prefix_Type)
6673                          or else
6674                           (Is_Generic_Type (Prefix_Type)
6675                             and then Ekind (Current_Scope) = E_Generic_Package
6676                             and then In_Package_Body (Current_Scope)))
6677
6678               and then (Is_Declared_Within_Variant (Comp)
6679                           or else Has_Discriminant_Dependent_Constraint (Comp))
6680               and then (not P_Aliased or else Ada_Version >= Ada_2005)
6681             then
6682                return True;
6683
6684             else
6685                return
6686                  Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
6687
6688             end if;
6689
6690          elsif Nkind (Object) = N_Indexed_Component
6691            or else Nkind (Object) = N_Slice
6692          then
6693             return Is_Dependent_Component_Of_Mutable_Object (Prefix (Object));
6694
6695          --  A type conversion that Is_Variable is a view conversion:
6696          --  go back to the denoted object.
6697
6698          elsif Nkind (Object) = N_Type_Conversion then
6699             return
6700               Is_Dependent_Component_Of_Mutable_Object (Expression (Object));
6701          end if;
6702       end if;
6703
6704       return False;
6705    end Is_Dependent_Component_Of_Mutable_Object;
6706
6707    ---------------------
6708    -- Is_Dereferenced --
6709    ---------------------
6710
6711    function Is_Dereferenced (N : Node_Id) return Boolean is
6712       P : constant Node_Id := Parent (N);
6713    begin
6714       return
6715          (Nkind (P) = N_Selected_Component
6716             or else
6717           Nkind (P) = N_Explicit_Dereference
6718             or else
6719           Nkind (P) = N_Indexed_Component
6720             or else
6721           Nkind (P) = N_Slice)
6722         and then Prefix (P) = N;
6723    end Is_Dereferenced;
6724
6725    ----------------------
6726    -- Is_Descendent_Of --
6727    ----------------------
6728
6729    function Is_Descendent_Of (T1 : Entity_Id; T2 : Entity_Id) return Boolean is
6730       T    : Entity_Id;
6731       Etyp : Entity_Id;
6732
6733    begin
6734       pragma Assert (Nkind (T1) in N_Entity);
6735       pragma Assert (Nkind (T2) in N_Entity);
6736
6737       T := Base_Type (T1);
6738
6739       --  Immediate return if the types match
6740
6741       if T = T2 then
6742          return True;
6743
6744       --  Comment needed here ???
6745
6746       elsif Ekind (T) = E_Class_Wide_Type then
6747          return Etype (T) = T2;
6748
6749       --  All other cases
6750
6751       else
6752          loop
6753             Etyp := Etype (T);
6754
6755             --  Done if we found the type we are looking for
6756
6757             if Etyp = T2 then
6758                return True;
6759
6760             --  Done if no more derivations to check
6761
6762             elsif T = T1
6763               or else T = Etyp
6764             then
6765                return False;
6766
6767             --  Following test catches error cases resulting from prev errors
6768
6769             elsif No (Etyp) then
6770                return False;
6771
6772             elsif Is_Private_Type (T) and then Etyp = Full_View (T) then
6773                return False;
6774
6775             elsif Is_Private_Type (Etyp) and then Full_View (Etyp) = T then
6776                return False;
6777             end if;
6778
6779             T := Base_Type (Etyp);
6780          end loop;
6781       end if;
6782    end Is_Descendent_Of;
6783
6784    ----------------------------
6785    -- Is_Expression_Function --
6786    ----------------------------
6787
6788    function Is_Expression_Function (Subp : Entity_Id) return Boolean is
6789       Decl : constant Node_Id := Unit_Declaration_Node (Subp);
6790
6791    begin
6792       return Ekind (Subp) = E_Function
6793         and then Nkind (Decl) = N_Subprogram_Declaration
6794         and then
6795           (Nkind (Original_Node (Decl)) = N_Expression_Function
6796             or else
6797               (Present (Corresponding_Body (Decl))
6798                 and then
6799                   Nkind (Original_Node
6800                      (Unit_Declaration_Node (Corresponding_Body (Decl))))
6801                  = N_Expression_Function));
6802    end Is_Expression_Function;
6803
6804    --------------
6805    -- Is_False --
6806    --------------
6807
6808    function Is_False (U : Uint) return Boolean is
6809    begin
6810       return (U = 0);
6811    end Is_False;
6812
6813    ---------------------------
6814    -- Is_Fixed_Model_Number --
6815    ---------------------------
6816
6817    function Is_Fixed_Model_Number (U : Ureal; T : Entity_Id) return Boolean is
6818       S : constant Ureal := Small_Value (T);
6819       M : Urealp.Save_Mark;
6820       R : Boolean;
6821    begin
6822       M := Urealp.Mark;
6823       R := (U = UR_Trunc (U / S) * S);
6824       Urealp.Release (M);
6825       return R;
6826    end Is_Fixed_Model_Number;
6827
6828    -------------------------------
6829    -- Is_Fully_Initialized_Type --
6830    -------------------------------
6831
6832    function Is_Fully_Initialized_Type (Typ : Entity_Id) return Boolean is
6833    begin
6834       if Is_Scalar_Type (Typ) then
6835          return False;
6836
6837       elsif Is_Access_Type (Typ) then
6838          return True;
6839
6840       elsif Is_Array_Type (Typ) then
6841          if Is_Fully_Initialized_Type (Component_Type (Typ)) then
6842             return True;
6843          end if;
6844
6845          --  An interesting case, if we have a constrained type one of whose
6846          --  bounds is known to be null, then there are no elements to be
6847          --  initialized, so all the elements are initialized!
6848
6849          if Is_Constrained (Typ) then
6850             declare
6851                Indx     : Node_Id;
6852                Indx_Typ : Entity_Id;
6853                Lbd, Hbd : Node_Id;
6854
6855             begin
6856                Indx := First_Index (Typ);
6857                while Present (Indx) loop
6858                   if Etype (Indx) = Any_Type then
6859                      return False;
6860
6861                   --  If index is a range, use directly
6862
6863                   elsif Nkind (Indx) = N_Range then
6864                      Lbd := Low_Bound  (Indx);
6865                      Hbd := High_Bound (Indx);
6866
6867                   else
6868                      Indx_Typ := Etype (Indx);
6869
6870                      if Is_Private_Type (Indx_Typ)  then
6871                         Indx_Typ := Full_View (Indx_Typ);
6872                      end if;
6873
6874                      if No (Indx_Typ) or else Etype (Indx_Typ) = Any_Type then
6875                         return False;
6876                      else
6877                         Lbd := Type_Low_Bound  (Indx_Typ);
6878                         Hbd := Type_High_Bound (Indx_Typ);
6879                      end if;
6880                   end if;
6881
6882                   if Compile_Time_Known_Value (Lbd)
6883                     and then Compile_Time_Known_Value (Hbd)
6884                   then
6885                      if Expr_Value (Hbd) < Expr_Value (Lbd) then
6886                         return True;
6887                      end if;
6888                   end if;
6889
6890                   Next_Index (Indx);
6891                end loop;
6892             end;
6893          end if;
6894
6895          --  If no null indexes, then type is not fully initialized
6896
6897          return False;
6898
6899       --  Record types
6900
6901       elsif Is_Record_Type (Typ) then
6902          if Has_Discriminants (Typ)
6903            and then
6904              Present (Discriminant_Default_Value (First_Discriminant (Typ)))
6905            and then Is_Fully_Initialized_Variant (Typ)
6906          then
6907             return True;
6908          end if;
6909
6910          --  Controlled records are considered to be fully initialized if
6911          --  there is a user defined Initialize routine. This may not be
6912          --  entirely correct, but as the spec notes, we are guessing here
6913          --  what is best from the point of view of issuing warnings.
6914
6915          if Is_Controlled (Typ) then
6916             declare
6917                Utyp : constant Entity_Id := Underlying_Type (Typ);
6918
6919             begin
6920                if Present (Utyp) then
6921                   declare
6922                      Init : constant Entity_Id :=
6923                               (Find_Prim_Op
6924                                  (Underlying_Type (Typ), Name_Initialize));
6925
6926                   begin
6927                      if Present (Init)
6928                        and then Comes_From_Source (Init)
6929                        and then not
6930                          Is_Predefined_File_Name
6931                            (File_Name (Get_Source_File_Index (Sloc (Init))))
6932                      then
6933                         return True;
6934
6935                      elsif Has_Null_Extension (Typ)
6936                         and then
6937                           Is_Fully_Initialized_Type
6938                             (Etype (Base_Type (Typ)))
6939                      then
6940                         return True;
6941                      end if;
6942                   end;
6943                end if;
6944             end;
6945          end if;
6946
6947          --  Otherwise see if all record components are initialized
6948
6949          declare
6950             Ent : Entity_Id;
6951
6952          begin
6953             Ent := First_Entity (Typ);
6954             while Present (Ent) loop
6955                if Ekind (Ent) = E_Component
6956                  and then (No (Parent (Ent))
6957                              or else No (Expression (Parent (Ent))))
6958                  and then not Is_Fully_Initialized_Type (Etype (Ent))
6959
6960                   --  Special VM case for tag components, which need to be
6961                   --  defined in this case, but are never initialized as VMs
6962                   --  are using other dispatching mechanisms. Ignore this
6963                   --  uninitialized case. Note that this applies both to the
6964                   --  uTag entry and the main vtable pointer (CPP_Class case).
6965
6966                  and then (Tagged_Type_Expansion or else not Is_Tag (Ent))
6967                then
6968                   return False;
6969                end if;
6970
6971                Next_Entity (Ent);
6972             end loop;
6973          end;
6974
6975          --  No uninitialized components, so type is fully initialized.
6976          --  Note that this catches the case of no components as well.
6977
6978          return True;
6979
6980       elsif Is_Concurrent_Type (Typ) then
6981          return True;
6982
6983       elsif Is_Private_Type (Typ) then
6984          declare
6985             U : constant Entity_Id := Underlying_Type (Typ);
6986
6987          begin
6988             if No (U) then
6989                return False;
6990             else
6991                return Is_Fully_Initialized_Type (U);
6992             end if;
6993          end;
6994
6995       else
6996          return False;
6997       end if;
6998    end Is_Fully_Initialized_Type;
6999
7000    ----------------------------------
7001    -- Is_Fully_Initialized_Variant --
7002    ----------------------------------
7003
7004    function Is_Fully_Initialized_Variant (Typ : Entity_Id) return Boolean is
7005       Loc           : constant Source_Ptr := Sloc (Typ);
7006       Constraints   : constant List_Id    := New_List;
7007       Components    : constant Elist_Id   := New_Elmt_List;
7008       Comp_Elmt     : Elmt_Id;
7009       Comp_Id       : Node_Id;
7010       Comp_List     : Node_Id;
7011       Discr         : Entity_Id;
7012       Discr_Val     : Node_Id;
7013
7014       Report_Errors : Boolean;
7015       pragma Warnings (Off, Report_Errors);
7016
7017    begin
7018       if Serious_Errors_Detected > 0 then
7019          return False;
7020       end if;
7021
7022       if Is_Record_Type (Typ)
7023         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
7024         and then Nkind (Type_Definition (Parent (Typ))) = N_Record_Definition
7025       then
7026          Comp_List := Component_List (Type_Definition (Parent (Typ)));
7027
7028          Discr := First_Discriminant (Typ);
7029          while Present (Discr) loop
7030             if Nkind (Parent (Discr)) = N_Discriminant_Specification then
7031                Discr_Val := Expression (Parent (Discr));
7032
7033                if Present (Discr_Val)
7034                  and then Is_OK_Static_Expression (Discr_Val)
7035                then
7036                   Append_To (Constraints,
7037                     Make_Component_Association (Loc,
7038                       Choices    => New_List (New_Occurrence_Of (Discr, Loc)),
7039                       Expression => New_Copy (Discr_Val)));
7040                else
7041                   return False;
7042                end if;
7043             else
7044                return False;
7045             end if;
7046
7047             Next_Discriminant (Discr);
7048          end loop;
7049
7050          Gather_Components
7051            (Typ           => Typ,
7052             Comp_List     => Comp_List,
7053             Governed_By   => Constraints,
7054             Into          => Components,
7055             Report_Errors => Report_Errors);
7056
7057          --  Check that each component present is fully initialized
7058
7059          Comp_Elmt := First_Elmt (Components);
7060          while Present (Comp_Elmt) loop
7061             Comp_Id := Node (Comp_Elmt);
7062
7063             if Ekind (Comp_Id) = E_Component
7064               and then (No (Parent (Comp_Id))
7065                          or else No (Expression (Parent (Comp_Id))))
7066               and then not Is_Fully_Initialized_Type (Etype (Comp_Id))
7067             then
7068                return False;
7069             end if;
7070
7071             Next_Elmt (Comp_Elmt);
7072          end loop;
7073
7074          return True;
7075
7076       elsif Is_Private_Type (Typ) then
7077          declare
7078             U : constant Entity_Id := Underlying_Type (Typ);
7079
7080          begin
7081             if No (U) then
7082                return False;
7083             else
7084                return Is_Fully_Initialized_Variant (U);
7085             end if;
7086          end;
7087       else
7088          return False;
7089       end if;
7090    end Is_Fully_Initialized_Variant;
7091
7092    ------------
7093    -- Is_LHS --
7094    ------------
7095
7096    --  We seem to have a lot of overlapping functions that do similar things
7097    --  (testing for left hand sides or lvalues???). Anyway, since this one is
7098    --  purely syntactic, it should be in Sem_Aux I would think???
7099
7100    function Is_LHS (N : Node_Id) return Boolean is
7101       P : constant Node_Id := Parent (N);
7102
7103    begin
7104       if Nkind (P) = N_Assignment_Statement then
7105          return Name (P) = N;
7106
7107       elsif
7108         Nkind_In (P, N_Indexed_Component, N_Selected_Component, N_Slice)
7109       then
7110          return N = Prefix (P) and then Is_LHS (P);
7111
7112       else
7113          return False;
7114       end if;
7115    end Is_LHS;
7116
7117    ----------------------------
7118    -- Is_Inherited_Operation --
7119    ----------------------------
7120
7121    function Is_Inherited_Operation (E : Entity_Id) return Boolean is
7122       Kind : constant Node_Kind := Nkind (Parent (E));
7123    begin
7124       pragma Assert (Is_Overloadable (E));
7125       return Kind = N_Full_Type_Declaration
7126         or else Kind = N_Private_Extension_Declaration
7127         or else Kind = N_Subtype_Declaration
7128         or else (Ekind (E) = E_Enumeration_Literal
7129                   and then Is_Derived_Type (Etype (E)));
7130    end Is_Inherited_Operation;
7131
7132    -------------------------------------
7133    -- Is_Inherited_Operation_For_Type --
7134    -------------------------------------
7135
7136    function Is_Inherited_Operation_For_Type
7137      (E : Entity_Id; Typ : Entity_Id) return Boolean
7138    is
7139    begin
7140       return Is_Inherited_Operation (E)
7141         and then Etype (Parent (E)) = Typ;
7142    end Is_Inherited_Operation_For_Type;
7143
7144    -----------------------------
7145    -- Is_Library_Level_Entity --
7146    -----------------------------
7147
7148    function Is_Library_Level_Entity (E : Entity_Id) return Boolean is
7149    begin
7150       --  The following is a small optimization, and it also properly handles
7151       --  discriminals, which in task bodies might appear in expressions before
7152       --  the corresponding procedure has been created, and which therefore do
7153       --  not have an assigned scope.
7154
7155       if Is_Formal (E) then
7156          return False;
7157       end if;
7158
7159       --  Normal test is simply that the enclosing dynamic scope is Standard
7160
7161       return Enclosing_Dynamic_Scope (E) = Standard_Standard;
7162    end Is_Library_Level_Entity;
7163
7164    ---------------------------------
7165    -- Is_Local_Variable_Reference --
7166    ---------------------------------
7167
7168    function Is_Local_Variable_Reference (Expr : Node_Id) return Boolean is
7169    begin
7170       if not Is_Entity_Name (Expr) then
7171          return False;
7172
7173       else
7174          declare
7175             Ent : constant Entity_Id := Entity (Expr);
7176             Sub : constant Entity_Id := Enclosing_Subprogram (Ent);
7177          begin
7178             if not Ekind_In (Ent, E_Variable, E_In_Out_Parameter) then
7179                return False;
7180             else
7181                return Present (Sub) and then Sub = Current_Subprogram;
7182             end if;
7183          end;
7184       end if;
7185    end Is_Local_Variable_Reference;
7186
7187    -------------------------
7188    -- Is_Object_Reference --
7189    -------------------------
7190
7191    function Is_Object_Reference (N : Node_Id) return Boolean is
7192    begin
7193       if Is_Entity_Name (N) then
7194          return Present (Entity (N)) and then Is_Object (Entity (N));
7195
7196       else
7197          case Nkind (N) is
7198             when N_Indexed_Component | N_Slice =>
7199                return
7200                  Is_Object_Reference (Prefix (N))
7201                    or else Is_Access_Type (Etype (Prefix (N)));
7202
7203             --  In Ada95, a function call is a constant object; a procedure
7204             --  call is not.
7205
7206             when N_Function_Call =>
7207                return Etype (N) /= Standard_Void_Type;
7208
7209             --  A reference to the stream attribute Input is a function call
7210
7211             when N_Attribute_Reference =>
7212                return Attribute_Name (N) = Name_Input;
7213
7214             when N_Selected_Component =>
7215                return
7216                  Is_Object_Reference (Selector_Name (N))
7217                    and then
7218                      (Is_Object_Reference (Prefix (N))
7219                         or else Is_Access_Type (Etype (Prefix (N))));
7220
7221             when N_Explicit_Dereference =>
7222                return True;
7223
7224             --  A view conversion of a tagged object is an object reference
7225
7226             when N_Type_Conversion =>
7227                return Is_Tagged_Type (Etype (Subtype_Mark (N)))
7228                  and then Is_Tagged_Type (Etype (Expression (N)))
7229                  and then Is_Object_Reference (Expression (N));
7230
7231             --  An unchecked type conversion is considered to be an object if
7232             --  the operand is an object (this construction arises only as a
7233             --  result of expansion activities).
7234
7235             when N_Unchecked_Type_Conversion =>
7236                return True;
7237
7238             when others =>
7239                return False;
7240          end case;
7241       end if;
7242    end Is_Object_Reference;
7243
7244    -----------------------------------
7245    -- Is_OK_Variable_For_Out_Formal --
7246    -----------------------------------
7247
7248    function Is_OK_Variable_For_Out_Formal (AV : Node_Id) return Boolean is
7249    begin
7250       Note_Possible_Modification (AV, Sure => True);
7251
7252       --  We must reject parenthesized variable names. The check for
7253       --  Comes_From_Source is present because there are currently
7254       --  cases where the compiler violates this rule (e.g. passing
7255       --  a task object to its controlled Initialize routine).
7256
7257       if Paren_Count (AV) > 0 and then Comes_From_Source (AV) then
7258          return False;
7259
7260       --  A variable is always allowed
7261
7262       elsif Is_Variable (AV) then
7263          return True;
7264
7265       --  Unchecked conversions are allowed only if they come from the
7266       --  generated code, which sometimes uses unchecked conversions for out
7267       --  parameters in cases where code generation is unaffected. We tell
7268       --  source unchecked conversions by seeing if they are rewrites of an
7269       --  original Unchecked_Conversion function call, or of an explicit
7270       --  conversion of a function call.
7271
7272       elsif Nkind (AV) = N_Unchecked_Type_Conversion then
7273          if Nkind (Original_Node (AV)) = N_Function_Call then
7274             return False;
7275
7276          elsif Comes_From_Source (AV)
7277            and then Nkind (Original_Node (Expression (AV))) = N_Function_Call
7278          then
7279             return False;
7280
7281          elsif Nkind (Original_Node (AV)) = N_Type_Conversion then
7282             return Is_OK_Variable_For_Out_Formal (Expression (AV));
7283
7284          else
7285             return True;
7286          end if;
7287
7288       --  Normal type conversions are allowed if argument is a variable
7289
7290       elsif Nkind (AV) = N_Type_Conversion then
7291          if Is_Variable (Expression (AV))
7292            and then Paren_Count (Expression (AV)) = 0
7293          then
7294             Note_Possible_Modification (Expression (AV), Sure => True);
7295             return True;
7296
7297          --  We also allow a non-parenthesized expression that raises
7298          --  constraint error if it rewrites what used to be a variable
7299
7300          elsif Raises_Constraint_Error (Expression (AV))
7301             and then Paren_Count (Expression (AV)) = 0
7302             and then Is_Variable (Original_Node (Expression (AV)))
7303          then
7304             return True;
7305
7306          --  Type conversion of something other than a variable
7307
7308          else
7309             return False;
7310          end if;
7311
7312       --  If this node is rewritten, then test the original form, if that is
7313       --  OK, then we consider the rewritten node OK (for example, if the
7314       --  original node is a conversion, then Is_Variable will not be true
7315       --  but we still want to allow the conversion if it converts a variable).
7316
7317       elsif Original_Node (AV) /= AV then
7318          return Is_OK_Variable_For_Out_Formal (Original_Node (AV));
7319
7320       --  All other non-variables are rejected
7321
7322       else
7323          return False;
7324       end if;
7325    end Is_OK_Variable_For_Out_Formal;
7326
7327    -----------------------------------
7328    -- Is_Partially_Initialized_Type --
7329    -----------------------------------
7330
7331    function Is_Partially_Initialized_Type
7332      (Typ              : Entity_Id;
7333       Include_Implicit : Boolean := True) return Boolean
7334    is
7335    begin
7336       if Is_Scalar_Type (Typ) then
7337          return False;
7338
7339       elsif Is_Access_Type (Typ) then
7340          return Include_Implicit;
7341
7342       elsif Is_Array_Type (Typ) then
7343
7344          --  If component type is partially initialized, so is array type
7345
7346          if Is_Partially_Initialized_Type
7347               (Component_Type (Typ), Include_Implicit)
7348          then
7349             return True;
7350
7351          --  Otherwise we are only partially initialized if we are fully
7352          --  initialized (this is the empty array case, no point in us
7353          --  duplicating that code here).
7354
7355          else
7356             return Is_Fully_Initialized_Type (Typ);
7357          end if;
7358
7359       elsif Is_Record_Type (Typ) then
7360
7361          --  A discriminated type is always partially initialized if in
7362          --  all mode
7363
7364          if Has_Discriminants (Typ) and then Include_Implicit then
7365             return True;
7366
7367          --  A tagged type is always partially initialized
7368
7369          elsif Is_Tagged_Type (Typ) then
7370             return True;
7371
7372          --  Case of non-discriminated record
7373
7374          else
7375             declare
7376                Ent : Entity_Id;
7377
7378                Component_Present : Boolean := False;
7379                --  Set True if at least one component is present. If no
7380                --  components are present, then record type is fully
7381                --  initialized (another odd case, like the null array).
7382
7383             begin
7384                --  Loop through components
7385
7386                Ent := First_Entity (Typ);
7387                while Present (Ent) loop
7388                   if Ekind (Ent) = E_Component then
7389                      Component_Present := True;
7390
7391                      --  If a component has an initialization expression then
7392                      --  the enclosing record type is partially initialized
7393
7394                      if Present (Parent (Ent))
7395                        and then Present (Expression (Parent (Ent)))
7396                      then
7397                         return True;
7398
7399                      --  If a component is of a type which is itself partially
7400                      --  initialized, then the enclosing record type is also.
7401
7402                      elsif Is_Partially_Initialized_Type
7403                              (Etype (Ent), Include_Implicit)
7404                      then
7405                         return True;
7406                      end if;
7407                   end if;
7408
7409                   Next_Entity (Ent);
7410                end loop;
7411
7412                --  No initialized components found. If we found any components
7413                --  they were all uninitialized so the result is false.
7414
7415                if Component_Present then
7416                   return False;
7417
7418                --  But if we found no components, then all the components are
7419                --  initialized so we consider the type to be initialized.
7420
7421                else
7422                   return True;
7423                end if;
7424             end;
7425          end if;
7426
7427       --  Concurrent types are always fully initialized
7428
7429       elsif Is_Concurrent_Type (Typ) then
7430          return True;
7431
7432       --  For a private type, go to underlying type. If there is no underlying
7433       --  type then just assume this partially initialized. Not clear if this
7434       --  can happen in a non-error case, but no harm in testing for this.
7435
7436       elsif Is_Private_Type (Typ) then
7437          declare
7438             U : constant Entity_Id := Underlying_Type (Typ);
7439          begin
7440             if No (U) then
7441                return True;
7442             else
7443                return Is_Partially_Initialized_Type (U, Include_Implicit);
7444             end if;
7445          end;
7446
7447       --  For any other type (are there any?) assume partially initialized
7448
7449       else
7450          return True;
7451       end if;
7452    end Is_Partially_Initialized_Type;
7453
7454    ------------------------------------
7455    -- Is_Potentially_Persistent_Type --
7456    ------------------------------------
7457
7458    function Is_Potentially_Persistent_Type (T : Entity_Id) return Boolean is
7459       Comp : Entity_Id;
7460       Indx : Node_Id;
7461
7462    begin
7463       --  For private type, test corresponding full type
7464
7465       if Is_Private_Type (T) then
7466          return Is_Potentially_Persistent_Type (Full_View (T));
7467
7468       --  Scalar types are potentially persistent
7469
7470       elsif Is_Scalar_Type (T) then
7471          return True;
7472
7473       --  Record type is potentially persistent if not tagged and the types of
7474       --  all it components are potentially persistent, and no component has
7475       --  an initialization expression.
7476
7477       elsif Is_Record_Type (T)
7478         and then not Is_Tagged_Type (T)
7479         and then not Is_Partially_Initialized_Type (T)
7480       then
7481          Comp := First_Component (T);
7482          while Present (Comp) loop
7483             if not Is_Potentially_Persistent_Type (Etype (Comp)) then
7484                return False;
7485             else
7486                Next_Entity (Comp);
7487             end if;
7488          end loop;
7489
7490          return True;
7491
7492       --  Array type is potentially persistent if its component type is
7493       --  potentially persistent and if all its constraints are static.
7494
7495       elsif Is_Array_Type (T) then
7496          if not Is_Potentially_Persistent_Type (Component_Type (T)) then
7497             return False;
7498          end if;
7499
7500          Indx := First_Index (T);
7501          while Present (Indx) loop
7502             if not Is_OK_Static_Subtype (Etype (Indx)) then
7503                return False;
7504             else
7505                Next_Index (Indx);
7506             end if;
7507          end loop;
7508
7509          return True;
7510
7511       --  All other types are not potentially persistent
7512
7513       else
7514          return False;
7515       end if;
7516    end Is_Potentially_Persistent_Type;
7517
7518    ---------------------------------
7519    -- Is_Protected_Self_Reference --
7520    ---------------------------------
7521
7522    function Is_Protected_Self_Reference (N : Node_Id) return Boolean is
7523
7524       function In_Access_Definition (N : Node_Id) return Boolean;
7525       --  Returns true if N belongs to an access definition
7526
7527       --------------------------
7528       -- In_Access_Definition --
7529       --------------------------
7530
7531       function In_Access_Definition (N : Node_Id) return Boolean is
7532          P : Node_Id;
7533
7534       begin
7535          P := Parent (N);
7536          while Present (P) loop
7537             if Nkind (P) = N_Access_Definition then
7538                return True;
7539             end if;
7540
7541             P := Parent (P);
7542          end loop;
7543
7544          return False;
7545       end In_Access_Definition;
7546
7547    --  Start of processing for Is_Protected_Self_Reference
7548
7549    begin
7550       --  Verify that prefix is analyzed and has the proper form. Note that
7551       --  the attributes Elab_Spec, Elab_Body, and UET_Address, which also
7552       --  produce the address of an entity, do not analyze their prefix
7553       --  because they denote entities that are not necessarily visible.
7554       --  Neither of them can apply to a protected type.
7555
7556       return Ada_Version >= Ada_2005
7557         and then Is_Entity_Name (N)
7558         and then Present (Entity (N))
7559         and then Is_Protected_Type (Entity (N))
7560         and then In_Open_Scopes (Entity (N))
7561         and then not In_Access_Definition (N);
7562    end Is_Protected_Self_Reference;
7563
7564    -----------------------------
7565    -- Is_RCI_Pkg_Spec_Or_Body --
7566    -----------------------------
7567
7568    function Is_RCI_Pkg_Spec_Or_Body (Cunit : Node_Id) return Boolean is
7569
7570       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean;
7571       --  Return True if the unit of Cunit is an RCI package declaration
7572
7573       ---------------------------
7574       -- Is_RCI_Pkg_Decl_Cunit --
7575       ---------------------------
7576
7577       function Is_RCI_Pkg_Decl_Cunit (Cunit : Node_Id) return Boolean is
7578          The_Unit : constant Node_Id := Unit (Cunit);
7579
7580       begin
7581          if Nkind (The_Unit) /= N_Package_Declaration then
7582             return False;
7583          end if;
7584
7585          return Is_Remote_Call_Interface (Defining_Entity (The_Unit));
7586       end Is_RCI_Pkg_Decl_Cunit;
7587
7588    --  Start of processing for Is_RCI_Pkg_Spec_Or_Body
7589
7590    begin
7591       return Is_RCI_Pkg_Decl_Cunit (Cunit)
7592         or else
7593          (Nkind (Unit (Cunit)) = N_Package_Body
7594            and then Is_RCI_Pkg_Decl_Cunit (Library_Unit (Cunit)));
7595    end Is_RCI_Pkg_Spec_Or_Body;
7596
7597    -----------------------------------------
7598    -- Is_Remote_Access_To_Class_Wide_Type --
7599    -----------------------------------------
7600
7601    function Is_Remote_Access_To_Class_Wide_Type
7602      (E : Entity_Id) return Boolean
7603    is
7604    begin
7605       --  A remote access to class-wide type is a general access to object type
7606       --  declared in the visible part of a Remote_Types or Remote_Call_
7607       --  Interface unit.
7608
7609       return Ekind (E) = E_General_Access_Type
7610         and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
7611    end Is_Remote_Access_To_Class_Wide_Type;
7612
7613    -----------------------------------------
7614    -- Is_Remote_Access_To_Subprogram_Type --
7615    -----------------------------------------
7616
7617    function Is_Remote_Access_To_Subprogram_Type
7618      (E : Entity_Id) return Boolean
7619    is
7620    begin
7621       return (Ekind (E) = E_Access_Subprogram_Type
7622                 or else (Ekind (E) = E_Record_Type
7623                            and then Present (Corresponding_Remote_Type (E))))
7624         and then (Is_Remote_Call_Interface (E) or else Is_Remote_Types (E));
7625    end Is_Remote_Access_To_Subprogram_Type;
7626
7627    --------------------
7628    -- Is_Remote_Call --
7629    --------------------
7630
7631    function Is_Remote_Call (N : Node_Id) return Boolean is
7632    begin
7633       if Nkind (N) /= N_Procedure_Call_Statement
7634         and then Nkind (N) /= N_Function_Call
7635       then
7636          --  An entry call cannot be remote
7637
7638          return False;
7639
7640       elsif Nkind (Name (N)) in N_Has_Entity
7641         and then Is_Remote_Call_Interface (Entity (Name (N)))
7642       then
7643          --  A subprogram declared in the spec of a RCI package is remote
7644
7645          return True;
7646
7647       elsif Nkind (Name (N)) = N_Explicit_Dereference
7648         and then Is_Remote_Access_To_Subprogram_Type
7649                    (Etype (Prefix (Name (N))))
7650       then
7651          --  The dereference of a RAS is a remote call
7652
7653          return True;
7654
7655       elsif Present (Controlling_Argument (N))
7656         and then Is_Remote_Access_To_Class_Wide_Type
7657           (Etype (Controlling_Argument (N)))
7658       then
7659          --  Any primitive operation call with a controlling argument of
7660          --  a RACW type is a remote call.
7661
7662          return True;
7663       end if;
7664
7665       --  All other calls are local calls
7666
7667       return False;
7668    end Is_Remote_Call;
7669
7670    ----------------------
7671    -- Is_Renamed_Entry --
7672    ----------------------
7673
7674    function Is_Renamed_Entry (Proc_Nam : Entity_Id) return Boolean is
7675       Orig_Node : Node_Id := Empty;
7676       Subp_Decl : Node_Id := Parent (Parent (Proc_Nam));
7677
7678       function Is_Entry (Nam : Node_Id) return Boolean;
7679       --  Determine whether Nam is an entry. Traverse selectors if there are
7680       --  nested selected components.
7681
7682       --------------
7683       -- Is_Entry --
7684       --------------
7685
7686       function Is_Entry (Nam : Node_Id) return Boolean is
7687       begin
7688          if Nkind (Nam) = N_Selected_Component then
7689             return Is_Entry (Selector_Name (Nam));
7690          end if;
7691
7692          return Ekind (Entity (Nam)) = E_Entry;
7693       end Is_Entry;
7694
7695    --  Start of processing for Is_Renamed_Entry
7696
7697    begin
7698       if Present (Alias (Proc_Nam)) then
7699          Subp_Decl := Parent (Parent (Alias (Proc_Nam)));
7700       end if;
7701
7702       --  Look for a rewritten subprogram renaming declaration
7703
7704       if Nkind (Subp_Decl) = N_Subprogram_Declaration
7705         and then Present (Original_Node (Subp_Decl))
7706       then
7707          Orig_Node := Original_Node (Subp_Decl);
7708       end if;
7709
7710       --  The rewritten subprogram is actually an entry
7711
7712       if Present (Orig_Node)
7713         and then Nkind (Orig_Node) = N_Subprogram_Renaming_Declaration
7714         and then Is_Entry (Name (Orig_Node))
7715       then
7716          return True;
7717       end if;
7718
7719       return False;
7720    end Is_Renamed_Entry;
7721
7722    ----------------------
7723    -- Is_Selector_Name --
7724    ----------------------
7725
7726    function Is_Selector_Name (N : Node_Id) return Boolean is
7727    begin
7728       if not Is_List_Member (N) then
7729          declare
7730             P : constant Node_Id   := Parent (N);
7731             K : constant Node_Kind := Nkind (P);
7732          begin
7733             return
7734               (K = N_Expanded_Name          or else
7735                K = N_Generic_Association    or else
7736                K = N_Parameter_Association  or else
7737                K = N_Selected_Component)
7738               and then Selector_Name (P) = N;
7739          end;
7740
7741       else
7742          declare
7743             L : constant List_Id := List_Containing (N);
7744             P : constant Node_Id := Parent (L);
7745          begin
7746             return (Nkind (P) = N_Discriminant_Association
7747                      and then Selector_Names (P) = L)
7748               or else
7749                    (Nkind (P) = N_Component_Association
7750                      and then Choices (P) = L);
7751          end;
7752       end if;
7753    end Is_Selector_Name;
7754
7755    ----------------------------------
7756    -- Is_SPARK_Initialization_Expr --
7757    ----------------------------------
7758
7759    function Is_SPARK_Initialization_Expr (N : Node_Id) return Boolean is
7760       Is_Ok     : Boolean;
7761       Expr      : Node_Id;
7762       Comp_Assn : Node_Id;
7763       Orig_N    : constant Node_Id := Original_Node (N);
7764
7765    begin
7766       Is_Ok := True;
7767
7768       if not Comes_From_Source (Orig_N) then
7769          goto Done;
7770       end if;
7771
7772       pragma Assert (Nkind (Orig_N) in N_Subexpr);
7773
7774       case Nkind (Orig_N) is
7775          when N_Character_Literal |
7776               N_Integer_Literal   |
7777               N_Real_Literal      |
7778               N_String_Literal    =>
7779             null;
7780
7781          when N_Identifier    |
7782               N_Expanded_Name =>
7783             if Is_Entity_Name (Orig_N)
7784               and then Present (Entity (Orig_N))  --  needed in some cases
7785             then
7786                case Ekind (Entity (Orig_N)) is
7787                   when E_Constant            |
7788                        E_Enumeration_Literal |
7789                        E_Named_Integer       |
7790                        E_Named_Real          =>
7791                      null;
7792                   when others =>
7793                      if Is_Type (Entity (Orig_N)) then
7794                         null;
7795                      else
7796                         Is_Ok := False;
7797                      end if;
7798                end case;
7799             end if;
7800
7801          when N_Qualified_Expression |
7802               N_Type_Conversion      =>
7803             Is_Ok := Is_SPARK_Initialization_Expr (Expression (Orig_N));
7804
7805          when N_Unary_Op =>
7806             Is_Ok := Is_SPARK_Initialization_Expr (Right_Opnd (Orig_N));
7807
7808          when N_Binary_Op       |
7809               N_Short_Circuit   |
7810               N_Membership_Test =>
7811             Is_Ok := Is_SPARK_Initialization_Expr (Left_Opnd (Orig_N))
7812               and then Is_SPARK_Initialization_Expr (Right_Opnd (Orig_N));
7813
7814          when N_Aggregate           |
7815               N_Extension_Aggregate =>
7816             if Nkind (Orig_N) = N_Extension_Aggregate then
7817                Is_Ok := Is_SPARK_Initialization_Expr (Ancestor_Part (Orig_N));
7818             end if;
7819
7820             Expr := First (Expressions (Orig_N));
7821             while Present (Expr) loop
7822                if not Is_SPARK_Initialization_Expr (Expr) then
7823                   Is_Ok := False;
7824                   goto Done;
7825                end if;
7826
7827                Next (Expr);
7828             end loop;
7829
7830             Comp_Assn := First (Component_Associations (Orig_N));
7831             while Present (Comp_Assn) loop
7832                Expr := Expression (Comp_Assn);
7833                if Present (Expr)  --  needed for box association
7834                  and then not Is_SPARK_Initialization_Expr (Expr)
7835                then
7836                   Is_Ok := False;
7837                   goto Done;
7838                end if;
7839
7840                Next (Comp_Assn);
7841             end loop;
7842
7843          when N_Attribute_Reference =>
7844             if Nkind (Prefix (Orig_N)) in N_Subexpr then
7845                Is_Ok := Is_SPARK_Initialization_Expr (Prefix (Orig_N));
7846             end if;
7847
7848             Expr := First (Expressions (Orig_N));
7849             while Present (Expr) loop
7850                if not Is_SPARK_Initialization_Expr (Expr) then
7851                   Is_Ok := False;
7852                   goto Done;
7853                end if;
7854
7855                Next (Expr);
7856             end loop;
7857
7858          --  Selected components might be expanded named not yet resolved, so
7859          --  default on the safe side. (Eg on sparklex.ads)
7860
7861          when N_Selected_Component =>
7862             null;
7863
7864          when others =>
7865             Is_Ok := False;
7866       end case;
7867
7868    <<Done>>
7869       return Is_Ok;
7870    end Is_SPARK_Initialization_Expr;
7871
7872    -------------------------------
7873    -- Is_SPARK_Object_Reference --
7874    -------------------------------
7875
7876    function Is_SPARK_Object_Reference (N : Node_Id) return Boolean is
7877    begin
7878       if Is_Entity_Name (N) then
7879          return Present (Entity (N))
7880            and then
7881              (Ekind_In (Entity (N), E_Constant, E_Variable)
7882               or else Ekind (Entity (N)) in Formal_Kind);
7883
7884       else
7885          case Nkind (N) is
7886             when N_Selected_Component =>
7887                return Is_SPARK_Object_Reference (Prefix (N));
7888
7889             when others =>
7890                return False;
7891          end case;
7892       end if;
7893    end Is_SPARK_Object_Reference;
7894
7895    ------------------
7896    -- Is_Statement --
7897    ------------------
7898
7899    function Is_Statement (N : Node_Id) return Boolean is
7900    begin
7901       return
7902         Nkind (N) in N_Statement_Other_Than_Procedure_Call
7903           or else Nkind (N) = N_Procedure_Call_Statement;
7904    end Is_Statement;
7905
7906    ---------------------------------
7907    -- Is_Synchronized_Tagged_Type --
7908    ---------------------------------
7909
7910    function Is_Synchronized_Tagged_Type (E : Entity_Id) return Boolean is
7911       Kind : constant Entity_Kind := Ekind (Base_Type (E));
7912
7913    begin
7914       --  A task or protected type derived from an interface is a tagged type.
7915       --  Such a tagged type is called a synchronized tagged type, as are
7916       --  synchronized interfaces and private extensions whose declaration
7917       --  includes the reserved word synchronized.
7918
7919       return (Is_Tagged_Type (E)
7920                 and then (Kind = E_Task_Type
7921                            or else Kind = E_Protected_Type))
7922             or else
7923              (Is_Interface (E)
7924                 and then Is_Synchronized_Interface (E))
7925             or else
7926              (Ekind (E) = E_Record_Type_With_Private
7927                 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
7928                 and then (Synchronized_Present (Parent (E))
7929                            or else Is_Synchronized_Interface (Etype (E))));
7930    end Is_Synchronized_Tagged_Type;
7931
7932    -----------------
7933    -- Is_Transfer --
7934    -----------------
7935
7936    function Is_Transfer (N : Node_Id) return Boolean is
7937       Kind : constant Node_Kind := Nkind (N);
7938
7939    begin
7940       if Kind = N_Simple_Return_Statement
7941            or else
7942          Kind = N_Extended_Return_Statement
7943            or else
7944          Kind = N_Goto_Statement
7945            or else
7946          Kind = N_Raise_Statement
7947            or else
7948          Kind = N_Requeue_Statement
7949       then
7950          return True;
7951
7952       elsif (Kind = N_Exit_Statement or else Kind in N_Raise_xxx_Error)
7953         and then No (Condition (N))
7954       then
7955          return True;
7956
7957       elsif Kind = N_Procedure_Call_Statement
7958         and then Is_Entity_Name (Name (N))
7959         and then Present (Entity (Name (N)))
7960         and then No_Return (Entity (Name (N)))
7961       then
7962          return True;
7963
7964       elsif Nkind (Original_Node (N)) = N_Raise_Statement then
7965          return True;
7966
7967       else
7968          return False;
7969       end if;
7970    end Is_Transfer;
7971
7972    -------------
7973    -- Is_True --
7974    -------------
7975
7976    function Is_True (U : Uint) return Boolean is
7977    begin
7978       return (U /= 0);
7979    end Is_True;
7980
7981    -------------------------------
7982    -- Is_Universal_Numeric_Type --
7983    -------------------------------
7984
7985    function Is_Universal_Numeric_Type (T : Entity_Id) return Boolean is
7986    begin
7987       return T = Universal_Integer or else T = Universal_Real;
7988    end Is_Universal_Numeric_Type;
7989
7990    -------------------
7991    -- Is_Value_Type --
7992    -------------------
7993
7994    function Is_Value_Type (T : Entity_Id) return Boolean is
7995    begin
7996       return VM_Target = CLI_Target
7997         and then Nkind (T) in N_Has_Chars
7998         and then Chars (T) /= No_Name
7999         and then Get_Name_String (Chars (T)) = "valuetype";
8000    end Is_Value_Type;
8001
8002    ---------------------
8003    -- Is_VMS_Operator --
8004    ---------------------
8005
8006    function Is_VMS_Operator (Op : Entity_Id) return Boolean is
8007    begin
8008       --  The VMS operators are declared in a child of System that is loaded
8009       --  through pragma Extend_System. In some rare cases a program is run
8010       --  with this extension but without indicating that the target is VMS.
8011
8012       return Ekind (Op) = E_Function
8013         and then Is_Intrinsic_Subprogram (Op)
8014         and then
8015           ((Present_System_Aux
8016             and then Scope (Op) = System_Aux_Id)
8017            or else
8018            (True_VMS_Target
8019              and then Scope (Scope (Op)) = RTU_Entity (System)));
8020    end Is_VMS_Operator;
8021
8022    -----------------
8023    -- Is_Variable --
8024    -----------------
8025
8026    function Is_Variable
8027      (N                 : Node_Id;
8028       Use_Original_Node : Boolean := True) return Boolean
8029    is
8030       Orig_Node : Node_Id;
8031
8032       function In_Protected_Function (E : Entity_Id) return Boolean;
8033       --  Within a protected function, the private components of the enclosing
8034       --  protected type are constants. A function nested within a (protected)
8035       --  procedure is not itself protected.
8036
8037       function Is_Variable_Prefix (P : Node_Id) return Boolean;
8038       --  Prefixes can involve implicit dereferences, in which case we must
8039       --  test for the case of a reference of a constant access type, which can
8040       --  can never be a variable.
8041
8042       ---------------------------
8043       -- In_Protected_Function --
8044       ---------------------------
8045
8046       function In_Protected_Function (E : Entity_Id) return Boolean is
8047          Prot : constant Entity_Id := Scope (E);
8048          S    : Entity_Id;
8049
8050       begin
8051          if not Is_Protected_Type (Prot) then
8052             return False;
8053          else
8054             S := Current_Scope;
8055             while Present (S) and then S /= Prot loop
8056                if Ekind (S) = E_Function and then Scope (S) = Prot then
8057                   return True;
8058                end if;
8059
8060                S := Scope (S);
8061             end loop;
8062
8063             return False;
8064          end if;
8065       end In_Protected_Function;
8066
8067       ------------------------
8068       -- Is_Variable_Prefix --
8069       ------------------------
8070
8071       function Is_Variable_Prefix (P : Node_Id) return Boolean is
8072       begin
8073          if Is_Access_Type (Etype (P)) then
8074             return not Is_Access_Constant (Root_Type (Etype (P)));
8075
8076          --  For the case of an indexed component whose prefix has a packed
8077          --  array type, the prefix has been rewritten into a type conversion.
8078          --  Determine variable-ness from the converted expression.
8079
8080          elsif Nkind (P) = N_Type_Conversion
8081            and then not Comes_From_Source (P)
8082            and then Is_Array_Type (Etype (P))
8083            and then Is_Packed (Etype (P))
8084          then
8085             return Is_Variable (Expression (P));
8086
8087          else
8088             return Is_Variable (P);
8089          end if;
8090       end Is_Variable_Prefix;
8091
8092    --  Start of processing for Is_Variable
8093
8094    begin
8095       --  Check if we perform the test on the original node since this may be a
8096       --  test of syntactic categories which must not be disturbed by whatever
8097       --  rewriting might have occurred. For example, an aggregate, which is
8098       --  certainly NOT a variable, could be turned into a variable by
8099       --  expansion.
8100
8101       if Use_Original_Node then
8102          Orig_Node := Original_Node (N);
8103       else
8104          Orig_Node := N;
8105       end if;
8106
8107       --  Definitely OK if Assignment_OK is set. Since this is something that
8108       --  only gets set for expanded nodes, the test is on N, not Orig_Node.
8109
8110       if Nkind (N) in N_Subexpr and then Assignment_OK (N) then
8111          return True;
8112
8113       --  Normally we go to the original node, but there is one exception where
8114       --  we use the rewritten node, namely when it is an explicit dereference.
8115       --  The generated code may rewrite a prefix which is an access type with
8116       --  an explicit dereference. The dereference is a variable, even though
8117       --  the original node may not be (since it could be a constant of the
8118       --  access type).
8119
8120       --  In Ada 2005 we have a further case to consider: the prefix may be a
8121       --  function call given in prefix notation. The original node appears to
8122       --  be a selected component, but we need to examine the call.
8123
8124       elsif Nkind (N) = N_Explicit_Dereference
8125         and then Nkind (Orig_Node) /= N_Explicit_Dereference
8126         and then Present (Etype (Orig_Node))
8127         and then Is_Access_Type (Etype (Orig_Node))
8128       then
8129          --  Note that if the prefix is an explicit dereference that does not
8130          --  come from source, we must check for a rewritten function call in
8131          --  prefixed notation before other forms of rewriting, to prevent a
8132          --  compiler crash.
8133
8134          return
8135            (Nkind (Orig_Node) = N_Function_Call
8136              and then not Is_Access_Constant (Etype (Prefix (N))))
8137            or else
8138              Is_Variable_Prefix (Original_Node (Prefix (N)));
8139
8140       --  A function call is never a variable
8141
8142       elsif Nkind (N) = N_Function_Call then
8143          return False;
8144
8145       --  All remaining checks use the original node
8146
8147       elsif Is_Entity_Name (Orig_Node)
8148         and then Present (Entity (Orig_Node))
8149       then
8150          declare
8151             E : constant Entity_Id := Entity (Orig_Node);
8152             K : constant Entity_Kind := Ekind (E);
8153
8154          begin
8155             return (K = E_Variable
8156                       and then Nkind (Parent (E)) /= N_Exception_Handler)
8157               or else  (K = E_Component
8158                           and then not In_Protected_Function (E))
8159               or else  K = E_Out_Parameter
8160               or else  K = E_In_Out_Parameter
8161               or else  K = E_Generic_In_Out_Parameter
8162
8163                --  Current instance of type:
8164
8165               or else (Is_Type (E) and then In_Open_Scopes (E))
8166               or else (Is_Incomplete_Or_Private_Type (E)
8167                         and then In_Open_Scopes (Full_View (E)));
8168          end;
8169
8170       else
8171          case Nkind (Orig_Node) is
8172             when N_Indexed_Component | N_Slice =>
8173                return Is_Variable_Prefix (Prefix (Orig_Node));
8174
8175             when N_Selected_Component =>
8176                return Is_Variable_Prefix (Prefix (Orig_Node))
8177                  and then Is_Variable (Selector_Name (Orig_Node));
8178
8179             --  For an explicit dereference, the type of the prefix cannot
8180             --  be an access to constant or an access to subprogram.
8181
8182             when N_Explicit_Dereference =>
8183                declare
8184                   Typ : constant Entity_Id := Etype (Prefix (Orig_Node));
8185                begin
8186                   return Is_Access_Type (Typ)
8187                     and then not Is_Access_Constant (Root_Type (Typ))
8188                     and then Ekind (Typ) /= E_Access_Subprogram_Type;
8189                end;
8190
8191             --  The type conversion is the case where we do not deal with the
8192             --  context dependent special case of an actual parameter. Thus
8193             --  the type conversion is only considered a variable for the
8194             --  purposes of this routine if the target type is tagged. However,
8195             --  a type conversion is considered to be a variable if it does not
8196             --  come from source (this deals for example with the conversions
8197             --  of expressions to their actual subtypes).
8198
8199             when N_Type_Conversion =>
8200                return Is_Variable (Expression (Orig_Node))
8201                  and then
8202                    (not Comes_From_Source (Orig_Node)
8203                       or else
8204                         (Is_Tagged_Type (Etype (Subtype_Mark (Orig_Node)))
8205                           and then
8206                          Is_Tagged_Type (Etype (Expression (Orig_Node)))));
8207
8208             --  GNAT allows an unchecked type conversion as a variable. This
8209             --  only affects the generation of internal expanded code, since
8210             --  calls to instantiations of Unchecked_Conversion are never
8211             --  considered variables (since they are function calls).
8212             --  This is also true for expression actions.
8213
8214             when N_Unchecked_Type_Conversion =>
8215                return Is_Variable (Expression (Orig_Node));
8216
8217             when others =>
8218                return False;
8219          end case;
8220       end if;
8221    end Is_Variable;
8222
8223    ---------------------------
8224    -- Is_Visibly_Controlled --
8225    ---------------------------
8226
8227    function Is_Visibly_Controlled (T : Entity_Id) return Boolean is
8228       Root : constant Entity_Id := Root_Type (T);
8229    begin
8230       return Chars (Scope (Root)) = Name_Finalization
8231         and then Chars (Scope (Scope (Root))) = Name_Ada
8232         and then Scope (Scope (Scope (Root))) = Standard_Standard;
8233    end Is_Visibly_Controlled;
8234
8235    ------------------------
8236    -- Is_Volatile_Object --
8237    ------------------------
8238
8239    function Is_Volatile_Object (N : Node_Id) return Boolean is
8240
8241       function Object_Has_Volatile_Components (N : Node_Id) return Boolean;
8242       --  Determines if given object has volatile components
8243
8244       function Is_Volatile_Prefix (N : Node_Id) return Boolean;
8245       --  If prefix is an implicit dereference, examine designated type
8246
8247       ------------------------
8248       -- Is_Volatile_Prefix --
8249       ------------------------
8250
8251       function Is_Volatile_Prefix (N : Node_Id) return Boolean is
8252          Typ  : constant Entity_Id := Etype (N);
8253
8254       begin
8255          if Is_Access_Type (Typ) then
8256             declare
8257                Dtyp : constant Entity_Id := Designated_Type (Typ);
8258
8259             begin
8260                return Is_Volatile (Dtyp)
8261                  or else Has_Volatile_Components (Dtyp);
8262             end;
8263
8264          else
8265             return Object_Has_Volatile_Components (N);
8266          end if;
8267       end Is_Volatile_Prefix;
8268
8269       ------------------------------------
8270       -- Object_Has_Volatile_Components --
8271       ------------------------------------
8272
8273       function Object_Has_Volatile_Components (N : Node_Id) return Boolean is
8274          Typ : constant Entity_Id := Etype (N);
8275
8276       begin
8277          if Is_Volatile (Typ)
8278            or else Has_Volatile_Components (Typ)
8279          then
8280             return True;
8281
8282          elsif Is_Entity_Name (N)
8283            and then (Has_Volatile_Components (Entity (N))
8284                       or else Is_Volatile (Entity (N)))
8285          then
8286             return True;
8287
8288          elsif Nkind (N) = N_Indexed_Component
8289            or else Nkind (N) = N_Selected_Component
8290          then
8291             return Is_Volatile_Prefix (Prefix (N));
8292
8293          else
8294             return False;
8295          end if;
8296       end Object_Has_Volatile_Components;
8297
8298    --  Start of processing for Is_Volatile_Object
8299
8300    begin
8301       if Is_Volatile (Etype (N))
8302         or else (Is_Entity_Name (N) and then Is_Volatile (Entity (N)))
8303       then
8304          return True;
8305
8306       elsif Nkind (N) = N_Indexed_Component
8307         or else Nkind (N) = N_Selected_Component
8308       then
8309          return Is_Volatile_Prefix (Prefix (N));
8310
8311       else
8312          return False;
8313       end if;
8314    end Is_Volatile_Object;
8315
8316    -------------------------
8317    -- Kill_Current_Values --
8318    -------------------------
8319
8320    procedure Kill_Current_Values
8321      (Ent                  : Entity_Id;
8322       Last_Assignment_Only : Boolean := False)
8323    is
8324    begin
8325       --  ??? do we have to worry about clearing cached checks?
8326
8327       if Is_Assignable (Ent) then
8328          Set_Last_Assignment (Ent, Empty);
8329       end if;
8330
8331       if Is_Object (Ent) then
8332          if not Last_Assignment_Only then
8333             Kill_Checks (Ent);
8334             Set_Current_Value (Ent, Empty);
8335
8336             if not Can_Never_Be_Null (Ent) then
8337                Set_Is_Known_Non_Null (Ent, False);
8338             end if;
8339
8340             Set_Is_Known_Null (Ent, False);
8341
8342             --  Reset Is_Known_Valid unless type is always valid, or if we have
8343             --  a loop parameter (loop parameters are always valid, since their
8344             --  bounds are defined by the bounds given in the loop header).
8345
8346             if not Is_Known_Valid (Etype (Ent))
8347               and then Ekind (Ent) /= E_Loop_Parameter
8348             then
8349                Set_Is_Known_Valid (Ent, False);
8350             end if;
8351          end if;
8352       end if;
8353    end Kill_Current_Values;
8354
8355    procedure Kill_Current_Values (Last_Assignment_Only : Boolean := False) is
8356       S : Entity_Id;
8357
8358       procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id);
8359       --  Clear current value for entity E and all entities chained to E
8360
8361       ------------------------------------------
8362       -- Kill_Current_Values_For_Entity_Chain --
8363       ------------------------------------------
8364
8365       procedure Kill_Current_Values_For_Entity_Chain (E : Entity_Id) is
8366          Ent : Entity_Id;
8367       begin
8368          Ent := E;
8369          while Present (Ent) loop
8370             Kill_Current_Values (Ent, Last_Assignment_Only);
8371             Next_Entity (Ent);
8372          end loop;
8373       end Kill_Current_Values_For_Entity_Chain;
8374
8375    --  Start of processing for Kill_Current_Values
8376
8377    begin
8378       --  Kill all saved checks, a special case of killing saved values
8379
8380       if not Last_Assignment_Only then
8381          Kill_All_Checks;
8382       end if;
8383
8384       --  Loop through relevant scopes, which includes the current scope and
8385       --  any parent scopes if the current scope is a block or a package.
8386
8387       S := Current_Scope;
8388       Scope_Loop : loop
8389
8390          --  Clear current values of all entities in current scope
8391
8392          Kill_Current_Values_For_Entity_Chain (First_Entity (S));
8393
8394          --  If scope is a package, also clear current values of all
8395          --  private entities in the scope.
8396
8397          if Is_Package_Or_Generic_Package (S)
8398            or else Is_Concurrent_Type (S)
8399          then
8400             Kill_Current_Values_For_Entity_Chain (First_Private_Entity (S));
8401          end if;
8402
8403          --  If this is a not a subprogram, deal with parents
8404
8405          if not Is_Subprogram (S) then
8406             S := Scope (S);
8407             exit Scope_Loop when S = Standard_Standard;
8408          else
8409             exit Scope_Loop;
8410          end if;
8411       end loop Scope_Loop;
8412    end Kill_Current_Values;
8413
8414    --------------------------
8415    -- Kill_Size_Check_Code --
8416    --------------------------
8417
8418    procedure Kill_Size_Check_Code (E : Entity_Id) is
8419    begin
8420       if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
8421         and then Present (Size_Check_Code (E))
8422       then
8423          Remove (Size_Check_Code (E));
8424          Set_Size_Check_Code (E, Empty);
8425       end if;
8426    end Kill_Size_Check_Code;
8427
8428    --------------------------
8429    -- Known_To_Be_Assigned --
8430    --------------------------
8431
8432    function Known_To_Be_Assigned (N : Node_Id) return Boolean is
8433       P : constant Node_Id := Parent (N);
8434
8435    begin
8436       case Nkind (P) is
8437
8438          --  Test left side of assignment
8439
8440          when N_Assignment_Statement =>
8441             return N = Name (P);
8442
8443             --  Function call arguments are never lvalues
8444
8445          when N_Function_Call =>
8446             return False;
8447
8448          --  Positional parameter for procedure or accept call
8449
8450          when N_Procedure_Call_Statement |
8451               N_Accept_Statement
8452           =>
8453             declare
8454                Proc : Entity_Id;
8455                Form : Entity_Id;
8456                Act  : Node_Id;
8457
8458             begin
8459                Proc := Get_Subprogram_Entity (P);
8460
8461                if No (Proc) then
8462                   return False;
8463                end if;
8464
8465                --  If we are not a list member, something is strange, so
8466                --  be conservative and return False.
8467
8468                if not Is_List_Member (N) then
8469                   return False;
8470                end if;
8471
8472                --  We are going to find the right formal by stepping forward
8473                --  through the formals, as we step backwards in the actuals.
8474
8475                Form := First_Formal (Proc);
8476                Act  := N;
8477                loop
8478                   --  If no formal, something is weird, so be conservative
8479                   --  and return False.
8480
8481                   if No (Form) then
8482                      return False;
8483                   end if;
8484
8485                   Prev (Act);
8486                   exit when No (Act);
8487                   Next_Formal (Form);
8488                end loop;
8489
8490                return Ekind (Form) /= E_In_Parameter;
8491             end;
8492
8493          --  Named parameter for procedure or accept call
8494
8495          when N_Parameter_Association =>
8496             declare
8497                Proc : Entity_Id;
8498                Form : Entity_Id;
8499
8500             begin
8501                Proc := Get_Subprogram_Entity (Parent (P));
8502
8503                if No (Proc) then
8504                   return False;
8505                end if;
8506
8507                --  Loop through formals to find the one that matches
8508
8509                Form := First_Formal (Proc);
8510                loop
8511                   --  If no matching formal, that's peculiar, some kind of
8512                   --  previous error, so return False to be conservative.
8513
8514                   if No (Form) then
8515                      return False;
8516                   end if;
8517
8518                   --  Else test for match
8519
8520                   if Chars (Form) = Chars (Selector_Name (P)) then
8521                      return Ekind (Form) /= E_In_Parameter;
8522                   end if;
8523
8524                   Next_Formal (Form);
8525                end loop;
8526             end;
8527
8528          --  Test for appearing in a conversion that itself appears
8529          --  in an lvalue context, since this should be an lvalue.
8530
8531          when N_Type_Conversion =>
8532             return Known_To_Be_Assigned (P);
8533
8534          --  All other references are definitely not known to be modifications
8535
8536          when others =>
8537             return False;
8538
8539       end case;
8540    end Known_To_Be_Assigned;
8541
8542    ---------------------------
8543    -- Last_Source_Statement --
8544    ---------------------------
8545
8546    function Last_Source_Statement (HSS : Node_Id) return Node_Id is
8547       N : Node_Id;
8548
8549    begin
8550       N := Last (Statements (HSS));
8551       while Present (N) loop
8552          exit when Comes_From_Source (N);
8553          Prev (N);
8554       end loop;
8555
8556       return N;
8557    end Last_Source_Statement;
8558
8559    ----------------------------------
8560    -- Matching_Static_Array_Bounds --
8561    ----------------------------------
8562
8563    function Matching_Static_Array_Bounds
8564      (L_Typ : Node_Id;
8565       R_Typ : Node_Id) return Boolean
8566    is
8567       L_Ndims : constant Nat := Number_Dimensions (L_Typ);
8568       R_Ndims : constant Nat := Number_Dimensions (R_Typ);
8569
8570       L_Index : Node_Id;
8571       R_Index : Node_Id;
8572       L_Low   : Node_Id;
8573       L_High  : Node_Id;
8574       L_Len   : Uint;
8575       R_Low   : Node_Id;
8576       R_High  : Node_Id;
8577       R_Len   : Uint;
8578
8579    begin
8580       if L_Ndims /= R_Ndims then
8581          return False;
8582       end if;
8583
8584       --  Unconstrained types do not have static bounds
8585
8586       if not Is_Constrained (L_Typ) or else not Is_Constrained (R_Typ) then
8587          return False;
8588       end if;
8589
8590       --  First treat specially the first dimension, as the lower bound and
8591       --  length of string literals are not stored like those of arrays.
8592
8593       if Ekind (L_Typ) = E_String_Literal_Subtype then
8594          L_Low := String_Literal_Low_Bound (L_Typ);
8595          L_Len := String_Literal_Length (L_Typ);
8596       else
8597          L_Index := First_Index (L_Typ);
8598          Get_Index_Bounds (L_Index, L_Low, L_High);
8599
8600          if         Is_OK_Static_Expression (L_Low)
8601            and then Is_OK_Static_Expression (L_High)
8602          then
8603             if Expr_Value (L_High) < Expr_Value (L_Low) then
8604                L_Len := Uint_0;
8605             else
8606                L_Len := (Expr_Value (L_High) - Expr_Value (L_Low)) + 1;
8607             end if;
8608          else
8609             return False;
8610          end if;
8611       end if;
8612
8613       if Ekind (R_Typ) = E_String_Literal_Subtype then
8614          R_Low := String_Literal_Low_Bound (R_Typ);
8615          R_Len := String_Literal_Length (R_Typ);
8616       else
8617          R_Index := First_Index (R_Typ);
8618          Get_Index_Bounds (R_Index, R_Low, R_High);
8619
8620          if         Is_OK_Static_Expression (R_Low)
8621            and then Is_OK_Static_Expression (R_High)
8622          then
8623             if Expr_Value (R_High) < Expr_Value (R_Low) then
8624                R_Len := Uint_0;
8625             else
8626                R_Len := (Expr_Value (R_High) - Expr_Value (R_Low)) + 1;
8627             end if;
8628          else
8629             return False;
8630          end if;
8631       end if;
8632
8633       if         Is_OK_Static_Expression (L_Low)
8634         and then Is_OK_Static_Expression (R_Low)
8635         and then Expr_Value (L_Low) = Expr_Value (R_Low)
8636         and then L_Len = R_Len
8637       then
8638          null;
8639       else
8640          return False;
8641       end if;
8642
8643       --  Then treat all other dimensions
8644
8645       for Indx in 2 .. L_Ndims loop
8646          Next (L_Index);
8647          Next (R_Index);
8648
8649          Get_Index_Bounds (L_Index, L_Low, L_High);
8650          Get_Index_Bounds (R_Index, R_Low, R_High);
8651
8652          if         Is_OK_Static_Expression (L_Low)
8653            and then Is_OK_Static_Expression (L_High)
8654            and then Is_OK_Static_Expression (R_Low)
8655            and then Is_OK_Static_Expression (R_High)
8656            and then Expr_Value (L_Low)  = Expr_Value (R_Low)
8657            and then Expr_Value (L_High) = Expr_Value (R_High)
8658          then
8659             null;
8660          else
8661             return False;
8662          end if;
8663       end loop;
8664
8665       --  If we fall through the loop, all indexes matched
8666
8667       return True;
8668    end Matching_Static_Array_Bounds;
8669
8670    -------------------
8671    -- May_Be_Lvalue --
8672    -------------------
8673
8674    function May_Be_Lvalue (N : Node_Id) return Boolean is
8675       P : constant Node_Id := Parent (N);
8676
8677    begin
8678       case Nkind (P) is
8679
8680          --  Test left side of assignment
8681
8682          when N_Assignment_Statement =>
8683             return N = Name (P);
8684
8685          --  Test prefix of component or attribute. Note that the prefix of an
8686          --  explicit or implicit dereference cannot be an l-value.
8687
8688          when N_Attribute_Reference =>
8689             return N = Prefix (P)
8690               and then Name_Implies_Lvalue_Prefix (Attribute_Name (P));
8691
8692          --  For an expanded name, the name is an lvalue if the expanded name
8693          --  is an lvalue, but the prefix is never an lvalue, since it is just
8694          --  the scope where the name is found.
8695
8696          when N_Expanded_Name        =>
8697             if N = Prefix (P) then
8698                return May_Be_Lvalue (P);
8699             else
8700                return False;
8701             end if;
8702
8703          --  For a selected component A.B, A is certainly an lvalue if A.B is.
8704          --  B is a little interesting, if we have A.B := 3, there is some
8705          --  discussion as to whether B is an lvalue or not, we choose to say
8706          --  it is. Note however that A is not an lvalue if it is of an access
8707          --  type since this is an implicit dereference.
8708
8709          when N_Selected_Component   =>
8710             if N = Prefix (P)
8711               and then Present (Etype (N))
8712               and then Is_Access_Type (Etype (N))
8713             then
8714                return False;
8715             else
8716                return May_Be_Lvalue (P);
8717             end if;
8718
8719          --  For an indexed component or slice, the index or slice bounds is
8720          --  never an lvalue. The prefix is an lvalue if the indexed component
8721          --  or slice is an lvalue, except if it is an access type, where we
8722          --  have an implicit dereference.
8723
8724          when N_Indexed_Component    =>
8725             if N /= Prefix (P)
8726               or else (Present (Etype (N)) and then Is_Access_Type (Etype (N)))
8727             then
8728                return False;
8729             else
8730                return May_Be_Lvalue (P);
8731             end if;
8732
8733          --  Prefix of a reference is an lvalue if the reference is an lvalue
8734
8735          when N_Reference            =>
8736             return May_Be_Lvalue (P);
8737
8738          --  Prefix of explicit dereference is never an lvalue
8739
8740          when N_Explicit_Dereference =>
8741             return False;
8742
8743          --  Positional parameter for subprogram, entry, or accept call.
8744          --  In older versions of Ada function call arguments are never
8745          --  lvalues. In Ada 2012 functions can have in-out parameters.
8746
8747          when N_Function_Call            |
8748               N_Procedure_Call_Statement |
8749               N_Entry_Call_Statement     |
8750               N_Accept_Statement
8751          =>
8752             if Nkind (P) = N_Function_Call
8753               and then Ada_Version < Ada_2012
8754             then
8755                return False;
8756             end if;
8757
8758             --  The following mechanism is clumsy and fragile. A single
8759             --  flag set in Resolve_Actuals would be preferable ???
8760
8761             declare
8762                Proc : Entity_Id;
8763                Form : Entity_Id;
8764                Act  : Node_Id;
8765
8766             begin
8767                Proc := Get_Subprogram_Entity (P);
8768
8769                if No (Proc) then
8770                   return True;
8771                end if;
8772
8773                --  If we are not a list member, something is strange, so
8774                --  be conservative and return True.
8775
8776                if not Is_List_Member (N) then
8777                   return True;
8778                end if;
8779
8780                --  We are going to find the right formal by stepping forward
8781                --  through the formals, as we step backwards in the actuals.
8782
8783                Form := First_Formal (Proc);
8784                Act  := N;
8785                loop
8786                   --  If no formal, something is weird, so be conservative
8787                   --  and return True.
8788
8789                   if No (Form) then
8790                      return True;
8791                   end if;
8792
8793                   Prev (Act);
8794                   exit when No (Act);
8795                   Next_Formal (Form);
8796                end loop;
8797
8798                return Ekind (Form) /= E_In_Parameter;
8799             end;
8800
8801          --  Named parameter for procedure or accept call
8802
8803          when N_Parameter_Association =>
8804             declare
8805                Proc : Entity_Id;
8806                Form : Entity_Id;
8807
8808             begin
8809                Proc := Get_Subprogram_Entity (Parent (P));
8810
8811                if No (Proc) then
8812                   return True;
8813                end if;
8814
8815                --  Loop through formals to find the one that matches
8816
8817                Form := First_Formal (Proc);
8818                loop
8819                   --  If no matching formal, that's peculiar, some kind of
8820                   --  previous error, so return True to be conservative.
8821
8822                   if No (Form) then
8823                      return True;
8824                   end if;
8825
8826                   --  Else test for match
8827
8828                   if Chars (Form) = Chars (Selector_Name (P)) then
8829                      return Ekind (Form) /= E_In_Parameter;
8830                   end if;
8831
8832                   Next_Formal (Form);
8833                end loop;
8834             end;
8835
8836          --  Test for appearing in a conversion that itself appears in an
8837          --  lvalue context, since this should be an lvalue.
8838
8839          when N_Type_Conversion =>
8840             return May_Be_Lvalue (P);
8841
8842          --  Test for appearance in object renaming declaration
8843
8844          when N_Object_Renaming_Declaration =>
8845             return True;
8846
8847          --  All other references are definitely not lvalues
8848
8849          when others =>
8850             return False;
8851
8852       end case;
8853    end May_Be_Lvalue;
8854
8855    -----------------------
8856    -- Mark_Coextensions --
8857    -----------------------
8858
8859    procedure Mark_Coextensions (Context_Nod : Node_Id; Root_Nod : Node_Id) is
8860       Is_Dynamic : Boolean;
8861       --  Indicates whether the context causes nested coextensions to be
8862       --  dynamic or static
8863
8864       function Mark_Allocator (N : Node_Id) return Traverse_Result;
8865       --  Recognize an allocator node and label it as a dynamic coextension
8866
8867       --------------------
8868       -- Mark_Allocator --
8869       --------------------
8870
8871       function Mark_Allocator (N : Node_Id) return Traverse_Result is
8872       begin
8873          if Nkind (N) = N_Allocator then
8874             if Is_Dynamic then
8875                Set_Is_Dynamic_Coextension (N);
8876
8877             --  If the allocator expression is potentially dynamic, it may
8878             --  be expanded out of order and require dynamic allocation
8879             --  anyway, so we treat the coextension itself as dynamic.
8880             --  Potential optimization ???
8881
8882             elsif Nkind (Expression (N)) = N_Qualified_Expression
8883               and then Nkind (Expression (Expression (N))) = N_Op_Concat
8884             then
8885                Set_Is_Dynamic_Coextension (N);
8886
8887             else
8888                Set_Is_Static_Coextension (N);
8889             end if;
8890          end if;
8891
8892          return OK;
8893       end Mark_Allocator;
8894
8895       procedure Mark_Allocators is new Traverse_Proc (Mark_Allocator);
8896
8897    --  Start of processing Mark_Coextensions
8898
8899    begin
8900       case Nkind (Context_Nod) is
8901          when N_Assignment_Statement    |
8902               N_Simple_Return_Statement =>
8903             Is_Dynamic := Nkind (Expression (Context_Nod)) = N_Allocator;
8904
8905          when N_Object_Declaration =>
8906             Is_Dynamic := Nkind (Root_Nod) = N_Allocator;
8907
8908          --  This routine should not be called for constructs which may not
8909          --  contain coextensions.
8910
8911          when others =>
8912             raise Program_Error;
8913       end case;
8914
8915       Mark_Allocators (Root_Nod);
8916    end Mark_Coextensions;
8917
8918    ----------------------
8919    -- Needs_One_Actual --
8920    ----------------------
8921
8922    function Needs_One_Actual (E : Entity_Id) return Boolean is
8923       Formal : Entity_Id;
8924
8925    begin
8926       if Ada_Version >= Ada_2005
8927         and then Present (First_Formal (E))
8928       then
8929          Formal := Next_Formal (First_Formal (E));
8930          while Present (Formal) loop
8931             if No (Default_Value (Formal)) then
8932                return False;
8933             end if;
8934
8935             Next_Formal (Formal);
8936          end loop;
8937
8938          return True;
8939
8940       else
8941          return False;
8942       end if;
8943    end Needs_One_Actual;
8944
8945    ------------------------
8946    -- New_Copy_List_Tree --
8947    ------------------------
8948
8949    function New_Copy_List_Tree (List : List_Id) return List_Id is
8950       NL : List_Id;
8951       E  : Node_Id;
8952
8953    begin
8954       if List = No_List then
8955          return No_List;
8956
8957       else
8958          NL := New_List;
8959          E := First (List);
8960
8961          while Present (E) loop
8962             Append (New_Copy_Tree (E), NL);
8963             E := Next (E);
8964          end loop;
8965
8966          return NL;
8967       end if;
8968    end New_Copy_List_Tree;
8969
8970    -------------------
8971    -- New_Copy_Tree --
8972    -------------------
8973
8974    use Atree.Unchecked_Access;
8975    use Atree_Private_Part;
8976
8977    --  Our approach here requires a two pass traversal of the tree. The
8978    --  first pass visits all nodes that eventually will be copied looking
8979    --  for defining Itypes. If any defining Itypes are found, then they are
8980    --  copied, and an entry is added to the replacement map. In the second
8981    --  phase, the tree is copied, using the replacement map to replace any
8982    --  Itype references within the copied tree.
8983
8984    --  The following hash tables are used if the Map supplied has more
8985    --  than hash threshold entries to speed up access to the map. If
8986    --  there are fewer entries, then the map is searched sequentially
8987    --  (because setting up a hash table for only a few entries takes
8988    --  more time than it saves.
8989
8990    function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num;
8991    --  Hash function used for hash operations
8992
8993    -------------------
8994    -- New_Copy_Hash --
8995    -------------------
8996
8997    function New_Copy_Hash (E : Entity_Id) return NCT_Header_Num is
8998    begin
8999       return Nat (E) mod (NCT_Header_Num'Last + 1);
9000    end New_Copy_Hash;
9001
9002    ---------------
9003    -- NCT_Assoc --
9004    ---------------
9005
9006    --  The hash table NCT_Assoc associates old entities in the table
9007    --  with their corresponding new entities (i.e. the pairs of entries
9008    --  presented in the original Map argument are Key-Element pairs).
9009
9010    package NCT_Assoc is new Simple_HTable (
9011      Header_Num => NCT_Header_Num,
9012      Element    => Entity_Id,
9013      No_Element => Empty,
9014      Key        => Entity_Id,
9015      Hash       => New_Copy_Hash,
9016      Equal      => Types."=");
9017
9018    ---------------------
9019    -- NCT_Itype_Assoc --
9020    ---------------------
9021
9022    --  The hash table NCT_Itype_Assoc contains entries only for those
9023    --  old nodes which have a non-empty Associated_Node_For_Itype set.
9024    --  The key is the associated node, and the element is the new node
9025    --  itself (NOT the associated node for the new node).
9026
9027    package NCT_Itype_Assoc is new Simple_HTable (
9028      Header_Num => NCT_Header_Num,
9029      Element    => Entity_Id,
9030      No_Element => Empty,
9031      Key        => Entity_Id,
9032      Hash       => New_Copy_Hash,
9033      Equal      => Types."=");
9034
9035    --  Start of processing for New_Copy_Tree function
9036
9037    function New_Copy_Tree
9038      (Source    : Node_Id;
9039       Map       : Elist_Id := No_Elist;
9040       New_Sloc  : Source_Ptr := No_Location;
9041       New_Scope : Entity_Id := Empty) return Node_Id
9042    is
9043       Actual_Map : Elist_Id := Map;
9044       --  This is the actual map for the copy. It is initialized with the
9045       --  given elements, and then enlarged as required for Itypes that are
9046       --  copied during the first phase of the copy operation. The visit
9047       --  procedures add elements to this map as Itypes are encountered.
9048       --  The reason we cannot use Map directly, is that it may well be
9049       --  (and normally is) initialized to No_Elist, and if we have mapped
9050       --  entities, we have to reset it to point to a real Elist.
9051
9052       function Assoc (N : Node_Or_Entity_Id) return Node_Id;
9053       --  Called during second phase to map entities into their corresponding
9054       --  copies using Actual_Map. If the argument is not an entity, or is not
9055       --  in Actual_Map, then it is returned unchanged.
9056
9057       procedure Build_NCT_Hash_Tables;
9058       --  Builds hash tables (number of elements >= threshold value)
9059
9060       function Copy_Elist_With_Replacement
9061         (Old_Elist : Elist_Id) return Elist_Id;
9062       --  Called during second phase to copy element list doing replacements
9063
9064       procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id);
9065       --  Called during the second phase to process a copied Itype. The actual
9066       --  copy happened during the first phase (so that we could make the entry
9067       --  in the mapping), but we still have to deal with the descendents of
9068       --  the copied Itype and copy them where necessary.
9069
9070       function Copy_List_With_Replacement (Old_List : List_Id) return List_Id;
9071       --  Called during second phase to copy list doing replacements
9072
9073       function Copy_Node_With_Replacement (Old_Node : Node_Id) return Node_Id;
9074       --  Called during second phase to copy node doing replacements
9075
9076       procedure Visit_Elist (E : Elist_Id);
9077       --  Called during first phase to visit all elements of an Elist
9078
9079       procedure Visit_Field (F : Union_Id; N : Node_Id);
9080       --  Visit a single field, recursing to call Visit_Node or Visit_List
9081       --  if the field is a syntactic descendent of the current node (i.e.
9082       --  its parent is Node N).
9083
9084       procedure Visit_Itype (Old_Itype : Entity_Id);
9085       --  Called during first phase to visit subsidiary fields of a defining
9086       --  Itype, and also create a copy and make an entry in the replacement
9087       --  map for the new copy.
9088
9089       procedure Visit_List (L : List_Id);
9090       --  Called during first phase to visit all elements of a List
9091
9092       procedure Visit_Node (N : Node_Or_Entity_Id);
9093       --  Called during first phase to visit a node and all its subtrees
9094
9095       -----------
9096       -- Assoc --
9097       -----------
9098
9099       function Assoc (N : Node_Or_Entity_Id) return Node_Id is
9100          E   : Elmt_Id;
9101          Ent : Entity_Id;
9102
9103       begin
9104          if not Has_Extension (N) or else No (Actual_Map) then
9105             return N;
9106
9107          elsif NCT_Hash_Tables_Used then
9108             Ent := NCT_Assoc.Get (Entity_Id (N));
9109
9110             if Present (Ent) then
9111                return Ent;
9112             else
9113                return N;
9114             end if;
9115
9116          --  No hash table used, do serial search
9117
9118          else
9119             E := First_Elmt (Actual_Map);
9120             while Present (E) loop
9121                if Node (E) = N then
9122                   return Node (Next_Elmt (E));
9123                else
9124                   E := Next_Elmt (Next_Elmt (E));
9125                end if;
9126             end loop;
9127          end if;
9128
9129          return N;
9130       end Assoc;
9131
9132       ---------------------------
9133       -- Build_NCT_Hash_Tables --
9134       ---------------------------
9135
9136       procedure Build_NCT_Hash_Tables is
9137          Elmt : Elmt_Id;
9138          Ent  : Entity_Id;
9139       begin
9140          if NCT_Hash_Table_Setup then
9141             NCT_Assoc.Reset;
9142             NCT_Itype_Assoc.Reset;
9143          end if;
9144
9145          Elmt := First_Elmt (Actual_Map);
9146          while Present (Elmt) loop
9147             Ent := Node (Elmt);
9148
9149             --  Get new entity, and associate old and new
9150
9151             Next_Elmt (Elmt);
9152             NCT_Assoc.Set (Ent, Node (Elmt));
9153
9154             if Is_Type (Ent) then
9155                declare
9156                   Anode : constant Entity_Id :=
9157                             Associated_Node_For_Itype (Ent);
9158
9159                begin
9160                   if Present (Anode) then
9161
9162                      --  Enter a link between the associated node of the
9163                      --  old Itype and the new Itype, for updating later
9164                      --  when node is copied.
9165
9166                      NCT_Itype_Assoc.Set (Anode, Node (Elmt));
9167                   end if;
9168                end;
9169             end if;
9170
9171             Next_Elmt (Elmt);
9172          end loop;
9173
9174          NCT_Hash_Tables_Used := True;
9175          NCT_Hash_Table_Setup := True;
9176       end Build_NCT_Hash_Tables;
9177
9178       ---------------------------------
9179       -- Copy_Elist_With_Replacement --
9180       ---------------------------------
9181
9182       function Copy_Elist_With_Replacement
9183         (Old_Elist : Elist_Id) return Elist_Id
9184       is
9185          M         : Elmt_Id;
9186          New_Elist : Elist_Id;
9187
9188       begin
9189          if No (Old_Elist) then
9190             return No_Elist;
9191
9192          else
9193             New_Elist := New_Elmt_List;
9194
9195             M := First_Elmt (Old_Elist);
9196             while Present (M) loop
9197                Append_Elmt (Copy_Node_With_Replacement (Node (M)), New_Elist);
9198                Next_Elmt (M);
9199             end loop;
9200          end if;
9201
9202          return New_Elist;
9203       end Copy_Elist_With_Replacement;
9204
9205       ---------------------------------
9206       -- Copy_Itype_With_Replacement --
9207       ---------------------------------
9208
9209       --  This routine exactly parallels its phase one analog Visit_Itype,
9210
9211       procedure Copy_Itype_With_Replacement (New_Itype : Entity_Id) is
9212       begin
9213          --  Translate Next_Entity, Scope and Etype fields, in case they
9214          --  reference entities that have been mapped into copies.
9215
9216          Set_Next_Entity (New_Itype, Assoc (Next_Entity (New_Itype)));
9217          Set_Etype       (New_Itype, Assoc (Etype       (New_Itype)));
9218
9219          if Present (New_Scope) then
9220             Set_Scope    (New_Itype, New_Scope);
9221          else
9222             Set_Scope    (New_Itype, Assoc (Scope       (New_Itype)));
9223          end if;
9224
9225          --  Copy referenced fields
9226
9227          if Is_Discrete_Type (New_Itype) then
9228             Set_Scalar_Range (New_Itype,
9229               Copy_Node_With_Replacement (Scalar_Range (New_Itype)));
9230
9231          elsif Has_Discriminants (Base_Type (New_Itype)) then
9232             Set_Discriminant_Constraint (New_Itype,
9233               Copy_Elist_With_Replacement
9234                 (Discriminant_Constraint (New_Itype)));
9235
9236          elsif Is_Array_Type (New_Itype) then
9237             if Present (First_Index (New_Itype)) then
9238                Set_First_Index (New_Itype,
9239                  First (Copy_List_With_Replacement
9240                          (List_Containing (First_Index (New_Itype)))));
9241             end if;
9242
9243             if Is_Packed (New_Itype) then
9244                Set_Packed_Array_Type (New_Itype,
9245                  Copy_Node_With_Replacement
9246                    (Packed_Array_Type (New_Itype)));
9247             end if;
9248          end if;
9249       end Copy_Itype_With_Replacement;
9250
9251       --------------------------------
9252       -- Copy_List_With_Replacement --
9253       --------------------------------
9254
9255       function Copy_List_With_Replacement
9256         (Old_List : List_Id) return List_Id
9257       is
9258          New_List : List_Id;
9259          E        : Node_Id;
9260
9261       begin
9262          if Old_List = No_List then
9263             return No_List;
9264
9265          else
9266             New_List := Empty_List;
9267
9268             E := First (Old_List);
9269             while Present (E) loop
9270                Append (Copy_Node_With_Replacement (E), New_List);
9271                Next (E);
9272             end loop;
9273
9274             return New_List;
9275          end if;
9276       end Copy_List_With_Replacement;
9277
9278       --------------------------------
9279       -- Copy_Node_With_Replacement --
9280       --------------------------------
9281
9282       function Copy_Node_With_Replacement
9283         (Old_Node : Node_Id) return Node_Id
9284       is
9285          New_Node : Node_Id;
9286
9287          procedure Adjust_Named_Associations
9288            (Old_Node : Node_Id;
9289             New_Node : Node_Id);
9290          --  If a call node has named associations, these are chained through
9291          --  the First_Named_Actual, Next_Named_Actual links. These must be
9292          --  propagated separately to the new parameter list, because these
9293          --  are not syntactic fields.
9294
9295          function Copy_Field_With_Replacement
9296            (Field : Union_Id) return Union_Id;
9297          --  Given Field, which is a field of Old_Node, return a copy of it
9298          --  if it is a syntactic field (i.e. its parent is Node), setting
9299          --  the parent of the copy to poit to New_Node. Otherwise returns
9300          --  the field (possibly mapped if it is an entity).
9301
9302          -------------------------------
9303          -- Adjust_Named_Associations --
9304          -------------------------------
9305
9306          procedure Adjust_Named_Associations
9307            (Old_Node : Node_Id;
9308             New_Node : Node_Id)
9309          is
9310             Old_E : Node_Id;
9311             New_E : Node_Id;
9312
9313             Old_Next : Node_Id;
9314             New_Next : Node_Id;
9315
9316          begin
9317             Old_E := First (Parameter_Associations (Old_Node));
9318             New_E := First (Parameter_Associations (New_Node));
9319             while Present (Old_E) loop
9320                if Nkind (Old_E) = N_Parameter_Association
9321                  and then Present (Next_Named_Actual (Old_E))
9322                then
9323                   if First_Named_Actual (Old_Node)
9324                     =  Explicit_Actual_Parameter (Old_E)
9325                   then
9326                      Set_First_Named_Actual
9327                        (New_Node, Explicit_Actual_Parameter (New_E));
9328                   end if;
9329
9330                   --  Now scan parameter list from the beginning,to locate
9331                   --  next named actual, which can be out of order.
9332
9333                   Old_Next := First (Parameter_Associations (Old_Node));
9334                   New_Next := First (Parameter_Associations (New_Node));
9335
9336                   while Nkind (Old_Next) /= N_Parameter_Association
9337                     or else  Explicit_Actual_Parameter (Old_Next)
9338                       /= Next_Named_Actual (Old_E)
9339                   loop
9340                      Next (Old_Next);
9341                      Next (New_Next);
9342                   end loop;
9343
9344                   Set_Next_Named_Actual
9345                     (New_E, Explicit_Actual_Parameter (New_Next));
9346                end if;
9347
9348                Next (Old_E);
9349                Next (New_E);
9350             end loop;
9351          end Adjust_Named_Associations;
9352
9353          ---------------------------------
9354          -- Copy_Field_With_Replacement --
9355          ---------------------------------
9356
9357          function Copy_Field_With_Replacement
9358            (Field : Union_Id) return Union_Id
9359          is
9360          begin
9361             if Field = Union_Id (Empty) then
9362                return Field;
9363
9364             elsif Field in Node_Range then
9365                declare
9366                   Old_N : constant Node_Id := Node_Id (Field);
9367                   New_N : Node_Id;
9368
9369                begin
9370                   --  If syntactic field, as indicated by the parent pointer
9371                   --  being set, then copy the referenced node recursively.
9372
9373                   if Parent (Old_N) = Old_Node then
9374                      New_N := Copy_Node_With_Replacement (Old_N);
9375
9376                      if New_N /= Old_N then
9377                         Set_Parent (New_N, New_Node);
9378                      end if;
9379
9380                   --  For semantic fields, update possible entity reference
9381                   --  from the replacement map.
9382
9383                   else
9384                      New_N := Assoc (Old_N);
9385                   end if;
9386
9387                   return Union_Id (New_N);
9388                end;
9389
9390             elsif Field in List_Range then
9391                declare
9392                   Old_L : constant List_Id := List_Id (Field);
9393                   New_L : List_Id;
9394
9395                begin
9396                   --  If syntactic field, as indicated by the parent pointer,
9397                   --  then recursively copy the entire referenced list.
9398
9399                   if Parent (Old_L) = Old_Node then
9400                      New_L := Copy_List_With_Replacement (Old_L);
9401                      Set_Parent (New_L, New_Node);
9402
9403                   --  For semantic list, just returned unchanged
9404
9405                   else
9406                      New_L := Old_L;
9407                   end if;
9408
9409                   return Union_Id (New_L);
9410                end;
9411
9412             --  Anything other than a list or a node is returned unchanged
9413
9414             else
9415                return Field;
9416             end if;
9417          end Copy_Field_With_Replacement;
9418
9419       --  Start of processing for Copy_Node_With_Replacement
9420
9421       begin
9422          if Old_Node <= Empty_Or_Error then
9423             return Old_Node;
9424
9425          elsif Has_Extension (Old_Node) then
9426             return Assoc (Old_Node);
9427
9428          else
9429             New_Node := New_Copy (Old_Node);
9430
9431             --  If the node we are copying is the associated node of a
9432             --  previously copied Itype, then adjust the associated node
9433             --  of the copy of that Itype accordingly.
9434
9435             if Present (Actual_Map) then
9436                declare
9437                   E   : Elmt_Id;
9438                   Ent : Entity_Id;
9439
9440                begin
9441                   --  Case of hash table used
9442
9443                   if NCT_Hash_Tables_Used then
9444                      Ent := NCT_Itype_Assoc.Get (Old_Node);
9445
9446                      if Present (Ent) then
9447                         Set_Associated_Node_For_Itype (Ent, New_Node);
9448                      end if;
9449
9450                   --  Case of no hash table used
9451
9452                   else
9453                      E := First_Elmt (Actual_Map);
9454                      while Present (E) loop
9455                         if Is_Itype (Node (E))
9456                           and then
9457                             Old_Node = Associated_Node_For_Itype (Node (E))
9458                         then
9459                            Set_Associated_Node_For_Itype
9460                              (Node (Next_Elmt (E)), New_Node);
9461                         end if;
9462
9463                         E := Next_Elmt (Next_Elmt (E));
9464                      end loop;
9465                   end if;
9466                end;
9467             end if;
9468
9469             --  Recursively copy descendents
9470
9471             Set_Field1
9472               (New_Node, Copy_Field_With_Replacement (Field1 (New_Node)));
9473             Set_Field2
9474               (New_Node, Copy_Field_With_Replacement (Field2 (New_Node)));
9475             Set_Field3
9476               (New_Node, Copy_Field_With_Replacement (Field3 (New_Node)));
9477             Set_Field4
9478               (New_Node, Copy_Field_With_Replacement (Field4 (New_Node)));
9479             Set_Field5
9480               (New_Node, Copy_Field_With_Replacement (Field5 (New_Node)));
9481
9482             --  Adjust Sloc of new node if necessary
9483
9484             if New_Sloc /= No_Location then
9485                Set_Sloc (New_Node, New_Sloc);
9486
9487                --  If we adjust the Sloc, then we are essentially making
9488                --  a completely new node, so the Comes_From_Source flag
9489                --  should be reset to the proper default value.
9490
9491                Nodes.Table (New_Node).Comes_From_Source :=
9492                  Default_Node.Comes_From_Source;
9493             end if;
9494
9495             --  If the node is call and has named associations,
9496             --  set the corresponding links in the copy.
9497
9498             if (Nkind (Old_Node) = N_Function_Call
9499                  or else Nkind (Old_Node) = N_Entry_Call_Statement
9500                  or else
9501                    Nkind (Old_Node) = N_Procedure_Call_Statement)
9502               and then Present (First_Named_Actual (Old_Node))
9503             then
9504                Adjust_Named_Associations (Old_Node, New_Node);
9505             end if;
9506
9507             --  Reset First_Real_Statement for Handled_Sequence_Of_Statements.
9508             --  The replacement mechanism applies to entities, and is not used
9509             --  here. Eventually we may need a more general graph-copying
9510             --  routine. For now, do a sequential search to find desired node.
9511
9512             if Nkind (Old_Node) = N_Handled_Sequence_Of_Statements
9513               and then Present (First_Real_Statement (Old_Node))
9514             then
9515                declare
9516                   Old_F  : constant Node_Id := First_Real_Statement (Old_Node);
9517                   N1, N2 : Node_Id;
9518
9519                begin
9520                   N1 := First (Statements (Old_Node));
9521                   N2 := First (Statements (New_Node));
9522
9523                   while N1 /= Old_F loop
9524                      Next (N1);
9525                      Next (N2);
9526                   end loop;
9527
9528                   Set_First_Real_Statement (New_Node, N2);
9529                end;
9530             end if;
9531          end if;
9532
9533          --  All done, return copied node
9534
9535          return New_Node;
9536       end Copy_Node_With_Replacement;
9537
9538       -----------------
9539       -- Visit_Elist --
9540       -----------------
9541
9542       procedure Visit_Elist (E : Elist_Id) is
9543          Elmt : Elmt_Id;
9544       begin
9545          if Present (E) then
9546             Elmt := First_Elmt (E);
9547
9548             while Elmt /= No_Elmt loop
9549                Visit_Node (Node (Elmt));
9550                Next_Elmt (Elmt);
9551             end loop;
9552          end if;
9553       end Visit_Elist;
9554
9555       -----------------
9556       -- Visit_Field --
9557       -----------------
9558
9559       procedure Visit_Field (F : Union_Id; N : Node_Id) is
9560       begin
9561          if F = Union_Id (Empty) then
9562             return;
9563
9564          elsif F in Node_Range then
9565
9566             --  Copy node if it is syntactic, i.e. its parent pointer is
9567             --  set to point to the field that referenced it (certain
9568             --  Itypes will also meet this criterion, which is fine, since
9569             --  these are clearly Itypes that do need to be copied, since
9570             --  we are copying their parent.)
9571
9572             if Parent (Node_Id (F)) = N then
9573                Visit_Node (Node_Id (F));
9574                return;
9575
9576             --  Another case, if we are pointing to an Itype, then we want
9577             --  to copy it if its associated node is somewhere in the tree
9578             --  being copied.
9579
9580             --  Note: the exclusion of self-referential copies is just an
9581             --  optimization, since the search of the already copied list
9582             --  would catch it, but it is a common case (Etype pointing
9583             --  to itself for an Itype that is a base type).
9584
9585             elsif Has_Extension (Node_Id (F))
9586               and then Is_Itype (Entity_Id (F))
9587               and then Node_Id (F) /= N
9588             then
9589                declare
9590                   P : Node_Id;
9591
9592                begin
9593                   P := Associated_Node_For_Itype (Node_Id (F));
9594                   while Present (P) loop
9595                      if P = Source then
9596                         Visit_Node (Node_Id (F));
9597                         return;
9598                      else
9599                         P := Parent (P);
9600                      end if;
9601                   end loop;
9602
9603                   --  An Itype whose parent is not being copied definitely
9604                   --  should NOT be copied, since it does not belong in any
9605                   --  sense to the copied subtree.
9606
9607                   return;
9608                end;
9609             end if;
9610
9611          elsif F in List_Range
9612            and then Parent (List_Id (F)) = N
9613          then
9614             Visit_List (List_Id (F));
9615             return;
9616          end if;
9617       end Visit_Field;
9618
9619       -----------------
9620       -- Visit_Itype --
9621       -----------------
9622
9623       procedure Visit_Itype (Old_Itype : Entity_Id) is
9624          New_Itype : Entity_Id;
9625          E         : Elmt_Id;
9626          Ent       : Entity_Id;
9627
9628       begin
9629          --  Itypes that describe the designated type of access to subprograms
9630          --  have the structure of subprogram declarations, with signatures,
9631          --  etc. Either we duplicate the signatures completely, or choose to
9632          --  share such itypes, which is fine because their elaboration will
9633          --  have no side effects.
9634
9635          if Ekind (Old_Itype) = E_Subprogram_Type then
9636             return;
9637          end if;
9638
9639          New_Itype := New_Copy (Old_Itype);
9640
9641          --  The new Itype has all the attributes of the old one, and
9642          --  we just copy the contents of the entity. However, the back-end
9643          --  needs different names for debugging purposes, so we create a
9644          --  new internal name for it in all cases.
9645
9646          Set_Chars (New_Itype, New_Internal_Name ('T'));
9647
9648          --  If our associated node is an entity that has already been copied,
9649          --  then set the associated node of the copy to point to the right
9650          --  copy. If we have copied an Itype that is itself the associated
9651          --  node of some previously copied Itype, then we set the right
9652          --  pointer in the other direction.
9653
9654          if Present (Actual_Map) then
9655
9656             --  Case of hash tables used
9657
9658             if NCT_Hash_Tables_Used then
9659
9660                Ent := NCT_Assoc.Get (Associated_Node_For_Itype (Old_Itype));
9661
9662                if Present (Ent) then
9663                   Set_Associated_Node_For_Itype (New_Itype, Ent);
9664                end if;
9665
9666                Ent := NCT_Itype_Assoc.Get (Old_Itype);
9667                if Present (Ent) then
9668                   Set_Associated_Node_For_Itype (Ent, New_Itype);
9669
9670                --  If the hash table has no association for this Itype and
9671                --  its associated node, enter one now.
9672
9673                else
9674                   NCT_Itype_Assoc.Set
9675                     (Associated_Node_For_Itype (Old_Itype), New_Itype);
9676                end if;
9677
9678             --  Case of hash tables not used
9679
9680             else
9681                E := First_Elmt (Actual_Map);
9682                while Present (E) loop
9683                   if Associated_Node_For_Itype (Old_Itype) = Node (E) then
9684                      Set_Associated_Node_For_Itype
9685                        (New_Itype, Node (Next_Elmt (E)));
9686                   end if;
9687
9688                   if Is_Type (Node (E))
9689                     and then
9690                       Old_Itype = Associated_Node_For_Itype (Node (E))
9691                   then
9692                      Set_Associated_Node_For_Itype
9693                        (Node (Next_Elmt (E)), New_Itype);
9694                   end if;
9695
9696                   E := Next_Elmt (Next_Elmt (E));
9697                end loop;
9698             end if;
9699          end if;
9700
9701          if Present (Freeze_Node (New_Itype)) then
9702             Set_Is_Frozen (New_Itype, False);
9703             Set_Freeze_Node (New_Itype, Empty);
9704          end if;
9705
9706          --  Add new association to map
9707
9708          if No (Actual_Map) then
9709             Actual_Map := New_Elmt_List;
9710          end if;
9711
9712          Append_Elmt (Old_Itype, Actual_Map);
9713          Append_Elmt (New_Itype, Actual_Map);
9714
9715          if NCT_Hash_Tables_Used then
9716             NCT_Assoc.Set (Old_Itype, New_Itype);
9717
9718          else
9719             NCT_Table_Entries := NCT_Table_Entries + 1;
9720
9721             if NCT_Table_Entries > NCT_Hash_Threshold then
9722                Build_NCT_Hash_Tables;
9723             end if;
9724          end if;
9725
9726          --  If a record subtype is simply copied, the entity list will be
9727          --  shared. Thus cloned_Subtype must be set to indicate the sharing.
9728
9729          if Ekind_In (Old_Itype, E_Record_Subtype, E_Class_Wide_Subtype) then
9730             Set_Cloned_Subtype (New_Itype, Old_Itype);
9731          end if;
9732
9733          --  Visit descendents that eventually get copied
9734
9735          Visit_Field (Union_Id (Etype (Old_Itype)), Old_Itype);
9736
9737          if Is_Discrete_Type (Old_Itype) then
9738             Visit_Field (Union_Id (Scalar_Range (Old_Itype)), Old_Itype);
9739
9740          elsif Has_Discriminants (Base_Type (Old_Itype)) then
9741             --  ??? This should involve call to Visit_Field
9742             Visit_Elist (Discriminant_Constraint (Old_Itype));
9743
9744          elsif Is_Array_Type (Old_Itype) then
9745             if Present (First_Index (Old_Itype)) then
9746                Visit_Field (Union_Id (List_Containing
9747                                 (First_Index (Old_Itype))),
9748                             Old_Itype);
9749             end if;
9750
9751             if Is_Packed (Old_Itype) then
9752                Visit_Field (Union_Id (Packed_Array_Type (Old_Itype)),
9753                             Old_Itype);
9754             end if;
9755          end if;
9756       end Visit_Itype;
9757
9758       ----------------
9759       -- Visit_List --
9760       ----------------
9761
9762       procedure Visit_List (L : List_Id) is
9763          N : Node_Id;
9764       begin
9765          if L /= No_List then
9766             N := First (L);
9767
9768             while Present (N) loop
9769                Visit_Node (N);
9770                Next (N);
9771             end loop;
9772          end if;
9773       end Visit_List;
9774
9775       ----------------
9776       -- Visit_Node --
9777       ----------------
9778
9779       procedure Visit_Node (N : Node_Or_Entity_Id) is
9780
9781       --  Start of processing for Visit_Node
9782
9783       begin
9784          --  Handle case of an Itype, which must be copied
9785
9786          if Has_Extension (N)
9787            and then Is_Itype (N)
9788          then
9789             --  Nothing to do if already in the list. This can happen with an
9790             --  Itype entity that appears more than once in the tree.
9791             --  Note that we do not want to visit descendents in this case.
9792
9793             --  Test for already in list when hash table is used
9794
9795             if NCT_Hash_Tables_Used then
9796                if Present (NCT_Assoc.Get (Entity_Id (N))) then
9797                   return;
9798                end if;
9799
9800             --  Test for already in list when hash table not used
9801
9802             else
9803                declare
9804                   E : Elmt_Id;
9805                begin
9806                   if Present (Actual_Map) then
9807                      E := First_Elmt (Actual_Map);
9808                      while Present (E) loop
9809                         if Node (E) = N then
9810                            return;
9811                         else
9812                            E := Next_Elmt (Next_Elmt (E));
9813                         end if;
9814                      end loop;
9815                   end if;
9816                end;
9817             end if;
9818
9819             Visit_Itype (N);
9820          end if;
9821
9822          --  Visit descendents
9823
9824          Visit_Field (Field1 (N), N);
9825          Visit_Field (Field2 (N), N);
9826          Visit_Field (Field3 (N), N);
9827          Visit_Field (Field4 (N), N);
9828          Visit_Field (Field5 (N), N);
9829       end Visit_Node;
9830
9831    --  Start of processing for New_Copy_Tree
9832
9833    begin
9834       Actual_Map := Map;
9835
9836       --  See if we should use hash table
9837
9838       if No (Actual_Map) then
9839          NCT_Hash_Tables_Used := False;
9840
9841       else
9842          declare
9843             Elmt : Elmt_Id;
9844
9845          begin
9846             NCT_Table_Entries := 0;
9847
9848             Elmt := First_Elmt (Actual_Map);
9849             while Present (Elmt) loop
9850                NCT_Table_Entries := NCT_Table_Entries + 1;
9851                Next_Elmt (Elmt);
9852                Next_Elmt (Elmt);
9853             end loop;
9854
9855             if NCT_Table_Entries > NCT_Hash_Threshold then
9856                Build_NCT_Hash_Tables;
9857             else
9858                NCT_Hash_Tables_Used := False;
9859             end if;
9860          end;
9861       end if;
9862
9863       --  Hash table set up if required, now start phase one by visiting
9864       --  top node (we will recursively visit the descendents).
9865
9866       Visit_Node (Source);
9867
9868       --  Now the second phase of the copy can start. First we process
9869       --  all the mapped entities, copying their descendents.
9870
9871       if Present (Actual_Map) then
9872          declare
9873             Elmt      : Elmt_Id;
9874             New_Itype : Entity_Id;
9875          begin
9876             Elmt := First_Elmt (Actual_Map);
9877             while Present (Elmt) loop
9878                Next_Elmt (Elmt);
9879                New_Itype := Node (Elmt);
9880                Copy_Itype_With_Replacement (New_Itype);
9881                Next_Elmt (Elmt);
9882             end loop;
9883          end;
9884       end if;
9885
9886       --  Now we can copy the actual tree
9887
9888       return Copy_Node_With_Replacement (Source);
9889    end New_Copy_Tree;
9890
9891    -------------------------
9892    -- New_External_Entity --
9893    -------------------------
9894
9895    function New_External_Entity
9896      (Kind         : Entity_Kind;
9897       Scope_Id     : Entity_Id;
9898       Sloc_Value   : Source_Ptr;
9899       Related_Id   : Entity_Id;
9900       Suffix       : Character;
9901       Suffix_Index : Nat := 0;
9902       Prefix       : Character := ' ') return Entity_Id
9903    is
9904       N : constant Entity_Id :=
9905             Make_Defining_Identifier (Sloc_Value,
9906               New_External_Name
9907                 (Chars (Related_Id), Suffix, Suffix_Index, Prefix));
9908
9909    begin
9910       Set_Ekind          (N, Kind);
9911       Set_Is_Internal    (N, True);
9912       Append_Entity      (N, Scope_Id);
9913       Set_Public_Status  (N);
9914
9915       if Kind in Type_Kind then
9916          Init_Size_Align (N);
9917       end if;
9918
9919       return N;
9920    end New_External_Entity;
9921
9922    -------------------------
9923    -- New_Internal_Entity --
9924    -------------------------
9925
9926    function New_Internal_Entity
9927      (Kind       : Entity_Kind;
9928       Scope_Id   : Entity_Id;
9929       Sloc_Value : Source_Ptr;
9930       Id_Char    : Character) return Entity_Id
9931    is
9932       N : constant Entity_Id := Make_Temporary (Sloc_Value, Id_Char);
9933
9934    begin
9935       Set_Ekind          (N, Kind);
9936       Set_Is_Internal    (N, True);
9937       Append_Entity      (N, Scope_Id);
9938
9939       if Kind in Type_Kind then
9940          Init_Size_Align (N);
9941       end if;
9942
9943       return N;
9944    end New_Internal_Entity;
9945
9946    -----------------
9947    -- Next_Actual --
9948    -----------------
9949
9950    function Next_Actual (Actual_Id : Node_Id) return Node_Id is
9951       N  : Node_Id;
9952
9953    begin
9954       --  If we are pointing at a positional parameter, it is a member of a
9955       --  node list (the list of parameters), and the next parameter is the
9956       --  next node on the list, unless we hit a parameter association, then
9957       --  we shift to using the chain whose head is the First_Named_Actual in
9958       --  the parent, and then is threaded using the Next_Named_Actual of the
9959       --  Parameter_Association. All this fiddling is because the original node
9960       --  list is in the textual call order, and what we need is the
9961       --  declaration order.
9962
9963       if Is_List_Member (Actual_Id) then
9964          N := Next (Actual_Id);
9965
9966          if Nkind (N) = N_Parameter_Association then
9967             return First_Named_Actual (Parent (Actual_Id));
9968          else
9969             return N;
9970          end if;
9971
9972       else
9973          return Next_Named_Actual (Parent (Actual_Id));
9974       end if;
9975    end Next_Actual;
9976
9977    procedure Next_Actual (Actual_Id : in out Node_Id) is
9978    begin
9979       Actual_Id := Next_Actual (Actual_Id);
9980    end Next_Actual;
9981
9982    -----------------------
9983    -- Normalize_Actuals --
9984    -----------------------
9985
9986    --  Chain actuals according to formals of subprogram. If there are no named
9987    --  associations, the chain is simply the list of Parameter Associations,
9988    --  since the order is the same as the declaration order. If there are named
9989    --  associations, then the First_Named_Actual field in the N_Function_Call
9990    --  or N_Procedure_Call_Statement node points to the Parameter_Association
9991    --  node for the parameter that comes first in declaration order. The
9992    --  remaining named parameters are then chained in declaration order using
9993    --  Next_Named_Actual.
9994
9995    --  This routine also verifies that the number of actuals is compatible with
9996    --  the number and default values of formals, but performs no type checking
9997    --  (type checking is done by the caller).
9998
9999    --  If the matching succeeds, Success is set to True and the caller proceeds
10000    --  with type-checking. If the match is unsuccessful, then Success is set to
10001    --  False, and the caller attempts a different interpretation, if there is
10002    --  one.
10003
10004    --  If the flag Report is on, the call is not overloaded, and a failure to
10005    --  match can be reported here, rather than in the caller.
10006
10007    procedure Normalize_Actuals
10008      (N       : Node_Id;
10009       S       : Entity_Id;
10010       Report  : Boolean;
10011       Success : out Boolean)
10012    is
10013       Actuals     : constant List_Id := Parameter_Associations (N);
10014       Actual      : Node_Id := Empty;
10015       Formal      : Entity_Id;
10016       Last        : Node_Id := Empty;
10017       First_Named : Node_Id := Empty;
10018       Found       : Boolean;
10019
10020       Formals_To_Match : Integer := 0;
10021       Actuals_To_Match : Integer := 0;
10022
10023       procedure Chain (A : Node_Id);
10024       --  Add named actual at the proper place in the list, using the
10025       --  Next_Named_Actual link.
10026
10027       function Reporting return Boolean;
10028       --  Determines if an error is to be reported. To report an error, we
10029       --  need Report to be True, and also we do not report errors caused
10030       --  by calls to init procs that occur within other init procs. Such
10031       --  errors must always be cascaded errors, since if all the types are
10032       --  declared correctly, the compiler will certainly build decent calls!
10033
10034       -----------
10035       -- Chain --
10036       -----------
10037
10038       procedure Chain (A : Node_Id) is
10039       begin
10040          if No (Last) then
10041
10042             --  Call node points to first actual in list
10043
10044             Set_First_Named_Actual (N, Explicit_Actual_Parameter (A));
10045
10046          else
10047             Set_Next_Named_Actual (Last, Explicit_Actual_Parameter (A));
10048          end if;
10049
10050          Last := A;
10051          Set_Next_Named_Actual (Last, Empty);
10052       end Chain;
10053
10054       ---------------
10055       -- Reporting --
10056       ---------------
10057
10058       function Reporting return Boolean is
10059       begin
10060          if not Report then
10061             return False;
10062
10063          elsif not Within_Init_Proc then
10064             return True;
10065
10066          elsif Is_Init_Proc (Entity (Name (N))) then
10067             return False;
10068
10069          else
10070             return True;
10071          end if;
10072       end Reporting;
10073
10074    --  Start of processing for Normalize_Actuals
10075
10076    begin
10077       if Is_Access_Type (S) then
10078
10079          --  The name in the call is a function call that returns an access
10080          --  to subprogram. The designated type has the list of formals.
10081
10082          Formal := First_Formal (Designated_Type (S));
10083       else
10084          Formal := First_Formal (S);
10085       end if;
10086
10087       while Present (Formal) loop
10088          Formals_To_Match := Formals_To_Match + 1;
10089          Next_Formal (Formal);
10090       end loop;
10091
10092       --  Find if there is a named association, and verify that no positional
10093       --  associations appear after named ones.
10094
10095       if Present (Actuals) then
10096          Actual := First (Actuals);
10097       end if;
10098
10099       while Present (Actual)
10100         and then Nkind (Actual) /= N_Parameter_Association
10101       loop
10102          Actuals_To_Match := Actuals_To_Match + 1;
10103          Next (Actual);
10104       end loop;
10105
10106       if No (Actual) and Actuals_To_Match = Formals_To_Match then
10107
10108          --  Most common case: positional notation, no defaults
10109
10110          Success := True;
10111          return;
10112
10113       elsif Actuals_To_Match > Formals_To_Match then
10114
10115          --  Too many actuals: will not work
10116
10117          if Reporting then
10118             if Is_Entity_Name (Name (N)) then
10119                Error_Msg_N ("too many arguments in call to&", Name (N));
10120             else
10121                Error_Msg_N ("too many arguments in call", N);
10122             end if;
10123          end if;
10124
10125          Success := False;
10126          return;
10127       end if;
10128
10129       First_Named := Actual;
10130
10131       while Present (Actual) loop
10132          if Nkind (Actual) /= N_Parameter_Association then
10133             Error_Msg_N
10134               ("positional parameters not allowed after named ones", Actual);
10135             Success := False;
10136             return;
10137
10138          else
10139             Actuals_To_Match := Actuals_To_Match + 1;
10140          end if;
10141
10142          Next (Actual);
10143       end loop;
10144
10145       if Present (Actuals) then
10146          Actual := First (Actuals);
10147       end if;
10148
10149       Formal := First_Formal (S);
10150       while Present (Formal) loop
10151
10152          --  Match the formals in order. If the corresponding actual is
10153          --  positional, nothing to do. Else scan the list of named actuals
10154          --  to find the one with the right name.
10155
10156          if Present (Actual)
10157            and then Nkind (Actual) /= N_Parameter_Association
10158          then
10159             Next (Actual);
10160             Actuals_To_Match := Actuals_To_Match - 1;
10161             Formals_To_Match := Formals_To_Match - 1;
10162
10163          else
10164             --  For named parameters, search the list of actuals to find
10165             --  one that matches the next formal name.
10166
10167             Actual := First_Named;
10168             Found  := False;
10169             while Present (Actual) loop
10170                if Chars (Selector_Name (Actual)) = Chars (Formal) then
10171                   Found := True;
10172                   Chain (Actual);
10173                   Actuals_To_Match := Actuals_To_Match - 1;
10174                   Formals_To_Match := Formals_To_Match - 1;
10175                   exit;
10176                end if;
10177
10178                Next (Actual);
10179             end loop;
10180
10181             if not Found then
10182                if Ekind (Formal) /= E_In_Parameter
10183                  or else No (Default_Value (Formal))
10184                then
10185                   if Reporting then
10186                      if (Comes_From_Source (S)
10187                           or else Sloc (S) = Standard_Location)
10188                        and then Is_Overloadable (S)
10189                      then
10190                         if No (Actuals)
10191                           and then
10192                            (Nkind (Parent (N)) = N_Procedure_Call_Statement
10193                              or else
10194                            (Nkind (Parent (N)) = N_Function_Call
10195                              or else
10196                             Nkind (Parent (N)) = N_Parameter_Association))
10197                           and then Ekind (S) /= E_Function
10198                         then
10199                            Set_Etype (N, Etype (S));
10200                         else
10201                            Error_Msg_Name_1 := Chars (S);
10202                            Error_Msg_Sloc := Sloc (S);
10203                            Error_Msg_NE
10204                              ("missing argument for parameter & " &
10205                                 "in call to % declared #", N, Formal);
10206                         end if;
10207
10208                      elsif Is_Overloadable (S) then
10209                         Error_Msg_Name_1 := Chars (S);
10210
10211                         --  Point to type derivation that generated the
10212                         --  operation.
10213
10214                         Error_Msg_Sloc := Sloc (Parent (S));
10215
10216                         Error_Msg_NE
10217                           ("missing argument for parameter & " &
10218                              "in call to % (inherited) #", N, Formal);
10219
10220                      else
10221                         Error_Msg_NE
10222                           ("missing argument for parameter &", N, Formal);
10223                      end if;
10224                   end if;
10225
10226                   Success := False;
10227                   return;
10228
10229                else
10230                   Formals_To_Match := Formals_To_Match - 1;
10231                end if;
10232             end if;
10233          end if;
10234
10235          Next_Formal (Formal);
10236       end loop;
10237
10238       if Formals_To_Match = 0 and then Actuals_To_Match = 0 then
10239          Success := True;
10240          return;
10241
10242       else
10243          if Reporting then
10244
10245             --  Find some superfluous named actual that did not get
10246             --  attached to the list of associations.
10247
10248             Actual := First (Actuals);
10249             while Present (Actual) loop
10250                if Nkind (Actual) = N_Parameter_Association
10251                  and then Actual /= Last
10252                  and then No (Next_Named_Actual (Actual))
10253                then
10254                   Error_Msg_N ("unmatched actual & in call",
10255                     Selector_Name (Actual));
10256                   exit;
10257                end if;
10258
10259                Next (Actual);
10260             end loop;
10261          end if;
10262
10263          Success := False;
10264          return;
10265       end if;
10266    end Normalize_Actuals;
10267
10268    --------------------------------
10269    -- Note_Possible_Modification --
10270    --------------------------------
10271
10272    procedure Note_Possible_Modification (N : Node_Id; Sure : Boolean) is
10273       Modification_Comes_From_Source : constant Boolean :=
10274                                          Comes_From_Source (Parent (N));
10275
10276       Ent : Entity_Id;
10277       Exp : Node_Id;
10278
10279    begin
10280       --  Loop to find referenced entity, if there is one
10281
10282       Exp := N;
10283       loop
10284          <<Continue>>
10285          Ent := Empty;
10286
10287          if Is_Entity_Name (Exp) then
10288             Ent := Entity (Exp);
10289
10290             --  If the entity is missing, it is an undeclared identifier,
10291             --  and there is nothing to annotate.
10292
10293             if No (Ent) then
10294                return;
10295             end if;
10296
10297          elsif Nkind (Exp) = N_Explicit_Dereference then
10298             declare
10299                P : constant Node_Id := Prefix (Exp);
10300
10301             begin
10302                if Nkind (P) = N_Selected_Component
10303                  and then Present (
10304                    Entry_Formal (Entity (Selector_Name (P))))
10305                then
10306                   --  Case of a reference to an entry formal
10307
10308                   Ent := Entry_Formal (Entity (Selector_Name (P)));
10309
10310                elsif Nkind (P) = N_Identifier
10311                  and then Nkind (Parent (Entity (P))) = N_Object_Declaration
10312                  and then Present (Expression (Parent (Entity (P))))
10313                  and then Nkind (Expression (Parent (Entity (P))))
10314                    = N_Reference
10315                then
10316                   --  Case of a reference to a value on which side effects have
10317                   --  been removed.
10318
10319                   Exp := Prefix (Expression (Parent (Entity (P))));
10320                   goto Continue;
10321
10322                else
10323                   return;
10324
10325                end if;
10326             end;
10327
10328          elsif     Nkind (Exp) = N_Type_Conversion
10329            or else Nkind (Exp) = N_Unchecked_Type_Conversion
10330          then
10331             Exp := Expression (Exp);
10332             goto Continue;
10333
10334          elsif     Nkind (Exp) = N_Slice
10335            or else Nkind (Exp) = N_Indexed_Component
10336            or else Nkind (Exp) = N_Selected_Component
10337          then
10338             Exp := Prefix (Exp);
10339             goto Continue;
10340
10341          else
10342             return;
10343          end if;
10344
10345          --  Now look for entity being referenced
10346
10347          if Present (Ent) then
10348             if Is_Object (Ent) then
10349                if Comes_From_Source (Exp)
10350                  or else Modification_Comes_From_Source
10351                then
10352                   --  Give warning if pragma unmodified given and we are
10353                   --  sure this is a modification.
10354
10355                   if Has_Pragma_Unmodified (Ent) and then Sure then
10356                      Error_Msg_NE ("?pragma Unmodified given for &!", N, Ent);
10357                   end if;
10358
10359                   Set_Never_Set_In_Source (Ent, False);
10360                end if;
10361
10362                Set_Is_True_Constant (Ent, False);
10363                Set_Current_Value    (Ent, Empty);
10364                Set_Is_Known_Null    (Ent, False);
10365
10366                if not Can_Never_Be_Null (Ent) then
10367                   Set_Is_Known_Non_Null (Ent, False);
10368                end if;
10369
10370                --  Follow renaming chain
10371
10372                if (Ekind (Ent) = E_Variable or else Ekind (Ent) = E_Constant)
10373                  and then Present (Renamed_Object (Ent))
10374                then
10375                   Exp := Renamed_Object (Ent);
10376                   goto Continue;
10377                end if;
10378
10379                --  Generate a reference only if the assignment comes from
10380                --  source. This excludes, for example, calls to a dispatching
10381                --  assignment operation when the left-hand side is tagged.
10382
10383                if Modification_Comes_From_Source then
10384                   Generate_Reference (Ent, Exp, 'm');
10385
10386                   --  If the target of the assignment is the bound variable
10387                   --  in an iterator, indicate that the corresponding array
10388                   --  or container is also modified.
10389
10390                   if Ada_Version >= Ada_2012
10391                     and then
10392                       Nkind (Parent (Ent)) = N_Iterator_Specification
10393                   then
10394                      declare
10395                         Domain : constant Node_Id := Name (Parent (Ent));
10396
10397                      begin
10398                         --  TBD : in the full version of the construct, the
10399                         --  domain of iteration can be given by an expression.
10400
10401                         if Is_Entity_Name (Domain) then
10402                            Generate_Reference      (Entity (Domain), Exp, 'm');
10403                            Set_Is_True_Constant    (Entity (Domain), False);
10404                            Set_Never_Set_In_Source (Entity (Domain), False);
10405                         end if;
10406                      end;
10407                   end if;
10408                end if;
10409
10410                Check_Nested_Access (Ent);
10411             end if;
10412
10413             Kill_Checks (Ent);
10414
10415             --  If we are sure this is a modification from source, and we know
10416             --  this modifies a constant, then give an appropriate warning.
10417
10418             if Overlays_Constant (Ent)
10419               and then Modification_Comes_From_Source
10420               and then Sure
10421             then
10422                declare
10423                   A : constant Node_Id := Address_Clause (Ent);
10424                begin
10425                   if Present (A) then
10426                      declare
10427                         Exp : constant Node_Id := Expression (A);
10428                      begin
10429                         if Nkind (Exp) = N_Attribute_Reference
10430                           and then Attribute_Name (Exp) = Name_Address
10431                           and then Is_Entity_Name (Prefix (Exp))
10432                         then
10433                            Error_Msg_Sloc := Sloc (A);
10434                            Error_Msg_NE
10435                              ("constant& may be modified via address clause#?",
10436                               N, Entity (Prefix (Exp)));
10437                         end if;
10438                      end;
10439                   end if;
10440                end;
10441             end if;
10442
10443             return;
10444          end if;
10445       end loop;
10446    end Note_Possible_Modification;
10447
10448    -------------------------
10449    -- Object_Access_Level --
10450    -------------------------
10451
10452    function Object_Access_Level (Obj : Node_Id) return Uint is
10453       E : Entity_Id;
10454
10455    --  Returns the static accessibility level of the view denoted by Obj. Note
10456    --  that the value returned is the result of a call to Scope_Depth. Only
10457    --  scope depths associated with dynamic scopes can actually be returned.
10458    --  Since only relative levels matter for accessibility checking, the fact
10459    --  that the distance between successive levels of accessibility is not
10460    --  always one is immaterial (invariant: if level(E2) is deeper than
10461    --  level(E1), then Scope_Depth(E1) < Scope_Depth(E2)).
10462
10463       function Reference_To (Obj : Node_Id) return Node_Id;
10464       --  An explicit dereference is created when removing side-effects from
10465       --  expressions for constraint checking purposes. In this case a local
10466       --  access type is created for it. The correct access level is that of
10467       --  the original source node. We detect this case by noting that the
10468       --  prefix of the dereference is created by an object declaration whose
10469       --  initial expression is a reference.
10470
10471       ------------------
10472       -- Reference_To --
10473       ------------------
10474
10475       function Reference_To (Obj : Node_Id) return Node_Id is
10476          Pref : constant Node_Id := Prefix (Obj);
10477       begin
10478          if Is_Entity_Name (Pref)
10479            and then Nkind (Parent (Entity (Pref))) = N_Object_Declaration
10480            and then Present (Expression (Parent (Entity (Pref))))
10481            and then Nkind (Expression (Parent (Entity (Pref)))) = N_Reference
10482          then
10483             return (Prefix (Expression (Parent (Entity (Pref)))));
10484          else
10485             return Empty;
10486          end if;
10487       end Reference_To;
10488
10489    --  Start of processing for Object_Access_Level
10490
10491    begin
10492       if Is_Entity_Name (Obj) then
10493          E := Entity (Obj);
10494
10495          if Is_Prival (E) then
10496             E := Prival_Link (E);
10497          end if;
10498
10499          --  If E is a type then it denotes a current instance. For this case
10500          --  we add one to the normal accessibility level of the type to ensure
10501          --  that current instances are treated as always being deeper than
10502          --  than the level of any visible named access type (see 3.10.2(21)).
10503
10504          if Is_Type (E) then
10505             return Type_Access_Level (E) +  1;
10506
10507          elsif Present (Renamed_Object (E)) then
10508             return Object_Access_Level (Renamed_Object (E));
10509
10510          --  Similarly, if E is a component of the current instance of a
10511          --  protected type, any instance of it is assumed to be at a deeper
10512          --  level than the type. For a protected object (whose type is an
10513          --  anonymous protected type) its components are at the same level
10514          --  as the type itself.
10515
10516          elsif not Is_Overloadable (E)
10517            and then Ekind (Scope (E)) = E_Protected_Type
10518            and then Comes_From_Source (Scope (E))
10519          then
10520             return Type_Access_Level (Scope (E)) + 1;
10521
10522          else
10523             return Scope_Depth (Enclosing_Dynamic_Scope (E));
10524          end if;
10525
10526       elsif Nkind (Obj) = N_Selected_Component then
10527          if Is_Access_Type (Etype (Prefix (Obj))) then
10528             return Type_Access_Level (Etype (Prefix (Obj)));
10529          else
10530             return Object_Access_Level (Prefix (Obj));
10531          end if;
10532
10533       elsif Nkind (Obj) = N_Indexed_Component then
10534          if Is_Access_Type (Etype (Prefix (Obj))) then
10535             return Type_Access_Level (Etype (Prefix (Obj)));
10536          else
10537             return Object_Access_Level (Prefix (Obj));
10538          end if;
10539
10540       elsif Nkind (Obj) = N_Explicit_Dereference then
10541
10542          --  If the prefix is a selected access discriminant then we make a
10543          --  recursive call on the prefix, which will in turn check the level
10544          --  of the prefix object of the selected discriminant.
10545
10546          if Nkind (Prefix (Obj)) = N_Selected_Component
10547            and then Ekind (Etype (Prefix (Obj))) = E_Anonymous_Access_Type
10548            and then
10549              Ekind (Entity (Selector_Name (Prefix (Obj)))) = E_Discriminant
10550          then
10551             return Object_Access_Level (Prefix (Obj));
10552
10553          elsif not (Comes_From_Source (Obj)) then
10554             declare
10555                Ref : constant Node_Id := Reference_To (Obj);
10556             begin
10557                if Present (Ref) then
10558                   return Object_Access_Level (Ref);
10559                else
10560                   return Type_Access_Level (Etype (Prefix (Obj)));
10561                end if;
10562             end;
10563
10564          else
10565             return Type_Access_Level (Etype (Prefix (Obj)));
10566          end if;
10567
10568       elsif Nkind (Obj) = N_Type_Conversion
10569         or else Nkind (Obj) = N_Unchecked_Type_Conversion
10570       then
10571          return Object_Access_Level (Expression (Obj));
10572
10573       elsif Nkind (Obj) = N_Function_Call then
10574
10575          --  Function results are objects, so we get either the access level of
10576          --  the function or, in the case of an indirect call, the level of the
10577          --  access-to-subprogram type. (This code is used for Ada 95, but it
10578          --  looks wrong, because it seems that we should be checking the level
10579          --  of the call itself, even for Ada 95. However, using the Ada 2005
10580          --  version of the code causes regressions in several tests that are
10581          --  compiled with -gnat95. ???)
10582
10583          if Ada_Version < Ada_2005 then
10584             if Is_Entity_Name (Name (Obj)) then
10585                return Subprogram_Access_Level (Entity (Name (Obj)));
10586             else
10587                return Type_Access_Level (Etype (Prefix (Name (Obj))));
10588             end if;
10589
10590          --  For Ada 2005, the level of the result object of a function call is
10591          --  defined to be the level of the call's innermost enclosing master.
10592          --  We determine that by querying the depth of the innermost enclosing
10593          --  dynamic scope.
10594
10595          else
10596             Return_Master_Scope_Depth_Of_Call : declare
10597
10598                function Innermost_Master_Scope_Depth
10599                  (N : Node_Id) return Uint;
10600                --  Returns the scope depth of the given node's innermost
10601                --  enclosing dynamic scope (effectively the accessibility
10602                --  level of the innermost enclosing master).
10603
10604                ----------------------------------
10605                -- Innermost_Master_Scope_Depth --
10606                ----------------------------------
10607
10608                function Innermost_Master_Scope_Depth
10609                  (N : Node_Id) return Uint
10610                is
10611                   Node_Par : Node_Id := Parent (N);
10612
10613                begin
10614                   --  Locate the nearest enclosing node (by traversing Parents)
10615                   --  that Defining_Entity can be applied to, and return the
10616                   --  depth of that entity's nearest enclosing dynamic scope.
10617
10618                   while Present (Node_Par) loop
10619                      case Nkind (Node_Par) is
10620                         when N_Component_Declaration           |
10621                              N_Entry_Declaration               |
10622                              N_Formal_Object_Declaration       |
10623                              N_Formal_Type_Declaration         |
10624                              N_Full_Type_Declaration           |
10625                              N_Incomplete_Type_Declaration     |
10626                              N_Loop_Parameter_Specification    |
10627                              N_Object_Declaration              |
10628                              N_Protected_Type_Declaration      |
10629                              N_Private_Extension_Declaration   |
10630                              N_Private_Type_Declaration        |
10631                              N_Subtype_Declaration             |
10632                              N_Function_Specification          |
10633                              N_Procedure_Specification         |
10634                              N_Task_Type_Declaration           |
10635                              N_Body_Stub                       |
10636                              N_Generic_Instantiation           |
10637                              N_Proper_Body                     |
10638                              N_Implicit_Label_Declaration      |
10639                              N_Package_Declaration             |
10640                              N_Single_Task_Declaration         |
10641                              N_Subprogram_Declaration          |
10642                              N_Generic_Declaration             |
10643                              N_Renaming_Declaration            |
10644                              N_Block_Statement                 |
10645                              N_Formal_Subprogram_Declaration   |
10646                              N_Abstract_Subprogram_Declaration |
10647                              N_Entry_Body                      |
10648                              N_Exception_Declaration           |
10649                              N_Formal_Package_Declaration      |
10650                              N_Number_Declaration              |
10651                              N_Package_Specification           |
10652                              N_Parameter_Specification         |
10653                              N_Single_Protected_Declaration    |
10654                              N_Subunit                         =>
10655
10656                            return Scope_Depth
10657                                     (Nearest_Dynamic_Scope
10658                                        (Defining_Entity (Node_Par)));
10659
10660                         when others =>
10661                            null;
10662                      end case;
10663
10664                      Node_Par := Parent (Node_Par);
10665                   end loop;
10666
10667                   pragma Assert (False);
10668
10669                   --  Should never reach the following return
10670
10671                   return Scope_Depth (Current_Scope) + 1;
10672                end Innermost_Master_Scope_Depth;
10673
10674             --  Start of processing for Return_Master_Scope_Depth_Of_Call
10675
10676             begin
10677                return Innermost_Master_Scope_Depth (Obj);
10678             end Return_Master_Scope_Depth_Of_Call;
10679          end if;
10680
10681       --  For convenience we handle qualified expressions, even though
10682       --  they aren't technically object names.
10683
10684       elsif Nkind (Obj) = N_Qualified_Expression then
10685          return Object_Access_Level (Expression (Obj));
10686
10687       --  Otherwise return the scope level of Standard.
10688       --  (If there are cases that fall through
10689       --  to this point they will be treated as
10690       --  having global accessibility for now. ???)
10691
10692       else
10693          return Scope_Depth (Standard_Standard);
10694       end if;
10695    end Object_Access_Level;
10696
10697    --------------------------------------
10698    -- Original_Corresponding_Operation --
10699    --------------------------------------
10700
10701    function Original_Corresponding_Operation (S : Entity_Id) return Entity_Id
10702    is
10703       Typ : constant Entity_Id := Find_Dispatching_Type (S);
10704
10705    begin
10706       --  If S is an inherited primitive S2 the original corresponding
10707       --  operation of S is the original corresponding operation of S2
10708
10709       if Present (Alias (S))
10710         and then Find_Dispatching_Type (Alias (S)) /= Typ
10711       then
10712          return Original_Corresponding_Operation (Alias (S));
10713
10714       --  If S overrides an inherited subprogram S2 the original corresponding
10715       --  operation of S is the original corresponding operation of S2
10716
10717       elsif Present (Overridden_Operation (S)) then
10718          return Original_Corresponding_Operation (Overridden_Operation (S));
10719
10720       --  otherwise it is S itself
10721
10722       else
10723          return S;
10724       end if;
10725    end Original_Corresponding_Operation;
10726
10727    -----------------------
10728    -- Private_Component --
10729    -----------------------
10730
10731    function Private_Component (Type_Id : Entity_Id) return Entity_Id is
10732       Ancestor  : constant Entity_Id := Base_Type (Type_Id);
10733
10734       function Trace_Components
10735         (T     : Entity_Id;
10736          Check : Boolean) return Entity_Id;
10737       --  Recursive function that does the work, and checks against circular
10738       --  definition for each subcomponent type.
10739
10740       ----------------------
10741       -- Trace_Components --
10742       ----------------------
10743
10744       function Trace_Components
10745          (T     : Entity_Id;
10746           Check : Boolean) return Entity_Id
10747        is
10748          Btype     : constant Entity_Id := Base_Type (T);
10749          Component : Entity_Id;
10750          P         : Entity_Id;
10751          Candidate : Entity_Id := Empty;
10752
10753       begin
10754          if Check and then Btype = Ancestor then
10755             Error_Msg_N ("circular type definition", Type_Id);
10756             return Any_Type;
10757          end if;
10758
10759          if Is_Private_Type (Btype)
10760            and then not Is_Generic_Type (Btype)
10761          then
10762             if Present (Full_View (Btype))
10763               and then Is_Record_Type (Full_View (Btype))
10764               and then not Is_Frozen (Btype)
10765             then
10766                --  To indicate that the ancestor depends on a private type, the
10767                --  current Btype is sufficient. However, to check for circular
10768                --  definition we must recurse on the full view.
10769
10770                Candidate := Trace_Components (Full_View (Btype), True);
10771
10772                if Candidate = Any_Type then
10773                   return Any_Type;
10774                else
10775                   return Btype;
10776                end if;
10777
10778             else
10779                return Btype;
10780             end if;
10781
10782          elsif Is_Array_Type (Btype) then
10783             return Trace_Components (Component_Type (Btype), True);
10784
10785          elsif Is_Record_Type (Btype) then
10786             Component := First_Entity (Btype);
10787             while Present (Component) loop
10788
10789                --  Skip anonymous types generated by constrained components
10790
10791                if not Is_Type (Component) then
10792                   P := Trace_Components (Etype (Component), True);
10793
10794                   if Present (P) then
10795                      if P = Any_Type then
10796                         return P;
10797                      else
10798                         Candidate := P;
10799                      end if;
10800                   end if;
10801                end if;
10802
10803                Next_Entity (Component);
10804             end loop;
10805
10806             return Candidate;
10807
10808          else
10809             return Empty;
10810          end if;
10811       end Trace_Components;
10812
10813    --  Start of processing for Private_Component
10814
10815    begin
10816       return Trace_Components (Type_Id, False);
10817    end Private_Component;
10818
10819    ---------------------------
10820    -- Primitive_Names_Match --
10821    ---------------------------
10822
10823    function Primitive_Names_Match (E1, E2 : Entity_Id) return Boolean is
10824
10825       function Non_Internal_Name (E : Entity_Id) return Name_Id;
10826       --  Given an internal name, returns the corresponding non-internal name
10827
10828       ------------------------
10829       --  Non_Internal_Name --
10830       ------------------------
10831
10832       function Non_Internal_Name (E : Entity_Id) return Name_Id is
10833       begin
10834          Get_Name_String (Chars (E));
10835          Name_Len := Name_Len - 1;
10836          return Name_Find;
10837       end Non_Internal_Name;
10838
10839    --  Start of processing for Primitive_Names_Match
10840
10841    begin
10842       pragma Assert (Present (E1) and then Present (E2));
10843
10844       return Chars (E1) = Chars (E2)
10845         or else
10846            (not Is_Internal_Name (Chars (E1))
10847               and then Is_Internal_Name (Chars (E2))
10848               and then Non_Internal_Name (E2) = Chars (E1))
10849         or else
10850            (not Is_Internal_Name (Chars (E2))
10851               and then Is_Internal_Name (Chars (E1))
10852               and then Non_Internal_Name (E1) = Chars (E2))
10853         or else
10854            (Is_Predefined_Dispatching_Operation (E1)
10855               and then Is_Predefined_Dispatching_Operation (E2)
10856               and then Same_TSS (E1, E2))
10857         or else
10858            (Is_Init_Proc (E1) and then Is_Init_Proc (E2));
10859    end Primitive_Names_Match;
10860
10861    -----------------------
10862    -- Process_End_Label --
10863    -----------------------
10864
10865    procedure Process_End_Label
10866      (N   : Node_Id;
10867       Typ : Character;
10868       Ent : Entity_Id)
10869    is
10870       Loc  : Source_Ptr;
10871       Nam  : Node_Id;
10872       Scop : Entity_Id;
10873
10874       Label_Ref : Boolean;
10875       --  Set True if reference to end label itself is required
10876
10877       Endl : Node_Id;
10878       --  Gets set to the operator symbol or identifier that references the
10879       --  entity Ent. For the child unit case, this is the identifier from the
10880       --  designator. For other cases, this is simply Endl.
10881
10882       procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id);
10883       --  N is an identifier node that appears as a parent unit reference in
10884       --  the case where Ent is a child unit. This procedure generates an
10885       --  appropriate cross-reference entry. E is the corresponding entity.
10886
10887       -------------------------
10888       -- Generate_Parent_Ref --
10889       -------------------------
10890
10891       procedure Generate_Parent_Ref (N : Node_Id; E : Entity_Id) is
10892       begin
10893          --  If names do not match, something weird, skip reference
10894
10895          if Chars (E) = Chars (N) then
10896
10897             --  Generate the reference. We do NOT consider this as a reference
10898             --  for unreferenced symbol purposes.
10899
10900             Generate_Reference (E, N, 'r', Set_Ref => False, Force => True);
10901
10902             if Style_Check then
10903                Style.Check_Identifier (N, E);
10904             end if;
10905          end if;
10906       end Generate_Parent_Ref;
10907
10908    --  Start of processing for Process_End_Label
10909
10910    begin
10911       --  If no node, ignore. This happens in some error situations, and
10912       --  also for some internally generated structures where no end label
10913       --  references are required in any case.
10914
10915       if No (N) then
10916          return;
10917       end if;
10918
10919       --  Nothing to do if no End_Label, happens for internally generated
10920       --  constructs where we don't want an end label reference anyway. Also
10921       --  nothing to do if Endl is a string literal, which means there was
10922       --  some prior error (bad operator symbol)
10923
10924       Endl := End_Label (N);
10925
10926       if No (Endl) or else Nkind (Endl) = N_String_Literal then
10927          return;
10928       end if;
10929
10930       --  Reference node is not in extended main source unit
10931
10932       if not In_Extended_Main_Source_Unit (N) then
10933
10934          --  Generally we do not collect references except for the extended
10935          --  main source unit. The one exception is the 'e' entry for a
10936          --  package spec, where it is useful for a client to have the
10937          --  ending information to define scopes.
10938
10939          if Typ /= 'e' then
10940             return;
10941
10942          else
10943             Label_Ref := False;
10944
10945             --  For this case, we can ignore any parent references, but we
10946             --  need the package name itself for the 'e' entry.
10947
10948             if Nkind (Endl) = N_Designator then
10949                Endl := Identifier (Endl);
10950             end if;
10951          end if;
10952
10953       --  Reference is in extended main source unit
10954
10955       else
10956          Label_Ref := True;
10957
10958          --  For designator, generate references for the parent entries
10959
10960          if Nkind (Endl) = N_Designator then
10961
10962             --  Generate references for the prefix if the END line comes from
10963             --  source (otherwise we do not need these references) We climb the
10964             --  scope stack to find the expected entities.
10965
10966             if Comes_From_Source (Endl) then
10967                Nam  := Name (Endl);
10968                Scop := Current_Scope;
10969                while Nkind (Nam) = N_Selected_Component loop
10970                   Scop := Scope (Scop);
10971                   exit when No (Scop);
10972                   Generate_Parent_Ref (Selector_Name (Nam), Scop);
10973                   Nam := Prefix (Nam);
10974                end loop;
10975
10976                if Present (Scop) then
10977                   Generate_Parent_Ref (Nam, Scope (Scop));
10978                end if;
10979             end if;
10980
10981             Endl := Identifier (Endl);
10982          end if;
10983       end if;
10984
10985       --  If the end label is not for the given entity, then either we have
10986       --  some previous error, or this is a generic instantiation for which
10987       --  we do not need to make a cross-reference in this case anyway. In
10988       --  either case we simply ignore the call.
10989
10990       if Chars (Ent) /= Chars (Endl) then
10991          return;
10992       end if;
10993
10994       --  If label was really there, then generate a normal reference and then
10995       --  adjust the location in the end label to point past the name (which
10996       --  should almost always be the semicolon).
10997
10998       Loc := Sloc (Endl);
10999
11000       if Comes_From_Source (Endl) then
11001
11002          --  If a label reference is required, then do the style check and
11003          --  generate an l-type cross-reference entry for the label
11004
11005          if Label_Ref then
11006             if Style_Check then
11007                Style.Check_Identifier (Endl, Ent);
11008             end if;
11009
11010             Generate_Reference (Ent, Endl, 'l', Set_Ref => False);
11011          end if;
11012
11013          --  Set the location to point past the label (normally this will
11014          --  mean the semicolon immediately following the label). This is
11015          --  done for the sake of the 'e' or 't' entry generated below.
11016
11017          Get_Decoded_Name_String (Chars (Endl));
11018          Set_Sloc (Endl, Sloc (Endl) + Source_Ptr (Name_Len));
11019
11020       else
11021          --  In SPARK mode, no missing label is allowed for packages and
11022          --  subprogram bodies. Detect those cases by testing whether
11023          --  Process_End_Label was called for a body (Typ = 't') or a package.
11024
11025          if (SPARK_Mode or else Restriction_Check_Required (SPARK))
11026            and then (Typ = 't' or else Ekind (Ent) = E_Package)
11027          then
11028             Error_Msg_Node_1 := Endl;
11029             Check_SPARK_Restriction ("`END &` required", Endl, Force => True);
11030          end if;
11031       end if;
11032
11033       --  Now generate the e/t reference
11034
11035       Generate_Reference (Ent, Endl, Typ, Set_Ref => False, Force => True);
11036
11037       --  Restore Sloc, in case modified above, since we have an identifier
11038       --  and the normal Sloc should be left set in the tree.
11039
11040       Set_Sloc (Endl, Loc);
11041    end Process_End_Label;
11042
11043    ------------------------------------
11044    -- References_Generic_Formal_Type --
11045    ------------------------------------
11046
11047    function References_Generic_Formal_Type (N : Node_Id) return Boolean is
11048
11049       function Process (N : Node_Id) return Traverse_Result;
11050       --  Process one node in search for generic formal type
11051
11052       -------------
11053       -- Process --
11054       -------------
11055
11056       function Process (N : Node_Id) return Traverse_Result is
11057       begin
11058          if Nkind (N) in N_Has_Entity then
11059             declare
11060                E : constant Entity_Id := Entity (N);
11061             begin
11062                if Present (E) then
11063                   if Is_Generic_Type (E) then
11064                      return Abandon;
11065                   elsif Present (Etype (E))
11066                     and then Is_Generic_Type (Etype (E))
11067                   then
11068                      return Abandon;
11069                   end if;
11070                end if;
11071             end;
11072          end if;
11073
11074          return Atree.OK;
11075       end Process;
11076
11077       function Traverse is new Traverse_Func (Process);
11078       --  Traverse tree to look for generic type
11079
11080    begin
11081       if Inside_A_Generic then
11082          return Traverse (N) = Abandon;
11083       else
11084          return False;
11085       end if;
11086    end References_Generic_Formal_Type;
11087
11088    --------------------
11089    -- Remove_Homonym --
11090    --------------------
11091
11092    procedure Remove_Homonym (E : Entity_Id) is
11093       Prev  : Entity_Id := Empty;
11094       H     : Entity_Id;
11095
11096    begin
11097       if E = Current_Entity (E) then
11098          if Present (Homonym (E)) then
11099             Set_Current_Entity (Homonym (E));
11100          else
11101             Set_Name_Entity_Id (Chars (E), Empty);
11102          end if;
11103       else
11104          H := Current_Entity (E);
11105          while Present (H) and then H /= E loop
11106             Prev := H;
11107             H    := Homonym (H);
11108          end loop;
11109
11110          Set_Homonym (Prev, Homonym (E));
11111       end if;
11112    end Remove_Homonym;
11113
11114    ---------------------
11115    -- Rep_To_Pos_Flag --
11116    ---------------------
11117
11118    function Rep_To_Pos_Flag (E : Entity_Id; Loc : Source_Ptr) return Node_Id is
11119    begin
11120       return New_Occurrence_Of
11121                (Boolean_Literals (not Range_Checks_Suppressed (E)), Loc);
11122    end Rep_To_Pos_Flag;
11123
11124    --------------------
11125    -- Require_Entity --
11126    --------------------
11127
11128    procedure Require_Entity (N : Node_Id) is
11129    begin
11130       if Is_Entity_Name (N) and then No (Entity (N)) then
11131          if Total_Errors_Detected /= 0 then
11132             Set_Entity (N, Any_Id);
11133          else
11134             raise Program_Error;
11135          end if;
11136       end if;
11137    end Require_Entity;
11138
11139    ------------------------------
11140    -- Requires_Transient_Scope --
11141    ------------------------------
11142
11143    --  A transient scope is required when variable-sized temporaries are
11144    --  allocated in the primary or secondary stack, or when finalization
11145    --  actions must be generated before the next instruction.
11146
11147    function Requires_Transient_Scope (Id : Entity_Id) return Boolean is
11148       Typ : constant Entity_Id := Underlying_Type (Id);
11149
11150    --  Start of processing for Requires_Transient_Scope
11151
11152    begin
11153       --  This is a private type which is not completed yet. This can only
11154       --  happen in a default expression (of a formal parameter or of a
11155       --  record component). Do not expand transient scope in this case
11156
11157       if No (Typ) then
11158          return False;
11159
11160       --  Do not expand transient scope for non-existent procedure return
11161
11162       elsif Typ = Standard_Void_Type then
11163          return False;
11164
11165       --  Elementary types do not require a transient scope
11166
11167       elsif Is_Elementary_Type (Typ) then
11168          return False;
11169
11170       --  Generally, indefinite subtypes require a transient scope, since the
11171       --  back end cannot generate temporaries, since this is not a valid type
11172       --  for declaring an object. It might be possible to relax this in the
11173       --  future, e.g. by declaring the maximum possible space for the type.
11174
11175       elsif Is_Indefinite_Subtype (Typ) then
11176          return True;
11177
11178       --  Functions returning tagged types may dispatch on result so their
11179       --  returned value is allocated on the secondary stack. Controlled
11180       --  type temporaries need finalization.
11181
11182       elsif Is_Tagged_Type (Typ)
11183         or else Has_Controlled_Component (Typ)
11184       then
11185          return not Is_Value_Type (Typ);
11186
11187       --  Record type
11188
11189       elsif Is_Record_Type (Typ) then
11190          declare
11191             Comp : Entity_Id;
11192          begin
11193             Comp := First_Entity (Typ);
11194             while Present (Comp) loop
11195                if Ekind (Comp) = E_Component
11196                   and then Requires_Transient_Scope (Etype (Comp))
11197                then
11198                   return True;
11199                else
11200                   Next_Entity (Comp);
11201                end if;
11202             end loop;
11203          end;
11204
11205          return False;
11206
11207       --  String literal types never require transient scope
11208
11209       elsif Ekind (Typ) = E_String_Literal_Subtype then
11210          return False;
11211
11212       --  Array type. Note that we already know that this is a constrained
11213       --  array, since unconstrained arrays will fail the indefinite test.
11214
11215       elsif Is_Array_Type (Typ) then
11216
11217          --  If component type requires a transient scope, the array does too
11218
11219          if Requires_Transient_Scope (Component_Type (Typ)) then
11220             return True;
11221
11222          --  Otherwise, we only need a transient scope if the size depends on
11223          --  the value of one or more discriminants.
11224
11225          else
11226             return Size_Depends_On_Discriminant (Typ);
11227          end if;
11228
11229       --  All other cases do not require a transient scope
11230
11231       else
11232          return False;
11233       end if;
11234    end Requires_Transient_Scope;
11235
11236    --------------------------
11237    -- Reset_Analyzed_Flags --
11238    --------------------------
11239
11240    procedure Reset_Analyzed_Flags (N : Node_Id) is
11241
11242       function Clear_Analyzed (N : Node_Id) return Traverse_Result;
11243       --  Function used to reset Analyzed flags in tree. Note that we do
11244       --  not reset Analyzed flags in entities, since there is no need to
11245       --  reanalyze entities, and indeed, it is wrong to do so, since it
11246       --  can result in generating auxiliary stuff more than once.
11247
11248       --------------------
11249       -- Clear_Analyzed --
11250       --------------------
11251
11252       function Clear_Analyzed (N : Node_Id) return Traverse_Result is
11253       begin
11254          if not Has_Extension (N) then
11255             Set_Analyzed (N, False);
11256          end if;
11257
11258          return OK;
11259       end Clear_Analyzed;
11260
11261       procedure Reset_Analyzed is new Traverse_Proc (Clear_Analyzed);
11262
11263    --  Start of processing for Reset_Analyzed_Flags
11264
11265    begin
11266       Reset_Analyzed (N);
11267    end Reset_Analyzed_Flags;
11268
11269    ---------------------------
11270    -- Safe_To_Capture_Value --
11271    ---------------------------
11272
11273    function Safe_To_Capture_Value
11274      (N    : Node_Id;
11275       Ent  : Entity_Id;
11276       Cond : Boolean := False) return Boolean
11277    is
11278    begin
11279       --  The only entities for which we track constant values are variables
11280       --  which are not renamings, constants, out parameters, and in out
11281       --  parameters, so check if we have this case.
11282
11283       --  Note: it may seem odd to track constant values for constants, but in
11284       --  fact this routine is used for other purposes than simply capturing
11285       --  the value. In particular, the setting of Known[_Non]_Null.
11286
11287       if (Ekind (Ent) = E_Variable and then No (Renamed_Object (Ent)))
11288             or else
11289           Ekind (Ent) = E_Constant
11290             or else
11291           Ekind (Ent) = E_Out_Parameter
11292             or else
11293           Ekind (Ent) = E_In_Out_Parameter
11294       then
11295          null;
11296
11297       --  For conditionals, we also allow loop parameters and all formals,
11298       --  including in parameters.
11299
11300       elsif Cond
11301         and then
11302           (Ekind (Ent) = E_Loop_Parameter
11303              or else
11304            Ekind (Ent) = E_In_Parameter)
11305       then
11306          null;
11307
11308       --  For all other cases, not just unsafe, but impossible to capture
11309       --  Current_Value, since the above are the only entities which have
11310       --  Current_Value fields.
11311
11312       else
11313          return False;
11314       end if;
11315
11316       --  Skip if volatile or aliased, since funny things might be going on in
11317       --  these cases which we cannot necessarily track. Also skip any variable
11318       --  for which an address clause is given, or whose address is taken. Also
11319       --  never capture value of library level variables (an attempt to do so
11320       --  can occur in the case of package elaboration code).
11321
11322       if Treat_As_Volatile (Ent)
11323         or else Is_Aliased (Ent)
11324         or else Present (Address_Clause (Ent))
11325         or else Address_Taken (Ent)
11326         or else (Is_Library_Level_Entity (Ent)
11327                    and then Ekind (Ent) = E_Variable)
11328       then
11329          return False;
11330       end if;
11331
11332       --  OK, all above conditions are met. We also require that the scope of
11333       --  the reference be the same as the scope of the entity, not counting
11334       --  packages and blocks and loops.
11335
11336       declare
11337          E_Scope : constant Entity_Id := Scope (Ent);
11338          R_Scope : Entity_Id;
11339
11340       begin
11341          R_Scope := Current_Scope;
11342          while R_Scope /= Standard_Standard loop
11343             exit when R_Scope = E_Scope;
11344
11345             if not Ekind_In (R_Scope, E_Package, E_Block, E_Loop) then
11346                return False;
11347             else
11348                R_Scope := Scope (R_Scope);
11349             end if;
11350          end loop;
11351       end;
11352
11353       --  We also require that the reference does not appear in a context
11354       --  where it is not sure to be executed (i.e. a conditional context
11355       --  or an exception handler). We skip this if Cond is True, since the
11356       --  capturing of values from conditional tests handles this ok.
11357
11358       if Cond then
11359          return True;
11360       end if;
11361
11362       declare
11363          Desc : Node_Id;
11364          P    : Node_Id;
11365
11366       begin
11367          Desc := N;
11368
11369          P := Parent (N);
11370          while Present (P) loop
11371             if         Nkind (P) = N_If_Statement
11372               or else  Nkind (P) = N_Case_Statement
11373               or else (Nkind (P) in N_Short_Circuit
11374                          and then Desc = Right_Opnd (P))
11375               or else (Nkind (P) = N_Conditional_Expression
11376                          and then Desc /= First (Expressions (P)))
11377               or else  Nkind (P) = N_Exception_Handler
11378               or else  Nkind (P) = N_Selective_Accept
11379               or else  Nkind (P) = N_Conditional_Entry_Call
11380               or else  Nkind (P) = N_Timed_Entry_Call
11381               or else  Nkind (P) = N_Asynchronous_Select
11382             then
11383                return False;
11384             else
11385                Desc := P;
11386                P    := Parent (P);
11387             end if;
11388          end loop;
11389       end;
11390
11391       --  OK, looks safe to set value
11392
11393       return True;
11394    end Safe_To_Capture_Value;
11395
11396    ---------------
11397    -- Same_Name --
11398    ---------------
11399
11400    function Same_Name (N1, N2 : Node_Id) return Boolean is
11401       K1 : constant Node_Kind := Nkind (N1);
11402       K2 : constant Node_Kind := Nkind (N2);
11403
11404    begin
11405       if (K1 = N_Identifier or else K1 = N_Defining_Identifier)
11406         and then (K2 = N_Identifier or else K2 = N_Defining_Identifier)
11407       then
11408          return Chars (N1) = Chars (N2);
11409
11410       elsif (K1 = N_Selected_Component or else K1 = N_Expanded_Name)
11411         and then (K2 = N_Selected_Component or else K2 = N_Expanded_Name)
11412       then
11413          return Same_Name (Selector_Name (N1), Selector_Name (N2))
11414            and then Same_Name (Prefix (N1), Prefix (N2));
11415
11416       else
11417          return False;
11418       end if;
11419    end Same_Name;
11420
11421    -----------------
11422    -- Same_Object --
11423    -----------------
11424
11425    function Same_Object (Node1, Node2 : Node_Id) return Boolean is
11426       N1 : constant Node_Id := Original_Node (Node1);
11427       N2 : constant Node_Id := Original_Node (Node2);
11428       --  We do the tests on original nodes, since we are most interested
11429       --  in the original source, not any expansion that got in the way.
11430
11431       K1 : constant Node_Kind := Nkind (N1);
11432       K2 : constant Node_Kind := Nkind (N2);
11433
11434    begin
11435       --  First case, both are entities with same entity
11436
11437       if K1 in N_Has_Entity and then K2 in N_Has_Entity then
11438          declare
11439             EN1 : constant Entity_Id := Entity (N1);
11440             EN2 : constant Entity_Id := Entity (N2);
11441          begin
11442             if Present (EN1) and then Present (EN2)
11443               and then (Ekind_In (EN1, E_Variable, E_Constant)
11444                          or else Is_Formal (EN1))
11445               and then EN1 = EN2
11446             then
11447                return True;
11448             end if;
11449          end;
11450       end if;
11451
11452       --  Second case, selected component with same selector, same record
11453
11454       if K1 = N_Selected_Component
11455         and then K2 = N_Selected_Component
11456         and then Chars (Selector_Name (N1)) = Chars (Selector_Name (N2))
11457       then
11458          return Same_Object (Prefix (N1), Prefix (N2));
11459
11460       --  Third case, indexed component with same subscripts, same array
11461
11462       elsif K1 = N_Indexed_Component
11463         and then K2 = N_Indexed_Component
11464         and then Same_Object (Prefix (N1), Prefix (N2))
11465       then
11466          declare
11467             E1, E2 : Node_Id;
11468          begin
11469             E1 := First (Expressions (N1));
11470             E2 := First (Expressions (N2));
11471             while Present (E1) loop
11472                if not Same_Value (E1, E2) then
11473                   return False;
11474                else
11475                   Next (E1);
11476                   Next (E2);
11477                end if;
11478             end loop;
11479
11480             return True;
11481          end;
11482
11483       --  Fourth case, slice of same array with same bounds
11484
11485       elsif K1 = N_Slice
11486         and then K2 = N_Slice
11487         and then Nkind (Discrete_Range (N1)) = N_Range
11488         and then Nkind (Discrete_Range (N2)) = N_Range
11489         and then Same_Value (Low_Bound (Discrete_Range (N1)),
11490                              Low_Bound (Discrete_Range (N2)))
11491         and then Same_Value (High_Bound (Discrete_Range (N1)),
11492                              High_Bound (Discrete_Range (N2)))
11493       then
11494          return Same_Name (Prefix (N1), Prefix (N2));
11495
11496       --  All other cases, not clearly the same object
11497
11498       else
11499          return False;
11500       end if;
11501    end Same_Object;
11502
11503    ---------------
11504    -- Same_Type --
11505    ---------------
11506
11507    function Same_Type (T1, T2 : Entity_Id) return Boolean is
11508    begin
11509       if T1 = T2 then
11510          return True;
11511
11512       elsif not Is_Constrained (T1)
11513         and then not Is_Constrained (T2)
11514         and then Base_Type (T1) = Base_Type (T2)
11515       then
11516          return True;
11517
11518       --  For now don't bother with case of identical constraints, to be
11519       --  fiddled with later on perhaps (this is only used for optimization
11520       --  purposes, so it is not critical to do a best possible job)
11521
11522       else
11523          return False;
11524       end if;
11525    end Same_Type;
11526
11527    ----------------
11528    -- Same_Value --
11529    ----------------
11530
11531    function Same_Value (Node1, Node2 : Node_Id) return Boolean is
11532    begin
11533       if Compile_Time_Known_Value (Node1)
11534         and then Compile_Time_Known_Value (Node2)
11535         and then Expr_Value (Node1) = Expr_Value (Node2)
11536       then
11537          return True;
11538       elsif Same_Object (Node1, Node2) then
11539          return True;
11540       else
11541          return False;
11542       end if;
11543    end Same_Value;
11544
11545    -----------------
11546    -- Save_Actual --
11547    -----------------
11548
11549    procedure Save_Actual (N : Node_Id;  Writable : Boolean := False) is
11550    begin
11551       if Ada_Version < Ada_2012 then
11552          return;
11553
11554       elsif Is_Entity_Name (N)
11555         or else
11556           Nkind_In (N, N_Indexed_Component, N_Selected_Component, N_Slice)
11557         or else
11558           (Nkind (N) = N_Attribute_Reference
11559             and then Attribute_Name (N) = Name_Access)
11560
11561       then
11562          --  We are only interested in IN OUT parameters of inner calls
11563
11564          if not Writable
11565            or else Nkind (Parent (N)) = N_Function_Call
11566            or else Nkind (Parent (N)) in N_Op
11567          then
11568             Actuals_In_Call.Increment_Last;
11569             Actuals_In_Call.Table (Actuals_In_Call.Last) := (N, Writable);
11570          end if;
11571       end if;
11572    end Save_Actual;
11573
11574    ------------------------
11575    -- Scope_Is_Transient --
11576    ------------------------
11577
11578    function Scope_Is_Transient return Boolean is
11579    begin
11580       return Scope_Stack.Table (Scope_Stack.Last).Is_Transient;
11581    end Scope_Is_Transient;
11582
11583    ------------------
11584    -- Scope_Within --
11585    ------------------
11586
11587    function Scope_Within (Scope1, Scope2 : Entity_Id) return Boolean is
11588       Scop : Entity_Id;
11589
11590    begin
11591       Scop := Scope1;
11592       while Scop /= Standard_Standard loop
11593          Scop := Scope (Scop);
11594
11595          if Scop = Scope2 then
11596             return True;
11597          end if;
11598       end loop;
11599
11600       return False;
11601    end Scope_Within;
11602
11603    --------------------------
11604    -- Scope_Within_Or_Same --
11605    --------------------------
11606
11607    function Scope_Within_Or_Same (Scope1, Scope2 : Entity_Id) return Boolean is
11608       Scop : Entity_Id;
11609
11610    begin
11611       Scop := Scope1;
11612       while Scop /= Standard_Standard loop
11613          if Scop = Scope2 then
11614             return True;
11615          else
11616             Scop := Scope (Scop);
11617          end if;
11618       end loop;
11619
11620       return False;
11621    end Scope_Within_Or_Same;
11622
11623    --------------------
11624    -- Set_Convention --
11625    --------------------
11626
11627    procedure Set_Convention (E : Entity_Id; Val : Snames.Convention_Id) is
11628    begin
11629       Basic_Set_Convention (E, Val);
11630
11631       if Is_Type (E)
11632         and then Is_Access_Subprogram_Type (Base_Type (E))
11633         and then Has_Foreign_Convention (E)
11634       then
11635          Set_Can_Use_Internal_Rep (E, False);
11636       end if;
11637    end Set_Convention;
11638
11639    ------------------------
11640    -- Set_Current_Entity --
11641    ------------------------
11642
11643    --  The given entity is to be set as the currently visible definition
11644    --  of its associated name (i.e. the Node_Id associated with its name).
11645    --  All we have to do is to get the name from the identifier, and
11646    --  then set the associated Node_Id to point to the given entity.
11647
11648    procedure Set_Current_Entity (E : Entity_Id) is
11649    begin
11650       Set_Name_Entity_Id (Chars (E), E);
11651    end Set_Current_Entity;
11652
11653    ---------------------------
11654    -- Set_Debug_Info_Needed --
11655    ---------------------------
11656
11657    procedure Set_Debug_Info_Needed (T : Entity_Id) is
11658
11659       procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id);
11660       pragma Inline (Set_Debug_Info_Needed_If_Not_Set);
11661       --  Used to set debug info in a related node if not set already
11662
11663       --------------------------------------
11664       -- Set_Debug_Info_Needed_If_Not_Set --
11665       --------------------------------------
11666
11667       procedure Set_Debug_Info_Needed_If_Not_Set (E : Entity_Id) is
11668       begin
11669          if Present (E)
11670            and then not Needs_Debug_Info (E)
11671          then
11672             Set_Debug_Info_Needed (E);
11673
11674             --  For a private type, indicate that the full view also needs
11675             --  debug information.
11676
11677             if Is_Type (E)
11678               and then Is_Private_Type (E)
11679               and then Present (Full_View (E))
11680             then
11681                Set_Debug_Info_Needed (Full_View (E));
11682             end if;
11683          end if;
11684       end Set_Debug_Info_Needed_If_Not_Set;
11685
11686    --  Start of processing for Set_Debug_Info_Needed
11687
11688    begin
11689       --  Nothing to do if argument is Empty or has Debug_Info_Off set, which
11690       --  indicates that Debug_Info_Needed is never required for the entity.
11691
11692       if No (T)
11693         or else Debug_Info_Off (T)
11694       then
11695          return;
11696       end if;
11697
11698       --  Set flag in entity itself. Note that we will go through the following
11699       --  circuitry even if the flag is already set on T. That's intentional,
11700       --  it makes sure that the flag will be set in subsidiary entities.
11701
11702       Set_Needs_Debug_Info (T);
11703
11704       --  Set flag on subsidiary entities if not set already
11705
11706       if Is_Object (T) then
11707          Set_Debug_Info_Needed_If_Not_Set (Etype (T));
11708
11709       elsif Is_Type (T) then
11710          Set_Debug_Info_Needed_If_Not_Set (Etype (T));
11711
11712          if Is_Record_Type (T) then
11713             declare
11714                Ent : Entity_Id := First_Entity (T);
11715             begin
11716                while Present (Ent) loop
11717                   Set_Debug_Info_Needed_If_Not_Set (Ent);
11718                   Next_Entity (Ent);
11719                end loop;
11720             end;
11721
11722             --  For a class wide subtype, we also need debug information
11723             --  for the equivalent type.
11724
11725             if Ekind (T) = E_Class_Wide_Subtype then
11726                Set_Debug_Info_Needed_If_Not_Set (Equivalent_Type (T));
11727             end if;
11728
11729          elsif Is_Array_Type (T) then
11730             Set_Debug_Info_Needed_If_Not_Set (Component_Type (T));
11731
11732             declare
11733                Indx : Node_Id := First_Index (T);
11734             begin
11735                while Present (Indx) loop
11736                   Set_Debug_Info_Needed_If_Not_Set (Etype (Indx));
11737                   Indx := Next_Index (Indx);
11738                end loop;
11739             end;
11740
11741             if Is_Packed (T) then
11742                Set_Debug_Info_Needed_If_Not_Set (Packed_Array_Type (T));
11743             end if;
11744
11745          elsif Is_Access_Type (T) then
11746             Set_Debug_Info_Needed_If_Not_Set (Directly_Designated_Type (T));
11747
11748          elsif Is_Private_Type (T) then
11749             Set_Debug_Info_Needed_If_Not_Set (Full_View (T));
11750
11751          elsif Is_Protected_Type (T) then
11752             Set_Debug_Info_Needed_If_Not_Set (Corresponding_Record_Type (T));
11753          end if;
11754       end if;
11755    end Set_Debug_Info_Needed;
11756
11757    ---------------------------------
11758    -- Set_Entity_With_Style_Check --
11759    ---------------------------------
11760
11761    procedure Set_Entity_With_Style_Check (N : Node_Id; Val : Entity_Id) is
11762       Val_Actual : Entity_Id;
11763       Nod        : Node_Id;
11764
11765    begin
11766       Set_Entity (N, Val);
11767
11768       if Style_Check
11769         and then not Suppress_Style_Checks (Val)
11770         and then not In_Instance
11771       then
11772          if Nkind (N) = N_Identifier then
11773             Nod := N;
11774          elsif Nkind (N) = N_Expanded_Name then
11775             Nod := Selector_Name (N);
11776          else
11777             return;
11778          end if;
11779
11780          --  A special situation arises for derived operations, where we want
11781          --  to do the check against the parent (since the Sloc of the derived
11782          --  operation points to the derived type declaration itself).
11783
11784          Val_Actual := Val;
11785          while not Comes_From_Source (Val_Actual)
11786            and then Nkind (Val_Actual) in N_Entity
11787            and then (Ekind (Val_Actual) = E_Enumeration_Literal
11788                       or else Is_Subprogram (Val_Actual)
11789                       or else Is_Generic_Subprogram (Val_Actual))
11790            and then Present (Alias (Val_Actual))
11791          loop
11792             Val_Actual := Alias (Val_Actual);
11793          end loop;
11794
11795          --  Renaming declarations for generic actuals do not come from source,
11796          --  and have a different name from that of the entity they rename, so
11797          --  there is no style check to perform here.
11798
11799          if Chars (Nod) = Chars (Val_Actual) then
11800             Style.Check_Identifier (Nod, Val_Actual);
11801          end if;
11802       end if;
11803
11804       Set_Entity (N, Val);
11805    end Set_Entity_With_Style_Check;
11806
11807    ------------------------
11808    -- Set_Name_Entity_Id --
11809    ------------------------
11810
11811    procedure Set_Name_Entity_Id (Id : Name_Id; Val : Entity_Id) is
11812    begin
11813       Set_Name_Table_Info (Id, Int (Val));
11814    end Set_Name_Entity_Id;
11815
11816    ---------------------
11817    -- Set_Next_Actual --
11818    ---------------------
11819
11820    procedure Set_Next_Actual (Ass1_Id : Node_Id; Ass2_Id : Node_Id) is
11821    begin
11822       if Nkind (Parent (Ass1_Id)) = N_Parameter_Association then
11823          Set_First_Named_Actual (Parent (Ass1_Id), Ass2_Id);
11824       end if;
11825    end Set_Next_Actual;
11826
11827    ----------------------------------
11828    -- Set_Optimize_Alignment_Flags --
11829    ----------------------------------
11830
11831    procedure Set_Optimize_Alignment_Flags (E : Entity_Id) is
11832    begin
11833       if Optimize_Alignment = 'S' then
11834          Set_Optimize_Alignment_Space (E);
11835       elsif Optimize_Alignment = 'T' then
11836          Set_Optimize_Alignment_Time (E);
11837       end if;
11838    end Set_Optimize_Alignment_Flags;
11839
11840    -----------------------
11841    -- Set_Public_Status --
11842    -----------------------
11843
11844    procedure Set_Public_Status (Id : Entity_Id) is
11845       S : constant Entity_Id := Current_Scope;
11846
11847       function Within_HSS_Or_If (E : Entity_Id) return Boolean;
11848       --  Determines if E is defined within handled statement sequence or
11849       --  an if statement, returns True if so, False otherwise.
11850
11851       ----------------------
11852       -- Within_HSS_Or_If --
11853       ----------------------
11854
11855       function Within_HSS_Or_If (E : Entity_Id) return Boolean is
11856          N : Node_Id;
11857       begin
11858          N := Declaration_Node (E);
11859          loop
11860             N := Parent (N);
11861
11862             if No (N) then
11863                return False;
11864
11865             elsif Nkind_In (N, N_Handled_Sequence_Of_Statements,
11866                                N_If_Statement)
11867             then
11868                return True;
11869             end if;
11870          end loop;
11871       end Within_HSS_Or_If;
11872
11873    --  Start of processing for Set_Public_Status
11874
11875    begin
11876       --  Everything in the scope of Standard is public
11877
11878       if S = Standard_Standard then
11879          Set_Is_Public (Id);
11880
11881       --  Entity is definitely not public if enclosing scope is not public
11882
11883       elsif not Is_Public (S) then
11884          return;
11885
11886       --  An object or function declaration that occurs in a handled sequence
11887       --  of statements or within an if statement is the declaration for a
11888       --  temporary object or local subprogram generated by the expander. It
11889       --  never needs to be made public and furthermore, making it public can
11890       --  cause back end problems.
11891
11892       elsif Nkind_In (Parent (Id), N_Object_Declaration,
11893                                    N_Function_Specification)
11894         and then Within_HSS_Or_If (Id)
11895       then
11896          return;
11897
11898       --  Entities in public packages or records are public
11899
11900       elsif Ekind (S) = E_Package or Is_Record_Type (S) then
11901          Set_Is_Public (Id);
11902
11903       --  The bounds of an entry family declaration can generate object
11904       --  declarations that are visible to the back-end, e.g. in the
11905       --  the declaration of a composite type that contains tasks.
11906
11907       elsif Is_Concurrent_Type (S)
11908         and then not Has_Completion (S)
11909         and then Nkind (Parent (Id)) = N_Object_Declaration
11910       then
11911          Set_Is_Public (Id);
11912       end if;
11913    end Set_Public_Status;
11914
11915    -----------------------------
11916    -- Set_Referenced_Modified --
11917    -----------------------------
11918
11919    procedure Set_Referenced_Modified (N : Node_Id; Out_Param : Boolean) is
11920       Pref : Node_Id;
11921
11922    begin
11923       --  Deal with indexed or selected component where prefix is modified
11924
11925       if Nkind_In (N, N_Indexed_Component, N_Selected_Component) then
11926          Pref := Prefix (N);
11927
11928          --  If prefix is access type, then it is the designated object that is
11929          --  being modified, which means we have no entity to set the flag on.
11930
11931          if No (Etype (Pref)) or else Is_Access_Type (Etype (Pref)) then
11932             return;
11933
11934             --  Otherwise chase the prefix
11935
11936          else
11937             Set_Referenced_Modified (Pref, Out_Param);
11938          end if;
11939
11940       --  Otherwise see if we have an entity name (only other case to process)
11941
11942       elsif Is_Entity_Name (N) and then Present (Entity (N)) then
11943          Set_Referenced_As_LHS           (Entity (N), not Out_Param);
11944          Set_Referenced_As_Out_Parameter (Entity (N), Out_Param);
11945       end if;
11946    end Set_Referenced_Modified;
11947
11948    ----------------------------
11949    -- Set_Scope_Is_Transient --
11950    ----------------------------
11951
11952    procedure Set_Scope_Is_Transient (V : Boolean := True) is
11953    begin
11954       Scope_Stack.Table (Scope_Stack.Last).Is_Transient := V;
11955    end Set_Scope_Is_Transient;
11956
11957    -------------------
11958    -- Set_Size_Info --
11959    -------------------
11960
11961    procedure Set_Size_Info (T1, T2 : Entity_Id) is
11962    begin
11963       --  We copy Esize, but not RM_Size, since in general RM_Size is
11964       --  subtype specific and does not get inherited by all subtypes.
11965
11966       Set_Esize                     (T1, Esize                     (T2));
11967       Set_Has_Biased_Representation (T1, Has_Biased_Representation (T2));
11968
11969       if Is_Discrete_Or_Fixed_Point_Type (T1)
11970            and then
11971          Is_Discrete_Or_Fixed_Point_Type (T2)
11972       then
11973          Set_Is_Unsigned_Type       (T1, Is_Unsigned_Type          (T2));
11974       end if;
11975
11976       Set_Alignment                 (T1, Alignment                 (T2));
11977    end Set_Size_Info;
11978
11979    --------------------
11980    -- Static_Boolean --
11981    --------------------
11982
11983    function Static_Boolean (N : Node_Id) return Uint is
11984    begin
11985       Analyze_And_Resolve (N, Standard_Boolean);
11986
11987       if N = Error
11988         or else Error_Posted (N)
11989         or else Etype (N) = Any_Type
11990       then
11991          return No_Uint;
11992       end if;
11993
11994       if Is_Static_Expression (N) then
11995          if not Raises_Constraint_Error (N) then
11996             return Expr_Value (N);
11997          else
11998             return No_Uint;
11999          end if;
12000
12001       elsif Etype (N) = Any_Type then
12002          return No_Uint;
12003
12004       else
12005          Flag_Non_Static_Expr
12006            ("static boolean expression required here", N);
12007          return No_Uint;
12008       end if;
12009    end Static_Boolean;
12010
12011    --------------------
12012    -- Static_Integer --
12013    --------------------
12014
12015    function Static_Integer (N : Node_Id) return Uint is
12016    begin
12017       Analyze_And_Resolve (N, Any_Integer);
12018
12019       if N = Error
12020         or else Error_Posted (N)
12021         or else Etype (N) = Any_Type
12022       then
12023          return No_Uint;
12024       end if;
12025
12026       if Is_Static_Expression (N) then
12027          if not Raises_Constraint_Error (N) then
12028             return Expr_Value (N);
12029          else
12030             return No_Uint;
12031          end if;
12032
12033       elsif Etype (N) = Any_Type then
12034          return No_Uint;
12035
12036       else
12037          Flag_Non_Static_Expr
12038            ("static integer expression required here", N);
12039          return No_Uint;
12040       end if;
12041    end Static_Integer;
12042
12043    --------------------------
12044    -- Statically_Different --
12045    --------------------------
12046
12047    function Statically_Different (E1, E2 : Node_Id) return Boolean is
12048       R1 : constant Node_Id := Get_Referenced_Object (E1);
12049       R2 : constant Node_Id := Get_Referenced_Object (E2);
12050    begin
12051       return     Is_Entity_Name (R1)
12052         and then Is_Entity_Name (R2)
12053         and then Entity (R1) /= Entity (R2)
12054         and then not Is_Formal (Entity (R1))
12055         and then not Is_Formal (Entity (R2));
12056    end Statically_Different;
12057
12058    -----------------------------
12059    -- Subprogram_Access_Level --
12060    -----------------------------
12061
12062    function Subprogram_Access_Level (Subp : Entity_Id) return Uint is
12063    begin
12064       if Present (Alias (Subp)) then
12065          return Subprogram_Access_Level (Alias (Subp));
12066       else
12067          return Scope_Depth (Enclosing_Dynamic_Scope (Subp));
12068       end if;
12069    end Subprogram_Access_Level;
12070
12071    -----------------
12072    -- Trace_Scope --
12073    -----------------
12074
12075    procedure Trace_Scope (N : Node_Id; E : Entity_Id; Msg : String) is
12076    begin
12077       if Debug_Flag_W then
12078          for J in 0 .. Scope_Stack.Last loop
12079             Write_Str ("  ");
12080          end loop;
12081
12082          Write_Str (Msg);
12083          Write_Name (Chars (E));
12084          Write_Str (" from ");
12085          Write_Location (Sloc (N));
12086          Write_Eol;
12087       end if;
12088    end Trace_Scope;
12089
12090    -----------------------
12091    -- Transfer_Entities --
12092    -----------------------
12093
12094    procedure Transfer_Entities (From : Entity_Id; To : Entity_Id) is
12095       Ent : Entity_Id := First_Entity (From);
12096
12097    begin
12098       if No (Ent) then
12099          return;
12100       end if;
12101
12102       if (Last_Entity (To)) = Empty then
12103          Set_First_Entity (To, Ent);
12104       else
12105          Set_Next_Entity (Last_Entity (To), Ent);
12106       end if;
12107
12108       Set_Last_Entity (To, Last_Entity (From));
12109
12110       while Present (Ent) loop
12111          Set_Scope (Ent, To);
12112
12113          if not Is_Public (Ent) then
12114             Set_Public_Status (Ent);
12115
12116             if Is_Public (Ent)
12117               and then Ekind (Ent) = E_Record_Subtype
12118
12119             then
12120                --  The components of the propagated Itype must be public
12121                --  as well.
12122
12123                declare
12124                   Comp : Entity_Id;
12125                begin
12126                   Comp := First_Entity (Ent);
12127                   while Present (Comp) loop
12128                      Set_Is_Public (Comp);
12129                      Next_Entity (Comp);
12130                   end loop;
12131                end;
12132             end if;
12133          end if;
12134
12135          Next_Entity (Ent);
12136       end loop;
12137
12138       Set_First_Entity (From, Empty);
12139       Set_Last_Entity (From, Empty);
12140    end Transfer_Entities;
12141
12142    -----------------------
12143    -- Type_Access_Level --
12144    -----------------------
12145
12146    function Type_Access_Level (Typ : Entity_Id) return Uint is
12147       Btyp : Entity_Id;
12148
12149    begin
12150       Btyp := Base_Type (Typ);
12151
12152       --  Ada 2005 (AI-230): For most cases of anonymous access types, we
12153       --  simply use the level where the type is declared. This is true for
12154       --  stand-alone object declarations, and for anonymous access types
12155       --  associated with components the level is the same as that of the
12156       --  enclosing composite type. However, special treatment is needed for
12157       --  the cases of access parameters, return objects of an anonymous access
12158       --  type, and, in Ada 95, access discriminants of limited types.
12159
12160       if Ekind (Btyp) in Access_Kind then
12161          if Ekind (Btyp) = E_Anonymous_Access_Type then
12162
12163             --  If the type is a nonlocal anonymous access type (such as for
12164             --  an access parameter) we treat it as being declared at the
12165             --  library level to ensure that names such as X.all'access don't
12166             --  fail static accessibility checks.
12167
12168             if not Is_Local_Anonymous_Access (Typ) then
12169                return Scope_Depth (Standard_Standard);
12170
12171             --  If this is a return object, the accessibility level is that of
12172             --  the result subtype of the enclosing function. The test here is
12173             --  little complicated, because we have to account for extended
12174             --  return statements that have been rewritten as blocks, in which
12175             --  case we have to find and the Is_Return_Object attribute of the
12176             --  itype's associated object. It would be nice to find a way to
12177             --  simplify this test, but it doesn't seem worthwhile to add a new
12178             --  flag just for purposes of this test. ???
12179
12180             elsif Ekind (Scope (Btyp)) = E_Return_Statement
12181               or else
12182                 (Is_Itype (Btyp)
12183                   and then Nkind (Associated_Node_For_Itype (Btyp)) =
12184                              N_Object_Declaration
12185                   and then Is_Return_Object
12186                              (Defining_Identifier
12187                                 (Associated_Node_For_Itype (Btyp))))
12188             then
12189                declare
12190                   Scop : Entity_Id;
12191
12192                begin
12193                   Scop := Scope (Scope (Btyp));
12194                   while Present (Scop) loop
12195                      exit when Ekind (Scop) = E_Function;
12196                      Scop := Scope (Scop);
12197                   end loop;
12198
12199                   --  Treat the return object's type as having the level of the
12200                   --  function's result subtype (as per RM05-6.5(5.3/2)).
12201
12202                   return Type_Access_Level (Etype (Scop));
12203                end;
12204             end if;
12205          end if;
12206
12207          Btyp := Root_Type (Btyp);
12208
12209          --  The accessibility level of anonymous access types associated with
12210          --  discriminants is that of the current instance of the type, and
12211          --  that's deeper than the type itself (AARM 3.10.2 (12.3.21)).
12212
12213          --  AI-402: access discriminants have accessibility based on the
12214          --  object rather than the type in Ada 2005, so the above paragraph
12215          --  doesn't apply.
12216
12217          --  ??? Needs completion with rules from AI-416
12218
12219          if Ada_Version <= Ada_95
12220            and then Ekind (Typ) = E_Anonymous_Access_Type
12221            and then Present (Associated_Node_For_Itype (Typ))
12222            and then Nkind (Associated_Node_For_Itype (Typ)) =
12223                                                  N_Discriminant_Specification
12224          then
12225             return Scope_Depth (Enclosing_Dynamic_Scope (Btyp)) + 1;
12226          end if;
12227       end if;
12228
12229       return Scope_Depth (Enclosing_Dynamic_Scope (Btyp));
12230    end Type_Access_Level;
12231
12232    ----------------------------
12233    -- Unique_Defining_Entity --
12234    ----------------------------
12235
12236    function Unique_Defining_Entity (N : Node_Id) return Entity_Id is
12237    begin
12238       case Nkind (N) is
12239          when N_Package_Body =>
12240             return Corresponding_Spec (N);
12241
12242          when N_Subprogram_Body =>
12243             if Acts_As_Spec (N) then
12244                return Defining_Entity (N);
12245             else
12246                return Corresponding_Spec (N);
12247             end if;
12248
12249          when others =>
12250             return Defining_Entity (N);
12251       end case;
12252    end Unique_Defining_Entity;
12253
12254    -----------------
12255    -- Unique_Name --
12256    -----------------
12257
12258    function Unique_Name (E : Entity_Id) return String is
12259       Name : constant String := Get_Name_String (Chars (E));
12260    begin
12261       if Has_Fully_Qualified_Name (E)
12262         or else E = Standard_Standard
12263       then
12264          return Name;
12265       else
12266          return Unique_Name (Scope (E)) & "__" & Name;
12267       end if;
12268    end Unique_Name;
12269
12270    --------------------------
12271    -- Unit_Declaration_Node --
12272    --------------------------
12273
12274    function Unit_Declaration_Node (Unit_Id : Entity_Id) return Node_Id is
12275       N : Node_Id := Parent (Unit_Id);
12276
12277    begin
12278       --  Predefined operators do not have a full function declaration
12279
12280       if Ekind (Unit_Id) = E_Operator then
12281          return N;
12282       end if;
12283
12284       --  Isn't there some better way to express the following ???
12285
12286       while Nkind (N) /= N_Abstract_Subprogram_Declaration
12287         and then Nkind (N) /= N_Formal_Package_Declaration
12288         and then Nkind (N) /= N_Function_Instantiation
12289         and then Nkind (N) /= N_Generic_Package_Declaration
12290         and then Nkind (N) /= N_Generic_Subprogram_Declaration
12291         and then Nkind (N) /= N_Package_Declaration
12292         and then Nkind (N) /= N_Package_Body
12293         and then Nkind (N) /= N_Package_Instantiation
12294         and then Nkind (N) /= N_Package_Renaming_Declaration
12295         and then Nkind (N) /= N_Procedure_Instantiation
12296         and then Nkind (N) /= N_Protected_Body
12297         and then Nkind (N) /= N_Subprogram_Declaration
12298         and then Nkind (N) /= N_Subprogram_Body
12299         and then Nkind (N) /= N_Subprogram_Body_Stub
12300         and then Nkind (N) /= N_Subprogram_Renaming_Declaration
12301         and then Nkind (N) /= N_Task_Body
12302         and then Nkind (N) /= N_Task_Type_Declaration
12303         and then Nkind (N) not in N_Formal_Subprogram_Declaration
12304         and then Nkind (N) not in N_Generic_Renaming_Declaration
12305       loop
12306          N := Parent (N);
12307          pragma Assert (Present (N));
12308       end loop;
12309
12310       return N;
12311    end Unit_Declaration_Node;
12312
12313    ---------------------
12314    -- Unit_Is_Visible --
12315    ---------------------
12316
12317    function Unit_Is_Visible (U : Entity_Id) return Boolean is
12318       Curr        : constant Node_Id   := Cunit (Current_Sem_Unit);
12319       Curr_Entity : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
12320
12321       function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean;
12322       --  For a child unit, check whether unit appears in a with_clause
12323       --  of a parent.
12324
12325       function Unit_In_Context (Comp_Unit : Node_Id) return Boolean;
12326       --  Scan the context clause of one compilation unit looking for a
12327       --  with_clause for the unit in question.
12328
12329       ----------------------------
12330       -- Unit_In_Parent_Context --
12331       ----------------------------
12332
12333       function Unit_In_Parent_Context (Par_Unit : Node_Id) return Boolean is
12334       begin
12335          if Unit_In_Context (Par_Unit) then
12336             return True;
12337
12338          elsif Is_Child_Unit (Defining_Entity (Unit (Par_Unit))) then
12339             return Unit_In_Parent_Context (Parent_Spec (Unit (Par_Unit)));
12340
12341          else
12342             return False;
12343          end if;
12344       end Unit_In_Parent_Context;
12345
12346       ---------------------
12347       -- Unit_In_Context --
12348       ---------------------
12349
12350       function Unit_In_Context (Comp_Unit : Node_Id) return Boolean is
12351          Clause : Node_Id;
12352
12353       begin
12354          Clause := First (Context_Items (Comp_Unit));
12355          while Present (Clause) loop
12356             if Nkind (Clause) = N_With_Clause then
12357                if Library_Unit (Clause) = U then
12358                   return True;
12359
12360                --  The with_clause may denote a renaming of the unit we are
12361                --  looking for, eg. Text_IO which renames Ada.Text_IO.
12362
12363                elsif
12364                  Renamed_Entity (Entity (Name (Clause))) =
12365                                                 Defining_Entity (Unit (U))
12366                then
12367                   return True;
12368                end if;
12369             end if;
12370
12371             Next (Clause);
12372          end loop;
12373
12374          return False;
12375       end Unit_In_Context;
12376
12377    --  Start of processing for Unit_Is_Visible
12378
12379    begin
12380       --  The currrent unit is directly visible.
12381
12382       if Curr = U then
12383          return True;
12384
12385       elsif Unit_In_Context (Curr) then
12386          return True;
12387
12388       --  If the current unit is a body, check the context of the spec.
12389
12390       elsif Nkind (Unit (Curr)) = N_Package_Body
12391         or else
12392           (Nkind (Unit (Curr)) = N_Subprogram_Body
12393             and then not Acts_As_Spec (Unit (Curr)))
12394       then
12395          if Unit_In_Context (Library_Unit (Curr)) then
12396             return True;
12397          end if;
12398       end if;
12399
12400       --  If the spec is a child unit, examine the parents.
12401
12402       if Is_Child_Unit (Curr_Entity) then
12403          if Nkind (Unit (Curr)) in N_Unit_Body then
12404             return
12405               Unit_In_Parent_Context
12406                 (Parent_Spec (Unit (Library_Unit (Curr))));
12407          else
12408             return Unit_In_Parent_Context (Parent_Spec (Unit (Curr)));
12409          end if;
12410
12411       else
12412          return False;
12413       end if;
12414    end Unit_Is_Visible;
12415
12416    ------------------------------
12417    -- Universal_Interpretation --
12418    ------------------------------
12419
12420    function Universal_Interpretation (Opnd : Node_Id) return Entity_Id is
12421       Index : Interp_Index;
12422       It    : Interp;
12423
12424    begin
12425       --  The argument may be a formal parameter of an operator or subprogram
12426       --  with multiple interpretations, or else an expression for an actual.
12427
12428       if Nkind (Opnd) = N_Defining_Identifier
12429         or else not Is_Overloaded (Opnd)
12430       then
12431          if Etype (Opnd) = Universal_Integer
12432            or else Etype (Opnd) = Universal_Real
12433          then
12434             return Etype (Opnd);
12435          else
12436             return Empty;
12437          end if;
12438
12439       else
12440          Get_First_Interp (Opnd, Index, It);
12441          while Present (It.Typ) loop
12442             if It.Typ = Universal_Integer
12443               or else It.Typ = Universal_Real
12444             then
12445                return It.Typ;
12446             end if;
12447
12448             Get_Next_Interp (Index, It);
12449          end loop;
12450
12451          return Empty;
12452       end if;
12453    end Universal_Interpretation;
12454
12455    ---------------
12456    -- Unqualify --
12457    ---------------
12458
12459    function Unqualify (Expr : Node_Id) return Node_Id is
12460    begin
12461       --  Recurse to handle unlikely case of multiple levels of qualification
12462
12463       if Nkind (Expr) = N_Qualified_Expression then
12464          return Unqualify (Expression (Expr));
12465
12466       --  Normal case, not a qualified expression
12467
12468       else
12469          return Expr;
12470       end if;
12471    end Unqualify;
12472
12473    -----------------------
12474    -- Visible_Ancestors --
12475    -----------------------
12476
12477    function Visible_Ancestors (Typ : Entity_Id) return Elist_Id is
12478       List_1 : Elist_Id;
12479       List_2 : Elist_Id;
12480       Elmt   : Elmt_Id;
12481
12482    begin
12483       pragma Assert (Is_Record_Type (Typ)
12484         and then Is_Tagged_Type (Typ));
12485
12486       --  Collect all the parents and progenitors of Typ. If the full-view of
12487       --  private parents and progenitors is available then it is used to
12488       --  generate the list of visible ancestors; otherwise their partial
12489       --  view is added to the resulting list.
12490
12491       Collect_Parents
12492         (T               => Typ,
12493          List            => List_1,
12494          Use_Full_View   => True);
12495
12496       Collect_Interfaces
12497         (T               => Typ,
12498          Ifaces_List     => List_2,
12499          Exclude_Parents => True,
12500          Use_Full_View   => True);
12501
12502       --  Join the two lists. Avoid duplications because an interface may
12503       --  simultaneously be parent and progenitor of a type.
12504
12505       Elmt := First_Elmt (List_2);
12506       while Present (Elmt) loop
12507          Append_Unique_Elmt (Node (Elmt), List_1);
12508          Next_Elmt (Elmt);
12509       end loop;
12510
12511       return List_1;
12512    end Visible_Ancestors;
12513
12514    ----------------------
12515    -- Within_Init_Proc --
12516    ----------------------
12517
12518    function Within_Init_Proc return Boolean is
12519       S : Entity_Id;
12520
12521    begin
12522       S := Current_Scope;
12523       while not Is_Overloadable (S) loop
12524          if S = Standard_Standard then
12525             return False;
12526          else
12527             S := Scope (S);
12528          end if;
12529       end loop;
12530
12531       return Is_Init_Proc (S);
12532    end Within_Init_Proc;
12533
12534    ----------------
12535    -- Wrong_Type --
12536    ----------------
12537
12538    procedure Wrong_Type (Expr : Node_Id; Expected_Type : Entity_Id) is
12539       Found_Type : constant Entity_Id := First_Subtype (Etype (Expr));
12540       Expec_Type : constant Entity_Id := First_Subtype (Expected_Type);
12541
12542       Matching_Field : Entity_Id;
12543       --  Entity to give a more precise suggestion on how to write a one-
12544       --  element positional aggregate.
12545
12546       function Has_One_Matching_Field return Boolean;
12547       --  Determines if Expec_Type is a record type with a single component or
12548       --  discriminant whose type matches the found type or is one dimensional
12549       --  array whose component type matches the found type.
12550
12551       ----------------------------
12552       -- Has_One_Matching_Field --
12553       ----------------------------
12554
12555       function Has_One_Matching_Field return Boolean is
12556          E : Entity_Id;
12557
12558       begin
12559          Matching_Field := Empty;
12560
12561          if Is_Array_Type (Expec_Type)
12562            and then Number_Dimensions (Expec_Type) = 1
12563            and then
12564              Covers (Etype (Component_Type (Expec_Type)), Found_Type)
12565          then
12566             --  Use type name if available. This excludes multidimensional
12567             --  arrays and anonymous arrays.
12568
12569             if Comes_From_Source (Expec_Type) then
12570                Matching_Field := Expec_Type;
12571
12572             --  For an assignment, use name of target.
12573
12574             elsif Nkind (Parent (Expr)) = N_Assignment_Statement
12575               and then Is_Entity_Name (Name (Parent (Expr)))
12576             then
12577                Matching_Field := Entity (Name (Parent (Expr)));
12578             end if;
12579
12580             return True;
12581
12582          elsif not Is_Record_Type (Expec_Type) then
12583             return False;
12584
12585          else
12586             E := First_Entity (Expec_Type);
12587             loop
12588                if No (E) then
12589                   return False;
12590
12591                elsif (Ekind (E) /= E_Discriminant
12592                        and then Ekind (E) /= E_Component)
12593                  or else (Chars (E) = Name_uTag
12594                            or else Chars (E) = Name_uParent)
12595                then
12596                   Next_Entity (E);
12597
12598                else
12599                   exit;
12600                end if;
12601             end loop;
12602
12603             if not Covers (Etype (E), Found_Type) then
12604                return False;
12605
12606             elsif Present (Next_Entity (E)) then
12607                return False;
12608
12609             else
12610                Matching_Field := E;
12611                return True;
12612             end if;
12613          end if;
12614       end Has_One_Matching_Field;
12615
12616    --  Start of processing for Wrong_Type
12617
12618    begin
12619       --  Don't output message if either type is Any_Type, or if a message
12620       --  has already been posted for this node. We need to do the latter
12621       --  check explicitly (it is ordinarily done in Errout), because we
12622       --  are using ! to force the output of the error messages.
12623
12624       if Expec_Type = Any_Type
12625         or else Found_Type = Any_Type
12626         or else Error_Posted (Expr)
12627       then
12628          return;
12629
12630       --  In  an instance, there is an ongoing problem with completion of
12631       --  type derived from private types. Their structure is what Gigi
12632       --  expects, but the  Etype is the parent type rather than the
12633       --  derived private type itself. Do not flag error in this case. The
12634       --  private completion is an entity without a parent, like an Itype.
12635       --  Similarly, full and partial views may be incorrect in the instance.
12636       --  There is no simple way to insure that it is consistent ???
12637
12638       elsif In_Instance then
12639          if Etype (Etype (Expr)) = Etype (Expected_Type)
12640            and then
12641              (Has_Private_Declaration (Expected_Type)
12642                or else Has_Private_Declaration (Etype (Expr)))
12643            and then No (Parent (Expected_Type))
12644          then
12645             return;
12646          end if;
12647       end if;
12648
12649       --  An interesting special check. If the expression is parenthesized
12650       --  and its type corresponds to the type of the sole component of the
12651       --  expected record type, or to the component type of the expected one
12652       --  dimensional array type, then assume we have a bad aggregate attempt.
12653
12654       if Nkind (Expr) in N_Subexpr
12655         and then Paren_Count (Expr) /= 0
12656         and then Has_One_Matching_Field
12657       then
12658          Error_Msg_N ("positional aggregate cannot have one component", Expr);
12659          if Present (Matching_Field) then
12660             if Is_Array_Type (Expec_Type) then
12661                Error_Msg_NE
12662                  ("\write instead `&''First ='> ...`", Expr, Matching_Field);
12663
12664             else
12665                Error_Msg_NE
12666                  ("\write instead `& ='> ...`", Expr, Matching_Field);
12667             end if;
12668          end if;
12669
12670       --  Another special check, if we are looking for a pool-specific access
12671       --  type and we found an E_Access_Attribute_Type, then we have the case
12672       --  of an Access attribute being used in a context which needs a pool-
12673       --  specific type, which is never allowed. The one extra check we make
12674       --  is that the expected designated type covers the Found_Type.
12675
12676       elsif Is_Access_Type (Expec_Type)
12677         and then Ekind (Found_Type) = E_Access_Attribute_Type
12678         and then Ekind (Base_Type (Expec_Type)) /= E_General_Access_Type
12679         and then Ekind (Base_Type (Expec_Type)) /= E_Anonymous_Access_Type
12680         and then Covers
12681           (Designated_Type (Expec_Type), Designated_Type (Found_Type))
12682       then
12683          Error_Msg_N -- CODEFIX
12684            ("result must be general access type!", Expr);
12685          Error_Msg_NE -- CODEFIX
12686            ("add ALL to }!", Expr, Expec_Type);
12687
12688       --  Another special check, if the expected type is an integer type,
12689       --  but the expression is of type System.Address, and the parent is
12690       --  an addition or subtraction operation whose left operand is the
12691       --  expression in question and whose right operand is of an integral
12692       --  type, then this is an attempt at address arithmetic, so give
12693       --  appropriate message.
12694
12695       elsif Is_Integer_Type (Expec_Type)
12696         and then Is_RTE (Found_Type, RE_Address)
12697         and then (Nkind (Parent (Expr)) = N_Op_Add
12698                     or else
12699                   Nkind (Parent (Expr)) = N_Op_Subtract)
12700         and then Expr = Left_Opnd (Parent (Expr))
12701         and then Is_Integer_Type (Etype (Right_Opnd (Parent (Expr))))
12702       then
12703          Error_Msg_N
12704            ("address arithmetic not predefined in package System",
12705             Parent (Expr));
12706          Error_Msg_N
12707            ("\possible missing with/use of System.Storage_Elements",
12708             Parent (Expr));
12709          return;
12710
12711       --  If the expected type is an anonymous access type, as for access
12712       --  parameters and discriminants, the error is on the designated types.
12713
12714       elsif Ekind (Expec_Type) = E_Anonymous_Access_Type then
12715          if Comes_From_Source (Expec_Type) then
12716             Error_Msg_NE ("expected}!", Expr, Expec_Type);
12717          else
12718             Error_Msg_NE
12719               ("expected an access type with designated}",
12720                  Expr, Designated_Type (Expec_Type));
12721          end if;
12722
12723          if Is_Access_Type (Found_Type)
12724            and then not Comes_From_Source (Found_Type)
12725          then
12726             Error_Msg_NE
12727               ("\\found an access type with designated}!",
12728                 Expr, Designated_Type (Found_Type));
12729          else
12730             if From_With_Type (Found_Type) then
12731                Error_Msg_NE ("\\found incomplete}!", Expr, Found_Type);
12732                Error_Msg_Qual_Level := 99;
12733                Error_Msg_NE -- CODEFIX
12734                  ("\\missing `WITH &;", Expr, Scope (Found_Type));
12735                Error_Msg_Qual_Level := 0;
12736             else
12737                Error_Msg_NE ("found}!", Expr, Found_Type);
12738             end if;
12739          end if;
12740
12741       --  Normal case of one type found, some other type expected
12742
12743       else
12744          --  If the names of the two types are the same, see if some number
12745          --  of levels of qualification will help. Don't try more than three
12746          --  levels, and if we get to standard, it's no use (and probably
12747          --  represents an error in the compiler) Also do not bother with
12748          --  internal scope names.
12749
12750          declare
12751             Expec_Scope : Entity_Id;
12752             Found_Scope : Entity_Id;
12753
12754          begin
12755             Expec_Scope := Expec_Type;
12756             Found_Scope := Found_Type;
12757
12758             for Levels in Int range 0 .. 3 loop
12759                if Chars (Expec_Scope) /= Chars (Found_Scope) then
12760                   Error_Msg_Qual_Level := Levels;
12761                   exit;
12762                end if;
12763
12764                Expec_Scope := Scope (Expec_Scope);
12765                Found_Scope := Scope (Found_Scope);
12766
12767                exit when Expec_Scope = Standard_Standard
12768                  or else Found_Scope = Standard_Standard
12769                  or else not Comes_From_Source (Expec_Scope)
12770                  or else not Comes_From_Source (Found_Scope);
12771             end loop;
12772          end;
12773
12774          if Is_Record_Type (Expec_Type)
12775            and then Present (Corresponding_Remote_Type (Expec_Type))
12776          then
12777             Error_Msg_NE ("expected}!", Expr,
12778                           Corresponding_Remote_Type (Expec_Type));
12779          else
12780             Error_Msg_NE ("expected}!", Expr, Expec_Type);
12781          end if;
12782
12783          if Is_Entity_Name (Expr)
12784            and then Is_Package_Or_Generic_Package (Entity (Expr))
12785          then
12786             Error_Msg_N ("\\found package name!", Expr);
12787
12788          elsif Is_Entity_Name (Expr)
12789            and then
12790              (Ekind (Entity (Expr)) = E_Procedure
12791                 or else
12792               Ekind (Entity (Expr)) = E_Generic_Procedure)
12793          then
12794             if Ekind (Expec_Type) = E_Access_Subprogram_Type then
12795                Error_Msg_N
12796                  ("found procedure name, possibly missing Access attribute!",
12797                    Expr);
12798             else
12799                Error_Msg_N
12800                  ("\\found procedure name instead of function!", Expr);
12801             end if;
12802
12803          elsif Nkind (Expr) = N_Function_Call
12804            and then Ekind (Expec_Type) = E_Access_Subprogram_Type
12805            and then Etype (Designated_Type (Expec_Type)) = Etype (Expr)
12806            and then No (Parameter_Associations (Expr))
12807          then
12808             Error_Msg_N
12809               ("found function name, possibly missing Access attribute!",
12810                Expr);
12811
12812          --  Catch common error: a prefix or infix operator which is not
12813          --  directly visible because the type isn't.
12814
12815          elsif Nkind (Expr) in N_Op
12816             and then Is_Overloaded (Expr)
12817             and then not Is_Immediately_Visible (Expec_Type)
12818             and then not Is_Potentially_Use_Visible (Expec_Type)
12819             and then not In_Use (Expec_Type)
12820             and then Has_Compatible_Type (Right_Opnd (Expr), Expec_Type)
12821          then
12822             Error_Msg_N
12823               ("operator of the type is not directly visible!", Expr);
12824
12825          elsif Ekind (Found_Type) = E_Void
12826            and then Present (Parent (Found_Type))
12827            and then Nkind (Parent (Found_Type)) = N_Full_Type_Declaration
12828          then
12829             Error_Msg_NE ("\\found premature usage of}!", Expr, Found_Type);
12830
12831          else
12832             Error_Msg_NE ("\\found}!", Expr, Found_Type);
12833          end if;
12834
12835          --  A special check for cases like M1 and M2 = 0 where M1 and M2 are
12836          --  of the same modular type, and (M1 and M2) = 0 was intended.
12837
12838          if Expec_Type = Standard_Boolean
12839            and then Is_Modular_Integer_Type (Found_Type)
12840            and then Nkind_In (Parent (Expr), N_Op_And, N_Op_Or, N_Op_Xor)
12841            and then Nkind (Right_Opnd (Parent (Expr))) in N_Op_Compare
12842          then
12843             declare
12844                Op : constant Node_Id := Right_Opnd (Parent (Expr));
12845                L  : constant Node_Id := Left_Opnd (Op);
12846                R  : constant Node_Id := Right_Opnd (Op);
12847             begin
12848                --  The case for the message is when the left operand of the
12849                --  comparison is the same modular type, or when it is an
12850                --  integer literal (or other universal integer expression),
12851                --  which would have been typed as the modular type if the
12852                --  parens had been there.
12853
12854                if (Etype (L) = Found_Type
12855                      or else
12856                    Etype (L) = Universal_Integer)
12857                  and then Is_Integer_Type (Etype (R))
12858                then
12859                   Error_Msg_N
12860                     ("\\possible missing parens for modular operation", Expr);
12861                end if;
12862             end;
12863          end if;
12864
12865          --  Reset error message qualification indication
12866
12867          Error_Msg_Qual_Level := 0;
12868       end if;
12869    end Wrong_Type;
12870
12871 end Sem_Util;