OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch6.adb
index 6c9b399..668a2a7 100644 (file)
@@ -6,18 +6,17 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2003, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2008, 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- --
--- ware  Foundation;  either version 2,  or (at your option) any later ver- --
+-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
 -- 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.                                                      --
+-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
+-- http://www.gnu.org/licenses for a complete copy of the license.          --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
@@ -31,10 +30,16 @@ with Einfo;    use Einfo;
 with Elists;   use Elists;
 with Errout;   use Errout;
 with Expander; use Expander;
+with Exp_Ch6;  use Exp_Ch6;
 with Exp_Ch7;  use Exp_Ch7;
+with Exp_Ch9;  use Exp_Ch9;
+with Exp_Tss;  use Exp_Tss;
+with Exp_Util; use Exp_Util;
 with Fname;    use Fname;
 with Freeze;   use Freeze;
+with Itypes;   use Itypes;
 with Lib.Xref; use Lib.Xref;
+with Layout;   use Layout;
 with Namet;    use Namet;
 with Lib;      use Lib;
 with Nlists;   use Nlists;
@@ -48,6 +53,7 @@ with Sem_Ch3;  use Sem_Ch3;
 with Sem_Ch4;  use Sem_Ch4;
 with Sem_Ch5;  use Sem_Ch5;
 with Sem_Ch8;  use Sem_Ch8;
+with Sem_Ch10; use Sem_Ch10;
 with Sem_Ch12; use Sem_Ch12;
 with Sem_Disp; use Sem_Disp;
 with Sem_Dist; use Sem_Dist;
@@ -74,38 +80,53 @@ with Validsw;  use Validsw;
 
 package body Sem_Ch6 is
 
+   May_Hide_Profile : Boolean := False;
+   --  This flag is used to indicate that two formals in two subprograms being
+   --  checked for conformance differ only in that one is an access parameter
+   --  while the other is of a general access type with the same designated
+   --  type. In this case, if the rest of the signatures match, a call to
+   --  either subprogram may be ambiguous, which is worth a warning. The flag
+   --  is set in Compatible_Types, and the warning emitted in
+   --  New_Overloaded_Entity.
+
    -----------------------
    -- Local Subprograms --
    -----------------------
 
+   procedure Analyze_Return_Statement (N : Node_Id);
+   --  Common processing for simple_ and extended_return_statements
+
+   procedure Analyze_Function_Return (N : Node_Id);
+   --  Subsidiary to Analyze_Return_Statement. Called when the return statement
+   --  applies to a [generic] function.
+
+   procedure Analyze_Return_Type (N : Node_Id);
+   --  Subsidiary to Process_Formals: analyze subtype mark in function
+   --  specification, in a context where the formals are visible and hide
+   --  outer homographs.
+
    procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
-   --  Analyze a generic subprogram body. N is the body to be analyzed,
-   --  and Gen_Id is the defining entity Id for the corresponding spec.
-
-   function Build_Body_To_Inline
-     (N         : Node_Id;
-      Subp      : Entity_Id;
-      Orig_Body : Node_Id)
-      return      Boolean;
+   --  Analyze a generic subprogram body. N is the body to be analyzed, and
+   --  Gen_Id is the defining entity Id for the corresponding spec.
+
+   procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
    --  If a subprogram has pragma Inline and inlining is active, use generic
    --  machinery to build an unexpanded body for the subprogram. This body is
    --  subsequenty used for inline expansions at call sites. If subprogram can
    --  be inlined (depending on size and nature of local declarations) this
    --  function returns true. Otherwise subprogram body is treated normally.
-
-   type Conformance_Type is
-     (Type_Conformant, Mode_Conformant, Subtype_Conformant, Fully_Conformant);
-   --  Conformance type used for following call, meaning matches the
-   --  RM definitions of the corresponding terms.
+   --  If proper warnings are enabled and the subprogram contains a construct
+   --  that cannot be inlined, the offending construct is flagged accordingly.
 
    procedure Check_Conformance
-     (New_Id   : Entity_Id;
-      Old_Id   : Entity_Id;
-      Ctype    : Conformance_Type;
-      Errmsg   : Boolean;
-      Conforms : out Boolean;
-      Err_Loc  : Node_Id := Empty;
-      Get_Inst : Boolean := False);
+     (New_Id                   : Entity_Id;
+      Old_Id                   : Entity_Id;
+      Ctype                    : Conformance_Type;
+      Errmsg                   : Boolean;
+      Conforms                 : out Boolean;
+      Err_Loc                  : Node_Id := Empty;
+      Get_Inst                 : Boolean := False;
+      Skip_Controlling_Formals : Boolean := False);
    --  Given two entities, this procedure checks that the profiles associated
    --  with these entities meet the conformance criterion given by the third
    --  parameter. If they conform, Conforms is set True and control returns
@@ -118,62 +139,40 @@ package body Sem_Ch6 is
    --  against a formal access-to-subprogram type so Get_Instance_Of must
    --  be called.
 
-   procedure Check_Overriding_Operation
-     (N    : Node_Id;
-      Subp : Entity_Id);
-   --  Check that a subprogram with a pragma Overriding or Optional_Overriding
-   --  is legal. This check is performed here rather than in Sem_Prag because
-   --  the pragma must follow immediately the declaration, and can be treated
-   --  as part of the declaration itself, as described in AI-218.
-
    procedure Check_Subprogram_Order (N : Node_Id);
    --  N is the N_Subprogram_Body node for a subprogram. This routine applies
    --  the alpha ordering rule for N if this ordering requirement applicable.
 
-   function Is_Non_Overriding_Operation
-     (Prev_E : Entity_Id;
-      New_E  : Entity_Id)
-      return   Boolean;
-   --  Enforce the rule given in 12.3(18): a private operation in an instance
-   --  overrides an inherited operation only if the corresponding operation
-   --  was overriding in the generic. This can happen for primitive operations
-   --  of types derived (in the generic unit) from formal private or formal
-   --  derived types.
-
    procedure Check_Returns
      (HSS  : Node_Id;
       Mode : Character;
-      Err  : out Boolean);
-   --  Called to check for missing return statements in a function body,
-   --  or for returns present in a procedure body which has No_Return set.
-   --  L is the handled statement sequence for the subprogram body. This
-   --  procedure checks all flow paths to make sure they either have a
-   --  return (Mode = 'F') or do not have a return (Mode = 'P'). The flag
-   --  Err is set if there are any control paths not explicitly terminated
-   --  by a return in the function case, and is True otherwise.
-
-   function Conforming_Types
-     (T1       : Entity_Id;
-      T2       : Entity_Id;
-      Ctype    : Conformance_Type;
-      Get_Inst : Boolean := False)
-      return     Boolean;
-   --  Check that two formal parameter types conform, checking both
-   --  for equality of base types, and where required statically
-   --  matching subtypes, depending on the setting of Ctype.
+      Err  : out Boolean;
+      Proc : Entity_Id := Empty);
+   --  Called to check for missing return statements in a function body, or for
+   --  returns present in a procedure body which has No_Return set. HSS is the
+   --  handled statement sequence for the subprogram body. This procedure
+   --  checks all flow paths to make sure they either have return (Mode = 'F',
+   --  used for functions) or do not have a return (Mode = 'P', used for
+   --  No_Return procedures). The flag Err is set if there are any control
+   --  paths not explicitly terminated by a return in the function case, and is
+   --  True otherwise. Proc is the entity for the procedure case and is used
+   --  in posting the warning message.
 
    procedure Enter_Overloaded_Entity (S : Entity_Id);
-   --  This procedure makes S, a new overloaded entity, into the first
-   --  visible entity with that name.
+   --  This procedure makes S, a new overloaded entity, into the first visible
+   --  entity with that name.
 
    procedure Install_Entity (E : Entity_Id);
-   --  Make single entity visible. Used for generic formals as well.
+   --  Make single entity visible. Used for generic formals as well
 
-   procedure Install_Formals (Id : Entity_Id);
-   --  On entry to a subprogram body, make the formals visible. Note
-   --  that simply placing the subprogram on the scope stack is not
-   --  sufficient: the formals must become the current entities for
-   --  their names.
+   function Is_Non_Overriding_Operation
+     (Prev_E : Entity_Id;
+      New_E  : Entity_Id) return Boolean;
+   --  Enforce the rule given in 12.3(18): a private operation in an instance
+   --  overrides an inherited operation only if the corresponding operation
+   --  was overriding in the generic. This can happen for primitive operations
+   --  of types derived (in the generic unit) from formal private or formal
+   --  derived types.
 
    procedure Make_Inequality_Operator (S : Entity_Id);
    --  Create the declaration for an inequality operator that is implicitly
@@ -183,17 +182,152 @@ package body Sem_Ch6 is
    --  Flag functions that can be called without parameters, i.e. those that
    --  have no parameters, or those for which defaults exist for all parameters
 
-   procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id);
-   --  If there is a separate spec for a subprogram or generic subprogram,
-   --  the formals of the body are treated as references to the corresponding
-   --  formals of the spec. This reference does not count as an actual use of
-   --  the formal, in order to diagnose formals that are unused in the body.
+   procedure Process_PPCs
+     (N       : Node_Id;
+      Spec_Id : Entity_Id;
+      Body_Id : Entity_Id);
+   --  Called from Analyze_Body to deal with scanning post conditions for the
+   --  body and assembling and inserting the _postconditions procedure. N is
+   --  the node for the subprogram body and Body_Id/Spec_Id are the entities
+   --  for the body and separate spec (if there is no separate spec, Spec_Id
+   --  is Empty).
 
    procedure Set_Formal_Validity (Formal_Id : Entity_Id);
    --  Formal_Id is an formal parameter entity. This procedure deals with
    --  setting the proper validity status for this entity, which depends
    --  on the kind of parameter and the validity checking mode.
 
+   ------------------------------
+   -- Analyze_Return_Statement --
+   ------------------------------
+
+   procedure Analyze_Return_Statement (N : Node_Id) is
+
+      pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
+                                  N_Extended_Return_Statement));
+
+      Returns_Object : constant Boolean :=
+                         Nkind (N) = N_Extended_Return_Statement
+                           or else
+                            (Nkind (N) = N_Simple_Return_Statement
+                              and then Present (Expression (N)));
+      --  True if we're returning something; that is, "return <expression>;"
+      --  or "return Result : T [:= ...]". False for "return;". Used for error
+      --  checking: If Returns_Object is True, N should apply to a function
+      --  body; otherwise N should apply to a procedure body, entry body,
+      --  accept statement, or extended return statement.
+
+      function Find_What_It_Applies_To return Entity_Id;
+      --  Find the entity representing the innermost enclosing body, accept
+      --  statement, or extended return statement. If the result is a callable
+      --  construct or extended return statement, then this will be the value
+      --  of the Return_Applies_To attribute. Otherwise, the program is
+      --  illegal. See RM-6.5(4/2).
+
+      -----------------------------
+      -- Find_What_It_Applies_To --
+      -----------------------------
+
+      function Find_What_It_Applies_To return Entity_Id is
+         Result : Entity_Id := Empty;
+
+      begin
+         --  Loop outward through the Scope_Stack, skipping blocks and loops
+
+         for J in reverse 0 .. Scope_Stack.Last loop
+            Result := Scope_Stack.Table (J).Entity;
+            exit when Ekind (Result) /= E_Block and then
+                      Ekind (Result) /= E_Loop;
+         end loop;
+
+         pragma Assert (Present (Result));
+         return Result;
+      end Find_What_It_Applies_To;
+
+      --  Local declarations
+
+      Scope_Id   : constant Entity_Id   := Find_What_It_Applies_To;
+      Kind       : constant Entity_Kind := Ekind (Scope_Id);
+      Loc        : constant Source_Ptr  := Sloc (N);
+      Stm_Entity : constant Entity_Id   :=
+                     New_Internal_Entity
+                       (E_Return_Statement, Current_Scope, Loc, 'R');
+
+   --  Start of processing for Analyze_Return_Statement
+
+   begin
+      Set_Return_Statement_Entity (N, Stm_Entity);
+
+      Set_Etype (Stm_Entity, Standard_Void_Type);
+      Set_Return_Applies_To (Stm_Entity, Scope_Id);
+
+      --  Place Return entity on scope stack, to simplify enforcement of 6.5
+      --  (4/2): an inner return statement will apply to this extended return.
+
+      if Nkind (N) = N_Extended_Return_Statement then
+         Push_Scope (Stm_Entity);
+      end if;
+
+      --  Check that pragma No_Return is obeyed
+
+      if No_Return (Scope_Id) then
+         Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
+      end if;
+
+      --  Warn on any unassigned OUT parameters if in procedure
+
+      if Ekind (Scope_Id) = E_Procedure then
+         Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
+      end if;
+
+      --  Check that functions return objects, and other things do not
+
+      if Kind = E_Function or else Kind = E_Generic_Function then
+         if not Returns_Object then
+            Error_Msg_N ("missing expression in return from function", N);
+         end if;
+
+      elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
+         if Returns_Object then
+            Error_Msg_N ("procedure cannot return value (use function)", N);
+         end if;
+
+      elsif Kind = E_Entry or else Kind = E_Entry_Family then
+         if Returns_Object then
+            if Is_Protected_Type (Scope (Scope_Id)) then
+               Error_Msg_N ("entry body cannot return value", N);
+            else
+               Error_Msg_N ("accept statement cannot return value", N);
+            end if;
+         end if;
+
+      elsif Kind = E_Return_Statement then
+
+         --  We are nested within another return statement, which must be an
+         --  extended_return_statement.
+
+         if Returns_Object then
+            Error_Msg_N
+              ("extended_return_statement cannot return value; " &
+               "use `""RETURN;""`", N);
+         end if;
+
+      else
+         Error_Msg_N ("illegal context for return statement", N);
+      end if;
+
+      if Kind = E_Function or else Kind = E_Generic_Function then
+         Analyze_Function_Return (N);
+      end if;
+
+      if Nkind (N) = N_Extended_Return_Statement then
+         End_Scope;
+      end if;
+
+      Kill_Current_Values (Last_Assignment_Only => True);
+      Check_Unreachable_Code (N);
+   end Analyze_Return_Statement;
+
    ---------------------------------------------
    -- Analyze_Abstract_Subprogram_Declaration --
    ---------------------------------------------
@@ -205,7 +339,7 @@ package body Sem_Ch6 is
 
    begin
       Generate_Definition (Designator);
-      Set_Is_Abstract (Designator);
+      Set_Is_Abstract_Subprogram (Designator);
       New_Overloaded_Entity (Designator);
       Check_Delayed_Subprogram (Designator);
 
@@ -214,11 +348,33 @@ package body Sem_Ch6 is
       if Ekind (Scope (Designator)) = E_Protected_Type then
          Error_Msg_N
            ("abstract subprogram not allowed in protected type", N);
+
+      --  Issue a warning if the abstract subprogram is neither a dispatching
+      --  operation nor an operation that overrides an inherited subprogram or
+      --  predefined operator, since this most likely indicates a mistake.
+
+      elsif Warn_On_Redundant_Constructs
+        and then not Is_Dispatching_Operation (Designator)
+        and then not Is_Overriding_Operation (Designator)
+        and then (not Is_Operator_Symbol_Name (Chars (Designator))
+                   or else Scop /= Scope (Etype (First_Formal (Designator))))
+      then
+         Error_Msg_N
+           ("?abstract subprogram is not dispatching or overriding", N);
       end if;
 
       Generate_Reference_To_Formals (Designator);
    end Analyze_Abstract_Subprogram_Declaration;
 
+   ----------------------------------------
+   -- Analyze_Extended_Return_Statement  --
+   ----------------------------------------
+
+   procedure Analyze_Extended_Return_Statement (N : Node_Id) is
+   begin
+      Analyze_Return_Statement (N);
+   end Analyze_Extended_Return_Statement;
+
    ----------------------------
    -- Analyze_Function_Call  --
    ----------------------------
@@ -231,6 +387,18 @@ package body Sem_Ch6 is
    begin
       Analyze (P);
 
+      --  A call of the form A.B (X) may be an Ada05 call, which is rewritten
+      --  as B (A, X). If the rewriting is successful, the call has been
+      --  analyzed and we just return.
+
+      if Nkind (P) = N_Selected_Component
+        and then Name (N) /= P
+        and then Is_Rewrite_Substitution (N)
+        and then Present (Etype (N))
+      then
+         return;
+      end if;
+
       --  If error analyzing name, then set Any_Type as result type and return
 
       if Etype (P) = Any_Type then
@@ -242,7 +410,6 @@ package body Sem_Ch6 is
 
       if Present (L) then
          Actual := First (L);
-
          while Present (Actual) loop
             Analyze (Actual);
             Check_Parameterless_Call (Actual);
@@ -253,6 +420,333 @@ package body Sem_Ch6 is
       Analyze_Call (N);
    end Analyze_Function_Call;
 
