OSDN Git Service

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