OSDN Git Service

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