OSDN Git Service

2007-04-20 Javier Miranda <miranda@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / freeze.adb
index 0ac32c3..f7876ba 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2003, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2006, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -16,8 +16,8 @@
 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
 -- for  more details.  You should have  received  a copy of the GNU General --
 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
+-- Boston, MA 02110-1301, USA.                                              --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
@@ -30,7 +30,6 @@ with Einfo;    use Einfo;
 with Elists;   use Elists;
 with Errout;   use Errout;
 with Exp_Ch7;  use Exp_Ch7;
-with Exp_Ch11; use Exp_Ch11;
 with Exp_Pakd; use Exp_Pakd;
 with Exp_Util; use Exp_Util;
 with Exp_Tss;  use Exp_Tss;
@@ -40,6 +39,7 @@ with Nlists;   use Nlists;
 with Nmake;    use Nmake;
 with Opt;      use Opt;
 with Restrict; use Restrict;
+with Rident;   use Rident;
 with Sem;      use Sem;
 with Sem_Cat;  use Sem_Cat;
 with Sem_Ch6;  use Sem_Ch6;
@@ -77,7 +77,7 @@ package body Freeze is
      (Decl  : Node_Id;
       New_S : Entity_Id;
       After : in out Node_Id);
-   --  Build body for a renaming declaration, insert in tree and analyze.
+   --  Build body for a renaming declaration, insert in tree and analyze
 
    procedure Check_Address_Clause (E : Entity_Id);
    --  Apply legality checks to address clauses for object declarations,
@@ -124,7 +124,7 @@ package body Freeze is
    --  a subprogram type (i.e. an access to a subprogram).
 
    function Is_Fully_Defined (T : Entity_Id) return Boolean;
-   --  true if T is not private and has no private components, or has a full
+   --  True if T is not private and has no private components, or has a full
    --  view. Used to determine whether the designated type of an access type
    --  should be frozen when the access type is frozen. This is done when an
    --  allocator is frozen, or an expression that may involve attributes of
@@ -163,6 +163,11 @@ package body Freeze is
    --  needed -- see body for details). Never has any effect on T if the
    --  Debug_Info_Off flag is set.
 
+   procedure Undelay_Type (T : Entity_Id);
+   --  T is a type of a component that we know to be an Itype.
+   --  We don't want this to have a Freeze_Node, so ensure it doesn't.
+   --  Do the same for any Full_View or Corresponding_Record_Type.
+
    procedure Warn_Overlay
      (Expr : Node_Id;
       Typ  : Entity_Id;
@@ -202,7 +207,6 @@ package body Freeze is
       After : in out Node_Id)
    is
       Body_Node : constant Node_Id := Build_Renamed_Body (Decl, New_S);
-
    begin
       Insert_After (After, Body_Node);
       Mark_Rewrite_Insertion (Body_Node);
@@ -387,7 +391,7 @@ package body Freeze is
              Parameter_Associations => Actuals);
       end if;
 
-      --  Create entities for subprogram body and formals.
+      --  Create entities for subprogram body and formals
 
       Set_Defining_Unit_Name (Spec,
         Make_Defining_Identifier (Loc, Chars => Chars (New_S)));
@@ -505,9 +509,9 @@ package body Freeze is
 
    procedure Check_Compile_Time_Size (T : Entity_Id) is
 
-      procedure Set_Small_Size (S : Uint);
+      procedure Set_Small_Size (T : Entity_Id; S : Uint);
       --  Sets the compile time known size (32 bits or less) in the Esize
-      --  field, checking for a size clause that was given which attempts
+      --  field, of T checking for a size clause that was given which attempts
       --  to give a smaller size.
 
       function Size_Known (T : Entity_Id) return Boolean;
@@ -524,7 +528,7 @@ package body Freeze is
       -- Set_Small_Size --
       --------------------
 
-      procedure Set_Small_Size (S : Uint) is
+      procedure Set_Small_Size (T : Entity_Id; S : Uint) is
       begin
          if S > 32 then
             return;
@@ -575,7 +579,8 @@ package body Freeze is
 
          elsif Is_Array_Type (T) then
             if Ekind (T) = E_String_Literal_Subtype then
-               Set_Small_Size (Component_Size (T) * String_Literal_Length (T));
+               Set_Small_Size (T, Component_Size (T)
+                               * String_Literal_Length (T));
                return True;
 
             elsif not Is_Constrained (T) then
@@ -600,7 +605,6 @@ package body Freeze is
 
             begin
                Index := First_Index (T);
-
                while Present (Index) loop
                   if Nkind (Index) = N_Range then
                      Get_Index_Bounds (Index, Low, High);
@@ -632,7 +636,7 @@ package body Freeze is
                   Next_Index (Index);
                end loop;
 
-               Set_Small_Size (Esiz);
+               Set_Small_Size (T, Esiz);
                return True;
             end;
 
@@ -723,148 +727,136 @@ package body Freeze is
 
                --  Loop through components
 
-               Comp := First_Entity (T);
+               Comp := First_Component_Or_Discriminant (T);
                while Present (Comp) loop
-                  if Ekind (Comp) = E_Component
-                       or else
-                     Ekind (Comp) = E_Discriminant
-                  then
-                     Ctyp := Etype (Comp);
+                  Ctyp := Etype (Comp);
 
-                     --  We do not know the packed size if there is a
-                     --  component clause present (we possibly could,
-                     --  but this would only help in the case of a record
-                     --  with partial rep clauses. That's because in the
-                     --  case of full rep clauses, the size gets figured
-                     --  out anyway by a different circuit).
+                  --  We do not know the packed size if there is a component
+                  --  clause present (we possibly could, but this would only
+                  --  help in the case of a record with partial rep clauses.
+                  --  That's because in the case of full rep clauses, the
+                  --  size gets figured out anyway by a different circuit).
 
-                     if Present (Component_Clause (Comp)) then
-                        Packed_Size_Known := False;
-                     end if;
+                  if Present (Component_Clause (Comp)) then
+                     Packed_Size_Known := False;
+                  end if;
 
-                     --  We need to identify a component that is an array
-                     --  where the index type is an enumeration type with
-                     --  non-standard representation, and some bound of the
-                     --  type depends on a discriminant.
-
-                     --  This is because gigi computes the size by doing a
-                     --  substituation of the appropriate discriminant value
-                     --  in the size expression for the base type, and gigi
-                     --  is not clever enough to evaluate the resulting
-                     --  expression (which involves a call to rep_to_pos)
-                     --  at compile time.
-
-                     --  It would be nice if gigi would either recognize that
-                     --  this expression can be computed at compile time, or
-                     --  alternatively figured out the size from the subtype
-                     --  directly, where all the information is at hand ???
-
-                     if Is_Array_Type (Etype (Comp))
-                       and then Present (Packed_Array_Type (Etype (Comp)))
-                     then
-                        declare
-                           Ocomp  : constant Entity_Id :=
-                                      Original_Record_Component (Comp);
-                           OCtyp  : constant Entity_Id := Etype (Ocomp);
-                           Ind    : Node_Id;
-                           Indtyp : Entity_Id;
-                           Lo, Hi : Node_Id;
+                  --  We need to identify a component that is an array where
+                  --  the index type is an enumeration type with non-standard
+                  --  representation, and some bound of the type depends on a
+                  --  discriminant.
 
-                        begin
-                           Ind := First_Index (OCtyp);
-                           while Present (Ind) loop
-                              Indtyp := Etype (Ind);
+                  --  This is because gigi computes the size by doing a
+                  --  substituation of the appropriate discriminant value in
+                  --  the size expression for the base type, and gigi is not
+                  --  clever enough to evaluate the resulting expression (which
+                  --  involves a call to rep_to_pos) at compile time.
 
-                              if Is_Enumeration_Type (Indtyp)
-                                and then Has_Non_Standard_Rep (Indtyp)
-                              then
-                                 Lo := Type_Low_Bound  (Indtyp);
-                                 Hi := Type_High_Bound (Indtyp);
-
-                                 if Is_Entity_Name (Lo)
-                                   and then
-                                     Ekind (Entity (Lo)) = E_Discriminant
-                                 then
-                                    return False;
-
-                                 elsif Is_Entity_Name (Hi)
-                                   and then
-                                     Ekind (Entity (Hi)) = E_Discriminant
-                                 then
-                                    return False;
-                                 end if;
-                              end if;
+                  --  It would be nice if gigi would either recognize that
+                  --  this expression can be computed at compile time, or
+                  --  alternatively figured out the size from the subtype
+                  --  directly, where all the information is at hand ???
 
-                              Next_Index (Ind);
-                           end loop;
-                        end;
-                     end if;
+                  if Is_Array_Type (Etype (Comp))
+                    and then Present (Packed_Array_Type (Etype (Comp)))
+                  then
+                     declare
+                        Ocomp  : constant Entity_Id :=
+                                   Original_Record_Component (Comp);
+                        OCtyp  : constant Entity_Id := Etype (Ocomp);
+                        Ind    : Node_Id;
+                        Indtyp : Entity_Id;
+                        Lo, Hi : Node_Id;
+
+                     begin
+                        Ind := First_Index (OCtyp);
+                        while Present (Ind) loop
+                           Indtyp := Etype (Ind);
+
+                           if Is_Enumeration_Type (Indtyp)
+                             and then Has_Non_Standard_Rep (Indtyp)
+                           then
+                              Lo := Type_Low_Bound  (Indtyp);
+                              Hi := Type_High_Bound (Indtyp);
 
-                     --  Clearly size of record is not known if the size of
-                     --  one of the components is not known.
+                              if Is_Entity_Name (Lo)
+                                and then Ekind (Entity (Lo)) = E_Discriminant
+                              then
+                                 return False;
 
-                     if not Size_Known (Ctyp) then
-                        return False;
-                     end if;
+                              elsif Is_Entity_Name (Hi)
+                                and then Ekind (Entity (Hi)) = E_Discriminant
+                              then
+                                 return False;
+                              end if;
+                           end if;
 
-                     --  Accumulate packed size if possible
+                           Next_Index (Ind);
+                        end loop;
+                     end;
+                  end if;
 
-                     if Packed_Size_Known then
+                  --  Clearly size of record is not known if the size of
+                  --  one of the components is not known.
 
-                        --  We can only deal with elementary types, since for
-                        --  non-elementary components, alignment enters into
-                        --  the picture, and we don't know enough to handle
-                        --  proper alignment in this context. Packed arrays
-                        --  count as elementary if the representation is a
-                        --  modular type.
+                  if not Size_Known (Ctyp) then
+                     return False;
+                  end if;
 
-                        if Is_Elementary_Type (Ctyp)
-                          or else (Is_Array_Type (Ctyp)
-                                     and then
-                                       Present (Packed_Array_Type (Ctyp))
-                                     and then
-                                       Is_Modular_Integer_Type
-                                         (Packed_Array_Type (Ctyp)))
-                        then
-                           --  If RM_Size is known and static, then we can
-                           --  keep accumulating the packed size.
+                  --  Accumulate packed size if possible
+
+                  if Packed_Size_Known then
 
-                           if Known_Static_RM_Size (Ctyp) then
+                     --  We can only deal with elementary types, since for
+                     --  non-elementary components, alignment enters into the
+                     --  picture, and we don't know enough to handle proper
+                     --  alignment in this context. Packed arrays count as
+                     --  elementary if the representation is a modular type.
 
-                              --  A little glitch, to be removed sometime ???
-                              --  gigi does not understand zero sizes yet.
+                     if Is_Elementary_Type (Ctyp)
+                       or else (Is_Array_Type (Ctyp)
+                                and then Present (Packed_Array_Type (Ctyp))
+                                and then Is_Modular_Integer_Type
+                                           (Packed_Array_Type (Ctyp)))
+                     then
+                        --  If RM_Size is known and static, then we can
+                        --  keep accumulating the packed size.
 
-                              if RM_Size (Ctyp) = Uint_0 then
-                                 Packed_Size_Known := False;
+                        if Known_Static_RM_Size (Ctyp) then
 
-                              --  Normal case where we can keep accumulating
-                              --  the packed array size.
+                           --  A little glitch, to be removed sometime ???
+                           --  gigi does not understand zero sizes yet.
 
-                              else
-                                 Packed_Size := Packed_Size + RM_Size (Ctyp);
-                              end if;
+                           if RM_Size (Ctyp) = Uint_0 then
+                              Packed_Size_Known := False;
 
-                           --  If we have a field whose RM_Size is not known
-                           --  then we can't figure out the packed size here.
+                           --  Normal case where we can keep accumulating the
+                           --  packed array size.
 
                            else
-                              Packed_Size_Known := False;
+                              Packed_Size := Packed_Size + RM_Size (Ctyp);
                            end if;
 
-                        --  If we have a non-elementary type we can't figure
-                        --  out the packed array size (alignment issues).
+                        --  If we have a field whose RM_Size is not known then
+                        --  we can't figure out the packed size here.
 
                         else
                            Packed_Size_Known := False;
                         end if;