+   -----------------------------
+   -- Analyze_Function_Return --
+   -----------------------------
+
+   procedure Analyze_Function_Return (N : Node_Id) is
+      Loc        : constant Source_Ptr  := Sloc (N);
+      Stm_Entity : constant Entity_Id   := Return_Statement_Entity (N);
+      Scope_Id   : constant Entity_Id   := Return_Applies_To (Stm_Entity);
+
+      R_Type : constant Entity_Id := Etype (Scope_Id);
+      --  Function result subtype
+
+      procedure Check_Limited_Return (Expr : Node_Id);
+      --  Check the appropriate (Ada 95 or Ada 2005) rules for returning
+      --  limited types. Used only for simple return statements.
+      --  Expr is the expression returned.
+
+      procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
+      --  Check that the return_subtype_indication properly matches the result
+      --  subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
+
+      --------------------------
+      -- Check_Limited_Return --
+      --------------------------
+
+      procedure Check_Limited_Return (Expr : Node_Id) is
+      begin
+         --  Ada 2005 (AI-318-02): Return-by-reference types have been
+         --  removed and replaced by anonymous access results. This is an
+         --  incompatibility with Ada 95. Not clear whether this should be
+         --  enforced yet or perhaps controllable with special switch. ???
+
+         if Is_Limited_Type (R_Type)
+           and then Comes_From_Source (N)
+           and then not In_Instance_Body
+           and then not OK_For_Limited_Init_In_05 (Expr)
+         then
+            --  Error in Ada 2005
+
+            if Ada_Version >= Ada_05
+              and then not Debug_Flag_Dot_L
+              and then not GNAT_Mode
+            then
+               Error_Msg_N
+                 ("(Ada 2005) cannot copy object of a limited type " &
+                  "(RM-2005 6.5(5.5/2))", Expr);
+               if Is_Inherently_Limited_Type (R_Type) then
+                  Error_Msg_N
+                    ("\return by reference not permitted in Ada 2005", Expr);
+               end if;
+
+            --  Warn in Ada 95 mode, to give folks a heads up about this
+            --  incompatibility.
+
+            --  In GNAT mode, this is just a warning, to allow it to be
+            --  evilly turned off. Otherwise it is a real error.
+
+            elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
+               if Is_Inherently_Limited_Type (R_Type) then
+                  Error_Msg_N
+                    ("return by reference not permitted in Ada 2005 " &
+                     "(RM-2005 6.5(5.5/2))?", Expr);
+               else
+                  Error_Msg_N
+                    ("cannot copy object of a limited type in Ada 2005 " &
+                     "(RM-2005 6.5(5.5/2))?", Expr);
+               end if;
+
+            --  Ada 95 mode, compatibility warnings disabled
+
+            else
+               return; --  skip continuation messages below
+            end if;
+
+            Error_Msg_N
+              ("\consider switching to return of access type", Expr);
+            Explain_Limited_Type (R_Type, Expr);
+         end if;
+      end Check_Limited_Return;
+
+      -------------------------------------
+      -- Check_Return_Subtype_Indication --
+      -------------------------------------
+
+      procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
+         Return_Obj  : constant Node_Id   := Defining_Identifier (Obj_Decl);
+         R_Stm_Type  : constant Entity_Id := Etype (Return_Obj);
+         --  Subtype given in the extended return statement;
+         --  this must match R_Type.
+
+         Subtype_Ind : constant Node_Id :=
+                         Object_Definition (Original_Node (Obj_Decl));
+
+         R_Type_Is_Anon_Access :
+           constant Boolean :=
+             Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
+               or else
+             Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
+               or else
+             Ekind (R_Type) = E_Anonymous_Access_Type;
+         --  True if return type of the function is an anonymous access type
+         --  Can't we make Is_Anonymous_Access_Type in einfo ???
+
+         R_Stm_Type_Is_Anon_Access :
+           constant Boolean :=
+             Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
+               or else
+             Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
+               or else
+             Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
+         --  True if type of the return object is an anonymous access type
+
+      begin
+         --  First, avoid cascade errors:
+
+         if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
+            return;
+         end if;
+
+         --  "return access T" case; check that the return statement also has
+         --  "access T", and that the subtypes statically match:
+
+         if R_Type_Is_Anon_Access then
+            if R_Stm_Type_Is_Anon_Access then
+               if Base_Type (Designated_Type (R_Stm_Type)) /=
+                    Base_Type (Designated_Type (R_Type))
+                 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
+               then
+                  Error_Msg_N
+                    ("subtype must statically match function result subtype",
+                     Subtype_Mark (Subtype_Ind));
+               end if;
+
+            else
+               Error_Msg_N ("must use anonymous access type", Subtype_Ind);
+            end if;
+
+         --  Subtype_indication case; check that the types are the same, and
+         --  statically match if appropriate. A null exclusion may be present
+         --  on the return type, on the function specification, on the object
+         --  declaration or on the subtype itself.
+
+         elsif Base_Type (R_Stm_Type) = Base_Type (R_Type) then
+            if Is_Access_Type (R_Type)
+              and then
+               (Can_Never_Be_Null (R_Type)
+                 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
+                                              Can_Never_Be_Null (R_Stm_Type)
+            then
+               Error_Msg_N
+                 ("subtype must statically match function result subtype",
+                  Subtype_Ind);
+            end if;
+
+            if Is_Constrained (R_Type) then
+               if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
+                  Error_Msg_N
+                    ("subtype must statically match function result subtype",
+                     Subtype_Ind);
+               end if;
+            end if;
+
+         --  If the function's result type doesn't match the return object
+         --  entity's type, then we check for the case where the result type
+         --  is class-wide, and allow the declaration if the type of the object
+         --  definition matches the class-wide type. This prevents rejection
+         --  in the case where the object declaration is initialized by a call
+         --  to a build-in-place function with a specific result type and the
+         --  object entity had its type changed to that specific type. (Note
+         --  that the ARG believes that return objects should be allowed to
+         --  have a type covered by a class-wide result type in any case, so
+         --  once that relaxation is made (see AI05-32), the above check for
+         --  type compatibility should be changed to test Covers rather than
+         --  equality, and then the following special test will no longer be
+         --  needed. ???)
+
+         elsif Is_Class_Wide_Type (R_Type)
+           and then
+             R_Type = Etype (Object_Definition (Original_Node (Obj_Decl)))
+         then
+            null;
+
+         else
+            Error_Msg_N
+              ("wrong type for return_subtype_indication", Subtype_Ind);
+         end if;
+      end Check_Return_Subtype_Indication;
+
+      ---------------------
+      -- Local Variables --
+      ---------------------
+
+      Expr : Node_Id;
+
+   --  Start of processing for Analyze_Function_Return
+
+   begin
+      Set_Return_Present (Scope_Id);
+
+      if Nkind (N) = N_Simple_Return_Statement then
+         Expr := Expression (N);
+         Analyze_And_Resolve (Expr, R_Type);
+         Check_Limited_Return (Expr);
+
+      else
+         --  Analyze parts specific to extended_return_statement:
+
+         declare
+            Obj_Decl : constant Node_Id :=
+                         Last (Return_Object_Declarations (N));
+
+            HSS : constant Node_Id := Handled_Statement_Sequence (N);
+
+         begin
+            Expr := Expression (Obj_Decl);
+
+            --  Note: The check for OK_For_Limited_Init will happen in
+            --  Analyze_Object_Declaration; we treat it as a normal
+            --  object declaration.
+
+            Analyze (Obj_Decl);
+
+            Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
+            Check_Return_Subtype_Indication (Obj_Decl);
+
+            if Present (HSS) then
+               Analyze (HSS);
+
+               if Present (Exception_Handlers (HSS)) then
+
+                  --  ???Has_Nested_Block_With_Handler needs to be set.
+                  --  Probably by creating an actual N_Block_Statement.
+                  --  Probably in Expand.
+
+                  null;
+               end if;
+            end if;
+
+            Check_References (Stm_Entity);
+         end;
+      end if;
+
+      --  Case of Expr present
+
+      if Present (Expr)
+
+         --  Defend against previous errors
+
+        and then Nkind (Expr) /= N_Empty
+        and then Present (Etype (Expr))
+      then
+         --  Apply constraint check. Note that this is done before the implicit
+         --  conversion of the expression done for anonymous access types to
+         --  ensure correct generation of the null-excluding check asssociated
+         --  with null-excluding expressions found in return statements.
+
+         Apply_Constraint_Check (Expr, R_Type);
+
+         --  Ada 2005 (AI-318-02): When the result type is an anonymous access
+         --  type, apply an implicit conversion of the expression to that type
+         --  to force appropriate static and run-time accessibility checks.
+
+         if Ada_Version >= Ada_05
+           and then Ekind (R_Type) = E_Anonymous_Access_Type
+         then
+            Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
+            Analyze_And_Resolve (Expr, R_Type);
+         end if;
+
+         --  If the result type is class-wide, then check that the return
+         --  expression's type is not declared at a deeper level than the
+         --  function (RM05-6.5(5.6/2)).
+
+         if Ada_Version >= Ada_05
+           and then Is_Class_Wide_Type (R_Type)
+         then
+            if Type_Access_Level (Etype (Expr)) >
+                 Subprogram_Access_Level (Scope_Id)
+            then
+               Error_Msg_N
+                 ("level of return expression type is deeper than " &
+                  "class-wide function!", Expr);
+            end if;
+         end if;
+
+         if (Is_Class_Wide_Type (Etype (Expr))
+              or else Is_Dynamically_Tagged (Expr))
+           and then not Is_Class_Wide_Type (R_Type)
+         then
+            Error_Msg_N
+              ("dynamically tagged expression not allowed!", Expr);
+         end if;
+
+         --  ??? A real run-time accessibility check is needed in cases
+         --  involving dereferences of access parameters. For now we just
+         --  check the static cases.
+
+         if (Ada_Version < Ada_05 or else Debug_Flag_Dot_L)
+           and then Is_Inherently_Limited_Type (Etype (Scope_Id))
+           and then Object_Access_Level (Expr) >
+                      Subprogram_Access_Level (Scope_Id)
+         then
+            Rewrite (N,
+              Make_Raise_Program_Error (Loc,
+                Reason => PE_Accessibility_Check_Failed));
+            Analyze (N);
+
+            Error_Msg_N
+              ("cannot return a local value by reference?", N);
+            Error_Msg_NE
+              ("\& will be raised at run time?",
+               N, Standard_Program_Error);
+         end if;
+
+         if Known_Null (Expr)
+           and then Nkind (Parent (Scope_Id)) = N_Function_Specification
+           and then Null_Exclusion_Present (Parent (Scope_Id))
+         then
+            Apply_Compile_Time_Constraint_Error
+              (N      => Expr,
+               Msg    => "(Ada 2005) null not allowed for "
+                         & "null-excluding return?",
+               Reason => CE_Null_Not_Allowed);
+         end if;
+      end if;
+   end Analyze_Function_Return;
+
    -------------------------------------
    -- Analyze_Generic_Subprogram_Body --
    -------------------------------------
@@ -268,9 +762,9 @@ package body Sem_Ch6 is
       Spec     : Node_Id;
 
    begin
-      --  Copy body and disable expansion while analyzing the generic
-      --  For a stub, do not copy the stub (which would load the proper body),
-      --  this will be done when the proper body is analyzed.
+      --  Copy body and disable expansion while analyzing the generic For a
+      --  stub, do not copy the stub (which would load the proper body), this
+      --  will be done when the proper body is analyzed.
 
       if Nkind (N) /= N_Subprogram_Body_Stub then
          New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
@@ -323,7 +817,7 @@ package body Sem_Ch6 is
       --  needed to process the formals declarations. Then make the formals
       --  visible in a separate step.
 
-      New_Scope (Gen_Id);
+      Push_Scope (Gen_Id);
 
       declare
          E         : Entity_Id;
@@ -359,17 +853,18 @@ package body Sem_Ch6 is
             end loop;
          end if;
 
-         --  Visible generic entity is callable within its own body.
+         --  Visible generic entity is callable within its own body
 
-         Set_Ekind (Gen_Id, Ekind (Body_Id));
-         Set_Ekind (Body_Id, E_Subprogram_Body);
-         Set_Convention (Body_Id, Convention (Gen_Id));
-         Set_Scope (Body_Id, Scope (Gen_Id));
+         Set_Ekind          (Gen_Id,  Ekind (Body_Id));
+         Set_Ekind          (Body_Id, E_Subprogram_Body);
+         Set_Convention     (Body_Id, Convention (Gen_Id));
+         Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
+         Set_Scope          (Body_Id, Scope (Gen_Id));
          Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
 
          if Nkind (N) = N_Subprogram_Body_Stub then
 
-            --  No body to analyze, so restore state of generic unit.
+            --  No body to analyze, so restore state of generic unit
 
             Set_Ekind (Gen_Id, Kind);
             Set_Ekind (Body_Id, Kind);
@@ -382,14 +877,18 @@ package body Sem_Ch6 is
             return;
          end if;
 
-         --  If this is a compilation unit, it must be made visible
-         --  explicitly, because the compilation of the declaration,
-         --  unlike other library unit declarations, does not. If it
-         --  is not a unit, the following is redundant but harmless.
+         --  If this is a compilation unit, it must be made visible explicitly,
+         --  because the compilation of the declaration, unlike other library
+         --  unit declarations, does not. If it is not a unit, the following
+         --  is redundant but harmless.
 
          Set_Is_Immediately_Visible (Gen_Id);
          Reference_Body_Formals (Gen_Id, Body_Id);
 
+         if Is_Child_Unit (Gen_Id) then
+            Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
+         end if;
+
          Set_Actual_Subtypes (N, Current_Scope);
          Analyze_Declarations (Declarations (N));
          Check_Completion;
@@ -397,8 +896,8 @@ package body Sem_Ch6 is
 
          Save_Global_References (Original_Node (N));
 
-         --  Prior to exiting the scope, include generic formals again
-         --  (if any are present) in the set of local entities.
+         --  Prior to exiting the scope, include generic formals again (if any
+         --  are present) in the set of local entities.
 
          if Present (First_Ent) then
             Set_First_Entity (Gen_Id, First_Ent);
@@ -407,14 +906,18 @@ package body Sem_Ch6 is
          Check_References (Gen_Id);
       end;
 
+      Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
       End_Scope;
       Check_Subprogram_Order (N);
 
-      --  Outside of its body, unit is generic again.
+      --  Outside of its body, unit is generic again
 
       Set_Ekind (Gen_Id, Kind);
       Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
-      Style.Check_Identifier (Body_Id, Gen_Id);
+
+      if Style_Check then
+         Style.Check_Identifier (Body_Id, Gen_Id);
+      end if;
       End_Generic;
    end Analyze_Generic_Subprogram_Body;
 
@@ -422,25 +925,27 @@ package body Sem_Ch6 is
    -- Analyze_Operator_Symbol --
    -----------------------------
 
-   --  An operator symbol such as "+" or "and" may appear in context where
-   --  the literal denotes an entity name, such as  "+"(x, y) or in a
-   --  context when it is just a string, as in  (conjunction = "or"). In
-   --  these cases the parser generates this node, and the semantics does
-   --  the disambiguation. Other such case are actuals in an instantiation,
-   --  the generic unit in an instantiation, and pragma arguments.
+   --  An operator symbol such as "+" or "and" may appear in context where the
+   --  literal denotes an entity name, such as "+"(x, y) or in context when it
+   --  is just a string, as in (conjunction = "or"). In these cases the parser
+   --  generates this node, and the semantics does the disambiguation. Other
+   --  such case are actuals in an instantiation, the generic unit in an
+   --  instantiation, and pragma arguments.
 
    procedure Analyze_Operator_Symbol (N : Node_Id) is
       Par : constant Node_Id := Parent (N);
 
    begin
-      if        (Nkind (Par) = N_Function_Call and then N = Name (Par))
+      if        (Nkind (Par) = N_Function_Call
+                   and then N = Name (Par))
         or else  Nkind (Par) = N_Function_Instantiation
-        or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
+        or else (Nkind (Par) = N_Indexed_Component
+                   and then N = Prefix (Par))
         or else (Nkind (Par) = N_Pragma_Argument_Association
                    and then not Is_Pragma_String_Literal (Par))
         or else  Nkind (Par) = N_Subprogram_Renaming_Declaration
-        or else  (Nkind (Par) = N_Attribute_Reference
-                   and then Attribute_Name (Par) /= Name_Value)
+        or else (Nkind (Par) = N_Attribute_Reference
+                  and then Attribute_Name (Par) /= Name_Value)
       then
          Find_Direct_Name (N);
 
@@ -501,6 +1006,14 @@ package body Sem_Ch6 is
 
       Analyze (P);
 
+      --  If this is a call of the form Obj.Op, the call may have been
+      --  analyzed and possibly rewritten into a block, in which case
+      --  we are done.
+
+      if Analyzed (N) then
+         return;
+      end if;
+
       --  If error analyzing prefix, then set Any_Type as result and return
 
       if Etype (P) = Any_Type then
@@ -563,9 +1076,9 @@ package body Sem_Ch6 is
         and then Present (Actuals)
         and then No (Next (First (Actuals)))
       then
-         --  Can be call to parameterless entry family. What appears to be
-         --  the sole argument is in fact the entry index. Rewrite prefix
-         --  of node accordingly. Source representation is unchanged by this
+         --  Can be call to parameterless entry family. What appears to be the
+         --  sole argument is in fact the entry index. Rewrite prefix of node
+         --  accordingly. Source representation is unchanged by this
          --  transformation.
 
          New_N :=
@@ -587,9 +1100,9 @@ package body Sem_Ch6 is
             Error_Msg_N ("expect access to procedure in call", P);
          end if;
 
-      --  The name can be a selected component or an indexed component
-      --  that yields an access to subprogram. Such a prefix is legal if
-      --  the call has parameter associations.
+      --  The name can be a selected component or an indexed component that
+      --  yields an access to subprogram. Such a prefix is legal if the call
+      --  has parameter associations.
 
       elsif Is_Access_Type (Etype (P))
         and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
@@ -600,15 +1113,14 @@ package body Sem_Ch6 is
             Error_Msg_N ("missing explicit dereference in call ", N);
          end if;
 
-      --  If not an access to subprogram, then the prefix must resolve to
-      --  the name of an entry, entry family, or protected operation.
+      --  If not an access to subprogram, then the prefix must resolve to the
+      --  name of an entry, entry family, or protected operation.
 
-      --  For the case of a simple entry call, P is a selected component
-      --  where the prefix is the task and the selector name is the entry.
-      --  A call to a protected procedure will have the same syntax. If
-      --  the protected object contains overloaded operations, the entity
-      --  may appear as a function, the context will select the operation
-      --  whose type is Void.
+      --  For the case of a simple entry call, P is a selected component where
+      --  the prefix is the task and the selector name is the entry. A call to
+      --  a protected procedure will have the same syntax. If the protected
+      --  object contains overloaded operations, the entity may appear as a
+      --  function, the context will select the operation whose type is Void.
 
       elsif Nkind (P) = N_Selected_Component
         and then (Ekind (Entity (Selector_Name (P))) = E_Entry
@@ -624,9 +1136,9 @@ package body Sem_Ch6 is
         and then Present (Actuals)
         and then No (Next (First (Actuals)))
       then
-         --  Can be call to parameterless entry family. What appears to be
-         --  the sole argument is in fact the entry index. Rewrite prefix
-         --  of node accordingly. Source representation is unchanged by this
+         --  Can be call to parameterless entry family. What appears to be the
+         --  sole argument is in fact the entry index. Rewrite prefix of node
+         --  accordingly. Source representation is unchanged by this
          --  transformation.
 
          New_N :=
@@ -663,110 +1175,76 @@ package body Sem_Ch6 is
          Analyze (P);
          Analyze_Call_And_Resolve;
 
-      --  Anything else is an error.
+      --  Anything else is an error
 
       else
-         Error_Msg_N ("Invalid procedure or entry call", N);
+         Error_Msg_N ("invalid procedure or entry call", N);
       end if;
    end Analyze_Procedure_Call;
 
-   ------------------------------
-   -- Analyze_Return_Statement --
-   ------------------------------
-
-   procedure Analyze_Return_Statement (N : Node_Id) is
-      Loc      : constant Source_Ptr := Sloc (N);
-      Expr     : Node_Id;
-      Scope_Id : Entity_Id;
-      Kind     : Entity_Kind;
-      R_Type   : Entity_Id;
+   -------------------------------------
+   -- Analyze_Simple_Return_Statement --
+   -------------------------------------
 
+   procedure Analyze_Simple_Return_Statement (N : Node_Id) is
    begin
-      --  Find subprogram or accept statement enclosing the return statement
-
-      Scope_Id := Empty;
-      for J in reverse 0 .. Scope_Stack.Last loop
-         Scope_Id := Scope_Stack.Table (J).Entity;
-         exit when Ekind (Scope_Id) /= E_Block and then
-                   Ekind (Scope_Id) /= E_Loop;
-      end loop;
+      if Present (Expression (N)) then
+         Mark_Coextensions (N, Expression (N));
+      end if;
 
-      pragma Assert (Present (Scope_Id));
+      Analyze_Return_Statement (N);
+   end Analyze_Simple_Return_Statement;
 
-      Kind := Ekind (Scope_Id);
-      Expr := Expression (N);
+   -------------------------
+   -- Analyze_Return_Type --
+   -------------------------
 
-      if Kind /= E_Function
-        and then Kind /= E_Generic_Function
-        and then Kind /= E_Procedure
-        and then Kind /= E_Generic_Procedure
-        and then Kind /= E_Entry
-        and then Kind /= E_Entry_Family
-      then
-         Error_Msg_N ("illegal context for return statement", N);
+   procedure Analyze_Return_Type (N : Node_Id) is
+      Designator : constant Entity_Id := Defining_Entity (N);
+      Typ        : Entity_Id := Empty;
 
-      elsif Present (Expr) then
-         if Kind = E_Function or else Kind = E_Generic_Function then
-            Set_Return_Present (Scope_Id);
-            R_Type := Etype (Scope_Id);
-            Set_Return_Type (N, R_Type);
-            Analyze_And_Resolve (Expr, R_Type);
+   begin
+      --  Normal case where result definition does not indicate an error
 
-            if (Is_Class_Wide_Type (Etype (Expr))
-                 or else Is_Dynamically_Tagged (Expr))
-              and then not Is_Class_Wide_Type (R_Type)
-            then
-               Error_Msg_N
-                 ("dynamically tagged expression not allowed!", Expr);
-            end if;
+      if Result_Definition (N) /= Error then
+         if Nkind (Result_Definition (N)) = N_Access_Definition then
+            Typ := Access_Definition (N, Result_Definition (N));
+            Set_Parent (Typ, Result_Definition (N));
+            Set_Is_Local_Anonymous_Access (Typ);
+            Set_Etype (Designator, Typ);
 
-            Apply_Constraint_Check (Expr, R_Type);
+         --  Subtype_Mark case
 
-            --  ??? A real run-time accessibility check is needed
-            --  in cases involving dereferences of access parameters.
-            --  For now we just check the static cases.
+         else
+            Find_Type (Result_Definition (N));
+            Typ := Entity (Result_Definition (N));
+            Set_Etype (Designator, Typ);
 
-            if Is_Return_By_Reference_Type (Etype (Scope_Id))
-              and then Object_Access_Level (Expr)
-                > Subprogram_Access_Level (Scope_Id)
+            if Ekind (Typ) = E_Incomplete_Type
+              and then Is_Value_Type (Typ)
             then
-               Rewrite (N,
-                 Make_Raise_Program_Error (Loc,
-                   Reason => PE_Accessibility_Check_Failed));
-               Analyze (N);
+               null;
 
+            elsif Ekind (Typ) = E_Incomplete_Type
+              or else (Is_Class_Wide_Type (Typ)
+                         and then
+                           Ekind (Root_Type (Typ)) = E_Incomplete_Type)
+            then
                Error_Msg_N
-                 ("cannot return a local value by reference?", N);
-               Error_Msg_NE
-                 ("& will be raised at run time?!",
-                  N, Standard_Program_Error);
+                 ("invalid use of incomplete type", Result_Definition (N));
             end if;
+         end if;
 
-         elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
-            Error_Msg_N ("procedure cannot return value (use function)", N);
+         --  Ada 2005 (AI-231): Ensure proper usage of null exclusion
 
-         else
-            Error_Msg_N ("accept statement cannot return value", N);
-         end if;
+         Null_Exclusion_Static_Checks (N);
 
-      --  No expression present
+      --  Case where result definition does indicate an error
 
       else
-         if Kind = E_Function or Kind = E_Generic_Function then
-            Error_Msg_N ("missing expression in return from function", N);
-         end if;
-
-         if (Ekind (Scope_Id) = E_Procedure
-              or else Ekind (Scope_Id) = E_Generic_Procedure)
-           and then No_Return (Scope_Id)
-         then
-            Error_Msg_N
-              ("RETURN statement not allowed (No_Return)", N);
-         end if;
+         Set_Etype (Designator, Any_Type);
       end if;
-
-      Check_Unreachable_Code (N);
-   end Analyze_Return_Statement;
+   end Analyze_Return_Type;
 
    -----------------------------
    -- Analyze_Subprogram_Body --
@@ -783,14 +1261,266 @@ package body Sem_Ch6 is
       Body_Id      : Entity_Id           := Defining_Entity (Body_Spec);
       Prev_Id      : constant Entity_Id  := Current_Entity_In_Scope (Body_Id);
       Body_Deleted : constant Boolean    := False;
-
-      HSS          : Node_Id;
-      Spec_Id      : Entity_Id;
-      Spec_Decl    : Node_Id   := Empty;
-      Last_Formal  : Entity_Id := Empty;
       Conformant   : Boolean;
+      HSS          : Node_Id;
       Missing_Ret  : Boolean;
       P_Ent        : Entity_Id;
+      Prot_Typ     : Entity_Id := Empty;
+      Spec_Id      : Entity_Id;
+      Spec_Decl    : Node_Id   := Empty;
+
+      Last_Real_Spec_Entity : Entity_Id := Empty;
+      --  When we analyze a separate spec, the entity chain ends up containing
+      --  the formals, as well as any itypes generated during analysis of the
+      --  default expressions for parameters, or the arguments of associated
+      --  precondition/postcondition pragmas (which are analyzed in the context
+      --  of the spec since they have visibility on formals).
+      --
+      --  These entities belong with the spec and not the body. However we do
+      --  the analysis of the body in the context of the spec (again to obtain
+      --  visibility to the formals), and all the entities generated during
+      --  this analysis end up also chained to the entity chain of the spec.
+      --  But they really belong to the body, and there is circuitry to move
+      --  them from the spec to the body.
+      --
+      --  However, when we do this move, we don't want to move the real spec
+      --  entities (first para above) to the body. The Last_Real_Spec_Entity
+      --  variable points to the last real spec entity, so we only move those
+      --  chained beyond that point. It is initialized to Empty to deal with
+      --  the case where there is no separate spec.
+
+      procedure Check_Anonymous_Return;
+      --  (Ada 2005): if a function returns an access type that denotes a task,
+      --  or a type that contains tasks, we must create a master entity for
+      --  the anonymous type, which typically will be used in an allocator
+      --  in the body of the function.
+
+      procedure Check_Inline_Pragma (Spec : in out Node_Id);
+      --  Look ahead to recognize a pragma that may appear after the body.
+      --  If there is a previous spec, check that it appears in the same
+      --  declarative part. If the pragma is Inline_Always, perform inlining
+      --  unconditionally, otherwise only if Front_End_Inlining is requested.
+      --  If the body acts as a spec, and inlining is required, we create a
+      --  subprogram declaration for it, in order to attach the body to inline.
+      --  If pragma does not appear after the body, check whether there is
+      --  an inline pragma before any local declarations.
+
+      procedure Set_Trivial_Subprogram (N : Node_Id);
+      --  Sets the Is_Trivial_Subprogram flag in both spec and body of the
+      --  subprogram whose body is being analyzed. N is the statement node
+      --  causing the flag to be set, if the following statement is a return
+      --  of an entity, we mark the entity as set in source to suppress any
+      --  warning on the stylized use of function stubs with a dummy return.
+
+      procedure Verify_Overriding_Indicator;
+      --  If there was a previous spec, the entity has been entered in the
+      --  current scope previously. If the body itself carries an overriding
+      --  indicator, check that it is consistent with the known status of the
+      --  entity.
+
+      ----------------------------
+      -- Check_Anonymous_Return --
+      ----------------------------
+
+      procedure Check_Anonymous_Return is
+         Decl : Node_Id;
+         Scop : Entity_Id;
+
+      begin
+         if Present (Spec_Id) then
+            Scop := Spec_Id;
+         else
+            Scop := Body_Id;
+         end if;
+
+         if Ekind (Scop) = E_Function
+           and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
+           and then Has_Task (Designated_Type (Etype (Scop)))
+           and then Expander_Active
+         then
+            Decl :=
+              Make_Object_Declaration (Loc,
+                Defining_Identifier =>
+                  Make_Defining_Identifier (Loc, Name_uMaster),
+                Constant_Present => True,
+                Object_Definition =>
+                  New_Reference_To (RTE (RE_Master_Id), Loc),
+                Expression =>
+                  Make_Explicit_Dereference (Loc,
+                    New_Reference_To (RTE (RE_Current_Master), Loc)));
+
+            if Present (Declarations (N)) then
+               Prepend (Decl, Declarations (N));
+            else
+               Set_Declarations (N, New_List (Decl));
+            end if;
+
+            Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
+            Set_Has_Master_Entity (Scop);
+         end if;
+      end Check_Anonymous_Return;
+
+      -------------------------
+      -- Check_Inline_Pragma --
+      -------------------------
+
+      procedure Check_Inline_Pragma (Spec : in out Node_Id) is
+         Prag  : Node_Id;
+         Plist : List_Id;
+
+         function Is_Inline_Pragma (N : Node_Id) return Boolean;
+         --  Simple predicate, used twice.
+
+         -----------------------
+         --  Is_Inline_Pragma --
+         -----------------------
+
+         function Is_Inline_Pragma (N : Node_Id) return Boolean is
+         begin
+            return
+              Nkind (N) = N_Pragma
+                and then
+                   (Pragma_Name (N) = Name_Inline_Always
+                     or else
+                      (Front_End_Inlining
+                        and then Pragma_Name (N) = Name_Inline))
+                and then
+                   Chars
+                     (Expression (First (Pragma_Argument_Associations (N))))
+                        = Chars (Body_Id);
+         end Is_Inline_Pragma;
+
+      --  Start of processing for Check_Inline_Pragma
+
+      begin
+         if not Expander_Active then
+            return;
+         end if;
+
+         if Is_List_Member (N)
+           and then Present (Next (N))
+           and then Is_Inline_Pragma (Next (N))
+         then
+            Prag := Next (N);
+
+         elsif Nkind (N) /= N_Subprogram_Body_Stub
+           and then Present (Declarations (N))
+           and then Is_Inline_Pragma (First (Declarations (N)))
+         then
+            Prag := First (Declarations (N));
+
+         else
+            Prag := Empty;
+         end if;
+
+         if Present (Prag) then
+            if Present (Spec_Id) then
+               if List_Containing (N) =
+                 List_Containing (Unit_Declaration_Node (Spec_Id))
+               then
+                  Analyze (Prag);
+               end if;
+
+            else
+               --  Create a subprogram declaration, to make treatment uniform
+
+               declare
+                  Subp : constant Entity_Id :=
+                    Make_Defining_Identifier (Loc, Chars (Body_Id));
+                  Decl : constant Node_Id :=
+                    Make_Subprogram_Declaration (Loc,
+                      Specification =>  New_Copy_Tree (Specification (N)));
+               begin
+                  Set_Defining_Unit_Name (Specification (Decl), Subp);
+
+                  if Present (First_Formal (Body_Id)) then
+                     Plist := Copy_Parameter_List (Body_Id);
+                     Set_Parameter_Specifications
+                       (Specification (Decl), Plist);
+                  end if;
+
+                  Insert_Before (N, Decl);
+                  Analyze (Decl);
+                  Analyze (Prag);
+                  Set_Has_Pragma_Inline (Subp);
+
+                  if Pragma_Name (Prag) = Name_Inline_Always then
+                     Set_Is_Inlined (Subp);
+                     Set_Has_Pragma_Inline_Always (Subp);
+                  end if;
+
+                  Spec := Subp;
+               end;
+            end if;
+         end if;
+      end Check_Inline_Pragma;
+
+      ----------------------------
+      -- Set_Trivial_Subprogram --
+      ----------------------------
+
+      procedure Set_Trivial_Subprogram (N : Node_Id) is
+         Nxt : constant Node_Id := Next (N);
+
+      begin
+         Set_Is_Trivial_Subprogram (Body_Id);
+
+         if Present (Spec_Id) then
+            Set_Is_Trivial_Subprogram (Spec_Id);
+         end if;
+
+         if Present (Nxt)
+           and then Nkind (Nxt) = N_Simple_Return_Statement
+           and then No (Next (Nxt))
+           and then Present (Expression (Nxt))
+           and then Is_Entity_Name (Expression (Nxt))
+         then
+            Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
+         end if;
+      end Set_Trivial_Subprogram;
+
+      ---------------------------------
+      -- Verify_Overriding_Indicator --
+      ---------------------------------
+
+      procedure Verify_Overriding_Indicator is
+      begin
+         if Must_Override (Body_Spec) then
+            if Nkind (Spec_Id) = N_Defining_Operator_Symbol
+              and then  Operator_Matches_Spec (Spec_Id, Spec_Id)
+            then
+               null;
+
+            elsif not Is_Overriding_Operation (Spec_Id) then
+               Error_Msg_NE
+                 ("subprogram& is not overriding", Body_Spec, Spec_Id);
+            end if;
+
+         elsif Must_Not_Override (Body_Spec) then
+            if Is_Overriding_Operation (Spec_Id) then
+               Error_Msg_NE
+                 ("subprogram& overrides inherited operation",
+                  Body_Spec, Spec_Id);
+
+            elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
+              and then  Operator_Matches_Spec (Spec_Id, Spec_Id)
+            then
+               Error_Msg_NE
+                 ("subprogram & overrides predefined operator ",
+                    Body_Spec, Spec_Id);
+
+            --  If this is not a primitive operation the overriding indicator
+            --  is altogether illegal.
+
+            elsif not Is_Primitive (Spec_Id) then
+               Error_Msg_N ("overriding indicator only allowed " &
+                "if subprogram is primitive",
+                Body_Spec);
+            end if;
+         end if;
+      end Verify_Overriding_Indicator;
+
+   --  Start of processing for Analyze_Subprogram_Body
 
    begin
       if Debug_Flag_C then
@@ -801,15 +1531,15 @@ package body Sem_Ch6 is
          Write_Eol;
       end if;
 
-      Trace_Scope (N, Body_Id, " Analyze subprogram");
+      Trace_Scope (N, Body_Id, " Analyze subprogram");
 
-      --  Generic subprograms are handled separately. They always have
-      --  a generic specification. Determine whether current scope has
-      --  previous declaration.
+      --  Generic subprograms are handled separately. They always have a
+      --  generic specification. Determine whether current scope has a
+      --  previous declaration.
 
-      --  If the subprogram body is defined within an instance of the
-      --  same name, the instance appears as a package renaming, and
-      --  will be hidden within the subprogram.
+      --  If the subprogram body is defined within an instance of the same
+      --  name, the instance appears as a package renaming, and will be hidden
+      --  within the subprogram.
 
       if Present (Prev_Id)
         and then not Is_Overloadable (Prev_Id)
@@ -825,18 +1555,18 @@ package body Sem_Ch6 is
             return;
 
          else
-            --  Previous entity conflicts with subprogram name.
-            --  Attempting to enter name will post error.
+            --  Previous entity conflicts with subprogram name. Attempting to
+            --  enter name will post error.
 
             Enter_Name (Body_Id);
             return;
          end if;
 
-      --  Non-generic case, find the subprogram declaration, if one was
-      --  seen, or enter new overloaded entity in the current scope.
-      --  If the current_entity is the body_id itself, the unit is being
-      --  analyzed as part of the context of one of its subunits. No need
-      --  to redo the analysis.
+      --  Non-generic case, find the subprogram declaration, if one was seen,
+      --  or enter new overloaded entity in the current scope. If the
+      --  Current_Entity is the Body_Id itself, the unit is being analyzed as
+      --  part of the context of one of its subunits. No need to redo the
+      --  analysis.
 
       elsif Prev_Id = Body_Id
         and then Has_Completion (Body_Id)
@@ -857,13 +1587,12 @@ package body Sem_Ch6 is
                return;
             end if;
 
-            --  A subprogram body should cause freezing of its own
-            --  declaration, but if there was no previous explicit
-            --  declaration, then the subprogram will get frozen too
-            --  late (there may be code within the body that depends
-            --  on the subprogram having been frozen, such as uses of
-            --  extra formals), so we force it to be frozen here.
-            --  Same holds if the body and the spec are compilation units.
+            --  A subprogram body should cause freezing of its own declaration,
+            --  but if there was no previous explicit declaration, then the
+            --  subprogram will get frozen too late (there may be code within
+            --  the body that depends on the subprogram having been frozen,
+            --  such as uses of extra formals), so we force it to be frozen
+            --  here. Same holds if the body and spec are compilation units.
 
             if No (Spec_Id) then
                Freeze_Before (N, Body_Id);
@@ -876,18 +1605,23 @@ package body Sem_Ch6 is
          end if;
       end if;
 
-      --  Do not inline any subprogram that contains nested subprograms,
-      --  since the backend inlining circuit seems to generate uninitialized
+      --  Do not inline any subprogram that contains nested subprograms, since
+      --  the backend inlining circuit seems to generate uninitialized
       --  references in this case. We know this happens in the case of front
-      --  end ZCX support, but it also appears it can happen in other cases
-      --  as well. The backend often rejects attempts to inline in the case
-      --  of nested procedures anyway, so little if anything is lost by this.
+      --  end ZCX support, but it also appears it can happen in other cases as
+      --  well. The backend often rejects attempts to inline in the case of
+      --  nested procedures anyway, so little if anything is lost by this.
+      --  Note that this is test is for the benefit of the back-end. There is
+      --  a separate test for front-end inlining that also rejects nested
+      --  subprograms.
 
       --  Do not do this test if errors have been detected, because in some
       --  error cases, this code blows up, and we don't need it anyway if
       --  there have been errors, since we won't get to the linker anyway.
 
-      if Serious_Errors_Detected = 0 then
+      if Comes_From_Source (Body_Id)
+        and then Serious_Errors_Detected = 0
+      then
          P_Ent := Body_Id;
          loop
             P_Ent := Scope (P_Ent);
@@ -907,6 +1641,8 @@ package body Sem_Ch6 is
          end loop;
       end if;
 
+      Check_Inline_Pragma (Spec_Id);
+
       --  Case of fully private operation in the body of the protected type.
       --  We must create a declaration for the subprogram, in order to attach
       --  the protected subprogram that will be used in internal calls.
@@ -924,36 +1660,18 @@ package body Sem_Ch6 is
          begin
             Formal := First_Formal (Body_Id);
 
-            --  The protected operation always has at least one formal,
-            --  namely the object itself, but it is only placed in the
-            --  parameter list if expansion is enabled.
+            --  The protected operation always has at least one formal, namely
+            --  the object itself, but it is only placed in the parameter list
+            --  if expansion is enabled.
 
             if Present (Formal)
               or else Expander_Active
             then
-               Plist := New_List;
-
+               Plist := Copy_Parameter_List (Body_Id);
             else
                Plist := No_List;
             end if;
 
-            while Present (Formal) loop
-               Append
-                 (Make_Parameter_Specification (Loc,
-                   Defining_Identifier =>
-                     Make_Defining_Identifier (Sloc (Formal),
-                       Chars => Chars (Formal)),
-                   In_Present  => In_Present (Parent (Formal)),
-                   Out_Present => Out_Present (Parent (Formal)),
-                   Parameter_Type =>
-                     New_Reference_To (Etype (Formal), Loc),
-                   Expression =>
-                     New_Copy_Tree (Expression (Parent (Formal)))),
-                 Plist);
-
-               Next_Formal (Formal);
-            end loop;
-
             if Nkind (Body_Spec) = N_Procedure_Specification then
                New_Spec :=
                  Make_Procedure_Specification (Loc,
@@ -968,21 +1686,52 @@ package body Sem_Ch6 is
                       Make_Defining_Identifier (Sloc (Body_Id),
                         Chars => Chars (Body_Id)),
                     Parameter_Specifications => Plist,
-                    Subtype_Mark => New_Occurrence_Of (Etype (Body_Id), Loc));
+                    Result_Definition =>
+                      New_Occurrence_Of (Etype (Body_Id), Loc));
             end if;
 
             Decl :=
               Make_Subprogram_Declaration (Loc,
                 Specification => New_Spec);
             Insert_Before (N, Decl);
-            Analyze (Decl);
             Spec_Id := Defining_Unit_Name (New_Spec);
+
+            --  Indicate that the entity comes from source, to ensure that
+            --  cross-reference information is properly generated. The body
+            --  itself is rewritten during expansion, and the body entity will
+            --  not appear in calls to the operation.
+
+            Set_Comes_From_Source (Spec_Id, True);
+            Analyze (Decl);
             Set_Has_Completion (Spec_Id);
             Set_Convention (Spec_Id, Convention_Protected);
          end;
 
       elsif Present (Spec_Id) then
          Spec_Decl := Unit_Declaration_Node (Spec_Id);
+         Verify_Overriding_Indicator;
+
+         --  In general, the spec will be frozen when we start analyzing the
+         --  body. However, for internally generated operations, such as
+         --  wrapper functions for inherited operations with controlling
+         --  results, the spec may not have been frozen by the time we
+         --  expand the freeze actions that include the bodies. In particular,
+         --  extra formals for accessibility or for return-in-place may need
+         --  to be generated. Freeze nodes, if any, are inserted before the
+         --  current body.
+
+         if not Is_Frozen (Spec_Id)
+           and then Expander_Active
+         then
+            --  Force the generation of its freezing node to ensure proper
+            --  management of access types in the backend.
+
+            --  This is definitely needed for some cases, but it is not clear
+            --  why, to be investigated further???
+
+            Set_Has_Delayed_Freeze (Spec_Id);
+            Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
+         end if;
       end if;
 
       --  Place subprogram on scope stack, and make formals visible. If there
@@ -990,6 +1739,11 @@ package body Sem_Ch6 is
 
       if Present (Spec_Id) then
          Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
+
+         if Is_Child_Unit (Spec_Id) then
+            Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
+         end if;
+
          if Style_Check then
             Style.Check_Identifier (Body_Id, Spec_Id);
          end if;
@@ -997,15 +1751,16 @@ package body Sem_Ch6 is
          Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
          Set_Is_Child_Unit       (Body_Id, Is_Child_Unit       (Spec_Id));
 
-         if Is_Abstract (Spec_Id) then
+         if Is_Abstract_Subprogram (Spec_Id) then
             Error_Msg_N ("an abstract subprogram cannot have a body", N);
             return;
+
          else
             Set_Convention (Body_Id, Convention (Spec_Id));
             Set_Has_Completion (Spec_Id);
 
             if Is_Protected_Type (Scope (Spec_Id)) then
-               Set_Privals_Chain (Spec_Id, New_Elmt_List);
+               Prot_Typ := Scope (Spec_Id);
             end if;
 
             --  If this is a body generated for a renaming, do not check for
@@ -1025,10 +1780,11 @@ package body Sem_Ch6 is
                                            N_Subprogram_Renaming_Declaration))
             then
                Conformant := True;
