OSDN Git Service

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