+
+                     --  If we have a non-elementary type we can't figure out
+                     --  the packed array size (alignment issues).
+
+                     else
+                        Packed_Size_Known := False;
                      end if;
                   end if;
 
-                  Next_Entity (Comp);
+                  Next_Component_Or_Discriminant (Comp);
                end loop;
 
                if Packed_Size_Known then
-                  Set_Small_Size (Packed_Size);
+                  Set_Small_Size (T, Packed_Size);
                end if;
 
                return True;
@@ -880,8 +872,7 @@ package body Freeze is
       -------------------------------------
 
       function Static_Discriminated_Components
-        (T    : Entity_Id)
-         return Boolean
+        (T : Entity_Id) return Boolean
       is
          Constraint : Elmt_Id;
 
@@ -1339,7 +1330,6 @@ package body Freeze is
       Result : in out List_Id)
    is
       L : constant List_Id := Freeze_Entity (Ent, Loc);
-
    begin
       if Is_Non_Empty_List (L) then
          if Result = No_List then
@@ -1356,7 +1346,6 @@ package body Freeze is
 
    procedure Freeze_Before (N : Node_Id; T : Entity_Id) is
       Freeze_Nodes : constant List_Id := Freeze_Entity (T, Sloc (N));
-
    begin
       if Is_Non_Empty_List (Freeze_Nodes) then
          Insert_Actions (N, Freeze_Nodes);
@@ -1368,6 +1357,7 @@ package body Freeze is
    -------------------
 
    function Freeze_Entity (E : Entity_Id; Loc : Source_Ptr) return List_Id is
+      Test_E : Entity_Id := E;
       Comp   : Entity_Id;
       F_Node : Node_Id;
       Result : List_Id;
@@ -1376,17 +1366,17 @@ package body Freeze is
       Atype  : Entity_Id;
 
       procedure Check_Current_Instance (Comp_Decl : Node_Id);
-      --  Check that an Access or Unchecked_Access attribute with
-      --  a prefix which is the current instance type can only be
-      --  applied when the type is limited.
+      --  Check that an Access or Unchecked_Access attribute with a prefix
+      --  which is the current instance type can only be applied when the type
+      --  is limited.
 
       function After_Last_Declaration return Boolean;
       --  If Loc is a freeze_entity that appears after the last declaration
       --  in the scope, inhibit error messages on late completion.
 
       procedure Freeze_Record_Type (Rec : Entity_Id);
-      --  Freeze each component, handle some representation clauses, and
-      --  freeze primitive operations if this is a tagged type.
+      --  Freeze each component, handle some representation clauses, and freeze
+      --  primitive operations if this is a tagged type.
 
       ----------------------------
       -- After_Last_Declaration --
@@ -1418,7 +1408,7 @@ package body Freeze is
       procedure Check_Current_Instance (Comp_Decl : Node_Id) is
 
          function Process (N : Node_Id) return Traverse_Result;
-         --  Process routine to apply check to given node.
+         --  Process routine to apply check to given node
 
          -------------
          -- Process --
@@ -1463,6 +1453,7 @@ package body Freeze is
          IR   : Node_Id;
          Junk : Boolean;
          ADC  : Node_Id;
+         Prev : Entity_Id;
 
          Unplaced_Component : Boolean := False;
          --  Set True if we find at least one component with no component
@@ -1472,6 +1463,45 @@ package body Freeze is
          --  Set True if we find at least one component with a component
          --  clause (used to warn about useless Bit_Order pragmas).
 
+         procedure Check_Itype (Desig : Entity_Id);
+         --  If the component subtype is an access to a constrained subtype
+         --  of an already frozen type, make the subtype frozen as well. It
+         --  might otherwise be frozen in the wrong scope, and a freeze node
+         --  on subtype has no effect.
+
+         -----------------
+         -- Check_Itype --
+         -----------------
+
+         procedure Check_Itype (Desig : Entity_Id) is
+         begin
+            if not Is_Frozen (Desig)
+              and then Is_Frozen (Base_Type (Desig))
+            then
+               Set_Is_Frozen (Desig);
+
+               --  In addition, add an Itype_Reference to ensure that the
+               --  access subtype is elaborated early enough. This cannot
+               --  be done if the subtype may depend on discriminants.
+
+               if Ekind (Comp) = E_Component
+                 and then Is_Itype (Etype (Comp))
+                 and then not Has_Discriminants (Rec)
+               then
+                  IR := Make_Itype_Reference (Sloc (Comp));
+                  Set_Itype (IR, Desig);
+
+                  if No (Result) then
+                     Result := New_List (IR);
+                  else
+                     Append (IR, Result);
+                  end if;
+               end if;
+            end if;
+         end Check_Itype;
+
+      --  Start of processing for Freeze_Record_Type
+
       begin
          --  If this is a subtype of a controlled type, declared without
          --  a constraint, the _controller may not appear in the component
@@ -1487,16 +1517,21 @@ package body Freeze is
                Set_First_Entity (Rec, First_Entity (Base_Type (Rec)));
 
             --  If this is an internal type without a declaration, as for
-            --  a record component, the base type may not yet be frozen,
-            --  and its controller has not been created. Add an explicit
-            --  freeze node for the itype, so it will be frozen after the
-            --  base type.
+            --  record component, the base type may not yet be frozen, and its
+            --  controller has not been created. Add an explicit freeze node
+            --  for the itype, so it will be frozen after the base type. This
+            --  freeze node is used to communicate with the expander, in order
+            --  to create the controller for the enclosing record, and it is
+            --  deleted afterwards (see exp_ch3). It must not be created when
+            --  expansion is off, because it might appear in the wrong context
+            --  for the back end.
 
             elsif Is_Itype (Rec)
               and then Has_Delayed_Freeze (Base_Type (Rec))
               and then
                 Nkind (Associated_Node_For_Itype (Rec)) =
-                  N_Component_Declaration
+                                                     N_Component_Declaration
+              and then Expander_Active
             then
                Ensure_Freeze_Node (Rec);
             end if;
@@ -1505,112 +1540,41 @@ package body Freeze is
          --  Freeze components and embedded subtypes
 
          Comp := First_Entity (Rec);
+         Prev := Empty;
          while Present (Comp) loop
-            if not Is_Type (Comp) then
-               Freeze_And_Append (Etype (Comp), Loc, Result);
-            end if;
 
-            --  If the component is an access type with an allocator
-            --  as default value, the designated type will be frozen
-            --  by the corresponding expression in init_proc. In  order
-            --  to place the freeze node for the designated type before
-            --  that for the current record type, freeze it now.
+            --  First handle the (real) component case
 
-            --  Same process if the component is an array of access types,
-            --  initialized with an aggregate. If the designated type is
-            --  private, it cannot contain allocators, and it is premature
-            --  to freeze the type, so we check for this as well.
-
-            if Is_Access_Type (Etype (Comp))
-              and then Present (Parent (Comp))
-              and then Present (Expression (Parent (Comp)))
-              and then Nkind (Expression (Parent (Comp))) = N_Allocator
+            if Ekind (Comp) = E_Component
+              or else Ekind (Comp) = E_Discriminant
             then
                declare
-                  Alloc : constant Node_Id := Expression (Parent (Comp));
+                  CC : constant Node_Id := Component_Clause (Comp);
 
                begin
-                  --  If component is pointer to a classwide type, freeze
-                  --  the specific type in the expression being allocated.
-                  --  The expression may be a subtype indication, in which
-                  --  case freeze the subtype mark.
-
-                  if Is_Class_Wide_Type (Designated_Type (Etype (Comp))) then
-                     if Is_Entity_Name (Expression (Alloc)) then
-                        Freeze_And_Append
-                          (Entity (Expression (Alloc)), Loc, Result);
-                     elsif
-                       Nkind (Expression (Alloc)) = N_Subtype_Indication
-                     then
-                        Freeze_And_Append
-                         (Entity (Subtype_Mark (Expression (Alloc))),
-                           Loc, Result);
-                     end if;
-
-                  else
-                     Freeze_And_Append
-                       (Designated_Type (Etype (Comp)), Loc, Result);
-                  end if;
-               end;
-
-            --  If this is a constrained subtype of an already frozen type,
-            --  make the subtype frozen as well. It might otherwise be frozen
-            --  in the wrong scope, and a freeze node on subtype has no effect.
-
-            elsif Is_Access_Type (Etype (Comp))
-              and then not Is_Frozen (Designated_Type (Etype (Comp)))
-              and then Is_Itype (Designated_Type (Etype (Comp)))
-              and then Is_Frozen (Base_Type (Designated_Type (Etype (Comp))))
-            then
-               Set_Is_Frozen (Designated_Type (Etype (Comp)));
-
-               --  In addition, add an Itype_Reference to ensure that the
-               --  access subtype is elaborated early enough. This cannot
-               --  be done if the subtype may depend on discriminants.
-
-               if Ekind (Comp) = E_Component
-                 and then Is_Itype (Etype (Comp))
-                 and then not Has_Discriminants (Rec)
-               then
-                  IR := Make_Itype_Reference (Sloc (Comp));
-                  Set_Itype (IR, Designated_Type (Etype (Comp)));
-
-                  if No (Result) then
-                     Result := New_List (IR);
-                  else
-                     Append (IR, Result);
+                  --  Freezing a record type freezes the type of each of its
+                  --  components. However, if the type of the component is
+                  --  part of this record, we do not want or need a separate
+                  --  Freeze_Node. Note that Is_Itype is wrong because that's
+                  --  also set in private type cases. We also can't check for
+                  --  the Scope being exactly Rec because of private types and
+                  --  record extensions.
+
+                  if Is_Itype (Etype (Comp))
+                    and then Is_Record_Type (Underlying_Type
+                                             (Scope (Etype (Comp))))
+                  then
+                     Undelay_Type (Etype (Comp));
                   end if;
-               end if;
 
-            elsif Is_Array_Type (Etype (Comp))
-              and then Is_Access_Type (Component_Type (Etype (Comp)))
-              and then Present (Parent (Comp))
-              and then Nkind (Parent (Comp)) = N_Component_Declaration
-              and then Present (Expression (Parent (Comp)))
-              and then Nkind (Expression (Parent (Comp))) = N_Aggregate
-              and then Is_Fully_Defined
-                 (Designated_Type (Component_Type (Etype (Comp))))
-            then
-               Freeze_And_Append
-                 (Designated_Type
-                   (Component_Type (Etype (Comp))), Loc, Result);
-            end if;
-
-            --  Processing for real components (exclude anonymous subtypes)
-
-            if Ekind (Comp) = E_Component
-              or else Ekind (Comp) = E_Discriminant
-            then
-               --  Check for error of component clause given for variable
-               --  sized type. We have to delay this test till this point,
-               --  since the component type has to be frozen for us to know
-               --  if it is variable length. We omit this test in a generic
-               --  context, it will be applied at instantiation time.
+                  Freeze_And_Append (Etype (Comp), Loc, Result);
 
-               declare
-                  CC : constant Node_Id := Component_Clause (Comp);
+                  --  Check for error of component clause given for variable
+                  --  sized type. We have to delay this test till this point,
+                  --  since the component type has to be frozen for us to know
+                  --  if it is variable length. We omit this test in a generic
+                  --  context, it will be applied at instantiation time.
 
-               begin
                   if Present (CC) then
                      Placed_Component := True;
 
@@ -1628,149 +1592,328 @@ package body Freeze is
                   else
                      Unplaced_Component := True;
                   end if;
-               end;
 
-               --  If component clause is present, then deal with the
-               --  non-default bit order case. We cannot do this before
-               --  the freeze point, because there is no required order
-               --  for the component clause and the bit_order clause.
+                  --  Case of component requires byte alignment
 
-               --  We only do this processing for the base type, and in
-               --  fact that's important, since otherwise if there are
-               --  record subtypes, we could reverse the bits once for
-               --  each subtype, which would be incorrect.
+                  if Must_Be_On_Byte_Boundary (Etype (Comp)) then
 
-               if Present (Component_Clause (Comp))
-                 and then Reverse_Bit_Order (Rec)
-                 and then Ekind (E) = E_Record_Type
-               then
-                  declare
-                     CFB : constant Uint    := Component_Bit_Offset (Comp);
-                     CSZ : constant Uint    := Esize (Comp);
-                     CLC : constant Node_Id := Component_Clause (Comp);
-                     Pos : constant Node_Id := Position (CLC);
-                     FB  : constant Node_Id := First_Bit (CLC);
+                     --  Set the enclosing record to also require byte align
 
-                     Storage_Unit_Offset : constant Uint :=
-                                             CFB / System_Storage_Unit;
+                     Set_Must_Be_On_Byte_Boundary (Rec);
 
-                     Start_Bit : constant Uint :=
-                                   CFB mod System_Storage_Unit;
+                     --  Check for component clause that is inconsistent
+                     --  with the required byte boundary alignment.
 
