OSDN Git Service

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