OSDN Git Service

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