-                  begin
-                     --  Cases where field goes over storage unit boundary
+                     if Present (CC)
+                       and then Normalized_First_Bit (Comp) mod
+                                  System_Storage_Unit /= 0
+                     then
+                        Error_Msg_N
+                          ("component & must be byte aligned",
+                           Component_Name (Component_Clause (Comp)));
+                     end if;
+                  end if;
 
-                     if Start_Bit + CSZ > System_Storage_Unit then
+                  --  If component clause is present, then deal with the
+                  --  non-default bit order case for Ada 95 mode. The required
+                  --  processing for Ada 2005 mode is handled separately after
+                  --  processing all components.
 
-                        --  Allow multi-byte field but generate warning
+                  --  We only do this processing for the base type, and in
+                  --  fact that's important, since otherwise if there are
+                  --  record subtypes, we could reverse the bits once for
+                  --  each subtype, which would be incorrect.
 
-                        if Start_Bit mod System_Storage_Unit = 0
-                          and then CSZ mod System_Storage_Unit = 0
-                        then
-                           Error_Msg_N
-                             ("multi-byte field specified with non-standard"
-                                & " Bit_Order?", CLC);
+                  if Present (CC)
+                    and then Reverse_Bit_Order (Rec)
+                    and then Ekind (E) = E_Record_Type
+                    and then Ada_Version <= Ada_95
+                  then
+                     declare
+                        CFB : constant Uint    := Component_Bit_Offset (Comp);
+                        CSZ : constant Uint    := Esize (Comp);
+                        CLC : constant Node_Id := Component_Clause (Comp);
+                        Pos : constant Node_Id := Position (CLC);
+                        FB  : constant Node_Id := First_Bit (CLC);
 
-                           if Bytes_Big_Endian then
+                        Storage_Unit_Offset : constant Uint :=
+                                                CFB / System_Storage_Unit;
+
+                        Start_Bit : constant Uint :=
+                                      CFB mod System_Storage_Unit;
+
+                     begin
+                        --  Cases where field goes over storage unit boundary
+
+                        if Start_Bit + CSZ > System_Storage_Unit then
+
+                           --  Allow multi-byte field but generate warning
+
+                           if Start_Bit mod System_Storage_Unit = 0
+                             and then CSZ mod System_Storage_Unit = 0
+                           then
                               Error_Msg_N
-                                ("bytes are not reversed "
-                                   & "(component is big-endian)?", CLC);
+                                ("multi-byte field specified with non-standard"
+                                 & " Bit_Order?", CLC);
+
+                              if Bytes_Big_Endian then
+                                 Error_Msg_N
+                                   ("bytes are not reversed "
+                                    & "(component is big-endian)?", CLC);
+                              else
+                                 Error_Msg_N
+                                   ("bytes are not reversed "
+                                    & "(component is little-endian)?", CLC);
+                              end if;
+
+                              --  Do not allow non-contiguous field
+
                            else
                               Error_Msg_N
-                                ("bytes are not reversed "
-                                   & "(component is little-endian)?", CLC);
+                                ("attempt to specify non-contiguous field"
+                                 & " not permitted", CLC);
+                              Error_Msg_N
+                                ("\(caused by non-standard Bit_Order "
+                                 & "specified)", CLC);
                            end if;
 
-                        --  Do not allow non-contiguous field
+                           --  Case where field fits in one storage unit
 
                         else
-                           Error_Msg_N
-                             ("attempt to specify non-contiguous field"
-                                & " not permitted", CLC);
-                           Error_Msg_N
-                             ("\(caused by non-standard Bit_Order "
-                                & "specified)", CLC);
-                        end if;
+                           --  Give warning if suspicious component clause
 
-                     --  Case where field fits in one storage unit
+                           if Intval (FB) >= System_Storage_Unit
+                             and then Warn_On_Reverse_Bit_Order
+                           then
+                              Error_Msg_N
+                                ("?Bit_Order clause does not affect " &
+                                 "byte ordering", Pos);
+                              Error_Msg_Uint_1 :=
+                                Intval (Pos) + Intval (FB) /
+                                  System_Storage_Unit;
+                              Error_Msg_N
+                                ("?position normalized to ^ before bit " &
+                                 "order interpreted", Pos);
+                           end if;
 
-                     else
-                        --  Give warning if suspicious component clause
+                           --  Here is where we fix up the Component_Bit_Offset
+                           --  value to account for the reverse bit order.
+                           --  Some examples of what needs to be done are:
 
-                        if Intval (FB) >= System_Storage_Unit then
-                           Error_Msg_N
-                             ("?Bit_Order clause does not affect " &
-                              "byte ordering", Pos);
-                           Error_Msg_Uint_1 :=
-                             Intval (Pos) + Intval (FB) / System_Storage_Unit;
-                           Error_Msg_N
-                             ("?position normalized to ^ before bit " &
-                              "order interpreted", Pos);
+                           --    First_Bit .. Last_Bit     Component_Bit_Offset
+                           --      old          new          old       new
+
+                           --     0 .. 0       7 .. 7         0         7
+                           --     0 .. 1       6 .. 7         0         6
+                           --     0 .. 2       5 .. 7         0         5
+                           --     0 .. 7       0 .. 7         0         4
+
+                           --     1 .. 1       6 .. 6         1         6
+                           --     1 .. 4       3 .. 6         1         3
+                           --     4 .. 7       0 .. 3         4         0
+
+                           --  The general rule is that the first bit is
+                           --  is obtained by subtracting the old ending bit
+                           --  from storage_unit - 1.
+
+                           Set_Component_Bit_Offset
+                             (Comp,
+                              (Storage_Unit_Offset * System_Storage_Unit) +
+                                (System_Storage_Unit - 1) -
+                                  (Start_Bit + CSZ - 1));
+
+                           Set_Normalized_First_Bit
+                             (Comp,
+                                Component_Bit_Offset (Comp) mod
+                                  System_Storage_Unit);
                         end if;
+                     end;
+                  end if;
+               end;
+            end if;
 
-                        --  Here is where we fix up the Component_Bit_Offset
-                        --  value to account for the reverse bit order.
-                        --  Some examples of what needs to be done are:
+            --  If the component is an Itype with Delayed_Freeze and is either
+            --  a record or array subtype and its base type has not yet been
+            --  frozen, we must remove this from the entity list of this
+            --  record and put it on the entity list of the scope of its base
+            --  type. Note that we know that this is not the type of a
+            --  component since we cleared Has_Delayed_Freeze for it in the
+            --  previous loop. Thus this must be the Designated_Type of an
+            --  access type, which is the type of a component.
+
+            if Is_Itype (Comp)
+              and then Is_Type (Scope (Comp))
+              and then Is_Composite_Type (Comp)
+              and then Base_Type (Comp) /= Comp
+              and then Has_Delayed_Freeze (Comp)
+              and then not Is_Frozen (Base_Type (Comp))
+            then
+               declare
+                  Will_Be_Frozen : Boolean := False;
+                  S : Entity_Id := Scope (Rec);
 
-                        --    First_Bit .. Last_Bit     Component_Bit_Offset
-                        --      old          new          old       new
+               begin
+                  --  We have a pretty bad kludge here. Suppose Rec is subtype
+                  --  being defined in a subprogram that's created as part of
+                  --  the freezing of Rec'Base. In that case, we know that
+                  --  Comp'Base must have already been frozen by the time we
+                  --  get to elaborate this because Gigi doesn't elaborate any
+                  --  bodies until it has elaborated all of the declarative
+                  --  part. But Is_Frozen will not be set at this point because
+                  --  we are processing code in lexical order.
+
+                  --  We detect this case by going up the Scope chain of Rec
+                  --  and seeing if we have a subprogram scope before reaching
+                  --  the top of the scope chain or that of Comp'Base. If we
+                  --  do, then mark that Comp'Base will actually be frozen. If
+                  --  so, we merely undelay it.
+
+                  while Present (S) loop
+                     if Is_Subprogram (S) then
+                        Will_Be_Frozen := True;
+                        exit;
+                     elsif S = Scope (Base_Type (Comp)) then
+                        exit;
+                     end if;
 
-                        --     0 .. 0       7 .. 7         0         7
-                        --     0 .. 1       6 .. 7         0         6
-                        --     0 .. 2       5 .. 7         0         5
-                        --     0 .. 7       0 .. 7         0         4
+                     S := Scope (S);
+                  end loop;
 
-                        --     1 .. 1       6 .. 6         1         6
-                        --     1 .. 4       3 .. 6         1         3
-                        --     4 .. 7       0 .. 3         4         0
+                  if Will_Be_Frozen then
+                     Undelay_Type (Comp);
+                  else
+                     if Present (Prev) then
+                        Set_Next_Entity (Prev, Next_Entity (Comp));
+                     else
+                        Set_First_Entity (Rec, Next_Entity (Comp));
+                     end if;
+
+                     --  Insert in entity list of scope of base type (which
+                     --  must be an enclosing scope, because still unfrozen).
+
+                     Append_Entity (Comp, Scope (Base_Type (Comp)));
+                  end if;
+               end;
+
+            --  If the component is an access type with an allocator as
+            --  default value, the designated type will be frozen by the
+            --  corresponding expression in init_proc. In order to place the
+            --  freeze node for the designated type before that for the
+            --  current record type, freeze it now.
 
-                        --  The general rule is that the first bit is
-                        --  is obtained by subtracting the old ending bit
-                        --  from storage_unit - 1.
+            --  Same process if the component is an array of access types,
+            --  initialized with an aggregate. If the designated type is
+            --  private, it cannot contain allocators, and it is premature to
+            --  freeze the type, so we check for this as well.
+
+            elsif Is_Access_Type (Etype (Comp))
+              and then Present (Parent (Comp))
+              and then Present (Expression (Parent (Comp)))
+              and then Nkind (Expression (Parent (Comp))) = N_Allocator
+            then
+               declare
+                  Alloc : constant Node_Id := Expression (Parent (Comp));
 
-                        Set_Component_Bit_Offset (Comp,
-                          (Storage_Unit_Offset * System_Storage_Unit)
-                          + (System_Storage_Unit - 1)
-                          - (Start_Bit + CSZ - 1));
+               begin
+                  --  If component is pointer to a classwide type, freeze
+                  --  the specific type in the expression being allocated.
+                  --  The expression may be a subtype indication, in which
+                  --  case freeze the subtype mark.
 
-                        Set_Normalized_First_Bit (Comp,
-                          Component_Bit_Offset (Comp) mod System_Storage_Unit);
+                  if Is_Class_Wide_Type (Designated_Type (Etype (Comp))) then
+                     if Is_Entity_Name (Expression (Alloc)) then
+                        Freeze_And_Append
+                          (Entity (Expression (Alloc)), Loc, Result);
+                     elsif
+                       Nkind (Expression (Alloc)) = N_Subtype_Indication
+                     then
+                        Freeze_And_Append
+                         (Entity (Subtype_Mark (Expression (Alloc))),
+                           Loc, Result);
                      end if;
-                  end;
-               end if;
+
+                  elsif Is_Itype (Designated_Type (Etype (Comp))) then
+                     Check_Itype (Designated_Type (Etype (Comp)));
+
+                  else
+                     Freeze_And_Append
+                       (Designated_Type (Etype (Comp)), Loc, Result);
+                  end if;
+               end;
+
+            elsif Is_Access_Type (Etype (Comp))
+              and then Is_Itype (Designated_Type (Etype (Comp)))
+            then
+               Check_Itype (Designated_Type (Etype (Comp)));
+
+            elsif Is_Array_Type (Etype (Comp))
+              and then Is_Access_Type (Component_Type (Etype (Comp)))
+              and then Present (Parent (Comp))
+              and then Nkind (Parent (Comp)) = N_Component_Declaration
+              and then Present (Expression (Parent (Comp)))
+              and then Nkind (Expression (Parent (Comp))) = N_Aggregate
+              and then Is_Fully_Defined
+                 (Designated_Type (Component_Type (Etype (Comp))))
+            then
+               Freeze_And_Append
+                 (Designated_Type
+                   (Component_Type (Etype (Comp))), Loc, Result);
             end if;
 
+            Prev := Comp;
             Next_Entity (Comp);
          end loop;
 
-         --  Check for useless pragma Bit_Order
+         --  Deal with pragma Bit_Order
 
-         if not Placed_Component and then Reverse_Bit_Order (Rec) then
-            ADC := Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
-            Error_Msg_N ("?Bit_Order specification has no effect", ADC);
-            Error_Msg_N ("\?since no component clauses were specified", ADC);
+         if Reverse_Bit_Order (Rec) and then Base_Type (Rec) = Rec then
+            if not Placed_Component then
+               ADC :=
+                 Get_Attribute_Definition_Clause (Rec, Attribute_Bit_Order);
+               Error_Msg_N
+                 ("?Bit_Order specification has no effect", ADC);
+               Error_Msg_N
+                 ("\?since no component clauses were specified", ADC);
+
+            --  Here is where we do Ada 2005 processing for bit order (the
+            --  Ada 95 case was already taken care of above).
+
+            elsif Ada_Version >= Ada_05 then
+               Adjust_Record_For_Reverse_Bit_Order (Rec);
+            end if;
          end if;
 
