OSDN Git Service

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