OSDN Git Service

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