-         --  Check for useless pragma Pack when all components placed
+         --  Check for useless pragma Pack when all components placed. We only
+         --  do this check for record types, not subtypes, since a subtype may
+         --  have all its components placed, and it still makes perfectly good
+         --  sense to pack other subtypes or the parent type.
 
-         if Is_Packed (Rec)
+         if Ekind (Rec) = E_Record_Type
+           and then Is_Packed (Rec)
            and then not Unplaced_Component
-           and then Warn_On_Redundant_Constructs
          then
-            Error_Msg_N
-              ("?pragma Pack has no effect, no unplaced components",
-               Get_Rep_Pragma (Rec, Name_Pack));
+            --  Reset packed status. Probably not necessary, but we do it
+            --  so that there is no chance of the back end doing something
+            --  strange with this redundant indication of packing.
+
             Set_Is_Packed (Rec, False);
+
+            --  Give warning if redundant constructs warnings on
+
+            if Warn_On_Redundant_Constructs then
+               Error_Msg_N
+                 ("?pragma Pack has no effect, no unplaced components",
+                  Get_Rep_Pragma (Rec, Name_Pack));
+            end if;
          end if;
 
-         --  If this is the record corresponding to a remote type,
-         --  freeze the remote type here since that is what we are
-         --  semantically freezing. This prevents having the freeze
-         --  node for that type in an inner scope.
+         --  If this is the record corresponding to a remote type, freeze the
+         --  remote type here since that is what we are semantically freezing.
+         --  This prevents the freeze node for that type in an inner scope.
 
          --  Also, Check for controlled components and unchecked unions.
-         --  Finally, enforce the restriction that access attributes with
-         --  current instance prefix can only apply to limited types.
+         --  Finally, enforce the restriction that access attributes with a
+         --  current instance prefix can only apply to limited types.
 
          if  Ekind (Rec) = E_Record_Type then
             if Present (Corresponding_Remote_Type (Rec)) then
@@ -1800,9 +1943,8 @@ package body Freeze is
                if Has_Per_Object_Constraint (Comp)
                  and then not Is_Limited_Type (Rec)
                then
-                  --  Scan component declaration for likely misuses of
-                  --  current instance, either in a constraint or in a
-                  --  default expression.
+                  --  Scan component declaration for likely misuses of current
+                  --  instance, either in a constraint or a default expression.
 
                   Check_Current_Instance (Parent (Comp));
                end if;
@@ -1813,20 +1955,23 @@ package body Freeze is
 
          Set_Component_Alignment_If_Not_Set (Rec);
 
-         --  For first subtypes, check if there are any fixed-point
-         --  fields with component clauses, where we must check the size.
-         --  This is not done till the freeze point, since for fixed-point
-         --  types, we do not know the size until the type is frozen.
+         --  For first subtypes, check if there are any fixed-point fields with
+         --  component clauses, where we must check the size. This is not done
+         --  till the freeze point, since for fixed-point types, we do not know
+         --  the size until the type is frozen. Similar processing applies to
+         --  bit packed arrays.
 
          if Is_First_Subtype (Rec) then
             Comp := First_Component (Rec);
 
             while Present (Comp) loop
                if Present (Component_Clause (Comp))
-                 and then Is_Fixed_Point_Type (Etype (Comp))
+                 and then (Is_Fixed_Point_Type (Etype (Comp))
+                             or else
+                           Is_Bit_Packed_Array (Etype (Comp)))
                then
                   Check_Size
-                    (Component_Clause (Comp),
+                    (Component_Name (Component_Clause (Comp)),
                      Etype (Comp),
                      Esize (Comp),
                      Junk);
@@ -1840,19 +1985,28 @@ package body Freeze is
    --  Start of processing for Freeze_Entity
 
    begin
+      --  We are going to test for various reasons why this entity need not be
+      --  frozen here, but in the case of an Itype that's defined within a
+      --  record, that test actually applies to the record.
+
+      if Is_Itype (E) and then Is_Record_Type (Scope (E)) then
+         Test_E := Scope (E);
+      elsif Is_Itype (E) and then Present (Underlying_Type (Scope (E)))
+        and then Is_Record_Type (Underlying_Type (Scope (E)))
+      then
+         Test_E := Underlying_Type (Scope (E));
+      end if;
+
       --  Do not freeze if already frozen since we only need one freeze node
 
       if Is_Frozen (E) then
          return No_List;
 
-      --  It is improper to freeze an external entity within a generic
-      --  because its freeze node will appear in a non-valid context.
-      --  ??? We should probably freeze the entity at that point and insert
-      --  the freeze node in a proper place but this proper place is not
-      --  easy to find, and the proper scope is not easy to restore. For
-      --  now, just wait to get out of the generic to freeze ???
+      --  It is improper to freeze an external entity within a generic because
+      --  its freeze node will appear in a non-valid context. The entity will
+      --  be frozen in the proper scope after the current generic is analyzed.
 
-      elsif Inside_A_Generic and then External_Ref_In_Generic (E) then
+      elsif Inside_A_Generic and then External_Ref_In_Generic (Test_E) then
          return No_List;
 
       --  Do not freeze a global entity within an inner scope created during
@@ -1865,10 +2019,12 @@ package body Freeze is
       --  must be diagnosed (see e.g. 1522-005). If the enclosing subprogram
       --  comes from source, or is a generic instance, then the freeze point
       --  is the one mandated by the language. and we freze the entity.
+      --  A subprogram that is a child unit body that acts as a spec does not
+      --  have a spec that comes from source, but can only come from source.
 
-      elsif In_Open_Scopes (Scope (E))
-        and then Scope (E) /= Current_Scope
-        and then Ekind (E) /= E_Constant
+      elsif In_Open_Scopes (Scope (Test_E))
+        and then Scope (Test_E) /= Current_Scope
+        and then Ekind (Test_E) /= E_Constant
       then
          declare
             S : Entity_Id := Current_Scope;
@@ -1878,6 +2034,7 @@ package body Freeze is
                if Is_Overloadable (S) then
                   if Comes_From_Source (S)
                     or else Is_Generic_Instance (S)
+                    or else Is_Child_Unit (S)
                   then
                      exit;
                   else
@@ -1888,6 +2045,36 @@ package body Freeze is
                S := Scope (S);
             end loop;
          end;
+
+      --  Similarly, an inlined instance body may make reference to global
+      --  entities, but these references cannot be the proper freezing point
+      --  for them, and the the absence of inlining freezing will take place
+      --  in their own scope. Normally instance bodies are analyzed after
+      --  the enclosing compilation, and everything has been frozen at the
+      --  proper place, but with front-end inlining an instance body is
+      --  compiled before the end of the enclosing scope, and as a result
+      --  out-of-order freezing must be prevented.
+
+      elsif Front_End_Inlining
+        and then  In_Instance_Body
+        and then Present (Scope (Test_E))
+      then
+         declare
+            S : Entity_Id := Scope (Test_E);
+
+         begin
+            while Present (S) loop
+               if Is_Generic_Instance (S) then
+                  exit;
+               else
+                  S := Scope (S);
+               end if;
+            end loop;
+
+            if No (S) then
+               return No_List;
+            end if;
+         end;
       end if;
 
       --  Here to freeze the entity
@@ -1929,12 +2116,12 @@ package body Freeze is
                if Nkind (Expr) = N_Aggregate then
                   Expand_Atomic_Aggregate (Expr, Etype (E));
 
-               --  If the expression is a reference to a record or array
-               --  object entity, then reset Is_True_Constant to False so
-               --  that the compiler will not optimize away the intermediate
-               --  object, which we need in this case for the same reason
-               --  (to ensure that the actual assignment is atomic, rather
-               --  than component-wise).
+               --  If the expression is a reference to a record or array object
+               --  entity, then reset Is_True_Constant to False so that the
+               --  compiler will not optimize away the intermediate object,
+               --  which we need in this case for the same reason (to ensure
+               --  that the actual assignment is atomic, rather than
+               --  component-wise).
 
                elsif Is_Entity_Name (Expr)
                  and then (Is_Record_Type (Etype (Expr))
@@ -1955,7 +2142,8 @@ package body Freeze is
          if Is_Subprogram (E) then
             if not Is_Internal (E) then
                declare
-                  F_Type : Entity_Id;
+                  F_Type    : Entity_Id;
+                  Warn_Node : Node_Id;
 
                   function Is_Fat_C_Ptr_Type (T : Entity_Id) return Boolean;
                   --  Determines if given type entity is a fat pointer type
@@ -1979,7 +2167,6 @@ package body Freeze is
                   --  Loop through formals
 
                   Formal := First_Formal (E);
-
                   while Present (Formal) loop
                      F_Type := Etype (Formal);
                      Freeze_And_Append (F_Type, Loc, Result);
@@ -2033,15 +2220,62 @@ package body Freeze is
                        and then Warn_On_Export_Import
                      then
                         Error_Msg_Qual_Level := 1;
-                        Error_Msg_N
+
+                        --  If this is an inherited operation, place the
+                        --  warning on the derived type declaration, rather
+                        --  than on the original subprogram.
+
+                        if Nkind (Original_Node (Parent (E))) =
+                          N_Full_Type_Declaration
+                        then
+                           Warn_Node := Parent (E);
+
+                           if Formal = First_Formal (E) then
+                              Error_Msg_NE
+                                ("?in inherited operation&", Warn_Node, E);
+                           end if;
+                        else
+                           Warn_Node := Formal;
+                        end if;
+
+                        Error_Msg_NE
                           ("?type of argument& is unconstrained array",
-                           Formal);
-                        Error_Msg_N
+                           Warn_Node, Formal);
+                        Error_Msg_NE
                           ("?foreign caller must pass bounds explicitly",
-                           Formal);
+                           Warn_Node, Formal);
                         Error_Msg_Qual_Level := 0;
                      end if;
 
+                     --  Ada 2005 (AI-326): Check wrong use of tag incomplete
+                     --  types with unknown discriminants. For example:
+
+                     --    type T (<>) is tagged;
+                     --    procedure P (X : access T); -- ERROR
+                     --    procedure P (X : T);        -- ERROR
+
+                     if not From_With_Type (F_Type) then
+                        if Is_Access_Type (F_Type) then
+                           F_Type := Designated_Type (F_Type);
+                        end if;
+
+                        if Ekind (F_Type) = E_Incomplete_Type
+                          and then Is_Tagged_Type (F_Type)
+                          and then not Is_Class_Wide_Type (F_Type)
+                          and then No (Full_View (F_Type))
+                          and then Unknown_Discriminants_Present
+                                     (Parent (F_Type))
+                          and then No (Stored_Constraint (F_Type))
+                        then
+                           Error_Msg_N
+                             ("(Ada 2005): invalid use of unconstrained tagged"
+                              & " incomplete type", E);
+
+                        elsif Ekind (F_Type) = E_Subprogram_Type then
+                           Freeze_And_Append (F_Type, Loc, Result);
+                        end if;
+                     end if;
+
                      Next_Formal (Formal);
                   end loop;
 
@@ -2066,6 +2300,20 @@ package body Freeze is
                         Error_Msg_N
                           ("?foreign convention function& should not " &
                            "return unconstrained array", E);
+
+                     --  Ada 2005 (AI-326): Check wrong use of tagged
+                     --  incomplete type
+                     --
+                     --    type T is tagged;
+                     --    function F (X : Boolean) return T; -- ERROR
+
+                     elsif Ekind (Etype (E)) = E_Incomplete_Type
+                       and then Is_Tagged_Type (Etype (E))
+                       and then No (Full_View (Etype (E)))
+                     then
+                        Error_Msg_N
+                          ("(Ada 2005): invalid use of tagged incomplete type",
+                           E);
                      end if;
                   end if;
                end;
@@ -2077,17 +2325,6 @@ package body Freeze is
                Freeze_And_Append (Alias (E), Loc, Result);
             end if;
 
-            --  If the return type requires a transient scope, and we are on
-            --  a target allowing functions to return with a depressed stack
-            --  pointer, then we mark the function as requiring this treatment.
-
-            if Ekind (E) = E_Function
-              and then Functions_Return_By_DSP_On_Target
-              and then Requires_Transient_Scope (Etype (E))
-            then
-               Set_Function_Returns_With_DSP (E);
-            end if;
-
             if not Is_Internal (E) then
                Freeze_Subprogram (E);
             end if;
@@ -2104,14 +2341,35 @@ package body Freeze is
                Freeze_And_Append (Etype (E), Loc, Result);
             end if;
 
-            --  For object created by object declaration, perform required
-            --  categorization (preelaborate and pure) checks. Defer these
-            --  checks to freeze time since pragma Import inhibits default
-            --  initialization and thus pragma Import affects these checks.
+            --  Special processing for objects created by object declaration
 
             if Nkind (Declaration_Node (E)) = N_Object_Declaration then
