OSDN Git Service

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