+
             else
                Check_Conformance
                  (Body_Id, Spec_Id,
-                   Fully_Conformant, True, Conformant, Body_Id);
+                  Fully_Conformant, True, Conformant, Body_Id);
             end if;
 
             --  If the body is not fully conformant, we have to decide if we
@@ -1054,9 +1810,51 @@ package body Sem_Ch6 is
 
          if Nkind (N) /= N_Subprogram_Body_Stub then
             Set_Corresponding_Spec (N, Spec_Id);
+
+            --  Ada 2005 (AI-345): If the operation is a primitive operation
+            --  of a concurrent type, the type of the first parameter has been
+            --  replaced with the corresponding record, which is the proper
+            --  run-time structure to use. However, within the body there may
+            --  be uses of the formals that depend on primitive operations
+            --  of the type (in particular calls in prefixed form) for which
+            --  we need the original concurrent type. The operation may have
+            --  several controlling formals, so the replacement must be done
+            --  for all of them.
+
+            if Comes_From_Source (Spec_Id)
+              and then Present (First_Entity (Spec_Id))
+              and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
+              and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
+              and then
+                Present (Abstract_Interfaces (Etype (First_Entity (Spec_Id))))
+              and then
+                Present
+                  (Corresponding_Concurrent_Type
+                     (Etype (First_Entity (Spec_Id))))
+            then
+               declare
+                  Typ  : constant Entity_Id := Etype (First_Entity (Spec_Id));
+                  Form : Entity_Id;
+
+               begin
+                  Form := First_Formal (Spec_Id);
+                  while Present (Form) loop
+                     if Etype (Form) = Typ then
+                        Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
+                     end if;
+
+                     Next_Formal (Form);
+                  end loop;
+               end;
+            end if;
+
+            --  Make the formals visible, and place subprogram on scope stack.
+            --  This is also the point at which we set Last_Real_Spec_Entity
+            --  to mark the entities which will not be moved to the body.
+
             Install_Formals (Spec_Id);
-            Last_Formal := Last_Entity (Spec_Id);
-            New_Scope (Spec_Id);
+            Last_Real_Spec_Entity := Last_Entity (Spec_Id);
+            Push_Scope (Spec_Id);
 
             --  Make sure that the subprogram is immediately visible. For
             --  child units that have no separate spec this is indispensable.
@@ -1068,6 +1866,7 @@ package body Sem_Ch6 is
          Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
          Set_Ekind (Body_Id, E_Subprogram_Body);
          Set_Scope (Body_Id, Scope (Spec_Id));
+         Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
 
       --  Case of subprogram body with no previous spec
 