+
+               --  For object created by object declaration, perform required
+               --  categorization (preelaborate and pure) checks. Defer these
+               --  checks to freeze time since pragma Import inhibits default
+               --  initialization and thus pragma Import affects these checks.
+
                Validate_Object_Declaration (Declaration_Node (E));
+
+               --  If there is an address clause, check it is valid
+
                Check_Address_Clause (E);
+
+               --  For imported objects, set Is_Public unless there is also
+               --  an address clause, which means that there is no external
+               --  symbol needed for the Import (Is_Public may still be set
+               --  for other unrelated reasons). Note that we delayed this
+               --  processing till freeze time so that we can be sure not
+               --  to set the flag if there is an address clause. If there
+               --  is such a clause, then the only purpose of the import
+               --  pragma is to suppress implicit initialization.
+
+               if Is_Imported (E)
+                 and then No (Address_Clause (E))
+               then
+                  Set_Is_Public (E);
+               end if;
             end if;
 
             --  Check that a constant which has a pragma Volatile[_Components]
@@ -2127,14 +2385,21 @@ package body Freeze is
                --  inherited the indication from elsewhere (e.g. an address
                --  clause, which is not good enough in RM terms!)
 
-               if Present (Get_Rep_Pragma (E, Name_Atomic))            or else
-                  Present (Get_Rep_Pragma (E, Name_Atomic_Components)) or else
-                  Present (Get_Rep_Pragma (E, Name_Volatile))          or else
-                  Present (Get_Rep_Pragma (E, Name_Volatile_Components))
+               if Has_Rep_Pragma (E, Name_Atomic)
+                    or else
+                  Has_Rep_Pragma (E, Name_Atomic_Components)
                then
                   Error_Msg_N
-                    ("stand alone atomic/volatile constant must be imported",
-                     E);
+                    ("stand alone atomic constant must be " &
+                     "imported ('R'M 'C.6(13))", E);
+
+               elsif Has_Rep_Pragma (E, Name_Volatile)
+                       or else
+                     Has_Rep_Pragma (E, Name_Volatile_Components)
+               then
+                  Error_Msg_N
+                    ("stand alone volatile constant must be " &
+                     "imported ('R'M 'C.6(13))", E);
                end if;
             end if;
 
@@ -2163,6 +2428,16 @@ package body Freeze is
       --  Case of a type or subtype being frozen
 
       else
+         --  Check preelaborable initialization for full type completing a
+         --  private type for which pragma Preelaborable_Initialization given.
+
+         if Must_Have_Preelab_Init (E)
+           and then not Has_Preelaborable_Initialization (E)
+         then
+            Error_Msg_N
+              ("full view of & does not have preelaborable initialization", E);
+         end if;
+
          --  The type may be defined in a generic unit. This can occur when
          --  freezing a generic function that returns the type (which is
          --  defined in a parent unit). It is clearly meaningless to freeze
@@ -2382,6 +2657,29 @@ package body Freeze is
                   end if;
                end if;
 
+               --  For bit-packed arrays, check the size
+
+               if Is_Bit_Packed_Array (E)
+                 and then Known_Esize (E)
+               then
+                  declare
+                     Discard : Boolean;
+                     SizC    : constant Node_Id := Size_Clause (E);
+
+                  begin
+                     --  It is not clear if it is possible to have no size
+                     --  clause at this stage, but this is not worth worrying
+                     --  about. Post the error on the entity name in the size
+                     --  clause if present, else on the type entity itself.
+
+                     if Present (SizC) then
+                        Check_Size (Name (SizC), E, Esize (E), Discard);
+                     else
+                        Check_Size (E, E, Esize (E), Discard);
+                     end if;
+                  end;
+               end if;
+
                --  Check one common case of a size given where the array
                --  needs to be packed, but was not so the size cannot be
                --  honored. This would of course be caught by the backend,
@@ -2447,27 +2745,50 @@ package body Freeze is
                   Set_Has_Non_Standard_Rep (Base_Type (E));
                   Set_Is_Packed            (Base_Type (E));
                end if;
-            end;
 
-            Set_Component_Alignment_If_Not_Set (E);
+               Set_Component_Alignment_If_Not_Set (E);
 
-            --  If the array is packed, we must create the packed array
-            --  type to be used to actually implement the type. This is
-            --  only needed for real array types (not for string literal
-            --  types, since they are present only for the front end).
+               --  If the array is packed, we must create the packed array
+               --  type to be used to actually implement the type. This is
+               --  only needed for real array types (not for string literal
+               --  types, since they are present only for the front end).
 
-            if Is_Packed (E)
-              and then Ekind (E) /= E_String_Literal_Subtype
-            then
-               Create_Packed_Array_Type (E);
-               Freeze_And_Append (Packed_Array_Type (E), Loc, Result);
+               if Is_Packed (E)
+                 and then Ekind (E) /= E_String_Literal_Subtype
+               then
+                  Create_Packed_Array_Type (E);
+                  Freeze_And_Append (Packed_Array_Type (E), Loc, Result);
 
-               --  Size information of packed array type is copied to the
-               --  array type, since this is really the representation.
+                  --  Size information of packed array type is copied to the
+                  --  array type, since this is really the representation. But
+                  --  do not override explicit existing size values.
 
-               Set_Size_Info (E, Packed_Array_Type (E));
-               Set_RM_Size   (E, RM_Size (Packed_Array_Type (E)));
-            end if;
+                  if not Has_Size_Clause (E) then
+                     Set_Esize     (E, Esize     (Packed_Array_Type (E)));
+                     Set_RM_Size   (E, RM_Size   (Packed_Array_Type (E)));
+                  end if;
+
+                  if not Has_Alignment_Clause (E) then
+                     Set_Alignment (E, Alignment (Packed_Array_Type (E)));
+                  end if;
+               end if;
+
+               --  For non-packed arrays set the alignment of the array
+               --  to the alignment of the component type if it is unknown.
+               --  Skip this in the atomic case, since atomic arrays may
+               --  need larger alignments.
+
+               if not Is_Packed (E)
+                 and then Unknown_Alignment (E)
+                 and then Known_Alignment (Ctyp)
+                 and then Known_Static_Component_Size (E)
+                 and then Known_Static_Esize (Ctyp)
+                 and then Esize (Ctyp) = Component_Size (E)
+                 and then not Is_Atomic (E)
+               then
+                  Set_Alignment (E, Alignment (Component_Type (E)));
+               end if;
+            end;
 
          --  For a class-wide type, the corresponding specific type is
          --  frozen as well (RM 13.14(15))
@@ -2540,29 +2861,33 @@ package body Freeze is
                   Freeze_And_Append (Comp, Loc, Result);
 
                elsif (Ekind (Comp)) /= E_Function then
+                  if Is_Itype (Etype (Comp))
+                    and then Underlying_Type (Scope (Etype (Comp))) = E
+                  then
+                     Undelay_Type (Etype (Comp));
+                  end if;
+
                   Freeze_And_Append (Etype (Comp), Loc, Result);
                end if;
 
                Next_Entity (Comp);
             end loop;
 
-         --  Private types are required to point to the same freeze node
-         --  as their corresponding full views. The freeze node itself
-         --  has to point to the partial view of the entity (because
-         --  from the partial view, we can retrieve the full view, but
-         --  not the reverse). However, in order to freeze correctly,
-         --  we need to freeze the full view. If we are freezing at the
-         --  end of a scope (or within the scope of the private type),
-         --  the partial and full views will have been swapped, the
-         --  full view appears first in the entity chain and the swapping
-         --  mechanism ensures that the pointers are properly set (on
-         --  scope exit).
-
-         --  If we encounter the partial view before the full view
-         --  (e.g. when freezing from another scope), we freeze the
-         --  full view, and then set the pointers appropriately since
-         --  we cannot rely on swapping to fix things up (subtypes in an
-         --  outer scope might not get swapped).
+         --  Private types are required to point to the same freeze node as
+         --  their corresponding full views. The freeze node itself has to
+         --  point to the partial view of the entity (because from the partial
+         --  view, we can retrieve the full view, but not the reverse).
+         --  However, in order to freeze correctly, we need to freeze the full
+         --  view. If we are freezing at the end of a scope (or within the
+         --  scope of the private type), the partial and full views will have
+         --  been swapped, the full view appears first in the entity chain and
+         --  the swapping mechanism ensures that the pointers are properly set
+         --  (on scope exit).
+
+         --  If we encounter the partial view before the full view (e.g. when
+         --  freezing from another scope), we freeze the full view, and then
+         --  set the pointers appropriately since we cannot rely on swapping to
+         --  fix things up (subtypes in an outer scope might not get swapped).
 
          elsif Is_Incomplete_Or_Private_Type (E)
            and then not Is_Generic_Type (E)
@@ -2571,8 +2896,8 @@ package body Freeze is
 
             if Present (Full_View (E)) then
 
-               --  If full view has already been frozen, then no
-               --  further processing is required
+               --  If full view has already been frozen, then no further
+               --  processing is required
 
                if Is_Frozen (Full_View (E)) then
 
@@ -2580,9 +2905,8 @@ package body Freeze is
                   Set_Freeze_Node (E, Empty);
                   Check_Debug_Info_Needed (E);
 
-               --  Otherwise freeze full view and patch the pointers
-               --  so that the freeze node will elaborate both views
-               --  in the back-end.
+               --  Otherwise freeze full view and patch the pointers so that
+               --  the freeze node will elaborate both views in the back-end.
 
                else
                   declare
@@ -2618,11 +2942,11 @@ package body Freeze is
                   Check_Debug_Info_Needed (E);
                end if;
 
-               --  AI-117 requires that the convention of a partial view
-               --  be the same as the convention of the full view. Note
-               --  that this is a recognized breach of privacy, but it's
-               --  essential for logical consistency of representation,
-               --  and the lack of a rule in RM95 was an oversight.
+               --  AI-117 requires that the convention of a partial view be the
+               --  same as the convention of the full view. Note that this is a
+               --  recognized breach of privacy, but it's essential for logical
+               --  consistency of representation, and the lack of a rule in
+               --  RM95 was an oversight.
 
                Set_Convention (E, Convention (Full_View (E)));
 
@@ -2632,10 +2956,10 @@ package body Freeze is
                --  Size information is copied from the full view to the
                --  incomplete or private view for consistency
 
-               --  We skip this is the full view is not a type. This is
-               --  very strange of course, and can only happen as a result
-               --  of certain illegalities, such as a premature attempt to
-               --  derive from an incomplete type.
+               --  We skip this is the full view is not a type. This is very
+               --  strange of course, and can only happen as a result of
+               --  certain illegalities, such as a premature attempt to derive
+               --  from an incomplete type.
 
                if Is_Type (Full_View (E)) then
                   Set_Size_Info (E, Full_View (E));
@@ -2670,27 +2994,55 @@ package body Freeze is
                Next_Formal (Formal);
             end loop;
 
-            --  If the return type requires a transient scope, and we are on
-            --  a target allowing functions to return with a depressed stack
-            --  pointer, then we mark the function as requiring this treatment.
+            Freeze_Subprogram (E);
+
+            --  Ada 2005 (AI-326): Check wrong use of tag incomplete type
+            --
+            --    type T is tagged;
+            --    type Acc is access function (X : T) return T; -- ERROR
 
-            if Functions_Return_By_DSP_On_Target
-              and then Requires_Transient_Scope (Etype (E))
+            if Ekind (Etype (E)) = E_Incomplete_Type
+              and then Is_Tagged_Type (Etype (E))
+              and then No (Full_View (Etype (E)))
             then
-               Set_Function_Returns_With_DSP (E);
+               Error_Msg_N
+                 ("(Ada 2005): invalid use of tagged incomplete type", E);
             end if;
 
-            Freeze_Subprogram (E);
+         --  For access to a protected subprogram, freeze the equivalent type
+         --  (however this is not set if we are not generating code or if this
+         --  is an anonymous type used just for resolution).
 
-         --  For access to a protected subprogram, freeze the equivalent
-         --  type (however this is not set if we are not generating code)
-         --  or if this is an anonymous type used just for resolution).
+         elsif Is_Access_Protected_Subprogram_Type (E) then
 
-         elsif Ekind (E) = E_Access_Protected_Subprogram_Type
-           and then Operating_Mode = Generate_Code
-           and then Present (Equivalent_Type (E))
-         then
-            Freeze_And_Append (Equivalent_Type (E), Loc, Result);
+            --  AI-326: Check wrong use of tagged incomplete types
+
+            --    type T is tagged;
+            --    type As3D is access protected
+            --      function (X : Float) return T; -- ERROR
+
+            declare
+               Etyp : Entity_Id;
+
+            begin
+               Etyp := Etype (Directly_Designated_Type (E));
+
+               if Is_Class_Wide_Type (Etyp) then
+                  Etyp := Etype (Etyp);
+               end if;
+
+               if Ekind (Etyp) = E_Incomplete_Type
+                 and then Is_Tagged_Type (Etyp)
+                 and then No (Full_View (Etyp))
+               then
+                  Error_Msg_N
+                    ("(Ada 2005): invalid use of tagged incomplete type", E);
+               end if;
+            end;
+
+            if Present (Equivalent_Type (E)) then
+               Freeze_And_Append (Equivalent_Type (E), Loc, Result);
+            end if;
          end if;
 
          --  Generic types are never seen by the back-end, and are also not
