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