@@ -1089,10 +1888,37 @@ package body Sem_Ch6 is
               (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
             Generate_Reference_To_Formals (Body_Id);
             Install_Formals (Body_Id);
-            New_Scope (Body_Id);
+            Push_Scope (Body_Id);
          end if;
       end if;
 
+      --  If the return type is an anonymous access type whose designated type
+      --  is the limited view of a class-wide type and the non-limited view is
+      --  available, update the return type accordingly.
+
+      if Ada_Version >= Ada_05
+        and then Comes_From_Source (N)
+      then
+         declare
+            Etyp : Entity_Id;
+            Rtyp : Entity_Id;
+
+         begin
+            Rtyp := Etype (Current_Scope);
+
+            if Ekind (Rtyp) = E_Anonymous_Access_Type then
+               Etyp := Directly_Designated_Type (Rtyp);
+
+               if Is_Class_Wide_Type (Etyp)
+                 and then From_With_Type (Etyp)
+               then
+                  Set_Directly_Designated_Type
+                    (Etype (Current_Scope), Available_View (Etyp));
+               end if;
+            end if;
+         end;
+      end if;
+
       --  If this is the proper body of a stub, we must verify that the stub
       --  conforms to the body, and to the previous spec if one was present.
       --  we know already that the body conforms to that spec. This test is
@@ -1101,6 +1927,8 @@ package body Sem_Ch6 is
       if Nkind (Parent (N)) = N_Subunit
         and then Comes_From_Source (N)
         and then not Error_Posted (Body_Id)
+        and then Nkind (Corresponding_Stub (Parent (N))) =
+                                                N_Subprogram_Body_Stub
       then
          declare
             Old_Id : constant Entity_Id :=
@@ -1134,29 +1962,97 @@ package body Sem_Ch6 is
       if Nkind (N) = N_Subprogram_Body_Stub then
          return;
 
-      elsif  Present (Spec_Id)
+      elsif Present (Spec_Id)
         and then Expander_Active
-        and then (Is_Always_Inlined (Spec_Id)
-                    or else (Has_Pragma_Inline (Spec_Id)
-                              and then
-                                (Front_End_Inlining
-                                  or else Configurable_Run_Time_Mode)))
+        and then
+          (Has_Pragma_Inline_Always (Spec_Id)
+             or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
       then
-         if Build_Body_To_Inline (N, Spec_Id, Copy_Separate_Tree (N)) then
-            null;
-         end if;
+         Build_Body_To_Inline (N, Spec_Id);
+      end if;
+
+      --  Ada 2005 (AI-262): In library subprogram bodies, after the analysis
+      --  if its specification we have to install the private withed units.
+      --  This holds for child units as well.
+
+      if Is_Compilation_Unit (Body_Id)
+        or else Nkind (Parent (N)) = N_Compilation_Unit
+      then
+         Install_Private_With_Clauses (Body_Id);
+      end if;
+
+      Check_Anonymous_Return;
+
+      --  Set the Protected_Formal field of each extra formal of the protected
+      --  subprogram to reference the corresponding extra formal of the
+      --  subprogram that implements it. For regular formals this occurs when
+      --  the protected subprogram's declaration is expanded, but the extra
+      --  formals don't get created until the subprogram is frozen. We need to
+      --  do this before analyzing the protected subprogram's body so that any
+      --  references to the original subprogram's extra formals will be changed
+      --  refer to the implementing subprogram's formals (see Expand_Formal).
+
+      if Present (Spec_Id)
+        and then Is_Protected_Type (Scope (Spec_Id))
+        and then Present (Protected_Body_Subprogram (Spec_Id))
+      then
+         declare
+            Impl_Subp       : constant Entity_Id :=
+                                Protected_Body_Subprogram (Spec_Id);
+            Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
+            Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
+         begin
+            while Present (Prot_Ext_Formal) loop
+               pragma Assert (Present (Impl_Ext_Formal));
+               Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
+               Next_Formal_With_Extras (Prot_Ext_Formal);
+               Next_Formal_With_Extras (Impl_Ext_Formal);
+            end loop;
+         end;
       end if;
 
       --  Now we can go on to analyze the body
 
       HSS := Handled_Statement_Sequence (N);
       Set_Actual_Subtypes (N, Current_Scope);
+
+      --  Deal with preconditions and postconditions
+
+      Process_PPCs (N, Spec_Id, Body_Id);
+
+      --  Add a declaration for the Protection objcect, renaming declarations
+      --  for discriminals and privals and finally a declaration for the entry
+      --  family index (if applicable). This form of early expansion is done
+      --  when the Expander is active because Install_Private_Data_Declarations
+      --  references entities which were created during regular expansion.
+
+      if Expander_Active
+        and then Comes_From_Source (N)
+        and then Present (Prot_Typ)
+        and then Present (Spec_Id)
+        and then not Is_Eliminated (Spec_Id)
+      then
+         Install_Private_Data_Declarations
+           (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
+      end if;
+
+      --  Analyze the declarations (this call will analyze the precondition
+      --  Check pragmas we prepended to the list, as well as the declaration
+      --  of the _Postconditions procedure).
+
       Analyze_Declarations (Declarations (N));
+
+      --  Check completion, and analyze the statements
+
       Check_Completion;
       Analyze (HSS);
+
+      --  Deal with end of scope processing for the body
+
       Process_End_Label (HSS, 't', Current_Scope);
       End_Scope;
       Check_Subprogram_Order (N);
+      Set_Analyzed (Body_Id);
 
       --  If we have a separate spec, then the analysis of the declarations
       --  caused the entities in the body to be chained to the spec id, but
@@ -1165,26 +2061,46 @@ package body Sem_Ch6 is
 
       if Present (Spec_Id) then
 
-         --  If a parent unit is categorized, the context of a subunit
-         --  must conform to the categorization. Conversely, if a child
-         --  unit is categorized, the parents themselves must conform.
+         --  We must conform to the categorization of our spec
 
-         if Nkind (Parent (N)) = N_Subunit then
-            Validate_Categorization_Dependency (N, Spec_Id);
+         Validate_Categorization_Dependency (N, Spec_Id);
 
-         elsif Is_Child_Unit (Spec_Id) then
+         --  And if this is a child unit, the parent units must conform
+
+         if Is_Child_Unit (Spec_Id) then
             Validate_Categorization_Dependency
               (Unit_Declaration_Node (Spec_Id), Spec_Id);
          end if;
 
-         if Present (Last_Formal) then
-            Set_Next_Entity
-              (Last_Entity (Body_Id), Next_Entity (Last_Formal));
-            Set_Next_Entity (Last_Formal, Empty);
+         --  Here is where we move entities from the spec to the body
+
+         --  Case where there are entities that stay with the spec
+
+         if Present (Last_Real_Spec_Entity) then
+
+            --  No body entities (happens when the only real spec entities
+            --  come from precondition and postcondition pragmas)
+
+            if No (Last_Entity (Body_Id)) then
+               Set_First_Entity
+                 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
+
+            --  Body entities present (formals), so chain stuff past them
+
+            else
+               Set_Next_Entity
+                 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
+            end if;
+
+            Set_Next_Entity (Last_Real_Spec_Entity, Empty);
             Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
-            Set_Last_Entity (Spec_Id, Last_Formal);
+            Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
+
+         --  Case where there are no spec entities, in this case there can
+         --  be no body entities either, so just move everything.
 
          else
+            pragma Assert (No (Last_Entity (Body_Id)));
             Set_First_Entity (Body_Id, First_Entity (Spec_Id));
             Set_Last_Entity  (Body_Id, Last_Entity (Spec_Id));
             Set_First_Entity (Spec_Id, Empty);
@@ -1225,14 +2141,19 @@ package body Sem_Ch6 is
         and then Present (Spec_Id)
         and then No_Return (Spec_Id)
       then
-         Check_Returns (HSS, 'P', Missing_Ret);
+         Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
       end if;
 
-      --  Now we are going to check for variables that are never modified
-      --  in the body of the procedure. We omit these checks if the first
-      --  statement of the procedure raises an exception. In particular
-      --  this deals with the common idiom of a stubbed function, which
-      --  might appear as something like
+      --  Now we are going to check for variables that are never modified in
+      --  the body of the procedure. But first we deal with a special case
+      --  where we want to modify this check. If the body of the subprogram
+      --  starts with a raise statement or its equivalent, or if the body
+      --  consists entirely of a null statement, then it is pretty obvious
+      --  that it is OK to not reference the parameters. For example, this
+      --  might be the following common idiom for a stubbed function:
+      --  statement of the procedure raises an exception. In particular this
+      --  deals with the common idiom of a stubbed function, which might
+      --  appear as something like
 
       --     function F (A : Integer) return Some_Type;
       --        X : Some_Type;
@@ -1241,21 +2162,32 @@ package body Sem_Ch6 is
       --        return X;
       --     end F;
 
-      --  Here the purpose of X is simply to satisfy the (annoying)
-      --  requirement in Ada that there be at least one return, and
-      --  we certainly do not want to go posting warnings on X that
-      --  it is not initialized!
+      --  Here the purpose of X is simply to satisfy the annoying requirement
+      --  in Ada that there be at least one return, and we certainly do not
+      --  want to go posting warnings on X that it is not initialized! On
+      --  the other hand, if X is entirely unreferenced that should still
+      --  get a warning.
+
+      --  What we do is to detect these cases, and if we find them, flag the
+      --  subprogram as being Is_Trivial_Subprogram and then use that flag to
+      --  suppress unwanted warnings. For the case of the function stub above
+      --  we have a special test to set X as apparently assigned to suppress
+      --  the warning.
 
       declare
-         Stm : Node_Id := First (Statements (HSS));
+         Stm : Node_Id;
 
       begin
-         --  Skip an initial label (for one thing this occurs when we
-         --  are in front end ZCX mode, but in any case it is irrelevant).
+         --  Skip initial labels (for one thing this occurs when we are in
+         --  front end ZCX mode, but in any case it is irrelevant), and also
+         --  initial Push_xxx_Error_Label nodes, which are also irrelevant.
 
-         if Nkind (Stm) = N_Label then
+         Stm := First (Statements (HSS));
+         while Nkind (Stm) = N_Label
+           or else Nkind (Stm) in N_Push_xxx_Label
+         loop
             Next (Stm);
-         end if;
+         end loop;
 
          --  Do the test on the original statement before expansion
 
@@ -1263,10 +2195,18 @@ package body Sem_Ch6 is
             Ostm : constant Node_Id := Original_Node (Stm);
 
          begin
-            --  If explicit raise statement, return with no checks
+            --  If explicit raise statement, turn on flag
 
             if Nkind (Ostm) = N_Raise_Statement then
-               return;
+               Set_Trivial_Subprogram (Stm);
+
+            --  If null statement, and no following statemennts, turn on flag
+
+            elsif Nkind (Stm) = N_Null_Statement
+              and then Comes_From_Source (Stm)
+              and then No (Next (Stm))
+            then
+               Set_Trivial_Subprogram (Stm);
 
             --  Check for explicit call cases which likely raise an exception
 
@@ -1278,22 +2218,23 @@ package body Sem_Ch6 is
                   begin
                      --  If the procedure is marked No_Return, then likely it
                      --  raises an exception, but in any case it is not coming
-                     --  back here, so no need to check beyond the call.
+                     --  back here, so turn on the flag.
 
                      if Ekind (Ent) = E_Procedure
                        and then No_Return (Ent)
                      then
-                        return;
+                        Set_Trivial_Subprogram (Stm);
 
                      --  If the procedure name is Raise_Exception, then also
                      --  assume that it raises an exception. The main target
                      --  here is Ada.Exceptions.Raise_Exception, but this name
                      --  is pretty evocative in any context! Note that the
                      --  procedure in Ada.Exceptions is not marked No_Return
-                     --  because of the annoying case of the null exception Id.
+                     --  because of the annoying case of the null exception Id
+                     --  when operating in Ada 95 mode.
 
                      elsif Chars (Ent) = Name_Raise_Exception then
-                        return;
+                        Set_Trivial_Subprogram (Stm);
                      end if;
                   end;
                end if;
@@ -1314,7 +2255,6 @@ package body Sem_Ch6 is
 
          if Present (Spec_Id) then
             E1 := First_Entity (Spec_Id);
-
             while Present (E1) loop
                if Ekind (E1) = E_Out_Parameter then
                   E2 := First_Entity (Body_Id);
@@ -1356,16 +2296,16 @@ package body Sem_Ch6 is
    begin
       Generate_Definition (Designator);
 
-      --  Check for RCI unit subprogram declarations against in-lined
+      --  Check for RCI unit subprogram declarations for illegal inlined
       --  subprograms and subprograms having access parameter or limited
-      --  parameter without Read and Write (RM E.2.3(12-13)).
+      --  parameter without Read and Write attributes (RM E.2.3(12-13)).
 
       Validate_RCI_Subprogram_Declaration (N);
 
       Trace_Scope
         (N,
          Defining_Entity (N),
-         " Analyze subprogram spec. ");
+         " Analyze subprogram spec: ");
 
       if Debug_Flag_C then
          Write_Str ("====  Compiling subprogram spec ");
@@ -1378,6 +2318,50 @@ package body Sem_Ch6 is
       New_Overloaded_Entity (Designator);
       Check_Delayed_Subprogram (Designator);
 
+      --  Ada 2005 (AI-251): Abstract interface primitives must be abstract
+      --  or null.
+
+      if Ada_Version >= Ada_05
+        and then Comes_From_Source (N)
+        and then Is_Dispatching_Operation (Designator)
+      then
+         declare
+            E    : Entity_Id;
+            Etyp : Entity_Id;
+
+         begin
+            if Has_Controlling_Result (Designator) then
+               Etyp := Etype (Designator);
+
+            else
+               E := First_Entity (Designator);
+               while Present (E)
+                 and then Is_Formal (E)
+                 and then not Is_Controlling_Formal (E)
+               loop
+                  Next_Entity (E);
+               end loop;
+
+               Etyp := Etype (E);
+            end if;
+
+            if Is_Access_Type (Etyp) then
+               Etyp := Directly_Designated_Type (Etyp);
+            end if;
+
+            if Is_Interface (Etyp)
+              and then not Is_Abstract_Subprogram (Designator)
+              and then not (Ekind (Designator) = E_Procedure
+                              and then Null_Present (Specification (N)))
+            then
+               Error_Msg_Name_1 := Chars (Defining_Entity (N));
+               Error_Msg_N
+                 ("(Ada 2005) interface subprogram % must be abstract or null",
+                  N);
+            end if;
+         end;
+      end if;
+
       --  What is the following code for, it used to be
 
       --  ???   Set_Suppress_Elaboration_Checks
@@ -1394,9 +2378,9 @@ package body Sem_Ch6 is
       then
          Set_Categorization_From_Scope (Designator, Scop);
       else
-         --  For a compilation unit, check for library-unit pragmas.
+         --  For a compilation unit, check for library-unit pragmas
 
-         New_Scope (Designator);
+         Push_Scope (Designator);
          Set_Categorization_From_Pragmas (N);
          Validate_Categorization_Dependency (N, Designator);
          Pop_Scope;
@@ -1407,17 +2391,33 @@ package body Sem_Ch6 is
 
       if Nkind (Parent (N)) = N_Compilation_Unit then
          Set_Body_Required (Parent (N), True);
+
+         if Ada_Version >= Ada_05
+           and then Nkind (Specification (N)) = N_Procedure_Specification
+           and then Null_Present (Specification (N))
+         then
+            Error_Msg_N
+              ("null procedure cannot be declared at library level", N);
+         end if;
       end if;
 
       Generate_Reference_To_Formals (Designator);
       Check_Eliminated (Designator);
 
-      if Comes_From_Source (N)
-        and then Is_List_Member (N)
+      --  Ada 2005: if procedure is declared with "is null" qualifier,
+      --  it requires no body.
+
+      if Nkind (Specification (N)) = N_Procedure_Specification
+        and then Null_Present (Specification (N))
       then
-         Check_Overriding_Operation (N, Designator);
-      end if;
+         Set_Has_Completion (Designator);
+         Set_Is_Inlined (Designator);
 
+         if Is_Protected_Type (Current_Scope) then
+            Error_Msg_N
+              ("protected operation cannot be a null procedure", N);
+         end if;
+      end if;
    end Analyze_Subprogram_Declaration;
 
    --------------------------------------
@@ -1431,7 +2431,10 @@ package body Sem_Ch6 is
    function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
       Designator : constant Entity_Id := Defining_Entity (N);
       Formals    : constant List_Id   := Parameter_Specifications (N);
-      Typ        : Entity_Id;
+      Formal     : Entity_Id;
+      Formal_Typ : Entity_Id;
+
+   --  Start of processing for Analyze_Subprogram_Specification
 
    begin
       Generate_Definition (Designator);
@@ -1440,34 +2443,47 @@ package body Sem_Ch6 is
          Set_Ekind (Designator, E_Function);
          Set_Mechanism (Designator, Default_Mechanism);
 
-         if Subtype_Mark (N) /= Error then
-            Find_Type (Subtype_Mark (N));
-            Typ := Entity (Subtype_Mark (N));
-            Set_Etype (Designator, Typ);
-
-            if Ekind (Typ) = E_Incomplete_Type
-              or else (Is_Class_Wide_Type (Typ)
-                         and then
-                           Ekind (Root_Type (Typ)) = E_Incomplete_Type)
-            then
-               Error_Msg_N
-                 ("invalid use of incomplete type", Subtype_Mark (N));
-            end if;
-
-         else
-            Set_Etype (Designator, Any_Type);
-         end if;
-
       else
          Set_Ekind (Designator, E_Procedure);
          Set_Etype (Designator, Standard_Void_Type);
       end if;
 
+      --  Introduce new scope for analysis of the formals and the return type
+
+      Set_Scope (Designator, Current_Scope);
+
       if Present (Formals) then
-         Set_Scope (Designator, Current_Scope);
-         New_Scope (Designator);
+         Push_Scope (Designator);
          Process_Formals (Formals, N);
+
+         --  Ada 2005 (AI-345): Allow the overriding of interface primitives
+         --  by subprograms which belong to a concurrent type implementing an
+         --  interface. Set the parameter type of each controlling formal to
+         --  the corresponding record type.
+
+         if Ada_Version >= Ada_05 then
+            Formal := First_Formal (Designator);
+            while Present (Formal) loop
+               Formal_Typ := Etype (Formal);
+
+               if (Ekind (Formal_Typ) = E_Protected_Type
+                     or else Ekind (Formal_Typ) = E_Task_Type)
+                 and then Present (Corresponding_Record_Type (Formal_Typ))
+                 and then Present (Abstract_Interfaces
+                                  (Corresponding_Record_Type (Formal_Typ)))
+               then
+                  Set_Etype (Formal,
+                    Corresponding_Record_Type (Formal_Typ));
+               end if;
+
+               Formal := Next_Formal (Formal);
+            end loop;
+         end if;
+
          End_Scope;
+
+      elsif Nkind (N) = N_Function_Specification then
+         Analyze_Return_Type (N);
       end if;
 
       if Nkind (N) = N_Function_Specification then
@@ -1477,8 +2493,21 @@ package body Sem_Ch6 is
 
          May_Need_Actuals (Designator);
 
-         if Is_Abstract (Etype (Designator))
-           and then Nkind (Parent (N)) /= N_Abstract_Subprogram_Declaration
+         --  Ada 2005 (AI-251): In case of primitives associated with abstract
+         --  interface types the following error message will be reported later
+         --  (see Analyze_Subprogram_Declaration).
+
+         if Is_Abstract_Type (Etype (Designator))
+           and then not Is_Interface (Etype (Designator))
+           and then Nkind (Parent (N)) /=
+                      N_Abstract_Subprogram_Declaration
+           and then
+             (Nkind (Parent (N))) /= N_Formal_Abstract_Subprogram_Declaration
+                and then
+                  (Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
+                     or else not Is_Entity_Name (Name (Parent (N)))
+                     or else not Is_Abstract_Subprogram
+                                    (Entity (Name (Parent (N)))))
          then
             Error_Msg_N
               ("function that returns abstract type must be abstract", N);
@@ -1492,38 +2521,45 @@ package body Sem_Ch6 is
    -- Build_Body_To_Inline --
    --------------------------
 
-   function Build_Body_To_Inline
-     (N         : Node_Id;
-      Subp      : Entity_Id;
-      Orig_Body : Node_Id)
-      return      Boolean
-   is
-      Decl : constant Node_Id := Unit_Declaration_Node (Subp);
+   procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
+      Decl            : constant Node_Id := Unit_Declaration_Node (Subp);
       Original_Body   : Node_Id;
       Body_To_Analyze : Node_Id;
       Max_Size        : constant := 10;
       Stat_Count      : Integer := 0;
 
       function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
-      --  Check for declarations that make inlining not worthwhile.
+      --  Check for declarations that make inlining not worthwhile
 
       function Has_Excluded_Statement   (Stats : List_Id) return Boolean;
-      --  Check for statements that make inlining not worthwhile: any
-      --  tasking statement, nested at any level. Keep track of total
-      --  number of elementary statements, as a measure of acceptable size.
+      --  Check for statements that make inlining not worthwhile: any tasking
+      --  statement, nested at any level. Keep track of total number of
+      --  elementary statements, as a measure of acceptable size.
 
       function Has_Pending_Instantiation return Boolean;
-      --  If some enclosing body contains instantiations that appear before
-      --  the corresponding generic body, the enclosing body has a freeze node
-      --  so that it can be elaborated after the generic itself. This might
+      --  If some enclosing body contains instantiations that appear before the
+      --  corresponding generic body, the enclosing body has a freeze node so
+      --  that it can be elaborated after the generic itself. This might
       --  conflict with subsequent inlinings, so that it is unsafe to try to
       --  inline in such a case.
 
+      function Has_Single_Return return Boolean;
+      --  In general we cannot inline functions that return unconstrained type.
+      --  However, we can handle such functions if all return statements return
+      --  a local variable that is the only declaration in the body of the
+      --  function. In that case the call can be replaced by that local
+      --  variable as is done for other inlined calls.
+
       procedure Remove_Pragmas;
-      --  A pragma Unreferenced that mentions a formal parameter has no
-      --  meaning when the body is inlined and the formals are rewritten.
-      --  Remove it from body to inline. The analysis of the non-inlined
-      --  body will handle the pragma properly.
+      --  A pragma Unreferenced or pragma Unmodified that mentions a formal
+      --  parameter has no meaning when the body is inlined and the formals
+      --  are rewritten. Remove it from body to inline. The analysis of the
+      --  non-inlined body will handle the pragma properly.
+
+      function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
+      --  If the body of the subprogram includes a call that returns an
+      --  unconstrained type, the secondary stack is involved, and it
+      --  is not worth inlining.
 
       ------------------------------
       -- Has_Excluded_Declaration --
@@ -1533,14 +2569,17 @@ package body Sem_Ch6 is
          D : Node_Id;
 
          function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
-         --  Nested subprograms make a given body ineligible for inlining,
-         --  but we make an exception for instantiations of unchecked
-         --  conversion. The body has not been analyzed yet, so we check
-         --  the name, and verify that the visible entity with that name is
-         --  the predefined unit.
+         --  Nested subprograms make a given body ineligible for inlining, but
+         --  we make an exception for instantiations of unchecked conversion.
+         --  The body has not been analyzed yet, so check the name, and verify
+         --  that the visible entity with that name is the predefined unit.
+
+         -----------------------------
+         -- Is_Unchecked_Conversion --
+         -----------------------------
 
          function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
-            Id : constant Node_Id := Name (D);
+            Id   : constant Node_Id := Name (D);
             Conv : Entity_Id;
 
          begin
@@ -1549,18 +2588,17 @@ package body Sem_Ch6 is
             then
                Conv := Current_Entity (Id);
 
-            elsif Nkind (Id) = N_Selected_Component
+            elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
               and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
             then
                Conv := Current_Entity (Selector_Name (Id));
-
             else
                return False;
             end if;
 
-            return
-              Present (Conv)
-              and then Scope (Conv) = Standard_Standard
+            return Present (Conv)
+              and then Is_Predefined_File_Name
+                         (Unit_File_Name (Get_Source_Unit (Conv)))
               and then Is_Intrinsic_Subprogram (Conv);
          end Is_Unchecked_Conversion;
 
@@ -1568,16 +2606,15 @@ package body Sem_Ch6 is
 
       begin
          D := First (Decls);
-
          while Present (D) loop
-            if       (Nkind (D) = N_Function_Instantiation
-                        and then not Is_Unchecked_Conversion (D))
-              or else Nkind (D) = N_Protected_Type_Declaration
-              or else Nkind (D) = N_Package_Declaration
-              or else Nkind (D) = N_Package_Instantiation
-              or else Nkind (D) = N_Subprogram_Body
-              or else Nkind (D) = N_Procedure_Instantiation
-              or else Nkind (D) = N_Task_Type_Declaration
+            if (Nkind (D) = N_Function_Instantiation
+                  and then not Is_Unchecked_Conversion (D))
+              or else Nkind_In (D, N_Protected_Type_Declaration,
+                                   N_Package_Declaration,
+                                   N_Package_Instantiation,
+                                   N_Subprogram_Body,
+                                   N_Procedure_Instantiation,
+                                   N_Task_Type_Declaration)
             then
                Cannot_Inline
                  ("cannot inline & (non-allowed declaration)?", D, Subp);
@@ -1600,17 +2637,16 @@ package body Sem_Ch6 is
 
       begin
          S := First (Stats);
-
          while Present (S) loop
             Stat_Count := Stat_Count + 1;
 
-            if Nkind (S) = N_Abort_Statement
-              or else Nkind (S) = N_Asynchronous_Select
-              or else Nkind (S) = N_Conditional_Entry_Call
-              or else Nkind (S) = N_Delay_Relative_Statement
-              or else Nkind (S) = N_Delay_Until_Statement
-              or else Nkind (S) = N_Selective_Accept
-              or else Nkind (S) = N_Timed_Entry_Call
+            if Nkind_In (S, N_Abort_Statement,
+                            N_Asynchronous_Select,
+                            N_Conditional_Entry_Call,
+                            N_Delay_Relative_Statement,
+                            N_Delay_Until_Statement,
+                            N_Selective_Accept,
+                            N_Timed_Entry_Call)
             then
                Cannot_Inline
                  ("cannot inline & (non-allowed statement)?", S, Subp);
@@ -1635,7 +2671,6 @@ package body Sem_Ch6 is
 
             elsif Nkind (S) = N_Case_Statement then
                E := First (Alternatives (S));
-
                while Present (E) loop
                   if Has_Excluded_Statement (Statements (E)) then
                      return True;
@@ -1651,7 +2686,6 @@ package body Sem_Ch6 is
 
                if Present (Elsif_Parts (S)) then
                   E := First (Elsif_Parts (S));
-
                   while Present (E) loop
                      if Has_Excluded_Statement (Then_Statements (E)) then
                         return True;
@@ -1683,9 +2717,10 @@ package body Sem_Ch6 is
       -------------------------------
 
       function Has_Pending_Instantiation return Boolean is
-         S : Entity_Id := Current_Scope;
+         S : Entity_Id;
 
       begin
+         S := Current_Scope;
          while Present (S) loop
             if Is_Compilation_Unit (S)
               or else Is_Child_Unit (S)
@@ -1703,6 +2738,61 @@ package body Sem_Ch6 is
          return False;
       end Has_Pending_Instantiation;
 
+      ------------------------
+      --  Has_Single_Return --
+      ------------------------
+
+      function Has_Single_Return return Boolean is
+         Return_Statement : Node_Id := Empty;
+
+         function Check_Return (N : Node_Id) return Traverse_Result;
+
+         ------------------
+         -- Check_Return --
+         ------------------
+
+         function Check_Return (N : Node_Id) return Traverse_Result is
+         begin
+            if Nkind (N) = N_Simple_Return_Statement then
+               if Present (Expression (N))
+                 and then Is_Entity_Name (Expression (N))
+               then
+                  if No (Return_Statement) then
+                     Return_Statement := N;
+                     return OK;
+
+                  elsif Chars (Expression (N)) =
+                        Chars (Expression (Return_Statement))
+                  then
+                     return OK;
+
+                  else
+                     return Abandon;
+                  end if;
+
+               else
+                  --  Expression has wrong form
+
+                  return Abandon;
+               end if;
+
+            else
+               return OK;
+            end if;
+         end Check_Return;
+
+         function Check_All_Returns is new Traverse_Func (Check_Return);
+
+      --  Start of processing for Has_Single_Return
+
+      begin
+         return Check_All_Returns (N) = OK
+           and then Present (Declarations (N))
+           and then Present (First (Declarations (N)))
+           and then Chars (Expression (Return_Statement)) =
+                    Chars (Defining_Identifier (First (Declarations (N))));
+      end Has_Single_Return;
+
       --------------------
       -- Remove_Pragmas --
       --------------------
@@ -1717,7 +2807,9 @@ package body Sem_Ch6 is
             Nxt := Next (Decl);
 
             if Nkind (Decl) = N_Pragma
-              and then Chars (Decl) = Name_Unreferenced
+              and then (Pragma_Name (Decl) = Name_Unreferenced
+                          or else
+                        Pragma_Name (Decl) = Name_Unmodified)
             then
                Remove (Decl);
             end if;
@@ -1726,82 +2818,79 @@ package body Sem_Ch6 is
          end loop;
       end Remove_Pragmas;
 
+      --------------------------
+      -- Uses_Secondary_Stack --
+      --------------------------
+
+      function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
+         function Check_Call (N : Node_Id) return Traverse_Result;
+         --  Look for function calls that return an unconstrained type
+
+         ----------------
+         -- Check_Call --
+         ----------------
+
+         function Check_Call (N : Node_Id) return Traverse_Result is
+         begin
+            if Nkind (N) = N_Function_Call
+              and then Is_Entity_Name (Name (N))
+              and then Is_Composite_Type (Etype (Entity (Name (N))))
+              and then not Is_Constrained (Etype (Entity (Name (N))))
+            then
+               Cannot_Inline
+                 ("cannot inline & (call returns unconstrained type)?",
+                    N, Subp);
+               return Abandon;
+            else
+               return OK;
+            end if;
+         end Check_Call;
+
+         function Check_Calls is new Traverse_Func (Check_Call);
+
+      begin
+         return Check_Calls (Bod) = Abandon;
+      end Uses_Secondary_Stack;
+
    --  Start of processing for Build_Body_To_Inline
 
    begin
       if Nkind (Decl) = N_Subprogram_Declaration
         and then Present (Body_To_Inline (Decl))
       then
-         return True;    --  Done already.
+         return;    --  Done already.
 
-      --  Functions that return unconstrained composite types will require
-      --  secondary stack handling, and cannot currently be inlined.
+      --  Functions that return unconstrained composite types require
+      --  secondary stack handling, and cannot currently be inlined, unless
+      --  all return statements return a local variable that is the first
+      --  local declaration in the body.
 
       elsif Ekind (Subp) = E_Function
         and then not Is_Scalar_Type (Etype (Subp))
         and then not Is_Access_Type (Etype (Subp))
         and then not Is_Constrained (Etype (Subp))
       then
-         Cannot_Inline
-           ("cannot inline & (unconstrained return type)?", N, Subp);
-         return False;
-      end if;
-
-      --  We need to capture references to the formals in order to substitute
-      --  the actuals at the point of inlining, i.e. instantiation. To treat
-      --  the formals as globals to the body to inline, we nest it within
-      --  a dummy parameterless subprogram, declared within the real one.
-
-      Original_Body := Orig_Body;
-
-      --  Within an instance, the current tree is already the result of
-      --  a generic copy, and not what we need for subsequent inlining.
-      --  We create the required body by doing an instantiating copy, to
-      --  obtain the proper partially analyzed tree.
-
-      if In_Instance then
-         if No (Generic_Parent (Specification (N))) then
-            return False;
-
-         elsif Is_Child_Unit (Scope (Current_Scope)) then
-            return False;
-
-         elsif Scope (Current_Scope) = Cunit_Entity (Main_Unit) then
-
-            --  compiling an instantiation. There is no point in generating
-            --  bodies to inline, because they will not be used.
-
-            return False;
-
-         else
-            Body_To_Analyze :=
-              Copy_Generic_Node
-                (Generic_Parent (Specification (N)), Empty,
-                   Instantiating => True);
+         if not Has_Single_Return then
+            Cannot_Inline
+              ("cannot inline & (unconstrained return type)?", N, Subp);
+            return;
          end if;
 
-      --  Case of not in an instance
-
-      else
-         Body_To_Analyze :=
-           Copy_Generic_Node (Original_Body, Empty,
-             Instantiating => False);
-      end if;
-
-      Set_Parameter_Specifications (Specification (Original_Body), No_List);
-      Set_Defining_Unit_Name (Specification (Original_Body),
-        Make_Defining_Identifier (Sloc (N), New_Internal_Name ('S')));
-      Set_Corresponding_Spec (Original_Body, Empty);
+      --  Ditto for functions that return controlled types, where controlled
+      --  actions interfere in complex ways with inlining.
 
-      if Ekind (Subp) = E_Function then
-         Set_Subtype_Mark (Specification (Original_Body),
-           New_Occurrence_Of (Etype (Subp), Sloc (N)));
+      elsif Ekind (Subp) = E_Function
+        and then Controlled_Type (Etype (Subp))
+      then
+         Cannot_Inline
+           ("cannot inline & (controlled return type)?", N, Subp);
+         return;
       end if;
 
-      if Present (Declarations (Orig_Body))
-        and then Has_Excluded_Declaration (Declarations (Orig_Body))
+      if Present (Declarations (N))
+        and then Has_Excluded_Declaration (Declarations (N))
       then
-         return False;
+         return;
       end if;
 
       if Present (Handled_Statement_Sequence (N)) then
@@ -1810,12 +2899,12 @@ package body Sem_Ch6 is
               ("cannot inline& (exception handler)?",
                First (Exception_Handlers (Handled_Statement_Sequence (N))),
                Subp);
-            return False;
+            return;
          elsif
            Has_Excluded_Statement
              (Statements (Handled_Statement_Sequence (N)))
          then
-            return False;
+            return;
          end if;
       end if;
 
@@ -1824,26 +2913,54 @@ package body Sem_Ch6 is
       --  checks on inlining (forbidden declarations, handlers, etc).
 
       if Stat_Count > Max_Size
-        and then not Is_Always_Inlined (Subp)
+        and then not Has_Pragma_Inline_Always (Subp)
       then
          Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
-         return False;
+         return;
       end if;
 
       if Has_Pending_Instantiation then
          Cannot_Inline
            ("cannot inline& (forward instance within enclosing body)?",
              N, Subp);
-         return False;
+         return;
       end if;
 
+      --  Within an instance, the body to inline must be treated as a nested
+      --  generic, so that the proper global references are preserved.
+
+      --  Note that we do not do this at the library level, because it is not
+      --  needed, and furthermore this causes trouble if front end inlining
+      --  is activated (-gnatN).
+
+      if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
+         Save_Env (Scope (Current_Scope), Scope (Current_Scope));
+         Original_Body := Copy_Generic_Node (N, Empty, True);
+      else
+         Original_Body := Copy_Separate_Tree (N);
+      end if;
+
+      --  We need to capture references to the formals in order to substitute
+      --  the actuals at the point of inlining, i.e. instantiation. To treat
+      --  the formals as globals to the body to inline, we nest it within
+      --  a dummy parameterless subprogram, declared within the real one.
+      --  To avoid generating an internal name (which is never public, and
+      --  which affects serial numbers of other generated names), we use
+      --  an internal symbol that cannot conflict with user declarations.
+
+      Set_Parameter_Specifications (Specification (Original_Body), No_List);
+      Set_Defining_Unit_Name
+        (Specification (Original_Body),
+          Make_Defining_Identifier (Sloc (N), Name_uParent));
+      Set_Corresponding_Spec (Original_Body, Empty);
+
       Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
 
       --  Set return type of function, which is also global and does not need
       --  to be resolved.
 
       if Ekind (Subp) = E_Function then
-         Set_Subtype_Mark (Specification (Body_To_Analyze),
+         Set_Result_Definition (Specification (Body_To_Analyze),
            New_Occurrence_Of (Etype (Subp), Sloc (N)));
       end if;
 
@@ -1857,16 +2974,29 @@ package body Sem_Ch6 is
       Remove_Pragmas;
 
       Analyze (Body_To_Analyze);
-      New_Scope (Defining_Entity (Body_To_Analyze));
+      Push_Scope (Defining_Entity (Body_To_Analyze));
       Save_Global_References (Original_Body);
       End_Scope;
       Remove (Body_To_Analyze);
 
       Expander_Mode_Restore;
+
+      --  Restore environment if previously saved
+
+      if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
+         Restore_Env;
+      end if;
+
+      --  If secondary stk used there is no point in inlining. We have
+      --  already issued the warning in this case, so nothing to do.
+
+      if Uses_Secondary_Stack (Body_To_Analyze) then
+         return;
+      end if;
+
       Set_Body_To_Inline (Decl, Original_Body);
       Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
       Set_Is_Inlined (Subp);
-      return True;
    end Build_Body_To_Inline;
 
    -------------------
@@ -1885,8 +3015,12 @@ package body Sem_Ch6 is
       then
          null;
 
-      elsif Is_Always_Inlined (Subp) then
-         Error_Msg_NE (Msg (1 .. Msg'Length - 1), N, Subp);
+      elsif Has_Pragma_Inline_Always (Subp) then
+
+         --  Remove last character (question mark) to make this into an error,
+         --  because the Inline_Always pragma cannot be obeyed.
+
+         Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
 
       elsif Ineffective_Inline_Warnings then
          Error_Msg_NE (Msg, N, Subp);
@@ -1898,27 +3032,22 @@ package body Sem_Ch6 is
    -----------------------
 
    procedure Check_Conformance
-     (New_Id   : Entity_Id;
-      Old_Id   : Entity_Id;
-      Ctype    : Conformance_Type;
-      Errmsg   : Boolean;
-      Conforms : out Boolean;
-      Err_Loc  : Node_Id := Empty;
-      Get_Inst : Boolean := False)
+     (New_Id                   : Entity_Id;
+      Old_Id                   : Entity_Id;
+      Ctype                    : Conformance_Type;
+      Errmsg                   : Boolean;
+      Conforms                 : out Boolean;
+      Err_Loc                  : Node_Id := Empty;
+      Get_Inst                 : Boolean := False;
+      Skip_Controlling_Formals : Boolean := False)
    is
-      Old_Type   : constant Entity_Id := Etype (Old_Id);
-      New_Type   : constant Entity_Id := Etype (New_Id);
-      Old_Formal : Entity_Id;
-      New_Formal : Entity_Id;
-
       procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
-      --  Post error message for conformance error on given node.
-      --  Two messages are output. The first points to the previous
-      --  declaration with a general "no conformance" message.
-      --  The second is the detailed reason, supplied as Msg. The
-      --  parameter N provide information for a possible & insertion
-      --  in the message, and also provides the location for posting
-      --  the message in the absence of a specified Err_Loc location.
+      --  Post error message for conformance error on given node. Two messages
+      --  are output. The first points to the previous declaration with a
+      --  general "no conformance" message. The second is the detailed reason,
+      --  supplied as Msg. The parameter N provide information for a possible
+      --  & insertion in the message, and also provides the location for
+      --  posting the message in the absence of a specified Err_Loc location.
 
       -----------------------
       -- Conformance_Error --
@@ -1945,29 +3074,57 @@ package body Sem_Ch6 is
                     ("not type conformant with declaration#!", Enode);
 
                when Mode_Conformant =>
-                  Error_Msg_N
-                    ("not mode conformant with declaration#!", Enode);
+                  if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
+                     Error_Msg_N
+                       ("not mode conformant with operation inherited#!",
+                         Enode);
+                  else
+                     Error_Msg_N
+                       ("not mode conformant with declaration#!", Enode);
+                  end if;
 
                when Subtype_Conformant =>
-                  Error_Msg_N
-                    ("not subtype conformant with declaration#!", Enode);
+                  if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
+                     Error_Msg_N
+                       ("not subtype conformant with operation inherited#!",
+                         Enode);
+                  else
+                     Error_Msg_N
+                       ("not subtype conformant with declaration#!", Enode);
+                  end if;
 
                when Fully_Conformant =>
-                  Error_Msg_N
-                    ("not fully conformant with declaration#!", Enode);
+                  if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
+                     Error_Msg_N
+                       ("not fully conformant with operation inherited#!",
+                         Enode);
+                  else
+                     Error_Msg_N
+                       ("not fully conformant with declaration#!", Enode);
+                  end if;
             end case;
 
             Error_Msg_NE (Msg, Enode, N);
          end if;
       end Conformance_Error;
 
+      --  Local Variables
+
+      Old_Type           : constant Entity_Id := Etype (Old_Id);
+      New_Type           : constant Entity_Id := Etype (New_Id);
+      Old_Formal         : Entity_Id;
+      New_Formal         : Entity_Id;
+      Access_Types_Match : Boolean;
+      Old_Formal_Base    : Entity_Id;
+      New_Formal_Base    : Entity_Id;
+
    --  Start of processing for Check_Conformance
 
    begin
       Conforms := True;
 
-      --  We need a special case for operators, since they don't
-      --  appear explicitly.
+      --  We need a special case for operators, since they don't appear
+      --  explicitly.
 
       if Ctype = Type_Conformant then
          if Ekind (New_Id) = E_Operator
@@ -1983,7 +3140,22 @@ package body Sem_Ch6 is
         and then New_Type /= Standard_Void_Type
       then
          if not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
-            Conformance_Error ("return type does not match!", New_Id);
+            Conformance_Error ("\return type does not match!", New_Id);
+            return;
+         end if;
+
+         --  Ada 2005 (AI-231): In case of anonymous access types check the
+         --  null-exclusion and access-to-constant attributes match.
+
+         if Ada_Version >= Ada_05
+           and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
+           and then
+             (Can_Never_Be_Null (Old_Type)
+                /= Can_Never_Be_Null (New_Type)
+              or else Is_Access_Constant (Etype (Old_Type))
+                        /= Is_Access_Constant (Etype (New_Type)))
+         then
+            Conformance_Error ("\return type does not match!", New_Id);
             return;
          end if;
 
@@ -1992,11 +3164,11 @@ package body Sem_Ch6 is
       elsif Old_Type /= Standard_Void_Type
         or else New_Type /= Standard_Void_Type
       then
-         Conformance_Error ("functions can only match functions!", New_Id);
+         Conformance_Error ("\functions can only match functions!", New_Id);
          return;
       end if;
 
-      --  In subtype conformant case, conventions must match (RM 6.3.1(16))
+      --  In subtype conformant case, conventions must match (RM 6.3.1(16)).
       --  If this is a renaming as body, refine error message to indicate that
       --  the conflict is with the original declaration. If the entity is not
       --  frozen, the conventions don't have to match, the one of the renamed
@@ -2016,10 +3188,10 @@ package body Sem_Ch6 is
                Error_Msg_Name_2 :=
                  Name_Ada + Convention_Id'Pos (Convention (New_Id));
 
-               Conformance_Error ("prior declaration for% has convention %!");
+               Conformance_Error ("\prior declaration for% has convention %!");
 
             else
-               Conformance_Error ("calling conventions do not match!");
+               Conformance_Error ("\calling conventions do not match!");
             end if;
 
             return;
@@ -2027,7 +3199,7 @@ package body Sem_Ch6 is
          elsif Is_Formal_Subprogram (Old_Id)
            or else Is_Formal_Subprogram (New_Id)
          then
-            Conformance_Error ("formal subprograms not allowed!");
+            Conformance_Error ("\formal subprograms not allowed!");
             return;
          end if;
       end if;
@@ -2043,13 +3215,20 @@ package body Sem_Ch6 is
       New_Formal := First_Formal (New_Id);
 
       while Present (Old_Formal) and then Present (New_Formal) loop
+         if Is_Controlling_Formal (Old_Formal)
+           and then Is_Controlling_Formal (New_Formal)
+           and then Skip_Controlling_Formals
+         then
+            goto Skip_Controlling_Formal;
+         end if;
+
          if Ctype = Fully_Conformant then
 
             --  Names must match. Error message is more accurate if we do
             --  this before checking that the types of the formals match.
 
             if Chars (Old_Formal) /= Chars (New_Formal) then
-               Conformance_Error ("name & does not match!", New_Formal);
+               Conformance_Error ("\name & does not match!", New_Formal);
 
                --  Set error posted flag on new formal as well to stop
                --  junk cascaded messages in some cases.
@@ -2059,6 +3238,73 @@ package body Sem_Ch6 is
             end if;
          end if;
 
+         --  Ada 2005 (AI-423): Possible access [sub]type and itype match. This
+         --  case occurs whenever a subprogram is being renamed and one of its
+         --  parameters imposes a null exclusion. For example:
+
+         --     type T is null record;
+         --     type Acc_T is access T;
+         --     subtype Acc_T_Sub is Acc_T;
+
+         --     procedure P     (Obj : not null Acc_T_Sub);  --  itype
+         --     procedure Ren_P (Obj :          Acc_T_Sub)   --  subtype
+         --       renames P;
+
+         Old_Formal_Base := Etype (Old_Formal);
+         New_Formal_Base := Etype (New_Formal);
+
+         if Get_Inst then
+            Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
+            New_Formal_Base := Get_Instance_Of (New_Formal_Base);
+         end if;
+
+         Access_Types_Match := Ada_Version >= Ada_05
+
+            --  Ensure that this rule is only applied when New_Id is a
+            --  renaming of Old_Id.
+
+           and then Nkind (Parent (Parent (New_Id))) =
+                      N_Subprogram_Renaming_Declaration
+           and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
+           and then Present (Entity (Name (Parent (Parent (New_Id)))))
+           and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
+
+            --  Now handle the allowed access-type case
+
+           and then Is_Access_Type (Old_Formal_Base)
+           and then Is_Access_Type (New_Formal_Base)
+
+            --  The type kinds must match. The only exception occurs with
+            --  multiple generics of the form:
+
+            --   generic                    generic
+            --     type F is private;         type A is private;
+            --     type F_Ptr is access F;    type A_Ptr is access A;
+            --     with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
+            --   package F_Pack is ...      package A_Pack is
+            --                                package F_Inst is
+            --                                  new F_Pack (A, A_Ptr, A_P);
+
+            --  When checking for conformance between the parameters of A_P
+            --  and F_P, the type kinds of F_Ptr and A_Ptr will not match
+            --  because the compiler has transformed A_Ptr into a subtype of
+            --  F_Ptr. We catch this case in the code below.
+
+           and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
+                  or else
+                    (Is_Generic_Type (Old_Formal_Base)
+                       and then Is_Generic_Type (New_Formal_Base)
+                       and then Is_Internal (New_Formal_Base)
+                       and then Etype (Etype (New_Formal_Base)) =
+                                  Old_Formal_Base))
+           and then Directly_Designated_Type (Old_Formal_Base) =
+                      Directly_Designated_Type (New_Formal_Base)
+           and then ((Is_Itype (Old_Formal_Base)
+                       and then Can_Never_Be_Null (Old_Formal_Base))
+                    or else
+                     (Is_Itype (New_Formal_Base)
+                       and then Can_Never_Be_Null (New_Formal_Base)));
+
          --  Types must always match. In the visible part of an instance,
          --  usual overloading rules for dispatching operations apply, and
          --  we check base types (not the actual subtypes).
@@ -2067,37 +3313,93 @@ package body Sem_Ch6 is
            and then Is_Dispatching_Operation (New_Id)
          then
             if not Conforming_Types
-              (Base_Type (Etype (Old_Formal)),
-                 Base_Type (Etype (New_Formal)), Ctype, Get_Inst)
+                     (T1       => Base_Type (Etype (Old_Formal)),
+                      T2       => Base_Type (Etype (New_Formal)),
+                      Ctype    => Ctype,
+                      Get_Inst => Get_Inst)
+               and then not Access_Types_Match
             then
-               Conformance_Error ("type of & does not match!", New_Formal);
+               Conformance_Error ("\type of & does not match!", New_Formal);
                return;
             end if;
 
          elsif not Conforming_Types
-           (Etype (Old_Formal), Etype (New_Formal), Ctype, Get_Inst)
+                     (T1       => Old_Formal_Base,
+                      T2       => New_Formal_Base,
+                      Ctype    => Ctype,
+                      Get_Inst => Get_Inst)
+           and then not Access_Types_Match
          then
-            Conformance_Error ("type of & does not match!", New_Formal);
+            Conformance_Error ("\type of & does not match!", New_Formal);
             return;
          end if;
 
          --  For mode conformance, mode must match
 
-         if Ctype >= Mode_Conformant
-           and then Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal)
-         then
-            Conformance_Error ("mode of & does not match!", New_Formal);
-            return;
+         if Ctype >= Mode_Conformant then
+            if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
+               Conformance_Error ("\mode of & does not match!", New_Formal);
+               return;
+
+            --  Part of mode conformance for access types is having the same
+            --  constant modifier.
+
+            elsif Access_Types_Match
+              and then Is_Access_Constant (Old_Formal_Base) /=
+                       Is_Access_Constant (New_Formal_Base)
+            then
+               Conformance_Error
+                 ("\constant modifier does not match!", New_Formal);
+               return;
+            end if;
+         end if;
+
+         if Ctype >= Subtype_Conformant then
+
+            --  Ada 2005 (AI-231): In case of anonymous access types check
+            --  the null-exclusion and access-to-constant attributes must
+            --  match.
+
+            if Ada_Version >= Ada_05
+              and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
+              and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
+              and then
+                (Can_Never_Be_Null (Old_Formal) /=
+                 Can_Never_Be_Null (New_Formal)
+                   or else
+                 Is_Access_Constant (Etype (Old_Formal)) /=
+                 Is_Access_Constant (Etype (New_Formal)))
+            then
+               --  It is allowed to omit the null-exclusion in case of stream
+               --  attribute subprograms. We recognize stream subprograms
+               --  through their TSS-generated suffix.
+
+               declare
+                  TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
+               begin
+                  if TSS_Name /= TSS_Stream_Read
+                    and then TSS_Name /= TSS_Stream_Write
+                    and then TSS_Name /= TSS_Stream_Input
+                    and then TSS_Name /= TSS_Stream_Output
+                  then
+                     Conformance_Error
+                       ("\type of & does not match!", New_Formal);
+                     return;
+                  end if;
+               end;
+            end if;
          end if;
 
          --  Full conformance checks
 
          if Ctype = Fully_Conformant then
 
-            --  We have checked already that names match.
-            --  Check default expressions for in parameters
+            --  We have checked already that names match
 
             if Parameter_Mode (Old_Formal) = E_In_Parameter then
+
+               --  Check default expressions for in parameters
+
                declare
                   NewD : constant Boolean :=
                            Present (Default_Value (New_Formal));
@@ -2106,15 +3408,14 @@ package body Sem_Ch6 is
                begin
                   if NewD or OldD then
 
-                     --  The old default value has been analyzed because
-                     --  the current full declaration will have frozen
-                     --  everything before. The new default values have not
-                     --  been analyzed, so analyze them now before we check
-                     --  for conformance.
+                     --  The old default value has been analyzed because the
+                     --  current full declaration will have frozen everything
+                     --  before. The new default value has not been analyzed,
+                     --  so analyze it now before we check for conformance.
 
                      if NewD then
-                        New_Scope (New_Id);
-                        Analyze_Per_Use_Expression
+                        Push_Scope (New_Id);
+                        Preanalyze_Spec_Expression
                           (Default_Value (New_Formal), Etype (New_Formal));
                         End_Scope;
                      end if;
@@ -2125,7 +3426,7 @@ package body Sem_Ch6 is
                                      Default_Value (New_Formal))
                      then
                         Conformance_Error
-                          ("default expression for & does not match!",
+                          ("\default expression for & does not match!",
                            New_Formal);
                         return;
                      end if;
@@ -2135,10 +3436,10 @@ package body Sem_Ch6 is
          end if;
 
          --  A couple of special checks for Ada 83 mode. These checks are
-         --  skipped if either entity is an operator in package Standard.
+         --  skipped if either entity is an operator in package Standard,
          --  or if either old or new instance is not from the source program.
 
-         if Ada_83
+         if Ada_Version = Ada_83
            and then Sloc (Old_Id) > Standard_Location
            and then Sloc (New_Id) > Standard_Location
            and then Comes_From_Source (Old_Id)
@@ -2156,7 +3457,7 @@ package body Sem_Ch6 is
                  and then Ctype = Fully_Conformant
                then
                   Conformance_Error
-                    ("(Ada 83) IN must appear in both declarations",
+                    ("\(Ada 83) IN must appear in both declarations",
                      New_Formal);
                   return;
                end if;
@@ -2164,7 +3465,7 @@ package body Sem_Ch6 is
                --  Grouping (use of comma in param lists) must be the same
                --  This is where we catch a misconformance like:
 
-               --    A,B : Integer
+               --    A, B : Integer
                --    A : Integer; B : Integer
 
                --  which are represented identically in the tree except
@@ -2174,26 +3475,168 @@ package body Sem_Ch6 is
                  or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
                then
                   Conformance_Error
-                    ("grouping of & does not match!", New_Formal);
+                    ("\grouping of & does not match!", New_Formal);
                   return;
                end if;
             end;
          end if;
 
+         --  This label is required when skipping controlling formals
+
+         <<Skip_Controlling_Formal>>
+
          Next_Formal (Old_Formal);
          Next_Formal (New_Formal);
       end loop;
 
       if Present (Old_Formal) then
-         Conformance_Error ("too few parameters!");
+         Conformance_Error ("\too few parameters!");
          return;
 
       elsif Present (New_Formal) then
-         Conformance_Error ("too many parameters!", New_Formal);
+         Conformance_Error ("\too many parameters!", New_Formal);
          return;
       end if;
+   end Check_Conformance;
+
+   -----------------------
+   -- Check_Conventions --
+   -----------------------
+
+   procedure Check_Conventions (Typ : Entity_Id) is
+
+      function Skip_Check (Op : Entity_Id) return Boolean;
+      pragma Inline (Skip_Check);
+      --  A small optimization: skip the predefined dispatching operations,
+      --  since they always have the same convention. Also do not consider
+      --  abstract primitives since those are left by an erroneous overriding.
+      --  This function returns True for any operation that is thus exempted
+      --  exempted from checking.
+
+      procedure Check_Convention
+        (Op          : Entity_Id;
+         Search_From : Elmt_Id);
+      --  Verify that the convention of inherited dispatching operation Op is
+      --  consistent among all subprograms it overrides. In order to minimize
+      --  the search, Search_From is utilized to designate a specific point in
+      --  the list rather than iterating over the whole list once more.
+
+      ----------------------
+      -- Check_Convention --
+      ----------------------
+
+      procedure Check_Convention
+        (Op          : Entity_Id;
+         Search_From : Elmt_Id)
+      is
+         procedure Error_Msg_Operation (Op : Entity_Id);
+         --  Emit a continuation to an error message depicting the kind, name,
+         --  convention and source location of subprogram Op.
+
+         -------------------------
+         -- Error_Msg_Operation --
+         -------------------------
+
+         procedure Error_Msg_Operation (Op : Entity_Id) is
+         begin
+            Error_Msg_Name_1 := Chars (Op);
+
+            --  Error messages of primitive subprograms do not contain a
+            --  convention attribute since the convention may have been first
+            --  inherited from a parent subprogram, then changed by a pragma.
+
+            if Comes_From_Source (Op) then
+               Error_Msg_Sloc := Sloc (Op);
+               Error_Msg_N
+                ("\ primitive % defined #", Typ);
+
+            else
+               Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
+
+               if Present (Abstract_Interface_Alias (Op)) then
+                  Error_Msg_Sloc := Sloc (Abstract_Interface_Alias (Op));
+                  Error_Msg_N ("\\overridden operation % with " &
+                               "convention % defined #", Typ);
+
+               else pragma Assert (Present (Alias (Op)));
+                  Error_Msg_Sloc := Sloc (Alias (Op));
+                  Error_Msg_N ("\\inherited operation % with " &
+                               "convention % defined #", Typ);
+               end if;
+            end if;
+         end Error_Msg_Operation;
+
+         --  Local variables
+
+         Second_Prim_Op      : Entity_Id;
+         Second_Prim_Op_Elmt : Elmt_Id;
+
+      --  Start of processing for Check_Convention
+
+      begin
+         Second_Prim_Op_Elmt := Next_Elmt (Search_From);
+         while Present (Second_Prim_Op_Elmt) loop
+            Second_Prim_Op := Node (Second_Prim_Op_Elmt);
+
+            if not Skip_Check (Second_Prim_Op)
+              and then Chars (Second_Prim_Op) = Chars (Op)
+              and then Type_Conformant (Second_Prim_Op, Op)
+              and then Convention (Second_Prim_Op) /= Convention (Op)
+            then
+               Error_Msg_N
+                 ("inconsistent conventions in primitive operations", Typ);
+
+               Error_Msg_Operation (Op);
+               Error_Msg_Operation (Second_Prim_Op);
+
+               --  Avoid cascading errors
+
+               return;
+            end if;
+
+            Next_Elmt (Second_Prim_Op_Elmt);
+         end loop;
+      end Check_Convention;
+
+      ----------------
+      -- Skip_Check --
+      ----------------
+
+      function Skip_Check (Op : Entity_Id) return Boolean is
+      begin
+         return Is_Predefined_Dispatching_Operation (Op)
+           or else Is_Abstract_Subprogram (Op);
+      end Skip_Check;
+
+      --  Local variables
+
+      Prim_Op      : Entity_Id;
+      Prim_Op_Elmt : Elmt_Id;
+
+   --  Start of processing for Check_Conventions
+
+   begin
+      --  The algorithm checks every overriding dispatching operation against
+      --  all the corresponding overridden dispatching operations, detecting
+      --  differences in coventions.
+
+      Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
+      while Present (Prim_Op_Elmt) loop
+         Prim_Op := Node (Prim_Op_Elmt);
+
+         --  A small optimization: skip the predefined dispatching operations
+         --  since they always have the same convention. Also avoid processing
+         --  of abstract primitives left from an erroneous overriding.
+
+         if not Skip_Check (Prim_Op) then
+            Check_Convention
+              (Op          => Prim_Op,
+               Search_From => Prim_Op_Elmt);
+         end if;
 
-   end Check_Conformance;
+         Next_Elmt (Prim_Op_Elmt);
+      end loop;
+   end Check_Conventions;
 
    ------------------------------
    -- Check_Delayed_Subprogram --
@@ -2207,6 +3650,10 @@ package body Sem_Ch6 is
       --  If T is not yet frozen and needs a delayed freeze, then the
       --  subprogram itself must be delayed.
 
+      ---------------------
+      -- Possible_Freeze --
+      ---------------------
+
       procedure Possible_Freeze (T : Entity_Id) is
       begin
          if Has_Delayed_Freeze (T)
@@ -2227,7 +3674,9 @@ package body Sem_Ch6 is
    begin
       --  Never need to freeze abstract subprogram
 
-      if Is_Abstract (Designator) then
+      if Ekind (Designator) /= E_Subprogram_Type
+        and then Is_Abstract_Subprogram (Designator)
+      then
          null;
       else
          --  Need delayed freeze if return type itself needs a delayed
@@ -2259,10 +3708,10 @@ package body Sem_Ch6 is
             Utyp : constant Entity_Id := Underlying_Type (Typ);
 
          begin
-            if Is_Return_By_Reference_Type (Typ) then
+            if Is_Inherently_Limited_Type (Typ) then
                Set_Returns_By_Ref (Designator);
 
-            elsif Present (Utyp) and then Controlled_Type (Utyp) then
+            elsif Present (Utyp) and then CW_Or_Controlled_Type (Utyp) then
                Set_Returns_By_Ref (Designator);
             end if;
          end;
@@ -2284,12 +3733,11 @@ package body Sem_Ch6 is
       New_Discr_Type : Entity_Id;
 
       procedure Conformance_Error (Msg : String; N : Node_Id);
-      --  Post error message for conformance error on given node.
-      --  Two messages are output. The first points to the previous
-      --  declaration with a general "no conformance" message.
-      --  The second is the detailed reason, supplied as Msg. The
-      --  parameter N provide information for a possible & insertion
-      --  in the message.
+      --  Post error message for conformance error on given node. Two messages
+      --  are output. The first points to the previous declaration with a
+      --  general "no conformance" message. The second is the detailed reason,
+      --  supplied as Msg. The parameter N provide information for a possible
+      --  & insertion in the message.
 
       -----------------------
       -- Conformance_Error --
@@ -2309,9 +3757,9 @@ package body Sem_Ch6 is
 
          New_Discr_Id := Defining_Identifier (New_Discr);
 
-         --  The subtype mark of the discriminant on the full type
-         --  has not been analyzed so we do it here. For an access
-         --  discriminant a new type is created.
+         --  The subtype mark of the discriminant on the full type has not
+         --  been analyzed so we do it here. For an access discriminant a new
+         --  type is created.
 
          if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
             New_Discr_Type :=
@@ -2328,8 +3776,8 @@ package body Sem_Ch6 is
             Conformance_Error ("type of & does not match!", New_Discr_Id);
             return;
          else
-            --  Treat the new discriminant as an occurrence of the old
-            --  one, for navigation purposes, and fill in some semantic
+            --  Treat the new discriminant as an occurrence of the old one,
+            --  for navigation purposes, and fill in some semantic
             --  information, for completeness.
 
             Generate_Reference (Old_Discr, New_Discr_Id, 'r');
@@ -2357,11 +3805,11 @@ package body Sem_Ch6 is
 
                --  The old default value has been analyzed and expanded,
                --  because the current full declaration will have frozen
-               --  everything before. The new default values have not
-               --  been expanded, so expand now to check conformance.
+               --  everything before. The new default values have not been
+               --  expanded, so expand now to check conformance.
 
                if NewD then
-                  Analyze_Per_Use_Expression
+                  Preanalyze_Spec_Expression
                     (Expression (New_Discr), New_Discr_Type);
                end if;
 
@@ -2381,7 +3829,7 @@ package body Sem_Ch6 is
 
          --  In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
 
-         if Ada_83 then
+         if Ada_Version = Ada_83 then
             declare
                Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
 
@@ -2430,7 +3878,7 @@ package body Sem_Ch6 is
       Err_Loc : Node_Id := Empty)
    is
       Result : Boolean;
-
+      pragma Warnings (Off, Result);
    begin
       Check_Conformance
         (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
@@ -2447,116 +3895,143 @@ package body Sem_Ch6 is
       Get_Inst : Boolean := False)
    is
       Result : Boolean;
-
+      pragma Warnings (Off, Result);
    begin
       Check_Conformance
         (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
    end Check_Mode_Conformant;
 
    --------------------------------
-   -- Check_Overriding_Operation --
+   -- Check_Overriding_Indicator --
    --------------------------------
 
-   procedure Check_Overriding_Operation
-     (N    : Node_Id;
-      Subp : Entity_Id)
+   procedure Check_Overriding_Indicator
+     (Subp            : Entity_Id;
+      Overridden_Subp : Entity_Id;
+      Is_Primitive    : Boolean)
    is
-      Arg1       : Node_Id;
-      Decl       : Node_Id;
-      Has_Pragma : Boolean := False;
+      Decl : Node_Id;
+      Spec : Node_Id;
 
    begin
-      --  See whether there is an overriding pragma immediately following
-      --  the declaration. Intervening pragmas, such as Inline, are allowed.
-
-      Decl := Next (N);
-      while Present (Decl)
-        and then Nkind (Decl) = N_Pragma
-      loop
-         if Chars (Decl) = Name_Overriding
-           or else Chars (Decl) = Name_Optional_Overriding
-         then
-            --  For now disable the use of these pragmas, until the ARG
-            --  finalizes the design of this feature.
+      --  No overriding indicator for literals
 
-            Error_Msg_N ("?unrecognized pragma", Decl);
+      if Ekind (Subp) = E_Enumeration_Literal then
+         return;
 
-            if not Is_Overriding_Operation (Subp) then
+      elsif Ekind (Subp) = E_Entry then
+         Decl := Parent (Subp);
 
-               --  Before emitting an error message, check whether this
-               --  may override an operation that is not yet visible, as
-               --  in the case of a derivation of a private operation in
-               --  a child unit. Such an operation is introduced with a
-               --  different name, but its alias is the parent operation.
+      else
+         Decl := Unit_Declaration_Node (Subp);
+      end if;
 
-               declare
-                  E : Entity_Id;
+      if Nkind_In (Decl, N_Subprogram_Body,
+                         N_Subprogram_Body_Stub,
+                         N_Subprogram_Declaration,
+                         N_Abstract_Subprogram_Declaration,
+                         N_Subprogram_Renaming_Declaration)
+      then
+         Spec := Specification (Decl);
 
-               begin
-                  E := First_Entity (Current_Scope);
+      elsif Nkind (Decl) = N_Entry_Declaration then
+         Spec := Decl;
 
-                  while Present (E) loop
-                     if Ekind (E) = Ekind (Subp)
-                       and then not Comes_From_Source (E)
-                       and then Present (Alias (E))
-                       and then Chars (Alias (E)) = Chars (Subp)
-                       and then In_Open_Scopes (Scope (Alias (E)))
-                     then
-                        exit;
-                     else
-                        Next_Entity (E);
-                     end if;
-                  end loop;
+      else
+         return;
+      end if;
 
-                  if No (E) then
-                     Error_Msg_NE
-                       ("& must override an inherited operation",
-                         Decl, Subp);
-                  end if;
-               end;
+      if Present (Overridden_Subp) then
+         if Must_Not_Override (Spec) then
+            Error_Msg_Sloc := Sloc (Overridden_Subp);
+
+            if Ekind (Subp) = E_Entry then
+               Error_Msg_NE
+                 ("entry & overrides inherited operation #", Spec, Subp);
+            else
+               Error_Msg_NE
+                 ("subprogram & overrides inherited operation #", Spec, Subp);
             end if;
 
-            --  Verify syntax of pragma
+         elsif Is_Subprogram (Subp) then
+            Set_Is_Overriding_Operation (Subp);
+         end if;
 
-            Arg1 := First (Pragma_Argument_Associations (Decl));
+      --  If Subp is an operator, it may override a predefined operation.
+      --  In that case overridden_subp is empty because of our implicit
+      --  representation for predefined operators. We have to check whether the
+      --  signature of Subp matches that of a predefined operator. Note that
+      --  first argument provides the name of the operator, and the second
+      --  argument the signature that may match that of a standard operation.
+      --  If the indicator is overriding, then the operator must match a
+      --  predefined signature, because we know already that there is no
+      --  explicit overridden operation.
 
-            if Present (Arg1) then
-               if not Is_Entity_Name (Expression (Arg1)) then
-                  Error_Msg_N ("pragma applies to local subprogram", Decl);
+      elsif Nkind (Subp) = N_Defining_Operator_Symbol then
 
-               elsif Chars (Expression (Arg1)) /= Chars (Subp) then
-                  Error_Msg_N
-                    ("pragma must apply to preceding subprogram", Decl);
+         if Must_Not_Override (Spec) then
+            if not Is_Primitive then
+               Error_Msg_N
+                 ("overriding indicator only allowed "
+                    & "if subprogram is primitive", Subp);
 
-               elsif Present (Next (Arg1)) then
-                  Error_Msg_N ("illegal pragma format", Decl);
-               end if;
+            elsif Operator_Matches_Spec (Subp, Subp) then
+               Error_Msg_NE
+                 ("subprogram & overrides predefined operator ", Spec, Subp);
             end if;
 
-            Set_Analyzed (Decl);
-            Has_Pragma := True;
-            exit;
+         elsif Is_Overriding_Operation (Subp) then
+            null;
+
+         elsif Must_Override (Spec) then
+            if not Operator_Matches_Spec (Subp, Subp) then
+               Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
+
+            else
+               Set_Is_Overriding_Operation (Subp);
+            end if;
          end if;
 
-         Next (Decl);
-      end loop;
+      elsif Must_Override (Spec) then
+         if Ekind (Subp) = E_Entry then
+            Error_Msg_NE ("entry & is not overriding", Spec, Subp);
+         else
+            Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
+         end if;
+
+      --  If the operation is marked "not overriding" and it's not primitive
+      --  then an error is issued, unless this is an operation of a task or
+      --  protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
+      --  has been specified have already been checked above.
 
-      if not Has_Pragma
-        and then Explicit_Overriding
-        and then Is_Overriding_Operation (Subp)
+      elsif Must_Not_Override (Spec)
+        and then not Is_Primitive
+        and then Ekind (Subp) /= E_Entry
+        and then Ekind (Scope (Subp)) /= E_Protected_Type
       then
-         Error_Msg_NE ("Missing overriding pragma for&", Subp, Subp);
+         Error_Msg_N
+           ("overriding indicator only allowed if subprogram is primitive",
+            Subp);
+         return;
       end if;
-   end Check_Overriding_Operation;
+   end Check_Overriding_Indicator;
 
    -------------------
    -- Check_Returns --
    -------------------
 
+   --  Note: this procedure needs to know far too much about how the expander
+   --  messes with exceptions. The use of the flag Exception_Junk and the
+   --  incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
+   --  works, but is not very clean. It would be better if the expansion
+   --  routines would leave Original_Node working nicely, and we could use
+   --  Original_Node here to ignore all the peculiar expander messing ???
+
    procedure Check_Returns
      (HSS  : Node_Id;
       Mode : Character;
-      Err  : out Boolean)
+      Err  : out Boolean;
+      Proc : Entity_Id := Empty)
    is
       Handler : Node_Id;
 
@@ -2571,6 +4046,7 @@ package body Sem_Ch6 is
 
       procedure Check_Statement_Sequence (L : List_Id) is
          Last_Stm : Node_Id;
+         Stm      : Node_Id;
          Kind     : Node_Kind;
 
          Raise_Exception_Call : Boolean;
@@ -2584,6 +4060,65 @@ package body Sem_Ch6 is
 
          Last_Stm := Last (L);
 
+         --  Deal with digging out exception handler statement sequences that
+         --  have been transformed by the local raise to goto optimization.
+         --  See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
+         --  optimization has occurred, we are looking at something like:
+
+         --  begin
+         --     original stmts in block
+
+         --  exception            \
+         --     when excep1 =>     |
+         --        goto L1;        | omitted if No_Exception_Propagation
+         --     when excep2 =>     |
+         --        goto L2;       /
+         --  end;
+
+         --  goto L3;      -- skip handler when exception not raised
+
+         --  <<L1>>        -- target label for local exception
+         --     begin
+         --        estmts1
+         --     end;
+
+         --     goto L3;
+
+         --  <<L2>>
+         --     begin
+         --        estmts2
+         --     end;
+
+         --  <<L3>>
+
+         --  and what we have to do is to dig out the estmts1 and estmts2
+         --  sequences (which were the original sequences of statements in
+         --  the exception handlers) and check them.
+
+         if Nkind (Last_Stm) = N_Label
+           and then Exception_Junk (Last_Stm)
+         then
+            Stm := Last_Stm;
+            loop
+               Prev (Stm);
+               exit when No (Stm);
+               exit when Nkind (Stm) /= N_Block_Statement;
+               exit when not Exception_Junk (Stm);
+               Prev (Stm);
+               exit when No (Stm);
+               exit when Nkind (Stm) /= N_Label;
+               exit when not Exception_Junk (Stm);
+               Check_Statement_Sequence
+                 (Statements (Handled_Statement_Sequence (Next (Stm))));
+
+               Prev (Stm);
+               Last_Stm := Stm;
+               exit when No (Stm);
+               exit when Nkind (Stm) /= N_Goto_Statement;
+               exit when not Exception_Junk (Stm);
+            end loop;
+         end if;
+
          --  Don't count pragmas
 
          while Nkind (Last_Stm) = N_Pragma
@@ -2600,10 +4135,12 @@ package body Sem_Ch6 is
          --  Don't count exception junk
 
            or else
-             ((Nkind (Last_Stm) = N_Goto_Statement
-                 or else Nkind (Last_Stm) = N_Label
-                 or else Nkind (Last_Stm) = N_Object_Declaration)
-               and then Exception_Junk (Last_Stm))
+             (Nkind_In (Last_Stm, N_Goto_Statement,
+                                   N_Label,
+                                   N_Object_Declaration)
+                and then Exception_Junk (Last_Stm))
+           or else Nkind (Last_Stm) in N_Push_xxx_Label
+           or else Nkind (Last_Stm) in N_Pop_xxx_Label
          loop
             Prev (Last_Stm);
          end loop;
@@ -2633,6 +4170,9 @@ package body Sem_Ch6 is
             --  missing return curious, and raising Program_Error does not
             --  seem such a bad behavior if this does occur.
 
+            --  Note that in the Ada 2005 case for Raise_Exception, the actual
+            --  behavior will be to raise Constraint_Error (see AI-329).
+
             if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
                  or else
                Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
@@ -2646,7 +4186,6 @@ package body Sem_Ch6 is
                declare
                   Arg : constant Node_Id :=
                           Original_Node (First_Actual (Last_Stm));
-
                begin
                   if Nkind (Arg) = N_Attribute_Reference
                     and then Attribute_Name (Arg) = Name_Identity
@@ -2684,7 +4223,6 @@ package body Sem_Ch6 is
          elsif Kind = N_Case_Statement then
             declare
                Case_Alt : Node_Id;
-
             begin
                Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
                while Present (Case_Alt) loop
@@ -2801,13 +4339,12 @@ package body Sem_Ch6 is
          --  If we fall through, issue appropriate message
 
          if Mode = 'F' then
-
             if not Raise_Exception_Call then
                Error_Msg_N
                  ("?RETURN statement missing following this statement!",
                   Last_Stm);
                Error_Msg_N
-                 ("\?Program_Error may be raised at run time",
+                 ("\?Program_Error may be raised at run time!",
                   Last_Stm);
             end if;
 
@@ -2818,10 +4355,27 @@ package body Sem_Ch6 is
 
             Err := True;
 
+         --  Otherwise we have the case of a procedure marked No_Return
+
          else
-            Error_Msg_N
-              ("implied return after this statement not allowed (No_Return)",
-               Last_Stm);
+            if not Raise_Exception_Call then
+               Error_Msg_N
+                 ("?implied return after this statement " &
+                  "will raise Program_Error",
+                  Last_Stm);
+               Error_Msg_NE
+                 ("\?procedure & is marked as No_Return!",
+                  Last_Stm, Proc);
+            end if;
+
+            declare
+               RE : constant Node_Id :=
+                      Make_Raise_Program_Error (Sloc (Last_Stm),
+                        Reason => PE_Implicit_Return);
+            begin
+               Insert_After (Last_Stm, RE);
+               Analyze (RE);
+            end;
          end if;
       end Check_Statement_Sequence;
 
@@ -2850,6 +4404,10 @@ package body Sem_Ch6 is
       --  This is used to check if S1 > S2 in the sense required by this
       --  test, for example nameab < namec, but name2 < name10.
 
+      -----------------------------
+      -- Subprogram_Name_Greater --
+      -----------------------------
+
       function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
          L1, L2 : Positive;
          N1, N2 : Natural;
@@ -2901,7 +4459,7 @@ package body Sem_Ch6 is
       --  Check body in alpha order if this is option
 
       if Style_Check
-        and then Style_Check_Subprogram_Order
+        and then Style_Check_Order_Subprograms
         and then Nkind (N) = N_Subprogram_Body
         and then Comes_From_Source (N)
         and then In_Extended_Main_Source_Unit (N)
@@ -2942,7 +4500,7 @@ package body Sem_Ch6 is
       Err_Loc : Node_Id := Empty)
    is
       Result : Boolean;
-
+      pragma Warnings (Off, Result);
    begin
       Check_Conformance
         (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc);
@@ -2958,7 +4516,7 @@ package body Sem_Ch6 is
       Err_Loc : Node_Id := Empty)
    is
       Result : Boolean;
-
+      pragma Warnings (Off, Result);
    begin
       Check_Conformance
         (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
@@ -2972,20 +4530,33 @@ package body Sem_Ch6 is
      (T1       : Entity_Id;
       T2       : Entity_Id;
       Ctype    : Conformance_Type;
-      Get_Inst : Boolean := False)
-      return     Boolean
+      Get_Inst : Boolean := False) return Boolean
    is
       Type_1 : Entity_Id := T1;
       Type_2 : Entity_Id := T2;
+      Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
 
       function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
-      --  If neither T1 nor T2 are generic actual types, or if they are
-      --  in different scopes (e.g. parent and child instances), then verify
-      --  that the base types are equal. Otherwise T1 and T2 must be
-      --  on the same subtype chain. The whole purpose of this procedure
-      --  is to prevent spurious ambiguities in an instantiation that may
-      --  arise if two distinct generic types are instantiated with the
-      --  same actual.
+      --  If neither T1 nor T2 are generic actual types, or if they are in
+      --  different scopes (e.g. parent and child instances), then verify that
+      --  the base types are equal. Otherwise T1 and T2 must be on the same
+      --  subtype chain. The whole purpose of this procedure is to prevent
+      --  spurious ambiguities in an instantiation that may arise if two
+      --  distinct generic types are instantiated with the same actual.
+
+      function Find_Designated_Type (T : Entity_Id) return Entity_Id;
+      --  An access parameter can designate an incomplete type. If the
+      --  incomplete type is the limited view of a type from a limited_
+      --  with_clause, check whether the non-limited view is available. If
+      --  it is a (non-limited) incomplete type, get the full view.
+
+      function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
+      --  Returns True if and only if either T1 denotes a limited view of T2
+      --  or T2 denotes a limited view of T1. This can arise when the limited
+      --  with view of a type is used in a subprogram declaration and the
+      --  subprogram body is in the scope of a regular with clause for the
+      --  same unit. In such a case, the two type entities can be considered
+      --  identical for purposes of conformance checking.
 
       ----------------------
       -- Base_Types_Match --
@@ -2998,7 +4569,7 @@ package body Sem_Ch6 is
 
          elsif Base_Type (T1) = Base_Type (T2) then
 
-            --  The following is too permissive. A more precise test must
+            --  The following is too permissive. A more precise test should
             --  check that the generic actual is an ancestor subtype of the
             --  other ???.
 
@@ -3011,20 +4582,80 @@ package body Sem_Ch6 is
          end if;
       end Base_Types_Match;
 
+      --------------------------
+      -- Find_Designated_Type --
+      --------------------------
+
+      function Find_Designated_Type (T : Entity_Id) return Entity_Id is
+         Desig : Entity_Id;
+
+      begin
+         Desig := Directly_Designated_Type (T);
+
+         if Ekind (Desig) = E_Incomplete_Type then
+
+            --  If regular incomplete type, get full view if available
+
+            if Present (Full_View (Desig)) then
+               Desig := Full_View (Desig);
+
+            --  If limited view of a type, get non-limited view if available,
+            --  and check again for a regular incomplete type.
+
+            elsif Present (Non_Limited_View (Desig)) then
+               Desig := Get_Full_View (Non_Limited_View (Desig));
+            end if;
+         end if;
+
+         return Desig;
+      end Find_Designated_Type;
+
+      -------------------------------
+      -- Matches_Limited_With_View --
+      -------------------------------
+
+      function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
+      begin
+         --  In some cases a type imported through a limited_with clause, and
+         --  its nonlimited view are both visible, for example in an anonymous
+         --  access-to-class-wide type in a formal. Both entities designate the
+         --  same type.
+
+         if From_With_Type (T1)
+           and then T2 = Available_View (T1)
+         then
+            return True;
+
+         elsif From_With_Type (T2)
+           and then T1 = Available_View (T2)
+         then
+            return True;
+
+         else
+            return False;
+         end if;
+      end Matches_Limited_With_View;
+
+   --  Start of processing for Conforming_Types
+
    begin
       --  The context is an instance association for a formal
-      --  access-to-subprogram type; the formal parameter types
-      --  require mapping because they may denote other formal
-      --  parameters of the generic unit.
+      --  access-to-subprogram type; the formal parameter types require
+      --  mapping because they may denote other formal parameters of the
+      --  generic unit.
 
       if Get_Inst then
          Type_1 := Get_Instance_Of (T1);
          Type_2 := Get_Instance_Of (T2);
       end if;
 
-      --  First see if base types match
+      --  If one of the types is a view of the other introduced by a limited
+      --  with clause, treat these as conforming for all purposes.
+
+      if Matches_Limited_With_View (T1, T2) then
+         return True;
 
-      if Base_Types_Match (Type_1, Type_2) then
+      elsif Base_Types_Match (Type_1, Type_2) then
          return Ctype <= Mode_Conformant
            or else Subtypes_Statically_Match (Type_1, Type_2);
 
@@ -3051,51 +4682,62 @@ package body Sem_Ch6 is
            or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
       end if;
 
-      --  Test anonymous access type case. For this case, static subtype
-      --  matching is required for mode conformance (RM 6.3.1(15))
+      --  Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
+      --  treated recursively because they carry a signature.
 
-      if Ekind (Type_1) = E_Anonymous_Access_Type
-        and then Ekind (Type_2) = E_Anonymous_Access_Type
+      Are_Anonymous_Access_To_Subprogram_Types :=
+        Ekind (Type_1) = Ekind (Type_2)
+          and then
+            (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
+             or else
+               Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
+
+      --  Test anonymous access type case. For this case, static subtype
+      --  matching is required for mode conformance (RM 6.3.1(15)). We check
+      --  the base types because we may have built internal subtype entities
+      --  to handle null-excluding types (see Process_Formals).
+
+      if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
+            and then
+          Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
+        or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
       then
          declare
             Desig_1 : Entity_Id;
             Desig_2 : Entity_Id;
 
          begin
-            Desig_1 := Directly_Designated_Type (Type_1);
+            --  In Ada2005, access constant indicators must match for
+            --  subtype conformance.
 
-            --  An access parameter can designate an incomplete type.
-
-            if Ekind (Desig_1) = E_Incomplete_Type
-              and then Present (Full_View (Desig_1))
+            if Ada_Version >= Ada_05
+              and then Ctype >= Subtype_Conformant
+              and then
+                Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
             then
-               Desig_1 := Full_View (Desig_1);
+               return False;
             end if;
 
-            Desig_2 := Directly_Designated_Type (Type_2);
+            Desig_1 := Find_Designated_Type (Type_1);
 
-            if Ekind (Desig_2) = E_Incomplete_Type
-              and then Present (Full_View (Desig_2))
-            then
-               Desig_2 := Full_View (Desig_2);
-            end if;
+            Desig_2 := Find_Designated_Type (Type_2);
 
-            --  The context is an instance association for a formal
-            --  access-to-subprogram type; formal access parameter
-            --  designated types require mapping because they may
-            --  denote other formal parameters of the generic unit.
+            --  If the context is an instance association for a formal
+            --  access-to-subprogram type; formal access parameter designated
+            --  types require mapping because they may denote other formal
+            --  parameters of the generic unit.
 
             if Get_Inst then
                Desig_1 := Get_Instance_Of (Desig_1);
                Desig_2 := Get_Instance_Of (Desig_2);
             end if;
 
-            --  It is possible for a Class_Wide_Type to be introduced for
-            --  an incomplete type, in which case there is a separate class_
-            --  wide type for the full view. The types conform if their
-            --  Etypes conform, i.e. one may be the full view of the other.
-            --  This can only happen in the context of an access parameter,
-            --  other uses of an incomplete Class_Wide_Type are illegal.
+            --  It is possible for a Class_Wide_Type to be introduced for an
+            --  incomplete type, in which case there is a separate class_ wide
+            --  type for the full view. The types conform if their Etypes
+            --  conform, i.e. one may be the full view of the other. This can
+            --  only happen in the context of an access parameter, other uses
+            --  of an incomplete Class_Wide_Type are illegal.
 
             if Is_Class_Wide_Type (Desig_1)
               and then Is_Class_Wide_Type (Desig_2)
@@ -3104,20 +4746,49 @@ package body Sem_Ch6 is
                  Conforming_Types
                    (Etype (Base_Type (Desig_1)),
                     Etype (Base_Type (Desig_2)), Ctype);
+
+            elsif Are_Anonymous_Access_To_Subprogram_Types then
+               if Ada_Version < Ada_05 then
+                  return Ctype = Type_Conformant
+                    or else
+                      Subtypes_Statically_Match (Desig_1, Desig_2);
+
+               --  We must check the conformance of the signatures themselves
+
+               else
+                  declare
+                     Conformant : Boolean;
+                  begin
+                     Check_Conformance
+                       (Desig_1, Desig_2, Ctype, False, Conformant);
+                     return Conformant;
+                  end;
+               end if;
+
             else
                return Base_Type (Desig_1) = Base_Type (Desig_2)
                 and then (Ctype = Type_Conformant
-                          or else
-                        Subtypes_Statically_Match (Desig_1, Desig_2));
+                            or else
+                          Subtypes_Statically_Match (Desig_1, Desig_2));
             end if;
          end;
 
       --  Otherwise definitely no match
 
       else
+         if ((Ekind (Type_1) = E_Anonymous_Access_Type
+               and then Is_Access_Type (Type_2))
+            or else (Ekind (Type_2) = E_Anonymous_Access_Type
+                       and then Is_Access_Type (Type_1)))
+           and then
+             Conforming_Types
+               (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
+         then
+            May_Hide_Profile := True;
+         end if;
+
          return False;
       end if;
-
    end Conforming_Types;
 
    --------------------------
@@ -3126,35 +4797,41 @@ package body Sem_Ch6 is
 
    procedure Create_Extra_Formals (E : Entity_Id) is
       Formal      : Entity_Id;
+      First_Extra : Entity_Id := Empty;
       Last_Extra  : Entity_Id;
       Formal_Type : Entity_Id;
       P_Formal    : Entity_Id := Empty;
 
-      function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id;
-      --  Add an extra formal, associated with the current Formal. The
-      --  extra formal is added to the list of extra formals, and also
-      --  returned as the result. These formals are always of mode IN.
+      function Add_Extra_Formal
+        (Assoc_Entity : Entity_Id;
+         Typ          : Entity_Id;
+         Scope        : Entity_Id;
+         Suffix       : String) return Entity_Id;
+      --  Add an extra formal to the current list of formals and extra formals.
+      --  The extra formal is added to the end of the list of extra formals,
+      --  and also returned as the result. These formals are always of mode IN.
+      --  The new formal has the type Typ, is declared in Scope, and its name
+      --  is given by a concatenation of the name of Assoc_Entity and Suffix.
 
       ----------------------
       -- Add_Extra_Formal --
       ----------------------
 
-      function Add_Extra_Formal (Typ : Entity_Id) return Entity_Id is
+      function Add_Extra_Formal
+        (Assoc_Entity : Entity_Id;
+         Typ          : Entity_Id;
+         Scope        : Entity_Id;
+         Suffix       : String) return Entity_Id
+      is
          EF : constant Entity_Id :=
-                Make_Defining_Identifier (Sloc (Formal),
-                  Chars => New_External_Name (Chars (Formal), 'F'));
+                Make_Defining_Identifier (Sloc (Assoc_Entity),
+                  Chars  => New_External_Name (Chars (Assoc_Entity),
+                                               Suffix => Suffix));
 
       begin
-         --  We never generate extra formals if expansion is not active
-         --  because we don't need them unless we are generating code.
-
-         if not Expander_Active then
-            return Empty;
-         end if;
-
-         --  A little optimization. Never generate an extra formal for
-         --  the _init operand of an initialization procedure, since it
-         --  could never be used.
+         --  A little optimization. Never generate an extra formal for the
+         --  _init operand of an initialization procedure, since it could
+         --  never be used.
 
          if Chars (Formal) = Name_uInit then
             return Empty;
@@ -3163,21 +4840,37 @@ package body Sem_Ch6 is
          Set_Ekind           (EF, E_In_Parameter);
          Set_Actual_Subtype  (EF, Typ);
          Set_Etype           (EF, Typ);
-         Set_Scope           (EF, Scope (Formal));
+         Set_Scope           (EF, Scope);
          Set_Mechanism       (EF, Default_Mechanism);
          Set_Formal_Validity (EF);
 
-         Set_Extra_Formal (Last_Extra, EF);
+         if No (First_Extra) then
+            First_Extra := EF;
+            Set_Extra_Formals (Scope, First_Extra);
+         end if;
+
+         if Present (Last_Extra) then
+            Set_Extra_Formal (Last_Extra, EF);
+         end if;
+
          Last_Extra := EF;
+
          return EF;
       end Add_Extra_Formal;
 
    --  Start of processing for Create_Extra_Formals
 
    begin
-      --  If this is a derived subprogram then the subtypes of the
-      --  parent subprogram's formal parameters will be used to
-      --  to determine the need for extra formals.
+      --  We never generate extra formals if expansion is not active
+      --  because we don't need them unless we are generating code.
+
+      if not Expander_Active then
+         return;
+      end if;
+
+      --  If this is a derived subprogram then the subtypes of the parent
+      --  subprogram's formal parameters will be used to to determine the need
+      --  for extra formals.
 
       if Is_Overloadable (E) and then Present (Alias (E)) then
          P_Formal := First_Formal (Alias (E));
@@ -3190,9 +4883,9 @@ package body Sem_Ch6 is
          Next_Formal (Formal);
       end loop;
 
-      --  If Extra_formals where already created, don't do it again
-      --  This situation may arise for subprogram types created as part
-      --  of dispatching calls (see Expand_Dispatch_Call)
+      --  If Extra_formals were already created, don't do it again. This
+      --  situation may arise for subprogram types created as part of
+      --  dispatching calls (see Expand_Dispatching_Call)
 
       if Present (Last_Extra) and then
         Present (Extra_Formal (Last_Extra))
@@ -3200,8 +4893,18 @@ package body Sem_Ch6 is
          return;
       end if;
 
-      Formal := First_Formal (E);
+      --  If the subprogram is a predefined dispatching subprogram then don't
+      --  generate any extra constrained or accessibility level formals. In
+      --  general we suppress these for internal subprograms (by not calling
+      --  Freeze_Subprogram and Create_Extra_Formals at all), but internally
+      --  generated stream attributes do get passed through because extra
+      --  build-in-place formals are needed in some cases (limited 'Input).
+
+      if Is_Predefined_Dispatching_Operation (E) then
+         goto Test_For_BIP_Extras;
+      end if;
 
+      Formal := First_Formal (E);
       while Present (Formal) loop
 
          --  Create extra formal for supporting the attribute 'Constrained.
@@ -3216,6 +4919,13 @@ package body Sem_Ch6 is
                Formal_Type := Etype (Formal);
             end if;
 
+            --  Do not produce extra formals for Unchecked_Union parameters.
+            --  Jump directly to the end of the loop.
+
+            if Is_Unchecked_Union (Base_Type (Formal_Type)) then
+               goto Skip_Extra_Formal_Generation;
+            end if;
+
             if not Has_Discriminants (Formal_Type)
               and then Ekind (Formal_Type) in Private_Kind
               and then Present (Underlying_Type (Formal_Type))
@@ -3224,54 +4934,169 @@ package body Sem_Ch6 is
             end if;
 
             if Has_Discriminants (Formal_Type)
-              and then
-                ((not Is_Constrained (Formal_Type)
-                    and then not Is_Indefinite_Subtype (Formal_Type))
-                  or else Present (Extra_Formal (Formal)))
+              and then not Is_Constrained (Formal_Type)
+              and then not Is_Indefinite_Subtype (Formal_Type)
             then
                Set_Extra_Constrained
-                 (Formal, Add_Extra_Formal (Standard_Boolean));
+                 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "F"));
             end if;
          end if;
 
-         --  Create extra formal for supporting accessibility checking
+         --  Create extra formal for supporting accessibility checking. This
+         --  is done for both anonymous access formals and formals of named
+         --  access types that are marked as controlling formals. The latter
+         --  case can occur when Expand_Dispatching_Call creates a subprogram
+         --  type and substitutes the types of access-to-class-wide actuals
+         --  for the anonymous access-to-specific-type of controlling formals.
+         --  Base_Type is applied because in cases where there is a null
+         --  exclusion the formal may have an access subtype.
 
          --  This is suppressed if we specifically suppress accessibility
-         --  checks at the pacage level for either the subprogram, or the
+         --  checks at the package level for either the subprogram, or the
          --  package in which it resides. However, we do not suppress it
          --  simply if the scope has accessibility checks suppressed, since
          --  this could cause trouble when clients are compiled with a
          --  different suppression setting. The explicit checks at the
          --  package level are safe from this point of view.
 
-         if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
+         if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
+              or else (Is_Controlling_Formal (Formal)
+                        and then Is_Access_Type (Base_Type (Etype (Formal)))))
            and then not
              (Explicit_Suppress (E, Accessibility_Check)
                or else
               Explicit_Suppress (Scope (E), Accessibility_Check))
            and then
-             (not Present (P_Formal)
+             (No (P_Formal)
                or else Present (Extra_Accessibility (P_Formal)))
          then
-            --  Temporary kludge: for now we avoid creating the extra
-            --  formal for access parameters of protected operations
-            --  because of problem with the case of internal protected
-            --  calls. ???
+            --  Temporary kludge: for now we avoid creating the extra formal
+            --  for access parameters of protected operations because of
+            --  problem with the case of internal protected calls. ???
 
             if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
               and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
             then
                Set_Extra_Accessibility
-                 (Formal, Add_Extra_Formal (Standard_Natural));
+                 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "F"));
             end if;
          end if;
 