@@ -2707,9 +3059,9 @@ package body Freeze is
          if Is_Fixed_Point_Type (E) then
             Freeze_Fixed_Point_Type (E);
 
-            --  Some error checks required for ordinary fixed-point type.
-            --  Defer these till the freeze-point since we need the small
-            --  and range values. We only do these checks for base types
+            --  Some error checks required for ordinary fixed-point type. Defer
+            --  these till the freeze-point since we need the small and range
+            --  values. We only do these checks for base types
 
             if Is_Ordinary_Fixed_Point_Type (E)
               and then E = Base_Type (E)
@@ -2744,84 +3096,40 @@ package body Freeze is
          elsif Is_Integer_Type (E) then
             Adjust_Esize_For_Alignment (E);
 
-         elsif Is_Access_Type (E)
-           and then No (Associated_Storage_Pool (E))
-         then
-            Check_Restriction (No_Standard_Storage_Pools, E);
-         end if;
-
-         --  If the current entity is an array or record subtype and has
-         --  discriminants used to constrain it, it must not freeze, because
-         --  Freeze_Entity nodes force Gigi to process the frozen type.
-
-         if Is_Composite_Type (E) then
-
-            if Is_Array_Type (E) then
-               declare
-                  Index : Node_Id := First_Index (E);
-                  Expr1 : Node_Id;
-                  Expr2 : Node_Id;
+         elsif Is_Access_Type (E) then
 
-               begin
-                  while Present (Index) loop
-                     if Etype (Index) /= Any_Type then
-                        Get_Index_Bounds (Index, Expr1, Expr2);
+            --  Check restriction for standard storage pool
 
-                        for J in 1 .. 2 loop
-                           if Nkind (Expr1) = N_Identifier
-                             and then Ekind (Entity (Expr1)) = E_Discriminant
-                           then
-                              Set_Has_Delayed_Freeze (E, False);
-                              Set_Freeze_Node (E, Empty);
-                              Check_Debug_Info_Needed (E);
-                              return Result;
-                           end if;
+            if No (Associated_Storage_Pool (E)) then
+               Check_Restriction (No_Standard_Storage_Pools, E);
+            end if;
 
-                           Expr1 := Expr2;
-                        end loop;
-                     end if;
+            --  Deal with error message for pure access type. This is not an
+            --  error in Ada 2005 if there is no pool (see AI-366).
 
-                     Next_Index (Index);
-                  end loop;
-               end;
-
-            elsif Has_Discriminants (E)
-              and Is_Constrained (E)
+            if Is_Pure_Unit_Access_Type (E)
+              and then (Ada_Version < Ada_05
+                        or else not No_Pool_Assigned (E))
             then
-               declare
-                  Constraint : Elmt_Id;
-                  Expr       : Node_Id;
+               Error_Msg_N ("named access type not allowed in pure unit", E);
+            end if;
+         end if;
 
-               begin
-                  Constraint := First_Elmt (Discriminant_Constraint (E));
-                  while Present (Constraint) loop
-                     Expr := Node (Constraint);
-                     if Nkind (Expr) = N_Identifier
-                       and then Ekind (Entity (Expr)) = E_Discriminant
-                     then
-                        Set_Has_Delayed_Freeze (E, False);
-                        Set_Freeze_Node (E, Empty);
-                        Check_Debug_Info_Needed (E);
-                        return Result;
-                     end if;
+         --  Case of composite types
 
-                     Next_Elmt (Constraint);
-                  end loop;
-               end;
-            end if;
+         if Is_Composite_Type (E) then
 
-            --  AI-117 requires that all new primitives of a tagged type
-            --  must inherit the convention of the full view of the type.
-            --  Inherited and overriding operations are defined to inherit
-            --  the convention of their parent or overridden subprogram
-            --  (also specified in AI-117), and that will have occurred
-            --  earlier (in Derive_Subprogram and New_Overloaded_Entity).
-            --  Here we set the convention of primitives that are still
-            --  convention Ada, which will ensure that any new primitives
-            --  inherit the type's convention. Class-wide types can have
-            --  a foreign convention inherited from their specific type,
-            --  but are excluded from this since they don't have any
-            --  associated primitives.
+            --  AI-117 requires that all new primitives of a tagged type must
+            --  inherit the convention of the full view of the type. Inherited
+            --  and overriding operations are defined to inherit the convention
+            --  of their parent or overridden subprogram (also specified in
+            --  AI-117), which will have occurred earlier (in Derive_Subprogram
+            --  and New_Overloaded_Entity). Here we set the convention of
+            --  primitives that are still convention Ada, which will ensure
+            --  that any new primitives inherit the type's convention.
+            --  Class-wide types can have a foreign convention inherited from
+            --  their specific type, but are excluded from this since they
+            --  don't have any associated primitives.
 
             if Is_Tagged_Type (E)
               and then not Is_Class_Wide_Type (E)
@@ -2849,40 +3157,64 @@ package body Freeze is
            and then not Is_Class_Wide_Type (E)
          then
             declare
-               Prim_List : constant Elist_Id := Primitive_Operations (E);
+               Prim_List : Elist_Id;
                Prim      : Elmt_Id;
                Ent       : Entity_Id;
+               Aux_E     : Entity_Id;
 
             begin
-               Prim := First_Elmt (Prim_List);
-               while Present (Prim) loop
-                  Ent := Node (Prim);
+               --  Handle subtypes
+
+               if Ekind (E) = E_Protected_Subtype
+                 or else Ekind (E) = E_Task_Subtype
+               then
+                  Aux_E := Etype (E);
+               else
+                  Aux_E := E;
+               end if;
 
-                  --  If the operation is derived, get the original for
-                  --  cross-reference purposes (it is the original for
-                  --  which we want the xref, and for which the comes
-                  --  from source test needs to be performed).
+               --  Ada 2005 (AI-345): In case of concurrent type generate
+               --  reference to the wrapper that allow us to dispatch calls
+               --  through their implemented abstract interface types.
 
-                  while Present (Alias (Ent)) loop
-                     Ent := Alias (Ent);
-                  end loop;
+               --  The check for Present here is to protect against previously
+               --  reported critical errors.
 
-                  Generate_Reference (E, Ent, 'p', Set_Ref => False);
-                  Next_Elmt (Prim);
-               end loop;
+               if Is_Concurrent_Type (Aux_E)
+                 and then Present (Corresponding_Record_Type (Aux_E))
+               then
+                  Prim_List := Primitive_Operations
+                                (Corresponding_Record_Type (Aux_E));
+               else
+                  Prim_List := Primitive_Operations (Aux_E);
+               end if;
 
-            --  If we get an exception, then something peculiar has happened
-            --  probably as a result of a previous error. Since this is only
-            --  for non-critical cross-references, ignore the error.
+               --  Loop to generate references for primitive operations
 
-            exception
-               when others => null;
+               if Present (Prim_List) then
+                  Prim := First_Elmt (Prim_List);
+                  while Present (Prim) loop
+
+                     --  If the operation is derived, get the original for
+                     --  cross-reference purposes (it is the original for
+                     --  which we want the xref, and for which the comes
+                     --  from source test needs to be performed).
+
+                     Ent := Node (Prim);
+                     while Present (Alias (Ent)) loop
+                        Ent := Alias (Ent);
+                     end loop;
+
+                     Generate_Reference (E, Ent, 'p', Set_Ref => False);
+                     Next_Elmt (Prim);
+                  end loop;
+               end if;
             end;
          end if;
 
-         --  Now that all types from which E may depend are frozen, see
-         --  if the size is known at compile time, if it must be unsigned,
-         --  or if strict alignent is required
+         --  Now that all types from which E may depend are frozen, see if the
+         --  size is known at compile time, if it must be unsigned, or if
+         --  strict alignent is required
 
          Check_Compile_Time_Size (E);
          Check_Unsigned_Type (E);
@@ -2911,7 +3243,7 @@ package body Freeze is
          --  in particular the size and alignment values. This processing is
          --  not required for generic types, since generic types do not play
          --  any part in code generation, and so the size and alignment values
-         --  for suhc types are irrelevant.
+         --  for such types are irrelevant.
 
          if Is_Generic_Type (E) then
             return Result;
@@ -2955,6 +3287,44 @@ package body Freeze is
          else
             Append (F_Node, Result);
          end if;
+
+         --  A final pass over record types with discriminants. If the type
+         --  has an incomplete declaration, there may be constrained access
+         --  subtypes declared elsewhere, which do not depend on the discrimi-
+         --  nants of the type, and which are used as component types (i.e.
+         --  the full view is a recursive type). The designated types of these
+         --  subtypes can only be elaborated after the type itself, and they
+         --  need an itype reference.
+
+         if Ekind (E) = E_Record_Type
+           and then Has_Discriminants (E)
+         then
+            declare
+               Comp : Entity_Id;
+               IR   : Node_Id;
+               Typ  : Entity_Id;
+
+            begin
+               Comp := First_Component (E);
+
+               while Present (Comp) loop
+                  Typ  := Etype (Comp);
+
+                  if Ekind (Comp) = E_Component
+                    and then Is_Access_Type (Typ)
+                    and then Scope (Typ) /= E
+                    and then Base_Type (Designated_Type (Typ)) = E
+                    and then Is_Itype (Designated_Type (Typ))
+                  then
+                     IR := Make_Itype_Reference (Sloc (Comp));
+                     Set_Itype (IR, Designated_Type (Typ));
+                     Append (IR, Result);
+                  end if;
+
+                  Next_Component (Comp);
+               end loop;
+            end;
+         end if;
       end if;
 
       --  When a type is frozen, the first subtype of the type is frozen as
@@ -3003,9 +3373,6 @@ package body Freeze is
             if Result = No_List then
                Result := Empty_List;
             end if;
-
-            Generate_Subprogram_Descriptor_For_Imported_Subprogram
-              (E, Result);
          end if;
       end if;
 
@@ -3050,7 +3417,7 @@ package body Freeze is
 
       function In_Exp_Body (N : Node_Id) return Boolean;
       --  Given an N_Handled_Sequence_Of_Statements node N, determines whether
-      --  it is the handled statement sequence of an expander generated
+      --  it is the handled statement sequence of an expander-generated
       --  subprogram (init proc, or stream subprogram). If so, it returns
       --  True, otherwise False.
 
@@ -3091,10 +3458,10 @@ package body Freeze is
    --  Start of processing for Freeze_Expression
 
    begin
-      --  Immediate return if freezing is inhibited. This flag is set by
-      --  the analyzer to stop freezing on generated expressions that would
-      --  cause freezing if they were in the source program, but which are
-      --  not supposed to freeze, since they are created.
+      --  Immediate return if freezing is inhibited. This flag is set by the
+      --  analyzer to stop freezing on generated expressions that would cause
+      --  freezing if they were in the source program, but which are not
+      --  supposed to freeze, since they are created.
 
       if Must_Not_Freeze (N) then
          return;
@@ -3148,13 +3515,13 @@ package body Freeze is
          Nam := Empty;
       end if;
 
-      --  For an allocator freeze designated type if not frozen already.
+      --  For an allocator freeze designated type if not frozen already
 
-      --  For an aggregate whose component type is an access type, freeze
-      --  the designated type now, so that its freeze  does not appear within
-      --  the loop that might be created in the expansion of the aggregate.
-      --  If the designated type is a private type without full view, the
-      --  expression cannot contain an allocator, so the type is not frozen.
+      --  For an aggregate whose component type is an access type, freeze the
+      --  designated type now, so that its freeze does not appear within the
+      --  loop that might be created in the expansion of the aggregate. If the
+      --  designated type is a private type without full view, the expression
+      --  cannot contain an allocator, so the type is not frozen.
 
       Desig_Typ := Empty;
 
@@ -3208,11 +3575,11 @@ package body Freeze is
       loop
          Parent_P := Parent (P);
 
-         --  If we don't have a parent, then we are not in a well-formed
-         --  tree. This is an unusual case, but there are some legitimate
-         --  situations in which this occurs, notably when the expressions
-         --  in the range of a type declaration are resolved. We simply
-         --  ignore the freeze request in this case. Is this right ???
+         --  If we don't have a parent, then we are not in a well-formed tree.
+         --  This is an unusual case, but there are some legitimate situations
+         --  in which this occurs, notably when the expressions in the range of
+         --  a type declaration are resolved. We simply ignore the freeze
+         --  request in this case. Is this right ???
 
          if No (Parent_P) then
             return;
@@ -3222,12 +3589,12 @@ package body Freeze is
 
          case Nkind (Parent_P) is
 
