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