+         --  This label is required when skipping extra formal generation for
+         --  Unchecked_Union parameters.
+
+         <<Skip_Extra_Formal_Generation>>
+
          if Present (P_Formal) then
             Next_Formal (P_Formal);
          end if;
 
          Next_Formal (Formal);
       end loop;
+
+      <<Test_For_BIP_Extras>>
+
+      --  Ada 2005 (AI-318-02): In the case of build-in-place functions, add
+      --  appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
+
+      if Ada_Version >= Ada_05 and then Is_Build_In_Place_Function (E) then
+         declare
+            Result_Subt : constant Entity_Id := Etype (E);
+
+            Discard : Entity_Id;
+            pragma Warnings (Off, Discard);
+
+         begin
+            --  In the case of functions with unconstrained result subtypes,
+            --  add a 3-state formal indicating whether the return object is
+            --  allocated by the caller (0), or should be allocated by the
+            --  callee on the secondary stack (1) or in the global heap (2).
+            --  For the moment we just use Natural for the type of this formal.
+            --  Note that this formal isn't usually needed in the case where
+            --  the result subtype is constrained, but it is needed when the
+            --  function has a tagged result, because generally such functions
+            --  can be called in a dispatching context and such calls must be
+            --  handled like calls to a class-wide function.
+
+            if not Is_Constrained (Result_Subt)
+              or else Is_Tagged_Type (Underlying_Type (Result_Subt))
+            then
+               Discard :=
+                 Add_Extra_Formal
+                   (E, Standard_Natural,
+                    E, BIP_Formal_Suffix (BIP_Alloc_Form));
+            end if;
+
+            --  In the case of functions whose result type has controlled
+            --  parts, we have an extra formal of type
+            --  System.Finalization_Implementation.Finalizable_Ptr_Ptr. That
+            --  is, we are passing a pointer to a finalization list (which is
+            --  itself a pointer). This extra formal is then passed along to
+            --  Move_Final_List in case of successful completion of a return
+            --  statement. We cannot pass an 'in out' parameter, because we
+            --  need to update the finalization list during an abort-deferred
+            --  region, rather than using copy-back after the function
+            --  returns. This is true even if we are able to get away with
+            --  having 'in out' parameters, which are normally illegal for
+            --  functions. This formal is also needed when the function has
+            --  a tagged result, because generally such functions can be called
+            --  in a dispatching context and such calls must be handled like
+            --  calls to class-wide functions.
+
+            if Controlled_Type (Result_Subt)
+              or else Is_Tagged_Type (Underlying_Type (Result_Subt))
+            then
+               Discard :=
+                 Add_Extra_Formal
+                   (E, RTE (RE_Finalizable_Ptr_Ptr),
+                    E, BIP_Formal_Suffix (BIP_Final_List));
+            end if;
+
+            --  If the result type contains tasks, we have two extra formals:
+            --  the master of the tasks to be created, and the caller's
+            --  activation chain.
+
+            if Has_Task (Result_Subt) then
+               Discard :=
+                 Add_Extra_Formal
+                   (E, RTE (RE_Master_Id),
+                    E, BIP_Formal_Suffix (BIP_Master));
+               Discard :=
+                 Add_Extra_Formal
+                   (E, RTE (RE_Activation_Chain_Access),
+                    E, BIP_Formal_Suffix (BIP_Activation_Chain));
+            end if;
+
+            --  All build-in-place functions get an extra formal that will be
+            --  passed the address of the return object within the caller.
+
+            declare
+               Formal_Type : constant Entity_Id :=
+                               Create_Itype
+                                 (E_Anonymous_Access_Type, E,
+                                  Scope_Id => Scope (E));
+            begin
+               Set_Directly_Designated_Type (Formal_Type, Result_Subt);
+               Set_Etype (Formal_Type, Formal_Type);
+               Set_Depends_On_Private
+                 (Formal_Type, Has_Private_Component (Formal_Type));
+               Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
+               Set_Is_Access_Constant (Formal_Type, False);
+
+               --  Ada 2005 (AI-50217): Propagate the attribute that indicates
+               --  the designated type comes from the limited view (for
+               --  back-end purposes).
+
+               Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
+
+               Layout_Type (Formal_Type);
+
+               Discard :=
+                 Add_Extra_Formal
+                   (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
+            end;
+         end;
+      end if;
    end Create_Extra_Formals;
 
    -----------------------------
@@ -3316,8 +5141,8 @@ package body Sem_Ch6 is
       if Debug_Flag_E then
          Write_Str ("New overloaded entity chain: ");
          Write_Name (Chars (S));
-         E := S;
 
+         E := S;
          while Present (E) loop
             Write_Str (" "); Write_Int (Int (E));
             E := Homonym (E);
@@ -3339,8 +5164,10 @@ package body Sem_Ch6 is
 
             --  Warn unless genuine overloading
 
-            if (not Is_Overloadable (E))
-              or else Subtype_Conformant (E, S)
+            if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
+                  and then (Is_Immediately_Visible (E)
+                              or else
+                            Is_Potentially_Use_Visible (S))
             then
                Error_Msg_Sloc := Sloc (E);
                Error_Msg_N ("declaration of & hides one#?", S);
@@ -3361,7 +5188,6 @@ package body Sem_Ch6 is
 
    begin
       E := Current_Entity (Designator);
-
       while Present (E) loop
 
          --  We are looking for a matching spec. It must have the same scope,
@@ -3413,6 +5239,36 @@ package body Sem_Ch6 is
 
                   return E;
 
+               --  If E is an internal function with a controlling result
+               --  that was created for an operation inherited by a null
+               --  extension, it may be overridden by a body without a previous
+               --  spec (one more reason why these should be shunned). In that
+               --  case remove the generated body, because the current one is
+               --  the explicit overriding.
+
+               elsif Ekind (E) = E_Function
+                 and then Ada_Version >= Ada_05
+                 and then not Comes_From_Source (E)
+                 and then Has_Controlling_Result (E)
+                 and then Is_Null_Extension (Etype (E))
+                 and then Comes_From_Source (Spec)
+               then
+                  Set_Has_Completion (E, False);
+
+                  if Expander_Active then
+                     Remove
+                       (Unit_Declaration_Node
+                         (Corresponding_Body (Unit_Declaration_Node (E))));
+                     return E;
+
+                  --  If expansion is disabled, the wrapper function has not
+                  --  been generated, and this is the standard case of a late
+                  --  body overriding an inherited operation.
+
+                  else
+                     return Empty;
+                  end if;
+
                --  If body already exists, this is an error unless the
                --  previous declaration is the implicit declaration of
                --  a derived subprogram, or this is a spurious overloading
@@ -3436,10 +5292,9 @@ package body Sem_Ch6 is
               and then
                 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
               and then
-                Nkind (Parent (Unit_Declaration_Node (Designator)))
-                  = N_Compilation_Unit
+                Nkind (Parent (Unit_Declaration_Node (Designator))) =
+                                                             N_Compilation_Unit
             then
-
                --  Child units cannot be overloaded, so a conformance mismatch
                --  between body and a previous spec is an error.
 
@@ -3463,7 +5318,6 @@ package body Sem_Ch6 is
 
    function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
       Result : Boolean;
-
    begin
       Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
       return Result;
@@ -3475,8 +5329,7 @@ package body Sem_Ch6 is
 
    function Fully_Conformant_Expressions
      (Given_E1 : Node_Id;
-      Given_E2 : Node_Id)
-      return     Boolean
+      Given_E2 : Node_Id) return Boolean
    is
       E1 : constant Node_Id := Original_Node (Given_E1);
       E2 : constant Node_Id := Original_Node (Given_E2);
