OSDN Git Service

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