OSDN Git Service

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