@@ -3493,7 +5346,7 @@ package body Sem_Ch6 is
       --  match explicit actuals with the same value.
 
       function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
-      --  Compare an operator node with a function call.
+      --  Compare an operator node with a function call
 
       ---------
       -- FCL --
@@ -3573,13 +5426,13 @@ package body Sem_Ch6 is
    begin
       --  Non-conformant if paren count does not match. Note: if some idiot
       --  complains that we don't do this right for more than 3 levels of
-      --  parentheses, they will be treated with the respect they deserve :-)
+      --  parentheses, they will be treated with the respect they deserve!
 
       if Paren_Count (E1) /= Paren_Count (E2) then
          return False;
 
-      --  If same entities are referenced, then they are conformant
-      --  even if they have different forms (RM 8.3.1(19-20)).
+      --  If same entities are referenced, then they are conformant even if
+      --  they have different forms (RM 8.3.1(19-20)).
 
       elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
          if Present (Entity (E1)) then
@@ -3705,7 +5558,7 @@ package body Sem_Ch6 is
                    and then FCE (Left_Opnd  (E1), Left_Opnd  (E2))
                    and then FCE (Right_Opnd (E1), Right_Opnd (E2));
 
-            when N_And_Then | N_Or_Else | N_In | N_Not_In =>
+            when N_And_Then | N_Or_Else | N_Membership_Test =>
                return
                  FCE (Left_Opnd  (E1), Left_Opnd  (E2))
                    and then
@@ -3849,18 +5702,21 @@ package body Sem_Ch6 is
 
    function Fully_Conformant_Discrete_Subtypes
      (Given_S1 : Node_Id;
-      Given_S2 : Node_Id)
-      return     Boolean
+      Given_S2 : Node_Id) return Boolean
    is
       S1 : constant Node_Id := Original_Node (Given_S1);
       S2 : constant Node_Id := Original_Node (Given_S2);
 
       function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
-      --  Special-case for a bound given by a discriminant, which in the
-      --  body is replaced with the discriminal of the enclosing type.
+      --  Special-case for a bound given by a discriminant, which in the body
+      --  is replaced with the discriminal of the enclosing type.
 
       function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
-      --  Check both bounds.
+      --  Check both bounds
+
+      -----------------------
+      -- Conforming_Bounds --
+      -----------------------
 
       function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
       begin
@@ -3875,6 +5731,10 @@ package body Sem_Ch6 is
          end if;
       end Conforming_Bounds;
 
+      -----------------------
+      -- Conforming_Ranges --
+      -----------------------
+
       function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
       begin
          return
@@ -3913,7 +5773,6 @@ package body Sem_Ch6 is
 
    procedure Install_Entity (E : Entity_Id) is
       Prev : constant Entity_Id := Current_Entity (E);
-
    begin
       Set_Is_Immediately_Visible (E);
       Set_Current_Entity (E);
@@ -3926,10 +5785,8 @@ package body Sem_Ch6 is
 
    procedure Install_Formals (Id : Entity_Id) is
       F : Entity_Id;
-
    begin
       F := First_Formal (Id);
-
       while Present (F) loop
          Install_Entity (F);
          Next_Formal (F);
@@ -3942,25 +5799,22 @@ package body Sem_Ch6 is
 
    function Is_Non_Overriding_Operation
      (Prev_E : Entity_Id;
-      New_E  : Entity_Id)
-      return Boolean
+      New_E  : Entity_Id) return Boolean
    is
       Formal : Entity_Id;
       F_Typ  : Entity_Id;
       G_Typ  : Entity_Id := Empty;
 
       function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
