OSDN Git Service

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