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-2015, 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 3,  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 COPYING3.  If not, go to --
19-- http://www.gnu.org/licenses for a complete copy of the license.          --
20--                                                                          --
21-- GNAT was originally developed  by the GNAT team at  New York University. --
22-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23--                                                                          --
24------------------------------------------------------------------------------
25
26with Aspects;  use Aspects;
27with Atree;    use Atree;
28with Checks;   use Checks;
29with Debug;    use Debug;
30with Einfo;    use Einfo;
31with Elists;   use Elists;
32with Errout;   use Errout;
33with Exp_Ch3;  use Exp_Ch3;
34with Exp_Ch7;  use Exp_Ch7;
35with Exp_Disp; use Exp_Disp;
36with Exp_Pakd; use Exp_Pakd;
37with Exp_Util; use Exp_Util;
38with Exp_Tss;  use Exp_Tss;
39with Fname;    use Fname;
40with Ghost;    use Ghost;
41with Layout;   use Layout;
42with Lib;      use Lib;
43with Namet;    use Namet;
44with Nlists;   use Nlists;
45with Nmake;    use Nmake;
46with Opt;      use Opt;
47with Restrict; use Restrict;
48with Rident;   use Rident;
49with Rtsfind;  use Rtsfind;
50with Sem;      use Sem;
51with Sem_Aux;  use Sem_Aux;
52with Sem_Cat;  use Sem_Cat;
53with Sem_Ch6;  use Sem_Ch6;
54with Sem_Ch7;  use Sem_Ch7;
55with Sem_Ch8;  use Sem_Ch8;
56with Sem_Ch13; use Sem_Ch13;
57with Sem_Eval; use Sem_Eval;
58with Sem_Mech; use Sem_Mech;
59with Sem_Prag; use Sem_Prag;
60with Sem_Res;  use Sem_Res;
61with Sem_Util; use Sem_Util;
62with Sinfo;    use Sinfo;
63with Snames;   use Snames;
64with Stand;    use Stand;
65with Targparm; use Targparm;
66with Tbuild;   use Tbuild;
67with Ttypes;   use Ttypes;
68with Uintp;    use Uintp;
69with Urealp;   use Urealp;
70with Warnsw;   use Warnsw;
71
72package body Freeze is
73
74   -----------------------
75   -- Local Subprograms --
76   -----------------------
77
78   procedure Adjust_Esize_For_Alignment (Typ : Entity_Id);
79   --  Typ is a type that is being frozen. If no size clause is given,
80   --  but a default Esize has been computed, then this default Esize is
81   --  adjusted up if necessary to be consistent with a given alignment,
82   --  but never to a value greater than Long_Long_Integer'Size. This
83   --  is used for all discrete types and for fixed-point types.
84
85   procedure Build_And_Analyze_Renamed_Body
86     (Decl  : Node_Id;
87      New_S : Entity_Id;
88      After : in out Node_Id);
89   --  Build body for a renaming declaration, insert in tree and analyze
90
91   procedure Check_Address_Clause (E : Entity_Id);
92   --  Apply legality checks to address clauses for object declarations,
93   --  at the point the object is frozen. Also ensure any initialization is
94   --  performed only after the object has been frozen.
95
96   procedure Check_Component_Storage_Order
97     (Encl_Type        : Entity_Id;
98      Comp             : Entity_Id;
99      ADC              : Node_Id;
100      Comp_ADC_Present : out Boolean);
101   --  For an Encl_Type that has a Scalar_Storage_Order attribute definition
102   --  clause, verify that the component type has an explicit and compatible
103   --  attribute/aspect. For arrays, Comp is Empty; for records, it is the
104   --  entity of the component under consideration. For an Encl_Type that
105   --  does not have a Scalar_Storage_Order attribute definition clause,
106   --  verify that the component also does not have such a clause.
107   --  ADC is the attribute definition clause if present (or Empty). On return,
108   --  Comp_ADC_Present is set True if the component has a Scalar_Storage_Order
109   --  attribute definition clause.
110
111   procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id);
112   --  When an expression function is frozen by a use of it, the expression
113   --  itself is frozen. Check that the expression does not include references
114   --  to deferred constants without completion. We report this at the freeze
115   --  point of the function, to provide a better error message.
116   --
117   --  In most cases the expression itself is frozen by the time the function
118   --  itself is frozen, because the formals will be frozen by then. However,
119   --  Attribute references to outer types are freeze points for those types;
120   --  this routine generates the required freeze nodes for them.
121
122   procedure Check_Strict_Alignment (E : Entity_Id);
123   --  E is a base type. If E is tagged or has a component that is aliased
124   --  or tagged or contains something this is aliased or tagged, set
125   --  Strict_Alignment.
126
127   procedure Check_Unsigned_Type (E : Entity_Id);
128   pragma Inline (Check_Unsigned_Type);
129   --  If E is a fixed-point or discrete type, then all the necessary work
130   --  to freeze it is completed except for possible setting of the flag
131   --  Is_Unsigned_Type, which is done by this procedure. The call has no
132   --  effect if the entity E is not a discrete or fixed-point type.
133
134   procedure Freeze_And_Append
135     (Ent    : Entity_Id;
136      N      : Node_Id;
137      Result : in out List_Id);
138   --  Freezes Ent using Freeze_Entity, and appends the resulting list of
139   --  nodes to Result, modifying Result from No_List if necessary. N has
140   --  the same usage as in Freeze_Entity.
141
142   procedure Freeze_Enumeration_Type (Typ : Entity_Id);
143   --  Freeze enumeration type. The Esize field is set as processing
144   --  proceeds (i.e. set by default when the type is declared and then
145   --  adjusted by rep clauses. What this procedure does is to make sure
146   --  that if a foreign convention is specified, and no specific size
147   --  is given, then the size must be at least Integer'Size.
148
149   procedure Freeze_Static_Object (E : Entity_Id);
150   --  If an object is frozen which has Is_Statically_Allocated set, then
151   --  all referenced types must also be marked with this flag. This routine
152   --  is in charge of meeting this requirement for the object entity E.
153
154   procedure Freeze_Subprogram (E : Entity_Id);
155   --  Perform freezing actions for a subprogram (create extra formals,
156   --  and set proper default mechanism values). Note that this routine
157   --  is not called for internal subprograms, for which neither of these
158   --  actions is needed (or desirable, we do not want for example to have
159   --  these extra formals present in initialization procedures, where they
160   --  would serve no purpose). In this call E is either a subprogram or
161   --  a subprogram type (i.e. an access to a subprogram).
162
163   function Is_Fully_Defined (T : Entity_Id) return Boolean;
164   --  True if T is not private and has no private components, or has a full
165   --  view. Used to determine whether the designated type of an access type
166   --  should be frozen when the access type is frozen. This is done when an
167   --  allocator is frozen, or an expression that may involve attributes of
168   --  the designated type. Otherwise freezing the access type does not freeze
169   --  the designated type.
170
171   procedure Process_Default_Expressions
172     (E     : Entity_Id;
173      After : in out Node_Id);
174   --  This procedure is called for each subprogram to complete processing of
175   --  default expressions at the point where all types are known to be frozen.
176   --  The expressions must be analyzed in full, to make sure that all error
177   --  processing is done (they have only been pre-analyzed). If the expression
178   --  is not an entity or literal, its analysis may generate code which must
179   --  not be executed. In that case we build a function body to hold that
180   --  code. This wrapper function serves no other purpose (it used to be
181   --  called to evaluate the default, but now the default is inlined at each
182   --  point of call).
183
184   procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id);
185   --  Typ is a record or array type that is being frozen. This routine sets
186   --  the default component alignment from the scope stack values if the
187   --  alignment is otherwise not specified.
188
189   procedure Check_Debug_Info_Needed (T : Entity_Id);
190   --  As each entity is frozen, this routine is called to deal with the
191   --  setting of Debug_Info_Needed for the entity. This flag is set if
192   --  the entity comes from source, or if we are in Debug_Generated_Code
193   --  mode or if the -gnatdV debug flag is set. However, it never sets
194   --  the flag if Debug_Info_Off is set. This procedure also ensures that
195   --  subsidiary entities have the flag set as required.
196
197   procedure Set_SSO_From_Default (T : Entity_Id);
198   --  T is a record or array type that is being frozen. If it is a base type,
199   --  and if SSO_Set_Low/High_By_Default is set, then Reverse_Storage order
200   --  will be set appropriately. Note that an explicit occurrence of aspect
201   --  Scalar_Storage_Order or an explicit setting of this aspect with an
202   --  attribute definition clause occurs, then these two flags are reset in
203   --  any case, so call will have no effect.
204
205   procedure Undelay_Type (T : Entity_Id);
206   --  T is a type of a component that we know to be an Itype. We don't want
207   --  this to have a Freeze_Node, so ensure it doesn't. Do the same for any
208   --  Full_View or Corresponding_Record_Type.
209
210   procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Node_Id);
211   --  Expr is the expression for an address clause for entity Nam whose type
212   --  is Typ. If Typ has a default initialization, and there is no explicit
213   --  initialization in the source declaration, check whether the address
214   --  clause might cause overlaying of an entity, and emit a warning on the
215   --  side effect that the initialization will cause.
216
217   -------------------------------
218   -- Adjust_Esize_For_Alignment --
219   -------------------------------
220
221   procedure Adjust_Esize_For_Alignment (Typ : Entity_Id) is
222      Align : Uint;
223
224   begin
225      if Known_Esize (Typ) and then Known_Alignment (Typ) then
226         Align := Alignment_In_Bits (Typ);
227
228         if Align > Esize (Typ)
229           and then Align <= Standard_Long_Long_Integer_Size
230         then
231            Set_Esize (Typ, Align);
232         end if;
233      end if;
234   end Adjust_Esize_For_Alignment;
235
236   ------------------------------------
237   -- Build_And_Analyze_Renamed_Body --
238   ------------------------------------
239
240   procedure Build_And_Analyze_Renamed_Body
241     (Decl  : Node_Id;
242      New_S : Entity_Id;
243      After : in out Node_Id)
244   is
245      Body_Decl    : constant Node_Id := Unit_Declaration_Node (New_S);
246      Ent          : constant Entity_Id := Defining_Entity (Decl);
247      Body_Node    : Node_Id;
248      Renamed_Subp : Entity_Id;
249
250   begin
251      --  If the renamed subprogram is intrinsic, there is no need for a
252      --  wrapper body: we set the alias that will be called and expanded which
253      --  completes the declaration. This transformation is only legal if the
254      --  renamed entity has already been elaborated.
255
256      --  Note that it is legal for a renaming_as_body to rename an intrinsic
257      --  subprogram, as long as the renaming occurs before the new entity
258      --  is frozen (RM 8.5.4 (5)).
259
260      if Nkind (Body_Decl) = N_Subprogram_Renaming_Declaration
261        and then Is_Entity_Name (Name (Body_Decl))
262      then
263         Renamed_Subp := Entity (Name (Body_Decl));
264      else
265         Renamed_Subp := Empty;
266      end if;
267
268      if Present (Renamed_Subp)
269        and then Is_Intrinsic_Subprogram (Renamed_Subp)
270        and then
271          (not In_Same_Source_Unit (Renamed_Subp, Ent)
272            or else Sloc (Renamed_Subp) < Sloc (Ent))
273
274        --  We can make the renaming entity intrinsic if the renamed function
275        --  has an interface name, or if it is one of the shift/rotate
276        --  operations known to the compiler.
277
278        and then
279          (Present (Interface_Name (Renamed_Subp))
280            or else Nam_In (Chars (Renamed_Subp), Name_Rotate_Left,
281                                                  Name_Rotate_Right,
282                                                  Name_Shift_Left,
283                                                  Name_Shift_Right,
284                                                  Name_Shift_Right_Arithmetic))
285      then
286         Set_Interface_Name (Ent, Interface_Name (Renamed_Subp));
287
288         if Present (Alias (Renamed_Subp)) then
289            Set_Alias (Ent, Alias (Renamed_Subp));
290         else
291            Set_Alias (Ent, Renamed_Subp);
292         end if;
293
294         Set_Is_Intrinsic_Subprogram (Ent);
295         Set_Has_Completion (Ent);
296
297      else
298         Body_Node := Build_Renamed_Body (Decl, New_S);
299         Insert_After (After, Body_Node);
300         Mark_Rewrite_Insertion (Body_Node);
301         Analyze (Body_Node);
302         After := Body_Node;
303      end if;
304   end Build_And_Analyze_Renamed_Body;
305
306   ------------------------
307   -- Build_Renamed_Body --
308   ------------------------
309
310   function Build_Renamed_Body
311     (Decl  : Node_Id;
312      New_S : Entity_Id) return Node_Id
313   is
314      Loc : constant Source_Ptr := Sloc (New_S);
315      --  We use for the source location of the renamed body, the location of
316      --  the spec entity. It might seem more natural to use the location of
317      --  the renaming declaration itself, but that would be wrong, since then
318      --  the body we create would look as though it was created far too late,
319      --  and this could cause problems with elaboration order analysis,
320      --  particularly in connection with instantiations.
321
322      N          : constant Node_Id := Unit_Declaration_Node (New_S);
323      Nam        : constant Node_Id := Name (N);
324      Old_S      : Entity_Id;
325      Spec       : constant Node_Id := New_Copy_Tree (Specification (Decl));
326      Actuals    : List_Id := No_List;
327      Call_Node  : Node_Id;
328      Call_Name  : Node_Id;
329      Body_Node  : Node_Id;
330      Formal     : Entity_Id;
331      O_Formal   : Entity_Id;
332      Param_Spec : Node_Id;
333
334      Pref : Node_Id := Empty;
335      --  If the renamed entity is a primitive operation given in prefix form,
336      --  the prefix is the target object and it has to be added as the first
337      --  actual in the generated call.
338
339   begin
340      --  Determine the entity being renamed, which is the target of the call
341      --  statement. If the name is an explicit dereference, this is a renaming
342      --  of a subprogram type rather than a subprogram. The name itself is
343      --  fully analyzed.
344
345      if Nkind (Nam) = N_Selected_Component then
346         Old_S := Entity (Selector_Name (Nam));
347
348      elsif Nkind (Nam) = N_Explicit_Dereference then
349         Old_S := Etype (Nam);
350
351      elsif Nkind (Nam) = N_Indexed_Component then
352         if Is_Entity_Name (Prefix (Nam)) then
353            Old_S := Entity (Prefix (Nam));
354         else
355            Old_S := Entity (Selector_Name (Prefix (Nam)));
356         end if;
357
358      elsif Nkind (Nam) = N_Character_Literal then
359         Old_S := Etype (New_S);
360
361      else
362         Old_S := Entity (Nam);
363      end if;
364
365      if Is_Entity_Name (Nam) then
366
367         --  If the renamed entity is a predefined operator, retain full name
368         --  to ensure its visibility.
369
370         if Ekind (Old_S) = E_Operator
371           and then Nkind (Nam) = N_Expanded_Name
372         then
373            Call_Name := New_Copy (Name (N));
374         else
375            Call_Name := New_Occurrence_Of (Old_S, Loc);
376         end if;
377
378      else
379         if Nkind (Nam) = N_Selected_Component
380           and then Present (First_Formal (Old_S))
381           and then
382             (Is_Controlling_Formal (First_Formal (Old_S))
383                or else Is_Class_Wide_Type (Etype (First_Formal (Old_S))))
384         then
385
386            --  Retrieve the target object, to be added as a first actual
387            --  in the call.
388
389            Call_Name := New_Occurrence_Of (Old_S, Loc);
390            Pref := Prefix (Nam);
391
392         else
393            Call_Name := New_Copy (Name (N));
394         end if;
395
396         --  Original name may have been overloaded, but is fully resolved now
397
398         Set_Is_Overloaded (Call_Name, False);
399      end if;
400
401      --  For simple renamings, subsequent calls can be expanded directly as
402      --  calls to the renamed entity. The body must be generated in any case
403      --  for calls that may appear elsewhere. This is not done in the case
404      --  where the subprogram is an instantiation because the actual proper
405      --  body has not been built yet.
406
407      if Ekind_In (Old_S, E_Function, E_Procedure)
408        and then Nkind (Decl) = N_Subprogram_Declaration
409        and then not Is_Generic_Instance (Old_S)
410      then
411         Set_Body_To_Inline (Decl, Old_S);
412      end if;
413
414      --  Check whether the return type is a limited view. If the subprogram
415      --  is already frozen the generated body may have a non-limited view
416      --  of the type, that must be used, because it is the one in the spec
417      --  of the renaming declaration.
418
419      if Ekind (Old_S) = E_Function
420        and then Is_Entity_Name (Result_Definition (Spec))
421      then
422         declare
423            Ret_Type : constant Entity_Id := Etype (Result_Definition (Spec));
424         begin
425            if Has_Non_Limited_View (Ret_Type) then
426               Set_Result_Definition
427                 (Spec, New_Occurrence_Of (Non_Limited_View (Ret_Type), Loc));
428            end if;
429         end;
430      end if;
431
432      --  The body generated for this renaming is an internal artifact, and
433      --  does not  constitute a freeze point for the called entity.
434
435      Set_Must_Not_Freeze (Call_Name);
436
437      Formal := First_Formal (Defining_Entity (Decl));
438
439      if Present (Pref) then
440         declare
441            Pref_Type : constant Entity_Id := Etype (Pref);
442            Form_Type : constant Entity_Id := Etype (First_Formal (Old_S));
443
444         begin
445            --  The controlling formal may be an access parameter, or the
446            --  actual may be an access value, so adjust accordingly.
447
448            if Is_Access_Type (Pref_Type)
449              and then not Is_Access_Type (Form_Type)
450            then
451               Actuals := New_List
452                 (Make_Explicit_Dereference (Loc, Relocate_Node (Pref)));
453
454            elsif Is_Access_Type (Form_Type)
455              and then not Is_Access_Type (Pref)
456            then
457               Actuals :=
458                 New_List (
459                   Make_Attribute_Reference (Loc,
460                     Attribute_Name => Name_Access,
461                     Prefix         => Relocate_Node (Pref)));
462            else
463               Actuals := New_List (Pref);
464            end if;
465         end;
466
467      elsif Present (Formal) then
468         Actuals := New_List;
469
470      else
471         Actuals := No_List;
472      end if;
473
474      if Present (Formal) then
475         while Present (Formal) loop
476            Append (New_Occurrence_Of (Formal, Loc), Actuals);
477            Next_Formal (Formal);
478         end loop;
479      end if;
480
481      --  If the renamed entity is an entry, inherit its profile. For other
482      --  renamings as bodies, both profiles must be subtype conformant, so it
483      --  is not necessary to replace the profile given in the declaration.
484      --  However, default values that are aggregates are rewritten when
485      --  partially analyzed, so we recover the original aggregate to insure
486      --  that subsequent conformity checking works. Similarly, if the default
487      --  expression was constant-folded, recover the original expression.
488
489      Formal := First_Formal (Defining_Entity (Decl));
490
491      if Present (Formal) then
492         O_Formal := First_Formal (Old_S);
493         Param_Spec := First (Parameter_Specifications (Spec));
494         while Present (Formal) loop
495            if Is_Entry (Old_S) then
496               if Nkind (Parameter_Type (Param_Spec)) /=
497                                                    N_Access_Definition
498               then
499                  Set_Etype (Formal, Etype (O_Formal));
500                  Set_Entity (Parameter_Type (Param_Spec), Etype (O_Formal));
501               end if;
502
503            elsif Nkind (Default_Value (O_Formal)) = N_Aggregate
504              or else Nkind (Original_Node (Default_Value (O_Formal))) /=
505                                           Nkind (Default_Value (O_Formal))
506            then
507               Set_Expression (Param_Spec,
508                 New_Copy_Tree (Original_Node (Default_Value (O_Formal))));
509            end if;
510
511            Next_Formal (Formal);
512            Next_Formal (O_Formal);
513            Next (Param_Spec);
514         end loop;
515      end if;
516
517      --  If the renamed entity is a function, the generated body contains a
518      --  return statement. Otherwise, build a procedure call. If the entity is
519      --  an entry, subsequent analysis of the call will transform it into the
520      --  proper entry or protected operation call. If the renamed entity is
521      --  a character literal, return it directly.
522
523      if Ekind (Old_S) = E_Function
524        or else Ekind (Old_S) = E_Operator
525        or else (Ekind (Old_S) = E_Subprogram_Type
526                  and then Etype (Old_S) /= Standard_Void_Type)
527      then
528         Call_Node :=
529           Make_Simple_Return_Statement (Loc,
530              Expression =>
531                Make_Function_Call (Loc,
532                  Name                   => Call_Name,
533                  Parameter_Associations => Actuals));
534
535      elsif Ekind (Old_S) = E_Enumeration_Literal then
536         Call_Node :=
537           Make_Simple_Return_Statement (Loc,
538              Expression => New_Occurrence_Of (Old_S, Loc));
539
540      elsif Nkind (Nam) = N_Character_Literal then
541         Call_Node :=
542           Make_Simple_Return_Statement (Loc, Expression => Call_Name);
543
544      else
545         Call_Node :=
546           Make_Procedure_Call_Statement (Loc,
547             Name                   => Call_Name,
548             Parameter_Associations => Actuals);
549      end if;
550
551      --  Create entities for subprogram body and formals
552
553      Set_Defining_Unit_Name (Spec,
554        Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
555
556      Param_Spec := First (Parameter_Specifications (Spec));
557      while Present (Param_Spec) loop
558         Set_Defining_Identifier (Param_Spec,
559           Make_Defining_Identifier (Loc,
560             Chars => Chars (Defining_Identifier (Param_Spec))));
561         Next (Param_Spec);
562      end loop;
563
564      Body_Node :=
565        Make_Subprogram_Body (Loc,
566          Specification => Spec,
567          Declarations => New_List,
568          Handled_Statement_Sequence =>
569            Make_Handled_Sequence_Of_Statements (Loc,
570              Statements => New_List (Call_Node)));
571
572      if Nkind (Decl) /= N_Subprogram_Declaration then
573         Rewrite (N,
574           Make_Subprogram_Declaration (Loc,
575             Specification => Specification (N)));
576      end if;
577
578      --  Link the body to the entity whose declaration it completes. If
579      --  the body is analyzed when the renamed entity is frozen, it may
580      --  be necessary to restore the proper scope (see package Exp_Ch13).
581
582      if Nkind (N) = N_Subprogram_Renaming_Declaration
583        and then Present (Corresponding_Spec (N))
584      then
585         Set_Corresponding_Spec (Body_Node, Corresponding_Spec (N));
586      else
587         Set_Corresponding_Spec (Body_Node, New_S);
588      end if;
589
590      return Body_Node;
591   end Build_Renamed_Body;
592
593   --------------------------
594   -- Check_Address_Clause --
595   --------------------------
596
597   procedure Check_Address_Clause (E : Entity_Id) is
598      Addr       : constant Node_Id   := Address_Clause (E);
599      Typ        : constant Entity_Id := Etype (E);
600      Decl       : Node_Id;
601      Expr       : Node_Id;
602      Init       : Node_Id;
603      Lhs        : Node_Id;
604      Tag_Assign : Node_Id;
605
606   begin
607      if Present (Addr) then
608
609         --  For a deferred constant, the initialization value is on full view
610
611         if Ekind (E) = E_Constant and then Present (Full_View (E)) then
612            Decl := Declaration_Node (Full_View (E));
613         else
614            Decl := Declaration_Node (E);
615         end if;
616
617         Expr := Expression (Addr);
618
619         if Needs_Constant_Address (Decl, Typ) then
620            Check_Constant_Address_Clause (Expr, E);
621
622            --  Has_Delayed_Freeze was set on E when the address clause was
623            --  analyzed, and must remain set because we want the address
624            --  clause to be elaborated only after any entity it references
625            --  has been elaborated.
626         end if;
627
628         --  If Rep_Clauses are to be ignored, remove address clause from
629         --  list attached to entity, because it may be illegal for gigi,
630         --  for example by breaking order of elaboration..
631
632         if Ignore_Rep_Clauses then
633            declare
634               Rep : Node_Id;
635
636            begin
637               Rep := First_Rep_Item (E);
638
639               if Rep = Addr then
640                  Set_First_Rep_Item (E, Next_Rep_Item (Addr));
641
642               else
643                  while Present (Rep)
644                    and then Next_Rep_Item (Rep) /= Addr
645                  loop
646                     Rep := Next_Rep_Item (Rep);
647                  end loop;
648               end if;
649
650               if Present (Rep) then
651                  Set_Next_Rep_Item (Rep, Next_Rep_Item (Addr));
652               end if;
653            end;
654
655            --  And now remove the address clause
656
657            Kill_Rep_Clause (Addr);
658
659         elsif not Error_Posted (Expr)
660           and then not Needs_Finalization (Typ)
661         then
662            Warn_Overlay (Expr, Typ, Name (Addr));
663         end if;
664
665         Init := Expression (Decl);
666
667         --  If a variable, or a non-imported constant, overlays a constant
668         --  object and has an initialization value, then the initialization
669         --  may end up writing into read-only memory. Detect the cases of
670         --  statically identical values and remove the initialization. In
671         --  the other cases, give a warning. We will give other warnings
672         --  later for the variable if it is assigned.
673
674         if (Ekind (E) = E_Variable
675              or else (Ekind (E) = E_Constant
676                        and then not Is_Imported (E)))
677           and then Overlays_Constant (E)
678           and then Present (Init)
679         then
680            declare
681               O_Ent : Entity_Id;
682               Off   : Boolean;
683
684            begin
685               Find_Overlaid_Entity (Addr, O_Ent, Off);
686
687               if Ekind (O_Ent) = E_Constant
688                 and then Etype (O_Ent) = Typ
689                 and then Present (Constant_Value (O_Ent))
690                 and then Compile_Time_Compare
691                            (Init,
692                             Constant_Value (O_Ent),
693                             Assume_Valid => True) = EQ
694               then
695                  Set_No_Initialization (Decl);
696                  return;
697
698               elsif Comes_From_Source (Init)
699                 and then Address_Clause_Overlay_Warnings
700               then
701                  Error_Msg_Sloc := Sloc (Addr);
702                  Error_Msg_NE
703                    ("??constant& may be modified via address clause#",
704                     Decl, O_Ent);
705               end if;
706            end;
707         end if;
708
709         if Present (Init) then
710
711            --  Capture initialization value at point of declaration,
712            --  and make explicit assignment legal, because object may
713            --  be a constant.
714
715            Remove_Side_Effects (Init);
716            Lhs := New_Occurrence_Of (E, Sloc (Decl));
717            Set_Assignment_OK (Lhs);
718
719            --  Move initialization to freeze actions, once the object has
720            --  been frozen and the address clause alignment check has been
721            --  performed.
722
723            Append_Freeze_Action (E,
724              Make_Assignment_Statement (Sloc (Decl),
725                Name       => Lhs,
726                Expression => Expression (Decl)));
727
728            Set_No_Initialization (Decl);
729
730            --  If the objet is tagged, check whether the tag must be
731            --  reassigned explicitly.
732
733            Tag_Assign := Make_Tag_Assignment (Decl);
734            if Present (Tag_Assign) then
735               Append_Freeze_Action (E, Tag_Assign);
736            end if;
737         end if;
738      end if;
739   end Check_Address_Clause;
740
741   -----------------------------
742   -- Check_Compile_Time_Size --
743   -----------------------------
744
745   procedure Check_Compile_Time_Size (T : Entity_Id) is
746
747      procedure Set_Small_Size (T : Entity_Id; S : Uint);
748      --  Sets the compile time known size (32 bits or less) in the Esize
749      --  field, of T checking for a size clause that was given which attempts
750      --  to give a smaller size, and also checking for an alignment clause.
751
752      function Size_Known (T : Entity_Id) return Boolean;
753      --  Recursive function that does all the work
754
755      function Static_Discriminated_Components (T : Entity_Id) return Boolean;
756      --  If T is a constrained subtype, its size is not known if any of its
757      --  discriminant constraints is not static and it is not a null record.
758      --  The test is conservative and doesn't check that the components are
759      --  in fact constrained by non-static discriminant values. Could be made
760      --  more precise ???
761
762      --------------------
763      -- Set_Small_Size --
764      --------------------
765
766      procedure Set_Small_Size (T : Entity_Id; S : Uint) is
767      begin
768         if S > 32 then
769            return;
770
771         --  Check for bad size clause given
772
773         elsif Has_Size_Clause (T) then
774            if RM_Size (T) < S then
775               Error_Msg_Uint_1 := S;
776               Error_Msg_NE
777                 ("size for& too small, minimum allowed is ^",
778                  Size_Clause (T), T);
779            end if;
780
781         --  Set size if not set already
782
783         elsif Unknown_RM_Size (T) then
784            Set_RM_Size (T, S);
785         end if;
786      end Set_Small_Size;
787
788      ----------------
789      -- Size_Known --
790      ----------------
791
792      function Size_Known (T : Entity_Id) return Boolean is
793         Index : Entity_Id;
794         Comp  : Entity_Id;
795         Ctyp  : Entity_Id;
796         Low   : Node_Id;
797         High  : Node_Id;
798
799      begin
800         if Size_Known_At_Compile_Time (T) then
801            return True;
802
803         --  Always True for scalar types. This is true even for generic formal
804         --  scalar types. We used to return False in the latter case, but the
805         --  size is known at compile time, even in the template, we just do
806         --  not know the exact size but that's not the point of this routine.
807
808         elsif Is_Scalar_Type (T)
809           or else Is_Task_Type (T)
810         then
811            return True;
812
813         --  Array types
814
815         elsif Is_Array_Type (T) then
816
817            --  String literals always have known size, and we can set it
818
819            if Ekind (T) = E_String_Literal_Subtype then
820               Set_Small_Size (T, Component_Size (T)
821                               * String_Literal_Length (T));
822               return True;
823
824            --  Unconstrained types never have known at compile time size
825
826            elsif not Is_Constrained (T) then
827               return False;
828
829            --  Don't do any recursion on type with error posted, since we may
830            --  have a malformed type that leads us into a loop.
831
832            elsif Error_Posted (T) then
833               return False;
834
835            --  Otherwise if component size unknown, then array size unknown
836
837            elsif not Size_Known (Component_Type (T)) then
838               return False;
839            end if;
840
841            --  Check for all indexes static, and also compute possible size
842            --  (in case it is less than 32 and may be packable).
843
844            declare
845               Esiz : Uint := Component_Size (T);
846               Dim  : Uint;
847
848            begin
849               Index := First_Index (T);
850               while Present (Index) loop
851                  if Nkind (Index) = N_Range then
852                     Get_Index_Bounds (Index, Low, High);
853
854                  elsif Error_Posted (Scalar_Range (Etype (Index))) then
855                     return False;
856
857                  else
858                     Low  := Type_Low_Bound (Etype (Index));
859                     High := Type_High_Bound (Etype (Index));
860                  end if;
861
862                  if not Compile_Time_Known_Value (Low)
863                    or else not Compile_Time_Known_Value (High)
864                    or else Etype (Index) = Any_Type
865                  then
866                     return False;
867
868                  else
869                     Dim := Expr_Value (High) - Expr_Value (Low) + 1;
870
871                     if Dim >= 0 then
872                        Esiz := Esiz * Dim;
873                     else
874                        Esiz := Uint_0;
875                     end if;
876                  end if;
877
878                  Next_Index (Index);
879               end loop;
880
881               Set_Small_Size (T, Esiz);
882               return True;
883            end;
884
885         --  Access types always have known at compile time sizes
886
887         elsif Is_Access_Type (T) then
888            return True;
889
890         --  For non-generic private types, go to underlying type if present
891
892         elsif Is_Private_Type (T)
893           and then not Is_Generic_Type (T)
894           and then Present (Underlying_Type (T))
895         then
896            --  Don't do any recursion on type with error posted, since we may
897            --  have a malformed type that leads us into a loop.
898
899            if Error_Posted (T) then
900               return False;
901            else
902               return Size_Known (Underlying_Type (T));
903            end if;
904
905         --  Record types
906
907         elsif Is_Record_Type (T) then
908
909            --  A class-wide type is never considered to have a known size
910
911            if Is_Class_Wide_Type (T) then
912               return False;
913
914            --  A subtype of a variant record must not have non-static
915            --  discriminated components.
916
917            elsif T /= Base_Type (T)
918              and then not Static_Discriminated_Components (T)
919            then
920               return False;
921
922            --  Don't do any recursion on type with error posted, since we may
923            --  have a malformed type that leads us into a loop.
924
925            elsif Error_Posted (T) then
926               return False;
927            end if;
928
929            --  Now look at the components of the record
930
931            declare
932               --  The following two variables are used to keep track of the
933               --  size of packed records if we can tell the size of the packed
934               --  record in the front end. Packed_Size_Known is True if so far
935               --  we can figure out the size. It is initialized to True for a
936               --  packed record, unless the record has discriminants or atomic
937               --  components or independent components.
938
939               --  The reason we eliminate the discriminated case is that
940               --  we don't know the way the back end lays out discriminated
941               --  packed records. If Packed_Size_Known is True, then
942               --  Packed_Size is the size in bits so far.
943
944               Packed_Size_Known : Boolean :=
945                 Is_Packed (T)
946                   and then not Has_Discriminants (T)
947                   and then not Has_Atomic_Components (T)
948                   and then not Has_Independent_Components (T);
949
950               Packed_Size : Uint := Uint_0;
951               --  Size in bits so far
952
953            begin
954               --  Test for variant part present
955
956               if Has_Discriminants (T)
957                 and then Present (Parent (T))
958                 and then Nkind (Parent (T)) = N_Full_Type_Declaration
959                 and then Nkind (Type_Definition (Parent (T))) =
960                                               N_Record_Definition
961                 and then not Null_Present (Type_Definition (Parent (T)))
962                 and then
963                   Present (Variant_Part
964                              (Component_List (Type_Definition (Parent (T)))))
965               then
966                  --  If variant part is present, and type is unconstrained,
967                  --  then we must have defaulted discriminants, or a size
968                  --  clause must be present for the type, or else the size
969                  --  is definitely not known at compile time.
970
971                  if not Is_Constrained (T)
972                    and then
973                      No (Discriminant_Default_Value (First_Discriminant (T)))
974                    and then Unknown_RM_Size (T)
975                  then
976                     return False;
977                  end if;
978               end if;
979
980               --  Loop through components
981
982               Comp := First_Component_Or_Discriminant (T);
983               while Present (Comp) loop
984                  Ctyp := Etype (Comp);
985
986                  --  We do not know the packed size if there is a component
987                  --  clause present (we possibly could, but this would only
988                  --  help in the case of a record with partial rep clauses.
989                  --  That's because in the case of full rep clauses, the
990                  --  size gets figured out anyway by a different circuit).
991
992                  if Present (Component_Clause (Comp)) then
993                     Packed_Size_Known := False;
994                  end if;
995
996                  --  We do not know the packed size for an atomic/VFA type
997                  --  or component, or an independent type or component, or a
998                  --  by-reference type or aliased component (because packing
999                  --  does not touch these).
1000
1001                  if        Is_Atomic_Or_VFA (Ctyp)
1002                    or else Is_Atomic_Or_VFA (Comp)
1003                    or else Is_Independent (Ctyp)
1004                    or else Is_Independent (Comp)
1005                    or else Is_By_Reference_Type (Ctyp)
1006                    or else Is_Aliased (Comp)
1007                  then
1008                     Packed_Size_Known := False;
1009                  end if;
1010
1011                  --  We need to identify a component that is an array where
1012                  --  the index type is an enumeration type with non-standard
1013                  --  representation, and some bound of the type depends on a
1014                  --  discriminant.
1015
1016                  --  This is because gigi computes the size by doing a
1017                  --  substitution of the appropriate discriminant value in
1018                  --  the size expression for the base type, and gigi is not
1019                  --  clever enough to evaluate the resulting expression (which
1020                  --  involves a call to rep_to_pos) at compile time.
1021
1022                  --  It would be nice if gigi would either recognize that
1023                  --  this expression can be computed at compile time, or
1024                  --  alternatively figured out the size from the subtype
1025                  --  directly, where all the information is at hand ???
1026
1027                  if Is_Array_Type (Etype (Comp))
1028                    and then Present (Packed_Array_Impl_Type (Etype (Comp)))
1029                  then
1030                     declare
1031                        Ocomp  : constant Entity_Id :=
1032                                   Original_Record_Component (Comp);
1033                        OCtyp  : constant Entity_Id := Etype (Ocomp);
1034                        Ind    : Node_Id;
1035                        Indtyp : Entity_Id;
1036                        Lo, Hi : Node_Id;
1037
1038                     begin
1039                        Ind := First_Index (OCtyp);
1040                        while Present (Ind) loop
1041                           Indtyp := Etype (Ind);
1042
1043                           if Is_Enumeration_Type (Indtyp)
1044                             and then Has_Non_Standard_Rep (Indtyp)
1045                           then
1046                              Lo := Type_Low_Bound  (Indtyp);
1047                              Hi := Type_High_Bound (Indtyp);
1048
1049                              if Is_Entity_Name (Lo)
1050                                and then Ekind (Entity (Lo)) = E_Discriminant
1051                              then
1052                                 return False;
1053
1054                              elsif Is_Entity_Name (Hi)
1055                                and then Ekind (Entity (Hi)) = E_Discriminant
1056                              then
1057                                 return False;
1058                              end if;
1059                           end if;
1060
1061                           Next_Index (Ind);
1062                        end loop;
1063                     end;
1064                  end if;
1065
1066                  --  Clearly size of record is not known if the size of one of
1067                  --  the components is not known.
1068
1069                  if not Size_Known (Ctyp) then
1070                     return False;
1071                  end if;
1072
1073                  --  Accumulate packed size if possible
1074
1075                  if Packed_Size_Known then
1076
1077                     --  We can only deal with elementary types, since for
1078                     --  non-elementary components, alignment enters into the
1079                     --  picture, and we don't know enough to handle proper
1080                     --  alignment in this context. Packed arrays count as
1081                     --  elementary if the representation is a modular type.
1082
1083                     if Is_Elementary_Type (Ctyp)
1084                       or else (Is_Array_Type (Ctyp)
1085                                 and then Present
1086                                            (Packed_Array_Impl_Type (Ctyp))
1087                                 and then Is_Modular_Integer_Type
1088                                            (Packed_Array_Impl_Type (Ctyp)))
1089                     then
1090                        --  Packed size unknown if we have an atomic/VFA type
1091                        --  or a by-reference type, since the back end knows
1092                        --  how these are layed out.
1093
1094                        if Is_Atomic_Or_VFA (Ctyp)
1095                          or else Is_By_Reference_Type (Ctyp)
1096                        then
1097                           Packed_Size_Known := False;
1098
1099                        --  If RM_Size is known and static, then we can keep
1100                        --  accumulating the packed size
1101
1102                        elsif Known_Static_RM_Size (Ctyp) then
1103
1104                           --  A little glitch, to be removed sometime ???
1105                           --  gigi does not understand zero sizes yet.
1106
1107                           if RM_Size (Ctyp) = Uint_0 then
1108                              Packed_Size_Known := False;
1109
1110                           --  Normal case where we can keep accumulating the
1111                           --  packed array size.
1112
1113                           else
1114                              Packed_Size := Packed_Size + RM_Size (Ctyp);
1115                           end if;
1116
1117                        --  If we have a field whose RM_Size is not known then
1118                        --  we can't figure out the packed size here.
1119
1120                        else
1121                           Packed_Size_Known := False;
1122                        end if;
1123
1124                     --  If we have a non-elementary type we can't figure out
1125                     --  the packed array size (alignment issues).
1126
1127                     else
1128                        Packed_Size_Known := False;
1129                     end if;
1130                  end if;
1131
1132                  Next_Component_Or_Discriminant (Comp);
1133               end loop;
1134
1135               if Packed_Size_Known then
1136                  Set_Small_Size (T, Packed_Size);
1137               end if;
1138
1139               return True;
1140            end;
1141
1142         --  All other cases, size not known at compile time
1143
1144         else
1145            return False;
1146         end if;
1147      end Size_Known;
1148
1149      -------------------------------------
1150      -- Static_Discriminated_Components --
1151      -------------------------------------
1152
1153      function Static_Discriminated_Components
1154        (T : Entity_Id) return Boolean
1155      is
1156         Constraint : Elmt_Id;
1157
1158      begin
1159         if Has_Discriminants (T)
1160           and then Present (Discriminant_Constraint (T))
1161           and then Present (First_Component (T))
1162         then
1163            Constraint := First_Elmt (Discriminant_Constraint (T));
1164            while Present (Constraint) loop
1165               if not Compile_Time_Known_Value (Node (Constraint)) then
1166                  return False;
1167               end if;
1168
1169               Next_Elmt (Constraint);
1170            end loop;
1171         end if;
1172
1173         return True;
1174      end Static_Discriminated_Components;
1175
1176   --  Start of processing for Check_Compile_Time_Size
1177
1178   begin
1179      Set_Size_Known_At_Compile_Time (T, Size_Known (T));
1180   end Check_Compile_Time_Size;
1181
1182   -----------------------------------
1183   -- Check_Component_Storage_Order --
1184   -----------------------------------
1185
1186   procedure Check_Component_Storage_Order
1187     (Encl_Type        : Entity_Id;
1188      Comp             : Entity_Id;
1189      ADC              : Node_Id;
1190      Comp_ADC_Present : out Boolean)
1191   is
1192      Comp_Type : Entity_Id;
1193      Comp_ADC  : Node_Id;
1194      Err_Node  : Node_Id;
1195
1196      Comp_Byte_Aligned : Boolean;
1197      --  Set for the record case, True if Comp starts on a byte boundary
1198      --  (in which case it is allowed to have different storage order).
1199
1200      Comp_SSO_Differs  : Boolean;
1201      --  Set True when the component is a nested composite, and it does not
1202      --  have the same scalar storage order as Encl_Type.
1203
1204      Component_Aliased : Boolean;
1205
1206   begin
1207      --  Record case
1208
1209      if Present (Comp) then
1210         Err_Node  := Comp;
1211         Comp_Type := Etype (Comp);
1212
1213         if Is_Tag (Comp) then
1214            Comp_Byte_Aligned := True;
1215            Component_Aliased := False;
1216
1217         else
1218            --  If a component clause is present, check if the component starts
1219            --  on a storage element boundary. Otherwise conservatively assume
1220            --  it does so only in the case where the record is not packed.
1221
1222            if Present (Component_Clause (Comp)) then
1223               Comp_Byte_Aligned :=
1224                 Normalized_First_Bit (Comp) mod System_Storage_Unit = 0;
1225            else
1226               Comp_Byte_Aligned := not Is_Packed (Encl_Type);
1227            end if;
1228
1229            Component_Aliased := Is_Aliased (Comp);
1230         end if;
1231
1232      --  Array case
1233
1234      else
1235         Err_Node  := Encl_Type;
1236         Comp_Type := Component_Type (Encl_Type);
1237
1238         Component_Aliased := Has_Aliased_Components (Encl_Type);
1239      end if;
1240
1241      --  Note: the Reverse_Storage_Order flag is set on the base type, but
1242      --  the attribute definition clause is attached to the first subtype.
1243
1244      Comp_Type := Base_Type (Comp_Type);
1245      Comp_ADC := Get_Attribute_Definition_Clause
1246                    (First_Subtype (Comp_Type),
1247                     Attribute_Scalar_Storage_Order);
1248      Comp_ADC_Present := Present (Comp_ADC);
1249
1250      --  Case of record or array component: check storage order compatibility.
1251      --  But, if the record has Complex_Representation, then it is treated as
1252      --  a scalar in the back end so the storage order is irrelevant.
1253
1254      if (Is_Record_Type (Comp_Type)
1255            and then not Has_Complex_Representation (Comp_Type))
1256        or else Is_Array_Type (Comp_Type)
1257      then
1258         Comp_SSO_Differs :=
1259           Reverse_Storage_Order (Encl_Type)
1260             /=
1261           Reverse_Storage_Order (Comp_Type);
1262
1263         --  Parent and extension must have same storage order
1264
1265         if Present (Comp) and then Chars (Comp) = Name_uParent then
1266            if Comp_SSO_Differs then
1267               Error_Msg_N
1268                 ("record extension must have same scalar storage order as "
1269                  & "parent", Err_Node);
1270            end if;
1271
1272         --  If enclosing composite has explicit SSO then nested composite must
1273         --  have explicit SSO as well.
1274
1275         elsif Present (ADC) and then No (Comp_ADC) then
1276            Error_Msg_N ("nested composite must have explicit scalar "
1277                         & "storage order", Err_Node);
1278
1279         --  If component and composite SSO differs, check that component
1280         --  falls on byte boundaries and isn't packed.
1281
1282         elsif Comp_SSO_Differs then
1283
1284            --  Component SSO differs from enclosing composite:
1285
1286            --  Reject if component is a packed array, as it may be represented
1287            --  as a scalar internally.
1288
1289            if Is_Packed_Array (Comp_Type) then
1290               Error_Msg_N
1291                 ("type of packed component must have same scalar "
1292                  & "storage order as enclosing composite", Err_Node);
1293
1294            --  Reject if composite is a packed array, as it may be rewritten
1295            --  into an array of scalars.
1296
1297            elsif Is_Packed_Array (Encl_Type) then
1298               Error_Msg_N ("type of packed array must have same scalar "
1299                  & "storage order as component", Err_Node);
1300
1301            --  Reject if not byte aligned
1302
1303            elsif Is_Record_Type (Encl_Type)
1304                    and then not Comp_Byte_Aligned
1305            then
1306               Error_Msg_N
1307                 ("type of non-byte-aligned component must have same scalar "
1308                  & "storage order as enclosing composite", Err_Node);
1309            end if;
1310         end if;
1311
1312      --  Enclosing type has explicit SSO: non-composite component must not
1313      --  be aliased.
1314
1315      elsif Present (ADC) and then Component_Aliased then
1316         Error_Msg_N
1317           ("aliased component not permitted for type with "
1318            & "explicit Scalar_Storage_Order", Err_Node);
1319      end if;
1320   end Check_Component_Storage_Order;
1321
1322   -----------------------------
1323   -- Check_Debug_Info_Needed --
1324   -----------------------------
1325
1326   procedure Check_Debug_Info_Needed (T : Entity_Id) is
1327   begin
1328      if Debug_Info_Off (T) then
1329         return;
1330
1331      elsif Comes_From_Source (T)
1332        or else Debug_Generated_Code
1333        or else Debug_Flag_VV
1334        or else Needs_Debug_Info (T)
1335      then
1336         Set_Debug_Info_Needed (T);
1337      end if;
1338   end Check_Debug_Info_Needed;
1339
1340   -------------------------------
1341   -- Check_Expression_Function --
1342   -------------------------------
1343
1344   procedure Check_Expression_Function (N : Node_Id; Nam : Entity_Id) is
1345      Decl : Node_Id;
1346
1347      function Find_Constant (Nod : Node_Id) return Traverse_Result;
1348      --  Function to search for deferred constant
1349
1350      -------------------
1351      -- Find_Constant --
1352      -------------------
1353
1354      function Find_Constant (Nod : Node_Id) return Traverse_Result is
1355      begin
1356         --  When a constant is initialized with the result of a dispatching
1357         --  call, the constant declaration is rewritten as a renaming of the
1358         --  displaced function result. This scenario is not a premature use of
1359         --  a constant even though the Has_Completion flag is not set.
1360
1361         if Is_Entity_Name (Nod)
1362           and then Present (Entity (Nod))
1363           and then Ekind (Entity (Nod)) = E_Constant
1364           and then Scope (Entity (Nod)) = Current_Scope
1365           and then Nkind (Declaration_Node (Entity (Nod))) =
1366                                                         N_Object_Declaration
1367           and then not Is_Imported (Entity (Nod))
1368           and then not Has_Completion (Entity (Nod))
1369         then
1370            Error_Msg_NE
1371              ("premature use of& in call or instance", N, Entity (Nod));
1372
1373         elsif Nkind (Nod) = N_Attribute_Reference then
1374            Analyze (Prefix (Nod));
1375
1376            if Is_Entity_Name (Prefix (Nod))
1377              and then Is_Type (Entity (Prefix (Nod)))
1378            then
1379               Freeze_Before (N, Entity (Prefix (Nod)));
1380            end if;
1381         end if;
1382
1383         return OK;
1384      end Find_Constant;
1385
1386      procedure Check_Deferred is new Traverse_Proc (Find_Constant);
1387
1388   --  Start of processing for Check_Expression_Function
1389
1390   begin
1391      Decl := Original_Node (Unit_Declaration_Node (Nam));
1392
1393      if Scope (Nam) = Current_Scope
1394        and then Nkind (Decl) = N_Expression_Function
1395      then
1396         Check_Deferred (Expression (Decl));
1397      end if;
1398   end Check_Expression_Function;
1399
1400   ----------------------------
1401   -- Check_Strict_Alignment --
1402   ----------------------------
1403
1404   procedure Check_Strict_Alignment (E : Entity_Id) is
1405      Comp  : Entity_Id;
1406
1407   begin
1408      if Is_Tagged_Type (E) or else Is_Concurrent_Type (E) then
1409         Set_Strict_Alignment (E);
1410
1411      elsif Is_Array_Type (E) then
1412         Set_Strict_Alignment (E, Strict_Alignment (Component_Type (E)));
1413
1414      elsif Is_Record_Type (E) then
1415         if Is_Limited_Record (E) then
1416            Set_Strict_Alignment (E);
1417            return;
1418         end if;
1419
1420         Comp := First_Component (E);
1421         while Present (Comp) loop
1422            if not Is_Type (Comp)
1423              and then (Strict_Alignment (Etype (Comp))
1424                         or else Is_Aliased (Comp))
1425            then
1426               Set_Strict_Alignment (E);
1427               return;
1428            end if;
1429
1430            Next_Component (Comp);
1431         end loop;
1432      end if;
1433   end Check_Strict_Alignment;
1434
1435   -------------------------
1436   -- Check_Unsigned_Type --
1437   -------------------------
1438
1439   procedure Check_Unsigned_Type (E : Entity_Id) is
1440      Ancestor : Entity_Id;
1441      Lo_Bound : Node_Id;
1442      Btyp     : Entity_Id;
1443
1444   begin
1445      if not Is_Discrete_Or_Fixed_Point_Type (E) then
1446         return;
1447      end if;
1448
1449      --  Do not attempt to analyze case where range was in error
1450
1451      if No (Scalar_Range (E)) or else Error_Posted (Scalar_Range (E)) then
1452         return;
1453      end if;
1454
1455      --  The situation that is nontrivial is something like:
1456
1457      --     subtype x1 is integer range -10 .. +10;
1458      --     subtype x2 is x1 range 0 .. V1;
1459      --     subtype x3 is x2 range V2 .. V3;
1460      --     subtype x4 is x3 range V4 .. V5;
1461
1462      --  where Vn are variables. Here the base type is signed, but we still
1463      --  know that x4 is unsigned because of the lower bound of x2.
1464
1465      --  The only way to deal with this is to look up the ancestor chain
1466
1467      Ancestor := E;
1468      loop
1469         if Ancestor = Any_Type or else Etype (Ancestor) = Any_Type then
1470            return;
1471         end if;
1472
1473         Lo_Bound := Type_Low_Bound (Ancestor);
1474
1475         if Compile_Time_Known_Value (Lo_Bound) then
1476            if Expr_Rep_Value (Lo_Bound) >= 0 then
1477               Set_Is_Unsigned_Type (E, True);
1478            end if;
1479
1480            return;
1481
1482         else
1483            Ancestor := Ancestor_Subtype (Ancestor);
1484
1485            --  If no ancestor had a static lower bound, go to base type
1486
1487            if No (Ancestor) then
1488
1489               --  Note: the reason we still check for a compile time known
1490               --  value for the base type is that at least in the case of
1491               --  generic formals, we can have bounds that fail this test,
1492               --  and there may be other cases in error situations.
1493
1494               Btyp := Base_Type (E);
1495
1496               if Btyp = Any_Type or else Etype (Btyp) = Any_Type then
1497                  return;
1498               end if;
1499
1500               Lo_Bound := Type_Low_Bound (Base_Type (E));
1501
1502               if Compile_Time_Known_Value (Lo_Bound)
1503                 and then Expr_Rep_Value (Lo_Bound) >= 0
1504               then
1505                  Set_Is_Unsigned_Type (E, True);
1506               end if;
1507
1508               return;
1509            end if;
1510         end if;
1511      end loop;
1512   end Check_Unsigned_Type;
1513
1514   -----------------------------
1515   -- Is_Atomic_VFA_Aggregate --
1516   -----------------------------
1517
1518   function Is_Atomic_VFA_Aggregate (N : Node_Id) return Boolean is
1519      Loc   : constant Source_Ptr := Sloc (N);
1520      New_N : Node_Id;
1521      Par   : Node_Id;
1522      Temp  : Entity_Id;
1523      Typ   : Entity_Id;
1524
1525   begin
1526      Par := Parent (N);
1527
1528      --  Array may be qualified, so find outer context
1529
1530      if Nkind (Par) = N_Qualified_Expression then
1531         Par := Parent (Par);
1532      end if;
1533
1534      if not Comes_From_Source (Par) then
1535         return False;
1536      end if;
1537
1538      case Nkind (Par) is
1539         when N_Assignment_Statement =>
1540            Typ := Etype (Name (Par));
1541
1542            if not Is_Atomic_Or_VFA (Typ)
1543              and then not (Is_Entity_Name (Name (Par))
1544                             and then Is_Atomic_Or_VFA (Entity (Name (Par))))
1545            then
1546               return False;
1547            end if;
1548
1549         when N_Object_Declaration =>
1550            Typ := Etype (Defining_Identifier (Par));
1551
1552            if not Is_Atomic_Or_VFA (Typ)
1553              and then not Is_Atomic_Or_VFA (Defining_Identifier (Par))
1554            then
1555               return False;
1556            end if;
1557
1558         when others =>
1559            return False;
1560      end case;
1561
1562      Temp := Make_Temporary (Loc, 'T', N);
1563      New_N :=
1564        Make_Object_Declaration (Loc,
1565          Defining_Identifier => Temp,
1566          Object_Definition   => New_Occurrence_Of (Typ, Loc),
1567          Expression          => Relocate_Node (N));
1568      Insert_Before (Par, New_N);
1569      Analyze (New_N);
1570
1571      Set_Expression (Par, New_Occurrence_Of (Temp, Loc));
1572      return True;
1573   end Is_Atomic_VFA_Aggregate;
1574
1575   -----------------------------------------------
1576   -- Explode_Initialization_Compound_Statement --
1577   -----------------------------------------------
1578
1579   procedure Explode_Initialization_Compound_Statement (E : Entity_Id) is
1580      Init_Stmts : constant Node_Id := Initialization_Statements (E);
1581
1582   begin
1583      if Present (Init_Stmts)
1584        and then Nkind (Init_Stmts) = N_Compound_Statement
1585      then
1586         Insert_List_Before (Init_Stmts, Actions (Init_Stmts));
1587
1588         --  Note that we rewrite Init_Stmts into a NULL statement, rather than
1589         --  just removing it, because Freeze_All may rely on this particular
1590         --  Node_Id still being present in the enclosing list to know where to
1591         --  stop freezing.
1592
1593         Rewrite (Init_Stmts, Make_Null_Statement (Sloc (Init_Stmts)));
1594
1595         Set_Initialization_Statements (E, Empty);
1596      end if;
1597   end Explode_Initialization_Compound_Statement;
1598
1599   ----------------
1600   -- Freeze_All --
1601   ----------------
1602
1603   --  Note: the easy coding for this procedure would be to just build a
1604   --  single list of freeze nodes and then insert them and analyze them
1605   --  all at once. This won't work, because the analysis of earlier freeze
1606   --  nodes may recursively freeze types which would otherwise appear later
1607   --  on in the freeze list. So we must analyze and expand the freeze nodes
1608   --  as they are generated.
1609
1610   procedure Freeze_All (From : Entity_Id; After : in out Node_Id) is
1611      E     : Entity_Id;
1612      Decl  : Node_Id;
1613
1614      procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id);
1615      --  This is the internal recursive routine that does freezing of entities
1616      --  (but NOT the analysis of default expressions, which should not be
1617      --  recursive, we don't want to analyze those till we are sure that ALL
1618      --  the types are frozen).
1619
1620      --------------------
1621      -- Freeze_All_Ent --
1622      --------------------
1623
1624      procedure Freeze_All_Ent (From : Entity_Id; After : in out Node_Id) is
1625         E     : Entity_Id;
1626         Flist : List_Id;
1627         Lastn : Node_Id;
1628
1629         procedure Process_Flist;
1630         --  If freeze nodes are present, insert and analyze, and reset cursor
1631         --  for next insertion.
1632
1633         -------------------
1634         -- Process_Flist --
1635         -------------------
1636
1637         procedure Process_Flist is
1638         begin
1639            if Is_Non_Empty_List (Flist) then
1640               Lastn := Next (After);
1641               Insert_List_After_And_Analyze (After, Flist);
1642
1643               if Present (Lastn) then
1644                  After := Prev (Lastn);
1645               else
1646                  After := Last (List_Containing (After));
1647               end if;
1648            end if;
1649         end Process_Flist;
1650
1651      --  Start of processing for Freeze_All_Ent
1652
1653      begin
1654         E := From;
1655         while Present (E) loop
1656
1657            --  If the entity is an inner package which is not a package
1658            --  renaming, then its entities must be frozen at this point. Note
1659            --  that such entities do NOT get frozen at the end of the nested
1660            --  package itself (only library packages freeze).
1661
1662            --  Same is true for task declarations, where anonymous records
1663            --  created for entry parameters must be frozen.
1664
1665            if Ekind (E) = E_Package
1666              and then No (Renamed_Object (E))
1667              and then not Is_Child_Unit (E)
1668              and then not Is_Frozen (E)
1669            then
1670               Push_Scope (E);
1671               Install_Visible_Declarations (E);
1672               Install_Private_Declarations (E);
1673
1674               Freeze_All (First_Entity (E), After);
1675
1676               End_Package_Scope (E);
1677
1678               if Is_Generic_Instance (E)
1679                 and then Has_Delayed_Freeze (E)
1680               then
1681                  Set_Has_Delayed_Freeze (E, False);
1682                  Expand_N_Package_Declaration (Unit_Declaration_Node (E));
1683               end if;
1684
1685            elsif Ekind (E) in Task_Kind
1686              and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1687                                             N_Single_Task_Declaration)
1688            then
1689               Push_Scope (E);
1690               Freeze_All (First_Entity (E), After);
1691               End_Scope;
1692
1693            --  For a derived tagged type, we must ensure that all the
1694            --  primitive operations of the parent have been frozen, so that
1695            --  their addresses will be in the parent's dispatch table at the
1696            --  point it is inherited.
1697
1698            elsif Ekind (E) = E_Record_Type
1699              and then Is_Tagged_Type (E)
1700              and then Is_Tagged_Type (Etype (E))
1701              and then Is_Derived_Type (E)
1702            then
1703               declare
1704                  Prim_List : constant Elist_Id :=
1705                               Primitive_Operations (Etype (E));
1706
1707                  Prim : Elmt_Id;
1708                  Subp : Entity_Id;
1709
1710               begin
1711                  Prim := First_Elmt (Prim_List);
1712                  while Present (Prim) loop
1713                     Subp := Node (Prim);
1714
1715                     if Comes_From_Source (Subp)
1716                       and then not Is_Frozen (Subp)
1717                     then
1718                        Flist := Freeze_Entity (Subp, After);
1719                        Process_Flist;
1720                     end if;
1721
1722                     Next_Elmt (Prim);
1723                  end loop;
1724               end;
1725            end if;
1726
1727            if not Is_Frozen (E) then
1728               Flist := Freeze_Entity (E, After);
1729               Process_Flist;
1730
1731            --  If already frozen, and there are delayed aspects, this is where
1732            --  we do the visibility check for these aspects (see Sem_Ch13 spec
1733            --  for a description of how we handle aspect visibility).
1734
1735            elsif Has_Delayed_Aspects (E) then
1736
1737               --  Retrieve the visibility to the discriminants in order to
1738               --  analyze properly the aspects.
1739
1740               Push_Scope_And_Install_Discriminants (E);
1741
1742               declare
1743                  Ritem : Node_Id;
1744
1745               begin
1746                  Ritem := First_Rep_Item (E);
1747                  while Present (Ritem) loop
1748                     if Nkind (Ritem) = N_Aspect_Specification
1749                       and then Entity (Ritem) = E
1750                       and then Is_Delayed_Aspect (Ritem)
1751                     then
1752                        Check_Aspect_At_End_Of_Declarations (Ritem);
1753                     end if;
1754
1755                     Ritem := Next_Rep_Item (Ritem);
1756                  end loop;
1757               end;
1758
1759               Uninstall_Discriminants_And_Pop_Scope (E);
1760            end if;
1761
1762            --  If an incomplete type is still not frozen, this may be a
1763            --  premature freezing because of a body declaration that follows.
1764            --  Indicate where the freezing took place. Freezing will happen
1765            --  if the body comes from source, but not if it is internally
1766            --  generated, for example as the body of a type invariant.
1767
1768            --  If the freezing is caused by the end of the current declarative
1769            --  part, it is a Taft Amendment type, and there is no error.
1770
1771            if not Is_Frozen (E)
1772              and then Ekind (E) = E_Incomplete_Type
1773            then
1774               declare
1775                  Bod : constant Node_Id := Next (After);
1776
1777               begin
1778                  --  The presence of a body freezes all entities previously
1779                  --  declared in the current list of declarations, but this
1780                  --  does not apply if the body does not come from source.
1781                  --  A type invariant is transformed into a subprogram body
1782                  --  which is placed at the end of the private part of the
1783                  --  current package, but this body does not freeze incomplete
1784                  --  types that may be declared in this private part.
1785
1786                  if (Nkind_In (Bod, N_Subprogram_Body,
1787                                     N_Entry_Body,
1788                                     N_Package_Body,
1789                                     N_Protected_Body,
1790                                     N_Task_Body)
1791                        or else Nkind (Bod) in N_Body_Stub)
1792                    and then
1793                      List_Containing (After) = List_Containing (Parent (E))
1794                    and then Comes_From_Source (Bod)
1795                  then
1796                     Error_Msg_Sloc := Sloc (Next (After));
1797                     Error_Msg_NE
1798                       ("type& is frozen# before its full declaration",
1799                         Parent (E), E);
1800                  end if;
1801               end;
1802            end if;
1803
1804            Next_Entity (E);
1805         end loop;
1806      end Freeze_All_Ent;
1807
1808   --  Start of processing for Freeze_All
1809
1810   begin
1811      Freeze_All_Ent (From, After);
1812
1813      --  Now that all types are frozen, we can deal with default expressions
1814      --  that require us to build a default expression functions. This is the
1815      --  point at which such functions are constructed (after all types that
1816      --  might be used in such expressions have been frozen).
1817
1818      --  For subprograms that are renaming_as_body, we create the wrapper
1819      --  bodies as needed.
1820
1821      --  We also add finalization chains to access types whose designated
1822      --  types are controlled. This is normally done when freezing the type,
1823      --  but this misses recursive type definitions where the later members
1824      --  of the recursion introduce controlled components.
1825
1826      --  Loop through entities
1827
1828      E := From;
1829      while Present (E) loop
1830         if Is_Subprogram (E) then
1831            if not Default_Expressions_Processed (E) then
1832               Process_Default_Expressions (E, After);
1833            end if;
1834
1835            if not Has_Completion (E) then
1836               Decl := Unit_Declaration_Node (E);
1837
1838               if Nkind (Decl) = N_Subprogram_Renaming_Declaration then
1839                  if Error_Posted (Decl) then
1840                     Set_Has_Completion (E);
1841                  else
1842                     Build_And_Analyze_Renamed_Body (Decl, E, After);
1843                  end if;
1844
1845               elsif Nkind (Decl) = N_Subprogram_Declaration
1846                 and then Present (Corresponding_Body (Decl))
1847                 and then
1848                   Nkind (Unit_Declaration_Node (Corresponding_Body (Decl)))
1849                                          = N_Subprogram_Renaming_Declaration
1850               then
1851                  Build_And_Analyze_Renamed_Body
1852                    (Decl, Corresponding_Body (Decl), After);
1853               end if;
1854            end if;
1855
1856         elsif Ekind (E) in Task_Kind
1857           and then Nkind_In (Parent (E), N_Task_Type_Declaration,
1858                                          N_Single_Task_Declaration)
1859         then
1860            declare
1861               Ent : Entity_Id;
1862
1863            begin
1864               Ent := First_Entity (E);
1865               while Present (Ent) loop
1866                  if Is_Entry (Ent)
1867                    and then not Default_Expressions_Processed (Ent)
1868                  then
1869                     Process_Default_Expressions (Ent, After);
1870                  end if;
1871
1872                  Next_Entity (Ent);
1873               end loop;
1874            end;
1875         end if;
1876
1877         --  Historical note: We used to create a finalization master for an
1878         --  access type whose designated type is not controlled, but contains
1879         --  private controlled compoments. This form of postprocessing is no
1880         --  longer needed because the finalization master is now created when
1881         --  the access type is frozen (see Exp_Ch3.Freeze_Type).
1882
1883         Next_Entity (E);
1884      end loop;
1885   end Freeze_All;
1886
1887   -----------------------
1888   -- Freeze_And_Append --
1889   -----------------------
1890
1891   procedure Freeze_And_Append
1892     (Ent    : Entity_Id;
1893      N      : Node_Id;
1894      Result : in out List_Id)
1895   is
1896      L : constant List_Id := Freeze_Entity (Ent, N);
1897   begin
1898      if Is_Non_Empty_List (L) then
1899         if Result = No_List then
1900            Result := L;
1901         else
1902            Append_List (L, Result);
1903         end if;
1904      end if;
1905   end Freeze_And_Append;
1906
1907   -------------------
1908   -- Freeze_Before --
1909   -------------------
1910
1911   procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
1912      Freeze_Nodes : constant List_Id := Freeze_Entity (T, N);
1913
1914   begin
1915      if Ekind (T) = E_Function then
1916         Check_Expression_Function (N, T);
1917      end if;
1918
1919      if Is_Non_Empty_List (Freeze_Nodes) then
1920         Insert_Actions (N, Freeze_Nodes);
1921      end if;
1922   end Freeze_Before;
1923
1924   -------------------
1925   -- Freeze_Entity --
1926   -------------------
1927
1928   function Freeze_Entity (E : Entity_Id; N : Node_Id) return List_Id is
1929      Loc    : constant Source_Ptr := Sloc (N);
1930      Atype  : Entity_Id;
1931      Comp   : Entity_Id;
1932      F_Node : Node_Id;
1933      Formal : Entity_Id;
1934      Indx   : Node_Id;
1935
1936      Has_Default_Initialization : Boolean := False;
1937      --  This flag gets set to true for a variable with default initialization
1938
1939      Late_Freezing : Boolean := False;
1940      --  Used to detect attempt to freeze function declared in another unit
1941
1942      Result : List_Id := No_List;
1943      --  List of freezing actions, left at No_List if none
1944
1945      Test_E : Entity_Id := E;
1946      --  This could use a comment ???
1947
1948      procedure Add_To_Result (N : Node_Id);
1949      --  N is a freezing action to be appended to the Result
1950
1951      function After_Last_Declaration return Boolean;
1952      --  If Loc is a freeze_entity that appears after the last declaration
1953      --  in the scope, inhibit error messages on late completion.
1954
1955      procedure Check_Current_Instance (Comp_Decl : Node_Id);
1956      --  Check that an Access or Unchecked_Access attribute with a prefix
1957      --  which is the current instance type can only be applied when the type
1958      --  is limited.
1959
1960      procedure Check_Suspicious_Modulus (Utype : Entity_Id);
1961      --  Give warning for modulus of 8, 16, 32, or 64 given as an explicit
1962      --  integer literal without an explicit corresponding size clause. The
1963      --  caller has checked that Utype is a modular integer type.
1964
1965      procedure Freeze_Array_Type (Arr : Entity_Id);
1966      --  Freeze array type, including freezing index and component types
1967
1968      procedure Freeze_Object_Declaration (E : Entity_Id);
1969      --  Perform checks and generate freeze node if needed for a constant or
1970      --  variable declared by an object declaration.
1971
1972      function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id;
1973      --  Create Freeze_Generic_Entity nodes for types declared in a generic
1974      --  package. Recurse on inner generic packages.
1975
1976      function Freeze_Profile (E : Entity_Id) return Boolean;
1977      --  Freeze formals and return type of subprogram. If some type in the
1978      --  profile is a limited view, freezing of the entity will take place
1979      --  elsewhere, and the function returns False. This routine will be
1980      --  modified if and when we can implement AI05-019 efficiently ???
1981
1982      procedure Freeze_Record_Type (Rec : Entity_Id);
1983      --  Freeze record type, including freezing component types, and freezing
1984      --  primitive operations if this is a tagged type.
1985
1986      function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean;
1987      --  Determine whether an arbitrary entity is subject to Boolean aspect
1988      --  Import and its value is specified as True.
1989
1990      procedure Late_Freeze_Subprogram (E : Entity_Id);
1991      --  Following AI05-151, a function can return a limited view of a type
1992      --  declared elsewhere. In that case the function cannot be frozen at
1993      --  the end of its enclosing package. If its first use is in a different
1994      --  unit, it cannot be frozen there, but if the call is legal the full
1995      --  view of the return type is available and the subprogram can now be
1996      --  frozen. However the freeze node cannot be inserted at the point of
1997      --  call, but rather must go in the package holding the function, so that
1998      --  the backend can process it in the proper context.
1999
2000      procedure Wrap_Imported_Subprogram (E : Entity_Id);
2001      --  If E is an entity for an imported subprogram with pre/post-conditions
2002      --  then this procedure will create a wrapper to ensure that proper run-
2003      --  time checking of the pre/postconditions. See body for details.
2004
2005      -------------------
2006      -- Add_To_Result --
2007      -------------------
2008
2009      procedure Add_To_Result (N : Node_Id) is
2010      begin
2011         if No (Result) then
2012            Result := New_List (N);
2013         else
2014            Append (N, Result);
2015         end if;
2016      end Add_To_Result;
2017
2018      ----------------------------
2019      -- After_Last_Declaration --
2020      ----------------------------
2021
2022      function After_Last_Declaration return Boolean is
2023         Spec : constant Node_Id := Parent (Current_Scope);
2024
2025      begin
2026         if Nkind (Spec) = N_Package_Specification then
2027            if Present (Private_Declarations (Spec)) then
2028               return Loc >= Sloc (Last (Private_Declarations (Spec)));
2029            elsif Present (Visible_Declarations (Spec)) then
2030               return Loc >= Sloc (Last (Visible_Declarations (Spec)));
2031            else
2032               return False;
2033            end if;
2034
2035         else
2036            return False;
2037         end if;
2038      end After_Last_Declaration;
2039
2040      ----------------------------
2041      -- Check_Current_Instance --
2042      ----------------------------
2043
2044      procedure Check_Current_Instance (Comp_Decl : Node_Id) is
2045
2046         function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean;
2047         --  Determine whether Typ is compatible with the rules for aliased
2048         --  views of types as defined in RM 3.10 in the various dialects.
2049
2050         function Process (N : Node_Id) return Traverse_Result;
2051         --  Process routine to apply check to given node
2052
2053         -----------------------------
2054         -- Is_Aliased_View_Of_Type --
2055         -----------------------------
2056
2057         function Is_Aliased_View_Of_Type (Typ : Entity_Id) return Boolean is
2058            Typ_Decl : constant Node_Id := Parent (Typ);
2059
2060         begin
2061            --  Common case
2062
2063            if Nkind (Typ_Decl) = N_Full_Type_Declaration
2064              and then Limited_Present (Type_Definition (Typ_Decl))
2065            then
2066               return True;
2067
2068            --  The following paragraphs describe what a legal aliased view of
2069            --  a type is in the various dialects of Ada.
2070
2071            --  Ada 95
2072
2073            --  The current instance of a limited type, and a formal parameter
2074            --  or generic formal object of a tagged type.
2075
2076            --  Ada 95 limited type
2077            --    * Type with reserved word "limited"
2078            --    * A protected or task type
2079            --    * A composite type with limited component
2080
2081            elsif Ada_Version <= Ada_95 then
2082               return Is_Limited_Type (Typ);
2083
2084            --  Ada 2005
2085
2086            --  The current instance of a limited tagged type, a protected
2087            --  type, a task type, or a type that has the reserved word
2088            --  "limited" in its full definition ... a formal parameter or
2089            --  generic formal object of a tagged type.
2090
2091            --  Ada 2005 limited type
2092            --    * Type with reserved word "limited", "synchronized", "task"
2093            --      or "protected"
2094            --    * A composite type with limited component
2095            --    * A derived type whose parent is a non-interface limited type
2096
2097            elsif Ada_Version = Ada_2005 then
2098               return
2099                 (Is_Limited_Type (Typ) and then Is_Tagged_Type (Typ))
2100                   or else
2101                     (Is_Derived_Type (Typ)
2102                       and then not Is_Interface (Etype (Typ))
2103                       and then Is_Limited_Type (Etype (Typ)));
2104
2105            --  Ada 2012 and beyond
2106
2107            --  The current instance of an immutably limited type ... a formal
2108            --  parameter or generic formal object of a tagged type.
2109
2110            --  Ada 2012 limited type
2111            --    * Type with reserved word "limited", "synchronized", "task"
2112            --      or "protected"
2113            --    * A composite type with limited component
2114            --    * A derived type whose parent is a non-interface limited type
2115            --    * An incomplete view
2116
2117            --  Ada 2012 immutably limited type
2118            --    * Explicitly limited record type
2119            --    * Record extension with "limited" present
2120            --    * Non-formal limited private type that is either tagged
2121            --      or has at least one access discriminant with a default
2122            --      expression
2123            --    * Task type, protected type or synchronized interface
2124            --    * Type derived from immutably limited type
2125
2126            else
2127               return
2128                 Is_Immutably_Limited_Type (Typ)
2129                   or else Is_Incomplete_Type (Typ);
2130            end if;
2131         end Is_Aliased_View_Of_Type;
2132
2133         -------------
2134         -- Process --
2135         -------------
2136
2137         function Process (N : Node_Id) return Traverse_Result is
2138         begin
2139            case Nkind (N) is
2140               when N_Attribute_Reference =>
2141                  if Nam_In (Attribute_Name (N), Name_Access,
2142                                                 Name_Unchecked_Access)
2143                    and then Is_Entity_Name (Prefix (N))
2144                    and then Is_Type (Entity (Prefix (N)))
2145                    and then Entity (Prefix (N)) = E
2146                  then
2147                     if Ada_Version < Ada_2012 then
2148                        Error_Msg_N
2149                          ("current instance must be a limited type",
2150                           Prefix (N));
2151                     else
2152                        Error_Msg_N
2153                          ("current instance must be an immutably limited "
2154                           & "type (RM-2012, 7.5 (8.1/3))", Prefix (N));
2155                     end if;
2156
2157                     return Abandon;
2158
2159                  else
2160                     return OK;
2161                  end if;
2162
2163               when others => return OK;
2164            end case;
2165         end Process;
2166
2167         procedure Traverse is new Traverse_Proc (Process);
2168
2169         --  Local variables
2170
2171         Rec_Type : constant Entity_Id :=
2172                      Scope (Defining_Identifier (Comp_Decl));
2173
2174      --  Start of processing for Check_Current_Instance
2175
2176      begin
2177         if not Is_Aliased_View_Of_Type (Rec_Type) then
2178            Traverse (Comp_Decl);
2179         end if;
2180      end Check_Current_Instance;
2181
2182      ------------------------------
2183      -- Check_Suspicious_Modulus --
2184      ------------------------------
2185
2186      procedure Check_Suspicious_Modulus (Utype : Entity_Id) is
2187         Decl : constant Node_Id := Declaration_Node (Underlying_Type (Utype));
2188
2189      begin
2190         if not Warn_On_Suspicious_Modulus_Value then
2191            return;
2192         end if;
2193
2194         if Nkind (Decl) = N_Full_Type_Declaration then
2195            declare
2196               Tdef : constant Node_Id := Type_Definition (Decl);
2197
2198            begin
2199               if Nkind (Tdef) = N_Modular_Type_Definition then
2200                  declare
2201                     Modulus : constant Node_Id :=
2202                                 Original_Node (Expression (Tdef));
2203
2204                  begin
2205                     if Nkind (Modulus) = N_Integer_Literal then
2206                        declare
2207                           Modv : constant Uint := Intval (Modulus);
2208                           Sizv : constant Uint := RM_Size (Utype);
2209
2210                        begin
2211                           --  First case, modulus and size are the same. This
2212                           --  happens if you have something like mod 32, with
2213                           --  an explicit size of 32, this is for sure a case
2214                           --  where the warning is given, since it is seems
2215                           --  very unlikely that someone would want e.g. a
2216                           --  five bit type stored in 32 bits. It is much
2217                           --  more likely they wanted a 32-bit type.
2218
2219                           if Modv = Sizv then
2220                              null;
2221
2222                           --  Second case, the modulus is 32 or 64 and no
2223                           --  size clause is present. This is a less clear
2224                           --  case for giving the warning, but in the case
2225                           --  of 32/64 (5-bit or 6-bit types) these seem rare
2226                           --  enough that it is a likely error (and in any
2227                           --  case using 2**5 or 2**6 in these cases seems
2228                           --  clearer. We don't include 8 or 16 here, simply
2229                           --  because in practice 3-bit and 4-bit types are
2230                           --  more common and too many false positives if
2231                           --  we warn in these cases.
2232
2233                           elsif not Has_Size_Clause (Utype)
2234                             and then (Modv = Uint_32 or else Modv = Uint_64)
2235                           then
2236                              null;
2237
2238                           --  No warning needed
2239
2240                           else
2241                              return;
2242                           end if;
2243
2244                           --  If we fall through, give warning
2245
2246                           Error_Msg_Uint_1 := Modv;
2247                           Error_Msg_N
2248                             ("?M?2 '*'*^' may have been intended here",
2249                              Modulus);
2250                        end;
2251                     end if;
2252                  end;
2253               end if;
2254            end;
2255         end if;
2256      end Check_Suspicious_Modulus;
2257
2258      -----------------------
2259      -- Freeze_Array_Type --
2260      -----------------------
2261
2262      procedure Freeze_Array_Type (Arr : Entity_Id) is
2263         FS     : constant Entity_Id := First_Subtype (Arr);
2264         Ctyp   : constant Entity_Id := Component_Type (Arr);
2265         Clause : Entity_Id;
2266
2267         Non_Standard_Enum : Boolean := False;
2268         --  Set true if any of the index types is an enumeration type with a
2269         --  non-standard representation.
2270
2271      begin
2272         Freeze_And_Append (Ctyp, N, Result);
2273
2274         Indx := First_Index (Arr);
2275         while Present (Indx) loop
2276            Freeze_And_Append (Etype (Indx), N, Result);
2277
2278            if Is_Enumeration_Type (Etype (Indx))
2279              and then Has_Non_Standard_Rep (Etype (Indx))
2280            then
2281               Non_Standard_Enum := True;
2282            end if;
2283
2284            Next_Index (Indx);
2285         end loop;
2286
2287         --  Processing that is done only for base types
2288
2289         if Ekind (Arr) = E_Array_Type then
2290
2291            --  Deal with default setting of reverse storage order
2292
2293            Set_SSO_From_Default (Arr);
2294
2295            --  Propagate flags for component type
2296
2297            if Is_Controlled_Active (Component_Type (Arr))
2298              or else Has_Controlled_Component (Ctyp)
2299            then
2300               Set_Has_Controlled_Component (Arr);
2301            end if;
2302
2303            if Has_Unchecked_Union (Component_Type (Arr)) then
2304               Set_Has_Unchecked_Union (Arr);
2305            end if;
2306
2307            --  Warn for pragma Pack overriding foreign convention
2308
2309            if Has_Foreign_Convention (Ctyp)
2310              and then Has_Pragma_Pack (Arr)
2311            then
2312               declare
2313                  CN : constant Name_Id :=
2314                         Get_Convention_Name (Convention (Ctyp));
2315                  PP : constant Node_Id :=
2316                         Get_Pragma (First_Subtype (Arr), Pragma_Pack);
2317               begin
2318                  if Present (PP) then
2319                     Error_Msg_Name_1 := CN;
2320                     Error_Msg_Sloc := Sloc (Arr);
2321                     Error_Msg_N
2322                       ("pragma Pack affects convention % components #??", PP);
2323                     Error_Msg_Name_1 := CN;
2324                     Error_Msg_N
2325                       ("\array components may not have % compatible "
2326                        & "representation??", PP);
2327                  end if;
2328               end;
2329            end if;
2330
2331            --  If packing was requested or if the component size was
2332            --  set explicitly, then see if bit packing is required. This
2333            --  processing is only done for base types, since all of the
2334            --  representation aspects involved are type-related.
2335
2336            --  This is not just an optimization, if we start processing the
2337            --  subtypes, they interfere with the settings on the base type
2338            --  (this is because Is_Packed has a slightly different meaning
2339            --  before and after freezing).
2340
2341            declare
2342               Csiz : Uint;
2343               Esiz : Uint;
2344
2345            begin
2346               if (Is_Packed (Arr) or else Has_Pragma_Pack (Arr))
2347                 and then Known_Static_RM_Size (Ctyp)
2348                 and then not Has_Component_Size_Clause (Arr)
2349               then
2350                  Csiz := UI_Max (RM_Size (Ctyp), 1);
2351
2352               elsif Known_Component_Size (Arr) then
2353                  Csiz := Component_Size (Arr);
2354
2355               elsif not Known_Static_Esize (Ctyp) then
2356                  Csiz := Uint_0;
2357
2358               else
2359                  Esiz := Esize (Ctyp);
2360
2361                  --  We can set the component size if it is less than 16,
2362                  --  rounding it up to the next storage unit size.
2363
2364                  if Esiz <= 8 then
2365                     Csiz := Uint_8;
2366                  elsif Esiz <= 16 then
2367                     Csiz := Uint_16;
2368                  else
2369                     Csiz := Uint_0;
2370                  end if;
2371
2372                  --  Set component size up to match alignment if it would
2373                  --  otherwise be less than the alignment. This deals with
2374                  --  cases of types whose alignment exceeds their size (the
2375                  --  padded type cases).
2376
2377                  if Csiz /= 0 then
2378                     declare
2379                        A : constant Uint := Alignment_In_Bits (Ctyp);
2380                     begin
2381                        if Csiz < A then
2382                           Csiz := A;
2383                        end if;
2384                     end;
2385                  end if;
2386               end if;
2387
2388               --  Case of component size that may result in packing
2389
2390               if 1 <= Csiz and then Csiz <= 64 then
2391                  declare
2392                     Ent         : constant Entity_Id :=
2393                                     First_Subtype (Arr);
2394                     Pack_Pragma : constant Node_Id :=
2395                                     Get_Rep_Pragma (Ent, Name_Pack);
2396                     Comp_Size_C : constant Node_Id :=
2397                                     Get_Attribute_Definition_Clause
2398                                       (Ent, Attribute_Component_Size);
2399
2400                  begin
2401                     --  Warn if we have pack and component size so that the
2402                     --  pack is ignored.
2403
2404                     --  Note: here we must check for the presence of a
2405                     --  component size before checking for a Pack pragma to
2406                     --  deal with the case where the array type is a derived
2407                     --  type whose parent is currently private.
2408
2409                     if Present (Comp_Size_C)
2410                       and then Has_Pragma_Pack (Ent)
2411                       and then Warn_On_Redundant_Constructs
2412                     then
2413                        Error_Msg_Sloc := Sloc (Comp_Size_C);
2414                        Error_Msg_NE
2415                          ("?r?pragma Pack for& ignored!", Pack_Pragma, Ent);
2416                        Error_Msg_N
2417                          ("\?r?explicit component size given#!", Pack_Pragma);
2418                        Set_Is_Packed (Base_Type (Ent), False);
2419                        Set_Is_Bit_Packed_Array (Base_Type (Ent), False);
2420                     end if;
2421
2422                     --  Set component size if not already set by a component
2423                     --  size clause.
2424
2425                     if not Present (Comp_Size_C) then
2426                        Set_Component_Size (Arr, Csiz);
2427                     end if;
2428
2429                     --  Check for base type of 8, 16, 32 bits, where an
2430                     --  unsigned subtype has a length one less than the
2431                     --  base type (e.g. Natural subtype of Integer).
2432
2433                     --  In such cases, if a component size was not set
2434                     --  explicitly, then generate a warning.
2435
2436                     if Has_Pragma_Pack (Arr)
2437                       and then not Present (Comp_Size_C)
2438                       and then (Csiz = 7 or else Csiz = 15 or else Csiz = 31)
2439                       and then Esize (Base_Type (Ctyp)) = Csiz + 1
2440                     then
2441                        Error_Msg_Uint_1 := Csiz;
2442
2443                        if Present (Pack_Pragma) then
2444                           Error_Msg_N
2445                             ("??pragma Pack causes component size to be ^!",
2446                              Pack_Pragma);
2447                           Error_Msg_N
2448                             ("\??use Component_Size to set desired value!",
2449                              Pack_Pragma);
2450                        end if;
2451                     end if;
2452
2453                     --  Actual packing is not needed for 8, 16, 32, 64. Also
2454                     --  not needed for 24 if alignment is 1.
2455
2456                     if        Csiz = 8
2457                       or else Csiz = 16
2458                       or else Csiz = 32
2459                       or else Csiz = 64
2460                       or else (Csiz = 24 and then Alignment (Ctyp) = 1)
2461                     then
2462                        --  Here the array was requested to be packed, but
2463                        --  the packing request had no effect, so Is_Packed
2464                        --  is reset.
2465
2466                        --  Note: semantically this means that we lose track
2467                        --  of the fact that a derived type inherited a pragma
2468                        --  Pack that was non- effective, but that seems fine.
2469
2470                        --  We regard a Pack pragma as a request to set a
2471                        --  representation characteristic, and this request
2472                        --  may be ignored.
2473
2474                        Set_Is_Packed           (Base_Type (Arr), False);
2475                        Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2476
2477                        if Known_Static_Esize (Component_Type (Arr))
2478                          and then Esize (Component_Type (Arr)) = Csiz
2479                        then
2480                           Set_Has_Non_Standard_Rep (Base_Type (Arr), False);
2481                        end if;
2482
2483                        --  In all other cases, packing is indeed needed
2484
2485                     else
2486                        Set_Has_Non_Standard_Rep (Base_Type (Arr), True);
2487                        Set_Is_Bit_Packed_Array  (Base_Type (Arr), True);
2488                        Set_Is_Packed            (Base_Type (Arr), True);
2489                     end if;
2490                  end;
2491               end if;
2492            end;
2493
2494            --  Check for Aliased or Atomic_Components/Atomic/VFA with
2495            --  unsuitable packing or explicit component size clause given.
2496
2497            if (Has_Aliased_Components (Arr)
2498                 or else Has_Atomic_Components (Arr)
2499                 or else Is_Atomic_Or_VFA (Ctyp))
2500              and then
2501                (Has_Component_Size_Clause (Arr) or else Is_Packed (Arr))
2502            then
2503               Alias_Atomic_Check : declare
2504
2505                  procedure Complain_CS (T : String);
2506                  --  Outputs error messages for incorrect CS clause or pragma
2507                  --  Pack for aliased or atomic/VFA components (T is "aliased"
2508                  --  or "atomic/vfa");
2509
2510                  -----------------
2511                  -- Complain_CS --
2512                  -----------------
2513
2514                  procedure Complain_CS (T : String) is
2515                  begin
2516                     if Has_Component_Size_Clause (Arr) then
2517                        Clause :=
2518                          Get_Attribute_Definition_Clause
2519                            (FS, Attribute_Component_Size);
2520
2521                        Error_Msg_N
2522                          ("incorrect component size for "
2523                           & T & " components", Clause);
2524                        Error_Msg_Uint_1 := Esize (Ctyp);
2525                        Error_Msg_N
2526                          ("\only allowed value is^", Clause);
2527
2528                     else
2529                        Error_Msg_N
2530                          ("cannot pack " & T & " components",
2531                           Get_Rep_Pragma (FS, Name_Pack));
2532                     end if;
2533                  end Complain_CS;
2534
2535                  --  Start of processing for Alias_Atomic_Check
2536
2537               begin
2538                  --  If object size of component type isn't known, we cannot
2539                  --  be sure so we defer to the back end.
2540
2541                  if not Known_Static_Esize (Ctyp) then
2542                     null;
2543
2544                  --  Case where component size has no effect. First check for
2545                  --  object size of component type multiple of the storage
2546                  --  unit size.
2547
2548                  elsif Esize (Ctyp) mod System_Storage_Unit = 0
2549
2550                    --  OK in both packing case and component size case if RM
2551                    --  size is known and static and same as the object size.
2552
2553                    and then
2554                      ((Known_Static_RM_Size (Ctyp)
2555                         and then Esize (Ctyp) = RM_Size (Ctyp))
2556
2557                        --  Or if we have an explicit component size clause and
2558                        --  the component size and object size are equal.
2559
2560                        or else
2561                          (Has_Component_Size_Clause (Arr)
2562                            and then Component_Size (Arr) = Esize (Ctyp)))
2563                  then
2564                     null;
2565
2566                  elsif Has_Aliased_Components (Arr) then
2567                     Complain_CS ("aliased");
2568
2569                  elsif Has_Atomic_Components (Arr)
2570                    or else Is_Atomic (Ctyp)
2571                  then
2572                     Complain_CS ("atomic");
2573
2574                  elsif Is_Volatile_Full_Access (Ctyp) then
2575                     Complain_CS ("volatile full access");
2576                  end if;
2577               end Alias_Atomic_Check;
2578            end if;
2579
2580            --  Check for Independent_Components/Independent with unsuitable
2581            --  packing or explicit component size clause given.
2582
2583            if (Has_Independent_Components (Arr) or else Is_Independent (Ctyp))
2584                  and then
2585               (Has_Component_Size_Clause  (Arr) or else Is_Packed (Arr))
2586            then
2587               begin
2588                  --  If object size of component type isn't known, we cannot
2589                  --  be sure so we defer to the back end.
2590
2591                  if not Known_Static_Esize (Ctyp) then
2592                     null;
2593
2594                  --  Case where component size has no effect. First check for
2595                  --  object size of component type multiple of the storage
2596                  --  unit size.
2597
2598                  elsif Esize (Ctyp) mod System_Storage_Unit = 0
2599
2600                    --  OK in both packing case and component size case if RM
2601                    --  size is known and multiple of the storage unit size.
2602
2603                    and then
2604                      ((Known_Static_RM_Size (Ctyp)
2605                         and then RM_Size (Ctyp) mod System_Storage_Unit = 0)
2606
2607                        --  Or if we have an explicit component size clause and
2608                        --  the component size is larger than the object size.
2609
2610                        or else
2611                          (Has_Component_Size_Clause (Arr)
2612                            and then Component_Size (Arr) >= Esize (Ctyp)))
2613                  then
2614                     null;
2615
2616                  else
2617                     if Has_Component_Size_Clause (Arr) then
2618                        Clause :=
2619                          Get_Attribute_Definition_Clause
2620                            (FS, Attribute_Component_Size);
2621
2622                        Error_Msg_N
2623                          ("incorrect component size for "
2624                           & "independent components", Clause);
2625                        Error_Msg_Uint_1 := Esize (Ctyp);
2626                        Error_Msg_N
2627                          ("\minimum allowed is^", Clause);
2628
2629                     else
2630                        Error_Msg_N
2631                          ("cannot pack independent components",
2632                           Get_Rep_Pragma (FS, Name_Pack));
2633                     end if;
2634                  end if;
2635               end;
2636            end if;
2637
2638            --  Warn for case of atomic type
2639
2640            Clause := Get_Rep_Pragma (FS, Name_Atomic);
2641
2642            if Present (Clause)
2643              and then not Addressable (Component_Size (FS))
2644            then
2645               Error_Msg_NE
2646                 ("non-atomic components of type& may not be "
2647                  & "accessible by separate tasks??", Clause, Arr);
2648
2649               if Has_Component_Size_Clause (Arr) then
2650                  Error_Msg_Sloc := Sloc (Get_Attribute_Definition_Clause
2651                                           (FS, Attribute_Component_Size));
2652                  Error_Msg_N ("\because of component size clause#??", Clause);
2653
2654               elsif Has_Pragma_Pack (Arr) then
2655                  Error_Msg_Sloc := Sloc (Get_Rep_Pragma (FS, Name_Pack));
2656                  Error_Msg_N ("\because of pragma Pack#??", Clause);
2657               end if;
2658            end if;
2659
2660            --  Check for scalar storage order
2661
2662            declare
2663               Dummy : Boolean;
2664            begin
2665               Check_Component_Storage_Order
2666                 (Encl_Type        => Arr,
2667                  Comp             => Empty,
2668                  ADC              => Get_Attribute_Definition_Clause
2669                                        (First_Subtype (Arr),
2670                                         Attribute_Scalar_Storage_Order),
2671                  Comp_ADC_Present => Dummy);
2672            end;
2673
2674         --  Processing that is done only for subtypes
2675
2676         else
2677            --  Acquire alignment from base type
2678
2679            if Unknown_Alignment (Arr) then
2680               Set_Alignment (Arr, Alignment (Base_Type (Arr)));
2681               Adjust_Esize_Alignment (Arr);
2682            end if;
2683         end if;
2684
2685         --  Specific checks for bit-packed arrays
2686
2687         if Is_Bit_Packed_Array (Arr) then
2688
2689            --  Check number of elements for bit packed arrays that come from
2690            --  source and have compile time known ranges. The bit-packed
2691            --  arrays circuitry does not support arrays with more than
2692            --  Integer'Last + 1 elements, and when this restriction is
2693            --  violated, causes incorrect data access.
2694
2695            --  For the case where this is not compile time known, a run-time
2696            --  check should be generated???
2697
2698            if Comes_From_Source (Arr) and then Is_Constrained (Arr) then
2699               declare
2700                  Elmts : Uint;
2701                  Index : Node_Id;
2702                  Ilen  : Node_Id;
2703                  Ityp  : Entity_Id;
2704
2705               begin
2706                  Elmts := Uint_1;
2707                  Index := First_Index (Arr);
2708                  while Present (Index) loop
2709                     Ityp := Etype (Index);
2710
2711                     --  Never generate an error if any index is of a generic
2712                     --  type. We will check this in instances.
2713
2714                     if Is_Generic_Type (Ityp) then
2715                        Elmts := Uint_0;
2716                        exit;
2717                     end if;
2718
2719                     Ilen :=
2720                       Make_Attribute_Reference (Loc,
2721                         Prefix         => New_Occurrence_Of (Ityp, Loc),
2722                         Attribute_Name => Name_Range_Length);
2723                     Analyze_And_Resolve (Ilen);
2724
2725                     --  No attempt is made to check number of elements if not
2726                     --  compile time known.
2727
2728                     if Nkind (Ilen) /= N_Integer_Literal then
2729                        Elmts := Uint_0;
2730                        exit;
2731                     end if;
2732
2733                     Elmts := Elmts * Intval (Ilen);
2734                     Next_Index (Index);
2735                  end loop;
2736
2737                  if Elmts > Intval (High_Bound
2738                                       (Scalar_Range (Standard_Integer))) + 1
2739                  then
2740                     Error_Msg_N
2741                       ("bit packed array type may not have "
2742                        & "more than Integer''Last+1 elements", Arr);
2743                  end if;
2744               end;
2745            end if;
2746
2747            --  Check size
2748
2749            if Known_RM_Size (Arr) then
2750               declare
2751                  SizC    : constant Node_Id := Size_Clause (Arr);
2752                  Discard : Boolean;
2753
2754               begin
2755                  --  It is not clear if it is possible to have no size clause
2756                  --  at this stage, but it is not worth worrying about. Post
2757                  --  error on the entity name in the size clause if present,
2758                  --  else on the type entity itself.
2759
2760                  if Present (SizC) then
2761                     Check_Size (Name (SizC), Arr, RM_Size (Arr), Discard);
2762                  else
2763                     Check_Size (Arr, Arr, RM_Size (Arr), Discard);
2764                  end if;
2765               end;
2766            end if;
2767         end if;
2768
2769         --  If any of the index types was an enumeration type with a non-
2770         --  standard rep clause, then we indicate that the array type is
2771         --  always packed (even if it is not bit packed).
2772
2773         if Non_Standard_Enum then
2774            Set_Has_Non_Standard_Rep (Base_Type (Arr));
2775            Set_Is_Packed            (Base_Type (Arr));
2776         end if;
2777
2778         Set_Component_Alignment_If_Not_Set (Arr);
2779
2780         --  If the array is packed, we must create the packed array type to be
2781         --  used to actually implement the type. This is only needed for real
2782         --  array types (not for string literal types, since they are present
2783         --  only for the front end).
2784
2785         if Is_Packed (Arr)
2786           and then Ekind (Arr) /= E_String_Literal_Subtype
2787         then
2788            Create_Packed_Array_Impl_Type (Arr);
2789            Freeze_And_Append (Packed_Array_Impl_Type (Arr), N, Result);
2790
2791            --  Make sure that we have the necessary routines to implement the
2792            --  packing, and complain now if not. Note that we only test this
2793            --  for constrained array types.
2794
2795            if Is_Constrained (Arr)
2796              and then Is_Bit_Packed_Array (Arr)
2797              and then Present (Packed_Array_Impl_Type (Arr))
2798              and then Is_Array_Type (Packed_Array_Impl_Type (Arr))
2799            then
2800               declare
2801                  CS : constant Uint  := Component_Size (Arr);
2802                  RE : constant RE_Id := Get_Id (UI_To_Int (CS));
2803
2804               begin
2805                  if RE /= RE_Null
2806                    and then not RTE_Available (RE)
2807                  then
2808                     Error_Msg_CRT
2809                       ("packing of " & UI_Image (CS) & "-bit components",
2810                        First_Subtype (Etype (Arr)));
2811
2812                     --  Cancel the packing
2813
2814                     Set_Is_Packed (Base_Type (Arr), False);
2815                     Set_Is_Bit_Packed_Array (Base_Type (Arr), False);
2816                     Set_Packed_Array_Impl_Type (Arr, Empty);
2817                     goto Skip_Packed;
2818                  end if;
2819               end;
2820            end if;
2821
2822            --  Size information of packed array type is copied to the array
2823            --  type, since this is really the representation. But do not
2824            --  override explicit existing size values. If the ancestor subtype
2825            --  is constrained the Packed_Array_Impl_Type will be inherited
2826            --  from it, but the size may have been provided already, and
2827            --  must not be overridden either.
2828
2829            if not Has_Size_Clause (Arr)
2830              and then
2831                (No (Ancestor_Subtype (Arr))
2832                  or else not Has_Size_Clause (Ancestor_Subtype (Arr)))
2833            then
2834               Set_Esize     (Arr, Esize     (Packed_Array_Impl_Type (Arr)));
2835               Set_RM_Size   (Arr, RM_Size   (Packed_Array_Impl_Type (Arr)));
2836            end if;
2837
2838            if not Has_Alignment_Clause (Arr) then
2839               Set_Alignment (Arr, Alignment (Packed_Array_Impl_Type (Arr)));
2840            end if;
2841         end if;
2842
2843         <<Skip_Packed>>
2844
2845         --  For non-packed arrays set the alignment of the array to the
2846         --  alignment of the component type if it is unknown. Skip this
2847         --  in atomic/VFA case (atomic/VFA arrays may need larger alignments).
2848
2849         if not Is_Packed (Arr)
2850           and then Unknown_Alignment (Arr)
2851           and then Known_Alignment (Ctyp)
2852           and then Known_Static_Component_Size (Arr)
2853           and then Known_Static_Esize (Ctyp)
2854           and then Esize (Ctyp) = Component_Size (Arr)
2855           and then not Is_Atomic_Or_VFA (Arr)
2856         then
2857            Set_Alignment (Arr, Alignment (Component_Type (Arr)));
2858         end if;
2859
2860         --  A Ghost type cannot have a component of protected or task type
2861         --  (SPARK RM 6.9(19)).
2862
2863         if Is_Ghost_Entity (Arr) and then Is_Concurrent_Type (Ctyp) then
2864            Error_Msg_N
2865              ("ghost array type & cannot have concurrent component type",
2866               Arr);
2867         end if;
2868      end Freeze_Array_Type;
2869
2870      -------------------------------
2871      -- Freeze_Object_Declaration --
2872      -------------------------------
2873
2874      procedure Freeze_Object_Declaration (E : Entity_Id) is
2875      begin
2876         --  Abstract type allowed only for C++ imported variables or constants
2877
2878         --  Note: we inhibit this check for objects that do not come from
2879         --  source because there is at least one case (the expansion of
2880         --  x'Class'Input where x is abstract) where we legitimately
2881         --  generate an abstract object.
2882
2883         if Is_Abstract_Type (Etype (E))
2884           and then Comes_From_Source (Parent (E))
2885           and then not (Is_Imported (E) and then Is_CPP_Class (Etype (E)))
2886         then
2887            Error_Msg_N ("type of object cannot be abstract",
2888                         Object_Definition (Parent (E)));
2889
2890            if Is_CPP_Class (Etype (E)) then
2891               Error_Msg_NE
2892                 ("\} may need a cpp_constructor",
2893                  Object_Definition (Parent (E)), Etype (E));
2894
2895            elsif Present (Expression (Parent (E))) then
2896               Error_Msg_N --  CODEFIX
2897                 ("\maybe a class-wide type was meant",
2898                  Object_Definition (Parent (E)));
2899            end if;
2900         end if;
2901
2902         --  For object created by object declaration, perform required
2903         --  categorization (preelaborate and pure) checks. Defer these
2904         --  checks to freeze time since pragma Import inhibits default
2905         --  initialization and thus pragma Import affects these checks.
2906
2907         Validate_Object_Declaration (Declaration_Node (E));
2908
2909         --  If there is an address clause, check that it is valid
2910         --  and if need be move initialization to the freeze node.
2911
2912         Check_Address_Clause (E);
2913
2914         --  Similar processing is needed for aspects that may affect
2915         --  object layout, like Alignment, if there is an initialization
2916         --  expression.
2917
2918         if Has_Delayed_Aspects (E)
2919           and then Expander_Active
2920           and then Is_Array_Type (Etype (E))
2921           and then Present (Expression (Parent (E)))
2922         then
2923            declare
2924               Decl : constant Node_Id := Parent (E);
2925               Lhs  : constant Node_Id := New_Occurrence_Of (E, Loc);
2926
2927            begin
2928
2929               --  Capture initialization value at point of declaration, and
2930               --  make explicit assignment legal, because object may be a
2931               --  constant.
2932
2933               Remove_Side_Effects (Expression (Decl));
2934               Set_Assignment_OK (Lhs);
2935
2936               --  Move initialization to freeze actions.
2937
2938               Append_Freeze_Action (E,
2939                 Make_Assignment_Statement (Loc,
2940                   Name       => Lhs,
2941                   Expression => Expression (Decl)));
2942
2943               Set_No_Initialization (Decl);
2944               --  Set_Is_Frozen (E, False);
2945            end;
2946         end if;
2947
2948         --  Reset Is_True_Constant for non-constant aliased object. We
2949         --  consider that the fact that a non-constant object is aliased may
2950         --  indicate that some funny business is going on, e.g. an aliased
2951         --  object is passed by reference to a procedure which captures the
2952         --  address of the object, which is later used to assign a new value,
2953         --  even though the compiler thinks that it is not modified. Such
2954         --  code is highly dubious, but we choose to make it "work" for
2955         --  non-constant aliased objects.
2956
2957         --  Note that we used to do this for all aliased objects, whether or
2958         --  not constant, but this caused anomalies down the line because we
2959         --  ended up with static objects that were not Is_True_Constant. Not
2960         --  resetting Is_True_Constant for (aliased) constant objects ensures
2961         --  that this anomaly never occurs.
2962
2963         --  However, we don't do that for internal entities. We figure that if
2964         --  we deliberately set Is_True_Constant for an internal entity, e.g.
2965         --  a dispatch table entry, then we mean it.
2966
2967         if Ekind (E) /= E_Constant
2968           and then (Is_Aliased (E) or else Is_Aliased (Etype (E)))
2969           and then not Is_Internal_Name (Chars (E))
2970         then
2971            Set_Is_True_Constant (E, False);
2972         end if;
2973
2974         --  If the object needs any kind of default initialization, an error
2975         --  must be issued if No_Default_Initialization applies. The check
2976         --  doesn't apply to imported objects, which are not ever default
2977         --  initialized, and is why the check is deferred until freezing, at
2978         --  which point we know if Import applies. Deferred constants are also
2979         --  exempted from this test because their completion is explicit, or
2980         --  through an import pragma.
2981
2982         if Ekind (E) = E_Constant and then Present (Full_View (E)) then
2983            null;
2984
2985         elsif Comes_From_Source (E)
2986           and then not Is_Imported (E)
2987           and then not Has_Init_Expression (Declaration_Node (E))
2988           and then
2989             ((Has_Non_Null_Base_Init_Proc (Etype (E))
2990                and then not No_Initialization (Declaration_Node (E))
2991                and then not Initialization_Suppressed (Etype (E)))
2992              or else
2993                (Needs_Simple_Initialization (Etype (E))
2994                  and then not Is_Internal (E)))
2995         then
2996            Has_Default_Initialization := True;
2997            Check_Restriction
2998              (No_Default_Initialization, Declaration_Node (E));
2999         end if;
3000
3001         --  Check that a Thread_Local_Storage variable does not have
3002         --  default initialization, and any explicit initialization must
3003         --  either be the null constant or a static constant.
3004
3005         if Has_Pragma_Thread_Local_Storage (E) then
3006            declare
3007               Decl : constant Node_Id := Declaration_Node (E);
3008            begin
3009               if Has_Default_Initialization
3010                 or else
3011                   (Has_Init_Expression (Decl)
3012                     and then
3013                      (No (Expression (Decl))
3014                        or else not
3015                          (Is_OK_Static_Expression (Expression (Decl))
3016                            or else Nkind (Expression (Decl)) = N_Null)))
3017               then
3018                  Error_Msg_NE
3019                    ("Thread_Local_Storage variable& is "
3020                     & "improperly initialized", Decl, E);
3021                  Error_Msg_NE
3022                    ("\only allowed initialization is explicit "
3023                     & "NULL or static expression", Decl, E);
3024               end if;
3025            end;
3026         end if;
3027
3028         --  For imported objects, set Is_Public unless there is also an
3029         --  address clause, which means that there is no external symbol
3030         --  needed for the Import (Is_Public may still be set for other
3031         --  unrelated reasons). Note that we delayed this processing
3032         --  till freeze time so that we can be sure not to set the flag
3033         --  if there is an address clause. If there is such a clause,
3034         --  then the only purpose of the Import pragma is to suppress
3035         --  implicit initialization.
3036
3037         if Is_Imported (E) and then No (Address_Clause (E)) then
3038            Set_Is_Public (E);
3039         end if;
3040
3041         --  For source objects that are not Imported and are library
3042         --  level, if no linker section pragma was given inherit the
3043         --  appropriate linker section from the corresponding type.
3044
3045         if Comes_From_Source (E)
3046           and then not Is_Imported (E)
3047           and then Is_Library_Level_Entity (E)
3048           and then No (Linker_Section_Pragma (E))
3049         then
3050            Set_Linker_Section_Pragma
3051              (E, Linker_Section_Pragma (Etype (E)));
3052         end if;
3053
3054         --  For convention C objects of an enumeration type, warn if the
3055         --  size is not integer size and no explicit size given. Skip
3056         --  warning for Boolean, and Character, assume programmer expects
3057         --  8-bit sizes for these cases.
3058
3059         if (Convention (E) = Convention_C
3060               or else
3061             Convention (E) = Convention_CPP)
3062           and then Is_Enumeration_Type (Etype (E))
3063           and then not Is_Character_Type (Etype (E))
3064           and then not Is_Boolean_Type (Etype (E))
3065           and then Esize (Etype (E)) < Standard_Integer_Size
3066           and then not Has_Size_Clause (E)
3067         then
3068            Error_Msg_Uint_1 := UI_From_Int (Standard_Integer_Size);
3069            Error_Msg_N
3070              ("??convention C enumeration object has size less than ^", E);
3071            Error_Msg_N ("\??use explicit size clause to set size", E);
3072         end if;
3073      end Freeze_Object_Declaration;
3074
3075      -----------------------------
3076      -- Freeze_Generic_Entities --
3077      -----------------------------
3078
3079      function Freeze_Generic_Entities (Pack : Entity_Id) return List_Id is
3080         E     : Entity_Id;
3081         F     : Node_Id;
3082         Flist : List_Id;
3083
3084      begin
3085         Flist := New_List;
3086         E := First_Entity (Pack);
3087         while Present (E) loop
3088            if Is_Type (E) and then not Is_Generic_Type (E) then
3089               F := Make_Freeze_Generic_Entity (Sloc (Pack));
3090               Set_Entity (F, E);
3091               Append_To (Flist, F);
3092
3093            elsif Ekind (E) = E_Generic_Package then
3094               Append_List_To (Flist, Freeze_Generic_Entities (E));
3095            end if;
3096
3097            Next_Entity (E);
3098         end loop;
3099
3100         return Flist;
3101      end Freeze_Generic_Entities;
3102
3103      --------------------
3104      -- Freeze_Profile --
3105      --------------------
3106
3107      function Freeze_Profile (E : Entity_Id) return Boolean is
3108         F_Type    : Entity_Id;
3109         R_Type    : Entity_Id;
3110         Warn_Node : Node_Id;
3111
3112      begin
3113         --  Loop through formals
3114
3115         Formal := First_Formal (E);
3116         while Present (Formal) loop
3117            F_Type := Etype (Formal);
3118
3119            --  AI05-0151: incomplete types can appear in a profile. By the
3120            --  time the entity is frozen, the full view must be available,
3121            --  unless it is a limited view.
3122
3123            if Is_Incomplete_Type (F_Type)
3124              and then Present (Full_View (F_Type))
3125              and then not From_Limited_With (F_Type)
3126            then
3127               F_Type := Full_View (F_Type);
3128               Set_Etype (Formal, F_Type);
3129            end if;
3130
3131            if not From_Limited_With (F_Type) then
3132               Freeze_And_Append (F_Type, N, Result);
3133            end if;
3134
3135            if Is_Private_Type (F_Type)
3136              and then Is_Private_Type (Base_Type (F_Type))
3137              and then No (Full_View (Base_Type (F_Type)))
3138              and then not Is_Generic_Type (F_Type)
3139              and then not Is_Derived_Type (F_Type)
3140            then
3141               --  If the type of a formal is incomplete, subprogram is being
3142               --  frozen prematurely. Within an instance (but not within a
3143               --  wrapper package) this is an artifact of our need to regard
3144               --  the end of an instantiation as a freeze point. Otherwise it
3145               --  is a definite error.
3146
3147               if In_Instance then
3148                  Set_Is_Frozen (E, False);
3149                  Result := No_List;
3150                  return False;
3151
3152               elsif not After_Last_Declaration
3153                 and then not Freezing_Library_Level_Tagged_Type
3154               then
3155                  Error_Msg_Node_1 := F_Type;
3156                  Error_Msg
3157                    ("type & must be fully defined before this point", Loc);
3158               end if;
3159            end if;
3160
3161            --  Check suspicious parameter for C function. These tests apply
3162            --  only to exported/imported subprograms.
3163
3164            if Warn_On_Export_Import
3165              and then Comes_From_Source (E)
3166              and then (Convention (E) = Convention_C
3167                          or else
3168                        Convention (E) = Convention_CPP)
3169              and then (Is_Imported (E) or else Is_Exported (E))
3170              and then Convention (E) /= Convention (Formal)
3171              and then not Has_Warnings_Off (E)
3172              and then not Has_Warnings_Off (F_Type)
3173              and then not Has_Warnings_Off (Formal)
3174            then
3175               --  Qualify mention of formals with subprogram name
3176
3177               Error_Msg_Qual_Level := 1;
3178
3179               --  Check suspicious use of fat C pointer
3180
3181               if Is_Access_Type (F_Type)
3182                 and then Esize (F_Type) > Ttypes.System_Address_Size
3183               then
3184                  Error_Msg_N
3185                    ("?x?type of & does not correspond to C pointer!", Formal);
3186
3187               --  Check suspicious return of boolean
3188
3189               elsif Root_Type (F_Type) = Standard_Boolean
3190                 and then Convention (F_Type) = Convention_Ada
3191                 and then not Has_Warnings_Off (F_Type)
3192                 and then not Has_Size_Clause (F_Type)
3193               then
3194                  Error_Msg_N
3195                    ("& is an 8-bit Ada Boolean?x?", Formal);
3196                  Error_Msg_N
3197                    ("\use appropriate corresponding type in C "
3198                     & "(e.g. char)?x?", Formal);
3199
3200               --  Check suspicious tagged type
3201
3202               elsif (Is_Tagged_Type (F_Type)
3203                       or else
3204                        (Is_Access_Type (F_Type)
3205                          and then Is_Tagged_Type (Designated_Type (F_Type))))
3206                 and then Convention (E) = Convention_C
3207               then
3208                  Error_Msg_N
3209                    ("?x?& involves a tagged type which does not "
3210                     & "correspond to any C type!", Formal);
3211
3212               --  Check wrong convention subprogram pointer
3213
3214               elsif Ekind (F_Type) = E_Access_Subprogram_Type
3215                 and then not Has_Foreign_Convention (F_Type)
3216               then
3217                  Error_Msg_N
3218                    ("?x?subprogram pointer & should "
3219                     & "have foreign convention!", Formal);
3220                  Error_Msg_Sloc := Sloc (F_Type);
3221                  Error_Msg_NE
3222                    ("\?x?add Convention pragma to declaration of &#",
3223                     Formal, F_Type);
3224               end if;
3225
3226               --  Turn off name qualification after message output
3227
3228               Error_Msg_Qual_Level := 0;
3229            end if;
3230
3231            --  Check for unconstrained array in exported foreign convention
3232            --  case.
3233
3234            if Has_Foreign_Convention (E)
3235              and then not Is_Imported (E)
3236              and then Is_Array_Type (F_Type)
3237              and then not Is_Constrained (F_Type)
3238              and then Warn_On_Export_Import
3239            then
3240               Error_Msg_Qual_Level := 1;
3241
3242               --  If this is an inherited operation, place the warning on
3243               --  the derived type declaration, rather than on the original
3244               --  subprogram.
3245
3246               if Nkind (Original_Node (Parent (E))) = N_Full_Type_Declaration
3247               then
3248                  Warn_Node := Parent (E);
3249
3250                  if Formal = First_Formal (E) then
3251                     Error_Msg_NE ("??in inherited operation&", Warn_Node, E);
3252                  end if;
3253               else
3254                  Warn_Node := Formal;
3255               end if;
3256
3257               Error_Msg_NE ("?x?type of argument& is unconstrained array",
3258                  Warn_Node, Formal);
3259               Error_Msg_NE ("?x?foreign caller must pass bounds explicitly",
3260                  Warn_Node, Formal);
3261               Error_Msg_Qual_Level := 0;
3262            end if;
3263
3264            if not From_Limited_With (F_Type) then
3265               if Is_Access_Type (F_Type) then
3266                  F_Type := Designated_Type (F_Type);
3267               end if;
3268
3269               --  If the formal is an anonymous_access_to_subprogram
3270               --  freeze the  subprogram type as well, to prevent
3271               --  scope anomalies in gigi, because there is no other
3272               --  clear point at which it could be frozen.
3273
3274               if Is_Itype (Etype (Formal))
3275                 and then Ekind (F_Type) = E_Subprogram_Type
3276               then
3277                  Freeze_And_Append (F_Type, N, Result);
3278               end if;
3279            end if;
3280
3281            Next_Formal (Formal);
3282         end loop;
3283
3284         --  Case of function: similar checks on return type
3285
3286         if Ekind (E) = E_Function then
3287
3288            --  Check whether function is declared elsewhere.
3289
3290            Late_Freezing :=
3291              Get_Source_Unit (E) /= Get_Source_Unit (N)
3292                and then Returns_Limited_View (E)
3293                and then not In_Open_Scopes (Scope (E));
3294
3295            --  Freeze return type
3296
3297            R_Type := Etype (E);
3298
3299            --  AI05-0151: the return type may have been incomplete
3300            --  at the point of declaration. Replace it with the full
3301            --  view, unless the current type is a limited view. In
3302            --  that case the full view is in a different unit, and
3303            --  gigi finds the non-limited view after the other unit
3304            --  is elaborated.
3305
3306            if Ekind (R_Type) = E_Incomplete_Type
3307              and then Present (Full_View (R_Type))
3308              and then not From_Limited_With (R_Type)
3309            then
3310               R_Type := Full_View (R_Type);
3311               Set_Etype (E, R_Type);
3312
3313            --  If the return type is a limited view and the non-limited
3314            --  view is still incomplete, the function has to be frozen at a
3315            --  later time. If the function is abstract there is no place at
3316            --  which the full view will become available, and no code to be
3317            --  generated for it, so mark type as frozen.
3318
3319            elsif Ekind (R_Type) = E_Incomplete_Type
3320              and then From_Limited_With (R_Type)
3321              and then Ekind (Non_Limited_View (R_Type)) = E_Incomplete_Type
3322            then
3323               if Is_Abstract_Subprogram (E) then
3324                  null;
3325               else
3326                  Set_Is_Frozen (E, False);
3327                  Set_Returns_Limited_View (E);
3328                  return False;
3329               end if;
3330            end if;
3331
3332            Freeze_And_Append (R_Type, N, Result);
3333
3334            --  Check suspicious return type for C function
3335
3336            if Warn_On_Export_Import
3337              and then (Convention (E) = Convention_C
3338                          or else
3339                        Convention (E) = Convention_CPP)
3340              and then (Is_Imported (E) or else Is_Exported (E))
3341            then
3342               --  Check suspicious return of fat C pointer
3343
3344               if Is_Access_Type (R_Type)
3345                 and then Esize (R_Type) > Ttypes.System_Address_Size
3346                 and then not Has_Warnings_Off (E)
3347                 and then not Has_Warnings_Off (R_Type)
3348               then
3349                  Error_Msg_N ("?x?return type of& does not "
3350                     & "correspond to C pointer!", E);
3351
3352               --  Check suspicious return of boolean
3353
3354               elsif Root_Type (R_Type) = Standard_Boolean
3355                 and then Convention (R_Type) = Convention_Ada
3356                 and then not Has_Warnings_Off (E)
3357                 and then not Has_Warnings_Off (R_Type)
3358                 and then not Has_Size_Clause (R_Type)
3359               then
3360                  declare
3361                     N : constant Node_Id :=
3362                           Result_Definition (Declaration_Node (E));
3363                  begin
3364                     Error_Msg_NE
3365                       ("return type of & is an 8-bit Ada Boolean?x?", N, E);
3366                     Error_Msg_NE
3367                       ("\use appropriate corresponding type in C "
3368                        & "(e.g. char)?x?", N, E);
3369                  end;
3370
3371               --  Check suspicious return tagged type
3372
3373               elsif (Is_Tagged_Type (R_Type)
3374                       or else (Is_Access_Type (R_Type)
3375                                 and then
3376                                   Is_Tagged_Type
3377                                     (Designated_Type (R_Type))))
3378                 and then Convention (E) = Convention_C
3379                 and then not Has_Warnings_Off (E)
3380                 and then not Has_Warnings_Off (R_Type)
3381               then
3382                  Error_Msg_N ("?x?return type of & does not "
3383                     & "correspond to C type!", E);
3384
3385               --  Check return of wrong convention subprogram pointer
3386
3387               elsif Ekind (R_Type) = E_Access_Subprogram_Type
3388                 and then not Has_Foreign_Convention (R_Type)
3389                 and then not Has_Warnings_Off (E)
3390                 and then not Has_Warnings_Off (R_Type)
3391               then
3392                  Error_Msg_N ("?x?& should return a foreign "
3393                     & "convention subprogram pointer", E);
3394                  Error_Msg_Sloc := Sloc (R_Type);
3395                  Error_Msg_NE
3396                    ("\?x?add Convention pragma to declaration of& #",
3397                     E, R_Type);
3398               end if;
3399            end if;
3400
3401            --  Give warning for suspicious return of a result of an
3402            --  unconstrained array type in a foreign convention function.
3403
3404            if Has_Foreign_Convention (E)
3405
3406              --  We are looking for a return of unconstrained array
3407
3408              and then Is_Array_Type (R_Type)
3409              and then not Is_Constrained (R_Type)
3410
3411              --  Exclude imported routines, the warning does not belong on
3412              --  the import, but rather on the routine definition.
3413
3414              and then not Is_Imported (E)
3415
3416              --  Check that general warning is enabled, and that it is not
3417              --  suppressed for this particular case.
3418
3419              and then Warn_On_Export_Import
3420              and then not Has_Warnings_Off (E)
3421              and then not Has_Warnings_Off (R_Type)
3422            then
3423               Error_Msg_N
3424                 ("?x?foreign convention function& should not return "
3425                  & "unconstrained array!", E);
3426            end if;
3427         end if;
3428
3429         --  Check suspicious use of Import in pure unit (cases where the RM
3430         --  allows calls to be omitted).
3431
3432         if Is_Imported (E)
3433
3434           --  It might be suspicious if the compilation unit has the Pure
3435           --  aspect/pragma.
3436
3437           and then Has_Pragma_Pure (Cunit_Entity (Current_Sem_Unit))
3438
3439           --  The RM allows omission of calls only in the case of
3440           --  library-level subprograms (see RM-10.2.1(18)).
3441
3442           and then Is_Library_Level_Entity (E)
3443
3444           --  Ignore internally generated entity. This happens in some cases
3445           --  of subprograms in specs, where we generate an implied body.
3446
3447           and then Comes_From_Source (Import_Pragma (E))
3448
3449           --  Assume run-time knows what it is doing
3450
3451           and then not GNAT_Mode
3452
3453           --  Assume explicit Pure_Function means import is pure
3454
3455           and then not Has_Pragma_Pure_Function (E)
3456
3457           --  Don't need warning in relaxed semantics mode
3458
3459           and then not Relaxed_RM_Semantics
3460
3461           --  Assume convention Intrinsic is OK, since this is specialized.
3462           --  This deals with the DEC unit current_exception.ads
3463
3464           and then Convention (E) /= Convention_Intrinsic
3465
3466            --  Assume that ASM interface knows what it is doing. This deals
3467            --  with unsigned.ads in the AAMP back end.
3468
3469           and then Convention (E) /= Convention_Assembler
3470         then
3471            Error_Msg_N
3472              ("pragma Import in Pure unit??", Import_Pragma (E));
3473            Error_Msg_NE
3474              ("\calls to & may be omitted (RM 10.2.1(18/3))??",
3475               Import_Pragma (E), E);
3476         end if;
3477
3478         return True;
3479      end Freeze_Profile;
3480
3481      ------------------------
3482      -- Freeze_Record_Type --
3483      ------------------------
3484
3485      procedure Freeze_Record_Type (Rec : Entity_Id) is
3486         ADC  : Node_Id;
3487         Comp : Entity_Id;
3488         IR   : Node_Id;
3489         Prev : Entity_Id;
3490
3491         Junk : Boolean;
3492         pragma Warnings (Off, Junk);
3493
3494         Rec_Pushed : Boolean := False;
3495         --  Set True if the record type scope Rec has been pushed on the scope
3496         --  stack. Needed for the analysis of delayed aspects specified to the
3497         --  components of Rec.
3498
3499         SSO_ADC : Node_Id;
3500         --  Scalar_Storage_Order attribute definition clause for the record
3501
3502         Unplaced_Component : Boolean := False;
3503         --  Set True if we find at least one component with no component
3504         --  clause (used to warn about useless Pack pragmas).
3505
3506         Placed_Component : Boolean := False;
3507         --  Set True if we find at least one component with a component
3508         --  clause (used to warn about useless Bit_Order pragmas, and also
3509         --  to detect cases where Implicit_Packing may have an effect).
3510
3511         Aliased_Component : Boolean := False;
3512         --  Set True if we find at least one component which is aliased. This
3513         --  is used to prevent Implicit_Packing of the record, since packing
3514         --  cannot modify the size of alignment of an aliased component.
3515
3516         SSO_ADC_Component : Boolean := False;
3517         --  Set True if we find at least one component whose type has a
3518         --  Scalar_Storage_Order attribute definition clause.
3519
3520         All_Scalar_Components : Boolean := True;
3521         --  Set False if we encounter a component of a non-scalar type
3522
3523         Scalar_Component_Total_RM_Size : Uint := Uint_0;
3524         Scalar_Component_Total_Esize   : Uint := Uint_0;
3525         --  Accumulates total RM_Size values and total Esize values of all
3526         --  scalar components. Used for processing of Implicit_Packing.
3527
3528         function Check_Allocator (N : Node_Id) return Node_Id;
3529         --  If N is an allocator, possibly wrapped in one or more level of
3530         --  qualified expression(s), return the inner allocator node, else
3531         --  return Empty.
3532
3533         procedure Check_Itype (Typ : Entity_Id);
3534         --  If the component subtype is an access to a constrained subtype of
3535         --  an already frozen type, make the subtype frozen as well. It might
3536         --  otherwise be frozen in the wrong scope, and a freeze node on
3537         --  subtype has no effect. Similarly, if the component subtype is a
3538         --  regular (not protected) access to subprogram, set the anonymous
3539         --  subprogram type to frozen as well, to prevent an out-of-scope
3540         --  freeze node at some eventual point of call. Protected operations
3541         --  are handled elsewhere.
3542
3543         procedure Freeze_Choices_In_Variant_Part (VP : Node_Id);
3544         --  Make sure that all types mentioned in Discrete_Choices of the
3545         --  variants referenceed by the Variant_Part VP are frozen. This is
3546         --  a recursive routine to deal with nested variants.
3547
3548         ---------------------
3549         -- Check_Allocator --
3550         ---------------------
3551
3552         function Check_Allocator (N : Node_Id) return Node_Id is
3553            Inner : Node_Id;
3554         begin
3555            Inner := N;
3556            loop
3557               if Nkind (Inner) = N_Allocator then
3558                  return Inner;
3559               elsif Nkind (Inner) = N_Qualified_Expression then
3560                  Inner := Expression (Inner);
3561               else
3562                  return Empty;
3563               end if;
3564            end loop;
3565         end Check_Allocator;
3566
3567         -----------------
3568         -- Check_Itype --
3569         -----------------
3570
3571         procedure Check_Itype (Typ : Entity_Id) is
3572            Desig : constant Entity_Id := Designated_Type (Typ);
3573
3574         begin
3575            if not Is_Frozen (Desig)
3576              and then Is_Frozen (Base_Type (Desig))
3577            then
3578               Set_Is_Frozen (Desig);
3579
3580               --  In addition, add an Itype_Reference to ensure that the
3581               --  access subtype is elaborated early enough. This cannot be
3582               --  done if the subtype may depend on discriminants.
3583
3584               if Ekind (Comp) = E_Component
3585                 and then Is_Itype (Etype (Comp))
3586                 and then not Has_Discriminants (Rec)
3587               then
3588                  IR := Make_Itype_Reference (Sloc (Comp));
3589                  Set_Itype (IR, Desig);
3590                  Add_To_Result (IR);
3591               end if;
3592
3593            elsif Ekind (Typ) = E_Anonymous_Access_Subprogram_Type
3594              and then Convention (Desig) /= Convention_Protected
3595            then
3596               Set_Is_Frozen (Desig);
3597            end if;
3598         end Check_Itype;
3599
3600         ------------------------------------
3601         -- Freeze_Choices_In_Variant_Part --
3602         ------------------------------------
3603
3604         procedure Freeze_Choices_In_Variant_Part (VP : Node_Id) is
3605            pragma Assert (Nkind (VP) = N_Variant_Part);
3606
3607            Variant : Node_Id;
3608            Choice  : Node_Id;
3609            CL      : Node_Id;
3610
3611         begin
3612            --  Loop through variants
3613
3614            Variant := First_Non_Pragma (Variants (VP));
3615            while Present (Variant) loop
3616
3617               --  Loop through choices, checking that all types are frozen
3618
3619               Choice := First_Non_Pragma (Discrete_Choices (Variant));
3620               while Present (Choice) loop
3621                  if Nkind (Choice) in N_Has_Etype
3622                    and then Present (Etype (Choice))
3623                  then
3624                     Freeze_And_Append (Etype (Choice), N, Result);
3625                  end if;
3626
3627                  Next_Non_Pragma (Choice);
3628               end loop;
3629
3630               --  Check for nested variant part to process
3631
3632               CL := Component_List (Variant);
3633
3634               if not Null_Present (CL) then
3635                  if Present (Variant_Part (CL)) then
3636                     Freeze_Choices_In_Variant_Part (Variant_Part (CL));
3637                  end if;
3638               end if;
3639
3640               Next_Non_Pragma (Variant);
3641            end loop;
3642         end Freeze_Choices_In_Variant_Part;
3643
3644      --  Start of processing for Freeze_Record_Type
3645
3646      begin
3647         --  Deal with delayed aspect specifications for components. The
3648         --  analysis of the aspect is required to be delayed to the freeze
3649         --  point, thus we analyze the pragma or attribute definition
3650         --  clause in the tree at this point. We also analyze the aspect
3651         --  specification node at the freeze point when the aspect doesn't
3652         --  correspond to pragma/attribute definition clause.
3653
3654         Comp := First_Entity (Rec);
3655         while Present (Comp) loop
3656            if Ekind (Comp) = E_Component
3657              and then Has_Delayed_Aspects (Comp)
3658            then
3659               if not Rec_Pushed then
3660                  Push_Scope (Rec);
3661                  Rec_Pushed := True;
3662
3663                  --  The visibility to the discriminants must be restored in
3664                  --  order to properly analyze the aspects.
3665
3666                  if Has_Discriminants (Rec) then
3667                     Install_Discriminants (Rec);
3668                  end if;
3669               end if;
3670
3671               Analyze_Aspects_At_Freeze_Point (Comp);
3672            end if;
3673
3674            Next_Entity (Comp);
3675         end loop;
3676
3677         --  Pop the scope if Rec scope has been pushed on the scope stack
3678         --  during the delayed aspect analysis process.
3679
3680         if Rec_Pushed then
3681            if Has_Discriminants (Rec) then
3682               Uninstall_Discriminants (Rec);
3683            end if;
3684
3685            Pop_Scope;
3686         end if;
3687
3688         --  Freeze components and embedded subtypes
3689
3690         Comp := First_Entity (Rec);
3691         Prev := Empty;
3692         while Present (Comp) loop
3693            if Is_Aliased (Comp) then
3694               Aliased_Component := True;
3695            end if;
3696
3697            --  Handle the component and discriminant case
3698
3699            if Ekind_In (Comp, E_Component, E_Discriminant) then
3700               declare
3701                  CC : constant Node_Id := Component_Clause (Comp);
3702
3703               begin
3704                  --  Freezing a record type freezes the type of each of its
3705                  --  components. However, if the type of the component is
3706                  --  part of this record, we do not want or need a separate
3707                  --  Freeze_Node. Note that Is_Itype is wrong because that's
3708                  --  also set in private type cases. We also can't check for
3709                  --  the Scope being exactly Rec because of private types and
3710                  --  record extensions.
3711
3712                  if Is_Itype (Etype (Comp))
3713                    and then Is_Record_Type (Underlying_Type
3714                                               (Scope (Etype (Comp))))
3715                  then
3716                     Undelay_Type (Etype (Comp));
3717                  end if;
3718
3719                  Freeze_And_Append (Etype (Comp), N, Result);
3720
3721                  --  Warn for pragma Pack overriding foreign convention
3722
3723                  if Has_Foreign_Convention (Etype (Comp))
3724                    and then Has_Pragma_Pack (Rec)
3725
3726                    --  Don't warn for aliased components, since override
3727                    --  cannot happen in that case.
3728
3729                    and then not Is_Aliased (Comp)
3730                  then
3731                     declare
3732                        CN : constant Name_Id :=
3733                               Get_Convention_Name (Convention (Etype (Comp)));
3734                        PP : constant Node_Id :=
3735                               Get_Pragma (Rec, Pragma_Pack);
3736                     begin
3737                        if Present (PP) then
3738                           Error_Msg_Name_1 := CN;
3739                           Error_Msg_Sloc := Sloc (Comp);
3740                           Error_Msg_N
3741                             ("pragma Pack affects convention % component#??",
3742                              PP);
3743                           Error_Msg_Name_1 := CN;
3744                           Error_Msg_NE
3745                             ("\component & may not have % compatible "
3746                              & "representation??", PP, Comp);
3747                        end if;
3748                     end;
3749                  end if;
3750
3751                  --  Check for error of component clause given for variable
3752                  --  sized type. We have to delay this test till this point,
3753                  --  since the component type has to be frozen for us to know
3754                  --  if it is variable length.
3755
3756                  if Present (CC) then
3757                     Placed_Component := True;
3758
3759                     --  We omit this test in a generic context, it will be
3760                     --  applied at instantiation time.
3761
3762                     if Inside_A_Generic then
3763                        null;
3764
3765                     --  Also omit this test in CodePeer mode, since we do not
3766                     --  have sufficient info on size and rep clauses.
3767
3768                     elsif CodePeer_Mode then
3769                        null;
3770
3771                     --  Omit check if component has a generic type. This can
3772                     --  happen in an instantiation within a generic in ASIS
3773                     --  mode, where we force freeze actions without full
3774                     --  expansion.
3775
3776                     elsif Is_Generic_Type (Etype (Comp)) then
3777                        null;
3778
3779                     --  Do the check
3780
3781                     elsif not
3782                       Size_Known_At_Compile_Time
3783                         (Underlying_Type (Etype (Comp)))
3784                     then
3785                        Error_Msg_N
3786                          ("component clause not allowed for variable " &
3787                           "length component", CC);
3788                     end if;
3789
3790                  else
3791                     Unplaced_Component := True;
3792                  end if;
3793
3794                  --  Case of component requires byte alignment
3795
3796                  if Must_Be_On_Byte_Boundary (Etype (Comp)) then
3797
3798                     --  Set the enclosing record to also require byte align
3799
3800                     Set_Must_Be_On_Byte_Boundary (Rec);
3801
3802                     --  Check for component clause that is inconsistent with
3803                     --  the required byte boundary alignment.
3804
3805                     if Present (CC)
3806                       and then Normalized_First_Bit (Comp) mod
3807                                  System_Storage_Unit /= 0
3808                     then
3809                        Error_Msg_N
3810                          ("component & must be byte aligned",
3811                           Component_Name (Component_Clause (Comp)));
3812                     end if;
3813                  end if;
3814               end;
3815            end if;
3816
3817            --  Gather data for possible Implicit_Packing later. Note that at
3818            --  this stage we might be dealing with a real component, or with
3819            --  an implicit subtype declaration.
3820
3821            if not Is_Scalar_Type (Etype (Comp)) then
3822               All_Scalar_Components := False;
3823            else
3824               Scalar_Component_Total_RM_Size :=
3825                 Scalar_Component_Total_RM_Size + RM_Size (Etype (Comp));
3826               Scalar_Component_Total_Esize :=
3827                 Scalar_Component_Total_Esize + Esize (Etype (Comp));
3828            end if;
3829
3830            --  If the component is an Itype with Delayed_Freeze and is either
3831            --  a record or array subtype and its base type has not yet been
3832            --  frozen, we must remove this from the entity list of this record
3833            --  and put it on the entity list of the scope of its base type.
3834            --  Note that we know that this is not the type of a component
3835            --  since we cleared Has_Delayed_Freeze for it in the previous
3836            --  loop. Thus this must be the Designated_Type of an access type,
3837            --  which is the type of a component.
3838
3839            if Is_Itype (Comp)
3840              and then Is_Type (Scope (Comp))
3841              and then Is_Composite_Type (Comp)
3842              and then Base_Type (Comp) /= Comp
3843              and then Has_Delayed_Freeze (Comp)
3844              and then not Is_Frozen (Base_Type (Comp))
3845            then
3846               declare
3847                  Will_Be_Frozen : Boolean := False;
3848                  S              : Entity_Id;
3849
3850               begin
3851                  --  We have a difficult case to handle here. Suppose Rec is
3852                  --  subtype being defined in a subprogram that's created as
3853                  --  part of the freezing of Rec'Base. In that case, we know
3854                  --  that Comp'Base must have already been frozen by the time
3855                  --  we get to elaborate this because Gigi doesn't elaborate
3856                  --  any bodies until it has elaborated all of the declarative
3857                  --  part. But Is_Frozen will not be set at this point because
3858                  --  we are processing code in lexical order.
3859
3860                  --  We detect this case by going up the Scope chain of Rec
3861                  --  and seeing if we have a subprogram scope before reaching
3862                  --  the top of the scope chain or that of Comp'Base. If we
3863                  --  do, then mark that Comp'Base will actually be frozen. If
3864                  --  so, we merely undelay it.
3865
3866                  S := Scope (Rec);
3867                  while Present (S) loop
3868                     if Is_Subprogram (S) then
3869                        Will_Be_Frozen := True;
3870                        exit;
3871                     elsif S = Scope (Base_Type (Comp)) then
3872                        exit;
3873                     end if;
3874
3875                     S := Scope (S);
3876                  end loop;
3877
3878                  if Will_Be_Frozen then
3879                     Undelay_Type (Comp);
3880
3881                  else
3882                     if Present (Prev) then
3883                        Set_Next_Entity (Prev, Next_Entity (Comp));
3884                     else
3885                        Set_First_Entity (Rec, Next_Entity (Comp));
3886                     end if;
3887
3888                     --  Insert in entity list of scope of base type (which
3889                     --  must be an enclosing scope, because still unfrozen).
3890
3891                     Append_Entity (Comp, Scope (Base_Type (Comp)));
3892                  end if;
3893               end;
3894
3895            --  If the component is an access type with an allocator as default
3896            --  value, the designated type will be frozen by the corresponding
3897            --  expression in init_proc. In order to place the freeze node for
3898            --  the designated type before that for the current record type,
3899            --  freeze it now.
3900
3901            --  Same process if the component is an array of access types,
3902            --  initialized with an aggregate. If the designated type is
3903            --  private, it cannot contain allocators, and it is premature
3904            --  to freeze the type, so we check for this as well.
3905
3906            elsif Is_Access_Type (Etype (Comp))
3907              and then Present (Parent (Comp))
3908              and then Present (Expression (Parent (Comp)))
3909            then
3910               declare
3911                  Alloc : constant Node_Id :=
3912                            Check_Allocator (Expression (Parent (Comp)));
3913
3914               begin
3915                  if Present (Alloc) then
3916
3917                     --  If component is pointer to a class-wide type, freeze
3918                     --  the specific type in the expression being allocated.
3919                     --  The expression may be a subtype indication, in which
3920                     --  case freeze the subtype mark.
3921
3922                     if Is_Class_Wide_Type
3923                          (Designated_Type (Etype (Comp)))
3924                     then
3925                        if Is_Entity_Name (Expression (Alloc)) then
3926                           Freeze_And_Append
3927                             (Entity (Expression (Alloc)), N, Result);
3928
3929                        elsif Nkind (Expression (Alloc)) = N_Subtype_Indication
3930                        then
3931                           Freeze_And_Append
3932                            (Entity (Subtype_Mark (Expression (Alloc))),
3933                             N, Result);
3934                        end if;
3935
3936                     elsif Is_Itype (Designated_Type (Etype (Comp))) then
3937                        Check_Itype (Etype (Comp));
3938
3939                     else
3940                        Freeze_And_Append
3941                          (Designated_Type (Etype (Comp)), N, Result);
3942                     end if;
3943                  end if;
3944               end;
3945
3946            elsif Is_Access_Type (Etype (Comp))
3947              and then Is_Itype (Designated_Type (Etype (Comp)))
3948            then
3949               Check_Itype (Etype (Comp));
3950
3951            --  Freeze the designated type when initializing a component with
3952            --  an aggregate in case the aggregate contains allocators.
3953
3954            --     type T is ...;
3955            --     type T_Ptr is access all T;
3956            --     type T_Array is array ... of T_Ptr;
3957
3958            --     type Rec is record
3959            --        Comp : T_Array := (others => ...);
3960            --     end record;
3961
3962            elsif Is_Array_Type (Etype (Comp))
3963              and then Is_Access_Type (Component_Type (Etype (Comp)))
3964            then
3965               declare
3966                  Comp_Par  : constant Node_Id   := Parent (Comp);
3967                  Desig_Typ : constant Entity_Id :=
3968                                Designated_Type
3969                                  (Component_Type (Etype (Comp)));
3970
3971               begin
3972                  --  The only case when this sort of freezing is not done is
3973                  --  when the designated type is class-wide and the root type
3974                  --  is the record owning the component. This scenario results
3975                  --  in a circularity because the class-wide type requires
3976                  --  primitives that have not been created yet as the root
3977                  --  type is in the process of being frozen.
3978
3979                  --     type Rec is tagged;
3980                  --     type Rec_Ptr is access all Rec'Class;
3981                  --     type Rec_Array is array ... of Rec_Ptr;
3982
3983                  --     type Rec is record
3984                  --        Comp : Rec_Array := (others => ...);
3985                  --     end record;
3986
3987                  if Is_Class_Wide_Type (Desig_Typ)
3988                    and then Root_Type (Desig_Typ) = Rec
3989                  then
3990                     null;
3991
3992                  elsif Is_Fully_Defined (Desig_Typ)
3993                    and then Present (Comp_Par)
3994                    and then Nkind (Comp_Par) = N_Component_Declaration
3995                    and then Present (Expression (Comp_Par))
3996                    and then Nkind (Expression (Comp_Par)) = N_Aggregate
3997                  then
3998                     Freeze_And_Append (Desig_Typ, N, Result);
3999                  end if;
4000               end;
4001            end if;
4002
4003            Prev := Comp;
4004            Next_Entity (Comp);
4005         end loop;
4006
4007         SSO_ADC :=
4008           Get_Attribute_Definition_Clause
4009             (Rec, Attribute_Scalar_Storage_Order);
4010
4011         --  If the record type has Complex_Representation, then it is treated
4012         --  as a scalar in the back end so the storage order is irrelevant.
4013
4014         if Has_Complex_Representation (Rec) then
4015            if Present (SSO_ADC) then
4016               Error_Msg_N
4017                 ("??storage order has no effect with Complex_Representation",
4018                  SSO_ADC);
4019            end if;
4020
4021         else
4022            --  Deal with default setting of reverse storage order
4023
4024            Set_SSO_From_Default (Rec);
4025
4026            --  Check consistent attribute setting on component types
4027
4028            declare
4029               Comp_ADC_Present : Boolean;
4030            begin
4031               Comp := First_Component (Rec);
4032               while Present (Comp) loop
4033                  Check_Component_Storage_Order
4034                    (Encl_Type        => Rec,
4035                     Comp             => Comp,
4036                     ADC              => SSO_ADC,
4037                     Comp_ADC_Present => Comp_ADC_Present);
4038                  SSO_ADC_Component := SSO_ADC_Component or Comp_ADC_Present;
4039                  Next_Component (Comp);
4040               end loop;
4041            end;
4042
4043            --  Now deal with reverse storage order/bit order issues
4044
4045            if Present (SSO_ADC) then
4046
4047               --  Check compatibility of Scalar_Storage_Order with Bit_Order,
4048               --  if the former is specified.
4049
4050               if Reverse_Bit_Order (Rec) /= Reverse_Storage_Order (Rec) then
4051
4052                  --  Note: report error on Rec, not on SSO_ADC, as ADC may
4053                  --  apply to some ancestor type.
4054
4055                  Error_Msg_Sloc := Sloc (SSO_ADC);
4056                  Error_Msg_N
4057                    ("scalar storage order for& specified# inconsistent with "
4058                     & "bit order", Rec);
4059               end if;
4060
4061               --  Warn if there is a Scalar_Storage_Order attribute definition
4062               --  clause but no component clause, no component that itself has
4063               --  such an attribute definition, and no pragma Pack.
4064
4065               if not (Placed_Component
4066                         or else
4067                       SSO_ADC_Component
4068                         or else
4069                       Is_Packed (Rec))
4070               then
4071                  Error_Msg_N
4072                    ("??scalar storage order specified but no component "
4073                     & "clause", SSO_ADC);
4074               end if;
4075            end if;
4076         end if;
4077
4078         --  Deal with Bit_Order aspect
4079
4080         ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
4081
4082         if Present (ADC) and then Base_Type (Rec) = Rec then
4083            if not (Placed_Component
4084                     or else Present (SSO_ADC)
4085                     or else Is_Packed (Rec))
4086            then
4087               --  Warn if clause has no effect when no component clause is
4088               --  present, but suppress warning if the Bit_Order is required
4089               --  due to the presence of a Scalar_Storage_Order attribute.
4090
4091               Error_Msg_N
4092                 ("??bit order specification has no effect", ADC);
4093               Error_Msg_N
4094                 ("\??since no component clauses were specified", ADC);
4095
4096            --  Here is where we do the processing to adjust component clauses
4097            --  for reversed bit order, when not using reverse SSO.
4098
4099            elsif Reverse_Bit_Order (Rec)
4100              and then not Reverse_Storage_Order (Rec)
4101            then
4102               Adjust_Record_For_Reverse_Bit_Order (Rec);
4103
4104            --  Case where we have both an explicit Bit_Order and the same
4105            --  Scalar_Storage_Order: leave record untouched, the back-end
4106            --  will take care of required layout conversions.
4107
4108            else
4109               null;
4110
4111            end if;
4112         end if;
4113
4114         --  Complete error checking on record representation clause (e.g.
4115         --  overlap of components). This is called after adjusting the
4116         --  record for reverse bit order.
4117
4118         declare
4119            RRC : constant Node_Id := Get_Record_Representation_Clause (Rec);
4120         begin
4121            if Present (RRC) then
4122               Check_Record_Representation_Clause (RRC);
4123            end if;
4124         end;
4125
4126         --  Set OK_To_Reorder_Components depending on debug flags
4127
4128         if Is_Base_Type (Rec) and then Convention (Rec) = Convention_Ada then
4129            if (Has_Discriminants (Rec) and then Debug_Flag_Dot_V)
4130                 or else
4131                   (not Has_Discriminants (Rec) and then Debug_Flag_Dot_R)
4132            then
4133               Set_OK_To_Reorder_Components (Rec);
4134            end if;
4135         end if;
4136
4137         --  Check for useless pragma Pack when all components placed. We only
4138         --  do this check for record types, not subtypes, since a subtype may
4139         --  have all its components placed, and it still makes perfectly good
4140         --  sense to pack other subtypes or the parent type. We do not give
4141         --  this warning if Optimize_Alignment is set to Space, since the
4142         --  pragma Pack does have an effect in this case (it always resets
4143         --  the alignment to one).
4144
4145         if Ekind (Rec) = E_Record_Type
4146           and then Is_Packed (Rec)
4147           and then not Unplaced_Component
4148           and then Optimize_Alignment /= 'S'
4149         then
4150            --  Reset packed status. Probably not necessary, but we do it so
4151            --  that there is no chance of the back end doing something strange
4152            --  with this redundant indication of packing.
4153
4154            Set_Is_Packed (Rec, False);
4155
4156            --  Give warning if redundant constructs warnings on
4157
4158            if Warn_On_Redundant_Constructs then
4159               Error_Msg_N -- CODEFIX
4160                 ("??pragma Pack has no effect, no unplaced components",
4161                  Get_Rep_Pragma (Rec, Name_Pack));
4162            end if;
4163         end if;
4164
4165         --  If this is the record corresponding to a remote type, freeze the
4166         --  remote type here since that is what we are semantically freezing.
4167         --  This prevents the freeze node for that type in an inner scope.
4168
4169         if Ekind (Rec) = E_Record_Type then
4170            if Present (Corresponding_Remote_Type (Rec)) then
4171               Freeze_And_Append (Corresponding_Remote_Type (Rec), N, Result);
4172            end if;
4173
4174            --  Check for controlled components and unchecked unions.
4175
4176            Comp := First_Component (Rec);
4177            while Present (Comp) loop
4178
4179               --  Do not set Has_Controlled_Component on a class-wide
4180               --  equivalent type. See Make_CW_Equivalent_Type.
4181
4182               if not Is_Class_Wide_Equivalent_Type (Rec)
4183                 and then
4184                   (Has_Controlled_Component (Etype (Comp))
4185                     or else
4186                       (Chars (Comp) /= Name_uParent
4187                         and then Is_Controlled_Active (Etype (Comp)))
4188                     or else
4189                       (Is_Protected_Type (Etype (Comp))
4190                         and then
4191                           Present (Corresponding_Record_Type (Etype (Comp)))
4192                         and then
4193                           Has_Controlled_Component
4194                             (Corresponding_Record_Type (Etype (Comp)))))
4195               then
4196                  Set_Has_Controlled_Component (Rec);
4197               end if;
4198
4199               if Has_Unchecked_Union (Etype (Comp)) then
4200                  Set_Has_Unchecked_Union (Rec);
4201               end if;
4202
4203               --  Scan component declaration for likely misuses of current
4204               --  instance, either in a constraint or a default expression.
4205
4206               if Has_Per_Object_Constraint (Comp) then
4207                  Check_Current_Instance (Parent (Comp));
4208               end if;
4209
4210               Next_Component (Comp);
4211            end loop;
4212         end if;
4213
4214         --  Enforce the restriction that access attributes with a current
4215         --  instance prefix can only apply to limited types. This comment
4216         --  is floating here, but does not seem to belong here???
4217
4218         --  Set component alignment if not otherwise already set
4219
4220         Set_Component_Alignment_If_Not_Set (Rec);
4221
4222         --  For first subtypes, check if there are any fixed-point fields with
4223         --  component clauses, where we must check the size. This is not done
4224         --  till the freeze point since for fixed-point types, we do not know
4225         --  the size until the type is frozen. Similar processing applies to
4226         --  bit packed arrays.
4227
4228         if Is_First_Subtype (Rec) then
4229            Comp := First_Component (Rec);
4230            while Present (Comp) loop
4231               if Present (Component_Clause (Comp))
4232                 and then (Is_Fixed_Point_Type (Etype (Comp))
4233                            or else Is_Bit_Packed_Array (Etype (Comp)))
4234               then
4235                  Check_Size
4236                    (Component_Name (Component_Clause (Comp)),
4237                     Etype (Comp),
4238                     Esize (Comp),
4239                     Junk);
4240               end if;
4241
4242               Next_Component (Comp);
4243            end loop;
4244         end if;
4245
4246         --  Generate warning for applying C or C++ convention to a record
4247         --  with discriminants. This is suppressed for the unchecked union
4248         --  case, since the whole point in this case is interface C. We also
4249         --  do not generate this within instantiations, since we will have
4250         --  generated a message on the template.
4251
4252         if Has_Discriminants (E)
4253           and then not Is_Unchecked_Union (E)
4254           and then (Convention (E) = Convention_C
4255                       or else
4256                     Convention (E) = Convention_CPP)
4257           and then Comes_From_Source (E)
4258           and then not In_Instance
4259           and then not Has_Warnings_Off (E)
4260           and then not Has_Warnings_Off (Base_Type (E))
4261         then
4262            declare
4263               Cprag : constant Node_Id := Get_Rep_Pragma (E, Name_Convention);
4264               A2    : Node_Id;
4265
4266            begin
4267               if Present (Cprag) then
4268                  A2 := Next (First (Pragma_Argument_Associations (Cprag)));
4269
4270                  if Convention (E) = Convention_C then
4271                     Error_Msg_N
4272                       ("?x?variant record has no direct equivalent in C",
4273                        A2);
4274                  else
4275                     Error_Msg_N
4276                       ("?x?variant record has no direct equivalent in C++",
4277                        A2);
4278                  end if;
4279
4280                  Error_Msg_NE
4281                    ("\?x?use of convention for type& is dubious", A2, E);
4282               end if;
4283            end;
4284         end if;
4285
4286         --  See if Size is too small as is (and implicit packing might help)
4287
4288         if not Is_Packed (Rec)
4289
4290           --  No implicit packing if even one component is explicitly placed
4291
4292           and then not Placed_Component
4293
4294           --  Or even one component is aliased
4295
4296           and then not Aliased_Component
4297
4298           --  Must have size clause and all scalar components
4299
4300           and then Has_Size_Clause (Rec)
4301           and then All_Scalar_Components
4302
4303           --  Do not try implicit packing on records with discriminants, too
4304           --  complicated, especially in the variant record case.
4305
4306           and then not Has_Discriminants (Rec)
4307
4308           --  We can implicitly pack if the specified size of the record is
4309           --  less than the sum of the object sizes (no point in packing if
4310           --  this is not the case).
4311
4312           and then RM_Size (Rec) < Scalar_Component_Total_Esize
4313
4314           --  And the total RM size cannot be greater than the specified size
4315           --  since otherwise packing will not get us where we have to be.
4316
4317           and then RM_Size (Rec) >= Scalar_Component_Total_RM_Size
4318
4319           --  Never do implicit packing in CodePeer or SPARK modes since
4320           --  we don't do any packing in these modes, since this generates
4321           --  over-complex code that confuses static analysis, and in
4322           --  general, neither CodePeer not GNATprove care about the
4323           --  internal representation of objects.
4324
4325           and then not (CodePeer_Mode or GNATprove_Mode)
4326         then
4327            --  If implicit packing enabled, do it
4328
4329            if Implicit_Packing then
4330               Set_Is_Packed (Rec);
4331
4332               --  Otherwise flag the size clause
4333
4334            else
4335               declare
4336                  Sz : constant Node_Id := Size_Clause (Rec);
4337               begin
4338                  Error_Msg_NE -- CODEFIX
4339                    ("size given for& too small", Sz, Rec);
4340                  Error_Msg_N -- CODEFIX
4341                    ("\use explicit pragma Pack "
4342                     & "or use pragma Implicit_Packing", Sz);
4343               end;
4344            end if;
4345         end if;
4346
4347         --  The following checks are relevant only when SPARK_Mode is on as
4348         --  they are not standard Ada legality rules.
4349
4350         if SPARK_Mode = On then
4351            if Is_Effectively_Volatile (Rec) then
4352
4353               --  A discriminated type cannot be effectively volatile
4354               --  (SPARK RM C.6(4)).
4355
4356               if Has_Discriminants (Rec) then
4357                  Error_Msg_N ("discriminated type & cannot be volatile", Rec);
4358
4359               --  A tagged type cannot be effectively volatile
4360               --  (SPARK RM C.6(5)).
4361
4362               elsif Is_Tagged_Type (Rec) then
4363                  Error_Msg_N ("tagged type & cannot be volatile", Rec);
4364               end if;
4365
4366            --  A non-effectively volatile record type cannot contain
4367            --  effectively volatile components (SPARK RM C.6(2)).
4368
4369            else
4370               Comp := First_Component (Rec);
4371               while Present (Comp) loop
4372                  if Comes_From_Source (Comp)
4373                    and then Is_Effectively_Volatile (Etype (Comp))
4374                  then
4375                     Error_Msg_Name_1 := Chars (Rec);
4376                     Error_Msg_N
4377                       ("component & of non-volatile type % cannot be "
4378                        & "volatile", Comp);
4379                  end if;
4380
4381                  Next_Component (Comp);
4382               end loop;
4383            end if;
4384
4385            --  A type which does not yield a synchronized object cannot have
4386            --  a component that yields a synchronized object (SPARK RM 9.5).
4387
4388            if not Yields_Synchronized_Object (Rec) then
4389               Comp := First_Component (Rec);
4390               while Present (Comp) loop
4391                  if Comes_From_Source (Comp)
4392                    and then Yields_Synchronized_Object (Etype (Comp))
4393                  then
4394                     Error_Msg_Name_1 := Chars (Rec);
4395                     Error_Msg_N
4396                       ("component & of non-synchronized type % cannot be "
4397                        & "synchronized", Comp);
4398                  end if;
4399
4400                  Next_Component (Comp);
4401               end loop;
4402            end if;
4403
4404            --  A Ghost type cannot have a component of protected or task type
4405            --  (SPARK RM 6.9(19)).
4406
4407            if Is_Ghost_Entity (Rec) then
4408               Comp := First_Component (Rec);
4409               while Present (Comp) loop
4410                  if Comes_From_Source (Comp)
4411                    and then Is_Concurrent_Type (Etype (Comp))
4412                  then
4413                     Error_Msg_Name_1 := Chars (Rec);
4414                     Error_Msg_N
4415                       ("component & of ghost type % cannot be concurrent",
4416                        Comp);
4417                  end if;
4418
4419                  Next_Component (Comp);
4420               end loop;
4421            end if;
4422         end if;
4423
4424         --  Make sure that if we have an iterator aspect, then we have
4425         --  either Constant_Indexing or Variable_Indexing.
4426
4427         declare
4428            Iterator_Aspect : Node_Id;
4429
4430         begin
4431            Iterator_Aspect := Find_Aspect (Rec, Aspect_Iterator_Element);
4432
4433            if No (Iterator_Aspect) then
4434               Iterator_Aspect := Find_Aspect (Rec, Aspect_Default_Iterator);
4435            end if;
4436
4437            if Present (Iterator_Aspect) then
4438               if Has_Aspect (Rec, Aspect_Constant_Indexing)
4439                    or else
4440                  Has_Aspect (Rec, Aspect_Variable_Indexing)
4441               then
4442                  null;
4443               else
4444                  Error_Msg_N
4445                    ("Iterator_Element requires indexing aspect",
4446                     Iterator_Aspect);
4447               end if;
4448            end if;
4449         end;
4450
4451         --  All done if not a full record definition
4452
4453         if Ekind (Rec) /= E_Record_Type then
4454            return;
4455         end if;
4456
4457         --  Finally we need to check the variant part to make sure that
4458         --  all types within choices are properly frozen as part of the
4459         --  freezing of the record type.
4460
4461         Check_Variant_Part : declare
4462            D : constant Node_Id := Declaration_Node (Rec);
4463            T : Node_Id;
4464            C : Node_Id;
4465
4466         begin
4467            --  Find component list
4468
4469            C := Empty;
4470
4471            if Nkind (D) = N_Full_Type_Declaration then
4472               T := Type_Definition (D);
4473
4474               if Nkind (T) = N_Record_Definition then
4475                  C := Component_List (T);
4476
4477               elsif Nkind (T) = N_Derived_Type_Definition
4478                 and then Present (Record_Extension_Part (T))
4479               then
4480                  C := Component_List (Record_Extension_Part (T));
4481               end if;
4482            end if;
4483
4484            --  Case of variant part present
4485
4486            if Present (C) and then Present (Variant_Part (C)) then
4487               Freeze_Choices_In_Variant_Part (Variant_Part (C));
4488            end if;
4489
4490            --  Note: we used to call Check_Choices here, but it is too early,
4491            --  since predicated subtypes are frozen here, but their freezing
4492            --  actions are in Analyze_Freeze_Entity, which has not been called
4493            --  yet for entities frozen within this procedure, so we moved that
4494            --  call to the Analyze_Freeze_Entity for the record type.
4495
4496         end Check_Variant_Part;
4497
4498         --  Check that all the primitives of an interface type are abstract
4499         --  or null procedures.
4500
4501         if Is_Interface (Rec)
4502           and then not Error_Posted (Parent (Rec))
4503         then
4504            declare
4505               Elmt : Elmt_Id;
4506               Subp : Entity_Id;
4507
4508            begin
4509               Elmt := First_Elmt (Primitive_Operations (Rec));
4510               while Present (Elmt) loop
4511                  Subp := Node (Elmt);
4512
4513                  if not Is_Abstract_Subprogram (Subp)
4514
4515                     --  Avoid reporting the error on inherited primitives
4516
4517                    and then Comes_From_Source (Subp)
4518                  then
4519                     Error_Msg_Name_1 := Chars (Subp);
4520
4521                     if Ekind (Subp) = E_Procedure then
4522                        if not Null_Present (Parent (Subp)) then
4523                           Error_Msg_N
4524                             ("interface procedure % must be abstract or null",
4525                              Parent (Subp));
4526                        end if;
4527                     else
4528                        Error_Msg_N
4529                          ("interface function % must be abstract",
4530                           Parent (Subp));
4531                     end if;
4532                  end if;
4533
4534                  Next_Elmt (Elmt);
4535               end loop;
4536            end;
4537         end if;
4538      end Freeze_Record_Type;
4539
4540      -------------------------------
4541      -- Has_Boolean_Aspect_Import --
4542      -------------------------------
4543
4544      function Has_Boolean_Aspect_Import (E : Entity_Id) return Boolean is
4545         Decl : constant Node_Id := Declaration_Node (E);
4546         Asp  : Node_Id;
4547         Expr : Node_Id;
4548
4549      begin
4550         if Has_Aspects (Decl) then
4551            Asp := First (Aspect_Specifications (Decl));
4552            while Present (Asp) loop
4553               Expr := Expression (Asp);
4554
4555               --  The value of aspect Import is True when the expression is
4556               --  either missing or it is explicitly set to True.
4557
4558               if Get_Aspect_Id (Asp) = Aspect_Import
4559                 and then (No (Expr)
4560                            or else (Compile_Time_Known_Value (Expr)
4561                                      and then Is_True (Expr_Value (Expr))))
4562               then
4563                  return True;
4564               end if;
4565
4566               Next (Asp);
4567            end loop;
4568         end if;
4569
4570         return False;
4571      end Has_Boolean_Aspect_Import;
4572
4573      ----------------------------
4574      -- Late_Freeze_Subprogram --
4575      ----------------------------
4576
4577      procedure Late_Freeze_Subprogram (E : Entity_Id) is
4578         Spec  : constant Node_Id :=
4579                   Specification (Unit_Declaration_Node (Scope (E)));
4580         Decls : List_Id;
4581
4582      begin
4583         if Present (Private_Declarations (Spec)) then
4584            Decls := Private_Declarations (Spec);
4585         else
4586            Decls := Visible_Declarations (Spec);
4587         end if;
4588
4589         Append_List (Result, Decls);
4590      end Late_Freeze_Subprogram;
4591
4592      ------------------------------
4593      -- Wrap_Imported_Subprogram --
4594      ------------------------------
4595
4596      --  The issue here is that our normal approach of checking preconditions
4597      --  and postconditions does not work for imported procedures, since we
4598      --  are not generating code for the body. To get around this we create
4599      --  a wrapper, as shown by the following example:
4600
4601      --    procedure K (A : Integer);
4602      --    pragma Import (C, K);
4603
4604      --  The spec is rewritten by removing the effects of pragma Import, but
4605      --  leaving the convention unchanged, as though the source had said:
4606
4607      --    procedure K (A : Integer);
4608      --    pragma Convention (C, K);
4609
4610      --  and we create a body, added to the entity K freeze actions, which
4611      --  looks like:
4612
4613      --    procedure K (A : Integer) is
4614      --       procedure K (A : Integer);
4615      --       pragma Import (C, K);
4616      --    begin
4617      --       K (A);
4618      --    end K;
4619
4620      --  Now the contract applies in the normal way to the outer procedure,
4621      --  and the inner procedure has no contracts, so there is no problem
4622      --  in just calling it to get the original effect.
4623
4624      --  In the case of a function, we create an appropriate return statement
4625      --  for the subprogram body that calls the inner procedure.
4626
4627      procedure Wrap_Imported_Subprogram (E : Entity_Id) is
4628         Loc   : constant Source_Ptr := Sloc (E);
4629         CE    : constant Name_Id    := Chars (E);
4630         Spec  : Node_Id;
4631         Parms : List_Id;
4632         Stmt  : Node_Id;
4633         Iprag : Node_Id;
4634         Bod   : Node_Id;
4635         Forml : Entity_Id;
4636
4637      begin
4638         --  Nothing to do if not imported
4639
4640         if not Is_Imported (E) then
4641            return;
4642
4643         --  Test enabling conditions for wrapping
4644
4645         elsif Is_Subprogram (E)
4646           and then Present (Contract (E))
4647           and then Present (Pre_Post_Conditions (Contract (E)))
4648           and then not GNATprove_Mode
4649         then
4650            --  Here we do the wrap
4651
4652            --  Note on calls to Copy_Separate_Tree. The trees we are copying
4653            --  here are fully analyzed, but we definitely want fully syntactic
4654            --  unanalyzed trees in the body we construct, so that the analysis
4655            --  generates the right visibility, and that is exactly what the
4656            --  calls to Copy_Separate_Tree give us.
4657
4658            --  Acquire copy of Inline pragma, and indicate that it does not
4659            --  come from an aspect, as it applies to an internal entity.
4660
4661            Iprag := Copy_Separate_Tree (Import_Pragma (E));
4662            Set_From_Aspect_Specification (Iprag, False);
4663
4664            --  Fix up spec to be not imported any more
4665
4666            Set_Is_Imported    (E, False);
4667            Set_Interface_Name (E, Empty);
4668            Set_Has_Completion (E, False);
4669            Set_Import_Pragma  (E, Empty);
4670
4671            --  Grab the subprogram declaration and specification
4672
4673            Spec := Declaration_Node (E);
4674
4675            --  Build parameter list that we need
4676
4677            Parms := New_List;
4678            Forml := First_Formal (E);
4679            while Present (Forml) loop
4680               Append_To (Parms, Make_Identifier (Loc, Chars (Forml)));
4681               Next_Formal (Forml);
4682            end loop;
4683
4684            --  Build the call
4685
4686            if Ekind_In (E, E_Function, E_Generic_Function) then
4687               Stmt :=
4688                 Make_Simple_Return_Statement (Loc,
4689                   Expression =>
4690                     Make_Function_Call (Loc,
4691                       Name                   => Make_Identifier (Loc, CE),
4692                       Parameter_Associations => Parms));
4693
4694            else
4695               Stmt :=
4696                 Make_Procedure_Call_Statement (Loc,
4697                   Name                   => Make_Identifier (Loc, CE),
4698                   Parameter_Associations => Parms);
4699            end if;
4700
4701            --  Now build the body
4702
4703            Bod :=
4704              Make_Subprogram_Body (Loc,
4705                Specification              =>
4706                  Copy_Separate_Tree (Spec),
4707                Declarations               => New_List (
4708                  Make_Subprogram_Declaration (Loc,
4709                    Specification =>
4710                      Copy_Separate_Tree (Spec)),
4711                    Iprag),
4712                Handled_Statement_Sequence =>
4713                  Make_Handled_Sequence_Of_Statements (Loc,
4714                    Statements             => New_List (Stmt),
4715                    End_Label              => Make_Identifier (Loc, CE)));
4716
4717            --  Append the body to freeze result
4718
4719            Add_To_Result (Bod);
4720            return;
4721
4722         --  Case of imported subprogram that does not get wrapped
4723
4724         else
4725            --  Set Is_Public. All imported entities need an external symbol
4726            --  created for them since they are always referenced from another
4727            --  object file. Note this used to be set when we set Is_Imported
4728            --  back in Sem_Prag, but now we delay it to this point, since we
4729            --  don't want to set this flag if we wrap an imported subprogram.
4730
4731            Set_Is_Public (E);
4732         end if;
4733      end Wrap_Imported_Subprogram;
4734
4735      --  Local variables
4736
4737      Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
4738
4739   --  Start of processing for Freeze_Entity
4740
4741   begin
4742      --  The entity being frozen may be subject to pragma Ghost. Set the mode
4743      --  now to ensure that any nodes generated during freezing are properly
4744      --  flagged as Ghost.
4745
4746      Set_Ghost_Mode_From_Entity (E);
4747
4748      --  We are going to test for various reasons why this entity need not be
4749      --  frozen here, but in the case of an Itype that's defined within a
4750      --  record, that test actually applies to the record.
4751
4752      if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
4753         Test_E := Scope (E);
4754      elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
4755        and then Is_Record_Type (Underlying_Type (Scope (E)))
4756      then
4757         Test_E := Underlying_Type (Scope (E));
4758      end if;
4759
4760      --  Do not freeze if already frozen since we only need one freeze node
4761
4762      if Is_Frozen (E) then
4763         Ghost_Mode := Save_Ghost_Mode;
4764         return No_List;
4765
4766      --  It is improper to freeze an external entity within a generic because
4767      --  its freeze node will appear in a non-valid context. The entity will
4768      --  be frozen in the proper scope after the current generic is analyzed.
4769      --  However, aspects must be analyzed because they may be queried later
4770      --  within the generic itself, and the corresponding pragma or attribute
4771      --  definition has not been analyzed yet.
4772
4773      elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
4774         if Has_Delayed_Aspects (E) then
4775            Analyze_Aspects_At_Freeze_Point (E);
4776         end if;
4777
4778         Ghost_Mode := Save_Ghost_Mode;
4779         return No_List;
4780
4781      --  AI05-0213: A formal incomplete type does not freeze the actual. In
4782      --  the instance, the same applies to the subtype renaming the actual.
4783
4784      elsif Is_Private_Type (E)
4785        and then Is_Generic_Actual_Type (E)
4786        and then No (Full_View (Base_Type (E)))
4787        and then Ada_Version >= Ada_2012
4788      then
4789         Ghost_Mode := Save_Ghost_Mode;
4790         return No_List;
4791
4792      --  Formal subprograms are never frozen
4793
4794      elsif Is_Formal_Subprogram (E) then
4795         Ghost_Mode := Save_Ghost_Mode;
4796         return No_List;
4797
4798      --  Generic types are never frozen as they lack delayed semantic checks
4799
4800      elsif Is_Generic_Type (E) then
4801         Ghost_Mode := Save_Ghost_Mode;
4802         return No_List;
4803
4804      --  Do not freeze a global entity within an inner scope created during
4805      --  expansion. A call to subprogram E within some internal procedure
4806      --  (a stream attribute for example) might require freezing E, but the
4807      --  freeze node must appear in the same declarative part as E itself.
4808      --  The two-pass elaboration mechanism in gigi guarantees that E will
4809      --  be frozen before the inner call is elaborated. We exclude constants
4810      --  from this test, because deferred constants may be frozen early, and
4811      --  must be diagnosed (e.g. in the case of a deferred constant being used
4812      --  in a default expression). If the enclosing subprogram comes from
4813      --  source, or is a generic instance, then the freeze point is the one
4814      --  mandated by the language, and we freeze the entity. A subprogram that
4815      --  is a child unit body that acts as a spec does not have a spec that
4816      --  comes from source, but can only come from source.
4817
4818      elsif In_Open_Scopes (Scope (Test_E))
4819        and then Scope (Test_E) /= Current_Scope
4820        and then Ekind (Test_E) /= E_Constant
4821      then
4822         declare
4823            S : Entity_Id;
4824
4825         begin
4826            S := Current_Scope;
4827            while Present (S) loop
4828               if Is_Overloadable (S) then
4829                  if Comes_From_Source (S)
4830                    or else Is_Generic_Instance (S)
4831                    or else Is_Child_Unit (S)
4832                  then
4833                     exit;
4834                  else
4835                     Ghost_Mode := Save_Ghost_Mode;
4836                     return No_List;
4837                  end if;
4838               end if;
4839
4840               S := Scope (S);
4841            end loop;
4842         end;
4843
4844      --  Similarly, an inlined instance body may make reference to global
4845      --  entities, but these references cannot be the proper freezing point
4846      --  for them, and in the absence of inlining freezing will take place in
4847      --  their own scope. Normally instance bodies are analyzed after the
4848      --  enclosing compilation, and everything has been frozen at the proper
4849      --  place, but with front-end inlining an instance body is compiled
4850      --  before the end of the enclosing scope, and as a result out-of-order
4851      --  freezing must be prevented.
4852
4853      elsif Front_End_Inlining
4854        and then In_Instance_Body
4855        and then Present (Scope (Test_E))
4856      then
4857         declare
4858            S : Entity_Id;
4859
4860         begin
4861            S := Scope (Test_E);
4862            while Present (S) loop
4863               if Is_Generic_Instance (S) then
4864                  exit;
4865               else
4866                  S := Scope (S);
4867               end if;
4868            end loop;
4869
4870            if No (S) then
4871               Ghost_Mode := Save_Ghost_Mode;
4872               return No_List;
4873            end if;
4874         end;
4875
4876      elsif Ekind (E) = E_Generic_Package then
4877         Result := Freeze_Generic_Entities (E);
4878
4879         Ghost_Mode := Save_Ghost_Mode;
4880         return Result;
4881      end if;
4882
4883      --  Add checks to detect proper initialization of scalars that may appear
4884      --  as subprogram parameters.
4885
4886      if Is_Subprogram (E) and then Check_Validity_Of_Parameters then
4887         Apply_Parameter_Validity_Checks (E);
4888      end if;
4889
4890      --  Deal with delayed aspect specifications. The analysis of the aspect
4891      --  is required to be delayed to the freeze point, thus we analyze the
4892      --  pragma or attribute definition clause in the tree at this point. We
4893      --  also analyze the aspect specification node at the freeze point when
4894      --  the aspect doesn't correspond to pragma/attribute definition clause.
4895
4896      if Has_Delayed_Aspects (E) then
4897         Analyze_Aspects_At_Freeze_Point (E);
4898      end if;
4899
4900      --  Here to freeze the entity
4901
4902      Set_Is_Frozen (E);
4903
4904      --  Case of entity being frozen is other than a type
4905
4906      if not Is_Type (E) then
4907
4908         --  If entity is exported or imported and does not have an external
4909         --  name, now is the time to provide the appropriate default name.
4910         --  Skip this if the entity is stubbed, since we don't need a name
4911         --  for any stubbed routine. For the case on intrinsics, if no
4912         --  external name is specified, then calls will be handled in
4913         --  Exp_Intr.Expand_Intrinsic_Call, and no name is needed. If an
4914         --  external name is provided, then Expand_Intrinsic_Call leaves
4915         --  calls in place for expansion by GIGI.
4916
4917         if (Is_Imported (E) or else Is_Exported (E))
4918           and then No (Interface_Name (E))
4919           and then Convention (E) /= Convention_Stubbed
4920           and then Convention (E) /= Convention_Intrinsic
4921         then
4922            Set_Encoded_Interface_Name
4923              (E, Get_Default_External_Name (E));
4924
4925         --  If entity is an atomic object appearing in a declaration and
4926         --  the expression is an aggregate, assign it to a temporary to
4927         --  ensure that the actual assignment is done atomically rather
4928         --  than component-wise (the assignment to the temp may be done
4929         --  component-wise, but that is harmless).
4930
4931         elsif Is_Atomic_Or_VFA (E)
4932           and then Nkind (Parent (E)) = N_Object_Declaration
4933           and then Present (Expression (Parent (E)))
4934           and then Nkind (Expression (Parent (E))) = N_Aggregate
4935           and then Is_Atomic_VFA_Aggregate (Expression (Parent (E)))
4936         then
4937            null;
4938         end if;
4939
4940         --  Subprogram case
4941
4942         if Is_Subprogram (E) then
4943
4944            --  Check for needing to wrap imported subprogram
4945
4946            Wrap_Imported_Subprogram (E);
4947
4948            --  Freeze all parameter types and the return type (RM 13.14(14)).
4949            --  However skip this for internal subprograms. This is also where
4950            --  any extra formal parameters are created since we now know
4951            --  whether the subprogram will use a foreign convention.
4952
4953            --  In Ada 2012, freezing a subprogram does not always freeze
4954            --  the corresponding profile (see AI05-019). An attribute
4955            --  reference is not a freezing point of the profile.
4956            --  Other constructs that should not freeze ???
4957
4958            --  This processing doesn't apply to internal entities (see below)
4959
4960            if not Is_Internal (E) then
4961               if not Freeze_Profile (E) then
4962                  Ghost_Mode := Save_Ghost_Mode;
4963                  return Result;
4964               end if;
4965            end if;
4966
4967            --  Must freeze its parent first if it is a derived subprogram
4968
4969            if Present (Alias (E)) then
4970               Freeze_And_Append (Alias (E), N, Result);
4971            end if;
4972
4973            --  We don't freeze internal subprograms, because we don't normally
4974            --  want addition of extra formals or mechanism setting to happen
4975            --  for those. However we do pass through predefined dispatching
4976            --  cases, since extra formals may be needed in some cases, such as
4977            --  for the stream 'Input function (build-in-place formals).
4978
4979            if not Is_Internal (E)
4980              or else Is_Predefined_Dispatching_Operation (E)
4981            then
4982               Freeze_Subprogram (E);
4983            end if;
4984
4985            if Late_Freezing then
4986               Late_Freeze_Subprogram (E);
4987               Ghost_Mode := Save_Ghost_Mode;
4988               return No_List;
4989            end if;
4990
4991            --  If warning on suspicious contracts then check for the case of
4992            --  a postcondition other than False for a No_Return subprogram.
4993
4994            if No_Return (E)
4995              and then Warn_On_Suspicious_Contract
4996              and then Present (Contract (E))
4997            then
4998               declare
4999                  Prag : Node_Id := Pre_Post_Conditions (Contract (E));
5000                  Exp  : Node_Id;
5001
5002               begin
5003                  while Present (Prag) loop
5004                     if Nam_In (Pragma_Name (Prag), Name_Post,
5005                                                    Name_Postcondition,
5006                                                    Name_Refined_Post)
5007                     then
5008                        Exp :=
5009                          Expression
5010                            (First (Pragma_Argument_Associations (Prag)));
5011
5012                        if Nkind (Exp) /= N_Identifier
5013                          or else Chars (Exp) /= Name_False
5014                        then
5015                           Error_Msg_NE
5016                             ("useless postcondition, & is marked "
5017                              & "No_Return?T?", Exp, E);
5018                        end if;
5019                     end if;
5020
5021                     Prag := Next_Pragma (Prag);
5022                  end loop;
5023               end;
5024            end if;
5025
5026         --  Here for other than a subprogram or type
5027
5028         else
5029            --  If entity has a type, and it is not a generic unit, then
5030            --  freeze it first (RM 13.14(10)).
5031
5032            if Present (Etype (E))
5033              and then Ekind (E) /= E_Generic_Function
5034            then
5035               Freeze_And_Append (Etype (E), N, Result);
5036
5037               --  For an object of an anonymous array type, aspects on the
5038               --  object declaration apply to the type itself. This is the
5039               --  case for Atomic_Components, Volatile_Components, and
5040               --  Independent_Components. In these cases analysis of the
5041               --  generated pragma will mark the anonymous types accordingly,
5042               --  and the object itself does not require a freeze node.
5043
5044               if Ekind (E) = E_Variable
5045                 and then Is_Itype (Etype (E))
5046                 and then Is_Array_Type (Etype (E))
5047                 and then Has_Delayed_Aspects (E)
5048               then
5049                  Set_Has_Delayed_Aspects (E, False);
5050                  Set_Has_Delayed_Freeze (E, False);
5051                  Set_Freeze_Node (E, Empty);
5052               end if;
5053            end if;
5054
5055            --  Special processing for objects created by object declaration
5056
5057            if Nkind (Declaration_Node (E)) = N_Object_Declaration then
5058               Freeze_Object_Declaration (E);
5059            end if;
5060
5061            --  Check that a constant which has a pragma Volatile[_Components]
5062            --  or Atomic[_Components] also has a pragma Import (RM C.6(13)).
5063
5064            --  Note: Atomic[_Components] also sets Volatile[_Components]
5065
5066            if Ekind (E) = E_Constant
5067              and then (Has_Volatile_Components (E) or else Is_Volatile (E))
5068              and then not Is_Imported (E)
5069              and then not Has_Boolean_Aspect_Import (E)
5070            then
5071               --  Make sure we actually have a pragma, and have not merely
5072               --  inherited the indication from elsewhere (e.g. an address
5073               --  clause, which is not good enough in RM terms).
5074
5075               if Has_Rep_Pragma (E, Name_Atomic)
5076                    or else
5077                  Has_Rep_Pragma (E, Name_Atomic_Components)
5078               then
5079                  Error_Msg_N
5080                    ("stand alone atomic constant must be " &
5081                     "imported (RM C.6(13))", E);
5082
5083               elsif Has_Rep_Pragma (E, Name_Volatile)
5084                       or else
5085                     Has_Rep_Pragma (E, Name_Volatile_Components)
5086               then
5087                  Error_Msg_N
5088                    ("stand alone volatile constant must be " &
5089                     "imported (RM C.6(13))", E);
5090               end if;
5091            end if;
5092
5093            --  Static objects require special handling
5094
5095            if (Ekind (E) = E_Constant or else Ekind (E) = E_Variable)
5096              and then Is_Statically_Allocated (E)
5097            then
5098               Freeze_Static_Object (E);
5099            end if;
5100
5101            --  Remaining step is to layout objects
5102
5103            if Ekind_In (E, E_Variable, E_Constant, E_Loop_Parameter)
5104              or else Is_Formal (E)
5105            then
5106               Layout_Object (E);
5107            end if;
5108
5109            --  For an object that does not have delayed freezing, and whose
5110            --  initialization actions have been captured in a compound
5111            --  statement, move them back now directly within the enclosing
5112            --  statement sequence.
5113
5114            if Ekind_In (E, E_Constant, E_Variable)
5115              and then not Has_Delayed_Freeze (E)
5116            then
5117               Explode_Initialization_Compound_Statement (E);
5118            end if;
5119         end if;
5120
5121      --  Case of a type or subtype being frozen
5122
5123      else
5124         --  We used to check here that a full type must have preelaborable
5125         --  initialization if it completes a private type specified with
5126         --  pragma Preelaborable_Initialization, but that missed cases where
5127         --  the types occur within a generic package, since the freezing
5128         --  that occurs within a containing scope generally skips traversal
5129         --  of a generic unit's declarations (those will be frozen within
5130         --  instances). This check was moved to Analyze_Package_Specification.
5131
5132         --  The type may be defined in a generic unit. This can occur when
5133         --  freezing a generic function that returns the type (which is
5134         --  defined in a parent unit). It is clearly meaningless to freeze
5135         --  this type. However, if it is a subtype, its size may be determi-
5136         --  nable and used in subsequent checks, so might as well try to
5137         --  compute it.
5138
5139         --  In Ada 2012, Freeze_Entities is also used in the front end to
5140         --  trigger the analysis of aspect expressions, so in this case we
5141         --  want to continue the freezing process.
5142
5143         if Present (Scope (E))
5144           and then Is_Generic_Unit (Scope (E))
5145           and then
5146             (not Has_Predicates (E)
5147               and then not Has_Delayed_Freeze (E))
5148         then
5149            Check_Compile_Time_Size (E);
5150            Ghost_Mode := Save_Ghost_Mode;
5151            return No_List;
5152         end if;
5153
5154         --  Check for error of Type_Invariant'Class applied to an untagged
5155         --  type (check delayed to freeze time when full type is available).
5156
5157         declare
5158            Prag : constant Node_Id := Get_Pragma (E, Pragma_Invariant);
5159         begin
5160            if Present (Prag)
5161              and then Class_Present (Prag)
5162              and then not Is_Tagged_Type (E)
5163            then
5164               Error_Msg_NE
5165                 ("Type_Invariant''Class cannot be specified for &",
5166                  Prag, E);
5167               Error_Msg_N
5168                 ("\can only be specified for a tagged type", Prag);
5169            end if;
5170         end;
5171
5172         if Is_Ghost_Entity (E) then
5173
5174            --  A Ghost type cannot be concurrent (SPARK RM 6.9(19)). Verify
5175            --  this legality rule first to five a finer-grained diagnostic.
5176
5177            if Is_Concurrent_Type (E) then
5178               Error_Msg_N ("ghost type & cannot be concurrent", E);
5179
5180            --  A Ghost type cannot be effectively volatile (SPARK RM 6.9(8))
5181
5182            elsif Is_Effectively_Volatile (E) then
5183               Error_Msg_N ("ghost type & cannot be volatile", E);
5184            end if;
5185         end if;
5186
5187         --  Deal with special cases of freezing for subtype
5188
5189         if E /= Base_Type (E) then
5190
5191            --  Before we do anything else, a specialized test for the case of
5192            --  a size given for an array where the array needs to be packed,
5193            --  but was not so the size cannot be honored. This is the case
5194            --  where implicit packing may apply. The reason we do this so
5195            --  early is that if we have implicit packing, the layout of the
5196            --  base type is affected, so we must do this before we freeze
5197            --  the base type.
5198
5199            --  We could do this processing only if implicit packing is enabled
5200            --  since in all other cases, the error would be caught by the back
5201            --  end. However, we choose to do the check even if we do not have
5202            --  implicit packing enabled, since this allows us to give a more
5203            --  useful error message (advising use of pragmas Implicit_Packing
5204            --  or Pack).
5205
5206            if Is_Array_Type (E) then
5207               declare
5208                  Ctyp : constant Entity_Id := Component_Type (E);
5209                  Rsiz : constant Uint      := RM_Size (Ctyp);
5210                  SZ   : constant Node_Id   := Size_Clause (E);
5211                  Btyp : constant Entity_Id := Base_Type (E);
5212
5213                  Lo   : Node_Id;
5214                  Hi   : Node_Id;
5215                  Indx : Node_Id;
5216
5217                  Num_Elmts : Uint;
5218                  --  Number of elements in array
5219
5220               begin
5221                  --  Check enabling conditions. These are straightforward
5222                  --  except for the test for a limited composite type. This
5223                  --  eliminates the rare case of a array of limited components
5224                  --  where there are issues of whether or not we can go ahead
5225                  --  and pack the array (since we can't freely pack and unpack
5226                  --  arrays if they are limited).
5227
5228                  --  Note that we check the root type explicitly because the
5229                  --  whole point is we are doing this test before we have had
5230                  --  a chance to freeze the base type (and it is that freeze
5231                  --  action that causes stuff to be inherited).
5232
5233                  if Has_Size_Clause (E)
5234                    and then Known_Static_RM_Size (E)
5235                    and then not Is_Packed (E)
5236                    and then not Has_Pragma_Pack (E)
5237                    and then not Has_Component_Size_Clause (E)
5238                    and then Known_Static_RM_Size (Ctyp)
5239                    and then RM_Size (Ctyp) < 64
5240                    and then not Is_Limited_Composite (E)
5241                    and then not Is_Packed (Root_Type (E))
5242                    and then not Has_Component_Size_Clause (Root_Type (E))
5243                    and then not (CodePeer_Mode or GNATprove_Mode)
5244                  then
5245                     --  Compute number of elements in array
5246
5247                     Num_Elmts := Uint_1;
5248                     Indx := First_Index (E);
5249                     while Present (Indx) loop
5250                        Get_Index_Bounds (Indx, Lo, Hi);
5251
5252                        if not (Compile_Time_Known_Value (Lo)
5253                                  and then
5254                                Compile_Time_Known_Value (Hi))
5255                        then
5256                           goto No_Implicit_Packing;
5257                        end if;
5258
5259                        Num_Elmts :=
5260                          Num_Elmts *
5261                            UI_Max (Uint_0,
5262                                    Expr_Value (Hi) - Expr_Value (Lo) + 1);
5263                        Next_Index (Indx);
5264                     end loop;
5265
5266                     --  What we are looking for here is the situation where
5267                     --  the RM_Size given would be exactly right if there was
5268                     --  a pragma Pack (resulting in the component size being
5269                     --  the same as the RM_Size). Furthermore, the component
5270                     --  type size must be an odd size (not a multiple of
5271                     --  storage unit). If the component RM size is an exact
5272                     --  number of storage units that is a power of two, the
5273                     --  array is not packed and has a standard representation.
5274
5275                     if RM_Size (E) = Num_Elmts * Rsiz
5276                       and then Rsiz mod System_Storage_Unit /= 0
5277                     then
5278                        --  For implicit packing mode, just set the component
5279                        --  size silently.
5280
5281                        if Implicit_Packing then
5282                           Set_Component_Size       (Btyp, Rsiz);
5283                           Set_Is_Bit_Packed_Array  (Btyp);
5284                           Set_Is_Packed            (Btyp);
5285                           Set_Has_Non_Standard_Rep (Btyp);
5286
5287                           --  Otherwise give an error message
5288
5289                        else
5290                           Error_Msg_NE
5291                             ("size given for& too small", SZ, E);
5292                           Error_Msg_N -- CODEFIX
5293                             ("\use explicit pragma Pack "
5294                              & "or use pragma Implicit_Packing", SZ);
5295                        end if;
5296
5297                     elsif RM_Size (E) = Num_Elmts * Rsiz
5298                       and then Implicit_Packing
5299                       and then
5300                         (Rsiz / System_Storage_Unit = 1
5301                            or else
5302                          Rsiz / System_Storage_Unit = 2
5303                            or else
5304                          Rsiz / System_Storage_Unit = 4)
5305                     then
5306                        --  Not a packed array, but indicate the desired
5307                        --  component size, for the back-end.
5308
5309                        Set_Component_Size (Btyp, Rsiz);
5310                     end if;
5311                  end if;
5312               end;
5313            end if;
5314
5315            <<No_Implicit_Packing>>
5316
5317            --  If ancestor subtype present, freeze that first. Note that this
5318            --  will also get the base type frozen. Need RM reference ???
5319
5320            Atype := Ancestor_Subtype (E);
5321
5322            if Present (Atype) then
5323               Freeze_And_Append (Atype, N, Result);
5324
5325            --  No ancestor subtype present
5326
5327            else
5328               --  See if we have a nearest ancestor that has a predicate.
5329               --  That catches the case of derived type with a predicate.
5330               --  Need RM reference here ???
5331
5332               Atype := Nearest_Ancestor (E);
5333
5334               if Present (Atype) and then Has_Predicates (Atype) then
5335                  Freeze_And_Append (Atype, N, Result);
5336               end if;
5337
5338               --  Freeze base type before freezing the entity (RM 13.14(15))
5339
5340               if E /= Base_Type (E) then
5341                  Freeze_And_Append (Base_Type (E), N, Result);
5342               end if;
5343            end if;
5344
5345            --  A subtype inherits all the type-related representation aspects
5346            --  from its parents (RM 13.1(8)).
5347
5348            Inherit_Aspects_At_Freeze_Point (E);
5349
5350         --  For a derived type, freeze its parent type first (RM 13.14(15))
5351
5352         elsif Is_Derived_Type (E) then
5353            Freeze_And_Append (Etype (E), N, Result);
5354            Freeze_And_Append (First_Subtype (Etype (E)), N, Result);
5355
5356            --  A derived type inherits each type-related representation aspect
5357            --  of its parent type that was directly specified before the
5358            --  declaration of the derived type (RM 13.1(15)).
5359
5360            Inherit_Aspects_At_Freeze_Point (E);
5361         end if;
5362
5363         --  Check for incompatible size and alignment for record type
5364
5365         if Warn_On_Size_Alignment
5366           and then Is_Record_Type (E)
5367           and then Has_Size_Clause (E) and then Has_Alignment_Clause (E)
5368
5369           --  If explicit Object_Size clause given assume that the programmer
5370           --  knows what he is doing, and expects the compiler behavior.
5371
5372           and then not Has_Object_Size_Clause (E)
5373
5374           --  Check for size not a multiple of alignment
5375
5376           and then RM_Size (E) mod (Alignment (E) * System_Storage_Unit) /= 0
5377         then
5378            declare
5379               SC    : constant Node_Id := Size_Clause (E);
5380               AC    : constant Node_Id := Alignment_Clause (E);
5381               Loc   : Node_Id;
5382               Abits : constant Uint := Alignment (E) * System_Storage_Unit;
5383
5384            begin
5385               if Present (SC) and then Present (AC) then
5386
5387                  --  Give a warning
5388
5389                  if Sloc (SC) > Sloc (AC) then
5390                     Loc := SC;
5391                     Error_Msg_NE
5392                       ("?Z?size is not a multiple of alignment for &",
5393                        Loc, E);
5394                     Error_Msg_Sloc := Sloc (AC);
5395                     Error_Msg_Uint_1 := Alignment (E);
5396                     Error_Msg_N ("\?Z?alignment of ^ specified #", Loc);
5397
5398                  else
5399                     Loc := AC;
5400                     Error_Msg_NE
5401                       ("?Z?size is not a multiple of alignment for &",
5402                        Loc, E);
5403                     Error_Msg_Sloc := Sloc (SC);
5404                     Error_Msg_Uint_1 := RM_Size (E);
5405                     Error_Msg_N ("\?Z?size of ^ specified #", Loc);
5406                  end if;
5407
5408                  Error_Msg_Uint_1 := ((RM_Size (E) / Abits) + 1) * Abits;
5409                  Error_Msg_N ("\?Z?Object_Size will be increased to ^", Loc);
5410               end if;
5411            end;
5412         end if;
5413
5414         --  Array type
5415
5416         if Is_Array_Type (E) then
5417            Freeze_Array_Type (E);
5418
5419         --  For a class-wide type, the corresponding specific type is
5420         --  frozen as well (RM 13.14(15))
5421
5422         elsif Is_Class_Wide_Type (E) then
5423            Freeze_And_Append (Root_Type (E), N, Result);
5424
5425            --  If the base type of the class-wide type is still incomplete,
5426            --  the class-wide remains unfrozen as well. This is legal when
5427            --  E is the formal of a primitive operation of some other type
5428            --  which is being frozen.
5429
5430            if not Is_Frozen (Root_Type (E)) then
5431               Set_Is_Frozen (E, False);
5432               Ghost_Mode := Save_Ghost_Mode;
5433               return Result;
5434            end if;
5435
5436            --  The equivalent type associated with a class-wide subtype needs
5437            --  to be frozen to ensure that its layout is done.
5438
5439            if Ekind (E) = E_Class_Wide_Subtype
5440              and then Present (Equivalent_Type (E))
5441            then
5442               Freeze_And_Append (Equivalent_Type (E), N, Result);
5443            end if;
5444
5445            --  Generate an itype reference for a library-level class-wide type
5446            --  at the freeze point. Otherwise the first explicit reference to
5447            --  the type may appear in an inner scope which will be rejected by
5448            --  the back-end.
5449
5450            if Is_Itype (E)
5451              and then Is_Compilation_Unit (Scope (E))
5452            then
5453               declare
5454                  Ref : constant Node_Id := Make_Itype_Reference (Loc);
5455
5456               begin
5457                  Set_Itype (Ref, E);
5458
5459                  --  From a gigi point of view, a class-wide subtype derives
5460                  --  from its record equivalent type. As a result, the itype
5461                  --  reference must appear after the freeze node of the
5462                  --  equivalent type or gigi will reject the reference.
5463
5464                  if Ekind (E) = E_Class_Wide_Subtype
5465                    and then Present (Equivalent_Type (E))
5466                  then
5467                     Insert_After (Freeze_Node (Equivalent_Type (E)), Ref);
5468                  else
5469                     Add_To_Result (Ref);
5470                  end if;
5471               end;
5472            end if;
5473
5474         --  For a record type or record subtype, freeze all component types
5475         --  (RM 13.14(15)). We test for E_Record_(sub)Type here, rather than
5476         --  using Is_Record_Type, because we don't want to attempt the freeze
5477         --  for the case of a private type with record extension (we will do
5478         --  that later when the full type is frozen).
5479
5480         elsif Ekind_In (E, E_Record_Type, E_Record_Subtype)
5481           and then not (Present (Scope (E))
5482                          and then Is_Generic_Unit (Scope (E)))
5483         then
5484            Freeze_Record_Type (E);
5485
5486         --  For a concurrent type, freeze corresponding record type. This does
5487         --  not correspond to any specific rule in the RM, but the record type
5488         --  is essentially part of the concurrent type. Also freeze all local
5489         --  entities. This includes record types created for entry parameter
5490         --  blocks and whatever local entities may appear in the private part.
5491
5492         elsif Is_Concurrent_Type (E) then
5493            if Present (Corresponding_Record_Type (E)) then
5494               Freeze_And_Append (Corresponding_Record_Type (E), N, Result);
5495            end if;
5496
5497            Comp := First_Entity (E);
5498            while Present (Comp) loop
5499               if Is_Type (Comp) then
5500                  Freeze_And_Append (Comp, N, Result);
5501
5502               elsif (Ekind (Comp)) /= E_Function then
5503
5504                  --  The guard on the presence of the Etype seems to be needed
5505                  --  for some CodePeer (-gnatcC) cases, but not clear why???
5506
5507                  if Present (Etype (Comp)) then
5508                     if Is_Itype (Etype (Comp))
5509                       and then Underlying_Type (Scope (Etype (Comp))) = E
5510                     then
5511                        Undelay_Type (Etype (Comp));
5512                     end if;
5513
5514                     Freeze_And_Append (Etype (Comp), N, Result);
5515                  end if;
5516               end if;
5517
5518               Next_Entity (Comp);
5519            end loop;
5520
5521         --  Private types are required to point to the same freeze node as
5522         --  their corresponding full views. The freeze node itself has to
5523         --  point to the partial view of the entity (because from the partial
5524         --  view, we can retrieve the full view, but not the reverse).
5525         --  However, in order to freeze correctly, we need to freeze the full
5526         --  view. If we are freezing at the end of a scope (or within the
5527         --  scope) of the private type, the partial and full views will have
5528         --  been swapped, the full view appears first in the entity chain and
5529         --  the swapping mechanism ensures that the pointers are properly set
5530         --  (on scope exit).
5531
5532         --  If we encounter the partial view before the full view (e.g. when
5533         --  freezing from another scope), we freeze the full view, and then
5534         --  set the pointers appropriately since we cannot rely on swapping to
5535         --  fix things up (subtypes in an outer scope might not get swapped).
5536
5537         --  If the full view is itself private, the above requirements apply
5538         --  to the underlying full view instead of the full view. But there is
5539         --  no swapping mechanism for the underlying full view so we need to
5540         --  set the pointers appropriately in both cases.
5541
5542         elsif Is_Incomplete_Or_Private_Type (E)
5543           and then not Is_Generic_Type (E)
5544         then
5545            --  The construction of the dispatch table associated with library
5546            --  level tagged types forces freezing of all the primitives of the
5547            --  type, which may cause premature freezing of the partial view.
5548            --  For example:
5549
5550            --     package Pkg is
5551            --        type T is tagged private;
5552            --        type DT is new T with private;
5553            --        procedure Prim (X : in out T; Y : in out DT'Class);
5554            --     private
5555            --        type T is tagged null record;
5556            --        Obj : T;
5557            --        type DT is new T with null record;
5558            --     end;
5559
5560            --  In this case the type will be frozen later by the usual
5561            --  mechanism: an object declaration, an instantiation, or the
5562            --  end of a declarative part.
5563
5564            if Is_Library_Level_Tagged_Type (E)
5565              and then not Present (Full_View (E))
5566            then
5567               Set_Is_Frozen (E, False);
5568               Ghost_Mode := Save_Ghost_Mode;
5569               return Result;
5570
5571            --  Case of full view present
5572
5573            elsif Present (Full_View (E)) then
5574
5575               --  If full view has already been frozen, then no further
5576               --  processing is required
5577
5578               if Is_Frozen (Full_View (E)) then
5579                  Set_Has_Delayed_Freeze (E, False);
5580                  Set_Freeze_Node (E, Empty);
5581
5582               --  Otherwise freeze full view and patch the pointers so that
5583               --  the freeze node will elaborate both views in the back end.
5584               --  However, if full view is itself private, freeze underlying
5585               --  full view instead and patch the pointers so that the freeze
5586               --  node will elaborate the three views in the back end.
5587
5588               else
5589                  declare
5590                     Full : Entity_Id := Full_View (E);
5591
5592                  begin
5593                     if Is_Private_Type (Full)
5594                       and then Present (Underlying_Full_View (Full))
5595                     then
5596                        Full := Underlying_Full_View (Full);
5597                     end if;
5598
5599                     Freeze_And_Append (Full, N, Result);
5600
5601                     if Full /= Full_View (E)
5602                       and then Has_Delayed_Freeze (Full_View (E))
5603                     then
5604                        F_Node := Freeze_Node (Full);
5605
5606                        if Present (F_Node) then
5607                           Set_Freeze_Node (Full_View (E), F_Node);
5608                           Set_Entity (F_Node, Full_View (E));
5609
5610                        else
5611                           Set_Has_Delayed_Freeze (Full_View (E), False);
5612                           Set_Freeze_Node (Full_View (E), Empty);
5613                        end if;
5614                     end if;
5615
5616                     if Has_Delayed_Freeze (E) then
5617                        F_Node := Freeze_Node (Full_View (E));
5618
5619                        if Present (F_Node) then
5620                           Set_Freeze_Node (E, F_Node);
5621                           Set_Entity (F_Node, E);
5622
5623                        else
5624                           --  {Incomplete,Private}_Subtypes with Full_Views
5625                           --  constrained by discriminants.
5626
5627                           Set_Has_Delayed_Freeze (E, False);
5628                           Set_Freeze_Node (E, Empty);
5629                        end if;
5630                     end if;
5631                  end;
5632               end if;
5633
5634               Check_Debug_Info_Needed (E);
5635
5636               --  AI-117 requires that the convention of a partial view be the
5637               --  same as the convention of the full view. Note that this is a
5638               --  recognized breach of privacy, but it's essential for logical
5639               --  consistency of representation, and the lack of a rule in
5640               --  RM95 was an oversight.
5641
5642               Set_Convention (E, Convention (Full_View (E)));
5643
5644               Set_Size_Known_At_Compile_Time (E,
5645                 Size_Known_At_Compile_Time (Full_View (E)));
5646
5647               --  Size information is copied from the full view to the
5648               --  incomplete or private view for consistency.
5649
5650               --  We skip this is the full view is not a type. This is very
5651               --  strange of course, and can only happen as a result of
5652               --  certain illegalities, such as a premature attempt to derive
5653               --  from an incomplete type.
5654
5655               if Is_Type (Full_View (E)) then
5656                  Set_Size_Info (E, Full_View (E));
5657                  Set_RM_Size   (E, RM_Size (Full_View (E)));
5658               end if;
5659
5660               Ghost_Mode := Save_Ghost_Mode;
5661               return Result;
5662
5663            --  Case of underlying full view present
5664
5665            elsif Is_Private_Type (E)
5666              and then Present (Underlying_Full_View (E))
5667            then
5668               if not Is_Frozen (Underlying_Full_View (E)) then
5669                  Freeze_And_Append (Underlying_Full_View (E), N, Result);
5670               end if;
5671
5672               --  Patch the pointers so that the freeze node will elaborate
5673               --  both views in the back end.
5674
5675               if Has_Delayed_Freeze (E) then
5676                  F_Node := Freeze_Node (Underlying_Full_View (E));
5677
5678                  if Present (F_Node) then
5679                     Set_Freeze_Node (E, F_Node);
5680                     Set_Entity (F_Node, E);
5681
5682                  else
5683                     Set_Has_Delayed_Freeze (E, False);
5684                     Set_Freeze_Node (E, Empty);
5685                  end if;
5686               end if;
5687
5688               Check_Debug_Info_Needed (E);
5689
5690               Ghost_Mode := Save_Ghost_Mode;
5691               return Result;
5692
5693            --  Case of no full view present. If entity is derived or subtype,
5694            --  it is safe to freeze, correctness depends on the frozen status
5695            --  of parent. Otherwise it is either premature usage, or a Taft
5696            --  amendment type, so diagnosis is at the point of use and the
5697            --  type might be frozen later.
5698
5699            elsif E /= Base_Type (E) or else Is_Derived_Type (E) then
5700               null;
5701
5702            else
5703               Set_Is_Frozen (E, False);
5704               Ghost_Mode := Save_Ghost_Mode;
5705               return No_List;
5706            end if;
5707
5708         --  For access subprogram, freeze types of all formals, the return
5709         --  type was already frozen, since it is the Etype of the function.
5710         --  Formal types can be tagged Taft amendment types, but otherwise
5711         --  they cannot be incomplete.
5712
5713         elsif Ekind (E) = E_Subprogram_Type then
5714            Formal := First_Formal (E);
5715            while Present (Formal) loop
5716               if Ekind (Etype (Formal)) = E_Incomplete_Type
5717                 and then No (Full_View (Etype (Formal)))
5718               then
5719                  if Is_Tagged_Type (Etype (Formal)) then
5720                     null;
5721
5722                  --  AI05-151: Incomplete types are allowed in access to
5723                  --  subprogram specifications.
5724
5725                  elsif Ada_Version < Ada_2012 then
5726                     Error_Msg_NE
5727                       ("invalid use of incomplete type&", E, Etype (Formal));
5728                  end if;
5729               end if;
5730
5731               Freeze_And_Append (Etype (Formal), N, Result);
5732               Next_Formal (Formal);
5733            end loop;
5734
5735            Freeze_Subprogram (E);
5736
5737         --  For access to a protected subprogram, freeze the equivalent type
5738         --  (however this is not set if we are not generating code or if this
5739         --  is an anonymous type used just for resolution).
5740
5741         elsif Is_Access_Protected_Subprogram_Type (E) then
5742            if Present (Equivalent_Type (E)) then
5743               Freeze_And_Append (Equivalent_Type (E), N, Result);
5744            end if;
5745         end if;
5746
5747         --  Generic types are never seen by the back-end, and are also not
5748         --  processed by the expander (since the expander is turned off for
5749         --  generic processing), so we never need freeze nodes for them.
5750
5751         if Is_Generic_Type (E) then
5752            Ghost_Mode := Save_Ghost_Mode;
5753            return Result;
5754         end if;
5755
5756         --  Some special processing for non-generic types to complete
5757         --  representation details not known till the freeze point.
5758
5759         if Is_Fixed_Point_Type (E) then
5760            Freeze_Fixed_Point_Type (E);
5761
5762            --  Some error checks required for ordinary fixed-point type. Defer
5763            --  these till the freeze-point since we need the small and range
5764            --  values. We only do these checks for base types
5765
5766            if Is_Ordinary_Fixed_Point_Type (E) and then Is_Base_Type (E) then
5767               if Small_Value (E) < Ureal_2_M_80 then
5768                  Error_Msg_Name_1 := Name_Small;
5769                  Error_Msg_N
5770                    ("`&''%` too small, minimum allowed is 2.0'*'*(-80)", E);
5771
5772               elsif Small_Value (E) > Ureal_2_80 then
5773                  Error_Msg_Name_1 := Name_Small;
5774                  Error_Msg_N
5775                    ("`&''%` too large, maximum allowed is 2.0'*'*80", E);
5776               end if;
5777
5778               if Expr_Value_R (Type_Low_Bound (E)) < Ureal_M_10_36 then
5779                  Error_Msg_Name_1 := Name_First;
5780                  Error_Msg_N
5781                    ("`&''%` too small, minimum allowed is -10.0'*'*36", E);
5782               end if;
5783
5784               if Expr_Value_R (Type_High_Bound (E)) > Ureal_10_36 then
5785                  Error_Msg_Name_1 := Name_Last;
5786                  Error_Msg_N
5787                    ("`&''%` too large, maximum allowed is 10.0'*'*36", E);
5788               end if;
5789            end if;
5790
5791         elsif Is_Enumeration_Type (E) then
5792            Freeze_Enumeration_Type (E);
5793
5794         elsif Is_Integer_Type (E) then
5795            Adjust_Esize_For_Alignment (E);
5796
5797            if Is_Modular_Integer_Type (E)
5798              and then Warn_On_Suspicious_Modulus_Value
5799            then
5800               Check_Suspicious_Modulus (E);
5801            end if;
5802
5803         --  The pool applies to named and anonymous access types, but not
5804         --  to subprogram and to  internal types generated for 'Access
5805         --  references.
5806
5807         elsif Is_Access_Type (E)
5808           and then not Is_Access_Subprogram_Type (E)
5809           and then Ekind (E) /= E_Access_Attribute_Type
5810         then
5811            --  If a pragma Default_Storage_Pool applies, and this type has no
5812            --  Storage_Pool or Storage_Size clause (which must have occurred
5813            --  before the freezing point), then use the default. This applies
5814            --  only to base types.
5815
5816            --  None of this applies to access to subprograms, for which there
5817            --  are clearly no pools.
5818
5819            if Present (Default_Pool)
5820              and then Is_Base_Type (E)
5821              and then not Has_Storage_Size_Clause (E)
5822              and then No (Associated_Storage_Pool (E))
5823            then
5824               --  Case of pragma Default_Storage_Pool (null)
5825
5826               if Nkind (Default_Pool) = N_Null then
5827                  Set_No_Pool_Assigned (E);
5828
5829               --  Case of pragma Default_Storage_Pool (storage_pool_NAME)
5830
5831               else
5832                  Set_Associated_Storage_Pool (E, Entity (Default_Pool));
5833               end if;
5834            end if;
5835
5836            --  Check restriction for standard storage pool
5837
5838            if No (Associated_Storage_Pool (E)) then
5839               Check_Restriction (No_Standard_Storage_Pools, E);
5840            end if;
5841
5842            --  Deal with error message for pure access type. This is not an
5843            --  error in Ada 2005 if there is no pool (see AI-366).
5844
5845            if Is_Pure_Unit_Access_Type (E)
5846              and then (Ada_Version < Ada_2005
5847                         or else not No_Pool_Assigned (E))
5848              and then not Is_Generic_Unit (Scope (E))
5849            then
5850               Error_Msg_N ("named access type not allowed in pure unit", E);
5851
5852               if Ada_Version >= Ada_2005 then
5853                  Error_Msg_N
5854                    ("\would be legal if Storage_Size of 0 given??", E);
5855
5856               elsif No_Pool_Assigned (E) then
5857                  Error_Msg_N
5858                    ("\would be legal in Ada 2005??", E);
5859
5860               else
5861                  Error_Msg_N
5862                    ("\would be legal in Ada 2005 if "
5863                     & "Storage_Size of 0 given??", E);
5864               end if;
5865            end if;
5866         end if;
5867
5868         --  Case of composite types
5869
5870         if Is_Composite_Type (E) then
5871
5872            --  AI-117 requires that all new primitives of a tagged type must
5873            --  inherit the convention of the full view of the type. Inherited
5874            --  and overriding operations are defined to inherit the convention
5875            --  of their parent or overridden subprogram (also specified in
5876            --  AI-117), which will have occurred earlier (in Derive_Subprogram
5877            --  and New_Overloaded_Entity). Here we set the convention of
5878            --  primitives that are still convention Ada, which will ensure
5879            --  that any new primitives inherit the type's convention. Class-
5880            --  wide types can have a foreign convention inherited from their
5881            --  specific type, but are excluded from this since they don't have
5882            --  any associated primitives.
5883
5884            if Is_Tagged_Type (E)
5885              and then not Is_Class_Wide_Type (E)
5886              and then Convention (E) /= Convention_Ada
5887            then
5888               declare
5889                  Prim_List : constant Elist_Id := Primitive_Operations (E);
5890                  Prim      : Elmt_Id;
5891
5892               begin
5893                  Prim := First_Elmt (Prim_List);
5894                  while Present (Prim) loop
5895                     if Convention (Node (Prim)) = Convention_Ada then
5896                        Set_Convention (Node (Prim), Convention (E));
5897                     end if;
5898
5899                     Next_Elmt (Prim);
5900                  end loop;
5901               end;
5902            end if;
5903
5904            --  If the type is a simple storage pool type, then this is where
5905            --  we attempt to locate and validate its Allocate, Deallocate, and
5906            --  Storage_Size operations (the first is required, and the latter
5907            --  two are optional). We also verify that the full type for a
5908            --  private type is allowed to be a simple storage pool type.
5909
5910            if Present (Get_Rep_Pragma (E, Name_Simple_Storage_Pool_Type))
5911              and then (Is_Base_Type (E) or else Has_Private_Declaration (E))
5912            then
5913               --  If the type is marked Has_Private_Declaration, then this is
5914               --  a full type for a private type that was specified with the
5915               --  pragma Simple_Storage_Pool_Type, and here we ensure that the
5916               --  pragma is allowed for the full type (for example, it can't
5917               --  be an array type, or a nonlimited record type).
5918
5919               if Has_Private_Declaration (E) then
5920                  if (not Is_Record_Type (E) or else not Is_Limited_View (E))
5921                    and then not Is_Private_Type (E)
5922                  then
5923                     Error_Msg_Name_1 := Name_Simple_Storage_Pool_Type;
5924                     Error_Msg_N
5925                       ("pragma% can only apply to full type that is an " &
5926                        "explicitly limited type", E);
5927                  end if;
5928               end if;
5929
5930               Validate_Simple_Pool_Ops : declare
5931                  Pool_Type    : Entity_Id renames E;
5932                  Address_Type : constant Entity_Id := RTE (RE_Address);
5933                  Stg_Cnt_Type : constant Entity_Id := RTE (RE_Storage_Count);
5934
5935                  procedure Validate_Simple_Pool_Op_Formal
5936                    (Pool_Op        : Entity_Id;
5937                     Pool_Op_Formal : in out Entity_Id;
5938                     Expected_Mode  : Formal_Kind;
5939                     Expected_Type  : Entity_Id;
5940                     Formal_Name    : String;
5941                     OK_Formal      : in out Boolean);
5942                  --  Validate one formal Pool_Op_Formal of the candidate pool
5943                  --  operation Pool_Op. The formal must be of Expected_Type
5944                  --  and have mode Expected_Mode. OK_Formal will be set to
5945                  --  False if the formal doesn't match. If OK_Formal is False
5946                  --  on entry, then the formal will effectively be ignored
5947                  --  (because validation of the pool op has already failed).
5948                  --  Upon return, Pool_Op_Formal will be updated to the next
5949                  --  formal, if any.
5950
5951                  procedure Validate_Simple_Pool_Operation
5952                    (Op_Name : Name_Id);
5953                  --  Search for and validate a simple pool operation with the
5954                  --  name Op_Name. If the name is Allocate, then there must be
5955                  --  exactly one such primitive operation for the simple pool
5956                  --  type. If the name is Deallocate or Storage_Size, then
5957                  --  there can be at most one such primitive operation. The
5958                  --  profile of the located primitive must conform to what
5959                  --  is expected for each operation.
5960
5961                  ------------------------------------
5962                  -- Validate_Simple_Pool_Op_Formal --
5963                  ------------------------------------
5964
5965                  procedure Validate_Simple_Pool_Op_Formal
5966                    (Pool_Op        : Entity_Id;
5967                     Pool_Op_Formal : in out Entity_Id;
5968                     Expected_Mode  : Formal_Kind;
5969                     Expected_Type  : Entity_Id;
5970                     Formal_Name    : String;
5971                     OK_Formal      : in out Boolean)
5972                  is
5973                  begin
5974                     --  If OK_Formal is False on entry, then simply ignore
5975                     --  the formal, because an earlier formal has already
5976                     --  been flagged.
5977
5978                     if not OK_Formal then
5979                        return;
5980
5981                     --  If no formal is passed in, then issue an error for a
5982                     --  missing formal.
5983
5984                     elsif not Present (Pool_Op_Formal) then
5985                        Error_Msg_NE
5986                          ("simple storage pool op missing formal " &
5987                           Formal_Name & " of type&", Pool_Op, Expected_Type);
5988                        OK_Formal := False;
5989
5990                        return;
5991                     end if;
5992
5993                     if Etype (Pool_Op_Formal) /= Expected_Type then
5994
5995                        --  If the pool type was expected for this formal, then
5996                        --  this will not be considered a candidate operation
5997                        --  for the simple pool, so we unset OK_Formal so that
5998                        --  the op and any later formals will be ignored.
5999
6000                        if Expected_Type = Pool_Type then
6001                           OK_Formal := False;
6002
6003                           return;
6004
6005                        else
6006                           Error_Msg_NE
6007                             ("wrong type for formal " & Formal_Name &
6008                              " of simple storage pool op; expected type&",
6009                              Pool_Op_Formal, Expected_Type);
6010                        end if;
6011                     end if;
6012
6013                     --  Issue error if formal's mode is not the expected one
6014
6015                     if Ekind (Pool_Op_Formal) /= Expected_Mode then
6016                        Error_Msg_N
6017                          ("wrong mode for formal of simple storage pool op",
6018                           Pool_Op_Formal);
6019                     end if;
6020
6021                     --  Advance to the next formal
6022
6023                     Next_Formal (Pool_Op_Formal);
6024                  end Validate_Simple_Pool_Op_Formal;
6025
6026                  ------------------------------------
6027                  -- Validate_Simple_Pool_Operation --
6028                  ------------------------------------
6029
6030                  procedure Validate_Simple_Pool_Operation
6031                    (Op_Name : Name_Id)
6032                  is
6033                     Op       : Entity_Id;
6034                     Found_Op : Entity_Id := Empty;
6035                     Formal   : Entity_Id;
6036                     Is_OK    : Boolean;
6037
6038                  begin
6039                     pragma Assert
6040                       (Nam_In (Op_Name, Name_Allocate,
6041                                         Name_Deallocate,
6042                                         Name_Storage_Size));
6043
6044                     Error_Msg_Name_1 := Op_Name;
6045
6046                     --  For each homonym declared immediately in the scope
6047                     --  of the simple storage pool type, determine whether
6048                     --  the homonym is an operation of the pool type, and,
6049                     --  if so, check that its profile is as expected for
6050                     --  a simple pool operation of that name.
6051
6052                     Op := Get_Name_Entity_Id (Op_Name);
6053                     while Present (Op) loop
6054                        if Ekind_In (Op, E_Function, E_Procedure)
6055                          and then Scope (Op) = Current_Scope
6056                        then
6057                           Formal := First_Entity (Op);
6058
6059                           Is_OK := True;
6060
6061                           --  The first parameter must be of the pool type
6062                           --  in order for the operation to qualify.
6063
6064                           if Op_Name = Name_Storage_Size then
6065                              Validate_Simple_Pool_Op_Formal
6066                                (Op, Formal, E_In_Parameter, Pool_Type,
6067                                 "Pool", Is_OK);
6068                           else
6069                              Validate_Simple_Pool_Op_Formal
6070                                (Op, Formal, E_In_Out_Parameter, Pool_Type,
6071                                 "Pool", Is_OK);
6072                           end if;
6073
6074                           --  If another operation with this name has already
6075                           --  been located for the type, then flag an error,
6076                           --  since we only allow the type to have a single
6077                           --  such primitive.
6078
6079                           if Present (Found_Op) and then Is_OK then
6080                              Error_Msg_NE
6081                                ("only one % operation allowed for " &
6082                                 "simple storage pool type&", Op, Pool_Type);
6083                           end if;
6084
6085                           --  In the case of Allocate and Deallocate, a formal
6086                           --  of type System.Address is required.
6087
6088                           if Op_Name = Name_Allocate then
6089                              Validate_Simple_Pool_Op_Formal
6090                                (Op, Formal, E_Out_Parameter,
6091                                  Address_Type, "Storage_Address", Is_OK);
6092
6093                           elsif Op_Name = Name_Deallocate then
6094                              Validate_Simple_Pool_Op_Formal
6095                                (Op, Formal, E_In_Parameter,
6096                                 Address_Type, "Storage_Address", Is_OK);
6097                           end if;
6098
6099                           --  In the case of Allocate and Deallocate, formals
6100                           --  of type Storage_Count are required as the third
6101                           --  and fourth parameters.
6102
6103                           if Op_Name /= Name_Storage_Size then
6104                              Validate_Simple_Pool_Op_Formal
6105                                (Op, Formal, E_In_Parameter,
6106                                 Stg_Cnt_Type, "Size_In_Storage_Units", Is_OK);
6107                              Validate_Simple_Pool_Op_Formal
6108                                (Op, Formal, E_In_Parameter,
6109                                 Stg_Cnt_Type, "Alignment", Is_OK);
6110                           end if;
6111
6112                           --  If no mismatched formals have been found (Is_OK)
6113                           --  and no excess formals are present, then this
6114                           --  operation has been validated, so record it.
6115
6116                           if not Present (Formal) and then Is_OK then
6117                              Found_Op := Op;
6118                           end if;
6119                        end if;
6120
6121                        Op := Homonym (Op);
6122                     end loop;
6123
6124                     --  There must be a valid Allocate operation for the type,
6125                     --  so issue an error if none was found.
6126
6127                     if Op_Name = Name_Allocate
6128                       and then not Present (Found_Op)
6129                     then
6130                        Error_Msg_N ("missing % operation for simple " &
6131                                     "storage pool type", Pool_Type);
6132
6133                     elsif Present (Found_Op) then
6134
6135                        --  Simple pool operations can't be abstract
6136
6137                        if Is_Abstract_Subprogram (Found_Op) then
6138                           Error_Msg_N
6139                             ("simple storage pool operation must not be " &
6140                              "abstract", Found_Op);
6141                        end if;
6142
6143                        --  The Storage_Size operation must be a function with
6144                        --  Storage_Count as its result type.
6145
6146                        if Op_Name = Name_Storage_Size then
6147                           if Ekind (Found_Op) = E_Procedure then
6148                              Error_Msg_N
6149                                ("% operation must be a function", Found_Op);
6150
6151                           elsif Etype (Found_Op) /= Stg_Cnt_Type then
6152                              Error_Msg_NE
6153                                ("wrong result type for%, expected type&",
6154                                 Found_Op, Stg_Cnt_Type);
6155                           end if;
6156
6157                        --  Allocate and Deallocate must be procedures
6158
6159                        elsif Ekind (Found_Op) = E_Function then
6160                           Error_Msg_N
6161                             ("% operation must be a procedure", Found_Op);
6162                        end if;
6163                     end if;
6164                  end Validate_Simple_Pool_Operation;
6165
6166               --  Start of processing for Validate_Simple_Pool_Ops
6167
6168               begin
6169                  Validate_Simple_Pool_Operation (Name_Allocate);
6170                  Validate_Simple_Pool_Operation (Name_Deallocate);
6171                  Validate_Simple_Pool_Operation (Name_Storage_Size);
6172               end Validate_Simple_Pool_Ops;
6173            end if;
6174         end if;
6175
6176         --  Now that all types from which E may depend are frozen, see if the
6177         --  size is known at compile time, if it must be unsigned, or if
6178         --  strict alignment is required
6179
6180         Check_Compile_Time_Size (E);
6181         Check_Unsigned_Type (E);
6182
6183         if Base_Type (E) = E then
6184            Check_Strict_Alignment (E);
6185         end if;
6186
6187         --  Do not allow a size clause for a type which does not have a size
6188         --  that is known at compile time
6189
6190         if Has_Size_Clause (E)
6191           and then not Size_Known_At_Compile_Time (E)
6192         then
6193            --  Suppress this message if errors posted on E, even if we are
6194            --  in all errors mode, since this is often a junk message
6195
6196            if not Error_Posted (E) then
6197               Error_Msg_N
6198                 ("size clause not allowed for variable length type",
6199                  Size_Clause (E));
6200            end if;
6201         end if;
6202
6203         --  Now we set/verify the representation information, in particular
6204         --  the size and alignment values. This processing is not required for
6205         --  generic types, since generic types do not play any part in code
6206         --  generation, and so the size and alignment values for such types
6207         --  are irrelevant. Ditto for types declared within a generic unit,
6208         --  which may have components that depend on generic parameters, and
6209         --  that will be recreated in an instance.
6210
6211         if Inside_A_Generic then
6212            null;
6213
6214         --  Otherwise we call the layout procedure
6215
6216         else
6217            Layout_Type (E);
6218         end if;
6219
6220         --  If this is an access to subprogram whose designated type is itself
6221         --  a subprogram type, the return type of this anonymous subprogram
6222         --  type must be decorated as well.
6223
6224         if Ekind (E) = E_Anonymous_Access_Subprogram_Type
6225           and then Ekind (Designated_Type (E)) = E_Subprogram_Type
6226         then
6227            Layout_Type (Etype (Designated_Type (E)));
6228         end if;
6229
6230         --  If the type has a Defaut_Value/Default_Component_Value aspect,
6231         --  this is where we analye the expression (after the type is frozen,
6232         --  since in the case of Default_Value, we are analyzing with the
6233         --  type itself, and we treat Default_Component_Value similarly for
6234         --  the sake of uniformity).
6235
6236         if Is_First_Subtype (E) and then Has_Default_Aspect (E) then
6237            declare
6238               Nam : Name_Id;
6239               Exp : Node_Id;
6240               Typ : Entity_Id;
6241
6242            begin
6243               if Is_Scalar_Type (E) then
6244                  Nam := Name_Default_Value;
6245                  Typ := E;
6246                  Exp := Default_Aspect_Value (Typ);
6247               else
6248                  Nam := Name_Default_Component_Value;
6249                  Typ := Component_Type (E);
6250                  Exp := Default_Aspect_Component_Value (E);
6251               end if;
6252
6253               Analyze_And_Resolve (Exp, Typ);
6254
6255               if Etype (Exp) /= Any_Type then
6256                  if not Is_OK_Static_Expression (Exp) then
6257                     Error_Msg_Name_1 := Nam;
6258                     Flag_Non_Static_Expr
6259                       ("aspect% requires static expression", Exp);
6260                  end if;
6261               end if;
6262            end;
6263         end if;
6264
6265         --  End of freeze processing for type entities
6266      end if;
6267
6268      --  Here is where we logically freeze the current entity. If it has a
6269      --  freeze node, then this is the point at which the freeze node is
6270      --  linked into the result list.
6271
6272      if Has_Delayed_Freeze (E) then
6273
6274         --  If a freeze node is already allocated, use it, otherwise allocate
6275         --  a new one. The preallocation happens in the case of anonymous base
6276         --  types, where we preallocate so that we can set First_Subtype_Link.
6277         --  Note that we reset the Sloc to the current freeze location.
6278
6279         if Present (Freeze_Node (E)) then
6280            F_Node := Freeze_Node (E);
6281            Set_Sloc (F_Node, Loc);
6282
6283         else
6284            F_Node := New_Node (N_Freeze_Entity, Loc);
6285            Set_Freeze_Node (E, F_Node);
6286            Set_Access_Types_To_Process (F_Node, No_Elist);
6287            Set_TSS_Elist (F_Node, No_Elist);
6288            Set_Actions (F_Node, No_List);
6289         end if;
6290
6291         Set_Entity (F_Node, E);
6292         Add_To_Result (F_Node);
6293
6294         --  A final pass over record types with discriminants. If the type
6295         --  has an incomplete declaration, there may be constrained access
6296         --  subtypes declared elsewhere, which do not depend on the discrimi-
6297         --  nants of the type, and which are used as component types (i.e.
6298         --  the full view is a recursive type). The designated types of these
6299         --  subtypes can only be elaborated after the type itself, and they
6300         --  need an itype reference.
6301
6302         if Ekind (E) = E_Record_Type
6303           and then Has_Discriminants (E)
6304         then
6305            declare
6306               Comp : Entity_Id;
6307               IR   : Node_Id;
6308               Typ  : Entity_Id;
6309
6310            begin
6311               Comp := First_Component (E);
6312               while Present (Comp) loop
6313                  Typ  := Etype (Comp);
6314
6315                  if Ekind (Comp) = E_Component
6316                    and then Is_Access_Type (Typ)
6317                    and then Scope (Typ) /= E
6318                    and then Base_Type (Designated_Type (Typ)) = E
6319                    and then Is_Itype (Designated_Type (Typ))
6320                  then
6321                     IR := Make_Itype_Reference (Sloc (Comp));
6322                     Set_Itype (IR, Designated_Type (Typ));
6323                     Append (IR, Result);
6324                  end if;
6325
6326                  Next_Component (Comp);
6327               end loop;
6328            end;
6329         end if;
6330      end if;
6331
6332      --  When a type is frozen, the first subtype of the type is frozen as
6333      --  well (RM 13.14(15)). This has to be done after freezing the type,
6334      --  since obviously the first subtype depends on its own base type.
6335
6336      if Is_Type (E) then
6337         Freeze_And_Append (First_Subtype (E), N, Result);
6338
6339         --  If we just froze a tagged non-class wide record, then freeze the
6340         --  corresponding class-wide type. This must be done after the tagged
6341         --  type itself is frozen, because the class-wide type refers to the
6342         --  tagged type which generates the class.
6343
6344         if Is_Tagged_Type (E)
6345           and then not Is_Class_Wide_Type (E)
6346           and then Present (Class_Wide_Type (E))
6347         then
6348            Freeze_And_Append (Class_Wide_Type (E), N, Result);
6349         end if;
6350      end if;
6351
6352      Check_Debug_Info_Needed (E);
6353
6354      --  Special handling for subprograms
6355
6356      if Is_Subprogram (E) then
6357
6358         --  If subprogram has address clause then reset Is_Public flag, since
6359         --  we do not want the backend to generate external references.
6360
6361         if Present (Address_Clause (E))
6362           and then not Is_Library_Level_Entity (E)
6363         then
6364            Set_Is_Public (E, False);
6365         end if;
6366      end if;
6367
6368      Ghost_Mode := Save_Ghost_Mode;
6369      return Result;
6370   end Freeze_Entity;
6371
6372   -----------------------------
6373   -- Freeze_Enumeration_Type --
6374   -----------------------------
6375
6376   procedure Freeze_Enumeration_Type (Typ : Entity_Id) is
6377   begin
6378      --  By default, if no size clause is present, an enumeration type with
6379      --  Convention C is assumed to interface to a C enum, and has integer
6380      --  size. This applies to types. For subtypes, verify that its base
6381      --  type has no size clause either. Treat other foreign conventions
6382      --  in the same way, and also make sure alignment is set right.
6383
6384      if Has_Foreign_Convention (Typ)
6385        and then not Has_Size_Clause (Typ)
6386        and then not Has_Size_Clause (Base_Type (Typ))
6387        and then Esize (Typ) < Standard_Integer_Size
6388
6389        --  Don't do this if Short_Enums on target
6390
6391        and then not Target_Short_Enums
6392      then
6393         Init_Esize (Typ, Standard_Integer_Size);
6394         Set_Alignment (Typ, Alignment (Standard_Integer));
6395
6396      --  Normal Ada case or size clause present or not Long_C_Enums on target
6397
6398      else
6399         --  If the enumeration type interfaces to C, and it has a size clause
6400         --  that specifies less than int size, it warrants a warning. The
6401         --  user may intend the C type to be an enum or a char, so this is
6402         --  not by itself an error that the Ada compiler can detect, but it
6403         --  it is a worth a heads-up. For Boolean and Character types we
6404         --  assume that the programmer has the proper C type in mind.
6405
6406         if Convention (Typ) = Convention_C
6407           and then Has_Size_Clause (Typ)
6408           and then Esize (Typ) /= Esize (Standard_Integer)
6409           and then not Is_Boolean_Type (Typ)
6410           and then not Is_Character_Type (Typ)
6411
6412           --  Don't do this if Short_Enums on target
6413
6414           and then not Target_Short_Enums
6415         then
6416            Error_Msg_N
6417              ("C enum types have the size of a C int??", Size_Clause (Typ));
6418         end if;
6419
6420         Adjust_Esize_For_Alignment (Typ);
6421      end if;
6422   end Freeze_Enumeration_Type;
6423
6424   -----------------------
6425   -- Freeze_Expression --
6426   -----------------------
6427
6428   procedure Freeze_Expression (N : Node_Id) is
6429      In_Spec_Exp : constant Boolean := In_Spec_Expression;
6430      Typ         : Entity_Id;
6431      Nam         : Entity_Id;
6432      Desig_Typ   : Entity_Id;
6433      P           : Node_Id;
6434      Parent_P    : Node_Id;
6435
6436      Freeze_Outside : Boolean := False;
6437      --  This flag is set true if the entity must be frozen outside the
6438      --  current subprogram. This happens in the case of expander generated
6439      --  subprograms (_Init_Proc, _Input, _Output, _Read, _Write) which do
6440      --  not freeze all entities like other bodies, but which nevertheless
6441      --  may reference entities that have to be frozen before the body and
6442      --  obviously cannot be frozen inside the body.
6443
6444      function Find_Aggregate_Component_Desig_Type return Entity_Id;
6445      --  If the expression is an array aggregate, the type of the component
6446      --  expressions is also frozen. If the component type is an access type
6447      --  and the expressions include allocators, the designed type is frozen
6448      --  as well.
6449
6450      function In_Expanded_Body (N : Node_Id) return Boolean;
6451      --  Given an N_Handled_Sequence_Of_Statements node N, determines whether
6452      --  it is the handled statement sequence of an expander-generated
6453      --  subprogram (init proc, stream subprogram, or renaming as body).
6454      --  If so, this is not a freezing context.
6455
6456      -----------------------------------------
6457      -- Find_Aggregate_Component_Desig_Type --
6458      -----------------------------------------
6459
6460      function Find_Aggregate_Component_Desig_Type return Entity_Id is
6461         Assoc : Node_Id;
6462         Exp   : Node_Id;
6463
6464      begin
6465         if Present (Expressions (N)) then
6466            Exp := First (Expressions (N));
6467            while Present (Exp) loop
6468               if Nkind (Exp) = N_Allocator then
6469                  return Designated_Type (Component_Type (Etype (N)));
6470               end if;
6471
6472               Next (Exp);
6473            end loop;
6474         end if;
6475
6476         if Present (Component_Associations (N)) then
6477            Assoc := First  (Component_Associations (N));
6478            while Present (Assoc) loop
6479               if Nkind (Expression (Assoc)) = N_Allocator then
6480                  return Designated_Type (Component_Type (Etype (N)));
6481               end if;
6482
6483               Next (Assoc);
6484            end loop;
6485         end if;
6486
6487         return Empty;
6488      end Find_Aggregate_Component_Desig_Type;
6489
6490      ----------------------
6491      -- In_Expanded_Body --
6492      ----------------------
6493
6494      function In_Expanded_Body (N : Node_Id) return Boolean is
6495         P  : Node_Id;
6496         Id : Entity_Id;
6497
6498      begin
6499         if Nkind (N) = N_Subprogram_Body then
6500            P := N;
6501         else
6502            P := Parent (N);
6503         end if;
6504
6505         if Nkind (P) /= N_Subprogram_Body then
6506            return False;
6507
6508         else
6509            Id := Defining_Unit_Name (Specification (P));
6510
6511            --  The following are expander-created bodies, or bodies that
6512            --  are not freeze points.
6513
6514            if Nkind (Id) = N_Defining_Identifier
6515              and then (Is_Init_Proc (Id)
6516                         or else Is_TSS (Id, TSS_Stream_Input)
6517                         or else Is_TSS (Id, TSS_Stream_Output)
6518                         or else Is_TSS (Id, TSS_Stream_Read)
6519                         or else Is_TSS (Id, TSS_Stream_Write)
6520                         or else Nkind_In (Original_Node (P),
6521                                           N_Subprogram_Renaming_Declaration,
6522                                           N_Expression_Function))
6523            then
6524               return True;
6525            else
6526               return False;
6527            end if;
6528         end if;
6529      end In_Expanded_Body;
6530
6531   --  Start of processing for Freeze_Expression
6532
6533   begin
6534      --  Immediate return if freezing is inhibited. This flag is set by the
6535      --  analyzer to stop freezing on generated expressions that would cause
6536      --  freezing if they were in the source program, but which are not
6537      --  supposed to freeze, since they are created.
6538
6539      if Must_Not_Freeze (N) then
6540         return;
6541      end if;
6542
6543      --  If expression is non-static, then it does not freeze in a default
6544      --  expression, see section "Handling of Default Expressions" in the
6545      --  spec of package Sem for further details. Note that we have to make
6546      --  sure that we actually have a real expression (if we have a subtype
6547      --  indication, we can't test Is_OK_Static_Expression). However, we
6548      --  exclude the case of the prefix of an attribute of a static scalar
6549      --  subtype from this early return, because static subtype attributes
6550      --  should always cause freezing, even in default expressions, but
6551      --  the attribute may not have been marked as static yet (because in
6552      --  Resolve_Attribute, the call to Eval_Attribute follows the call of
6553      --  Freeze_Expression on the prefix).
6554
6555      if In_Spec_Exp
6556        and then Nkind (N) in N_Subexpr
6557        and then not Is_OK_Static_Expression (N)
6558        and then (Nkind (Parent (N)) /= N_Attribute_Reference
6559                   or else not (Is_Entity_Name (N)
6560                                 and then Is_Type (Entity (N))
6561                                 and then Is_OK_Static_Subtype (Entity (N))))
6562      then
6563         return;
6564      end if;
6565
6566      --  Freeze type of expression if not frozen already
6567
6568      Typ := Empty;
6569
6570      if Nkind (N) in N_Has_Etype then
6571         if not Is_Frozen (Etype (N)) then
6572            Typ := Etype (N);
6573
6574         --  Base type may be an derived numeric type that is frozen at
6575         --  the point of declaration, but first_subtype is still unfrozen.
6576
6577         elsif not Is_Frozen (First_Subtype (Etype (N))) then
6578            Typ := First_Subtype (Etype (N));
6579         end if;
6580      end if;
6581
6582      --  For entity name, freeze entity if not frozen already. A special
6583      --  exception occurs for an identifier that did not come from source.
6584      --  We don't let such identifiers freeze a non-internal entity, i.e.
6585      --  an entity that did come from source, since such an identifier was
6586      --  generated by the expander, and cannot have any semantic effect on
6587      --  the freezing semantics. For example, this stops the parameter of
6588      --  an initialization procedure from freezing the variable.
6589
6590      if Is_Entity_Name (N)
6591        and then not Is_Frozen (Entity (N))
6592        and then (Nkind (N) /= N_Identifier
6593                   or else Comes_From_Source (N)
6594                   or else not Comes_From_Source (Entity (N)))
6595      then
6596         Nam := Entity (N);
6597
6598         if Present (Nam) and then Ekind (Nam) = E_Function then
6599            Check_Expression_Function (N, Nam);
6600         end if;
6601
6602      else
6603         Nam := Empty;
6604      end if;
6605
6606      --  For an allocator freeze designated type if not frozen already
6607
6608      --  For an aggregate whose component type is an access type, freeze the
6609      --  designated type now, so that its freeze does not appear within the
6610      --  loop that might be created in the expansion of the aggregate. If the
6611      --  designated type is a private type without full view, the expression
6612      --  cannot contain an allocator, so the type is not frozen.
6613
6614      --  For a function, we freeze the entity when the subprogram declaration
6615      --  is frozen, but a function call may appear in an initialization proc.
6616      --  before the declaration is frozen. We need to generate the extra
6617      --  formals, if any, to ensure that the expansion of the call includes
6618      --  the proper actuals. This only applies to Ada subprograms, not to
6619      --  imported ones.
6620
6621      Desig_Typ := Empty;
6622
6623      case Nkind (N) is
6624         when N_Allocator =>
6625            Desig_Typ := Designated_Type (Etype (N));
6626
6627         when N_Aggregate =>
6628            if Is_Array_Type (Etype (N))
6629              and then Is_Access_Type (Component_Type (Etype (N)))
6630            then
6631
6632               --  Check whether aggregate includes allocators.
6633
6634               Desig_Typ := Find_Aggregate_Component_Desig_Type;
6635            end if;
6636
6637         when N_Selected_Component |
6638            N_Indexed_Component    |
6639            N_Slice                =>
6640
6641            if Is_Access_Type (Etype (Prefix (N))) then
6642               Desig_Typ := Designated_Type (Etype (Prefix (N)));
6643            end if;
6644
6645         when N_Identifier =>
6646            if Present (Nam)
6647              and then Ekind (Nam) = E_Function
6648              and then Nkind (Parent (N)) = N_Function_Call
6649              and then Convention (Nam) = Convention_Ada
6650            then
6651               Create_Extra_Formals (Nam);
6652            end if;
6653
6654         when others =>
6655            null;
6656      end case;
6657
6658      if Desig_Typ /= Empty
6659        and then (Is_Frozen (Desig_Typ)
6660                   or else (not Is_Fully_Defined (Desig_Typ)))
6661      then
6662         Desig_Typ := Empty;
6663      end if;
6664
6665      --  All done if nothing needs freezing
6666
6667      if No (Typ)
6668        and then No (Nam)
6669        and then No (Desig_Typ)
6670      then
6671         return;
6672      end if;
6673
6674      --  Examine the enclosing context by climbing the parent chain. The
6675      --  traversal serves two purposes - to detect scenarios where freezeing
6676      --  is not needed and to find the proper insertion point for the freeze
6677      --  nodes. Although somewhat similar to Insert_Actions, this traversal
6678      --  is freezing semantics-sensitive. Inserting freeze nodes blindly in
6679      --  the tree may result in types being frozen too early.
6680
6681      P := N;
6682      loop
6683         Parent_P := Parent (P);
6684
6685         --  If we don't have a parent, then we are not in a well-formed tree.
6686         --  This is an unusual case, but there are some legitimate situations
6687         --  in which this occurs, notably when the expressions in the range of
6688         --  a type declaration are resolved. We simply ignore the freeze
6689         --  request in this case. Is this right ???
6690
6691         if No (Parent_P) then
6692            return;
6693         end if;
6694
6695         --  See if we have got to an appropriate point in the tree
6696
6697         case Nkind (Parent_P) is
6698
6699            --  A special test for the exception of (RM 13.14(8)) for the case
6700            --  of per-object expressions (RM 3.8(18)) occurring in component
6701            --  definition or a discrete subtype definition. Note that we test
6702            --  for a component declaration which includes both cases we are
6703            --  interested in, and furthermore the tree does not have explicit
6704            --  nodes for either of these two constructs.
6705
6706            when N_Component_Declaration =>
6707
6708               --  The case we want to test for here is an identifier that is
6709               --  a per-object expression, this is either a discriminant that
6710               --  appears in a context other than the component declaration
6711               --  or it is a reference to the type of the enclosing construct.
6712
6713               --  For either of these cases, we skip the freezing
6714
6715               if not In_Spec_Expression
6716                 and then Nkind (N) = N_Identifier
6717                 and then (Present (Entity (N)))
6718               then
6719                  --  We recognize the discriminant case by just looking for
6720                  --  a reference to a discriminant. It can only be one for
6721                  --  the enclosing construct. Skip freezing in this case.
6722
6723                  if Ekind (Entity (N)) = E_Discriminant then
6724                     return;
6725
6726                  --  For the case of a reference to the enclosing record,
6727                  --  (or task or protected type), we look for a type that
6728                  --  matches the current scope.
6729
6730                  elsif Entity (N) = Current_Scope then
6731                     return;
6732                  end if;
6733               end if;
6734
6735            --  If we have an enumeration literal that appears as the choice in
6736            --  the aggregate of an enumeration representation clause, then
6737            --  freezing does not occur (RM 13.14(10)).
6738
6739            when N_Enumeration_Representation_Clause =>
6740
6741               --  The case we are looking for is an enumeration literal
6742
6743               if (Nkind (N) = N_Identifier or Nkind (N) = N_Character_Literal)
6744                 and then Is_Enumeration_Type (Etype (N))
6745               then
6746                  --  If enumeration literal appears directly as the choice,
6747                  --  do not freeze (this is the normal non-overloaded case)
6748
6749                  if Nkind (Parent (N)) = N_Component_Association
6750                    and then First (Choices (Parent (N))) = N
6751                  then
6752                     return;
6753
6754                  --  If enumeration literal appears as the name of function
6755                  --  which is the choice, then also do not freeze. This
6756                  --  happens in the overloaded literal case, where the
6757                  --  enumeration literal is temporarily changed to a function
6758                  --  call for overloading analysis purposes.
6759
6760                  elsif Nkind (Parent (N)) = N_Function_Call
6761                     and then
6762                       Nkind (Parent (Parent (N))) = N_Component_Association
6763                     and then
6764                       First (Choices (Parent (Parent (N)))) = Parent (N)
6765                  then
6766                     return;
6767                  end if;
6768               end if;
6769
6770            --  Normally if the parent is a handled sequence of statements,
6771            --  then the current node must be a statement, and that is an
6772            --  appropriate place to insert a freeze node.
6773
6774            when N_Handled_Sequence_Of_Statements =>
6775
6776               --  An exception occurs when the sequence of statements is for
6777               --  an expander generated body that did not do the usual freeze
6778               --  all operation. In this case we usually want to freeze
6779               --  outside this body, not inside it, and we skip past the
6780               --  subprogram body that we are inside.
6781
6782               if In_Expanded_Body (Parent_P) then
6783                  declare
6784                     Subp : constant Node_Id := Parent (Parent_P);
6785                     Spec : Entity_Id;
6786
6787                  begin
6788                     --  Freeze the entity only when it is declared inside the
6789                     --  body of the expander generated procedure. This case
6790                     --  is recognized by the scope of the entity or its type,
6791                     --  which is either the spec for some enclosing body, or
6792                     --  (in the case of init_procs, for which there are no
6793                     --  separate specs) the current scope.
6794
6795                     if Nkind (Subp) = N_Subprogram_Body then
6796                        Spec := Corresponding_Spec (Subp);
6797
6798                        if (Present (Typ) and then Scope (Typ) = Spec)
6799                             or else
6800                           (Present (Nam) and then Scope (Nam) = Spec)
6801                        then
6802                           exit;
6803
6804                        elsif Present (Typ)
6805                          and then Scope (Typ) = Current_Scope
6806                          and then Defining_Entity (Subp) = Current_Scope
6807                        then
6808                           exit;
6809                        end if;
6810                     end if;
6811
6812                     --  An expression function may act as a completion of
6813                     --  a function declaration. As such, it can reference
6814                     --  entities declared between the two views:
6815
6816                     --     Hidden [];                             -- 1
6817                     --     function F return ...;
6818                     --     private
6819                     --        function Hidden return ...;
6820                     --        function F return ... is (Hidden);  -- 2
6821
6822                     --  Refering to the example above, freezing the expression
6823                     --  of F (2) would place Hidden's freeze node (1) in the
6824                     --  wrong place. Avoid explicit freezing and let the usual
6825                     --  scenarios do the job - for example, reaching the end
6826                     --  of the private declarations, or a call to F.
6827
6828                     if Nkind (Original_Node (Subp)) =
6829                                                N_Expression_Function
6830                     then
6831                        null;
6832
6833                     --  Freeze outside the body
6834
6835                     else
6836                        Parent_P := Parent (Parent_P);
6837                        Freeze_Outside := True;
6838                     end if;
6839                  end;
6840
6841               --  Here if normal case where we are in handled statement
6842               --  sequence and want to do the insertion right there.
6843
6844               else
6845                  exit;
6846               end if;
6847
6848            --  If parent is a body or a spec or a block, then the current node
6849            --  is a statement or declaration and we can insert the freeze node
6850            --  before it.
6851
6852            when N_Block_Statement       |
6853                 N_Entry_Body            |
6854                 N_Package_Body          |
6855                 N_Package_Specification |
6856                 N_Protected_Body        |
6857                 N_Subprogram_Body       |
6858                 N_Task_Body             => exit;
6859
6860            --  The expander is allowed to define types in any statements list,
6861            --  so any of the following parent nodes also mark a freezing point
6862            --  if the actual node is in a list of statements or declarations.
6863
6864            when N_Abortable_Part             |
6865                 N_Accept_Alternative         |
6866                 N_And_Then                   |
6867                 N_Case_Statement_Alternative |
6868                 N_Compilation_Unit_Aux       |
6869                 N_Conditional_Entry_Call     |
6870                 N_Delay_Alternative          |
6871                 N_Elsif_Part                 |
6872                 N_Entry_Call_Alternative     |
6873                 N_Exception_Handler          |
6874                 N_Extended_Return_Statement  |
6875                 N_Freeze_Entity              |
6876                 N_If_Statement               |
6877                 N_Or_Else                    |
6878                 N_Selective_Accept           |
6879                 N_Triggering_Alternative     =>
6880
6881               exit when Is_List_Member (P);
6882
6883            --  Freeze nodes produced by an expression coming from the Actions
6884            --  list of a N_Expression_With_Actions node must remain within the
6885            --  Actions list. Inserting the freeze nodes further up the tree
6886            --  may lead to use before declaration issues in the case of array
6887            --  types.
6888
6889            when N_Expression_With_Actions =>
6890               if Is_List_Member (P)
6891                 and then List_Containing (P) = Actions (Parent_P)
6892               then
6893                  exit;
6894               end if;
6895
6896            --  Note: N_Loop_Statement is a special case. A type that appears
6897            --  in the source can never be frozen in a loop (this occurs only
6898            --  because of a loop expanded by the expander), so we keep on
6899            --  going. Otherwise we terminate the search. Same is true of any
6900            --  entity which comes from source. (if they have predefined type,
6901            --  that type does not appear to come from source, but the entity
6902            --  should not be frozen here).
6903
6904            when N_Loop_Statement =>
6905               exit when not Comes_From_Source (Etype (N))
6906                 and then (No (Nam) or else not Comes_From_Source (Nam));
6907
6908            --  For all other cases, keep looking at parents
6909
6910            when others =>
6911               null;
6912         end case;
6913
6914         --  We fall through the case if we did not yet find the proper
6915         --  place in the free for inserting the freeze node, so climb.
6916
6917         P := Parent_P;
6918      end loop;
6919
6920      --  If the expression appears in a record or an initialization procedure,
6921      --  the freeze nodes are collected and attached to the current scope, to
6922      --  be inserted and analyzed on exit from the scope, to insure that
6923      --  generated entities appear in the correct scope. If the expression is
6924      --  a default for a discriminant specification, the scope is still void.
6925      --  The expression can also appear in the discriminant part of a private
6926      --  or concurrent type.
6927
6928      --  If the expression appears in a constrained subcomponent of an
6929      --  enclosing record declaration, the freeze nodes must be attached to
6930      --  the outer record type so they can eventually be placed in the
6931      --  enclosing declaration list.
6932
6933      --  The other case requiring this special handling is if we are in a
6934      --  default expression, since in that case we are about to freeze a
6935      --  static type, and the freeze scope needs to be the outer scope, not
6936      --  the scope of the subprogram with the default parameter.
6937
6938      --  For default expressions and other spec expressions in generic units,
6939      --  the Move_Freeze_Nodes mechanism (see sem_ch12.adb) takes care of
6940      --  placing them at the proper place, after the generic unit.
6941
6942      if (In_Spec_Exp and not Inside_A_Generic)
6943        or else Freeze_Outside
6944        or else (Is_Type (Current_Scope)
6945                  and then (not Is_Concurrent_Type (Current_Scope)
6946                             or else not Has_Completion (Current_Scope)))
6947        or else Ekind (Current_Scope) = E_Void
6948      then
6949         declare
6950            N            : constant Node_Id := Current_Scope;
6951            Freeze_Nodes : List_Id          := No_List;
6952            Pos          : Int              := Scope_Stack.Last;
6953
6954         begin
6955            if Present (Desig_Typ) then
6956               Freeze_And_Append (Desig_Typ, N, Freeze_Nodes);
6957            end if;
6958
6959            if Present (Typ) then
6960               Freeze_And_Append (Typ, N, Freeze_Nodes);
6961            end if;
6962
6963            if Present (Nam) then
6964               Freeze_And_Append (Nam, N, Freeze_Nodes);
6965            end if;
6966
6967            --  The current scope may be that of a constrained component of
6968            --  an enclosing record declaration, or of a loop of an enclosing
6969            --  quantified expression, which is above the current scope in the
6970            --  scope stack. Indeed in the context of a quantified expression,
6971            --  a scope is created and pushed above the current scope in order
6972            --  to emulate the loop-like behavior of the quantified expression.
6973            --  If the expression is within a top-level pragma, as for a pre-
6974            --  condition on a library-level subprogram, nothing to do.
6975
6976            if not Is_Compilation_Unit (Current_Scope)
6977              and then (Is_Record_Type (Scope (Current_Scope))
6978                         or else Nkind (Parent (Current_Scope)) =
6979                                                     N_Quantified_Expression)
6980            then
6981               Pos := Pos - 1;
6982            end if;
6983
6984            if Is_Non_Empty_List (Freeze_Nodes) then
6985               if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
6986                  Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
6987                    Freeze_Nodes;
6988               else
6989                  Append_List (Freeze_Nodes,
6990                    Scope_Stack.Table (Pos).Pending_Freeze_Actions);
6991               end if;
6992            end if;
6993         end;
6994
6995         return;
6996      end if;
6997
6998      --  Now we have the right place to do the freezing. First, a special
6999      --  adjustment, if we are in spec-expression analysis mode, these freeze
7000      --  actions must not be thrown away (normally all inserted actions are
7001      --  thrown away in this mode. However, the freeze actions are from static
7002      --  expressions and one of the important reasons we are doing this
7003      --  special analysis is to get these freeze actions. Therefore we turn
7004      --  off the In_Spec_Expression mode to propagate these freeze actions.
7005      --  This also means they get properly analyzed and expanded.
7006
7007      In_Spec_Expression := False;
7008
7009      --  Freeze the designated type of an allocator (RM 13.14(13))
7010
7011      if Present (Desig_Typ) then
7012         Freeze_Before (P, Desig_Typ);
7013      end if;
7014
7015      --  Freeze type of expression (RM 13.14(10)). Note that we took care of
7016      --  the enumeration representation clause exception in the loop above.
7017
7018      if Present (Typ) then
7019         Freeze_Before (P, Typ);
7020      end if;
7021
7022      --  Freeze name if one is present (RM 13.14(11))
7023
7024      if Present (Nam) then
7025         Freeze_Before (P, Nam);
7026      end if;
7027
7028      --  Restore In_Spec_Expression flag
7029
7030      In_Spec_Expression := In_Spec_Exp;
7031   end Freeze_Expression;
7032
7033   -----------------------------
7034   -- Freeze_Fixed_Point_Type --
7035   -----------------------------
7036
7037   --  Certain fixed-point types and subtypes, including implicit base types
7038   --  and declared first subtypes, have not yet set up a range. This is
7039   --  because the range cannot be set until the Small and Size values are
7040   --  known, and these are not known till the type is frozen.
7041
7042   --  To signal this case, Scalar_Range contains an unanalyzed syntactic range
7043   --  whose bounds are unanalyzed real literals. This routine will recognize
7044   --  this case, and transform this range node into a properly typed range
7045   --  with properly analyzed and resolved values.
7046
7047   procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
7048      Rng   : constant Node_Id    := Scalar_Range (Typ);
7049      Lo    : constant Node_Id    := Low_Bound (Rng);
7050      Hi    : constant Node_Id    := High_Bound (Rng);
7051      Btyp  : constant Entity_Id  := Base_Type (Typ);
7052      Brng  : constant Node_Id    := Scalar_Range (Btyp);
7053      BLo   : constant Node_Id    := Low_Bound (Brng);
7054      BHi   : constant Node_Id    := High_Bound (Brng);
7055      Small : constant Ureal      := Small_Value (Typ);
7056      Loval : Ureal;
7057      Hival : Ureal;
7058      Atype : Entity_Id;
7059
7060      Orig_Lo : Ureal;
7061      Orig_Hi : Ureal;
7062      --  Save original bounds (for shaving tests)
7063
7064      Actual_Size : Nat;
7065      --  Actual size chosen
7066
7067      function Fsize (Lov, Hiv : Ureal) return Nat;
7068      --  Returns size of type with given bounds. Also leaves these
7069      --  bounds set as the current bounds of the Typ.
7070
7071      -----------
7072      -- Fsize --
7073      -----------
7074
7075      function Fsize (Lov, Hiv : Ureal) return Nat is
7076      begin
7077         Set_Realval (Lo, Lov);
7078         Set_Realval (Hi, Hiv);
7079         return Minimum_Size (Typ);
7080      end Fsize;
7081
7082   --  Start of processing for Freeze_Fixed_Point_Type
7083
7084   begin
7085      --  If Esize of a subtype has not previously been set, set it now
7086
7087      if Unknown_Esize (Typ) then
7088         Atype := Ancestor_Subtype (Typ);
7089
7090         if Present (Atype) then
7091            Set_Esize (Typ, Esize (Atype));
7092         else
7093            Set_Esize (Typ, Esize (Base_Type (Typ)));
7094         end if;
7095      end if;
7096
7097      --  Immediate return if the range is already analyzed. This means that
7098      --  the range is already set, and does not need to be computed by this
7099      --  routine.
7100
7101      if Analyzed (Rng) then
7102         return;
7103      end if;
7104
7105      --  Immediate return if either of the bounds raises Constraint_Error
7106
7107      if Raises_Constraint_Error (Lo)
7108        or else Raises_Constraint_Error (Hi)
7109      then
7110         return;
7111      end if;
7112
7113      Loval := Realval (Lo);
7114      Hival := Realval (Hi);
7115
7116      Orig_Lo := Loval;
7117      Orig_Hi := Hival;
7118
7119      --  Ordinary fixed-point case
7120
7121      if Is_Ordinary_Fixed_Point_Type (Typ) then
7122
7123         --  For the ordinary fixed-point case, we are allowed to fudge the
7124         --  end-points up or down by small. Generally we prefer to fudge up,
7125         --  i.e. widen the bounds for non-model numbers so that the end points
7126         --  are included. However there are cases in which this cannot be
7127         --  done, and indeed cases in which we may need to narrow the bounds.
7128         --  The following circuit makes the decision.
7129
7130         --  Note: our terminology here is that Incl_EP means that the bounds
7131         --  are widened by Small if necessary to include the end points, and
7132         --  Excl_EP means that the bounds are narrowed by Small to exclude the
7133         --  end-points if this reduces the size.
7134
7135         --  Note that in the Incl case, all we care about is including the
7136         --  end-points. In the Excl case, we want to narrow the bounds as
7137         --  much as permitted by the RM, to give the smallest possible size.
7138
7139         Fudge : declare
7140            Loval_Incl_EP : Ureal;
7141            Hival_Incl_EP : Ureal;
7142
7143            Loval_Excl_EP : Ureal;
7144            Hival_Excl_EP : Ureal;
7145
7146            Size_Incl_EP  : Nat;
7147            Size_Excl_EP  : Nat;
7148
7149            Model_Num     : Ureal;
7150            First_Subt    : Entity_Id;
7151            Actual_Lo     : Ureal;
7152            Actual_Hi     : Ureal;
7153
7154         begin
7155            --  First step. Base types are required to be symmetrical. Right
7156            --  now, the base type range is a copy of the first subtype range.
7157            --  This will be corrected before we are done, but right away we
7158            --  need to deal with the case where both bounds are non-negative.
7159            --  In this case, we set the low bound to the negative of the high
7160            --  bound, to make sure that the size is computed to include the
7161            --  required sign. Note that we do not need to worry about the
7162            --  case of both bounds negative, because the sign will be dealt
7163            --  with anyway. Furthermore we can't just go making such a bound
7164            --  symmetrical, since in a twos-complement system, there is an
7165            --  extra negative value which could not be accommodated on the
7166            --  positive side.
7167
7168            if Typ = Btyp
7169              and then not UR_Is_Negative (Loval)
7170              and then Hival > Loval
7171            then
7172               Loval := -Hival;
7173               Set_Realval (Lo, Loval);
7174            end if;
7175
7176            --  Compute the fudged bounds. If the number is a model number,
7177            --  then we do nothing to include it, but we are allowed to backoff
7178            --  to the next adjacent model number when we exclude it. If it is
7179            --  not a model number then we straddle the two values with the
7180            --  model numbers on either side.
7181
7182            Model_Num := UR_Trunc (Loval / Small) * Small;
7183
7184            if Loval = Model_Num then
7185               Loval_Incl_EP := Model_Num;
7186            else
7187               Loval_Incl_EP := Model_Num - Small;
7188            end if;
7189
7190            --  The low value excluding the end point is Small greater, but
7191            --  we do not do this exclusion if the low value is positive,
7192            --  since it can't help the size and could actually hurt by
7193            --  crossing the high bound.
7194
7195            if UR_Is_Negative (Loval_Incl_EP) then
7196               Loval_Excl_EP := Loval_Incl_EP + Small;
7197
7198               --  If the value went from negative to zero, then we have the
7199               --  case where Loval_Incl_EP is the model number just below
7200               --  zero, so we want to stick to the negative value for the
7201               --  base type to maintain the condition that the size will
7202               --  include signed values.
7203
7204               if Typ = Btyp
7205                 and then UR_Is_Zero (Loval_Excl_EP)
7206               then
7207                  Loval_Excl_EP := Loval_Incl_EP;
7208               end if;
7209
7210            else
7211               Loval_Excl_EP := Loval_Incl_EP;
7212            end if;
7213
7214            --  Similar processing for upper bound and high value
7215
7216            Model_Num := UR_Trunc (Hival / Small) * Small;
7217
7218            if Hival = Model_Num then
7219               Hival_Incl_EP := Model_Num;
7220            else
7221               Hival_Incl_EP := Model_Num + Small;
7222            end if;
7223
7224            if UR_Is_Positive (Hival_Incl_EP) then
7225               Hival_Excl_EP := Hival_Incl_EP - Small;
7226            else
7227               Hival_Excl_EP := Hival_Incl_EP;
7228            end if;
7229
7230            --  One further adjustment is needed. In the case of subtypes, we
7231            --  cannot go outside the range of the base type, or we get
7232            --  peculiarities, and the base type range is already set. This
7233            --  only applies to the Incl values, since clearly the Excl values
7234            --  are already as restricted as they are allowed to be.
7235
7236            if Typ /= Btyp then
7237               Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
7238               Hival_Incl_EP := UR_Min (Hival_Incl_EP, Realval (BHi));
7239            end if;
7240
7241            --  Get size including and excluding end points
7242
7243            Size_Incl_EP := Fsize (Loval_Incl_EP, Hival_Incl_EP);
7244            Size_Excl_EP := Fsize (Loval_Excl_EP, Hival_Excl_EP);
7245
7246            --  No need to exclude end-points if it does not reduce size
7247
7248            if Fsize (Loval_Incl_EP, Hival_Excl_EP) = Size_Excl_EP then
7249               Loval_Excl_EP := Loval_Incl_EP;
7250            end if;
7251
7252            if Fsize (Loval_Excl_EP, Hival_Incl_EP) = Size_Excl_EP then
7253               Hival_Excl_EP := Hival_Incl_EP;
7254            end if;
7255
7256            --  Now we set the actual size to be used. We want to use the
7257            --  bounds fudged up to include the end-points but only if this
7258            --  can be done without violating a specifically given size
7259            --  size clause or causing an unacceptable increase in size.
7260
7261            --  Case of size clause given
7262
7263            if Has_Size_Clause (Typ) then
7264
7265               --  Use the inclusive size only if it is consistent with
7266               --  the explicitly specified size.
7267
7268               if Size_Incl_EP <= RM_Size (Typ) then
7269                  Actual_Lo   := Loval_Incl_EP;
7270                  Actual_Hi   := Hival_Incl_EP;
7271                  Actual_Size := Size_Incl_EP;
7272
7273               --  If the inclusive size is too large, we try excluding
7274               --  the end-points (will be caught later if does not work).
7275
7276               else
7277                  Actual_Lo   := Loval_Excl_EP;
7278                  Actual_Hi   := Hival_Excl_EP;
7279                  Actual_Size := Size_Excl_EP;
7280               end if;
7281
7282            --  Case of size clause not given
7283
7284            else
7285               --  If we have a base type whose corresponding first subtype
7286               --  has an explicit size that is large enough to include our
7287               --  end-points, then do so. There is no point in working hard
7288               --  to get a base type whose size is smaller than the specified
7289               --  size of the first subtype.
7290
7291               First_Subt := First_Subtype (Typ);
7292
7293               if Has_Size_Clause (First_Subt)
7294                 and then Size_Incl_EP <= Esize (First_Subt)
7295               then
7296                  Actual_Size := Size_Incl_EP;
7297                  Actual_Lo   := Loval_Incl_EP;
7298                  Actual_Hi   := Hival_Incl_EP;
7299
7300               --  If excluding the end-points makes the size smaller and
7301               --  results in a size of 8,16,32,64, then we take the smaller
7302               --  size. For the 64 case, this is compulsory. For the other
7303               --  cases, it seems reasonable. We like to include end points
7304               --  if we can, but not at the expense of moving to the next
7305               --  natural boundary of size.
7306
7307               elsif Size_Incl_EP /= Size_Excl_EP
7308                 and then Addressable (Size_Excl_EP)
7309               then
7310                  Actual_Size := Size_Excl_EP;
7311                  Actual_Lo   := Loval_Excl_EP;
7312                  Actual_Hi   := Hival_Excl_EP;
7313
7314               --  Otherwise we can definitely include the end points
7315
7316               else
7317                  Actual_Size := Size_Incl_EP;
7318                  Actual_Lo   := Loval_Incl_EP;
7319                  Actual_Hi   := Hival_Incl_EP;
7320               end if;
7321
7322               --  One pathological case: normally we never fudge a low bound
7323               --  down, since it would seem to increase the size (if it has
7324               --  any effect), but for ranges containing single value, or no
7325               --  values, the high bound can be small too large. Consider:
7326
7327               --    type t is delta 2.0**(-14)
7328               --      range 131072.0 .. 0;
7329
7330               --  That lower bound is *just* outside the range of 32 bits, and
7331               --  does need fudging down in this case. Note that the bounds
7332               --  will always have crossed here, since the high bound will be
7333               --  fudged down if necessary, as in the case of:
7334
7335               --    type t is delta 2.0**(-14)
7336               --      range 131072.0 .. 131072.0;
7337
7338               --  So we detect the situation by looking for crossed bounds,
7339               --  and if the bounds are crossed, and the low bound is greater
7340               --  than zero, we will always back it off by small, since this
7341               --  is completely harmless.
7342
7343               if Actual_Lo > Actual_Hi then
7344                  if UR_Is_Positive (Actual_Lo) then
7345                     Actual_Lo   := Loval_Incl_EP - Small;
7346                     Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7347
7348                  --  And of course, we need to do exactly the same parallel
7349                  --  fudge for flat ranges in the negative region.
7350
7351                  elsif UR_Is_Negative (Actual_Hi) then
7352                     Actual_Hi := Hival_Incl_EP + Small;
7353                     Actual_Size := Fsize (Actual_Lo, Actual_Hi);
7354                  end if;
7355               end if;
7356            end if;
7357
7358            Set_Realval (Lo, Actual_Lo);
7359            Set_Realval (Hi, Actual_Hi);
7360         end Fudge;
7361
7362      --  For the decimal case, none of this fudging is required, since there
7363      --  are no end-point problems in the decimal case (the end-points are
7364      --  always included).
7365
7366      else
7367         Actual_Size := Fsize (Loval, Hival);
7368      end if;
7369
7370      --  At this stage, the actual size has been calculated and the proper
7371      --  required bounds are stored in the low and high bounds.
7372
7373      if Actual_Size > 64 then
7374         Error_Msg_Uint_1 := UI_From_Int (Actual_Size);
7375         Error_Msg_N
7376           ("size required (^) for type& too large, maximum allowed is 64",
7377            Typ);
7378         Actual_Size := 64;
7379      end if;
7380
7381      --  Check size against explicit given size
7382
7383      if Has_Size_Clause (Typ) then
7384         if Actual_Size > RM_Size (Typ) then
7385            Error_Msg_Uint_1 := RM_Size (Typ);
7386            Error_Msg_Uint_2 := UI_From_Int (Actual_Size);
7387            Error_Msg_NE
7388              ("size given (^) for type& too small, minimum allowed is ^",
7389               Size_Clause (Typ), Typ);
7390
7391         else
7392            Actual_Size := UI_To_Int (Esize (Typ));
7393         end if;
7394
7395      --  Increase size to next natural boundary if no size clause given
7396
7397      else
7398         if Actual_Size <= 8 then
7399            Actual_Size := 8;
7400         elsif Actual_Size <= 16 then
7401            Actual_Size := 16;
7402         elsif Actual_Size <= 32 then
7403            Actual_Size := 32;
7404         else
7405            Actual_Size := 64;
7406         end if;
7407
7408         Init_Esize (Typ, Actual_Size);
7409         Adjust_Esize_For_Alignment (Typ);
7410      end if;
7411
7412      --  If we have a base type, then expand the bounds so that they extend to
7413      --  the full width of the allocated size in bits, to avoid junk range
7414      --  checks on intermediate computations.
7415
7416      if Base_Type (Typ) = Typ then
7417         Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
7418         Set_Realval (Hi,  (Small * (Uint_2 ** (Actual_Size - 1) - 1)));
7419      end if;
7420
7421      --  Final step is to reanalyze the bounds using the proper type
7422      --  and set the Corresponding_Integer_Value fields of the literals.
7423
7424      Set_Etype (Lo, Empty);
7425      Set_Analyzed (Lo, False);
7426      Analyze (Lo);
7427
7428      --  Resolve with universal fixed if the base type, and the base type if
7429      --  it is a subtype. Note we can't resolve the base type with itself,
7430      --  that would be a reference before definition.
7431
7432      if Typ = Btyp then
7433         Resolve (Lo, Universal_Fixed);
7434      else
7435         Resolve (Lo, Btyp);
7436      end if;
7437
7438      --  Set corresponding integer value for bound
7439
7440      Set_Corresponding_Integer_Value
7441        (Lo, UR_To_Uint (Realval (Lo) / Small));
7442
7443      --  Similar processing for high bound
7444
7445      Set_Etype (Hi, Empty);
7446      Set_Analyzed (Hi, False);
7447      Analyze (Hi);
7448
7449      if Typ = Btyp then
7450         Resolve (Hi, Universal_Fixed);
7451      else
7452         Resolve (Hi, Btyp);
7453      end if;
7454
7455      Set_Corresponding_Integer_Value
7456        (Hi, UR_To_Uint (Realval (Hi) / Small));
7457
7458      --  Set type of range to correspond to bounds
7459
7460      Set_Etype (Rng, Etype (Lo));
7461
7462      --  Set Esize to calculated size if not set already
7463
7464      if Unknown_Esize (Typ) then
7465         Init_Esize (Typ, Actual_Size);
7466      end if;
7467
7468      --  Set RM_Size if not already set. If already set, check value
7469
7470      declare
7471         Minsiz : constant Uint := UI_From_Int (Minimum_Size (Typ));
7472
7473      begin
7474         if RM_Size (Typ) /= Uint_0 then
7475            if RM_Size (Typ) < Minsiz then
7476               Error_Msg_Uint_1 := RM_Size (Typ);
7477               Error_Msg_Uint_2 := Minsiz;
7478               Error_Msg_NE
7479                 ("size given (^) for type& too small, minimum allowed is ^",
7480                  Size_Clause (Typ), Typ);
7481            end if;
7482
7483         else
7484            Set_RM_Size (Typ, Minsiz);
7485         end if;
7486      end;
7487
7488      --  Check for shaving
7489
7490      if Comes_From_Source (Typ) then
7491         if Orig_Lo < Expr_Value_R (Lo) then
7492            Error_Msg_N
7493              ("declared low bound of type & is outside type range??", Typ);
7494            Error_Msg_N
7495              ("\low bound adjusted up by delta (RM 3.5.9(13))??", Typ);
7496         end if;
7497
7498         if Orig_Hi > Expr_Value_R (Hi) then
7499            Error_Msg_N
7500              ("declared high bound of type & is outside type range??", Typ);
7501            Error_Msg_N
7502              ("\high bound adjusted down by delta (RM 3.5.9(13))??", Typ);
7503         end if;
7504      end if;
7505   end Freeze_Fixed_Point_Type;
7506
7507   ------------------
7508   -- Freeze_Itype --
7509   ------------------
7510
7511   procedure Freeze_Itype (T : Entity_Id; N : Node_Id) is
7512      L : List_Id;
7513
7514   begin
7515      Set_Has_Delayed_Freeze (T);
7516      L := Freeze_Entity (T, N);
7517
7518      if Is_Non_Empty_List (L) then
7519         Insert_Actions (N, L);
7520      end if;
7521   end Freeze_Itype;
7522
7523   --------------------------
7524   -- Freeze_Static_Object --
7525   --------------------------
7526
7527   procedure Freeze_Static_Object (E : Entity_Id) is
7528
7529      Cannot_Be_Static : exception;
7530      --  Exception raised if the type of a static object cannot be made
7531      --  static. This happens if the type depends on non-global objects.
7532
7533      procedure Ensure_Expression_Is_SA (N : Node_Id);
7534      --  Called to ensure that an expression used as part of a type definition
7535      --  is statically allocatable, which means that the expression type is
7536      --  statically allocatable, and the expression is either static, or a
7537      --  reference to a library level constant.
7538
7539      procedure Ensure_Type_Is_SA (Typ : Entity_Id);
7540      --  Called to mark a type as static, checking that it is possible
7541      --  to set the type as static. If it is not possible, then the
7542      --  exception Cannot_Be_Static is raised.
7543
7544      -----------------------------
7545      -- Ensure_Expression_Is_SA --
7546      -----------------------------
7547
7548      procedure Ensure_Expression_Is_SA (N : Node_Id) is
7549         Ent : Entity_Id;
7550
7551      begin
7552         Ensure_Type_Is_SA (Etype (N));
7553
7554         if Is_OK_Static_Expression (N) then
7555            return;
7556
7557         elsif Nkind (N) = N_Identifier then
7558            Ent := Entity (N);
7559
7560            if Present (Ent)
7561              and then Ekind (Ent) = E_Constant
7562              and then Is_Library_Level_Entity (Ent)
7563            then
7564               return;
7565            end if;
7566         end if;
7567
7568         raise Cannot_Be_Static;
7569      end Ensure_Expression_Is_SA;
7570
7571      -----------------------
7572      -- Ensure_Type_Is_SA --
7573      -----------------------
7574
7575      procedure Ensure_Type_Is_SA (Typ : Entity_Id) is
7576         N : Node_Id;
7577         C : Entity_Id;
7578
7579      begin
7580         --  If type is library level, we are all set
7581
7582         if Is_Library_Level_Entity (Typ) then
7583            return;
7584         end if;
7585
7586         --  We are also OK if the type already marked as statically allocated,
7587         --  which means we processed it before.
7588
7589         if Is_Statically_Allocated (Typ) then
7590            return;
7591         end if;
7592
7593         --  Mark type as statically allocated
7594
7595         Set_Is_Statically_Allocated (Typ);
7596
7597         --  Check that it is safe to statically allocate this type
7598
7599         if Is_Scalar_Type (Typ) or else Is_Real_Type (Typ) then
7600            Ensure_Expression_Is_SA (Type_Low_Bound (Typ));
7601            Ensure_Expression_Is_SA (Type_High_Bound (Typ));
7602
7603         elsif Is_Array_Type (Typ) then
7604            N := First_Index (Typ);
7605            while Present (N) loop
7606               Ensure_Type_Is_SA (Etype (N));
7607               Next_Index (N);
7608            end loop;
7609
7610            Ensure_Type_Is_SA (Component_Type (Typ));
7611
7612         elsif Is_Access_Type (Typ) then
7613            if Ekind (Designated_Type (Typ)) = E_Subprogram_Type then
7614
7615               declare
7616                  F : Entity_Id;
7617                  T : constant Entity_Id := Etype (Designated_Type (Typ));
7618
7619               begin
7620                  if T /= Standard_Void_Type then
7621                     Ensure_Type_Is_SA (T);
7622                  end if;
7623
7624                  F := First_Formal (Designated_Type (Typ));
7625                  while Present (F) loop
7626                     Ensure_Type_Is_SA (Etype (F));
7627                     Next_Formal (F);
7628                  end loop;
7629               end;
7630
7631            else
7632               Ensure_Type_Is_SA (Designated_Type (Typ));
7633            end if;
7634
7635         elsif Is_Record_Type (Typ) then
7636            C := First_Entity (Typ);
7637            while Present (C) loop
7638               if Ekind (C) = E_Discriminant
7639                 or else Ekind (C) = E_Component
7640               then
7641                  Ensure_Type_Is_SA (Etype (C));
7642
7643               elsif Is_Type (C) then
7644                  Ensure_Type_Is_SA (C);
7645               end if;
7646
7647               Next_Entity (C);
7648            end loop;
7649
7650         elsif Ekind (Typ) = E_Subprogram_Type then
7651            Ensure_Type_Is_SA (Etype (Typ));
7652
7653            C := First_Formal (Typ);
7654            while Present (C) loop
7655               Ensure_Type_Is_SA (Etype (C));
7656               Next_Formal (C);
7657            end loop;
7658
7659         else
7660            raise Cannot_Be_Static;
7661         end if;
7662      end Ensure_Type_Is_SA;
7663
7664   --  Start of processing for Freeze_Static_Object
7665
7666   begin
7667      Ensure_Type_Is_SA (Etype (E));
7668
7669   exception
7670      when Cannot_Be_Static =>
7671
7672         --  If the object that cannot be static is imported or exported, then
7673         --  issue an error message saying that this object cannot be imported
7674         --  or exported. If it has an address clause it is an overlay in the
7675         --  current partition and the static requirement is not relevant.
7676         --  Do not issue any error message when ignoring rep clauses.
7677
7678         if Ignore_Rep_Clauses then
7679            null;
7680
7681         elsif Is_Imported (E) then
7682            if No (Address_Clause (E)) then
7683               Error_Msg_N
7684                 ("& cannot be imported (local type is not constant)", E);
7685            end if;
7686
7687         --  Otherwise must be exported, something is wrong if compiler
7688         --  is marking something as statically allocated which cannot be).
7689
7690         else pragma Assert (Is_Exported (E));
7691            Error_Msg_N
7692              ("& cannot be exported (local type is not constant)", E);
7693         end if;
7694   end Freeze_Static_Object;
7695
7696   -----------------------
7697   -- Freeze_Subprogram --
7698   -----------------------
7699
7700   procedure Freeze_Subprogram (E : Entity_Id) is
7701      Retype : Entity_Id;
7702      F      : Entity_Id;
7703
7704   begin
7705      --  Subprogram may not have an address clause unless it is imported
7706
7707      if Present (Address_Clause (E)) then
7708         if not Is_Imported (E) then
7709            Error_Msg_N
7710              ("address clause can only be given " &
7711               "for imported subprogram",
7712               Name (Address_Clause (E)));
7713         end if;
7714      end if;
7715
7716      --  Reset the Pure indication on an imported subprogram unless an
7717      --  explicit Pure_Function pragma was present or the subprogram is an
7718      --  intrinsic. We do this because otherwise it is an insidious error
7719      --  to call a non-pure function from pure unit and have calls
7720      --  mysteriously optimized away. What happens here is that the Import
7721      --  can bypass the normal check to ensure that pure units call only pure
7722      --  subprograms.
7723
7724      --  The reason for the intrinsic exception is that in general, intrinsic
7725      --  functions (such as shifts) are pure anyway. The only exceptions are
7726      --  the intrinsics in GNAT.Source_Info, and that unit is not marked Pure
7727      --  in any case, so no problem arises.
7728
7729      if Is_Imported (E)
7730        and then Is_Pure (E)
7731        and then not Has_Pragma_Pure_Function (E)
7732        and then not Is_Intrinsic_Subprogram (E)
7733      then
7734         Set_Is_Pure (E, False);
7735      end if;
7736
7737      --  We also reset the Pure indication on a subprogram with an Address
7738      --  parameter, because the parameter may be used as a pointer and the
7739      --  referenced data may change even if the address value does not.
7740
7741      --  Note that if the programmer gave an explicit Pure_Function pragma,
7742      --  then we believe the programmer, and leave the subprogram Pure.
7743      --  We also suppress this check on run-time files.
7744
7745      if Is_Pure (E)
7746        and then Is_Subprogram (E)
7747        and then not Has_Pragma_Pure_Function (E)
7748        and then not Is_Internal_File_Name (Unit_File_Name (Current_Sem_Unit))
7749      then
7750         Check_Function_With_Address_Parameter (E);
7751      end if;
7752
7753      --  For non-foreign convention subprograms, this is where we create
7754      --  the extra formals (for accessibility level and constrained bit
7755      --  information). We delay this till the freeze point precisely so
7756      --  that we know the convention.
7757
7758      if not Has_Foreign_Convention (E) then
7759         Create_Extra_Formals (E);
7760         Set_Mechanisms (E);
7761
7762         --  If this is convention Ada and a Valued_Procedure, that's odd
7763
7764         if Ekind (E) = E_Procedure
7765           and then Is_Valued_Procedure (E)
7766           and then Convention (E) = Convention_Ada
7767           and then Warn_On_Export_Import
7768         then
7769            Error_Msg_N
7770              ("??Valued_Procedure has no effect for convention Ada", E);
7771            Set_Is_Valued_Procedure (E, False);
7772         end if;
7773
7774      --  Case of foreign convention
7775
7776      else
7777         Set_Mechanisms (E);
7778
7779         --  For foreign conventions, warn about return of unconstrained array
7780
7781         if Ekind (E) = E_Function then
7782            Retype := Underlying_Type (Etype (E));
7783
7784            --  If no return type, probably some other error, e.g. a
7785            --  missing full declaration, so ignore.
7786
7787            if No (Retype) then
7788               null;
7789
7790            --  If the return type is generic, we have emitted a warning
7791            --  earlier on, and there is nothing else to check here. Specific
7792            --  instantiations may lead to erroneous behavior.
7793
7794            elsif Is_Generic_Type (Etype (E)) then
7795               null;
7796
7797            --  Display warning if returning unconstrained array
7798
7799            elsif Is_Array_Type (Retype)
7800              and then not Is_Constrained (Retype)
7801
7802               --  Check appropriate warning is enabled (should we check for
7803               --  Warnings (Off) on specific entities here, probably so???)
7804
7805              and then Warn_On_Export_Import
7806            then
7807               Error_Msg_N
7808                ("?x?foreign convention function& should not return " &
7809                  "unconstrained array", E);
7810               return;
7811            end if;
7812         end if;
7813
7814         --  If any of the formals for an exported foreign convention
7815         --  subprogram have defaults, then emit an appropriate warning since
7816         --  this is odd (default cannot be used from non-Ada code)
7817
7818         if Is_Exported (E) then
7819            F := First_Formal (E);
7820            while Present (F) loop
7821               if Warn_On_Export_Import
7822                 and then Present (Default_Value (F))
7823               then
7824                  Error_Msg_N
7825                    ("?x?parameter cannot be defaulted in non-Ada call",
7826                     Default_Value (F));
7827               end if;
7828
7829               Next_Formal (F);
7830            end loop;
7831         end if;
7832      end if;
7833
7834      --  Pragma Inline_Always is disallowed for dispatching subprograms
7835      --  because the address of such subprograms is saved in the dispatch
7836      --  table to support dispatching calls, and dispatching calls cannot
7837      --  be inlined. This is consistent with the restriction against using
7838      --  'Access or 'Address on an Inline_Always subprogram.
7839
7840      if Is_Dispatching_Operation (E)
7841        and then Has_Pragma_Inline_Always (E)
7842      then
7843         Error_Msg_N
7844           ("pragma Inline_Always not allowed for dispatching subprograms", E);
7845      end if;
7846
7847      --  Because of the implicit representation of inherited predefined
7848      --  operators in the front-end, the overriding status of the operation
7849      --  may be affected when a full view of a type is analyzed, and this is
7850      --  not captured by the analysis of the corresponding type declaration.
7851      --  Therefore the correctness of a not-overriding indicator must be
7852      --  rechecked when the subprogram is frozen.
7853
7854      if Nkind (E) = N_Defining_Operator_Symbol
7855        and then not Error_Posted (Parent (E))
7856      then
7857         Check_Overriding_Indicator (E, Empty, Is_Primitive (E));
7858      end if;
7859   end Freeze_Subprogram;
7860
7861   ----------------------
7862   -- Is_Fully_Defined --
7863   ----------------------
7864
7865   function Is_Fully_Defined (T : Entity_Id) return Boolean is
7866   begin
7867      if Ekind (T) = E_Class_Wide_Type then
7868         return Is_Fully_Defined (Etype (T));
7869
7870      elsif Is_Array_Type (T) then
7871         return Is_Fully_Defined (Component_Type (T));
7872
7873      elsif Is_Record_Type (T)
7874        and not Is_Private_Type (T)
7875      then
7876         --  Verify that the record type has no components with private types
7877         --  without completion.
7878
7879         declare
7880            Comp : Entity_Id;
7881
7882         begin
7883            Comp := First_Component (T);
7884            while Present (Comp) loop
7885               if not Is_Fully_Defined (Etype (Comp)) then
7886                  return False;
7887               end if;
7888
7889               Next_Component (Comp);
7890            end loop;
7891            return True;
7892         end;
7893
7894      --  For the designated type of an access to subprogram, all types in
7895      --  the profile must be fully defined.
7896
7897      elsif Ekind (T) = E_Subprogram_Type then
7898         declare
7899            F : Entity_Id;
7900
7901         begin
7902            F := First_Formal (T);
7903            while Present (F) loop
7904               if not Is_Fully_Defined (Etype (F)) then
7905                  return False;
7906               end if;
7907
7908               Next_Formal (F);
7909            end loop;
7910
7911            return Is_Fully_Defined (Etype (T));
7912         end;
7913
7914      else
7915         return not Is_Private_Type (T)
7916           or else Present (Full_View (Base_Type (T)));
7917      end if;
7918   end Is_Fully_Defined;
7919
7920   ---------------------------------
7921   -- Process_Default_Expressions --
7922   ---------------------------------
7923
7924   procedure Process_Default_Expressions
7925     (E     : Entity_Id;
7926      After : in out Node_Id)
7927   is
7928      Loc    : constant Source_Ptr := Sloc (E);
7929      Dbody  : Node_Id;
7930      Formal : Node_Id;
7931      Dcopy  : Node_Id;
7932      Dnam   : Entity_Id;
7933
7934   begin
7935      Set_Default_Expressions_Processed (E);
7936
7937      --  A subprogram instance and its associated anonymous subprogram share
7938      --  their signature. The default expression functions are defined in the
7939      --  wrapper packages for the anonymous subprogram, and should not be
7940      --  generated again for the instance.
7941
7942      if Is_Generic_Instance (E)
7943        and then Present (Alias (E))
7944        and then Default_Expressions_Processed (Alias (E))
7945      then
7946         return;
7947      end if;
7948
7949      Formal := First_Formal (E);
7950      while Present (Formal) loop
7951         if Present (Default_Value (Formal)) then
7952
7953            --  We work with a copy of the default expression because we
7954            --  do not want to disturb the original, since this would mess
7955            --  up the conformance checking.
7956
7957            Dcopy := New_Copy_Tree (Default_Value (Formal));
7958
7959            --  The analysis of the expression may generate insert actions,
7960            --  which of course must not be executed. We wrap those actions
7961            --  in a procedure that is not called, and later on eliminated.
7962            --  The following cases have no side-effects, and are analyzed
7963            --  directly.
7964
7965            if Nkind (Dcopy) = N_Identifier
7966              or else Nkind_In (Dcopy, N_Expanded_Name,
7967                                       N_Integer_Literal,
7968                                       N_Character_Literal,
7969                                       N_String_Literal,
7970                                       N_Real_Literal)
7971              or else (Nkind (Dcopy) = N_Attribute_Reference
7972                        and then Attribute_Name (Dcopy) = Name_Null_Parameter)
7973              or else Known_Null (Dcopy)
7974            then
7975               --  If there is no default function, we must still do a full
7976               --  analyze call on the default value, to ensure that all error
7977               --  checks are performed, e.g. those associated with static
7978               --  evaluation. Note: this branch will always be taken if the
7979               --  analyzer is turned off (but we still need the error checks).
7980
7981               --  Note: the setting of parent here is to meet the requirement
7982               --  that we can only analyze the expression while attached to
7983               --  the tree. Really the requirement is that the parent chain
7984               --  be set, we don't actually need to be in the tree.
7985
7986               Set_Parent (Dcopy, Declaration_Node (Formal));
7987               Analyze (Dcopy);
7988
7989               --  Default expressions are resolved with their own type if the
7990               --  context is generic, to avoid anomalies with private types.
7991
7992               if Ekind (Scope (E)) = E_Generic_Package then
7993                  Resolve (Dcopy);
7994               else
7995                  Resolve (Dcopy, Etype (Formal));
7996               end if;
7997
7998               --  If that resolved expression will raise constraint error,
7999               --  then flag the default value as raising constraint error.
8000               --  This allows a proper error message on the calls.
8001
8002               if Raises_Constraint_Error (Dcopy) then
8003                  Set_Raises_Constraint_Error (Default_Value (Formal));
8004               end if;
8005
8006            --  If the default is a parameterless call, we use the name of
8007            --  the called function directly, and there is no body to build.
8008
8009            elsif Nkind (Dcopy) = N_Function_Call
8010              and then No (Parameter_Associations (Dcopy))
8011            then
8012               null;
8013
8014            --  Else construct and analyze the body of a wrapper procedure
8015            --  that contains an object declaration to hold the expression.
8016            --  Given that this is done only to complete the analysis, it
8017            --  simpler to build a procedure than a function which might
8018            --  involve secondary stack expansion.
8019
8020            else
8021               Dnam := Make_Temporary (Loc, 'D');
8022
8023               Dbody :=
8024                 Make_Subprogram_Body (Loc,
8025                   Specification =>
8026                     Make_Procedure_Specification (Loc,
8027                       Defining_Unit_Name => Dnam),
8028
8029                   Declarations => New_List (
8030                     Make_Object_Declaration (Loc,
8031                       Defining_Identifier => Make_Temporary (Loc, 'T'),
8032                       Object_Definition   =>
8033                         New_Occurrence_Of (Etype (Formal), Loc),
8034                       Expression          => New_Copy_Tree (Dcopy))),
8035
8036                   Handled_Statement_Sequence =>
8037                     Make_Handled_Sequence_Of_Statements (Loc,
8038                       Statements => Empty_List));
8039
8040               Set_Scope (Dnam, Scope (E));
8041               Set_Assignment_OK (First (Declarations (Dbody)));
8042               Set_Is_Eliminated (Dnam);
8043               Insert_After (After, Dbody);
8044               Analyze (Dbody);
8045               After := Dbody;
8046            end if;
8047         end if;
8048
8049         Next_Formal (Formal);
8050      end loop;
8051   end Process_Default_Expressions;
8052
8053   ----------------------------------------
8054   -- Set_Component_Alignment_If_Not_Set --
8055   ----------------------------------------
8056
8057   procedure Set_Component_Alignment_If_Not_Set (Typ : Entity_Id) is
8058   begin
8059      --  Ignore if not base type, subtypes don't need anything
8060
8061      if Typ /= Base_Type (Typ) then
8062         return;
8063      end if;
8064
8065      --  Do not override existing representation
8066
8067      if Is_Packed (Typ) then
8068         return;
8069
8070      elsif Has_Specified_Layout (Typ) then
8071         return;
8072
8073      elsif Component_Alignment (Typ) /= Calign_Default then
8074         return;
8075
8076      else
8077         Set_Component_Alignment
8078           (Typ, Scope_Stack.Table
8079                  (Scope_Stack.Last).Component_Alignment_Default);
8080      end if;
8081   end Set_Component_Alignment_If_Not_Set;
8082
8083   --------------------------
8084   -- Set_SSO_From_Default --
8085   --------------------------
8086
8087   procedure Set_SSO_From_Default (T : Entity_Id) is
8088      Reversed : Boolean;
8089
8090   begin
8091      --  Set default SSO for an array or record base type, except in case of
8092      --  a type extension (which always inherits the SSO of its parent type).
8093
8094      if Is_Base_Type (T)
8095        and then (Is_Array_Type (T)
8096                   or else (Is_Record_Type (T)
8097                             and then not (Is_Tagged_Type (T)
8098                                            and then Is_Derived_Type (T))))
8099      then
8100         Reversed :=
8101            (Bytes_Big_Endian     and then SSO_Set_Low_By_Default (T))
8102              or else
8103            (not Bytes_Big_Endian and then SSO_Set_High_By_Default (T));
8104
8105         if (SSO_Set_Low_By_Default (T) or else SSO_Set_High_By_Default (T))
8106
8107           --  For a record type, if bit order is specified explicitly,
8108           --  then do not set SSO from default if not consistent. Note that
8109           --  we do not want to look at a Bit_Order attribute definition
8110           --  for a parent: if we were to inherit Bit_Order, then both
8111           --  SSO_Set_*_By_Default flags would have been cleared already
8112           --  (by Inherit_Aspects_At_Freeze_Point).
8113
8114           and then not
8115             (Is_Record_Type (T)
8116               and then
8117                 Has_Rep_Item (T, Name_Bit_Order, Check_Parents => False)
8118               and then Reverse_Bit_Order (T) /= Reversed)
8119         then
8120            --  If flags cause reverse storage order, then set the result. Note
8121            --  that we would have ignored the pragma setting the non default
8122            --  storage order in any case, hence the assertion at this point.
8123
8124            pragma Assert
8125              (not Reversed or else Support_Nondefault_SSO_On_Target);
8126
8127            Set_Reverse_Storage_Order (T, Reversed);
8128
8129            --  For a record type, also set reversed bit order. Note: if a bit
8130            --  order has been specified explicitly, then this is a no-op.
8131
8132            if Is_Record_Type (T) then
8133               Set_Reverse_Bit_Order (T, Reversed);
8134            end if;
8135         end if;
8136      end if;
8137   end Set_SSO_From_Default;
8138
8139   ------------------
8140   -- Undelay_Type --
8141   ------------------
8142
8143   procedure Undelay_Type (T : Entity_Id) is
8144   begin
8145      Set_Has_Delayed_Freeze (T, False);
8146      Set_Freeze_Node (T, Empty);
8147
8148      --  Since we don't want T to have a Freeze_Node, we don't want its
8149      --  Full_View or Corresponding_Record_Type to have one either.
8150
8151      --  ??? Fundamentally, this whole handling is unpleasant. What we really
8152      --  want is to be sure that for an Itype that's part of record R and is a
8153      --  subtype of type T, that it's frozen after the later of the freeze
8154      --  points of R and T. We have no way of doing that directly, so what we
8155      --  do is force most such Itypes to be frozen as part of freezing R via
8156      --  this procedure and only delay the ones that need to be delayed
8157      --  (mostly the designated types of access types that are defined as part
8158      --  of the record).
8159
8160      if Is_Private_Type (T)
8161        and then Present (Full_View (T))
8162        and then Is_Itype (Full_View (T))
8163        and then Is_Record_Type (Scope (Full_View (T)))
8164      then
8165         Undelay_Type (Full_View (T));
8166      end if;
8167
8168      if Is_Concurrent_Type (T)
8169        and then Present (Corresponding_Record_Type (T))
8170        and then Is_Itype (Corresponding_Record_Type (T))
8171        and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
8172      then
8173         Undelay_Type (Corresponding_Record_Type (T));
8174      end if;
8175   end Undelay_Type;
8176
8177   ------------------
8178   -- Warn_Overlay --
8179   ------------------
8180
8181   procedure Warn_Overlay (Expr : Node_Id; Typ : Entity_Id; Nam : Entity_Id) is
8182      Ent : constant Entity_Id := Entity (Nam);
8183      --  The object to which the address clause applies
8184
8185      Init : Node_Id;
8186      Old  : Entity_Id := Empty;
8187      Decl : Node_Id;
8188
8189   begin
8190      --  No warning if address clause overlay warnings are off
8191
8192      if not Address_Clause_Overlay_Warnings then
8193         return;
8194      end if;
8195
8196      --  No warning if there is an explicit initialization
8197
8198      Init := Original_Node (Expression (Declaration_Node (Ent)));
8199
8200      if Present (Init) and then Comes_From_Source (Init) then
8201         return;
8202      end if;
8203
8204      --  We only give the warning for non-imported entities of a type for
8205      --  which a non-null base init proc is defined, or for objects of access
8206      --  types with implicit null initialization, or when Normalize_Scalars
8207      --  applies and the type is scalar or a string type (the latter being
8208      --  tested for because predefined String types are initialized by inline
8209      --  code rather than by an init_proc). Note that we do not give the
8210      --  warning for Initialize_Scalars, since we suppressed initialization
8211      --  in this case. Also, do not warn if Suppress_Initialization is set.
8212
8213      if Present (Expr)
8214        and then not Is_Imported (Ent)
8215        and then not Initialization_Suppressed (Typ)
8216        and then (Has_Non_Null_Base_Init_Proc (Typ)
8217                   or else Is_Access_Type (Typ)
8218                   or else (Normalize_Scalars
8219                             and then (Is_Scalar_Type (Typ)
8220                                        or else Is_String_Type (Typ))))
8221      then
8222         if Nkind (Expr) = N_Attribute_Reference
8223           and then Is_Entity_Name (Prefix (Expr))
8224         then
8225            Old := Entity (Prefix (Expr));
8226
8227         elsif Is_Entity_Name (Expr)
8228           and then Ekind (Entity (Expr)) = E_Constant
8229         then
8230            Decl := Declaration_Node (Entity (Expr));
8231
8232            if Nkind (Decl) = N_Object_Declaration
8233              and then Present (Expression (Decl))
8234              and then Nkind (Expression (Decl)) = N_Attribute_Reference
8235              and then Is_Entity_Name (Prefix (Expression (Decl)))
8236            then
8237               Old := Entity (Prefix (Expression (Decl)));
8238
8239            elsif Nkind (Expr) = N_Function_Call then
8240               return;
8241            end if;
8242
8243         --  A function call (most likely to To_Address) is probably not an
8244         --  overlay, so skip warning. Ditto if the function call was inlined
8245         --  and transformed into an entity.
8246
8247         elsif Nkind (Original_Node (Expr)) = N_Function_Call then
8248            return;
8249         end if;
8250
8251         --  If a pragma Import follows, we assume that it is for the current
8252         --  target of the address clause, and skip the warning. There may be
8253         --  a source pragma or an aspect that specifies import and generates
8254         --  the corresponding pragma. These will indicate that the entity is
8255         --  imported and that is checked above so that the spurious warning
8256         --  (generated when the entity is frozen) will be suppressed. The
8257         --  pragma may be attached to the aspect, so it is not yet a list
8258         --  member.
8259
8260         if Is_List_Member (Parent (Expr)) then
8261            Decl := Next (Parent (Expr));
8262
8263            if Present (Decl)
8264              and then Nkind (Decl) = N_Pragma
8265              and then Pragma_Name (Decl) = Name_Import
8266            then
8267               return;
8268            end if;
8269         end if;
8270
8271         --  Otherwise give warning message
8272
8273         if Present (Old) then
8274            Error_Msg_Node_2 := Old;
8275            Error_Msg_N
8276              ("default initialization of & may modify &??",
8277               Nam);
8278         else
8279            Error_Msg_N
8280              ("default initialization of & may modify overlaid storage??",
8281               Nam);
8282         end if;
8283
8284         --  Add friendly warning if initialization comes from a packed array
8285         --  component.
8286
8287         if Is_Record_Type (Typ)  then
8288            declare
8289               Comp : Entity_Id;
8290
8291            begin
8292               Comp := First_Component (Typ);
8293               while Present (Comp) loop
8294                  if Nkind (Parent (Comp)) = N_Component_Declaration
8295                    and then Present (Expression (Parent (Comp)))
8296                  then
8297                     exit;
8298                  elsif Is_Array_Type (Etype (Comp))
8299                     and then Present (Packed_Array_Impl_Type (Etype (Comp)))
8300                  then
8301                     Error_Msg_NE
8302                       ("\packed array component& " &
8303                        "will be initialized to zero??",
8304                        Nam, Comp);
8305                     exit;
8306                  else
8307                     Next_Component (Comp);
8308                  end if;
8309               end loop;
8310            end;
8311         end if;
8312
8313         Error_Msg_N
8314           ("\use pragma Import for & to " &
8315            "suppress initialization (RM B.1(24))??",
8316            Nam);
8317      end if;
8318   end Warn_Overlay;
8319
8320end Freeze;
8321