-      --  If F_Type is a derived type associated with a generic actual
-      --  subtype, then return its Generic_Parent_Type attribute, else
-      --  return Empty.
+      --  If F_Type is a derived type associated with a generic actual subtype,
+      --  then return its Generic_Parent_Type attribute, else return Empty.
 
       function Types_Correspond
         (P_Type : Entity_Id;
-         N_Type : Entity_Id)
-         return   Boolean;
-      --  Returns true if and only if the types (or designated types
-      --  in the case of anonymous access types) are the same or N_Type
-      --  is derived directly or indirectly from P_Type.
+         N_Type : Entity_Id) return Boolean;
+      --  Returns true if and only if the types (or designated types in the
+      --  case of anonymous access types) are the same or N_Type is derived
+      --  directly or indirectly from P_Type.
 
       -----------------------------
       -- Get_Generic_Parent_Type --
@@ -3974,11 +5828,11 @@ package body Sem_Ch6 is
          if Is_Derived_Type (F_Typ)
            and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
          then
-            --  The tree must be traversed to determine the parent
-            --  subtype in the generic unit, which unfortunately isn't
-            --  always available via semantic attributes. ???
-            --  (Note: The use of Original_Node is needed for cases
-            --  where a full derived type has been rewritten.)
+            --  The tree must be traversed to determine the parent subtype in
+            --  the generic unit, which unfortunately isn't always available
+            --  via semantic attributes. ??? (Note: The use of Original_Node
+            --  is needed for cases where a full derived type has been
+            --  rewritten.)
 
             Indic := Subtype_Indication
                        (Type_Definition (Original_Node (Parent (F_Typ))));
@@ -4005,8 +5859,7 @@ package body Sem_Ch6 is
 
       function Types_Correspond
         (P_Type : Entity_Id;
-         N_Type : Entity_Id)
-         return   Boolean
+         N_Type : Entity_Id) return Boolean
       is
          Prev_Type : Entity_Id := Base_Type (P_Type);
          New_Type  : Entity_Id := Base_Type (N_Type);
@@ -4037,10 +5890,10 @@ package body Sem_Ch6 is
    --  Start of processing for Is_Non_Overriding_Operation
 
    begin
-      --  In the case where both operations are implicit derived
-      --  subprograms then neither overrides the other. This can
-      --  only occur in certain obscure cases (e.g., derivation
-      --  from homographs created in a generic instantiation).
+      --  In the case where both operations are implicit derived subprograms
+      --  then neither overrides the other. This can only occur in certain
+      --  obscure cases (e.g., derivation from homographs created in a generic
+      --  instantiation).
 
       if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
          return True;
@@ -4051,8 +5904,8 @@ package body Sem_Ch6 is
         and then Comes_From_Source (New_E)
       then
          --  We examine the formals and result subtype of the inherited
-         --  operation, to determine whether their type is derived from
-         --  (the instance of) a generic type.
+         --  operation, to determine whether their type is derived from (the
+         --  instance of) a generic type.
 
          Formal := First_Formal (Prev_E);
 
@@ -4068,7 +5921,7 @@ package body Sem_Ch6 is
             Next_Formal (Formal);
          end loop;
 
-         if not Present (G_Typ) and then Ekind (Prev_E) = E_Function then
+         if No (G_Typ) and then Ekind (Prev_E) = E_Function then
             G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
          end if;
 
@@ -4120,12 +5973,12 @@ package body Sem_Ch6 is
                         Next_Entity (N_Formal);
                      end loop;
 
-                     --  Found a matching primitive operation belonging to
-                     --  the formal ancestor type, so the new subprogram
-                     --  is overriding.
+                     --  Found a matching primitive operation belonging to the
+                     --  formal ancestor type, so the new subprogram is
+                     --  overriding.
 
-                     if not Present (P_Formal)
-                       and then not Present (N_Formal)
+                     if No (P_Formal)
+                       and then No (N_Formal)
                        and then (Ekind (New_E) /= E_Function
                                   or else
                                  Types_Correspond
@@ -4138,8 +5991,8 @@ package body Sem_Ch6 is
                   Next_Elmt (Prim_Elt);
                end loop;
 
-               --  If no match found, then the new subprogram does
-               --  not override in the generic (nor in the instance).
+               --  If no match found, then the new subprogram does not
+               --  override in the generic (nor in the instance).
 
                return True;
             end;
@@ -4164,61 +6017,77 @@ package body Sem_Ch6 is
       Formals : List_Id;
       Op_Name : Entity_Id;
 
-      A : Entity_Id;
-      B : Entity_Id;
+      FF : constant Entity_Id := First_Formal (S);
+      NF : constant Entity_Id := Next_Formal (FF);
 
    begin
-      --  Check that equality was properly defined.
+      --  Check that equality was properly defined, ignore call if not
 
-      if  No (Next_Formal (First_Formal (S))) then
+      if No (NF) then
          return;
       end if;
 
-      A := Make_Defining_Identifier (Loc, Chars (First_Formal (S)));
-      B := Make_Defining_Identifier (Loc,
-             Chars (Next_Formal (First_Formal (S))));
-
-      Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
-
-      Formals := New_List (
-        Make_Parameter_Specification (Loc,
-          Defining_Identifier => A,
-          Parameter_Type =>
-            New_Reference_To (Etype (First_Formal (S)), Loc)),
-
-        Make_Parameter_Specification (Loc,
-          Defining_Identifier => B,
-          Parameter_Type =>
-            New_Reference_To (Etype (Next_Formal (First_Formal (S))), Loc)));
-
-      Decl :=
-        Make_Subprogram_Declaration (Loc,
-          Specification =>
-            Make_Function_Specification (Loc,
-              Defining_Unit_Name => Op_Name,
-              Parameter_Specifications => Formals,
-              Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)));
-
-      --  Insert inequality right after equality if it is explicit or after
-      --  the derived type when implicit. These entities are created only
-      --  for visibility purposes, and eventually replaced in the course of
-      --  expansion, so they do not need to be attached to the tree and seen
-      --  by the back-end. Keeping them internal also avoids spurious freezing
-      --  problems. The parent field is set simply to make analysis safe.
-
-      if No (Alias (S)) then
-         Set_Parent (Decl, Parent (Unit_Declaration_Node (S)));
-      else
-         Set_Parent (Decl, Parent (Parent (Etype (First_Formal (S)))));
-      end if;
+      declare
+         A : constant Entity_Id :=
+               Make_Defining_Identifier (Sloc (FF),
+                 Chars => Chars (FF));
+
+         B : constant Entity_Id :=
+               Make_Defining_Identifier (Sloc (NF),
+                 Chars => Chars (NF));
 
-      Mark_Rewrite_Insertion (Decl);
-      Set_Is_Intrinsic_Subprogram (Op_Name);
-      Analyze (Decl);
-      Set_Has_Completion (Op_Name);
-      Set_Corresponding_Equality (Op_Name, S);
-      Set_Is_Abstract (Op_Name, Is_Abstract (S));
+      begin
+         Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
+
+         Formals := New_List (
+           Make_Parameter_Specification (Loc,
+             Defining_Identifier => A,
+             Parameter_Type      =>
+               New_Reference_To (Etype (First_Formal (S)),
+                 Sloc (Etype (First_Formal (S))))),
+
+           Make_Parameter_Specification (Loc,
+             Defining_Identifier => B,
+             Parameter_Type      =>
+               New_Reference_To (Etype (Next_Formal (First_Formal (S))),
+                 Sloc (Etype (Next_Formal (First_Formal (S)))))));
+
+         Decl :=
+           Make_Subprogram_Declaration (Loc,
+             Specification =>
+               Make_Function_Specification (Loc,
+                 Defining_Unit_Name       => Op_Name,
+                 Parameter_Specifications => Formals,
+                 Result_Definition        =>
+                   New_Reference_To (Standard_Boolean, Loc)));
+
+         --  Insert inequality right after equality if it is explicit or after
+         --  the derived type when implicit. These entities are created only
+         --  for visibility purposes, and eventually replaced in the course of
+         --  expansion, so they do not need to be attached to the tree and seen
+         --  by the back-end. Keeping them internal also avoids spurious
+         --  freezing problems. The declaration is inserted in the tree for
+         --  analysis, and removed afterwards. If the equality operator comes
+         --  from an explicit declaration, attach the inequality immediately
+         --  after. Else the equality is inherited from a derived type
+         --  declaration, so insert inequality after that declaration.
+
+         if No (Alias (S)) then
+            Insert_After (Unit_Declaration_Node (S), Decl);
+         elsif Is_List_Member (Parent (S)) then
+            Insert_After (Parent (S), Decl);
+         else
+            Insert_After (Parent (Etype (First_Formal (S))), Decl);
+         end if;
 
+         Mark_Rewrite_Insertion (Decl);
+         Set_Is_Intrinsic_Subprogram (Op_Name);
+         Analyze (Decl);
+         Remove (Decl);
+         Set_Has_Completion (Op_Name);
+         Set_Corresponding_Equality (Op_Name, S);
+         Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
+      end;
    end Make_Inequality_Operator;
 
    ----------------------
@@ -4232,7 +6101,6 @@ package body Sem_Ch6 is
    begin
       F := First_Formal (Fun);
       B := True;
-
       while Present (F) loop
          if No (Default_Value (F)) then
             B := False;
@@ -4251,7 +6119,6 @@ package body Sem_Ch6 is
 
    function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
       Result : Boolean;
-
    begin
       Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
       return Result;
@@ -4265,11 +6132,36 @@ package body Sem_Ch6 is
      (S            : Entity_Id;
       Derived_Type : Entity_Id := Empty)
    is
+      Overridden_Subp : Entity_Id := Empty;
+      --  Set if the current scope has an operation that is type-conformant
+      --  with S, and becomes hidden by S.
+
+      Is_Primitive_Subp : Boolean;
+      --  Set to True if the new subprogram is primitive
+
       E : Entity_Id;
       --  Entity that S overrides
 
       Prev_Vis : Entity_Id := Empty;
-      --  Needs comment ???
+      --  Predecessor of E in Homonym chain
+
+      procedure Check_For_Primitive_Subprogram
+        (Is_Primitive  : out Boolean;
+         Is_Overriding : Boolean := False);
+      --  If the subprogram being analyzed is a primitive operation of the type
+      --  of a formal or result, set the Has_Primitive_Operations flag on the
+      --  type, and set Is_Primitive to True (otherwise set to False). Set the
+      --  corresponding flag on the entity itself for later use.
+
+      procedure Check_Synchronized_Overriding
+        (Def_Id          : Entity_Id;
+         First_Hom       : Entity_Id;
+         Overridden_Subp : out Entity_Id);
+      --  First determine if Def_Id is an entry or a subprogram either defined
+      --  in the scope of a task or protected type, or is a primitive of such
+      --  a type. Check whether Def_Id overrides a subprogram of an interface
+      --  implemented by the synchronized type, return the overridden entity
+      --  or Empty.
 
       function Is_Private_Declaration (E : Entity_Id) return Boolean;
       --  Check that E is declared in the private part of the current package,
@@ -4278,43 +6170,14 @@ package body Sem_Ch6 is
       --  set when freezing entities, so we must examine the place of the
       --  declaration in the tree, and recognize wrapper packages as well.
 
-      procedure Maybe_Primitive_Operation (Overriding : Boolean := False);
-      --  If the subprogram being analyzed is a primitive operation of
-      --  the type of one of its formals, set the corresponding flag.
-
-      ----------------------------
-      -- Is_Private_Declaration --
-      ----------------------------
-
-      function Is_Private_Declaration (E : Entity_Id) return Boolean is
-         Priv_Decls : List_Id;
-         Decl       : constant Node_Id := Unit_Declaration_Node (E);
-
-      begin
-         if Is_Package (Current_Scope)
-           and then In_Private_Part (Current_Scope)
-         then
-            Priv_Decls :=
-              Private_Declarations (
-                Specification (Unit_Declaration_Node (Current_Scope)));
-
-            return In_Package_Body (Current_Scope)
-              or else List_Containing (Decl) = Priv_Decls
-              or else (Nkind (Parent (Decl)) = N_Package_Specification
-                         and then not Is_Compilation_Unit (
-                           Defining_Entity (Parent (Decl)))
-                         and then List_Containing (Parent (Parent (Decl)))
-                           = Priv_Decls);
-         else
-            return False;
-         end if;
-      end Is_Private_Declaration;
-
-      -------------------------------
-      -- Maybe_Primitive_Operation --
-      -------------------------------
+      ------------------------------------
+      -- Check_For_Primitive_Subprogram --
+      ------------------------------------
 
-      procedure Maybe_Primitive_Operation (Overriding : Boolean := False) is
+      procedure Check_For_Primitive_Subprogram
+        (Is_Primitive  : out Boolean;
+         Is_Overriding : Boolean := False)
+      is
          Formal : Entity_Id;
          F_Typ  : Entity_Id;
          B_Typ  : Entity_Id;
@@ -4343,24 +6206,25 @@ package body Sem_Ch6 is
               and then Visible_Part_Type (T)
               and then not In_Instance
             then
-               if Is_Abstract (T)
-                 and then Is_Abstract (S)
-                 and then (not Overriding or else not Is_Abstract (E))
+               if Is_Abstract_Type (T)
+                 and then Is_Abstract_Subprogram (S)
+                 and then (not Is_Overriding
+                           or else not Is_Abstract_Subprogram (E))
                then
                   Error_Msg_N ("abstract subprograms must be visible "
-                                & "('R'M 3.9.3(10))!", S);
+                                   & "(RM 3.9.3(10))!", S);
 
                elsif Ekind (S) = E_Function
                  and then Is_Tagged_Type (T)
                  and then T = Base_Type (Etype (S))
-                 and then not Overriding
+                 and then not Is_Overriding
                then
                   Error_Msg_N
                     ("private function with tagged result must"
                      & " override visible-part function", S);
                   Error_Msg_N
                     ("\move subprogram to the visible part"
-                     & " ('R'M 3.9.3(10))", S);
+                     & " (RM 3.9.3(10))", S);
                end if;
             end if;
          end Check_Private_Overriding;
@@ -4395,9 +6259,8 @@ package body Sem_Ch6 is
                then
                   return True;
 
-               elsif (Nkind (N) = N_Private_Type_Declaration
-                       or else
-                      Nkind (N) = N_Private_Extension_Declaration)
+               elsif Nkind_In (N, N_Private_Type_Declaration,
+                                  N_Private_Extension_Declaration)
                  and then Present (Defining_Identifier (N))
                  and then T = Full_View (Defining_Identifier (N))
                then
@@ -4410,23 +6273,42 @@ package body Sem_Ch6 is
             return False;
          end Visible_Part_Type;
 
-      --  Start of processing for Maybe_Primitive_Operation
+      --  Start of processing for Check_For_Primitive_Subprogram
+
+      begin
+         Is_Primitive := False;
+
+         if not Comes_From_Source (S) then
+            null;
+
+         --  If subprogram is at library level, it is not primitive operation
 
-      begin
-         if not Comes_From_Source (S) then
+         elsif Current_Scope = Standard_Standard then
             null;
 
