OSDN Git Service

PR target/50678
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_dist.adb
index 10eae08..82d5898 100644 (file)
@@ -6,18 +6,17 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2010, 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,  51  Franklin  Street,  Fifth  Floor, --
--- Boston, MA 02110-1301, 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.      --
@@ -28,6 +27,7 @@ with Atree;    use Atree;
 with Einfo;    use Einfo;
 with Elists;   use Elists;
 with Exp_Atag; use Exp_Atag;
+with Exp_Disp; use Exp_Disp;
 with Exp_Strm; use Exp_Strm;
 with Exp_Tss;  use Exp_Tss;
 with Exp_Util; use Exp_Util;
@@ -37,14 +37,15 @@ with Nmake;    use Nmake;
 with Opt;      use Opt;
 with Rtsfind;  use Rtsfind;
 with Sem;      use Sem;
+with Sem_Aux;  use Sem_Aux;
 with Sem_Cat;  use Sem_Cat;
 with Sem_Ch3;  use Sem_Ch3;
 with Sem_Ch8;  use Sem_Ch8;
+with Sem_Ch12; use Sem_Ch12;
 with Sem_Dist; use Sem_Dist;
 with Sem_Eval; use Sem_Eval;
 with Sem_Util; use Sem_Util;
 with Sinfo;    use Sinfo;
-with Snames;   use Snames;
 with Stand;    use Stand;
 with Stringt;  use Stringt;
 with Tbuild;   use Tbuild;
@@ -56,8 +57,7 @@ with GNAT.HTable; use GNAT.HTable;
 package body Exp_Dist is
 
    --  The following model has been used to implement distributed objects:
-   --  given a designated type D and a RACW type R, then a record of the
-   --  form:
+   --  given a designated type D and a RACW type R, then a record of the form:
 
    --    type Stub is tagged record
    --       [...declaration similar to s-parint.ads RACW_Stub_Type...]
@@ -65,10 +65,10 @@ package body Exp_Dist is
 
    --  is built. This type has two properties:
 
-   --    1) Since it has the same structure than RACW_Stub_Type, it can be
-   --       converted to and from this type to make it suitable for
+   --    1) Since it has the same structure as RACW_Stub_Type, it can
+   --       be converted to and from this type to make it suitable for
    --       System.Partition_Interface.Get_Unique_Remote_Pointer in order
-   --       to avoid memory leaks when the same remote object arrive on the
+   --       to avoid memory leaks when the same remote object arrives on the
    --       same partition through several paths;
 
    --    2) It also has the same dispatching table as the designated type D,
@@ -83,11 +83,10 @@ package body Exp_Dist is
    --  RCI subprograms are numbered starting at 2. The RCI receiver for
    --  an RCI package can thus identify calls received through remote
    --  access-to-subprogram dereferences by the fact that they have a
-   --  (primitive) subprogram id of 0, and 1 is used for the internal
-   --  RAS information lookup operation. (This is for the Garlic code
-   --  generation, where subprograms are identified by numbers; in the
-   --  PolyORB version, they are identified by name, with a numeric suffix
-   --  for homonyms.)
+   --  (primitive) subprogram id of 0, and 1 is used for the internal RAS
+   --  information lookup operation. (This is for the Garlic code generation,
+   --  where subprograms are identified by numbers; in the PolyORB version,
+   --  they are identified by name, with a numeric suffix for homonyms.)
 
    type Hash_Index is range 0 .. 50;
 
@@ -96,13 +95,13 @@ package body Exp_Dist is
    -----------------------
 
    function Hash (F : Entity_Id) return Hash_Index;
-   --  DSA expansion associates stubs to distributed object types using
-   --  a hash table on entity ids.
+   --  DSA expansion associates stubs to distributed object types using a hash
+   --  table on entity ids.
 
    function Hash (F : Name_Id) return Hash_Index;
    --  The generation of subprogram identifiers requires an overload counter
-   --  to be associated with each remote subprogram names. These counters
-   --  are maintained in a hash table on name ids.
+   --  to be associated with each remote subprogram name. These counters are
+   --  maintained in a hash table on name ids.
 
    type Subprogram_Identifiers is record
       Str_Identifier : String_Id;
@@ -116,8 +115,8 @@ package body Exp_Dist is
                          Key        => Entity_Id,
                          Hash       => Hash,
                          Equal      => "=");
-   --  Mapping between a remote subprogram and the corresponding
-   --  subprogram identifiers.
+   --  Mapping between a remote subprogram and the corresponding subprogram
+   --  identifiers.
 
    package Overload_Counter_Table is
       new Simple_HTable (Header_Num => Hash_Index,
@@ -126,9 +125,9 @@ package body Exp_Dist is
                          Key        => Name_Id,
                          Hash       => Hash,
                          Equal      => "=");
-   --  Mapping between a subprogram name and an integer that
-   --  counts the number of defining subprogram names with that
-   --  Name_Id encountered so far in a given context (an interface).
+   --  Mapping between a subprogram name and an integer that counts the number
+   --  of defining subprogram names with that Name_Id encountered so far in a
+   --  given context (an interface).
 
    function Get_Subprogram_Ids (Def : Entity_Id) return Subprogram_Identifiers;
    function Get_Subprogram_Id  (Def : Entity_Id) return String_Id;
@@ -172,10 +171,9 @@ package body Exp_Dist is
    function Build_Remote_Subprogram_Proxy_Type
      (Loc            : Source_Ptr;
       ACR_Expression : Node_Id) return Node_Id;
-   --  Build and return a tagged record type definition for an RCI
-   --  subprogram proxy type.
-   --  ACR_Expression is use as the initialization value for
-   --  the All_Calls_Remote component.
+   --  Build and return a tagged record type definition for an RCI subprogram
+   --  proxy type. ACR_Expression is used as the initialization value for the
+   --  All_Calls_Remote component.
 
    function Build_Get_Unique_RP_Call
      (Loc       : Source_Ptr;
@@ -185,6 +183,12 @@ package body Exp_Dist is
    --  tag fixup (Get_Unique_Remote_Pointer may have changed Pointer'Tag to
    --  RACW_Stub_Type'Tag, while the desired tag is that of Stub_Type).
 
+   function Build_Stub_Tag
+     (Loc       : Source_Ptr;
+      RACW_Type : Entity_Id) return Node_Id;
+   --  Return an expression denoting the tag of the stub type associated with
+   --  RACW_Type.
+
    function Build_Subprogram_Calling_Stubs
      (Vis_Decl                 : Node_Id;
       Subp_Id                  : Node_Id;
@@ -196,13 +200,13 @@ package body Exp_Dist is
       New_Name                 : Name_Id   := No_Name) return Node_Id;
    --  Build the calling stub for a given subprogram with the subprogram ID
    --  being Subp_Id. If Stub_Type is given, then the "addr" field of
-   --  parameters of this type will be marshalled instead of the object
-   --  itself. It will then be converted into Stub_Type before performing
-   --  the real call. If Dynamically_Asynchronous is True, then it will be
-   --  computed at run time whether the call is asynchronous or not.
-   --  Otherwise, the value of the formal Asynchronous will be used.
-   --  If Locator is not Empty, it will be used instead of RCI_Cache. If
-   --  New_Name is given, then it will be used instead of the original name.
+   --  parameters of this type will be marshalled instead of the object itself.
+   --  It will then be converted into Stub_Type before performing the real
+   --  call. If Dynamically_Asynchronous is True, then it will be computed at
+   --  run time whether the call is asynchronous or not. Otherwise, the value
+   --  of the formal Asynchronous will be used. If Locator is not Empty, it
+   --  will be used instead of RCI_Cache. If New_Name is given, then it will
+   --  be used instead of the original name.
 
    function Build_RPC_Receiver_Specification
      (RPC_Receiver      : Entity_Id;
@@ -217,14 +221,17 @@ package body Exp_Dist is
    --  the controlling formal of the equivalent RACW operation for a RAS
    --  type is always left in first position.
 
-   procedure Add_Calling_Stubs_To_Declarations
-     (Pkg_Spec : Node_Id;
-      Decls    : List_Id);
+   function Transmit_As_Unconstrained (Typ : Entity_Id) return Boolean;
+   --  True when Typ is an unconstrained type, or a null-excluding access type.
+   --  In either case, this means stubs cannot contain a default-initialized
+   --  object declaration of such type.
+
+   procedure Add_Calling_Stubs_To_Declarations (Pkg_Spec : Node_Id);
    --  Add calling stubs to the declarative part
 
    function Could_Be_Asynchronous (Spec : Node_Id) return Boolean;
    --  Return True if nothing prevents the program whose specification is
-   --  given to be asynchronous (i.e. no out parameter).
+   --  given to be asynchronous (i.e. no [IN] OUT parameters).
 
    function Pack_Entity_Into_Stream_Access
      (Loc    : Source_Ptr;
@@ -254,8 +261,8 @@ package body Exp_Dist is
      (Loc           : Source_Ptr;
       Prefix        : Entity_Id;
       Selector_Name : Name_Id) return Node_Id;
-   --  Return a selected_component whose prefix denotes the given entity,
-   --  and with the given Selector_Name.
+   --  Return a selected_component whose prefix denotes the given entity, and
+   --  with the given Selector_Name.
 
    function Scope_Of_Spec (Spec : Node_Id) return Entity_Id;
    --  Return the scope represented by a given spec
@@ -264,8 +271,8 @@ package body Exp_Dist is
      (Typ     : Entity_Id;
       Nam     : Entity_Id;
       TSS_Nam : TSS_Name_Type);
-   --  Create a renaming declaration of subprogram Nam,
-   --  and register it as a TSS for Typ with name TSS_Nam.
+   --  Create a renaming declaration of subprogram Nam, and register it as a
+   --  TSS for Typ with name TSS_Nam.
 
    function Need_Extra_Constrained (Parameter : Node_Id) return Boolean;
    --  Return True if the current parameter needs an extra formal to reflect
@@ -378,6 +385,9 @@ package body Exp_Dist is
                          Equal      => "=");
    --  Mapping between a RCI subprogram and the corresponding calling stubs
 
+   function Get_Stub_Elements (RACW_Type : Entity_Id) return Stub_Structure;
+   --  Return the stub information associated with the given RACW type
+
    procedure Add_Stub_Type
      (Designated_Type   : Entity_Id;
       RACW_Type         : Entity_Id;
@@ -472,9 +482,10 @@ package body Exp_Dist is
       RPC_Receiver_Decl   : Node_Id;
       Body_Decls          : List_Id);
    --  Add declaration for TSSs for a given RACW type. The declarations are
-   --  added just after the declaration of the RACW type itself, while the
-   --  bodies are inserted at the end of Body_Decls. Runtime-specific ancillary
-   --  subprogram for Add_RACW_Features.
+   --  added just after the declaration of the RACW type itself. If the RACW
+   --  appears in the main unit, Body_Decls is a list of declarations to which
+   --  the bodies are appended. Else Body_Decls is No_List.
+   --  PCS-specific ancillary subprogram for Add_RACW_Features.
 
    procedure Specific_Add_RAST_Features
      (Vis_Decl : Node_Id;
@@ -489,12 +500,13 @@ package body Exp_Dist is
    type RPC_Target (PCS_Kind : PCS_Names) is record
       case PCS_Kind is
          when Name_PolyORB_DSA =>
-            Object       : Node_Id;
+            Object : Node_Id;
             --  An expression whose value is a PolyORB reference to the target
             --  object.
+
          when others           =>
-            Partition    : Entity_Id;
-            --  A variable containing the Partition_ID of the target parition
+            Partition : Entity_Id;
+            --  A variable containing the Partition_ID of the target partition
 
             RPC_Receiver : Node_Id;
             --  An expression whose value is the address of the target RPC
@@ -548,11 +560,10 @@ package body Exp_Dist is
 
    procedure Specific_Build_Stub_Type
      (RACW_Type         : Entity_Id;
-      Stub_Type         : Entity_Id;
-      Stub_Type_Decl    : out Node_Id;
+      Stub_Type_Comps   : out List_Id;
       RPC_Receiver_Decl : out Node_Id);
-   --  Build a type declaration for the stub type associated with an RACW
-   --  type, and the necessary RPC receiver, if applicable. PCS-specific
+   --  Build a components list for the stub type associated with an RACW type,
+   --  and build the necessary RPC receiver, if applicable. PCS-specific
    --  ancillary subprogram for Add_Stub_Type. If no RPC receiver declaration
    --  is generated, then RPC_Receiver_Decl is set to Empty.
 
@@ -601,13 +612,16 @@ package body Exp_Dist is
       Stmts    : List_Id);
    --  Add receiving stubs to the declarative part of an RCI unit
 
+   --------------------
+   -- GARLIC_Support --
+   --------------------
+
    package GARLIC_Support is
 
       --  Support for generating DSA code that uses the GARLIC PCS
 
-      --  The subprograms below provide the GARLIC versions of
-      --  the corresponding Specific_<subprogram> routine declared
-      --  above.
+      --  The subprograms below provide the GARLIC versions of the
+      --  corresponding Specific_<subprogram> routine declared above.
 
       procedure Add_RACW_Features
         (RACW_Type         : Entity_Id;
@@ -642,9 +656,8 @@ package body Exp_Dist is
          Controlling_Parameter : Entity_Id) return RPC_Target;
 
       procedure Build_Stub_Type
-        (RACW_Type : Entity_Id;
-         Stub_Type : Entity_Id;
-         Stub_Type_Decl    : out Node_Id;
+        (RACW_Type         : Entity_Id;
+         Stub_Type_Comps   : out List_Id;
          RPC_Receiver_Decl : out Node_Id);
 
       function Build_Subprogram_Receiving_Stubs
@@ -676,13 +689,16 @@ package body Exp_Dist is
 
    end GARLIC_Support;
 
+   ---------------------
+   -- PolyORB_Support --
+   ---------------------
+
    package PolyORB_Support is
 
       --  Support for generating DSA code that uses the PolyORB PCS
 
-      --  The subprograms below provide the PolyORB versions of
-      --  the corresponding Specific_<subprogram> routine declared
-      --  above.
+      --  The subprograms below provide the PolyORB versions of the
+      --  corresponding Specific_<subprogram> routine declared above.
 
       procedure Add_RACW_Features
         (RACW_Type         : Entity_Id;
@@ -718,8 +734,7 @@ package body Exp_Dist is
 
       procedure Build_Stub_Type
         (RACW_Type         : Entity_Id;
-         Stub_Type         : Entity_Id;
-         Stub_Type_Decl    : out Node_Id;
+         Stub_Type_Comps   : out List_Id;
          RPC_Receiver_Decl : out Node_Id);
 
       function Build_Subprogram_Receiving_Stubs
@@ -756,20 +771,26 @@ package body Exp_Dist is
       --  their methods to be accessed as objects, for the implementation of
       --  remote access-to-subprogram types).
 
+      -------------
+      -- Helpers --
+      -------------
+
       package Helpers is
 
-         --  Routines to build distribtion helper subprograms for user-defined
+         --  Routines to build distribution helper subprograms for user-defined
          --  types. For implementation of the Distributed systems annex (DSA)
          --  over the PolyORB generic middleware components, it is necessary to
          --  generate several supporting subprograms for each application data
          --  type used in inter-partition communication. These subprograms are:
-         --    * a Typecode function returning a high-level description of the
-         --      type's structure;
-         --    * two conversion functions allowing conversion of values of the
-         --      type from and to the generic data containers used by PolyORB.
-         --      These generic containers are called 'Any' type values after
-         --      the CORBA terminology, and hence the conversion subprograms
-         --      are named To_Any and From_Any.
+
+         --    A Typecode function returning a high-level description of the
+         --    type's structure;
+
+         --    Two conversion functions allowing conversion of values of the
+         --    type from and to the generic data containers used by PolyORB.
+         --    These generic containers are called 'Any' type values after the
+         --    CORBA terminology, and hence the conversion subprograms are
+         --    named To_Any and From_Any.
 
          function Build_From_Any_Call
            (Typ   : Entity_Id;
@@ -840,10 +861,44 @@ package body Exp_Dist is
          --  for entity E (a distributed object type or operation): one
          --  containing the name of E, the second containing its repository id.
 
+         procedure Assign_Opaque_From_Any
+           (Loc    : Source_Ptr;
+            Stms   : List_Id;
+            Typ    : Entity_Id;
+            N      : Node_Id;
+            Target : Entity_Id);
+         --  For a Target object of type Typ, which has opaque representation
+         --  as a sequence of octets determined by stream attributes (which
+         --  includes all limited types), append code to Stmts performing the
+         --  equivalent of:
+         --    Target := Typ'From_Any (N)
+         --
+         --  or, if Target is Empty:
+         --    return Typ'From_Any (N)
+
       end Helpers;
 
    end PolyORB_Support;
 
+   --  The following PolyORB-specific subprograms are made visible to Exp_Attr:
+
+   function Build_From_Any_Call
+     (Typ   : Entity_Id;
+      N     : Node_Id;
+      Decls : List_Id) return Node_Id
+     renames PolyORB_Support.Helpers.Build_From_Any_Call;
+
+   function Build_To_Any_Call
+     (N     : Node_Id;
+      Decls : List_Id) return Node_Id
+     renames PolyORB_Support.Helpers.Build_To_Any_Call;
+
+   function Build_TypeCode_Call
+     (Loc   : Source_Ptr;
+      Typ   : Entity_Id;
+      Decls : List_Id) return Node_Id
+     renames PolyORB_Support.Helpers.Build_TypeCode_Call;
+
    ------------------------------------
    -- Local variables and structures --
    ------------------------------------
@@ -859,78 +914,170 @@ package body Exp_Dist is
    --  since this require separate mechanisms ('Input is a function while
    --  'Read is a procedure).
 
+   generic
+      with procedure Process_Subprogram_Declaration (Decl : Node_Id);
+      --  Generate calling or receiving stub for this subprogram declaration
+
+   procedure Build_Package_Stubs (Pkg_Spec : Node_Id);
+   --  Recursively visit the given RCI Package_Specification, calling
+   --  Process_Subprogram_Declaration for each remote subprogram.
+
+   -------------------------
+   -- Build_Package_Stubs --
+   -------------------------
+
+   procedure Build_Package_Stubs (Pkg_Spec : Node_Id) is
+      Decls : constant List_Id := Visible_Declarations (Pkg_Spec);
+      Decl  : Node_Id;
+
+      procedure Visit_Nested_Pkg (Nested_Pkg_Decl : Node_Id);
+      --  Recurse for the given nested package declaration
+
+      -----------------------
+      -- Visit_Nested_Spec --
+      -----------------------
+
+      procedure Visit_Nested_Pkg (Nested_Pkg_Decl : Node_Id) is
+         Nested_Pkg_Spec : constant Node_Id := Specification (Nested_Pkg_Decl);
+      begin
+         Push_Scope (Scope_Of_Spec (Nested_Pkg_Spec));
+         Build_Package_Stubs (Nested_Pkg_Spec);
+         Pop_Scope;
+      end Visit_Nested_Pkg;
+
+   --  Start of processing for Build_Package_Stubs
+
+   begin
+      Decl := First (Decls);
+      while Present (Decl) loop
+         case Nkind (Decl) is
+            when N_Subprogram_Declaration =>
+
+               --  Note: we test Comes_From_Source on Spec, not Decl, because
+               --  in the case of a subprogram instance, only the specification
+               --  (not the declaration) is marked as coming from source.
+
+               if Comes_From_Source (Specification (Decl)) then
+                  Process_Subprogram_Declaration (Decl);
+               end if;
+
+            when N_Package_Declaration =>
+
+               --  Case of a nested package or package instantiation coming
+               --  from source. Note that the anonymous wrapper package for
+               --  subprogram instances is not flagged Is_Generic_Instance at
+               --  this point, so there is a distinct circuit to handle them
+               --  (see case N_Subprogram_Instantiation below).
+
+               declare
+                  Pkg_Ent : constant Entity_Id :=
+                              Defining_Unit_Name (Specification (Decl));
+               begin
+                  if Comes_From_Source (Decl)
+                    or else
+                      (Is_Generic_Instance (Pkg_Ent)
+                         and then Comes_From_Source
+                                    (Get_Package_Instantiation_Node (Pkg_Ent)))
+                  then
+                     Visit_Nested_Pkg (Decl);
+                  end if;
+               end;
+
+            when N_Subprogram_Instantiation =>
+
+               --  The subprogram declaration for an instance of a generic
+               --  subprogram is wrapped in a package that does not come from
+               --  source, so we need to explicitly traverse it here.
+
+               if Comes_From_Source (Decl) then
+                  Visit_Nested_Pkg (Instance_Spec (Decl));
+               end if;
+
+            when others =>
+               null;
+         end case;
+         Next (Decl);
+      end loop;
+   end Build_Package_Stubs;
+
    ---------------------------------------
    -- Add_Calling_Stubs_To_Declarations --
    ---------------------------------------
 
-   procedure Add_Calling_Stubs_To_Declarations
-     (Pkg_Spec : Node_Id;
-      Decls    : List_Id)
-   is
+   procedure Add_Calling_Stubs_To_Declarations (Pkg_Spec : Node_Id) is
+      Loc   : constant Source_Ptr := Sloc (Pkg_Spec);
+
       Current_Subprogram_Number : Int := First_RCI_Subprogram_Id;
       --  Subprogram id 0 is reserved for calls received from
       --  remote access-to-subprogram dereferences.
 
-      Current_Declaration       : Node_Id;
-      Loc                       : constant Source_Ptr := Sloc (Pkg_Spec);
-      RCI_Instantiation         : Node_Id;
-      Subp_Stubs                : Node_Id;
-      Subp_Str                  : String_Id;
+      RCI_Instantiation : Node_Id;
+
+      procedure Visit_Subprogram (Decl : Node_Id);
+      --  Generate calling stub for one remote subprogram
+
+      ----------------------
+      -- Visit_Subprogram --
+      ----------------------
+
+      procedure Visit_Subprogram (Decl : Node_Id) is
+         Loc        : constant Source_Ptr := Sloc (Decl);
+         Spec       : constant Node_Id := Specification (Decl);
+         Subp_Stubs : Node_Id;
+
+         Subp_Str : String_Id;
+         pragma Warnings (Off, Subp_Str);
+
+      begin
+         Assign_Subprogram_Identifier
+           (Defining_Unit_Name (Spec), Current_Subprogram_Number, Subp_Str);
+
+         Subp_Stubs :=
+           Build_Subprogram_Calling_Stubs
+             (Vis_Decl     => Decl,
+              Subp_Id      =>
+                Build_Subprogram_Id (Loc, Defining_Unit_Name (Spec)),
+              Asynchronous =>
+                Nkind (Spec) = N_Procedure_Specification
+                  and then Is_Asynchronous (Defining_Unit_Name (Spec)));
+
+         Append_To (List_Containing (Decl), Subp_Stubs);
+         Analyze (Subp_Stubs);
+
+         Current_Subprogram_Number := Current_Subprogram_Number + 1;
+      end Visit_Subprogram;
+
+      procedure Visit_Spec is new Build_Package_Stubs (Visit_Subprogram);
+
+   --  Start of processing for Add_Calling_Stubs_To_Declarations
 
    begin
+      Push_Scope (Scope_Of_Spec (Pkg_Spec));
+
       --  The first thing added is an instantiation of the generic package
-      --  System.Partition_Interface.RCI_Locator with the name of this
-      --  remote package. This will act as an interface with the name server
-      --  to determine the Partition_ID and the RPC_Receiver for the
-      --  receiver of this package.
+      --  System.Partition_Interface.RCI_Locator with the name of this remote
+      --  package. This will act as an interface with the name server to
+      --  determine the Partition_ID and the RPC_Receiver for the receiver
+      --  of this package.
 
       RCI_Instantiation := RCI_Package_Locator (Loc, Pkg_Spec);
       RCI_Cache         := Defining_Unit_Name (RCI_Instantiation);
 
-      Append_To (Decls, RCI_Instantiation);
+      Append_To (Visible_Declarations (Pkg_Spec), RCI_Instantiation);
       Analyze (RCI_Instantiation);
 
-      --  For each subprogram declaration visible in the spec, we do
-      --  build a body. We also increment a counter to assign a different
-      --  Subprogram_Id to each subprograms. The receiving stubs processing
-      --  do use the same mechanism and will thus assign the same Id and
-      --  do the correct dispatching.
+      --  For each subprogram declaration visible in the spec, we do build a
+      --  body. We also increment a counter to assign a different Subprogram_Id
+      --  to each subprogram. The receiving stubs processing uses the same
+      --  mechanism and will thus assign the same Id and do the correct
+      --  dispatching.
 
       Overload_Counter_Table.Reset;
       PolyORB_Support.Reserve_NamingContext_Methods;
 
-      Current_Declaration := First (Visible_Declarations (Pkg_Spec));
+      Visit_Spec (Pkg_Spec);
 
-      while Present (Current_Declaration) loop
-         if Nkind (Current_Declaration) = N_Subprogram_Declaration
-           and then Comes_From_Source (Current_Declaration)
-         then
-            Assign_Subprogram_Identifier (
-              Defining_Unit_Name (Specification (Current_Declaration)),
-              Current_Subprogram_Number,
-              Subp_Str);
-
-            Subp_Stubs :=
-              Build_Subprogram_Calling_Stubs (
-                Vis_Decl     => Current_Declaration,
-                Subp_Id      =>
-                  Build_Subprogram_Id (Loc,
-                    Defining_Unit_Name (Specification (Current_Declaration))),
-                Asynchronous =>
-                  Nkind (Specification (Current_Declaration)) =
-                    N_Procedure_Specification
-                  and then
-                    Is_Asynchronous (Defining_Unit_Name (Specification
-                      (Current_Declaration))));
-
-            Append_To (Decls, Subp_Stubs);
-            Analyze (Subp_Stubs);
-
-            Current_Subprogram_Number := Current_Subprogram_Number + 1;
-         end if;
-
-         Next (Current_Declaration);
-      end loop;
+      Pop_Scope;
    end Add_Calling_Stubs_To_Declarations;
 
    -----------------------------
@@ -952,9 +1099,9 @@ package body Exp_Dist is
         (Loc         : Source_Ptr;
          Parameter   : Entity_Id;
          Constrained : Boolean) return Node_Id;
-      --  Return an expression that denotes the parameter passing
-      --  mode to be used for Parameter in distribution stubs,
-      --  where Constrained is Parameter's constrained status.
+      --  Return an expression that denotes the parameter passing mode to be
+      --  used for Parameter in distribution stubs, where Constrained is
+      --  Parameter's constrained status.
 
       ----------------------------
       -- Parameter_Passing_Mode --
@@ -994,8 +1141,7 @@ package body Exp_Dist is
       if Nkind (Parameter) = N_Defining_Identifier then
          Get_Name_String (Chars (Parameter));
       else
-         Get_Name_String (Chars (Defining_Identifier
-                                  (Parameter)));
+         Get_Name_String (Chars (Defining_Identifier (Parameter)));
       end if;
 
       Parameter_Name_String := String_From_Name_Buffer;
@@ -1010,8 +1156,8 @@ package body Exp_Dist is
          Parameter_Mode := New_Occurrence_Of (RTE (RE_Mode_In), Loc);
 
       else
-         Parameter_Mode := Parameter_Passing_Mode (Loc,
-                             Parameter, Constrained);
+         Parameter_Mode :=
+           Parameter_Passing_Mode (Loc, Parameter, Constrained);
       end if;
 
       return
@@ -1079,8 +1225,8 @@ package body Exp_Dist is
       Existing : Boolean;
       --  True when appropriate stubs have already been generated (this is the
       --  case when another RACW with the same designated type has already been
-      --  encountered, in which case we reuse the previous stubs rather than
-      --  generating new ones).
+      --  encountered), in which case we reuse the previous stubs rather than
+      --  generating new ones.
 
    begin
       if not Expander_Active then
@@ -1112,7 +1258,6 @@ package body Exp_Dist is
          end if;
 
       else
-
          --  Case of declaring the RACW in another package than its designated
          --  type: use the private declarations list if present; otherwise
          --  use the visible declarations.
@@ -1139,6 +1284,13 @@ package body Exp_Dist is
          Body_Decls          => Body_Decls,
          Existing            => Existing);
 
+      --  If this RACW is not in the main unit, do not generate primitive or
+      --  TSS bodies.
+
+      if not Entity_Is_In_Main_Unit (RACW_Type) then
+         Body_Decls := No_List;
+      end if;
+
       Add_RACW_Asynchronous_Flag
         (Declarations        => Decls,
          RACW_Type           => RACW_Type);
@@ -1151,12 +1303,13 @@ package body Exp_Dist is
          RPC_Receiver_Decl   => RPC_Receiver_Decl,
          Body_Decls          => Body_Decls);
 
-      if not Same_Scope and then not Existing then
+      --  If we already have stubs for this designated type, nothing to do
 
-         --  The RACW has been declared in another scope than the designated
-         --  type and has not been handled by another RACW in the same package
-         --  as the first one, so add primitives for the stub type here.
+      if Existing then
+         return;
+      end if;
 
+      if Is_Frozen (Desig) then
          Validate_RACW_Primitives (RACW_Type);
          Add_RACW_Primitive_Declarations_And_Bodies
            (Designated_Type  => Desig,
@@ -1164,9 +1317,9 @@ package body Exp_Dist is
             Body_Decls       => Body_Decls);
 
       else
-         --  Validate_RACW_Primitives will be called when the designated type
-         --  is frozen, see Exp_Ch3.Freeze_Type.
-         --  ??? Shouldn't we have a pragma Assert (not Is_Frozen (Desig))?
+         --  Validate_RACW_Primitives requires the list of all primitives of
+         --  the designated type, so defer processing until Desig is frozen.
+         --  See Exp_Ch3.Freeze_Type.
 
          Add_Access_Type_To_Process (E => Desig, A => RACW_Type);
       end if;
@@ -1218,15 +1371,11 @@ package body Exp_Dist is
       Current_Primitive_Spec   : Node_Id;
       Current_Primitive_Decl   : Node_Id;
       Current_Primitive_Number : Int := 0;
-
-      Current_Primitive_Alias : Node_Id;
-
-      Current_Receiver      : Entity_Id;
-      Current_Receiver_Body : Node_Id;
-
-      RPC_Receiver_Decl : Node_Id;
-
-      Possibly_Asynchronous : Boolean;
+      Current_Primitive_Alias  : Node_Id;
+      Current_Receiver         : Entity_Id;
+      Current_Receiver_Body    : Node_Id;
+      RPC_Receiver_Decl        : Node_Id;
+      Possibly_Asynchronous    : Boolean;
 
    begin
       if not Expander_Active then
@@ -1234,15 +1383,15 @@ package body Exp_Dist is
       end if;
 
       if not Is_RAS then
-         RPC_Receiver := Make_Defining_Identifier (Loc,
-                           New_Internal_Name ('P'));
-         Specific_Build_RPC_Receiver_Body (
-           RPC_Receiver => RPC_Receiver,
-           Request      => RPC_Receiver_Request,
-           Subp_Id      => RPC_Receiver_Subp_Id,
-           Subp_Index   => RPC_Receiver_Subp_Index,
-           Stmts        => RPC_Receiver_Statements,
-           Decl         => RPC_Receiver_Decl);
+         RPC_Receiver := Make_Temporary (Loc, 'P');
+
+         Specific_Build_RPC_Receiver_Body
+           (RPC_Receiver => RPC_Receiver,
+            Request      => RPC_Receiver_Request,
+            Subp_Id      => RPC_Receiver_Subp_Id,
+            Subp_Index   => RPC_Receiver_Subp_Index,
+            Stmts        => RPC_Receiver_Statements,
+            Decl         => RPC_Receiver_Decl);
 
          if Get_PCS_Name = Name_PolyORB_DSA then
 
@@ -1255,18 +1404,24 @@ package body Exp_Dist is
       end if;
 
       --  Build callers, receivers for every primitive operations and a RPC
-      --  receiver for this type.
+      --  receiver for this type. Note that we use Direct_Primitive_Operations,
+      --  not Primitive_Operations, because we really want just the primitives
+      --  of the tagged type itself, and in the case of a tagged synchronized
+      --  type we do not want to get the primitives of the corresponding
+      --  record type).
 
-      if Present (Primitive_Operations (Designated_Type)) then
+      if Present (Direct_Primitive_Operations (Designated_Type)) then
          Overload_Counter_Table.Reset;
 
          Current_Primitive_Elmt :=
-           First_Elmt (Primitive_Operations (Designated_Type));
+           First_Elmt (Direct_Primitive_Operations (Designated_Type));
          while Current_Primitive_Elmt /= No_Elmt loop
             Current_Primitive := Node (Current_Primitive_Elmt);
 
             --  Copy the primitive of all the parents, except predefined ones
-            --  that are not remotely dispatching.
+            --  that are not remotely dispatching. Also omit hidden primitives
+            --  (occurs in the case of primitives of interface progenitors
+            --  other than immediate ancestors of the Designated_Type).
 
             if Chars (Current_Primitive) /= Name_uSize
               and then Chars (Current_Primitive) /= Name_uAlignment
@@ -1275,26 +1430,23 @@ package body Exp_Dist is
                  Is_TSS (Current_Primitive, TSS_Stream_Input)  or else
                  Is_TSS (Current_Primitive, TSS_Stream_Output) or else
                  Is_TSS (Current_Primitive, TSS_Stream_Read)   or else
-                 Is_TSS (Current_Primitive, TSS_Stream_Write))
+                 Is_TSS (Current_Primitive, TSS_Stream_Write)
+                   or else
+                     Is_Predefined_Interface_Primitive (Current_Primitive))
+              and then not Is_Hidden (Current_Primitive)
             then
                --  The first thing to do is build an up-to-date copy of the
-               --  spec with all the formals referencing Designated_Type
+               --  spec with all the formals referencing Controlling_Type
                --  transformed into formals referencing Stub_Type. Since this
                --  primitive may have been inherited, go back the alias chain
                --  until the real primitive has been found.
 
-               Current_Primitive_Alias := Current_Primitive;
-               while Present (Alias (Current_Primitive_Alias)) loop
-                  pragma Assert
-                    (Current_Primitive_Alias
-                      /= Alias (Current_Primitive_Alias));
-                  Current_Primitive_Alias := Alias (Current_Primitive_Alias);
-               end loop;
+               Current_Primitive_Alias := Ultimate_Alias (Current_Primitive);
 
                --  Copy the spec from the original declaration for the purpose
                --  of declaring an overriding subprogram: we need to replace
                --  the type of each controlling formal with Stub_Type. The
-               --  primitive may have been declared for Designated_Type or
+               --  primitive may have been declared for Controlling_Type or
                --  inherited from some ancestor type for which we do not have
                --  an easily determined Entity_Id. We have no systematic way
                --  of knowing which type to substitute Stub_Type for. Instead,
@@ -1336,10 +1488,10 @@ package body Exp_Dist is
                        RACW_Type                => Stub_Elements.RACW_Type);
                   Append_To (Body_Decls, Current_Primitive_Body);
 
-                  --  Analyzing the body here would cause the Stub type to be
-                  --  frozen, thus preventing subsequent primitive
-                  --  declarations. For this reason, it will be analyzed later
-                  --  in the regular flow (and in the context of the
+                  --  Analyzing the body here would cause the Stub type to
+                  --  be frozen, thus preventing subsequent primitive
+                  --  declarations. For this reason, it will be analyzed
+                  --  later in the regular flow (and in the context of the
                   --  appropriate unit body, see Append_RACW_Bodies).
 
                end if;
@@ -1356,8 +1508,8 @@ package body Exp_Dist is
                        RACW_Type                => Stub_Elements.RACW_Type,
                        Parent_Primitive         => Current_Primitive);
 
-                  Current_Receiver := Defining_Unit_Name (
-                    Specification (Current_Receiver_Body));
+                  Current_Receiver :=
+                    Defining_Unit_Name (Specification (Current_Receiver_Body));
 
                   Append_To (Body_Decls, Current_Receiver_Body);
 
@@ -1374,13 +1526,14 @@ package body Exp_Dist is
                              Parameter_Associations => New_List (
                                New_Occurrence_Of (RPC_Receiver_Subp_Id, Loc),
                                Make_String_Literal (Loc, Subp_Str))),
+
                          Then_Statements => New_List (
                            Make_Assignment_Statement (Loc,
                              Name => New_Occurrence_Of (
                                        RPC_Receiver_Subp_Index, Loc),
                              Expression =>
                                Make_Integer_Literal (Loc,
-                                  Current_Primitive_Number)))));
+                                  Intval => Current_Primitive_Number)))));
                   end if;
 
                   Append_To (RPC_Receiver_Case_Alternatives,
@@ -1447,15 +1600,10 @@ package body Exp_Dist is
    procedure Add_RAS_Dereference_TSS (N : Node_Id) is
       Loc : constant Source_Ptr := Sloc (N);
 
-      Type_Def : constant Node_Id   := Type_Definition (N);
-
+      Type_Def  : constant Node_Id   := Type_Definition (N);
       RAS_Type  : constant Entity_Id := Defining_Identifier (N);
       Fat_Type  : constant Entity_Id := Equivalent_Type (RAS_Type);
       RACW_Type : constant Entity_Id := Underlying_RACW_Type (RAS_Type);
-      Desig     : constant Entity_Id := Etype (Designated_Type (RACW_Type));
-
-      Stub_Elements : constant Stub_Structure := Stubs_Table.Get (Desig);
-      pragma Assert (Stub_Elements /= Empty_Stub_Structure);
 
       RACW_Primitive_Name : Node_Id;
 
@@ -1468,9 +1616,7 @@ package body Exp_Dist is
       Param_Assoc : constant List_Id := New_List;
       Stmts       : constant List_Id := New_List;
 
-      RAS_Parameter : constant Entity_Id :=
-                        Make_Defining_Identifier (Loc,
-                          Chars => New_Internal_Name ('P'));
+      RAS_Parameter : constant Entity_Id := Make_Temporary (Loc, 'P');
 
       Is_Function : constant Boolean :=
                       Nkind (Type_Def) = N_Access_Function_Definition;
@@ -1540,9 +1686,9 @@ package body Exp_Dist is
 
          --  Generate a dummy body. This code will never actually be executed,
          --  because null is the only legal value for a degenerate RAS type.
-         --  For legality's sake (in order to avoid generating a function
-         --  that does not contain a return statement), we include a dummy
-         --  recursive call on the TSS itself.
+         --  For legality's sake (in order to avoid generating a function that
+         --  does not contain a return statement), we include a dummy recursive
+         --  call on the TSS itself.
 
          Append_To (Stmts,
            Make_Raise_Program_Error (Loc, Reason => PE_Explicit_Raise));
@@ -1565,7 +1711,7 @@ package body Exp_Dist is
 
       if Is_Function then
          Append_To (Stmts,
-            Make_Return_Statement (Loc,
+            Make_Simple_Return_Statement (Loc,
               Expression =>
                 Make_Function_Call (Loc,
                   Name                   => RACW_Primitive_Name,
@@ -1629,17 +1775,16 @@ package body Exp_Dist is
       Subp_Name : constant Entity_Id :=
                      Defining_Unit_Name (Specification (Vis_Decl));
 
-      Pkg_Name   : constant Entity_Id :=
-                     Make_Defining_Identifier (Loc,
-                       Chars =>
-                         New_External_Name (Chars (Subp_Name), 'P', -1));
+      Pkg_Name : constant Entity_Id :=
+                   Make_Defining_Identifier (Loc,
+                     Chars => New_External_Name (Chars (Subp_Name), 'P', -1));
 
       Proxy_Type : constant Entity_Id :=
                      Make_Defining_Identifier (Loc,
                        Chars =>
-                         New_External_Name (
-                           Related_Id => Chars (Subp_Name),
-                           Suffix     => 'P'));
+                         New_External_Name
+                           (Related_Id => Chars (Subp_Name),
+                            Suffix     => 'P'));
 
       Proxy_Type_Full_View : constant Entity_Id :=
                                Make_Defining_Identifier (Loc,
@@ -1685,12 +1830,9 @@ package body Exp_Dist is
 
       Append_To (Vis_Decls,
         Make_Object_Declaration (Loc,
-          Defining_Identifier =>
-            Proxy_Object_Addr,
-          Constant_Present     =>
-            True,
-          Object_Definition   =>
-            New_Occurrence_Of (RTE (RE_Address), Loc)));
+          Defining_Identifier => Proxy_Object_Addr,
+          Constant_Present    => True,
+          Object_Definition   => New_Occurrence_Of (RTE (RE_Address), Loc)));
 
       --  private
 
@@ -1701,8 +1843,7 @@ package body Exp_Dist is
 
       Append_To (Pvt_Decls,
         Make_Full_Type_Declaration (Loc,
-          Defining_Identifier =>
-            Proxy_Type_Full_View,
+          Defining_Identifier => Proxy_Type_Full_View,
           Type_Definition     =>
             Build_Remote_Subprogram_Proxy_Type (Loc,
               New_Occurrence_Of (All_Calls_Remote_E, Loc))));
@@ -1730,19 +1871,15 @@ package body Exp_Dist is
       if Nkind (Subp_Decl_Spec) = N_Procedure_Specification then
          Perform_Call :=
            Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (Subp_Name, Loc),
-             Parameter_Associations =>
-               Actuals);
+             Name                   => New_Occurrence_Of (Subp_Name, Loc),
+             Parameter_Associations => Actuals);
       else
          Perform_Call :=
-           Make_Return_Statement (Loc,
+           Make_Simple_Return_Statement (Loc,
              Expression =>
-           Make_Function_Call (Loc,
-             Name =>
-               New_Occurrence_Of (Subp_Name, Loc),
-             Parameter_Associations =>
-               Actuals));
+               Make_Function_Call (Loc,
+                 Name                   => New_Occurrence_Of (Subp_Name, Loc),
+                 Parameter_Associations => Actuals));
       end if;
 
       Formal := First (Parameter_Specifications (Subp_Decl_Spec));
@@ -1758,31 +1895,23 @@ package body Exp_Dist is
 
       Append_To (Pvt_Decls,
         Make_Object_Declaration (Loc,
-          Defining_Identifier =>
-            Make_Defining_Identifier (Loc,
-              Name_uO),
-          Aliased_Present =>
-            True,
-          Object_Definition =>
-            New_Occurrence_Of (Proxy_Type, Loc)));
+          Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
+          Aliased_Present     => True,
+          Object_Definition   => New_Occurrence_Of (Proxy_Type, Loc)));
 
       --  A : constant System.Address := O'Address;
 
       Append_To (Pvt_Decls,
         Make_Object_Declaration (Loc,
           Defining_Identifier =>
-            Make_Defining_Identifier (Loc,
-              Chars (Proxy_Object_Addr)),
-          Constant_Present =>
-            True,
-          Object_Definition =>
-            New_Occurrence_Of (RTE (RE_Address), Loc),
+            Make_Defining_Identifier (Loc, Chars (Proxy_Object_Addr)),
+          Constant_Present    => True,
+          Object_Definition   => New_Occurrence_Of (RTE (RE_Address), Loc),
           Expression =>
             Make_Attribute_Reference (Loc,
               Prefix => New_Occurrence_Of (
                 Defining_Identifier (Last (Pvt_Decls)), Loc),
-              Attribute_Name =>
-                Name_Address)));
+              Attribute_Name => Name_Address)));
 
       Append_To (Decls,
         Make_Package_Declaration (Loc,
@@ -1796,12 +1925,10 @@ package body Exp_Dist is
       Append_To (Decls,
         Make_Package_Body (Loc,
           Defining_Unit_Name =>
-            Make_Defining_Identifier (Loc,
-              Chars (Pkg_Name)),
+            Make_Defining_Identifier (Loc, Chars (Pkg_Name)),
           Declarations => New_List (
             Make_Subprogram_Body (Loc,
-              Specification  =>
-                Subp_Body_Spec,
+              Specification  => Subp_Body_Spec,
               Declarations   => New_List,
               Handled_Statement_Sequence =>
                 Make_Handled_Sequence_Of_Statements (Loc,
@@ -1838,8 +1965,9 @@ package body Exp_Dist is
    is
       Loc : constant Source_Ptr := Sloc (RACW_Type);
 
-      Stub_Elements : constant Stub_Structure :=
-                        Stubs_Table.Get (Designated_Type);
+      Stub_Elements         : constant Stub_Structure :=
+                                Stubs_Table.Get (Designated_Type);
+      Stub_Type_Comps       : List_Id;
       Stub_Type_Decl        : Node_Id;
       Stub_Type_Access_Decl : Node_Id;
 
@@ -1853,18 +1981,33 @@ package body Exp_Dist is
          return;
       end if;
 
-      Existing         := False;
-      Stub_Type        :=
-        Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
+      Existing := False;
+      Stub_Type := Make_Temporary (Loc, 'S');
+      Set_Ekind (Stub_Type, E_Record_Type);
+      Set_Is_RACW_Stub_Type (Stub_Type);
       Stub_Type_Access :=
         Make_Defining_Identifier (Loc,
-          New_External_Name (
-            Related_Id => Chars (Stub_Type),
-            Suffix     => 'A'));
+          Chars => New_External_Name
+                     (Related_Id => Chars (Stub_Type), Suffix => 'A'));
+
+      Specific_Build_Stub_Type (RACW_Type, Stub_Type_Comps, RPC_Receiver_Decl);
+
+      Stub_Type_Decl :=
+        Make_Full_Type_Declaration (Loc,
+          Defining_Identifier => Stub_Type,
+          Type_Definition     =>
+            Make_Record_Definition (Loc,
+              Tagged_Present  => True,
+              Limited_Present => True,
+              Component_List  =>
+                Make_Component_List (Loc,
+                  Component_Items => Stub_Type_Comps)));
 
-      Specific_Build_Stub_Type (
-        RACW_Type, Stub_Type,
-        Stub_Type_Decl, RPC_Receiver_Decl);
+      --  Does the stub type need to explicitly implement interfaces from the
+      --  designated type???
+
+      --  In particular are there issues in the case where the designated type
+      --  is a synchronized interface???
 
       Stub_Type_Access_Decl :=
         Make_Full_Type_Declaration (Loc,
@@ -1879,9 +2022,10 @@ package body Exp_Dist is
       Append_To (Decls, Stub_Type_Access_Decl);
       Analyze (Last (Decls));
 
-      --  This is in no way a type derivation, but we fake it to make sure that
-      --  the dispatching table gets built with the corresponding primitive
-      --  operations at the right place.
+      --  We can't directly derive the stub type from the designated type,
+      --  because we don't want any components or discriminants from the real
+      --  type, so instead we manually fake a derivation to get an appropriate
+      --  dispatch table.
 
       Derive_Subprograms (Parent_Type  => Designated_Type,
                           Derived_Type => Stub_Type);
@@ -1939,10 +2083,9 @@ package body Exp_Dist is
 
       Get_Name_String (N);
 
-      --  Homonym handling: as in Exp_Dbug, but much simpler,
-      --  because the only entities for which we have to generate
-      --  names here need only to be disambiguated within their
-      --  own scope.
+      --  Homonym handling: as in Exp_Dbug, but much simpler, because the only
+      --  entities for which we have to generate names here need only to be
+      --  disambiguated within their own scope.
 
       if Overload_Order > 1 then
          Name_Buffer (Name_Len + 1 .. Name_Len + 2) := "__";
@@ -1951,8 +2094,9 @@ package body Exp_Dist is
       end if;
 
       Id := String_From_Name_Buffer;
-      Subprogram_Identifier_Table.Set (Def,
-        Subprogram_Identifiers'(Str_Identifier => Id, Int_Identifier => Spn));
+      Subprogram_Identifier_Table.Set
+        (Def,
+         Subprogram_Identifiers'(Str_Identifier => Id, Int_Identifier => Spn));
    end Assign_Subprogram_Identifier;
 
    -------------------------------------
@@ -1967,6 +2111,7 @@ package body Exp_Dist is
       Decls    : List_Id)
    is
       Loc : constant Source_Ptr := Sloc (Object);
+
    begin
       --  Declare a temporary object for the actual, possibly initialized with
       --  a 'Input/From_Any call.
@@ -1997,8 +2142,8 @@ package body Exp_Dist is
 
             declare
                Constant_Object : constant Entity_Id :=
-                                   Make_Defining_Identifier (Loc,
-                                     New_Internal_Name ('P'));
+                                   Make_Temporary (Loc, 'P');
+
             begin
                Set_Defining_Identifier
                  (Last (Decls), Constant_Object);
@@ -2006,7 +2151,7 @@ package body Exp_Dist is
                --  We have an unconstrained Etyp: build the actual constrained
                --  subtype for the value we just read from the stream.
 
-               --  suubtype S is <actual subtype of Constant_Object>;
+               --  subtype S is <actual subtype of Constant_Object>;
 
                Append_To (Decls,
                  Build_Actual_Subtype (Etyp,
@@ -2044,15 +2189,12 @@ package body Exp_Dist is
                    Chars      => Name_Address,
                    Expression =>
                      Make_Attribute_Reference (Loc,
-                       Prefix =>
-                         New_Occurrence_Of (Constant_Object, Loc),
-                       Attribute_Name =>
-                         Name_Address)));
+                       Prefix => New_Occurrence_Of (Constant_Object, Loc),
+                       Attribute_Name => Name_Address)));
             end;
          end if;
 
       else
-
          --  General case of a regular object declaration. Object is flagged
          --  constant unless it has mode out or in out, to allow the backend
          --  to optimize where possible.
@@ -2063,8 +2205,7 @@ package body Exp_Dist is
            Make_Object_Declaration (Loc,
              Defining_Identifier => Object,
              Constant_Present    => Present (Expr) and then not Variable,
-             Object_Definition   =>
-               New_Occurrence_Of (Etyp, Loc),
+             Object_Definition   => New_Occurrence_Of (Etyp, Loc),
              Expression          => Expr));
 
          if Constant_Present (Last (Decls)) then
@@ -2096,17 +2237,14 @@ package body Exp_Dist is
         Make_Assignment_Statement (Loc,
           Name =>
             Make_Selected_Component (Loc,
-              Prefix =>
-                New_Occurrence_Of (Pointer, Loc),
+              Prefix => New_Occurrence_Of (Pointer, Loc),
               Selector_Name =>
                 New_Occurrence_Of (First_Tag_Component
                   (Designated_Type (Etype (Pointer))), Loc)),
           Expression =>
             Make_Attribute_Reference (Loc,
-              Prefix =>
-                New_Occurrence_Of (Stub_Type, Loc),
-              Attribute_Name =>
-                Name_Tag)));
+              Prefix         => New_Occurrence_Of (Stub_Type, Loc),
+              Attribute_Name => Name_Tag)));
 
       --  Note: The assignment to Pointer._Tag is safe here because
       --  we carefully ensured that Stub_Type has exactly the same layout
@@ -2122,6 +2260,7 @@ package body Exp_Dist is
       Constrained_List   : List_Id;
       Unconstrained_List : List_Id;
       Current_Parameter  : Node_Id;
+      Ptyp               : Node_Id;
 
       First_Parameter : Node_Id;
       For_RAS         : Boolean := False;
@@ -2141,15 +2280,17 @@ package body Exp_Dist is
          For_RAS := True;
       end if;
 
-      --  Loop through the parameters and add them to the right list
+      --  Loop through the parameters and add them to the right list. Note that
+      --  we treat a parameter of a null-excluding access type as unconstrained
+      --  because we can't declare an object of such a type with default
+      --  initialization.
 
       Current_Parameter := First_Parameter;
       while Present (Current_Parameter) loop
-         if (Nkind (Parameter_Type (Current_Parameter)) = N_Access_Definition
-             or else
-               Is_Constrained (Etype (Parameter_Type (Current_Parameter)))
-             or else
-               Is_Elementary_Type (Etype (Parameter_Type (Current_Parameter))))
+         Ptyp := Parameter_Type (Current_Parameter);
+
+         if (Nkind (Ptyp) = N_Access_Definition
+               or else not Transmit_As_Unconstrained (Etype (Ptyp)))
            and then not (For_RAS and then Current_Parameter = First_Parameter)
          then
             Append_To (Constrained_List, New_Copy (Current_Parameter));
@@ -2210,8 +2351,7 @@ package body Exp_Dist is
             Make_Attribute_Reference (Loc,
               Prefix         =>
                 New_Occurrence_Of (Defining_Entity (Pkg_Spec), Loc),
-              Attribute_Name =>
-                Name_Version)));
+              Attribute_Name => Name_Version)));
       Append_To (L, Reg);
       Analyze (Reg);
    end Build_Passive_Partition_Stub;
@@ -2285,6 +2425,22 @@ package body Exp_Dist is
                         New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc))))));
    end Build_Remote_Subprogram_Proxy_Type;
 
+   --------------------
+   -- Build_Stub_Tag --
+   --------------------
+
+   function Build_Stub_Tag
+     (Loc       : Source_Ptr;
+      RACW_Type : Entity_Id) return Node_Id
+   is
+      Stub_Type : constant Entity_Id := Corresponding_Stub_Type (RACW_Type);
+   begin
+      return
+        Make_Attribute_Reference (Loc,
+          Prefix         => New_Occurrence_Of (Stub_Type, Loc),
+          Attribute_Name => Name_Tag);
+   end Build_Stub_Tag;
+
    ------------------------------------
    -- Build_Subprogram_Calling_Stubs --
    ------------------------------------
@@ -2357,9 +2513,10 @@ package body Exp_Dist is
    --  Start of processing for Build_Subprogram_Calling_Stubs
 
    begin
-      Subp_Spec := Copy_Specification (Loc,
-        Spec     => Specification (Vis_Decl),
-        New_Name => New_Name);
+      Subp_Spec :=
+        Copy_Specification (Loc,
+          Spec     => Specification (Vis_Decl),
+          New_Name => New_Name);
 
       if Locator = Empty then
          RCI_Locator := RCI_Cache;
@@ -2452,6 +2609,8 @@ package body Exp_Dist is
             Current_Subp_Str    : String_Id;
             Current_Subp_Number : Int := First_RCI_Subprogram_Id;
 
+            pragma Warnings (Off, Current_Subp_Str);
+
          begin
             --  Build_Subprogram_Id is called outside of the context of
             --  generating calling or receiving stubs. Hence we are processing
@@ -2670,8 +2829,7 @@ package body Exp_Dist is
 
       Append_To (Stmts,
         Make_Procedure_Call_Statement (Loc,
-          Name =>
-            New_Occurrence_Of (RTE (RE_NVList_Create), Loc),
+          Name => New_Occurrence_Of (RTE (RE_NVList_Create), Loc),
           Parameter_Associations => New_List (
             New_Occurrence_Of (NVList, Loc))));
    end Declare_Create_NVList;
@@ -2681,11 +2839,11 @@ package body Exp_Dist is
    ---------------------------------------------
 
    procedure Expand_All_Calls_Remote_Subprogram_Call (N : Node_Id) is
+      Loc               : constant Source_Ptr := Sloc (N);
       Called_Subprogram : constant Entity_Id  := Entity (Name (N));
       RCI_Package       : constant Entity_Id  := Scope (Called_Subprogram);
-      Loc               : constant Source_Ptr := Sloc (N);
-      RCI_Locator       : Node_Id;
-      RCI_Cache         : Entity_Id;
+      RCI_Locator_Decl  : Node_Id;
+      RCI_Locator       : Entity_Id;
       Calling_Stubs     : Node_Id;
       E_Calling_Stubs   : Entity_Id;
 
@@ -2693,41 +2851,35 @@ package body Exp_Dist is
       E_Calling_Stubs := RCI_Calling_Stubs_Table.Get (Called_Subprogram);
 
       if E_Calling_Stubs = Empty then
-         RCI_Cache := RCI_Locator_Table.Get (RCI_Package);
-
-         if RCI_Cache = Empty then
-            RCI_Locator :=
-              RCI_Package_Locator
-                (Loc, Specification (Unit_Declaration_Node (RCI_Package)));
-            Prepend_To (Current_Sem_Unit_Declarations, RCI_Locator);
-
-            --  The RCI_Locator package is inserted at the top level in the
-            --  current unit, and must appear in the proper scope, so that it
-            --  is not prematurely removed by the GCC back-end.
-
-            declare
-               Scop : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
-
-            begin
-               if Ekind (Scop) = E_Package_Body then
-                  Push_Scope (Spec_Entity (Scop));
-
-               elsif Ekind (Scop) = E_Subprogram_Body then
-                  Push_Scope
-                     (Corresponding_Spec (Unit_Declaration_Node (Scop)));
+         RCI_Locator := RCI_Locator_Table.Get (RCI_Package);
 
-               else
-                  Push_Scope (Scop);
-               end if;
+         --  The RCI_Locator package and calling stub are is inserted at the
+         --  top level in the current unit, and must appear in the proper scope
+         --  so that it is not prematurely removed by the GCC back end.
 
-               Analyze (RCI_Locator);
-               Pop_Scope;
-            end;
+         declare
+            Scop : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
+         begin
+            if Ekind (Scop) = E_Package_Body then
+               Push_Scope (Spec_Entity (Scop));
+            elsif Ekind (Scop) = E_Subprogram_Body then
+               Push_Scope
+                 (Corresponding_Spec (Unit_Declaration_Node (Scop)));
+            else
+               Push_Scope (Scop);
+            end if;
+         end;
 
-            RCI_Cache   := Defining_Unit_Name (RCI_Locator);
+         if RCI_Locator = Empty then
+            RCI_Locator_Decl :=
+              RCI_Package_Locator
+                (Loc, Specification (Unit_Declaration_Node (RCI_Package)));
+            Prepend_To (Current_Sem_Unit_Declarations, RCI_Locator_Decl);
+            Analyze (RCI_Locator_Decl);
+            RCI_Locator := Defining_Unit_Name (RCI_Locator_Decl);
 
          else
-            RCI_Locator := Parent (RCI_Cache);
+            RCI_Locator_Decl := Parent (RCI_Locator);
          end if;
 
          Calling_Stubs := Build_Subprogram_Calling_Stubs
@@ -2737,10 +2889,12 @@ package body Exp_Dist is
             Asynchronous           => Nkind (N) = N_Procedure_Call_Statement
                                         and then
                                       Is_Asynchronous (Called_Subprogram),
-            Locator                => RCI_Cache,
+            Locator                => RCI_Locator,
             New_Name               => New_Internal_Name ('S'));
-         Insert_After (RCI_Locator, Calling_Stubs);
+         Insert_After (RCI_Locator_Decl, Calling_Stubs);
          Analyze (Calling_Stubs);
+         Pop_Scope;
+
          E_Calling_Stubs := Defining_Unit_Name (Specification (Calling_Stubs));
       end if;
 
@@ -2753,12 +2907,8 @@ package body Exp_Dist is
 
    procedure Expand_Calling_Stubs_Bodies (Unit_Node : Node_Id) is
       Spec  : constant Node_Id := Specification (Unit_Node);
-      Decls : constant List_Id := Visible_Declarations (Spec);
    begin
-      Push_Scope (Scope_Of_Spec (Spec));
-      Add_Calling_Stubs_To_Declarations
-        (Specification (Unit_Node), Decls);
-      Pop_Scope;
+      Add_Calling_Stubs_To_Declarations (Spec);
    end Expand_Calling_Stubs_Bodies;
 
    -----------------------------------
@@ -2766,9 +2916,10 @@ package body Exp_Dist is
    -----------------------------------
 
    procedure Expand_Receiving_Stubs_Bodies (Unit_Node : Node_Id) is
-      Spec  : Node_Id;
-      Decls : List_Id;
-      Temp  : List_Id;
+      Spec        : Node_Id;
+      Decls       : List_Id;
+      Stubs_Decls : List_Id;
+      Stubs_Stmts : List_Id;
 
    begin
       if Nkind (Unit_Node) = N_Package_Declaration then
@@ -2780,18 +2931,34 @@ package body Exp_Dist is
          end if;
 
          Push_Scope (Scope_Of_Spec (Spec));
-         Specific_Add_Receiving_Stubs_To_Declarations
-           (Spec, Decls, Decls);
+         Specific_Add_Receiving_Stubs_To_Declarations (Spec, Decls, Decls);
+
       else
-         Spec  :=
+         Spec :=
            Package_Specification_Of_Scope (Corresponding_Spec (Unit_Node));
          Decls := Declarations (Unit_Node);
 
          Push_Scope (Scope_Of_Spec (Unit_Node));
-         Temp := New_List;
+         Stubs_Decls := New_List;
+         Stubs_Stmts := New_List;
          Specific_Add_Receiving_Stubs_To_Declarations
-           (Spec, Temp, Statements (Handled_Statement_Sequence (Unit_Node)));
-         Insert_List_Before (First (Decls), Temp);
+           (Spec, Stubs_Decls, Stubs_Stmts);
+
+         Insert_List_Before (First (Decls), Stubs_Decls);
+
+         declare
+            HSS_Stmts : constant List_Id :=
+                          Statements (Handled_Statement_Sequence (Unit_Node));
+
+            First_HSS_Stmt : constant Node_Id := First (HSS_Stmts);
+
+         begin
+            if No (First_HSS_Stmt) then
+               Append_List_To (HSS_Stmts, Stubs_Stmts);
+            else
+               Insert_List_Before (First_HSS_Stmt, Stubs_Stmts);
+            end if;
+         end;
       end if;
 
       Pop_Scope;
@@ -2812,7 +2979,8 @@ package body Exp_Dist is
          Body_Decls       : List_Id);
       --  Add Read attribute for the RACW type. The declaration and attribute
       --  definition clauses are inserted right after the declaration of
-      --  RACW_Type, while the subprogram body is appended to Body_Decls.
+      --  RACW_Type. If Body_Decls is not No_List, the subprogram body is
+      --  appended to it (case where the RACW declaration is in the main unit).
 
       procedure Add_RACW_Write_Attribute
         (RACW_Type        : Entity_Id;
@@ -2843,7 +3011,8 @@ package body Exp_Dist is
         (Loc           : Source_Ptr;
          Decls         : List_Id;
          RPC_Receiver  : Entity_Id;
-         Stub_Elements : Stub_Structure) is
+         Stub_Elements : Stub_Structure)
+      is
       begin
          --  The RPC receiver body should not be the completion of the
          --  declaration recorded in the stub structure, because then the
@@ -2896,18 +3065,18 @@ package body Exp_Dist is
               Attribute_Name => Name_Address);
          end if;
 
-         Add_RACW_Write_Attribute (
-           RACW_Type,
-           Stub_Type,
-           Stub_Type_Access,
-           RPC_Receiver,
-           Body_Decls);
-
-         Add_RACW_Read_Attribute (
-           RACW_Type,
-           Stub_Type,
-           Stub_Type_Access,
-           Body_Decls);
+         Add_RACW_Write_Attribute
+           (RACW_Type,
+            Stub_Type,
+            Stub_Type_Access,
+            RPC_Receiver,
+            Body_Decls);
+
+         Add_RACW_Read_Attribute
+           (RACW_Type,
+            Stub_Type,
+            Stub_Type_Access,
+            Body_Decls);
       end Add_RACW_Features;
 
       -----------------------------
@@ -2925,36 +3094,60 @@ package body Exp_Dist is
 
          Body_Node : Node_Id;
 
+         Statements        : constant List_Id := New_List;
          Decls             : List_Id;
-         Statements        : List_Id;
          Local_Statements  : List_Id;
          Remote_Statements : List_Id;
          --  Various parts of the procedure
 
-         Procedure_Name    : constant Name_Id   :=
-                               New_Internal_Name ('R');
-         Source_Partition  : constant Entity_Id :=
-                               Make_Defining_Identifier
-                                 (Loc, New_Internal_Name ('P'));
-         Source_Receiver   : constant Entity_Id :=
-                               Make_Defining_Identifier
-                                 (Loc, New_Internal_Name ('S'));
-         Source_Address    : constant Entity_Id :=
-                               Make_Defining_Identifier
-                                 (Loc, New_Internal_Name ('P'));
-         Local_Stub        : constant Entity_Id :=
-                               Make_Defining_Identifier
-                                 (Loc, New_Internal_Name ('L'));
-         Stubbed_Result    : constant Entity_Id :=
-                               Make_Defining_Identifier
-                                 (Loc, New_Internal_Name ('S'));
+         Pnam              : constant Entity_Id := Make_Temporary (Loc, 'R');
          Asynchronous_Flag : constant Entity_Id :=
                                Asynchronous_Flags_Table.Get (RACW_Type);
          pragma Assert (Present (Asynchronous_Flag));
 
+         --  Prepare local identifiers
+
+         Source_Partition : Entity_Id;
+         Source_Receiver  : Entity_Id;
+         Source_Address   : Entity_Id;
+         Local_Stub       : Entity_Id;
+         Stubbed_Result   : Entity_Id;
+
       --  Start of processing for Add_RACW_Read_Attribute
 
       begin
+         Build_Stream_Procedure (Loc,
+           RACW_Type, Body_Node, Pnam, Statements, Outp => True);
+         Proc_Decl := Make_Subprogram_Declaration (Loc,
+           Copy_Specification (Loc, Specification (Body_Node)));
+
+         Attr_Decl :=
+           Make_Attribute_Definition_Clause (Loc,
+             Name       => New_Occurrence_Of (RACW_Type, Loc),
+             Chars      => Name_Read,
+             Expression =>
+               New_Occurrence_Of (
+                 Defining_Unit_Name (Specification (Proc_Decl)), Loc));
+
+         Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
+         Insert_After (Proc_Decl, Attr_Decl);
+
+         if No (Body_Decls) then
+
+            --  Case of processing an RACW type from another unit than the
+            --  main one: do not generate a body.
+
+            return;
+         end if;
+
+         --  Prepare local identifiers
+
+         Source_Partition := Make_Temporary (Loc, 'P');
+         Source_Receiver  := Make_Temporary (Loc, 'S');
+         Source_Address   := Make_Temporary (Loc, 'P');
+         Local_Stub       := Make_Temporary (Loc, 'L');
+         Stubbed_Result   := Make_Temporary (Loc, 'S');
+
          --  Generate object declarations
 
          Decls := New_List (
@@ -2991,7 +3184,7 @@ package body Exp_Dist is
 
          --  Read the source Partition_ID and RPC_Receiver from incoming stream
 
-         Statements := New_List (
+         Append_List_To (Statements, New_List (
            Make_Attribute_Reference (Loc,
              Prefix         =>
                New_Occurrence_Of (RTE (RE_Partition_ID), Loc),
@@ -3016,25 +3209,41 @@ package body Exp_Dist is
                Name_Read,
              Expressions    => New_List (
                Stream_Parameter,
-               New_Occurrence_Of (Source_Address, Loc))));
+               New_Occurrence_Of (Source_Address, Loc)))));
 
          --  Build_Get_Unique_RP_Call needs the type of Stubbed_Result
 
          Set_Etype (Stubbed_Result, Stub_Type_Access);
 
-         --  If the Address is Null_Address, then return a null object
+         --  If the Address is Null_Address, then return a null object, unless
+         --  RACW_Type is null-excluding, in which case unconditionally raise
+         --  CONSTRAINT_ERROR instead.
 
-         Append_To (Statements,
-           Make_Implicit_If_Statement (RACW_Type,
-             Condition       =>
-               Make_Op_Eq (Loc,
-                 Left_Opnd  => New_Occurrence_Of (Source_Address, Loc),
-                 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
-             Then_Statements => New_List (
-               Make_Assignment_Statement (Loc,
-                 Name       => Result,
-                 Expression => Make_Null (Loc)),
-               Make_Return_Statement (Loc))));
+         declare
+            Zero_Statements : List_Id;
+            --  Statements executed when a zero value is received
+
+         begin
+            if Can_Never_Be_Null (RACW_Type) then
+               Zero_Statements := New_List (
+                 Make_Raise_Constraint_Error (Loc,
+                   Reason => CE_Null_Not_Allowed));
+            else
+               Zero_Statements := New_List (
+                 Make_Assignment_Statement (Loc,
+                   Name       => Result,
+                   Expression => Make_Null (Loc)),
+                 Make_Simple_Return_Statement (Loc));
+            end if;
+
+            Append_To (Statements,
+              Make_Implicit_If_Statement (RACW_Type,
+                Condition       =>
+                  Make_Op_Eq (Loc,
+                    Left_Opnd  => New_Occurrence_Of (Source_Address, Loc),
+                    Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
+                Then_Statements => Zero_Statements));
+         end;
 
          --  If the RACW denotes an object created on the current partition,
          --  Local_Statements will be executed. The real object will be used.
@@ -3115,25 +3324,7 @@ package body Exp_Dist is
              Then_Statements => Local_Statements,
              Else_Statements => Remote_Statements));
 
-         Build_Stream_Procedure
-           (Loc, RACW_Type, Body_Node,
-            Make_Defining_Identifier (Loc, Procedure_Name),
-            Statements, Outp => True);
          Set_Declarations (Body_Node, Decls);
-
-         Proc_Decl := Make_Subprogram_Declaration (Loc,
-           Copy_Specification (Loc, Specification (Body_Node)));
-
-         Attr_Decl :=
-           Make_Attribute_Definition_Clause (Loc,
-             Name       => New_Occurrence_Of (RACW_Type, Loc),
-             Chars      => Name_Read,
-             Expression =>
-               New_Occurrence_Of (
-                 Defining_Unit_Name (Specification (Proc_Decl)), Loc));
-
-         Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
-         Insert_After (Proc_Decl, Attr_Decl);
          Append_To (Body_Decls, Body_Node);
       end Add_RACW_Read_Attribute;
 
@@ -3152,14 +3343,35 @@ package body Exp_Dist is
          Proc_Decl : Node_Id;
          Attr_Decl : Node_Id;
 
-         Statements        : List_Id;
+         Statements        : constant List_Id := New_List;
          Local_Statements  : List_Id;
          Remote_Statements : List_Id;
          Null_Statements   : List_Id;
 
-         Procedure_Name : constant Name_Id := New_Internal_Name ('R');
+         Pnam : constant Entity_Id := Make_Temporary (Loc, 'R');
 
       begin
+         Build_Stream_Procedure
+           (Loc, RACW_Type, Body_Node, Pnam, Statements, Outp => False);
+
+         Proc_Decl := Make_Subprogram_Declaration (Loc,
+           Copy_Specification (Loc, Specification (Body_Node)));
+
+         Attr_Decl :=
+           Make_Attribute_Definition_Clause (Loc,
+             Name       => New_Occurrence_Of (RACW_Type, Loc),
+             Chars      => Name_Write,
+             Expression =>
+               New_Occurrence_Of (
+                 Defining_Unit_Name (Specification (Proc_Decl)), Loc));
+
+         Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
+         Insert_After (Proc_Decl, Attr_Decl);
+
+         if No (Body_Decls) then
+            return;
+         end if;
+
          --  Build the code fragment corresponding to the marshalling of a
          --  local object.
 
@@ -3188,35 +3400,31 @@ package body Exp_Dist is
          --  a remote object.
 
          Remote_Statements := New_List (
-
            Pack_Node_Into_Stream_Access (Loc,
-            Stream => Stream_Parameter,
-            Object =>
+             Stream => Stream_Parameter,
+             Object =>
                Make_Selected_Component (Loc,
-                 Prefix        => Unchecked_Convert_To (Stub_Type_Access,
-                   Object),
-                 Selector_Name =>
-                   Make_Identifier (Loc, Name_Origin)),
-            Etyp   => RTE (RE_Partition_ID)),
+                 Prefix        =>
+                   Unchecked_Convert_To (Stub_Type_Access, Object),
+                 Selector_Name => Make_Identifier (Loc, Name_Origin)),
+            Etyp    => RTE (RE_Partition_ID)),
 
            Pack_Node_Into_Stream_Access (Loc,
             Stream => Stream_Parameter,
             Object =>
                Make_Selected_Component (Loc,
-                 Prefix        => Unchecked_Convert_To (Stub_Type_Access,
-                   Object),
-                 Selector_Name =>
-                   Make_Identifier (Loc, Name_Receiver)),
+                 Prefix        =>
+                   Unchecked_Convert_To (Stub_Type_Access, Object),
+                 Selector_Name => Make_Identifier (Loc, Name_Receiver)),
             Etyp   => RTE (RE_Unsigned_64)),
 
            Pack_Node_Into_Stream_Access (Loc,
             Stream => Stream_Parameter,
             Object =>
                Make_Selected_Component (Loc,
-                 Prefix        => Unchecked_Convert_To (Stub_Type_Access,
-                   Object),
-                 Selector_Name =>
-                   Make_Identifier (Loc, Name_Addr)),
+                 Prefix        =>
+                   Unchecked_Convert_To (Stub_Type_Access, Object),
+                 Selector_Name => Make_Identifier (Loc, Name_Addr)),
             Etyp   => RTE (RE_Unsigned_64)));
 
          --  Build code fragment corresponding to marshalling of a null object
@@ -3237,13 +3445,15 @@ package body Exp_Dist is
              Object => Make_Integer_Literal (Loc, Uint_0),
              Etyp   => RTE (RE_Unsigned_64)));
 
-         Statements := New_List (
+         Append_To (Statements,
            Make_Implicit_If_Statement (RACW_Type,
              Condition       =>
                Make_Op_Eq (Loc,
                  Left_Opnd  => Object,
                  Right_Opnd => Make_Null (Loc)),
+
              Then_Statements => Null_Statements,
+
              Elsif_Parts     => New_List (
                Make_Elsif_Part (Loc,
                  Condition       =>
@@ -3252,6 +3462,7 @@ package body Exp_Dist is
                        Make_Attribute_Reference (Loc,
                          Prefix         => Object,
                          Attribute_Name => Name_Tag),
+
                      Right_Opnd =>
                        Make_Attribute_Reference (Loc,
                          Prefix         => New_Occurrence_Of (Stub_Type, Loc),
@@ -3259,24 +3470,6 @@ package body Exp_Dist is
                  Then_Statements => Remote_Statements)),
              Else_Statements => Local_Statements));
 
-         Build_Stream_Procedure
-           (Loc, RACW_Type, Body_Node,
-            Make_Defining_Identifier (Loc, Procedure_Name),
-            Statements, Outp => False);
-
-         Proc_Decl := Make_Subprogram_Declaration (Loc,
-           Copy_Specification (Loc, Specification (Body_Node)));
-
-         Attr_Decl :=
-           Make_Attribute_Definition_Clause (Loc,
-             Name       => New_Occurrence_Of (RACW_Type, Loc),
-             Chars      => Name_Write,
-             Expression =>
-               New_Occurrence_Of (
-                 Defining_Unit_Name (Specification (Proc_Decl)), Loc));
-
-         Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
-         Insert_After (Proc_Decl, Attr_Decl);
          Append_To (Body_Decls, Body_Node);
       end Add_RACW_Write_Attribute;
 
@@ -3335,25 +3528,16 @@ package body Exp_Dist is
          Proc_Decls      : List_Id;
          Proc_Statements : List_Id;
 
-         Origin : constant Entity_Id :=
-                    Make_Defining_Identifier (Loc,
-                      Chars => New_Internal_Name ('P'));
+         Origin : constant Entity_Id := Make_Temporary (Loc, 'P');
 
          --  Additional local variables for the local case
 
-         Proxy_Addr : constant Entity_Id :=
-                        Make_Defining_Identifier (Loc,
-                          Chars => New_Internal_Name ('P'));
+         Proxy_Addr : constant Entity_Id := Make_Temporary (Loc, 'P');
 
          --  Additional local variables for the remote case
 
-         Local_Stub : constant Entity_Id :=
-                        Make_Defining_Identifier (Loc,
-                          Chars => New_Internal_Name ('L'));
-
-         Stub_Ptr : constant Entity_Id :=
-                      Make_Defining_Identifier (Loc,
-                        Chars => New_Internal_Name ('S'));
+         Local_Stub : constant Entity_Id := Make_Temporary (Loc, 'L');
+         Stub_Ptr   : constant Entity_Id := Make_Temporary (Loc, 'S');
 
          function Set_Field
            (Field_Name : Name_Id;
@@ -3384,7 +3568,7 @@ package body Exp_Dist is
       begin
          Proc_Decls := New_List (
 
-         --  Common declarations
+            --  Common declarations
 
            Make_Object_Declaration (Loc,
              Defining_Identifier => Origin,
@@ -3398,15 +3582,15 @@ package body Exp_Dist is
                  Parameter_Associations => New_List (
                    New_Occurrence_Of (Package_Name, Loc)))),
 
-         --  Declaration use only in the local case: proxy address
+            --  Declaration use only in the local case: proxy address
 
            Make_Object_Declaration (Loc,
              Defining_Identifier => Proxy_Addr,
              Object_Definition   =>
                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc)),
 
-         --  Declarations used only in the remote case: stub object and
-         --  stub pointer.
+            --  Declarations used only in the remote case: stub object and
+            --  stub pointer.
 
            Make_Object_Declaration (Loc,
              Defining_Identifier => Local_Stub,
@@ -3425,7 +3609,8 @@ package body Exp_Dist is
                  Attribute_Name => Name_Unchecked_Access)));
 
          Set_Etype (Stub_Ptr, Stub_Elements.Stub_Type_Access);
-         --  Build_Get_Unique_RP_Call needs this information
+
+         --  Build_Get_Unique_RP_Call needs above information
 
          --  Note: Here we assume that the Fat_Type is a record
          --  containing just a pointer to a proxy or stub object.
@@ -3442,8 +3627,7 @@ package body Exp_Dist is
          --    end if;
 
             Make_Procedure_Call_Statement (Loc,
-              Name =>
-                New_Occurrence_Of (RTE (RE_Get_RAS_Info), Loc),
+              Name => New_Occurrence_Of (RTE (RE_Get_RAS_Info), Loc),
               Parameter_Associations => New_List (
                 New_Occurrence_Of (Package_Name, Loc),
                 New_Occurrence_Of (Subp_Id, Loc),
@@ -3460,11 +3644,13 @@ package body Exp_Dist is
                        Make_Function_Call (Loc,
                          New_Occurrence_Of (
                            RTE (RE_Get_Local_Partition_Id), Loc))),
+
                  Right_Opnd =>
                    Make_Op_Not (Loc,
                      New_Occurrence_Of (All_Calls_Remote, Loc))),
+
              Then_Statements => New_List (
-               Make_Return_Statement (Loc,
+               Make_Simple_Return_Statement (Loc,
                  Unchecked_Convert_To (Fat_Type,
                    OK_Convert_To (RTE (RE_Address),
                      New_Occurrence_Of (Proxy_Addr, Loc)))))),
@@ -3481,12 +3667,12 @@ package body Exp_Dist is
 
            Set_Field (Name_Addr, New_Occurrence_Of (Proxy_Addr, Loc)),
 
-         --  E.4.1(9) A remote call is asynchronous if it is a call to
-         --  a procedure, or a call through a value of an access-to-procedure
-         --  type, to which a pragma Asynchronous applies.
+            --  E.4.1(9) A remote call is asynchronous if it is a call to
+            --  a procedure or a call through a value of an access-to-procedure
+            --  type to which a pragma Asynchronous applies.
 
-         --    Parameter Asynch_P is true when the procedure is asynchronous;
-         --    Expression Asynch_T is true when the type is asynchronous.
+            --  Asynch_P is true when the procedure is asynchronous;
+            --  Asynch_T is true when the type is asynchronous.
 
            Set_Field (Name_Asynchronous,
              Make_Or_Else (Loc,
@@ -3501,7 +3687,7 @@ package body Exp_Dist is
          --  Return the newly created value
 
          Append_To (Proc_Statements,
-           Make_Return_Statement (Loc,
+           Make_Simple_Return_Statement (Loc,
              Expression =>
                Unchecked_Convert_To (Fat_Type,
                  New_Occurrence_Of (Stub_Ptr, Loc))));
@@ -3558,9 +3744,7 @@ package body Exp_Dist is
         (Vis_Decl : Node_Id;
          RAS_Type : Entity_Id)
       is
-         pragma Warnings (Off);
          pragma Unreferenced (RAS_Type);
-         pragma Warnings (On);
       begin
          Add_RAS_Access_TSS (Vis_Decl);
       end Add_RAST_Features;
@@ -3579,32 +3763,26 @@ package body Exp_Dist is
          Request_Parameter : Node_Id;
 
          Pkg_RPC_Receiver            : constant Entity_Id :=
-                                         Make_Defining_Identifier (Loc,
-                                           New_Internal_Name ('H'));
+                                         Make_Temporary (Loc, 'H');
          Pkg_RPC_Receiver_Statements : List_Id;
          Pkg_RPC_Receiver_Cases      : constant List_Id := New_List;
          Pkg_RPC_Receiver_Body       : Node_Id;
          --  A Pkg_RPC_Receiver is built to decode the request
 
-         Lookup_RAS_Info : constant Entity_Id :=
-                             Make_Defining_Identifier (Loc,
-                               Chars => New_Internal_Name ('R'));
-         --  A remote subprogram is created to allow peers to look up
-         --  RAS information using subprogram ids.
+         Lookup_RAS      : Node_Id;
+         Lookup_RAS_Info : constant Entity_Id := Make_Temporary (Loc, 'R');
+         --  A remote subprogram is created to allow peers to look up RAS
+         --  information using subprogram ids.
 
          Subp_Id    : Entity_Id;
          Subp_Index : Entity_Id;
          --  Subprogram_Id as read from the incoming stream
 
-         Current_Declaration       : Node_Id;
-         Current_Subprogram_Number : Int := First_RCI_Subprogram_Id;
-         Current_Stubs             : Node_Id;
+         Current_Subp_Number : Int := First_RCI_Subprogram_Id;
+         Current_Stubs       : Node_Id;
 
-         Subp_Info_Array : constant Entity_Id :=
-                             Make_Defining_Identifier (Loc,
-                               Chars => New_Internal_Name ('I'));
-
-         Subp_Info_List : constant List_Id := New_List;
+         Subp_Info_Array : constant Entity_Id := Make_Temporary (Loc, 'I');
+         Subp_Info_List  : constant List_Id := New_List;
 
          Register_Pkg_Actuals : constant List_Id := New_List;
 
@@ -3619,6 +3797,9 @@ package body Exp_Dist is
          --  associating Subprogram_Number with the subprogram declared
          --  by Declaration, for which we have receiving stubs in Stubs.
 
+         procedure Visit_Subprogram (Decl : Node_Id);
+         --  Generate receiving stub for one remote subprogram
+
          ---------------------
          -- Append_Stubs_To --
          ---------------------
@@ -3637,12 +3818,81 @@ package body Exp_Dist is
                   New_List (
                     Make_Procedure_Call_Statement (Loc,
                       Name                   =>
-                        New_Occurrence_Of (
-                          Defining_Entity (Stubs), Loc),
+                        New_Occurrence_Of (Defining_Entity (Stubs), Loc),
                       Parameter_Associations => New_List (
                         New_Occurrence_Of (Request_Parameter, Loc))))));
          end Append_Stubs_To;
 
+         ----------------------
+         -- Visit_Subprogram --
+         ----------------------
+
+         procedure Visit_Subprogram (Decl : Node_Id) is
+            Loc      : constant Source_Ptr := Sloc (Decl);
+            Spec     : constant Node_Id    := Specification (Decl);
+            Subp_Def : constant Entity_Id  := Defining_Unit_Name (Spec);
+
+            Subp_Val : String_Id;
+            pragma Warnings (Off, Subp_Val);
+
+         begin
+            --  Build receiving stub
+
+            Current_Stubs :=
+              Build_Subprogram_Receiving_Stubs
+                (Vis_Decl     => Decl,
+                 Asynchronous =>
+                   Nkind (Spec) = N_Procedure_Specification
+                     and then Is_Asynchronous (Subp_Def));
+
+            Append_To (Decls, Current_Stubs);
+            Analyze (Current_Stubs);
+
+            --  Build RAS proxy
+
+            Add_RAS_Proxy_And_Analyze (Decls,
+              Vis_Decl           => Decl,
+              All_Calls_Remote_E => All_Calls_Remote_E,
+              Proxy_Object_Addr  => Proxy_Object_Addr);
+
+            --  Compute distribution identifier
+
+            Assign_Subprogram_Identifier
+              (Subp_Def, Current_Subp_Number,  Subp_Val);
+
+            pragma Assert (Current_Subp_Number = Get_Subprogram_Id (Subp_Def));
+
+            --  Add subprogram descriptor (RCI_Subp_Info) to the subprograms
+            --  table for this receiver. This aggregate must be kept consistent
+            --  with the declaration of RCI_Subp_Info in
+            --  System.Partition_Interface.
+
+            Append_To (Subp_Info_List,
+              Make_Component_Association (Loc,
+                Choices    => New_List (
+                  Make_Integer_Literal (Loc, Current_Subp_Number)),
+
+                Expression =>
+                  Make_Aggregate (Loc,
+                    Component_Associations => New_List (
+
+                      --  Addr =>
+
+                      Make_Component_Association (Loc,
+                        Choices    =>
+                          New_List (Make_Identifier (Loc, Name_Addr)),
+                        Expression =>
+                          New_Occurrence_Of (Proxy_Object_Addr, Loc))))));
+
+            Append_Stubs_To (Pkg_RPC_Receiver_Cases,
+                             Stubs             => Current_Stubs,
+                             Subprogram_Number => Current_Subp_Number);
+
+            Current_Subp_Number := Current_Subp_Number + 1;
+         end Visit_Subprogram;
+
+         procedure Visit_Spec is new Build_Package_Stubs (Visit_Subprogram);
+
       --  Start of processing for Add_Receiving_Stubs_To_Declarations
 
       begin
@@ -3681,10 +3931,12 @@ package body Exp_Dist is
                Make_Op_Eq (Loc,
                  New_Occurrence_Of (Subp_Id, Loc),
                  Make_Integer_Literal (Loc, 0)),
+
              Then_Statements => New_List (
                Make_Assignment_Statement (Loc,
                  Name =>
                    New_Occurrence_Of (Subp_Id, Loc),
+
                  Expression =>
                    Make_Selected_Component (Loc,
                      Prefix =>
@@ -3699,12 +3951,12 @@ package body Exp_Dist is
                                Make_Selected_Component (Loc,
                                  Prefix        => Request_Parameter,
                                  Selector_Name => Name_Params))))),
-                     Selector_Name =>
-                       Make_Identifier (Loc, Name_Subp_Id))))));
+
+                     Selector_Name => Make_Identifier (Loc, Name_Subp_Id))))));
 
          --  Build a subprogram for RAS information lookups
 
-         Current_Declaration :=
+         Lookup_RAS :=
            Make_Subprogram_Declaration (Loc,
              Specification =>
                Make_Function_Specification (Loc,
@@ -3720,112 +3972,30 @@ package body Exp_Dist is
                        New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc))),
                  Result_Definition =>
                    New_Occurrence_Of (RTE (RE_Unsigned_64), Loc)));
-         Append_To (Decls, Current_Declaration);
-         Analyze (Current_Declaration);
+         Append_To (Decls, Lookup_RAS);
+         Analyze (Lookup_RAS);
 
          Current_Stubs := Build_Subprogram_Receiving_Stubs
-           (Vis_Decl     => Current_Declaration,
+           (Vis_Decl     => Lookup_RAS,
             Asynchronous => False);
          Append_To (Decls, Current_Stubs);
          Analyze (Current_Stubs);
 
          Append_Stubs_To (Pkg_RPC_Receiver_Cases,
-           Stubs       =>
-             Current_Stubs,
+           Stubs             => Current_Stubs,
            Subprogram_Number => 1);
 
          --  For each subprogram, the receiving stub will be built and a
          --  case statement will be made on the Subprogram_Id to dispatch
          --  to the right subprogram.
 
-         All_Calls_Remote_E := Boolean_Literals (
-           Has_All_Calls_Remote (Defining_Entity (Pkg_Spec)));
+         All_Calls_Remote_E :=
+           Boolean_Literals
+             (Has_All_Calls_Remote (Defining_Entity (Pkg_Spec)));
 
          Overload_Counter_Table.Reset;
 
-         Current_Declaration := First (Visible_Declarations (Pkg_Spec));
-         while Present (Current_Declaration) loop
-            if Nkind (Current_Declaration) = N_Subprogram_Declaration
-              and then Comes_From_Source (Current_Declaration)
-            then
-               declare
-                  Loc : constant Source_Ptr :=
-                          Sloc (Current_Declaration);
-                  --  While specifically processing Current_Declaration, use
-                  --  its Sloc as the location of all generated nodes.
-
-                  Subp_Def : constant Entity_Id :=
-                               Defining_Unit_Name
-                                 (Specification (Current_Declaration));
-
-                  Subp_Val : String_Id;
-
-               begin
-                  --  Build receiving stub
-
-                  Current_Stubs :=
-                    Build_Subprogram_Receiving_Stubs
-                      (Vis_Decl     => Current_Declaration,
-                       Asynchronous =>
-                         Nkind (Specification (Current_Declaration)) =
-                             N_Procedure_Specification
-                           and then Is_Asynchronous (Subp_Def));
-
-                  Append_To (Decls, Current_Stubs);
-                  Analyze (Current_Stubs);
-
-                  --  Build RAS proxy
-
-                  Add_RAS_Proxy_And_Analyze (Decls,
-                    Vis_Decl           =>
-                      Current_Declaration,
-                    All_Calls_Remote_E =>
-                      All_Calls_Remote_E,
-                    Proxy_Object_Addr  =>
-                      Proxy_Object_Addr);
-
-                  --  Compute distribution identifier
-
-                  Assign_Subprogram_Identifier (
-                    Subp_Def,
-                    Current_Subprogram_Number,
-                    Subp_Val);
-
-                  pragma Assert (Current_Subprogram_Number =
-                    Get_Subprogram_Id (Subp_Def));
-
-                  --  Add subprogram descriptor (RCI_Subp_Info) to the
-                  --  subprograms table for this receiver. The aggregate
-                  --  below must be kept consistent with the declaration
-                  --  of type RCI_Subp_Info in System.Partition_Interface.
-
-                  Append_To (Subp_Info_List,
-                    Make_Component_Association (Loc,
-                      Choices => New_List (
-                        Make_Integer_Literal (Loc,
-                          Current_Subprogram_Number)),
-                      Expression =>
-                        Make_Aggregate (Loc,
-                          Component_Associations => New_List (
-                            Make_Component_Association (Loc,
-                              Choices => New_List (
-                                Make_Identifier (Loc, Name_Addr)),
-                              Expression =>
-                                New_Occurrence_Of (
-                                  Proxy_Object_Addr, Loc))))));
-
-                  Append_Stubs_To (Pkg_RPC_Receiver_Cases,
-                    Stubs =>
-                      Current_Stubs,
-                    Subprogram_Number =>
-                      Current_Subprogram_Number);
-               end;
-
-               Current_Subprogram_Number := Current_Subprogram_Number + 1;
-            end if;
-
-            Next (Current_Declaration);
-         end loop;
+         Visit_Spec (Pkg_Spec);
 
          --  If we receive an invalid Subprogram_Id, it is best to do nothing
          --  rather than raising an exception since we do not want someone
@@ -3837,15 +4007,12 @@ package body Exp_Dist is
 
          Append_To (Pkg_RPC_Receiver_Cases,
            Make_Case_Statement_Alternative (Loc,
-             Discrete_Choices =>
-               New_List (Make_Others_Choice (Loc)),
-             Statements       =>
-               New_List (Make_Null_Statement (Loc))));
+             Discrete_Choices => New_List (Make_Others_Choice (Loc)),
+             Statements       => New_List (Make_Null_Statement (Loc))));
 
          Append_To (Pkg_RPC_Receiver_Statements,
            Make_Case_Statement (Loc,
-             Expression   =>
-               New_Occurrence_Of (Subp_Id, Loc),
+             Expression   => New_Occurrence_Of (Subp_Id, Loc),
              Alternatives => Pkg_RPC_Receiver_Cases));
 
          Append_To (Decls,
@@ -3865,8 +4032,9 @@ package body Exp_Dist is
                            First_RCI_Subprogram_Id),
                          High_Bound =>
                            Make_Integer_Literal (Loc,
-                             First_RCI_Subprogram_Id
-                             + List_Length (Subp_Info_List) - 1)))))));
+                             Intval =>
+                               First_RCI_Subprogram_Id
+                               + List_Length (Subp_Info_List) - 1)))))));
 
          --  For a degenerate RCI with no visible subprograms, Subp_Info_List
          --  has zero length, and the declaration is for an empty array, in
@@ -3897,13 +4065,11 @@ package body Exp_Dist is
                  Make_Selected_Component (Loc,
                    Prefix =>
                      Make_Indexed_Component (Loc,
-                       Prefix =>
-                         New_Occurrence_Of (Subp_Info_Array, Loc),
+                       Prefix => New_Occurrence_Of (Subp_Info_Array, Loc),
                        Expressions => New_List (
                          Convert_To (Standard_Integer,
                            Make_Identifier (Loc, Name_Subp_Id)))),
-                   Selector_Name =>
-                     Make_Identifier (Loc, Name_Addr));
+                   Selector_Name => Make_Identifier (Loc, Name_Addr));
 
             --  Case of no visible subprogram: just raise Constraint_Error, we
             --  know for sure we got junk from a remote partition.
@@ -3919,15 +4085,14 @@ package body Exp_Dist is
               Make_Subprogram_Body (Loc,
                 Specification =>
                   Copy_Specification (Loc, Parent (Lookup_RAS_Info)),
-                Declarations =>
-                  No_List,
+                Declarations  => No_List,
                 Handled_Statement_Sequence =>
                   Make_Handled_Sequence_Of_Statements (Loc,
                     Statements => New_List (
-                      Make_Return_Statement (Loc,
+                      Make_Simple_Return_Statement (Loc,
                         Expression =>
-                          OK_Convert_To (RTE (RE_Unsigned_64),
-                                         Subp_Info_Addr))))));
+                          OK_Convert_To
+                            (RTE (RE_Unsigned_64), Subp_Info_Addr))))));
          end;
 
          Analyze (Last (Decls));
@@ -3947,10 +4112,8 @@ package body Exp_Dist is
 
          Append_To (Register_Pkg_Actuals,
            Make_Attribute_Reference (Loc,
-             Prefix         =>
-               New_Occurrence_Of (Pkg_RPC_Receiver, Loc),
-             Attribute_Name =>
-               Name_Unrestricted_Access));
+             Prefix         => New_Occurrence_Of (Pkg_RPC_Receiver, Loc),
+             Attribute_Name => Name_Unrestricted_Access));
 
          --  Version
 
@@ -3958,26 +4121,21 @@ package body Exp_Dist is
            Make_Attribute_Reference (Loc,
              Prefix         =>
                New_Occurrence_Of (Defining_Entity (Pkg_Spec), Loc),
-             Attribute_Name =>
-               Name_Version));
+             Attribute_Name => Name_Version));
 
          --  Subp_Info
 
          Append_To (Register_Pkg_Actuals,
            Make_Attribute_Reference (Loc,
-             Prefix         =>
-               New_Occurrence_Of (Subp_Info_Array, Loc),
-             Attribute_Name =>
-               Name_Address));
+             Prefix         => New_Occurrence_Of (Subp_Info_Array, Loc),
+             Attribute_Name => Name_Address));
 
          --  Subp_Info_Len
 
          Append_To (Register_Pkg_Actuals,
            Make_Attribute_Reference (Loc,
-             Prefix         =>
-               New_Occurrence_Of (Subp_Info_Array, Loc),
-             Attribute_Name =>
-               Name_Length));
+             Prefix         => New_Occurrence_Of (Subp_Info_Array, Loc),
+             Attribute_Name => Name_Length));
 
          --  Generate the call
 
@@ -4011,8 +4169,8 @@ package body Exp_Dist is
          Loc : constant Source_Ptr := Sloc (Nod);
 
          Stream_Parameter : Node_Id;
-         --  Name of the stream used to transmit parameters to the
-         --  remote package.
+         --  Name of the stream used to transmit parameters to the remote
+         --  package.
 
          Result_Parameter : Node_Id;
          --  Name of the result parameter (in non-APC cases) which get the
@@ -4036,10 +4194,8 @@ package body Exp_Dist is
          --  List of statements for extra formal parameters. It will appear
          --  after the regular statements for writing out parameters.
 
-         pragma Warnings (Off);
          pragma Unreferenced (RACW_Type);
          --  Used only for the PolyORB case
-         pragma Warnings (On);
 
       begin
          --  The general form of a calling stub for a given subprogram is:
@@ -4059,8 +4215,7 @@ package body Exp_Dist is
          --  well as the declaration of Result. For a function call, 'Input is
          --  always used to read the result even if it is constrained.
 
-         Stream_Parameter :=
-           Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
+         Stream_Parameter := Make_Temporary (Loc, 'S');
 
          Append_To (Decls,
            Make_Object_Declaration (Loc,
@@ -4076,8 +4231,7 @@ package body Exp_Dist is
                        New_List (Make_Integer_Literal (Loc, 0))))));
 
          if not Is_Known_Asynchronous then
-            Result_Parameter :=
-              Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
+            Result_Parameter := Make_Temporary (Loc, 'R');
 
             Append_To (Decls,
               Make_Object_Declaration (Loc,
@@ -4092,8 +4246,7 @@ package body Exp_Dist is
                         Constraints =>
                           New_List (Make_Integer_Literal (Loc, 0))))));
 
-            Exception_Return_Parameter :=
-              Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
+            Exception_Return_Parameter := Make_Temporary (Loc, 'E');
 
             Append_To (Decls,
               Make_Object_Declaration (Loc,
@@ -4115,10 +4268,8 @@ package body Exp_Dist is
              Attribute_Name => Name_Write,
              Expressions    => New_List (
                Make_Attribute_Reference (Loc,
-                 Prefix         =>
-                   New_Occurrence_Of (Stream_Parameter, Loc),
-                 Attribute_Name =>
-                   Name_Access),
+                 Prefix         => New_Occurrence_Of (Stream_Parameter, Loc),
+                 Attribute_Name => Name_Access),
                Target_RPC_Receiver)));
 
          --  Then put the Subprogram_Id of the subprogram we want to call in
@@ -4126,14 +4277,11 @@ package body Exp_Dist is
 
          Append_To (Statements,
            Make_Attribute_Reference (Loc,
-             Prefix         =>
-               New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc),
-             Attribute_Name =>
-               Name_Write,
+             Prefix         => New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc),
+             Attribute_Name => Name_Write,
              Expressions      => New_List (
                Make_Attribute_Reference (Loc,
-                 Prefix         =>
-                   New_Occurrence_Of (Stream_Parameter, Loc),
+                 Prefix         => New_Occurrence_Of (Stream_Parameter, Loc),
                  Attribute_Name => Name_Access),
                Subprogram_Id)));
 
@@ -4149,7 +4297,7 @@ package body Exp_Dist is
 
             begin
                if Is_RACW_Controlling_Formal
-                 (Current_Parameter, Stub_Type)
+                    (Current_Parameter, Stub_Type)
                then
                   --  In the case of a controlling formal argument, we marshall
                   --  its addr field rather than the local stub.
@@ -4165,8 +4313,9 @@ package body Exp_Dist is
                        Etyp   => RTE (RE_Unsigned_64)));
 
                else
-                  Value := New_Occurrence_Of
-                    (Defining_Identifier (Current_Parameter), Loc);
+                  Value :=
+                    New_Occurrence_Of
+                      (Defining_Identifier (Current_Parameter), Loc);
 
                   --  Access type parameters are transmitted as in out
                   --  parameters. However, a dereference is needed so that
@@ -4179,8 +4328,7 @@ package body Exp_Dist is
                      Etyp := Etype (Typ);
                   end if;
 
-                  Constrained :=
-                    Is_Constrained (Etyp) or else Is_Elementary_Type (Etyp);
+                  Constrained := not Transmit_As_Unconstrained (Etyp);
 
                   --  Any parameter but unconstrained out parameters are
                   --  transmitted to the peer.
@@ -4191,8 +4339,7 @@ package body Exp_Dist is
                   then
                      Append_To (Statements,
                        Make_Attribute_Reference (Loc,
-                         Prefix         =>
-                           New_Occurrence_Of (Etyp, Loc),
+                         Prefix         => New_Occurrence_Of (Etyp, Loc),
                          Attribute_Name =>
                            Output_From_Constrained (Constrained),
                          Expressions    => New_List (
@@ -4218,8 +4365,7 @@ package body Exp_Dist is
                   --  type and push it in the stream after the regular
                   --  parameters.
 
-                  Extra_Parameter := Make_Defining_Identifier
-                                       (Loc, New_Internal_Name ('P'));
+                  Extra_Parameter := Make_Temporary (Loc, 'P');
 
                   Append_To (Decls,
                      Make_Object_Declaration (Loc,
@@ -4238,13 +4384,12 @@ package body Exp_Dist is
                      Make_Attribute_Reference (Loc,
                        Prefix         =>
                          New_Occurrence_Of (Standard_Boolean, Loc),
-                       Attribute_Name =>
-                         Name_Write,
+                       Attribute_Name => Name_Write,
                        Expressions    => New_List (
                          Make_Attribute_Reference (Loc,
                            Prefix         =>
-                             New_Occurrence_Of (Stream_Parameter, Loc),
-                           Attribute_Name =>
+                             New_Occurrence_Of
+                              (Stream_Parameter, Loc), Attribute_Name =>
                              Name_Access),
                          New_Occurrence_Of (Extra_Parameter, Loc))));
                end if;
@@ -4270,8 +4415,7 @@ package body Exp_Dist is
                   Make_Attribute_Reference (Loc,
                     Prefix         =>
                       New_Occurrence_Of (Stream_Parameter, Loc),
-                    Attribute_Name =>
-                      Name_Access))));
+                    Attribute_Name => Name_Access))));
          else
             Asynchronous_Statements := No_List;
          end if;
@@ -4290,14 +4434,12 @@ package body Exp_Dist is
                   Make_Attribute_Reference (Loc,
                     Prefix         =>
                       New_Occurrence_Of (Stream_Parameter, Loc),
-                    Attribute_Name =>
-                      Name_Access),
+                    Attribute_Name => Name_Access),
 
                   Make_Attribute_Reference (Loc,
                     Prefix         =>
                       New_Occurrence_Of (Result_Parameter, Loc),
-                    Attribute_Name =>
-                      Name_Access))));
+                    Attribute_Name => Name_Access))));
 
             --  Read the exception occurrence from the result stream and
             --  reraise it. It does no harm if this is a Null_Occurrence since
@@ -4308,15 +4450,13 @@ package body Exp_Dist is
                 Prefix         =>
                   New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc),
 
-                Attribute_Name =>
-                  Name_Read,
+                Attribute_Name => Name_Read,
 
                 Expressions    => New_List (
                   Make_Attribute_Reference (Loc,
                     Prefix         =>
                       New_Occurrence_Of (Result_Parameter, Loc),
-                    Attribute_Name =>
-                      Name_Access),
+                    Attribute_Name => Name_Access),
                   New_Occurrence_Of (Exception_Return_Parameter, Loc))));
 
             Append_To (Non_Asynchronous_Statements,
@@ -4333,7 +4473,7 @@ package body Exp_Dist is
 
                Append_To (Non_Asynchronous_Statements,
                  Make_Tag_Check (Loc,
-                   Make_Return_Statement (Loc,
+                   Make_Simple_Return_Statement (Loc,
                      Expression =>
                        Make_Attribute_Reference (Loc,
                          Prefix         =>
@@ -4351,8 +4491,8 @@ package body Exp_Dist is
             else
                --  Loop around parameters and assign out (or in out)
                --  parameters. In the case of RACW, controlling arguments
-               --  cannot possibly have changed since they are remote, so we do
-               --  not read them from the stream.
+               --  cannot possibly have changed since they are remote, so
+               --  we do not read them from the stream.
 
                Current_Parameter := First (Ordered_Parameters_List);
                while Present (Current_Parameter) loop
@@ -4389,8 +4529,7 @@ package body Exp_Dist is
                                Make_Attribute_Reference (Loc,
                                  Prefix         =>
                                    New_Occurrence_Of (Result_Parameter, Loc),
-                                 Attribute_Name =>
-                                   Name_Access),
+                                 Attribute_Name => Name_Access),
                                Value)));
                      end if;
                   end;
@@ -4463,7 +4602,7 @@ package body Exp_Dist is
              (RPC_Receiver      => RPC_Receiver,
               Request_Parameter => Request);
 
-         Subp_Id    := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
+         Subp_Id    := Make_Temporary (Loc, 'P');
          Subp_Index := Subp_Id;
 
          --  Subp_Id may not be a constant, because in the case of the RPC
@@ -4481,9 +4620,9 @@ package body Exp_Dist is
                    New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc),
                  Attribute_Name  => Name_Input,
                  Expressions     => New_List (
-                                      Make_Selected_Component (Loc,
-                                        Prefix        => Request,
-                                        Selector_Name => Name_Params)))));
+                   Make_Selected_Component (Loc,
+                     Prefix        => Request,
+                     Selector_Name => Name_Params)))));
 
          Stmts := New_List;
 
@@ -4507,9 +4646,10 @@ package body Exp_Dist is
          Controlling_Parameter : Entity_Id) return RPC_Target
       is
          Target_Info : RPC_Target (PCS_Kind => Name_GARLIC_DSA);
+
       begin
-         Target_Info.Partition :=
-           Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
+         Target_Info.Partition := Make_Temporary (Loc, 'P');
+
          if Present (Controlling_Parameter) then
             Append_To (Decls,
               Make_Object_Declaration (Loc,
@@ -4561,62 +4701,49 @@ package body Exp_Dist is
 
       procedure Build_Stub_Type
         (RACW_Type         : Entity_Id;
-         Stub_Type         : Entity_Id;
-         Stub_Type_Decl    : out Node_Id;
+         Stub_Type_Comps   : out List_Id;
          RPC_Receiver_Decl : out Node_Id)
       is
-         Loc    : constant Source_Ptr := Sloc (Stub_Type);
+         Loc    : constant Source_Ptr := Sloc (RACW_Type);
          Is_RAS : constant Boolean    := not Comes_From_Source (RACW_Type);
 
       begin
-         Stub_Type_Decl :=
-           Make_Full_Type_Declaration (Loc,
-             Defining_Identifier => Stub_Type,
-             Type_Definition     =>
-               Make_Record_Definition (Loc,
-                 Tagged_Present  => True,
-                 Limited_Present => True,
-                 Component_List  =>
-                   Make_Component_List (Loc,
-                     Component_Items => New_List (
-
-                       Make_Component_Declaration (Loc,
-                         Defining_Identifier =>
-                           Make_Defining_Identifier (Loc, Name_Origin),
-                         Component_Definition =>
-                           Make_Component_Definition (Loc,
-                             Aliased_Present    => False,
-                             Subtype_Indication =>
-                               New_Occurrence_Of (
-                                 RTE (RE_Partition_ID), Loc))),
-
-                       Make_Component_Declaration (Loc,
-                         Defining_Identifier =>
-                           Make_Defining_Identifier (Loc, Name_Receiver),
-                         Component_Definition =>
-                           Make_Component_Definition (Loc,
-                             Aliased_Present    => False,
-                             Subtype_Indication =>
-                               New_Occurrence_Of (RTE (RE_Unsigned_64), Loc))),
-
-                       Make_Component_Declaration (Loc,
-                         Defining_Identifier =>
-                           Make_Defining_Identifier (Loc, Name_Addr),
-                         Component_Definition =>
-                           Make_Component_Definition (Loc,
-                             Aliased_Present    => False,
-                             Subtype_Indication =>
-                               New_Occurrence_Of (RTE (RE_Unsigned_64), Loc))),
-
-                       Make_Component_Declaration (Loc,
-                         Defining_Identifier =>
-                           Make_Defining_Identifier (Loc, Name_Asynchronous),
-                         Component_Definition =>
-                           Make_Component_Definition (Loc,
-                             Aliased_Present    => False,
-                             Subtype_Indication =>
-                               New_Occurrence_Of (
-                                 Standard_Boolean, Loc)))))));
+         Stub_Type_Comps := New_List (
+           Make_Component_Declaration (Loc,
+             Defining_Identifier =>
+               Make_Defining_Identifier (Loc, Name_Origin),
+             Component_Definition =>
+               Make_Component_Definition (Loc,
+                 Aliased_Present    => False,
+                 Subtype_Indication =>
+                   New_Occurrence_Of (RTE (RE_Partition_ID), Loc))),
+
+           Make_Component_Declaration (Loc,
+             Defining_Identifier =>
+               Make_Defining_Identifier (Loc, Name_Receiver),
+             Component_Definition =>
+               Make_Component_Definition (Loc,
+                 Aliased_Present    => False,
+                 Subtype_Indication =>
+                   New_Occurrence_Of (RTE (RE_Unsigned_64), Loc))),
+
+           Make_Component_Declaration (Loc,
+             Defining_Identifier =>
+               Make_Defining_Identifier (Loc, Name_Addr),
+             Component_Definition =>
+               Make_Component_Definition (Loc,
+                 Aliased_Present    => False,
+                 Subtype_Indication =>
+                   New_Occurrence_Of (RTE (RE_Unsigned_64), Loc))),
+
+           Make_Component_Declaration (Loc,
+             Defining_Identifier =>
+               Make_Defining_Identifier (Loc, Name_Asynchronous),
+             Component_Definition =>
+               Make_Component_Definition (Loc,
+                 Aliased_Present    => False,
+                 Subtype_Indication =>
+                   New_Occurrence_Of (Standard_Boolean, Loc))));
 
          if Is_RAS then
             RPC_Receiver_Decl := Empty;
@@ -4627,10 +4754,9 @@ package body Exp_Dist is
             begin
                RPC_Receiver_Decl :=
                  Make_Subprogram_Declaration (Loc,
-                   Build_RPC_Receiver_Specification (
-                     RPC_Receiver      => Make_Defining_Identifier (Loc,
-                                            New_Internal_Name ('R')),
-                     Request_Parameter => RPC_Receiver_Request));
+                   Build_RPC_Receiver_Specification
+                     (RPC_Receiver      => Make_Temporary (Loc, 'R'),
+                      Request_Parameter => RPC_Receiver_Request));
             end;
          end if;
       end Build_Stub_Type;
@@ -4649,9 +4775,7 @@ package body Exp_Dist is
       is
          Loc : constant Source_Ptr := Sloc (Vis_Decl);
 
-         Request_Parameter : constant Entity_Id :=
-                               Make_Defining_Identifier (Loc,
-                                 New_Internal_Name ('R'));
+         Request_Parameter : constant Entity_Id := Make_Temporary (Loc, 'R');
          --  Formal parameter for receiving stubs: a descriptor for an incoming
          --  request.
 
@@ -4704,8 +4828,7 @@ package body Exp_Dist is
          end if;
 
          if Dynamically_Asynchronous then
-            Dynamic_Async :=
-              Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
+            Dynamic_Async := Make_Temporary (Loc, 'S');
          else
             Dynamic_Async := Empty;
          end if;
@@ -4721,9 +4844,9 @@ package body Exp_Dist is
                   New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc),
                 Attribute_Name => Name_Write,
                 Expressions    => New_List (
-                                    Make_Selected_Component (Loc,
-                                      Prefix        => Request_Parameter,
-                                      Selector_Name => Name_Result),
+                  Make_Selected_Component (Loc,
+                    Prefix        => Request_Parameter,
+                    Selector_Name => Name_Result),
                   New_Occurrence_Of (RTE (RE_Null_Occurrence), Loc)));
 
             if Dynamically_Asynchronous then
@@ -4750,9 +4873,7 @@ package body Exp_Dist is
                Need_Extra_Constrained : Boolean;
                --  True when an Extra_Constrained actual is required
 
-               Object : constant Entity_Id :=
-                          Make_Defining_Identifier (Loc,
-                            New_Internal_Name ('P'));
+               Object : constant Entity_Id := Make_Temporary (Loc, 'P');
 
                Expr : Node_Id := Empty;
 
@@ -4772,8 +4893,7 @@ package body Exp_Dist is
                   Etyp := Etype (Parameter_Type (Current_Parameter));
                end if;
 
-               Constrained :=
-                 Is_Constrained (Etyp) or else Is_Elementary_Type (Etyp);
+               Constrained := not Transmit_As_Unconstrained (Etyp);
 
                if In_Present (Current_Parameter)
                  or else not Out_Present (Current_Parameter)
@@ -4807,15 +4927,19 @@ package body Exp_Dist is
                      Append_To (Decls,
                        Input_With_Tag_Check (Loc,
                          Var_Type => Etyp,
-                         Stream   => Make_Selected_Component (Loc,
-                                       Prefix        => Request_Parameter,
-                                       Selector_Name => Name_Params)));
+                         Stream   =>
+                           Make_Selected_Component (Loc,
+                             Prefix        => Request_Parameter,
+                             Selector_Name => Name_Params)));
 
                      --  Prepare function call expression
 
-                     Expr := Make_Function_Call (Loc,
-                       New_Occurrence_Of (Defining_Unit_Name
-                         (Specification (Last (Decls))), Loc));
+                     Expr :=
+                       Make_Function_Call (Loc,
+                         Name =>
+                           New_Occurrence_Of
+                             (Defining_Unit_Name
+                               (Specification (Last (Decls))), Loc));
                   end if;
                end if;
 
@@ -4968,9 +5092,8 @@ package body Exp_Dist is
             declare
                Etyp   : constant Entity_Id :=
                           Etype (Result_Definition (Specification (Vis_Decl)));
-               Result : constant Node_Id   :=
-                          Make_Defining_Identifier (Loc,
-                             New_Internal_Name ('R'));
+               Result : constant Node_Id   := Make_Temporary (Loc, 'R');
+
             begin
                Inner_Decls := New_List (
                  Make_Object_Declaration (Loc,
@@ -5056,8 +5179,7 @@ package body Exp_Dist is
             --  exception occurrence is copied into the output stream and
             --  no other output parameter is written.
 
-            Excep_Choice :=
-              Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
+            Excep_Choice := Make_Temporary (Loc, 'E');
 
             Excep_Code := New_List (
               Make_Attribute_Reference (Loc,
@@ -5088,8 +5210,7 @@ package body Exp_Dist is
 
          Subp_Spec :=
            Make_Procedure_Specification (Loc,
-             Defining_Unit_Name       =>
-               Make_Defining_Identifier (Loc, New_Internal_Name ('F')),
+             Defining_Unit_Name       => Make_Temporary (Loc, 'F'),
 
              Parameter_Specifications => New_List (
                Make_Parameter_Specification (Loc,
@@ -5132,7 +5253,9 @@ package body Exp_Dist is
    -------------------------------
 
    function Get_And_Reset_RACW_Bodies (RACW_Type : Entity_Id) return List_Id is
-      Desig : constant Entity_Id := Etype (Designated_Type (RACW_Type));
+      Desig         : constant Entity_Id :=
+                        Etype (Designated_Type (RACW_Type));
+
       Stub_Elements : Stub_Structure := Stubs_Table.Get (Desig);
 
       Body_Decls : List_Id;
@@ -5154,6 +5277,19 @@ package body Exp_Dist is
    end Get_And_Reset_RACW_Bodies;
 
    -----------------------
+   -- Get_Stub_Elements --
+   -----------------------
+
+   function Get_Stub_Elements (RACW_Type : Entity_Id) return Stub_Structure is
+      Desig         : constant Entity_Id :=
+                        Etype (Designated_Type (RACW_Type));
+      Stub_Elements : constant Stub_Structure := Stubs_Table.Get (Desig);
+   begin
+      pragma Assert (Stub_Elements /= Empty_Stub_Structure);
+      return Stub_Elements;
+   end Get_Stub_Elements;
+
+   -----------------------
    -- Get_Subprogram_Id --
    -----------------------
 
@@ -5210,15 +5346,15 @@ package body Exp_Dist is
    begin
       return
         Make_Subprogram_Body (Loc,
-          Specification              => Make_Function_Specification (Loc,
-            Defining_Unit_Name =>
-              Make_Defining_Identifier (Loc, New_Internal_Name ('S')),
-            Result_Definition  => New_Occurrence_Of (Var_Type, Loc)),
+          Specification              =>
+            Make_Function_Specification (Loc,
+              Defining_Unit_Name => Make_Temporary (Loc, 'S'),
+              Result_Definition  => New_Occurrence_Of (Var_Type, Loc)),
           Declarations               => No_List,
           Handled_Statement_Sequence =>
             Make_Handled_Sequence_Of_Statements (Loc, New_List (
               Make_Tag_Check (Loc,
-                Make_Return_Statement (Loc,
+                Make_Simple_Return_Statement (Loc,
                   Make_Attribute_Reference (Loc,
                     Prefix         => New_Occurrence_Of (Var_Type, Loc),
                     Attribute_Name => Name_Input,
@@ -5237,15 +5373,15 @@ package body Exp_Dist is
       Typ : Entity_Id;
 
    begin
-      --  If the kind of the parameter is E_Void, then it is not a
-      --  controlling formal (this can happen in the context of RAS).
+      --  If the kind of the parameter is E_Void, then it is not a controlling
+      --  formal (this can happen in the context of RAS).
 
       if Ekind (Defining_Identifier (Parameter)) = E_Void then
          return False;
       end if;
 
-      --  If the parameter is not a controlling formal, then it cannot
-      --  be possibly a RACW_Controlling_Formal.
+      --  If the parameter is not a controlling formal, then it cannot be
+      --  possibly a RACW_Controlling_Formal.
 
       if not Is_Controlling_Formal (Defining_Identifier (Parameter)) then
          return False;
@@ -5296,8 +5432,7 @@ package body Exp_Dist is
    --------------------
 
    function Make_Tag_Check (Loc : Source_Ptr; N : Node_Id) return Node_Id is
-      Occ : constant Entity_Id :=
-              Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
+      Occ : constant Entity_Id := Make_Temporary (Loc, 'E');
 
    begin
       return Make_Block_Statement (Loc,
@@ -5427,7 +5562,8 @@ package body Exp_Dist is
          Body_Decls       : List_Id);
       --  Add Read attribute for the RACW type. The declaration and attribute
       --  definition clauses are inserted right after the declaration of
-      --  RACW_Type, while the subprogram body is appended to Body_Decls.
+      --  RACW_Type. If Body_Decls is not No_List, the subprogram body is
+      --  appended to it (case where the RACW declaration is in the main unit).
 
       procedure Add_RACW_Write_Attribute
         (RACW_Type        : Entity_Id;
@@ -5438,16 +5574,11 @@ package body Exp_Dist is
 
       procedure Add_RACW_From_Any
         (RACW_Type        : Entity_Id;
-         Stub_Type        : Entity_Id;
-         Stub_Type_Access : Entity_Id;
          Body_Decls       : List_Id);
       --  Add the From_Any TSS for this RACW type
 
       procedure Add_RACW_To_Any
-        (Designated_Type  : Entity_Id;
-         RACW_Type        : Entity_Id;
-         Stub_Type        : Entity_Id;
-         Stub_Type_Access : Entity_Id;
+        (RACW_Type        : Entity_Id;
          Body_Decls       : List_Id);
       --  Add the To_Any TSS for this RACW type
 
@@ -5493,7 +5624,7 @@ package body Exp_Dist is
                --  Name
 
                 Make_String_Literal (Loc,
-                  Full_Qualified_Name (Desig)),
+                  Fully_Qualified_Name_String (Desig)),
 
                --  Handler
 
@@ -5527,28 +5658,17 @@ package body Exp_Dist is
          RPC_Receiver_Decl : Node_Id;
          Body_Decls        : List_Id)
       is
-         pragma Warnings (Off);
          pragma Unreferenced (RPC_Receiver_Decl);
-         pragma Warnings (On);
 
       begin
          Add_RACW_From_Any
            (RACW_Type           => RACW_Type,
-            Stub_Type           => Stub_Type,
-            Stub_Type_Access    => Stub_Type_Access,
             Body_Decls          => Body_Decls);
 
          Add_RACW_To_Any
-           (Designated_Type     => Desig,
-            RACW_Type           => RACW_Type,
-            Stub_Type           => Stub_Type,
-            Stub_Type_Access    => Stub_Type_Access,
+           (RACW_Type           => RACW_Type,
             Body_Decls          => Body_Decls);
 
-         --  In the PolyORB case, the RACW 'Read and 'Write attributes are
-         --  implemented in terms of the From_Any and To_Any TSSs, so these
-         --  TSSs must be expanded before 'Read and 'Write.
-
          Add_RACW_Write_Attribute
            (RACW_Type           => RACW_Type,
             Stub_Type           => Stub_Type,
@@ -5573,158 +5693,51 @@ package body Exp_Dist is
 
       procedure Add_RACW_From_Any
         (RACW_Type        : Entity_Id;
-         Stub_Type        : Entity_Id;
-         Stub_Type_Access : Entity_Id;
          Body_Decls       : List_Id)
       is
          Loc    : constant Source_Ptr := Sloc (RACW_Type);
          Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
-
          Fnam   : constant Entity_Id :=
-                    Make_Defining_Identifier (Loc, New_Internal_Name ('F'));
+                    Make_Defining_Identifier (Loc,
+                      Chars => New_External_Name (Chars (RACW_Type), 'F'));
 
          Func_Spec : Node_Id;
          Func_Decl : Node_Id;
          Func_Body : Node_Id;
 
-         Decls            : List_Id;
          Statements       : List_Id;
-         Stub_Statements  : List_Id;
-         Local_Statements : List_Id;
          --  Various parts of the subprogram
 
-         Any_Parameter  : constant Entity_Id :=
-                            Make_Defining_Identifier (Loc, Name_A);
-         Reference      : constant Entity_Id :=
-                            Make_Defining_Identifier
-                              (Loc, New_Internal_Name ('R'));
-         Is_Local       : constant Entity_Id  :=
-                            Make_Defining_Identifier
-                              (Loc, New_Internal_Name ('L'));
-         Addr           : constant Entity_Id  :=
-                            Make_Defining_Identifier
-                              (Loc, New_Internal_Name ('A'));
-         Local_Stub     : constant Entity_Id  :=
-                            Make_Defining_Identifier
-                              (Loc, New_Internal_Name ('L'));
-         Stubbed_Result : constant Entity_Id  :=
-                            Make_Defining_Identifier
-                              (Loc, New_Internal_Name ('S'));
-
-         Stub_Condition : Node_Id;
-         --  An expression that determines whether we create a stub for the
-         --  newly-unpacked RACW. Normally we create a stub only for remote
-         --  objects, but in the case of an RACW used to implement a RAS, we
-         --  also create a stub for local subprograms if a pragma
-         --  All_Calls_Remote applies.
+         Any_Parameter : constant Entity_Id :=
+                           Make_Defining_Identifier (Loc, Name_A);
 
          Asynchronous_Flag : constant Entity_Id :=
                                Asynchronous_Flags_Table.Get (RACW_Type);
          --  The flag object declared in Add_RACW_Asynchronous_Flag
 
       begin
+         Func_Spec :=
+           Make_Function_Specification (Loc,
+             Defining_Unit_Name =>
+               Fnam,
+             Parameter_Specifications => New_List (
+               Make_Parameter_Specification (Loc,
+                 Defining_Identifier =>
+                   Any_Parameter,
+                 Parameter_Type =>
+                   New_Occurrence_Of (RTE (RE_Any), Loc))),
+             Result_Definition => New_Occurrence_Of (RACW_Type, Loc));
 
-         --  Object declarations
-
-         Decls := New_List (
-           Make_Object_Declaration (Loc,
-             Defining_Identifier =>
-               Reference,
-             Object_Definition =>
-               New_Occurrence_Of (RTE (RE_Object_Ref), Loc),
-             Expression =>
-               Make_Function_Call (Loc,
-                 Name =>
-                   New_Occurrence_Of (RTE (RE_FA_ObjRef), Loc),
-                 Parameter_Associations => New_List (
-                   New_Occurrence_Of (Any_Parameter, Loc)))),
-
-           Make_Object_Declaration (Loc,
-             Defining_Identifier => Local_Stub,
-             Aliased_Present     => True,
-             Object_Definition   => New_Occurrence_Of (Stub_Type, Loc)),
-
-           Make_Object_Declaration (Loc,
-             Defining_Identifier => Stubbed_Result,
-             Object_Definition   =>
-               New_Occurrence_Of (Stub_Type_Access, Loc),
-             Expression          =>
-               Make_Attribute_Reference (Loc,
-                 Prefix =>
-                   New_Occurrence_Of (Local_Stub, Loc),
-                 Attribute_Name =>
-                   Name_Unchecked_Access)),
-
-           Make_Object_Declaration (Loc,
-             Defining_Identifier => Is_Local,
-             Object_Definition   =>
-               New_Occurrence_Of (Standard_Boolean, Loc)),
-
-           Make_Object_Declaration (Loc,
-             Defining_Identifier => Addr,
-             Object_Definition =>
-               New_Occurrence_Of (RTE (RE_Address), Loc)));
-
-         --  Build_Get_Unique_RP_Call needs the type of Stubbed_Result
-
-         Set_Etype (Stubbed_Result, Stub_Type_Access);
-
-         --  If the ref Is_Nil, return a null pointer
-
-         Statements := New_List (
-           Make_Implicit_If_Statement (RACW_Type,
-             Condition =>
-               Make_Function_Call (Loc,
-                 Name =>
-                   New_Occurrence_Of (RTE (RE_Is_Nil), Loc),
-                 Parameter_Associations => New_List (
-                   New_Occurrence_Of (Reference, Loc))),
-             Then_Statements => New_List (
-               Make_Return_Statement (Loc,
-                 Expression =>
-                   Make_Null (Loc)))));
-
-         Append_To (Statements,
-           Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
-             Parameter_Associations => New_List (
-               New_Occurrence_Of (Reference, Loc),
-               New_Occurrence_Of (Is_Local, Loc),
-               New_Occurrence_Of (Addr, Loc))));
-
-         --  If the object is located on another partition, then a stub object
-         --  will be created with all the information needed to rebuild the
-         --  real object at the other end. This stanza is always used in the
-         --  case of RAS types, for which a stub is required even for local
-         --  subprograms.
-
-         Stub_Statements := New_List (
-           Make_Assignment_Statement (Loc,
-             Name       => Make_Selected_Component (Loc,
-               Prefix        => Stubbed_Result,
-               Selector_Name => Name_Target),
-             Expression =>
-               Make_Function_Call (Loc,
-                 Name =>
-                   New_Occurrence_Of (RTE (RE_Entity_Of), Loc),
-                 Parameter_Associations => New_List (
-                   New_Occurrence_Of (Reference, Loc)))),
+         --  NOTE: The usage occurrences of RACW_Parameter must refer to the
+         --  entity in the declaration spec, not those of the body spec.
 
-           Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Inc_Usage), Loc),
-             Parameter_Associations => New_List (
-               Make_Selected_Component (Loc,
-                 Prefix        => Stubbed_Result,
-                 Selector_Name => Name_Target))),
+         Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
+         Insert_After (Declaration_Node (RACW_Type), Func_Decl);
+         Set_Renaming_TSS (RACW_Type, Fnam, TSS_From_Any);
 
-           Make_Assignment_Statement (Loc,
-             Name       => Make_Selected_Component (Loc,
-               Prefix        => Stubbed_Result,
-               Selector_Name => Name_Asynchronous),
-             Expression =>
-               New_Occurrence_Of (Asynchronous_Flag, Loc)));
+         if No (Body_Decls) then
+            return;
+         end if;
 
          --  ??? Issue with asynchronous calls here: the Asynchronous flag is
          --  set on the stub type if, and only if, the RACW type has a pragma
@@ -5736,77 +5749,29 @@ package body Exp_Dist is
          --  the Asynchronous component in the stub type in the RAS's _From_Any
          --  TSS.
 
-         Append_List_To (Stub_Statements,
-           Build_Get_Unique_RP_Call (Loc, Stubbed_Result, Stub_Type));
-
-         --  Distinguish between the local and remote cases, and execute the
-         --  appropriate piece of code.
-
-         Stub_Condition := New_Occurrence_Of (Is_Local, Loc);
-
-         if Is_RAS then
-            Stub_Condition := Make_And_Then (Loc,
-              Left_Opnd  =>
-                Stub_Condition,
-              Right_Opnd =>
-                Make_Selected_Component (Loc,
-                  Prefix =>
-                    Unchecked_Convert_To (
-                      RTE (RE_RAS_Proxy_Type_Access),
-                      New_Occurrence_Of (Addr, Loc)),
-                  Selector_Name =>
-                    Make_Identifier (Loc,
-                      Name_All_Calls_Remote)));
-         end if;
-
-         Local_Statements := New_List (
-           Make_Return_Statement (Loc,
-             Expression =>
-               Unchecked_Convert_To (RACW_Type,
-                 New_Occurrence_Of (Addr, Loc))));
-
-         Append_To (Statements,
-           Make_Implicit_If_Statement (RACW_Type,
-             Condition =>
-               Stub_Condition,
-             Then_Statements => Local_Statements,
-             Else_Statements => Stub_Statements));
-
-         Append_To (Statements,
-           Make_Return_Statement (Loc,
+         Statements := New_List (
+           Make_Simple_Return_Statement (Loc,
              Expression => Unchecked_Convert_To (RACW_Type,
-               New_Occurrence_Of (Stubbed_Result, Loc))));
-
-         Func_Spec :=
-           Make_Function_Specification (Loc,
-             Defining_Unit_Name =>
-               Fnam,
-             Parameter_Specifications => New_List (
-               Make_Parameter_Specification (Loc,
-                 Defining_Identifier =>
-                   Any_Parameter,
-                 Parameter_Type =>
-                   New_Occurrence_Of (RTE (RE_Any), Loc))),
-             Result_Definition => New_Occurrence_Of (RACW_Type, Loc));
-
-         --  NOTE: The usage occurrences of RACW_Parameter must refer to the
-         --  entity in the declaration spec, not those of the body spec.
-
-         Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
+               Make_Function_Call (Loc,
+                 Name => New_Occurrence_Of (RTE (RE_Get_RACW), Loc),
+                 Parameter_Associations => New_List (
+                   Make_Function_Call (Loc,
+                     Name => New_Occurrence_Of (RTE (RE_FA_ObjRef), Loc),
+                     Parameter_Associations => New_List (
+                       New_Occurrence_Of (Any_Parameter, Loc))),
+                   Build_Stub_Tag (Loc, RACW_Type),
+                   New_Occurrence_Of (Boolean_Literals (Is_RAS), Loc),
+                   New_Occurrence_Of (Asynchronous_Flag, Loc))))));
 
          Func_Body :=
            Make_Subprogram_Body (Loc,
-             Specification              =>
-               Copy_Specification (Loc, Func_Spec),
-             Declarations               => Decls,
+             Specification => Copy_Specification (Loc, Func_Spec),
+             Declarations  => No_List,
              Handled_Statement_Sequence =>
                Make_Handled_Sequence_Of_Statements (Loc,
                  Statements => Statements));
 
-         Insert_After (Declaration_Node (RACW_Type), Func_Decl);
          Append_To (Body_Decls, Func_Body);
-
-         Set_Renaming_TSS (RACW_Type, Fnam, TSS_From_Any);
       end Add_RACW_From_Any;
 
       -----------------------------
@@ -5819,9 +5784,8 @@ package body Exp_Dist is
          Stub_Type_Access : Entity_Id;
          Body_Decls       : List_Id)
       is
-         pragma Warnings (Off);
          pragma Unreferenced (Stub_Type, Stub_Type_Access);
-         pragma Warnings (On);
+
          Loc : constant Source_Ptr := Sloc (RACW_Type);
 
          Proc_Decl : Node_Id;
@@ -5829,21 +5793,23 @@ package body Exp_Dist is
 
          Body_Node : Node_Id;
 
-         Decls             : List_Id;
-         Statements        : List_Id;
+         Decls      : constant List_Id   := New_List;
+         Statements : constant List_Id   := New_List;
+         Reference  : constant Entity_Id :=
+                        Make_Defining_Identifier (Loc, Name_R);
          --  Various parts of the procedure
 
-         Procedure_Name    : constant Name_Id   :=
-                               New_Internal_Name ('R');
-         Source_Ref        : constant Entity_Id :=
-                               Make_Defining_Identifier
-                                 (Loc, New_Internal_Name ('R'));
+         Pnam : constant Entity_Id := Make_Temporary (Loc, 'R');
+
+         Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
+
          Asynchronous_Flag : constant Entity_Id :=
                                Asynchronous_Flags_Table.Get (RACW_Type);
          pragma Assert (Present (Asynchronous_Flag));
 
          function Stream_Parameter return Node_Id;
          function Result return Node_Id;
+
          --  Functions to create occurrences of the formal parameter names
 
          ------------
@@ -5867,54 +5833,58 @@ package body Exp_Dist is
       --  Start of processing for Add_RACW_Read_Attribute
 
       begin
-         --  Generate object declarations
+         Build_Stream_Procedure
+           (Loc, RACW_Type, Body_Node, Pnam, Statements, Outp => True);
 
-         Decls := New_List (
+         Proc_Decl := Make_Subprogram_Declaration (Loc,
+           Copy_Specification (Loc, Specification (Body_Node)));
+
+         Attr_Decl :=
+           Make_Attribute_Definition_Clause (Loc,
+             Name       => New_Occurrence_Of (RACW_Type, Loc),
+             Chars      => Name_Read,
+             Expression =>
+               New_Occurrence_Of (
+                 Defining_Unit_Name (Specification (Proc_Decl)), Loc));
+
+         Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
+         Insert_After (Proc_Decl, Attr_Decl);
+
+         if No (Body_Decls) then
+            return;
+         end if;
+
+         Append_To (Decls,
            Make_Object_Declaration (Loc,
-             Defining_Identifier => Source_Ref,
-             Object_Definition   =>
+             Defining_Identifier =>
+               Reference,
+             Object_Definition =>
                New_Occurrence_Of (RTE (RE_Object_Ref), Loc)));
 
-         Statements := New_List (
+         Append_List_To (Statements, New_List (
            Make_Attribute_Reference (Loc,
              Prefix         =>
                New_Occurrence_Of (RTE (RE_Object_Ref), Loc),
              Attribute_Name => Name_Read,
              Expressions    => New_List (
                Stream_Parameter,
-               New_Occurrence_Of (Source_Ref, Loc))),
+               New_Occurrence_Of (Reference, Loc))),
+
            Make_Assignment_Statement (Loc,
-             Name =>
+             Name       =>
                Result,
              Expression =>
-               PolyORB_Support.Helpers.Build_From_Any_Call (
-                 RACW_Type,
+               Unchecked_Convert_To (RACW_Type,
                  Make_Function_Call (Loc,
-                   Name =>
-                     New_Occurrence_Of (RTE (RE_TA_ObjRef), Loc),
+                   Name                   =>
+                     New_Occurrence_Of (RTE (RE_Get_RACW), Loc),
                    Parameter_Associations => New_List (
-                     New_Occurrence_Of (Source_Ref, Loc))),
-                 Decls)));
+                     New_Occurrence_Of (Reference, Loc),
+                     Build_Stub_Tag (Loc, RACW_Type),
+                     New_Occurrence_Of (Boolean_Literals (Is_RAS), Loc),
+                     New_Occurrence_Of (Asynchronous_Flag, Loc)))))));
 
-         Build_Stream_Procedure
-           (Loc, RACW_Type, Body_Node,
-            Make_Defining_Identifier (Loc, Procedure_Name),
-            Statements, Outp => True);
          Set_Declarations (Body_Node, Decls);
-
-         Proc_Decl := Make_Subprogram_Declaration (Loc,
-           Copy_Specification (Loc, Specification (Body_Node)));
-
-         Attr_Decl :=
-           Make_Attribute_Definition_Clause (Loc,
-             Name       => New_Occurrence_Of (RACW_Type, Loc),
-             Chars      => Name_Read,
-             Expression =>
-               New_Occurrence_Of (
-                 Defining_Unit_Name (Specification (Proc_Decl)), Loc));
-
-         Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
-         Insert_After (Proc_Decl, Attr_Decl);
          Append_To (Body_Decls, Body_Node);
       end Add_RACW_Read_Attribute;
 
@@ -5923,160 +5893,115 @@ package body Exp_Dist is
       ---------------------
 
       procedure Add_RACW_To_Any
-        (Designated_Type  : Entity_Id;
-         RACW_Type        : Entity_Id;
-         Stub_Type        : Entity_Id;
-         Stub_Type_Access : Entity_Id;
+        (RACW_Type        : Entity_Id;
          Body_Decls       : List_Id)
       is
          Loc : constant Source_Ptr := Sloc (RACW_Type);
 
-         Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
+         Fnam : constant Entity_Id :=
+                  Make_Defining_Identifier (Loc,
+                    Chars => New_External_Name (Chars (RACW_Type), 'T'));
 
-         Fnam : Entity_Id;
+         Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
 
          Stub_Elements : constant Stub_Structure :=
-           Stubs_Table.Get (Designated_Type);
-         pragma Assert (Stub_Elements /= Empty_Stub_Structure);
+                           Get_Stub_Elements (RACW_Type);
 
          Func_Spec : Node_Id;
          Func_Decl : Node_Id;
          Func_Body : Node_Id;
 
-         Decls             : List_Id;
-         Statements        : List_Id;
-         Null_Statements   : List_Id;
-         Local_Statements  : List_Id := No_List;
-         Stub_Statements   : List_Id;
-         If_Node           : Node_Id;
+         Decls      : List_Id;
+         Statements : List_Id;
          --  Various parts of the subprogram
 
-         RACW_Parameter : constant Entity_Id
-           := Make_Defining_Identifier (Loc, Name_R);
+         RACW_Parameter : constant Entity_Id :=
+                            Make_Defining_Identifier (Loc, Name_R);
 
-         Reference         : constant Entity_Id :=
-                               Make_Defining_Identifier
-                                 (Loc, New_Internal_Name ('R'));
-         Any               : constant Entity_Id :=
-                               Make_Defining_Identifier
-                                 (Loc, New_Internal_Name ('A'));
+         Reference : constant Entity_Id := Make_Temporary (Loc, 'R');
+         Any       : constant Entity_Id := Make_Temporary (Loc, 'A');
 
       begin
+         Func_Spec :=
+           Make_Function_Specification (Loc,
+             Defining_Unit_Name =>
+               Fnam,
+             Parameter_Specifications => New_List (
+               Make_Parameter_Specification (Loc,
+                 Defining_Identifier =>
+                   RACW_Parameter,
+                 Parameter_Type =>
+                   New_Occurrence_Of (RACW_Type, Loc))),
+             Result_Definition => New_Occurrence_Of (RTE (RE_Any), Loc));
 
-         --  Object declarations
-
-         Decls := New_List (
-           Make_Object_Declaration (Loc,
-             Defining_Identifier =>
-               Reference,
-             Object_Definition =>
-               New_Occurrence_Of (RTE (RE_Object_Ref), Loc)),
-           Make_Object_Declaration (Loc,
-             Defining_Identifier =>
-               Any,
-             Object_Definition =>
-               New_Occurrence_Of (RTE (RE_Any), Loc)));
-
-         --  If the object is null, nothing to do (Reference is already
-         --  a Nil ref.)
-
-         Null_Statements := New_List (Make_Null_Statement (Loc));
+         --  NOTE: The usage occurrences of RACW_Parameter must refer to the
+         --  entity in the declaration spec, not in the body spec.
 
-         if Is_RAS then
+         Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
 
-            --  If the object is a RAS designating a local subprogram, we
-            --  already have a target reference.
+         Insert_After (Declaration_Node (RACW_Type), Func_Decl);
+         Set_Renaming_TSS (RACW_Type, Fnam, TSS_To_Any);
 
-            Local_Statements := New_List (
-              Make_Procedure_Call_Statement (Loc,
-                Name =>
-                  New_Occurrence_Of (RTE (RE_Set_Ref), Loc),
-                Parameter_Associations => New_List (
-                  New_Occurrence_Of (Reference, Loc),
-                  Make_Selected_Component (Loc,
-                    Prefix =>
-                      Unchecked_Convert_To (RTE (RE_RAS_Proxy_Type_Access),
-                        New_Occurrence_Of (RACW_Parameter, Loc)),
-                    Selector_Name => Make_Identifier (Loc, Name_Target)))));
+         if No (Body_Decls) then
+            return;
+         end if;
 
-         else
-            --  If the object is a local RACW object, use Get_Reference now to
-            --  obtain a reference.
+         --  Generate:
 
-            Local_Statements := New_List (
-              Make_Procedure_Call_Statement (Loc,
-                Name =>
-                  New_Occurrence_Of (RTE (RE_Get_Reference), Loc),
-                Parameter_Associations => New_List (
-                  Unchecked_Convert_To (
-                    RTE (RE_Address),
-                    New_Occurrence_Of (RACW_Parameter, Loc)),
-                  Make_String_Literal (Loc,
-                    Full_Qualified_Name (Designated_Type)),
-                  Make_Attribute_Reference (Loc,
-                    Prefix =>
-                      New_Occurrence_Of (
-                        Defining_Identifier (
-                          Stub_Elements.RPC_Receiver_Decl), Loc),
-                    Attribute_Name =>
-                      Name_Access),
-                  New_Occurrence_Of (Reference, Loc))));
-         end if;
+         --    R : constant Object_Ref :=
+         --          Get_Reference
+         --            (Address!(RACW),
+         --             "typ",
+         --             Stub_Type'Tag,
+         --             Is_RAS,
+         --             RPC_Receiver'Access);
+         --    A : Any;
 
-         --  If the object is located on another partition, use the target from
-         --  the stub.
+         Decls := New_List (
+           Make_Object_Declaration (Loc,
+             Defining_Identifier => Reference,
+             Constant_Present    => True,
+             Object_Definition   =>
+               New_Occurrence_Of (RTE (RE_Object_Ref), Loc),
+             Expression          =>
+               Make_Function_Call (Loc,
+                 Name => New_Occurrence_Of (RTE (RE_Get_Reference), Loc),
+                 Parameter_Associations => New_List (
+                   Unchecked_Convert_To (RTE (RE_Address),
+                     New_Occurrence_Of (RACW_Parameter, Loc)),
+                   Make_String_Literal (Loc,
+                     Strval => Fully_Qualified_Name_String
+                                 (Etype (Designated_Type (RACW_Type)))),
+                   Build_Stub_Tag (Loc, RACW_Type),
+                   New_Occurrence_Of (Boolean_Literals (Is_RAS), Loc),
+                   Make_Attribute_Reference (Loc,
+                     Prefix         =>
+                       New_Occurrence_Of
+                         (Defining_Identifier
+                           (Stub_Elements.RPC_Receiver_Decl), Loc),
+                     Attribute_Name => Name_Access)))),
 
-         Stub_Statements := New_List (
-           Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Set_Ref), Loc),
-             Parameter_Associations => New_List (
-               New_Occurrence_Of (Reference, Loc),
-               Make_Selected_Component (Loc,
-                 Prefix        => Unchecked_Convert_To (Stub_Type_Access,
-                   New_Occurrence_Of (RACW_Parameter, Loc)),
-                 Selector_Name =>
-                   Make_Identifier (Loc, Name_Target)))));
+           Make_Object_Declaration (Loc,
+             Defining_Identifier => Any,
+             Object_Definition   => New_Occurrence_Of (RTE (RE_Any), Loc)));
 
-         --  Distinguish between the null, local and remote cases, and execute
-         --  the appropriate piece of code.
+         --  Generate:
 
-         If_Node :=
-           Make_Implicit_If_Statement (RACW_Type,
-             Condition       =>
-               Make_Op_Eq (Loc,
-                 Left_Opnd  => New_Occurrence_Of (RACW_Parameter, Loc),
-                 Right_Opnd => Make_Null (Loc)),
-             Then_Statements => Null_Statements,
-             Elsif_Parts     => New_List (
-               Make_Elsif_Part (Loc,
-                 Condition       =>
-                   Make_Op_Ne (Loc,
-                     Left_Opnd  =>
-                        Make_Attribute_Reference (Loc,
-                         Prefix         =>
-                           New_Occurrence_Of (RACW_Parameter, Loc),
-                         Attribute_Name => Name_Tag),
-                     Right_Opnd =>
-                       Make_Attribute_Reference (Loc,
-                         Prefix         => New_Occurrence_Of (Stub_Type, Loc),
-                         Attribute_Name => Name_Tag)),
-                 Then_Statements => Local_Statements)),
-             Else_Statements => Stub_Statements);
+         --    Any := TA_ObjRef (Reference);
+         --    Set_TC (Any, RPC_Receiver.Obj_TypeCode);
+         --    return Any;
 
          Statements := New_List (
-           If_Node,
            Make_Assignment_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (Any, Loc),
+             Name => New_Occurrence_Of (Any, Loc),
              Expression =>
                Make_Function_Call (Loc,
                  Name => New_Occurrence_Of (RTE (RE_TA_ObjRef), Loc),
                  Parameter_Associations => New_List (
                    New_Occurrence_Of (Reference, Loc)))),
+
            Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Set_TC), Loc),
+             Name => New_Occurrence_Of (RTE (RE_Set_TC), Loc),
              Parameter_Associations => New_List (
                New_Occurrence_Of (Any, Loc),
                Make_Selected_Component (Loc,
@@ -6084,43 +6009,18 @@ package body Exp_Dist is
                      Defining_Identifier (
                        Stub_Elements.RPC_Receiver_Decl),
                  Selector_Name => Name_Obj_TypeCode))),
-           Make_Return_Statement (Loc,
-             Expression =>
-               New_Occurrence_Of (Any, Loc)));
 
-         Fnam := Make_Defining_Identifier (
-           Loc, New_Internal_Name ('T'));
-
-         Func_Spec :=
-           Make_Function_Specification (Loc,
-             Defining_Unit_Name =>
-               Fnam,
-             Parameter_Specifications => New_List (
-               Make_Parameter_Specification (Loc,
-                 Defining_Identifier =>
-                   RACW_Parameter,
-                 Parameter_Type =>
-                   New_Occurrence_Of (RACW_Type, Loc))),
-             Result_Definition => New_Occurrence_Of (RTE (RE_Any), Loc));
-
-         --  NOTE: The usage occurrences of RACW_Parameter must refer to the
-         --  entity in the declaration spec, not in the body spec.
-
-         Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
+           Make_Simple_Return_Statement (Loc,
+             Expression => New_Occurrence_Of (Any, Loc)));
 
          Func_Body :=
            Make_Subprogram_Body (Loc,
-             Specification              =>
-               Copy_Specification (Loc, Func_Spec),
+             Specification              => Copy_Specification (Loc, Func_Spec),
              Declarations               => Decls,
              Handled_Statement_Sequence =>
                Make_Handled_Sequence_Of_Statements (Loc,
                  Statements => Statements));
-
-         Insert_After (Declaration_Node (RACW_Type), Func_Decl);
          Append_To (Body_Decls, Func_Body);
-
-         Set_Renaming_TSS (RACW_Type, Fnam, TSS_To_Any);
       end Add_RACW_To_Any;
 
       -----------------------
@@ -6134,7 +6034,9 @@ package body Exp_Dist is
       is
          Loc : constant Source_Ptr := Sloc (RACW_Type);
 
-         Fnam : Entity_Id;
+         Fnam : constant Entity_Id :=
+                  Make_Defining_Identifier (Loc,
+                    Chars => New_External_Name (Chars (RACW_Type), 'Y'));
 
          Stub_Elements : constant Stub_Structure :=
                            Stubs_Table.Get (Designated_Type);
@@ -6145,44 +6047,41 @@ package body Exp_Dist is
          Func_Body : Node_Id;
 
       begin
-         Fnam :=
-           Make_Defining_Identifier (Loc,
-             Chars => New_Internal_Name ('T'));
-
          --  The spec for this subprogram has a dummy 'access RACW' argument,
          --  which serves only for overloading purposes.
 
          Func_Spec :=
            Make_Function_Specification (Loc,
-             Defining_Unit_Name =>
-               Fnam,
-             Result_Definition => New_Occurrence_Of (RTE (RE_TypeCode), Loc));
+             Defining_Unit_Name => Fnam,
+             Result_Definition  => New_Occurrence_Of (RTE (RE_TypeCode), Loc));
 
          --  NOTE: The usage occurrences of RACW_Parameter must refer to the
          --  entity in the declaration spec, not those of the body spec.
 
          Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
+         Insert_After (Declaration_Node (RACW_Type), Func_Decl);
+         Set_Renaming_TSS (RACW_Type, Fnam, TSS_TypeCode);
+
+         if No (Body_Decls) then
+            return;
+         end if;
 
          Func_Body :=
            Make_Subprogram_Body (Loc,
-             Specification              =>
-               Copy_Specification (Loc, Func_Spec),
+             Specification              => Copy_Specification (Loc, Func_Spec),
              Declarations               => Empty_List,
              Handled_Statement_Sequence =>
                Make_Handled_Sequence_Of_Statements (Loc,
                  Statements => New_List (
-                   Make_Return_Statement (Loc,
+                   Make_Simple_Return_Statement (Loc,
                      Expression =>
                        Make_Selected_Component (Loc,
                          Prefix =>
-                             Defining_Identifier (
-                               Stub_Elements.RPC_Receiver_Decl),
+                           Defining_Identifier
+                             (Stub_Elements.RPC_Receiver_Decl),
                          Selector_Name => Name_Obj_TypeCode)))));
 
-         Insert_After (Declaration_Node (RACW_Type), Func_Decl);
          Append_To (Body_Decls, Func_Body);
-
-         Set_Renaming_TSS (RACW_Type, Fnam, TSS_TypeCode);
       end Add_RACW_TypeCode;
 
       ------------------------------
@@ -6195,18 +6094,21 @@ package body Exp_Dist is
          Stub_Type_Access : Entity_Id;
          Body_Decls       : List_Id)
       is
-         pragma Warnings (Off);
          pragma Unreferenced (Stub_Type, Stub_Type_Access);
-         pragma Warnings (On);
 
          Loc : constant Source_Ptr := Sloc (RACW_Type);
 
+         Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
+
+         Stub_Elements : constant Stub_Structure :=
+                            Get_Stub_Elements (RACW_Type);
+
          Body_Node : Node_Id;
          Proc_Decl : Node_Id;
          Attr_Decl : Node_Id;
 
-         Statements     : List_Id;
-         Procedure_Name : constant Name_Id := New_Internal_Name ('R');
+         Statements : constant List_Id := New_List;
+         Pnam : constant Entity_Id := Make_Temporary (Loc, 'R');
 
          function Stream_Parameter return Node_Id;
          function Object return Node_Id;
@@ -6217,15 +6119,8 @@ package body Exp_Dist is
          ------------
 
          function Object return Node_Id is
-            Object_Ref : constant Node_Id :=
-                           Make_Identifier (Loc, Name_V);
-
          begin
-            --  Etype must be set for Build_To_Any_Call
-
-            Set_Etype (Object_Ref, RACW_Type);
-
-            return Object_Ref;
+            return Make_Identifier (Loc, Name_V);
          end Object;
 
          ----------------------
@@ -6240,22 +6135,8 @@ package body Exp_Dist is
       --  Start of processing for Add_RACW_Write_Attribute
 
       begin
-         Statements := New_List (
-           Pack_Node_Into_Stream_Access (Loc,
-             Stream => Stream_Parameter,
-             Object =>
-               Make_Function_Call (Loc,
-                 Name =>
-                   New_Occurrence_Of (RTE (RE_FA_ObjRef), Loc),
-                 Parameter_Associations => New_List (
-                   PolyORB_Support.Helpers.Build_To_Any_Call
-                                             (Object, Body_Decls))),
-             Etyp => RTE (RE_Object_Ref)));
-
          Build_Stream_Procedure
-           (Loc, RACW_Type, Body_Node,
-            Make_Defining_Identifier (Loc, Procedure_Name),
-            Statements, Outp => False);
+           (Loc, RACW_Type, Body_Node, Pnam, Statements, Outp => False);
 
          Proc_Decl :=
            Make_Subprogram_Declaration (Loc,
@@ -6271,6 +6152,33 @@ package body Exp_Dist is
 
          Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
          Insert_After (Proc_Decl, Attr_Decl);
+
+         if No (Body_Decls) then
+            return;
+         end if;
+
+         Append_To (Statements,
+           Pack_Node_Into_Stream_Access (Loc,
+             Stream => Stream_Parameter,
+             Object =>
+               Make_Function_Call (Loc,
+                 Name => New_Occurrence_Of (RTE (RE_Get_Reference), Loc),
+                 Parameter_Associations => New_List (
+                   Unchecked_Convert_To (RTE (RE_Address), Object),
+                  Make_String_Literal (Loc,
+                    Strval => Fully_Qualified_Name_String
+                                (Etype (Designated_Type (RACW_Type)))),
+                  Build_Stub_Tag (Loc, RACW_Type),
+                  New_Occurrence_Of (Boolean_Literals (Is_RAS), Loc),
+                  Make_Attribute_Reference (Loc,
+                    Prefix         =>
+                       New_Occurrence_Of
+                         (Defining_Identifier
+                           (Stub_Elements.RPC_Receiver_Decl), Loc),
+                    Attribute_Name => Name_Access))),
+
+             Etyp => RTE (RE_Object_Ref)));
+
          Append_To (Body_Decls, Body_Node);
       end Add_RACW_Write_Attribute;
 
@@ -6306,13 +6214,10 @@ package body Exp_Dist is
          --  corresponding record type.
 
          RACW_Type : constant Entity_Id :=
-           Underlying_RACW_Type (Ras_Type);
-         Desig     : constant Entity_Id :=
-           Etype (Designated_Type (RACW_Type));
+                       Underlying_RACW_Type (Ras_Type);
 
          Stub_Elements : constant Stub_Structure :=
-           Stubs_Table.Get (Desig);
-         pragma Assert (Stub_Elements /= Empty_Stub_Structure);
+                           Get_Stub_Elements (RACW_Type);
 
          Proc : constant Entity_Id :=
                   Make_Defining_Identifier (Loc,
@@ -6361,16 +6266,10 @@ package body Exp_Dist is
            Make_Defining_Identifier (Loc, Name_A);
          --  For the call to Get_Local_Address
 
+         Local_Stub : constant Entity_Id := Make_Temporary (Loc, 'L');
+         Stub_Ptr   : constant Entity_Id := Make_Temporary (Loc, 'S');
          --  Additional local variables for the remote case
 
-         Local_Stub : constant Entity_Id :=
-                        Make_Defining_Identifier (Loc,
-                          Chars => New_Internal_Name ('L'));
-
-         Stub_Ptr : constant Entity_Id :=
-                      Make_Defining_Identifier (Loc,
-                        Chars => New_Internal_Name ('S'));
-
          function Set_Field
            (Field_Name : Name_Id;
             Value      : Node_Id) return Node_Id;
@@ -6424,8 +6323,7 @@ package body Exp_Dist is
                New_Occurrence_Of (Stub_Elements.Stub_Type, Loc)),
 
            Make_Object_Declaration (Loc,
-             Defining_Identifier =>
-               Stub_Ptr,
+             Defining_Identifier => Stub_Ptr,
              Object_Definition   =>
                New_Occurrence_Of (Stub_Elements.Stub_Type_Access, Loc),
              Expression          =>
@@ -6441,8 +6339,7 @@ package body Exp_Dist is
 
          Proc_Statements := New_List (
            Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Get_RAS_Info), Loc),
+             Name => New_Occurrence_Of (RTE (RE_Get_RAS_Info), Loc),
              Parameter_Associations => New_List (
                New_Occurrence_Of (Package_Name, Loc),
                New_Occurrence_Of (Subp_Id, Loc),
@@ -6453,8 +6350,7 @@ package body Exp_Dist is
          --  obtain the local address of its proxy (A).
 
            Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
+             Name => New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
              Parameter_Associations => New_List (
                New_Occurrence_Of (Subp_Ref, Loc),
                New_Occurrence_Of (Is_Local, Loc),
@@ -6465,117 +6361,112 @@ package body Exp_Dist is
 
          Append_To (Proc_Statements,
 
-         --  if L then
+           --  if L then
 
            Make_Implicit_If_Statement (N,
-             Condition =>
-               New_Occurrence_Of (Is_Local, Loc),
+             Condition => New_Occurrence_Of (Is_Local, Loc),
 
              Then_Statements => New_List (
 
-         --     if A.Target = null then
+               --  if A.Target = null then
 
                Make_Implicit_If_Statement (N,
                  Condition =>
                    Make_Op_Eq (Loc,
                      Make_Selected_Component (Loc,
-                       Prefix =>
-                         Unchecked_Convert_To (
-                           RTE (RE_RAS_Proxy_Type_Access),
-                           New_Occurrence_Of (Local_Addr, Loc)),
-                         Selector_Name =>
-                           Make_Identifier (Loc, Name_Target)),
+                       Prefix        =>
+                         Unchecked_Convert_To
+                           (RTE (RE_RAS_Proxy_Type_Access),
+                            New_Occurrence_Of (Local_Addr, Loc)),
+                       Selector_Name => Make_Identifier (Loc, Name_Target)),
                      Make_Null (Loc)),
 
                  Then_Statements => New_List (
 
-         --        A.Target := Entity_Of (Ref);
+                   --    A.Target := Entity_Of (Ref);
 
                    Make_Assignment_Statement (Loc,
                      Name =>
                        Make_Selected_Component (Loc,
-                         Prefix =>
-                           Unchecked_Convert_To (
-                             RTE (RE_RAS_Proxy_Type_Access),
-                             New_Occurrence_Of (Local_Addr, Loc)),
-                           Selector_Name =>
-                             Make_Identifier (Loc, Name_Target)),
+                         Prefix        =>
+                           Unchecked_Convert_To
+                             (RTE (RE_RAS_Proxy_Type_Access),
+                              New_Occurrence_Of (Local_Addr, Loc)),
+                         Selector_Name => Make_Identifier (Loc, Name_Target)),
                      Expression =>
                        Make_Function_Call (Loc,
-                         Name =>
-                           New_Occurrence_Of (RTE (RE_Entity_Of), Loc),
+                         Name => New_Occurrence_Of (RTE (RE_Entity_Of), Loc),
                          Parameter_Associations => New_List (
                            New_Occurrence_Of (Subp_Ref, Loc)))),
 
-         --        Inc_Usage (A.Target);
+                   --    Inc_Usage (A.Target);
+                   --  end if;
 
                    Make_Procedure_Call_Statement (Loc,
-                     Name =>
-                       New_Occurrence_Of (RTE (RE_Inc_Usage), Loc),
+                     Name => New_Occurrence_Of (RTE (RE_Inc_Usage), Loc),
                      Parameter_Associations => New_List (
                        Make_Selected_Component (Loc,
                          Prefix        =>
-                           Unchecked_Convert_To (
-                             RTE (RE_RAS_Proxy_Type_Access),
-                             New_Occurrence_Of (Local_Addr, Loc)),
-                         Selector_Name => Make_Identifier (Loc,
-                           Name_Target)))))),
+                           Unchecked_Convert_To
+                             (RTE (RE_RAS_Proxy_Type_Access),
+                              New_Occurrence_Of (Local_Addr, Loc)),
+                         Selector_Name =>
+                           Make_Identifier (Loc, Name_Target)))))),
 
-         --     end if;
-         --     if not All_Calls_Remote then
-         --        return Fat_Type!(A);
-         --     end if;
+                 --     if not All_Calls_Remote then
+                 --        return Fat_Type!(A);
+                 --     end if;
 
                  Make_Implicit_If_Statement (N,
                    Condition =>
                      Make_Op_Not (Loc,
-                       New_Occurrence_Of (All_Calls_Remote, Loc)),
+                       Right_Opnd =>
+                         New_Occurrence_Of (All_Calls_Remote, Loc)),
 
                    Then_Statements => New_List (
-                     Make_Return_Statement (Loc,
-                       Unchecked_Convert_To (Fat_Type,
-                         New_Occurrence_Of (Local_Addr, Loc))))))));
+                     Make_Simple_Return_Statement (Loc,
+                     Expression =>
+                       Unchecked_Convert_To
+                         (Fat_Type, New_Occurrence_Of (Local_Addr, Loc))))))));
 
          Append_List_To (Proc_Statements, New_List (
 
-         --  Stub.Target := Entity_Of (Ref);
+           --  Stub.Target := Entity_Of (Ref);
 
            Set_Field (Name_Target,
              Make_Function_Call (Loc,
-               Name =>
-                 New_Occurrence_Of (RTE (RE_Entity_Of), Loc),
+               Name => New_Occurrence_Of (RTE (RE_Entity_Of), Loc),
                Parameter_Associations => New_List (
                  New_Occurrence_Of (Subp_Ref, Loc)))),
 
-         --  Inc_Usage (Stub.Target);
+           --  Inc_Usage (Stub.Target);
 
            Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Inc_Usage), Loc),
+             Name => New_Occurrence_Of (RTE (RE_Inc_Usage), Loc),
              Parameter_Associations => New_List (
                Make_Selected_Component (Loc,
                  Prefix        => Stub_Ptr,
                  Selector_Name => Name_Target))),
 
-         --  E.4.1(9) A remote call is asynchronous if it is a call to
-         --  a procedure, or a call through a value of an access-to-procedure
-         --  type, to which a pragma Asynchronous applies.
+           --  E.4.1(9) A remote call is asynchronous if it is a call to
+           --  a procedure, or a call through a value of an access-to-procedure
+           --  type, to which a pragma Asynchronous applies.
 
-         --    Parameter Asynch_P is true when the procedure is asynchronous;
-         --    Expression Asynch_T is true when the type is asynchronous.
+           --    Parameter Asynch_P is true when the procedure is asynchronous;
+           --    Expression Asynch_T is true when the type is asynchronous.
 
            Set_Field (Name_Asynchronous,
              Make_Or_Else (Loc,
-               New_Occurrence_Of (Asynch_P, Loc),
-               New_Occurrence_Of (Boolean_Literals (
-                 Is_Asynchronous (Ras_Type)), Loc)))));
+               Left_Opnd  => New_Occurrence_Of (Asynch_P, Loc),
+               Right_Opnd =>
+                 New_Occurrence_Of
+                   (Boolean_Literals (Is_Asynchronous (Ras_Type)), Loc)))));
 
          Append_List_To (Proc_Statements,
-           Build_Get_Unique_RP_Call (Loc,
-             Stub_Ptr, Stub_Elements.Stub_Type));
+           Build_Get_Unique_RP_Call (Loc, Stub_Ptr, Stub_Elements.Stub_Type));
 
          Append_To (Proc_Statements,
-           Make_Return_Statement (Loc,
+           Make_Simple_Return_Statement (Loc,
              Expression =>
                Unchecked_Convert_To (Fat_Type,
                  New_Occurrence_Of (Stub_Ptr, Loc))));
@@ -6643,13 +6534,12 @@ package body Exp_Dist is
 
       begin
          Statements := New_List (
-           Make_Return_Statement (Loc,
+           Make_Simple_Return_Statement (Loc,
              Expression =>
                Make_Aggregate (Loc,
                  Component_Associations => New_List (
                    Make_Component_Association (Loc,
-                     Choices => New_List (
-                       Make_Identifier (Loc, Name_Ras)),
+                     Choices => New_List (Make_Identifier (Loc, Name_Ras)),
                      Expression =>
                        PolyORB_Support.Helpers.Build_From_Any_Call (
                          Underlying_RACW_Type (RAS_Type),
@@ -6658,14 +6548,11 @@ package body Exp_Dist is
 
          Func_Spec :=
            Make_Function_Specification (Loc,
-             Defining_Unit_Name =>
-               Fnam,
+             Defining_Unit_Name       => Fnam,
              Parameter_Specifications => New_List (
                Make_Parameter_Specification (Loc,
-                 Defining_Identifier =>
-                   Any_Parameter,
-                 Parameter_Type =>
-                   New_Occurrence_Of (RTE (RE_Any), Loc))),
+                 Defining_Identifier => Any_Parameter,
+                 Parameter_Type => New_Occurrence_Of (RTE (RE_Any), Loc))),
              Result_Definition => New_Occurrence_Of (RAS_Type, Loc));
 
          Discard_Node (
@@ -6693,12 +6580,8 @@ package body Exp_Dist is
 
          Func_Spec : Node_Id;
 
-         Any            : constant Entity_Id :=
-                            Make_Defining_Identifier (Loc,
-                              Chars => New_Internal_Name ('A'));
-         RAS_Parameter  : constant Entity_Id :=
-                            Make_Defining_Identifier (Loc,
-                              Chars => New_Internal_Name ('R'));
+         Any            : constant Entity_Id := Make_Temporary (Loc, 'A');
+         RAS_Parameter  : constant Entity_Id := Make_Temporary (Loc, 'R');
          RACW_Parameter : constant Node_Id :=
                             Make_Selected_Component (Loc,
                               Prefix        => RAS_Parameter,
@@ -6710,36 +6593,30 @@ package body Exp_Dist is
          Set_Etype (RACW_Parameter, Underlying_RACW_Type (RAS_Type));
          Decls := New_List (
            Make_Object_Declaration (Loc,
-             Defining_Identifier =>
-               Any,
-             Object_Definition =>
-               New_Occurrence_Of (RTE (RE_Any), Loc),
-             Expression =>
+             Defining_Identifier => Any,
+             Object_Definition   => New_Occurrence_Of (RTE (RE_Any), Loc),
+             Expression          =>
                PolyORB_Support.Helpers.Build_To_Any_Call
                  (RACW_Parameter, No_List)));
 
          Statements := New_List (
            Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Set_TC), Loc),
+             Name => New_Occurrence_Of (RTE (RE_Set_TC), Loc),
              Parameter_Associations => New_List (
                New_Occurrence_Of (Any, Loc),
                PolyORB_Support.Helpers.Build_TypeCode_Call (Loc,
                  RAS_Type, Decls))),
-           Make_Return_Statement (Loc,
-             Expression =>
-               New_Occurrence_Of (Any, Loc)));
+
+           Make_Simple_Return_Statement (Loc,
+             Expression => New_Occurrence_Of (Any, Loc)));
 
          Func_Spec :=
            Make_Function_Specification (Loc,
-             Defining_Unit_Name =>
-               Fnam,
+             Defining_Unit_Name => Fnam,
              Parameter_Specifications => New_List (
                Make_Parameter_Specification (Loc,
-                 Defining_Identifier =>
-                   RAS_Parameter,
-                 Parameter_Type =>
-                   New_Occurrence_Of (RAS_Type, Loc))),
+                 Defining_Identifier => RAS_Parameter,
+                 Parameter_Type      => New_Occurrence_Of (RAS_Type, Loc))),
              Result_Definition => New_Occurrence_Of (RTE (RE_Any), Loc));
 
          Discard_Node (
@@ -6762,17 +6639,16 @@ package body Exp_Dist is
          Fnam : constant Entity_Id := Make_Defining_Identifier (Loc,
                   Make_TSS_Name (RAS_Type, TSS_TypeCode));
 
-         Func_Spec : Node_Id;
-
-         Decls : constant List_Id := New_List;
-         Name_String, Repo_Id_String : String_Id;
+         Func_Spec      : Node_Id;
+         Decls          : constant List_Id := New_List;
+         Name_String    : String_Id;
+         Repo_Id_String : String_Id;
 
       begin
          Func_Spec :=
            Make_Function_Specification (Loc,
-             Defining_Unit_Name =>
-               Fnam,
-             Result_Definition => New_Occurrence_Of (RTE (RE_TypeCode), Loc));
+             Defining_Unit_Name => Fnam,
+             Result_Definition  => New_Occurrence_Of (RTE (RE_TypeCode), Loc));
 
          PolyORB_Support.Helpers.Build_Name_And_Repository_Id
            (RAS_Type, Name_Str => Name_String, Repo_Id_Str => Repo_Id_String);
@@ -6784,27 +6660,28 @@ package body Exp_Dist is
              Handled_Statement_Sequence =>
                Make_Handled_Sequence_Of_Statements (Loc,
                  Statements => New_List (
-                   Make_Return_Statement (Loc,
+                   Make_Simple_Return_Statement (Loc,
                      Expression =>
                        Make_Function_Call (Loc,
-                         Name =>
-                           New_Occurrence_Of (RTE (RE_TC_Build), Loc),
+                         Name => New_Occurrence_Of (RTE (RE_TC_Build), Loc),
                          Parameter_Associations => New_List (
                            New_Occurrence_Of (RTE (RE_TC_Object), Loc),
                            Make_Aggregate (Loc,
                              Expressions =>
                                New_List (
                                  Make_Function_Call (Loc,
-                                   Name => New_Occurrence_Of (
-                                     RTE (RE_TA_String), Loc),
+                                   Name =>
+                                     New_Occurrence_Of
+                                       (RTE (RE_TA_Std_String), Loc),
                                    Parameter_Associations => New_List (
                                      Make_String_Literal (Loc, Name_String))),
                                  Make_Function_Call (Loc,
-                                   Name => New_Occurrence_Of (
-                                     RTE (RE_TA_String), Loc),
+                                   Name =>
+                                     New_Occurrence_Of
+                                       (RTE (RE_TA_Std_String), Loc),
                                    Parameter_Associations => New_List (
                                      Make_String_Literal (Loc,
-                                       Repo_Id_String))))))))))));
+                                       Strval => Repo_Id_String))))))))))));
          Set_TSS (RAS_Type, Fnam);
       end Add_RAS_TypeCode;
 
@@ -6820,47 +6697,40 @@ package body Exp_Dist is
          Loc : constant Source_Ptr := Sloc (Pkg_Spec);
 
          Pkg_RPC_Receiver            : constant Entity_Id :=
-                                         Make_Defining_Identifier (Loc,
-                                           New_Internal_Name ('H'));
+                                         Make_Temporary (Loc, 'H');
          Pkg_RPC_Receiver_Object     : Node_Id;
-
          Pkg_RPC_Receiver_Body       : Node_Id;
          Pkg_RPC_Receiver_Decls      : List_Id;
          Pkg_RPC_Receiver_Statements : List_Id;
-         Pkg_RPC_Receiver_Cases      : constant List_Id := New_List;
+
+         Pkg_RPC_Receiver_Cases : constant List_Id := New_List;
          --  A Pkg_RPC_Receiver is built to decode the request
 
-         Request                     : Node_Id;
+         Request : Node_Id;
          --  Request object received from neutral layer
 
          Subp_Id : Entity_Id;
-         --  Subprogram identifier as received from the neutral
-         --  distribution core.
+         --  Subprogram identifier as received from the neutral distribution
+         --  core.
 
          Subp_Index : Entity_Id;
-         --  Internal index as determined by matching either the
-         --  method name from the request structure, or the local
-         --  subprogram address (in case of a RAS).
+         --  Internal index as determined by matching either the method name
+         --  from the request structure, or the local subprogram address (in
+         --  case of a RAS).
 
-         Is_Local : constant Entity_Id :=
-           Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
-         Local_Address : constant Entity_Id :=
-           Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
-         --  Address of a local subprogram designated by a
-         --  reference corresponding to a RAS.
+         Is_Local : constant Entity_Id := Make_Temporary (Loc, 'L');
+
+         Local_Address : constant Entity_Id := Make_Temporary (Loc, 'A');
+         --  Address of a local subprogram designated by a reference
+         --  corresponding to a RAS.
 
          Dispatch_On_Address : constant List_Id := New_List;
          Dispatch_On_Name    : constant List_Id := New_List;
 
-         Current_Declaration       : Node_Id;
-         Current_Stubs             : Node_Id;
-         Current_Subprogram_Number : Int := First_RCI_Subprogram_Id;
+         Current_Subp_Number : Int := First_RCI_Subprogram_Id;
 
-         Subp_Info_Array : constant Entity_Id :=
-                             Make_Defining_Identifier (Loc,
-                               Chars => New_Internal_Name ('I'));
-
-         Subp_Info_List : constant List_Id := New_List;
+         Subp_Info_Array : constant Entity_Id := Make_Temporary (Loc, 'I');
+         Subp_Info_List  : constant List_Id := New_List;
 
          Register_Pkg_Actuals : constant List_Id := New_List;
 
@@ -6881,6 +6751,9 @@ package body Exp_Dist is
          --  object, used in the context of calls through remote
          --  access-to-subprogram types.
 
+         procedure Visit_Subprogram (Decl : Node_Id);
+         --  Generate receiving stub for one remote subprogram
+
          ---------------------
          -- Append_Stubs_To --
          ---------------------
@@ -6902,20 +6775,19 @@ package body Exp_Dist is
                     Defining_Entity (Stubs), Loc),
                 Parameter_Associations =>
                   New_List (New_Occurrence_Of (Request, Loc))));
-            if Nkind (Specification (Declaration))
-              = N_Function_Specification
+
+            if Nkind (Specification (Declaration)) = N_Function_Specification
               or else not
                 Is_Asynchronous (Defining_Entity (Specification (Declaration)))
             then
-               Append_To (Case_Stmts, Make_Return_Statement (Loc));
+               Append_To (Case_Stmts, Make_Simple_Return_Statement (Loc));
             end if;
 
             Append_To (RPC_Receiver_Cases,
               Make_Case_Statement_Alternative (Loc,
                 Discrete_Choices =>
                    New_List (Make_Integer_Literal (Loc, Subp_Number)),
-                Statements       =>
-                  Case_Stmts));
+                Statements       => Case_Stmts));
 
             Append_To (Dispatch_On_Name,
               Make_Elsif_Part (Loc,
@@ -6926,46 +6798,142 @@ package body Exp_Dist is
                     Parameter_Associations => New_List (
                       New_Occurrence_Of (Subp_Id, Loc),
                       New_Occurrence_Of (Subp_Dist_Name, Loc))),
+
                 Then_Statements => New_List (
                   Make_Assignment_Statement (Loc,
                     New_Occurrence_Of (Subp_Index, Loc),
-                    Make_Integer_Literal (Loc,
-                       Subp_Number)))));
+                    Make_Integer_Literal (Loc, Subp_Number)))));
 
             Append_To (Dispatch_On_Address,
               Make_Elsif_Part (Loc,
                 Condition =>
                   Make_Op_Eq (Loc,
-                    Left_Opnd  =>
-                      New_Occurrence_Of (Local_Address, Loc),
-                    Right_Opnd =>
-                      New_Occurrence_Of (Subp_Proxy_Addr, Loc)),
+                    Left_Opnd  => New_Occurrence_Of (Local_Address, Loc),
+                    Right_Opnd => New_Occurrence_Of (Subp_Proxy_Addr, Loc)),
+
                 Then_Statements => New_List (
                   Make_Assignment_Statement (Loc,
                     New_Occurrence_Of (Subp_Index, Loc),
-                    Make_Integer_Literal (Loc,
-                       Subp_Number)))));
+                    Make_Integer_Literal (Loc, Subp_Number)))));
          end Append_Stubs_To;
 
+         ----------------------
+         -- Visit_Subprogram --
+         ----------------------
+
+         procedure Visit_Subprogram (Decl : Node_Id) is
+            Loc      : constant Source_Ptr := Sloc (Decl);
+            Spec     : constant Node_Id    := Specification (Decl);
+            Subp_Def : constant Entity_Id  := Defining_Unit_Name (Spec);
+
+            Subp_Val : String_Id;
+
+            Subp_Dist_Name : constant Entity_Id :=
+                               Make_Defining_Identifier (Loc,
+                                 Chars =>
+                                   New_External_Name
+                                     (Related_Id   => Chars (Subp_Def),
+                                      Suffix       => 'D',
+                                      Suffix_Index => -1));
+
+            Current_Stubs  : Node_Id;
+            Proxy_Obj_Addr : Entity_Id;
+
+         begin
+            --  Build receiving stub
+
+            Current_Stubs :=
+              Build_Subprogram_Receiving_Stubs
+                (Vis_Decl     => Decl,
+                 Asynchronous => Nkind (Spec) = N_Procedure_Specification
+                                   and then Is_Asynchronous (Subp_Def));
+
+            Append_To (Decls, Current_Stubs);
+            Analyze (Current_Stubs);
+
+            --  Build RAS proxy
+
+            Add_RAS_Proxy_And_Analyze (Decls,
+              Vis_Decl           => Decl,
+              All_Calls_Remote_E => All_Calls_Remote_E,
+              Proxy_Object_Addr  => Proxy_Obj_Addr);
+
+            --  Compute distribution identifier
+
+            Assign_Subprogram_Identifier
+              (Subp_Def, Current_Subp_Number, Subp_Val);
+
+            pragma Assert
+              (Current_Subp_Number = Get_Subprogram_Id (Subp_Def));
+
+            Append_To (Decls,
+              Make_Object_Declaration (Loc,
+                Defining_Identifier => Subp_Dist_Name,
+                Constant_Present    => True,
+                Object_Definition   =>
+                  New_Occurrence_Of (Standard_String, Loc),
+                Expression          =>
+                  Make_String_Literal (Loc, Subp_Val)));
+            Analyze (Last (Decls));
+
+            --  Add subprogram descriptor (RCI_Subp_Info) to the subprograms
+            --  table for this receiver. The aggregate below must be kept
+            --  consistent with the declaration of RCI_Subp_Info in
+            --  System.Partition_Interface.
+
+            Append_To (Subp_Info_List,
+              Make_Component_Association (Loc,
+                Choices    =>
+                  New_List (Make_Integer_Literal (Loc, Current_Subp_Number)),
+
+                Expression =>
+                  Make_Aggregate (Loc,
+                    Expressions => New_List (
+
+                      --  Name =>
+
+                      Make_Attribute_Reference (Loc,
+                        Prefix         =>
+                          New_Occurrence_Of (Subp_Dist_Name, Loc),
+                        Attribute_Name => Name_Address),
+
+                      --  Name_Length =>
+
+                      Make_Attribute_Reference (Loc,
+                        Prefix         =>
+                          New_Occurrence_Of (Subp_Dist_Name, Loc),
+                        Attribute_Name => Name_Length),
+
+                      --  Addr =>
+
+                      New_Occurrence_Of (Proxy_Obj_Addr, Loc)))));
+
+            Append_Stubs_To (Pkg_RPC_Receiver_Cases,
+              Declaration     => Decl,
+              Stubs           => Current_Stubs,
+              Subp_Number     => Current_Subp_Number,
+              Subp_Dist_Name  => Subp_Dist_Name,
+              Subp_Proxy_Addr => Proxy_Obj_Addr);
+
+            Current_Subp_Number := Current_Subp_Number + 1;
+         end Visit_Subprogram;
+
+         procedure Visit_Spec is new Build_Package_Stubs (Visit_Subprogram);
+
       --  Start of processing for Add_Receiving_Stubs_To_Declarations
 
       begin
          --  Building receiving stubs consist in several operations:
 
-         --    - a package RPC receiver must be built. This subprogram
-         --      will get a Subprogram_Id from the incoming stream
-         --      and will dispatch the call to the right subprogram;
+         --    - a package RPC receiver must be built. This subprogram will get
+         --      a Subprogram_Id from the incoming stream and will dispatch the
+         --      call to the right subprogram;
 
          --    - a receiving stub for each subprogram visible in the package
          --      spec. This stub will read all the parameters from the stream,
          --      and put the result as well as the exception occurrence in the
          --      output stream;
 
-         --    - a dummy package with an empty spec and a body made of an
-         --      elaboration part, whose job is to register the receiving
-         --      part of this RCI package on the name server. This is done
-         --      by calling System.Partition_Interface.Register_Receiving_Stub.
-
          Build_RPC_Receiver_Body (
            RPC_Receiver => Pkg_RPC_Receiver,
            Request      => Request,
@@ -6982,20 +6950,19 @@ package body Exp_Dist is
 
          Append_To (Pkg_RPC_Receiver_Decls,
            Make_Object_Declaration (Loc,
-             Defining_Identifier =>
-               Is_Local,
+             Defining_Identifier => Is_Local,
              Object_Definition   =>
                New_Occurrence_Of (Standard_Boolean, Loc)));
+
          Append_To (Pkg_RPC_Receiver_Decls,
            Make_Object_Declaration (Loc,
-             Defining_Identifier =>
-               Local_Address,
+             Defining_Identifier => Local_Address,
              Object_Definition   =>
                New_Occurrence_Of (RTE (RE_Address), Loc)));
+
          Append_To (Pkg_RPC_Receiver_Statements,
            Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
+             Name => New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
              Parameter_Associations => New_List (
                Make_Selected_Component (Loc,
                  Prefix        => Request,
@@ -7003,9 +6970,9 @@ package body Exp_Dist is
                New_Occurrence_Of (Is_Local, Loc),
                New_Occurrence_Of (Local_Address, Loc))));
 
-         --  For each subprogram, the receiving stub will be built and a
-         --  case statement will be made on the Subprogram_Id to dispatch
-         --  to the right subprogram.
+         --  For each subprogram, the receiving stub will be built and a case
+         --  statement will be made on the Subprogram_Id to dispatch to the
+         --  right subprogram.
 
          All_Calls_Remote_E := Boolean_Literals (
            Has_All_Calls_Remote (Defining_Entity (Pkg_Spec)));
@@ -7013,114 +6980,7 @@ package body Exp_Dist is
          Overload_Counter_Table.Reset;
          Reserve_NamingContext_Methods;
 
-         Current_Declaration := First (Visible_Declarations (Pkg_Spec));
-         while Present (Current_Declaration) loop
-            if Nkind (Current_Declaration) = N_Subprogram_Declaration
-              and then Comes_From_Source (Current_Declaration)
-            then
-               declare
-                  Loc : constant Source_Ptr :=
-                          Sloc (Current_Declaration);
-                  --  While specifically processing Current_Declaration, use
-                  --  its Sloc as the location of all generated nodes.
-
-                  Subp_Def : constant Entity_Id :=
-                               Defining_Unit_Name
-                                 (Specification (Current_Declaration));
-
-                  Subp_Val : String_Id;
-
-                  Subp_Dist_Name : constant Entity_Id :=
-                    Make_Defining_Identifier (Loc,
-                      New_External_Name (
-                        Related_Id   => Chars (Subp_Def),
-                        Suffix       => 'D',
-                        Suffix_Index => -1));
-
-                  Proxy_Object_Addr : Entity_Id;
-
-               begin
-                  --  Build receiving stub
-
-                  Current_Stubs :=
-                    Build_Subprogram_Receiving_Stubs
-                      (Vis_Decl     => Current_Declaration,
-                       Asynchronous =>
-                         Nkind (Specification (Current_Declaration)) =
-                             N_Procedure_Specification
-                           and then Is_Asynchronous (Subp_Def));
-
-                  Append_To (Decls, Current_Stubs);
-                  Analyze (Current_Stubs);
-
-                  --  Build RAS proxy
-
-                  Add_RAS_Proxy_And_Analyze (Decls,
-                    Vis_Decl           =>
-                      Current_Declaration,
-                    All_Calls_Remote_E =>
-                      All_Calls_Remote_E,
-                    Proxy_Object_Addr  =>
-                      Proxy_Object_Addr);
-
-                  --  Compute distribution identifier
-
-                  Assign_Subprogram_Identifier (
-                    Subp_Def,
-                    Current_Subprogram_Number,
-                    Subp_Val);
-
-                  pragma Assert (Current_Subprogram_Number =
-                    Get_Subprogram_Id (Subp_Def));
-
-                  Append_To (Decls,
-                    Make_Object_Declaration (Loc,
-                      Defining_Identifier => Subp_Dist_Name,
-                      Constant_Present    => True,
-                      Object_Definition   => New_Occurrence_Of (
-                        Standard_String, Loc),
-                      Expression          =>
-                        Make_String_Literal (Loc, Subp_Val)));
-                  Analyze (Last (Decls));
-
-                  --  Add subprogram descriptor (RCI_Subp_Info) to the
-                  --  subprograms table for this receiver. The aggregate
-                  --  below must be kept consistent with the declaration
-                  --  of type RCI_Subp_Info in System.Partition_Interface.
-
-                  Append_To (Subp_Info_List,
-                    Make_Component_Association (Loc,
-                      Choices => New_List (
-                        Make_Integer_Literal (Loc,
-                          Current_Subprogram_Number)),
-                      Expression =>
-                        Make_Aggregate (Loc,
-                          Expressions => New_List (
-                            Make_Attribute_Reference (Loc,
-                              Prefix =>
-                                New_Occurrence_Of (
-                                  Subp_Dist_Name, Loc),
-                              Attribute_Name => Name_Address),
-                            Make_Attribute_Reference (Loc,
-                              Prefix =>
-                                New_Occurrence_Of (
-                                  Subp_Dist_Name, Loc),
-                              Attribute_Name => Name_Length),
-                            New_Occurrence_Of (Proxy_Object_Addr, Loc)))));
-
-                  Append_Stubs_To (Pkg_RPC_Receiver_Cases,
-                    Declaration     => Current_Declaration,
-                    Stubs           => Current_Stubs,
-                    Subp_Number     => Current_Subprogram_Number,
-                    Subp_Dist_Name  => Subp_Dist_Name,
-                    Subp_Proxy_Addr => Proxy_Object_Addr);
-               end;
-
-               Current_Subprogram_Number := Current_Subprogram_Number + 1;
-            end if;
-
-            Next (Current_Declaration);
-         end loop;
+         Visit_Spec (Pkg_Spec);
 
          Append_To (Decls,
            Make_Object_Declaration (Loc,
@@ -7135,12 +6995,14 @@ package body Exp_Dist is
                    Make_Index_Or_Discriminant_Constraint (Loc,
                      New_List (
                        Make_Range (Loc,
-                         Low_Bound  => Make_Integer_Literal (Loc,
-                           First_RCI_Subprogram_Id),
+                         Low_Bound  =>
+                           Make_Integer_Literal (Loc,
+                             Intval => First_RCI_Subprogram_Id),
                          High_Bound =>
                            Make_Integer_Literal (Loc,
-                             First_RCI_Subprogram_Id
-                             + List_Length (Subp_Info_List) - 1)))))));
+                             Intval =>
+                               First_RCI_Subprogram_Id
+                               + List_Length (Subp_Info_List) - 1)))))));
 
          if Present (First (Subp_Info_List)) then
             Set_Expression (Last (Decls),
@@ -7166,27 +7028,22 @@ package body Exp_Dist is
               Make_Implicit_If_Statement (Pkg_Spec,
                 Condition =>
                   Make_Op_Ne (Loc,
-                    Left_Opnd  => New_Occurrence_Of
-                                    (Local_Address, Loc),
+                    Left_Opnd  => New_Occurrence_Of (Local_Address, Loc),
                     Right_Opnd => New_Occurrence_Of
                                     (RTE (RE_Null_Address), Loc)),
+
                 Then_Statements => New_List (
                   Make_Implicit_If_Statement (Pkg_Spec,
-                    Condition =>
-                      New_Occurrence_Of (Standard_False, Loc),
+                    Condition       => New_Occurrence_Of (Standard_False, Loc),
                     Then_Statements => New_List (
                       Make_Null_Statement (Loc)),
-                    Elsif_Parts =>
-                      Dispatch_On_Address)),
+                    Elsif_Parts     => Dispatch_On_Address)),
 
                 Else_Statements => New_List (
                   Make_Implicit_If_Statement (Pkg_Spec,
-                    Condition =>
-                      New_Occurrence_Of (Standard_False, Loc),
-                    Then_Statements => New_List (
-                      Make_Null_Statement (Loc)),
-                    Elsif_Parts =>
-                      Dispatch_On_Name))));
+                    Condition       => New_Occurrence_Of (Standard_False, Loc),
+                    Then_Statements => New_List (Make_Null_Statement (Loc)),
+                    Elsif_Parts     => Dispatch_On_Name))));
 
          else
             --  For a degenerate RCI with no visible subprograms,
@@ -7214,15 +7071,12 @@ package body Exp_Dist is
 
          Append_To (Pkg_RPC_Receiver_Cases,
            Make_Case_Statement_Alternative (Loc,
-             Discrete_Choices =>
-               New_List (Make_Others_Choice (Loc)),
-             Statements       =>
-               New_List (Make_Null_Statement (Loc))));
+             Discrete_Choices => New_List (Make_Others_Choice (Loc)),
+             Statements       => New_List (Make_Null_Statement (Loc))));
 
          Append_To (Pkg_RPC_Receiver_Statements,
            Make_Case_Statement (Loc,
-             Expression   =>
-               New_Occurrence_Of (Subp_Index, Loc),
+             Expression   => New_Occurrence_Of (Subp_Index, Loc),
              Alternatives => Pkg_RPC_Receiver_Cases));
 
          --  Pkg_RPC_Receiver body is now complete: insert it into the tree and
@@ -7233,73 +7087,73 @@ package body Exp_Dist is
 
          Pkg_RPC_Receiver_Object :=
            Make_Object_Declaration (Loc,
-             Defining_Identifier =>
-               Make_Defining_Identifier (Loc, New_Internal_Name ('R')),
+             Defining_Identifier => Make_Temporary (Loc, 'R'),
              Aliased_Present     => True,
-             Object_Definition   =>
-               New_Occurrence_Of (RTE (RE_Servant), Loc));
+             Object_Definition   => New_Occurrence_Of (RTE (RE_Servant), Loc));
          Append_To (Decls, Pkg_RPC_Receiver_Object);
          Analyze (Last (Decls));
 
          Get_Library_Unit_Name_String (Pkg_Spec);
+
+         --  Name
+
          Append_To (Register_Pkg_Actuals,
-            --  Name
            Make_String_Literal (Loc,
              Strval => String_From_Name_Buffer));
 
+         --  Version
+
          Append_To (Register_Pkg_Actuals,
-            --  Version
            Make_Attribute_Reference (Loc,
              Prefix         =>
                New_Occurrence_Of
                  (Defining_Entity (Pkg_Spec), Loc),
-             Attribute_Name =>
-               Name_Version));
+             Attribute_Name => Name_Version));
+
+         --  Handler
 
          Append_To (Register_Pkg_Actuals,
-            --  Handler
            Make_Attribute_Reference (Loc,
              Prefix          =>
                New_Occurrence_Of (Pkg_RPC_Receiver, Loc),
              Attribute_Name  => Name_Access));
 
+         --  Receiver
+
          Append_To (Register_Pkg_Actuals,
-            --  Receiver
            Make_Attribute_Reference (Loc,
              Prefix         =>
                New_Occurrence_Of (
-                 Defining_Identifier (
-                   Pkg_RPC_Receiver_Object), Loc),
-             Attribute_Name =>
-               Name_Access));
+                 Defining_Identifier (Pkg_RPC_Receiver_Object), Loc),
+             Attribute_Name => Name_Access));
+
+         --  Subp_Info
 
          Append_To (Register_Pkg_Actuals,
-            --  Subp_Info
            Make_Attribute_Reference (Loc,
-             Prefix         =>
-               New_Occurrence_Of (Subp_Info_Array, Loc),
-             Attribute_Name =>
-               Name_Address));
+             Prefix         => New_Occurrence_Of (Subp_Info_Array, Loc),
+             Attribute_Name => Name_Address));
+
+         --  Subp_Info_Len
 
          Append_To (Register_Pkg_Actuals,
-            --  Subp_Info_Len
            Make_Attribute_Reference (Loc,
-             Prefix         =>
-               New_Occurrence_Of (Subp_Info_Array, Loc),
-             Attribute_Name =>
-               Name_Length));
+             Prefix         => New_Occurrence_Of (Subp_Info_Array, Loc),
+             Attribute_Name => Name_Length));
+
+         --  Is_All_Calls_Remote
 
          Append_To (Register_Pkg_Actuals,
-            --  Is_All_Calls_Remote
            New_Occurrence_Of (All_Calls_Remote_E, Loc));
 
+         --  Finally call Register_Pkg_Receiving_Stub with the above parameters
+
          Append_To (Stmts,
            Make_Procedure_Call_Statement (Loc,
              Name                   =>
                New_Occurrence_Of (RTE (RE_Register_Pkg_Receiving_Stub), Loc),
              Parameter_Associations => Register_Pkg_Actuals));
          Analyze (Last (Stmts));
-
       end Add_Receiving_Stubs_To_Declarations;
 
       ---------------------------------
@@ -7322,13 +7176,39 @@ package body Exp_Dist is
       is
          Loc : constant Source_Ptr := Sloc (Nod);
 
+         Request : constant Entity_Id := Make_Temporary (Loc, 'R');
+         --  The request object constructed by these stubs
+         --  Could we use Name_R instead??? (see GLADE client stubs)
+
+         function Make_Request_RTE_Call
+           (RE      : RE_Id;
+            Actuals : List_Id := New_List) return Node_Id;
+         --  Generate a procedure call statement calling RE with the given
+         --  actuals. Request'Access is appended to the list.
+
+         ---------------------------
+         -- Make_Request_RTE_Call --
+         ---------------------------
+
+         function Make_Request_RTE_Call
+           (RE      : RE_Id;
+            Actuals : List_Id := New_List) return Node_Id
+         is
+         begin
+            Append_To (Actuals,
+              Make_Attribute_Reference (Loc,
+                Prefix         => New_Occurrence_Of (Request, Loc),
+                Attribute_Name => Name_Access));
+            return Make_Procedure_Call_Statement (Loc,
+                     Name                   =>
+                       New_Occurrence_Of (RTE (RE), Loc),
+                     Parameter_Associations => Actuals);
+         end Make_Request_RTE_Call;
+
          Arguments : Node_Id;
          --  Name of the named values list used to transmit parameters
          --  to the remote package
 
-         Request : Node_Id;
-         --  The request object constructed by these stubs
-
          Result : Node_Id;
          --  Name of the result named value (in non-APC cases) which get the
          --  result of the remote subprogram.
@@ -7359,8 +7239,8 @@ package body Exp_Dist is
          --  after the regular statements for writing out parameters.
 
          After_Statements : constant List_Id := New_List;
-         --  Statements to be executed after call returns (to assign
-         --  in out or out parameter values).
+         --  Statements to be executed after call returns (to assign IN OUT or
+         --  OUT parameter values).
 
          Etyp : Entity_Id;
          --  The type of the formal parameter being processed
@@ -7374,22 +7254,20 @@ package body Exp_Dist is
 
       begin
          --  ??? document general form of stub subprograms for the PolyORB case
-         Request :=
-           Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
 
          Append_To (Decls,
            Make_Object_Declaration (Loc,
              Defining_Identifier => Request,
-             Aliased_Present     => False,
+             Aliased_Present     => True,
              Object_Definition   =>
-                 New_Occurrence_Of (RTE (RE_Request_Access), Loc)));
+               New_Occurrence_Of (RTE (RE_Request), Loc)));
 
-         Result :=
-           Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
+         Result := Make_Temporary (Loc, 'R');
 
          if Is_Function then
-            Result_TC := PolyORB_Support.Helpers.Build_TypeCode_Call (Loc,
-              Etype (Result_Definition (Spec)), Decls);
+            Result_TC :=
+              PolyORB_Support.Helpers.Build_TypeCode_Call
+                (Loc, Etype (Result_Definition (Spec)), Decls);
          else
             Result_TC := New_Occurrence_Of (RTE (RE_TC_Void), Loc);
          end if;
@@ -7404,8 +7282,7 @@ package body Exp_Dist is
                Make_Aggregate (Loc,
                  Component_Associations => New_List (
                    Make_Component_Association (Loc,
-                     Choices => New_List (
-                       Make_Identifier (Loc, Name_Name)),
+                     Choices    => New_List (Make_Identifier (Loc, Name_Name)),
                      Expression =>
                        New_Occurrence_Of (RTE (RE_Result_Name), Loc)),
                    Make_Component_Association (Loc,
@@ -7413,19 +7290,15 @@ package body Exp_Dist is
                        Make_Identifier (Loc, Name_Argument)),
                      Expression =>
                        Make_Function_Call (Loc,
-                         Name =>
-                           New_Occurrence_Of (RTE (RE_Create_Any), Loc),
-                         Parameter_Associations => New_List (
-                           Result_TC))),
+                         Name => New_Occurrence_Of (RTE (RE_Create_Any), Loc),
+                         Parameter_Associations => New_List (Result_TC))),
                    Make_Component_Association (Loc,
-                     Choices => New_List (
+                     Choices    => New_List (
                        Make_Identifier (Loc, Name_Arg_Modes)),
-                     Expression =>
-                       Make_Integer_Literal (Loc, 0))))));
+                     Expression => Make_Integer_Literal (Loc, 0))))));
 
          if not Is_Known_Asynchronous then
-            Exception_Return_Parameter :=
-              Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
+            Exception_Return_Parameter := Make_Temporary (Loc, 'E');
 
             Append_To (Decls,
               Make_Object_Declaration (Loc,
@@ -7439,18 +7312,17 @@ package body Exp_Dist is
 
          --  Initialize and fill in arguments list
 
-         Arguments :=
-           Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
+         Arguments := Make_Temporary (Loc, 'A');
          Declare_Create_NVList (Loc, Arguments, Decls, Statements);
 
          Current_Parameter := First (Ordered_Parameters_List);
          while Present (Current_Parameter) loop
-
             if Is_RACW_Controlling_Formal (Current_Parameter, Stub_Type) then
                Is_Controlling_Formal := True;
                Is_First_Controlling_Formal :=
                  not First_Controlling_Formal_Seen;
                First_Controlling_Formal_Seen := True;
+
             else
                Is_Controlling_Formal := False;
                Is_First_Controlling_Formal := False;
@@ -7458,8 +7330,7 @@ package body Exp_Dist is
 
             if Is_Controlling_Formal then
 
-               --  In the case of a controlling formal argument, we send its
-               --  reference.
+               --  For a controlling formal argument, we send its reference
 
                Etyp := RACW_Type;
 
@@ -7467,19 +7338,16 @@ package body Exp_Dist is
                Etyp := Etype (Parameter_Type (Current_Parameter));
             end if;
 
-            --  The first controlling formal parameter is treated specially: it
-            --  is used to set the target object of the call.
+            --  The first controlling formal parameter is treated specially:
+            --  it is used to set the target object of the call.
 
             if not Is_First_Controlling_Formal then
-
                declare
                   Constrained : constant Boolean :=
                                   Is_Constrained (Etyp)
                                     or else Is_Elementary_Type (Etyp);
 
-                  Any : constant Entity_Id :=
-                          Make_Defining_Identifier (Loc,
-                            New_Internal_Name ('A'));
+                  Any : constant Entity_Id := Make_Temporary (Loc, 'A');
 
                   Actual_Parameter : Node_Id :=
                                        New_Occurrence_Of (
@@ -7504,10 +7372,8 @@ package body Exp_Dist is
                      else
                         Actual_Parameter := OK_Convert_To (Etyp,
                           Make_Attribute_Reference (Loc,
-                            Prefix =>
-                              Actual_Parameter,
-                            Attribute_Name =>
-                              Name_Unrestricted_Access));
+                            Prefix         => Actual_Parameter,
+                            Attribute_Name => Name_Unrestricted_Access));
                      end if;
 
                   end if;
@@ -7522,26 +7388,24 @@ package body Exp_Dist is
                      --  parameter (always passed as a reference) other than
                      --  the first one.
 
-                     Expr := PolyORB_Support.Helpers.Build_To_Any_Call (
-                               Actual_Parameter, Decls);
+                     Expr := PolyORB_Support.Helpers.Build_To_Any_Call
+                               (Actual_Parameter, Decls);
+
                   else
                      Expr := Make_Function_Call (Loc,
-                       Name =>
-                         New_Occurrence_Of (RTE (RE_Create_Any), Loc),
+                       Name => New_Occurrence_Of (RTE (RE_Create_Any), Loc),
                        Parameter_Associations => New_List (
-                         PolyORB_Support.Helpers.Build_TypeCode_Call (Loc,
-                           Etyp, Decls)));
+                         PolyORB_Support.Helpers.Build_TypeCode_Call
+                           (Loc, Etyp, Decls)));
                   end if;
 
                   Append_To (Decls,
                     Make_Object_Declaration (Loc,
-                      Defining_Identifier =>
-                        Any,
+                      Defining_Identifier => Any,
                       Aliased_Present     => False,
                       Object_Definition   =>
                         New_Occurrence_Of (RTE (RE_Any), Loc),
-                      Expression          =>
-                        Expr));
+                      Expression          => Expr));
 
                   Append_To (Statements,
                     Add_Parameter_To_NVList (Loc,
@@ -7553,17 +7417,25 @@ package body Exp_Dist is
                   if Out_Present (Current_Parameter)
                     and then not Is_Controlling_Formal
                   then
-                     Append_To (After_Statements,
-                       Make_Assignment_Statement (Loc,
-                         Name =>
-                           New_Occurrence_Of (
-                             Defining_Identifier (Current_Parameter), Loc),
-                           Expression =>
-                             PolyORB_Support.Helpers.Build_From_Any_Call (
-                               Etype (Parameter_Type (Current_Parameter)),
-                               New_Occurrence_Of (Any, Loc),
-                               Decls)));
-
+                     if Is_Limited_Type (Etyp) then
+                        Helpers.Assign_Opaque_From_Any (Loc,
+                           Stms   => After_Statements,
+                           Typ    => Etyp,
+                           N      => New_Occurrence_Of (Any, Loc),
+                           Target =>
+                             Defining_Identifier (Current_Parameter));
+                     else
+                        Append_To (After_Statements,
+                          Make_Assignment_Statement (Loc,
+                            Name =>
+                              New_Occurrence_Of (
+                                Defining_Identifier (Current_Parameter), Loc),
+                              Expression =>
+                                PolyORB_Support.Helpers.Build_From_Any_Call
+                                  (Etyp,
+                                   New_Occurrence_Of (Any, Loc),
+                                   Decls)));
+                     end if;
                   end if;
                end;
             end if;
@@ -7572,8 +7444,8 @@ package body Exp_Dist is
             --  this status is transmitted as well.
             --  This should be done for accessibility as well ???
 
-            if Nkind (Parameter_Type (Current_Parameter))
-              /= N_Access_Definition
+            if Nkind (Parameter_Type (Current_Parameter)) /=
+                                                    N_Access_Definition
               and then Need_Extra_Constrained (Current_Parameter)
             then
                --  In this block, we do not use the extra formal that has been
@@ -7584,28 +7456,26 @@ package body Exp_Dist is
 
                declare
                   Extra_Any_Parameter : constant Entity_Id :=
-                                      Make_Defining_Identifier
-                                        (Loc, New_Internal_Name ('P'));
+                                          Make_Temporary (Loc, 'P');
 
                   Parameter_Exp : constant Node_Id :=
                      Make_Attribute_Reference (Loc,
                        Prefix         => New_Occurrence_Of (
                          Defining_Identifier (Current_Parameter), Loc),
                        Attribute_Name => Name_Constrained);
+
                begin
                   Set_Etype (Parameter_Exp, Etype (Standard_Boolean));
 
                   Append_To (Decls,
                     Make_Object_Declaration (Loc,
-                      Defining_Identifier =>
-                        Extra_Any_Parameter,
+                      Defining_Identifier => Extra_Any_Parameter,
                       Aliased_Present     => False,
                       Object_Definition   =>
                         New_Occurrence_Of (RTE (RE_Any), Loc),
                       Expression          =>
-                        PolyORB_Support.Helpers.Build_To_Any_Call (
-                          Parameter_Exp,
-                          Decls)));
+                        PolyORB_Support.Helpers.Build_To_Any_Call
+                          (Parameter_Exp, Decls)));
 
                   Append_To (Extra_Formal_Statements,
                     Add_Parameter_To_NVList (Loc,
@@ -7626,25 +7496,27 @@ package body Exp_Dist is
          Append_To (Statements,
            Make_Procedure_Call_Statement (Loc,
              Name =>
-               New_Occurrence_Of (RTE (RE_Request_Create), Loc),
+               New_Occurrence_Of (RTE (RE_Request_Setup), Loc),
              Parameter_Associations => New_List (
+               New_Occurrence_Of (Request, Loc),
                Target_Object,
                Subprogram_Id,
                New_Occurrence_Of (Arguments, Loc),
                New_Occurrence_Of (Result, Loc),
                New_Occurrence_Of (RTE (RE_Nil_Exc_List), Loc))));
 
-         Append_To (Parameter_Associations (Last (Statements)),
-               New_Occurrence_Of (Request, Loc));
+         pragma Assert
+           (not (Is_Known_Non_Asynchronous and Is_Known_Asynchronous));
 
-         pragma Assert (
-           not (Is_Known_Non_Asynchronous and Is_Known_Asynchronous));
          if Is_Known_Non_Asynchronous or Is_Known_Asynchronous then
-            Asynchronous_P := New_Occurrence_Of (
-              Boolean_Literals (Is_Known_Asynchronous), Loc);
+            Asynchronous_P :=
+              New_Occurrence_Of
+                (Boolean_Literals (Is_Known_Asynchronous), Loc);
+
          else
             pragma Assert (Present (Asynchronous));
             Asynchronous_P := New_Copy_Tree (Asynchronous);
+
             --  The expression node Asynchronous will be used to build an 'if'
             --  statement at the end of Build_General_Calling_Stubs: we need to
             --  make a copy here.
@@ -7657,22 +7529,21 @@ package body Exp_Dist is
                  RTE (RE_Asynchronous_P_To_Sync_Scope), Loc),
              Expressions => New_List (Asynchronous_P)));
 
-         Append_To (Statements,
-             Make_Procedure_Call_Statement (Loc,
-               Name                   =>
-                 New_Occurrence_Of (RTE (RE_Request_Invoke), Loc),
-               Parameter_Associations => New_List (
-                 New_Occurrence_Of (Request, Loc))));
+         Append_To (Statements, Make_Request_RTE_Call (RE_Request_Invoke));
 
-         Non_Asynchronous_Statements := New_List (Make_Null_Statement (Loc));
-         Asynchronous_Statements := New_List (Make_Null_Statement (Loc));
+         --  Asynchronous case
 
-         if not Is_Known_Asynchronous then
+         if not Is_Known_Non_Asynchronous then
+            Asynchronous_Statements := New_List (Make_Null_Statement (Loc));
+         end if;
 
+         --  Non-asynchronous case
+
+         if not Is_Known_Asynchronous then
             --  Reraise an exception occurrence from the completed request.
             --  If the exception occurrence is empty, this is a no-op.
 
-            Append_To (Non_Asynchronous_Statements,
+            Non_Asynchronous_Statements := New_List (
               Make_Procedure_Call_Statement (Loc,
                 Name                   =>
                   New_Occurrence_Of (RTE (RE_Request_Raise_Occurrence), Loc),
@@ -7680,24 +7551,26 @@ package body Exp_Dist is
                   New_Occurrence_Of (Request, Loc))));
 
             if Is_Function then
-
                --  If this is a function call, read the value and return it
 
                Append_To (Non_Asynchronous_Statements,
                  Make_Tag_Check (Loc,
-                   Make_Return_Statement (Loc,
-                     PolyORB_Support.Helpers.Build_From_Any_Call (
-                         Etype (Result_Definition (Spec)),
-                         Make_Selected_Component (Loc,
-                           Prefix        => Result,
-                           Selector_Name => Name_Argument),
-                         Decls))));
+                   Make_Simple_Return_Statement (Loc,
+                     PolyORB_Support.Helpers.Build_From_Any_Call
+                       (Etype (Result_Definition (Spec)),
+                        Make_Selected_Component (Loc,
+                          Prefix        => Result,
+                          Selector_Name => Name_Argument),
+                        Decls))));
+
+            else
+
+               --  Case of a procedure: deal with IN OUT and OUT formals
+
+               Append_List_To (Non_Asynchronous_Statements, After_Statements);
             end if;
          end if;
 
-         Append_List_To (Non_Asynchronous_Statements,
-           After_Statements);
-
          if Is_Known_Asynchronous then
             Append_List_To (Statements, Asynchronous_Statements);
 
@@ -7725,16 +7598,17 @@ package body Exp_Dist is
          Controlling_Parameter : Entity_Id) return RPC_Target
       is
          Target_Info : RPC_Target (PCS_Kind => Name_PolyORB_DSA);
-         Target_Reference : constant Entity_Id :=
-                              Make_Defining_Identifier (Loc,
-                                New_Internal_Name ('T'));
+         Target_Reference : constant Entity_Id := Make_Temporary (Loc, 'T');
+
       begin
          if Present (Controlling_Parameter) then
             Append_To (Decls,
               Make_Object_Declaration (Loc,
                 Defining_Identifier => Target_Reference,
+
                 Object_Definition   =>
                   New_Occurrence_Of (RTE (RE_Object_Ref), Loc),
+
                 Expression          =>
                   Make_Function_Call (Loc,
                     Name =>
@@ -7743,7 +7617,8 @@ package body Exp_Dist is
                       Make_Selected_Component (Loc,
                         Prefix        => Controlling_Parameter,
                         Selector_Name => Name_Target)))));
-            --  Controlling_Parameter has the same components as
+
+            --  Note: Controlling_Parameter has the same components as
             --  System.Partition_Interface.RACW_Stub_Type.
 
             Target_Info.Object := New_Occurrence_Of (Target_Reference, Loc);
@@ -7756,6 +7631,7 @@ package body Exp_Dist is
                 Selector_Name =>
                   Make_Identifier (Loc, Name_Get_RCI_Package_Ref));
          end if;
+
          return Target_Info;
       end Build_Stub_Target;
 
@@ -7765,51 +7641,35 @@ package body Exp_Dist is
 
       procedure Build_Stub_Type
         (RACW_Type         : Entity_Id;
-         Stub_Type         : Entity_Id;
-         Stub_Type_Decl    : out Node_Id;
+         Stub_Type_Comps   : out List_Id;
          RPC_Receiver_Decl : out Node_Id)
       is
-         Loc : constant Source_Ptr := Sloc (Stub_Type);
-         pragma Warnings (Off);
-         pragma Unreferenced (RACW_Type);
-         pragma Warnings (On);
+         Loc : constant Source_Ptr := Sloc (RACW_Type);
 
       begin
-         Stub_Type_Decl :=
-           Make_Full_Type_Declaration (Loc,
-             Defining_Identifier => Stub_Type,
-             Type_Definition     =>
-               Make_Record_Definition (Loc,
-                 Tagged_Present  => True,
-                 Limited_Present => True,
-                 Component_List  =>
-                   Make_Component_List (Loc,
-                     Component_Items => New_List (
-
-                       Make_Component_Declaration (Loc,
-                         Defining_Identifier =>
-                           Make_Defining_Identifier (Loc, Name_Target),
-                         Component_Definition =>
-                           Make_Component_Definition (Loc,
-                             Aliased_Present     =>
-                               False,
-                             Subtype_Indication  =>
-                               New_Occurrence_Of (RTE (RE_Entity_Ptr), Loc))),
-
-                       Make_Component_Declaration (Loc,
-                         Defining_Identifier =>
-                           Make_Defining_Identifier (Loc, Name_Asynchronous),
-                         Component_Definition =>
-                           Make_Component_Definition (Loc,
-                             Aliased_Present    => False,
-                             Subtype_Indication =>
-                               New_Occurrence_Of (
-                                 Standard_Boolean, Loc)))))));
+         Stub_Type_Comps := New_List (
+           Make_Component_Declaration (Loc,
+             Defining_Identifier =>
+               Make_Defining_Identifier (Loc, Name_Target),
+             Component_Definition =>
+               Make_Component_Definition (Loc,
+                 Aliased_Present     => False,
+                 Subtype_Indication  =>
+                   New_Occurrence_Of (RTE (RE_Entity_Ptr), Loc))),
+
+           Make_Component_Declaration (Loc,
+             Defining_Identifier =>
+               Make_Defining_Identifier (Loc, Name_Asynchronous),
+
+             Component_Definition =>
+               Make_Component_Definition (Loc,
+                 Aliased_Present    => False,
+                 Subtype_Indication =>
+                   New_Occurrence_Of (Standard_Boolean, Loc))));
 
          RPC_Receiver_Decl :=
            Make_Object_Declaration (Loc,
-             Defining_Identifier => Make_Defining_Identifier (Loc,
-                                      New_Internal_Name ('R')),
+             Defining_Identifier => Make_Temporary (Loc, 'R'),
              Aliased_Present     => True,
              Object_Definition   =>
                New_Occurrence_Of (RTE (RE_Servant), Loc));
@@ -7836,9 +7696,9 @@ package body Exp_Dist is
          Request := Make_Defining_Identifier (Loc, Name_R);
 
          RPC_Receiver_Spec :=
-           Build_RPC_Receiver_Specification (
-             RPC_Receiver      => RPC_Receiver,
-             Request_Parameter => Request);
+           Build_RPC_Receiver_Specification
+             (RPC_Receiver      => RPC_Receiver,
+              Request_Parameter => Request);
 
          Subp_Id    := Make_Defining_Identifier (Loc, Name_P);
          Subp_Index := Make_Defining_Identifier (Loc, Name_I);
@@ -7889,9 +7749,7 @@ package body Exp_Dist is
       is
          Loc : constant Source_Ptr := Sloc (Vis_Decl);
 
-         Request_Parameter : constant Entity_Id :=
-                               Make_Defining_Identifier (Loc,
-                                 New_Internal_Name ('R'));
+         Request_Parameter : constant Entity_Id := Make_Temporary (Loc, 'R');
          --  Formal parameter for receiving stubs: a descriptor for an incoming
          --  request.
 
@@ -7910,8 +7768,8 @@ package body Exp_Dist is
 
          Decls : constant List_Id := New_List;
          --  All the parameters will get declared before calling the real
-         --  subprograms. Also the out parameters will be declared.
-         --  At this level, parameters may be unconstrained.
+         --  subprograms. Also the out parameters will be declared. At this
+         --  level, parameters may be unconstrained.
 
          Statements : constant List_Id := New_List;
 
@@ -7935,9 +7793,7 @@ package body Exp_Dist is
                                      Build_Ordered_Parameters_List
                                        (Specification (Vis_Decl));
 
-         Arguments : constant Entity_Id :=
-                       Make_Defining_Identifier (Loc,
-                         New_Internal_Name ('A'));
+         Arguments : constant Entity_Id := Make_Temporary (Loc, 'A');
          --  Name of the named values list used to retrieve parameters
 
          Subp_Spec : Node_Id;
@@ -7952,8 +7808,8 @@ package body Exp_Dist is
               New_Occurrence_Of (Parent_Primitive, Loc);
          else
             Called_Subprogram :=
-              New_Occurrence_Of (
-                Defining_Unit_Name (Specification (Vis_Decl)), Loc);
+              New_Occurrence_Of
+                (Defining_Unit_Name (Specification (Vis_Decl)), Loc);
          end if;
 
          Declare_Create_NVList (Loc, Arguments, Outer_Decls, Outer_Statements);
@@ -7967,14 +7823,13 @@ package body Exp_Dist is
             declare
                Etyp        : Entity_Id;
                Constrained : Boolean;
-               Any         : Entity_Id := Empty;
-               Object      : constant Entity_Id :=
-                               Make_Defining_Identifier (Loc,
-                                 New_Internal_Name ('P'));
-               Expr        : Node_Id   := Empty;
+               Any         : Entity_Id          := Empty;
+               Object      : constant Entity_Id := Make_Temporary (Loc, 'P');
+               Expr        : Node_Id            := Empty;
 
-               Is_Controlling_Formal : constant Boolean
-                 := Is_RACW_Controlling_Formal (Current_Parameter, Stub_Type);
+               Is_Controlling_Formal : constant Boolean :=
+                                         Is_RACW_Controlling_Formal
+                                           (Current_Parameter, Stub_Type);
 
                Is_First_Controlling_Formal : Boolean := False;
 
@@ -7986,8 +7841,10 @@ package body Exp_Dist is
 
                   --  Controlling formals in distributed object primitive
                   --  operations are handled specially:
+
                   --    - the first controlling formal is used as the
                   --      target of the call;
+
                   --    - the remaining controlling formals are transmitted
                   --      as RACWs.
 
@@ -7995,30 +7852,28 @@ package body Exp_Dist is
                   Is_First_Controlling_Formal :=
                     not First_Controlling_Formal_Seen;
                   First_Controlling_Formal_Seen := True;
+
                else
                   Etyp := Etype (Parameter_Type (Current_Parameter));
                end if;
 
                Constrained :=
-                 Is_Constrained (Etyp)
-                 or else Is_Elementary_Type (Etyp);
+                 Is_Constrained (Etyp) or else Is_Elementary_Type (Etyp);
 
                if not Is_First_Controlling_Formal then
-                  Any := Make_Defining_Identifier (Loc,
-                           New_Internal_Name ('A'));
+                  Any := Make_Temporary (Loc, 'A');
+
                   Append_To (Outer_Decls,
                     Make_Object_Declaration (Loc,
-                      Defining_Identifier =>
-                        Any,
+                      Defining_Identifier => Any,
                       Object_Definition   =>
                         New_Occurrence_Of (RTE (RE_Any), Loc),
                       Expression =>
                         Make_Function_Call (Loc,
-                          Name =>
-                            New_Occurrence_Of (RTE (RE_Create_Any), Loc),
+                          Name => New_Occurrence_Of (RTE (RE_Create_Any), Loc),
                           Parameter_Associations => New_List (
-                            PolyORB_Support.Helpers.Build_TypeCode_Call (Loc,
-                              Etyp, Outer_Decls)))));
+                            PolyORB_Support.Helpers.Build_TypeCode_Call
+                              (Loc, Etyp, Outer_Decls)))));
 
                   Append_To (Outer_Statements,
                     Add_Parameter_To_NVList (Loc,
@@ -8030,38 +7885,35 @@ package body Exp_Dist is
 
                if Is_First_Controlling_Formal then
                   declare
-                     Addr : constant Entity_Id :=
-                       Make_Defining_Identifier (Loc,
-                         New_Internal_Name ('A'));
+                     Addr : constant Entity_Id := Make_Temporary (Loc, 'A');
+
                      Is_Local : constant Entity_Id :=
-                       Make_Defining_Identifier (Loc,
-                         New_Internal_Name ('L'));
-                  begin
+                                  Make_Temporary (Loc, 'L');
 
+                  begin
                      --  Special case: obtain the first controlling formal
                      --  from the target of the remote call, instead of the
                      --  argument list.
 
                      Append_To (Outer_Decls,
                        Make_Object_Declaration (Loc,
-                         Defining_Identifier =>
-                           Addr,
+                         Defining_Identifier => Addr,
                          Object_Definition =>
                            New_Occurrence_Of (RTE (RE_Address), Loc)));
+
                      Append_To (Outer_Decls,
                        Make_Object_Declaration (Loc,
-                         Defining_Identifier =>
-                           Is_Local,
+                         Defining_Identifier => Is_Local,
                          Object_Definition =>
                            New_Occurrence_Of (Standard_Boolean, Loc)));
+
                      Append_To (Outer_Statements,
                        Make_Procedure_Call_Statement (Loc,
                          Name =>
-                           New_Occurrence_Of (
-                             RTE (RE_Get_Local_Address), Loc),
+                           New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
                          Parameter_Associations => New_List (
                            Make_Selected_Component (Loc,
-                             Prefix =>
+                             Prefix        =>
                                New_Occurrence_Of (
                                  Request_Parameter, Loc),
                              Selector_Name =>
@@ -8083,23 +7935,32 @@ package body Exp_Dist is
                   --  the object declaration and the variable is set using
                   --  'Input instead of 'Read.
 
-                  Expr := PolyORB_Support.Helpers.Build_From_Any_Call (
-                            Etyp, New_Occurrence_Of (Any, Loc), Decls);
+                  if Constrained and then Is_Limited_Type (Etyp) then
+                     Helpers.Assign_Opaque_From_Any (Loc,
+                        Stms   => Statements,
+                        Typ    => Etyp,
+                        N      => New_Occurrence_Of (Any, Loc),
+                        Target => Object);
 
-                  if Constrained then
-                     Append_To (Statements,
-                       Make_Assignment_Statement (Loc,
-                         Name =>
-                           New_Occurrence_Of (Object, Loc),
-                         Expression =>
-                            Expr));
-                     Expr := Empty;
                   else
+                     Expr := Helpers.Build_From_Any_Call
+                               (Etyp, New_Occurrence_Of (Any, Loc), Decls);
+
+                     if Constrained then
+                        Append_To (Statements,
+                          Make_Assignment_Statement (Loc,
+                            Name       => New_Occurrence_Of (Object, Loc),
+                            Expression => Expr));
+                        Expr := Empty;
+
+                     else
+                        --  Expr will be used to initialize (and constrain) the
+                        --  parameter when it is declared.
+                        null;
+                     end if;
+
                      null;
-                     --  Expr will be used to initialize (and constrain) the
-                     --  parameter when it is declared.
                   end if;
-
                end if;
 
                Need_Extra_Constrained :=
@@ -8136,13 +7997,11 @@ package body Exp_Dist is
                then
                   Append_To (After_Statements,
                     Make_Procedure_Call_Statement (Loc,
-                      Name =>
-                        New_Occurrence_Of (RTE (RE_Move_Any_Value), Loc),
+                      Name => New_Occurrence_Of (RTE (RE_Move_Any_Value), Loc),
                       Parameter_Associations => New_List (
                         New_Occurrence_Of (Any, Loc),
-                        PolyORB_Support.Helpers.Build_To_Any_Call (
-                          New_Occurrence_Of (Object, Loc),
-                          Decls))));
+                        PolyORB_Support.Helpers.Build_To_Any_Call
+                          (New_Occurrence_Of (Object, Loc), Decls))));
                end if;
 
                --  For RACW controlling formals, the Etyp of Object is always
@@ -8151,29 +8010,26 @@ package body Exp_Dist is
 
                if Is_Controlling_Formal then
                   if Nkind (Parameter_Type (Current_Parameter)) /=
-                    N_Access_Definition
+                                                        N_Access_Definition
                   then
                      Append_To (Parameter_List,
                        Make_Parameter_Association (Loc,
                          Selector_Name             =>
-                           New_Occurrence_Of (
-                             Defining_Identifier (Current_Parameter), Loc),
+                           New_Occurrence_Of
+                             (Defining_Identifier (Current_Parameter), Loc),
                          Explicit_Actual_Parameter =>
                            Make_Explicit_Dereference (Loc,
-                             Unchecked_Convert_To (RACW_Type,
-                               OK_Convert_To (RTE (RE_Address),
-                                 New_Occurrence_Of (Object, Loc))))));
+                             Prefix => New_Occurrence_Of (Object, Loc))));
 
                   else
                      Append_To (Parameter_List,
                        Make_Parameter_Association (Loc,
                          Selector_Name             =>
-                           New_Occurrence_Of (
-                             Defining_Identifier (Current_Parameter), Loc),
+                           New_Occurrence_Of
+                             (Defining_Identifier (Current_Parameter), Loc),
+
                          Explicit_Actual_Parameter =>
-                           Unchecked_Convert_To (RACW_Type,
-                             OK_Convert_To (RTE (RE_Address),
-                               New_Occurrence_Of (Object, Loc)))));
+                           New_Occurrence_Of (Object, Loc)));
                   end if;
 
                else
@@ -8200,21 +8056,21 @@ package body Exp_Dist is
                                          Extra_Constrained
                                            (Defining_Identifier
                                              (Current_Parameter));
+
                      Extra_Any : constant Entity_Id :=
-                       Make_Defining_Identifier
-                         (Loc, New_Internal_Name ('A'));
+                                   Make_Temporary (Loc, 'A');
 
                      Formal_Entity : constant Entity_Id :=
-                                       Make_Defining_Identifier
-                                           (Loc, Chars (Extra_Parameter));
+                                       Make_Defining_Identifier (Loc,
+                                         Chars => Chars (Extra_Parameter));
 
                      Formal_Type : constant Entity_Id :=
                                      Etype (Extra_Parameter);
+
                   begin
                      Append_To (Outer_Decls,
                        Make_Object_Declaration (Loc,
-                         Defining_Identifier =>
-                           Extra_Any,
+                         Defining_Identifier => Extra_Any,
                          Object_Definition   =>
                            New_Occurrence_Of (RTE (RE_Any), Loc),
                          Expression =>
@@ -8240,13 +8096,12 @@ package body Exp_Dist is
 
                      Append_To (Statements,
                        Make_Assignment_Statement (Loc,
-                         Name =>
-                           New_Occurrence_Of (Formal_Entity, Loc),
+                         Name => New_Occurrence_Of (Formal_Entity, Loc),
                          Expression =>
-                           PolyORB_Support.Helpers.Build_From_Any_Call (
-                             Formal_Type,
-                             New_Occurrence_Of (Extra_Any, Loc),
-                             Decls)));
+                           PolyORB_Support.Helpers.Build_From_Any_Call
+                             (Formal_Type,
+                              New_Occurrence_Of (Extra_Any, Loc),
+                              Decls)));
                      Set_Extra_Constrained (Object, Formal_Entity);
                   end;
                end if;
@@ -8261,24 +8116,21 @@ package body Exp_Dist is
 
          Append_To (Outer_Statements,
            Make_Procedure_Call_Statement (Loc,
-             Name =>
-               New_Occurrence_Of (RTE (RE_Request_Arguments), Loc),
+             Name => New_Occurrence_Of (RTE (RE_Request_Arguments), Loc),
              Parameter_Associations => New_List (
                New_Occurrence_Of (Request_Parameter, Loc),
                New_Occurrence_Of (Arguments, Loc))));
 
          if Nkind (Specification (Vis_Decl)) = N_Function_Specification then
 
-            --  The remote subprogram is a function. We build an inner block to
-            --  be able to hold a potentially unconstrained result in a
-            --  variable.
+            --  The remote subprogram is a function: Build an inner block to be
+            --  able to hold a potentially unconstrained result in a variable.
 
             declare
                Etyp   : constant Entity_Id :=
                           Etype (Result_Definition (Specification (Vis_Decl)));
-               Result : constant Node_Id   :=
-                          Make_Defining_Identifier (Loc,
-                            New_Internal_Name ('R'));
+               Result : constant Node_Id   := Make_Temporary (Loc, 'R');
+
             begin
                Inner_Decls := New_List (
                  Make_Object_Declaration (Loc,
@@ -8294,7 +8146,7 @@ package body Exp_Dist is
 
                   --  For a remote call to a function with a class-wide type,
                   --  check that the returned value satisfies the requirements
-                  --  of E.4(18).
+                  --  of (RM E.4(18)).
 
                   Append_To (Inner_Decls,
                     Make_Transportable_Check (Loc,
@@ -8305,13 +8157,12 @@ package body Exp_Dist is
                Set_Etype (Result, Etyp);
                Append_To (After_Statements,
                  Make_Procedure_Call_Statement (Loc,
-                   Name =>
-                     New_Occurrence_Of (RTE (RE_Set_Result), Loc),
+                   Name => New_Occurrence_Of (RTE (RE_Set_Result), Loc),
                    Parameter_Associations => New_List (
                      New_Occurrence_Of (Request_Parameter, Loc),
-                     PolyORB_Support.Helpers.Build_To_Any_Call (
-                       New_Occurrence_Of (Result, Loc),
-                       Decls))));
+                     PolyORB_Support.Helpers.Build_To_Any_Call
+                       (New_Occurrence_Of (Result, Loc), Decls))));
+
                --  A DSA function does not have out or inout arguments
             end;
 
@@ -8332,8 +8183,7 @@ package body Exp_Dist is
 
             Append_To (After_Statements,
               Make_Procedure_Call_Statement (Loc,
-                Name =>
-                  New_Occurrence_Of (RTE (RE_Request_Set_Out), Loc),
+                Name => New_Occurrence_Of (RTE (RE_Request_Set_Out), Loc),
                 Parameter_Associations => New_List (
                   New_Occurrence_Of (Request_Parameter, Loc))));
 
@@ -8347,8 +8197,7 @@ package body Exp_Dist is
 
          Subp_Spec :=
            Make_Procedure_Specification (Loc,
-             Defining_Unit_Name       =>
-               Make_Defining_Identifier (Loc, New_Internal_Name ('F')),
+             Defining_Unit_Name       => Make_Temporary (Loc, 'F'),
 
              Parameter_Specifications => New_List (
                Make_Parameter_Specification (Loc,
@@ -8356,10 +8205,10 @@ package body Exp_Dist is
                  Parameter_Type      =>
                    New_Occurrence_Of (RTE (RE_Request_Access), Loc))));
 
-         --  An exception raised during the execution of an incoming
-         --  remote subprogram call and that needs to be sent back
-         --  to the caller is propagated by the receiving stubs, and
-         --  will be handled by the caller (the distribution runtime).
+         --  An exception raised during the execution of an incoming remote
+         --  subprogram call and that needs to be sent back to the caller is
+         --  propagated by the receiving stubs, and will be handled by the
+         --  caller (the distribution runtime).
 
          if Asynchronous and then not Dynamically_Asynchronous then
 
@@ -8371,7 +8220,6 @@ package body Exp_Dist is
                 Statements        => New_List (Make_Null_Statement (Loc))));
 
          else
-
             --  In the other cases, if an exception is raised, then the
             --  exception occurrence is propagated.
 
@@ -8380,8 +8228,7 @@ package body Exp_Dist is
 
          Append_To (Outer_Statements,
            Make_Block_Statement (Loc,
-             Declarations =>
-               Decls,
+             Declarations => Decls,
              Handled_Statement_Sequence =>
                Make_Handled_Sequence_Of_Statements (Loc,
                  Statements => Statements)));
@@ -8408,16 +8255,15 @@ package body Exp_Dist is
 
          function Find_Numeric_Representation
            (Typ : Entity_Id) return Entity_Id;
-         --  Given a numeric type Typ, return the smallest integer or floarting
+         --  Given a numeric type Typ, return the smallest integer or floating
          --  point type from Standard, or the smallest unsigned (modular) type
          --  from System.Unsigned_Types, whose range encompasses that of Typ.
 
-         function Make_Stream_Procedure_Function_Name
+         function Make_Helper_Function_Name
            (Loc : Source_Ptr;
             Typ : Entity_Id;
             Nam : Name_Id) return Entity_Id;
-         --  Return the name to be assigned for stream subprogram Nam of Typ.
-         --  (copied from exp_strm.adb, should be shared???)
+         --  Return the name to be assigned for helper subprogram Nam of Typ
 
          ------------------------------------------------------------
          -- Common subprograms for building various tree fragments --
@@ -8428,8 +8274,8 @@ package body Exp_Dist is
             Any : Entity_Id;
             TC  : Node_Id;
             Idx : Node_Id) return Node_Id;
-         --  Build a call to Get_Aggregate_Element on Any
-         --  for typecode TC, returning the Idx'th element.
+         --  Build a call to Get_Aggregate_Element on Any for typecode TC,
+         --  returning the Idx'th element.
 
          generic
             Subprogram : Entity_Id;
@@ -8438,7 +8284,7 @@ package body Exp_Dist is
             Arry : Entity_Id;
             --  For 'Range and Etype
 
-            Indices : List_Id;
+            Indexes : List_Id;
             --  For the construction of the innermost element expression
 
             with procedure Add_Process_Element
@@ -8454,7 +8300,7 @@ package body Exp_Dist is
             Depth   : Pos       := 1);
          --  Build nested loop statements that iterate over the elements of an
          --  array Arry. The statement(s) built by Add_Process_Element are
-         --  executed for each element; Indices is the list of indices to be
+         --  executed for each element; Indexes is the list of indexes to be
          --  used in the construction of the indexed component that denotes the
          --  current element. Subprogram is the entity for the subprogram for
          --  which this iterator is generated. The generated statements are
@@ -8512,10 +8358,12 @@ package body Exp_Dist is
             Item := First (CI);
             while Present (Item) loop
                Def := Defining_Identifier (Item);
+
                if not Is_Internal_Name (Chars (Def)) then
                   Add_Process_Element
                     (Stmts, Container, Counter, Rec, Def);
                end if;
+
                Next (Item);
             end loop;
 
@@ -8524,6 +8372,118 @@ package body Exp_Dist is
             end if;
          end Append_Record_Traversal;
 
+         -----------------------------
+         -- Assign_Opaque_From_Any --
+         -----------------------------
+
+         procedure Assign_Opaque_From_Any
+           (Loc    : Source_Ptr;
+            Stms   : List_Id;
+            Typ    : Entity_Id;
+            N      : Node_Id;
+            Target : Entity_Id)
+         is
+            Strm : constant Entity_Id := Make_Temporary (Loc, 'S');
+            Expr : Node_Id;
+
+            Read_Call_List : List_Id;
+            --  List on which to place the 'Read attribute reference
+
+         begin
+            --  Strm : Buffer_Stream_Type;
+
+            Append_To (Stms,
+              Make_Object_Declaration (Loc,
+                Defining_Identifier => Strm,
+                Aliased_Present     => True,
+                Object_Definition   =>
+                  New_Occurrence_Of (RTE (RE_Buffer_Stream_Type), Loc)));
+
+            --  Any_To_BS (Strm, A);
+
+            Append_To (Stms,
+              Make_Procedure_Call_Statement (Loc,
+                Name => New_Occurrence_Of (RTE (RE_Any_To_BS), Loc),
+                Parameter_Associations => New_List (
+                  N,
+                  New_Occurrence_Of (Strm, Loc))));
+
+            if Transmit_As_Unconstrained (Typ) then
+               Expr :=
+                 Make_Attribute_Reference (Loc,
+                   Prefix         => New_Occurrence_Of (Typ, Loc),
+                   Attribute_Name => Name_Input,
+                   Expressions    => New_List (
+                     Make_Attribute_Reference (Loc,
+                       Prefix         => New_Occurrence_Of (Strm, Loc),
+                       Attribute_Name => Name_Access)));
+
+               --  Target := Typ'Input (Strm'Access)
+
+               if Present (Target) then
+                  Append_To (Stms,
+                    Make_Assignment_Statement (Loc,
+                      Name       => New_Occurrence_Of (Target, Loc),
+                      Expression => Expr));
+
+               --  return Typ'Input (Strm'Access);
+
+               else
+                  Append_To (Stms,
+                    Make_Simple_Return_Statement (Loc,
+                      Expression => Expr));
+               end if;
+
+            else
+               if Present (Target) then
+                  Read_Call_List := Stms;
+                  Expr := New_Occurrence_Of (Target, Loc);
+
+               else
+                  declare
+                     Temp : constant Entity_Id := Make_Temporary (Loc, 'R');
+
+                  begin
+                     Read_Call_List := New_List;
+                     Expr := New_Occurrence_Of (Temp, Loc);
+
+                     Append_To (Stms, Make_Block_Statement (Loc,
+                       Declarations               => New_List (
+                         Make_Object_Declaration (Loc,
+                           Defining_Identifier =>
+                             Temp,
+                           Object_Definition   =>
+                             New_Occurrence_Of (Typ, Loc))),
+
+                       Handled_Statement_Sequence =>
+                         Make_Handled_Sequence_Of_Statements (Loc,
+                           Statements => Read_Call_List)));
+                  end;
+               end if;
+
+               --  Typ'Read (Strm'Access, [Target|Temp])
+
+               Append_To (Read_Call_List,
+                 Make_Attribute_Reference (Loc,
+                   Prefix         => New_Occurrence_Of (Typ, Loc),
+                   Attribute_Name => Name_Read,
+                   Expressions    => New_List (
+                     Make_Attribute_Reference (Loc,
+                       Prefix         => New_Occurrence_Of (Strm, Loc),
+                       Attribute_Name => Name_Access),
+                     Expr)));
+
+               if No (Target) then
+
+                  --  return Temp
+
+                  Append_To (Read_Call_List,
+                    Make_Simple_Return_Statement (Loc,
+                       Expression => New_Copy (Expr)));
+               end if;
+            end if;
+         end Assign_Opaque_From_Any;
+
          -------------------------
          -- Build_From_Any_Call --
          -------------------------
@@ -8540,13 +8500,22 @@ package body Exp_Dist is
             Fnam    : Entity_Id := Empty;
             Lib_RE  : RE_Id := RE_Null;
             Result  : Node_Id;
-         begin
 
+         begin
             --  First simple case where the From_Any function is present
             --  in the type's TSS.
 
             Fnam := Find_Inherited_TSS (U_Type, TSS_From_Any);
 
+            --  For the subtype representing a generic actual type, go to the
+            --  actual type.
+
+            if Is_Generic_Actual_Type (U_Type) then
+               U_Type := Underlying_Type (Base_Type (U_Type));
+            end if;
+
+            --  For a standard subtype, go to the base type
+
             if Sloc (U_Type) <= Standard_Location then
                U_Type := Base_Type (U_Type);
             end if;
@@ -8621,14 +8590,20 @@ package body Exp_Dist is
             elsif U_Type = RTE (RE_Long_Long_Unsigned) then
                Lib_RE := RE_FA_LLU;
 
-            elsif U_Type = Standard_String then
+            elsif Is_RTE (U_Type, RE_Unbounded_String) then
                Lib_RE := RE_FA_String;
 
+            --  Special DSA types
+
+            elsif Is_RTE (U_Type, RE_Any_Container_Ptr) then
+               Lib_RE := RE_FA_A;
+
             --  Other (non-primitive) types
 
             else
                declare
                   Decl : Entity_Id;
+
                begin
                   Build_From_Any_Function (Loc, U_Type, Decl, Fnam);
                   Append_To (Decls, Decl);
@@ -8665,52 +8640,68 @@ package body Exp_Dist is
             Decl : out Node_Id;
             Fnam : out Entity_Id)
          is
-            Spec : Node_Id;
+            Spec  : Node_Id;
             Decls : constant List_Id := New_List;
-            Stms : constant List_Id := New_List;
-            Any_Parameter : constant Entity_Id
-              := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
+            Stms  : constant List_Id := New_List;
+
+            Any_Parameter : constant Entity_Id := Make_Temporary (Loc, 'A');
+
+            Use_Opaque_Representation : Boolean;
+
          begin
-            if Is_Itype (Typ) then
+            --  For a derived type, we can't go past the base type (to the
+            --  parent type) here, because that would cause the attribute's
+            --  formal parameter to have the wrong type; hence the Base_Type
+            --  check here.
+
+            if Is_Itype (Typ) and then Typ /= Base_Type (Typ) then
                Build_From_Any_Function
                   (Loc  => Loc,
-                  Typ  => Etype (Typ),
-                  Decl => Decl,
-                  Fnam => Fnam);
+                   Typ  => Etype (Typ),
+                   Decl => Decl,
+                   Fnam => Fnam);
                return;
             end if;
 
-            Fnam := Make_Stream_Procedure_Function_Name (Loc,
-                      Typ, Name_uFrom_Any);
+            Fnam := Make_Helper_Function_Name (Loc, Typ, Name_From_Any);
 
             Spec :=
               Make_Function_Specification (Loc,
                 Defining_Unit_Name => Fnam,
                 Parameter_Specifications => New_List (
                   Make_Parameter_Specification (Loc,
-                    Defining_Identifier =>
-                      Any_Parameter,
-                    Parameter_Type =>
-                      New_Occurrence_Of (RTE (RE_Any), Loc))),
+                    Defining_Identifier => Any_Parameter,
+                    Parameter_Type => New_Occurrence_Of (RTE (RE_Any), Loc))),
                 Result_Definition => New_Occurrence_Of (Typ, Loc));
 
-            --  The following  is taken care of by Exp_Dist.Add_RACW_From_Any
+            --  The RACW case is taken care of by Exp_Dist.Add_RACW_From_Any
 
             pragma Assert
               (not (Is_Remote_Access_To_Class_Wide_Type (Typ)));
 
-            if Is_Derived_Type (Typ)
-              and then not Is_Tagged_Type (Typ)
+            Use_Opaque_Representation := False;
+
+            if Has_Stream_Attribute_Definition
+                 (Typ, TSS_Stream_Output, At_Any_Place => True)
+              or else
+               Has_Stream_Attribute_Definition
+                 (Typ, TSS_Stream_Write, At_Any_Place => True)
             then
+               --  If user-defined stream attributes are specified for this
+               --  type, use them and transmit data as an opaque sequence of
+               --  stream elements.
+
+               Use_Opaque_Representation := True;
+
+            elsif Is_Derived_Type (Typ) and then not Is_Tagged_Type (Typ) then
                Append_To (Stms,
-                 Make_Return_Statement (Loc,
+                 Make_Simple_Return_Statement (Loc,
                    Expression =>
-                     OK_Convert_To (
-                       Typ,
-                       Build_From_Any_Call (
-                         Root_Type (Typ),
-                         New_Occurrence_Of (Any_Parameter, Loc),
-                         Decls))));
+                     OK_Convert_To (Typ,
+                       Build_From_Any_Call
+                         (Root_Type (Typ),
+                          New_Occurrence_Of (Any_Parameter, Loc),
+                          Decls))));
 
             elsif Is_Record_Type (Typ)
               and then not Is_Derived_Type (Typ)
@@ -8718,27 +8709,25 @@ package body Exp_Dist is
             then
                if Nkind (Declaration_Node (Typ)) = N_Subtype_Declaration then
                   Append_To (Stms,
-                    Make_Return_Statement (Loc,
+                    Make_Simple_Return_Statement (Loc,
                       Expression =>
-                        OK_Convert_To (
-                          Typ,
-                          Build_From_Any_Call (
-                            Etype (Typ),
-                            New_Occurrence_Of (Any_Parameter, Loc),
-                            Decls))));
+                        Build_From_Any_Call
+                          (Etype (Typ),
+                           New_Occurrence_Of (Any_Parameter, Loc),
+                           Decls)));
+
                else
                   declare
-                     Disc : Entity_Id := Empty;
+                     Disc                      : Entity_Id := Empty;
                      Discriminant_Associations : List_Id;
-                     Rdef : constant Node_Id :=
-                       Type_Definition (Declaration_Node (Typ));
-                     Component_Counter : Int := 0;
+                     Rdef                      : constant Node_Id :=
+                                                   Type_Definition
+                                                     (Declaration_Node (Typ));
+                     Component_Counter         : Int := 0;
 
                      --  The returned object
 
-                     Res : constant Entity_Id :=
-                             Make_Defining_Identifier (Loc,
-                               New_Internal_Name ('R'));
+                     Res : constant Entity_Id := Make_Temporary (Loc, 'R');
 
                      Res_Definition : Node_Id := New_Occurrence_Of (Typ, Loc);
 
@@ -8751,8 +8740,8 @@ package body Exp_Dist is
 
                      procedure FA_Append_Record_Traversal is
                         new Append_Record_Traversal
-                       (Rec                 => Res,
-                        Add_Process_Element => FA_Rec_Add_Process_Element);
+                          (Rec                 => Res,
+                           Add_Process_Element => FA_Rec_Add_Process_Element);
 
                      --------------------------------
                      -- FA_Rec_Add_Process_Element --
@@ -8765,11 +8754,13 @@ package body Exp_Dist is
                         Rec     : Entity_Id;
                         Field   : Node_Id)
                      is
+                        Ctyp : Entity_Id;
                      begin
                         if Nkind (Field) = N_Defining_Identifier then
-
                            --  A regular component
 
+                           Ctyp := Etype (Field);
+
                            Append_To (Stmts,
                              Make_Assignment_Statement (Loc,
                                Name => Make_Selected_Component (Loc,
@@ -8777,21 +8768,22 @@ package body Exp_Dist is
                                    New_Occurrence_Of (Rec, Loc),
                                  Selector_Name =>
                                    New_Occurrence_Of (Field, Loc)),
+
                                Expression =>
-                                 Build_From_Any_Call (Etype (Field),
+                                 Build_From_Any_Call (Ctyp,
                                    Build_Get_Aggregate_Element (Loc,
                                      Any => Any,
-                                     Tc  => Build_TypeCode_Call (Loc,
-                                              Etype (Field), Decls),
-                                     Idx => Make_Integer_Literal (Loc,
-                                              Counter)),
+                                     TC  =>
+                                       Build_TypeCode_Call (Loc, Ctyp, Decls),
+                                     Idx =>
+                                       Make_Integer_Literal (Loc, Counter)),
                                    Decls)));
 
                         else
                            --  A variant part
 
                            declare
-                              Variant : Node_Id;
+                              Variant        : Node_Id;
                               Struct_Counter : Int := 0;
 
                               Block_Decls : constant List_Id := New_List;
@@ -8802,40 +8794,40 @@ package body Exp_Dist is
                               Choice_List : List_Id;
 
                               Struct_Any : constant Entity_Id :=
-                                             Make_Defining_Identifier (Loc,
-                                               New_Internal_Name ('S'));
+                                             Make_Temporary (Loc, 'S');
 
                            begin
                               Append_To (Decls,
                                 Make_Object_Declaration (Loc,
-                                  Defining_Identifier =>
-                                    Struct_Any,
-                                  Constant_Present =>
-                                     True,
-                                  Object_Definition =>
+                                  Defining_Identifier => Struct_Any,
+                                  Constant_Present    => True,
+                                  Object_Definition   =>
                                      New_Occurrence_Of (RTE (RE_Any), Loc),
-                                  Expression =>
+                                  Expression          =>
                                     Make_Function_Call (Loc,
-                                      Name => New_Occurrence_Of (
-                                        RTE (RE_Extract_Union_Value), Loc),
+                                      Name =>
+                                        New_Occurrence_Of
+                                          (RTE (RE_Extract_Union_Value), Loc),
+
                                       Parameter_Associations => New_List (
                                         Build_Get_Aggregate_Element (Loc,
                                           Any => Any,
-                                          Tc  => Make_Function_Call (Loc,
-                                            Name => New_Occurrence_Of (
-                                              RTE (RE_Any_Member_Type), Loc),
-                                            Parameter_Associations =>
-                                              New_List (
-                                                New_Occurrence_Of (Any, Loc),
-                                                Make_Integer_Literal (Loc,
-                                                  Counter))),
-                                          Idx => Make_Integer_Literal (Loc,
-                                            Counter))))));
+                                          TC  =>
+                                            Make_Function_Call (Loc,
+                                              Name => New_Occurrence_Of (
+                                                RTE (RE_Any_Member_Type), Loc),
+                                              Parameter_Associations =>
+                                                New_List (
+                                                  New_Occurrence_Of (Any, Loc),
+                                                  Make_Integer_Literal (Loc,
+                                                    Intval => Counter))),
+                                          Idx =>
+                                            Make_Integer_Literal (Loc,
+                                             Intval => Counter))))));
 
                               Append_To (Stmts,
                                 Make_Block_Statement (Loc,
-                                  Declarations =>
-                                    Block_Decls,
+                                  Declarations => Block_Decls,
                                   Handled_Statement_Sequence =>
                                     Make_Handled_Sequence_Of_Statements (Loc,
                                       Statements => Block_Stmts)));
@@ -8845,23 +8837,21 @@ package body Exp_Dist is
                                     Expression =>
                                       Make_Selected_Component (Loc,
                                         Prefix        => Rec,
-                                        Selector_Name =>
-                                          Chars (Name (Field))),
-                                    Alternatives =>
-                                      Alt_List));
+                                        Selector_Name => Chars (Name (Field))),
+                                    Alternatives => Alt_List));
 
                               Variant := First_Non_Pragma (Variants (Field));
-
                               while Present (Variant) loop
-                                 Choice_List := New_Copy_List_Tree
-                                   (Discrete_Choices (Variant));
+                                 Choice_List :=
+                                   New_Copy_List_Tree
+                                     (Discrete_Choices (Variant));
 
                                  VP_Stmts := New_List;
 
                                  --  Struct_Counter should be reset before
                                  --  handling a variant part. Indeed only one
                                  --  of the case statement alternatives will be
-                                 --  executed at run-time, so the counter must
+                                 --  executed at run time, so the counter must
                                  --  start at 0 for every case statement.
 
                                  Struct_Counter := 0;
@@ -8875,12 +8865,12 @@ package body Exp_Dist is
                                  Append_To (Alt_List,
                                    Make_Case_Statement_Alternative (Loc,
                                      Discrete_Choices => Choice_List,
-                                     Statements =>
-                                       VP_Stmts));
+                                     Statements       => VP_Stmts));
                                  Next_Non_Pragma (Variant);
                               end loop;
                            end;
                         end if;
+
                         Counter := Counter + 1;
                      end FA_Rec_Add_Process_Element;
 
@@ -8888,32 +8878,35 @@ package body Exp_Dist is
                      --  First all discriminants
 
                      if Has_Discriminants (Typ) then
-                        Disc := First_Discriminant (Typ);
                         Discriminant_Associations := New_List;
 
+                        Disc := First_Discriminant (Typ);
                         while Present (Disc) loop
                            declare
                               Disc_Var_Name : constant Entity_Id :=
-                                Make_Defining_Identifier (Loc, Chars (Disc));
-                              Disc_Type : constant Entity_Id :=
-                                Etype (Disc);
+                                                Make_Defining_Identifier (Loc,
+                                                  Chars => Chars (Disc));
+                              Disc_Type     : constant Entity_Id :=
+                                                Etype (Disc);
+
                            begin
                               Append_To (Decls,
                                 Make_Object_Declaration (Loc,
-                                  Defining_Identifier =>
-                                    Disc_Var_Name,
-                                  Constant_Present => True,
-                                  Object_Definition =>
+                                  Defining_Identifier => Disc_Var_Name,
+                                  Constant_Present    => True,
+                                  Object_Definition   =>
                                     New_Occurrence_Of (Disc_Type, Loc),
+
                                   Expression =>
                                     Build_From_Any_Call (Disc_Type,
                                       Build_Get_Aggregate_Element (Loc,
                                         Any => Any_Parameter,
-                                        Tc  => Build_TypeCode_Call
+                                        TC  => Build_TypeCode_Call
                                                  (Loc, Disc_Type, Decls),
-                                        Idx => Make_Integer_Literal
-                                                 (Loc, Component_Counter)),
+                                        Idx => Make_Integer_Literal (Loc,
+                                               Intval => Component_Counter)),
                                       Decls)));
+
                               Component_Counter := Component_Counter + 1;
 
                               Append_To (Discriminant_Associations,
@@ -8926,11 +8919,12 @@ package body Exp_Dist is
                            Next_Discriminant (Disc);
                         end loop;
 
-                        Res_Definition := Make_Subtype_Indication (Loc,
-                          Subtype_Mark => Res_Definition,
-                          Constraint   =>
-                            Make_Index_Or_Discriminant_Constraint (Loc,
-                              Discriminant_Associations));
+                        Res_Definition :=
+                          Make_Subtype_Indication (Loc,
+                            Subtype_Mark => Res_Definition,
+                            Constraint   =>
+                              Make_Index_Or_Discriminant_Constraint (Loc,
+                                Discriminant_Associations));
                      end if;
 
                      --  Now we have all the discriminants in variables, we can
@@ -8942,10 +8936,8 @@ package body Exp_Dist is
 
                      Append_To (Decls,
                        Make_Object_Declaration (Loc,
-                         Defining_Identifier =>
-                           Res,
-                         Object_Definition =>
-                           Res_Definition));
+                         Defining_Identifier => Res,
+                         Object_Definition   => Res_Definition));
 
                      --  ... then all components
 
@@ -8955,7 +8947,7 @@ package body Exp_Dist is
                        Counter   => Component_Counter);
 
                      Append_To (Stms,
-                       Make_Return_Statement (Loc,
+                       Make_Simple_Return_Statement (Loc,
                          Expression => New_Occurrence_Of (Res, Loc)));
                   end;
                end if;
@@ -8990,18 +8982,19 @@ package body Exp_Dist is
                          Expression => Empty);
 
                      Element_Any : Node_Id;
-                  begin
 
+                  begin
                      declare
                         Element_TC : Node_Id;
-                     begin
 
+                     begin
                         if Etype (Datum) = RTE (RE_Any) then
 
                            --  When Datum is an Any the Etype field is not
                            --  sufficient to determine the typecode of Datum
                            --  (which can be a TC_SEQUENCE or TC_ARRAY
                            --  depending on the value of Constrained).
+
                            --  Therefore we retrieve the typecode which has
                            --  been constructed in Append_Array_Traversal with
                            --  a call to Get_Any_Type.
@@ -9023,7 +9016,7 @@ package body Exp_Dist is
                         Element_Any :=
                           Build_Get_Aggregate_Element (Loc,
                             Any => Any,
-                            Tc  => Element_TC,
+                            TC  => Element_TC,
                             Idx => New_Occurrence_Of (Counter, Loc));
                      end;
 
@@ -9036,10 +9029,8 @@ package body Exp_Dist is
                            New_Occurrence_Of (Counter, Loc),
                          Expression =>
                            Make_Op_Add (Loc,
-                             Left_Opnd =>
-                               New_Occurrence_Of (Counter, Loc),
-                             Right_Opnd =>
-                               Make_Integer_Literal (Loc, 1))));
+                             Left_Opnd  => New_Occurrence_Of (Counter, Loc),
+                             Right_Opnd => Make_Integer_Literal (Loc, 1))));
 
                      if Nkind (Datum) /= N_Attribute_Reference then
 
@@ -9049,17 +9040,20 @@ package body Exp_Dist is
 
                         if Etype (Datum) /= RTE (RE_Any) then
                            Set_Expression (Assignment,
-                              Build_From_Any_Call (
-                                Component_Type (Typ),
-                                Element_Any,
-                                Decls));
+                              Build_From_Any_Call
+                                (Component_Type (Typ), Element_Any, Decls));
                         else
                            Set_Expression (Assignment, Element_Any);
                         end if;
+
                         Prepend_To (Stmts, Assignment);
                      end if;
                   end FA_Ary_Add_Process_Element;
 
+                  ------------------------
+                  -- Local Declarations --
+                  ------------------------
+
                   Counter : constant Entity_Id :=
                               Make_Defining_Identifier (Loc, Name_J);
 
@@ -9075,7 +9069,7 @@ package body Exp_Dist is
                     new Append_Array_Traversal (
                       Subprogram => Fnam,
                       Arry       => Res,
-                      Indices    => New_List,
+                      Indexes    => New_List,
                       Add_Process_Element => FA_Ary_Add_Process_Element);
 
                   Res_Subtype_Indication : Node_Id :=
@@ -9096,59 +9090,70 @@ package body Exp_Dist is
                         for J in 1 .. Ndim loop
                            Lnam := New_External_Name ('L', J);
                            Hnam := New_External_Name ('H', J);
-                           Indt := Etype (Indx);
+
+                           --  Note, for empty arrays bounds may be out of
+                           --  the range of Etype (Indx).
+
+                           Indt := Base_Type (Etype (Indx));
 
                            Append_To (Decls,
                              Make_Object_Declaration (Loc,
                                Defining_Identifier =>
                                  Make_Defining_Identifier (Loc, Lnam),
-                               Constant_Present    =>
-                                 True,
+                               Constant_Present    => True,
                                Object_Definition   =>
                                  New_Occurrence_Of (Indt, Loc),
                                Expression          =>
-                                 Build_From_Any_Call (
-                                   Indt,
-                                   Build_Get_Aggregate_Element (Loc,
-                                     Any => Any_Parameter,
-                                     Tc  => Build_TypeCode_Call (Loc,
-                                              Indt, Decls),
-                                     Idx => Make_Integer_Literal (Loc, J - 1)),
+                                 Build_From_Any_Call
+                                   (Indt,
+                                    Build_Get_Aggregate_Element (Loc,
+                                      Any => Any_Parameter,
+                                      TC  => Build_TypeCode_Call
+                                               (Loc, Indt, Decls),
+                                      Idx =>
+                                        Make_Integer_Literal (Loc, J - 1)),
                                    Decls)));
 
                            Append_To (Decls,
                              Make_Object_Declaration (Loc,
                                Defining_Identifier =>
                                  Make_Defining_Identifier (Loc, Hnam),
-                               Constant_Present =>
-                                 True,
+
+                               Constant_Present => True,
+
                                Object_Definition =>
                                  New_Occurrence_Of (Indt, Loc),
+
                                Expression => Make_Attribute_Reference (Loc,
                                  Prefix         =>
                                    New_Occurrence_Of (Indt, Loc),
+
                                  Attribute_Name => Name_Val,
+
                                  Expressions    => New_List (
                                    Make_Op_Subtract (Loc,
                                      Left_Opnd =>
                                        Make_Op_Add (Loc,
                                          Left_Opnd =>
-                                           OK_Convert_To (
-                                             Standard_Long_Integer,
-                                             Make_Identifier (Loc, Lnam)),
+                                           OK_Convert_To
+                                             (Standard_Long_Integer,
+                                              Make_Identifier (Loc, Lnam)),
+
                                          Right_Opnd =>
-                                           OK_Convert_To (
-                                             Standard_Long_Integer,
-                                             Make_Function_Call (Loc,
-                                               Name => New_Occurrence_Of (RTE (
-                                                 RE_Get_Nested_Sequence_Length
-                                                 ), Loc),
-                                               Parameter_Associations =>
-                                                 New_List (
-                                                   New_Occurrence_Of (
-                                                     Any_Parameter, Loc),
-                                                   Make_Integer_Literal (Loc,
-                                                     J))))),
+                                           OK_Convert_To
+                                             (Standard_Long_Integer,
+                                              Make_Function_Call (Loc,
+                                                Name =>
+                                                  New_Occurrence_Of (RTE (
+                                                  RE_Get_Nested_Sequence_Length
+                                                  ), Loc),
+                                                Parameter_Associations =>
+                                                  New_List (
+                                                    New_Occurrence_Of (
+                                                      Any_Parameter, Loc),
+                                                    Make_Integer_Literal (Loc,
+                                                      Intval => J))))),
+
                                      Right_Opnd =>
                                        Make_Integer_Literal (Loc, 1))))));
 
@@ -9166,8 +9171,7 @@ package body Exp_Dist is
 
                         Initial_Counter_Value := Ndim;
                         Res_Subtype_Indication := Make_Subtype_Indication (Loc,
-                          Subtype_Mark =>
-                            Res_Subtype_Indication,
+                          Subtype_Mark => Res_Subtype_Indication,
                           Constraint   =>
                             Make_Index_Or_Discriminant_Constraint (Loc,
                               Constraints => Ranges));
@@ -9191,110 +9195,41 @@ package body Exp_Dist is
                   Append_To (Decls,
                     Make_Object_Declaration (Loc,
                       Defining_Identifier => Component_TC,
-                      Constant_Present => True,
-                      Object_Definition =>
+                      Constant_Present    => True,
+                      Object_Definition   =>
                         New_Occurrence_Of (RTE (RE_TypeCode), Loc),
-                      Expression =>
+                      Expression          =>
                         Build_TypeCode_Call (Loc,
                           Component_Type (Typ), Decls)));
 
-                  Append_From_Any_Array_Iterator (Stms,
-                    Any_Parameter, Counter);
+                  Append_From_Any_Array_Iterator
+                    (Stms, Any_Parameter, Counter);
 
                   Append_To (Stms,
-                    Make_Return_Statement (Loc,
+                    Make_Simple_Return_Statement (Loc,
                       Expression => New_Occurrence_Of (Res, Loc)));
                end;
 
             elsif Is_Integer_Type (Typ) or else Is_Unsigned_Type (Typ) then
                Append_To (Stms,
-                 Make_Return_Statement (Loc,
+                 Make_Simple_Return_Statement (Loc,
                    Expression =>
-                     Unchecked_Convert_To (
-                       Typ,
-                       Build_From_Any_Call (
-                         Find_Numeric_Representation (Typ),
-                         New_Occurrence_Of (Any_Parameter, Loc),
-                         Decls))));
+                     Unchecked_Convert_To (Typ,
+                       Build_From_Any_Call
+                         (Find_Numeric_Representation (Typ),
+                          New_Occurrence_Of (Any_Parameter, Loc),
+                          Decls))));
 
             else
-               --  Default: type is represented as an opaque sequence of bytes
-
-               declare
-                  Strm : constant Entity_Id :=
-                           Make_Defining_Identifier (Loc,
-                             Chars => New_Internal_Name ('S'));
-                  Res  : constant Entity_Id :=
-                           Make_Defining_Identifier (Loc,
-                             Chars => New_Internal_Name ('R'));
-
-               begin
-                  --  Strm : Buffer_Stream_Type;
-
-                  Append_To (Decls,
-                    Make_Object_Declaration (Loc,
-                      Defining_Identifier =>
-                        Strm,
-                      Aliased_Present     =>
-                        True,
-                      Object_Definition   =>
-                        New_Occurrence_Of (RTE (RE_Buffer_Stream_Type), Loc)));
-
-                  --  Allocate_Buffer (Strm);
-
-                  Append_To (Stms,
-                    Make_Procedure_Call_Statement (Loc,
-                      Name =>
-                        New_Occurrence_Of (RTE (RE_Allocate_Buffer), Loc),
-                      Parameter_Associations => New_List (
-                        New_Occurrence_Of (Strm, Loc))));
-
-                  --  Any_To_BS (Strm, A);
-
-                  Append_To (Stms,
-                    Make_Procedure_Call_Statement (Loc,
-                      Name =>
-                        New_Occurrence_Of (RTE (RE_Any_To_BS), Loc),
-                      Parameter_Associations => New_List (
-                        New_Occurrence_Of (Any_Parameter, Loc),
-                        New_Occurrence_Of (Strm, Loc))));
-
-                  --  declare
-                  --     Res : constant T := T'Input (Strm);
-                  --  begin
-                  --     Release_Buffer (Strm);
-                  --     return Res;
-                  --  end;
-
-                  Append_To (Stms, Make_Block_Statement (Loc,
-                    Declarations => New_List (
-                      Make_Object_Declaration (Loc,
-                        Defining_Identifier => Res,
-                        Constant_Present    => True,
-                        Object_Definition   =>
-                          New_Occurrence_Of (Typ, Loc),
-                        Expression          =>
-                            Make_Attribute_Reference (Loc,
-                              Prefix         => New_Occurrence_Of (Typ, Loc),
-                              Attribute_Name => Name_Input,
-                              Expressions => New_List (
-                                Make_Attribute_Reference (Loc,
-                                  Prefix => New_Occurrence_Of (Strm, Loc),
-                                  Attribute_Name => Name_Access))))),
-
-                    Handled_Statement_Sequence =>
-                      Make_Handled_Sequence_Of_Statements (Loc,
-                        Statements => New_List (
-                          Make_Procedure_Call_Statement (Loc,
-                            Name =>
-                              New_Occurrence_Of (RTE (RE_Release_Buffer), Loc),
-                            Parameter_Associations =>
-                              New_List (
-                                New_Occurrence_Of (Strm, Loc))),
-                          Make_Return_Statement (Loc,
-                            Expression => New_Occurrence_Of (Res, Loc))))));
+               Use_Opaque_Representation := True;
+            end if;
 
-               end;
+            if Use_Opaque_Representation then
+               Assign_Opaque_From_Any (Loc,
+                  Stms   => Stms,
+                  Typ    => Typ,
+                  N      => New_Occurrence_Of (Any_Parameter, Loc),
+                  Target => Empty);
             end if;
 
             Decl :=
@@ -9319,8 +9254,7 @@ package body Exp_Dist is
          begin
             return Make_Function_Call (Loc,
               Name =>
-                New_Occurrence_Of (
-                  RTE (RE_Get_Aggregate_Element), Loc),
+                New_Occurrence_Of (RTE (RE_Get_Aggregate_Element), Loc),
               Parameter_Associations => New_List (
                 New_Occurrence_Of (Any, Loc),
                 TC,
@@ -9340,14 +9274,14 @@ package body Exp_Dist is
             Start_String;
             Store_String_Chars ("DSA:");
             Get_Library_Unit_Name_String (Scope (E));
-            Store_String_Chars (
-              Name_Buffer (Name_Buffer'First
-                .. Name_Buffer'First + Name_Len - 1));
+            Store_String_Chars
+              (Name_Buffer (Name_Buffer'First ..
+               Name_Buffer'First + Name_Len - 1));
             Store_String_Char ('.');
             Get_Name_String (Chars (E));
-            Store_String_Chars (
-              Name_Buffer (Name_Buffer'First
-                .. Name_Buffer'First + Name_Len - 1));
+            Store_String_Chars
+              (Name_Buffer (Name_Buffer'First ..
+               Name_Buffer'First + Name_Len - 1));
             Store_String_Chars (":1.0");
             Repo_Id_Str := End_String;
             Name_Str    := String_From_Name_Buffer;
@@ -9363,24 +9297,23 @@ package body Exp_Dist is
          is
             Loc : constant Source_Ptr := Sloc (N);
 
-            Typ     : Entity_Id := Etype (N);
-            U_Type  : Entity_Id;
-
-            Fnam    : Entity_Id := Empty;
-            Lib_RE  : RE_Id := RE_Null;
+            Typ    : Entity_Id := Etype (N);
+            U_Type : Entity_Id;
+            C_Type : Entity_Id;
+            Fnam   : Entity_Id := Empty;
+            Lib_RE : RE_Id := RE_Null;
 
          begin
             --  If N is a selected component, then maybe its Etype has not been
-            --  set yet: try to use the Etype of the selector_name in that
-            --  case.
+            --  set yet: try to use Etype of the selector_name in that case.
 
             if No (Typ) and then Nkind (N) = N_Selected_Component then
                Typ := Etype (Selector_Name (N));
             end if;
+
             pragma Assert (Present (Typ));
 
-            --  The full view, if Typ is private; the completion, if Typ is
-            --  incomplete.
+            --  Get full view for private type, completion for incomplete type
 
             U_Type := Underlying_Type (Typ);
 
@@ -9389,12 +9322,14 @@ package body Exp_Dist is
 
             Fnam := Find_Inherited_TSS (U_Type, TSS_To_Any);
 
-            --  Check first for Boolean and Character. These are enumeration
-            --  types, but we treat them specially, since they may require
-            --  special handling in the transfer protocol. However, this
-            --  special handling only applies if they have standard
-            --  representation, otherwise they are treated like any other
-            --  enumeration type.
+            --  For the subtype representing a generic actual type, go to the
+            --  actual type.
+
+            if Is_Generic_Actual_Type (U_Type) then
+               U_Type := Underlying_Type (Base_Type (U_Type));
+            end if;
+
+            --  For a standard subtype, go to the base type
 
             if Sloc (U_Type) <= Standard_Location then
                U_Type := Base_Type (U_Type);
@@ -9403,6 +9338,13 @@ package body Exp_Dist is
             if Present (Fnam) then
                null;
 
+            --  Check first for Boolean and Character. These are enumeration
+            --  types, but we treat them specially, since they may require
+            --  special handling in the transfer protocol. However, this
+            --  special handling only applies if they have standard
+            --  representation, otherwise they are treated like any other
+            --  enumeration type.
+
             elsif U_Type = Standard_Boolean then
                Lib_RE := RE_TA_B;
 
@@ -9463,10 +9405,19 @@ package body Exp_Dist is
             elsif U_Type = RTE (RE_Long_Long_Unsigned) then
                Lib_RE := RE_TA_LLU;
 
-            elsif U_Type = Standard_String then
+            elsif Is_RTE (U_Type, RE_Unbounded_String) then
                Lib_RE := RE_TA_String;
 
+            --  Special DSA types
+
+            elsif Is_RTE (U_Type, RE_Any_Container_Ptr) then
+               Lib_RE := RE_TA_A;
+               U_Type := Typ;
+
             elsif U_Type = Underlying_Type (RTE (RE_TypeCode)) then
+
+               --  No corresponding FA_TC ???
+
                Lib_RE := RE_TA_TC;
 
             --  Other (non-primitive) types
@@ -9487,11 +9438,23 @@ package body Exp_Dist is
                Fnam := RTE (Lib_RE);
             end if;
 
+            --  If Fnam is already analyzed, find the proper expected type,
+            --  else we have a newly constructed To_Any function and we know
+            --  that the expected type of its parameter is U_Type.
+
+            if Ekind (Fnam) = E_Function
+              and then Present (First_Formal (Fnam))
+            then
+               C_Type := Etype (First_Formal (Fnam));
+            else
+               C_Type := U_Type;
+            end if;
+
             return
                 Make_Function_Call (Loc,
                   Name                   => New_Occurrence_Of (Fnam, Loc),
                   Parameter_Associations =>
-                    New_List (Unchecked_Convert_To (U_Type, N)));
+                    New_List (OK_Convert_To (C_Type, N)));
          end Build_To_Any_Call;
 
          ---------------------------
@@ -9508,80 +9471,104 @@ package body Exp_Dist is
             Decls : constant List_Id := New_List;
             Stms  : constant List_Id := New_List;
 
-            Expr_Parameter : constant Entity_Id :=
-                               Make_Defining_Identifier (Loc, Name_E);
-
-            Any : constant Entity_Id :=
-                    Make_Defining_Identifier (Loc, Name_A);
+            Expr_Parameter : Entity_Id;
+            Any            : Entity_Id;
+            Result_TC      : Node_Id;
 
             Any_Decl  : Node_Id;
-            Result_TC : Node_Id := Build_TypeCode_Call (Loc, Typ, Decls);
+
+            Use_Opaque_Representation : Boolean;
+            --  When True, use stream attributes and represent type as an
+            --  opaque sequence of bytes.
 
          begin
-            if Is_Itype (Typ) then
+            --  For a derived type, we can't go past the base type (to the
+            --  parent type) here, because that would cause the attribute's
+            --  formal parameter to have the wrong type; hence the Base_Type
+            --  check here.
+
+            if Is_Itype (Typ) and then Typ /= Base_Type (Typ) then
                Build_To_Any_Function
-                  (Loc  => Loc,
+                 (Loc  => Loc,
                   Typ  => Etype (Typ),
                   Decl => Decl,
                   Fnam => Fnam);
                return;
             end if;
 
-            Fnam := Make_Stream_Procedure_Function_Name (Loc,
-                      Typ, Name_uTo_Any);
+            Expr_Parameter := Make_Defining_Identifier (Loc, Name_E);
+            Any            := Make_Defining_Identifier (Loc, Name_A);
+            Result_TC      := Build_TypeCode_Call (Loc, Typ, Decls);
+
+            Fnam := Make_Helper_Function_Name (Loc, Typ, Name_To_Any);
 
             Spec :=
               Make_Function_Specification (Loc,
                 Defining_Unit_Name => Fnam,
                 Parameter_Specifications => New_List (
                   Make_Parameter_Specification (Loc,
-                    Defining_Identifier =>
-                      Expr_Parameter,
-                    Parameter_Type =>
-                      New_Occurrence_Of (Typ, Loc))),
-                Result_Definition => New_Occurrence_Of (RTE (RE_Any), Loc));
+                    Defining_Identifier => Expr_Parameter,
+                    Parameter_Type => New_Occurrence_Of (Typ, Loc))),
+                Result_Definition  => New_Occurrence_Of (RTE (RE_Any), Loc));
             Set_Etype (Expr_Parameter, Typ);
 
             Any_Decl :=
               Make_Object_Declaration (Loc,
-                Defining_Identifier =>
-                  Any,
-                Object_Definition   =>
-                  New_Occurrence_Of (RTE (RE_Any), Loc));
+                Defining_Identifier => Any,
+                Object_Definition   => New_Occurrence_Of (RTE (RE_Any), Loc));
+
+            Use_Opaque_Representation := False;
+
+            if Has_Stream_Attribute_Definition
+                 (Typ, TSS_Stream_Output, At_Any_Place => True)
+              or else
+               Has_Stream_Attribute_Definition
+                 (Typ, TSS_Stream_Write,  At_Any_Place => True)
+            then
+               --  If user-defined stream attributes are specified for this
+               --  type, use them and transmit data as an opaque sequence of
+               --  stream elements.
+
+               Use_Opaque_Representation := True;
+
+            elsif Is_Derived_Type (Typ) and then not Is_Tagged_Type (Typ) then
+
+               --  Non-tagged derived type: convert to root type
 
-            if Is_Derived_Type (Typ) and then not Is_Tagged_Type (Typ) then
                declare
-                  Rt_Type : constant Entity_Id
-                    := Root_Type (Typ);
-                  Expr : constant Node_Id
-                    := OK_Convert_To (
-                         Rt_Type,
-                         New_Occurrence_Of (Expr_Parameter, Loc));
+                  Rt_Type : constant Entity_Id := Root_Type (Typ);
+                  Expr    : constant Node_Id :=
+                              OK_Convert_To
+                                (Rt_Type,
+                                 New_Occurrence_Of (Expr_Parameter, Loc));
                begin
                   Set_Expression (Any_Decl, Build_To_Any_Call (Expr, Decls));
                end;
 
             elsif Is_Record_Type (Typ) and then not Is_Tagged_Type (Typ) then
+
+               --  Non-tagged record type
+
                if Nkind (Declaration_Node (Typ)) = N_Subtype_Declaration then
                   declare
-                     Rt_Type : constant Entity_Id
-                       := Etype (Typ);
-                     Expr : constant Node_Id
-                       := OK_Convert_To (
-                            Rt_Type,
-                            New_Occurrence_Of (Expr_Parameter, Loc));
+                     Rt_Type : constant Entity_Id := Etype (Typ);
+                     Expr    : constant Node_Id :=
+                                 OK_Convert_To (Rt_Type,
+                                   New_Occurrence_Of (Expr_Parameter, Loc));
 
                   begin
-                     Set_Expression (Any_Decl,
-                       Build_To_Any_Call (Expr, Decls));
+                     Set_Expression
+                       (Any_Decl, Build_To_Any_Call (Expr, Decls));
                   end;
 
+               --  Comment needed here (and label on declare block ???)
+
                else
                   declare
-                     Disc : Entity_Id := Empty;
-                     Rdef : constant Node_Id :=
-                              Type_Definition (Declaration_Node (Typ));
-                     Counter : Int := 0;
+                     Disc     : Entity_Id := Empty;
+                     Rdef     : constant Node_Id :=
+                                  Type_Definition (Declaration_Node (Typ));
+                     Counter  : Int := 0;
                      Elements : constant List_Id := New_List;
 
                      procedure TA_Rec_Add_Process_Element
@@ -9590,6 +9577,7 @@ package body Exp_Dist is
                         Counter   : in out Int;
                         Rec       : Entity_Id;
                         Field     : Node_Id);
+                     --  Processing routine for traversal below
 
                      procedure TA_Append_Record_Traversal is
                         new Append_Record_Traversal
@@ -9631,29 +9619,27 @@ package body Exp_Dist is
                         else
                            --  A variant part
 
-                           declare
-                              Variant : Node_Id;
+                           Variant_Part : declare
+                              Variant        : Node_Id;
                               Struct_Counter : Int := 0;
 
                               Block_Decls : constant List_Id := New_List;
                               Block_Stmts : constant List_Id := New_List;
                               VP_Stmts    : List_Id;
 
-                              Alt_List : constant List_Id := New_List;
+                              Alt_List    : constant List_Id := New_List;
                               Choice_List : List_Id;
 
                               Union_Any : constant Entity_Id :=
-                                            Make_Defining_Identifier (Loc,
-                                              New_Internal_Name ('V'));
+                                            Make_Temporary (Loc, 'V');
 
                               Struct_Any : constant Entity_Id :=
-                                             Make_Defining_Identifier (Loc,
-                                                New_Internal_Name ('S'));
+                                             Make_Temporary (Loc, 'S');
 
                               function Make_Discriminant_Reference
                                 return Node_Id;
-                              --  Build a selected component for the
-                              --  discriminant of this variant part.
+                              --  Build reference to the discriminant for this
+                              --  variant part.
 
                               ---------------------------------
                               -- Make_Discriminant_Reference --
@@ -9672,6 +9658,8 @@ package body Exp_Dist is
                                  return Nod;
                               end Make_Discriminant_Reference;
 
+                           --  Start of processing for Variant_Part
+
                            begin
                               Append_To (Stmts,
                                 Make_Block_Statement (Loc,
@@ -9681,11 +9669,10 @@ package body Exp_Dist is
                                     Make_Handled_Sequence_Of_Statements (Loc,
                                       Statements => Block_Stmts)));
 
-                              --  Declare the Variant Part aggregate
-                              --  (Union_Any).
-                              --  Knowing the position of this VP in
-                              --  the variant record, we can fetch the
-                              --  VP typecode from Container.
+                              --  Declare variant part aggregate (Union_Any).
+                              --  Knowing the position of this VP in the
+                              --  variant record, we can fetch the VP typecode
+                              --  from Container.
 
                               Append_To (Block_Decls,
                                 Make_Object_Declaration (Loc,
@@ -9706,9 +9693,8 @@ package body Exp_Dist is
                                             Make_Integer_Literal (Loc,
                                               Counter)))))));
 
-                              --  Declare the inner struct aggregate
-                              --  (that will contain the components
-                              --   of this VP)
+                              --  Declare inner struct aggregate (which
+                              --  contains the components of this VP).
 
                               Append_To (Block_Decls,
                                 Make_Object_Declaration (Loc,
@@ -9729,16 +9715,12 @@ package body Exp_Dist is
                                             Make_Integer_Literal (Loc,
                                               Uint_1)))))));
 
-                              --  Construct a case statement that will choose
-                              --  the appropriate code at runtime depending on
-                              --  the discriminant.
+                              --  Build case statement
 
                               Append_To (Block_Stmts,
                                 Make_Case_Statement (Loc,
-                                    Expression =>
-                                      Make_Discriminant_Reference,
-                                    Alternatives =>
-                                      Alt_List));
+                                  Expression   => Make_Discriminant_Reference,
+                                  Alternatives => Alt_List));
 
                               Variant := First_Non_Pragma (Variants (Field));
                               while Present (Variant) loop
@@ -9747,8 +9729,7 @@ package body Exp_Dist is
 
                                  VP_Stmts := New_List;
 
-                                 --  Append discriminant value to union
-                                 --  aggregate.
+                                 --  Append discriminant val to union aggregate
 
                                  Append_To (VP_Stmts,
                                     Make_Procedure_Call_Statement (Loc,
@@ -9757,33 +9738,33 @@ package body Exp_Dist is
                                           RTE (RE_Add_Aggregate_Element), Loc),
                                       Parameter_Associations => New_List (
                                         New_Occurrence_Of (Union_Any, Loc),
-                                          Build_To_Any_Call (
-                                            Make_Discriminant_Reference,
-                                            Block_Decls))));
+                                          Build_To_Any_Call
+                                            (Make_Discriminant_Reference,
+                                             Block_Decls))));
 
                                  --  Populate inner struct aggregate
 
                                  --  Struct_Counter should be reset before
                                  --  handling a variant part. Indeed only one
                                  --  of the case statement alternatives will be
-                                 --  executed at run-time, so the counter must
+                                 --  executed at run time, so the counter must
                                  --  start at 0 for every case statement.
 
                                  Struct_Counter := 0;
 
-                                 TA_Append_Record_Traversal (
-                                   Stmts     => VP_Stmts,
-                                   Clist     => Component_List (Variant),
-                                   Container => Struct_Any,
-                                   Counter   => Struct_Counter);
+                                 TA_Append_Record_Traversal
+                                   (Stmts     => VP_Stmts,
+                                    Clist     => Component_List (Variant),
+                                    Container => Struct_Any,
+                                    Counter   => Struct_Counter);
 
                                  --  Append inner struct to union aggregate
 
                                  Append_To (VP_Stmts,
                                    Make_Procedure_Call_Statement (Loc,
                                      Name =>
-                                       New_Occurrence_Of (
-                                         RTE (RE_Add_Aggregate_Element), Loc),
+                                       New_Occurrence_Of
+                                         (RTE (RE_Add_Aggregate_Element), Loc),
                                      Parameter_Associations => New_List (
                                        New_Occurrence_Of (Union_Any, Loc),
                                        New_Occurrence_Of (Struct_Any, Loc))));
@@ -9793,8 +9774,8 @@ package body Exp_Dist is
                                  Append_To (VP_Stmts,
                                    Make_Procedure_Call_Statement (Loc,
                                      Name =>
-                                       New_Occurrence_Of (
-                                         RTE (RE_Add_Aggregate_Element), Loc),
+                                       New_Occurrence_Of
+                                         (RTE (RE_Add_Aggregate_Element), Loc),
                                        Parameter_Associations => New_List (
                                           New_Occurrence_Of (Container, Loc),
                                           New_Occurrence_Of
@@ -9803,30 +9784,32 @@ package body Exp_Dist is
                                  Append_To (Alt_List,
                                    Make_Case_Statement_Alternative (Loc,
                                      Discrete_Choices => Choice_List,
-                                     Statements => VP_Stmts));
+                                     Statements       => VP_Stmts));
 
                                  Next_Non_Pragma (Variant);
                               end loop;
-                           end;
+                           end Variant_Part;
                         end if;
+
                         Counter := Counter + 1;
                      end TA_Rec_Add_Process_Element;
 
                   begin
                      --  Records are encoded in a TC_STRUCT aggregate:
+
                      --  -- Outer aggregate (TC_STRUCT)
                      --  | [discriminant1]
                      --  | [discriminant2]
                      --  | ...
-                     --
+                     --  |
                      --  | [component1]
                      --  | [component2]
                      --  | ...
-                     --
-                     --  A component can be a common component or a variant
-                     --  part.
-                     --
+
+                     --  A component can be a common component or variant part
+
                      --  A variant part is encoded as a TC_UNION aggregate:
+
                      --  -- Variant Part Aggregate (TC_UNION)
                      --  | [discriminant choice for this Variant Part]
                      --  |
@@ -9835,20 +9818,20 @@ package body Exp_Dist is
                      --  | |  [component2]
                      --  | |  ...
 
-                     --  Let's start by building the outer aggregate
-                     --  First we construct an Elements array containing all
-                     --  the discriminants.
+                     --  Let's start by building the outer aggregate. First we
+                     --  construct Elements array containing all discriminants.
 
                      if Has_Discriminants (Typ) then
                         Disc := First_Discriminant (Typ);
-
                         while Present (Disc) loop
-
                            declare
                               Discriminant : constant Entity_Id :=
-                                 Make_Selected_Component (Loc,
-                                     Prefix        => Expr_Parameter,
-                                     Selector_Name => Chars (Disc));
+                                               Make_Selected_Component (Loc,
+                                                 Prefix        =>
+                                                   Expr_Parameter,
+                                                 Selector_Name =>
+                                                   Chars (Disc));
+
                            begin
                               Set_Etype (Discriminant, Etype (Disc));
 
@@ -9859,6 +9842,7 @@ package body Exp_Dist is
                                   Expression =>
                                     Build_To_Any_Call (Discriminant, Decls)));
                            end;
+
                            Counter := Counter + 1;
                            Next_Discriminant (Disc);
                         end loop;
@@ -9869,8 +9853,7 @@ package body Exp_Dist is
 
                         declare
                            Dummy_Any : constant Entity_Id :=
-                                         Make_Defining_Identifier (Loc,
-                                           Chars => New_Internal_Name ('A'));
+                                         Make_Temporary (Loc, 'A');
 
                         begin
                            Append_To (Decls,
@@ -9897,8 +9880,8 @@ package body Exp_Dist is
 
                      Set_Expression (Any_Decl,
                        Make_Function_Call (Loc,
-                         Name => New_Occurrence_Of (
-                                   RTE (RE_Any_Aggregate_Build), Loc),
+                         Name => New_Occurrence_Of
+                                   (RTE (RE_Any_Aggregate_Build), Loc),
                          Parameter_Associations => New_List (
                            Result_TC,
                            Make_Aggregate (Loc,
@@ -9916,6 +9899,9 @@ package body Exp_Dist is
                end if;
 
             elsif Is_Array_Type (Typ) then
+
+               --  Constrained and unconstrained array types
+
                declare
                   Constrained : constant Boolean := Is_Constrained (Typ);
 
@@ -9935,9 +9921,7 @@ package body Exp_Dist is
                      Counter : Entity_Id;
                      Datum   : Node_Id)
                   is
-                     pragma Warnings (Off);
                      pragma Unreferenced (Counter);
-                     pragma Warnings (On);
 
                      Element_Any : Node_Id;
 
@@ -9961,7 +9945,7 @@ package body Exp_Dist is
                     new Append_Array_Traversal (
                       Subprogram => Fnam,
                       Arry       => Expr_Parameter,
-                      Indices    => New_List,
+                      Indexes    => New_List,
                       Add_Process_Element => TA_Ary_Add_Process_Element);
 
                   Index : Node_Id;
@@ -10001,6 +9985,9 @@ package body Exp_Dist is
                end;
 
             elsif Is_Integer_Type (Typ) or else Is_Unsigned_Type (Typ) then
+
+               --  Integer types
+
                Set_Expression (Any_Decl,
                  Build_To_Any_Call (
                    OK_Convert_To (
@@ -10009,14 +9996,20 @@ package body Exp_Dist is
                    Decls));
 
             else
-               --  Default: type is represented as an opaque sequence of bytes
+               --  Default case, including tagged types: opaque representation
+
+               Use_Opaque_Representation := True;
+            end if;
 
+            if Use_Opaque_Representation then
                declare
-                  Strm : constant Entity_Id := Make_Defining_Identifier (Loc,
-                           New_Internal_Name ('S'));
+                  Strm : constant Entity_Id := Make_Temporary (Loc, 'S');
+                  --  Stream used to store data representation produced by
+                  --  stream attribute.
 
                begin
-                  --  Strm : aliased Buffer_Stream_Type;
+                  --  Generate:
+                  --    Strm : aliased Buffer_Stream_Type;
 
                   Append_To (Decls,
                     Make_Object_Declaration (Loc,
@@ -10027,43 +10020,35 @@ package body Exp_Dist is
                       Object_Definition   =>
                         New_Occurrence_Of (RTE (RE_Buffer_Stream_Type), Loc)));
 
-                  --  Allocate_Buffer (Strm);
-
-                  Append_To (Stms,
-                    Make_Procedure_Call_Statement (Loc,
-                      Name =>
-                        New_Occurrence_Of (RTE (RE_Allocate_Buffer), Loc),
-                      Parameter_Associations => New_List (
-                        New_Occurrence_Of (Strm, Loc))));
-
-                  --  T'Output (Strm'Access, E);
+                  --  Generate:
+                  --    T'Output (Strm'Access, E);
 
                   Append_To (Stms,
                       Make_Attribute_Reference (Loc,
                         Prefix         => New_Occurrence_Of (Typ, Loc),
                         Attribute_Name => Name_Output,
-                        Expressions => New_List (
+                        Expressions    => New_List (
                           Make_Attribute_Reference (Loc,
-                            Prefix => New_Occurrence_Of (Strm, Loc),
+                            Prefix         => New_Occurrence_Of (Strm, Loc),
                             Attribute_Name => Name_Access),
                           New_Occurrence_Of (Expr_Parameter, Loc))));
 
-                  --  BS_To_Any (Strm, A);
+                  --  Generate:
+                  --    BS_To_Any (Strm, A);
 
                   Append_To (Stms,
                     Make_Procedure_Call_Statement (Loc,
-                      Name =>
-                        New_Occurrence_Of (RTE (RE_BS_To_Any), Loc),
+                      Name => New_Occurrence_Of (RTE (RE_BS_To_Any), Loc),
                       Parameter_Associations => New_List (
                         New_Occurrence_Of (Strm, Loc),
                         New_Occurrence_Of (Any, Loc))));
 
-                  --  Release_Buffer (Strm);
+                  --  Generate:
+                  --    Release_Buffer (Strm);
 
                   Append_To (Stms,
                     Make_Procedure_Call_Statement (Loc,
-                      Name =>
-                        New_Occurrence_Of (RTE (RE_Release_Buffer), Loc),
+                      Name => New_Occurrence_Of (RTE (RE_Release_Buffer), Loc),
                       Parameter_Associations => New_List (
                         New_Occurrence_Of (Strm, Loc))));
                end;
@@ -10081,13 +10066,13 @@ package body Exp_Dist is
             end if;
 
             Append_To (Stms,
-              Make_Return_Statement (Loc,
+              Make_Simple_Return_Statement (Loc,
                 Expression => New_Occurrence_Of (Any, Loc)));
 
             Decl :=
               Make_Subprogram_Body (Loc,
-                Specification => Spec,
-                Declarations => Decls,
+                Specification              => Spec,
+                Declarations               => Decls,
                 Handled_Statement_Sequence =>
                   Make_Handled_Sequence_Of_Statements (Loc,
                     Statements => Stms));
@@ -10102,21 +10087,20 @@ package body Exp_Dist is
             Typ   : Entity_Id;
             Decls : List_Id) return Node_Id
          is
-            U_Type : Entity_Id  := Underlying_Type (Typ);
+            U_Type : Entity_Id := Underlying_Type (Typ);
             --  The full view, if Typ is private; the completion,
             --  if Typ is incomplete.
 
-            Fnam    : Entity_Id := Empty;
-            Lib_RE  : RE_Id := RE_Null;
-
-            Expr : Node_Id;
+            Fnam   : Entity_Id := Empty;
+            Lib_RE : RE_Id := RE_Null;
+            Expr   : Node_Id;
 
          begin
             --  Special case System.PolyORB.Interface.Any: its primitives have
             --  not been set yet, so can't call Find_Inherited_TSS.
 
             if Typ = RTE (RE_Any) then
-               Fnam := RTE (RE_TC_Any);
+               Fnam := RTE (RE_TC_A);
 
             else
                --  First simple case where the TypeCode is present
@@ -10125,15 +10109,20 @@ package body Exp_Dist is
                Fnam := Find_Inherited_TSS (U_Type, TSS_TypeCode);
             end if;
 
-            if No (Fnam) then
-               if Sloc (U_Type) <= Standard_Location then
+            --  For the subtype representing a generic actual type, go to the
+            --  actual type.
 
-                  --  Do not try to build alias typecodes for subtypes from
-                  --  Standard.
+            if Is_Generic_Actual_Type (U_Type) then
+               U_Type := Underlying_Type (Base_Type (U_Type));
+            end if;
 
-                  U_Type := Base_Type (U_Type);
-               end if;
+            --  For a standard subtype, go to the base type
+
+            if Sloc (U_Type) <= Standard_Location then
+               U_Type := Base_Type (U_Type);
+            end if;
 
+            if No (Fnam) then
                if U_Type = Standard_Boolean then
                   Lib_RE := RE_TC_B;
 
@@ -10194,9 +10183,14 @@ package body Exp_Dist is
                elsif U_Type = RTE (RE_Long_Long_Unsigned) then
                   Lib_RE := RE_TC_LLU;
 
-               elsif U_Type = Standard_String then
+               elsif Is_RTE (U_Type, RE_Unbounded_String) then
                   Lib_RE := RE_TC_String;
 
+               --  Special DSA types
+
+               elsif Is_RTE (U_Type, RE_Any_Container_Ptr) then
+                  Lib_RE := RE_TC_A;
+
                --  Other (non-primitive) types
 
                else
@@ -10240,8 +10234,7 @@ package body Exp_Dist is
             Stms  : constant List_Id := New_List;
 
             TCNam : constant Entity_Id :=
-                      Make_Stream_Procedure_Function_Name (Loc,
-                        Typ, Name_uTypeCode);
+                      Make_Helper_Function_Name (Loc, Typ, Name_TypeCode);
 
             Parameters : List_Id;
 
@@ -10290,8 +10283,7 @@ package body Exp_Dist is
             begin
                Append_To (Parameter_List,
                  Make_Function_Call (Loc,
-                   Name =>
-                     New_Occurrence_Of (RTE (RE_TA_String), Loc),
+                   Name => New_Occurrence_Of (RTE (RE_TA_Std_String), Loc),
                    Parameter_Associations => New_List (
                      Make_String_Literal (Loc, S))));
             end Add_String_Parameter;
@@ -10307,10 +10299,8 @@ package body Exp_Dist is
             begin
                Append_To (Parameter_List,
                  Make_Function_Call (Loc,
-                   Name =>
-                     New_Occurrence_Of (RTE (RE_TA_TC), Loc),
-                   Parameter_Associations => New_List (
-                     TC_Node)));
+                   Name => New_Occurrence_Of (RTE (RE_TA_TC), Loc),
+                   Parameter_Associations => New_List (TC_Node)));
             end Add_TypeCode_Parameter;
 
             ------------------------
@@ -10324,8 +10314,7 @@ package body Exp_Dist is
             begin
                Append_To (Parameter_List,
                  Make_Function_Call (Loc,
-                   Name =>
-                     New_Occurrence_Of (RTE (RE_TA_LI), Loc),
+                   Name => New_Occurrence_Of (RTE (RE_TA_LI), Loc),
                    Parameter_Associations => New_List (Expr_Node)));
             end Add_Long_Parameter;
 
@@ -10384,9 +10373,9 @@ package body Exp_Dist is
             procedure Return_Constructed_TypeCode (Kind : Entity_Id) is
             begin
                Append_To (Stms,
-                 Make_Return_Statement (Loc,
+                 Make_Simple_Return_Statement (Loc,
                    Expression =>
-                      Make_Constructed_TypeCode (Kind, Parameters)));
+                     Make_Constructed_TypeCode (Kind, Parameters)));
             end Return_Constructed_TypeCode;
 
             ------------------
@@ -10416,17 +10405,15 @@ package body Exp_Dist is
                Rec     : Entity_Id;
                Field   : Node_Id)
             is
-               pragma Warnings (Off);
                pragma Unreferenced (Any, Counter, Rec);
-               pragma Warnings (On);
 
             begin
                if Nkind (Field) = N_Defining_Identifier then
 
                   --  A regular component
 
-                  Add_TypeCode_Parameter (
-                    Build_TypeCode_Call (Loc, Etype (Field), Decls), Params);
+                  Add_TypeCode_Parameter
+                    (Build_TypeCode_Call (Loc, Etype (Field), Decls), Params);
                   Get_Name_String (Chars (Field));
                   Add_String_Parameter (String_From_Name_Buffer, Params);
 
@@ -10463,7 +10450,7 @@ package body Exp_Dist is
                      --  to the union parameter list.
 
                      --  Ordering of declarations is a complete mess in this
-                     --  area, it is supposed to be types/varibles, then
+                     --  area, it is supposed to be types/variables, then
                      --  subprogram specs, then subprogram bodies ???
 
                      ---------------------------------------
@@ -10549,13 +10536,7 @@ package body Exp_Dist is
                                             Make_Integer_Literal (Loc, J);
                                        end if;
                                        Append_To (Union_TC_Params,
-                                         Make_Function_Call (Loc,
-                                           Name => New_Occurrence_Of
-                                             (RTE (RE_TA_A), Loc),
-                                           Parameter_Associations =>
-                                             New_List (
-                                               Build_To_Any_Call
-                                                 (Expr, Decls))));
+                                         Build_To_Any_Call (Expr, Decls));
 
                                        Add_Params_For_Variant_Components;
                                        J := J + Uint_1;
@@ -10572,7 +10553,7 @@ package body Exp_Dist is
 
                                  declare
                                     Default_Node : constant Node_Id :=
-                                      Pick (Union_TC_Params, 4);
+                                                     Pick (Union_TC_Params, 4);
 
                                     New_Default_Node : constant Node_Id :=
                                       Make_Function_Call (Loc,
@@ -10593,8 +10574,7 @@ package body Exp_Dist is
 
                                  --  Add a placeholder member label
                                  --  for the default case.
-                                 --  It must be of the discriminant
-                                 --  type.
+                                 --  It must be of the discriminant type.
 
                                  declare
                                     Exp : constant Node_Id :=
@@ -10605,42 +10585,32 @@ package body Exp_Dist is
                                  begin
                                     Set_Etype (Exp, Discriminant_Type);
                                     Append_To (Union_TC_Params,
-                                      Make_Function_Call (Loc,
-                                        Name => New_Occurrence_Of
-                                          (RTE (RE_TA_A), Loc),
-                                        Parameter_Associations =>
-                                          New_List (
-                                            Build_To_Any_Call
-                                              (Exp, Decls))));
+                                      Build_To_Any_Call (Exp, Decls));
                                  end;
 
                                  Add_Params_For_Variant_Components;
 
                               when others =>
+
+                                 --  Case of an explicit choice
+
                                  declare
                                     Exp : constant Node_Id :=
-                                      New_Copy_Tree (Choice);
+                                            New_Copy_Tree (Choice);
                                  begin
                                     Append_To (Union_TC_Params,
-                                      Make_Function_Call (Loc,
-                                        Name => New_Occurrence_Of
-                                          (RTE (RE_TA_A), Loc),
-                                        Parameter_Associations =>
-                                          New_List (
-                                            Build_To_Any_Call
-                                              (Exp, Decls))));
+                                      Build_To_Any_Call (Exp, Decls));
                                  end;
 
                                  Add_Params_For_Variant_Components;
                            end case;
+
                            Next (Choice);
                            Choice_Index := Choice_Index + 1;
-
                         end loop;
 
                         Next_Non_Pragma (Variant);
                      end loop;
-
                   end;
                end if;
             end TC_Rec_Add_Process_Element;
@@ -10648,13 +10618,20 @@ package body Exp_Dist is
             Type_Name_Str    : String_Id;
             Type_Repo_Id_Str : String_Id;
 
+         --  Start of processing for Build_TypeCode_Function
+
          begin
-            if Is_Itype (Typ) then
+            --  For a derived type, we can't go past the base type (to the
+            --  parent type) here, because that would cause the attribute's
+            --  formal parameter to have the wrong type; hence the Base_Type
+            --  check here.
+
+            if Is_Itype (Typ) and then Typ /= Base_Type (Typ) then
                Build_TypeCode_Function
                   (Loc  => Loc,
-                  Typ  => Etype (Typ),
-                  Decl => Decl,
-                  Fnam => Fnam);
+                   Typ  => Etype (Typ),
+                   Decl => Decl,
+                   Fnam => Fnam);
                return;
             end if;
 
@@ -10662,32 +10639,40 @@ package body Exp_Dist is
 
             Spec :=
               Make_Function_Specification (Loc,
-                Defining_Unit_Name => Fnam,
+                Defining_Unit_Name       => Fnam,
                 Parameter_Specifications => Empty_List,
-                Result_Definition =>
+                Result_Definition        =>
                   New_Occurrence_Of (RTE (RE_TypeCode), Loc));
 
             Build_Name_And_Repository_Id (Typ,
               Name_Str => Type_Name_Str, Repo_Id_Str => Type_Repo_Id_Str);
+
             Initialize_Parameter_List
               (Type_Name_Str, Type_Repo_Id_Str, Parameters);
 
-            if Is_Derived_Type (Typ)
-              and then not Is_Tagged_Type (Typ)
+            if Has_Stream_Attribute_Definition
+                 (Typ, TSS_Stream_Output, At_Any_Place => True)
+              or else
+               Has_Stream_Attribute_Definition
+                 (Typ, TSS_Stream_Write, At_Any_Place => True)
             then
+               --  If user-defined stream attributes are specified for this
+               --  type, use them and transmit data as an opaque sequence of
+               --  stream elements.
+
+               Return_Alias_TypeCode
+                 (New_Occurrence_Of (RTE (RE_TC_Opaque), Loc));
+
+            elsif Is_Derived_Type (Typ) and then not Is_Tagged_Type (Typ) then
                Return_Alias_TypeCode (
                  Build_TypeCode_Call (Loc, Etype (Typ), Decls));
 
-            elsif Is_Integer_Type (Typ)
-              or else Is_Unsigned_Type (Typ)
-            then
+            elsif Is_Integer_Type (Typ) or else Is_Unsigned_Type (Typ) then
                Return_Alias_TypeCode (
                  Build_TypeCode_Call (Loc,
                    Find_Numeric_Representation (Typ), Decls));
 
-            elsif Is_Record_Type (Typ)
-              and then not Is_Tagged_Type (Typ)
-            then
+            elsif Is_Record_Type (Typ) and then not Is_Tagged_Type (Typ) then
 
                --  Record typecodes are encoded as follows:
                --  -- TC_STRUCT
@@ -10732,20 +10717,23 @@ package body Exp_Dist is
                --  |  [VP Name]
 
                if Nkind (Declaration_Node (Typ)) = N_Subtype_Declaration then
-                  Return_Alias_TypeCode (
-                    Build_TypeCode_Call (Loc, Etype (Typ), Decls));
+                  Return_Alias_TypeCode
+                    (Build_TypeCode_Call (Loc, Etype (Typ), Decls));
+
                else
                   declare
                      Disc : Entity_Id := Empty;
                      Rdef : constant Node_Id :=
-                       Type_Definition (Declaration_Node (Typ));
+                              Type_Definition (Declaration_Node (Typ));
                      Dummy_Counter : Int := 0;
+
                   begin
                      --  Construct the discriminants typecodes
 
                      if Has_Discriminants (Typ) then
                         Disc := First_Discriminant (Typ);
                      end if;
+
                      while Present (Disc) loop
                         Add_TypeCode_Parameter (
                           Build_TypeCode_Call (Loc, Etype (Disc), Decls),
@@ -10774,9 +10762,8 @@ package body Exp_Dist is
                   Indx           : Node_Id          := First_Index (Typ);
 
                begin
-                  Inner_TypeCode := Build_TypeCode_Call (Loc,
-                    Component_Type (Typ),
-                    Decls);
+                  Inner_TypeCode :=
+                    Build_TypeCode_Call (Loc, Component_Type (Typ), Decls);
 
                   for J in 1 .. Ndim loop
                      if Constrained then
@@ -10785,13 +10772,11 @@ package body Exp_Dist is
                             Build_To_Any_Call (
                               OK_Convert_To (RTE (RE_Long_Unsigned),
                                 Make_Attribute_Reference (Loc,
-                                  Prefix =>
-                                    New_Occurrence_Of (Typ, Loc),
-                                  Attribute_Name =>
-                                    Name_Length,
+                                  Prefix => New_Occurrence_Of (Typ, Loc),
+                                  Attribute_Name => Name_Length,
                                   Expressions => New_List (
                                     Make_Integer_Literal (Loc,
-                                      Ndim - J + 1)))),
+                                      Intval => Ndim - J + 1)))),
                               Decls),
                             Build_To_Any_Call (Inner_TypeCode, Decls)));
 
@@ -10838,8 +10823,8 @@ package body Exp_Dist is
 
             Decl :=
               Make_Subprogram_Body (Loc,
-                Specification => Spec,
-                Declarations => Decls,
+                Specification              => Spec,
+                Declarations               => Decls,
                 Handled_Statement_Sequence =>
                   Make_Handled_Sequence_Of_Statements (Loc,
                     Statements => Stms));
@@ -10942,8 +10927,7 @@ package body Exp_Dist is
                   Element_Expr : constant Node_Id :=
                                    Make_Indexed_Component (Loc,
                                      New_Occurrence_Of (Arry, Loc),
-                                     Indices);
-
+                                     Indexes);
                begin
                   Set_Etype (Element_Expr, Component_Type (Typ));
                   Add_Process_Element (Stmts,
@@ -10955,7 +10939,7 @@ package body Exp_Dist is
                return;
             end if;
 
-            Append_To (Indices,
+            Append_To (Indexes,
               Make_Identifier (Loc, New_External_Name ('L', Depth)));
 
             if not Constrained or else Depth > 1 then
@@ -10975,8 +10959,8 @@ package body Exp_Dist is
 
             declare
                Loop_Any : Node_Id := Inner_Any;
-            begin
 
+            begin
                --  For the first dimension of a constrained array, we add
                --  elements directly in the corresponding Any; there is no
                --  intervening inner Any.
@@ -11026,10 +11010,10 @@ package body Exp_Dist is
                   if Constrained then
                      Inner_Any_TypeCode_Expr :=
                        Make_Function_Call (Loc,
-                         Name =>
-                           New_Occurrence_Of (RTE (RE_Get_TC), Loc),
+                         Name => New_Occurrence_Of (RTE (RE_Get_TC), Loc),
                          Parameter_Associations => New_List (
                            New_Occurrence_Of (Any, Loc)));
+
                   else
                      Inner_Any_TypeCode_Expr :=
                        Make_Function_Call (Loc,
@@ -11039,14 +11023,14 @@ package body Exp_Dist is
                                New_Occurrence_Of (Any, Loc),
                                Make_Integer_Literal (Loc, Ndim)));
                   end if;
+
                else
                   Inner_Any_TypeCode_Expr :=
                     Make_Function_Call (Loc,
-                      Name =>
-                        New_Occurrence_Of (RTE (RE_Content_Type), Loc),
+                      Name => New_Occurrence_Of (RTE (RE_Content_Type), Loc),
                       Parameter_Associations => New_List (
                         Make_Identifier (Loc,
-                          New_External_Name ('T', Depth - 1))));
+                          Chars => New_External_Name ('T', Depth - 1))));
                end if;
 
                Append_To (Decls,
@@ -11120,29 +11104,43 @@ package body Exp_Dist is
             end;
          end Append_Array_Traversal;
 
-         -----------------------------------------
-         -- Make_Stream_Procedure_Function_Name --
-         -----------------------------------------
+         -------------------------------
+         -- Make_Helper_Function_Name --
+         -------------------------------
 
-         function Make_Stream_Procedure_Function_Name
+         function Make_Helper_Function_Name
            (Loc : Source_Ptr;
             Typ : Entity_Id;
             Nam : Name_Id) return Entity_Id
          is
          begin
-            --  For tagged types, we use a canonical name so that it matches
-            --  the primitive spec. For all other cases, we use a serialized
-            --  name so that multiple generations of the same procedure do not
-            --  clash.
+            declare
+               Serial : Nat := 0;
+               --  For tagged types that aren't frozen yet, generate the helper
+               --  under its canonical name so that it matches the primitive
+               --  spec. For all other cases, we use a serialized name so that
+               --  multiple generations of the same procedure do not clash.
 
-            if Is_Tagged_Type (Typ) then
-               return Make_Defining_Identifier (Loc, Nam);
-            else
-               return Make_Defining_Identifier (Loc,
+            begin
+               if Is_Tagged_Type (Typ) and then not Is_Frozen (Typ) then
+                  null;
+               else
+                  Serial := Increment_Serial_Number;
+               end if;
+
+               --  Use prefixed underscore to avoid potential clash with user
+               --  identifier (we use attribute names for Nam).
+
+               return
+                 Make_Defining_Identifier (Loc,
                    Chars =>
-                     New_External_Name (Nam, ' ', Increment_Serial_Number));
-            end if;
-         end Make_Stream_Procedure_Function_Name;
+                     New_External_Name
+                       (Related_Id   => Nam,
+                        Suffix       => ' ',
+                        Suffix_Index => Serial,
+                        Prefix       => '_'));
+            end;
+         end Make_Helper_Function_Name;
       end Helpers;
 
       -----------------------------------
@@ -11187,10 +11185,11 @@ package body Exp_Dist is
       Pkg_Name := String_From_Name_Buffer;
       Inst :=
         Make_Package_Instantiation (Loc,
-          Defining_Unit_Name   =>
-            Make_Defining_Identifier (Loc, New_Internal_Name ('R')),
+          Defining_Unit_Name   => Make_Temporary (Loc, 'R'),
+
           Name                 =>
             New_Occurrence_Of (RTE (RE_RCI_Locator), Loc),
+
           Generic_Associations => New_List (
             Make_Generic_Association (Loc,
               Selector_Name                     =>
@@ -11198,6 +11197,7 @@ package body Exp_Dist is
               Explicit_Generic_Actual_Parameter =>
                 Make_String_Literal (Loc,
                   Strval => Pkg_Name)),
+
             Make_Generic_Association (Loc,
               Selector_Name                     =>
                 Make_Identifier (Loc, Name_Version),
@@ -11208,8 +11208,9 @@ package body Exp_Dist is
                   Attribute_Name =>
                     Name_Version))));
 
-      RCI_Locator_Table.Set (Defining_Unit_Name (Package_Spec),
-        Defining_Unit_Name (Inst));
+      RCI_Locator_Table.Set
+        (Defining_Unit_Name (Package_Spec),
+         Defining_Unit_Name (Inst));
       return Inst;
    end RCI_Package_Locator;
 
@@ -11222,12 +11223,36 @@ package body Exp_Dist is
    is
       Stub_Elements : constant Stub_Structure :=
                         Stubs_Table.Get (Full_View);
+
    begin
+      --  For an RACW encountered before the freeze point of its designated
+      --  type, the stub type is generated at the point of the RACW declaration
+      --  but the primitives are generated only once the designated type is
+      --  frozen. That freeze can occur in another scope, for example when the
+      --  RACW is declared in a nested package. In that case we need to
+      --  reestablish the stub type's scope prior to generating its primitive
+      --  operations.
+
       if Stub_Elements /= Empty_Stub_Structure then
-         Add_RACW_Primitive_Declarations_And_Bodies
-           (Full_View,
-            Stub_Elements.RPC_Receiver_Decl,
-            Stub_Elements.Body_Decls);
+         declare
+            Saved_Scope : constant Entity_Id := Current_Scope;
+            Stubs_Scope : constant Entity_Id :=
+                            Scope (Stub_Elements.Stub_Type);
+
+         begin
+            if Current_Scope /= Stubs_Scope then
+               Push_Scope (Stubs_Scope);
+            end if;
+
+            Add_RACW_Primitive_Declarations_And_Bodies
+              (Full_View,
+               Stub_Elements.RPC_Receiver_Decl,
+               Stub_Elements.Body_Decls);
+
+            if Current_Scope /= Saved_Scope then
+               Pop_Scope;
+            end if;
+         end;
       end if;
    end Remote_Types_Tagged_Full_View_Encountered;
 
@@ -11290,15 +11315,16 @@ package body Exp_Dist is
      (Loc           : Source_Ptr;
       Decls         : List_Id;
       RPC_Receiver  : Entity_Id;
-      Stub_Elements : Stub_Structure) is
+      Stub_Elements : Stub_Structure)
+   is
    begin
       case Get_PCS_Name is
          when Name_PolyORB_DSA =>
-            PolyORB_Support.Add_Obj_RPC_Receiver_Completion (Loc,
-              Decls, RPC_Receiver, Stub_Elements);
+            PolyORB_Support.Add_Obj_RPC_Receiver_Completion
+              (Loc, Decls, RPC_Receiver, Stub_Elements);
          when others =>
-            GARLIC_Support.Add_Obj_RPC_Receiver_Completion (Loc,
-              Decls, RPC_Receiver, Stub_Elements);
+            GARLIC_Support.Add_Obj_RPC_Receiver_Completion
+              (Loc, Decls, RPC_Receiver, Stub_Elements);
       end case;
    end Specific_Add_Obj_RPC_Receiver_Completion;
 
@@ -11312,25 +11338,26 @@ package body Exp_Dist is
       Stub_Type         : Entity_Id;
       Stub_Type_Access  : Entity_Id;
       RPC_Receiver_Decl : Node_Id;
-      Body_Decls        : List_Id) is
+      Body_Decls        : List_Id)
+   is
    begin
       case Get_PCS_Name is
          when Name_PolyORB_DSA =>
-            PolyORB_Support.Add_RACW_Features (
-              RACW_Type,
-              Desig,
-              Stub_Type,
-              Stub_Type_Access,
-              RPC_Receiver_Decl,
-              Body_Decls);
+            PolyORB_Support.Add_RACW_Features
+              (RACW_Type,
+               Desig,
+               Stub_Type,
+               Stub_Type_Access,
+               RPC_Receiver_Decl,
+               Body_Decls);
 
          when others =>
-            GARLIC_Support.Add_RACW_Features (
-              RACW_Type,
-              Stub_Type,
-              Stub_Type_Access,
-              RPC_Receiver_Decl,
-              Body_Decls);
+            GARLIC_Support.Add_RACW_Features
+              (RACW_Type,
+               Stub_Type,
+               Stub_Type_Access,
+               RPC_Receiver_Decl,
+               Body_Decls);
       end case;
    end Specific_Add_RACW_Features;
 
@@ -11340,7 +11367,8 @@ package body Exp_Dist is
 
    procedure Specific_Add_RAST_Features
      (Vis_Decl : Node_Id;
-      RAS_Type : Entity_Id) is
+      RAS_Type : Entity_Id)
+   is
    begin
       case Get_PCS_Name is
          when Name_PolyORB_DSA =>
@@ -11362,11 +11390,11 @@ package body Exp_Dist is
    begin
       case Get_PCS_Name is
          when Name_PolyORB_DSA =>
-            PolyORB_Support.Add_Receiving_Stubs_To_Declarations (
-              Pkg_Spec, Decls, Stmts);
+            PolyORB_Support.Add_Receiving_Stubs_To_Declarations
+              (Pkg_Spec, Decls, Stmts);
          when others =>
-            GARLIC_Support.Add_Receiving_Stubs_To_Declarations (
-              Pkg_Spec, Decls, Stmts);
+            GARLIC_Support.Add_Receiving_Stubs_To_Declarations
+              (Pkg_Spec, Decls, Stmts);
       end case;
    end Specific_Add_Receiving_Stubs_To_Declarations;
 
@@ -11391,34 +11419,35 @@ package body Exp_Dist is
    begin
       case Get_PCS_Name is
          when Name_PolyORB_DSA =>
-            PolyORB_Support.Build_General_Calling_Stubs (
-              Decls,
-              Statements,
-              Target.Object,
-              Subprogram_Id,
-              Asynchronous,
-              Is_Known_Asynchronous,
-              Is_Known_Non_Asynchronous,
-              Is_Function,
-              Spec,
-              Stub_Type,
-              RACW_Type,
-              Nod);
+            PolyORB_Support.Build_General_Calling_Stubs
+              (Decls,
+               Statements,
+               Target.Object,
+               Subprogram_Id,
+               Asynchronous,
+               Is_Known_Asynchronous,
+               Is_Known_Non_Asynchronous,
+               Is_Function,
+               Spec,
+               Stub_Type,
+               RACW_Type,
+               Nod);
+
          when others =>
-            GARLIC_Support.Build_General_Calling_Stubs (
-              Decls,
-              Statements,
-              Target.Partition,
-              Target.RPC_Receiver,
-              Subprogram_Id,
-              Asynchronous,
-              Is_Known_Asynchronous,
-              Is_Known_Non_Asynchronous,
-              Is_Function,
-              Spec,
-              Stub_Type,
-              RACW_Type,
-              Nod);
+            GARLIC_Support.Build_General_Calling_Stubs
+              (Decls,
+               Statements,
+               Target.Partition,
+               Target.RPC_Receiver,
+               Subprogram_Id,
+               Asynchronous,
+               Is_Known_Asynchronous,
+               Is_Known_Non_Asynchronous,
+               Is_Function,
+               Spec,
+               Stub_Type,
+               RACW_Type,
+               Nod);
       end case;
    end Specific_Build_General_Calling_Stubs;
 
@@ -11444,6 +11473,7 @@ package body Exp_Dist is
                Subp_Index,
                Stmts,
                Decl);
+
          when others =>
             GARLIC_Support.Build_RPC_Receiver_Body
               (RPC_Receiver,
@@ -11468,11 +11498,14 @@ package body Exp_Dist is
    begin
       case Get_PCS_Name is
          when Name_PolyORB_DSA =>
-            return PolyORB_Support.Build_Stub_Target (Loc,
-                     Decls, RCI_Locator, Controlling_Parameter);
+            return
+              PolyORB_Support.Build_Stub_Target
+                (Loc, Decls, RCI_Locator, Controlling_Parameter);
+
          when others =>
-            return GARLIC_Support.Build_Stub_Target (Loc,
-                     Decls, RCI_Locator, Controlling_Parameter);
+            return
+              GARLIC_Support.Build_Stub_Target
+                (Loc, Decls, RCI_Locator, Controlling_Parameter);
       end case;
    end Specific_Build_Stub_Target;
 
@@ -11482,23 +11515,25 @@ package body Exp_Dist is
 
    procedure Specific_Build_Stub_Type
      (RACW_Type         : Entity_Id;
-      Stub_Type         : Entity_Id;
-      Stub_Type_Decl    : out Node_Id;
+      Stub_Type_Comps   : out List_Id;
       RPC_Receiver_Decl : out Node_Id)
    is
    begin
       case Get_PCS_Name is
          when Name_PolyORB_DSA =>
-            PolyORB_Support.Build_Stub_Type (
-              RACW_Type, Stub_Type,
-              Stub_Type_Decl, RPC_Receiver_Decl);
+            PolyORB_Support.Build_Stub_Type
+              (RACW_Type, Stub_Type_Comps, RPC_Receiver_Decl);
+
          when others =>
-            GARLIC_Support.Build_Stub_Type (
-              RACW_Type, Stub_Type,
-              Stub_Type_Decl, RPC_Receiver_Decl);
+            GARLIC_Support.Build_Stub_Type
+              (RACW_Type, Stub_Type_Comps, RPC_Receiver_Decl);
       end case;
    end Specific_Build_Stub_Type;
 
+   -----------------------------------------------
+   -- Specific_Build_Subprogram_Receiving_Stubs --
+   -----------------------------------------------
+
    function Specific_Build_Subprogram_Receiving_Stubs
      (Vis_Decl                 : Node_Id;
       Asynchronous             : Boolean;
@@ -11510,24 +11545,38 @@ package body Exp_Dist is
    begin
       case Get_PCS_Name is
          when Name_PolyORB_DSA =>
-            return PolyORB_Support.Build_Subprogram_Receiving_Stubs (
-                     Vis_Decl,
-                     Asynchronous,
-                     Dynamically_Asynchronous,
-                     Stub_Type,
-                     RACW_Type,
-                     Parent_Primitive);
+            return
+              PolyORB_Support.Build_Subprogram_Receiving_Stubs
+                (Vis_Decl,
+                 Asynchronous,
+                 Dynamically_Asynchronous,
+                 Stub_Type,
+                 RACW_Type,
+                 Parent_Primitive);
+
          when others =>
-            return GARLIC_Support.Build_Subprogram_Receiving_Stubs (
-                     Vis_Decl,
-                     Asynchronous,
-                     Dynamically_Asynchronous,
-                     Stub_Type,
-                     RACW_Type,
-                     Parent_Primitive);
+            return
+              GARLIC_Support.Build_Subprogram_Receiving_Stubs
+                (Vis_Decl,
+                 Asynchronous,
+                 Dynamically_Asynchronous,
+                 Stub_Type,
+                 RACW_Type,
+                 Parent_Primitive);
       end case;
    end Specific_Build_Subprogram_Receiving_Stubs;
 
+   -------------------------------
+   -- Transmit_As_Unconstrained --
+   -------------------------------
+
+   function Transmit_As_Unconstrained (Typ : Entity_Id) return Boolean is
+   begin
+      return
+        not (Is_Elementary_Type (Typ) or else Is_Constrained (Typ))
+          or else (Is_Access_Type (Typ) and then Can_Never_Be_Null (Typ));
+   end Transmit_As_Unconstrained;
+
    --------------------------
    -- Underlying_RACW_Type --
    --------------------------
@@ -11544,10 +11593,12 @@ package body Exp_Dist is
       end if;
 
       return
-        Etype (Subtype_Indication (
-          Component_Definition (
-           First (Component_Items (Component_List (
-            Type_Definition (Declaration_Node (Record_Type))))))));
+        Etype (Subtype_Indication
+                (Component_Definition
+                  (First (Component_Items
+                           (Component_List
+                             (Type_Definition
+                               (Declaration_Node (Record_Type))))))));
    end Underlying_RACW_Type;
 
 end Exp_Dist;