OSDN Git Service

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