-         elsif (Ekind (Current_Scope) = E_Package
+         elsif ((Ekind (Current_Scope) = E_Package
+                  or else Ekind (Current_Scope) = E_Generic_Package)
                  and then not In_Package_Body (Current_Scope))
-           or else Overriding
+           or else Is_Overriding
          then
             --  For function, check return type
 
             if Ekind (S) = E_Function then
-               B_Typ := Base_Type (Etype (S));
+               if Ekind (Etype (S)) = E_Anonymous_Access_Type then
+                  F_Typ := Designated_Type (Etype (S));
+               else
+                  F_Typ := Etype (S);
+               end if;
 
-               if Scope (B_Typ) = Current_Scope then
+               B_Typ := Base_Type (F_Typ);
+
+               if Scope (B_Typ) = Current_Scope
+                 and then not Is_Class_Wide_Type (B_Typ)
+                 and then not Is_Generic_Type (B_Typ)
+               then
+                  Is_Primitive := True;
                   Set_Has_Primitive_Operations (B_Typ);
+                  Set_Is_Primitive (S);
                   Check_Private_Overriding (B_Typ);
                end if;
             end if;
@@ -4443,7 +6325,16 @@ package body Sem_Ch6 is
 
                B_Typ := Base_Type (F_Typ);
 
-               if Scope (B_Typ) = Current_Scope then
+               if Ekind (B_Typ) = E_Access_Subtype then
+                  B_Typ := Base_Type (B_Typ);
+               end if;
+
+               if Scope (B_Typ) = Current_Scope
+                 and then not Is_Class_Wide_Type (B_Typ)
+                 and then not Is_Generic_Type (B_Typ)
+               then
+                  Is_Primitive := True;
+                  Set_Is_Primitive (S);
                   Set_Has_Primitive_Operations (B_Typ);
                   Check_Private_Overriding (B_Typ);
                end if;
@@ -4451,7 +6342,123 @@ package body Sem_Ch6 is
                Next_Formal (Formal);
             end loop;
          end if;
-      end Maybe_Primitive_Operation;
+      end Check_For_Primitive_Subprogram;
+
+      -----------------------------------
+      -- Check_Synchronized_Overriding --
+      -----------------------------------
+
+      procedure Check_Synchronized_Overriding
+        (Def_Id          : Entity_Id;
+         First_Hom       : Entity_Id;
+         Overridden_Subp : out Entity_Id)
+      is
+         Formal_Typ  : Entity_Id;
+         Ifaces_List : Elist_Id;
+         In_Scope    : Boolean;
+         Typ         : Entity_Id;
+
+      begin
+         Overridden_Subp := Empty;
+
+         --  Def_Id must be an entry or a subprogram
+
+         if Ekind (Def_Id) /= E_Entry
+           and then Ekind (Def_Id) /= E_Function
+           and then Ekind (Def_Id) /= E_Procedure
+         then
+            return;
+         end if;
+
+         --  Search for the concurrent declaration since it contains the list
+         --  of all implemented interfaces. In this case, the subprogram is
+         --  declared within the scope of a protected or a task type.
+
+         if Present (Scope (Def_Id))
+           and then Is_Concurrent_Type (Scope (Def_Id))
+           and then not Is_Generic_Actual_Type (Scope (Def_Id))
+         then
+            Typ := Scope (Def_Id);
+            In_Scope := True;
+
+         --  The subprogram may be a primitive of a concurrent type
+
+         elsif Present (First_Formal (Def_Id)) then
+            Formal_Typ := Etype (First_Formal (Def_Id));
+
+            if Is_Concurrent_Type (Formal_Typ)
+              and then not Is_Generic_Actual_Type (Formal_Typ)
+            then
+               Typ := Formal_Typ;
+               In_Scope := False;
+
+            --  This case occurs when the concurrent type is declared within
+            --  a generic unit. As a result the corresponding record has been
+            --  built and used as the type of the first formal, we just have
+            --  to retrieve the corresponding concurrent type.
+
+            elsif Is_Concurrent_Record_Type (Formal_Typ)
+              and then Present (Corresponding_Concurrent_Type (Formal_Typ))
+            then
+               Typ := Corresponding_Concurrent_Type (Formal_Typ);
+               In_Scope := False;
+
+            else
+               return;
+            end if;
+         else
+            return;
+         end if;
+
+         --  Gather all limited, protected and task interfaces that Typ
+         --  implements. There is no overriding to check if is an inherited
+         --  operation in a type derivation on for a generic actual.
+
+         if Nkind (Parent (Typ)) /= N_Full_Type_Declaration
+           and then
+             not Nkind_In (Parent (Def_Id), N_Subtype_Declaration,
+                                            N_Task_Type_Declaration,
+                                            N_Protected_Type_Declaration)
+         then
+            Collect_Abstract_Interfaces (Typ, Ifaces_List);
+
+            if not Is_Empty_Elmt_List (Ifaces_List) then
+               Overridden_Subp :=
+                 Find_Overridden_Synchronized_Primitive
+                   (Def_Id, First_Hom, Ifaces_List, In_Scope);
+            end if;
+         end if;
+      end Check_Synchronized_Overriding;
+
+      ----------------------------
+      -- Is_Private_Declaration --
+      ----------------------------
+
+      function Is_Private_Declaration (E : Entity_Id) return Boolean is
+         Priv_Decls : List_Id;
+         Decl       : constant Node_Id := Unit_Declaration_Node (E);
+
+      begin
+         if Is_Package_Or_Generic_Package (Current_Scope)
+           and then In_Private_Part (Current_Scope)
+         then
+            Priv_Decls :=
+              Private_Declarations (
+                Specification (Unit_Declaration_Node (Current_Scope)));
+
+            return In_Package_Body (Current_Scope)
+              or else
+                (Is_List_Member (Decl)
+                   and then List_Containing (Decl) = Priv_Decls)
+              or else (Nkind (Parent (Decl)) = N_Package_Specification
+                         and then not Is_Compilation_Unit (
+                           Defining_Entity (Parent (Decl)))
+                         and then List_Containing (Parent (Parent (Decl)))
+                           = Priv_Decls);
+         else
+            return False;
+         end if;
+      end Is_Private_Declaration;
 
    --  Start of processing for New_Overloaded_Entity
 
@@ -4467,7 +6474,16 @@ package body Sem_Ch6 is
       if No (E) then
          Enter_Overloaded_Entity (S);
          Check_Dispatching_Operation (S, Empty);
-         Maybe_Primitive_Operation;
+         Check_For_Primitive_Subprogram (Is_Primitive_Subp);
+
+         --  If subprogram has an explicit declaration, check whether it
+         --  has an overriding indicator.
+
+         if Comes_From_Source (S) then
+            Check_Synchronized_Overriding (S, Homonym (S), Overridden_Subp);
+            Check_Overriding_Indicator
+              (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
+         end if;
 
       --  If there is a homonym that is not overloadable, then we have an
       --  error, except for the special cases checked explicitly below.
@@ -4492,11 +6508,12 @@ package body Sem_Ch6 is
             Enter_Overloaded_Entity (S);
             Set_Homonym (S, Homonym (E));
             Check_Dispatching_Operation (S, Empty);
+            Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
 
          --  If the subprogram is implicit it is hidden by the previous
-         --  declaration. However if it is dispatching, it must appear in
-         --  the dispatch table anyway, because it can be dispatched to
-         --  even if it cannot be called directly.
+         --  declaration. However if it is dispatching, it must appear in the
+         --  dispatch table anyway, because it can be dispatched to even if it
+         --  cannot be called directly.
 
          elsif Present (Alias (S))
            and then not Comes_From_Source (S)
@@ -4511,12 +6528,14 @@ package body Sem_Ch6 is
 
          else
             Error_Msg_Sloc := Sloc (E);
-            Error_Msg_N ("& conflicts with declaration#", S);
 
-            --  Useful additional warning
+            --  Generate message,with useful additionalwarning if in generic
 
             if Is_Generic_Unit (E) then
-               Error_Msg_N ("\previous generic unit cannot be overloaded", S);
+               Error_Msg_N ("previous generic unit cannot be overloaded", S);
+               Error_Msg_N ("\& conflicts with declaration#", S);
+            else
+               Error_Msg_N ("& conflicts with declaration#", S);
             end if;
 
             return;
@@ -4525,8 +6544,24 @@ package body Sem_Ch6 is
       --  E exists and is overloadable
 
       else
-         --  Loop through E and its homonyms to determine if any of them
-         --  is the candidate for overriding by S.
+         --  Ada 2005 (AI-251): Derivation of abstract interface primitives
+         --  need no check against the homonym chain. They are directly added
+         --  to the list of primitive operations of Derived_Type.
+
+         if Ada_Version >= Ada_05
+           and then Present (Derived_Type)
+           and then Is_Dispatching_Operation (Alias (S))
+           and then Present (Find_Dispatching_Type (Alias (S)))
+           and then Is_Interface (Find_Dispatching_Type (Alias (S)))
+           and then not Is_Predefined_Dispatching_Operation (Alias (S))
+         then
+            goto Add_New_Entity;
+         end if;
+
+         Check_Synchronized_Overriding (S, E, Overridden_Subp);
+
+         --  Loop through E and its homonyms to determine if any of them is
+         --  the candidate for overriding by S.
 
          while Present (E) loop
 
@@ -4539,25 +6574,25 @@ package body Sem_Ch6 is
 
             elsif Type_Conformant (E, S) then
 
-               --  If the old and new entities have the same profile and
-               --  one is not the body of the other, then this is an error,
-               --  unless one of them is implicitly declared.
+               --  If the old and new entities have the same profile and one
+               --  is not the body of the other, then this is an error, unless
+               --  one of them is implicitly declared.
 
                --  There are some cases when both can be implicit, for example
                --  when both a literal and a function that overrides it are
                --  inherited in a derivation, or when an inhertited operation
-               --  of a tagged full type overrides the ineherited operation of
-               --  a private extension. Ada 83 had a special rule for the
-               --  the literal case. In Ada95, the later implicit operation
-               --  hides the former, and the literal is always the former.
-               --  In the odd case where both are derived operations declared
-               --  at the same point, both operations should be declared,
-               --  and in that case we bypass the following test and proceed
-               --  to the next part (this can only occur for certain obscure
-               --  cases involving homographs in instances and can't occur for
+               --  of a tagged full type overrides the inherited operation of
+               --  a private extension. Ada 83 had a special rule for the the
+               --  literal case. In Ada95, the later implicit operation hides
+               --  the former, and the literal is always the former. In the
+               --  odd case where both are derived operations declared at the
+               --  same point, both operations should be declared, and in that
+               --  case we bypass the following test and proceed to the next
+               --  part (this can only occur for certain obscure cases
+               --  involving homographs in instances and can't occur for
                --  dispatching operations ???). Note that the following
-               --  condition is less than clear. For example, it's not at
-               --  all clear why there's a test for E_Entry here. ???
+               --  condition is less than clear. For example, it's not at all
+               --  clear why there's a test for E_Entry here. ???
 
                if Present (Alias (S))
                  and then (No (Alias (E))
@@ -4567,18 +6602,33 @@ package body Sem_Ch6 is
                    (Ekind (E) = E_Entry
                      or else Ekind (E) /= E_Enumeration_Literal)
                then
-                  --  When an derived operation is overloaded it may be due
-                  --  to the fact that the full view of a private extension
+                  --  When an derived operation is overloaded it may be due to
+                  --  the fact that the full view of a private extension
                   --  re-inherits. It has to be dealt with.
 
-                  if Is_Package (Current_Scope)
+                  if Is_Package_Or_Generic_Package (Current_Scope)
                     and then In_Private_Part (Current_Scope)
                   then
                      Check_Operation_From_Private_View (S, E);
                   end if;
 
                   --  In any case the implicit operation remains hidden by
-                  --  the existing declaration.
+                  --  the existing declaration, which is overriding.
+
+                  Set_Is_Overriding_Operation (E);
+
+                  if Comes_From_Source (E) then
+                     Check_Overriding_Indicator (E, S, Is_Primitive => False);
+
+                     --  Indicate that E overrides the operation from which
+                     --  S is inherited.
+
+                     if Present (Alias (S)) then
+                        Set_Overridden_Operation (E, Alias (S));
+                     else
+                        Set_Overridden_Operation (E, S);
+                     end if;
+                  end if;
 
                   return;
 
@@ -4608,7 +6658,7 @@ package body Sem_Ch6 is
 
                   if Is_Non_Overriding_Operation (E, S) then
                      Enter_Overloaded_Entity (S);
-                     if not Present (Derived_Type)
+                     if No (Derived_Type)
                        or else Is_Tagged_Type (Derived_Type)
                      then
                         Check_Dispatching_Operation (S, Empty);
@@ -4623,6 +6673,8 @@ package body Sem_Ch6 is
                   --  replaced in the list of primitive operations of its type
                   --  (see Override_Dispatching_Operation).
 
+                  Overridden_Subp := E;
+
                   declare
                      Prev : Entity_Id;
 
@@ -4664,21 +6716,21 @@ package body Sem_Ch6 is
                      then
                         --  For nondispatching derived operations that are
                         --  overridden by a subprogram declared in the private
-                        --  part of a package, we retain the derived subprogram
-                        --  but mark it as not immediately visible. If the
-                        --  derived operation was declared in the visible part
-                        --  then this ensures that it will still be visible
-                        --  outside the package with the proper signature
-                        --  (calls from outside must also be directed to this
-                        --  version rather than the overriding one, unlike the
-                        --  dispatching case). Calls from inside the package
-                        --  will still resolve to the overriding subprogram
-                        --  since the derived one is marked as not visible
-                        --  within the package.
+                        --  part of a package, we retain the derived
+                        --  subprogram but mark it as not immediately visible.
+                        --  If the derived operation was declared in the
+                        --  visible part then this ensures that it will still
+                        --  be visible outside the package with the proper
+                        --  signature (calls from outside must also be
+                        --  directed to this version rather than the
+                        --  overriding one, unlike the dispatching case).
+                        --  Calls from inside the package will still resolve
+                        --  to the overriding subprogram since the derived one
+                        --  is marked as not visible within the package.
 
                         --  If the private operation is dispatching, we achieve
                         --  the overriding by keeping the implicit operation
-                        --  but setting its alias to be the overring one. In
+                        --  but setting its alias to be the overriding one. In
                         --  this fashion the proper body is executed in all
                         --  cases, but the original signature is used outside
                         --  of the package.
@@ -4691,8 +6743,8 @@ package body Sem_Ch6 is
                            if not Is_Dispatching_Operation (E) then
                               Set_Is_Immediately_Visible (E, False);
                            else
-
-                              --  work done in Override_Dispatching_Operation.
+                              --  Work done in Override_Dispatching_Operation,
+                              --  so nothing else need to be done here.
 
                               null;
                            end if;
@@ -4730,21 +6782,34 @@ package body Sem_Ch6 is
 
                      Enter_Overloaded_Entity (S);
                      Set_Is_Overriding_Operation (S);
+                     Check_Overriding_Indicator (S, E, Is_Primitive => True);
+
+                     --  Indicate that S overrides the operation from which
+                     --  E is inherited.
+
+                     if Comes_From_Source (S) then
+                        if Present (Alias (E)) then
+                           Set_Overridden_Operation (S, Alias (E));
+                        else
+                           Set_Overridden_Operation (S, E);
+                        end if;
+                     end if;
 
                      if Is_Dispatching_Operation (E) then
 
-                        --  An overriding dispatching subprogram inherits
-                        --  the convention of the overridden subprogram
-                        --  (by AI-117).
+                        --  An overriding dispatching subprogram inherits the
+                        --  convention of the overridden subprogram (by
+                        --  AI-117).
 
                         Set_Convention (S, Convention (E));
-
                         Check_Dispatching_Operation (S, E);
+
                      else
                         Check_Dispatching_Operation (S, Empty);
                      end if;
 
-                     Maybe_Primitive_Operation (Overriding => True);
+                     Check_For_Primitive_Subprogram
+                       (Is_Primitive_Subp, Is_Overriding => True);
                      goto Check_Inequality;
                   end;
 
@@ -4772,48 +6837,95 @@ package body Sem_Ch6 is
 
                   Set_Scope (S, Current_Scope);
 
-                  Error_Msg_N ("& conflicts with declaration#", S);
+                  --  Generate error, with extra useful warning for the case
+                  --  of a generic instance with no completion.
 
                   if Is_Generic_Instance (S)
                     and then not Has_Completion (E)
                   then
                      Error_Msg_N
-                       ("\instantiation cannot provide body for it", S);
+                       ("instantiation cannot provide body for&", S);
+                     Error_Msg_N ("\& conflicts with declaration#", S);
+                  else
+                     Error_Msg_N ("& conflicts with declaration#", S);
                   end if;
 
                   return;
                end if;
 
             else
-               null;
+               --  If one subprogram has an access parameter and the other
+               --  a parameter of an access type, calls to either might be
+               --  ambiguous. Verify that parameters match except for the
+               --  access parameter.
+
+               if May_Hide_Profile then
+                  declare
+                     F1 : Entity_Id;
+                     F2 : Entity_Id;
+                  begin
+                     F1 := First_Formal (S);
+                     F2 := First_Formal (E);
+                     while Present (F1) and then Present (F2) loop
+                        if Is_Access_Type (Etype (F1)) then
+                           if not Is_Access_Type (Etype (F2))
+                              or else not Conforming_Types
+                                (Designated_Type (Etype (F1)),
+                                 Designated_Type (Etype (F2)),
+                                 Type_Conformant)
+                           then
+                              May_Hide_Profile := False;
+                           end if;
+
+                        elsif
+                          not Conforming_Types
+                            (Etype (F1), Etype (F2), Type_Conformant)
+                        then
+                           May_Hide_Profile := False;
+                        end if;
+
+                        Next_Formal (F1);
+                        Next_Formal (F2);
+                     end loop;
+
+                     if May_Hide_Profile
+                       and then No (F1)
+                       and then No (F2)
+                     then
+                        Error_Msg_NE ("calls to& may be ambiguous?", S, S);
+                     end if;
+                  end;
+               end if;
             end if;
 
-            Prev_Vis := E;
             E := Homonym (E);
          end loop;
 
+         <<Add_New_Entity>>
+
          --  On exit, we know that S is a new entity
 
          Enter_Overloaded_Entity (S);
-         Maybe_Primitive_Operation;
+         Check_For_Primitive_Subprogram (Is_Primitive_Subp);
+         Check_Overriding_Indicator
+           (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
 
-         --  If S is a derived operation for an untagged type then
-         --  by definition it's not a dispatching operation (even
-         --  if the parent operation was dispatching), so we don't
-         --  call Check_Dispatching_Operation in that case.
+         --  If S is a derived operation for an untagged type then by
+         --  definition it's not a dispatching operation (even if the parent
+         --  operation was dispatching), so we don't call
+         --  Check_Dispatching_Operation in that case.
 
-         if not Present (Derived_Type)
+         if No (Derived_Type)
            or else Is_Tagged_Type (Derived_Type)
          then
             Check_Dispatching_Operation (S, Empty);
          end if;
       end if;
 
-      --  If this is a  user-defined equality operator that is not
-      --  a derived subprogram, create the corresponding inequality.
-      --  If the operation is dispatching, the expansion is done
-      --  elsewhere, and we do not create an explicit inequality
-      --  operation.
+      --  If this is a user-defined equality operator that is not a derived
+      --  subprogram, create the corresponding inequality. If the operation is
+      --  dispatching, the expansion is done elsewhere, and we do not create
+      --  an explicit inequality operation.
 
       <<Check_Inequality>>
          if Chars (S) = Name_Op_Eq
@@ -4839,10 +6951,14 @@ package body Sem_Ch6 is
       Default     : Node_Id;
       Ptype       : Entity_Id;
 
+      Num_Out_Params  : Nat       := 0;
+      First_Out_Param : Entity_Id := Empty;
+      --  Used for setting Is_Only_Out_Parameter
+
       function Is_Class_Wide_Default (D : Node_Id) return Boolean;
-      --  Check whether the default has a class-wide type. After analysis
-      --  the default has the type of the formal, so we must also check
-      --  explicitly for an access attribute.
+      --  Check whether the default has a class-wide type. After analysis the
+      --  default has the type of the formal, so we must also check explicitly
+      --  for an access attribute.
 
       ---------------------------
       -- Is_Class_Wide_Default --
@@ -4864,10 +6980,9 @@ package body Sem_Ch6 is
       --  analyzed. The Ekind is established in a separate loop at the end.
 
       Param_Spec := First (T);
-
       while Present (Param_Spec) loop
-
          Formal := Defining_Identifier (Param_Spec);
+         Set_Never_Set_In_Source (Formal, True);
          Enter_Name (Formal);
 
          --  Case of ordinary parameters
@@ -4882,24 +6997,33 @@ package body Sem_Ch6 is
 
             Formal_Type := Entity (Ptype);
 
-            if Ekind (Formal_Type) = E_Incomplete_Type
-              or else (Is_Class_Wide_Type (Formal_Type)
-                        and then Ekind (Root_Type (Formal_Type)) =
-                                                         E_Incomplete_Type)
+            if Is_Incomplete_Type (Formal_Type)
+              or else
+               (Is_Class_Wide_Type (Formal_Type)
+                  and then Is_Incomplete_Type (Root_Type (Formal_Type)))
             then
+               --  Ada 2005 (AI-326): Tagged incomplete types allowed
 
-               --  Incomplete tagged types that are made visible through
-               --  a limited with_clause are valid formal types.
+               if Is_Tagged_Type (Formal_Type) then
+                  null;
 
-               if From_With_Type (Formal_Type)
-                 and then Is_Tagged_Type (Formal_Type)
-               then
+               --  Special handling of Value_Type for CIL case
+
+               elsif Is_Value_Type (Formal_Type) then
                   null;
 
-               elsif Nkind (Parent (T)) /= N_Access_Function_Definition
-                 and then Nkind (Parent (T)) /= N_Access_Procedure_Definition
+               elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
+                                               N_Access_Procedure_Definition)
                then
                   Error_Msg_N ("invalid use of incomplete type", Param_Spec);
+
+               --  An incomplete type that is not tagged is allowed in an
+               --  access-to-subprogram type only if it is a local declaration
+               --  with a forthcoming completion (3.10.1 (9.2/2)).
+
+               elsif Scope (Formal_Type) /= Scope (Current_Scope) then
+                  Error_Msg_N
+                    ("invalid use of limited view of type", Param_Spec);
                end if;
 
             elsif Ekind (Formal_Type) = E_Void then
@@ -4907,15 +7031,76 @@ package body Sem_Ch6 is
                  Parameter_Type (Param_Spec), Formal_Type);
             end if;
 
+            --  Ada 2005 (AI-231): Create and decorate an internal subtype
+            --  declaration corresponding to the null-excluding type of the
+            --  formal in the enclosing scope. Finally, replace the parameter
+            --  type of the formal with the internal subtype.
+
+            if Ada_Version >= Ada_05
+              and then Null_Exclusion_Present (Param_Spec)
+            then
+               if not Is_Access_Type (Formal_Type) then
+                  Error_Msg_N
+                    ("`NOT NULL` allowed only for an access type", Param_Spec);
+
+               else
+                  if Can_Never_Be_Null (Formal_Type)
+                    and then Comes_From_Source (Related_Nod)
+                  then
+                     Error_Msg_NE
+                       ("`NOT NULL` not allowed (& already excludes null)",
+                        Param_Spec,
+                        Formal_Type);
+                  end if;
+
+                  Formal_Type :=
+                    Create_Null_Excluding_Itype
+                      (T           => Formal_Type,
+                       Related_Nod => Related_Nod,
+                       Scope_Id    => Scope (Current_Scope));
+
+                  --  If the designated type of the itype is an itype we
+                  --  decorate it with the Has_Delayed_Freeze attribute to
+                  --  avoid problems with the backend.
+
+                  --  Example:
+                  --     type T is access procedure;
+                  --     procedure Op (O : not null T);
+
+                  if Is_Itype (Directly_Designated_Type (Formal_Type)) then
+                     Set_Has_Delayed_Freeze (Formal_Type);
+                  end if;
+               end if;
+            end if;
+
          --  An access formal type
 
          else
             Formal_Type :=
               Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
+
+            --  No need to continue if we already notified errors
+
+            if not Present (Formal_Type) then
+               return;
+            end if;
+
+            --  Ada 2005 (AI-254)
+
+            declare
+               AD : constant Node_Id :=
+                      Access_To_Subprogram_Definition
+                        (Parameter_Type (Param_Spec));
+            begin
+               if Present (AD) and then Protected_Present (AD) then
+                  Formal_Type :=
+                    Replace_Anonymous_Access_To_Protected_Subprogram
+                      (Param_Spec);
+               end if;
+            end;
          end if;
 
          Set_Etype (Formal, Formal_Type);
-
          Default := Expression (Param_Spec);
 
          if Present (Default) then
@@ -4928,13 +7113,27 @@ package body Sem_Ch6 is
             --  Do the special preanalysis of the expression (see section on
             --  "Handling of Default Expressions" in the spec of package Sem).
 
-            Analyze_Per_Use_Expression (Default, Formal_Type);
+            Preanalyze_Spec_Expression (Default, Formal_Type);
+
+            --  An access to constant cannot be the default for
+            --  an access parameter that is an access to variable.
+
+            if Ekind (Formal_Type) = E_Anonymous_Access_Type
+              and then not Is_Access_Constant (Formal_Type)
+              and then Is_Access_Type (Etype (Default))
+              and then Is_Access_Constant (Etype (Default))
+            then
+               Error_Msg_N
+                 ("formal that is access to variable cannot be initialized " &
+                    "with an access-to-constant expression", Default);
+            end if;
 
-            --  Check that the designated type of an access parameter's
-            --  default is not a class-wide type unless the parameter's
-            --  designated type is also class-wide.
+            --  Check that the designated type of an access parameter's default
+            --  is not a class-wide type unless the parameter's designated type
+            --  is also class-wide.
 
             if Ekind (Formal_Type) = E_Anonymous_Access_Type
+              and then not From_With_Type (Formal_Type)
               and then Is_Class_Wide_Default (Default)
               and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
             then
@@ -4943,10 +7142,27 @@ package body Sem_Ch6 is
             end if;
          end if;
 
+         --  Ada 2005 (AI-231): Static checks
+
+         if Ada_Version >= Ada_05
+           and then Is_Access_Type (Etype (Formal))
+           and then Can_Never_Be_Null (Etype (Formal))
+         then
+            Null_Exclusion_Static_Checks (Param_Spec);
+         end if;
+
       <<Continue>>
          Next (Param_Spec);
       end loop;
 
+      --  If this is the formal part of a function specification, analyze the
+      --  subtype mark in the context where the formals are visible but not
+      --  yet usable, and may hide outer homographs.
+
+      if Nkind (Related_Nod) = N_Function_Specification then
+         Analyze_Return_Type (Related_Nod);
+      end if;
+
       --  Now set the kind (mode) of each formal
 
       Param_Spec := First (T);
@@ -4974,15 +7190,214 @@ package body Sem_Ch6 is
 
                   Apply_Scalar_Range_Check (Default, Formal_Type);
                end if;
+            end if;
+
+         elsif Ekind (Formal) = E_Out_Parameter then
+            Num_Out_Params := Num_Out_Params + 1;
 
+            if Num_Out_Params = 1 then
+               First_Out_Param := Formal;
             end if;
+
+         elsif Ekind (Formal) = E_In_Out_Parameter then
+            Num_Out_Params := Num_Out_Params + 1;
          end if;
 
          Next (Param_Spec);
       end loop;
 
+      if Present (First_Out_Param) and then Num_Out_Params = 1 then
+         Set_Is_Only_Out_Parameter (First_Out_Param);
+      end if;
    end Process_Formals;
 
+   ------------------
+   -- Process_PPCs --
+   ------------------
+
+   procedure Process_PPCs
+     (N       : Node_Id;
+      Spec_Id : Entity_Id;
+      Body_Id : Entity_Id)
+   is
+      Loc   : constant Source_Ptr := Sloc (N);
+      Prag  : Node_Id;
+      Plist : List_Id := No_List;
+      Subp  : Entity_Id;
+      Parms : List_Id;
+
+      function Grab_PPC (Nam : Name_Id) return Node_Id;
+      --  Prag contains an analyzed precondition or postcondition pragma.
+      --  This function copies the pragma, changes it to the corresponding
+      --  Check pragma and returns the Check pragma as the result. The
+      --  argument Nam is either Name_Precondition or Name_Postcondition.
+
+      --------------
+      -- Grab_PPC --
+      --------------
+
+      function Grab_PPC (Nam : Name_Id) return Node_Id is
+         CP : constant Node_Id := New_Copy_Tree (Prag);
+
+      begin
+         --  Set Analyzed to false, since we want to reanalyze the check
+         --  procedure. Note that it is only at the outer level that we
+         --  do this fiddling, for the spec cases, the already preanalyzed
+         --  parameters are not affected.
+
+         Set_Analyzed (CP, False);
+
+         --  Change pragma into corresponding pragma Check
+
+         Prepend_To (Pragma_Argument_Associations (CP),
+           Make_Pragma_Argument_Association (Sloc (Prag),
+             Expression =>
+               Make_Identifier (Loc,
+                 Chars => Nam)));
+         Set_Pragma_Identifier (CP,
+           Make_Identifier (Sloc (Prag),
+             Chars => Name_Check));
+
+         return CP;
+      end Grab_PPC;
+
+   --  Start of processing for Process_PPCs
+
+   begin
+      --  Grab preconditions from spec
+
+      if Present (Spec_Id) then
+
+         --  Loop through PPC pragmas from spec. Note that preconditions from
+         --  the body will be analyzed and converted when we scan the body
+         --  declarations below.
+
+         Prag := Spec_PPC_List (Spec_Id);
+         while Present (Prag) loop
+            if Pragma_Name (Prag) = Name_Precondition
+              and then PPC_Enabled (Prag)
+            then
+               --  Add pragma Check at the start of the declarations of N.
+               --  Note that this processing reverses the order of the list,
+               --  which is what we want since new entries were chained to
+               --  the head of the list.
+
+               Prepend (Grab_PPC (Name_Precondition), Declarations (N));
+            end if;
+
+            Prag := Next_Pragma (Prag);
+         end loop;
+      end if;
+
+      --  Build postconditions procedure if needed and prepend the following
+      --  declaration to the start of the declarations for the subprogram.
+
+      --     procedure _postconditions [(_Result : resulttype)] is
+      --     begin
+      --        pragma Check (Postcondition, condition [,message]);
+      --        pragma Check (Postcondition, condition [,message]);
+      --        ...
+      --     end;
+
+      --  First we deal with the postconditions in the body
+
+      if Is_Non_Empty_List (Declarations (N)) then
+
+         --  Loop through declarations
+
+         Prag := First (Declarations (N));
+         while Present (Prag) loop
+            if Nkind (Prag) = N_Pragma then
+
+               --  If pragma, capture if enabled postcondition, else ignore
+
+               if Pragma_Name (Prag) = Name_Postcondition
+                 and then Check_Enabled (Name_Postcondition)
+               then
+                  if Plist = No_List then
+                     Plist := Empty_List;
+                  end if;
+
+                  Analyze (Prag);
+                  Append (Grab_PPC (Name_Postcondition), Plist);
+               end if;
+
+               Next (Prag);
+
+               --  Not a pragma, if comes from source, then end scan
+
+            elsif Comes_From_Source (Prag) then
+               exit;
+
+               --  Skip stuff not coming from source
+
+            else
+               Next (Prag);
+            end if;
+         end loop;
+      end if;
+
+      --  Now deal with any postconditions from the spec
+
+      if Present (Spec_Id) then
+
+         --  Loop through PPC pragmas from spec
+
+         Prag := Spec_PPC_List (Spec_Id);
+         while Present (Prag) loop
+            if Pragma_Name (Prag) = Name_Postcondition
+              and then PPC_Enabled (Prag)
+            then
+               if Plist = No_List then
+                  Plist := Empty_List;
+               end if;
+
+               Append (Grab_PPC (Name_Postcondition), Plist);
+            end if;
+
+            Prag := Next_Pragma (Prag);
+         end loop;
+      end if;
+
+      --  If we had any postconditions, build the procedure
+
+      if Present (Plist) then
+         Subp := Defining_Entity (N);
+
+         if Etype (Subp) /= Standard_Void_Type then
+            Parms := New_List (
+              Make_Parameter_Specification (Loc,
+                Defining_Identifier =>
+                  Make_Defining_Identifier (Loc,
+                    Chars => Name_uResult),
+                Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
+         else
+            Parms := No_List;
+         end if;
+
+         Prepend_To (Declarations (N),
+           Make_Subprogram_Body (Loc,
+             Specification =>
+               Make_Procedure_Specification (Loc,
+                 Defining_Unit_Name =>
+                   Make_Defining_Identifier (Loc,
+                     Chars => Name_uPostconditions),
+                 Parameter_Specifications => Parms),
+
+             Declarations => Empty_List,
+
+             Handled_Statement_Sequence =>
+               Make_Handled_Sequence_Of_Statements (Loc,
+                 Statements => Plist)));
+
+         if Present (Spec_Id) then
+            Set_Has_Postconditions (Spec_Id);
+         else
+            Set_Has_Postconditions (Body_Id);
+         end if;
+      end if;
+   end Process_PPCs;
+
    ----------------------------
    -- Reference_Body_Formals --
    ----------------------------
@@ -4996,10 +7411,12 @@ package body Sem_Ch6 is
          return;
       end if;
 
+      --  Iterate over both lists. They may be of different lengths if the two
+      --  specs are not conformant.
+
       Fs := First_Formal (Spec);
       Fb := First_Formal (Bod);
-
-      while Present (Fs) loop
+      while Present (Fs) and then Present (Fb) loop
          Generate_Reference (Fs, Fb, 'b');
 
          if Style_Check then
@@ -5018,12 +7435,12 @@ package body Sem_Ch6 is
    -------------------------
 
    procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
-      Loc        : constant Source_Ptr := Sloc (N);
-      Decl       : Node_Id;
-      Formal     : Entity_Id;
-      T          : Entity_Id;
-      First_Stmt : Node_Id := Empty;
-      AS_Needed  : Boolean;
+      Loc            : constant Source_Ptr := Sloc (N);
+      Decl           : Node_Id;
+      Formal         : Entity_Id;
+      T              : Entity_Id;
+      First_Stmt     : Node_Id := Empty;
+      AS_Needed      : Boolean;
 
    begin
       --  If this is an emtpy initialization procedure, no need to create
@@ -5039,41 +7456,43 @@ package body Sem_Ch6 is
       while Present (Formal) loop
          T := Etype (Formal);
 
-         --  We never need an actual subtype for a constrained formal.
+         --  We never need an actual subtype for a constrained formal
 
          if Is_Constrained (T) then
             AS_Needed := False;
 
-         --  If we have unknown discriminants, then we do not need an
-         --  actual subtype, or more accurately we cannot figure it out!
-         --  Note that all class-wide types have unknown discriminants.
+         --  If we have unknown discriminants, then we do not need an actual
+         --  subtype, or more accurately we cannot figure it out! Note that
+         --  all class-wide types have unknown discriminants.
 
          elsif Has_Unknown_Discriminants (T) then
             AS_Needed := False;
 
-         --  At this stage we have an unconstrained type that may need
-         --  an actual subtype. For sure the actual subtype is needed
-         --  if we have an unconstrained array type.
+         --  At this stage we have an unconstrained type that may need an
+         --  actual subtype. For sure the actual subtype is needed if we have
+         --  an unconstrained array type.
 
          elsif Is_Array_Type (T) then
             AS_Needed := True;
 
-         --  The only other case which needs an actual subtype is an
-         --  unconstrained record type which is an IN parameter (we
-         --  cannot generate actual subtypes for the OUT or IN OUT case,
-         --  since an assignment can change the discriminant values.
-         --  However we exclude the case of initialization procedures,
-         --  since discriminants are handled very specially in this context,
-         --  see the section entitled "Handling of Discriminants" in Einfo.
-         --  We also exclude the case of Discrim_SO_Functions (functions
-         --  used in front end layout mode for size/offset values), since
-         --  in such functions only discriminants are referenced, and not
-         --  only are such subtypes not needed, but they cannot always
-         --  be generated, because of order of elaboration issues.
+         --  The only other case needing an actual subtype is an unconstrained
+         --  record type which is an IN parameter (we cannot generate actual
+         --  subtypes for the OUT or IN OUT case, since an assignment can
+         --  change the discriminant values. However we exclude the case of
+         --  initialization procedures, since discriminants are handled very
+         --  specially in this context, see the section entitled "Handling of
+         --  Discriminants" in Einfo.
+
+         --  We also exclude the case of Discrim_SO_Functions (functions used
+         --  in front end layout mode for size/offset values), since in such
+         --  functions only discriminants are referenced, and not only are such
+         --  subtypes not needed, but they cannot always be generated, because
+         --  of order of elaboration issues.
 
          elsif Is_Record_Type (T)
            and then Ekind (Formal) = E_In_Parameter
            and then Chars (Formal) /= Name_uInit
+           and then not Is_Unchecked_Union (T)
            and then not Is_Discrim_SO_Function (Subp)
          then
             AS_Needed := True;
@@ -5088,7 +7507,6 @@ package body Sem_Ch6 is
          --  unconstrained discriminated records.
 
          if AS_Needed then
-
             if Nkind (N) = N_Accept_Statement then
 
                --  If expansion is active, The formal is replaced by a local
@@ -5108,9 +7526,9 @@ package body Sem_Ch6 is
                   Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
                   Mark_Rewrite_Insertion (Decl);
                else
-                  --  If the accept statement has no body, there will be
-                  --  no reference to the actuals, so no need to compute
-                  --  actual subtypes.
+                  --  If the accept statement has no body, there will be no
+                  --  reference to the actuals, so no need to compute actual
+                  --  subtypes.
 
                   return;
                end if;
@@ -5121,7 +7539,10 @@ package body Sem_Ch6 is
                Mark_Rewrite_Insertion (Decl);
             end if;
 
-            Analyze (Decl);
+            --  The declaration uses the bounds of an existing object, and
+            --  therefore needs no constraint checks.
+
+            Analyze (Decl, Suppress => All_Checks);
 
             --  We need to freeze manually the generated type when it is
             --  inserted anywhere else than in a declarative part.
@@ -5179,12 +7600,27 @@ package body Sem_Ch6 is
       end if;
 
       --  Set Is_Known_Non_Null for access parameters since the language
-      --  guarantees that access parameters are always non-null. We also
-      --  set Can_Never_Be_Null, since there is no way to change the value.
+      --  guarantees that access parameters are always non-null. We also set
+      --  Can_Never_Be_Null, since there is no way to change the value.
 
       if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
+
+         --  Ada 2005 (AI-231): In Ada95, access parameters are always non-
+         --  null; In Ada 2005, only if then null_exclusion is explicit.
+
+         if Ada_Version < Ada_05
+           or else Can_Never_Be_Null (Etype (Formal_Id))
+         then
+            Set_Is_Known_Non_Null (Formal_Id);
+            Set_Can_Never_Be_Null (Formal_Id);
+         end if;
+
+      --  Ada 2005 (AI-231): Null-exclusion access subtype
+
+      elsif Is_Access_Type (Etype (Formal_Id))
+        and then Can_Never_Be_Null (Etype (Formal_Id))
+      then
          Set_Is_Known_Non_Null (Formal_Id);
-         Set_Can_Never_Be_Null (Formal_Id);
       end if;
 
       Set_Mechanism (Formal_Id, Default_Mechanism);
@@ -5197,9 +7633,9 @@ package body Sem_Ch6 is
 
    procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
    begin
-      --  If no validity checking, then we cannot assume anything about
-      --  the validity of parameters, since we do not know there is any
-      --  checking of the validity on the call side.
+      --  If no validity checking, then we cannot assume anything about the
+      --  validity of parameters, since we do not know there is any checking
+      --  of the validity on the call side.
 
       if not Validity_Checks_On then
          return;
@@ -5233,7 +7669,6 @@ package body Sem_Ch6 is
 
    function Subtype_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
       Result : Boolean;
-
    begin
       Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result);
       return Result;
@@ -5243,11 +7678,18 @@ package body Sem_Ch6 is
    -- Type_Conformant --
    ---------------------
 
-   function Type_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
+   function Type_Conformant
+     (New_Id                   : Entity_Id;
+      Old_Id                   : Entity_Id;
+      Skip_Controlling_Formals : Boolean := False) return Boolean
+   is
       Result : Boolean;
-
    begin
-      Check_Conformance (New_Id, Old_Id, Type_Conformant, False, Result);
+      May_Hide_Profile := False;
+
+      Check_Conformance
+        (New_Id, Old_Id, Type_Conformant, False, Result,
+         Skip_Controlling_Formals => Skip_Controlling_Formals);
       return Result;
    end Type_Conformant;
 
@@ -5263,7 +7705,6 @@ package body Sem_Ch6 is
 
    begin
       F := First_Formal (Designator);
-
       while Present (F) loop
          N := N + 1;