OSDN Git Service

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