OSDN Git Service

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