OSDN Git Service

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