OSDN Git Service

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