File : freeze.adb


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