OSDN Git Service

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