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