-            --  A special test for the exception of (RM 13.14(8)) for the
-            --  case of per-object expressions (RM 3.8(18)) occurring in a
-            --  component definition or a discrete subtype definition. Note
-            --  that we test for a component declaration which includes both
-            --  cases we are interested in, and furthermore the tree does not
-            --  have explicit nodes for either of these two constructs.
+            --  A special test for the exception of (RM 13.14(8)) for the case
+            --  of per-object expressions (RM 3.8(18)) occurring in component
+            --  definition or a discrete subtype definition. Note that we test
+            --  for a component declaration which includes both cases we are
+            --  interested in, and furthermore the tree does not have explicit
+            --  nodes for either of these two constructs.
 
             when N_Component_Declaration =>
 
@@ -3258,9 +3625,9 @@ package body Freeze is
                   end if;
                end if;
 
-            --  If we have an enumeration literal that appears as the
-            --  choice in the aggregate of an enumeration representation
-            --  clause, then freezing does not occur (RM 13.14(10)).
+            --  If we have an enumeration literal that appears as the choice in
+            --  the aggregate of an enumeration representation clause, then
+            --  freezing does not occur (RM 13.14(10)).
 
             when N_Enumeration_Representation_Clause =>
 
@@ -3277,9 +3644,9 @@ package body Freeze is
                   then
                      return;
 
-                  --  If enumeration literal appears as the name of a
-                  --  function which is the choice, then also do not freeze.
-                  --  This happens in the overloaded literal case, where the
+                  --  If enumeration literal appears as the name of function
+                  --  which is the choice, then also do not freeze. This
+                  --  happens in the overloaded literal case, where the
                   --  enumeration literal is temporarily changed to a function
                   --  call for overloading analysis purposes.
 
@@ -3299,11 +3666,11 @@ package body Freeze is
 
             when N_Handled_Sequence_Of_Statements =>
 
-               --  An exception occurs when the sequence of statements is
-               --  for an expander generated body that did not do the usual
-               --  freeze all operation. In this case we usually want to
-               --  freeze outside this body, not inside it, and we skip
-               --  past the subprogram body that we are inside.
+               --  An exception occurs when the sequence of statements is for
+               --  an expander generated body that did not do the usual freeze
+               --  all operation. In this case we usually want to freeze
+               --  outside this body, not inside it, and we skip past the
+               --  subprogram body that we are inside.
 
                if In_Exp_Body (Parent_P) then
 
@@ -3351,9 +3718,9 @@ package body Freeze is
                   exit;
                end if;
 
-            --  If parent is a body or a spec or a block, then the current
-            --  node is a statement or declaration and we can insert the
-            --  freeze node before it.
+            --  If parent is a body or a spec or a block, then the current node
+            --  is a statement or declaration and we can insert the freeze node
+            --  before it.
 
             when N_Package_Specification |
                  N_Package_Body          |
@@ -3385,11 +3752,11 @@ package body Freeze is
 
             --  Note: The N_Loop_Statement is a special case. A type that
             --  appears in the source can never be frozen in a loop (this
-            --  occurs only because of a loop expanded by the expander),
-            --  so we keep on going. Otherwise we terminate the search.
-            --  Same is true of any entity which comes from source. (if they
-            --  have a predefined type, that type does not appear to come
-            --  from source, but the entity should not be frozen here).
+            --  occurs only because of a loop expanded by the expander), so we
+            --  keep on going. Otherwise we terminate the search. Same is true
+            --  of any entity which comes from source. (if they have predefined
+            --  type, that type does not appear to come from source, but the
+            --  entity should not be frozen here).
 
             when N_Loop_Statement =>
                exit when not Comes_From_Source (Etype (N))
@@ -3407,22 +3774,27 @@ package body Freeze is
          P := Parent_P;
       end loop;
 
-      --  If the expression appears in a record or an initialization
-      --  procedure, the freeze nodes are collected and attached to
-      --  the current scope, to be inserted and analyzed on exit from
-      --  the scope, to insure that generated entities appear in the
-      --  correct scope. If the expression is a default for a discriminant
-      --  specification, the scope is still void. The expression can also
-      --  appear in the discriminant part of a private or concurrent type.
+      --  If the expression appears in a record or an initialization procedure,
+      --  the freeze nodes are collected and attached to the current scope, to
+      --  be inserted and analyzed on exit from the scope, to insure that
+      --  generated entities appear in the correct scope. If the expression is
+      --  a default for a discriminant specification, the scope is still void.
+      --  The expression can also appear in the discriminant part of a private
+      --  or concurrent type.
+
+      --  If the expression appears in a constrained subcomponent of an
+      --  enclosing record declaration, the freeze nodes must be attached to
+      --  the outer record type so they can eventually be placed in the
+      --  enclosing declaration list.
 
-      --  The other case requiring this special handling is if we are in
-      --  a default expression, since in that case we are about to freeze
-      --  a static type, and the freeze scope needs to be the outer scope,
-      --  not the scope of the subprogram with the default parameter.
+      --  The other case requiring this special handling is if we are in a
+      --  default expression, since in that case we are about to freeze a
+      --  static type, and the freeze scope needs to be the outer scope, not
+      --  the scope of the subprogram with the default parameter.
 
       --  For default expressions in generic units, the Move_Freeze_Nodes
-      --  mechanism (see sem_ch12.adb) takes care of placing them at the
-      --  proper place, after the generic unit.
+      --  mechanism (see sem_ch12.adb) takes care of placing them at the proper
+      --  place, after the generic unit.
 
       if (In_Def_Exp and not Inside_A_Generic)
         or else Freeze_Outside
@@ -3434,6 +3806,7 @@ package body Freeze is
          declare
             Loc          : constant Source_Ptr := Sloc (Current_Scope);
             Freeze_Nodes : List_Id := No_List;
+            Pos          : Int := Scope_Stack.Last;
 
          begin
             if Present (Desig_Typ) then
@@ -3448,16 +3821,21 @@ package body Freeze is
                Freeze_And_Append (Nam, Loc, Freeze_Nodes);
             end if;
 
+            --  The current scope may be that of a constrained component of
+            --  an enclosing record declaration, which is above the current
+            --  scope in the scope stack.
+
+            if Is_Record_Type (Scope (Current_Scope)) then
+               Pos := Pos - 1;
+            end if;
+
             if Is_Non_Empty_List (Freeze_Nodes) then
-               if No (Scope_Stack.Table
-                 (Scope_Stack.Last).Pending_Freeze_Actions)
-               then
-                  Scope_Stack.Table
-                    (Scope_Stack.Last).Pending_Freeze_Actions :=
+               if No (Scope_Stack.Table (Pos).Pending_Freeze_Actions) then
+                  Scope_Stack.Table (Pos).Pending_Freeze_Actions :=
                       Freeze_Nodes;
                else
                   Append_List (Freeze_Nodes, Scope_Stack.Table
-                                   (Scope_Stack.Last).Pending_Freeze_Actions);
+                                   (Pos).Pending_Freeze_Actions);
                end if;
             end if;
          end;
@@ -3467,13 +3845,12 @@ package body Freeze is
 
       --  Now we have the right place to do the freezing. First, a special
       --  adjustment, if we are in default expression analysis mode, these
