OSDN Git Service

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