OSDN Git Service

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