OSDN Git Service

2010-06-17 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / freeze.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               F R E E Z E                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17 --                                                                          --
18 -- You should have received a copy of the GNU General Public License along  --
19 -- with this program; see file COPYING3.  If not see                        --
20 -- <http://www.gnu.org/licenses/>.                                          --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Debug;    use Debug;
29 with Einfo;    use Einfo;
30 with Elists;   use Elists;
31 with Errout;   use Errout;
32 with Exp_Ch3;  use Exp_Ch3;
33 with Exp_Ch7;  use Exp_Ch7;
34 with Exp_Disp; use Exp_Disp;
35 with Exp_Pakd; use Exp_Pakd;
36 with Exp_Util; use Exp_Util;
37 with Exp_Tss;  use Exp_Tss;
38 with Layout;   use Layout;
39 with Namet;    use Namet;
40 with Nlists;   use Nlists;
41 with Nmake;    use Nmake;
42 with Opt;      use Opt;
43 with Restrict; use Restrict;
44 with Rident;   use Rident;
45 with Sem;      use Sem;
46 with Sem_Aux;  use Sem_Aux;
47 with Sem_Cat;  use Sem_Cat;
48 with Sem_Ch6;  use Sem_Ch6;
49 with Sem_Ch7;  use Sem_Ch7;
50 with Sem_Ch8;  use Sem_Ch8;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Mech; use Sem_Mech;
54 with Sem_Prag; use Sem_Prag;
55 with Sem_Res;  use Sem_Res;
56 with Sem_Util; use Sem_Util;
57 with Sinfo;    use Sinfo;
58 with Snames;   use Snames;
59 with Stand;    use Stand;
60 with Targparm; use Targparm;
61 with Tbuild;   use Tbuild;
62 with Ttypes;   use Ttypes;
63 with Uintp;    use Uintp;
64 with Urealp;   use Urealp;
65
66 package body Freeze is
67
68    -----------------------
69    -- Local Subprograms --
70    -----------------------
71
72    procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
73    --  Typ is a type that is being frozen. If no size clause is given,
74    --  but a default Esize has been computed, then this default Esize is
75    --  adjusted up if necessary to be consistent with a given alignment,
76    --  but never to a value greater than Long_Long_Integer'Size. This
77    --  is used for all discrete types and for fixed-point types.
78
79    procedure Build_And_Analyze_Renamed_Body
80      (Decl  : Node_Id;
81       New_S : Entity_Id;
82       After : in out Node_Id);
83    --  Build body for a renaming declaration, insert in tree and analyze
84
85    procedure Check_Address_Clause (E : Entity_Id);
86    --  Apply legality checks to address clauses for object declarations,
87    --  at the point the object is frozen.
88
89    procedure Check_Strict_Alignment (E : Entity_Id);
90    --  E is a base type. If E is tagged or has a component that is aliased
91    --  or tagged or contains something this is aliased or tagged, set
92    --  Strict_Alignment.
93
94    procedure Check_Unsigned_Type (E : Entity_Id);
95    pragma Inline (Check_Unsigned_Type);
96    --  If E is a fixed-point or discrete type, then all the necessary work
97    --  to freeze it is completed except for possible setting of the flag
98    --  Is_Unsigned_Type, which is done by this procedure. The call has no
99    --  effect if the entity E is not a discrete or fixed-point type.
100
101    procedure Freeze_And_Append
102      (Ent    : Entity_Id;
103       Loc    : Source_Ptr;
104       Result : in out List_Id);
105    --  Freezes Ent using Freeze_Entity, and appends the resulting list of
106    --  nodes to Result, modifying Result from No_List if necessary.
107
108    procedure Freeze_Enumeration_Type (Typ : Entity_Id);
109    --  Freeze enumeration type. The Esize field is set as processing
110    --  proceeds (i.e. set by default when the type is declared and then
111    --  adjusted by rep clauses. What this procedure does is to make sure
112    --  that if a foreign convention is specified, and no specific size
113    --  is given, then the size must be at least Integer'Size.
114
115    procedure Freeze_Static_Object (E : Entity_Id);
116    --  If an object is frozen which has Is_Statically_Allocated set, then
117    --  all referenced types must also be marked with this flag. This routine
118    --  is in charge of meeting this requirement for the object entity E.
119
120    procedure Freeze_Subprogram (E : Entity_Id);
121    --  Perform freezing actions for a subprogram (create extra formals,
122    --  and set proper default mechanism values). Note that this routine
123    --  is not called for internal subprograms, for which neither of these
124    --  actions is needed (or desirable, we do not want for example to have
125    --  these extra formals present in initialization procedures, where they
126    --  would serve no purpose). In this call E is either a subprogram or
127    --  a subprogram type (i.e. an access to a subprogram).
128
129    function Is_Fully_Defined (T : Entity_Id) return Boolean;
130    --  True if T is not private and has no private components, or has a full
131    --  view. Used to determine whether the designated type of an access type
132    --  should be frozen when the access type is frozen. This is done when an
133    --  allocator is frozen, or an expression that may involve attributes of
134    --  the designated type. Otherwise freezing the access type does not freeze
135    --  the designated type.
136
137    procedure Process_Default_Expressions
138      (E     : Entity_Id;
139       After : in out Node_Id);
140    --  This procedure is called for each subprogram to complete processing
141    --  of default expressions at the point where all types are known to be
142    --  frozen. The expressions must be analyzed in full, to make sure that
143    --  all error processing is done (they have only been pre-analyzed). If
144    --  the expression is not an entity or literal, its analysis may generate
145    --  code which must not be executed. In that case we build a function
146    --  body to hold that code. This wrapper function serves no other purpose
147    --  (it used to be called to evaluate the default, but now the default is
148    --  inlined at each point of call).
149
150    procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
151    --  Typ is a record or array type that is being frozen. This routine
152    --  sets the default component alignment from the scope stack values
153    --  if the alignment is otherwise not specified.
154
155    procedure Check_Debug_Info_Needed (T : Entity_Id);
156    --  As each entity is frozen, this routine is called to deal with the
157    --  setting of Debug_Info_Needed for the entity. This flag is set if
158    --  the entity comes from source, or if we are in Debug_Generated_Code
159    --  mode or if the -gnatdV debug flag is set. However, it never sets
160    --  the flag if Debug_Info_Off is set. This procedure also ensures that
161    --  subsidiary entities have the flag set as required.
162
163    procedure Undelay_Type (T : Entity_Id);
164    --  T is a type of a component that we know to be an Itype.
165    --  We don't want this to have a Freeze_Node, so ensure it doesn't.
166    --  Do the same for any Full_View or Corresponding_Record_Type.
167
168    procedure Warn_Overlay
169      (Expr : Node_Id;
170       Typ  : Entity_Id;
171       Nam  : Node_Id);
172    --  Expr is the expression for an address clause for entity Nam whose type
173    --  is Typ. If Typ has a default initialization, and there is no explicit
174    --  initialization in the source declaration, check whether the address
175    --  clause might cause overlaying of an entity, and emit a warning on the
176    --  side effect that the initialization will cause.
177
178    -------------------------------
179    -- Adjust_Esize_For_Alignment --
180    -------------------------------
181
182    procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
183       Align : Uint;
184
185    begin
186       if Known_Esize (Typ) and then Known_Alignment (Typ) then
187          Align := Alignment_In_Bits (Typ);
188
189          if Align > Esize (Typ)
190            and then Align <= Standard_Long_Long_Integer_Size
191          then
192             Set_Esize (Typ, Align);
193          end if;
194       end if;
195    end Adjust_Esize_For_Alignment;
196
197    ------------------------------------
198    -- Build_And_Analyze_Renamed_Body --
199    ------------------------------------
200
201    procedure Build_And_Analyze_Renamed_Body
202      (Decl  : Node_Id;
203       New_S : Entity_Id;
204       After : in out Node_Id)
205    is
206       Body_Node : constant Node_Id := Build_Renamed_Body (Decl, New_S);
207    begin
208       Insert_After (After, Body_Node);
209       Mark_Rewrite_Insertion (Body_Node);
210       Analyze (Body_Node);
211       After := Body_Node;
212    end Build_And_Analyze_Renamed_Body;
213
214    ------------------------
215    -- Build_Renamed_Body --
216    ------------------------
217
218    function Build_Renamed_Body
219      (Decl  : Node_Id;
220       New_S : Entity_Id) return Node_Id
221    is
222       Loc : constant Source_Ptr := Sloc (New_S);
223       --  We use for the source location of the renamed body, the location
224       --  of the spec entity. It might seem more natural to use the location
225       --  of the renaming declaration itself, but that would be wrong, since
226       --  then the body we create would look as though it was created far
227       --  too late, and this could cause problems with elaboration order
228       --  analysis, particularly in connection with instantiations.
229
230       N          : constant Node_Id := Unit_Declaration_Node (New_S);
231       Nam        : constant Node_Id := Name (N);
232       Old_S      : Entity_Id;
233       Spec       : constant Node_Id := New_Copy_Tree (Specification (Decl));
234       Actuals    : List_Id := No_List;
235       Call_Node  : Node_Id;
236       Call_Name  : Node_Id;
237       Body_Node  : Node_Id;
238       Formal     : Entity_Id;
239       O_Formal   : Entity_Id;
240       Param_Spec : Node_Id;
241
242       Pref : Node_Id := Empty;
243       --  If the renamed entity is a primitive operation given in prefix form,
244       --  the prefix is the target object and it has to be added as the first
245       --  actual in the generated call.
246
247    begin
248       --  Determine the entity being renamed, which is the target of the call
249       --  statement. If the name is an explicit dereference, this is a renaming
250       --  of a subprogram type rather than a subprogram. The name itself is
251       --  fully analyzed.
252
253       if Nkind (Nam) = N_Selected_Component then
254          Old_S := Entity (Selector_Name (Nam));
255
256       elsif Nkind (Nam) = N_Explicit_Dereference then
257          Old_S := Etype (Nam);
258
259       elsif Nkind (Nam) = N_Indexed_Component then
260          if Is_Entity_Name (Prefix (Nam)) then
261             Old_S := Entity (Prefix (Nam));
262          else
263             Old_S := Entity (Selector_Name (Prefix (Nam)));
264          end if;
265
266       elsif Nkind (Nam) = N_Character_Literal then
267          Old_S := Etype (New_S);
268
269       else
270          Old_S := Entity (Nam);
271       end if;
272
273       if Is_Entity_Name (Nam) then
274
275          --  If the renamed entity is a predefined operator, retain full name
276          --  to ensure its visibility.
277
278          if Ekind (Old_S) = E_Operator
279            and then Nkind (Nam) = N_Expanded_Name
280          then
281             Call_Name := New_Copy (Name (N));
282          else
283             Call_Name := New_Reference_To (Old_S, Loc);
284          end if;
285
286       else
287          if Nkind (Nam) = N_Selected_Component
288            and then Present (First_Formal (Old_S))
289            and then
290              (Is_Controlling_Formal (First_Formal (Old_S))
291                 or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
292          then
293
294             --  Retrieve the target object, to be added as a first actual
295             --  in the call.
296
297             Call_Name := New_Occurrence_Of (Old_S, Loc);
298             Pref := Prefix (Nam);
299
300          else
301             Call_Name := New_Copy (Name (N));
302          end if;
303
304          --  The original name may have been overloaded, but
305          --  is fully resolved now.
306
307          Set_Is_Overloaded (Call_Name, False);
308       end if;
309
310       --  For simple renamings, subsequent calls can be expanded directly as
311       --  called to the renamed entity. The body must be generated in any case
312       --  for calls they may appear elsewhere.
313
314       if (Ekind (Old_S) = E_Function
315            or else Ekind (Old_S) = E_Procedure)
316         and then Nkind (Decl) = N_Subprogram_Declaration
317       then
318          Set_Body_To_Inline (Decl, Old_S);
319       end if;
320
321       --  The body generated for this renaming is an internal artifact, and
322       --  does not  constitute a freeze point for the called entity.
323
324       Set_Must_Not_Freeze (Call_Name);
325
326       Formal := First_Formal (Defining_Entity (Decl));
327
328       if Present (Pref) then
329          declare
330             Pref_Type : constant Entity_Id := Etype (Pref);
331             Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
332
333          begin
334
335             --  The controlling formal may be an access parameter, or the
336             --  actual may be an access value, so adjust accordingly.
337
338             if Is_Access_Type (Pref_Type)
339               and then not Is_Access_Type (Form_Type)
340             then
341                Actuals := New_List
342                  (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
343
344             elsif Is_Access_Type (Form_Type)
345               and then not Is_Access_Type (Pref)
346             then
347                Actuals := New_List
348                  (Make_Attribute_Reference (Loc,
349                    Attribute_Name => Name_Access,
350                    Prefix => Relocate_Node (Pref)));
351             else
352                Actuals := New_List (Pref);
353             end if;
354          end;
355
356       elsif Present (Formal) then
357          Actuals := New_List;
358
359       else
360          Actuals := No_List;
361       end if;
362
363       if Present (Formal) then
364          while Present (Formal) loop
365             Append (New_Reference_To (Formal, Loc), Actuals);
366             Next_Formal (Formal);
367          end loop;
368       end if;
369
370       --  If the renamed entity is an entry, inherit its profile. For other
371       --  renamings as bodies, both profiles must be subtype conformant, so it
372       --  is not necessary to replace the profile given in the declaration.
373       --  However, default values that are aggregates are rewritten when
374       --  partially analyzed, so we recover the original aggregate to insure
375       --  that subsequent conformity checking works. Similarly, if the default
376       --  expression was constant-folded, recover the original expression.
377
378       Formal := First_Formal (Defining_Entity (Decl));
379
380       if Present (Formal) then
381          O_Formal := First_Formal (Old_S);
382          Param_Spec := First (Parameter_Specifications (Spec));
383
384          while Present (Formal) loop
385             if Is_Entry (Old_S) then
386
387                if Nkind (Parameter_Type (Param_Spec)) /=
388                                                     N_Access_Definition
389                then
390                   Set_Etype (Formal, Etype (O_Formal));
391                   Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
392                end if;
393
394             elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
395               or else Nkind (Original_Node (Default_Value (O_Formal))) /=
396                                            Nkind (Default_Value (O_Formal))
397             then
398                Set_Expression (Param_Spec,
399                  New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
400             end if;
401
402             Next_Formal (Formal);
403             Next_Formal (O_Formal);
404             Next (Param_Spec);
405          end loop;
406       end if;
407
408       --  If the renamed entity is a function, the generated body contains a
409       --  return statement. Otherwise, build a procedure call. If the entity is
410       --  an entry, subsequent analysis of the call will transform it into the
411       --  proper entry or protected operation call. If the renamed entity is
412       --  a character literal, return it directly.
413
414       if Ekind (Old_S) = E_Function
415         or else Ekind (Old_S) = E_Operator
416         or else (Ekind (Old_S) = E_Subprogram_Type
417                   and then Etype (Old_S) /= Standard_Void_Type)
418       then
419          Call_Node :=
420            Make_Simple_Return_Statement (Loc,
421               Expression =>
422                 Make_Function_Call (Loc,
423                   Name => Call_Name,
424                   Parameter_Associations => Actuals));
425
426       elsif Ekind (Old_S) = E_Enumeration_Literal then
427          Call_Node :=
428            Make_Simple_Return_Statement (Loc,
429               Expression => New_Occurrence_Of (Old_S, Loc));
430
431       elsif Nkind (Nam) = N_Character_Literal then
432          Call_Node :=
433            Make_Simple_Return_Statement (Loc,
434              Expression => Call_Name);
435
436       else
437          Call_Node :=
438            Make_Procedure_Call_Statement (Loc,
439              Name => Call_Name,
440              Parameter_Associations => Actuals);
441       end if;
442
443       --  Create entities for subprogram body and formals
444
445       Set_Defining_Unit_Name (Spec,
446         Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
447
448       Param_Spec := First (Parameter_Specifications (Spec));
449
450       while Present (Param_Spec) loop
451          Set_Defining_Identifier (Param_Spec,
452            Make_Defining_Identifier (Loc,
453              Chars => Chars (Defining_Identifier (Param_Spec))));
454          Next (Param_Spec);
455       end loop;
456
457       Body_Node :=
458         Make_Subprogram_Body (Loc,
459           Specification => Spec,
460           Declarations => New_List,
461           Handled_Statement_Sequence =>
462             Make_Handled_Sequence_Of_Statements (Loc,
463               Statements => New_List (Call_Node)));
464
465       if Nkind (Decl) /= N_Subprogram_Declaration then
466          Rewrite (N,
467            Make_Subprogram_Declaration (Loc,
468              Specification => Specification (N)));
469       end if;
470
471       --  Link the body to the entity whose declaration it completes. If
472       --  the body is analyzed when the renamed entity is frozen, it may
473       --  be necessary to restore the proper scope (see package Exp_Ch13).
474
475       if Nkind (N) =  N_Subprogram_Renaming_Declaration
476         and then Present (Corresponding_Spec (N))
477       then
478          Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
479       else
480          Set_Corresponding_Spec (Body_Node, New_S);
481       end if;
482
483       return Body_Node;
484    end Build_Renamed_Body;
485
486    --------------------------
487    -- Check_Address_Clause --
488    --------------------------
489
490    procedure Check_Address_Clause (E : Entity_Id) is
491       Addr : constant Node_Id   := Address_Clause (E);
492       Expr : Node_Id;
493       Decl : constant Node_Id   := Declaration_Node (E);
494       Typ  : constant Entity_Id := Etype (E);
495
496    begin
497       if Present (Addr) then
498          Expr := Expression (Addr);
499
500          --  If we have no initialization of any kind, then we don't need to
501          --  place any restrictions on the address clause, because the object
502          --  will be elaborated after the address clause is evaluated. This
503          --  happens if the declaration has no initial expression, or the type
504          --  has no implicit initialization, or the object is imported.
505
506          --  The same holds for all initialized scalar types and all access
507          --  types. Packed bit arrays of size up to 64 are represented using a
508          --  modular type with an initialization (to zero) and can be processed
509          --  like other initialized scalar types.
510
511          --  If the type is controlled, code to attach the object to a
512          --  finalization chain is generated at the point of declaration,
513          --  and therefore the elaboration of the object cannot be delayed:
514          --  the address expression must be a constant.
515
516          if (No (Expression (Decl))
517               and then not Needs_Finalization (Typ)
518               and then
519                 (not Has_Non_Null_Base_Init_Proc (Typ)
520                   or else Is_Imported (E)))
521
522            or else
523              (Present (Expression (Decl))
524                and then Is_Scalar_Type (Typ))
525
526            or else
527              Is_Access_Type (Typ)
528
529            or else
530              (Is_Bit_Packed_Array (Typ)
531                and then
532                  Is_Modular_Integer_Type (Packed_Array_Type (Typ)))
533          then
534             null;
535
536          --  Otherwise, we require the address clause to be constant because
537          --  the call to the initialization procedure (or the attach code) has
538          --  to happen at the point of the declaration.
539          --  Actually the IP call has been moved to the freeze actions
540          --  anyway, so maybe we can relax this restriction???
541
542          else
543             Check_Constant_Address_Clause (Expr, E);
544
545             --  Has_Delayed_Freeze was set on E when the address clause was
546             --  analyzed. Reset the flag now unless freeze actions were
547             --  attached to it in the mean time.
548
549             if No (Freeze_Node (E)) then
550                Set_Has_Delayed_Freeze (E, False);
551             end if;
552          end if;
553
554          if not Error_Posted (Expr)
555            and then not Needs_Finalization (Typ)
556          then
557             Warn_Overlay (Expr, Typ, Name (Addr));
558          end if;
559       end if;
560    end Check_Address_Clause;
561
562    -----------------------------
563    -- Check_Compile_Time_Size --
564    -----------------------------
565
566    procedure Check_Compile_Time_Size (T : Entity_Id) is
567
568       procedure Set_Small_Size (T : Entity_Id; S : Uint);
569       --  Sets the compile time known size (32 bits or less) in the Esize
570       --  field, of T checking for a size clause that was given which attempts
571       --  to give a smaller size, and also checking for an alignment clause.
572
573       function Size_Known (T : Entity_Id) return Boolean;
574       --  Recursive function that does all the work
575
576       function Static_Discriminated_Components (T : Entity_Id) return Boolean;
577       --  If T is a constrained subtype, its size is not known if any of its
578       --  discriminant constraints is not static and it is not a null record.
579       --  The test is conservative and doesn't check that the components are
580       --  in fact constrained by non-static discriminant values. Could be made
581       --  more precise ???
582
583       --------------------
584       -- Set_Small_Size --
585       --------------------
586
587       procedure Set_Small_Size (T : Entity_Id; S : Uint) is
588       begin
589          if S > 32 then
590             return;
591
592          --  Don't bother if alignment clause with a value other than 1 is
593          --  present, because size may be padded up to meet back end alignment
594          --  requirements, and only the back end knows the rules!
595
596          elsif Known_Alignment (T) and then Alignment (T) /= 1 then
597             return;
598
599          --  Check for bad size clause given
600
601          elsif Has_Size_Clause (T) then
602             if RM_Size (T) < S then
603                Error_Msg_Uint_1 := S;
604                Error_Msg_NE
605                  ("size for& too small, minimum allowed is ^",
606                   Size_Clause (T), T);
607
608             elsif Unknown_Esize (T) then
609                Set_Esize (T, S);
610             end if;
611
612          --  Set sizes if not set already
613
614          else
615             if Unknown_Esize (T) then
616                Set_Esize (T, S);
617             end if;
618
619             if Unknown_RM_Size (T) then
620                Set_RM_Size (T, S);
621             end if;
622          end if;
623       end Set_Small_Size;
624
625       ----------------
626       -- Size_Known --
627       ----------------
628
629       function Size_Known (T : Entity_Id) return Boolean is
630          Index : Entity_Id;
631          Comp  : Entity_Id;
632          Ctyp  : Entity_Id;
633          Low   : Node_Id;
634          High  : Node_Id;
635
636       begin
637          if Size_Known_At_Compile_Time (T) then
638             return True;
639
640          --  Always True for scalar types. This is true even for generic formal
641          --  scalar types. We used to return False in the latter case, but the
642          --  size is known at compile time, even in the template, we just do
643          --  not know the exact size but that's not the point of this routine.
644
645          elsif Is_Scalar_Type (T)
646            or else Is_Task_Type (T)
647          then
648             return True;
649
650          --  Array types
651
652          elsif Is_Array_Type (T) then
653
654             --  String literals always have known size, and we can set it
655
656             if Ekind (T) = E_String_Literal_Subtype then
657                Set_Small_Size (T, Component_Size (T)
658                                * String_Literal_Length (T));
659                return True;
660
661             --  Unconstrained types never have known at compile time size
662
663             elsif not Is_Constrained (T) then
664                return False;
665
666             --  Don't do any recursion on type with error posted, since we may
667             --  have a malformed type that leads us into a loop.
668
669             elsif Error_Posted (T) then
670                return False;
671
672             --  Otherwise if component size unknown, then array size unknown
673
674             elsif not Size_Known (Component_Type (T)) then
675                return False;
676             end if;
677
678             --  Check for all indexes static, and also compute possible size
679             --  (in case it is less than 32 and may be packable).
680
681             declare
682                Esiz : Uint := Component_Size (T);
683                Dim  : Uint;
684
685             begin
686                Index := First_Index (T);
687                while Present (Index) loop
688                   if Nkind (Index) = N_Range then
689                      Get_Index_Bounds (Index, Low, High);
690
691                   elsif Error_Posted (Scalar_Range (Etype (Index))) then
692                      return False;
693
694                   else
695                      Low  := Type_Low_Bound (Etype (Index));
696                      High := Type_High_Bound (Etype (Index));
697                   end if;
698
699                   if not Compile_Time_Known_Value (Low)
700                     or else not Compile_Time_Known_Value (High)
701                     or else Etype (Index) = Any_Type
702                   then
703                      return False;
704
705                   else
706                      Dim := Expr_Value (High) - Expr_Value (Low) + 1;
707
708                      if Dim >= 0 then
709                         Esiz := Esiz * Dim;
710                      else
711                         Esiz := Uint_0;
712                      end if;
713                   end if;
714
715                   Next_Index (Index);
716                end loop;
717
718                Set_Small_Size (T, Esiz);
719                return True;
720             end;
721
722          --  Access types always have known at compile time sizes
723
724          elsif Is_Access_Type (T) then
725             return True;
726
727          --  For non-generic private types, go to underlying type if present
728
729          elsif Is_Private_Type (T)
730            and then not Is_Generic_Type (T)
731            and then Present (Underlying_Type (T))
732          then
733             --  Don't do any recursion on type with error posted, since we may
734             --  have a malformed type that leads us into a loop.
735
736             if Error_Posted (T) then
737                return False;
738             else
739                return Size_Known (Underlying_Type (T));
740             end if;
741
742          --  Record types
743
744          elsif Is_Record_Type (T) then
745
746             --  A class-wide type is never considered to have a known size
747
748             if Is_Class_Wide_Type (T) then
749                return False;
750
751             --  A subtype of a variant record must not have non-static
752             --  discriminanted components.
753
754             elsif T /= Base_Type (T)
755               and then not Static_Discriminated_Components (T)
756             then
757                return False;
758
759             --  Don't do any recursion on type with error posted, since we may
760             --  have a malformed type that leads us into a loop.
761
762             elsif Error_Posted (T) then
763                return False;
764             end if;
765
766             --  Now look at the components of the record
767
768             declare
769                --  The following two variables are used to keep track of the
770                --  size of packed records if we can tell the size of the packed
771                --  record in the front end. Packed_Size_Known is True if so far
772                --  we can figure out the size. It is initialized to True for a
773                --  packed record, unless the record has discriminants. The
774                --  reason we eliminate the discriminated case is that we don't
775                --  know the way the back end lays out discriminated packed
776                --  records. If Packed_Size_Known is True, then Packed_Size is
777                --  the size in bits so far.
778
779                Packed_Size_Known : Boolean :=
780                                      Is_Packed (T)
781                                        and then not Has_Discriminants (T);
782
783                Packed_Size : Uint := Uint_0;
784
785             begin
786                --  Test for variant part present
787
788                if Has_Discriminants (T)
789                  and then Present (Parent (T))
790                  and then Nkind (Parent (T)) = N_Full_Type_Declaration
791                  and then Nkind (Type_Definition (Parent (T))) =
792                             N_Record_Definition
793                  and then not Null_Present (Type_Definition (Parent (T)))
794                  and then Present (Variant_Part
795                             (Component_List (Type_Definition (Parent (T)))))
796                then
797                   --  If variant part is present, and type is unconstrained,
798                   --  then we must have defaulted discriminants, or a size
799                   --  clause must be present for the type, or else the size
800                   --  is definitely not known at compile time.
801
802                   if not Is_Constrained (T)
803                     and then
804                       No (Discriminant_Default_Value
805                            (First_Discriminant (T)))
806                     and then Unknown_Esize (T)
807                   then
808                      return False;
809                   end if;
810                end if;
811
812                --  Loop through components
813
814                Comp := First_Component_Or_Discriminant (T);
815                while Present (Comp) loop
816                   Ctyp := Etype (Comp);
817
818                   --  We do not know the packed size if there is a component
819                   --  clause present (we possibly could, but this would only
820                   --  help in the case of a record with partial rep clauses.
821                   --  That's because in the case of full rep clauses, the
822                   --  size gets figured out anyway by a different circuit).
823
824                   if Present (Component_Clause (Comp)) then
825                      Packed_Size_Known := False;
826                   end if;
827
828                   --  We need to identify a component that is an array where
829                   --  the index type is an enumeration type with non-standard
830                   --  representation, and some bound of the type depends on a
831                   --  discriminant.
832
833                   --  This is because gigi computes the size by doing a
834                   --  substitution of the appropriate discriminant value in
835                   --  the size expression for the base type, and gigi is not
836                   --  clever enough to evaluate the resulting expression (which
837                   --  involves a call to rep_to_pos) at compile time.
838
839                   --  It would be nice if gigi would either recognize that
840                   --  this expression can be computed at compile time, or
841                   --  alternatively figured out the size from the subtype
842                   --  directly, where all the information is at hand ???
843
844                   if Is_Array_Type (Etype (Comp))
845                     and then Present (Packed_Array_Type (Etype (Comp)))
846                   then
847                      declare
848                         Ocomp  : constant Entity_Id :=
849                                    Original_Record_Component (Comp);
850                         OCtyp  : constant Entity_Id := Etype (Ocomp);
851                         Ind    : Node_Id;
852                         Indtyp : Entity_Id;
853                         Lo, Hi : Node_Id;
854
855                      begin
856                         Ind := First_Index (OCtyp);
857                         while Present (Ind) loop
858                            Indtyp := Etype (Ind);
859
860                            if Is_Enumeration_Type (Indtyp)
861                              and then Has_Non_Standard_Rep (Indtyp)
862                            then
863                               Lo := Type_Low_Bound  (Indtyp);
864                               Hi := Type_High_Bound (Indtyp);
865
866                               if Is_Entity_Name (Lo)
867                                 and then Ekind (Entity (Lo)) = E_Discriminant
868                               then
869                                  return False;
870
871                               elsif Is_Entity_Name (Hi)
872                                 and then Ekind (Entity (Hi)) = E_Discriminant
873                               then
874                                  return False;
875                               end if;
876                            end if;
877
878                            Next_Index (Ind);
879                         end loop;
880                      end;
881                   end if;
882
883                   --  Clearly size of record is not known if the size of one of
884                   --  the components is not known.
885
886                   if not Size_Known (Ctyp) then
887                      return False;
888                   end if;
889
890                   --  Accumulate packed size if possible
891
892                   if Packed_Size_Known then
893
894                      --  We can only deal with elementary types, since for
895                      --  non-elementary components, alignment enters into the
896                      --  picture, and we don't know enough to handle proper
897                      --  alignment in this context. Packed arrays count as
898                      --  elementary if the representation is a modular type.
899
900                      if Is_Elementary_Type (Ctyp)
901                        or else (Is_Array_Type (Ctyp)
902                                  and then Present (Packed_Array_Type (Ctyp))
903                                  and then Is_Modular_Integer_Type
904                                             (Packed_Array_Type (Ctyp)))
905                      then
906                         --  If RM_Size is known and static, then we can keep
907                         --  accumulating the packed size.
908
909                         if Known_Static_RM_Size (Ctyp) then
910
911                            --  A little glitch, to be removed sometime ???
912                            --  gigi does not understand zero sizes yet.
913
914                            if RM_Size (Ctyp) = Uint_0 then
915                               Packed_Size_Known := False;
916
917                            --  Normal case where we can keep accumulating the
918                            --  packed array size.
919
920                            else
921                               Packed_Size := Packed_Size + RM_Size (Ctyp);
922                            end if;
923
924                         --  If we have a field whose RM_Size is not known then
925                         --  we can't figure out the packed size here.
926
927                         else
928                            Packed_Size_Known := False;
929                         end if;
930
931                      --  If we have a non-elementary type we can't figure out
932                      --  the packed array size (alignment issues).
933
934                      else
935                         Packed_Size_Known := False;
936                      end if;
937                   end if;
938
939                   Next_Component_Or_Discriminant (Comp);
940                end loop;
941
942                if Packed_Size_Known then
943                   Set_Small_Size (T, Packed_Size);
944                end if;
945
946                return True;
947             end;
948
949          --  All other cases, size not known at compile time
950
951          else
952             return False;
953          end if;
954       end Size_Known;
955
956       -------------------------------------
957       -- Static_Discriminated_Components --
958       -------------------------------------
959
960       function Static_Discriminated_Components
961         (T : Entity_Id) return Boolean
962       is
963          Constraint : Elmt_Id;
964
965       begin
966          if Has_Discriminants (T)
967            and then Present (Discriminant_Constraint (T))
968            and then Present (First_Component (T))
969          then
970             Constraint := First_Elmt (Discriminant_Constraint (T));
971             while Present (Constraint) loop
972                if not Compile_Time_Known_Value (Node (Constraint)) then
973                   return False;
974                end if;
975
976                Next_Elmt (Constraint);
977             end loop;
978          end if;
979
980          return True;
981       end Static_Discriminated_Components;
982
983    --  Start of processing for Check_Compile_Time_Size
984
985    begin
986       Set_Size_Known_At_Compile_Time (T, Size_Known (T));
987    end Check_Compile_Time_Size;
988
989    -----------------------------
990    -- Check_Debug_Info_Needed --
991    -----------------------------
992
993    procedure Check_Debug_Info_Needed (T : Entity_Id) is
994    begin
995       if Debug_Info_Off (T) then
996          return;
997
998       elsif Comes_From_Source (T)
999         or else Debug_Generated_Code
1000         or else Debug_Flag_VV
1001         or else Needs_Debug_Info (T)
1002       then
1003          Set_Debug_Info_Needed (T);
1004       end if;
1005    end Check_Debug_Info_Needed;
1006
1007    ----------------------------
1008    -- Check_Strict_Alignment --
1009    ----------------------------
1010
1011    procedure Check_Strict_Alignment (E : Entity_Id) is
1012       Comp  : Entity_Id;
1013
1014    begin
1015       if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1016          Set_Strict_Alignment (E);
1017
1018       elsif Is_Array_Type (E) then
1019          Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1020
1021       elsif Is_Record_Type (E) then
1022          if Is_Limited_Record (E) then
1023             Set_Strict_Alignment (E);
1024             return;
1025          end if;
1026
1027          Comp := First_Component (E);
1028
1029          while Present (Comp) loop
1030             if not Is_Type (Comp)
1031               and then (Strict_Alignment (Etype (Comp))
1032                          or else Is_Aliased (Comp))
1033             then
1034                Set_Strict_Alignment (E);
1035                return;
1036             end if;
1037
1038             Next_Component (Comp);
1039          end loop;
1040       end if;
1041    end Check_Strict_Alignment;
1042
1043    -------------------------
1044    -- Check_Unsigned_Type --
1045    -------------------------
1046
1047    procedure Check_Unsigned_Type (E : Entity_Id) is
1048       Ancestor : Entity_Id;
1049       Lo_Bound : Node_Id;
1050       Btyp     : Entity_Id;
1051
1052    begin
1053       if not Is_Discrete_Or_Fixed_Point_Type (E) then
1054          return;
1055       end if;
1056
1057       --  Do not attempt to analyze case where range was in error
1058
1059       if Error_Posted (Scalar_Range (E)) then
1060          return;
1061       end if;
1062
1063       --  The situation that is non trivial is something like
1064
1065       --     subtype x1 is integer range -10 .. +10;
1066       --     subtype x2 is x1 range 0 .. V1;
1067       --     subtype x3 is x2 range V2 .. V3;
1068       --     subtype x4 is x3 range V4 .. V5;
1069
1070       --  where Vn are variables. Here the base type is signed, but we still
1071       --  know that x4 is unsigned because of the lower bound of x2.
1072
1073       --  The only way to deal with this is to look up the ancestor chain
1074
1075       Ancestor := E;
1076       loop
1077          if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1078             return;
1079          end if;
1080
1081          Lo_Bound := Type_Low_Bound (Ancestor);
1082
1083          if Compile_Time_Known_Value (Lo_Bound) then
1084
1085             if Expr_Rep_Value (Lo_Bound) >= 0 then
1086                Set_Is_Unsigned_Type (E, True);
1087             end if;
1088
1089             return;
1090
1091          else
1092             Ancestor := Ancestor_Subtype (Ancestor);
1093
1094             --  If no ancestor had a static lower bound, go to base type
1095
1096             if No (Ancestor) then
1097
1098                --  Note: the reason we still check for a compile time known
1099                --  value for the base type is that at least in the case of
1100                --  generic formals, we can have bounds that fail this test,
1101                --  and there may be other cases in error situations.
1102
1103                Btyp := Base_Type (E);
1104
1105                if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1106                   return;
1107                end if;
1108
1109                Lo_Bound := Type_Low_Bound (Base_Type (E));
1110
1111                if Compile_Time_Known_Value (Lo_Bound)
1112                  and then Expr_Rep_Value (Lo_Bound) >= 0
1113                then
1114                   Set_Is_Unsigned_Type (E, True);
1115                end if;
1116
1117                return;
1118             end if;
1119          end if;
1120       end loop;
1121    end Check_Unsigned_Type;
1122
1123    -------------------------
1124    -- Is_Atomic_Aggregate --
1125    -------------------------
1126
1127    function  Is_Atomic_Aggregate
1128      (E   : Entity_Id;
1129       Typ : Entity_Id) return Boolean
1130    is
1131       Loc   : constant Source_Ptr := Sloc (E);
1132       New_N : Node_Id;
1133       Par   : Node_Id;
1134       Temp  : Entity_Id;
1135
1136    begin
1137       Par := Parent (E);
1138
1139       --  Array may be qualified, so find outer context
1140
1141       if Nkind (Par) = N_Qualified_Expression then
1142          Par := Parent (Par);
1143       end if;
1144
1145       if Nkind_In (Par, N_Object_Declaration, N_Assignment_Statement)
1146         and then Comes_From_Source (Par)
1147       then
1148          Temp := Make_Temporary (Loc, 'T', E);
1149          New_N :=
1150            Make_Object_Declaration (Loc,
1151              Defining_Identifier => Temp,
1152              Object_Definition   => New_Occurrence_Of (Typ, Loc),
1153              Expression          => Relocate_Node (E));
1154          Insert_Before (Par, New_N);
1155          Analyze (New_N);
1156
1157          Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1158          return True;
1159
1160       else
1161          return False;
1162       end if;
1163    end Is_Atomic_Aggregate;
1164
1165    ----------------
1166    -- Freeze_All --
1167    ----------------
1168
1169    --  Note: the easy coding for this procedure would be to just build a
1170    --  single list of freeze nodes and then insert them and analyze them
1171    --  all at once. This won't work, because the analysis of earlier freeze
1172    --  nodes may recursively freeze types which would otherwise appear later
1173    --  on in the freeze list. So we must analyze and expand the freeze nodes
1174    --  as they are generated.
1175
1176    procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1177       Loc   : constant Source_Ptr := Sloc (After);
1178       E     : Entity_Id;
1179       Decl  : Node_Id;
1180
1181       procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1182       --  This is the internal recursive routine that does freezing of entities
1183       --  (but NOT the analysis of default expressions, which should not be
1184       --  recursive, we don't want to analyze those till we are sure that ALL
1185       --  the types are frozen).
1186
1187       --------------------
1188       -- Freeze_All_Ent --
1189       --------------------
1190
1191       procedure Freeze_All_Ent
1192         (From  : Entity_Id;
1193          After : in out Node_Id)
1194       is
1195          E     : Entity_Id;
1196          Flist : List_Id;
1197          Lastn : Node_Id;
1198
1199          procedure Process_Flist;
1200          --  If freeze nodes are present, insert and analyze, and reset cursor
1201          --  for next insertion.
1202
1203          -------------------
1204          -- Process_Flist --
1205          -------------------
1206
1207          procedure Process_Flist is
1208          begin
1209             if Is_Non_Empty_List (Flist) then
1210                Lastn := Next (After);
1211                Insert_List_After_And_Analyze (After, Flist);
1212
1213                if Present (Lastn) then
1214                   After := Prev (Lastn);
1215                else
1216                   After := Last (List_Containing (After));
1217                end if;
1218             end if;
1219          end Process_Flist;
1220
1221       --  Start or processing for Freeze_All_Ent
1222
1223       begin
1224          E := From;
1225          while Present (E) loop
1226
1227             --  If the entity is an inner package which is not a package
1228             --  renaming, then its entities must be frozen at this point. Note
1229             --  that such entities do NOT get frozen at the end of the nested
1230             --  package itself (only library packages freeze).
1231
1232             --  Same is true for task declarations, where anonymous records
1233             --  created for entry parameters must be frozen.
1234
1235             if Ekind (E) = E_Package
1236               and then No (Renamed_Object (E))
1237               and then not Is_Child_Unit (E)
1238               and then not Is_Frozen (E)
1239             then
1240                Push_Scope (E);
1241                Install_Visible_Declarations (E);
1242                Install_Private_Declarations (E);
1243
1244                Freeze_All (First_Entity (E), After);
1245
1246                End_Package_Scope (E);
1247
1248             elsif Ekind (E) in Task_Kind
1249               and then
1250                 (Nkind (Parent (E)) = N_Task_Type_Declaration
1251                    or else
1252                  Nkind (Parent (E)) = N_Single_Task_Declaration)
1253             then
1254                Push_Scope (E);
1255                Freeze_All (First_Entity (E), After);
1256                End_Scope;
1257
1258             --  For a derived tagged type, we must ensure that all the
1259             --  primitive operations of the parent have been frozen, so that
1260             --  their addresses will be in the parent's dispatch table at the
1261             --  point it is inherited.
1262
1263             elsif Ekind (E) = E_Record_Type
1264               and then Is_Tagged_Type (E)
1265               and then Is_Tagged_Type (Etype (E))
1266               and then Is_Derived_Type (E)
1267             then
1268                declare
1269                   Prim_List : constant Elist_Id :=
1270                                Primitive_Operations (Etype (E));
1271
1272                   Prim : Elmt_Id;
1273                   Subp : Entity_Id;
1274
1275                begin
1276                   Prim  := First_Elmt (Prim_List);
1277
1278                   while Present (Prim) loop
1279                      Subp := Node (Prim);
1280
1281                      if Comes_From_Source (Subp)
1282                        and then not Is_Frozen (Subp)
1283                      then
1284                         Flist := Freeze_Entity (Subp, Loc);
1285                         Process_Flist;
1286                      end if;
1287
1288                      Next_Elmt (Prim);
1289                   end loop;
1290                end;
1291             end if;
1292
1293             if not Is_Frozen (E) then
1294                Flist := Freeze_Entity (E, Loc);
1295                Process_Flist;
1296             end if;
1297
1298             --  If an incomplete type is still not frozen, this may be a
1299             --  premature freezing because of a body declaration that follows.
1300             --  Indicate where the freezing took place.
1301
1302             --  If the freezing is caused by the end of the current declarative
1303             --  part, it is a Taft Amendment type, and there is no error.
1304
1305             if not Is_Frozen (E)
1306               and then Ekind (E) = E_Incomplete_Type
1307             then
1308                declare
1309                   Bod : constant Node_Id := Next (After);
1310
1311                begin
1312                   if (Nkind (Bod) = N_Subprogram_Body
1313                         or else Nkind (Bod) = N_Entry_Body
1314                         or else Nkind (Bod) = N_Package_Body
1315                         or else Nkind (Bod) = N_Protected_Body
1316                         or else Nkind (Bod) = N_Task_Body
1317                         or else Nkind (Bod) in N_Body_Stub)
1318                      and then
1319                        List_Containing (After) = List_Containing (Parent (E))
1320                   then
1321                      Error_Msg_Sloc := Sloc (Next (After));
1322                      Error_Msg_NE
1323                        ("type& is frozen# before its full declaration",
1324                          Parent (E), E);
1325                   end if;
1326                end;
1327             end if;
1328
1329             Next_Entity (E);
1330          end loop;
1331       end Freeze_All_Ent;
1332
1333    --  Start of processing for Freeze_All
1334
1335    begin
1336       Freeze_All_Ent (From, After);
1337
1338       --  Now that all types are frozen, we can deal with default expressions
1339       --  that require us to build a default expression functions. This is the
1340       --  point at which such functions are constructed (after all types that
1341       --  might be used in such expressions have been frozen).
1342
1343       --  We also add finalization chains to access types whose designated
1344       --  types are controlled. This is normally done when freezing the type,
1345       --  but this misses recursive type definitions where the later members
1346       --  of the recursion introduce controlled components.
1347
1348       --  Loop through entities
1349
1350       E := From;
1351       while Present (E) loop
1352          if Is_Subprogram (E) then
1353
1354             if not Default_Expressions_Processed (E) then
1355                Process_Default_Expressions (E, After);
1356             end if;
1357
1358             if not Has_Completion (E) then
1359                Decl := Unit_Declaration_Node (E);
1360
1361                if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1362                   Build_And_Analyze_Renamed_Body (Decl, E, After);
1363
1364                elsif Nkind (Decl) = N_Subprogram_Declaration
1365                  and then Present (Corresponding_Body (Decl))
1366                  and then
1367                    Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1368                                           = N_Subprogram_Renaming_Declaration
1369                then
1370                   Build_And_Analyze_Renamed_Body
1371                     (Decl, Corresponding_Body (Decl), After);
1372                end if;
1373             end if;
1374
1375          elsif Ekind (E) in Task_Kind
1376            and then
1377              (Nkind (Parent (E)) = N_Task_Type_Declaration
1378                 or else
1379               Nkind (Parent (E)) = N_Single_Task_Declaration)
1380          then
1381             declare
1382                Ent : Entity_Id;
1383             begin
1384                Ent := First_Entity (E);
1385
1386                while Present (Ent) loop
1387
1388                   if Is_Entry (Ent)
1389                     and then not Default_Expressions_Processed (Ent)
1390                   then
1391                      Process_Default_Expressions (Ent, After);
1392                   end if;
1393
1394                   Next_Entity (Ent);
1395                end loop;
1396             end;
1397
1398          elsif Is_Access_Type (E)
1399            and then Comes_From_Source (E)
1400            and then Ekind (Directly_Designated_Type (E)) = E_Incomplete_Type
1401            and then Needs_Finalization (Designated_Type (E))
1402            and then No (Associated_Final_Chain (E))
1403          then
1404             Build_Final_List (Parent (E), E);
1405          end if;
1406
1407          Next_Entity (E);
1408       end loop;
1409    end Freeze_All;
1410
1411    -----------------------
1412    -- Freeze_And_Append --
1413    -----------------------
1414
1415    procedure Freeze_And_Append
1416      (Ent    : Entity_Id;
1417       Loc    : Source_Ptr;
1418       Result : in out List_Id)
1419    is
1420       L : constant List_Id := Freeze_Entity (Ent, Loc);
1421    begin
1422       if Is_Non_Empty_List (L) then
1423          if Result = No_List then
1424             Result := L;
1425          else
1426             Append_List (L, Result);
1427          end if;
1428       end if;
1429    end Freeze_And_Append;
1430
1431    -------------------
1432    -- Freeze_Before --
1433    -------------------
1434
1435    procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1436       Freeze_Nodes : constant List_Id := Freeze_Entity (T, Sloc (N));
1437    begin
1438       if Is_Non_Empty_List (Freeze_Nodes) then
1439          Insert_Actions (N, Freeze_Nodes);
1440       end if;
1441    end Freeze_Before;
1442
1443    -------------------
1444    -- Freeze_Entity --
1445    -------------------
1446
1447    function Freeze_Entity (E : Entity_Id; Loc : Source_Ptr) return List_Id is
1448       Test_E : Entity_Id := E;
1449       Comp   : Entity_Id;
1450       F_Node : Node_Id;
1451       Result : List_Id;
1452       Indx   : Node_Id;
1453       Formal : Entity_Id;
1454       Atype  : Entity_Id;
1455
1456       Has_Default_Initialization : Boolean := False;
1457       --  This flag gets set to true for a variable with default initialization
1458
1459       procedure Check_Current_Instance (Comp_Decl : Node_Id);
1460       --  Check that an Access or Unchecked_Access attribute with a prefix
1461       --  which is the current instance type can only be applied when the type
1462       --  is limited.
1463
1464       procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1465       --  Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1466       --  integer literal without an explicit corresponding size clause. The
1467       --  caller has checked that Utype is a modular integer type.
1468
1469       function After_Last_Declaration return Boolean;
1470       --  If Loc is a freeze_entity that appears after the last declaration
1471       --  in the scope, inhibit error messages on late completion.
1472
1473       procedure Freeze_Record_Type (Rec : Entity_Id);
1474       --  Freeze each component, handle some representation clauses, and freeze
1475       --  primitive operations if this is a tagged type.
1476
1477       ----------------------------
1478       -- After_Last_Declaration --
1479       ----------------------------
1480
1481       function After_Last_Declaration return Boolean is
1482          Spec : constant Node_Id := Parent (Current_Scope);
1483       begin
1484          if Nkind (Spec) = N_Package_Specification then
1485             if Present (Private_Declarations (Spec)) then
1486                return Loc >= Sloc (Last (Private_Declarations (Spec)));
1487             elsif Present (Visible_Declarations (Spec)) then
1488                return Loc >= Sloc (Last (Visible_Declarations (Spec)));
1489             else
1490                return False;
1491             end if;
1492          else
1493             return False;
1494          end if;
1495       end After_Last_Declaration;
1496
1497       ----------------------------
1498       -- Check_Current_Instance --
1499       ----------------------------
1500
1501       procedure Check_Current_Instance (Comp_Decl : Node_Id) is
1502
1503          Rec_Type : constant Entity_Id :=
1504                       Scope (Defining_Identifier (Comp_Decl));
1505
1506          Decl : constant Node_Id := Parent (Rec_Type);
1507
1508          function Process (N : Node_Id) return Traverse_Result;
1509          --  Process routine to apply check to given node
1510
1511          -------------
1512          -- Process --
1513          -------------
1514
1515          function Process (N : Node_Id) return Traverse_Result is
1516          begin
1517             case Nkind (N) is
1518                when N_Attribute_Reference =>
1519                   if (Attribute_Name (N) = Name_Access
1520                         or else
1521                       Attribute_Name (N) = Name_Unchecked_Access)
1522                     and then Is_Entity_Name (Prefix (N))
1523                     and then Is_Type (Entity (Prefix (N)))
1524                     and then Entity (Prefix (N)) = E
1525                   then
1526                      Error_Msg_N
1527                        ("current instance must be a limited type", Prefix (N));
1528                      return Abandon;
1529                   else
1530                      return OK;
1531                   end if;
1532
1533                when others => return OK;
1534             end case;
1535          end Process;
1536
1537          procedure Traverse is new Traverse_Proc (Process);
1538
1539       --  Start of processing for Check_Current_Instance
1540
1541       begin
1542          --  In Ada95, the (imprecise) rule is that the current instance of a
1543          --  limited type is aliased. In Ada2005, limitedness must be explicit:
1544          --  either a tagged type, or a limited record.
1545
1546          if Is_Limited_Type (Rec_Type)
1547            and then (Ada_Version < Ada_05 or else Is_Tagged_Type (Rec_Type))
1548          then
1549             return;
1550
1551          elsif Nkind (Decl) = N_Full_Type_Declaration
1552            and then Limited_Present (Type_Definition (Decl))
1553          then
1554             return;
1555
1556          else
1557             Traverse (Comp_Decl);
1558          end if;
1559       end Check_Current_Instance;
1560
1561       ------------------------------
1562       -- Check_Suspicious_Modulus --
1563       ------------------------------
1564
1565       procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
1566          Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
1567
1568       begin
1569          if Nkind (Decl) = N_Full_Type_Declaration then
1570             declare
1571                Tdef : constant Node_Id := Type_Definition (Decl);
1572             begin
1573                if Nkind (Tdef) = N_Modular_Type_Definition then
1574                   declare
1575                      Modulus : constant Node_Id :=
1576                                  Original_Node (Expression (Tdef));
1577                   begin
1578                      if Nkind (Modulus) = N_Integer_Literal then
1579                         declare
1580                            Modv : constant Uint := Intval (Modulus);
1581                            Sizv : constant Uint := RM_Size (Utype);
1582
1583                         begin
1584                            --  First case, modulus and size are the same. This
1585                            --  happens if you have something like mod 32, with
1586                            --  an explicit size of 32, this is for sure a case
1587                            --  where the warning is given, since it is seems
1588                            --  very unlikely that someone would want e.g. a
1589                            --  five bit type stored in 32 bits. It is much
1590                            --  more likely they wanted a 32-bit type.
1591
1592                            if Modv = Sizv then
1593                               null;
1594
1595                            --  Second case, the modulus is 32 or 64 and no
1596                            --  size clause is present. This is a less clear
1597                            --  case for giving the warning, but in the case
1598                            --  of 32/64 (5-bit or 6-bit types) these seem rare
1599                            --  enough that it is a likely error (and in any
1600                            --  case using 2**5 or 2**6 in these cases seems
1601                            --  clearer. We don't include 8 or 16 here, simply
1602                            --  because in practice 3-bit and 4-bit types are
1603                            --  more common and too many false positives if
1604                            --  we warn in these cases.
1605
1606                            elsif not Has_Size_Clause (Utype)
1607                              and then (Modv = Uint_32 or else Modv = Uint_64)
1608                            then
1609                               null;
1610
1611                            --  No warning needed
1612
1613                            else
1614                               return;
1615                            end if;
1616
1617                            --  If we fall through, give warning
1618
1619                            Error_Msg_Uint_1 := Modv;
1620                            Error_Msg_N
1621                              ("?2 '*'*^' may have been intended here",
1622                               Modulus);
1623                         end;
1624                      end if;
1625                   end;
1626                end if;
1627             end;
1628          end if;
1629       end Check_Suspicious_Modulus;
1630
1631       ------------------------
1632       -- Freeze_Record_Type --
1633       ------------------------
1634
1635       procedure Freeze_Record_Type (Rec : Entity_Id) is
1636          Comp : Entity_Id;
1637          IR   : Node_Id;
1638          ADC  : Node_Id;
1639          Prev : Entity_Id;
1640
1641          Junk : Boolean;
1642          pragma Warnings (Off, Junk);
1643
1644          Unplaced_Component : Boolean := False;
1645          --  Set True if we find at least one component with no component
1646          --  clause (used to warn about useless Pack pragmas).
1647
1648          Placed_Component : Boolean := False;
1649          --  Set True if we find at least one component with a component
1650          --  clause (used to warn about useless Bit_Order pragmas, and also
1651          --  to detect cases where Implicit_Packing may have an effect).
1652
1653          All_Scalar_Components : Boolean := True;
1654          --  Set False if we encounter a component of a non-scalar type
1655
1656          Scalar_Component_Total_RM_Size : Uint := Uint_0;
1657          Scalar_Component_Total_Esize   : Uint := Uint_0;
1658          --  Accumulates total RM_Size values and total Esize values of all
1659          --  scalar components. Used for processing of Implicit_Packing.
1660
1661          function Check_Allocator (N : Node_Id) return Node_Id;
1662          --  If N is an allocator, possibly wrapped in one or more level of
1663          --  qualified expression(s), return the inner allocator node, else
1664          --  return Empty.
1665
1666          procedure Check_Itype (Typ : Entity_Id);
1667          --  If the component subtype is an access to a constrained subtype of
1668          --  an already frozen type, make the subtype frozen as well. It might
1669          --  otherwise be frozen in the wrong scope, and a freeze node on
1670          --  subtype has no effect. Similarly, if the component subtype is a
1671          --  regular (not protected) access to subprogram, set the anonymous
1672          --  subprogram type to frozen as well, to prevent an out-of-scope
1673          --  freeze node at some eventual point of call. Protected operations
1674          --  are handled elsewhere.
1675
1676          ---------------------
1677          -- Check_Allocator --
1678          ---------------------
1679
1680          function Check_Allocator (N : Node_Id) return Node_Id is
1681             Inner : Node_Id;
1682          begin
1683             Inner := N;
1684             loop
1685                if Nkind (Inner) = N_Allocator then
1686                   return Inner;
1687                elsif Nkind (Inner) = N_Qualified_Expression then
1688                   Inner := Expression (Inner);
1689                else
1690                   return Empty;
1691                end if;
1692             end loop;
1693          end Check_Allocator;
1694
1695          -----------------
1696          -- Check_Itype --
1697          -----------------
1698
1699          procedure Check_Itype (Typ : Entity_Id) is
1700             Desig : constant Entity_Id := Designated_Type (Typ);
1701
1702          begin
1703             if not Is_Frozen (Desig)
1704               and then Is_Frozen (Base_Type (Desig))
1705             then
1706                Set_Is_Frozen (Desig);
1707
1708                --  In addition, add an Itype_Reference to ensure that the
1709                --  access subtype is elaborated early enough. This cannot be
1710                --  done if the subtype may depend on discriminants.
1711
1712                if Ekind (Comp) = E_Component
1713                  and then Is_Itype (Etype (Comp))
1714                  and then not Has_Discriminants (Rec)
1715                then
1716                   IR := Make_Itype_Reference (Sloc (Comp));
1717                   Set_Itype (IR, Desig);
1718
1719                   if No (Result) then
1720                      Result := New_List (IR);
1721                   else
1722                      Append (IR, Result);
1723                   end if;
1724                end if;
1725
1726             elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
1727               and then Convention (Desig) /= Convention_Protected
1728             then
1729                Set_Is_Frozen (Desig);
1730             end if;
1731          end Check_Itype;
1732
1733       --  Start of processing for Freeze_Record_Type
1734
1735       begin
1736          --  If this is a subtype of a controlled type, declared without a
1737          --  constraint, the _controller may not appear in the component list
1738          --  if the parent was not frozen at the point of subtype declaration.
1739          --  Inherit the _controller component now.
1740
1741          if Rec /= Base_Type (Rec)
1742            and then Has_Controlled_Component (Rec)
1743          then
1744             if Nkind (Parent (Rec)) = N_Subtype_Declaration
1745               and then Is_Entity_Name (Subtype_Indication (Parent (Rec)))
1746             then
1747                Set_First_Entity (Rec, First_Entity (Base_Type (Rec)));
1748
1749             --  If this is an internal type without a declaration, as for
1750             --  record component, the base type may not yet be frozen, and its
1751             --  controller has not been created. Add an explicit freeze node
1752             --  for the itype, so it will be frozen after the base type. This
1753             --  freeze node is used to communicate with the expander, in order
1754             --  to create the controller for the enclosing record, and it is
1755             --  deleted afterwards (see exp_ch3). It must not be created when
1756             --  expansion is off, because it might appear in the wrong context
1757             --  for the back end.
1758
1759             elsif Is_Itype (Rec)
1760               and then Has_Delayed_Freeze (Base_Type (Rec))
1761               and then
1762                 Nkind (Associated_Node_For_Itype (Rec)) =
1763                                                      N_Component_Declaration
1764               and then Expander_Active
1765             then
1766                Ensure_Freeze_Node (Rec);
1767             end if;
1768          end if;
1769
1770          --  Freeze components and embedded subtypes
1771
1772          Comp := First_Entity (Rec);
1773          Prev := Empty;
1774          while Present (Comp) loop
1775
1776             --  First handle the component case
1777
1778             if Ekind (Comp) = E_Component
1779               or else Ekind (Comp) = E_Discriminant
1780             then
1781                declare
1782                   CC : constant Node_Id := Component_Clause (Comp);
1783
1784                begin
1785                   --  Freezing a record type freezes the type of each of its
1786                   --  components. However, if the type of the component is
1787                   --  part of this record, we do not want or need a separate
1788                   --  Freeze_Node. Note that Is_Itype is wrong because that's
1789                   --  also set in private type cases. We also can't check for
1790                   --  the Scope being exactly Rec because of private types and
1791                   --  record extensions.
1792
1793                   if Is_Itype (Etype (Comp))
1794                     and then Is_Record_Type (Underlying_Type
1795                                              (Scope (Etype (Comp))))
1796                   then
1797                      Undelay_Type (Etype (Comp));
1798                   end if;
1799
1800                   Freeze_And_Append (Etype (Comp), Loc, Result);
1801
1802                   --  Check for error of component clause given for variable
1803                   --  sized type. We have to delay this test till this point,
1804                   --  since the component type has to be frozen for us to know
1805                   --  if it is variable length. We omit this test in a generic
1806                   --  context, it will be applied at instantiation time.
1807
1808                   if Present (CC) then
1809                      Placed_Component := True;
1810
1811                      if Inside_A_Generic then
1812                         null;
1813
1814                      elsif not
1815                        Size_Known_At_Compile_Time
1816                          (Underlying_Type (Etype (Comp)))
1817                      then
1818                         Error_Msg_N
1819                           ("component clause not allowed for variable " &
1820                            "length component", CC);
1821                      end if;
1822
1823                   else
1824                      Unplaced_Component := True;
1825                   end if;
1826
1827                   --  Case of component requires byte alignment
1828
1829                   if Must_Be_On_Byte_Boundary (Etype (Comp)) then
1830
1831                      --  Set the enclosing record to also require byte align
1832
1833                      Set_Must_Be_On_Byte_Boundary (Rec);
1834
1835                      --  Check for component clause that is inconsistent with
1836                      --  the required byte boundary alignment.
1837
1838                      if Present (CC)
1839                        and then Normalized_First_Bit (Comp) mod
1840                                   System_Storage_Unit /= 0
1841                      then
1842                         Error_Msg_N
1843                           ("component & must be byte aligned",
1844                            Component_Name (Component_Clause (Comp)));
1845                      end if;
1846                   end if;
1847                end;
1848             end if;
1849
1850             --  Gather data for possible Implicit_Packing later. Note that at
1851             --  this stage we might be dealing with a real component, or with
1852             --  an implicit subtype declaration.
1853
1854             if not Is_Scalar_Type (Etype (Comp)) then
1855                All_Scalar_Components := False;
1856             else
1857                Scalar_Component_Total_RM_Size :=
1858                  Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
1859                Scalar_Component_Total_Esize :=
1860                  Scalar_Component_Total_Esize + Esize (Etype (Comp));
1861             end if;
1862
1863             --  If the component is an Itype with Delayed_Freeze and is either
1864             --  a record or array subtype and its base type has not yet been
1865             --  frozen, we must remove this from the entity list of this
1866             --  record and put it on the entity list of the scope of its base
1867             --  type. Note that we know that this is not the type of a
1868             --  component since we cleared Has_Delayed_Freeze for it in the
1869             --  previous loop. Thus this must be the Designated_Type of an
1870             --  access type, which is the type of a component.
1871
1872             if Is_Itype (Comp)
1873               and then Is_Type (Scope (Comp))
1874               and then Is_Composite_Type (Comp)
1875               and then Base_Type (Comp) /= Comp
1876               and then Has_Delayed_Freeze (Comp)
1877               and then not Is_Frozen (Base_Type (Comp))
1878             then
1879                declare
1880                   Will_Be_Frozen : Boolean := False;
1881                   S              : Entity_Id;
1882
1883                begin
1884                   --  We have a pretty bad kludge here. Suppose Rec is subtype
1885                   --  being defined in a subprogram that's created as part of
1886                   --  the freezing of Rec'Base. In that case, we know that
1887                   --  Comp'Base must have already been frozen by the time we
1888                   --  get to elaborate this because Gigi doesn't elaborate any
1889                   --  bodies until it has elaborated all of the declarative
1890                   --  part. But Is_Frozen will not be set at this point because
1891                   --  we are processing code in lexical order.
1892
1893                   --  We detect this case by going up the Scope chain of Rec
1894                   --  and seeing if we have a subprogram scope before reaching
1895                   --  the top of the scope chain or that of Comp'Base. If we
1896                   --  do, then mark that Comp'Base will actually be frozen. If
1897                   --  so, we merely undelay it.
1898
1899                   S := Scope (Rec);
1900                   while Present (S) loop
1901                      if Is_Subprogram (S) then
1902                         Will_Be_Frozen := True;
1903                         exit;
1904                      elsif S = Scope (Base_Type (Comp)) then
1905                         exit;
1906                      end if;
1907
1908                      S := Scope (S);
1909                   end loop;
1910
1911                   if Will_Be_Frozen then
1912                      Undelay_Type (Comp);
1913                   else
1914                      if Present (Prev) then
1915                         Set_Next_Entity (Prev, Next_Entity (Comp));
1916                      else
1917                         Set_First_Entity (Rec, Next_Entity (Comp));
1918                      end if;
1919
1920                      --  Insert in entity list of scope of base type (which
1921                      --  must be an enclosing scope, because still unfrozen).
1922
1923                      Append_Entity (Comp, Scope (Base_Type (Comp)));
1924                   end if;
1925                end;
1926
1927             --  If the component is an access type with an allocator as default
1928             --  value, the designated type will be frozen by the corresponding
1929             --  expression in init_proc. In order to place the freeze node for
1930             --  the designated type before that for the current record type,
1931             --  freeze it now.
1932
1933             --  Same process if the component is an array of access types,
1934             --  initialized with an aggregate. If the designated type is
1935             --  private, it cannot contain allocators, and it is premature
1936             --  to freeze the type, so we check for this as well.
1937
1938             elsif Is_Access_Type (Etype (Comp))
1939               and then Present (Parent (Comp))
1940               and then Present (Expression (Parent (Comp)))
1941             then
1942                declare
1943                   Alloc : constant Node_Id :=
1944                             Check_Allocator (Expression (Parent (Comp)));
1945
1946                begin
1947                   if Present (Alloc) then
1948
1949                      --  If component is pointer to a classwide type, freeze
1950                      --  the specific type in the expression being allocated.
1951                      --  The expression may be a subtype indication, in which
1952                      --  case freeze the subtype mark.
1953
1954                      if Is_Class_Wide_Type
1955                           (Designated_Type (Etype (Comp)))
1956                      then
1957                         if Is_Entity_Name (Expression (Alloc)) then
1958                            Freeze_And_Append
1959                              (Entity (Expression (Alloc)), Loc, Result);
1960                         elsif
1961                           Nkind (Expression (Alloc)) = N_Subtype_Indication
1962                         then
1963                            Freeze_And_Append
1964                             (Entity (Subtype_Mark (Expression (Alloc))),
1965                               Loc, Result);
1966                         end if;
1967
1968                      elsif Is_Itype (Designated_Type (Etype (Comp))) then
1969                         Check_Itype (Etype (Comp));
1970
1971                      else
1972                         Freeze_And_Append
1973                           (Designated_Type (Etype (Comp)), Loc, Result);
1974                      end if;
1975                   end if;
1976                end;
1977
1978             elsif Is_Access_Type (Etype (Comp))
1979               and then Is_Itype (Designated_Type (Etype (Comp)))
1980             then
1981                Check_Itype (Etype (Comp));
1982
1983             elsif Is_Array_Type (Etype (Comp))
1984               and then Is_Access_Type (Component_Type (Etype (Comp)))
1985               and then Present (Parent (Comp))
1986               and then Nkind (Parent (Comp)) = N_Component_Declaration
1987               and then Present (Expression (Parent (Comp)))
1988               and then Nkind (Expression (Parent (Comp))) = N_Aggregate
1989               and then Is_Fully_Defined
1990                  (Designated_Type (Component_Type (Etype (Comp))))
1991             then
1992                Freeze_And_Append
1993                  (Designated_Type
1994                    (Component_Type (Etype (Comp))), Loc, Result);
1995             end if;
1996
1997             Prev := Comp;
1998             Next_Entity (Comp);
1999          end loop;
2000
2001          --  Deal with pragma Bit_Order setting non-standard bit order
2002
2003          if Reverse_Bit_Order (Rec) and then Base_Type (Rec) = Rec then
2004             if not Placed_Component then
2005                ADC :=
2006                  Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
2007                Error_Msg_N
2008                  ("?Bit_Order specification has no effect", ADC);
2009                Error_Msg_N
2010                  ("\?since no component clauses were specified", ADC);
2011
2012             --  Here is where we do the processing for reversed bit order
2013
2014             else
2015                Adjust_Record_For_Reverse_Bit_Order (Rec);
2016             end if;
2017          end if;
2018
2019          --  Complete error checking on record representation clause (e.g.
2020          --  overlap of components). This is called after adjusting the
2021          --  record for reverse bit order.
2022
2023          declare
2024             RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
2025          begin
2026             if Present (RRC) then
2027                Check_Record_Representation_Clause (RRC);
2028             end if;
2029          end;
2030
2031          --  Set OK_To_Reorder_Components depending on debug flags
2032
2033          if Rec = Base_Type (Rec)
2034            and then Convention (Rec) = Convention_Ada
2035          then
2036             if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
2037                   or else
2038                (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
2039             then
2040                Set_OK_To_Reorder_Components (Rec);
2041             end if;
2042          end if;
2043
2044          --  Check for useless pragma Pack when all components placed. We only
2045          --  do this check for record types, not subtypes, since a subtype may
2046          --  have all its components placed, and it still makes perfectly good
2047          --  sense to pack other subtypes or the parent type. We do not give
2048          --  this warning if Optimize_Alignment is set to Space, since the
2049          --  pragma Pack does have an effect in this case (it always resets
2050          --  the alignment to one).
2051
2052          if Ekind (Rec) = E_Record_Type
2053            and then Is_Packed (Rec)
2054            and then not Unplaced_Component
2055            and then Optimize_Alignment /= 'S'
2056          then
2057             --  Reset packed status. Probably not necessary, but we do it so
2058             --  that there is no chance of the back end doing something strange
2059             --  with this redundant indication of packing.
2060
2061             Set_Is_Packed (Rec, False);
2062
2063             --  Give warning if redundant constructs warnings on
2064
2065             if Warn_On_Redundant_Constructs then
2066                Error_Msg_N
2067                  ("?pragma Pack has no effect, no unplaced components",
2068                   Get_Rep_Pragma (Rec, Name_Pack));
2069             end if;
2070          end if;
2071
2072          --  If this is the record corresponding to a remote type, freeze the
2073          --  remote type here since that is what we are semantically freezing.
2074          --  This prevents the freeze node for that type in an inner scope.
2075
2076          --  Also, Check for controlled components and unchecked unions.
2077          --  Finally, enforce the restriction that access attributes with a
2078          --  current instance prefix can only apply to limited types.
2079
2080          if Ekind (Rec) = E_Record_Type then
2081             if Present (Corresponding_Remote_Type (Rec)) then
2082                Freeze_And_Append
2083                  (Corresponding_Remote_Type (Rec), Loc, Result);
2084             end if;
2085
2086             Comp := First_Component (Rec);
2087             while Present (Comp) loop
2088
2089                --  Do not set Has_Controlled_Component on a class-wide
2090                --  equivalent type. See Make_CW_Equivalent_Type.
2091
2092                if not Is_Class_Wide_Equivalent_Type (Rec)
2093                  and then (Has_Controlled_Component (Etype (Comp))
2094                             or else (Chars (Comp) /= Name_uParent
2095                                       and then Is_Controlled (Etype (Comp)))
2096                             or else (Is_Protected_Type (Etype (Comp))
2097                                       and then Present
2098                                         (Corresponding_Record_Type
2099                                           (Etype (Comp)))
2100                                       and then Has_Controlled_Component
2101                                         (Corresponding_Record_Type
2102                                           (Etype (Comp)))))
2103                then
2104                   Set_Has_Controlled_Component (Rec);
2105                   exit;
2106                end if;
2107
2108                if Has_Unchecked_Union (Etype (Comp)) then
2109                   Set_Has_Unchecked_Union (Rec);
2110                end if;
2111
2112                if Has_Per_Object_Constraint (Comp) then
2113
2114                   --  Scan component declaration for likely misuses of current
2115                   --  instance, either in a constraint or a default expression.
2116
2117                   Check_Current_Instance (Parent (Comp));
2118                end if;
2119
2120                Next_Component (Comp);
2121             end loop;
2122          end if;
2123
2124          Set_Component_Alignment_If_Not_Set (Rec);
2125
2126          --  For first subtypes, check if there are any fixed-point fields with
2127          --  component clauses, where we must check the size. This is not done
2128          --  till the freeze point, since for fixed-point types, we do not know
2129          --  the size until the type is frozen. Similar processing applies to
2130          --  bit packed arrays.
2131
2132          if Is_First_Subtype (Rec) then
2133             Comp := First_Component (Rec);
2134
2135             while Present (Comp) loop
2136                if Present (Component_Clause (Comp))
2137                  and then (Is_Fixed_Point_Type (Etype (Comp))
2138                              or else
2139                            Is_Bit_Packed_Array (Etype (Comp)))
2140                then
2141                   Check_Size
2142                     (Component_Name (Component_Clause (Comp)),
2143                      Etype (Comp),
2144                      Esize (Comp),
2145                      Junk);
2146                end if;
2147
2148                Next_Component (Comp);
2149             end loop;
2150          end if;
2151
2152          --  Generate warning for applying C or C++ convention to a record
2153          --  with discriminants. This is suppressed for the unchecked union
2154          --  case, since the whole point in this case is interface C. We also
2155          --  do not generate this within instantiations, since we will have
2156          --  generated a message on the template.
2157
2158          if Has_Discriminants (E)
2159            and then not Is_Unchecked_Union (E)
2160            and then (Convention (E) = Convention_C
2161                        or else
2162                      Convention (E) = Convention_CPP)
2163            and then Comes_From_Source (E)
2164            and then not In_Instance
2165            and then not Has_Warnings_Off (E)
2166            and then not Has_Warnings_Off (Base_Type (E))
2167          then
2168             declare
2169                Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
2170                A2    : Node_Id;
2171
2172             begin
2173                if Present (Cprag) then
2174                   A2 := Next (First (Pragma_Argument_Associations (Cprag)));
2175
2176                   if Convention (E) = Convention_C then
2177                      Error_Msg_N
2178                        ("?variant record has no direct equivalent in C", A2);
2179                   else
2180                      Error_Msg_N
2181                        ("?variant record has no direct equivalent in C++", A2);
2182                   end if;
2183
2184                   Error_Msg_NE
2185                     ("\?use of convention for type& is dubious", A2, E);
2186                end if;
2187             end;
2188          end if;
2189
2190          --  See if Size is too small as is (and implicit packing might help)
2191
2192          if not Is_Packed (Rec)
2193
2194            --  No implicit packing if even one component is explicitly placed
2195
2196            and then not Placed_Component
2197
2198            --  Must have size clause and all scalar components
2199
2200            and then Has_Size_Clause (Rec)
2201            and then All_Scalar_Components
2202
2203            --  Do not try implicit packing on records with discriminants, too
2204            --  complicated, especially in the variant record case.
2205
2206            and then not Has_Discriminants (Rec)
2207
2208            --  We can implicitly pack if the specified size of the record is
2209            --  less than the sum of the object sizes (no point in packing if
2210            --  this is not the case).
2211
2212            and then Esize (Rec) < Scalar_Component_Total_Esize
2213
2214            --  And the total RM size cannot be greater than the specified size
2215            --  since otherwise packing will not get us where we have to be!
2216
2217            and then Esize (Rec) >= Scalar_Component_Total_RM_Size
2218
2219            --  Never do implicit packing in CodePeer mode since we don't do
2220            --  any packing ever in this mode (why not???)
2221
2222            and then not CodePeer_Mode
2223          then
2224             --  If implicit packing enabled, do it
2225
2226             if Implicit_Packing then
2227                Set_Is_Packed (Rec);
2228
2229                --  Otherwise flag the size clause
2230
2231             else
2232                declare
2233                   Sz : constant Node_Id := Size_Clause (Rec);
2234                begin
2235                   Error_Msg_NE --  CODEFIX
2236                     ("size given for& too small", Sz, Rec);
2237                   Error_Msg_N --  CODEFIX
2238                     ("\use explicit pragma Pack "
2239                      & "or use pragma Implicit_Packing", Sz);
2240                end;
2241             end if;
2242          end if;
2243       end Freeze_Record_Type;
2244
2245    --  Start of processing for Freeze_Entity
2246
2247    begin
2248       --  We are going to test for various reasons why this entity need not be
2249       --  frozen here, but in the case of an Itype that's defined within a
2250       --  record, that test actually applies to the record.
2251
2252       if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
2253          Test_E := Scope (E);
2254       elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
2255         and then Is_Record_Type (Underlying_Type (Scope (E)))
2256       then
2257          Test_E := Underlying_Type (Scope (E));
2258       end if;
2259
2260       --  Do not freeze if already frozen since we only need one freeze node
2261
2262       if Is_Frozen (E) then
2263          return No_List;
2264
2265       --  It is improper to freeze an external entity within a generic because
2266       --  its freeze node will appear in a non-valid context. The entity will
2267       --  be frozen in the proper scope after the current generic is analyzed.
2268
2269       elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
2270          return No_List;
2271
2272       --  Do not freeze a global entity within an inner scope created during
2273       --  expansion. A call to subprogram E within some internal procedure
2274       --  (a stream attribute for example) might require freezing E, but the
2275       --  freeze node must appear in the same declarative part as E itself.
2276       --  The two-pass elaboration mechanism in gigi guarantees that E will
2277       --  be frozen before the inner call is elaborated. We exclude constants
2278       --  from this test, because deferred constants may be frozen early, and
2279       --  must be diagnosed (e.g. in the case of a deferred constant being used
2280       --  in a default expression). If the enclosing subprogram comes from
2281       --  source, or is a generic instance, then the freeze point is the one
2282       --  mandated by the language, and we freeze the entity. A subprogram that
2283       --  is a child unit body that acts as a spec does not have a spec that
2284       --  comes from source, but can only come from source.
2285
2286       elsif In_Open_Scopes (Scope (Test_E))
2287         and then Scope (Test_E) /= Current_Scope
2288         and then Ekind (Test_E) /= E_Constant
2289       then
2290          declare
2291             S : Entity_Id := Current_Scope;
2292
2293          begin
2294             while Present (S) loop
2295                if Is_Overloadable (S) then
2296                   if Comes_From_Source (S)
2297                     or else Is_Generic_Instance (S)
2298                     or else Is_Child_Unit (S)
2299                   then
2300                      exit;
2301                   else
2302                      return No_List;
2303                   end if;
2304                end if;
2305
2306                S := Scope (S);
2307             end loop;
2308          end;
2309
2310       --  Similarly, an inlined instance body may make reference to global
2311       --  entities, but these references cannot be the proper freezing point
2312       --  for them, and in the absence of inlining freezing will take place in
2313       --  their own scope. Normally instance bodies are analyzed after the
2314       --  enclosing compilation, and everything has been frozen at the proper
2315       --  place, but with front-end inlining an instance body is compiled
2316       --  before the end of the enclosing scope, and as a result out-of-order
2317       --  freezing must be prevented.
2318
2319       elsif Front_End_Inlining
2320         and then In_Instance_Body
2321         and then Present (Scope (Test_E))
2322       then
2323          declare
2324             S : Entity_Id := Scope (Test_E);
2325
2326          begin
2327             while Present (S) loop
2328                if Is_Generic_Instance (S) then
2329                   exit;
2330                else
2331                   S := Scope (S);
2332                end if;
2333             end loop;
2334
2335             if No (S) then
2336                return No_List;
2337             end if;
2338          end;
2339       end if;
2340
2341       --  Here to freeze the entity
2342
2343       Result := No_List;
2344       Set_Is_Frozen (E);
2345
2346       --  Case of entity being frozen is other than a type
2347
2348       if not Is_Type (E) then
2349
2350          --  If entity is exported or imported and does not have an external
2351          --  name, now is the time to provide the appropriate default name.
2352          --  Skip this if the entity is stubbed, since we don't need a name
2353          --  for any stubbed routine. For the case on intrinsics, if no
2354          --  external name is specified, then calls will be handled in
2355          --  Exp_Intr.Expand_Intrinsic_Call, and no name is needed; if
2356          --  an external name is provided, then Expand_Intrinsic_Call leaves
2357          --  calls in place for expansion by GIGI.
2358
2359          if (Is_Imported (E) or else Is_Exported (E))
2360            and then No (Interface_Name (E))
2361            and then Convention (E) /= Convention_Stubbed
2362            and then Convention (E) /= Convention_Intrinsic
2363          then
2364             Set_Encoded_Interface_Name
2365               (E, Get_Default_External_Name (E));
2366
2367          --  If entity is an atomic object appearing in a declaration and
2368          --  the expression is an aggregate, assign it to a temporary to
2369          --  ensure that the actual assignment is done atomically rather
2370          --  than component-wise (the assignment to the temp may be done
2371          --  component-wise, but that is harmless).
2372
2373          elsif Is_Atomic (E)
2374            and then Nkind (Parent (E)) = N_Object_Declaration
2375            and then Present (Expression (Parent (E)))
2376            and then Nkind (Expression (Parent (E))) = N_Aggregate
2377            and then
2378              Is_Atomic_Aggregate (Expression (Parent (E)), Etype (E))
2379          then
2380             null;
2381          end if;
2382
2383          --  For a subprogram, freeze all parameter types and also the return
2384          --  type (RM 13.14(14)). However skip this for internal subprograms.
2385          --  This is also the point where any extra formal parameters are
2386          --  created since we now know whether the subprogram will use a
2387          --  foreign convention.
2388
2389          if Is_Subprogram (E) then
2390             if not Is_Internal (E) then
2391                declare
2392                   F_Type    : Entity_Id;
2393                   R_Type    : Entity_Id;
2394                   Warn_Node : Node_Id;
2395
2396                begin
2397                   --  Loop through formals
2398
2399                   Formal := First_Formal (E);
2400                   while Present (Formal) loop
2401                      F_Type := Etype (Formal);
2402                      Freeze_And_Append (F_Type, Loc, Result);
2403
2404                      if Is_Private_Type (F_Type)
2405                        and then Is_Private_Type (Base_Type (F_Type))
2406                        and then No (Full_View (Base_Type (F_Type)))
2407                        and then not Is_Generic_Type (F_Type)
2408                        and then not Is_Derived_Type (F_Type)
2409                      then
2410                         --  If the type of a formal is incomplete, subprogram
2411                         --  is being frozen prematurely. Within an instance
2412                         --  (but not within a wrapper package) this is an
2413                         --  artifact of our need to regard the end of an
2414                         --  instantiation as a freeze point. Otherwise it is
2415                         --  a definite error.
2416
2417                         if In_Instance then
2418                            Set_Is_Frozen (E, False);
2419                            return No_List;
2420
2421                         elsif not After_Last_Declaration
2422                           and then not Freezing_Library_Level_Tagged_Type
2423                         then
2424                            Error_Msg_Node_1 := F_Type;
2425                            Error_Msg
2426                              ("type& must be fully defined before this point",
2427                                Loc);
2428                         end if;
2429                      end if;
2430
2431                      --  Check suspicious parameter for C function. These tests
2432                      --  apply only to exported/imported subprograms.
2433
2434                      if Warn_On_Export_Import
2435                        and then Comes_From_Source (E)
2436                        and then (Convention (E) = Convention_C
2437                                    or else
2438                                  Convention (E) = Convention_CPP)
2439                        and then (Is_Imported (E) or else Is_Exported (E))
2440                        and then Convention (E) /= Convention (Formal)
2441                        and then not Has_Warnings_Off (E)
2442                        and then not Has_Warnings_Off (F_Type)
2443                        and then not Has_Warnings_Off (Formal)
2444                      then
2445                         --  Qualify mention of formals with subprogram name
2446
2447                         Error_Msg_Qual_Level := 1;
2448
2449                         --  Check suspicious use of fat C pointer
2450
2451                         if Is_Access_Type (F_Type)
2452                           and then Esize (F_Type) > Ttypes.System_Address_Size
2453                         then
2454                            Error_Msg_N
2455                              ("?type of & does not correspond to C pointer!",
2456                               Formal);
2457
2458                         --  Check suspicious return of boolean
2459
2460                         elsif Root_Type (F_Type) = Standard_Boolean
2461                           and then Convention (F_Type) = Convention_Ada
2462                           and then not Has_Warnings_Off (F_Type)
2463                           and then not Has_Size_Clause (F_Type)
2464                           and then VM_Target = No_VM
2465                         then
2466                            Error_Msg_N
2467                              ("& is an 8-bit Ada Boolean?", Formal);
2468                            Error_Msg_N
2469                              ("\use appropriate corresponding type in C "
2470                               & "(e.g. char)?", Formal);
2471
2472                         --  Check suspicious tagged type
2473
2474                         elsif (Is_Tagged_Type (F_Type)
2475                                 or else (Is_Access_Type (F_Type)
2476                                            and then
2477                                              Is_Tagged_Type
2478                                                (Designated_Type (F_Type))))
2479                           and then Convention (E) = Convention_C
2480                         then
2481                            Error_Msg_N
2482                              ("?& involves a tagged type which does not "
2483                               & "correspond to any C type!", Formal);
2484
2485                         --  Check wrong convention subprogram pointer
2486
2487                         elsif Ekind (F_Type) = E_Access_Subprogram_Type
2488                           and then not Has_Foreign_Convention (F_Type)
2489                         then
2490                            Error_Msg_N
2491                              ("?subprogram pointer & should "
2492                               & "have foreign convention!", Formal);
2493                            Error_Msg_Sloc := Sloc (F_Type);
2494                            Error_Msg_NE
2495                              ("\?add Convention pragma to declaration of &#",
2496                               Formal, F_Type);
2497                         end if;
2498
2499                         --  Turn off name qualification after message output
2500
2501                         Error_Msg_Qual_Level := 0;
2502                      end if;
2503
2504                      --  Check for unconstrained array in exported foreign
2505                      --  convention case.
2506
2507                      if Has_Foreign_Convention (E)
2508                        and then not Is_Imported (E)
2509                        and then Is_Array_Type (F_Type)
2510                        and then not Is_Constrained (F_Type)
2511                        and then Warn_On_Export_Import
2512
2513                        --  Exclude VM case, since both .NET and JVM can handle
2514                        --  unconstrained arrays without a problem.
2515
2516                        and then VM_Target = No_VM
2517                      then
2518                         Error_Msg_Qual_Level := 1;
2519
2520                         --  If this is an inherited operation, place the
2521                         --  warning on the derived type declaration, rather
2522                         --  than on the original subprogram.
2523
2524                         if Nkind (Original_Node (Parent (E))) =
2525                           N_Full_Type_Declaration
2526                         then
2527                            Warn_Node := Parent (E);
2528
2529                            if Formal = First_Formal (E) then
2530                               Error_Msg_NE
2531                                 ("?in inherited operation&", Warn_Node, E);
2532                            end if;
2533                         else
2534                            Warn_Node := Formal;
2535                         end if;
2536
2537                         Error_Msg_NE
2538                           ("?type of argument& is unconstrained array",
2539                            Warn_Node, Formal);
2540                         Error_Msg_NE
2541                           ("?foreign caller must pass bounds explicitly",
2542                            Warn_Node, Formal);
2543                         Error_Msg_Qual_Level := 0;
2544                      end if;
2545
2546                      if not From_With_Type (F_Type) then
2547                         if Is_Access_Type (F_Type) then
2548                            F_Type := Designated_Type (F_Type);
2549                         end if;
2550
2551                         --  If the formal is an anonymous_access_to_subprogram
2552                         --  freeze the  subprogram type as well, to prevent
2553                         --  scope anomalies in gigi, because there is no other
2554                         --  clear point at which it could be frozen.
2555
2556                         if Is_Itype (Etype (Formal))
2557                           and then Ekind (F_Type) = E_Subprogram_Type
2558                         then
2559                            Freeze_And_Append (F_Type, Loc, Result);
2560                         end if;
2561                      end if;
2562
2563                      Next_Formal (Formal);
2564                   end loop;
2565
2566                   --  Case of function: similar checks on return type
2567
2568                   if Ekind (E) = E_Function then
2569
2570                      --  Freeze return type
2571
2572                      R_Type := Etype (E);
2573                      Freeze_And_Append (R_Type, Loc, Result);
2574
2575                      --  Check suspicious return type for C function
2576
2577                      if Warn_On_Export_Import
2578                        and then (Convention (E) = Convention_C
2579                                    or else
2580                                  Convention (E) = Convention_CPP)
2581                        and then (Is_Imported (E) or else Is_Exported (E))
2582                      then
2583                         --  Check suspicious return of fat C pointer
2584
2585                         if Is_Access_Type (R_Type)
2586                           and then Esize (R_Type) > Ttypes.System_Address_Size
2587                           and then not Has_Warnings_Off (E)
2588                           and then not Has_Warnings_Off (R_Type)
2589                         then
2590                            Error_Msg_N
2591                              ("?return type of& does not "
2592                               & "correspond to C pointer!", E);
2593
2594                         --  Check suspicious return of boolean
2595
2596                         elsif Root_Type (R_Type) = Standard_Boolean
2597                           and then Convention (R_Type) = Convention_Ada
2598                           and then VM_Target = No_VM
2599                           and then not Has_Warnings_Off (E)
2600                           and then not Has_Warnings_Off (R_Type)
2601                           and then not Has_Size_Clause (R_Type)
2602                         then
2603                            declare
2604                               N : constant Node_Id :=
2605                                     Result_Definition (Declaration_Node (E));
2606                            begin
2607                               Error_Msg_NE
2608                                 ("return type of & is an 8-bit Ada Boolean?",
2609                                  N, E);
2610                               Error_Msg_NE
2611                                 ("\use appropriate corresponding type in C "
2612                                  & "(e.g. char)?", N, E);
2613                            end;
2614
2615                         --  Check suspicious return tagged type
2616
2617                         elsif (Is_Tagged_Type (R_Type)
2618                                 or else (Is_Access_Type (R_Type)
2619                                            and then
2620                                              Is_Tagged_Type
2621                                                (Designated_Type (R_Type))))
2622                           and then Convention (E) = Convention_C
2623                           and then not Has_Warnings_Off (E)
2624                           and then not Has_Warnings_Off (R_Type)
2625                         then
2626                            Error_Msg_N
2627                              ("?return type of & does not "
2628                               & "correspond to C type!", E);
2629
2630                         --  Check return of wrong convention subprogram pointer
2631
2632                         elsif Ekind (R_Type) = E_Access_Subprogram_Type
2633                           and then not Has_Foreign_Convention (R_Type)
2634                           and then not Has_Warnings_Off (E)
2635                           and then not Has_Warnings_Off (R_Type)
2636                         then
2637                            Error_Msg_N
2638                              ("?& should return a foreign "
2639                               & "convention subprogram pointer", E);
2640                            Error_Msg_Sloc := Sloc (R_Type);
2641                            Error_Msg_NE
2642                              ("\?add Convention pragma to declaration of& #",
2643                               E, R_Type);
2644                         end if;
2645                      end if;
2646
2647                      --  Give warning for suspicous return of a result of an
2648                      --  unconstrained array type in a foreign convention
2649                      --  function.
2650
2651                      if Has_Foreign_Convention (E)
2652
2653                        --  We are looking for a return of unconstrained array
2654
2655                        and then Is_Array_Type (R_Type)
2656                        and then not Is_Constrained (R_Type)
2657
2658                        --  Exclude imported routines, the warning does not
2659                        --  belong on the import, but on the routine definition.
2660
2661                        and then not Is_Imported (E)
2662
2663                        --  Exclude VM case, since both .NET and JVM can handle
2664                        --  return of unconstrained arrays without a problem.
2665
2666                        and then VM_Target = No_VM
2667
2668                        --  Check that general warning is enabled, and that it
2669                        --  is not suppressed for this particular case.
2670
2671                        and then Warn_On_Export_Import
2672                        and then not Has_Warnings_Off (E)
2673                        and then not Has_Warnings_Off (R_Type)
2674                      then
2675                         Error_Msg_N
2676                           ("?foreign convention function& should not " &
2677                            "return unconstrained array!", E);
2678                      end if;
2679                   end if;
2680                end;
2681             end if;
2682
2683             --  Must freeze its parent first if it is a derived subprogram
2684
2685             if Present (Alias (E)) then
2686                Freeze_And_Append (Alias (E), Loc, Result);
2687             end if;
2688
2689             --  We don't freeze internal subprograms, because we don't normally
2690             --  want addition of extra formals or mechanism setting to happen
2691             --  for those. However we do pass through predefined dispatching
2692             --  cases, since extra formals may be needed in some cases, such as
2693             --  for the stream 'Input function (build-in-place formals).
2694
2695             if not Is_Internal (E)
2696               or else Is_Predefined_Dispatching_Operation (E)
2697             then
2698                Freeze_Subprogram (E);
2699             end if;
2700
2701          --  Here for other than a subprogram or type
2702
2703          else
2704             --  If entity has a type, and it is not a generic unit, then
2705             --  freeze it first (RM 13.14(10)).
2706
2707             if Present (Etype (E))
2708               and then Ekind (E) /= E_Generic_Function
2709             then
2710                Freeze_And_Append (Etype (E), Loc, Result);
2711             end if;
2712
2713             --  Special processing for objects created by object declaration
2714
2715             if Nkind (Declaration_Node (E)) = N_Object_Declaration then
2716
2717                --  Abstract type allowed only for C++ imported variables or
2718                --  constants.
2719
2720                --  Note: we inhibit this check for objects that do not come
2721                --  from source because there is at least one case (the
2722                --  expansion of x'class'input where x is abstract) where we
2723                --  legitimately generate an abstract object.
2724
2725                if Is_Abstract_Type (Etype (E))
2726                  and then Comes_From_Source (Parent (E))
2727                  and then not (Is_Imported (E)
2728                                  and then Is_CPP_Class (Etype (E)))
2729                then
2730                   Error_Msg_N ("type of object cannot be abstract",
2731                                Object_Definition (Parent (E)));
2732
2733                   if Is_CPP_Class (Etype (E)) then
2734                      Error_Msg_NE ("\} may need a cpp_constructor",
2735                        Object_Definition (Parent (E)), Etype (E));
2736                   end if;
2737                end if;
2738
2739                --  For object created by object declaration, perform required
2740                --  categorization (preelaborate and pure) checks. Defer these
2741                --  checks to freeze time since pragma Import inhibits default
2742                --  initialization and thus pragma Import affects these checks.
2743
2744                Validate_Object_Declaration (Declaration_Node (E));
2745
2746                --  If there is an address clause, check that it is valid
2747
2748                Check_Address_Clause (E);
2749
2750                --  If the object needs any kind of default initialization, an
2751                --  error must be issued if No_Default_Initialization applies.
2752                --  The check doesn't apply to imported objects, which are not
2753                --  ever default initialized, and is why the check is deferred
2754                --  until freezing, at which point we know if Import applies.
2755                --  Deferred constants are also exempted from this test because
2756                --  their completion is explicit, or through an import pragma.
2757
2758                if Ekind (E) = E_Constant
2759                  and then Present (Full_View (E))
2760                then
2761                   null;
2762
2763                elsif Comes_From_Source (E)
2764                  and then not Is_Imported (E)
2765                  and then not Has_Init_Expression (Declaration_Node (E))
2766                  and then
2767                    ((Has_Non_Null_Base_Init_Proc (Etype (E))
2768                       and then not No_Initialization (Declaration_Node (E))
2769                       and then not Is_Value_Type (Etype (E))
2770                       and then not Suppress_Init_Proc (Etype (E)))
2771                     or else
2772                       (Needs_Simple_Initialization (Etype (E))
2773                         and then not Is_Internal (E)))
2774                then
2775                   Has_Default_Initialization := True;
2776                   Check_Restriction
2777                     (No_Default_Initialization, Declaration_Node (E));
2778                end if;
2779
2780                --  Check that a Thread_Local_Storage variable does not have
2781                --  default initialization, and any explicit initialization must
2782                --  either be the null constant or a static constant.
2783
2784                if Has_Pragma_Thread_Local_Storage (E) then
2785                   declare
2786                      Decl : constant Node_Id := Declaration_Node (E);
2787                   begin
2788                      if Has_Default_Initialization
2789                        or else
2790                          (Has_Init_Expression (Decl)
2791                             and then
2792                              (No (Expression (Decl))
2793                                 or else not
2794                                   (Is_Static_Expression (Expression (Decl))
2795                                      or else
2796                                    Nkind (Expression (Decl)) = N_Null)))
2797                      then
2798                         Error_Msg_NE
2799                           ("Thread_Local_Storage variable& is "
2800                            & "improperly initialized", Decl, E);
2801                         Error_Msg_NE
2802                           ("\only allowed initialization is explicit "
2803                            & "NULL or static expression", Decl, E);
2804                      end if;
2805                   end;
2806                end if;
2807
2808                --  For imported objects, set Is_Public unless there is also an
2809                --  address clause, which means that there is no external symbol
2810                --  needed for the Import (Is_Public may still be set for other
2811                --  unrelated reasons). Note that we delayed this processing
2812                --  till freeze time so that we can be sure not to set the flag
2813                --  if there is an address clause. If there is such a clause,
2814                --  then the only purpose of the Import pragma is to suppress
2815                --  implicit initialization.
2816
2817                if Is_Imported (E)
2818                  and then No (Address_Clause (E))
2819                then
2820                   Set_Is_Public (E);
2821                end if;
2822
2823                --  For convention C objects of an enumeration type, warn if
2824                --  the size is not integer size and no explicit size given.
2825                --  Skip warning for Boolean, and Character, assume programmer
2826                --  expects 8-bit sizes for these cases.
2827
2828                if (Convention (E) = Convention_C
2829                     or else
2830                    Convention (E) = Convention_CPP)
2831                  and then Is_Enumeration_Type (Etype (E))
2832                  and then not Is_Character_Type (Etype (E))
2833                  and then not Is_Boolean_Type (Etype (E))
2834                  and then Esize (Etype (E)) < Standard_Integer_Size
2835                  and then not Has_Size_Clause (E)
2836                then
2837                   Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
2838                   Error_Msg_N
2839                     ("?convention C enumeration object has size less than ^",
2840                      E);
2841                   Error_Msg_N ("\?use explicit size clause to set size", E);
2842                end if;
2843             end if;
2844
2845             --  Check that a constant which has a pragma Volatile[_Components]
2846             --  or Atomic[_Components] also has a pragma Import (RM C.6(13)).
2847
2848             --  Note: Atomic[_Components] also sets Volatile[_Components]
2849
2850             if Ekind (E) = E_Constant
2851               and then (Has_Volatile_Components (E) or else Is_Volatile (E))
2852               and then not Is_Imported (E)
2853             then
2854                --  Make sure we actually have a pragma, and have not merely
2855                --  inherited the indication from elsewhere (e.g. an address
2856                --  clause, which is not good enough in RM terms!)
2857
2858                if Has_Rep_Pragma (E, Name_Atomic)
2859                     or else
2860                   Has_Rep_Pragma (E, Name_Atomic_Components)
2861                then
2862                   Error_Msg_N
2863                     ("stand alone atomic constant must be " &
2864                      "imported (RM C.6(13))", E);
2865
2866                elsif Has_Rep_Pragma (E, Name_Volatile)
2867                        or else
2868                      Has_Rep_Pragma (E, Name_Volatile_Components)
2869                then
2870                   Error_Msg_N
2871                     ("stand alone volatile constant must be " &
2872                      "imported (RM C.6(13))", E);
2873                end if;
2874             end if;
2875
2876             --  Static objects require special handling
2877
2878             if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
2879               and then Is_Statically_Allocated (E)
2880             then
2881                Freeze_Static_Object (E);
2882             end if;
2883
2884             --  Remaining step is to layout objects
2885
2886             if Ekind (E) = E_Variable
2887                  or else
2888                Ekind (E) = E_Constant
2889                  or else
2890                Ekind (E) = E_Loop_Parameter
2891                  or else
2892                Is_Formal (E)
2893             then
2894                Layout_Object (E);
2895             end if;
2896          end if;
2897
2898       --  Case of a type or subtype being frozen
2899
2900       else
2901          --  We used to check here that a full type must have preelaborable
2902          --  initialization if it completes a private type specified with
2903          --  pragma Preelaborable_Intialization, but that missed cases where
2904          --  the types occur within a generic package, since the freezing
2905          --  that occurs within a containing scope generally skips traversal
2906          --  of a generic unit's declarations (those will be frozen within
2907          --  instances). This check was moved to Analyze_Package_Specification.
2908
2909          --  The type may be defined in a generic unit. This can occur when
2910          --  freezing a generic function that returns the type (which is
2911          --  defined in a parent unit). It is clearly meaningless to freeze
2912          --  this type. However, if it is a subtype, its size may be determi-
2913          --  nable and used in subsequent checks, so might as well try to
2914          --  compute it.
2915
2916          if Present (Scope (E))
2917            and then Is_Generic_Unit (Scope (E))
2918          then
2919             Check_Compile_Time_Size (E);
2920             return No_List;
2921          end if;
2922
2923          --  Deal with special cases of freezing for subtype
2924
2925          if E /= Base_Type (E) then
2926
2927             --  Before we do anything else, a specialized test for the case of
2928             --  a size given for an array where the array needs to be packed,
2929             --  but was not so the size cannot be honored. This would of course
2930             --  be caught by the backend, and indeed we don't catch all cases.
2931             --  The point is that we can give a better error message in those
2932             --  cases that we do catch with the circuitry here. Also if pragma
2933             --  Implicit_Packing is set, this is where the packing occurs.
2934
2935             --  The reason we do this so early is that the processing in the
2936             --  automatic packing case affects the layout of the base type, so
2937             --  it must be done before we freeze the base type.
2938
2939             if Is_Array_Type (E) then
2940                declare
2941                   Lo, Hi : Node_Id;
2942                   Ctyp   : constant Entity_Id := Component_Type (E);
2943
2944                begin
2945                   --  Check enabling conditions. These are straightforward
2946                   --  except for the test for a limited composite type. This
2947                   --  eliminates the rare case of a array of limited components
2948                   --  where there are issues of whether or not we can go ahead
2949                   --  and pack the array (since we can't freely pack and unpack
2950                   --  arrays if they are limited).
2951
2952                   --  Note that we check the root type explicitly because the
2953                   --  whole point is we are doing this test before we have had
2954                   --  a chance to freeze the base type (and it is that freeze
2955                   --  action that causes stuff to be inherited).
2956
2957                   if Present (Size_Clause (E))
2958                     and then Known_Static_Esize (E)
2959                     and then not Is_Packed (E)
2960                     and then not Has_Pragma_Pack (E)
2961                     and then Number_Dimensions (E) = 1
2962                     and then not Has_Component_Size_Clause (E)
2963                     and then Known_Static_Esize (Ctyp)
2964                     and then not Is_Limited_Composite (E)
2965                     and then not Is_Packed (Root_Type (E))
2966                     and then not Has_Component_Size_Clause (Root_Type (E))
2967                     and then not CodePeer_Mode
2968                   then
2969                      Get_Index_Bounds (First_Index (E), Lo, Hi);
2970
2971                      if Compile_Time_Known_Value (Lo)
2972                        and then Compile_Time_Known_Value (Hi)
2973                        and then Known_Static_RM_Size (Ctyp)
2974                        and then RM_Size (Ctyp) < 64
2975                      then
2976                         declare
2977                            Lov  : constant Uint      := Expr_Value (Lo);
2978                            Hiv  : constant Uint      := Expr_Value (Hi);
2979                            Len  : constant Uint      := UI_Max
2980                                                          (Uint_0,
2981                                                           Hiv - Lov + 1);
2982                            Rsiz : constant Uint      := RM_Size (Ctyp);
2983                            SZ   : constant Node_Id   := Size_Clause (E);
2984                            Btyp : constant Entity_Id := Base_Type (E);
2985
2986                         --  What we are looking for here is the situation where
2987                         --  the RM_Size given would be exactly right if there
2988                         --  was a pragma Pack (resulting in the component size
2989                         --  being the same as the RM_Size). Furthermore, the
2990                         --  component type size must be an odd size (not a
2991                         --  multiple of storage unit). If the component RM size
2992                         --  is an exact number of storage units that is a power
2993                         --  of two, the array is not packed and has a standard
2994                         --  representation.
2995
2996                         begin
2997                            if RM_Size (E) = Len * Rsiz
2998                              and then Rsiz mod System_Storage_Unit /= 0
2999                            then
3000                               --  For implicit packing mode, just set the
3001                               --  component size silently.
3002
3003                               if Implicit_Packing then
3004                                  Set_Component_Size       (Btyp, Rsiz);
3005                                  Set_Is_Bit_Packed_Array  (Btyp);
3006                                  Set_Is_Packed            (Btyp);
3007                                  Set_Has_Non_Standard_Rep (Btyp);
3008
3009                                  --  Otherwise give an error message
3010
3011                               else
3012                                  Error_Msg_NE
3013                                    ("size given for& too small", SZ, E);
3014                                  Error_Msg_N
3015                                    ("\use explicit pragma Pack "
3016                                     & "or use pragma Implicit_Packing", SZ);
3017                               end if;
3018
3019                            elsif RM_Size (E) = Len * Rsiz
3020                              and then Implicit_Packing
3021                              and then
3022                                (Rsiz / System_Storage_Unit = 1
3023                                  or else Rsiz / System_Storage_Unit = 2
3024                                  or else Rsiz / System_Storage_Unit = 4)
3025                            then
3026
3027                               --  Not a packed array, but indicate the desired
3028                               --  component size, for the back-end.
3029
3030                               Set_Component_Size (Btyp, Rsiz);
3031                            end if;
3032                         end;
3033                      end if;
3034                   end if;
3035                end;
3036             end if;
3037
3038             --  If ancestor subtype present, freeze that first. Note that this
3039             --  will also get the base type frozen.
3040
3041             Atype := Ancestor_Subtype (E);
3042
3043             if Present (Atype) then
3044                Freeze_And_Append (Atype, Loc, Result);
3045
3046             --  Otherwise freeze the base type of the entity before freezing
3047             --  the entity itself (RM 13.14(15)).
3048
3049             elsif E /= Base_Type (E) then
3050                Freeze_And_Append (Base_Type (E), Loc, Result);
3051             end if;
3052
3053          --  For a derived type, freeze its parent type first (RM 13.14(15))
3054
3055          elsif Is_Derived_Type (E) then
3056             Freeze_And_Append (Etype (E), Loc, Result);
3057             Freeze_And_Append (First_Subtype (Etype (E)), Loc, Result);
3058          end if;
3059
3060          --  For array type, freeze index types and component type first
3061          --  before freezing the array (RM 13.14(15)).
3062
3063          if Is_Array_Type (E) then
3064             declare
3065                Ctyp : constant Entity_Id := Component_Type (E);
3066
3067                Non_Standard_Enum : Boolean := False;
3068                --  Set true if any of the index types is an enumeration type
3069                --  with a non-standard representation.
3070
3071             begin
3072                Freeze_And_Append (Ctyp, Loc, Result);
3073
3074                Indx := First_Index (E);
3075                while Present (Indx) loop
3076                   Freeze_And_Append (Etype (Indx), Loc, Result);
3077
3078                   if Is_Enumeration_Type (Etype (Indx))
3079                     and then Has_Non_Standard_Rep (Etype (Indx))
3080                   then
3081                      Non_Standard_Enum := True;
3082                   end if;
3083
3084                   Next_Index (Indx);
3085                end loop;
3086
3087                --  Processing that is done only for base types
3088
3089                if Ekind (E) = E_Array_Type then
3090
3091                   --  Propagate flags for component type
3092
3093                   if Is_Controlled (Component_Type (E))
3094                     or else Has_Controlled_Component (Ctyp)
3095                   then
3096                      Set_Has_Controlled_Component (E);
3097                   end if;
3098
3099                   if Has_Unchecked_Union (Component_Type (E)) then
3100                      Set_Has_Unchecked_Union (E);
3101                   end if;
3102
3103                   --  If packing was requested or if the component size was set
3104                   --  explicitly, then see if bit packing is required. This
3105                   --  processing is only done for base types, since all the
3106                   --  representation aspects involved are type-related. This
3107                   --  is not just an optimization, if we start processing the
3108                   --  subtypes, they interfere with the settings on the base
3109                   --  type (this is because Is_Packed has a slightly different
3110                   --  meaning before and after freezing).
3111
3112                   declare
3113                      Csiz : Uint;
3114                      Esiz : Uint;
3115
3116                   begin
3117                      if (Is_Packed (E) or else Has_Pragma_Pack (E))
3118                        and then not Has_Atomic_Components (E)
3119                        and then Known_Static_RM_Size (Ctyp)
3120                      then
3121                         Csiz := UI_Max (RM_Size (Ctyp), 1);
3122
3123                      elsif Known_Component_Size (E) then
3124                         Csiz := Component_Size (E);
3125
3126                      elsif not Known_Static_Esize (Ctyp) then
3127                         Csiz := Uint_0;
3128
3129                      else
3130                         Esiz := Esize (Ctyp);
3131
3132                         --  We can set the component size if it is less than
3133                         --  16, rounding it up to the next storage unit size.
3134
3135                         if Esiz <= 8 then
3136                            Csiz := Uint_8;
3137                         elsif Esiz <= 16 then
3138                            Csiz := Uint_16;
3139                         else
3140                            Csiz := Uint_0;
3141                         end if;
3142
3143                         --  Set component size up to match alignment if it
3144                         --  would otherwise be less than the alignment. This
3145                         --  deals with cases of types whose alignment exceeds
3146                         --  their size (padded types).
3147
3148                         if Csiz /= 0 then
3149                            declare
3150                               A : constant Uint := Alignment_In_Bits (Ctyp);
3151                            begin
3152                               if Csiz < A then
3153                                  Csiz := A;
3154                               end if;
3155                            end;
3156                         end if;
3157                      end if;
3158
3159                      --  Case of component size that may result in packing
3160
3161                      if 1 <= Csiz and then Csiz <= 64 then
3162                         declare
3163                            Ent         : constant Entity_Id :=
3164                                            First_Subtype (E);
3165                            Pack_Pragma : constant Node_Id :=
3166                                            Get_Rep_Pragma (Ent, Name_Pack);
3167                            Comp_Size_C : constant Node_Id :=
3168                                            Get_Attribute_Definition_Clause
3169                                              (Ent, Attribute_Component_Size);
3170                         begin
3171                            --  Warn if we have pack and component size so that
3172                            --  the pack is ignored.
3173
3174                            --  Note: here we must check for the presence of a
3175                            --  component size before checking for a Pack pragma
3176                            --  to deal with the case where the array type is a
3177                            --  derived type whose parent is currently private.
3178
3179                            if Present (Comp_Size_C)
3180                              and then Has_Pragma_Pack (Ent)
3181                            then
3182                               Error_Msg_Sloc := Sloc (Comp_Size_C);
3183                               Error_Msg_NE
3184                                 ("?pragma Pack for& ignored!",
3185                                  Pack_Pragma, Ent);
3186                               Error_Msg_N
3187                                 ("\?explicit component size given#!",
3188                                  Pack_Pragma);
3189                            end if;
3190
3191                            --  Set component size if not already set by a
3192                            --  component size clause.
3193
3194                            if not Present (Comp_Size_C) then
3195                               Set_Component_Size (E, Csiz);
3196                            end if;
3197
3198                            --  Check for base type of 8, 16, 32 bits, where an
3199                            --  unsigned subtype has a length one less than the
3200                            --  base type (e.g. Natural subtype of Integer).
3201
3202                            --  In such cases, if a component size was not set
3203                            --  explicitly, then generate a warning.
3204
3205                            if Has_Pragma_Pack (E)
3206                              and then not Present (Comp_Size_C)
3207                              and then
3208                                (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
3209                              and then Esize (Base_Type (Ctyp)) = Csiz + 1
3210                            then
3211                               Error_Msg_Uint_1 := Csiz;
3212
3213                               if Present (Pack_Pragma) then
3214                                  Error_Msg_N
3215                                    ("?pragma Pack causes component size "
3216                                     & "to be ^!", Pack_Pragma);
3217                                  Error_Msg_N
3218                                    ("\?use Component_Size to set "
3219                                     & "desired value!", Pack_Pragma);
3220                               end if;
3221                            end if;
3222
3223                            --  Actual packing is not needed for 8, 16, 32, 64.
3224                            --  Also not needed for 24 if alignment is 1.
3225
3226                            if        Csiz = 8
3227                              or else Csiz = 16
3228                              or else Csiz = 32
3229                              or else Csiz = 64
3230                              or else (Csiz = 24 and then Alignment (Ctyp) = 1)
3231                            then
3232                               --  Here the array was requested to be packed,
3233                               --  but the packing request had no effect, so
3234                               --  Is_Packed is reset.
3235
3236                               --  Note: semantically this means that we lose
3237                               --  track of the fact that a derived type
3238                               --  inherited a pragma Pack that was non-
3239                               --  effective, but that seems fine.
3240
3241                               --  We regard a Pack pragma as a request to set
3242                               --  a representation characteristic, and this
3243                               --  request may be ignored.
3244
3245                               Set_Is_Packed (Base_Type (E), False);
3246
3247                               --  In all other cases, packing is indeed needed
3248
3249                            else
3250                               Set_Has_Non_Standard_Rep (Base_Type (E));
3251                               Set_Is_Bit_Packed_Array  (Base_Type (E));
3252                               Set_Is_Packed            (Base_Type (E));
3253                            end if;
3254                         end;
3255                      end if;
3256                   end;
3257
3258                --  Processing that is done only for subtypes
3259
3260                else
3261                   --  Acquire alignment from base type
3262
3263                   if Unknown_Alignment (E) then
3264                      Set_Alignment (E, Alignment (Base_Type (E)));
3265                      Adjust_Esize_Alignment (E);
3266                   end if;
3267                end if;
3268
3269                --  For bit-packed arrays, check the size
3270
3271                if Is_Bit_Packed_Array (E) and then Known_RM_Size (E) then
3272                   declare
3273                      SizC : constant Node_Id := Size_Clause (E);
3274
3275                      Discard : Boolean;
3276                      pragma Warnings (Off, Discard);
3277
3278                   begin
3279                      --  It is not clear if it is possible to have no size
3280                      --  clause at this stage, but it is not worth worrying
3281                      --  about. Post error on the entity name in the size
3282                      --  clause if present, else on the type entity itself.
3283
3284                      if Present (SizC) then
3285                         Check_Size (Name (SizC), E, RM_Size (E), Discard);
3286                      else
3287                         Check_Size (E, E, RM_Size (E), Discard);
3288                      end if;
3289                   end;
3290                end if;
3291
3292                --  If any of the index types was an enumeration type with
3293                --  a non-standard rep clause, then we indicate that the
3294                --  array type is always packed (even if it is not bit packed).
3295
3296                if Non_Standard_Enum then
3297                   Set_Has_Non_Standard_Rep (Base_Type (E));
3298                   Set_Is_Packed            (Base_Type (E));
3299                end if;
3300
3301                Set_Component_Alignment_If_Not_Set (E);
3302
3303                --  If the array is packed, we must create the packed array
3304                --  type to be used to actually implement the type. This is
3305                --  only needed for real array types (not for string literal
3306                --  types, since they are present only for the front end).
3307
3308                if Is_Packed (E)
3309                  and then Ekind (E) /= E_String_Literal_Subtype
3310                then
3311                   Create_Packed_Array_Type (E);
3312                   Freeze_And_Append (Packed_Array_Type (E), Loc, Result);
3313
3314                   --  Size information of packed array type is copied to the
3315                   --  array type, since this is really the representation. But
3316                   --  do not override explicit existing size values. If the
3317                   --  ancestor subtype is constrained the packed_array_type
3318                   --  will be inherited from it, but the size may have been
3319                   --  provided already, and must not be overridden either.
3320
3321                   if not Has_Size_Clause (E)
3322                     and then
3323                       (No (Ancestor_Subtype (E))
3324                         or else not Has_Size_Clause (Ancestor_Subtype (E)))
3325                   then
3326                      Set_Esize     (E, Esize     (Packed_Array_Type (E)));
3327                      Set_RM_Size   (E, RM_Size   (Packed_Array_Type (E)));
3328                   end if;
3329
3330                   if not Has_Alignment_Clause (E) then
3331                      Set_Alignment (E, Alignment (Packed_Array_Type (E)));
3332                   end if;
3333                end if;
3334
3335                --  For non-packed arrays set the alignment of the array to the
3336                --  alignment of the component type if it is unknown. Skip this
3337                --  in atomic case (atomic arrays may need larger alignments).
3338
3339                if not Is_Packed (E)
3340                  and then Unknown_Alignment (E)
3341                  and then Known_Alignment (Ctyp)
3342                  and then Known_Static_Component_Size (E)
3343                  and then Known_Static_Esize (Ctyp)
3344                  and then Esize (Ctyp) = Component_Size (E)
3345                  and then not Is_Atomic (E)
3346                then
3347                   Set_Alignment (E, Alignment (Component_Type (E)));
3348                end if;
3349             end;
3350
3351          --  For a class-wide type, the corresponding specific type is
3352          --  frozen as well (RM 13.14(15))
3353
3354          elsif Is_Class_Wide_Type (E) then
3355             Freeze_And_Append (Root_Type (E), Loc, Result);
3356
3357             --  If the base type of the class-wide type is still incomplete,
3358             --  the class-wide remains unfrozen as well. This is legal when
3359             --  E is the formal of a primitive operation of some other type
3360             --  which is being frozen.
3361
3362             if not Is_Frozen (Root_Type (E)) then
3363                Set_Is_Frozen (E, False);
3364                return Result;
3365             end if;
3366
3367             --  If the Class_Wide_Type is an Itype (when type is the anonymous
3368             --  parent of a derived type) and it is a library-level entity,
3369             --  generate an itype reference for it. Otherwise, its first
3370             --  explicit reference may be in an inner scope, which will be
3371             --  rejected by the back-end.
3372
3373             if Is_Itype (E)
3374               and then Is_Compilation_Unit (Scope (E))
3375             then
3376                declare
3377                   Ref : constant Node_Id := Make_Itype_Reference (Loc);
3378
3379                begin
3380                   Set_Itype (Ref, E);
3381                   if No (Result) then
3382                      Result := New_List (Ref);
3383                   else
3384                      Append (Ref, Result);
3385                   end if;
3386                end;
3387             end if;
3388
3389             --  The equivalent type associated with a class-wide subtype needs
3390             --  to be frozen to ensure that its layout is done.
3391
3392             if Ekind (E) = E_Class_Wide_Subtype
3393               and then Present (Equivalent_Type (E))
3394             then
3395                Freeze_And_Append (Equivalent_Type (E), Loc, Result);
3396             end if;
3397
3398          --  For a record (sub)type, freeze all the component types (RM
3399          --  13.14(15). We test for E_Record_(sub)Type here, rather than using
3400          --  Is_Record_Type, because we don't want to attempt the freeze for
3401          --  the case of a private type with record extension (we will do that
3402          --  later when the full type is frozen).
3403
3404          elsif Ekind (E) = E_Record_Type
3405            or else Ekind (E) = E_Record_Subtype
3406          then
3407             Freeze_Record_Type (E);
3408
3409          --  For a concurrent type, freeze corresponding record type. This
3410          --  does not correspond to any specific rule in the RM, but the
3411          --  record type is essentially part of the concurrent type.
3412          --  Freeze as well all local entities. This includes record types
3413          --  created for entry parameter blocks, and whatever local entities
3414          --  may appear in the private part.
3415
3416          elsif Is_Concurrent_Type (E) then
3417             if Present (Corresponding_Record_Type (E)) then
3418                Freeze_And_Append
3419                  (Corresponding_Record_Type (E), Loc, Result);
3420             end if;
3421
3422             Comp := First_Entity (E);
3423             while Present (Comp) loop
3424                if Is_Type (Comp) then
3425                   Freeze_And_Append (Comp, Loc, Result);
3426
3427                elsif (Ekind (Comp)) /= E_Function then
3428                   if Is_Itype (Etype (Comp))
3429                     and then Underlying_Type (Scope (Etype (Comp))) = E
3430                   then
3431                      Undelay_Type (Etype (Comp));
3432                   end if;
3433
3434                   Freeze_And_Append (Etype (Comp), Loc, Result);
3435                end if;
3436
3437                Next_Entity (Comp);
3438             end loop;
3439
3440          --  Private types are required to point to the same freeze node as
3441          --  their corresponding full views. The freeze node itself has to
3442          --  point to the partial view of the entity (because from the partial
3443          --  view, we can retrieve the full view, but not the reverse).
3444          --  However, in order to freeze correctly, we need to freeze the full
3445          --  view. If we are freezing at the end of a scope (or within the
3446          --  scope of the private type), the partial and full views will have
3447          --  been swapped, the full view appears first in the entity chain and
3448          --  the swapping mechanism ensures that the pointers are properly set
3449          --  (on scope exit).
3450
3451          --  If we encounter the partial view before the full view (e.g. when
3452          --  freezing from another scope), we freeze the full view, and then
3453          --  set the pointers appropriately since we cannot rely on swapping to
3454          --  fix things up (subtypes in an outer scope might not get swapped).
3455
3456          elsif Is_Incomplete_Or_Private_Type (E)
3457            and then not Is_Generic_Type (E)
3458          then
3459             --  The construction of the dispatch table associated with library
3460             --  level tagged types forces freezing of all the primitives of the
3461             --  type, which may cause premature freezing of the partial view.
3462             --  For example:
3463
3464             --     package Pkg is
3465             --        type T is tagged private;
3466             --        type DT is new T with private;
3467             --        procedure Prim (X : in out T; Y : in out DT'class);
3468             --     private
3469             --        type T is tagged null record;
3470             --        Obj : T;
3471             --        type DT is new T with null record;
3472             --     end;
3473
3474             --  In this case the type will be frozen later by the usual
3475             --  mechanism: an object declaration, an instantiation, or the
3476             --  end of a declarative part.
3477
3478             if Is_Library_Level_Tagged_Type (E)
3479               and then not Present (Full_View (E))
3480             then
3481                Set_Is_Frozen (E, False);
3482                return Result;
3483
3484             --  Case of full view present
3485
3486             elsif Present (Full_View (E)) then
3487
3488                --  If full view has already been frozen, then no further
3489                --  processing is required
3490
3491                if Is_Frozen (Full_View (E)) then
3492
3493                   Set_Has_Delayed_Freeze (E, False);
3494                   Set_Freeze_Node (E, Empty);
3495                   Check_Debug_Info_Needed (E);
3496
3497                --  Otherwise freeze full view and patch the pointers so that
3498                --  the freeze node will elaborate both views in the back-end.
3499
3500                else
3501                   declare
3502                      Full : constant Entity_Id := Full_View (E);
3503
3504                   begin
3505                      if Is_Private_Type (Full)
3506                        and then Present (Underlying_Full_View (Full))
3507                      then
3508                         Freeze_And_Append
3509                           (Underlying_Full_View (Full), Loc, Result);
3510                      end if;
3511
3512                      Freeze_And_Append (Full, Loc, Result);
3513
3514                      if Has_Delayed_Freeze (E) then
3515                         F_Node := Freeze_Node (Full);
3516
3517                         if Present (F_Node) then
3518                            Set_Freeze_Node (E, F_Node);
3519                            Set_Entity (F_Node, E);
3520
3521                         else
3522                            --  {Incomplete,Private}_Subtypes with Full_Views
3523                            --  constrained by discriminants.
3524
3525                            Set_Has_Delayed_Freeze (E, False);
3526                            Set_Freeze_Node (E, Empty);
3527                         end if;
3528                      end if;
3529                   end;
3530
3531                   Check_Debug_Info_Needed (E);
3532                end if;
3533
3534                --  AI-117 requires that the convention of a partial view be the
3535                --  same as the convention of the full view. Note that this is a
3536                --  recognized breach of privacy, but it's essential for logical
3537                --  consistency of representation, and the lack of a rule in
3538                --  RM95 was an oversight.
3539
3540                Set_Convention (E, Convention (Full_View (E)));
3541
3542                Set_Size_Known_At_Compile_Time (E,
3543                  Size_Known_At_Compile_Time (Full_View (E)));
3544
3545                --  Size information is copied from the full view to the
3546                --  incomplete or private view for consistency.
3547
3548                --  We skip this is the full view is not a type. This is very
3549                --  strange of course, and can only happen as a result of
3550                --  certain illegalities, such as a premature attempt to derive
3551                --  from an incomplete type.
3552
3553                if Is_Type (Full_View (E)) then
3554                   Set_Size_Info (E, Full_View (E));
3555                   Set_RM_Size   (E, RM_Size (Full_View (E)));
3556                end if;
3557
3558                return Result;
3559
3560             --  Case of no full view present. If entity is derived or subtype,
3561             --  it is safe to freeze, correctness depends on the frozen status
3562             --  of parent. Otherwise it is either premature usage, or a Taft
3563             --  amendment type, so diagnosis is at the point of use and the
3564             --  type might be frozen later.
3565
3566             elsif E /= Base_Type (E)
3567               or else Is_Derived_Type (E)
3568             then
3569                null;
3570
3571             else
3572                Set_Is_Frozen (E, False);
3573                return No_List;
3574             end if;
3575
3576          --  For access subprogram, freeze types of all formals, the return
3577          --  type was already frozen, since it is the Etype of the function.
3578          --  Formal types can be tagged Taft amendment types, but otherwise
3579          --  they cannot be incomplete.
3580
3581          elsif Ekind (E) = E_Subprogram_Type then
3582             Formal := First_Formal (E);
3583
3584             while Present (Formal) loop
3585                if Ekind (Etype (Formal)) = E_Incomplete_Type
3586                  and then No (Full_View (Etype (Formal)))
3587                  and then not Is_Value_Type (Etype (Formal))
3588                then
3589                   if Is_Tagged_Type (Etype (Formal)) then
3590                      null;
3591                   else
3592                      Error_Msg_NE
3593                        ("invalid use of incomplete type&", E, Etype (Formal));
3594                   end if;
3595                end if;
3596
3597                Freeze_And_Append (Etype (Formal), Loc, Result);
3598                Next_Formal (Formal);
3599             end loop;
3600
3601             Freeze_Subprogram (E);
3602
3603          --  For access to a protected subprogram, freeze the equivalent type
3604          --  (however this is not set if we are not generating code or if this
3605          --  is an anonymous type used just for resolution).
3606
3607          elsif Is_Access_Protected_Subprogram_Type (E) then
3608             if Present (Equivalent_Type (E)) then
3609                Freeze_And_Append (Equivalent_Type (E), Loc, Result);
3610             end if;
3611          end if;
3612
3613          --  Generic types are never seen by the back-end, and are also not
3614          --  processed by the expander (since the expander is turned off for
3615          --  generic processing), so we never need freeze nodes for them.
3616
3617          if Is_Generic_Type (E) then
3618             return Result;
3619          end if;
3620
3621          --  Some special processing for non-generic types to complete
3622          --  representation details not known till the freeze point.
3623
3624          if Is_Fixed_Point_Type (E) then
3625             Freeze_Fixed_Point_Type (E);
3626
3627             --  Some error checks required for ordinary fixed-point type. Defer
3628             --  these till the freeze-point since we need the small and range
3629             --  values. We only do these checks for base types
3630
3631             if Is_Ordinary_Fixed_Point_Type (E)
3632               and then E = Base_Type (E)
3633             then
3634                if Small_Value (E) < Ureal_2_M_80 then
3635                   Error_Msg_Name_1 := Name_Small;
3636                   Error_Msg_N
3637                     ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
3638
3639                elsif Small_Value (E) > Ureal_2_80 then
3640                   Error_Msg_Name_1 := Name_Small;
3641                   Error_Msg_N
3642                     ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
3643                end if;
3644
3645                if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
3646                   Error_Msg_Name_1 := Name_First;
3647                   Error_Msg_N
3648                     ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
3649                end if;
3650
3651                if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
3652                   Error_Msg_Name_1 := Name_Last;
3653                   Error_Msg_N
3654                     ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
3655                end if;
3656             end if;
3657
3658          elsif Is_Enumeration_Type (E) then
3659             Freeze_Enumeration_Type (E);
3660
3661          elsif Is_Integer_Type (E) then
3662             Adjust_Esize_For_Alignment (E);
3663
3664             if Is_Modular_Integer_Type (E)
3665               and then Warn_On_Suspicious_Modulus_Value
3666             then
3667                Check_Suspicious_Modulus (E);
3668             end if;
3669
3670          elsif Is_Access_Type (E) then
3671
3672             --  Check restriction for standard storage pool
3673
3674             if No (Associated_Storage_Pool (E)) then
3675                Check_Restriction (No_Standard_Storage_Pools, E);
3676             end if;
3677
3678             --  Deal with error message for pure access type. This is not an
3679             --  error in Ada 2005 if there is no pool (see AI-366).
3680
3681             if Is_Pure_Unit_Access_Type (E)
3682               and then (Ada_Version < Ada_05
3683                          or else not No_Pool_Assigned (E))
3684             then
3685                Error_Msg_N ("named access type not allowed in pure unit", E);
3686
3687                if Ada_Version >= Ada_05 then
3688                   Error_Msg_N
3689                     ("\would be legal if Storage_Size of 0 given?", E);
3690
3691                elsif No_Pool_Assigned (E) then
3692                   Error_Msg_N
3693                     ("\would be legal in Ada 2005?", E);
3694
3695                else
3696                   Error_Msg_N
3697                     ("\would be legal in Ada 2005 if "
3698                      & "Storage_Size of 0 given?", E);
3699                end if;
3700             end if;
3701          end if;
3702
3703          --  Case of composite types
3704
3705          if Is_Composite_Type (E) then
3706
3707             --  AI-117 requires that all new primitives of a tagged type must
3708             --  inherit the convention of the full view of the type. Inherited
3709             --  and overriding operations are defined to inherit the convention
3710             --  of their parent or overridden subprogram (also specified in
3711             --  AI-117), which will have occurred earlier (in Derive_Subprogram
3712             --  and New_Overloaded_Entity). Here we set the convention of
3713             --  primitives that are still convention Ada, which will ensure
3714             --  that any new primitives inherit the type's convention. Class-
3715             --  wide types can have a foreign convention inherited from their
3716             --  specific type, but are excluded from this since they don't have
3717             --  any associated primitives.
3718
3719             if Is_Tagged_Type (E)
3720               and then not Is_Class_Wide_Type (E)
3721               and then Convention (E) /= Convention_Ada
3722             then
3723                declare
3724                   Prim_List : constant Elist_Id := Primitive_Operations (E);
3725                   Prim      : Elmt_Id;
3726                begin
3727                   Prim := First_Elmt (Prim_List);
3728                   while Present (Prim) loop
3729                      if Convention (Node (Prim)) = Convention_Ada then
3730                         Set_Convention (Node (Prim), Convention (E));
3731                      end if;
3732
3733                      Next_Elmt (Prim);
3734                   end loop;
3735                end;
3736             end if;
3737          end if;
3738
3739          --  Now that all types from which E may depend are frozen, see if the
3740          --  size is known at compile time, if it must be unsigned, or if
3741          --  strict alignment is required
3742
3743          Check_Compile_Time_Size (E);
3744          Check_Unsigned_Type (E);
3745
3746          if Base_Type (E) = E then
3747             Check_Strict_Alignment (E);
3748          end if;
3749
3750          --  Do not allow a size clause for a type which does not have a size
3751          --  that is known at compile time
3752
3753          if Has_Size_Clause (E)
3754            and then not Size_Known_At_Compile_Time (E)
3755          then
3756             --  Suppress this message if errors posted on E, even if we are
3757             --  in all errors mode, since this is often a junk message
3758
3759             if not Error_Posted (E) then
3760                Error_Msg_N
3761                  ("size clause not allowed for variable length type",
3762                   Size_Clause (E));
3763             end if;
3764          end if;
3765
3766          --  Remaining process is to set/verify the representation information,
3767          --  in particular the size and alignment values. This processing is
3768          --  not required for generic types, since generic types do not play
3769          --  any part in code generation, and so the size and alignment values
3770          --  for such types are irrelevant.
3771
3772          if Is_Generic_Type (E) then
3773             return Result;
3774
3775          --  Otherwise we call the layout procedure
3776
3777          else
3778             Layout_Type (E);
3779          end if;
3780
3781          --  End of freeze processing for type entities
3782       end if;
3783
3784       --  Here is where we logically freeze the current entity. If it has a
3785       --  freeze node, then this is the point at which the freeze node is
3786       --  linked into the result list.
3787
3788       if Has_Delayed_Freeze (E) then
3789
3790          --  If a freeze node is already allocated, use it, otherwise allocate
3791          --  a new one. The preallocation happens in the case of anonymous base
3792          --  types, where we preallocate so that we can set First_Subtype_Link.
3793          --  Note that we reset the Sloc to the current freeze location.
3794
3795          if Present (Freeze_Node (E)) then
3796             F_Node := Freeze_Node (E);
3797             Set_Sloc (F_Node, Loc);
3798
3799          else
3800             F_Node := New_Node (N_Freeze_Entity, Loc);
3801             Set_Freeze_Node (E, F_Node);
3802             Set_Access_Types_To_Process (F_Node, No_Elist);
3803             Set_TSS_Elist (F_Node, No_Elist);
3804             Set_Actions (F_Node, No_List);
3805          end if;
3806
3807          Set_Entity (F_Node, E);
3808
3809          if Result = No_List then
3810             Result := New_List (F_Node);
3811          else
3812             Append (F_Node, Result);
3813          end if;
3814
3815          --  A final pass over record types with discriminants. If the type
3816          --  has an incomplete declaration, there may be constrained access
3817          --  subtypes declared elsewhere, which do not depend on the discrimi-
3818          --  nants of the type, and which are used as component types (i.e.
3819          --  the full view is a recursive type). The designated types of these
3820          --  subtypes can only be elaborated after the type itself, and they
3821          --  need an itype reference.
3822
3823          if Ekind (E) = E_Record_Type
3824            and then Has_Discriminants (E)
3825          then
3826             declare
3827                Comp : Entity_Id;
3828                IR   : Node_Id;
3829                Typ  : Entity_Id;
3830
3831             begin
3832                Comp := First_Component (E);
3833
3834                while Present (Comp) loop
3835                   Typ  := Etype (Comp);
3836
3837                   if Ekind (Comp) = E_Component
3838                     and then Is_Access_Type (Typ)
3839                     and then Scope (Typ) /= E
3840                     and then Base_Type (Designated_Type (Typ)) = E
3841                     and then Is_Itype (Designated_Type (Typ))
3842                   then
3843                      IR := Make_Itype_Reference (Sloc (Comp));
3844                      Set_Itype (IR, Designated_Type (Typ));
3845                      Append (IR, Result);
3846                   end if;
3847
3848                   Next_Component (Comp);
3849                end loop;
3850             end;
3851          end if;
3852       end if;
3853
3854       --  When a type is frozen, the first subtype of the type is frozen as
3855       --  well (RM 13.14(15)). This has to be done after freezing the type,
3856       --  since obviously the first subtype depends on its own base type.
3857
3858       if Is_Type (E) then
3859          Freeze_And_Append (First_Subtype (E), Loc, Result);
3860
3861          --  If we just froze a tagged non-class wide record, then freeze the
3862          --  corresponding class-wide type. This must be done after the tagged
3863          --  type itself is frozen, because the class-wide type refers to the
3864          --  tagged type which generates the class.
3865
3866          if Is_Tagged_Type (E)
3867            and then not Is_Class_Wide_Type (E)
3868            and then Present (Class_Wide_Type (E))
3869          then
3870             Freeze_And_Append (Class_Wide_Type (E), Loc, Result);
3871          end if;
3872       end if;
3873
3874       Check_Debug_Info_Needed (E);
3875
3876       --  Special handling for subprograms
3877
3878       if Is_Subprogram (E) then
3879
3880          --  If subprogram has address clause then reset Is_Public flag, since
3881          --  we do not want the backend to generate external references.
3882
3883          if Present (Address_Clause (E))
3884            and then not Is_Library_Level_Entity (E)
3885          then
3886             Set_Is_Public (E, False);
3887
3888          --  If no address clause and not intrinsic, then for imported
3889          --  subprogram in main unit, generate descriptor if we are in
3890          --  Propagate_Exceptions mode.
3891
3892          elsif Propagate_Exceptions
3893            and then Is_Imported (E)
3894            and then not Is_Intrinsic_Subprogram (E)
3895            and then Convention (E) /= Convention_Stubbed
3896          then
3897             if Result = No_List then
3898                Result := Empty_List;
3899             end if;
3900          end if;
3901       end if;
3902
3903       return Result;
3904    end Freeze_Entity;
3905
3906    -----------------------------
3907    -- Freeze_Enumeration_Type --
3908    -----------------------------
3909
3910    procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
3911    begin
3912       --  By default, if no size clause is present, an enumeration type with
3913       --  Convention C is assumed to interface to a C enum, and has integer
3914       --  size. This applies to types. For subtypes, verify that its base
3915       --  type has no size clause either.
3916
3917       if Has_Foreign_Convention (Typ)
3918         and then not Has_Size_Clause (Typ)
3919         and then not Has_Size_Clause (Base_Type (Typ))
3920         and then Esize (Typ) < Standard_Integer_Size
3921       then
3922          Init_Esize (Typ, Standard_Integer_Size);
3923
3924       else
3925          --  If the enumeration type interfaces to C, and it has a size clause
3926          --  that specifies less than int size, it warrants a warning. The
3927          --  user may intend the C type to be an enum or a char, so this is
3928          --  not by itself an error that the Ada compiler can detect, but it
3929          --  it is a worth a heads-up. For Boolean and Character types we
3930          --  assume that the programmer has the proper C type in mind.
3931
3932          if Convention (Typ) = Convention_C
3933            and then Has_Size_Clause (Typ)
3934            and then Esize (Typ) /= Esize (Standard_Integer)
3935            and then not Is_Boolean_Type (Typ)
3936            and then not Is_Character_Type (Typ)
3937          then
3938             Error_Msg_N
3939               ("C enum types have the size of a C int?", Size_Clause (Typ));
3940          end if;
3941
3942          Adjust_Esize_For_Alignment (Typ);
3943       end if;
3944    end Freeze_Enumeration_Type;
3945
3946    -----------------------
3947    -- Freeze_Expression --
3948    -----------------------
3949
3950    procedure Freeze_Expression (N : Node_Id) is
3951       In_Spec_Exp : constant Boolean := In_Spec_Expression;
3952       Typ         : Entity_Id;
3953       Nam         : Entity_Id;
3954       Desig_Typ   : Entity_Id;
3955       P           : Node_Id;
3956       Parent_P    : Node_Id;
3957
3958       Freeze_Outside : Boolean := False;
3959       --  This flag is set true if the entity must be frozen outside the
3960       --  current subprogram. This happens in the case of expander generated
3961       --  subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
3962       --  not freeze all entities like other bodies, but which nevertheless
3963       --  may reference entities that have to be frozen before the body and
3964       --  obviously cannot be frozen inside the body.
3965
3966       function In_Exp_Body (N : Node_Id) return Boolean;
3967       --  Given an N_Handled_Sequence_Of_Statements node N, determines whether
3968       --  it is the handled statement sequence of an expander-generated
3969       --  subprogram (init proc, stream subprogram, or renaming as body).
3970       --  If so, this is not a freezing context.
3971
3972       -----------------
3973       -- In_Exp_Body --
3974       -----------------
3975
3976       function In_Exp_Body (N : Node_Id) return Boolean is
3977          P  : Node_Id;
3978          Id : Entity_Id;
3979
3980       begin
3981          if Nkind (N) = N_Subprogram_Body then
3982             P := N;
3983          else
3984             P := Parent (N);
3985          end if;
3986
3987          if Nkind (P) /= N_Subprogram_Body then
3988             return False;
3989
3990          else
3991             Id := Defining_Unit_Name (Specification (P));
3992
3993             if Nkind (Id) = N_Defining_Identifier
3994               and then (Is_Init_Proc (Id)              or else
3995                         Is_TSS (Id, TSS_Stream_Input)  or else
3996                         Is_TSS (Id, TSS_Stream_Output) or else
3997                         Is_TSS (Id, TSS_Stream_Read)   or else
3998                         Is_TSS (Id, TSS_Stream_Write)  or else
3999                         Nkind (Original_Node (P)) =
4000                           N_Subprogram_Renaming_Declaration)
4001             then
4002                return True;
4003             else
4004                return False;
4005             end if;
4006          end if;
4007       end In_Exp_Body;
4008
4009    --  Start of processing for Freeze_Expression
4010
4011    begin
4012       --  Immediate return if freezing is inhibited. This flag is set by the
4013       --  analyzer to stop freezing on generated expressions that would cause
4014       --  freezing if they were in the source program, but which are not
4015       --  supposed to freeze, since they are created.
4016
4017       if Must_Not_Freeze (N) then
4018          return;
4019       end if;
4020
4021       --  If expression is non-static, then it does not freeze in a default
4022       --  expression, see section "Handling of Default Expressions" in the
4023       --  spec of package Sem for further details. Note that we have to
4024       --  make sure that we actually have a real expression (if we have
4025       --  a subtype indication, we can't test Is_Static_Expression!)
4026
4027       if In_Spec_Exp
4028         and then Nkind (N) in N_Subexpr
4029         and then not Is_Static_Expression (N)
4030       then
4031          return;
4032       end if;
4033
4034       --  Freeze type of expression if not frozen already
4035
4036       Typ := Empty;
4037
4038       if Nkind (N) in N_Has_Etype then
4039          if not Is_Frozen (Etype (N)) then
4040             Typ := Etype (N);
4041
4042          --  Base type may be an derived numeric type that is frozen at
4043          --  the point of declaration, but first_subtype is still unfrozen.
4044
4045          elsif not Is_Frozen (First_Subtype (Etype (N))) then
4046             Typ := First_Subtype (Etype (N));
4047          end if;
4048       end if;
4049
4050       --  For entity name, freeze entity if not frozen already. A special
4051       --  exception occurs for an identifier that did not come from source.
4052       --  We don't let such identifiers freeze a non-internal entity, i.e.
4053       --  an entity that did come from source, since such an identifier was
4054       --  generated by the expander, and cannot have any semantic effect on
4055       --  the freezing semantics. For example, this stops the parameter of
4056       --  an initialization procedure from freezing the variable.
4057
4058       if Is_Entity_Name (N)
4059         and then not Is_Frozen (Entity (N))
4060         and then (Nkind (N) /= N_Identifier
4061                    or else Comes_From_Source (N)
4062                    or else not Comes_From_Source (Entity (N)))
4063       then
4064          Nam := Entity (N);
4065       else
4066          Nam := Empty;
4067       end if;
4068
4069       --  For an allocator freeze designated type if not frozen already
4070
4071       --  For an aggregate whose component type is an access type, freeze the
4072       --  designated type now, so that its freeze does not appear within the
4073       --  loop that might be created in the expansion of the aggregate. If the
4074       --  designated type is a private type without full view, the expression
4075       --  cannot contain an allocator, so the type is not frozen.
4076
4077       --  For a function, we freeze the entity when the subprogram declaration
4078       --  is frozen, but a function call may appear in an initialization proc.
4079       --  before the declaration is frozen. We need to generate the extra
4080       --  formals, if any, to ensure that the expansion of the call includes
4081       --  the proper actuals. This only applies to Ada subprograms, not to
4082       --  imported ones.
4083
4084       Desig_Typ := Empty;
4085
4086       case Nkind (N) is
4087          when N_Allocator =>
4088             Desig_Typ := Designated_Type (Etype (N));
4089
4090          when N_Aggregate =>
4091             if Is_Array_Type (Etype (N))
4092               and then Is_Access_Type (Component_Type (Etype (N)))
4093             then
4094                Desig_Typ := Designated_Type (Component_Type (Etype (N)));
4095             end if;
4096
4097          when N_Selected_Component |
4098             N_Indexed_Component    |
4099             N_Slice                =>
4100
4101             if Is_Access_Type (Etype (Prefix (N))) then
4102                Desig_Typ := Designated_Type (Etype (Prefix (N)));
4103             end if;
4104
4105          when N_Identifier =>
4106             if Present (Nam)
4107               and then Ekind (Nam) = E_Function
4108               and then Nkind (Parent (N)) = N_Function_Call
4109               and then Convention (Nam) = Convention_Ada
4110             then
4111                Create_Extra_Formals (Nam);
4112             end if;
4113
4114          when others =>
4115             null;
4116       end case;
4117
4118       if Desig_Typ /= Empty
4119         and then (Is_Frozen (Desig_Typ)
4120                    or else (not Is_Fully_Defined (Desig_Typ)))
4121       then
4122          Desig_Typ := Empty;
4123       end if;
4124
4125       --  All done if nothing needs freezing
4126
4127       if No (Typ)
4128         and then No (Nam)
4129         and then No (Desig_Typ)
4130       then
4131          return;
4132       end if;
4133
4134       --  Loop for looking at the right place to insert the freeze nodes,
4135       --  exiting from the loop when it is appropriate to insert the freeze
4136       --  node before the current node P.
4137
4138       --  Also checks some special exceptions to the freezing rules. These
4139       --  cases result in a direct return, bypassing the freeze action.
4140
4141       P := N;
4142       loop
4143          Parent_P := Parent (P);
4144
4145          --  If we don't have a parent, then we are not in a well-formed tree.
4146          --  This is an unusual case, but there are some legitimate situations
4147          --  in which this occurs, notably when the expressions in the range of
4148          --  a type declaration are resolved. We simply ignore the freeze
4149          --  request in this case. Is this right ???
4150
4151          if No (Parent_P) then
4152             return;
4153          end if;
4154
4155          --  See if we have got to an appropriate point in the tree
4156
4157          case Nkind (Parent_P) is
4158
4159             --  A special test for the exception of (RM 13.14(8)) for the case
4160             --  of per-object expressions (RM 3.8(18)) occurring in component
4161             --  definition or a discrete subtype definition. Note that we test
4162             --  for a component declaration which includes both cases we are
4163             --  interested in, and furthermore the tree does not have explicit
4164             --  nodes for either of these two constructs.
4165
4166             when N_Component_Declaration =>
4167
4168                --  The case we want to test for here is an identifier that is
4169                --  a per-object expression, this is either a discriminant that
4170                --  appears in a context other than the component declaration
4171                --  or it is a reference to the type of the enclosing construct.
4172
4173                --  For either of these cases, we skip the freezing
4174
4175                if not In_Spec_Expression
4176                  and then Nkind (N) = N_Identifier
4177                  and then (Present (Entity (N)))
4178                then
4179                   --  We recognize the discriminant case by just looking for
4180                   --  a reference to a discriminant. It can only be one for
4181                   --  the enclosing construct. Skip freezing in this case.
4182
4183                   if Ekind (Entity (N)) = E_Discriminant then
4184                      return;
4185
4186                   --  For the case of a reference to the enclosing record,
4187                   --  (or task or protected type), we look for a type that
4188                   --  matches the current scope.
4189
4190                   elsif Entity (N) = Current_Scope then
4191                      return;
4192                   end if;
4193                end if;
4194
4195             --  If we have an enumeration literal that appears as the choice in
4196             --  the aggregate of an enumeration representation clause, then
4197             --  freezing does not occur (RM 13.14(10)).
4198
4199             when N_Enumeration_Representation_Clause =>
4200
4201                --  The case we are looking for is an enumeration literal
4202
4203                if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
4204                  and then Is_Enumeration_Type (Etype (N))
4205                then
4206                   --  If enumeration literal appears directly as the choice,
4207                   --  do not freeze (this is the normal non-overloaded case)
4208
4209                   if Nkind (Parent (N)) = N_Component_Association
4210                     and then First (Choices (Parent (N))) = N
4211                   then
4212                      return;
4213
4214                   --  If enumeration literal appears as the name of function
4215                   --  which is the choice, then also do not freeze. This
4216                   --  happens in the overloaded literal case, where the
4217                   --  enumeration literal is temporarily changed to a function
4218                   --  call for overloading analysis purposes.
4219
4220                   elsif Nkind (Parent (N)) = N_Function_Call
4221                      and then
4222                        Nkind (Parent (Parent (N))) = N_Component_Association
4223                      and then
4224                        First (Choices (Parent (Parent (N)))) = Parent (N)
4225                   then
4226                      return;
4227                   end if;
4228                end if;
4229
4230             --  Normally if the parent is a handled sequence of statements,
4231             --  then the current node must be a statement, and that is an
4232             --  appropriate place to insert a freeze node.
4233
4234             when N_Handled_Sequence_Of_Statements =>
4235
4236                --  An exception occurs when the sequence of statements is for
4237                --  an expander generated body that did not do the usual freeze
4238                --  all operation. In this case we usually want to freeze
4239                --  outside this body, not inside it, and we skip past the
4240                --  subprogram body that we are inside.
4241
4242                if In_Exp_Body (Parent_P) then
4243
4244                   --  However, we *do* want to freeze at this point if we have
4245                   --  an entity to freeze, and that entity is declared *inside*
4246                   --  the body of the expander generated procedure. This case
4247                   --  is recognized by the scope of the type, which is either
4248                   --  the spec for some enclosing body, or (in the case of
4249                   --  init_procs, for which there are no separate specs) the
4250                   --  current scope.
4251
4252                   declare
4253                      Subp : constant Node_Id := Parent (Parent_P);
4254                      Cspc : Entity_Id;
4255
4256                   begin
4257                      if Nkind (Subp) = N_Subprogram_Body then
4258                         Cspc := Corresponding_Spec (Subp);
4259
4260                         if (Present (Typ) and then Scope (Typ) = Cspc)
4261                              or else
4262                            (Present (Nam) and then Scope (Nam) = Cspc)
4263                         then
4264                            exit;
4265
4266                         elsif Present (Typ)
4267                           and then Scope (Typ) = Current_Scope
4268                           and then Current_Scope = Defining_Entity (Subp)
4269                         then
4270                            exit;
4271                         end if;
4272                      end if;
4273                   end;
4274
4275                   --  If not that exception to the exception, then this is
4276                   --  where we delay the freeze till outside the body.
4277
4278                   Parent_P := Parent (Parent_P);
4279                   Freeze_Outside := True;
4280
4281                --  Here if normal case where we are in handled statement
4282                --  sequence and want to do the insertion right there.
4283
4284                else
4285                   exit;
4286                end if;
4287
4288             --  If parent is a body or a spec or a block, then the current node
4289             --  is a statement or declaration and we can insert the freeze node
4290             --  before it.
4291
4292             when N_Package_Specification |
4293                  N_Package_Body          |
4294                  N_Subprogram_Body       |
4295                  N_Task_Body             |
4296                  N_Protected_Body        |
4297                  N_Entry_Body            |
4298                  N_Block_Statement       => exit;
4299
4300             --  The expander is allowed to define types in any statements list,
4301             --  so any of the following parent nodes also mark a freezing point
4302             --  if the actual node is in a list of statements or declarations.
4303
4304             when N_Exception_Handler          |
4305                  N_If_Statement               |
4306                  N_Elsif_Part                 |
4307                  N_Case_Statement_Alternative |
4308                  N_Compilation_Unit_Aux       |
4309                  N_Selective_Accept           |
4310                  N_Accept_Alternative         |
4311                  N_Delay_Alternative          |
4312                  N_Conditional_Entry_Call     |
4313                  N_Entry_Call_Alternative     |
4314                  N_Triggering_Alternative     |
4315                  N_Abortable_Part             |
4316                  N_And_Then                   |
4317                  N_Or_Else                    |
4318                  N_Freeze_Entity              =>
4319
4320                exit when Is_List_Member (P);
4321
4322             --  Note: The N_Loop_Statement is a special case. A type that
4323             --  appears in the source can never be frozen in a loop (this
4324             --  occurs only because of a loop expanded by the expander), so we
4325             --  keep on going. Otherwise we terminate the search. Same is true
4326             --  of any entity which comes from source. (if they have predefined
4327             --  type, that type does not appear to come from source, but the
4328             --  entity should not be frozen here).
4329
4330             when N_Loop_Statement =>
4331                exit when not Comes_From_Source (Etype (N))
4332                  and then (No (Nam) or else not Comes_From_Source (Nam));
4333
4334             --  For all other cases, keep looking at parents
4335
4336             when others =>
4337                null;
4338          end case;
4339
4340          --  We fall through the case if we did not yet find the proper
4341          --  place in the free for inserting the freeze node, so climb!
4342
4343          P := Parent_P;
4344       end loop;
4345
4346       --  If the expression appears in a record or an initialization procedure,
4347       --  the freeze nodes are collected and attached to the current scope, to
4348       --  be inserted and analyzed on exit from the scope, to insure that
4349       --  generated entities appear in the correct scope. If the expression is
4350       --  a default for a discriminant specification, the scope is still void.
4351       --  The expression can also appear in the discriminant part of a private
4352       --  or concurrent type.
4353
4354       --  If the expression appears in a constrained subcomponent of an
4355       --  enclosing record declaration, the freeze nodes must be attached to
4356       --  the outer record type so they can eventually be placed in the
4357       --  enclosing declaration list.
4358
4359       --  The other case requiring this special handling is if we are in a
4360       --  default expression, since in that case we are about to freeze a
4361       --  static type, and the freeze scope needs to be the outer scope, not
4362       --  the scope of the subprogram with the default parameter.
4363
4364       --  For default expressions and other spec expressions in generic units,
4365       --  the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
4366       --  placing them at the proper place, after the generic unit.
4367
4368       if (In_Spec_Exp and not Inside_A_Generic)
4369         or else Freeze_Outside
4370         or else (Is_Type (Current_Scope)
4371                   and then (not Is_Concurrent_Type (Current_Scope)
4372                              or else not Has_Completion (Current_Scope)))
4373         or else Ekind (Current_Scope) = E_Void
4374       then
4375          declare
4376             Loc          : constant Source_Ptr := Sloc (Current_Scope);
4377             Freeze_Nodes : List_Id := No_List;
4378             Pos          : Int := Scope_Stack.Last;
4379
4380          begin
4381             if Present (Desig_Typ) then
4382                Freeze_And_Append (Desig_Typ, Loc, Freeze_Nodes);
4383             end if;
4384
4385             if Present (Typ) then
4386                Freeze_And_Append (Typ, Loc, Freeze_Nodes);
4387             end if;
4388
4389             if Present (Nam) then
4390                Freeze_And_Append (Nam, Loc, Freeze_Nodes);
4391             end if;
4392
4393             --  The current scope may be that of a constrained component of
4394             --  an enclosing record declaration, which is above the current
4395             --  scope in the scope stack.
4396
4397             if Is_Record_Type (Scope (Current_Scope)) then
4398                Pos := Pos - 1;
4399             end if;
4400
4401             if Is_Non_Empty_List (Freeze_Nodes) then
4402                if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
4403                   Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
4404                       Freeze_Nodes;
4405                else
4406                   Append_List (Freeze_Nodes, Scope_Stack.Table
4407                                    (Pos).Pending_Freeze_Actions);
4408                end if;
4409             end if;
4410          end;
4411
4412          return;
4413       end if;
4414
4415       --  Now we have the right place to do the freezing. First, a special
4416       --  adjustment, if we are in spec-expression analysis mode, these freeze
4417       --  actions must not be thrown away (normally all inserted actions are
4418       --  thrown away in this mode. However, the freeze actions are from static
4419       --  expressions and one of the important reasons we are doing this
4420       --  special analysis is to get these freeze actions. Therefore we turn
4421       --  off the In_Spec_Expression mode to propagate these freeze actions.
4422       --  This also means they get properly analyzed and expanded.
4423
4424       In_Spec_Expression := False;
4425
4426       --  Freeze the designated type of an allocator (RM 13.14(13))
4427
4428       if Present (Desig_Typ) then
4429          Freeze_Before (P, Desig_Typ);
4430       end if;
4431
4432       --  Freeze type of expression (RM 13.14(10)). Note that we took care of
4433       --  the enumeration representation clause exception in the loop above.
4434
4435       if Present (Typ) then
4436          Freeze_Before (P, Typ);
4437       end if;
4438
4439       --  Freeze name if one is present (RM 13.14(11))
4440
4441       if Present (Nam) then
4442          Freeze_Before (P, Nam);
4443       end if;
4444
4445       --  Restore In_Spec_Expression flag
4446
4447       In_Spec_Expression := In_Spec_Exp;
4448    end Freeze_Expression;
4449
4450    -----------------------------
4451    -- Freeze_Fixed_Point_Type --
4452    -----------------------------
4453
4454    --  Certain fixed-point types and subtypes, including implicit base types
4455    --  and declared first subtypes, have not yet set up a range. This is
4456    --  because the range cannot be set until the Small and Size values are
4457    --  known, and these are not known till the type is frozen.
4458
4459    --  To signal this case, Scalar_Range contains an unanalyzed syntactic range
4460    --  whose bounds are unanalyzed real literals. This routine will recognize
4461    --  this case, and transform this range node into a properly typed range
4462    --  with properly analyzed and resolved values.
4463
4464    procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
4465       Rng   : constant Node_Id    := Scalar_Range (Typ);
4466       Lo    : constant Node_Id    := Low_Bound (Rng);
4467       Hi    : constant Node_Id    := High_Bound (Rng);
4468       Btyp  : constant Entity_Id  := Base_Type (Typ);
4469       Brng  : constant Node_Id    := Scalar_Range (Btyp);
4470       BLo   : constant Node_Id    := Low_Bound (Brng);
4471       BHi   : constant Node_Id    := High_Bound (Brng);
4472       Small : constant Ureal      := Small_Value (Typ);
4473       Loval : Ureal;
4474       Hival : Ureal;
4475       Atype : Entity_Id;
4476
4477       Actual_Size : Nat;
4478
4479       function Fsize (Lov, Hiv : Ureal) return Nat;
4480       --  Returns size of type with given bounds. Also leaves these
4481       --  bounds set as the current bounds of the Typ.
4482
4483       -----------
4484       -- Fsize --
4485       -----------
4486
4487       function Fsize (Lov, Hiv : Ureal) return Nat is
4488       begin
4489          Set_Realval (Lo, Lov);
4490          Set_Realval (Hi, Hiv);
4491          return Minimum_Size (Typ);
4492       end Fsize;
4493
4494    --  Start of processing for Freeze_Fixed_Point_Type
4495
4496    begin
4497       --  If Esize of a subtype has not previously been set, set it now
4498
4499       if Unknown_Esize (Typ) then
4500          Atype := Ancestor_Subtype (Typ);
4501
4502          if Present (Atype) then
4503             Set_Esize (Typ, Esize (Atype));
4504          else
4505             Set_Esize (Typ, Esize (Base_Type (Typ)));
4506          end if;
4507       end if;
4508
4509       --  Immediate return if the range is already analyzed. This means that
4510       --  the range is already set, and does not need to be computed by this
4511       --  routine.
4512
4513       if Analyzed (Rng) then
4514          return;
4515       end if;
4516
4517       --  Immediate return if either of the bounds raises Constraint_Error
4518
4519       if Raises_Constraint_Error (Lo)
4520         or else Raises_Constraint_Error (Hi)
4521       then
4522          return;
4523       end if;
4524
4525       Loval := Realval (Lo);
4526       Hival := Realval (Hi);
4527
4528       --  Ordinary fixed-point case
4529
4530       if Is_Ordinary_Fixed_Point_Type (Typ) then
4531
4532          --  For the ordinary fixed-point case, we are allowed to fudge the
4533          --  end-points up or down by small. Generally we prefer to fudge up,
4534          --  i.e. widen the bounds for non-model numbers so that the end points
4535          --  are included. However there are cases in which this cannot be
4536          --  done, and indeed cases in which we may need to narrow the bounds.
4537          --  The following circuit makes the decision.
4538
4539          --  Note: our terminology here is that Incl_EP means that the bounds
4540          --  are widened by Small if necessary to include the end points, and
4541          --  Excl_EP means that the bounds are narrowed by Small to exclude the
4542          --  end-points if this reduces the size.
4543
4544          --  Note that in the Incl case, all we care about is including the
4545          --  end-points. In the Excl case, we want to narrow the bounds as
4546          --  much as permitted by the RM, to give the smallest possible size.
4547
4548          Fudge : declare
4549             Loval_Incl_EP : Ureal;
4550             Hival_Incl_EP : Ureal;
4551
4552             Loval_Excl_EP : Ureal;
4553             Hival_Excl_EP : Ureal;
4554
4555             Size_Incl_EP  : Nat;
4556             Size_Excl_EP  : Nat;
4557
4558             Model_Num     : Ureal;
4559             First_Subt    : Entity_Id;
4560             Actual_Lo     : Ureal;
4561             Actual_Hi     : Ureal;
4562
4563          begin
4564             --  First step. Base types are required to be symmetrical. Right
4565             --  now, the base type range is a copy of the first subtype range.
4566             --  This will be corrected before we are done, but right away we
4567             --  need to deal with the case where both bounds are non-negative.
4568             --  In this case, we set the low bound to the negative of the high
4569             --  bound, to make sure that the size is computed to include the
4570             --  required sign. Note that we do not need to worry about the
4571             --  case of both bounds negative, because the sign will be dealt
4572             --  with anyway. Furthermore we can't just go making such a bound
4573             --  symmetrical, since in a twos-complement system, there is an
4574             --  extra negative value which could not be accommodated on the
4575             --  positive side.
4576
4577             if Typ = Btyp
4578               and then not UR_Is_Negative (Loval)
4579               and then Hival > Loval
4580             then
4581                Loval := -Hival;
4582                Set_Realval (Lo, Loval);
4583             end if;
4584
4585             --  Compute the fudged bounds. If the number is a model number,
4586             --  then we do nothing to include it, but we are allowed to backoff
4587             --  to the next adjacent model number when we exclude it. If it is
4588             --  not a model number then we straddle the two values with the
4589             --  model numbers on either side.
4590
4591             Model_Num := UR_Trunc (Loval / Small) * Small;
4592
4593             if Loval = Model_Num then
4594                Loval_Incl_EP := Model_Num;
4595             else
4596                Loval_Incl_EP := Model_Num - Small;
4597             end if;
4598
4599             --  The low value excluding the end point is Small greater, but
4600             --  we do not do this exclusion if the low value is positive,
4601             --  since it can't help the size and could actually hurt by
4602             --  crossing the high bound.
4603
4604             if UR_Is_Negative (Loval_Incl_EP) then
4605                Loval_Excl_EP := Loval_Incl_EP + Small;
4606
4607                --  If the value went from negative to zero, then we have the
4608                --  case where Loval_Incl_EP is the model number just below
4609                --  zero, so we want to stick to the negative value for the
4610                --  base type to maintain the condition that the size will
4611                --  include signed values.
4612
4613                if Typ = Btyp
4614                  and then UR_Is_Zero (Loval_Excl_EP)
4615                then
4616                   Loval_Excl_EP := Loval_Incl_EP;
4617                end if;
4618
4619             else
4620                Loval_Excl_EP := Loval_Incl_EP;
4621             end if;
4622
4623             --  Similar processing for upper bound and high value
4624
4625             Model_Num := UR_Trunc (Hival / Small) * Small;
4626
4627             if Hival = Model_Num then
4628                Hival_Incl_EP := Model_Num;
4629             else
4630                Hival_Incl_EP := Model_Num + Small;
4631             end if;
4632
4633             if UR_Is_Positive (Hival_Incl_EP) then
4634                Hival_Excl_EP := Hival_Incl_EP - Small;
4635             else
4636                Hival_Excl_EP := Hival_Incl_EP;
4637             end if;
4638
4639             --  One further adjustment is needed. In the case of subtypes, we
4640             --  cannot go outside the range of the base type, or we get
4641             --  peculiarities, and the base type range is already set. This
4642             --  only applies to the Incl values, since clearly the Excl values
4643             --  are already as restricted as they are allowed to be.
4644
4645             if Typ /= Btyp then
4646                Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
4647                Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
4648             end if;
4649
4650             --  Get size including and excluding end points
4651
4652             Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
4653             Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
4654
4655             --  No need to exclude end-points if it does not reduce size
4656
4657             if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
4658                Loval_Excl_EP := Loval_Incl_EP;
4659             end if;
4660
4661             if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
4662                Hival_Excl_EP := Hival_Incl_EP;
4663             end if;
4664
4665             --  Now we set the actual size to be used. We want to use the
4666             --  bounds fudged up to include the end-points but only if this
4667             --  can be done without violating a specifically given size
4668             --  size clause or causing an unacceptable increase in size.
4669
4670             --  Case of size clause given
4671
4672             if Has_Size_Clause (Typ) then
4673
4674                --  Use the inclusive size only if it is consistent with
4675                --  the explicitly specified size.
4676
4677                if Size_Incl_EP <= RM_Size (Typ) then
4678                   Actual_Lo   := Loval_Incl_EP;
4679                   Actual_Hi   := Hival_Incl_EP;
4680                   Actual_Size := Size_Incl_EP;
4681
4682                --  If the inclusive size is too large, we try excluding
4683                --  the end-points (will be caught later if does not work).
4684
4685                else
4686                   Actual_Lo   := Loval_Excl_EP;
4687                   Actual_Hi   := Hival_Excl_EP;
4688                   Actual_Size := Size_Excl_EP;
4689                end if;
4690
4691             --  Case of size clause not given
4692
4693             else
4694                --  If we have a base type whose corresponding first subtype
4695                --  has an explicit size that is large enough to include our
4696                --  end-points, then do so. There is no point in working hard
4697                --  to get a base type whose size is smaller than the specified
4698                --  size of the first subtype.
4699
4700                First_Subt := First_Subtype (Typ);
4701
4702                if Has_Size_Clause (First_Subt)
4703                  and then Size_Incl_EP <= Esize (First_Subt)
4704                then
4705                   Actual_Size := Size_Incl_EP;
4706                   Actual_Lo   := Loval_Incl_EP;
4707                   Actual_Hi   := Hival_Incl_EP;
4708
4709                --  If excluding the end-points makes the size smaller and
4710                --  results in a size of 8,16,32,64, then we take the smaller
4711                --  size. For the 64 case, this is compulsory. For the other
4712                --  cases, it seems reasonable. We like to include end points
4713                --  if we can, but not at the expense of moving to the next
4714                --  natural boundary of size.
4715
4716                elsif Size_Incl_EP /= Size_Excl_EP
4717                  and then
4718                     (Size_Excl_EP = 8  or else
4719                      Size_Excl_EP = 16 or else
4720                      Size_Excl_EP = 32 or else
4721                      Size_Excl_EP = 64)
4722                then
4723                   Actual_Size := Size_Excl_EP;
4724                   Actual_Lo   := Loval_Excl_EP;
4725                   Actual_Hi   := Hival_Excl_EP;
4726
4727                --  Otherwise we can definitely include the end points
4728
4729                else
4730                   Actual_Size := Size_Incl_EP;
4731                   Actual_Lo   := Loval_Incl_EP;
4732                   Actual_Hi   := Hival_Incl_EP;
4733                end if;
4734
4735                --  One pathological case: normally we never fudge a low bound
4736                --  down, since it would seem to increase the size (if it has
4737                --  any effect), but for ranges containing single value, or no
4738                --  values, the high bound can be small too large. Consider:
4739
4740                --    type t is delta 2.0**(-14)
4741                --      range 131072.0 .. 0;
4742
4743                --  That lower bound is *just* outside the range of 32 bits, and
4744                --  does need fudging down in this case. Note that the bounds
4745                --  will always have crossed here, since the high bound will be
4746                --  fudged down if necessary, as in the case of:
4747
4748                --    type t is delta 2.0**(-14)
4749                --      range 131072.0 .. 131072.0;
4750
4751                --  So we detect the situation by looking for crossed bounds,
4752                --  and if the bounds are crossed, and the low bound is greater
4753                --  than zero, we will always back it off by small, since this
4754                --  is completely harmless.
4755
4756                if Actual_Lo > Actual_Hi then
4757                   if UR_Is_Positive (Actual_Lo) then
4758                      Actual_Lo   := Loval_Incl_EP - Small;
4759                      Actual_Size := Fsize (Actual_Lo, Actual_Hi);
4760
4761                   --  And of course, we need to do exactly the same parallel
4762                   --  fudge for flat ranges in the negative region.
4763
4764                   elsif UR_Is_Negative (Actual_Hi) then
4765                      Actual_Hi := Hival_Incl_EP + Small;
4766                      Actual_Size := Fsize (Actual_Lo, Actual_Hi);
4767                   end if;
4768                end if;
4769             end if;
4770
4771             Set_Realval (Lo, Actual_Lo);
4772             Set_Realval (Hi, Actual_Hi);
4773          end Fudge;
4774
4775       --  For the decimal case, none of this fudging is required, since there
4776       --  are no end-point problems in the decimal case (the end-points are
4777       --  always included).
4778
4779       else
4780          Actual_Size := Fsize (Loval, Hival);
4781       end if;
4782
4783       --  At this stage, the actual size has been calculated and the proper
4784       --  required bounds are stored in the low and high bounds.
4785
4786       if Actual_Size > 64 then
4787          Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
4788          Error_Msg_N
4789            ("size required (^) for type& too large, maximum allowed is 64",
4790             Typ);
4791          Actual_Size := 64;
4792       end if;
4793
4794       --  Check size against explicit given size
4795
4796       if Has_Size_Clause (Typ) then
4797          if Actual_Size > RM_Size (Typ) then
4798             Error_Msg_Uint_1 := RM_Size (Typ);
4799             Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
4800             Error_Msg_NE
4801               ("size given (^) for type& too small, minimum allowed is ^",
4802                Size_Clause (Typ), Typ);
4803
4804          else
4805             Actual_Size := UI_To_Int (Esize (Typ));
4806          end if;
4807
4808       --  Increase size to next natural boundary if no size clause given
4809
4810       else
4811          if Actual_Size <= 8 then
4812             Actual_Size := 8;
4813          elsif Actual_Size <= 16 then
4814             Actual_Size := 16;
4815          elsif Actual_Size <= 32 then
4816             Actual_Size := 32;
4817          else
4818             Actual_Size := 64;
4819          end if;
4820
4821          Init_Esize (Typ, Actual_Size);
4822          Adjust_Esize_For_Alignment (Typ);
4823       end if;
4824
4825       --  If we have a base type, then expand the bounds so that they extend to
4826       --  the full width of the allocated size in bits, to avoid junk range
4827       --  checks on intermediate computations.
4828
4829       if Base_Type (Typ) = Typ then
4830          Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
4831          Set_Realval (Hi,  (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
4832       end if;
4833
4834       --  Final step is to reanalyze the bounds using the proper type
4835       --  and set the Corresponding_Integer_Value fields of the literals.
4836
4837       Set_Etype (Lo, Empty);
4838       Set_Analyzed (Lo, False);
4839       Analyze (Lo);
4840
4841       --  Resolve with universal fixed if the base type, and the base type if
4842       --  it is a subtype. Note we can't resolve the base type with itself,
4843       --  that would be a reference before definition.
4844
4845       if Typ = Btyp then
4846          Resolve (Lo, Universal_Fixed);
4847       else
4848          Resolve (Lo, Btyp);
4849       end if;
4850
4851       --  Set corresponding integer value for bound
4852
4853       Set_Corresponding_Integer_Value
4854         (Lo, UR_To_Uint (Realval (Lo) / Small));
4855
4856       --  Similar processing for high bound
4857
4858       Set_Etype (Hi, Empty);
4859       Set_Analyzed (Hi, False);
4860       Analyze (Hi);
4861
4862       if Typ = Btyp then
4863          Resolve (Hi, Universal_Fixed);
4864       else
4865          Resolve (Hi, Btyp);
4866       end if;
4867
4868       Set_Corresponding_Integer_Value
4869         (Hi, UR_To_Uint (Realval (Hi) / Small));
4870
4871       --  Set type of range to correspond to bounds
4872
4873       Set_Etype (Rng, Etype (Lo));
4874
4875       --  Set Esize to calculated size if not set already
4876
4877       if Unknown_Esize (Typ) then
4878          Init_Esize (Typ, Actual_Size);
4879       end if;
4880
4881       --  Set RM_Size if not already set. If already set, check value
4882
4883       declare
4884          Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
4885
4886       begin
4887          if RM_Size (Typ) /= Uint_0 then
4888             if RM_Size (Typ) < Minsiz then
4889                Error_Msg_Uint_1 := RM_Size (Typ);
4890                Error_Msg_Uint_2 := Minsiz;
4891                Error_Msg_NE
4892                  ("size given (^) for type& too small, minimum allowed is ^",
4893                   Size_Clause (Typ), Typ);
4894             end if;
4895
4896          else
4897             Set_RM_Size (Typ, Minsiz);
4898          end if;
4899       end;
4900    end Freeze_Fixed_Point_Type;
4901
4902    ------------------
4903    -- Freeze_Itype --
4904    ------------------
4905
4906    procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
4907       L : List_Id;
4908
4909    begin
4910       Set_Has_Delayed_Freeze (T);
4911       L := Freeze_Entity (T, Sloc (N));
4912
4913       if Is_Non_Empty_List (L) then
4914          Insert_Actions (N, L);
4915       end if;
4916    end Freeze_Itype;
4917
4918    --------------------------
4919    -- Freeze_Static_Object --
4920    --------------------------
4921
4922    procedure Freeze_Static_Object (E : Entity_Id) is
4923
4924       Cannot_Be_Static : exception;
4925       --  Exception raised if the type of a static object cannot be made
4926       --  static. This happens if the type depends on non-global objects.
4927
4928       procedure Ensure_Expression_Is_SA (N : Node_Id);
4929       --  Called to ensure that an expression used as part of a type definition
4930       --  is statically allocatable, which means that the expression type is
4931       --  statically allocatable, and the expression is either static, or a
4932       --  reference to a library level constant.
4933
4934       procedure Ensure_Type_Is_SA (Typ : Entity_Id);
4935       --  Called to mark a type as static, checking that it is possible
4936       --  to set the type as static. If it is not possible, then the
4937       --  exception Cannot_Be_Static is raised.
4938
4939       -----------------------------
4940       -- Ensure_Expression_Is_SA --
4941       -----------------------------
4942
4943       procedure Ensure_Expression_Is_SA (N : Node_Id) is
4944          Ent : Entity_Id;
4945
4946       begin
4947          Ensure_Type_Is_SA (Etype (N));
4948
4949          if Is_Static_Expression (N) then
4950             return;
4951
4952          elsif Nkind (N) = N_Identifier then
4953             Ent := Entity (N);
4954
4955             if Present (Ent)
4956               and then Ekind (Ent) = E_Constant
4957               and then Is_Library_Level_Entity (Ent)
4958             then
4959                return;
4960             end if;
4961          end if;
4962
4963          raise Cannot_Be_Static;
4964       end Ensure_Expression_Is_SA;
4965
4966       -----------------------
4967       -- Ensure_Type_Is_SA --
4968       -----------------------
4969
4970       procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
4971          N : Node_Id;
4972          C : Entity_Id;
4973
4974       begin
4975          --  If type is library level, we are all set
4976
4977          if Is_Library_Level_Entity (Typ) then
4978             return;
4979          end if;
4980
4981          --  We are also OK if the type already marked as statically allocated,
4982          --  which means we processed it before.
4983
4984          if Is_Statically_Allocated (Typ) then
4985             return;
4986          end if;
4987
4988          --  Mark type as statically allocated
4989
4990          Set_Is_Statically_Allocated (Typ);
4991
4992          --  Check that it is safe to statically allocate this type
4993
4994          if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
4995             Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
4996             Ensure_Expression_Is_SA (Type_High_Bound (Typ));
4997
4998          elsif Is_Array_Type (Typ) then
4999             N := First_Index (Typ);
5000             while Present (N) loop
5001                Ensure_Type_Is_SA (Etype (N));
5002                Next_Index (N);
5003             end loop;
5004
5005             Ensure_Type_Is_SA (Component_Type (Typ));
5006
5007          elsif Is_Access_Type (Typ) then
5008             if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
5009
5010                declare
5011                   F : Entity_Id;
5012                   T : constant Entity_Id := Etype (Designated_Type (Typ));
5013
5014                begin
5015                   if T /= Standard_Void_Type then
5016                      Ensure_Type_Is_SA (T);
5017                   end if;
5018
5019                   F := First_Formal (Designated_Type (Typ));
5020
5021                   while Present (F) loop
5022                      Ensure_Type_Is_SA (Etype (F));
5023                      Next_Formal (F);
5024                   end loop;
5025                end;
5026
5027             else
5028                Ensure_Type_Is_SA (Designated_Type (Typ));
5029             end if;
5030
5031          elsif Is_Record_Type (Typ) then
5032             C := First_Entity (Typ);
5033             while Present (C) loop
5034                if Ekind (C) = E_Discriminant
5035                  or else Ekind (C) = E_Component
5036                then
5037                   Ensure_Type_Is_SA (Etype (C));
5038
5039                elsif Is_Type (C) then
5040                   Ensure_Type_Is_SA (C);
5041                end if;
5042
5043                Next_Entity (C);
5044             end loop;
5045
5046          elsif Ekind (Typ) = E_Subprogram_Type then
5047             Ensure_Type_Is_SA (Etype (Typ));
5048
5049             C := First_Formal (Typ);
5050             while Present (C) loop
5051                Ensure_Type_Is_SA (Etype (C));
5052                Next_Formal (C);
5053             end loop;
5054
5055          else
5056             raise Cannot_Be_Static;
5057          end if;
5058       end Ensure_Type_Is_SA;
5059
5060    --  Start of processing for Freeze_Static_Object
5061
5062    begin
5063       Ensure_Type_Is_SA (Etype (E));
5064
5065    exception
5066       when Cannot_Be_Static =>
5067
5068          --  If the object that cannot be static is imported or exported, then
5069          --  issue an error message saying that this object cannot be imported
5070          --  or exported. If it has an address clause it is an overlay in the
5071          --  current partition and the static requirement is not relevant.
5072
5073          if Is_Imported (E) and then No (Address_Clause (E)) then
5074             Error_Msg_N
5075               ("& cannot be imported (local type is not constant)", E);
5076
5077          --  Otherwise must be exported, something is wrong if compiler
5078          --  is marking something as statically allocated which cannot be).
5079
5080          else pragma Assert (Is_Exported (E));
5081             Error_Msg_N
5082               ("& cannot be exported (local type is not constant)", E);
5083          end if;
5084    end Freeze_Static_Object;
5085
5086    -----------------------
5087    -- Freeze_Subprogram --
5088    -----------------------
5089
5090    procedure Freeze_Subprogram (E : Entity_Id) is
5091       Retype : Entity_Id;
5092       F      : Entity_Id;
5093
5094    begin
5095       --  Subprogram may not have an address clause unless it is imported
5096
5097       if Present (Address_Clause (E)) then
5098          if not Is_Imported (E) then
5099             Error_Msg_N
5100               ("address clause can only be given " &
5101                "for imported subprogram",
5102                Name (Address_Clause (E)));
5103          end if;
5104       end if;
5105
5106       --  Reset the Pure indication on an imported subprogram unless an
5107       --  explicit Pure_Function pragma was present. We do this because
5108       --  otherwise it is an insidious error to call a non-pure function from
5109       --  pure unit and have calls mysteriously optimized away. What happens
5110       --  here is that the Import can bypass the normal check to ensure that
5111       --  pure units call only pure subprograms.
5112
5113       if Is_Imported (E)
5114         and then Is_Pure (E)
5115         and then not Has_Pragma_Pure_Function (E)
5116       then
5117          Set_Is_Pure (E, False);
5118       end if;
5119
5120       --  For non-foreign convention subprograms, this is where we create
5121       --  the extra formals (for accessibility level and constrained bit
5122       --  information). We delay this till the freeze point precisely so
5123       --  that we know the convention!
5124
5125       if not Has_Foreign_Convention (E) then
5126          Create_Extra_Formals (E);
5127          Set_Mechanisms (E);
5128
5129          --  If this is convention Ada and a Valued_Procedure, that's odd
5130
5131          if Ekind (E) = E_Procedure
5132            and then Is_Valued_Procedure (E)
5133            and then Convention (E) = Convention_Ada
5134            and then Warn_On_Export_Import
5135          then
5136             Error_Msg_N
5137               ("?Valued_Procedure has no effect for convention Ada", E);
5138             Set_Is_Valued_Procedure (E, False);
5139          end if;
5140
5141       --  Case of foreign convention
5142
5143       else
5144          Set_Mechanisms (E);
5145
5146          --  For foreign conventions, warn about return of an
5147          --  unconstrained array.
5148
5149          --  Note: we *do* allow a return by descriptor for the VMS case,
5150          --  though here there is probably more to be done ???
5151
5152          if Ekind (E) = E_Function then
5153             Retype := Underlying_Type (Etype (E));
5154
5155             --  If no return type, probably some other error, e.g. a
5156             --  missing full declaration, so ignore.
5157
5158             if No (Retype) then
5159                null;
5160
5161             --  If the return type is generic, we have emitted a warning
5162             --  earlier on, and there is nothing else to check here. Specific
5163             --  instantiations may lead to erroneous behavior.
5164
5165             elsif Is_Generic_Type (Etype (E)) then
5166                null;
5167
5168             --  Display warning if returning unconstrained array
5169
5170             elsif Is_Array_Type (Retype)
5171               and then not Is_Constrained (Retype)
5172
5173               --  Exclude cases where descriptor mechanism is set, since the
5174               --  VMS descriptor mechanisms allow such unconstrained returns.
5175
5176               and then Mechanism (E) not in Descriptor_Codes
5177
5178               --  Check appropriate warning is enabled (should we check for
5179               --  Warnings (Off) on specific entities here, probably so???)
5180
5181               and then Warn_On_Export_Import
5182
5183                --  Exclude the VM case, since return of unconstrained arrays
5184                --  is properly handled in both the JVM and .NET cases.
5185
5186               and then VM_Target = No_VM
5187             then
5188                Error_Msg_N
5189                 ("?foreign convention function& should not return " &
5190                   "unconstrained array", E);
5191                return;
5192             end if;
5193          end if;
5194
5195          --  If any of the formals for an exported foreign convention
5196          --  subprogram have defaults, then emit an appropriate warning since
5197          --  this is odd (default cannot be used from non-Ada code)
5198
5199          if Is_Exported (E) then
5200             F := First_Formal (E);
5201             while Present (F) loop
5202                if Warn_On_Export_Import
5203                  and then Present (Default_Value (F))
5204                then
5205                   Error_Msg_N
5206                     ("?parameter cannot be defaulted in non-Ada call",
5207                      Default_Value (F));
5208                end if;
5209
5210                Next_Formal (F);
5211             end loop;
5212          end if;
5213       end if;
5214
5215       --  For VMS, descriptor mechanisms for parameters are allowed only for
5216       --  imported/exported subprograms. Moreover, the NCA descriptor is not
5217       --  allowed for parameters of exported subprograms.
5218
5219       if OpenVMS_On_Target then
5220          if Is_Exported (E) then
5221             F := First_Formal (E);
5222             while Present (F) loop
5223                if Mechanism (F) = By_Descriptor_NCA then
5224                   Error_Msg_N
5225                     ("'N'C'A' descriptor for parameter not permitted", F);
5226                   Error_Msg_N
5227                     ("\can only be used for imported subprogram", F);
5228                end if;
5229
5230                Next_Formal (F);
5231             end loop;
5232
5233          elsif not Is_Imported (E) then
5234             F := First_Formal (E);
5235             while Present (F) loop
5236                if Mechanism (F) in Descriptor_Codes then
5237                   Error_Msg_N
5238                     ("descriptor mechanism for parameter not permitted", F);
5239                   Error_Msg_N
5240                     ("\can only be used for imported/exported subprogram", F);
5241                end if;
5242
5243                Next_Formal (F);
5244             end loop;
5245          end if;
5246       end if;
5247
5248       --  Pragma Inline_Always is disallowed for dispatching subprograms
5249       --  because the address of such subprograms is saved in the dispatch
5250       --  table to support dispatching calls, and dispatching calls cannot
5251       --  be inlined. This is consistent with the restriction against using
5252       --  'Access or 'Address on an Inline_Always subprogram.
5253
5254       if Is_Dispatching_Operation (E)
5255         and then Has_Pragma_Inline_Always (E)
5256       then
5257          Error_Msg_N
5258            ("pragma Inline_Always not allowed for dispatching subprograms", E);
5259       end if;
5260
5261       --  Because of the implicit representation of inherited predefined
5262       --  operators in the front-end, the overriding status of the operation
5263       --  may be affected when a full view of a type is analyzed, and this is
5264       --  not captured by the analysis of the corresponding type declaration.
5265       --  Therefore the correctness of a not-overriding indicator must be
5266       --  rechecked when the subprogram is frozen.
5267
5268       if Nkind (E) = N_Defining_Operator_Symbol
5269         and then not Error_Posted (Parent (E))
5270       then
5271          Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
5272       end if;
5273    end Freeze_Subprogram;
5274
5275    ----------------------
5276    -- Is_Fully_Defined --
5277    ----------------------
5278
5279    function Is_Fully_Defined (T : Entity_Id) return Boolean is
5280    begin
5281       if Ekind (T) = E_Class_Wide_Type then
5282          return Is_Fully_Defined (Etype (T));
5283
5284       elsif Is_Array_Type (T) then
5285          return Is_Fully_Defined (Component_Type (T));
5286
5287       elsif Is_Record_Type (T)
5288         and not Is_Private_Type (T)
5289       then
5290          --  Verify that the record type has no components with private types
5291          --  without completion.
5292
5293          declare
5294             Comp : Entity_Id;
5295
5296          begin
5297             Comp := First_Component (T);
5298
5299             while Present (Comp) loop
5300                if not Is_Fully_Defined (Etype (Comp)) then
5301                   return False;
5302                end if;
5303
5304                Next_Component (Comp);
5305             end loop;
5306             return True;
5307          end;
5308
5309       else
5310          return not Is_Private_Type (T)
5311            or else Present (Full_View (Base_Type (T)));
5312       end if;
5313    end Is_Fully_Defined;
5314
5315    ---------------------------------
5316    -- Process_Default_Expressions --
5317    ---------------------------------
5318
5319    procedure Process_Default_Expressions
5320      (E     : Entity_Id;
5321       After : in out Node_Id)
5322    is
5323       Loc    : constant Source_Ptr := Sloc (E);
5324       Dbody  : Node_Id;
5325       Formal : Node_Id;
5326       Dcopy  : Node_Id;
5327       Dnam   : Entity_Id;
5328
5329    begin
5330       Set_Default_Expressions_Processed (E);
5331
5332       --  A subprogram instance and its associated anonymous subprogram share
5333       --  their signature. The default expression functions are defined in the
5334       --  wrapper packages for the anonymous subprogram, and should not be
5335       --  generated again for the instance.
5336
5337       if Is_Generic_Instance (E)
5338         and then Present (Alias (E))
5339         and then Default_Expressions_Processed (Alias (E))
5340       then
5341          return;
5342       end if;
5343
5344       Formal := First_Formal (E);
5345       while Present (Formal) loop
5346          if Present (Default_Value (Formal)) then
5347
5348             --  We work with a copy of the default expression because we
5349             --  do not want to disturb the original, since this would mess
5350             --  up the conformance checking.
5351
5352             Dcopy := New_Copy_Tree (Default_Value (Formal));
5353
5354             --  The analysis of the expression may generate insert actions,
5355             --  which of course must not be executed. We wrap those actions
5356             --  in a procedure that is not called, and later on eliminated.
5357             --  The following cases have no side-effects, and are analyzed
5358             --  directly.
5359
5360             if Nkind (Dcopy) = N_Identifier
5361               or else Nkind (Dcopy) = N_Expanded_Name
5362               or else Nkind (Dcopy) = N_Integer_Literal
5363               or else (Nkind (Dcopy) = N_Real_Literal
5364                         and then not Vax_Float (Etype (Dcopy)))
5365               or else Nkind (Dcopy) = N_Character_Literal
5366               or else Nkind (Dcopy) = N_String_Literal
5367               or else Known_Null (Dcopy)
5368               or else (Nkind (Dcopy) = N_Attribute_Reference
5369                         and then
5370                        Attribute_Name (Dcopy) = Name_Null_Parameter)
5371             then
5372
5373                --  If there is no default function, we must still do a full
5374                --  analyze call on the default value, to ensure that all error
5375                --  checks are performed, e.g. those associated with static
5376                --  evaluation. Note: this branch will always be taken if the
5377                --  analyzer is turned off (but we still need the error checks).
5378
5379                --  Note: the setting of parent here is to meet the requirement
5380                --  that we can only analyze the expression while attached to
5381                --  the tree. Really the requirement is that the parent chain
5382                --  be set, we don't actually need to be in the tree.
5383
5384                Set_Parent (Dcopy, Declaration_Node (Formal));
5385                Analyze (Dcopy);
5386
5387                --  Default expressions are resolved with their own type if the
5388                --  context is generic, to avoid anomalies with private types.
5389
5390                if Ekind (Scope (E)) = E_Generic_Package then
5391                   Resolve (Dcopy);
5392                else
5393                   Resolve (Dcopy, Etype (Formal));
5394                end if;
5395
5396                --  If that resolved expression will raise constraint error,
5397                --  then flag the default value as raising constraint error.
5398                --  This allows a proper error message on the calls.
5399
5400                if Raises_Constraint_Error (Dcopy) then
5401                   Set_Raises_Constraint_Error (Default_Value (Formal));
5402                end if;
5403
5404             --  If the default is a parameterless call, we use the name of
5405             --  the called function directly, and there is no body to build.
5406
5407             elsif Nkind (Dcopy) = N_Function_Call
5408               and then No (Parameter_Associations (Dcopy))
5409             then
5410                null;
5411
5412             --  Else construct and analyze the body of a wrapper procedure
5413             --  that contains an object declaration to hold the expression.
5414             --  Given that this is done only to complete the analysis, it
5415             --  simpler to build a procedure than a function which might
5416             --  involve secondary stack expansion.
5417
5418             else
5419                Dnam := Make_Temporary (Loc, 'D');
5420
5421                Dbody :=
5422                  Make_Subprogram_Body (Loc,
5423                    Specification =>
5424                      Make_Procedure_Specification (Loc,
5425                        Defining_Unit_Name => Dnam),
5426
5427                    Declarations => New_List (
5428                      Make_Object_Declaration (Loc,
5429                        Defining_Identifier =>
5430                          Make_Defining_Identifier (Loc,
5431                            New_Internal_Name ('T')),
5432                          Object_Definition =>
5433                            New_Occurrence_Of (Etype (Formal), Loc),
5434                          Expression => New_Copy_Tree (Dcopy))),
5435
5436                    Handled_Statement_Sequence =>
5437                      Make_Handled_Sequence_Of_Statements (Loc,
5438                        Statements => New_List));
5439
5440                Set_Scope (Dnam, Scope (E));
5441                Set_Assignment_OK (First (Declarations (Dbody)));
5442                Set_Is_Eliminated (Dnam);
5443                Insert_After (After, Dbody);
5444                Analyze (Dbody);
5445                After := Dbody;
5446             end if;
5447          end if;
5448
5449          Next_Formal (Formal);
5450       end loop;
5451    end Process_Default_Expressions;
5452
5453    ----------------------------------------
5454    -- Set_Component_Alignment_If_Not_Set --
5455    ----------------------------------------
5456
5457    procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
5458    begin
5459       --  Ignore if not base type, subtypes don't need anything
5460
5461       if Typ /= Base_Type (Typ) then
5462          return;
5463       end if;
5464
5465       --  Do not override existing representation
5466
5467       if Is_Packed (Typ) then
5468          return;
5469
5470       elsif Has_Specified_Layout (Typ) then
5471          return;
5472
5473       elsif Component_Alignment (Typ) /= Calign_Default then
5474          return;
5475
5476       else
5477          Set_Component_Alignment
5478            (Typ, Scope_Stack.Table
5479                   (Scope_Stack.Last).Component_Alignment_Default);
5480       end if;
5481    end Set_Component_Alignment_If_Not_Set;
5482
5483    ------------------
5484    -- Undelay_Type --
5485    ------------------
5486
5487    procedure Undelay_Type (T : Entity_Id) is
5488    begin
5489       Set_Has_Delayed_Freeze (T, False);
5490       Set_Freeze_Node (T, Empty);
5491
5492       --  Since we don't want T to have a Freeze_Node, we don't want its
5493       --  Full_View or Corresponding_Record_Type to have one either.
5494
5495       --  ??? Fundamentally, this whole handling is a kludge. What we really
5496       --  want is to be sure that for an Itype that's part of record R and is a
5497       --  subtype of type T, that it's frozen after the later of the freeze
5498       --  points of R and T. We have no way of doing that directly, so what we
5499       --  do is force most such Itypes to be frozen as part of freezing R via
5500       --  this procedure and only delay the ones that need to be delayed
5501       --  (mostly the designated types of access types that are defined as part
5502       --  of the record).
5503
5504       if Is_Private_Type (T)
5505         and then Present (Full_View (T))
5506         and then Is_Itype (Full_View (T))
5507         and then Is_Record_Type (Scope (Full_View (T)))
5508       then
5509          Undelay_Type (Full_View (T));
5510       end if;
5511
5512       if Is_Concurrent_Type (T)
5513         and then Present (Corresponding_Record_Type (T))
5514         and then Is_Itype (Corresponding_Record_Type (T))
5515         and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
5516       then
5517          Undelay_Type (Corresponding_Record_Type (T));
5518       end if;
5519    end Undelay_Type;
5520
5521    ------------------
5522    -- Warn_Overlay --
5523    ------------------
5524
5525    procedure Warn_Overlay
5526      (Expr : Node_Id;
5527       Typ  : Entity_Id;
5528       Nam  : Entity_Id)
5529    is
5530       Ent : constant Entity_Id := Entity (Nam);
5531       --  The object to which the address clause applies
5532
5533       Init : Node_Id;
5534       Old  : Entity_Id := Empty;
5535       Decl : Node_Id;
5536
5537    begin
5538       --  No warning if address clause overlay warnings are off
5539
5540       if not Address_Clause_Overlay_Warnings then
5541          return;
5542       end if;
5543
5544       --  No warning if there is an explicit initialization
5545
5546       Init := Original_Node (Expression (Declaration_Node (Ent)));
5547
5548       if Present (Init) and then Comes_From_Source (Init) then
5549          return;
5550       end if;
5551
5552       --  We only give the warning for non-imported entities of a type for
5553       --  which a non-null base init proc is defined, or for objects of access
5554       --  types with implicit null initialization, or when Normalize_Scalars
5555       --  applies and the type is scalar or a string type (the latter being
5556       --  tested for because predefined String types are initialized by inline
5557       --  code rather than by an init_proc). Note that we do not give the
5558       --  warning for Initialize_Scalars, since we suppressed initialization
5559       --  in this case.
5560
5561       if Present (Expr)
5562         and then not Is_Imported (Ent)
5563         and then (Has_Non_Null_Base_Init_Proc (Typ)
5564                     or else Is_Access_Type (Typ)
5565                     or else (Normalize_Scalars
5566                               and then (Is_Scalar_Type (Typ)
5567                                          or else Is_String_Type (Typ))))
5568       then
5569          if Nkind (Expr) = N_Attribute_Reference
5570            and then Is_Entity_Name (Prefix (Expr))
5571          then
5572             Old := Entity (Prefix (Expr));
5573
5574          elsif Is_Entity_Name (Expr)
5575            and then Ekind (Entity (Expr)) = E_Constant
5576          then
5577             Decl := Declaration_Node (Entity (Expr));
5578
5579             if Nkind (Decl) = N_Object_Declaration
5580               and then Present (Expression (Decl))
5581               and then Nkind (Expression (Decl)) = N_Attribute_Reference
5582               and then Is_Entity_Name (Prefix (Expression (Decl)))
5583             then
5584                Old := Entity (Prefix (Expression (Decl)));
5585
5586             elsif Nkind (Expr) = N_Function_Call then
5587                return;
5588             end if;
5589
5590          --  A function call (most likely to To_Address) is probably not an
5591          --  overlay, so skip warning. Ditto if the function call was inlined
5592          --  and transformed into an entity.
5593
5594          elsif Nkind (Original_Node (Expr)) = N_Function_Call then
5595             return;
5596          end if;
5597
5598          Decl := Next (Parent (Expr));
5599
5600          --  If a pragma Import follows, we assume that it is for the current
5601          --  target of the address clause, and skip the warning.
5602
5603          if Present (Decl)
5604            and then Nkind (Decl) = N_Pragma
5605            and then Pragma_Name (Decl) = Name_Import
5606          then
5607             return;
5608          end if;
5609
5610          if Present (Old) then
5611             Error_Msg_Node_2 := Old;
5612             Error_Msg_N
5613               ("default initialization of & may modify &?",
5614                Nam);
5615          else
5616             Error_Msg_N
5617               ("default initialization of & may modify overlaid storage?",
5618                Nam);
5619          end if;
5620
5621          --  Add friendly warning if initialization comes from a packed array
5622          --  component.
5623
5624          if Is_Record_Type (Typ)  then
5625             declare
5626                Comp : Entity_Id;
5627
5628             begin
5629                Comp := First_Component (Typ);
5630
5631                while Present (Comp) loop
5632                   if Nkind (Parent (Comp)) = N_Component_Declaration
5633                     and then Present (Expression (Parent (Comp)))
5634                   then
5635                      exit;
5636                   elsif Is_Array_Type (Etype (Comp))
5637                      and then Present (Packed_Array_Type (Etype (Comp)))
5638                   then
5639                      Error_Msg_NE
5640                        ("\packed array component& " &
5641                         "will be initialized to zero?",
5642                         Nam, Comp);
5643                      exit;
5644                   else
5645                      Next_Component (Comp);
5646                   end if;
5647                end loop;
5648             end;
5649          end if;
5650
5651          Error_Msg_N
5652            ("\use pragma Import for & to " &
5653             "suppress initialization (RM B.1(24))?",
5654             Nam);
5655       end if;
5656    end Warn_Overlay;
5657
5658 end Freeze;