-      --  freeze actions must not be thrown away (normally all inserted
-      --  actions are thrown away in this mode. However, the freeze actions
-      --  are from static expressions and one of the important reasons we
-      --  are doing this special analysis is to get these freeze actions.
-      --  Therefore we turn off the In_Default_Expression mode to propagate
-      --  these freeze actions. This also means they get properly analyzed
-      --  and expanded.
+      --  freeze actions must not be thrown away (normally all inserted actions
+      --  are thrown away in this mode. However, the freeze actions are from
+      --  static expressions and one of the important reasons we are doing this
+      --  special analysis is to get these freeze actions. Therefore we turn
+      --  off the In_Default_Expression mode to propagate these freeze actions.
+      --  This also means they get properly analyzed and expanded.
 
       In_Default_Expression := False;
 
@@ -3503,15 +3880,15 @@ package body Freeze is
    -- Freeze_Fixed_Point_Type --
    -----------------------------
 
-   --  Certain fixed-point types and subtypes, including implicit base
-   --  types and declared first subtypes, have not yet set up a range.
-   --  This is because the range cannot be set until the Small and Size
-   --  values are known, and these are not known till the type is frozen.
+   --  Certain fixed-point types and subtypes, including implicit base types
+   --  and declared first subtypes, have not yet set up a range. This is
+   --  because the range cannot be set until the Small and Size values are
+   --  known, and these are not known till the type is frozen.
 
-   --  To signal this case, Scalar_Range contains an unanalyzed syntactic
-   --  range whose bounds are unanalyzed real literals. This routine will
-   --  recognize this case, and transform this range node into a properly
-   --  typed range with properly analyzed and resolved values.
+   --  To signal this case, Scalar_Range contains an unanalyzed syntactic range
+   --  whose bounds are unanalyzed real literals. This routine will recognize
+   --  this case, and transform this range node into a properly typed range
+   --  with properly analyzed and resolved values.
 
    procedure Freeze_Fixed_Point_Type (Typ : Entity_Id) is
       Rng   : constant Node_Id    := Scalar_Range (Typ);
@@ -3532,6 +3909,10 @@ package body Freeze is
       --  Returns size of type with given bounds. Also leaves these
       --  bounds set as the current bounds of the Typ.
 
+      -----------
+      -- Fsize --
+      -----------
+
       function Fsize (Lov, Hiv : Ureal) return Nat is
       begin
          Set_Realval (Lo, Lov);
@@ -3539,7 +3920,7 @@ package body Freeze is
          return Minimum_Size (Typ);
       end Fsize;
 
-   --  Start of processing for Freeze_Fixed_Point_Type;
+   --  Start of processing for Freeze_Fixed_Point_Type
 
    begin
       --  If Esize of a subtype has not previously been set, set it now
@@ -3554,9 +3935,9 @@ package body Freeze is
          end if;
       end if;
 
-      --  Immediate return if the range is already analyzed. This means
-      --  that the range is already set, and does not need to be computed
-      --  by this routine.
+      --  Immediate return if the range is already analyzed. This means that
+      --  the range is already set, and does not need to be computed by this
+      --  routine.
 
       if Analyzed (Rng) then
          return;
@@ -3578,16 +3959,16 @@ package body Freeze is
       if Is_Ordinary_Fixed_Point_Type (Typ) then
 
          --  For the ordinary fixed-point case, we are allowed to fudge the
-         --  end-points up or down by small. Generally we prefer to fudge
-         --  up, i.e. widen the bounds for non-model numbers so that the
-         --  end points are included. However there are cases in which this
-         --  cannot be done, and indeed cases in which we may need to narrow
-         --  the bounds. The following circuit makes the decision.
+         --  end-points up or down by small. Generally we prefer to fudge up,
+         --  i.e. widen the bounds for non-model numbers so that the end points
+         --  are included. However there are cases in which this cannot be
+         --  done, and indeed cases in which we may need to narrow the bounds.
+         --  The following circuit makes the decision.
 
-         --  Note: our terminology here is that Incl_EP means that the
-         --  bounds are widened by Small if necessary to include the end
-         --  points, and Excl_EP means that the bounds are narrowed by
-         --  Small to exclude the end-points if this reduces the size.
+         --  Note: our terminology here is that Incl_EP means that the bounds
+         --  are widened by Small if necessary to include the end points, and
+         --  Excl_EP means that the bounds are narrowed by Small to exclude the
+         --  end-points if this reduces the size.
 
          --  Note that in the Incl case, all we care about is including the
          --  end-points. In the Excl case, we want to narrow the bounds as
@@ -3631,10 +4012,10 @@ package body Freeze is
             end if;
 
             --  Compute the fudged bounds. If the number is a model number,
-            --  then we do nothing to include it, but we are allowed to
-            --  backoff to the next adjacent model number when we exclude
-            --  it. If it is not a model number then we straddle the two
-            --  values with the model numbers on either side.
+            --  then we do nothing to include it, but we are allowed to backoff
+            --  to the next adjacent model number when we exclude it. If it is
+            --  not a model number then we straddle the two values with the
+            --  model numbers on either side.
 
             Model_Num := UR_Trunc (Loval / Small) * Small;
 
@@ -3671,11 +4052,11 @@ package body Freeze is
                Hival_Excl_EP := Hival_Incl_EP;
             end if;
 
-            --  One further adjustment is needed. In the case of subtypes,
-            --  we cannot go outside the range of the base type, or we get
+            --  One further adjustment is needed. In the case of subtypes, we
+            --  cannot go outside the range of the base type, or we get
             --  peculiarities, and the base type range is already set. This
-            --  only applies to the Incl values, since clearly the Excl
-            --  values are already as restricted as they are allowed to be.
+            --  only applies to the Incl values, since clearly the Excl values
+            --  are already as restricted as they are allowed to be.
 
             if Typ /= Btyp then
                Loval_Incl_EP := UR_Max (Loval_Incl_EP, Realval (BLo));
@@ -3767,28 +4148,26 @@ package body Freeze is
                   Actual_Hi   := Hival_Incl_EP;
                end if;
 
-               --  One pathological case: normally we never fudge a low
-               --  bound down, since it would seem to increase the size
-               --  (if it has any effect), but for ranges containing a
-               --  single value, or no values, the high bound can be
-               --  small too large. Consider:
+               --  One pathological case: normally we never fudge a low bound
+               --  down, since it would seem to increase the size (if it has
+               --  any effect), but for ranges containing single value, or no
+               --  values, the high bound can be small too large. Consider:
 
                --    type t is delta 2.0**(-14)
                --      range 131072.0 .. 0;
 
-               --  That lower bound is *just* outside the range of 32
-               --  bits, and does need fudging down in this case. Note
-               --  that the bounds will always have crossed here, since
-               --  the high bound will be fudged down if necessary, as
-               --  in the case of:
+               --  That lower bound is *just* outside the range of 32 bits, and
+               --  does need fudging down in this case. Note that the bounds
+               --  will always have crossed here, since the high bound will be
+               --  fudged down if necessary, as in the case of:
 
                --    type t is delta 2.0**(-14)
                --      range 131072.0 .. 131072.0;
 
-               --  So we can detect the situation by looking for crossed
-               --  bounds, and if the bounds are crossed, and the low
-               --  bound is greater than zero, we will always back it
-               --  off by small, since this is completely harmless.
+               --  So we detect the situation by looking for crossed bounds,
+               --  and if the bounds are crossed, and the low bound is greater
+               --  than zero, we will always back it off by small, since this
+               --  is completely harmless.
 
                if Actual_Lo > Actual_Hi then
                   if UR_Is_Positive (Actual_Lo) then
@@ -3858,9 +4237,9 @@ package body Freeze is
          Adjust_Esize_For_Alignment (Typ);
       end if;
 
-      --  If we have a base type, then expand the bounds so that they
-      --  extend to the full width of the allocated size in bits, to
-      --  avoid junk range checks on intermediate computations.
+      --  If we have a base type, then expand the bounds so that they extend to
+      --  the full width of the allocated size in bits, to avoid junk range
+      --  checks on intermediate computations.
 
       if Base_Type (Typ) = Typ then
          Set_Realval (Lo, -(Small * (Uint_2 ** (Actual_Size - 1))));
@@ -3874,9 +4253,9 @@ package body Freeze is
       Set_Analyzed (Lo, False);
       Analyze (Lo);
 
-      --  Resolve with universal fixed if the base type, and the base
-      --  type if it is a subtype. Note we can't resolve the base type
-      --  with itself, that would be a reference before definition.
+      --  Resolve with universal fixed if the base type, and the base type if
+      --  it is a subtype. Note we can't resolve the base type with itself,
+      --  that would be a reference before definition.
 
       if Typ = Btyp then
          Resolve (Lo, Universal_Fixed);
@@ -3962,10 +4341,10 @@ package body Freeze is
       --  static. This happens if the type depends on non-global objects.
 
       procedure Ensure_Expression_Is_SA (N : Node_Id);
-      --  Called to ensure that an expression used as part of a type
-      --  definition is statically allocatable, which means that the type
-      --  of the expression is statically allocatable, and the expression
-      --  is either static, or a reference to a library level constant.
+      --  Called to ensure that an expression used as part of a type definition
+      --  is statically allocatable, which means that the expression type is
+      --  statically allocatable, and the expression is either static, or a
+      --  reference to a library level constant.
 
       procedure Ensure_Type_Is_SA (Typ : Entity_Id);
       --  Called to mark a type as static, checking that it is possible
@@ -4014,8 +4393,8 @@ package body Freeze is
             return;
          end if;
 
-         --  We are also OK if the type is already marked as statically
-         --  allocated, which means we processed it before.
+         --  We are also OK if the type already marked as statically allocated,
+         --  which means we processed it before.
 
          if Is_Statically_Allocated (Typ) then
             return;
@@ -4066,7 +4445,6 @@ package body Freeze is
 
          elsif Is_Record_Type (Typ) then
             C := First_Entity (Typ);
-
             while Present (C) loop
                if Ekind (C) = E_Discriminant
                  or else Ekind (C) = E_Component
@@ -4099,14 +4477,18 @@ package body Freeze is
    begin
       Ensure_Type_Is_SA (Etype (E));
 
-      --  Reset True_Constant flag, since something strange is going on
-      --  with the scoping here, and our simple value traceing may not
-      --  be sufficient for this indication to be reliable. We kill the
-      --  Constant_Value indication for the same reason.
+      --  Reset True_Constant flag, since something strange is going on with
+      --  the scoping here, and our simple value tracing may not be sufficient
+      --  for this indication to be reliable. We kill the Constant_Value
+      --  and Last_Assignment indications for the same reason.
 
       Set_Is_True_Constant (E, False);
       Set_Current_Value    (E, Empty);
 
+      if Ekind (E) = E_Variable then
+         Set_Last_Assignment  (E, Empty);
+      end if;
+
    exception
       when Cannot_Be_Static =>
 
@@ -4147,6 +4529,20 @@ package body Freeze is
          end if;
       end if;
 
+      --  Reset the Pure indication on an imported subprogram unless an
+      --  explicit Pure_Function pragma was present. We do this because
+      --  otherwise it is an insidious error to call a non-pure function from
+      --  pure unit and have calls mysteriously optimized away. What happens
+      --  here is that the Import can bypass the normal check to ensure that
+      --  pure units call only pure subprograms.
+
+      if Is_Imported (E)
+        and then Is_Pure (E)
+        and then not Has_Pragma_Pure_Function (E)
+      then
+         Set_Is_Pure (E, False);
+      end if;
+
       --  For non-foreign convention subprograms, this is where we create
       --  the extra formals (for accessibility level and constrained bit
       --  information). We delay this till the freeze point precisely so
@@ -4189,8 +4585,8 @@ package body Freeze is
                null;
 
             --  If the return type is generic, we have emitted a warning
-            --  earlier on, and there is nothing else to check here.
-            --  Specific instantiations may lead to erroneous behavior.
+            --  earlier on, and there is nothing else to check here. Specific
+            --  instantiations may lead to erroneous behavior.
 
             elsif Is_Generic_Type (Etype (E)) then
                null;
@@ -4208,8 +4604,8 @@ package body Freeze is
          end if;
 
          --  If any of the formals for an exported foreign convention
-         --  subprogram have defaults, then emit an appropriate warning
-         --  since this is odd (default cannot be used from non-Ada code)
+         --  subprogram have defaults, then emit an appropriate warning since
+         --  this is odd (default cannot be used from non-Ada code)
 
          if Is_Exported (E) then
             F := First_Formal (E);
@@ -4245,11 +4641,22 @@ package body Freeze is
             end loop;
          end if;
       end if;
+
+      --  Pragma Inline_Always is disallowed for dispatching subprograms
+      --  because the address of such subprograms is saved in the dispatch
+      --  table to support dispatching calls, and dispatching calls cannot
+      --  be inlined. This is consistent with the restriction against using
+      --  'Access or 'Address on an Inline_Always subprogram.
+
+      if Is_Dispatching_Operation (E) and then Is_Always_Inlined (E) then
+         Error_Msg_N
+           ("pragma Inline_Always not allowed for dispatching subprograms", E);
+      end if;
    end Freeze_Subprogram;
 
-   -----------------------
-   --  Is_Fully_Defined --
-   -----------------------
+   ----------------------
+   -- Is_Fully_Defined --
+   ----------------------
 
    function Is_Fully_Defined (T : Entity_Id) return Boolean is
    begin
@@ -4262,12 +4669,12 @@ package body Freeze is
       elsif Is_Record_Type (T)
         and not Is_Private_Type (T)
       then
-
-         --  Verify that the record type has no components with
-         --  private types without completion.
+         --  Verify that the record type has no components with private types
+         --  without completion.
 
          declare
             Comp : Entity_Id;
+
          begin
             Comp := First_Component (T);
 
@@ -4303,10 +4710,10 @@ package body Freeze is
    begin
       Set_Default_Expressions_Processed (E);
 
-      --  A subprogram instance and its associated anonymous subprogram
-      --  share their signature. The default expression functions are defined
-      --  in the wrapper packages for the anonymous subprogram, and should
-      --  not be generated again for the instance.
+      --  A subprogram instance and its associated anonymous subprogram share
+      --  their signature. The default expression functions are defined in the
+      --  wrapper packages for the anonymous subprogram, and should not be
+      --  generated again for the instance.
 
       if Is_Generic_Instance (E)
         and then Present (Alias (E))
@@ -4346,11 +4753,10 @@ package body Freeze is
             then
 
                --  If there is no default function, we must still do a full
-               --  analyze call on the default value, to ensure that all
-               --  error checks are performed, e.g. those associated with
-               --  static evaluation. Note that this branch will always be
-               --  taken if the analyzer is turned off (but we still need the
-               --  error checks).
+               --  analyze call on the default value, to ensure that all error
+               --  checks are performed, e.g. those associated with static
+               --  evaluation. Note: this branch will always be taken if the
+               --  analyzer is turned off (but we still need the error checks).
 
                --  Note: the setting of parent here is to meet the requirement
                --  that we can only analyze the expression while attached to
@@ -4518,6 +4924,44 @@ package body Freeze is
    end Set_Debug_Info_Needed;
 
    ------------------
+   -- Undelay_Type --
+   ------------------
+
+   procedure Undelay_Type (T : Entity_Id) is
+   begin
+      Set_Has_Delayed_Freeze (T, False);
+      Set_Freeze_Node (T, Empty);
+
+      --  Since we don't want T to have a Freeze_Node, we don't want its
+      --  Full_View or Corresponding_Record_Type to have one either.
+
+      --  ??? Fundamentally, this whole handling is a kludge. What we really
+      --  want is to be sure that for an Itype that's part of record R and is a
+      --  subtype of type T, that it's frozen after the later of the freeze
+      --  points of R and T. We have no way of doing that directly, so what we
+      --  do is force most such Itypes to be frozen as part of freezing R via
+      --  this procedure and only delay the ones that need to be delayed
+      --  (mostly the designated types of access types that are defined as part
+      --  of the record).
+
+      if Is_Private_Type (T)
+        and then Present (Full_View (T))
+        and then Is_Itype (Full_View (T))
+        and then Is_Record_Type (Scope (Full_View (T)))
+      then
+         Undelay_Type (Full_View (T));
+      end if;
+
+      if Is_Concurrent_Type (T)
+        and then Present (Corresponding_Record_Type (T))
+        and then Is_Itype (Corresponding_Record_Type (T))
+        and then Is_Record_Type (Scope (Corresponding_Record_Type (T)))
+      then
+         Undelay_Type (Corresponding_Record_Type (T));
+      end if;
+   end Undelay_Type;
+
+   ------------------
    -- Warn_Overlay --
    ------------------
 
@@ -4527,7 +4971,7 @@ package body Freeze is
       Nam  : Entity_Id)
    is
       Ent : constant Entity_Id := Entity (Nam);
-      --  The object to which the address clause applies.
+      --  The object to which the address clause applies
 
       Init : Node_Id;
       Old  : Entity_Id := Empty;
@@ -4548,9 +4992,9 @@ package body Freeze is
          return;
       end if;
 
-      --  We only give the warning for non-imported entities of a type
-      --  for which a non-null base init proc is defined (or for access
-      --  types which have implicit null initialization).
+      --  We only give the warning for non-imported entities of a type for
+      --  which a non-null base init proc is defined (or for access types which
+      --  have implicit null initialization).
 
       if Present (Expr)
         and then (Has_Non_Null_Base_Init_Proc (Typ)
@@ -4578,9 +5022,9 @@ package body Freeze is
                return;
             end if;
 
-         --  A function call (most likely to To_Address) is probably not
-         --  an overlay, so skip warning. Ditto if the function call was
-         --  inlined and transformed into an entity.
+         --  A function call (most likely to To_Address) is probably not an
+         --  overlay, so skip warning. Ditto if the function call was inlined
+         --  and transformed into an entity.
 
          elsif Nkind (Original_Node (Expr)) = N_Function_Call then
             return;
@@ -4628,8 +5072,9 @@ package body Freeze is
                      and then Present (Packed_Array_Type (Etype (Comp)))
                   then
                      Error_Msg_NE
-                       ("packed array component& will be initialized to zero?",
-                          Nam, Comp);
+                       ("\packed array component& " &
+                        "will be initialized to zero?",
+                        Nam, Comp);
                      exit;
                   else
                      Next_Component (Comp);
@@ -4639,9 +5084,9 @@ package body Freeze is
          end if;
 
          Error_Msg_N
-           ("use pragma Import for & to " &
-              "suppress initialization ('R'M B.1(24))?",
-             Nam);
+           ("\use pragma Import for & to " &
+            "suppress initialization ('R'M B.1(24))?",
+            Nam);
       end if;
    end Warn_Overlay;