OSDN Git Service

2009-04-16 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_dist.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              E X P_ D I S T                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Einfo;    use Einfo;
28 with Elists;   use Elists;
29 with Exp_Atag; use Exp_Atag;
30 with Exp_Strm; use Exp_Strm;
31 with Exp_Tss;  use Exp_Tss;
32 with Exp_Util; use Exp_Util;
33 with Lib;      use Lib;
34 with Nlists;   use Nlists;
35 with Nmake;    use Nmake;
36 with Opt;      use Opt;
37 with Rtsfind;  use Rtsfind;
38 with Sem;      use Sem;
39 with Sem_Aux;  use Sem_Aux;
40 with Sem_Cat;  use Sem_Cat;
41 with Sem_Ch3;  use Sem_Ch3;
42 with Sem_Ch8;  use Sem_Ch8;
43 with Sem_Dist; use Sem_Dist;
44 with Sem_Eval; use Sem_Eval;
45 with Sem_Util; use Sem_Util;
46 with Sinfo;    use Sinfo;
47 with Stand;    use Stand;
48 with Stringt;  use Stringt;
49 with Tbuild;   use Tbuild;
50 with Ttypes;   use Ttypes;
51 with Uintp;    use Uintp;
52
53 with GNAT.HTable; use GNAT.HTable;
54
55 package body Exp_Dist is
56
57    --  The following model has been used to implement distributed objects:
58    --  given a designated type D and a RACW type R, then a record of the
59    --  form:
60
61    --    type Stub is tagged record
62    --       [...declaration similar to s-parint.ads RACW_Stub_Type...]
63    --    end record;
64
65    --  is built. This type has two properties:
66
67    --    1) Since it has the same structure than RACW_Stub_Type, it can be
68    --       converted to and from this type to make it suitable for
69    --       System.Partition_Interface.Get_Unique_Remote_Pointer in order
70    --       to avoid memory leaks when the same remote object arrive on the
71    --       same partition through several paths;
72
73    --    2) It also has the same dispatching table as the designated type D,
74    --       and thus can be used as an object designated by a value of type
75    --       R on any partition other than the one on which the object has
76    --       been created, since only dispatching calls will be performed and
77    --       the fields themselves will not be used. We call Derive_Subprograms
78    --       to fake half a derivation to ensure that the subprograms do have
79    --       the same dispatching table.
80
81    First_RCI_Subprogram_Id : constant := 2;
82    --  RCI subprograms are numbered starting at 2. The RCI receiver for
83    --  an RCI package can thus identify calls received through remote
84    --  access-to-subprogram dereferences by the fact that they have a
85    --  (primitive) subprogram id of 0, and 1 is used for the internal
86    --  RAS information lookup operation. (This is for the Garlic code
87    --  generation, where subprograms are identified by numbers; in the
88    --  PolyORB version, they are identified by name, with a numeric suffix
89    --  for homonyms.)
90
91    type Hash_Index is range 0 .. 50;
92
93    -----------------------
94    -- Local subprograms --
95    -----------------------
96
97    function Hash (F : Entity_Id) return Hash_Index;
98    --  DSA expansion associates stubs to distributed object types using
99    --  a hash table on entity ids.
100
101    function Hash (F : Name_Id) return Hash_Index;
102    --  The generation of subprogram identifiers requires an overload counter
103    --  to be associated with each remote subprogram names. These counters
104    --  are maintained in a hash table on name ids.
105
106    type Subprogram_Identifiers is record
107       Str_Identifier : String_Id;
108       Int_Identifier : Int;
109    end record;
110
111    package Subprogram_Identifier_Table is
112       new Simple_HTable (Header_Num => Hash_Index,
113                          Element    => Subprogram_Identifiers,
114                          No_Element => (No_String, 0),
115                          Key        => Entity_Id,
116                          Hash       => Hash,
117                          Equal      => "=");
118    --  Mapping between a remote subprogram and the corresponding
119    --  subprogram identifiers.
120
121    package Overload_Counter_Table is
122       new Simple_HTable (Header_Num => Hash_Index,
123                          Element    => Int,
124                          No_Element => 0,
125                          Key        => Name_Id,
126                          Hash       => Hash,
127                          Equal      => "=");
128    --  Mapping between a subprogram name and an integer that
129    --  counts the number of defining subprogram names with that
130    --  Name_Id encountered so far in a given context (an interface).
131
132    function Get_Subprogram_Ids (Def : Entity_Id) return Subprogram_Identifiers;
133    function Get_Subprogram_Id  (Def : Entity_Id) return String_Id;
134    function Get_Subprogram_Id  (Def : Entity_Id) return Int;
135    --  Given a subprogram defined in a RCI package, get its distribution
136    --  subprogram identifiers (the distribution identifiers are a unique
137    --  subprogram number, and the non-qualified subprogram name, in the
138    --  casing used for the subprogram declaration; if the name is overloaded,
139    --  a double underscore and a serial number are appended.
140    --
141    --  The integer identifier is used to perform remote calls with GARLIC;
142    --  the string identifier is used in the case of PolyORB.
143    --
144    --  Although the PolyORB DSA receiving stubs will make a caseless comparison
145    --  when receiving a call, the calling stubs will create requests with the
146    --  exact casing of the defining unit name of the called subprogram, so as
147    --  to allow calls to subprograms on distributed nodes that do distinguish
148    --  between casings.
149    --
150    --  NOTE: Another design would be to allow a representation clause on
151    --  subprogram specs: for Subp'Distribution_Identifier use "fooBar";
152
153    pragma Warnings (Off, Get_Subprogram_Id);
154    --  One homonym only is unreferenced (specific to the GARLIC version)
155
156    procedure Add_RAS_Dereference_TSS (N : Node_Id);
157    --  Add a subprogram body for RAS Dereference TSS
158
159    procedure Add_RAS_Proxy_And_Analyze
160      (Decls              : List_Id;
161       Vis_Decl           : Node_Id;
162       All_Calls_Remote_E : Entity_Id;
163       Proxy_Object_Addr  : out Entity_Id);
164    --  Add the proxy type required, on the receiving (server) side, to handle
165    --  calls to the subprogram declared by Vis_Decl through a remote access
166    --  to subprogram type. All_Calls_Remote_E must be Standard_True if a pragma
167    --  All_Calls_Remote applies, Standard_False otherwise. The new proxy type
168    --  is appended to Decls. Proxy_Object_Addr is a constant of type
169    --  System.Address that designates an instance of the proxy object.
170
171    function Build_Remote_Subprogram_Proxy_Type
172      (Loc            : Source_Ptr;
173       ACR_Expression : Node_Id) return Node_Id;
174    --  Build and return a tagged record type definition for an RCI
175    --  subprogram proxy type.
176    --  ACR_Expression is use as the initialization value for
177    --  the All_Calls_Remote component.
178
179    function Build_Get_Unique_RP_Call
180      (Loc       : Source_Ptr;
181       Pointer   : Entity_Id;
182       Stub_Type : Entity_Id) return List_Id;
183    --  Build a call to Get_Unique_Remote_Pointer (Pointer), followed by a
184    --  tag fixup (Get_Unique_Remote_Pointer may have changed Pointer'Tag to
185    --  RACW_Stub_Type'Tag, while the desired tag is that of Stub_Type).
186
187    function Build_Stub_Tag
188      (Loc       : Source_Ptr;
189       RACW_Type : Entity_Id) return Node_Id;
190    --  Return an expression denoting the tag of the stub type associated with
191    --  RACW_Type.
192
193    function Build_Subprogram_Calling_Stubs
194      (Vis_Decl                 : Node_Id;
195       Subp_Id                  : Node_Id;
196       Asynchronous             : Boolean;
197       Dynamically_Asynchronous : Boolean   := False;
198       Stub_Type                : Entity_Id := Empty;
199       RACW_Type                : Entity_Id := Empty;
200       Locator                  : Entity_Id := Empty;
201       New_Name                 : Name_Id   := No_Name) return Node_Id;
202    --  Build the calling stub for a given subprogram with the subprogram ID
203    --  being Subp_Id. If Stub_Type is given, then the "addr" field of
204    --  parameters of this type will be marshalled instead of the object
205    --  itself. It will then be converted into Stub_Type before performing
206    --  the real call. If Dynamically_Asynchronous is True, then it will be
207    --  computed at run time whether the call is asynchronous or not.
208    --  Otherwise, the value of the formal Asynchronous will be used.
209    --  If Locator is not Empty, it will be used instead of RCI_Cache. If
210    --  New_Name is given, then it will be used instead of the original name.
211
212    function Build_RPC_Receiver_Specification
213      (RPC_Receiver      : Entity_Id;
214       Request_Parameter : Entity_Id) return Node_Id;
215    --  Make a subprogram specification for an RPC receiver, with the given
216    --  defining unit name and formal parameter.
217
218    function Build_Ordered_Parameters_List (Spec : Node_Id) return List_Id;
219    --  Return an ordered parameter list: unconstrained parameters are put
220    --  at the beginning of the list and constrained ones are put after. If
221    --  there are no parameters, an empty list is returned. Special case:
222    --  the controlling formal of the equivalent RACW operation for a RAS
223    --  type is always left in first position.
224
225    function Transmit_As_Unconstrained (Typ : Entity_Id) return Boolean;
226    --  True when Typ is an unconstrained type, or a null-excluding access type.
227    --  In either case, this means stubs cannot contain a default-initialized
228    --  object declaration of such type.
229
230    procedure Add_Calling_Stubs_To_Declarations
231      (Pkg_Spec : Node_Id;
232       Decls    : List_Id);
233    --  Add calling stubs to the declarative part
234
235    function Could_Be_Asynchronous (Spec : Node_Id) return Boolean;
236    --  Return True if nothing prevents the program whose specification is
237    --  given to be asynchronous (i.e. no out parameter).
238
239    function Pack_Entity_Into_Stream_Access
240      (Loc    : Source_Ptr;
241       Stream : Node_Id;
242       Object : Entity_Id;
243       Etyp   : Entity_Id := Empty) return Node_Id;
244    --  Pack Object (of type Etyp) into Stream. If Etyp is not given,
245    --  then Etype (Object) will be used if present. If the type is
246    --  constrained, then 'Write will be used to output the object,
247    --  If the type is unconstrained, 'Output will be used.
248
249    function Pack_Node_Into_Stream
250      (Loc    : Source_Ptr;
251       Stream : Entity_Id;
252       Object : Node_Id;
253       Etyp   : Entity_Id) return Node_Id;
254    --  Similar to above, with an arbitrary node instead of an entity
255
256    function Pack_Node_Into_Stream_Access
257      (Loc    : Source_Ptr;
258       Stream : Node_Id;
259       Object : Node_Id;
260       Etyp   : Entity_Id) return Node_Id;
261    --  Similar to above, with Stream instead of Stream'Access
262
263    function Make_Selected_Component
264      (Loc           : Source_Ptr;
265       Prefix        : Entity_Id;
266       Selector_Name : Name_Id) return Node_Id;
267    --  Return a selected_component whose prefix denotes the given entity,
268    --  and with the given Selector_Name.
269
270    function Scope_Of_Spec (Spec : Node_Id) return Entity_Id;
271    --  Return the scope represented by a given spec
272
273    procedure Set_Renaming_TSS
274      (Typ     : Entity_Id;
275       Nam     : Entity_Id;
276       TSS_Nam : TSS_Name_Type);
277    --  Create a renaming declaration of subprogram Nam,
278    --  and register it as a TSS for Typ with name TSS_Nam.
279
280    function Need_Extra_Constrained (Parameter : Node_Id) return Boolean;
281    --  Return True if the current parameter needs an extra formal to reflect
282    --  its constrained status.
283
284    function Is_RACW_Controlling_Formal
285      (Parameter : Node_Id;
286       Stub_Type : Entity_Id) return Boolean;
287    --  Return True if the current parameter is a controlling formal argument
288    --  of type Stub_Type or access to Stub_Type.
289
290    procedure Declare_Create_NVList
291      (Loc    : Source_Ptr;
292       NVList : Entity_Id;
293       Decls  : List_Id;
294       Stmts  : List_Id);
295    --  Append the declaration of NVList to Decls, and its
296    --  initialization to Stmts.
297
298    function Add_Parameter_To_NVList
299      (Loc         : Source_Ptr;
300       NVList      : Entity_Id;
301       Parameter   : Entity_Id;
302       Constrained : Boolean;
303       RACW_Ctrl   : Boolean := False;
304       Any         : Entity_Id) return Node_Id;
305    --  Return a call to Add_Item to add the Any corresponding to the designated
306    --  formal Parameter (with the indicated Constrained status) to NVList.
307    --  RACW_Ctrl must be set to True for controlling formals of distributed
308    --  object primitive operations.
309
310    --------------------
311    -- Stub_Structure --
312    --------------------
313
314    --  This record describes various tree fragments associated with the
315    --  generation of RACW calling stubs. One such record exists for every
316    --  distributed object type, i.e. each tagged type that is the designated
317    --  type of one or more RACW type.
318
319    type Stub_Structure is record
320       Stub_Type         : Entity_Id;
321       --  Stub type: this type has the same primitive operations as the
322       --  designated types, but the provided bodies for these operations
323       --  a remote call to an actual target object potentially located on
324       --  another partition; each value of the stub type encapsulates a
325       --  reference to a remote object.
326
327       Stub_Type_Access  : Entity_Id;
328       --  A local access type designating the stub type (this is not an RACW
329       --  type).
330
331       RPC_Receiver_Decl : Node_Id;
332       --  Declaration for the RPC receiver entity associated with the
333       --  designated type. As an exception, for the case of an RACW that
334       --  implements a RAS, no object RPC receiver is generated. Instead,
335       --  RPC_Receiver_Decl is the declaration after which the RPC receiver
336       --  would have been inserted.
337
338       Body_Decls        : List_Id;
339       --  List of subprogram bodies to be included in generated code: bodies
340       --  for the RACW's stream attributes, and for the primitive operations
341       --  of the stub type.
342
343       RACW_Type         : Entity_Id;
344       --  One of the RACW types designating this distributed object type
345       --  (they are all interchangeable; we use any one of them in order to
346       --  avoid having to create various anonymous access types).
347
348    end record;
349
350    Empty_Stub_Structure : constant Stub_Structure :=
351      (Empty, Empty, Empty, No_List, Empty);
352
353    package Stubs_Table is
354       new Simple_HTable (Header_Num => Hash_Index,
355                          Element    => Stub_Structure,
356                          No_Element => Empty_Stub_Structure,
357                          Key        => Entity_Id,
358                          Hash       => Hash,
359                          Equal      => "=");
360    --  Mapping between a RACW designated type and its stub type
361
362    package Asynchronous_Flags_Table is
363       new Simple_HTable (Header_Num => Hash_Index,
364                          Element    => Entity_Id,
365                          No_Element => Empty,
366                          Key        => Entity_Id,
367                          Hash       => Hash,
368                          Equal      => "=");
369    --  Mapping between a RACW type and a constant having the value True
370    --  if the RACW is asynchronous and False otherwise.
371
372    package RCI_Locator_Table is
373       new Simple_HTable (Header_Num => Hash_Index,
374                          Element    => Entity_Id,
375                          No_Element => Empty,
376                          Key        => Entity_Id,
377                          Hash       => Hash,
378                          Equal      => "=");
379    --  Mapping between a RCI package on which All_Calls_Remote applies and
380    --  the generic instantiation of RCI_Locator for this package.
381
382    package RCI_Calling_Stubs_Table is
383       new Simple_HTable (Header_Num => Hash_Index,
384                          Element    => Entity_Id,
385                          No_Element => Empty,
386                          Key        => Entity_Id,
387                          Hash       => Hash,
388                          Equal      => "=");
389    --  Mapping between a RCI subprogram and the corresponding calling stubs
390
391    function Get_Stub_Elements (RACW_Type : Entity_Id) return Stub_Structure;
392    --  Return the stub information associated with the given RACW type
393
394    procedure Add_Stub_Type
395      (Designated_Type   : Entity_Id;
396       RACW_Type         : Entity_Id;
397       Decls             : List_Id;
398       Stub_Type         : out Entity_Id;
399       Stub_Type_Access  : out Entity_Id;
400       RPC_Receiver_Decl : out Node_Id;
401       Body_Decls        : out List_Id;
402       Existing          : out Boolean);
403    --  Add the declaration of the stub type, the access to stub type and the
404    --  object RPC receiver at the end of Decls. If these already exist,
405    --  then nothing is added in the tree but the right values are returned
406    --  anyhow and Existing is set to True.
407
408    function Get_And_Reset_RACW_Bodies (RACW_Type : Entity_Id) return List_Id;
409    --  Retrieve the Body_Decls list associated to RACW_Type in the stub
410    --  structure table, reset it to No_List, and return the previous value.
411
412    procedure Add_RACW_Asynchronous_Flag
413      (Declarations : List_Id;
414       RACW_Type    : Entity_Id);
415    --  Declare a boolean constant associated with RACW_Type whose value
416    --  indicates at run time whether a pragma Asynchronous applies to it.
417
418    procedure Assign_Subprogram_Identifier
419      (Def : Entity_Id;
420       Spn : Int;
421       Id  : out String_Id);
422    --  Determine the distribution subprogram identifier to
423    --  be used for remote subprogram Def, return it in Id and
424    --  store it in a hash table for later retrieval by
425    --  Get_Subprogram_Id. Spn is the subprogram number.
426
427    function RCI_Package_Locator
428      (Loc          : Source_Ptr;
429       Package_Spec : Node_Id) return Node_Id;
430    --  Instantiate the generic package RCI_Locator in order to locate the
431    --  RCI package whose spec is given as argument.
432
433    function Make_Tag_Check (Loc : Source_Ptr; N : Node_Id) return Node_Id;
434    --  Surround a node N by a tag check, as in:
435    --      begin
436    --         <N>;
437    --      exception
438    --         when E : Ada.Tags.Tag_Error =>
439    --           Raise_Exception (Program_Error'Identity,
440    --                            Exception_Message (E));
441    --      end;
442
443    function Input_With_Tag_Check
444      (Loc      : Source_Ptr;
445       Var_Type : Entity_Id;
446       Stream   : Node_Id) return Node_Id;
447    --  Return a function with the following form:
448    --    function R return Var_Type is
449    --    begin
450    --       return Var_Type'Input (S);
451    --    exception
452    --       when E : Ada.Tags.Tag_Error =>
453    --           Raise_Exception (Program_Error'Identity,
454    --                            Exception_Message (E));
455    --    end R;
456
457    procedure Build_Actual_Object_Declaration
458      (Object   : Entity_Id;
459       Etyp     : Entity_Id;
460       Variable : Boolean;
461       Expr     : Node_Id;
462       Decls    : List_Id);
463    --  Build the declaration of an object with the given defining identifier,
464    --  initialized with Expr if provided, to serve as actual parameter in a
465    --  server stub. If Variable is true, the declared object will be a variable
466    --  (case of an out or in out formal), else it will be a constant. Object's
467    --  Ekind is set accordingly. The declaration, as well as any other
468    --  declarations it requires, are appended to Decls.
469
470    --------------------------------------------
471    -- Hooks for PCS-specific code generation --
472    --------------------------------------------
473
474    --  Part of the code generation circuitry for distribution needs to be
475    --  tailored for each implementation of the PCS. For each routine that
476    --  needs to be specialized, a Specific_<routine> wrapper is created,
477    --  which calls the corresponding <routine> in package
478    --  <pcs_implementation>_Support.
479
480    procedure Specific_Add_RACW_Features
481      (RACW_Type           : Entity_Id;
482       Desig               : Entity_Id;
483       Stub_Type           : Entity_Id;
484       Stub_Type_Access    : Entity_Id;
485       RPC_Receiver_Decl   : Node_Id;
486       Body_Decls          : List_Id);
487    --  Add declaration for TSSs for a given RACW type. The declarations are
488    --  added just after the declaration of the RACW type itself. If the RACW
489    --  appears in the main unit, Body_Decls is a list of declarations to which
490    --  the bodies are appended. Else Body_Decls is No_List.
491    --  PCS-specific ancillary subprogram for Add_RACW_Features.
492
493    procedure Specific_Add_RAST_Features
494      (Vis_Decl : Node_Id;
495       RAS_Type : Entity_Id);
496    --  Add declaration for TSSs for a given RAS type. PCS-specific ancillary
497    --  subprogram for Add_RAST_Features.
498
499    --  An RPC_Target record is used during construction of calling stubs
500    --  to pass PCS-specific tree fragments corresponding to the information
501    --  necessary to locate the target of a remote subprogram call.
502
503    type RPC_Target (PCS_Kind : PCS_Names) is record
504       case PCS_Kind is
505          when Name_PolyORB_DSA =>
506             Object : Node_Id;
507             --  An expression whose value is a PolyORB reference to the target
508             --  object.
509
510          when others           =>
511             Partition : Entity_Id;
512             --  A variable containing the Partition_ID of the target partition
513
514             RPC_Receiver : Node_Id;
515             --  An expression whose value is the address of the target RPC
516             --  receiver.
517       end case;
518    end record;
519
520    procedure Specific_Build_General_Calling_Stubs
521      (Decls                     : List_Id;
522       Statements                : List_Id;
523       Target                    : RPC_Target;
524       Subprogram_Id             : Node_Id;
525       Asynchronous              : Node_Id := Empty;
526       Is_Known_Asynchronous     : Boolean := False;
527       Is_Known_Non_Asynchronous : Boolean := False;
528       Is_Function               : Boolean;
529       Spec                      : Node_Id;
530       Stub_Type                 : Entity_Id := Empty;
531       RACW_Type                 : Entity_Id := Empty;
532       Nod                       : Node_Id);
533    --  Build calling stubs for general purpose. The parameters are:
534    --    Decls             : a place to put declarations
535    --    Statements        : a place to put statements
536    --    Target            : PCS-specific target information (see details
537    --                        in RPC_Target declaration).
538    --    Subprogram_Id     : a node containing the subprogram ID
539    --    Asynchronous      : True if an APC must be made instead of an RPC.
540    --                        The value needs not be supplied if one of the
541    --                        Is_Known_... is True.
542    --    Is_Known_Async... : True if we know that this is asynchronous
543    --    Is_Known_Non_A... : True if we know that this is not asynchronous
544    --    Spec              : a node with a Parameter_Specifications and
545    --                        a Result_Definition if applicable
546    --    Stub_Type         : in case of RACW stubs, parameters of type access
547    --                        to Stub_Type will be marshalled using the
548    --                        address of the object (the addr field) rather
549    --                        than using the 'Write on the stub itself
550    --    Nod               : used to provide sloc for generated code
551
552    function Specific_Build_Stub_Target
553      (Loc                   : Source_Ptr;
554       Decls                 : List_Id;
555       RCI_Locator           : Entity_Id;
556       Controlling_Parameter : Entity_Id) return RPC_Target;
557    --  Build call target information nodes for use within calling stubs. In the
558    --  RCI case, RCI_Locator is the entity for the instance of RCI_Locator. If
559    --  for an RACW, Controlling_Parameter is the entity for the controlling
560    --  formal parameter used to determine the location of the target of the
561    --  call. Decls provides a location where variable declarations can be
562    --  appended to construct the necessary values.
563
564    procedure Specific_Build_Stub_Type
565      (RACW_Type         : Entity_Id;
566       Stub_Type         : Entity_Id;
567       Stub_Type_Decl    : out Node_Id;
568       RPC_Receiver_Decl : out Node_Id);
569    --  Build a type declaration for the stub type associated with an RACW
570    --  type, and the necessary RPC receiver, if applicable. PCS-specific
571    --  ancillary subprogram for Add_Stub_Type. If no RPC receiver declaration
572    --  is generated, then RPC_Receiver_Decl is set to Empty.
573
574    procedure Specific_Build_RPC_Receiver_Body
575      (RPC_Receiver : Entity_Id;
576       Request      : out Entity_Id;
577       Subp_Id      : out Entity_Id;
578       Subp_Index   : out Entity_Id;
579       Stmts        : out List_Id;
580       Decl         : out Node_Id);
581    --  Make a subprogram body for an RPC receiver, with the given
582    --  defining unit name. On return:
583    --    - Subp_Id is the subprogram identifier from the PCS.
584    --    - Subp_Index is the index in the list of subprograms
585    --      used for dispatching (a variable of type Subprogram_Id).
586    --    - Stmts is the place where the request dispatching
587    --      statements can occur,
588    --    - Decl is the subprogram body declaration.
589
590    function Specific_Build_Subprogram_Receiving_Stubs
591      (Vis_Decl                 : Node_Id;
592       Asynchronous             : Boolean;
593       Dynamically_Asynchronous : Boolean   := False;
594       Stub_Type                : Entity_Id := Empty;
595       RACW_Type                : Entity_Id := Empty;
596       Parent_Primitive         : Entity_Id := Empty) return Node_Id;
597    --  Build the receiving stub for a given subprogram. The subprogram
598    --  declaration is also built by this procedure, and the value returned
599    --  is a N_Subprogram_Body. If a parameter of type access to Stub_Type is
600    --  found in the specification, then its address is read from the stream
601    --  instead of the object itself and converted into an access to
602    --  class-wide type before doing the real call using any of the RACW type
603    --  pointing on the designated type.
604
605    procedure Specific_Add_Obj_RPC_Receiver_Completion
606      (Loc           : Source_Ptr;
607       Decls         : List_Id;
608       RPC_Receiver  : Entity_Id;
609       Stub_Elements : Stub_Structure);
610    --  Add the necessary code to Decls after the completion of generation
611    --  of the RACW RPC receiver described by Stub_Elements.
612
613    procedure Specific_Add_Receiving_Stubs_To_Declarations
614      (Pkg_Spec : Node_Id;
615       Decls    : List_Id;
616       Stmts    : List_Id);
617    --  Add receiving stubs to the declarative part of an RCI unit
618
619    package GARLIC_Support is
620
621       --  Support for generating DSA code that uses the GARLIC PCS
622
623       --  The subprograms below provide the GARLIC versions of the
624       --  corresponding Specific_<subprogram> routine declared above.
625
626       procedure Add_RACW_Features
627         (RACW_Type         : Entity_Id;
628          Stub_Type         : Entity_Id;
629          Stub_Type_Access  : Entity_Id;
630          RPC_Receiver_Decl : Node_Id;
631          Body_Decls        : List_Id);
632
633       procedure Add_RAST_Features
634         (Vis_Decl : Node_Id;
635          RAS_Type : Entity_Id);
636
637       procedure Build_General_Calling_Stubs
638         (Decls                     : List_Id;
639          Statements                : List_Id;
640          Target_Partition          : Entity_Id; --  From RPC_Target
641          Target_RPC_Receiver       : Node_Id;   --  From RPC_Target
642          Subprogram_Id             : Node_Id;
643          Asynchronous              : Node_Id := Empty;
644          Is_Known_Asynchronous     : Boolean := False;
645          Is_Known_Non_Asynchronous : Boolean := False;
646          Is_Function               : Boolean;
647          Spec                      : Node_Id;
648          Stub_Type                 : Entity_Id := Empty;
649          RACW_Type                 : Entity_Id := Empty;
650          Nod                       : Node_Id);
651
652       function Build_Stub_Target
653         (Loc                   : Source_Ptr;
654          Decls                 : List_Id;
655          RCI_Locator           : Entity_Id;
656          Controlling_Parameter : Entity_Id) return RPC_Target;
657
658       procedure Build_Stub_Type
659         (RACW_Type         : Entity_Id;
660          Stub_Type         : Entity_Id;
661          Stub_Type_Decl    : out Node_Id;
662          RPC_Receiver_Decl : out Node_Id);
663
664       function Build_Subprogram_Receiving_Stubs
665         (Vis_Decl                 : Node_Id;
666          Asynchronous             : Boolean;
667          Dynamically_Asynchronous : Boolean   := False;
668          Stub_Type                : Entity_Id := Empty;
669          RACW_Type                : Entity_Id := Empty;
670          Parent_Primitive         : Entity_Id := Empty) return Node_Id;
671
672       procedure Add_Obj_RPC_Receiver_Completion
673         (Loc           : Source_Ptr;
674          Decls         : List_Id;
675          RPC_Receiver  : Entity_Id;
676          Stub_Elements : Stub_Structure);
677
678       procedure Add_Receiving_Stubs_To_Declarations
679         (Pkg_Spec : Node_Id;
680          Decls    : List_Id;
681          Stmts    : List_Id);
682
683       procedure Build_RPC_Receiver_Body
684         (RPC_Receiver : Entity_Id;
685          Request      : out Entity_Id;
686          Subp_Id      : out Entity_Id;
687          Subp_Index   : out Entity_Id;
688          Stmts        : out List_Id;
689          Decl         : out Node_Id);
690
691    end GARLIC_Support;
692
693    package PolyORB_Support is
694
695       --  Support for generating DSA code that uses the PolyORB PCS
696
697       --  The subprograms below provide the PolyORB versions of the
698       --  corresponding Specific_<subprogram> routine declared above.
699
700       procedure Add_RACW_Features
701         (RACW_Type         : Entity_Id;
702          Desig             : Entity_Id;
703          Stub_Type         : Entity_Id;
704          Stub_Type_Access  : Entity_Id;
705          RPC_Receiver_Decl : Node_Id;
706          Body_Decls        : List_Id);
707
708       procedure Add_RAST_Features
709         (Vis_Decl : Node_Id;
710          RAS_Type : Entity_Id);
711
712       procedure Build_General_Calling_Stubs
713         (Decls                     : List_Id;
714          Statements                : List_Id;
715          Target_Object             : Node_Id; --  From RPC_Target
716          Subprogram_Id             : Node_Id;
717          Asynchronous              : Node_Id := Empty;
718          Is_Known_Asynchronous     : Boolean := False;
719          Is_Known_Non_Asynchronous : Boolean := False;
720          Is_Function               : Boolean;
721          Spec                      : Node_Id;
722          Stub_Type                 : Entity_Id := Empty;
723          RACW_Type                 : Entity_Id := Empty;
724          Nod                       : Node_Id);
725
726       function Build_Stub_Target
727         (Loc                   : Source_Ptr;
728          Decls                 : List_Id;
729          RCI_Locator           : Entity_Id;
730          Controlling_Parameter : Entity_Id) return RPC_Target;
731
732       procedure Build_Stub_Type
733         (RACW_Type         : Entity_Id;
734          Stub_Type         : Entity_Id;
735          Stub_Type_Decl    : out Node_Id;
736          RPC_Receiver_Decl : out Node_Id);
737
738       function Build_Subprogram_Receiving_Stubs
739         (Vis_Decl                 : Node_Id;
740          Asynchronous             : Boolean;
741          Dynamically_Asynchronous : Boolean   := False;
742          Stub_Type                : Entity_Id := Empty;
743          RACW_Type                : Entity_Id := Empty;
744          Parent_Primitive         : Entity_Id := Empty) return Node_Id;
745
746       procedure Add_Obj_RPC_Receiver_Completion
747         (Loc           : Source_Ptr;
748          Decls         : List_Id;
749          RPC_Receiver  : Entity_Id;
750          Stub_Elements : Stub_Structure);
751
752       procedure Add_Receiving_Stubs_To_Declarations
753         (Pkg_Spec : Node_Id;
754          Decls    : List_Id;
755          Stmts    : List_Id);
756
757       procedure Build_RPC_Receiver_Body
758         (RPC_Receiver : Entity_Id;
759          Request      : out Entity_Id;
760          Subp_Id      : out Entity_Id;
761          Subp_Index   : out Entity_Id;
762          Stmts        : out List_Id;
763          Decl         : out Node_Id);
764
765       procedure Reserve_NamingContext_Methods;
766       --  Mark the method names for interface NamingContext as already used in
767       --  the overload table, so no clashes occur with user code (with the
768       --  PolyORB PCS, RCIs Implement The NamingContext interface to allow
769       --  their methods to be accessed as objects, for the implementation of
770       --  remote access-to-subprogram types).
771
772       package Helpers is
773
774          --  Routines to build distribution helper subprograms for user-defined
775          --  types. For implementation of the Distributed systems annex (DSA)
776          --  over the PolyORB generic middleware components, it is necessary to
777          --  generate several supporting subprograms for each application data
778          --  type used in inter-partition communication. These subprograms are:
779
780          --    A Typecode function returning a high-level description of the
781          --    type's structure;
782
783          --    Two conversion functions allowing conversion of values of the
784          --    type from and to the generic data containers used by PolyORB.
785          --    These generic containers are called 'Any' type values after the
786          --    CORBA terminology, and hence the conversion subprograms are
787          --    named To_Any and From_Any.
788
789          function Build_From_Any_Call
790            (Typ   : Entity_Id;
791             N     : Node_Id;
792             Decls : List_Id) return Node_Id;
793          --  Build call to From_Any attribute function of type Typ with
794          --  expression N as actual parameter. Decls is the declarations list
795          --  for an appropriate enclosing scope of the point where the call
796          --  will be inserted; if the From_Any attribute for Typ needs to be
797          --  generated at this point, its declaration is appended to Decls.
798
799          procedure Build_From_Any_Function
800            (Loc  : Source_Ptr;
801             Typ  : Entity_Id;
802             Decl : out Node_Id;
803             Fnam : out Entity_Id);
804          --  Build From_Any attribute function for Typ. Loc is the reference
805          --  location for generated nodes, Typ is the type for which the
806          --  conversion function is generated. On return, Decl and Fnam contain
807          --  the declaration and entity for the newly-created function.
808
809          function Build_To_Any_Call
810            (N     : Node_Id;
811             Decls : List_Id) return Node_Id;
812          --  Build call to To_Any attribute function with expression as actual
813          --  parameter. Decls is the declarations list for an appropriate
814          --  enclosing scope of the point where the call will be inserted; if
815          --  the To_Any attribute for Typ needs to be generated at this point,
816          --  its declaration is appended to Decls.
817
818          procedure Build_To_Any_Function
819            (Loc  : Source_Ptr;
820             Typ  : Entity_Id;
821             Decl : out Node_Id;
822             Fnam : out Entity_Id);
823          --  Build To_Any attribute function for Typ. Loc is the reference
824          --  location for generated nodes, Typ is the type for which the
825          --  conversion function is generated. On return, Decl and Fnam contain
826          --  the declaration and entity for the newly-created function.
827
828          function Build_TypeCode_Call
829            (Loc   : Source_Ptr;
830             Typ   : Entity_Id;
831             Decls : List_Id) return Node_Id;
832          --  Build call to TypeCode attribute function for Typ. Decls is the
833          --  declarations list for an appropriate enclosing scope of the point
834          --  where the call will be inserted; if the To_Any attribute for Typ
835          --  needs to be generated at this point, its declaration is appended
836          --  to Decls.
837
838          procedure Build_TypeCode_Function
839            (Loc  : Source_Ptr;
840             Typ  : Entity_Id;
841             Decl : out Node_Id;
842             Fnam : out Entity_Id);
843          --  Build TypeCode attribute function for Typ. Loc is the reference
844          --  location for generated nodes, Typ is the type for which the
845          --  conversion function is generated. On return, Decl and Fnam contain
846          --  the declaration and entity for the newly-created function.
847
848          procedure Build_Name_And_Repository_Id
849            (E           : Entity_Id;
850             Name_Str    : out String_Id;
851             Repo_Id_Str : out String_Id);
852          --  In the PolyORB distribution model, each distributed object type
853          --  and each distributed operation has a globally unique identifier,
854          --  its Repository Id. This subprogram builds and returns two strings
855          --  for entity E (a distributed object type or operation): one
856          --  containing the name of E, the second containing its repository id.
857
858       end Helpers;
859
860    end PolyORB_Support;
861
862    --  The following PolyORB-specific subprograms are made visible to Exp_Attr:
863
864    function Build_From_Any_Call
865      (Typ   : Entity_Id;
866       N     : Node_Id;
867       Decls : List_Id) return Node_Id
868      renames PolyORB_Support.Helpers.Build_From_Any_Call;
869
870    function Build_To_Any_Call
871      (N     : Node_Id;
872       Decls : List_Id) return Node_Id
873      renames PolyORB_Support.Helpers.Build_To_Any_Call;
874
875    function Build_TypeCode_Call
876      (Loc   : Source_Ptr;
877       Typ   : Entity_Id;
878       Decls : List_Id) return Node_Id
879      renames PolyORB_Support.Helpers.Build_TypeCode_Call;
880
881    ------------------------------------
882    -- Local variables and structures --
883    ------------------------------------
884
885    RCI_Cache : Node_Id;
886    --  Needs comments ???
887
888    Output_From_Constrained : constant array (Boolean) of Name_Id :=
889      (False => Name_Output,
890       True  => Name_Write);
891    --  The attribute to choose depending on the fact that the parameter
892    --  is constrained or not. There is no such thing as Input_From_Constrained
893    --  since this require separate mechanisms ('Input is a function while
894    --  'Read is a procedure).
895
896    ---------------------------------------
897    -- Add_Calling_Stubs_To_Declarations --
898    ---------------------------------------
899
900    procedure Add_Calling_Stubs_To_Declarations
901      (Pkg_Spec : Node_Id;
902       Decls    : List_Id)
903    is
904       Current_Subprogram_Number : Int := First_RCI_Subprogram_Id;
905       --  Subprogram id 0 is reserved for calls received from
906       --  remote access-to-subprogram dereferences.
907
908       Current_Declaration : Node_Id;
909       Loc                 : constant Source_Ptr := Sloc (Pkg_Spec);
910       RCI_Instantiation   : Node_Id;
911       Subp_Stubs          : Node_Id;
912       Subp_Str            : String_Id;
913
914       pragma Warnings (Off, Subp_Str);
915
916    begin
917       --  The first thing added is an instantiation of the generic package
918       --  System.Partition_Interface.RCI_Locator with the name of this remote
919       --  package. This will act as an interface with the name server to
920       --  determine the Partition_ID and the RPC_Receiver for the receiver
921       --  of this package.
922
923       RCI_Instantiation := RCI_Package_Locator (Loc, Pkg_Spec);
924       RCI_Cache         := Defining_Unit_Name (RCI_Instantiation);
925
926       Append_To (Decls, RCI_Instantiation);
927       Analyze (RCI_Instantiation);
928
929       --  For each subprogram declaration visible in the spec, we do build a
930       --  body. We also increment a counter to assign a different Subprogram_Id
931       --  to each subprograms. The receiving stubs processing do use the same
932       --  mechanism and will thus assign the same Id and do the correct
933       --  dispatching.
934
935       Overload_Counter_Table.Reset;
936       PolyORB_Support.Reserve_NamingContext_Methods;
937
938       Current_Declaration := First (Visible_Declarations (Pkg_Spec));
939       while Present (Current_Declaration) loop
940          if Nkind (Current_Declaration) = N_Subprogram_Declaration
941            and then Comes_From_Source (Current_Declaration)
942          then
943             Assign_Subprogram_Identifier
944               (Defining_Unit_Name (Specification (Current_Declaration)),
945                Current_Subprogram_Number,
946                Subp_Str);
947
948             Subp_Stubs :=
949               Build_Subprogram_Calling_Stubs (
950                 Vis_Decl     => Current_Declaration,
951                 Subp_Id      =>
952                   Build_Subprogram_Id (Loc,
953                     Defining_Unit_Name (Specification (Current_Declaration))),
954                 Asynchronous =>
955                   Nkind (Specification (Current_Declaration)) =
956                     N_Procedure_Specification
957                   and then
958                     Is_Asynchronous (Defining_Unit_Name (Specification
959                       (Current_Declaration))));
960
961             Append_To (Decls, Subp_Stubs);
962             Analyze (Subp_Stubs);
963
964             Current_Subprogram_Number := Current_Subprogram_Number + 1;
965          end if;
966
967          Next (Current_Declaration);
968       end loop;
969    end Add_Calling_Stubs_To_Declarations;
970
971    -----------------------------
972    -- Add_Parameter_To_NVList --
973    -----------------------------
974
975    function Add_Parameter_To_NVList
976      (Loc         : Source_Ptr;
977       NVList      : Entity_Id;
978       Parameter   : Entity_Id;
979       Constrained : Boolean;
980       RACW_Ctrl   : Boolean := False;
981       Any         : Entity_Id) return Node_Id
982    is
983       Parameter_Name_String : String_Id;
984       Parameter_Mode        : Node_Id;
985
986       function Parameter_Passing_Mode
987         (Loc         : Source_Ptr;
988          Parameter   : Entity_Id;
989          Constrained : Boolean) return Node_Id;
990       --  Return an expression that denotes the parameter passing mode to be
991       --  used for Parameter in distribution stubs, where Constrained is
992       --  Parameter's constrained status.
993
994       ----------------------------
995       -- Parameter_Passing_Mode --
996       ----------------------------
997
998       function Parameter_Passing_Mode
999         (Loc         : Source_Ptr;
1000          Parameter   : Entity_Id;
1001          Constrained : Boolean) return Node_Id
1002       is
1003          Lib_RE : RE_Id;
1004
1005       begin
1006          if Out_Present (Parameter) then
1007             if In_Present (Parameter)
1008               or else not Constrained
1009             then
1010                --  Unconstrained formals must be translated
1011                --  to 'in' or 'inout', not 'out', because
1012                --  they need to be constrained by the actual.
1013
1014                Lib_RE := RE_Mode_Inout;
1015             else
1016                Lib_RE := RE_Mode_Out;
1017             end if;
1018
1019          else
1020             Lib_RE := RE_Mode_In;
1021          end if;
1022
1023          return New_Occurrence_Of (RTE (Lib_RE), Loc);
1024       end Parameter_Passing_Mode;
1025
1026    --  Start of processing for Add_Parameter_To_NVList
1027
1028    begin
1029       if Nkind (Parameter) = N_Defining_Identifier then
1030          Get_Name_String (Chars (Parameter));
1031       else
1032          Get_Name_String (Chars (Defining_Identifier (Parameter)));
1033       end if;
1034
1035       Parameter_Name_String := String_From_Name_Buffer;
1036
1037       if RACW_Ctrl or else Nkind (Parameter) = N_Defining_Identifier then
1038
1039          --  When the parameter passed to Add_Parameter_To_NVList is an
1040          --  Extra_Constrained parameter, Parameter is an N_Defining_
1041          --  Identifier, instead of a complete N_Parameter_Specification.
1042          --  Thus, we explicitly set 'in' mode in this case.
1043
1044          Parameter_Mode := New_Occurrence_Of (RTE (RE_Mode_In), Loc);
1045
1046       else
1047          Parameter_Mode :=
1048            Parameter_Passing_Mode (Loc, Parameter, Constrained);
1049       end if;
1050
1051       return
1052         Make_Procedure_Call_Statement (Loc,
1053           Name =>
1054             New_Occurrence_Of
1055               (RTE (RE_NVList_Add_Item), Loc),
1056           Parameter_Associations => New_List (
1057             New_Occurrence_Of (NVList, Loc),
1058             Make_Function_Call (Loc,
1059               Name =>
1060                 New_Occurrence_Of
1061                   (RTE (RE_To_PolyORB_String), Loc),
1062               Parameter_Associations => New_List (
1063                 Make_String_Literal (Loc,
1064                   Strval => Parameter_Name_String))),
1065             New_Occurrence_Of (Any, Loc),
1066             Parameter_Mode));
1067    end Add_Parameter_To_NVList;
1068
1069    --------------------------------
1070    -- Add_RACW_Asynchronous_Flag --
1071    --------------------------------
1072
1073    procedure Add_RACW_Asynchronous_Flag
1074      (Declarations : List_Id;
1075       RACW_Type    : Entity_Id)
1076    is
1077       Loc : constant Source_Ptr := Sloc (RACW_Type);
1078
1079       Asynchronous_Flag : constant Entity_Id :=
1080                             Make_Defining_Identifier (Loc,
1081                               New_External_Name (Chars (RACW_Type), 'A'));
1082
1083    begin
1084       --  Declare the asynchronous flag. This flag will be changed to True
1085       --  whenever it is known that the RACW type is asynchronous.
1086
1087       Append_To (Declarations,
1088         Make_Object_Declaration (Loc,
1089           Defining_Identifier => Asynchronous_Flag,
1090           Constant_Present    => True,
1091           Object_Definition   => New_Occurrence_Of (Standard_Boolean, Loc),
1092           Expression          => New_Occurrence_Of (Standard_False, Loc)));
1093
1094       Asynchronous_Flags_Table.Set (RACW_Type, Asynchronous_Flag);
1095    end Add_RACW_Asynchronous_Flag;
1096
1097    -----------------------
1098    -- Add_RACW_Features --
1099    -----------------------
1100
1101    procedure Add_RACW_Features (RACW_Type : Entity_Id) is
1102       Desig      : constant Entity_Id := Etype (Designated_Type (RACW_Type));
1103       Same_Scope : constant Boolean   := Scope (Desig) = Scope (RACW_Type);
1104
1105       Pkg_Spec   : Node_Id;
1106       Decls      : List_Id;
1107       Body_Decls : List_Id;
1108
1109       Stub_Type         : Entity_Id;
1110       Stub_Type_Access  : Entity_Id;
1111       RPC_Receiver_Decl : Node_Id;
1112
1113       Existing : Boolean;
1114       --  True when appropriate stubs have already been generated (this is the
1115       --  case when another RACW with the same designated type has already been
1116       --  encountered), in which case we reuse the previous stubs rather than
1117       --  generating new ones.
1118
1119    begin
1120       if not Expander_Active then
1121          return;
1122       end if;
1123
1124       --  Mark the current package declaration as containing an RACW, so that
1125       --  the bodies for the calling stubs and the RACW stream subprograms
1126       --  are attached to the tree when the corresponding body is encountered.
1127
1128       Set_Has_RACW (Current_Scope);
1129
1130       --  Look for place to declare the RACW stub type and RACW operations
1131
1132       Pkg_Spec := Empty;
1133
1134       if Same_Scope then
1135
1136          --  Case of declaring the RACW in the same package as its designated
1137          --  type: we know that the designated type is a private type, so we
1138          --  use the private declarations list.
1139
1140          Pkg_Spec := Package_Specification_Of_Scope (Current_Scope);
1141
1142          if Present (Private_Declarations (Pkg_Spec)) then
1143             Decls := Private_Declarations (Pkg_Spec);
1144          else
1145             Decls := Visible_Declarations (Pkg_Spec);
1146          end if;
1147
1148       else
1149
1150          --  Case of declaring the RACW in another package than its designated
1151          --  type: use the private declarations list if present; otherwise
1152          --  use the visible declarations.
1153
1154          Decls := List_Containing (Declaration_Node (RACW_Type));
1155
1156       end if;
1157
1158       --  If we were unable to find the declarations, that means that the
1159       --  completion of the type was missing. We can safely return and let the
1160       --  error be caught by the semantic analysis.
1161
1162       if No (Decls) then
1163          return;
1164       end if;
1165
1166       Add_Stub_Type
1167         (Designated_Type     => Desig,
1168          RACW_Type           => RACW_Type,
1169          Decls               => Decls,
1170          Stub_Type           => Stub_Type,
1171          Stub_Type_Access    => Stub_Type_Access,
1172          RPC_Receiver_Decl   => RPC_Receiver_Decl,
1173          Body_Decls          => Body_Decls,
1174          Existing            => Existing);
1175
1176       --  If this RACW is not in the main unit, do not generate primitive or
1177       --  TSS bodies.
1178
1179       if not Entity_Is_In_Main_Unit (RACW_Type) then
1180          Body_Decls := No_List;
1181       end if;
1182
1183       Add_RACW_Asynchronous_Flag
1184         (Declarations        => Decls,
1185          RACW_Type           => RACW_Type);
1186
1187       Specific_Add_RACW_Features
1188         (RACW_Type           => RACW_Type,
1189          Desig               => Desig,
1190          Stub_Type           => Stub_Type,
1191          Stub_Type_Access    => Stub_Type_Access,
1192          RPC_Receiver_Decl   => RPC_Receiver_Decl,
1193          Body_Decls          => Body_Decls);
1194
1195       --  If we already have stubs for this designated type, nothing to do
1196
1197       if Existing then
1198          return;
1199       end if;
1200
1201       if Is_Frozen (Desig) then
1202          Validate_RACW_Primitives (RACW_Type);
1203          Add_RACW_Primitive_Declarations_And_Bodies
1204            (Designated_Type  => Desig,
1205             Insertion_Node   => RPC_Receiver_Decl,
1206             Body_Decls       => Body_Decls);
1207
1208       else
1209          --  Validate_RACW_Primitives requires the list of all primitives of
1210          --  the designated type, so defer processing until Desig is frozen.
1211          --  See Exp_Ch3.Freeze_Type.
1212
1213          Add_Access_Type_To_Process (E => Desig, A => RACW_Type);
1214       end if;
1215    end Add_RACW_Features;
1216
1217    ------------------------------------------------
1218    -- Add_RACW_Primitive_Declarations_And_Bodies --
1219    ------------------------------------------------
1220
1221    procedure Add_RACW_Primitive_Declarations_And_Bodies
1222      (Designated_Type : Entity_Id;
1223       Insertion_Node  : Node_Id;
1224       Body_Decls      : List_Id)
1225    is
1226       Loc : constant Source_Ptr := Sloc (Insertion_Node);
1227       --  Set Sloc of generated declaration copy of insertion node Sloc, so
1228       --  the declarations are recognized as belonging to the current package.
1229
1230       Stub_Elements : constant Stub_Structure :=
1231                         Stubs_Table.Get (Designated_Type);
1232
1233       pragma Assert (Stub_Elements /= Empty_Stub_Structure);
1234
1235       Is_RAS : constant Boolean :=
1236                  not Comes_From_Source (Stub_Elements.RACW_Type);
1237       --  Case of the RACW generated to implement a remote access-to-
1238       --  subprogram type.
1239
1240       Build_Bodies : constant Boolean :=
1241                        In_Extended_Main_Code_Unit (Stub_Elements.Stub_Type);
1242       --  True when bodies must be prepared in Body_Decls. Bodies are generated
1243       --  only when the main unit is the unit that contains the stub type.
1244
1245       Current_Insertion_Node : Node_Id := Insertion_Node;
1246
1247       RPC_Receiver                   : Entity_Id;
1248       RPC_Receiver_Statements        : List_Id;
1249       RPC_Receiver_Case_Alternatives : constant List_Id := New_List;
1250       RPC_Receiver_Elsif_Parts       : List_Id;
1251       RPC_Receiver_Request           : Entity_Id;
1252       RPC_Receiver_Subp_Id           : Entity_Id;
1253       RPC_Receiver_Subp_Index        : Entity_Id;
1254
1255       Subp_Str : String_Id;
1256
1257       Current_Primitive_Elmt   : Elmt_Id;
1258       Current_Primitive        : Entity_Id;
1259       Current_Primitive_Body   : Node_Id;
1260       Current_Primitive_Spec   : Node_Id;
1261       Current_Primitive_Decl   : Node_Id;
1262       Current_Primitive_Number : Int := 0;
1263       Current_Primitive_Alias  : Node_Id;
1264       Current_Receiver         : Entity_Id;
1265       Current_Receiver_Body    : Node_Id;
1266       RPC_Receiver_Decl        : Node_Id;
1267       Possibly_Asynchronous    : Boolean;
1268
1269    begin
1270       if not Expander_Active then
1271          return;
1272       end if;
1273
1274       if not Is_RAS then
1275          RPC_Receiver :=
1276            Make_Defining_Identifier (Loc,
1277              Chars => New_Internal_Name ('P'));
1278
1279          Specific_Build_RPC_Receiver_Body
1280            (RPC_Receiver => RPC_Receiver,
1281             Request      => RPC_Receiver_Request,
1282             Subp_Id      => RPC_Receiver_Subp_Id,
1283             Subp_Index   => RPC_Receiver_Subp_Index,
1284             Stmts        => RPC_Receiver_Statements,
1285             Decl         => RPC_Receiver_Decl);
1286
1287          if Get_PCS_Name = Name_PolyORB_DSA then
1288
1289             --  For the case of PolyORB, we need to map a textual operation
1290             --  name into a primitive index. Currently we do so using a simple
1291             --  sequence of string comparisons.
1292
1293             RPC_Receiver_Elsif_Parts := New_List;
1294          end if;
1295       end if;
1296
1297       --  Build callers, receivers for every primitive operations and a RPC
1298       --  receiver for this type.
1299
1300       if Present (Primitive_Operations (Designated_Type)) then
1301          Overload_Counter_Table.Reset;
1302
1303          Current_Primitive_Elmt :=
1304            First_Elmt (Primitive_Operations (Designated_Type));
1305          while Current_Primitive_Elmt /= No_Elmt loop
1306             Current_Primitive := Node (Current_Primitive_Elmt);
1307
1308             --  Copy the primitive of all the parents, except predefined ones
1309             --  that are not remotely dispatching. Also omit hidden primitives
1310             --  (occurs in the case of primitives of interface progenitors
1311             --  other than immediate ancestors of the Designated_Type).
1312
1313             if Chars (Current_Primitive) /= Name_uSize
1314               and then Chars (Current_Primitive) /= Name_uAlignment
1315               and then not
1316                 (Is_TSS (Current_Primitive, TSS_Deep_Finalize) or else
1317                  Is_TSS (Current_Primitive, TSS_Stream_Input)  or else
1318                  Is_TSS (Current_Primitive, TSS_Stream_Output) or else
1319                  Is_TSS (Current_Primitive, TSS_Stream_Read)   or else
1320                  Is_TSS (Current_Primitive, TSS_Stream_Write))
1321               and then not Is_Hidden (Current_Primitive)
1322             then
1323                --  The first thing to do is build an up-to-date copy of the
1324                --  spec with all the formals referencing Designated_Type
1325                --  transformed into formals referencing Stub_Type. Since this
1326                --  primitive may have been inherited, go back the alias chain
1327                --  until the real primitive has been found.
1328
1329                Current_Primitive_Alias := Current_Primitive;
1330                while Present (Alias (Current_Primitive_Alias)) loop
1331                   pragma Assert
1332                     (Current_Primitive_Alias
1333                       /= Alias (Current_Primitive_Alias));
1334                   Current_Primitive_Alias := Alias (Current_Primitive_Alias);
1335                end loop;
1336
1337                --  Copy the spec from the original declaration for the purpose
1338                --  of declaring an overriding subprogram: we need to replace
1339                --  the type of each controlling formal with Stub_Type. The
1340                --  primitive may have been declared for Designated_Type or
1341                --  inherited from some ancestor type for which we do not have
1342                --  an easily determined Entity_Id. We have no systematic way
1343                --  of knowing which type to substitute Stub_Type for. Instead,
1344                --  Copy_Specification relies on the flag Is_Controlling_Formal
1345                --  to determine which formals to change.
1346
1347                Current_Primitive_Spec :=
1348                  Copy_Specification (Loc,
1349                    Spec        => Parent (Current_Primitive_Alias),
1350                    Ctrl_Type   => Stub_Elements.Stub_Type);
1351
1352                Current_Primitive_Decl :=
1353                  Make_Subprogram_Declaration (Loc,
1354                    Specification => Current_Primitive_Spec);
1355
1356                Insert_After_And_Analyze (Current_Insertion_Node,
1357                  Current_Primitive_Decl);
1358                Current_Insertion_Node := Current_Primitive_Decl;
1359
1360                Possibly_Asynchronous :=
1361                  Nkind (Current_Primitive_Spec) = N_Procedure_Specification
1362                  and then Could_Be_Asynchronous (Current_Primitive_Spec);
1363
1364                Assign_Subprogram_Identifier (
1365                  Defining_Unit_Name (Current_Primitive_Spec),
1366                  Current_Primitive_Number,
1367                  Subp_Str);
1368
1369                if Build_Bodies then
1370                   Current_Primitive_Body :=
1371                     Build_Subprogram_Calling_Stubs
1372                       (Vis_Decl                 => Current_Primitive_Decl,
1373                        Subp_Id                  =>
1374                          Build_Subprogram_Id (Loc,
1375                            Defining_Unit_Name (Current_Primitive_Spec)),
1376                        Asynchronous             => Possibly_Asynchronous,
1377                        Dynamically_Asynchronous => Possibly_Asynchronous,
1378                        Stub_Type                => Stub_Elements.Stub_Type,
1379                        RACW_Type                => Stub_Elements.RACW_Type);
1380                   Append_To (Body_Decls, Current_Primitive_Body);
1381
1382                   --  Analyzing the body here would cause the Stub type to
1383                   --  be frozen, thus preventing subsequent primitive
1384                   --  declarations. For this reason, it will be analyzed
1385                   --  later in the regular flow (and in the context of the
1386                   --  appropriate unit body, see Append_RACW_Bodies).
1387
1388                end if;
1389
1390                --  Build the receiver stubs
1391
1392                if Build_Bodies and then not Is_RAS then
1393                   Current_Receiver_Body :=
1394                     Specific_Build_Subprogram_Receiving_Stubs
1395                       (Vis_Decl                 => Current_Primitive_Decl,
1396                        Asynchronous             => Possibly_Asynchronous,
1397                        Dynamically_Asynchronous => Possibly_Asynchronous,
1398                        Stub_Type                => Stub_Elements.Stub_Type,
1399                        RACW_Type                => Stub_Elements.RACW_Type,
1400                        Parent_Primitive         => Current_Primitive);
1401
1402                   Current_Receiver := Defining_Unit_Name (
1403                     Specification (Current_Receiver_Body));
1404
1405                   Append_To (Body_Decls, Current_Receiver_Body);
1406
1407                   --  Add a case alternative to the receiver
1408
1409                   if Get_PCS_Name = Name_PolyORB_DSA then
1410                      Append_To (RPC_Receiver_Elsif_Parts,
1411                        Make_Elsif_Part (Loc,
1412                          Condition =>
1413                            Make_Function_Call (Loc,
1414                              Name =>
1415                                New_Occurrence_Of (
1416                                  RTE (RE_Caseless_String_Eq), Loc),
1417                              Parameter_Associations => New_List (
1418                                New_Occurrence_Of (RPC_Receiver_Subp_Id, Loc),
1419                                Make_String_Literal (Loc, Subp_Str))),
1420
1421                          Then_Statements => New_List (
1422                            Make_Assignment_Statement (Loc,
1423                              Name => New_Occurrence_Of (
1424                                        RPC_Receiver_Subp_Index, Loc),
1425                              Expression =>
1426                                Make_Integer_Literal (Loc,
1427                                   Intval => Current_Primitive_Number)))));
1428                   end if;
1429
1430                   Append_To (RPC_Receiver_Case_Alternatives,
1431                     Make_Case_Statement_Alternative (Loc,
1432                       Discrete_Choices => New_List (
1433                         Make_Integer_Literal (Loc, Current_Primitive_Number)),
1434
1435                       Statements       => New_List (
1436                         Make_Procedure_Call_Statement (Loc,
1437                           Name                   =>
1438                             New_Occurrence_Of (Current_Receiver, Loc),
1439                           Parameter_Associations => New_List (
1440                             New_Occurrence_Of (RPC_Receiver_Request, Loc))))));
1441                end if;
1442
1443                --  Increment the index of current primitive
1444
1445                Current_Primitive_Number := Current_Primitive_Number + 1;
1446             end if;
1447
1448             Next_Elmt (Current_Primitive_Elmt);
1449          end loop;
1450       end if;
1451
1452       --  Build the case statement and the heart of the subprogram
1453
1454       if Build_Bodies and then not Is_RAS then
1455          if Get_PCS_Name = Name_PolyORB_DSA
1456            and then Present (First (RPC_Receiver_Elsif_Parts))
1457          then
1458             Append_To (RPC_Receiver_Statements,
1459               Make_Implicit_If_Statement (Designated_Type,
1460                 Condition       => New_Occurrence_Of (Standard_False, Loc),
1461                 Then_Statements => New_List,
1462                 Elsif_Parts     => RPC_Receiver_Elsif_Parts));
1463          end if;
1464
1465          Append_To (RPC_Receiver_Case_Alternatives,
1466            Make_Case_Statement_Alternative (Loc,
1467              Discrete_Choices => New_List (Make_Others_Choice (Loc)),
1468              Statements       => New_List (Make_Null_Statement (Loc))));
1469
1470          Append_To (RPC_Receiver_Statements,
1471            Make_Case_Statement (Loc,
1472              Expression   =>
1473                New_Occurrence_Of (RPC_Receiver_Subp_Index, Loc),
1474              Alternatives => RPC_Receiver_Case_Alternatives));
1475
1476          Append_To (Body_Decls, RPC_Receiver_Decl);
1477          Specific_Add_Obj_RPC_Receiver_Completion (Loc,
1478            Body_Decls, RPC_Receiver, Stub_Elements);
1479
1480       --  Do not analyze RPC receiver body at this stage since it references
1481       --  subprograms that have not been analyzed yet. It will be analyzed in
1482       --  the regular flow (see Append_RACW_Bodies).
1483
1484       end if;
1485    end Add_RACW_Primitive_Declarations_And_Bodies;
1486
1487    -----------------------------
1488    -- Add_RAS_Dereference_TSS --
1489    -----------------------------
1490
1491    procedure Add_RAS_Dereference_TSS (N : Node_Id) is
1492       Loc : constant Source_Ptr := Sloc (N);
1493
1494       Type_Def  : constant Node_Id   := Type_Definition (N);
1495       RAS_Type  : constant Entity_Id := Defining_Identifier (N);
1496       Fat_Type  : constant Entity_Id := Equivalent_Type (RAS_Type);
1497       RACW_Type : constant Entity_Id := Underlying_RACW_Type (RAS_Type);
1498
1499       RACW_Primitive_Name : Node_Id;
1500
1501       Proc : constant Entity_Id :=
1502                Make_Defining_Identifier (Loc,
1503                  Chars => Make_TSS_Name (RAS_Type, TSS_RAS_Dereference));
1504
1505       Proc_Spec   : Node_Id;
1506       Param_Specs : List_Id;
1507       Param_Assoc : constant List_Id := New_List;
1508       Stmts       : constant List_Id := New_List;
1509
1510       RAS_Parameter : constant Entity_Id :=
1511                         Make_Defining_Identifier (Loc,
1512                           Chars => New_Internal_Name ('P'));
1513
1514       Is_Function : constant Boolean :=
1515                       Nkind (Type_Def) = N_Access_Function_Definition;
1516
1517       Is_Degenerate : Boolean;
1518       --  Set to True if the subprogram_specification for this RAS has an
1519       --  anonymous access parameter (see Process_Remote_AST_Declaration).
1520
1521       Spec : constant Node_Id := Type_Def;
1522
1523       Current_Parameter : Node_Id;
1524
1525    --  Start of processing for Add_RAS_Dereference_TSS
1526
1527    begin
1528       --  The Dereference TSS for a remote access-to-subprogram type has the
1529       --  form:
1530
1531       --    [function|procedure] ras_typeRD (RAS_Value, <RAS_Parameters>)
1532       --       [return <>]
1533
1534       --  This is called whenever a value of a RAS type is dereferenced
1535
1536       --  First construct a list of parameter specifications:
1537
1538       --  The first formal is the RAS values
1539
1540       Param_Specs := New_List (
1541         Make_Parameter_Specification (Loc,
1542           Defining_Identifier => RAS_Parameter,
1543           In_Present          => True,
1544           Parameter_Type      =>
1545             New_Occurrence_Of (Fat_Type, Loc)));
1546
1547       --  The following formals are copied from the type declaration
1548
1549       Is_Degenerate := False;
1550       Current_Parameter := First (Parameter_Specifications (Type_Def));
1551       Parameters : while Present (Current_Parameter) loop
1552          if Nkind (Parameter_Type (Current_Parameter)) =
1553                                             N_Access_Definition
1554          then
1555             Is_Degenerate := True;
1556          end if;
1557
1558          Append_To (Param_Specs,
1559            Make_Parameter_Specification (Loc,
1560              Defining_Identifier =>
1561                Make_Defining_Identifier (Loc,
1562                  Chars => Chars (Defining_Identifier (Current_Parameter))),
1563              In_Present        => In_Present (Current_Parameter),
1564              Out_Present       => Out_Present (Current_Parameter),
1565              Parameter_Type    =>
1566                New_Copy_Tree (Parameter_Type (Current_Parameter)),
1567              Expression        =>
1568                New_Copy_Tree (Expression (Current_Parameter))));
1569
1570          Append_To (Param_Assoc,
1571            Make_Identifier (Loc,
1572              Chars => Chars (Defining_Identifier (Current_Parameter))));
1573
1574          Next (Current_Parameter);
1575       end loop Parameters;
1576
1577       if Is_Degenerate then
1578          Prepend_To (Param_Assoc, New_Occurrence_Of (RAS_Parameter, Loc));
1579
1580          --  Generate a dummy body. This code will never actually be executed,
1581          --  because null is the only legal value for a degenerate RAS type.
1582          --  For legality's sake (in order to avoid generating a function that
1583          --  does not contain a return statement), we include a dummy recursive
1584          --  call on the TSS itself.
1585
1586          Append_To (Stmts,
1587            Make_Raise_Program_Error (Loc, Reason => PE_Explicit_Raise));
1588          RACW_Primitive_Name := New_Occurrence_Of (Proc, Loc);
1589
1590       else
1591          --  For a normal RAS type, we cast the RAS formal to the corresponding
1592          --  tagged type, and perform a dispatching call to its Call primitive
1593          --  operation.
1594
1595          Prepend_To (Param_Assoc,
1596            Unchecked_Convert_To (RACW_Type,
1597              New_Occurrence_Of (RAS_Parameter, Loc)));
1598
1599          RACW_Primitive_Name :=
1600            Make_Selected_Component (Loc,
1601              Prefix        => Scope (RACW_Type),
1602              Selector_Name => Name_uCall);
1603       end if;
1604
1605       if Is_Function then
1606          Append_To (Stmts,
1607             Make_Simple_Return_Statement (Loc,
1608               Expression =>
1609                 Make_Function_Call (Loc,
1610                   Name                   => RACW_Primitive_Name,
1611                   Parameter_Associations => Param_Assoc)));
1612
1613       else
1614          Append_To (Stmts,
1615            Make_Procedure_Call_Statement (Loc,
1616              Name                   => RACW_Primitive_Name,
1617              Parameter_Associations => Param_Assoc));
1618       end if;
1619
1620       --  Build the complete subprogram
1621
1622       if Is_Function then
1623          Proc_Spec :=
1624            Make_Function_Specification (Loc,
1625              Defining_Unit_Name       => Proc,
1626              Parameter_Specifications => Param_Specs,
1627              Result_Definition        =>
1628                New_Occurrence_Of (
1629                  Entity (Result_Definition (Spec)), Loc));
1630
1631          Set_Ekind (Proc, E_Function);
1632          Set_Etype (Proc,
1633            New_Occurrence_Of (Entity (Result_Definition (Spec)), Loc));
1634
1635       else
1636          Proc_Spec :=
1637            Make_Procedure_Specification (Loc,
1638              Defining_Unit_Name       => Proc,
1639              Parameter_Specifications => Param_Specs);
1640
1641          Set_Ekind (Proc, E_Procedure);
1642          Set_Etype (Proc, Standard_Void_Type);
1643       end if;
1644
1645       Discard_Node (
1646         Make_Subprogram_Body (Loc,
1647           Specification              => Proc_Spec,
1648           Declarations               => New_List,
1649           Handled_Statement_Sequence =>
1650             Make_Handled_Sequence_Of_Statements (Loc,
1651               Statements => Stmts)));
1652
1653       Set_TSS (Fat_Type, Proc);
1654    end Add_RAS_Dereference_TSS;
1655
1656    -------------------------------
1657    -- Add_RAS_Proxy_And_Analyze --
1658    -------------------------------
1659
1660    procedure Add_RAS_Proxy_And_Analyze
1661      (Decls              : List_Id;
1662       Vis_Decl           : Node_Id;
1663       All_Calls_Remote_E : Entity_Id;
1664       Proxy_Object_Addr  : out Entity_Id)
1665    is
1666       Loc : constant Source_Ptr := Sloc (Vis_Decl);
1667
1668       Subp_Name : constant Entity_Id :=
1669                      Defining_Unit_Name (Specification (Vis_Decl));
1670
1671       Pkg_Name : constant Entity_Id :=
1672                    Make_Defining_Identifier (Loc,
1673                      Chars => New_External_Name (Chars (Subp_Name), 'P', -1));
1674
1675       Proxy_Type : constant Entity_Id :=
1676                      Make_Defining_Identifier (Loc,
1677                        Chars =>
1678                          New_External_Name
1679                            (Related_Id => Chars (Subp_Name),
1680                             Suffix     => 'P'));
1681
1682       Proxy_Type_Full_View : constant Entity_Id :=
1683                                Make_Defining_Identifier (Loc,
1684                                  Chars (Proxy_Type));
1685
1686       Subp_Decl_Spec : constant Node_Id :=
1687                          Build_RAS_Primitive_Specification
1688                            (Subp_Spec          => Specification (Vis_Decl),
1689                             Remote_Object_Type => Proxy_Type);
1690
1691       Subp_Body_Spec : constant Node_Id :=
1692                          Build_RAS_Primitive_Specification
1693                            (Subp_Spec          => Specification (Vis_Decl),
1694                             Remote_Object_Type => Proxy_Type);
1695
1696       Vis_Decls    : constant List_Id := New_List;
1697       Pvt_Decls    : constant List_Id := New_List;
1698       Actuals      : constant List_Id := New_List;
1699       Formal       : Node_Id;
1700       Perform_Call : Node_Id;
1701
1702    begin
1703       --  type subpP is tagged limited private;
1704
1705       Append_To (Vis_Decls,
1706         Make_Private_Type_Declaration (Loc,
1707           Defining_Identifier => Proxy_Type,
1708           Tagged_Present      => True,
1709           Limited_Present     => True));
1710
1711       --  [subprogram] Call
1712       --    (Self : access subpP;
1713       --     ...other-formals...)
1714       --     [return T];
1715
1716       Append_To (Vis_Decls,
1717         Make_Subprogram_Declaration (Loc,
1718           Specification => Subp_Decl_Spec));
1719
1720       --  A : constant System.Address;
1721
1722       Proxy_Object_Addr := Make_Defining_Identifier (Loc, Name_uA);
1723
1724       Append_To (Vis_Decls,
1725         Make_Object_Declaration (Loc,
1726           Defining_Identifier => Proxy_Object_Addr,
1727           Constant_Present    => True,
1728           Object_Definition   => New_Occurrence_Of (RTE (RE_Address), Loc)));
1729
1730       --  private
1731
1732       --  type subpP is tagged limited record
1733       --     All_Calls_Remote : Boolean := [All_Calls_Remote?];
1734       --     ...
1735       --  end record;
1736
1737       Append_To (Pvt_Decls,
1738         Make_Full_Type_Declaration (Loc,
1739           Defining_Identifier => Proxy_Type_Full_View,
1740           Type_Definition     =>
1741             Build_Remote_Subprogram_Proxy_Type (Loc,
1742               New_Occurrence_Of (All_Calls_Remote_E, Loc))));
1743
1744       --  Trick semantic analysis into swapping the public and full view when
1745       --  freezing the public view.
1746
1747       Set_Comes_From_Source (Proxy_Type_Full_View, True);
1748
1749       --  procedure Call
1750       --    (Self : access O;
1751       --     ...other-formals...) is
1752       --  begin
1753       --    P (...other-formals...);
1754       --  end Call;
1755
1756       --  function Call
1757       --    (Self : access O;
1758       --     ...other-formals...)
1759       --     return T is
1760       --  begin
1761       --    return F (...other-formals...);
1762       --  end Call;
1763
1764       if Nkind (Subp_Decl_Spec) = N_Procedure_Specification then
1765          Perform_Call :=
1766            Make_Procedure_Call_Statement (Loc,
1767              Name                   => New_Occurrence_Of (Subp_Name, Loc),
1768              Parameter_Associations => Actuals);
1769       else
1770          Perform_Call :=
1771            Make_Simple_Return_Statement (Loc,
1772              Expression =>
1773                Make_Function_Call (Loc,
1774                  Name                   => New_Occurrence_Of (Subp_Name, Loc),
1775                  Parameter_Associations => Actuals));
1776       end if;
1777
1778       Formal := First (Parameter_Specifications (Subp_Decl_Spec));
1779       pragma Assert (Present (Formal));
1780       loop
1781          Next (Formal);
1782          exit when No (Formal);
1783          Append_To (Actuals,
1784            New_Occurrence_Of (Defining_Identifier (Formal), Loc));
1785       end loop;
1786
1787       --  O : aliased subpP;
1788
1789       Append_To (Pvt_Decls,
1790         Make_Object_Declaration (Loc,
1791           Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
1792           Aliased_Present     => True,
1793           Object_Definition   => New_Occurrence_Of (Proxy_Type, Loc)));
1794
1795       --  A : constant System.Address := O'Address;
1796
1797       Append_To (Pvt_Decls,
1798         Make_Object_Declaration (Loc,
1799           Defining_Identifier =>
1800             Make_Defining_Identifier (Loc, Chars (Proxy_Object_Addr)),
1801           Constant_Present    => True,
1802           Object_Definition   => New_Occurrence_Of (RTE (RE_Address), Loc),
1803           Expression =>
1804             Make_Attribute_Reference (Loc,
1805               Prefix => New_Occurrence_Of (
1806                 Defining_Identifier (Last (Pvt_Decls)), Loc),
1807               Attribute_Name => Name_Address)));
1808
1809       Append_To (Decls,
1810         Make_Package_Declaration (Loc,
1811           Specification => Make_Package_Specification (Loc,
1812             Defining_Unit_Name   => Pkg_Name,
1813             Visible_Declarations => Vis_Decls,
1814             Private_Declarations => Pvt_Decls,
1815             End_Label            => Empty)));
1816       Analyze (Last (Decls));
1817
1818       Append_To (Decls,
1819         Make_Package_Body (Loc,
1820           Defining_Unit_Name =>
1821             Make_Defining_Identifier (Loc, Chars (Pkg_Name)),
1822           Declarations => New_List (
1823             Make_Subprogram_Body (Loc,
1824               Specification  => Subp_Body_Spec,
1825               Declarations   => New_List,
1826               Handled_Statement_Sequence =>
1827                 Make_Handled_Sequence_Of_Statements (Loc,
1828                   Statements => New_List (Perform_Call))))));
1829       Analyze (Last (Decls));
1830    end Add_RAS_Proxy_And_Analyze;
1831
1832    -----------------------
1833    -- Add_RAST_Features --
1834    -----------------------
1835
1836    procedure Add_RAST_Features (Vis_Decl : Node_Id) is
1837       RAS_Type : constant Entity_Id :=
1838                    Equivalent_Type (Defining_Identifier (Vis_Decl));
1839    begin
1840       pragma Assert (No (TSS (RAS_Type, TSS_RAS_Access)));
1841       Add_RAS_Dereference_TSS (Vis_Decl);
1842       Specific_Add_RAST_Features (Vis_Decl, RAS_Type);
1843    end Add_RAST_Features;
1844
1845    -------------------
1846    -- Add_Stub_Type --
1847    -------------------
1848
1849    procedure Add_Stub_Type
1850      (Designated_Type   : Entity_Id;
1851       RACW_Type         : Entity_Id;
1852       Decls             : List_Id;
1853       Stub_Type         : out Entity_Id;
1854       Stub_Type_Access  : out Entity_Id;
1855       RPC_Receiver_Decl : out Node_Id;
1856       Body_Decls        : out List_Id;
1857       Existing          : out Boolean)
1858    is
1859       Loc : constant Source_Ptr := Sloc (RACW_Type);
1860
1861       Stub_Elements : constant Stub_Structure :=
1862                         Stubs_Table.Get (Designated_Type);
1863       Stub_Type_Decl        : Node_Id;
1864       Stub_Type_Access_Decl : Node_Id;
1865
1866    begin
1867       if Stub_Elements /= Empty_Stub_Structure then
1868          Stub_Type           := Stub_Elements.Stub_Type;
1869          Stub_Type_Access    := Stub_Elements.Stub_Type_Access;
1870          RPC_Receiver_Decl   := Stub_Elements.RPC_Receiver_Decl;
1871          Body_Decls          := Stub_Elements.Body_Decls;
1872          Existing            := True;
1873          return;
1874       end if;
1875
1876       Existing := False;
1877       Stub_Type :=
1878         Make_Defining_Identifier (Loc,
1879           Chars => New_Internal_Name ('S'));
1880       Set_Ekind (Stub_Type, E_Record_Type);
1881       Set_Is_RACW_Stub_Type (Stub_Type);
1882       Stub_Type_Access :=
1883         Make_Defining_Identifier (Loc,
1884           Chars => New_External_Name
1885                      (Related_Id => Chars (Stub_Type), Suffix => 'A'));
1886
1887       Specific_Build_Stub_Type
1888         (RACW_Type, Stub_Type,
1889          Stub_Type_Decl, RPC_Receiver_Decl);
1890
1891       Stub_Type_Access_Decl :=
1892         Make_Full_Type_Declaration (Loc,
1893           Defining_Identifier => Stub_Type_Access,
1894           Type_Definition     =>
1895             Make_Access_To_Object_Definition (Loc,
1896               All_Present        => True,
1897               Subtype_Indication => New_Occurrence_Of (Stub_Type, Loc)));
1898
1899       Append_To (Decls, Stub_Type_Decl);
1900       Analyze (Last (Decls));
1901       Append_To (Decls, Stub_Type_Access_Decl);
1902       Analyze (Last (Decls));
1903
1904       --  This is in no way a type derivation, but we fake it to make sure that
1905       --  the dispatching table gets built with the corresponding primitive
1906       --  operations at the right place.
1907
1908       Derive_Subprograms (Parent_Type  => Designated_Type,
1909                           Derived_Type => Stub_Type);
1910
1911       if Present (RPC_Receiver_Decl) then
1912          Append_To (Decls, RPC_Receiver_Decl);
1913       else
1914          RPC_Receiver_Decl := Last (Decls);
1915       end if;
1916
1917       Body_Decls := New_List;
1918
1919       Stubs_Table.Set (Designated_Type,
1920         (Stub_Type           => Stub_Type,
1921          Stub_Type_Access    => Stub_Type_Access,
1922          RPC_Receiver_Decl   => RPC_Receiver_Decl,
1923          Body_Decls          => Body_Decls,
1924          RACW_Type           => RACW_Type));
1925    end Add_Stub_Type;
1926
1927    ------------------------
1928    -- Append_RACW_Bodies --
1929    ------------------------
1930
1931    procedure Append_RACW_Bodies (Decls : List_Id; Spec_Id : Entity_Id) is
1932       E : Entity_Id;
1933    begin
1934       E := First_Entity (Spec_Id);
1935       while Present (E) loop
1936          if Is_Remote_Access_To_Class_Wide_Type (E) then
1937             Append_List_To (Decls, Get_And_Reset_RACW_Bodies (E));
1938          end if;
1939
1940          Next_Entity (E);
1941       end loop;
1942    end Append_RACW_Bodies;
1943
1944    ----------------------------------
1945    -- Assign_Subprogram_Identifier --
1946    ----------------------------------
1947
1948    procedure Assign_Subprogram_Identifier
1949      (Def : Entity_Id;
1950       Spn : Int;
1951       Id  : out String_Id)
1952    is
1953       N : constant Name_Id := Chars (Def);
1954
1955       Overload_Order : constant Int :=
1956                          Overload_Counter_Table.Get (N) + 1;
1957
1958    begin
1959       Overload_Counter_Table.Set (N, Overload_Order);
1960
1961       Get_Name_String (N);
1962
1963       --  Homonym handling: as in Exp_Dbug, but much simpler,
1964       --  because the only entities for which we have to generate
1965       --  names here need only to be disambiguated within their
1966       --  own scope.
1967
1968       if Overload_Order > 1 then
1969          Name_Buffer (Name_Len + 1 .. Name_Len + 2) := "__";
1970          Name_Len := Name_Len + 2;
1971          Add_Nat_To_Name_Buffer (Overload_Order);
1972       end if;
1973
1974       Id := String_From_Name_Buffer;
1975       Subprogram_Identifier_Table.Set (Def,
1976         Subprogram_Identifiers'(Str_Identifier => Id, Int_Identifier => Spn));
1977    end Assign_Subprogram_Identifier;
1978
1979    -------------------------------------
1980    -- Build_Actual_Object_Declaration --
1981    -------------------------------------
1982
1983    procedure Build_Actual_Object_Declaration
1984      (Object   : Entity_Id;
1985       Etyp     : Entity_Id;
1986       Variable : Boolean;
1987       Expr     : Node_Id;
1988       Decls    : List_Id)
1989    is
1990       Loc : constant Source_Ptr := Sloc (Object);
1991    begin
1992       --  Declare a temporary object for the actual, possibly initialized with
1993       --  a 'Input/From_Any call.
1994
1995       --  Complication arises in the case of limited types, for which such a
1996       --  declaration is illegal in Ada 95. In that case, we first generate a
1997       --  renaming declaration of the 'Input call, and then if needed we
1998       --  generate an overlaid non-constant view.
1999
2000       if Ada_Version <= Ada_95
2001         and then Is_Limited_Type (Etyp)
2002         and then Present (Expr)
2003       then
2004
2005          --  Object : Etyp renames <func-call>
2006
2007          Append_To (Decls,
2008            Make_Object_Renaming_Declaration (Loc,
2009              Defining_Identifier => Object,
2010              Subtype_Mark        => New_Occurrence_Of (Etyp, Loc),
2011              Name                => Expr));
2012
2013          if Variable then
2014
2015             --  The name defined by the renaming declaration denotes a
2016             --  constant view; create a non-constant object at the same address
2017             --  to be used as the actual.
2018
2019             declare
2020                Constant_Object : constant Entity_Id :=
2021                                    Make_Defining_Identifier (Loc,
2022                                      New_Internal_Name ('P'));
2023             begin
2024                Set_Defining_Identifier
2025                  (Last (Decls), Constant_Object);
2026
2027                --  We have an unconstrained Etyp: build the actual constrained
2028                --  subtype for the value we just read from the stream.
2029
2030                --  subtype S is <actual subtype of Constant_Object>;
2031
2032                Append_To (Decls,
2033                  Build_Actual_Subtype (Etyp,
2034                    New_Occurrence_Of (Constant_Object, Loc)));
2035
2036                --  Object : S;
2037
2038                Append_To (Decls,
2039                  Make_Object_Declaration (Loc,
2040                    Defining_Identifier => Object,
2041                    Object_Definition   =>
2042                      New_Occurrence_Of
2043                        (Defining_Identifier (Last (Decls)), Loc)));
2044                Set_Ekind (Object, E_Variable);
2045
2046                --  Suppress default initialization:
2047                --  pragma Import (Ada, Object);
2048
2049                Append_To (Decls,
2050                  Make_Pragma (Loc,
2051                    Chars => Name_Import,
2052                    Pragma_Argument_Associations => New_List (
2053                      Make_Pragma_Argument_Association (Loc,
2054                        Chars      => Name_Convention,
2055                        Expression => Make_Identifier (Loc, Name_Ada)),
2056                      Make_Pragma_Argument_Association (Loc,
2057                        Chars      => Name_Entity,
2058                        Expression => New_Occurrence_Of (Object, Loc)))));
2059
2060                --  for Object'Address use Constant_Object'Address;
2061
2062                Append_To (Decls,
2063                  Make_Attribute_Definition_Clause (Loc,
2064                    Name       => New_Occurrence_Of (Object, Loc),
2065                    Chars      => Name_Address,
2066                    Expression =>
2067                      Make_Attribute_Reference (Loc,
2068                        Prefix => New_Occurrence_Of (Constant_Object, Loc),
2069                        Attribute_Name => Name_Address)));
2070             end;
2071          end if;
2072
2073       else
2074
2075          --  General case of a regular object declaration. Object is flagged
2076          --  constant unless it has mode out or in out, to allow the backend
2077          --  to optimize where possible.
2078
2079          --  Object : [constant] Etyp [:= <expr>];
2080
2081          Append_To (Decls,
2082            Make_Object_Declaration (Loc,
2083              Defining_Identifier => Object,
2084              Constant_Present    => Present (Expr) and then not Variable,
2085              Object_Definition   => New_Occurrence_Of (Etyp, Loc),
2086              Expression          => Expr));
2087
2088          if Constant_Present (Last (Decls)) then
2089             Set_Ekind (Object, E_Constant);
2090          else
2091             Set_Ekind (Object, E_Variable);
2092          end if;
2093       end if;
2094    end Build_Actual_Object_Declaration;
2095
2096    ------------------------------
2097    -- Build_Get_Unique_RP_Call --
2098    ------------------------------
2099
2100    function Build_Get_Unique_RP_Call
2101      (Loc       : Source_Ptr;
2102       Pointer   : Entity_Id;
2103       Stub_Type : Entity_Id) return List_Id
2104    is
2105    begin
2106       return New_List (
2107         Make_Procedure_Call_Statement (Loc,
2108           Name                   =>
2109             New_Occurrence_Of (RTE (RE_Get_Unique_Remote_Pointer), Loc),
2110           Parameter_Associations => New_List (
2111             Unchecked_Convert_To (RTE (RE_RACW_Stub_Type_Access),
2112               New_Occurrence_Of (Pointer, Loc)))),
2113
2114         Make_Assignment_Statement (Loc,
2115           Name =>
2116             Make_Selected_Component (Loc,
2117               Prefix => New_Occurrence_Of (Pointer, Loc),
2118               Selector_Name =>
2119                 New_Occurrence_Of (First_Tag_Component
2120                   (Designated_Type (Etype (Pointer))), Loc)),
2121           Expression =>
2122             Make_Attribute_Reference (Loc,
2123               Prefix         => New_Occurrence_Of (Stub_Type, Loc),
2124               Attribute_Name => Name_Tag)));
2125
2126       --  Note: The assignment to Pointer._Tag is safe here because
2127       --  we carefully ensured that Stub_Type has exactly the same layout
2128       --  as System.Partition_Interface.RACW_Stub_Type.
2129
2130    end Build_Get_Unique_RP_Call;
2131
2132    -----------------------------------
2133    -- Build_Ordered_Parameters_List --
2134    -----------------------------------
2135
2136    function Build_Ordered_Parameters_List (Spec : Node_Id) return List_Id is
2137       Constrained_List   : List_Id;
2138       Unconstrained_List : List_Id;
2139       Current_Parameter  : Node_Id;
2140       Ptyp               : Node_Id;
2141
2142       First_Parameter : Node_Id;
2143       For_RAS         : Boolean := False;
2144
2145    begin
2146       if No (Parameter_Specifications (Spec)) then
2147          return New_List;
2148       end if;
2149
2150       Constrained_List   := New_List;
2151       Unconstrained_List := New_List;
2152       First_Parameter    := First (Parameter_Specifications (Spec));
2153
2154       if Nkind (Parameter_Type (First_Parameter)) = N_Access_Definition
2155         and then Chars (Defining_Identifier (First_Parameter)) = Name_uS
2156       then
2157          For_RAS := True;
2158       end if;
2159
2160       --  Loop through the parameters and add them to the right list. Note that
2161       --  we treat a parameter of a null-excluding access type as unconstrained
2162       --  because we can't declare an object of such a type with default
2163       --  initialization.
2164
2165       Current_Parameter := First_Parameter;
2166       while Present (Current_Parameter) loop
2167          Ptyp := Parameter_Type (Current_Parameter);
2168
2169          if (Nkind (Ptyp) = N_Access_Definition
2170                or else not Transmit_As_Unconstrained (Etype (Ptyp)))
2171            and then not (For_RAS and then Current_Parameter = First_Parameter)
2172          then
2173             Append_To (Constrained_List, New_Copy (Current_Parameter));
2174          else
2175             Append_To (Unconstrained_List, New_Copy (Current_Parameter));
2176          end if;
2177
2178          Next (Current_Parameter);
2179       end loop;
2180
2181       --  Unconstrained parameters are returned first
2182
2183       Append_List_To (Unconstrained_List, Constrained_List);
2184
2185       return Unconstrained_List;
2186    end Build_Ordered_Parameters_List;
2187
2188    ----------------------------------
2189    -- Build_Passive_Partition_Stub --
2190    ----------------------------------
2191
2192    procedure Build_Passive_Partition_Stub (U : Node_Id) is
2193       Pkg_Spec : Node_Id;
2194       Pkg_Name : String_Id;
2195       L        : List_Id;
2196       Reg      : Node_Id;
2197       Loc      : constant Source_Ptr := Sloc (U);
2198
2199    begin
2200       --  Verify that the implementation supports distribution, by accessing
2201       --  a type defined in the proper version of system.rpc
2202
2203       declare
2204          Dist_OK : Entity_Id;
2205          pragma Warnings (Off, Dist_OK);
2206       begin
2207          Dist_OK := RTE (RE_Params_Stream_Type);
2208       end;
2209
2210       --  Use body if present, spec otherwise
2211
2212       if Nkind (U) = N_Package_Declaration then
2213          Pkg_Spec := Specification (U);
2214          L := Visible_Declarations (Pkg_Spec);
2215       else
2216          Pkg_Spec := Parent (Corresponding_Spec (U));
2217          L := Declarations (U);
2218       end if;
2219
2220       Get_Library_Unit_Name_String (Pkg_Spec);
2221       Pkg_Name := String_From_Name_Buffer;
2222       Reg :=
2223         Make_Procedure_Call_Statement (Loc,
2224           Name                   =>
2225             New_Occurrence_Of (RTE (RE_Register_Passive_Package), Loc),
2226           Parameter_Associations => New_List (
2227             Make_String_Literal (Loc, Pkg_Name),
2228             Make_Attribute_Reference (Loc,
2229               Prefix         =>
2230                 New_Occurrence_Of (Defining_Entity (Pkg_Spec), Loc),
2231               Attribute_Name => Name_Version)));
2232       Append_To (L, Reg);
2233       Analyze (Reg);
2234    end Build_Passive_Partition_Stub;
2235
2236    --------------------------------------
2237    -- Build_RPC_Receiver_Specification --
2238    --------------------------------------
2239
2240    function Build_RPC_Receiver_Specification
2241      (RPC_Receiver      : Entity_Id;
2242       Request_Parameter : Entity_Id) return Node_Id
2243    is
2244       Loc : constant Source_Ptr := Sloc (RPC_Receiver);
2245    begin
2246       return
2247         Make_Procedure_Specification (Loc,
2248           Defining_Unit_Name       => RPC_Receiver,
2249           Parameter_Specifications => New_List (
2250             Make_Parameter_Specification (Loc,
2251               Defining_Identifier => Request_Parameter,
2252               Parameter_Type      =>
2253                 New_Occurrence_Of (RTE (RE_Request_Access), Loc))));
2254    end Build_RPC_Receiver_Specification;
2255
2256    ----------------------------------------
2257    -- Build_Remote_Subprogram_Proxy_Type --
2258    ----------------------------------------
2259
2260    function Build_Remote_Subprogram_Proxy_Type
2261      (Loc            : Source_Ptr;
2262       ACR_Expression : Node_Id) return Node_Id
2263    is
2264    begin
2265       return
2266         Make_Record_Definition (Loc,
2267           Tagged_Present  => True,
2268           Limited_Present => True,
2269           Component_List  =>
2270             Make_Component_List (Loc,
2271
2272               Component_Items => New_List (
2273                 Make_Component_Declaration (Loc,
2274                   Defining_Identifier =>
2275                     Make_Defining_Identifier (Loc,
2276                       Name_All_Calls_Remote),
2277                   Component_Definition =>
2278                     Make_Component_Definition (Loc,
2279                       Subtype_Indication =>
2280                         New_Occurrence_Of (Standard_Boolean, Loc)),
2281                   Expression =>
2282                     ACR_Expression),
2283
2284                 Make_Component_Declaration (Loc,
2285                   Defining_Identifier =>
2286                     Make_Defining_Identifier (Loc,
2287                       Name_Receiver),
2288                   Component_Definition =>
2289                     Make_Component_Definition (Loc,
2290                       Subtype_Indication =>
2291                         New_Occurrence_Of (RTE (RE_Address), Loc)),
2292                   Expression =>
2293                     New_Occurrence_Of (RTE (RE_Null_Address), Loc)),
2294
2295                 Make_Component_Declaration (Loc,
2296                   Defining_Identifier =>
2297                     Make_Defining_Identifier (Loc,
2298                       Name_Subp_Id),
2299                   Component_Definition =>
2300                     Make_Component_Definition (Loc,
2301                       Subtype_Indication =>
2302                         New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc))))));
2303    end Build_Remote_Subprogram_Proxy_Type;
2304
2305    --------------------
2306    -- Build_Stub_Tag --
2307    --------------------
2308
2309    function Build_Stub_Tag
2310      (Loc       : Source_Ptr;
2311       RACW_Type : Entity_Id) return Node_Id
2312    is
2313       Stub_Type : constant Entity_Id := Corresponding_Stub_Type (RACW_Type);
2314    begin
2315       return
2316         Make_Attribute_Reference (Loc,
2317           Prefix         => New_Occurrence_Of (Stub_Type, Loc),
2318           Attribute_Name => Name_Tag);
2319    end Build_Stub_Tag;
2320
2321    ------------------------------------
2322    -- Build_Subprogram_Calling_Stubs --
2323    ------------------------------------
2324
2325    function Build_Subprogram_Calling_Stubs
2326      (Vis_Decl                 : Node_Id;
2327       Subp_Id                  : Node_Id;
2328       Asynchronous             : Boolean;
2329       Dynamically_Asynchronous : Boolean   := False;
2330       Stub_Type                : Entity_Id := Empty;
2331       RACW_Type                : Entity_Id := Empty;
2332       Locator                  : Entity_Id := Empty;
2333       New_Name                 : Name_Id   := No_Name) return Node_Id
2334    is
2335       Loc : constant Source_Ptr := Sloc (Vis_Decl);
2336
2337       Decls      : constant List_Id := New_List;
2338       Statements : constant List_Id := New_List;
2339
2340       Subp_Spec : Node_Id;
2341       --  The specification of the body
2342
2343       Controlling_Parameter : Entity_Id := Empty;
2344
2345       Asynchronous_Expr : Node_Id := Empty;
2346
2347       RCI_Locator : Entity_Id;
2348
2349       Spec_To_Use : Node_Id;
2350
2351       procedure Insert_Partition_Check (Parameter : Node_Id);
2352       --  Check that the parameter has been elaborated on the same partition
2353       --  than the controlling parameter (E.4(19)).
2354
2355       ----------------------------
2356       -- Insert_Partition_Check --
2357       ----------------------------
2358
2359       procedure Insert_Partition_Check (Parameter : Node_Id) is
2360          Parameter_Entity : constant Entity_Id :=
2361                               Defining_Identifier (Parameter);
2362       begin
2363          --  The expression that will be built is of the form:
2364
2365          --    if not Same_Partition (Parameter, Controlling_Parameter) then
2366          --      raise Constraint_Error;
2367          --    end if;
2368
2369          --  We do not check that Parameter is in Stub_Type since such a check
2370          --  has been inserted at the point of call already (a tag check since
2371          --  we have multiple controlling operands).
2372
2373          Append_To (Decls,
2374            Make_Raise_Constraint_Error (Loc,
2375              Condition       =>
2376                Make_Op_Not (Loc,
2377                  Right_Opnd =>
2378                    Make_Function_Call (Loc,
2379                      Name =>
2380                        New_Occurrence_Of (RTE (RE_Same_Partition), Loc),
2381                      Parameter_Associations =>
2382                        New_List (
2383                          Unchecked_Convert_To (RTE (RE_RACW_Stub_Type_Access),
2384                            New_Occurrence_Of (Parameter_Entity, Loc)),
2385                          Unchecked_Convert_To (RTE (RE_RACW_Stub_Type_Access),
2386                            New_Occurrence_Of (Controlling_Parameter, Loc))))),
2387              Reason => CE_Partition_Check_Failed));
2388       end Insert_Partition_Check;
2389
2390    --  Start of processing for Build_Subprogram_Calling_Stubs
2391
2392    begin
2393       Subp_Spec := Copy_Specification (Loc,
2394         Spec     => Specification (Vis_Decl),
2395         New_Name => New_Name);
2396
2397       if Locator = Empty then
2398          RCI_Locator := RCI_Cache;
2399          Spec_To_Use := Specification (Vis_Decl);
2400       else
2401          RCI_Locator := Locator;
2402          Spec_To_Use := Subp_Spec;
2403       end if;
2404
2405       --  Find a controlling argument if we have a stub type. Also check
2406       --  if this subprogram can be made asynchronous.
2407
2408       if Present (Stub_Type)
2409          and then Present (Parameter_Specifications (Spec_To_Use))
2410       then
2411          declare
2412             Current_Parameter : Node_Id :=
2413                                   First (Parameter_Specifications
2414                                            (Spec_To_Use));
2415          begin
2416             while Present (Current_Parameter) loop
2417                if
2418                  Is_RACW_Controlling_Formal (Current_Parameter, Stub_Type)
2419                then
2420                   if Controlling_Parameter = Empty then
2421                      Controlling_Parameter :=
2422                        Defining_Identifier (Current_Parameter);
2423                   else
2424                      Insert_Partition_Check (Current_Parameter);
2425                   end if;
2426                end if;
2427
2428                Next (Current_Parameter);
2429             end loop;
2430          end;
2431       end if;
2432
2433       pragma Assert (No (Stub_Type) or else Present (Controlling_Parameter));
2434
2435       if Dynamically_Asynchronous then
2436          Asynchronous_Expr := Make_Selected_Component (Loc,
2437                                 Prefix        => Controlling_Parameter,
2438                                 Selector_Name => Name_Asynchronous);
2439       end if;
2440
2441       Specific_Build_General_Calling_Stubs
2442         (Decls                 => Decls,
2443          Statements            => Statements,
2444          Target                => Specific_Build_Stub_Target (Loc,
2445                                     Decls, RCI_Locator, Controlling_Parameter),
2446          Subprogram_Id         => Subp_Id,
2447          Asynchronous          => Asynchronous_Expr,
2448          Is_Known_Asynchronous => Asynchronous
2449                                     and then not Dynamically_Asynchronous,
2450          Is_Known_Non_Asynchronous
2451                                => not Asynchronous
2452                                     and then not Dynamically_Asynchronous,
2453          Is_Function           => Nkind (Spec_To_Use) =
2454                                     N_Function_Specification,
2455          Spec                  => Spec_To_Use,
2456          Stub_Type             => Stub_Type,
2457          RACW_Type             => RACW_Type,
2458          Nod                   => Vis_Decl);
2459
2460       RCI_Calling_Stubs_Table.Set
2461         (Defining_Unit_Name (Specification (Vis_Decl)),
2462          Defining_Unit_Name (Spec_To_Use));
2463
2464       return
2465         Make_Subprogram_Body (Loc,
2466           Specification              => Subp_Spec,
2467           Declarations               => Decls,
2468           Handled_Statement_Sequence =>
2469             Make_Handled_Sequence_Of_Statements (Loc, Statements));
2470    end Build_Subprogram_Calling_Stubs;
2471
2472    -------------------------
2473    -- Build_Subprogram_Id --
2474    -------------------------
2475
2476    function Build_Subprogram_Id
2477      (Loc : Source_Ptr;
2478       E   : Entity_Id) return Node_Id
2479    is
2480    begin
2481       if Get_Subprogram_Ids (E).Str_Identifier = No_String then
2482          declare
2483             Current_Declaration : Node_Id;
2484             Current_Subp        : Entity_Id;
2485             Current_Subp_Str    : String_Id;
2486             Current_Subp_Number : Int := First_RCI_Subprogram_Id;
2487
2488             pragma Warnings (Off, Current_Subp_Str);
2489
2490          begin
2491             --  Build_Subprogram_Id is called outside of the context of
2492             --  generating calling or receiving stubs. Hence we are processing
2493             --  an 'Access attribute_reference for an RCI subprogram, for the
2494             --  purpose of obtaining a RAS value.
2495
2496             pragma Assert
2497               (Is_Remote_Call_Interface (Scope (E))
2498                  and then
2499                   (Nkind (Parent (E)) = N_Procedure_Specification
2500                      or else
2501                    Nkind (Parent (E)) = N_Function_Specification));
2502
2503             Current_Declaration :=
2504               First (Visible_Declarations
2505                 (Package_Specification_Of_Scope (Scope (E))));
2506             while Present (Current_Declaration) loop
2507                if Nkind (Current_Declaration) = N_Subprogram_Declaration
2508                  and then Comes_From_Source (Current_Declaration)
2509                then
2510                   Current_Subp := Defining_Unit_Name (Specification (
2511                     Current_Declaration));
2512
2513                   Assign_Subprogram_Identifier
2514                     (Current_Subp, Current_Subp_Number, Current_Subp_Str);
2515
2516                   Current_Subp_Number := Current_Subp_Number + 1;
2517                end if;
2518
2519                Next (Current_Declaration);
2520             end loop;
2521          end;
2522       end if;
2523
2524       case Get_PCS_Name is
2525          when Name_PolyORB_DSA =>
2526             return Make_String_Literal (Loc, Get_Subprogram_Id (E));
2527          when others =>
2528             return Make_Integer_Literal (Loc, Get_Subprogram_Id (E));
2529       end case;
2530    end Build_Subprogram_Id;
2531
2532    ------------------------
2533    -- Copy_Specification --
2534    ------------------------
2535
2536    function Copy_Specification
2537      (Loc         : Source_Ptr;
2538       Spec        : Node_Id;
2539       Ctrl_Type   : Entity_Id := Empty;
2540       New_Name    : Name_Id   := No_Name) return Node_Id
2541    is
2542       Parameters : List_Id := No_List;
2543
2544       Current_Parameter  : Node_Id;
2545       Current_Identifier : Entity_Id;
2546       Current_Type       : Node_Id;
2547
2548       Name_For_New_Spec : Name_Id;
2549
2550       New_Identifier : Entity_Id;
2551
2552    --  Comments needed in body below ???
2553
2554    begin
2555       if New_Name = No_Name then
2556          pragma Assert (Nkind (Spec) = N_Function_Specification
2557                 or else Nkind (Spec) = N_Procedure_Specification);
2558
2559          Name_For_New_Spec := Chars (Defining_Unit_Name (Spec));
2560       else
2561          Name_For_New_Spec := New_Name;
2562       end if;
2563
2564       if Present (Parameter_Specifications (Spec)) then
2565          Parameters        := New_List;
2566          Current_Parameter := First (Parameter_Specifications (Spec));
2567          while Present (Current_Parameter) loop
2568             Current_Identifier := Defining_Identifier (Current_Parameter);
2569             Current_Type       := Parameter_Type (Current_Parameter);
2570
2571             if Nkind (Current_Type) = N_Access_Definition then
2572                if Present (Ctrl_Type) then
2573                   pragma Assert (Is_Controlling_Formal (Current_Identifier));
2574                   Current_Type :=
2575                     Make_Access_Definition (Loc,
2576                       Subtype_Mark => New_Occurrence_Of (Ctrl_Type, Loc),
2577                       Null_Exclusion_Present =>
2578                         Null_Exclusion_Present (Current_Type));
2579
2580                else
2581                   Current_Type :=
2582                     Make_Access_Definition (Loc,
2583                       Subtype_Mark =>
2584                         New_Copy_Tree (Subtype_Mark (Current_Type)),
2585                       Null_Exclusion_Present =>
2586                         Null_Exclusion_Present (Current_Type));
2587                end if;
2588
2589             else
2590                if Present (Ctrl_Type)
2591                  and then Is_Controlling_Formal (Current_Identifier)
2592                then
2593                   Current_Type := New_Occurrence_Of (Ctrl_Type, Loc);
2594                else
2595                   Current_Type := New_Copy_Tree (Current_Type);
2596                end if;
2597             end if;
2598
2599             New_Identifier := Make_Defining_Identifier (Loc,
2600               Chars (Current_Identifier));
2601
2602             Append_To (Parameters,
2603               Make_Parameter_Specification (Loc,
2604                 Defining_Identifier => New_Identifier,
2605                 Parameter_Type      => Current_Type,
2606                 In_Present          => In_Present (Current_Parameter),
2607                 Out_Present         => Out_Present (Current_Parameter),
2608                 Expression          =>
2609                   New_Copy_Tree (Expression (Current_Parameter))));
2610
2611             --  For a regular formal parameter (that needs to be marshalled
2612             --  in the context of remote calls), set the Etype now, because
2613             --  marshalling processing might need it.
2614
2615             if Is_Entity_Name (Current_Type) then
2616                Set_Etype (New_Identifier, Entity (Current_Type));
2617
2618             --  Current_Type is an access definition, special processing
2619             --  (not requiring etype) will occur for marshalling.
2620
2621             else
2622                null;
2623             end if;
2624
2625             Next (Current_Parameter);
2626          end loop;
2627       end if;
2628
2629       case Nkind (Spec) is
2630
2631          when N_Function_Specification | N_Access_Function_Definition =>
2632             return
2633               Make_Function_Specification (Loc,
2634                 Defining_Unit_Name       =>
2635                   Make_Defining_Identifier (Loc,
2636                     Chars => Name_For_New_Spec),
2637                 Parameter_Specifications => Parameters,
2638                 Result_Definition        =>
2639                   New_Occurrence_Of (Entity (Result_Definition (Spec)), Loc));
2640
2641          when N_Procedure_Specification | N_Access_Procedure_Definition =>
2642             return
2643               Make_Procedure_Specification (Loc,
2644                 Defining_Unit_Name       =>
2645                   Make_Defining_Identifier (Loc,
2646                     Chars => Name_For_New_Spec),
2647                 Parameter_Specifications => Parameters);
2648
2649          when others =>
2650             raise Program_Error;
2651       end case;
2652    end Copy_Specification;
2653
2654    -----------------------------
2655    -- Corresponding_Stub_Type --
2656    -----------------------------
2657
2658    function Corresponding_Stub_Type (RACW_Type : Entity_Id) return Entity_Id is
2659       Desig         : constant Entity_Id      :=
2660                         Etype (Designated_Type (RACW_Type));
2661       Stub_Elements : constant Stub_Structure := Stubs_Table.Get (Desig);
2662    begin
2663       return Stub_Elements.Stub_Type;
2664    end Corresponding_Stub_Type;
2665
2666    ---------------------------
2667    -- Could_Be_Asynchronous --
2668    ---------------------------
2669
2670    function Could_Be_Asynchronous (Spec : Node_Id) return Boolean is
2671       Current_Parameter : Node_Id;
2672
2673    begin
2674       if Present (Parameter_Specifications (Spec)) then
2675          Current_Parameter := First (Parameter_Specifications (Spec));
2676          while Present (Current_Parameter) loop
2677             if Out_Present (Current_Parameter) then
2678                return False;
2679             end if;
2680
2681             Next (Current_Parameter);
2682          end loop;
2683       end if;
2684
2685       return True;
2686    end Could_Be_Asynchronous;
2687
2688    ---------------------------
2689    -- Declare_Create_NVList --
2690    ---------------------------
2691
2692    procedure Declare_Create_NVList
2693      (Loc    : Source_Ptr;
2694       NVList : Entity_Id;
2695       Decls  : List_Id;
2696       Stmts  : List_Id)
2697    is
2698    begin
2699       Append_To (Decls,
2700         Make_Object_Declaration (Loc,
2701           Defining_Identifier => NVList,
2702           Aliased_Present     => False,
2703           Object_Definition   =>
2704               New_Occurrence_Of (RTE (RE_NVList_Ref), Loc)));
2705
2706       Append_To (Stmts,
2707         Make_Procedure_Call_Statement (Loc,
2708           Name => New_Occurrence_Of (RTE (RE_NVList_Create), Loc),
2709           Parameter_Associations => New_List (
2710             New_Occurrence_Of (NVList, Loc))));
2711    end Declare_Create_NVList;
2712
2713    ---------------------------------------------
2714    -- Expand_All_Calls_Remote_Subprogram_Call --
2715    ---------------------------------------------
2716
2717    procedure Expand_All_Calls_Remote_Subprogram_Call (N : Node_Id) is
2718       Called_Subprogram : constant Entity_Id  := Entity (Name (N));
2719       RCI_Package       : constant Entity_Id  := Scope (Called_Subprogram);
2720       Loc               : constant Source_Ptr := Sloc (N);
2721       RCI_Locator       : Node_Id;
2722       RCI_Cache         : Entity_Id;
2723       Calling_Stubs     : Node_Id;
2724       E_Calling_Stubs   : Entity_Id;
2725
2726    begin
2727       E_Calling_Stubs := RCI_Calling_Stubs_Table.Get (Called_Subprogram);
2728
2729       if E_Calling_Stubs = Empty then
2730          RCI_Cache := RCI_Locator_Table.Get (RCI_Package);
2731
2732          if RCI_Cache = Empty then
2733             RCI_Locator :=
2734               RCI_Package_Locator
2735                 (Loc, Specification (Unit_Declaration_Node (RCI_Package)));
2736             Prepend_To (Current_Sem_Unit_Declarations, RCI_Locator);
2737
2738             --  The RCI_Locator package is inserted at the top level in the
2739             --  current unit, and must appear in the proper scope, so that it
2740             --  is not prematurely removed by the GCC back-end.
2741
2742             declare
2743                Scop : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
2744
2745             begin
2746                if Ekind (Scop) = E_Package_Body then
2747                   Push_Scope (Spec_Entity (Scop));
2748
2749                elsif Ekind (Scop) = E_Subprogram_Body then
2750                   Push_Scope
2751                      (Corresponding_Spec (Unit_Declaration_Node (Scop)));
2752
2753                else
2754                   Push_Scope (Scop);
2755                end if;
2756
2757                Analyze (RCI_Locator);
2758                Pop_Scope;
2759             end;
2760
2761             RCI_Cache   := Defining_Unit_Name (RCI_Locator);
2762
2763          else
2764             RCI_Locator := Parent (RCI_Cache);
2765          end if;
2766
2767          Calling_Stubs := Build_Subprogram_Calling_Stubs
2768            (Vis_Decl               => Parent (Parent (Called_Subprogram)),
2769             Subp_Id                =>
2770               Build_Subprogram_Id (Loc, Called_Subprogram),
2771             Asynchronous           => Nkind (N) = N_Procedure_Call_Statement
2772                                         and then
2773                                       Is_Asynchronous (Called_Subprogram),
2774             Locator                => RCI_Cache,
2775             New_Name               => New_Internal_Name ('S'));
2776          Insert_After (RCI_Locator, Calling_Stubs);
2777          Analyze (Calling_Stubs);
2778          E_Calling_Stubs := Defining_Unit_Name (Specification (Calling_Stubs));
2779       end if;
2780
2781       Rewrite (Name (N), New_Occurrence_Of (E_Calling_Stubs, Loc));
2782    end Expand_All_Calls_Remote_Subprogram_Call;
2783
2784    ---------------------------------
2785    -- Expand_Calling_Stubs_Bodies --
2786    ---------------------------------
2787
2788    procedure Expand_Calling_Stubs_Bodies (Unit_Node : Node_Id) is
2789       Spec  : constant Node_Id := Specification (Unit_Node);
2790       Decls : constant List_Id := Visible_Declarations (Spec);
2791    begin
2792       Push_Scope (Scope_Of_Spec (Spec));
2793       Add_Calling_Stubs_To_Declarations
2794         (Specification (Unit_Node), Decls);
2795       Pop_Scope;
2796    end Expand_Calling_Stubs_Bodies;
2797
2798    -----------------------------------
2799    -- Expand_Receiving_Stubs_Bodies --
2800    -----------------------------------
2801
2802    procedure Expand_Receiving_Stubs_Bodies (Unit_Node : Node_Id) is
2803       Spec        : Node_Id;
2804       Decls       : List_Id;
2805       Stubs_Decls : List_Id;
2806       Stubs_Stmts : List_Id;
2807
2808    begin
2809       if Nkind (Unit_Node) = N_Package_Declaration then
2810          Spec  := Specification (Unit_Node);
2811          Decls := Private_Declarations (Spec);
2812
2813          if No (Decls) then
2814             Decls := Visible_Declarations (Spec);
2815          end if;
2816
2817          Push_Scope (Scope_Of_Spec (Spec));
2818          Specific_Add_Receiving_Stubs_To_Declarations (Spec, Decls, Decls);
2819
2820       else
2821          Spec :=
2822            Package_Specification_Of_Scope (Corresponding_Spec (Unit_Node));
2823          Decls := Declarations (Unit_Node);
2824
2825          Push_Scope (Scope_Of_Spec (Unit_Node));
2826          Stubs_Decls := New_List;
2827          Stubs_Stmts := New_List;
2828          Specific_Add_Receiving_Stubs_To_Declarations
2829            (Spec, Stubs_Decls, Stubs_Stmts);
2830
2831          Insert_List_Before (First (Decls), Stubs_Decls);
2832
2833          declare
2834             HSS_Stmts : constant List_Id :=
2835                           Statements (Handled_Statement_Sequence (Unit_Node));
2836
2837             First_HSS_Stmt : constant Node_Id := First (HSS_Stmts);
2838
2839          begin
2840             if No (First_HSS_Stmt) then
2841                Append_List_To (HSS_Stmts, Stubs_Stmts);
2842             else
2843                Insert_List_Before (First_HSS_Stmt, Stubs_Stmts);
2844             end if;
2845          end;
2846       end if;
2847
2848       Pop_Scope;
2849    end Expand_Receiving_Stubs_Bodies;
2850
2851    --------------------
2852    -- GARLIC_Support --
2853    --------------------
2854
2855    package body GARLIC_Support is
2856
2857       --  Local subprograms
2858
2859       procedure Add_RACW_Read_Attribute
2860         (RACW_Type        : Entity_Id;
2861          Stub_Type        : Entity_Id;
2862          Stub_Type_Access : Entity_Id;
2863          Body_Decls       : List_Id);
2864       --  Add Read attribute for the RACW type. The declaration and attribute
2865       --  definition clauses are inserted right after the declaration of
2866       --  RACW_Type. If Body_Decls is not No_List, the subprogram body is
2867       --  appended to it (case where the RACW declaration is in the main unit).
2868
2869       procedure Add_RACW_Write_Attribute
2870         (RACW_Type        : Entity_Id;
2871          Stub_Type        : Entity_Id;
2872          Stub_Type_Access : Entity_Id;
2873          RPC_Receiver     : Node_Id;
2874          Body_Decls       : List_Id);
2875       --  Same as above for the Write attribute
2876
2877       function Stream_Parameter return Node_Id;
2878       function Result return Node_Id;
2879       function Object return Node_Id renames Result;
2880       --  Functions to create occurrences of the formal parameter names of the
2881       --  'Read and 'Write attributes.
2882
2883       Loc : Source_Ptr;
2884       --  Shared source location used by Add_{Read,Write}_Read_Attribute and
2885       --  their ancillary subroutines (set on entry by Add_RACW_Features).
2886
2887       procedure Add_RAS_Access_TSS (N : Node_Id);
2888       --  Add a subprogram body for RAS Access TSS
2889
2890       -------------------------------------
2891       -- Add_Obj_RPC_Receiver_Completion --
2892       -------------------------------------
2893
2894       procedure Add_Obj_RPC_Receiver_Completion
2895         (Loc           : Source_Ptr;
2896          Decls         : List_Id;
2897          RPC_Receiver  : Entity_Id;
2898          Stub_Elements : Stub_Structure)
2899       is
2900       begin
2901          --  The RPC receiver body should not be the completion of the
2902          --  declaration recorded in the stub structure, because then the
2903          --  occurrences of the formal parameters within the body should refer
2904          --  to the entities from the declaration, not from the completion, to
2905          --  which we do not have easy access. Instead, the RPC receiver body
2906          --  acts as its own declaration, and the RPC receiver declaration is
2907          --  completed by a renaming-as-body.
2908
2909          Append_To (Decls,
2910            Make_Subprogram_Renaming_Declaration (Loc,
2911              Specification =>
2912                Copy_Specification (Loc,
2913                  Specification (Stub_Elements.RPC_Receiver_Decl)),
2914              Name          => New_Occurrence_Of (RPC_Receiver, Loc)));
2915       end Add_Obj_RPC_Receiver_Completion;
2916
2917       -----------------------
2918       -- Add_RACW_Features --
2919       -----------------------
2920
2921       procedure Add_RACW_Features
2922         (RACW_Type         : Entity_Id;
2923          Stub_Type         : Entity_Id;
2924          Stub_Type_Access  : Entity_Id;
2925          RPC_Receiver_Decl : Node_Id;
2926          Body_Decls        : List_Id)
2927       is
2928          RPC_Receiver : Node_Id;
2929          Is_RAS       : constant Boolean := not Comes_From_Source (RACW_Type);
2930
2931       begin
2932          Loc := Sloc (RACW_Type);
2933
2934          if Is_RAS then
2935
2936             --  For a RAS, the RPC receiver is that of the RCI unit, not that
2937             --  of the corresponding distributed object type. We retrieve its
2938             --  address from the local proxy object.
2939
2940             RPC_Receiver := Make_Selected_Component (Loc,
2941               Prefix         =>
2942                 Unchecked_Convert_To (RTE (RE_RAS_Proxy_Type_Access), Object),
2943               Selector_Name  => Make_Identifier (Loc, Name_Receiver));
2944
2945          else
2946             RPC_Receiver := Make_Attribute_Reference (Loc,
2947               Prefix         => New_Occurrence_Of (
2948                 Defining_Unit_Name (Specification (RPC_Receiver_Decl)), Loc),
2949               Attribute_Name => Name_Address);
2950          end if;
2951
2952          Add_RACW_Write_Attribute
2953            (RACW_Type,
2954             Stub_Type,
2955             Stub_Type_Access,
2956             RPC_Receiver,
2957             Body_Decls);
2958
2959          Add_RACW_Read_Attribute
2960            (RACW_Type,
2961             Stub_Type,
2962             Stub_Type_Access,
2963             Body_Decls);
2964       end Add_RACW_Features;
2965
2966       -----------------------------
2967       -- Add_RACW_Read_Attribute --
2968       -----------------------------
2969
2970       procedure Add_RACW_Read_Attribute
2971         (RACW_Type        : Entity_Id;
2972          Stub_Type        : Entity_Id;
2973          Stub_Type_Access : Entity_Id;
2974          Body_Decls       : List_Id)
2975       is
2976          Proc_Decl : Node_Id;
2977          Attr_Decl : Node_Id;
2978
2979          Body_Node : Node_Id;
2980
2981          Statements        : constant List_Id := New_List;
2982          Decls             : List_Id;
2983          Local_Statements  : List_Id;
2984          Remote_Statements : List_Id;
2985          --  Various parts of the procedure
2986
2987          Pnam              : constant Entity_Id :=
2988                                Make_Defining_Identifier
2989                                  (Loc, New_Internal_Name ('R'));
2990          Asynchronous_Flag : constant Entity_Id :=
2991                                Asynchronous_Flags_Table.Get (RACW_Type);
2992          pragma Assert (Present (Asynchronous_Flag));
2993
2994          --  Prepare local identifiers
2995
2996          Source_Partition : Entity_Id;
2997          Source_Receiver  : Entity_Id;
2998          Source_Address   : Entity_Id;
2999          Local_Stub       : Entity_Id;
3000          Stubbed_Result   : Entity_Id;
3001
3002       --  Start of processing for Add_RACW_Read_Attribute
3003
3004       begin
3005          Build_Stream_Procedure (Loc,
3006            RACW_Type, Body_Node, Pnam, Statements, Outp => True);
3007          Proc_Decl := Make_Subprogram_Declaration (Loc,
3008            Copy_Specification (Loc, Specification (Body_Node)));
3009
3010          Attr_Decl :=
3011            Make_Attribute_Definition_Clause (Loc,
3012              Name       => New_Occurrence_Of (RACW_Type, Loc),
3013              Chars      => Name_Read,
3014              Expression =>
3015                New_Occurrence_Of (
3016                  Defining_Unit_Name (Specification (Proc_Decl)), Loc));
3017
3018          Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
3019          Insert_After (Proc_Decl, Attr_Decl);
3020
3021          if No (Body_Decls) then
3022
3023             --  Case of processing an RACW type from another unit than the
3024             --  main one: do not generate a body.
3025
3026             return;
3027          end if;
3028
3029          --  Prepare local identifiers
3030
3031          Source_Partition :=
3032            Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3033          Source_Receiver  :=
3034            Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
3035          Source_Address   :=
3036            Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
3037          Local_Stub       :=
3038            Make_Defining_Identifier (Loc, New_Internal_Name ('L'));
3039          Stubbed_Result   :=
3040            Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
3041
3042          --  Generate object declarations
3043
3044          Decls := New_List (
3045            Make_Object_Declaration (Loc,
3046              Defining_Identifier => Source_Partition,
3047              Object_Definition   =>
3048                New_Occurrence_Of (RTE (RE_Partition_ID), Loc)),
3049
3050            Make_Object_Declaration (Loc,
3051              Defining_Identifier => Source_Receiver,
3052              Object_Definition   =>
3053                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc)),
3054
3055            Make_Object_Declaration (Loc,
3056              Defining_Identifier => Source_Address,
3057              Object_Definition   =>
3058                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc)),
3059
3060            Make_Object_Declaration (Loc,
3061              Defining_Identifier => Local_Stub,
3062              Aliased_Present     => True,
3063              Object_Definition   => New_Occurrence_Of (Stub_Type, Loc)),
3064
3065            Make_Object_Declaration (Loc,
3066              Defining_Identifier => Stubbed_Result,
3067              Object_Definition   =>
3068                New_Occurrence_Of (Stub_Type_Access, Loc),
3069              Expression          =>
3070                Make_Attribute_Reference (Loc,
3071                  Prefix =>
3072                    New_Occurrence_Of (Local_Stub, Loc),
3073                  Attribute_Name =>
3074                    Name_Unchecked_Access)));
3075
3076          --  Read the source Partition_ID and RPC_Receiver from incoming stream
3077
3078          Append_List_To (Statements, New_List (
3079            Make_Attribute_Reference (Loc,
3080              Prefix         =>
3081                New_Occurrence_Of (RTE (RE_Partition_ID), Loc),
3082              Attribute_Name => Name_Read,
3083              Expressions    => New_List (
3084                Stream_Parameter,
3085                New_Occurrence_Of (Source_Partition, Loc))),
3086
3087            Make_Attribute_Reference (Loc,
3088              Prefix         =>
3089                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc),
3090              Attribute_Name =>
3091                Name_Read,
3092              Expressions    => New_List (
3093                Stream_Parameter,
3094                New_Occurrence_Of (Source_Receiver, Loc))),
3095
3096            Make_Attribute_Reference (Loc,
3097              Prefix         =>
3098                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc),
3099              Attribute_Name =>
3100                Name_Read,
3101              Expressions    => New_List (
3102                Stream_Parameter,
3103                New_Occurrence_Of (Source_Address, Loc)))));
3104
3105          --  Build_Get_Unique_RP_Call needs the type of Stubbed_Result
3106
3107          Set_Etype (Stubbed_Result, Stub_Type_Access);
3108
3109          --  If the Address is Null_Address, then return a null object, unless
3110          --  RACW_Type is null-excluding, in which case unconditionally raise
3111          --  CONSTRAINT_ERROR instead.
3112
3113          declare
3114             Zero_Statements : List_Id;
3115             --  Statements executed when a zero value is received
3116
3117          begin
3118             if Can_Never_Be_Null (RACW_Type) then
3119                Zero_Statements := New_List (
3120                  Make_Raise_Constraint_Error (Loc,
3121                    Reason => CE_Null_Not_Allowed));
3122             else
3123                Zero_Statements := New_List (
3124                  Make_Assignment_Statement (Loc,
3125                    Name       => Result,
3126                    Expression => Make_Null (Loc)),
3127                  Make_Simple_Return_Statement (Loc));
3128             end if;
3129
3130             Append_To (Statements,
3131               Make_Implicit_If_Statement (RACW_Type,
3132                 Condition       =>
3133                   Make_Op_Eq (Loc,
3134                     Left_Opnd  => New_Occurrence_Of (Source_Address, Loc),
3135                     Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
3136                 Then_Statements => Zero_Statements));
3137          end;
3138
3139          --  If the RACW denotes an object created on the current partition,
3140          --  Local_Statements will be executed. The real object will be used.
3141
3142          Local_Statements := New_List (
3143            Make_Assignment_Statement (Loc,
3144              Name       => Result,
3145              Expression =>
3146                Unchecked_Convert_To (RACW_Type,
3147                  OK_Convert_To (RTE (RE_Address),
3148                    New_Occurrence_Of (Source_Address, Loc)))));
3149
3150          --  If the object is located on another partition, then a stub object
3151          --  will be created with all the information needed to rebuild the
3152          --  real object at the other end.
3153
3154          Remote_Statements := New_List (
3155
3156            Make_Assignment_Statement (Loc,
3157              Name       => Make_Selected_Component (Loc,
3158                Prefix        => Stubbed_Result,
3159                Selector_Name => Name_Origin),
3160              Expression =>
3161                New_Occurrence_Of (Source_Partition, Loc)),
3162
3163            Make_Assignment_Statement (Loc,
3164              Name       => Make_Selected_Component (Loc,
3165                Prefix        => Stubbed_Result,
3166                Selector_Name => Name_Receiver),
3167              Expression =>
3168                New_Occurrence_Of (Source_Receiver, Loc)),
3169
3170            Make_Assignment_Statement (Loc,
3171              Name       => Make_Selected_Component (Loc,
3172                Prefix        => Stubbed_Result,
3173                Selector_Name => Name_Addr),
3174              Expression =>
3175                New_Occurrence_Of (Source_Address, Loc)));
3176
3177          Append_To (Remote_Statements,
3178            Make_Assignment_Statement (Loc,
3179              Name       => Make_Selected_Component (Loc,
3180                Prefix        => Stubbed_Result,
3181                Selector_Name => Name_Asynchronous),
3182              Expression =>
3183                New_Occurrence_Of (Asynchronous_Flag, Loc)));
3184
3185          Append_List_To (Remote_Statements,
3186            Build_Get_Unique_RP_Call (Loc, Stubbed_Result, Stub_Type));
3187          --  ??? Issue with asynchronous calls here: the Asynchronous flag is
3188          --  set on the stub type if, and only if, the RACW type has a pragma
3189          --  Asynchronous. This is incorrect for RACWs that implement RAS
3190          --  types, because in that case the /designated subprogram/ (not the
3191          --  type) might be asynchronous, and that causes the stub to need to
3192          --  be asynchronous too. A solution is to transport a RAS as a struct
3193          --  containing a RACW and an asynchronous flag, and to properly alter
3194          --  the Asynchronous component in the stub type in the RAS's Input
3195          --  TSS.
3196
3197          Append_To (Remote_Statements,
3198            Make_Assignment_Statement (Loc,
3199              Name       => Result,
3200              Expression => Unchecked_Convert_To (RACW_Type,
3201                New_Occurrence_Of (Stubbed_Result, Loc))));
3202
3203          --  Distinguish between the local and remote cases, and execute the
3204          --  appropriate piece of code.
3205
3206          Append_To (Statements,
3207            Make_Implicit_If_Statement (RACW_Type,
3208              Condition       =>
3209                Make_Op_Eq (Loc,
3210                  Left_Opnd  =>
3211                    Make_Function_Call (Loc,
3212                      Name => New_Occurrence_Of (
3213                        RTE (RE_Get_Local_Partition_Id), Loc)),
3214                  Right_Opnd => New_Occurrence_Of (Source_Partition, Loc)),
3215              Then_Statements => Local_Statements,
3216              Else_Statements => Remote_Statements));
3217
3218          Set_Declarations (Body_Node, Decls);
3219          Append_To (Body_Decls, Body_Node);
3220       end Add_RACW_Read_Attribute;
3221
3222       ------------------------------
3223       -- Add_RACW_Write_Attribute --
3224       ------------------------------
3225
3226       procedure Add_RACW_Write_Attribute
3227         (RACW_Type        : Entity_Id;
3228          Stub_Type        : Entity_Id;
3229          Stub_Type_Access : Entity_Id;
3230          RPC_Receiver     : Node_Id;
3231          Body_Decls       : List_Id)
3232       is
3233          Body_Node : Node_Id;
3234          Proc_Decl : Node_Id;
3235          Attr_Decl : Node_Id;
3236
3237          Statements        : constant List_Id := New_List;
3238          Local_Statements  : List_Id;
3239          Remote_Statements : List_Id;
3240          Null_Statements   : List_Id;
3241
3242          Pnam : constant Entity_Id :=
3243                   Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
3244
3245       begin
3246          Build_Stream_Procedure
3247            (Loc, RACW_Type, Body_Node, Pnam, Statements, Outp => False);
3248
3249          Proc_Decl := Make_Subprogram_Declaration (Loc,
3250            Copy_Specification (Loc, Specification (Body_Node)));
3251
3252          Attr_Decl :=
3253            Make_Attribute_Definition_Clause (Loc,
3254              Name       => New_Occurrence_Of (RACW_Type, Loc),
3255              Chars      => Name_Write,
3256              Expression =>
3257                New_Occurrence_Of (
3258                  Defining_Unit_Name (Specification (Proc_Decl)), Loc));
3259
3260          Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
3261          Insert_After (Proc_Decl, Attr_Decl);
3262
3263          if No (Body_Decls) then
3264             return;
3265          end if;
3266
3267          --  Build the code fragment corresponding to the marshalling of a
3268          --  local object.
3269
3270          Local_Statements := New_List (
3271
3272            Pack_Entity_Into_Stream_Access (Loc,
3273              Stream => Stream_Parameter,
3274              Object => RTE (RE_Get_Local_Partition_Id)),
3275
3276            Pack_Node_Into_Stream_Access (Loc,
3277              Stream => Stream_Parameter,
3278              Object => OK_Convert_To (RTE (RE_Unsigned_64), RPC_Receiver),
3279              Etyp   => RTE (RE_Unsigned_64)),
3280
3281           Pack_Node_Into_Stream_Access (Loc,
3282             Stream => Stream_Parameter,
3283             Object => OK_Convert_To (RTE (RE_Unsigned_64),
3284               Make_Attribute_Reference (Loc,
3285                 Prefix         =>
3286                   Make_Explicit_Dereference (Loc,
3287                     Prefix => Object),
3288                 Attribute_Name => Name_Address)),
3289             Etyp   => RTE (RE_Unsigned_64)));
3290
3291          --  Build the code fragment corresponding to the marshalling of
3292          --  a remote object.
3293
3294          Remote_Statements := New_List (
3295            Pack_Node_Into_Stream_Access (Loc,
3296              Stream => Stream_Parameter,
3297              Object =>
3298                Make_Selected_Component (Loc,
3299                  Prefix        =>
3300                    Unchecked_Convert_To (Stub_Type_Access, Object),
3301                  Selector_Name => Make_Identifier (Loc, Name_Origin)),
3302             Etyp    => RTE (RE_Partition_ID)),
3303
3304            Pack_Node_Into_Stream_Access (Loc,
3305             Stream => Stream_Parameter,
3306             Object =>
3307                Make_Selected_Component (Loc,
3308                  Prefix        =>
3309                    Unchecked_Convert_To (Stub_Type_Access, Object),
3310                  Selector_Name => Make_Identifier (Loc, Name_Receiver)),
3311             Etyp   => RTE (RE_Unsigned_64)),
3312
3313            Pack_Node_Into_Stream_Access (Loc,
3314             Stream => Stream_Parameter,
3315             Object =>
3316                Make_Selected_Component (Loc,
3317                  Prefix        =>
3318                    Unchecked_Convert_To (Stub_Type_Access, Object),
3319                  Selector_Name => Make_Identifier (Loc, Name_Addr)),
3320             Etyp   => RTE (RE_Unsigned_64)));
3321
3322          --  Build code fragment corresponding to marshalling of a null object
3323
3324          Null_Statements := New_List (
3325
3326            Pack_Entity_Into_Stream_Access (Loc,
3327              Stream => Stream_Parameter,
3328              Object => RTE (RE_Get_Local_Partition_Id)),
3329
3330            Pack_Node_Into_Stream_Access (Loc,
3331              Stream => Stream_Parameter,
3332              Object => OK_Convert_To (RTE (RE_Unsigned_64), RPC_Receiver),
3333              Etyp   => RTE (RE_Unsigned_64)),
3334
3335            Pack_Node_Into_Stream_Access (Loc,
3336              Stream => Stream_Parameter,
3337              Object => Make_Integer_Literal (Loc, Uint_0),
3338              Etyp   => RTE (RE_Unsigned_64)));
3339
3340          Append_To (Statements,
3341            Make_Implicit_If_Statement (RACW_Type,
3342              Condition       =>
3343                Make_Op_Eq (Loc,
3344                  Left_Opnd  => Object,
3345                  Right_Opnd => Make_Null (Loc)),
3346
3347              Then_Statements => Null_Statements,
3348
3349              Elsif_Parts     => New_List (
3350                Make_Elsif_Part (Loc,
3351                  Condition       =>
3352                    Make_Op_Eq (Loc,
3353                      Left_Opnd  =>
3354                        Make_Attribute_Reference (Loc,
3355                          Prefix         => Object,
3356                          Attribute_Name => Name_Tag),
3357
3358                      Right_Opnd =>
3359                        Make_Attribute_Reference (Loc,
3360                          Prefix         => New_Occurrence_Of (Stub_Type, Loc),
3361                          Attribute_Name => Name_Tag)),
3362                  Then_Statements => Remote_Statements)),
3363              Else_Statements => Local_Statements));
3364
3365          Append_To (Body_Decls, Body_Node);
3366       end Add_RACW_Write_Attribute;
3367
3368       ------------------------
3369       -- Add_RAS_Access_TSS --
3370       ------------------------
3371
3372       procedure Add_RAS_Access_TSS (N : Node_Id) is
3373          Loc : constant Source_Ptr := Sloc (N);
3374
3375          Ras_Type : constant Entity_Id := Defining_Identifier (N);
3376          Fat_Type : constant Entity_Id := Equivalent_Type (Ras_Type);
3377          --  Ras_Type is the access to subprogram type while Fat_Type is the
3378          --  corresponding record type.
3379
3380          RACW_Type : constant Entity_Id :=
3381                        Underlying_RACW_Type (Ras_Type);
3382          Desig     : constant Entity_Id :=
3383                        Etype (Designated_Type (RACW_Type));
3384
3385          Stub_Elements : constant Stub_Structure :=
3386                            Stubs_Table.Get (Desig);
3387          pragma Assert (Stub_Elements /= Empty_Stub_Structure);
3388
3389          Proc : constant Entity_Id :=
3390                   Make_Defining_Identifier (Loc,
3391                     Chars => Make_TSS_Name (Ras_Type, TSS_RAS_Access));
3392
3393          Proc_Spec : Node_Id;
3394
3395          --  Formal parameters
3396
3397          Package_Name : constant Entity_Id :=
3398                           Make_Defining_Identifier (Loc,
3399                             Chars => Name_P);
3400          --  Target package
3401
3402          Subp_Id : constant Entity_Id :=
3403                      Make_Defining_Identifier (Loc,
3404                        Chars => Name_S);
3405          --  Target subprogram
3406
3407          Asynch_P : constant Entity_Id :=
3408                       Make_Defining_Identifier (Loc,
3409                         Chars => Name_Asynchronous);
3410          --  Is the procedure to which the 'Access applies asynchronous?
3411
3412          All_Calls_Remote : constant Entity_Id :=
3413                               Make_Defining_Identifier (Loc,
3414                                 Chars => Name_All_Calls_Remote);
3415          --  True if an All_Calls_Remote pragma applies to the RCI unit
3416          --  that contains the subprogram.
3417
3418          --  Common local variables
3419
3420          Proc_Decls      : List_Id;
3421          Proc_Statements : List_Id;
3422
3423          Origin : constant Entity_Id :=
3424                     Make_Defining_Identifier (Loc,
3425                       Chars => New_Internal_Name ('P'));
3426
3427          --  Additional local variables for the local case
3428
3429          Proxy_Addr : constant Entity_Id :=
3430                         Make_Defining_Identifier (Loc,
3431                           Chars => New_Internal_Name ('P'));
3432
3433          --  Additional local variables for the remote case
3434
3435          Local_Stub : constant Entity_Id :=
3436                         Make_Defining_Identifier (Loc,
3437                           Chars => New_Internal_Name ('L'));
3438
3439          Stub_Ptr : constant Entity_Id :=
3440                       Make_Defining_Identifier (Loc,
3441                         Chars => New_Internal_Name ('S'));
3442
3443          function Set_Field
3444            (Field_Name : Name_Id;
3445             Value      : Node_Id) return Node_Id;
3446          --  Construct an assignment that sets the named component in the
3447          --  returned record
3448
3449          ---------------
3450          -- Set_Field --
3451          ---------------
3452
3453          function Set_Field
3454            (Field_Name : Name_Id;
3455             Value      : Node_Id) return Node_Id
3456          is
3457          begin
3458             return
3459               Make_Assignment_Statement (Loc,
3460                 Name       =>
3461                   Make_Selected_Component (Loc,
3462                     Prefix        => Stub_Ptr,
3463                     Selector_Name => Field_Name),
3464                 Expression => Value);
3465          end Set_Field;
3466
3467       --  Start of processing for Add_RAS_Access_TSS
3468
3469       begin
3470          Proc_Decls := New_List (
3471
3472             --  Common declarations
3473
3474            Make_Object_Declaration (Loc,
3475              Defining_Identifier => Origin,
3476              Constant_Present    => True,
3477              Object_Definition   =>
3478                New_Occurrence_Of (RTE (RE_Partition_ID), Loc),
3479              Expression          =>
3480                Make_Function_Call (Loc,
3481                  Name                   =>
3482                    New_Occurrence_Of (RTE (RE_Get_Active_Partition_Id), Loc),
3483                  Parameter_Associations => New_List (
3484                    New_Occurrence_Of (Package_Name, Loc)))),
3485
3486             --  Declaration use only in the local case: proxy address
3487
3488            Make_Object_Declaration (Loc,
3489              Defining_Identifier => Proxy_Addr,
3490              Object_Definition   =>
3491                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc)),
3492
3493             --  Declarations used only in the remote case: stub object and
3494             --  stub pointer.
3495
3496            Make_Object_Declaration (Loc,
3497              Defining_Identifier => Local_Stub,
3498              Aliased_Present     => True,
3499              Object_Definition   =>
3500                New_Occurrence_Of (Stub_Elements.Stub_Type, Loc)),
3501
3502            Make_Object_Declaration (Loc,
3503              Defining_Identifier =>
3504                Stub_Ptr,
3505              Object_Definition   =>
3506                New_Occurrence_Of (Stub_Elements.Stub_Type_Access, Loc),
3507              Expression          =>
3508                Make_Attribute_Reference (Loc,
3509                  Prefix => New_Occurrence_Of (Local_Stub, Loc),
3510                  Attribute_Name => Name_Unchecked_Access)));
3511
3512          Set_Etype (Stub_Ptr, Stub_Elements.Stub_Type_Access);
3513
3514          --  Build_Get_Unique_RP_Call needs above information
3515
3516          --  Note: Here we assume that the Fat_Type is a record
3517          --  containing just a pointer to a proxy or stub object.
3518
3519          Proc_Statements := New_List (
3520
3521          --  Generate:
3522
3523          --    Get_RAS_Info (Pkg, Subp, PA);
3524          --    if Origin = Local_Partition_Id
3525          --      and then not All_Calls_Remote
3526          --    then
3527          --       return Fat_Type!(PA);
3528          --    end if;
3529
3530             Make_Procedure_Call_Statement (Loc,
3531               Name => New_Occurrence_Of (RTE (RE_Get_RAS_Info), Loc),
3532               Parameter_Associations => New_List (
3533                 New_Occurrence_Of (Package_Name, Loc),
3534                 New_Occurrence_Of (Subp_Id, Loc),
3535                 New_Occurrence_Of (Proxy_Addr, Loc))),
3536
3537            Make_Implicit_If_Statement (N,
3538              Condition =>
3539                Make_And_Then (Loc,
3540                  Left_Opnd  =>
3541                    Make_Op_Eq (Loc,
3542                      Left_Opnd =>
3543                        New_Occurrence_Of (Origin, Loc),
3544                      Right_Opnd =>
3545                        Make_Function_Call (Loc,
3546                          New_Occurrence_Of (
3547                            RTE (RE_Get_Local_Partition_Id), Loc))),
3548
3549                  Right_Opnd =>
3550                    Make_Op_Not (Loc,
3551                      New_Occurrence_Of (All_Calls_Remote, Loc))),
3552
3553              Then_Statements => New_List (
3554                Make_Simple_Return_Statement (Loc,
3555                  Unchecked_Convert_To (Fat_Type,
3556                    OK_Convert_To (RTE (RE_Address),
3557                      New_Occurrence_Of (Proxy_Addr, Loc)))))),
3558
3559            Set_Field (Name_Origin,
3560                New_Occurrence_Of (Origin, Loc)),
3561
3562            Set_Field (Name_Receiver,
3563              Make_Function_Call (Loc,
3564                Name                   =>
3565                  New_Occurrence_Of (RTE (RE_Get_RCI_Package_Receiver), Loc),
3566                Parameter_Associations => New_List (
3567                  New_Occurrence_Of (Package_Name, Loc)))),
3568
3569            Set_Field (Name_Addr, New_Occurrence_Of (Proxy_Addr, Loc)),
3570
3571             --  E.4.1(9) A remote call is asynchronous if it is a call to
3572             --  a procedure or a call through a value of an access-to-procedure
3573             --  type to which a pragma Asynchronous applies.
3574
3575             --  Asynch_P is true when the procedure is asynchronous;
3576             --  Asynch_T is true when the type is asynchronous.
3577
3578            Set_Field (Name_Asynchronous,
3579              Make_Or_Else (Loc,
3580                New_Occurrence_Of (Asynch_P, Loc),
3581                New_Occurrence_Of (Boolean_Literals (
3582                  Is_Asynchronous (Ras_Type)), Loc))));
3583
3584          Append_List_To (Proc_Statements,
3585            Build_Get_Unique_RP_Call
3586              (Loc, Stub_Ptr, Stub_Elements.Stub_Type));
3587
3588          --  Return the newly created value
3589
3590          Append_To (Proc_Statements,
3591            Make_Simple_Return_Statement (Loc,
3592              Expression =>
3593                Unchecked_Convert_To (Fat_Type,
3594                  New_Occurrence_Of (Stub_Ptr, Loc))));
3595
3596          Proc_Spec :=
3597            Make_Function_Specification (Loc,
3598              Defining_Unit_Name       => Proc,
3599              Parameter_Specifications => New_List (
3600                Make_Parameter_Specification (Loc,
3601                  Defining_Identifier => Package_Name,
3602                  Parameter_Type      =>
3603                    New_Occurrence_Of (Standard_String, Loc)),
3604
3605                Make_Parameter_Specification (Loc,
3606                  Defining_Identifier => Subp_Id,
3607                  Parameter_Type      =>
3608                    New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc)),
3609
3610                Make_Parameter_Specification (Loc,
3611                  Defining_Identifier => Asynch_P,
3612                  Parameter_Type      =>
3613                    New_Occurrence_Of (Standard_Boolean, Loc)),
3614
3615                Make_Parameter_Specification (Loc,
3616                  Defining_Identifier => All_Calls_Remote,
3617                  Parameter_Type      =>
3618                    New_Occurrence_Of (Standard_Boolean, Loc))),
3619
3620             Result_Definition =>
3621               New_Occurrence_Of (Fat_Type, Loc));
3622
3623          --  Set the kind and return type of the function to prevent
3624          --  ambiguities between Ras_Type and Fat_Type in subsequent analysis.
3625
3626          Set_Ekind (Proc, E_Function);
3627          Set_Etype (Proc, Fat_Type);
3628
3629          Discard_Node (
3630            Make_Subprogram_Body (Loc,
3631              Specification              => Proc_Spec,
3632              Declarations               => Proc_Decls,
3633              Handled_Statement_Sequence =>
3634                Make_Handled_Sequence_Of_Statements (Loc,
3635                  Statements => Proc_Statements)));
3636
3637          Set_TSS (Fat_Type, Proc);
3638       end Add_RAS_Access_TSS;
3639
3640       -----------------------
3641       -- Add_RAST_Features --
3642       -----------------------
3643
3644       procedure Add_RAST_Features
3645         (Vis_Decl : Node_Id;
3646          RAS_Type : Entity_Id)
3647       is
3648          pragma Warnings (Off);
3649          pragma Unreferenced (RAS_Type);
3650          pragma Warnings (On);
3651       begin
3652          Add_RAS_Access_TSS (Vis_Decl);
3653       end Add_RAST_Features;
3654
3655       -----------------------------------------
3656       -- Add_Receiving_Stubs_To_Declarations --
3657       -----------------------------------------
3658
3659       procedure Add_Receiving_Stubs_To_Declarations
3660         (Pkg_Spec : Node_Id;
3661          Decls    : List_Id;
3662          Stmts    : List_Id)
3663       is
3664          Loc : constant Source_Ptr := Sloc (Pkg_Spec);
3665
3666          Request_Parameter : Node_Id;
3667
3668          Pkg_RPC_Receiver            : constant Entity_Id :=
3669                                          Make_Defining_Identifier (Loc,
3670                                            New_Internal_Name ('H'));
3671          Pkg_RPC_Receiver_Statements : List_Id;
3672          Pkg_RPC_Receiver_Cases      : constant List_Id := New_List;
3673          Pkg_RPC_Receiver_Body       : Node_Id;
3674          --  A Pkg_RPC_Receiver is built to decode the request
3675
3676          Lookup_RAS_Info : constant Entity_Id :=
3677                              Make_Defining_Identifier (Loc,
3678                                Chars => New_Internal_Name ('R'));
3679          --  A remote subprogram is created to allow peers to look up
3680          --  RAS information using subprogram ids.
3681
3682          Subp_Id    : Entity_Id;
3683          Subp_Index : Entity_Id;
3684          --  Subprogram_Id as read from the incoming stream
3685
3686          Current_Declaration       : Node_Id;
3687          Current_Subprogram_Number : Int := First_RCI_Subprogram_Id;
3688          Current_Stubs             : Node_Id;
3689
3690          Subp_Info_Array : constant Entity_Id :=
3691                              Make_Defining_Identifier (Loc,
3692                                Chars => New_Internal_Name ('I'));
3693
3694          Subp_Info_List : constant List_Id := New_List;
3695
3696          Register_Pkg_Actuals : constant List_Id := New_List;
3697
3698          All_Calls_Remote_E  : Entity_Id;
3699          Proxy_Object_Addr   : Entity_Id;
3700
3701          procedure Append_Stubs_To
3702            (RPC_Receiver_Cases : List_Id;
3703             Stubs              : Node_Id;
3704             Subprogram_Number  : Int);
3705          --  Add one case to the specified RPC receiver case list
3706          --  associating Subprogram_Number with the subprogram declared
3707          --  by Declaration, for which we have receiving stubs in Stubs.
3708
3709          ---------------------
3710          -- Append_Stubs_To --
3711          ---------------------
3712
3713          procedure Append_Stubs_To
3714            (RPC_Receiver_Cases : List_Id;
3715             Stubs              : Node_Id;
3716             Subprogram_Number  : Int)
3717          is
3718          begin
3719             Append_To (RPC_Receiver_Cases,
3720               Make_Case_Statement_Alternative (Loc,
3721                 Discrete_Choices =>
3722                    New_List (Make_Integer_Literal (Loc, Subprogram_Number)),
3723                 Statements       =>
3724                   New_List (
3725                     Make_Procedure_Call_Statement (Loc,
3726                       Name                   =>
3727                         New_Occurrence_Of (Defining_Entity (Stubs), Loc),
3728                       Parameter_Associations => New_List (
3729                         New_Occurrence_Of (Request_Parameter, Loc))))));
3730          end Append_Stubs_To;
3731
3732       --  Start of processing for Add_Receiving_Stubs_To_Declarations
3733
3734       begin
3735          --  Building receiving stubs consist in several operations:
3736
3737          --    - a package RPC receiver must be built. This subprogram
3738          --      will get a Subprogram_Id from the incoming stream
3739          --      and will dispatch the call to the right subprogram;
3740
3741          --    - a receiving stub for each subprogram visible in the package
3742          --      spec. This stub will read all the parameters from the stream,
3743          --      and put the result as well as the exception occurrence in the
3744          --      output stream;
3745
3746          --    - a dummy package with an empty spec and a body made of an
3747          --      elaboration part, whose job is to register the receiving
3748          --      part of this RCI package on the name server. This is done
3749          --      by calling System.Partition_Interface.Register_Receiving_Stub.
3750
3751          Build_RPC_Receiver_Body (
3752            RPC_Receiver => Pkg_RPC_Receiver,
3753            Request      => Request_Parameter,
3754            Subp_Id      => Subp_Id,
3755            Subp_Index   => Subp_Index,
3756            Stmts        => Pkg_RPC_Receiver_Statements,
3757            Decl         => Pkg_RPC_Receiver_Body);
3758          pragma Assert (Subp_Id = Subp_Index);
3759
3760          --  A null subp_id denotes a call through a RAS, in which case the
3761          --  next Uint_64 element in the stream is the address of the local
3762          --  proxy object, from which we can retrieve the actual subprogram id.
3763
3764          Append_To (Pkg_RPC_Receiver_Statements,
3765            Make_Implicit_If_Statement (Pkg_Spec,
3766              Condition =>
3767                Make_Op_Eq (Loc,
3768                  New_Occurrence_Of (Subp_Id, Loc),
3769                  Make_Integer_Literal (Loc, 0)),
3770
3771              Then_Statements => New_List (
3772                Make_Assignment_Statement (Loc,
3773                  Name =>
3774                    New_Occurrence_Of (Subp_Id, Loc),
3775
3776                  Expression =>
3777                    Make_Selected_Component (Loc,
3778                      Prefix =>
3779                        Unchecked_Convert_To (RTE (RE_RAS_Proxy_Type_Access),
3780                          OK_Convert_To (RTE (RE_Address),
3781                            Make_Attribute_Reference (Loc,
3782                              Prefix =>
3783                                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc),
3784                              Attribute_Name =>
3785                                Name_Input,
3786                              Expressions => New_List (
3787                                Make_Selected_Component (Loc,
3788                                  Prefix        => Request_Parameter,
3789                                  Selector_Name => Name_Params))))),
3790
3791                      Selector_Name =>
3792                        Make_Identifier (Loc, Name_Subp_Id))))));
3793
3794          --  Build a subprogram for RAS information lookups
3795
3796          Current_Declaration :=
3797            Make_Subprogram_Declaration (Loc,
3798              Specification =>
3799                Make_Function_Specification (Loc,
3800                  Defining_Unit_Name =>
3801                    Lookup_RAS_Info,
3802                  Parameter_Specifications => New_List (
3803                    Make_Parameter_Specification (Loc,
3804                      Defining_Identifier =>
3805                        Make_Defining_Identifier (Loc, Name_Subp_Id),
3806                      In_Present =>
3807                        True,
3808                      Parameter_Type =>
3809                        New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc))),
3810                  Result_Definition =>
3811                    New_Occurrence_Of (RTE (RE_Unsigned_64), Loc)));
3812
3813          Append_To (Decls, Current_Declaration);
3814          Analyze (Current_Declaration);
3815
3816          Current_Stubs := Build_Subprogram_Receiving_Stubs
3817            (Vis_Decl     => Current_Declaration,
3818             Asynchronous => False);
3819          Append_To (Decls, Current_Stubs);
3820          Analyze (Current_Stubs);
3821
3822          Append_Stubs_To (Pkg_RPC_Receiver_Cases,
3823            Stubs       =>
3824              Current_Stubs,
3825            Subprogram_Number => 1);
3826
3827          --  For each subprogram, the receiving stub will be built and a
3828          --  case statement will be made on the Subprogram_Id to dispatch
3829          --  to the right subprogram.
3830
3831          All_Calls_Remote_E :=
3832            Boolean_Literals
3833              (Has_All_Calls_Remote (Defining_Entity (Pkg_Spec)));
3834
3835          Overload_Counter_Table.Reset;
3836
3837          Current_Declaration := First (Visible_Declarations (Pkg_Spec));
3838          while Present (Current_Declaration) loop
3839             if Nkind (Current_Declaration) = N_Subprogram_Declaration
3840               and then Comes_From_Source (Current_Declaration)
3841             then
3842                declare
3843                   Loc : constant Source_Ptr := Sloc (Current_Declaration);
3844                   --  While specifically processing Current_Declaration, use
3845                   --  its Sloc as the location of all generated nodes.
3846
3847                   Subp_Def : constant Entity_Id :=
3848                                Defining_Unit_Name
3849                                  (Specification (Current_Declaration));
3850
3851                   Subp_Val : String_Id;
3852                   pragma Warnings (Off, Subp_Val);
3853
3854                begin
3855                   --  Build receiving stub
3856
3857                   Current_Stubs :=
3858                     Build_Subprogram_Receiving_Stubs
3859                       (Vis_Decl     => Current_Declaration,
3860                        Asynchronous =>
3861                          Nkind (Specification (Current_Declaration)) =
3862                              N_Procedure_Specification
3863                            and then Is_Asynchronous (Subp_Def));
3864
3865                   Append_To (Decls, Current_Stubs);
3866                   Analyze (Current_Stubs);
3867
3868                   --  Build RAS proxy
3869
3870                   Add_RAS_Proxy_And_Analyze (Decls,
3871                     Vis_Decl           => Current_Declaration,
3872                     All_Calls_Remote_E => All_Calls_Remote_E,
3873                     Proxy_Object_Addr  => Proxy_Object_Addr);
3874
3875                   --  Compute distribution identifier
3876
3877                   Assign_Subprogram_Identifier
3878                     (Subp_Def,
3879                      Current_Subprogram_Number,
3880                      Subp_Val);
3881
3882                   pragma Assert
3883                     (Current_Subprogram_Number = Get_Subprogram_Id (Subp_Def));
3884
3885                   --  Add subprogram descriptor (RCI_Subp_Info) to the
3886                   --  subprograms table for this receiver. The aggregate
3887                   --  below must be kept consistent with the declaration
3888                   --  of type RCI_Subp_Info in System.Partition_Interface.
3889
3890                   Append_To (Subp_Info_List,
3891                     Make_Component_Association (Loc,
3892                       Choices => New_List (
3893                         Make_Integer_Literal (Loc,
3894                           Current_Subprogram_Number)),
3895
3896                       Expression =>
3897                         Make_Aggregate (Loc,
3898                           Component_Associations => New_List (
3899                             Make_Component_Association (Loc,
3900                               Choices => New_List (
3901                                 Make_Identifier (Loc, Name_Addr)),
3902                               Expression =>
3903                                 New_Occurrence_Of (
3904                                   Proxy_Object_Addr, Loc))))));
3905
3906                   Append_Stubs_To (Pkg_RPC_Receiver_Cases,
3907                     Stubs             => Current_Stubs,
3908                     Subprogram_Number => Current_Subprogram_Number);
3909                end;
3910
3911                Current_Subprogram_Number := Current_Subprogram_Number + 1;
3912             end if;
3913
3914             Next (Current_Declaration);
3915          end loop;
3916
3917          --  If we receive an invalid Subprogram_Id, it is best to do nothing
3918          --  rather than raising an exception since we do not want someone
3919          --  to crash a remote partition by sending invalid subprogram ids.
3920          --  This is consistent with the other parts of the case statement
3921          --  since even in presence of incorrect parameters in the stream,
3922          --  every exception will be caught and (if the subprogram is not an
3923          --  APC) put into the result stream and sent away.
3924
3925          Append_To (Pkg_RPC_Receiver_Cases,
3926            Make_Case_Statement_Alternative (Loc,
3927              Discrete_Choices => New_List (Make_Others_Choice (Loc)),
3928              Statements       => New_List (Make_Null_Statement (Loc))));
3929
3930          Append_To (Pkg_RPC_Receiver_Statements,
3931            Make_Case_Statement (Loc,
3932              Expression   => New_Occurrence_Of (Subp_Id, Loc),
3933              Alternatives => Pkg_RPC_Receiver_Cases));
3934
3935          Append_To (Decls,
3936            Make_Object_Declaration (Loc,
3937              Defining_Identifier => Subp_Info_Array,
3938              Constant_Present    => True,
3939              Aliased_Present     => True,
3940              Object_Definition   =>
3941                Make_Subtype_Indication (Loc,
3942                  Subtype_Mark =>
3943                    New_Occurrence_Of (RTE (RE_RCI_Subp_Info_Array), Loc),
3944                  Constraint =>
3945                    Make_Index_Or_Discriminant_Constraint (Loc,
3946                      New_List (
3947                        Make_Range (Loc,
3948                          Low_Bound  => Make_Integer_Literal (Loc,
3949                            First_RCI_Subprogram_Id),
3950                          High_Bound =>
3951                            Make_Integer_Literal (Loc,
3952                              Intval =>
3953                                First_RCI_Subprogram_Id
3954                                + List_Length (Subp_Info_List) - 1)))))));
3955
3956          --  For a degenerate RCI with no visible subprograms, Subp_Info_List
3957          --  has zero length, and the declaration is for an empty array, in
3958          --  which case no initialization aggregate must be generated.
3959
3960          if Present (First (Subp_Info_List)) then
3961             Set_Expression (Last (Decls),
3962               Make_Aggregate (Loc,
3963                 Component_Associations => Subp_Info_List));
3964
3965          --  No initialization provided: remove CONSTANT so that the
3966          --  declaration is not an incomplete deferred constant.
3967
3968          else
3969             Set_Constant_Present (Last (Decls), False);
3970          end if;
3971
3972          Analyze (Last (Decls));
3973
3974          declare
3975             Subp_Info_Addr : Node_Id;
3976             --  Return statement for Lookup_RAS_Info: address of the subprogram
3977             --  information record for the requested subprogram id.
3978
3979          begin
3980             if Present (First (Subp_Info_List)) then
3981                Subp_Info_Addr :=
3982                  Make_Selected_Component (Loc,
3983                    Prefix =>
3984                      Make_Indexed_Component (Loc,
3985                        Prefix => New_Occurrence_Of (Subp_Info_Array, Loc),
3986                        Expressions => New_List (
3987                          Convert_To (Standard_Integer,
3988                            Make_Identifier (Loc, Name_Subp_Id)))),
3989                    Selector_Name => Make_Identifier (Loc, Name_Addr));
3990
3991             --  Case of no visible subprogram: just raise Constraint_Error, we
3992             --  know for sure we got junk from a remote partition.
3993
3994             else
3995                Subp_Info_Addr :=
3996                  Make_Raise_Constraint_Error (Loc,
3997                     Reason => CE_Range_Check_Failed);
3998                Set_Etype (Subp_Info_Addr, RTE (RE_Unsigned_64));
3999             end if;
4000
4001             Append_To (Decls,
4002               Make_Subprogram_Body (Loc,
4003                 Specification =>
4004                   Copy_Specification (Loc, Parent (Lookup_RAS_Info)),
4005                 Declarations  => No_List,
4006                 Handled_Statement_Sequence =>
4007                   Make_Handled_Sequence_Of_Statements (Loc,
4008                     Statements => New_List (
4009                       Make_Simple_Return_Statement (Loc,
4010                         Expression =>
4011                           OK_Convert_To
4012                             (RTE (RE_Unsigned_64), Subp_Info_Addr))))));
4013          end;
4014
4015          Analyze (Last (Decls));
4016
4017          Append_To (Decls, Pkg_RPC_Receiver_Body);
4018          Analyze (Last (Decls));
4019
4020          Get_Library_Unit_Name_String (Pkg_Spec);
4021
4022          --  Name
4023
4024          Append_To (Register_Pkg_Actuals,
4025            Make_String_Literal (Loc,
4026              Strval => String_From_Name_Buffer));
4027
4028          --  Receiver
4029
4030          Append_To (Register_Pkg_Actuals,
4031            Make_Attribute_Reference (Loc,
4032              Prefix         => New_Occurrence_Of (Pkg_RPC_Receiver, Loc),
4033              Attribute_Name => Name_Unrestricted_Access));
4034
4035          --  Version
4036
4037          Append_To (Register_Pkg_Actuals,
4038            Make_Attribute_Reference (Loc,
4039              Prefix         =>
4040                New_Occurrence_Of (Defining_Entity (Pkg_Spec), Loc),
4041              Attribute_Name => Name_Version));
4042
4043          --  Subp_Info
4044
4045          Append_To (Register_Pkg_Actuals,
4046            Make_Attribute_Reference (Loc,
4047              Prefix         => New_Occurrence_Of (Subp_Info_Array, Loc),
4048              Attribute_Name => Name_Address));
4049
4050          --  Subp_Info_Len
4051
4052          Append_To (Register_Pkg_Actuals,
4053            Make_Attribute_Reference (Loc,
4054              Prefix         => New_Occurrence_Of (Subp_Info_Array, Loc),
4055              Attribute_Name => Name_Length));
4056
4057          --  Generate the call
4058
4059          Append_To (Stmts,
4060            Make_Procedure_Call_Statement (Loc,
4061              Name                   =>
4062                New_Occurrence_Of (RTE (RE_Register_Receiving_Stub), Loc),
4063              Parameter_Associations => Register_Pkg_Actuals));
4064          Analyze (Last (Stmts));
4065       end Add_Receiving_Stubs_To_Declarations;
4066
4067       ---------------------------------
4068       -- Build_General_Calling_Stubs --
4069       ---------------------------------
4070
4071       procedure Build_General_Calling_Stubs
4072         (Decls                     : List_Id;
4073          Statements                : List_Id;
4074          Target_Partition          : Entity_Id;
4075          Target_RPC_Receiver       : Node_Id;
4076          Subprogram_Id             : Node_Id;
4077          Asynchronous              : Node_Id   := Empty;
4078          Is_Known_Asynchronous     : Boolean   := False;
4079          Is_Known_Non_Asynchronous : Boolean   := False;
4080          Is_Function               : Boolean;
4081          Spec                      : Node_Id;
4082          Stub_Type                 : Entity_Id := Empty;
4083          RACW_Type                 : Entity_Id := Empty;
4084          Nod                       : Node_Id)
4085       is
4086          Loc : constant Source_Ptr := Sloc (Nod);
4087
4088          Stream_Parameter : Node_Id;
4089          --  Name of the stream used to transmit parameters to the
4090          --  remote package.
4091
4092          Result_Parameter : Node_Id;
4093          --  Name of the result parameter (in non-APC cases) which get the
4094          --  result of the remote subprogram.
4095
4096          Exception_Return_Parameter : Node_Id;
4097          --  Name of the parameter which will hold the exception sent by the
4098          --  remote subprogram.
4099
4100          Current_Parameter : Node_Id;
4101          --  Current parameter being handled
4102
4103          Ordered_Parameters_List : constant List_Id :=
4104                                      Build_Ordered_Parameters_List (Spec);
4105
4106          Asynchronous_Statements     : List_Id := No_List;
4107          Non_Asynchronous_Statements : List_Id := No_List;
4108          --  Statements specifics to the Asynchronous/Non-Asynchronous cases
4109
4110          Extra_Formal_Statements : constant List_Id := New_List;
4111          --  List of statements for extra formal parameters. It will appear
4112          --  after the regular statements for writing out parameters.
4113
4114          pragma Warnings (Off);
4115          pragma Unreferenced (RACW_Type);
4116          --  Used only for the PolyORB case
4117          pragma Warnings (On);
4118
4119       begin
4120          --  The general form of a calling stub for a given subprogram is:
4121
4122          --    procedure X (...) is P : constant Partition_ID :=
4123          --      RCI_Cache.Get_Active_Partition_ID; Stream, Result : aliased
4124          --      System.RPC.Params_Stream_Type (0); begin
4125          --       Put_Package_RPC_Receiver_In_Stream; (the package RPC receiver
4126          --                  comes from RCI_Cache.Get_RCI_Package_Receiver)
4127          --       Put_Subprogram_Id_In_Stream; Put_Parameters_In_Stream; Do_RPC
4128          --       (Stream, Result); Read_Exception_Occurrence_From_Result;
4129          --       Raise_It;
4130          --       Read_Out_Parameters_And_Function_Return_From_Stream; end X;
4131
4132          --  There are some variations: Do_APC is called for an asynchronous
4133          --  procedure and the part after the call is completely ommitted as
4134          --  well as the declaration of Result. For a function call, 'Input is
4135          --  always used to read the result even if it is constrained.
4136
4137          Stream_Parameter :=
4138            Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
4139
4140          Append_To (Decls,
4141            Make_Object_Declaration (Loc,
4142              Defining_Identifier => Stream_Parameter,
4143              Aliased_Present     => True,
4144              Object_Definition   =>
4145                Make_Subtype_Indication (Loc,
4146                  Subtype_Mark =>
4147                    New_Occurrence_Of (RTE (RE_Params_Stream_Type), Loc),
4148                  Constraint   =>
4149                    Make_Index_Or_Discriminant_Constraint (Loc,
4150                      Constraints =>
4151                        New_List (Make_Integer_Literal (Loc, 0))))));
4152
4153          if not Is_Known_Asynchronous then
4154             Result_Parameter :=
4155               Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
4156
4157             Append_To (Decls,
4158               Make_Object_Declaration (Loc,
4159                 Defining_Identifier => Result_Parameter,
4160                 Aliased_Present     => True,
4161                 Object_Definition   =>
4162                   Make_Subtype_Indication (Loc,
4163                     Subtype_Mark =>
4164                       New_Occurrence_Of (RTE (RE_Params_Stream_Type), Loc),
4165                     Constraint   =>
4166                       Make_Index_Or_Discriminant_Constraint (Loc,
4167                         Constraints =>
4168                           New_List (Make_Integer_Literal (Loc, 0))))));
4169
4170             Exception_Return_Parameter :=
4171               Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
4172
4173             Append_To (Decls,
4174               Make_Object_Declaration (Loc,
4175                 Defining_Identifier => Exception_Return_Parameter,
4176                 Object_Definition   =>
4177                   New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc)));
4178
4179          else
4180             Result_Parameter := Empty;
4181             Exception_Return_Parameter := Empty;
4182          end if;
4183
4184          --  Put first the RPC receiver corresponding to the remote package
4185
4186          Append_To (Statements,
4187            Make_Attribute_Reference (Loc,
4188              Prefix         =>
4189                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc),
4190              Attribute_Name => Name_Write,
4191              Expressions    => New_List (
4192                Make_Attribute_Reference (Loc,
4193                  Prefix         => New_Occurrence_Of (Stream_Parameter, Loc),
4194                  Attribute_Name => Name_Access),
4195                Target_RPC_Receiver)));
4196
4197          --  Then put the Subprogram_Id of the subprogram we want to call in
4198          --  the stream.
4199
4200          Append_To (Statements,
4201            Make_Attribute_Reference (Loc,
4202              Prefix         => New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc),
4203              Attribute_Name => Name_Write,
4204              Expressions      => New_List (
4205                Make_Attribute_Reference (Loc,
4206                  Prefix         => New_Occurrence_Of (Stream_Parameter, Loc),
4207                  Attribute_Name => Name_Access),
4208                Subprogram_Id)));
4209
4210          Current_Parameter := First (Ordered_Parameters_List);
4211          while Present (Current_Parameter) loop
4212             declare
4213                Typ             : constant Node_Id :=
4214                                    Parameter_Type (Current_Parameter);
4215                Etyp            : Entity_Id;
4216                Constrained     : Boolean;
4217                Value           : Node_Id;
4218                Extra_Parameter : Entity_Id;
4219
4220             begin
4221                if Is_RACW_Controlling_Formal
4222                     (Current_Parameter, Stub_Type)
4223                then
4224                   --  In the case of a controlling formal argument, we marshall
4225                   --  its addr field rather than the local stub.
4226
4227                   Append_To (Statements,
4228                      Pack_Node_Into_Stream (Loc,
4229                        Stream => Stream_Parameter,
4230                        Object =>
4231                          Make_Selected_Component (Loc,
4232                            Prefix        =>
4233                              Defining_Identifier (Current_Parameter),
4234                            Selector_Name => Name_Addr),
4235                        Etyp   => RTE (RE_Unsigned_64)));
4236
4237                else
4238                   Value :=
4239                     New_Occurrence_Of
4240                       (Defining_Identifier (Current_Parameter), Loc);
4241
4242                   --  Access type parameters are transmitted as in out
4243                   --  parameters. However, a dereference is needed so that
4244                   --  we marshall the designated object.
4245
4246                   if Nkind (Typ) = N_Access_Definition then
4247                      Value := Make_Explicit_Dereference (Loc, Value);
4248                      Etyp  := Etype (Subtype_Mark (Typ));
4249                   else
4250                      Etyp := Etype (Typ);
4251                   end if;
4252
4253                   Constrained := not Transmit_As_Unconstrained (Etyp);
4254
4255                   --  Any parameter but unconstrained out parameters are
4256                   --  transmitted to the peer.
4257
4258                   if In_Present (Current_Parameter)
4259                     or else not Out_Present (Current_Parameter)
4260                     or else not Constrained
4261                   then
4262                      Append_To (Statements,
4263                        Make_Attribute_Reference (Loc,
4264                          Prefix         => New_Occurrence_Of (Etyp, Loc),
4265                          Attribute_Name =>
4266                            Output_From_Constrained (Constrained),
4267                          Expressions    => New_List (
4268                            Make_Attribute_Reference (Loc,
4269                              Prefix         =>
4270                                New_Occurrence_Of (Stream_Parameter, Loc),
4271                              Attribute_Name => Name_Access),
4272                            Value)));
4273                   end if;
4274                end if;
4275
4276                --  If the current parameter has a dynamic constrained status,
4277                --  then this status is transmitted as well.
4278                --  This should be done for accessibility as well ???
4279
4280                if Nkind (Typ) /= N_Access_Definition
4281                  and then Need_Extra_Constrained (Current_Parameter)
4282                then
4283                   --  In this block, we do not use the extra formal that has
4284                   --  been created because it does not exist at the time of
4285                   --  expansion when building calling stubs for remote access
4286                   --  to subprogram types. We create an extra variable of this
4287                   --  type and push it in the stream after the regular
4288                   --  parameters.
4289
4290                   Extra_Parameter := Make_Defining_Identifier
4291                                        (Loc, New_Internal_Name ('P'));
4292
4293                   Append_To (Decls,
4294                      Make_Object_Declaration (Loc,
4295                        Defining_Identifier => Extra_Parameter,
4296                        Constant_Present    => True,
4297                        Object_Definition   =>
4298                           New_Occurrence_Of (Standard_Boolean, Loc),
4299                        Expression          =>
4300                           Make_Attribute_Reference (Loc,
4301                             Prefix         =>
4302                               New_Occurrence_Of (
4303                                 Defining_Identifier (Current_Parameter), Loc),
4304                             Attribute_Name => Name_Constrained)));
4305
4306                   Append_To (Extra_Formal_Statements,
4307                      Make_Attribute_Reference (Loc,
4308                        Prefix         =>
4309                          New_Occurrence_Of (Standard_Boolean, Loc),
4310                        Attribute_Name => Name_Write,
4311                        Expressions    => New_List (
4312                          Make_Attribute_Reference (Loc,
4313                            Prefix         =>
4314                              New_Occurrence_Of
4315                               (Stream_Parameter, Loc), Attribute_Name =>
4316                              Name_Access),
4317                          New_Occurrence_Of (Extra_Parameter, Loc))));
4318                end if;
4319
4320                Next (Current_Parameter);
4321             end;
4322          end loop;
4323
4324          --  Append the formal statements list to the statements
4325
4326          Append_List_To (Statements, Extra_Formal_Statements);
4327
4328          if not Is_Known_Non_Asynchronous then
4329
4330             --  Build the call to System.RPC.Do_APC
4331
4332             Asynchronous_Statements := New_List (
4333               Make_Procedure_Call_Statement (Loc,
4334                 Name                   =>
4335                   New_Occurrence_Of (RTE (RE_Do_Apc), Loc),
4336                 Parameter_Associations => New_List (
4337                   New_Occurrence_Of (Target_Partition, Loc),
4338                   Make_Attribute_Reference (Loc,
4339                     Prefix         =>
4340                       New_Occurrence_Of (Stream_Parameter, Loc),
4341                     Attribute_Name => Name_Access))));
4342          else
4343             Asynchronous_Statements := No_List;
4344          end if;
4345
4346          if not Is_Known_Asynchronous then
4347
4348             --  Build the call to System.RPC.Do_RPC
4349
4350             Non_Asynchronous_Statements := New_List (
4351               Make_Procedure_Call_Statement (Loc,
4352                 Name                   =>
4353                   New_Occurrence_Of (RTE (RE_Do_Rpc), Loc),
4354                 Parameter_Associations => New_List (
4355                   New_Occurrence_Of (Target_Partition, Loc),
4356
4357                   Make_Attribute_Reference (Loc,
4358                     Prefix         =>
4359                       New_Occurrence_Of (Stream_Parameter, Loc),
4360                     Attribute_Name => Name_Access),
4361
4362                   Make_Attribute_Reference (Loc,
4363                     Prefix         =>
4364                       New_Occurrence_Of (Result_Parameter, Loc),
4365                     Attribute_Name => Name_Access))));
4366
4367             --  Read the exception occurrence from the result stream and
4368             --  reraise it. It does no harm if this is a Null_Occurrence since
4369             --  this does nothing.
4370
4371             Append_To (Non_Asynchronous_Statements,
4372               Make_Attribute_Reference (Loc,
4373                 Prefix         =>
4374                   New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc),
4375
4376                 Attribute_Name => Name_Read,
4377
4378                 Expressions    => New_List (
4379                   Make_Attribute_Reference (Loc,
4380                     Prefix         =>
4381                       New_Occurrence_Of (Result_Parameter, Loc),
4382                     Attribute_Name => Name_Access),
4383                   New_Occurrence_Of (Exception_Return_Parameter, Loc))));
4384
4385             Append_To (Non_Asynchronous_Statements,
4386               Make_Procedure_Call_Statement (Loc,
4387                 Name                   =>
4388                   New_Occurrence_Of (RTE (RE_Reraise_Occurrence), Loc),
4389                 Parameter_Associations => New_List (
4390                   New_Occurrence_Of (Exception_Return_Parameter, Loc))));
4391
4392             if Is_Function then
4393
4394                --  If this is a function call, then read the value and return
4395                --  it. The return value is written/read using 'Output/'Input.
4396
4397                Append_To (Non_Asynchronous_Statements,
4398                  Make_Tag_Check (Loc,
4399                    Make_Simple_Return_Statement (Loc,
4400                      Expression =>
4401                        Make_Attribute_Reference (Loc,
4402                          Prefix         =>
4403                            New_Occurrence_Of (
4404                              Etype (Result_Definition (Spec)), Loc),
4405
4406                          Attribute_Name => Name_Input,
4407
4408                          Expressions    => New_List (
4409                            Make_Attribute_Reference (Loc,
4410                              Prefix         =>
4411                                New_Occurrence_Of (Result_Parameter, Loc),
4412                              Attribute_Name => Name_Access))))));
4413
4414             else
4415                --  Loop around parameters and assign out (or in out)
4416                --  parameters. In the case of RACW, controlling arguments
4417                --  cannot possibly have changed since they are remote, so we do
4418                --  not read them from the stream.
4419
4420                Current_Parameter := First (Ordered_Parameters_List);
4421                while Present (Current_Parameter) loop
4422                   declare
4423                      Typ   : constant Node_Id :=
4424                                Parameter_Type (Current_Parameter);
4425                      Etyp  : Entity_Id;
4426                      Value : Node_Id;
4427
4428                   begin
4429                      Value :=
4430                        New_Occurrence_Of
4431                          (Defining_Identifier (Current_Parameter), Loc);
4432
4433                      if Nkind (Typ) = N_Access_Definition then
4434                         Value := Make_Explicit_Dereference (Loc, Value);
4435                         Etyp  := Etype (Subtype_Mark (Typ));
4436                      else
4437                         Etyp := Etype (Typ);
4438                      end if;
4439
4440                      if (Out_Present (Current_Parameter)
4441                           or else Nkind (Typ) = N_Access_Definition)
4442                        and then Etyp /= Stub_Type
4443                      then
4444                         Append_To (Non_Asynchronous_Statements,
4445                            Make_Attribute_Reference (Loc,
4446                              Prefix         =>
4447                                New_Occurrence_Of (Etyp, Loc),
4448
4449                              Attribute_Name => Name_Read,
4450
4451                              Expressions    => New_List (
4452                                Make_Attribute_Reference (Loc,
4453                                  Prefix         =>
4454                                    New_Occurrence_Of (Result_Parameter, Loc),
4455                                  Attribute_Name => Name_Access),
4456                                Value)));
4457                      end if;
4458                   end;
4459
4460                   Next (Current_Parameter);
4461                end loop;
4462             end if;
4463          end if;
4464
4465          if Is_Known_Asynchronous then
4466             Append_List_To (Statements, Asynchronous_Statements);
4467
4468          elsif Is_Known_Non_Asynchronous then
4469             Append_List_To (Statements, Non_Asynchronous_Statements);
4470
4471          else
4472             pragma Assert (Present (Asynchronous));
4473             Prepend_To (Asynchronous_Statements,
4474               Make_Attribute_Reference (Loc,
4475                 Prefix         => New_Occurrence_Of (Standard_Boolean, Loc),
4476                 Attribute_Name => Name_Write,
4477                 Expressions    => New_List (
4478                   Make_Attribute_Reference (Loc,
4479                     Prefix         =>
4480                       New_Occurrence_Of (Stream_Parameter, Loc),
4481                     Attribute_Name => Name_Access),
4482                   New_Occurrence_Of (Standard_True, Loc))));
4483
4484             Prepend_To (Non_Asynchronous_Statements,
4485               Make_Attribute_Reference (Loc,
4486                 Prefix         => New_Occurrence_Of (Standard_Boolean, Loc),
4487                 Attribute_Name => Name_Write,
4488                 Expressions    => New_List (
4489                   Make_Attribute_Reference (Loc,
4490                     Prefix         =>
4491                       New_Occurrence_Of (Stream_Parameter, Loc),
4492                     Attribute_Name => Name_Access),
4493                   New_Occurrence_Of (Standard_False, Loc))));
4494
4495             Append_To (Statements,
4496               Make_Implicit_If_Statement (Nod,
4497                 Condition       => Asynchronous,
4498                 Then_Statements => Asynchronous_Statements,
4499                 Else_Statements => Non_Asynchronous_Statements));
4500          end if;
4501       end Build_General_Calling_Stubs;
4502
4503       -----------------------------
4504       -- Build_RPC_Receiver_Body --
4505       -----------------------------
4506
4507       procedure Build_RPC_Receiver_Body
4508         (RPC_Receiver : Entity_Id;
4509          Request      : out Entity_Id;
4510          Subp_Id      : out Entity_Id;
4511          Subp_Index   : out Entity_Id;
4512          Stmts        : out List_Id;
4513          Decl         : out Node_Id)
4514       is
4515          Loc : constant Source_Ptr := Sloc (RPC_Receiver);
4516
4517          RPC_Receiver_Spec  : Node_Id;
4518          RPC_Receiver_Decls : List_Id;
4519
4520       begin
4521          Request := Make_Defining_Identifier (Loc, Name_R);
4522
4523          RPC_Receiver_Spec :=
4524            Build_RPC_Receiver_Specification
4525              (RPC_Receiver      => RPC_Receiver,
4526               Request_Parameter => Request);
4527
4528          Subp_Id    := Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
4529          Subp_Index := Subp_Id;
4530
4531          --  Subp_Id may not be a constant, because in the case of the RPC
4532          --  receiver for an RCI package, when a call is received from a RAS
4533          --  dereference, it will be assigned during subsequent processing.
4534
4535          RPC_Receiver_Decls := New_List (
4536            Make_Object_Declaration (Loc,
4537              Defining_Identifier => Subp_Id,
4538              Object_Definition   =>
4539                New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc),
4540              Expression          =>
4541                Make_Attribute_Reference (Loc,
4542                  Prefix          =>
4543                    New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc),
4544                  Attribute_Name  => Name_Input,
4545                  Expressions     => New_List (
4546                    Make_Selected_Component (Loc,
4547                      Prefix        => Request,
4548                      Selector_Name => Name_Params)))));
4549
4550          Stmts := New_List;
4551
4552          Decl :=
4553            Make_Subprogram_Body (Loc,
4554              Specification              => RPC_Receiver_Spec,
4555              Declarations               => RPC_Receiver_Decls,
4556              Handled_Statement_Sequence =>
4557                Make_Handled_Sequence_Of_Statements (Loc,
4558                  Statements => Stmts));
4559       end Build_RPC_Receiver_Body;
4560
4561       -----------------------
4562       -- Build_Stub_Target --
4563       -----------------------
4564
4565       function Build_Stub_Target
4566         (Loc                   : Source_Ptr;
4567          Decls                 : List_Id;
4568          RCI_Locator           : Entity_Id;
4569          Controlling_Parameter : Entity_Id) return RPC_Target
4570       is
4571          Target_Info : RPC_Target (PCS_Kind => Name_GARLIC_DSA);
4572       begin
4573          Target_Info.Partition :=
4574            Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
4575          if Present (Controlling_Parameter) then
4576             Append_To (Decls,
4577               Make_Object_Declaration (Loc,
4578                 Defining_Identifier => Target_Info.Partition,
4579                 Constant_Present    => True,
4580                 Object_Definition   =>
4581                   New_Occurrence_Of (RTE (RE_Partition_ID), Loc),
4582
4583                 Expression          =>
4584                   Make_Selected_Component (Loc,
4585                     Prefix        => Controlling_Parameter,
4586                     Selector_Name => Name_Origin)));
4587
4588             Target_Info.RPC_Receiver :=
4589               Make_Selected_Component (Loc,
4590                 Prefix        => Controlling_Parameter,
4591                 Selector_Name => Name_Receiver);
4592
4593          else
4594             Append_To (Decls,
4595               Make_Object_Declaration (Loc,
4596                 Defining_Identifier => Target_Info.Partition,
4597                 Constant_Present    => True,
4598                 Object_Definition   =>
4599                   New_Occurrence_Of (RTE (RE_Partition_ID), Loc),
4600
4601                 Expression          =>
4602                   Make_Function_Call (Loc,
4603                     Name => Make_Selected_Component (Loc,
4604                       Prefix        =>
4605                         Make_Identifier (Loc, Chars (RCI_Locator)),
4606                       Selector_Name =>
4607                         Make_Identifier (Loc,
4608                           Name_Get_Active_Partition_ID)))));
4609
4610             Target_Info.RPC_Receiver :=
4611               Make_Selected_Component (Loc,
4612                 Prefix        =>
4613                   Make_Identifier (Loc, Chars (RCI_Locator)),
4614                 Selector_Name =>
4615                   Make_Identifier (Loc, Name_Get_RCI_Package_Receiver));
4616          end if;
4617          return Target_Info;
4618       end Build_Stub_Target;
4619
4620       ---------------------
4621       -- Build_Stub_Type --
4622       ---------------------
4623
4624       procedure Build_Stub_Type
4625         (RACW_Type         : Entity_Id;
4626          Stub_Type         : Entity_Id;
4627          Stub_Type_Decl    : out Node_Id;
4628          RPC_Receiver_Decl : out Node_Id)
4629       is
4630          Loc    : constant Source_Ptr := Sloc (Stub_Type);
4631          Is_RAS : constant Boolean    := not Comes_From_Source (RACW_Type);
4632
4633       begin
4634          Stub_Type_Decl :=
4635            Make_Full_Type_Declaration (Loc,
4636              Defining_Identifier => Stub_Type,
4637              Type_Definition     =>
4638                Make_Record_Definition (Loc,
4639                  Tagged_Present  => True,
4640                  Limited_Present => True,
4641                  Component_List  =>
4642                    Make_Component_List (Loc,
4643                      Component_Items => New_List (
4644
4645                        Make_Component_Declaration (Loc,
4646                          Defining_Identifier =>
4647                            Make_Defining_Identifier (Loc, Name_Origin),
4648                          Component_Definition =>
4649                            Make_Component_Definition (Loc,
4650                              Aliased_Present    => False,
4651                              Subtype_Indication =>
4652                                New_Occurrence_Of (
4653                                  RTE (RE_Partition_ID), Loc))),
4654
4655                        Make_Component_Declaration (Loc,
4656                          Defining_Identifier =>
4657                            Make_Defining_Identifier (Loc, Name_Receiver),
4658                          Component_Definition =>
4659                            Make_Component_Definition (Loc,
4660                              Aliased_Present    => False,
4661                              Subtype_Indication =>
4662                                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc))),
4663
4664                        Make_Component_Declaration (Loc,
4665                          Defining_Identifier =>
4666                            Make_Defining_Identifier (Loc, Name_Addr),
4667                          Component_Definition =>
4668                            Make_Component_Definition (Loc,
4669                              Aliased_Present    => False,
4670                              Subtype_Indication =>
4671                                New_Occurrence_Of (RTE (RE_Unsigned_64), Loc))),
4672
4673                        Make_Component_Declaration (Loc,
4674                          Defining_Identifier =>
4675                            Make_Defining_Identifier (Loc, Name_Asynchronous),
4676                          Component_Definition =>
4677                            Make_Component_Definition (Loc,
4678                              Aliased_Present    => False,
4679                              Subtype_Indication =>
4680                                New_Occurrence_Of (
4681                                  Standard_Boolean, Loc)))))));
4682
4683          if Is_RAS then
4684             RPC_Receiver_Decl := Empty;
4685          else
4686             declare
4687                RPC_Receiver_Request : constant Entity_Id :=
4688                                         Make_Defining_Identifier (Loc, Name_R);
4689             begin
4690                RPC_Receiver_Decl :=
4691                  Make_Subprogram_Declaration (Loc,
4692                    Build_RPC_Receiver_Specification (
4693                      RPC_Receiver      => Make_Defining_Identifier (Loc,
4694                                             New_Internal_Name ('R')),
4695                      Request_Parameter => RPC_Receiver_Request));
4696             end;
4697          end if;
4698       end Build_Stub_Type;
4699
4700       --------------------------------------
4701       -- Build_Subprogram_Receiving_Stubs --
4702       --------------------------------------
4703
4704       function Build_Subprogram_Receiving_Stubs
4705         (Vis_Decl                 : Node_Id;
4706          Asynchronous             : Boolean;
4707          Dynamically_Asynchronous : Boolean   := False;
4708          Stub_Type                : Entity_Id := Empty;
4709          RACW_Type                : Entity_Id := Empty;
4710          Parent_Primitive         : Entity_Id := Empty) return Node_Id
4711       is
4712          Loc : constant Source_Ptr := Sloc (Vis_Decl);
4713
4714          Request_Parameter : constant Entity_Id :=
4715                                Make_Defining_Identifier (Loc,
4716                                  New_Internal_Name ('R'));
4717          --  Formal parameter for receiving stubs: a descriptor for an incoming
4718          --  request.
4719
4720          Decls : constant List_Id := New_List;
4721          --  All the parameters will get declared before calling the real
4722          --  subprograms. Also the out parameters will be declared.
4723
4724          Statements : constant List_Id := New_List;
4725
4726          Extra_Formal_Statements : constant List_Id := New_List;
4727          --  Statements concerning extra formal parameters
4728
4729          After_Statements : constant List_Id := New_List;
4730          --  Statements to be executed after the subprogram call
4731
4732          Inner_Decls : List_Id := No_List;
4733          --  In case of a function, the inner declarations are needed since
4734          --  the result may be unconstrained.
4735
4736          Excep_Handlers : List_Id := No_List;
4737          Excep_Choice   : Entity_Id;
4738          Excep_Code     : List_Id;
4739
4740          Parameter_List : constant List_Id := New_List;
4741          --  List of parameters to be passed to the subprogram
4742
4743          Current_Parameter : Node_Id;
4744
4745          Ordered_Parameters_List : constant List_Id :=
4746                                      Build_Ordered_Parameters_List
4747                                        (Specification (Vis_Decl));
4748
4749          Subp_Spec : Node_Id;
4750          --  Subprogram specification
4751
4752          Called_Subprogram : Node_Id;
4753          --  The subprogram to call
4754
4755          Null_Raise_Statement : Node_Id;
4756
4757          Dynamic_Async : Entity_Id;
4758
4759       begin
4760          if Present (RACW_Type) then
4761             Called_Subprogram := New_Occurrence_Of (Parent_Primitive, Loc);
4762          else
4763             Called_Subprogram :=
4764               New_Occurrence_Of
4765                 (Defining_Unit_Name (Specification (Vis_Decl)), Loc);
4766          end if;
4767
4768          if Dynamically_Asynchronous then
4769             Dynamic_Async :=
4770               Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
4771          else
4772             Dynamic_Async := Empty;
4773          end if;
4774
4775          if not Asynchronous or Dynamically_Asynchronous then
4776
4777             --  The first statement after the subprogram call is a statement to
4778             --  write a Null_Occurrence into the result stream.
4779
4780             Null_Raise_Statement :=
4781               Make_Attribute_Reference (Loc,
4782                 Prefix         =>
4783                   New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc),
4784                 Attribute_Name => Name_Write,
4785                 Expressions    => New_List (
4786                   Make_Selected_Component (Loc,
4787                     Prefix        => Request_Parameter,
4788                     Selector_Name => Name_Result),
4789                   New_Occurrence_Of (RTE (RE_Null_Occurrence), Loc)));
4790
4791             if Dynamically_Asynchronous then
4792                Null_Raise_Statement :=
4793                  Make_Implicit_If_Statement (Vis_Decl,
4794                    Condition       =>
4795                      Make_Op_Not (Loc, New_Occurrence_Of (Dynamic_Async, Loc)),
4796                    Then_Statements => New_List (Null_Raise_Statement));
4797             end if;
4798
4799             Append_To (After_Statements, Null_Raise_Statement);
4800          end if;
4801
4802          --  Loop through every parameter and get its value from the stream. If
4803          --  the parameter is unconstrained, then the parameter is read using
4804          --  'Input at the point of declaration.
4805
4806          Current_Parameter := First (Ordered_Parameters_List);
4807          while Present (Current_Parameter) loop
4808             declare
4809                Etyp        : Entity_Id;
4810                Constrained : Boolean;
4811
4812                Need_Extra_Constrained : Boolean;
4813                --  True when an Extra_Constrained actual is required
4814
4815                Object : constant Entity_Id :=
4816                           Make_Defining_Identifier (Loc,
4817                             New_Internal_Name ('P'));
4818
4819                Expr : Node_Id := Empty;
4820
4821                Is_Controlling_Formal : constant Boolean :=
4822                                          Is_RACW_Controlling_Formal
4823                                            (Current_Parameter, Stub_Type);
4824
4825             begin
4826                if Is_Controlling_Formal then
4827
4828                   --  We have a controlling formal parameter. Read its address
4829                   --  rather than a real object. The address is in Unsigned_64
4830                   --  form.
4831
4832                   Etyp := RTE (RE_Unsigned_64);
4833                else
4834                   Etyp := Etype (Parameter_Type (Current_Parameter));
4835                end if;
4836
4837                Constrained := not Transmit_As_Unconstrained (Etyp);
4838
4839                if In_Present (Current_Parameter)
4840                  or else not Out_Present (Current_Parameter)
4841                  or else not Constrained
4842                  or else Is_Controlling_Formal
4843                then
4844                   --  If an input parameter is constrained, then the read of
4845                   --  the parameter is deferred until the beginning of the
4846                   --  subprogram body. If it is unconstrained, then an
4847                   --  expression is built for the object declaration and the
4848                   --  variable is set using 'Input instead of 'Read. Note that
4849                   --  this deferral does not change the order in which the
4850                   --  actuals are read because Build_Ordered_Parameter_List
4851                   --  puts them unconstrained first.
4852
4853                   if Constrained then
4854                      Append_To (Statements,
4855                        Make_Attribute_Reference (Loc,
4856                          Prefix         => New_Occurrence_Of (Etyp, Loc),
4857                          Attribute_Name => Name_Read,
4858                          Expressions    => New_List (
4859                            Make_Selected_Component (Loc,
4860                              Prefix        => Request_Parameter,
4861                              Selector_Name => Name_Params),
4862                            New_Occurrence_Of (Object, Loc))));
4863
4864                   else
4865
4866                      --  Build and append Input_With_Tag_Check function
4867
4868                      Append_To (Decls,
4869                        Input_With_Tag_Check (Loc,
4870                          Var_Type => Etyp,
4871                          Stream   =>
4872                            Make_Selected_Component (Loc,
4873                              Prefix        => Request_Parameter,
4874                              Selector_Name => Name_Params)));
4875
4876                      --  Prepare function call expression
4877
4878                      Expr :=
4879                        Make_Function_Call (Loc,
4880                          Name =>
4881                            New_Occurrence_Of
4882                              (Defining_Unit_Name
4883                                (Specification (Last (Decls))), Loc));
4884                   end if;
4885                end if;
4886
4887                Need_Extra_Constrained :=
4888                  Nkind (Parameter_Type (Current_Parameter)) /=
4889                                                         N_Access_Definition
4890                    and then
4891                      Ekind (Defining_Identifier (Current_Parameter)) /= E_Void
4892                    and then
4893                       Present (Extra_Constrained
4894                                 (Defining_Identifier (Current_Parameter)));
4895
4896                --  We may not associate an extra constrained actual to a
4897                --  constant object, so if one is needed, declare the actual
4898                --  as a variable even if it won't be modified.
4899
4900                Build_Actual_Object_Declaration
4901                  (Object   => Object,
4902                   Etyp     => Etyp,
4903                   Variable => Need_Extra_Constrained
4904                                 or else Out_Present (Current_Parameter),
4905                   Expr     => Expr,
4906                   Decls    => Decls);
4907
4908                --  An out parameter may be written back using a 'Write
4909                --  attribute instead of a 'Output because it has been
4910                --  constrained by the parameter given to the caller. Note that
4911                --  out controlling arguments in the case of a RACW are not put
4912                --  back in the stream because the pointer on them has not
4913                --  changed.
4914
4915                if Out_Present (Current_Parameter)
4916                  and then
4917                    Etype (Parameter_Type (Current_Parameter)) /= Stub_Type
4918                then
4919                   Append_To (After_Statements,
4920                     Make_Attribute_Reference (Loc,
4921                       Prefix         => New_Occurrence_Of (Etyp, Loc),
4922                       Attribute_Name => Name_Write,
4923                       Expressions    => New_List (
4924                         Make_Selected_Component (Loc,
4925                           Prefix        => Request_Parameter,
4926                           Selector_Name => Name_Result),
4927                         New_Occurrence_Of (Object, Loc))));
4928                end if;
4929
4930                --  For RACW controlling formals, the Etyp of Object is always
4931                --  an RACW, even if the parameter is not of an anonymous access
4932                --  type. In such case, we need to dereference it at call time.
4933
4934                if Is_Controlling_Formal then
4935                   if Nkind (Parameter_Type (Current_Parameter)) /=
4936                     N_Access_Definition
4937                   then
4938                      Append_To (Parameter_List,
4939                        Make_Parameter_Association (Loc,
4940                          Selector_Name             =>
4941                            New_Occurrence_Of (
4942                              Defining_Identifier (Current_Parameter), Loc),
4943                          Explicit_Actual_Parameter =>
4944                            Make_Explicit_Dereference (Loc,
4945                              Unchecked_Convert_To (RACW_Type,
4946                                OK_Convert_To (RTE (RE_Address),
4947                                  New_Occurrence_Of (Object, Loc))))));
4948
4949                   else
4950                      Append_To (Parameter_List,
4951                        Make_Parameter_Association (Loc,
4952                          Selector_Name             =>
4953                            New_Occurrence_Of (
4954                              Defining_Identifier (Current_Parameter), Loc),
4955                          Explicit_Actual_Parameter =>
4956                            Unchecked_Convert_To (RACW_Type,
4957                              OK_Convert_To (RTE (RE_Address),
4958                                New_Occurrence_Of (Object, Loc)))));
4959                   end if;
4960
4961                else
4962                   Append_To (Parameter_List,
4963                     Make_Parameter_Association (Loc,
4964                       Selector_Name             =>
4965                         New_Occurrence_Of (
4966                           Defining_Identifier (Current_Parameter), Loc),
4967                       Explicit_Actual_Parameter =>
4968                         New_Occurrence_Of (Object, Loc)));
4969                end if;
4970
4971                --  If the current parameter needs an extra formal, then read it
4972                --  from the stream and set the corresponding semantic field in
4973                --  the variable. If the kind of the parameter identifier is
4974                --  E_Void, then this is a compiler generated parameter that
4975                --  doesn't need an extra constrained status.
4976
4977                --  The case of Extra_Accessibility should also be handled ???
4978
4979                if Need_Extra_Constrained then
4980                   declare
4981                      Extra_Parameter : constant Entity_Id :=
4982                                          Extra_Constrained
4983                                            (Defining_Identifier
4984                                              (Current_Parameter));
4985
4986                      Formal_Entity : constant Entity_Id :=
4987                                        Make_Defining_Identifier
4988                                            (Loc, Chars (Extra_Parameter));
4989
4990                      Formal_Type : constant Entity_Id :=
4991                                      Etype (Extra_Parameter);
4992
4993                   begin
4994                      Append_To (Decls,
4995                        Make_Object_Declaration (Loc,
4996                          Defining_Identifier => Formal_Entity,
4997                          Object_Definition   =>
4998                            New_Occurrence_Of (Formal_Type, Loc)));
4999
5000                      Append_To (Extra_Formal_Statements,
5001                        Make_Attribute_Reference (Loc,
5002                          Prefix         => New_Occurrence_Of (
5003                                              Formal_Type, Loc),
5004                          Attribute_Name => Name_Read,
5005                          Expressions    => New_List (
5006                            Make_Selected_Component (Loc,
5007                              Prefix        => Request_Parameter,
5008                              Selector_Name => Name_Params),
5009                            New_Occurrence_Of (Formal_Entity, Loc))));
5010
5011                      --  Note: the call to Set_Extra_Constrained below relies
5012                      --  on the fact that Object's Ekind has been set by
5013                      --  Build_Actual_Object_Declaration.
5014
5015                      Set_Extra_Constrained (Object, Formal_Entity);
5016                   end;
5017                end if;
5018             end;
5019
5020             Next (Current_Parameter);
5021          end loop;
5022
5023          --  Append the formal statements list at the end of regular statements
5024
5025          Append_List_To (Statements, Extra_Formal_Statements);
5026
5027          if Nkind (Specification (Vis_Decl)) = N_Function_Specification then
5028
5029             --  The remote subprogram is a function. We build an inner block to
5030             --  be able to hold a potentially unconstrained result in a
5031             --  variable.
5032
5033             declare
5034                Etyp   : constant Entity_Id :=
5035                           Etype (Result_Definition (Specification (Vis_Decl)));
5036                Result : constant Node_Id   :=
5037                           Make_Defining_Identifier (Loc,
5038                              New_Internal_Name ('R'));
5039             begin
5040                Inner_Decls := New_List (
5041                  Make_Object_Declaration (Loc,
5042                    Defining_Identifier => Result,
5043                    Constant_Present    => True,
5044                    Object_Definition   => New_Occurrence_Of (Etyp, Loc),
5045                    Expression          =>
5046                      Make_Function_Call (Loc,
5047                        Name                   => Called_Subprogram,
5048                        Parameter_Associations => Parameter_List)));
5049
5050                if Is_Class_Wide_Type (Etyp) then
5051
5052                   --  For a remote call to a function with a class-wide type,
5053                   --  check that the returned value satisfies the requirements
5054                   --  of E.4(18).
5055
5056                   Append_To (Inner_Decls,
5057                     Make_Transportable_Check (Loc,
5058                       New_Occurrence_Of (Result, Loc)));
5059
5060                end if;
5061
5062                Append_To (After_Statements,
5063                  Make_Attribute_Reference (Loc,
5064                    Prefix         => New_Occurrence_Of (Etyp, Loc),
5065                    Attribute_Name => Name_Output,
5066                    Expressions    => New_List (
5067                      Make_Selected_Component (Loc,
5068                        Prefix        => Request_Parameter,
5069                        Selector_Name => Name_Result),
5070                      New_Occurrence_Of (Result, Loc))));
5071             end;
5072
5073             Append_To (Statements,
5074               Make_Block_Statement (Loc,
5075                 Declarations               => Inner_Decls,
5076                 Handled_Statement_Sequence =>
5077                   Make_Handled_Sequence_Of_Statements (Loc,
5078                     Statements => After_Statements)));
5079
5080          else
5081             --  The remote subprogram is a procedure. We do not need any inner
5082             --  block in this case.
5083
5084             if Dynamically_Asynchronous then
5085                Append_To (Decls,
5086                  Make_Object_Declaration (Loc,
5087                    Defining_Identifier => Dynamic_Async,
5088                    Object_Definition   =>
5089                      New_Occurrence_Of (Standard_Boolean, Loc)));
5090
5091                Append_To (Statements,
5092                  Make_Attribute_Reference (Loc,
5093                    Prefix         => New_Occurrence_Of (Standard_Boolean, Loc),
5094                    Attribute_Name => Name_Read,
5095                    Expressions    => New_List (
5096                      Make_Selected_Component (Loc,
5097                        Prefix        => Request_Parameter,
5098                        Selector_Name => Name_Params),
5099                      New_Occurrence_Of (Dynamic_Async, Loc))));
5100             end if;
5101
5102             Append_To (Statements,
5103               Make_Procedure_Call_Statement (Loc,
5104                 Name                   => Called_Subprogram,
5105                 Parameter_Associations => Parameter_List));
5106
5107             Append_List_To (Statements, After_Statements);
5108          end if;
5109
5110          if Asynchronous and then not Dynamically_Asynchronous then
5111
5112             --  For an asynchronous procedure, add a null exception handler
5113
5114             Excep_Handlers := New_List (
5115               Make_Implicit_Exception_Handler (Loc,
5116                 Exception_Choices => New_List (Make_Others_Choice (Loc)),
5117                 Statements        => New_List (Make_Null_Statement (Loc))));
5118
5119          else
5120             --  In the other cases, if an exception is raised, then the
5121             --  exception occurrence is copied into the output stream and
5122             --  no other output parameter is written.
5123
5124             Excep_Choice :=
5125               Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
5126
5127             Excep_Code := New_List (
5128               Make_Attribute_Reference (Loc,
5129                 Prefix         =>
5130                   New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc),
5131                 Attribute_Name => Name_Write,
5132                 Expressions    => New_List (
5133                                     Make_Selected_Component (Loc,
5134                                       Prefix        => Request_Parameter,
5135                                       Selector_Name => Name_Result),
5136                                     New_Occurrence_Of (Excep_Choice, Loc))));
5137
5138             if Dynamically_Asynchronous then
5139                Excep_Code := New_List (
5140                  Make_Implicit_If_Statement (Vis_Decl,
5141                    Condition       => Make_Op_Not (Loc,
5142                      New_Occurrence_Of (Dynamic_Async, Loc)),
5143                    Then_Statements => Excep_Code));
5144             end if;
5145
5146             Excep_Handlers := New_List (
5147               Make_Implicit_Exception_Handler (Loc,
5148                 Choice_Parameter   => Excep_Choice,
5149                 Exception_Choices  => New_List (Make_Others_Choice (Loc)),
5150                 Statements         => Excep_Code));
5151
5152          end if;
5153
5154          Subp_Spec :=
5155            Make_Procedure_Specification (Loc,
5156              Defining_Unit_Name       =>
5157                Make_Defining_Identifier (Loc, New_Internal_Name ('F')),
5158
5159              Parameter_Specifications => New_List (
5160                Make_Parameter_Specification (Loc,
5161                  Defining_Identifier => Request_Parameter,
5162                  Parameter_Type      =>
5163                    New_Occurrence_Of (RTE (RE_Request_Access), Loc))));
5164
5165          return
5166            Make_Subprogram_Body (Loc,
5167              Specification              => Subp_Spec,
5168              Declarations               => Decls,
5169              Handled_Statement_Sequence =>
5170                Make_Handled_Sequence_Of_Statements (Loc,
5171                  Statements         => Statements,
5172                  Exception_Handlers => Excep_Handlers));
5173       end Build_Subprogram_Receiving_Stubs;
5174
5175       ------------
5176       -- Result --
5177       ------------
5178
5179       function Result return Node_Id is
5180       begin
5181          return Make_Identifier (Loc, Name_V);
5182       end Result;
5183
5184       ----------------------
5185       -- Stream_Parameter --
5186       ----------------------
5187
5188       function Stream_Parameter return Node_Id is
5189       begin
5190          return Make_Identifier (Loc, Name_S);
5191       end Stream_Parameter;
5192
5193    end GARLIC_Support;
5194
5195    -------------------------------
5196    -- Get_And_Reset_RACW_Bodies --
5197    -------------------------------
5198
5199    function Get_And_Reset_RACW_Bodies (RACW_Type : Entity_Id) return List_Id is
5200       Desig : constant Entity_Id := Etype (Designated_Type (RACW_Type));
5201       Stub_Elements : Stub_Structure := Stubs_Table.Get (Desig);
5202
5203       Body_Decls : List_Id;
5204       --  Returned list of declarations
5205
5206    begin
5207       if Stub_Elements = Empty_Stub_Structure then
5208
5209          --  Stub elements may be missing as a consequence of a previously
5210          --  detected error.
5211
5212          return No_List;
5213       end if;
5214
5215       Body_Decls := Stub_Elements.Body_Decls;
5216       Stub_Elements.Body_Decls := No_List;
5217       Stubs_Table.Set (Desig, Stub_Elements);
5218       return Body_Decls;
5219    end Get_And_Reset_RACW_Bodies;
5220
5221    -----------------------
5222    -- Get_Stub_Elements --
5223    -----------------------
5224
5225    function Get_Stub_Elements (RACW_Type : Entity_Id) return Stub_Structure is
5226       Desig         : constant Entity_Id :=
5227                         Etype (Designated_Type (RACW_Type));
5228       Stub_Elements : constant Stub_Structure := Stubs_Table.Get (Desig);
5229    begin
5230       pragma Assert (Stub_Elements /= Empty_Stub_Structure);
5231       return Stub_Elements;
5232    end Get_Stub_Elements;
5233
5234    -----------------------
5235    -- Get_Subprogram_Id --
5236    -----------------------
5237
5238    function Get_Subprogram_Id (Def : Entity_Id) return String_Id is
5239       Result : constant String_Id := Get_Subprogram_Ids (Def).Str_Identifier;
5240    begin
5241       pragma Assert (Result /= No_String);
5242       return Result;
5243    end Get_Subprogram_Id;
5244
5245    -----------------------
5246    -- Get_Subprogram_Id --
5247    -----------------------
5248
5249    function Get_Subprogram_Id (Def : Entity_Id) return Int is
5250    begin
5251       return Get_Subprogram_Ids (Def).Int_Identifier;
5252    end Get_Subprogram_Id;
5253
5254    ------------------------
5255    -- Get_Subprogram_Ids --
5256    ------------------------
5257
5258    function Get_Subprogram_Ids
5259      (Def : Entity_Id) return Subprogram_Identifiers
5260    is
5261    begin
5262       return Subprogram_Identifier_Table.Get (Def);
5263    end Get_Subprogram_Ids;
5264
5265    ----------
5266    -- Hash --
5267    ----------
5268
5269    function Hash (F : Entity_Id) return Hash_Index is
5270    begin
5271       return Hash_Index (Natural (F) mod Positive (Hash_Index'Last + 1));
5272    end Hash;
5273
5274    function Hash (F : Name_Id) return Hash_Index is
5275    begin
5276       return Hash_Index (Natural (F) mod Positive (Hash_Index'Last + 1));
5277    end Hash;
5278
5279    --------------------------
5280    -- Input_With_Tag_Check --
5281    --------------------------
5282
5283    function Input_With_Tag_Check
5284      (Loc      : Source_Ptr;
5285       Var_Type : Entity_Id;
5286       Stream   : Node_Id) return Node_Id
5287    is
5288    begin
5289       return
5290         Make_Subprogram_Body (Loc,
5291           Specification              => Make_Function_Specification (Loc,
5292             Defining_Unit_Name =>
5293               Make_Defining_Identifier (Loc, New_Internal_Name ('S')),
5294             Result_Definition  => New_Occurrence_Of (Var_Type, Loc)),
5295           Declarations               => No_List,
5296           Handled_Statement_Sequence =>
5297             Make_Handled_Sequence_Of_Statements (Loc, New_List (
5298               Make_Tag_Check (Loc,
5299                 Make_Simple_Return_Statement (Loc,
5300                   Make_Attribute_Reference (Loc,
5301                     Prefix         => New_Occurrence_Of (Var_Type, Loc),
5302                     Attribute_Name => Name_Input,
5303                     Expressions    =>
5304                       New_List (Stream)))))));
5305    end Input_With_Tag_Check;
5306
5307    --------------------------------
5308    -- Is_RACW_Controlling_Formal --
5309    --------------------------------
5310
5311    function Is_RACW_Controlling_Formal
5312      (Parameter : Node_Id;
5313       Stub_Type : Entity_Id) return Boolean
5314    is
5315       Typ : Entity_Id;
5316
5317    begin
5318       --  If the kind of the parameter is E_Void, then it is not a
5319       --  controlling formal (this can happen in the context of RAS).
5320
5321       if Ekind (Defining_Identifier (Parameter)) = E_Void then
5322          return False;
5323       end if;
5324
5325       --  If the parameter is not a controlling formal, then it cannot
5326       --  be possibly a RACW_Controlling_Formal.
5327
5328       if not Is_Controlling_Formal (Defining_Identifier (Parameter)) then
5329          return False;
5330       end if;
5331
5332       Typ := Parameter_Type (Parameter);
5333       return (Nkind (Typ) = N_Access_Definition
5334                and then Etype (Subtype_Mark (Typ)) = Stub_Type)
5335         or else Etype (Typ) = Stub_Type;
5336    end Is_RACW_Controlling_Formal;
5337
5338    ------------------------------
5339    -- Make_Transportable_Check --
5340    ------------------------------
5341
5342    function Make_Transportable_Check
5343      (Loc  : Source_Ptr;
5344       Expr : Node_Id) return Node_Id is
5345    begin
5346       return
5347         Make_Raise_Program_Error (Loc,
5348           Condition       =>
5349             Make_Op_Not (Loc,
5350               Build_Get_Transportable (Loc,
5351                 Make_Selected_Component (Loc,
5352                   Prefix        => Expr,
5353                   Selector_Name => Make_Identifier (Loc, Name_uTag)))),
5354           Reason => PE_Non_Transportable_Actual);
5355    end Make_Transportable_Check;
5356
5357    -----------------------------
5358    -- Make_Selected_Component --
5359    -----------------------------
5360
5361    function Make_Selected_Component
5362      (Loc           : Source_Ptr;
5363       Prefix        : Entity_Id;
5364       Selector_Name : Name_Id) return Node_Id
5365    is
5366    begin
5367       return Make_Selected_Component (Loc,
5368                Prefix        => New_Occurrence_Of (Prefix, Loc),
5369                Selector_Name => Make_Identifier (Loc, Selector_Name));
5370    end Make_Selected_Component;
5371
5372    --------------------
5373    -- Make_Tag_Check --
5374    --------------------
5375
5376    function Make_Tag_Check (Loc : Source_Ptr; N : Node_Id) return Node_Id is
5377       Occ : constant Entity_Id :=
5378               Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
5379
5380    begin
5381       return Make_Block_Statement (Loc,
5382         Handled_Statement_Sequence =>
5383           Make_Handled_Sequence_Of_Statements (Loc,
5384             Statements         => New_List (N),
5385
5386             Exception_Handlers => New_List (
5387               Make_Implicit_Exception_Handler (Loc,
5388                 Choice_Parameter => Occ,
5389
5390                 Exception_Choices =>
5391                   New_List (New_Occurrence_Of (RTE (RE_Tag_Error), Loc)),
5392
5393                 Statements =>
5394                   New_List (Make_Procedure_Call_Statement (Loc,
5395                     New_Occurrence_Of
5396                       (RTE (RE_Raise_Program_Error_Unknown_Tag), Loc),
5397                     New_List (New_Occurrence_Of (Occ, Loc))))))));
5398    end Make_Tag_Check;
5399
5400    ----------------------------
5401    -- Need_Extra_Constrained --
5402    ----------------------------
5403
5404    function Need_Extra_Constrained (Parameter : Node_Id) return Boolean is
5405       Etyp : constant Entity_Id := Etype (Parameter_Type (Parameter));
5406    begin
5407       return Out_Present (Parameter)
5408         and then Has_Discriminants (Etyp)
5409         and then not Is_Constrained (Etyp)
5410         and then not Is_Indefinite_Subtype (Etyp);
5411    end Need_Extra_Constrained;
5412
5413    ------------------------------------
5414    -- Pack_Entity_Into_Stream_Access --
5415    ------------------------------------
5416
5417    function Pack_Entity_Into_Stream_Access
5418      (Loc    : Source_Ptr;
5419       Stream : Node_Id;
5420       Object : Entity_Id;
5421       Etyp   : Entity_Id := Empty) return Node_Id
5422    is
5423       Typ : Entity_Id;
5424
5425    begin
5426       if Present (Etyp) then
5427          Typ := Etyp;
5428       else
5429          Typ := Etype (Object);
5430       end if;
5431
5432       return
5433         Pack_Node_Into_Stream_Access (Loc,
5434           Stream => Stream,
5435           Object => New_Occurrence_Of (Object, Loc),
5436           Etyp   => Typ);
5437    end Pack_Entity_Into_Stream_Access;
5438
5439    ---------------------------
5440    -- Pack_Node_Into_Stream --
5441    ---------------------------
5442
5443    function Pack_Node_Into_Stream
5444      (Loc    : Source_Ptr;
5445       Stream : Entity_Id;
5446       Object : Node_Id;
5447       Etyp   : Entity_Id) return Node_Id
5448    is
5449       Write_Attribute : Name_Id := Name_Write;
5450
5451    begin
5452       if not Is_Constrained (Etyp) then
5453          Write_Attribute := Name_Output;
5454       end if;
5455
5456       return
5457         Make_Attribute_Reference (Loc,
5458           Prefix         => New_Occurrence_Of (Etyp, Loc),
5459           Attribute_Name => Write_Attribute,
5460           Expressions    => New_List (
5461             Make_Attribute_Reference (Loc,
5462               Prefix         => New_Occurrence_Of (Stream, Loc),
5463               Attribute_Name => Name_Access),
5464             Object));
5465    end Pack_Node_Into_Stream;
5466
5467    ----------------------------------
5468    -- Pack_Node_Into_Stream_Access --
5469    ----------------------------------
5470
5471    function Pack_Node_Into_Stream_Access
5472      (Loc    : Source_Ptr;
5473       Stream : Node_Id;
5474       Object : Node_Id;
5475       Etyp   : Entity_Id) return Node_Id
5476    is
5477       Write_Attribute : Name_Id := Name_Write;
5478
5479    begin
5480       if not Is_Constrained (Etyp) then
5481          Write_Attribute := Name_Output;
5482       end if;
5483
5484       return
5485         Make_Attribute_Reference (Loc,
5486           Prefix         => New_Occurrence_Of (Etyp, Loc),
5487           Attribute_Name => Write_Attribute,
5488           Expressions    => New_List (
5489             Stream,
5490             Object));
5491    end Pack_Node_Into_Stream_Access;
5492
5493    ---------------------
5494    -- PolyORB_Support --
5495    ---------------------
5496
5497    package body PolyORB_Support is
5498
5499       --  Local subprograms
5500
5501       procedure Add_RACW_Read_Attribute
5502         (RACW_Type        : Entity_Id;
5503          Stub_Type        : Entity_Id;
5504          Stub_Type_Access : Entity_Id;
5505          Body_Decls       : List_Id);
5506       --  Add Read attribute for the RACW type. The declaration and attribute
5507       --  definition clauses are inserted right after the declaration of
5508       --  RACW_Type. If Body_Decls is not No_List, the subprogram body is
5509       --  appended to it (case where the RACW declaration is in the main unit).
5510
5511       procedure Add_RACW_Write_Attribute
5512         (RACW_Type        : Entity_Id;
5513          Stub_Type        : Entity_Id;
5514          Stub_Type_Access : Entity_Id;
5515          Body_Decls       : List_Id);
5516       --  Same as above for the Write attribute
5517
5518       procedure Add_RACW_From_Any
5519         (RACW_Type        : Entity_Id;
5520          Body_Decls       : List_Id);
5521       --  Add the From_Any TSS for this RACW type
5522
5523       procedure Add_RACW_To_Any
5524         (RACW_Type        : Entity_Id;
5525          Body_Decls       : List_Id);
5526       --  Add the To_Any TSS for this RACW type
5527
5528       procedure Add_RACW_TypeCode
5529         (Designated_Type : Entity_Id;
5530          RACW_Type       : Entity_Id;
5531          Body_Decls      : List_Id);
5532       --  Add the TypeCode TSS for this RACW type
5533
5534       procedure Add_RAS_From_Any (RAS_Type : Entity_Id);
5535       --  Add the From_Any TSS for this RAS type
5536
5537       procedure Add_RAS_To_Any   (RAS_Type : Entity_Id);
5538       --  Add the To_Any TSS for this RAS type
5539
5540       procedure Add_RAS_TypeCode (RAS_Type : Entity_Id);
5541       --  Add the TypeCode TSS for this RAS type
5542
5543       procedure Add_RAS_Access_TSS (N : Node_Id);
5544       --  Add a subprogram body for RAS Access TSS
5545
5546       -------------------------------------
5547       -- Add_Obj_RPC_Receiver_Completion --
5548       -------------------------------------
5549
5550       procedure Add_Obj_RPC_Receiver_Completion
5551         (Loc           : Source_Ptr;
5552          Decls         : List_Id;
5553          RPC_Receiver  : Entity_Id;
5554          Stub_Elements : Stub_Structure)
5555       is
5556          Desig : constant Entity_Id :=
5557            Etype (Designated_Type (Stub_Elements.RACW_Type));
5558       begin
5559          Append_To (Decls,
5560            Make_Procedure_Call_Statement (Loc,
5561               Name =>
5562                 New_Occurrence_Of (
5563                   RTE (RE_Register_Obj_Receiving_Stub), Loc),
5564
5565                 Parameter_Associations => New_List (
5566
5567                --  Name
5568
5569                 Make_String_Literal (Loc,
5570                   Full_Qualified_Name (Desig)),
5571
5572                --  Handler
5573
5574                 Make_Attribute_Reference (Loc,
5575                   Prefix =>
5576                     New_Occurrence_Of (
5577                       Defining_Unit_Name (Parent (RPC_Receiver)), Loc),
5578                   Attribute_Name =>
5579                     Name_Access),
5580
5581                --  Receiver
5582
5583                 Make_Attribute_Reference (Loc,
5584                   Prefix =>
5585                     New_Occurrence_Of (
5586                       Defining_Identifier (
5587                         Stub_Elements.RPC_Receiver_Decl), Loc),
5588                   Attribute_Name =>
5589                     Name_Access))));
5590       end Add_Obj_RPC_Receiver_Completion;
5591
5592       -----------------------
5593       -- Add_RACW_Features --
5594       -----------------------
5595
5596       procedure Add_RACW_Features
5597         (RACW_Type         : Entity_Id;
5598          Desig             : Entity_Id;
5599          Stub_Type         : Entity_Id;
5600          Stub_Type_Access  : Entity_Id;
5601          RPC_Receiver_Decl : Node_Id;
5602          Body_Decls        : List_Id)
5603       is
5604          pragma Warnings (Off);
5605          pragma Unreferenced (RPC_Receiver_Decl);
5606          pragma Warnings (On);
5607
5608       begin
5609          Add_RACW_From_Any
5610            (RACW_Type           => RACW_Type,
5611             Body_Decls          => Body_Decls);
5612
5613          Add_RACW_To_Any
5614            (RACW_Type           => RACW_Type,
5615             Body_Decls          => Body_Decls);
5616
5617          Add_RACW_Write_Attribute
5618            (RACW_Type           => RACW_Type,
5619             Stub_Type           => Stub_Type,
5620             Stub_Type_Access    => Stub_Type_Access,
5621             Body_Decls          => Body_Decls);
5622
5623          Add_RACW_Read_Attribute
5624            (RACW_Type           => RACW_Type,
5625             Stub_Type           => Stub_Type,
5626             Stub_Type_Access    => Stub_Type_Access,
5627             Body_Decls          => Body_Decls);
5628
5629          Add_RACW_TypeCode
5630            (Designated_Type     => Desig,
5631             RACW_Type           => RACW_Type,
5632             Body_Decls          => Body_Decls);
5633       end Add_RACW_Features;
5634
5635       -----------------------
5636       -- Add_RACW_From_Any --
5637       -----------------------
5638
5639       procedure Add_RACW_From_Any
5640         (RACW_Type        : Entity_Id;
5641          Body_Decls       : List_Id)
5642       is
5643          Loc    : constant Source_Ptr := Sloc (RACW_Type);
5644          Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
5645
5646          Fnam   : constant Entity_Id :=
5647                     Make_Defining_Identifier (Loc,
5648                       Chars => New_External_Name (Chars (RACW_Type), 'F'));
5649
5650          Func_Spec : Node_Id;
5651          Func_Decl : Node_Id;
5652          Func_Body : Node_Id;
5653
5654          Statements       : List_Id;
5655          --  Various parts of the subprogram
5656
5657          Any_Parameter  : constant Entity_Id :=
5658                             Make_Defining_Identifier (Loc, Name_A);
5659
5660          Asynchronous_Flag : constant Entity_Id :=
5661                                Asynchronous_Flags_Table.Get (RACW_Type);
5662          --  The flag object declared in Add_RACW_Asynchronous_Flag
5663
5664       begin
5665          Func_Spec :=
5666            Make_Function_Specification (Loc,
5667              Defining_Unit_Name =>
5668                Fnam,
5669              Parameter_Specifications => New_List (
5670                Make_Parameter_Specification (Loc,
5671                  Defining_Identifier =>
5672                    Any_Parameter,
5673                  Parameter_Type =>
5674                    New_Occurrence_Of (RTE (RE_Any), Loc))),
5675              Result_Definition => New_Occurrence_Of (RACW_Type, Loc));
5676
5677          --  NOTE: The usage occurrences of RACW_Parameter must refer to the
5678          --  entity in the declaration spec, not those of the body spec.
5679
5680          Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
5681          Insert_After (Declaration_Node (RACW_Type), Func_Decl);
5682          Set_Renaming_TSS (RACW_Type, Fnam, TSS_From_Any);
5683
5684          if No (Body_Decls) then
5685             return;
5686          end if;
5687
5688          --  ??? Issue with asynchronous calls here: the Asynchronous flag is
5689          --  set on the stub type if, and only if, the RACW type has a pragma
5690          --  Asynchronous. This is incorrect for RACWs that implement RAS
5691          --  types, because in that case the /designated subprogram/ (not the
5692          --  type) might be asynchronous, and that causes the stub to need to
5693          --  be asynchronous too. A solution is to transport a RAS as a struct
5694          --  containing a RACW and an asynchronous flag, and to properly alter
5695          --  the Asynchronous component in the stub type in the RAS's _From_Any
5696          --  TSS.
5697
5698          Statements := New_List (
5699            Make_Simple_Return_Statement (Loc,
5700              Expression => Unchecked_Convert_To (RACW_Type,
5701                Make_Function_Call (Loc,
5702                  Name => New_Occurrence_Of (RTE (RE_Get_RACW), Loc),
5703                  Parameter_Associations => New_List (
5704                    Make_Function_Call (Loc,
5705                      Name => New_Occurrence_Of (RTE (RE_FA_ObjRef), Loc),
5706                      Parameter_Associations => New_List (
5707                        New_Occurrence_Of (Any_Parameter, Loc))),
5708                    Build_Stub_Tag (Loc, RACW_Type),
5709                    New_Occurrence_Of (Boolean_Literals (Is_RAS), Loc),
5710                    New_Occurrence_Of (Asynchronous_Flag, Loc))))));
5711
5712          Func_Body :=
5713            Make_Subprogram_Body (Loc,
5714              Specification => Copy_Specification (Loc, Func_Spec),
5715              Declarations  => No_List,
5716              Handled_Statement_Sequence =>
5717                Make_Handled_Sequence_Of_Statements (Loc,
5718                  Statements => Statements));
5719
5720          Append_To (Body_Decls, Func_Body);
5721       end Add_RACW_From_Any;
5722
5723       -----------------------------
5724       -- Add_RACW_Read_Attribute --
5725       -----------------------------
5726
5727       procedure Add_RACW_Read_Attribute
5728         (RACW_Type        : Entity_Id;
5729          Stub_Type        : Entity_Id;
5730          Stub_Type_Access : Entity_Id;
5731          Body_Decls       : List_Id)
5732       is
5733          pragma Warnings (Off);
5734          pragma Unreferenced (Stub_Type, Stub_Type_Access);
5735          pragma Warnings (On);
5736          Loc : constant Source_Ptr := Sloc (RACW_Type);
5737
5738          Proc_Decl : Node_Id;
5739          Attr_Decl : Node_Id;
5740
5741          Body_Node : Node_Id;
5742
5743          Decls      : constant List_Id   := New_List;
5744          Statements : constant List_Id   := New_List;
5745          Reference  : constant Entity_Id :=
5746                         Make_Defining_Identifier (Loc, Name_R);
5747          --  Various parts of the procedure
5748
5749          Pnam : constant Entity_Id := Make_Defining_Identifier (Loc,
5750                                         New_Internal_Name ('R'));
5751
5752          Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
5753
5754          Asynchronous_Flag : constant Entity_Id :=
5755                                Asynchronous_Flags_Table.Get (RACW_Type);
5756          pragma Assert (Present (Asynchronous_Flag));
5757
5758          function Stream_Parameter return Node_Id;
5759          function Result return Node_Id;
5760
5761          --  Functions to create occurrences of the formal parameter names
5762
5763          ------------
5764          -- Result --
5765          ------------
5766
5767          function Result return Node_Id is
5768          begin
5769             return Make_Identifier (Loc, Name_V);
5770          end Result;
5771
5772          ----------------------
5773          -- Stream_Parameter --
5774          ----------------------
5775
5776          function Stream_Parameter return Node_Id is
5777          begin
5778             return Make_Identifier (Loc, Name_S);
5779          end Stream_Parameter;
5780
5781       --  Start of processing for Add_RACW_Read_Attribute
5782
5783       begin
5784          Build_Stream_Procedure
5785            (Loc, RACW_Type, Body_Node, Pnam, Statements, Outp => True);
5786
5787          Proc_Decl := Make_Subprogram_Declaration (Loc,
5788            Copy_Specification (Loc, Specification (Body_Node)));
5789
5790          Attr_Decl :=
5791            Make_Attribute_Definition_Clause (Loc,
5792              Name       => New_Occurrence_Of (RACW_Type, Loc),
5793              Chars      => Name_Read,
5794              Expression =>
5795                New_Occurrence_Of (
5796                  Defining_Unit_Name (Specification (Proc_Decl)), Loc));
5797
5798          Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
5799          Insert_After (Proc_Decl, Attr_Decl);
5800
5801          if No (Body_Decls) then
5802             return;
5803          end if;
5804
5805          Append_To (Decls,
5806            Make_Object_Declaration (Loc,
5807              Defining_Identifier =>
5808                Reference,
5809              Object_Definition =>
5810                New_Occurrence_Of (RTE (RE_Object_Ref), Loc)));
5811
5812          Append_List_To (Statements, New_List (
5813            Make_Attribute_Reference (Loc,
5814              Prefix         =>
5815                New_Occurrence_Of (RTE (RE_Object_Ref), Loc),
5816              Attribute_Name => Name_Read,
5817              Expressions    => New_List (
5818                Stream_Parameter,
5819                New_Occurrence_Of (Reference, Loc))),
5820
5821            Make_Assignment_Statement (Loc,
5822              Name       =>
5823                Result,
5824              Expression =>
5825                Unchecked_Convert_To (RACW_Type,
5826                  Make_Function_Call (Loc,
5827                    Name                   =>
5828                      New_Occurrence_Of (RTE (RE_Get_RACW), Loc),
5829                    Parameter_Associations => New_List (
5830                      New_Occurrence_Of (Reference, Loc),
5831                      Build_Stub_Tag (Loc, RACW_Type),
5832                      New_Occurrence_Of (Boolean_Literals (Is_RAS), Loc),
5833                      New_Occurrence_Of (Asynchronous_Flag, Loc)))))));
5834
5835          Set_Declarations (Body_Node, Decls);
5836          Append_To (Body_Decls, Body_Node);
5837       end Add_RACW_Read_Attribute;
5838
5839       ---------------------
5840       -- Add_RACW_To_Any --
5841       ---------------------
5842
5843       procedure Add_RACW_To_Any
5844         (RACW_Type        : Entity_Id;
5845          Body_Decls       : List_Id)
5846       is
5847          Loc : constant Source_Ptr := Sloc (RACW_Type);
5848
5849          Fnam : constant Entity_Id :=
5850                   Make_Defining_Identifier (Loc,
5851                     Chars => New_External_Name (Chars (RACW_Type), 'T'));
5852
5853          Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
5854
5855          Stub_Elements : constant Stub_Structure :=
5856                            Get_Stub_Elements (RACW_Type);
5857
5858          Func_Spec : Node_Id;
5859          Func_Decl : Node_Id;
5860          Func_Body : Node_Id;
5861
5862          Decls             : List_Id;
5863          Statements        : List_Id;
5864          --  Various parts of the subprogram
5865
5866          RACW_Parameter : constant Entity_Id :=
5867                             Make_Defining_Identifier (Loc, Name_R);
5868
5869          Reference         : constant Entity_Id :=
5870                                Make_Defining_Identifier
5871                                  (Loc, New_Internal_Name ('R'));
5872          Any               : constant Entity_Id :=
5873                                Make_Defining_Identifier
5874                                  (Loc, New_Internal_Name ('A'));
5875
5876       begin
5877          Func_Spec :=
5878            Make_Function_Specification (Loc,
5879              Defining_Unit_Name =>
5880                Fnam,
5881              Parameter_Specifications => New_List (
5882                Make_Parameter_Specification (Loc,
5883                  Defining_Identifier =>
5884                    RACW_Parameter,
5885                  Parameter_Type =>
5886                    New_Occurrence_Of (RACW_Type, Loc))),
5887              Result_Definition => New_Occurrence_Of (RTE (RE_Any), Loc));
5888
5889          --  NOTE: The usage occurrences of RACW_Parameter must refer to the
5890          --  entity in the declaration spec, not in the body spec.
5891
5892          Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
5893
5894          Insert_After (Declaration_Node (RACW_Type), Func_Decl);
5895          Set_Renaming_TSS (RACW_Type, Fnam, TSS_To_Any);
5896
5897          if No (Body_Decls) then
5898             return;
5899          end if;
5900
5901          --  Generate:
5902
5903          --    R : constant Object_Ref :=
5904          --          Get_Reference
5905          --            (Address!(RACW),
5906          --             "typ",
5907          --             Stub_Type'Tag,
5908          --             Is_RAS,
5909          --             RPC_Receiver'Access);
5910          --    A : Any;
5911
5912          Decls := New_List (
5913            Make_Object_Declaration (Loc,
5914              Defining_Identifier => Reference,
5915              Constant_Present    => True,
5916              Object_Definition   =>
5917                New_Occurrence_Of (RTE (RE_Object_Ref), Loc),
5918              Expression          =>
5919                Make_Function_Call (Loc,
5920                  Name => New_Occurrence_Of (RTE (RE_Get_Reference), Loc),
5921                  Parameter_Associations => New_List (
5922                    Unchecked_Convert_To (RTE (RE_Address),
5923                      New_Occurrence_Of (RACW_Parameter, Loc)),
5924                    Make_String_Literal (Loc,
5925                      Strval => Full_Qualified_Name
5926                                  (Etype (Designated_Type (RACW_Type)))),
5927                    Build_Stub_Tag (Loc, RACW_Type),
5928                    New_Occurrence_Of (Boolean_Literals (Is_RAS), Loc),
5929                    Make_Attribute_Reference (Loc,
5930                      Prefix         =>
5931                        New_Occurrence_Of
5932                          (Defining_Identifier
5933                            (Stub_Elements.RPC_Receiver_Decl), Loc),
5934                      Attribute_Name => Name_Access)))),
5935
5936            Make_Object_Declaration (Loc,
5937              Defining_Identifier => Any,
5938              Object_Definition   => New_Occurrence_Of (RTE (RE_Any), Loc)));
5939
5940          --  Generate:
5941
5942          --    Any := TA_ObjRef (Reference);
5943          --    Set_TC (Any, RPC_Receiver.Obj_TypeCode);
5944          --    return Any;
5945
5946          Statements := New_List (
5947            Make_Assignment_Statement (Loc,
5948              Name => New_Occurrence_Of (Any, Loc),
5949              Expression =>
5950                Make_Function_Call (Loc,
5951                  Name => New_Occurrence_Of (RTE (RE_TA_ObjRef), Loc),
5952                  Parameter_Associations => New_List (
5953                    New_Occurrence_Of (Reference, Loc)))),
5954
5955            Make_Procedure_Call_Statement (Loc,
5956              Name => New_Occurrence_Of (RTE (RE_Set_TC), Loc),
5957              Parameter_Associations => New_List (
5958                New_Occurrence_Of (Any, Loc),
5959                Make_Selected_Component (Loc,
5960                  Prefix =>
5961                      Defining_Identifier (
5962                        Stub_Elements.RPC_Receiver_Decl),
5963                  Selector_Name => Name_Obj_TypeCode))),
5964
5965            Make_Simple_Return_Statement (Loc,
5966              Expression => New_Occurrence_Of (Any, Loc)));
5967
5968          Func_Body :=
5969            Make_Subprogram_Body (Loc,
5970              Specification              => Copy_Specification (Loc, Func_Spec),
5971              Declarations               => Decls,
5972              Handled_Statement_Sequence =>
5973                Make_Handled_Sequence_Of_Statements (Loc,
5974                  Statements => Statements));
5975          Append_To (Body_Decls, Func_Body);
5976       end Add_RACW_To_Any;
5977
5978       -----------------------
5979       -- Add_RACW_TypeCode --
5980       -----------------------
5981
5982       procedure Add_RACW_TypeCode
5983         (Designated_Type  : Entity_Id;
5984          RACW_Type        : Entity_Id;
5985          Body_Decls       : List_Id)
5986       is
5987          Loc : constant Source_Ptr := Sloc (RACW_Type);
5988
5989          Fnam : constant Entity_Id :=
5990                   Make_Defining_Identifier (Loc,
5991                     Chars => New_External_Name (Chars (RACW_Type), 'Y'));
5992
5993          Stub_Elements : constant Stub_Structure :=
5994                            Stubs_Table.Get (Designated_Type);
5995          pragma Assert (Stub_Elements /= Empty_Stub_Structure);
5996
5997          Func_Spec : Node_Id;
5998          Func_Decl : Node_Id;
5999          Func_Body : Node_Id;
6000
6001       begin
6002
6003          --  The spec for this subprogram has a dummy 'access RACW' argument,
6004          --  which serves only for overloading purposes.
6005
6006          Func_Spec :=
6007            Make_Function_Specification (Loc,
6008              Defining_Unit_Name => Fnam,
6009              Result_Definition  => New_Occurrence_Of (RTE (RE_TypeCode), Loc));
6010
6011          --  NOTE: The usage occurrences of RACW_Parameter must refer to the
6012          --  entity in the declaration spec, not those of the body spec.
6013
6014          Func_Decl := Make_Subprogram_Declaration (Loc, Func_Spec);
6015          Insert_After (Declaration_Node (RACW_Type), Func_Decl);
6016          Set_Renaming_TSS (RACW_Type, Fnam, TSS_TypeCode);
6017
6018          if No (Body_Decls) then
6019             return;
6020          end if;
6021
6022          Func_Body :=
6023            Make_Subprogram_Body (Loc,
6024              Specification              => Copy_Specification (Loc, Func_Spec),
6025              Declarations               => Empty_List,
6026              Handled_Statement_Sequence =>
6027                Make_Handled_Sequence_Of_Statements (Loc,
6028                  Statements => New_List (
6029                    Make_Simple_Return_Statement (Loc,
6030                      Expression =>
6031                        Make_Selected_Component (Loc,
6032                          Prefix =>
6033                            Defining_Identifier
6034                              (Stub_Elements.RPC_Receiver_Decl),
6035                          Selector_Name => Name_Obj_TypeCode)))));
6036
6037          Append_To (Body_Decls, Func_Body);
6038       end Add_RACW_TypeCode;
6039
6040       ------------------------------
6041       -- Add_RACW_Write_Attribute --
6042       ------------------------------
6043
6044       procedure Add_RACW_Write_Attribute
6045         (RACW_Type        : Entity_Id;
6046          Stub_Type        : Entity_Id;
6047          Stub_Type_Access : Entity_Id;
6048          Body_Decls       : List_Id)
6049       is
6050          pragma Warnings (Off);
6051          pragma Unreferenced (Stub_Type, Stub_Type_Access);
6052          pragma Warnings (On);
6053
6054          Loc : constant Source_Ptr := Sloc (RACW_Type);
6055
6056          Is_RAS : constant Boolean := not Comes_From_Source (RACW_Type);
6057
6058          Stub_Elements : constant Stub_Structure :=
6059                             Get_Stub_Elements (RACW_Type);
6060
6061          Body_Node : Node_Id;
6062          Proc_Decl : Node_Id;
6063          Attr_Decl : Node_Id;
6064
6065          Statements : constant List_Id := New_List;
6066          Pnam : constant Entity_Id :=
6067                   Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
6068
6069          function Stream_Parameter return Node_Id;
6070          function Object return Node_Id;
6071          --  Functions to create occurrences of the formal parameter names
6072
6073          ------------
6074          -- Object --
6075          ------------
6076
6077          function Object return Node_Id is
6078          begin
6079             return Make_Identifier (Loc, Name_V);
6080          end Object;
6081
6082          ----------------------
6083          -- Stream_Parameter --
6084          ----------------------
6085
6086          function Stream_Parameter return Node_Id is
6087          begin
6088             return Make_Identifier (Loc, Name_S);
6089          end Stream_Parameter;
6090
6091       --  Start of processing for Add_RACW_Write_Attribute
6092
6093       begin
6094          Build_Stream_Procedure
6095            (Loc, RACW_Type, Body_Node, Pnam, Statements, Outp => False);
6096
6097          Proc_Decl :=
6098            Make_Subprogram_Declaration (Loc,
6099              Copy_Specification (Loc, Specification (Body_Node)));
6100
6101          Attr_Decl :=
6102            Make_Attribute_Definition_Clause (Loc,
6103              Name       => New_Occurrence_Of (RACW_Type, Loc),
6104              Chars      => Name_Write,
6105              Expression =>
6106                New_Occurrence_Of (
6107                  Defining_Unit_Name (Specification (Proc_Decl)), Loc));
6108
6109          Insert_After (Declaration_Node (RACW_Type), Proc_Decl);
6110          Insert_After (Proc_Decl, Attr_Decl);
6111
6112          if No (Body_Decls) then
6113             return;
6114          end if;
6115
6116          Append_To (Statements,
6117            Pack_Node_Into_Stream_Access (Loc,
6118              Stream => Stream_Parameter,
6119              Object =>
6120                Make_Function_Call (Loc,
6121                  Name => New_Occurrence_Of (RTE (RE_Get_Reference), Loc),
6122                  Parameter_Associations => New_List (
6123                    Unchecked_Convert_To (RTE (RE_Address), Object),
6124                   Make_String_Literal (Loc,
6125                     Strval => Full_Qualified_Name
6126                                 (Etype (Designated_Type (RACW_Type)))),
6127                   Build_Stub_Tag (Loc, RACW_Type),
6128                   New_Occurrence_Of (Boolean_Literals (Is_RAS), Loc),
6129                   Make_Attribute_Reference (Loc,
6130                     Prefix         =>
6131                        New_Occurrence_Of
6132                          (Defining_Identifier
6133                            (Stub_Elements.RPC_Receiver_Decl), Loc),
6134                     Attribute_Name => Name_Access))),
6135
6136              Etyp => RTE (RE_Object_Ref)));
6137
6138          Append_To (Body_Decls, Body_Node);
6139       end Add_RACW_Write_Attribute;
6140
6141       -----------------------
6142       -- Add_RAST_Features --
6143       -----------------------
6144
6145       procedure Add_RAST_Features
6146         (Vis_Decl : Node_Id;
6147          RAS_Type : Entity_Id)
6148       is
6149       begin
6150          Add_RAS_Access_TSS (Vis_Decl);
6151
6152          Add_RAS_From_Any (RAS_Type);
6153          Add_RAS_TypeCode (RAS_Type);
6154
6155          --  To_Any uses TypeCode, and therefore needs to be generated last
6156
6157          Add_RAS_To_Any   (RAS_Type);
6158       end Add_RAST_Features;
6159
6160       ------------------------
6161       -- Add_RAS_Access_TSS --
6162       ------------------------
6163
6164       procedure Add_RAS_Access_TSS (N : Node_Id) is
6165          Loc : constant Source_Ptr := Sloc (N);
6166
6167          Ras_Type : constant Entity_Id := Defining_Identifier (N);
6168          Fat_Type : constant Entity_Id := Equivalent_Type (Ras_Type);
6169          --  Ras_Type is the access to subprogram type; Fat_Type is the
6170          --  corresponding record type.
6171
6172          RACW_Type : constant Entity_Id :=
6173                        Underlying_RACW_Type (Ras_Type);
6174
6175          Stub_Elements : constant Stub_Structure :=
6176                            Get_Stub_Elements (RACW_Type);
6177
6178          Proc : constant Entity_Id :=
6179                   Make_Defining_Identifier (Loc,
6180                     Chars => Make_TSS_Name (Ras_Type, TSS_RAS_Access));
6181
6182          Proc_Spec : Node_Id;
6183
6184          --  Formal parameters
6185
6186          Package_Name : constant Entity_Id :=
6187                           Make_Defining_Identifier (Loc,
6188                             Chars => Name_P);
6189
6190          --  Target package
6191
6192          Subp_Id : constant Entity_Id :=
6193                      Make_Defining_Identifier (Loc,
6194                        Chars => Name_S);
6195
6196          --  Target subprogram
6197
6198          Asynch_P : constant Entity_Id :=
6199                       Make_Defining_Identifier (Loc,
6200                         Chars => Name_Asynchronous);
6201          --  Is the procedure to which the 'Access applies asynchronous?
6202
6203          All_Calls_Remote : constant Entity_Id :=
6204                               Make_Defining_Identifier (Loc,
6205                                 Chars => Name_All_Calls_Remote);
6206          --  True if an All_Calls_Remote pragma applies to the RCI unit
6207          --  that contains the subprogram.
6208
6209          --  Common local variables
6210
6211          Proc_Decls      : List_Id;
6212          Proc_Statements : List_Id;
6213
6214          Subp_Ref : constant Entity_Id :=
6215                       Make_Defining_Identifier (Loc, Name_R);
6216          --  Reference that designates the target subprogram (returned
6217          --  by Get_RAS_Info).
6218
6219          Is_Local : constant Entity_Id :=
6220            Make_Defining_Identifier (Loc, Name_L);
6221          Local_Addr : constant Entity_Id :=
6222            Make_Defining_Identifier (Loc, Name_A);
6223          --  For the call to Get_Local_Address
6224
6225          --  Additional local variables for the remote case
6226
6227          Local_Stub : constant Entity_Id :=
6228                         Make_Defining_Identifier (Loc,
6229                           Chars => New_Internal_Name ('L'));
6230
6231          Stub_Ptr : constant Entity_Id :=
6232                       Make_Defining_Identifier (Loc,
6233                         Chars => New_Internal_Name ('S'));
6234
6235          function Set_Field
6236            (Field_Name : Name_Id;
6237             Value      : Node_Id) return Node_Id;
6238          --  Construct an assignment that sets the named component in the
6239          --  returned record
6240
6241          ---------------
6242          -- Set_Field --
6243          ---------------
6244
6245          function Set_Field
6246            (Field_Name : Name_Id;
6247             Value      : Node_Id) return Node_Id
6248          is
6249          begin
6250             return
6251               Make_Assignment_Statement (Loc,
6252                 Name       =>
6253                   Make_Selected_Component (Loc,
6254                     Prefix        => Stub_Ptr,
6255                     Selector_Name => Field_Name),
6256                 Expression => Value);
6257          end Set_Field;
6258
6259       --  Start of processing for Add_RAS_Access_TSS
6260
6261       begin
6262          Proc_Decls := New_List (
6263
6264          --  Common declarations
6265
6266            Make_Object_Declaration (Loc,
6267              Defining_Identifier => Subp_Ref,
6268              Object_Definition   =>
6269                New_Occurrence_Of (RTE (RE_Object_Ref), Loc)),
6270
6271            Make_Object_Declaration (Loc,
6272              Defining_Identifier => Is_Local,
6273              Object_Definition   =>
6274                New_Occurrence_Of (Standard_Boolean, Loc)),
6275
6276            Make_Object_Declaration (Loc,
6277              Defining_Identifier => Local_Addr,
6278              Object_Definition   =>
6279                New_Occurrence_Of (RTE (RE_Address), Loc)),
6280
6281            Make_Object_Declaration (Loc,
6282              Defining_Identifier => Local_Stub,
6283              Aliased_Present     => True,
6284              Object_Definition   =>
6285                New_Occurrence_Of (Stub_Elements.Stub_Type, Loc)),
6286
6287            Make_Object_Declaration (Loc,
6288              Defining_Identifier => Stub_Ptr,
6289              Object_Definition   =>
6290                New_Occurrence_Of (Stub_Elements.Stub_Type_Access, Loc),
6291              Expression          =>
6292                Make_Attribute_Reference (Loc,
6293                  Prefix => New_Occurrence_Of (Local_Stub, Loc),
6294                  Attribute_Name => Name_Unchecked_Access)));
6295
6296          Set_Etype (Stub_Ptr, Stub_Elements.Stub_Type_Access);
6297          --  Build_Get_Unique_RP_Call needs this information
6298
6299          --  Get_RAS_Info (Pkg, Subp, R);
6300          --  Obtain a reference to the target subprogram
6301
6302          Proc_Statements := New_List (
6303            Make_Procedure_Call_Statement (Loc,
6304              Name => New_Occurrence_Of (RTE (RE_Get_RAS_Info), Loc),
6305              Parameter_Associations => New_List (
6306                New_Occurrence_Of (Package_Name, Loc),
6307                New_Occurrence_Of (Subp_Id, Loc),
6308                New_Occurrence_Of (Subp_Ref, Loc))),
6309
6310          --  Get_Local_Address (R, L, A);
6311          --  Determine whether the subprogram is local (L), and if so
6312          --  obtain the local address of its proxy (A).
6313
6314            Make_Procedure_Call_Statement (Loc,
6315              Name => New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
6316              Parameter_Associations => New_List (
6317                New_Occurrence_Of (Subp_Ref, Loc),
6318                New_Occurrence_Of (Is_Local, Loc),
6319                New_Occurrence_Of (Local_Addr, Loc))));
6320
6321          --  Note: Here we assume that the Fat_Type is a record containing just
6322          --  an access to a proxy or stub object.
6323
6324          Append_To (Proc_Statements,
6325
6326          --  if L then
6327
6328            Make_Implicit_If_Statement (N,
6329              Condition => New_Occurrence_Of (Is_Local, Loc),
6330
6331              Then_Statements => New_List (
6332
6333          --     if A.Target = null then
6334
6335                Make_Implicit_If_Statement (N,
6336                  Condition =>
6337                    Make_Op_Eq (Loc,
6338                      Make_Selected_Component (Loc,
6339                        Prefix        =>
6340                          Unchecked_Convert_To
6341                            (RTE (RE_RAS_Proxy_Type_Access),
6342                             New_Occurrence_Of (Local_Addr, Loc)),
6343                        Selector_Name => Make_Identifier (Loc, Name_Target)),
6344                      Make_Null (Loc)),
6345
6346                  Then_Statements => New_List (
6347
6348          --        A.Target := Entity_Of (Ref);
6349
6350                    Make_Assignment_Statement (Loc,
6351                      Name =>
6352                        Make_Selected_Component (Loc,
6353                          Prefix        =>
6354                            Unchecked_Convert_To
6355                              (RTE (RE_RAS_Proxy_Type_Access),
6356                               New_Occurrence_Of (Local_Addr, Loc)),
6357                          Selector_Name => Make_Identifier (Loc, Name_Target)),
6358                      Expression =>
6359                        Make_Function_Call (Loc,
6360                          Name => New_Occurrence_Of (RTE (RE_Entity_Of), Loc),
6361                          Parameter_Associations => New_List (
6362                            New_Occurrence_Of (Subp_Ref, Loc)))),
6363
6364          --        Inc_Usage (A.Target);
6365
6366                    Make_Procedure_Call_Statement (Loc,
6367                      Name => New_Occurrence_Of (RTE (RE_Inc_Usage), Loc),
6368                      Parameter_Associations => New_List (
6369                        Make_Selected_Component (Loc,
6370                          Prefix        =>
6371                            Unchecked_Convert_To
6372                              (RTE (RE_RAS_Proxy_Type_Access),
6373                               New_Occurrence_Of (Local_Addr, Loc)),
6374                          Selector_Name =>
6375                            Make_Identifier (Loc, Name_Target)))))),
6376
6377          --     end if;
6378          --     if not All_Calls_Remote then
6379          --        return Fat_Type!(A);
6380          --     end if;
6381
6382                  Make_Implicit_If_Statement (N,
6383                    Condition =>
6384                      Make_Op_Not (Loc,
6385                        Right_Opnd =>
6386                          New_Occurrence_Of (All_Calls_Remote, Loc)),
6387
6388                    Then_Statements => New_List (
6389                      Make_Simple_Return_Statement (Loc,
6390                      Expression =>
6391                        Unchecked_Convert_To
6392                          (Fat_Type, New_Occurrence_Of (Local_Addr, Loc))))))));
6393
6394          Append_List_To (Proc_Statements, New_List (
6395
6396          --  Stub.Target := Entity_Of (Ref);
6397
6398            Set_Field (Name_Target,
6399              Make_Function_Call (Loc,
6400                Name => New_Occurrence_Of (RTE (RE_Entity_Of), Loc),
6401                Parameter_Associations => New_List (
6402                  New_Occurrence_Of (Subp_Ref, Loc)))),
6403
6404          --  Inc_Usage (Stub.Target);
6405
6406            Make_Procedure_Call_Statement (Loc,
6407              Name => New_Occurrence_Of (RTE (RE_Inc_Usage), Loc),
6408              Parameter_Associations => New_List (
6409                Make_Selected_Component (Loc,
6410                  Prefix        => Stub_Ptr,
6411                  Selector_Name => Name_Target))),
6412
6413          --  E.4.1(9) A remote call is asynchronous if it is a call to
6414          --  a procedure, or a call through a value of an access-to-procedure
6415          --  type, to which a pragma Asynchronous applies.
6416
6417          --    Parameter Asynch_P is true when the procedure is asynchronous;
6418          --    Expression Asynch_T is true when the type is asynchronous.
6419
6420            Set_Field (Name_Asynchronous,
6421              Make_Or_Else (Loc,
6422                Left_Opnd  => New_Occurrence_Of (Asynch_P, Loc),
6423                Right_Opnd =>
6424                  New_Occurrence_Of
6425                    (Boolean_Literals (Is_Asynchronous (Ras_Type)), Loc)))));
6426
6427          Append_List_To (Proc_Statements,
6428            Build_Get_Unique_RP_Call (Loc, Stub_Ptr, Stub_Elements.Stub_Type));
6429
6430          Append_To (Proc_Statements,
6431            Make_Simple_Return_Statement (Loc,
6432              Expression =>
6433                Unchecked_Convert_To (Fat_Type,
6434                  New_Occurrence_Of (Stub_Ptr, Loc))));
6435
6436          Proc_Spec :=
6437            Make_Function_Specification (Loc,
6438              Defining_Unit_Name       => Proc,
6439              Parameter_Specifications => New_List (
6440                Make_Parameter_Specification (Loc,
6441                  Defining_Identifier => Package_Name,
6442                  Parameter_Type      =>
6443                    New_Occurrence_Of (Standard_String, Loc)),
6444
6445                Make_Parameter_Specification (Loc,
6446                  Defining_Identifier => Subp_Id,
6447                  Parameter_Type      =>
6448                    New_Occurrence_Of (Standard_String, Loc)),
6449
6450                Make_Parameter_Specification (Loc,
6451                  Defining_Identifier => Asynch_P,
6452                  Parameter_Type      =>
6453                    New_Occurrence_Of (Standard_Boolean, Loc)),
6454
6455                Make_Parameter_Specification (Loc,
6456                  Defining_Identifier => All_Calls_Remote,
6457                  Parameter_Type      =>
6458                    New_Occurrence_Of (Standard_Boolean, Loc))),
6459
6460             Result_Definition =>
6461               New_Occurrence_Of (Fat_Type, Loc));
6462
6463          --  Set the kind and return type of the function to prevent
6464          --  ambiguities between Ras_Type and Fat_Type in subsequent analysis.
6465
6466          Set_Ekind (Proc, E_Function);
6467          Set_Etype (Proc, Fat_Type);
6468
6469          Discard_Node (
6470            Make_Subprogram_Body (Loc,
6471              Specification              => Proc_Spec,
6472              Declarations               => Proc_Decls,
6473              Handled_Statement_Sequence =>
6474                Make_Handled_Sequence_Of_Statements (Loc,
6475                  Statements => Proc_Statements)));
6476
6477          Set_TSS (Fat_Type, Proc);
6478       end Add_RAS_Access_TSS;
6479
6480       ----------------------
6481       -- Add_RAS_From_Any --
6482       ----------------------
6483
6484       procedure Add_RAS_From_Any (RAS_Type : Entity_Id) is
6485          Loc : constant Source_Ptr := Sloc (RAS_Type);
6486
6487          Fnam : constant Entity_Id := Make_Defining_Identifier (Loc,
6488                   Make_TSS_Name (RAS_Type, TSS_From_Any));
6489
6490          Func_Spec : Node_Id;
6491
6492          Statements : List_Id;
6493
6494          Any_Parameter : constant Entity_Id :=
6495                            Make_Defining_Identifier (Loc, Name_A);
6496
6497       begin
6498          Statements := New_List (
6499            Make_Simple_Return_Statement (Loc,
6500              Expression =>
6501                Make_Aggregate (Loc,
6502                  Component_Associations => New_List (
6503                    Make_Component_Association (Loc,
6504                      Choices => New_List (
6505                        Make_Identifier (Loc, Name_Ras)),
6506                      Expression =>
6507                        PolyORB_Support.Helpers.Build_From_Any_Call (
6508                          Underlying_RACW_Type (RAS_Type),
6509                          New_Occurrence_Of (Any_Parameter, Loc),
6510                          No_List))))));
6511
6512          Func_Spec :=
6513            Make_Function_Specification (Loc,
6514              Defining_Unit_Name       => Fnam,
6515              Parameter_Specifications => New_List (
6516                Make_Parameter_Specification (Loc,
6517                  Defining_Identifier => Any_Parameter,
6518                  Parameter_Type => New_Occurrence_Of (RTE (RE_Any), Loc))),
6519              Result_Definition => New_Occurrence_Of (RAS_Type, Loc));
6520
6521          Discard_Node (
6522            Make_Subprogram_Body (Loc,
6523              Specification              => Func_Spec,
6524              Declarations               => No_List,
6525              Handled_Statement_Sequence =>
6526                Make_Handled_Sequence_Of_Statements (Loc,
6527                  Statements => Statements)));
6528          Set_TSS (RAS_Type, Fnam);
6529       end Add_RAS_From_Any;
6530
6531       --------------------
6532       -- Add_RAS_To_Any --
6533       --------------------
6534
6535       procedure Add_RAS_To_Any (RAS_Type : Entity_Id) is
6536          Loc : constant Source_Ptr := Sloc (RAS_Type);
6537
6538          Fnam : constant Entity_Id := Make_Defining_Identifier (Loc,
6539                   Make_TSS_Name (RAS_Type, TSS_To_Any));
6540
6541          Decls      : List_Id;
6542          Statements : List_Id;
6543
6544          Func_Spec : Node_Id;
6545
6546          Any            : constant Entity_Id :=
6547                             Make_Defining_Identifier (Loc,
6548                               Chars => New_Internal_Name ('A'));
6549          RAS_Parameter  : constant Entity_Id :=
6550                             Make_Defining_Identifier (Loc,
6551                               Chars => New_Internal_Name ('R'));
6552          RACW_Parameter : constant Node_Id :=
6553                             Make_Selected_Component (Loc,
6554                               Prefix        => RAS_Parameter,
6555                               Selector_Name => Name_Ras);
6556
6557       begin
6558          --  Object declarations
6559
6560          Set_Etype (RACW_Parameter, Underlying_RACW_Type (RAS_Type));
6561          Decls := New_List (
6562            Make_Object_Declaration (Loc,
6563              Defining_Identifier => Any,
6564              Object_Definition   => New_Occurrence_Of (RTE (RE_Any), Loc),
6565              Expression          =>
6566                PolyORB_Support.Helpers.Build_To_Any_Call
6567                  (RACW_Parameter, No_List)));
6568
6569          Statements := New_List (
6570            Make_Procedure_Call_Statement (Loc,
6571              Name => New_Occurrence_Of (RTE (RE_Set_TC), Loc),
6572              Parameter_Associations => New_List (
6573                New_Occurrence_Of (Any, Loc),
6574                PolyORB_Support.Helpers.Build_TypeCode_Call (Loc,
6575                  RAS_Type, Decls))),
6576
6577            Make_Simple_Return_Statement (Loc,
6578              Expression => New_Occurrence_Of (Any, Loc)));
6579
6580          Func_Spec :=
6581            Make_Function_Specification (Loc,
6582              Defining_Unit_Name => Fnam,
6583              Parameter_Specifications => New_List (
6584                Make_Parameter_Specification (Loc,
6585                  Defining_Identifier => RAS_Parameter,
6586                  Parameter_Type      => New_Occurrence_Of (RAS_Type, Loc))),
6587              Result_Definition => New_Occurrence_Of (RTE (RE_Any), Loc));
6588
6589          Discard_Node (
6590            Make_Subprogram_Body (Loc,
6591              Specification              => Func_Spec,
6592              Declarations               => Decls,
6593              Handled_Statement_Sequence =>
6594                Make_Handled_Sequence_Of_Statements (Loc,
6595                  Statements => Statements)));
6596          Set_TSS (RAS_Type, Fnam);
6597       end Add_RAS_To_Any;
6598
6599       ----------------------
6600       -- Add_RAS_TypeCode --
6601       ----------------------
6602
6603       procedure Add_RAS_TypeCode (RAS_Type : Entity_Id) is
6604          Loc : constant Source_Ptr := Sloc (RAS_Type);
6605
6606          Fnam : constant Entity_Id := Make_Defining_Identifier (Loc,
6607                   Make_TSS_Name (RAS_Type, TSS_TypeCode));
6608
6609          Func_Spec      : Node_Id;
6610          Decls          : constant List_Id := New_List;
6611          Name_String    : String_Id;
6612          Repo_Id_String : String_Id;
6613
6614       begin
6615          Func_Spec :=
6616            Make_Function_Specification (Loc,
6617              Defining_Unit_Name => Fnam,
6618              Result_Definition  => New_Occurrence_Of (RTE (RE_TypeCode), Loc));
6619
6620          PolyORB_Support.Helpers.Build_Name_And_Repository_Id
6621            (RAS_Type, Name_Str => Name_String, Repo_Id_Str => Repo_Id_String);
6622
6623          Discard_Node (
6624            Make_Subprogram_Body (Loc,
6625              Specification              => Func_Spec,
6626              Declarations               => Decls,
6627              Handled_Statement_Sequence =>
6628                Make_Handled_Sequence_Of_Statements (Loc,
6629                  Statements => New_List (
6630                    Make_Simple_Return_Statement (Loc,
6631                      Expression =>
6632                        Make_Function_Call (Loc,
6633                          Name => New_Occurrence_Of (RTE (RE_TC_Build), Loc),
6634                          Parameter_Associations => New_List (
6635                            New_Occurrence_Of (RTE (RE_TC_Object), Loc),
6636                            Make_Aggregate (Loc,
6637                              Expressions =>
6638                                New_List (
6639                                  Make_Function_Call (Loc,
6640                                    Name =>
6641                                      New_Occurrence_Of
6642                                        (RTE (RE_TA_String), Loc),
6643                                    Parameter_Associations => New_List (
6644                                      Make_String_Literal (Loc, Name_String))),
6645                                  Make_Function_Call (Loc,
6646                                    Name =>
6647                                      New_Occurrence_Of
6648                                        (RTE (RE_TA_String), Loc),
6649                                    Parameter_Associations => New_List (
6650                                      Make_String_Literal (Loc,
6651                                        Strval => Repo_Id_String))))))))))));
6652          Set_TSS (RAS_Type, Fnam);
6653       end Add_RAS_TypeCode;
6654
6655       -----------------------------------------
6656       -- Add_Receiving_Stubs_To_Declarations --
6657       -----------------------------------------
6658
6659       procedure Add_Receiving_Stubs_To_Declarations
6660         (Pkg_Spec : Node_Id;
6661          Decls    : List_Id;
6662          Stmts    : List_Id)
6663       is
6664          Loc : constant Source_Ptr := Sloc (Pkg_Spec);
6665
6666          Pkg_RPC_Receiver            : constant Entity_Id :=
6667                                          Make_Defining_Identifier (Loc,
6668                                            New_Internal_Name ('H'));
6669          Pkg_RPC_Receiver_Object     : Node_Id;
6670          Pkg_RPC_Receiver_Body       : Node_Id;
6671          Pkg_RPC_Receiver_Decls      : List_Id;
6672          Pkg_RPC_Receiver_Statements : List_Id;
6673
6674          Pkg_RPC_Receiver_Cases : constant List_Id := New_List;
6675          --  A Pkg_RPC_Receiver is built to decode the request
6676
6677          Request : Node_Id;
6678          --  Request object received from neutral layer
6679
6680          Subp_Id : Entity_Id;
6681          --  Subprogram identifier as received from the neutral
6682          --  distribution core.
6683
6684          Subp_Index : Entity_Id;
6685          --  Internal index as determined by matching either the method name
6686          --  from the request structure, or the local subprogram address (in
6687          --  case of a RAS).
6688
6689          Is_Local : constant Entity_Id :=
6690                       Make_Defining_Identifier (Loc,
6691                         Chars => New_Internal_Name ('L'));
6692
6693          Local_Address : constant Entity_Id :=
6694                            Make_Defining_Identifier (Loc,
6695                              Chars => New_Internal_Name ('A'));
6696          --  Address of a local subprogram designated by a reference
6697          --  corresponding to a RAS.
6698
6699          Dispatch_On_Address : constant List_Id := New_List;
6700          Dispatch_On_Name    : constant List_Id := New_List;
6701
6702          Current_Declaration       : Node_Id;
6703          Current_Stubs             : Node_Id;
6704          Current_Subprogram_Number : Int := First_RCI_Subprogram_Id;
6705
6706          Subp_Info_Array : constant Entity_Id :=
6707                              Make_Defining_Identifier (Loc,
6708                                Chars => New_Internal_Name ('I'));
6709
6710          Subp_Info_List : constant List_Id := New_List;
6711
6712          Register_Pkg_Actuals : constant List_Id := New_List;
6713
6714          All_Calls_Remote_E  : Entity_Id;
6715
6716          procedure Append_Stubs_To
6717            (RPC_Receiver_Cases : List_Id;
6718             Declaration        : Node_Id;
6719             Stubs              : Node_Id;
6720             Subp_Number        : Int;
6721             Subp_Dist_Name     : Entity_Id;
6722             Subp_Proxy_Addr    : Entity_Id);
6723          --  Add one case to the specified RPC receiver case list associating
6724          --  Subprogram_Number with the subprogram declared by Declaration, for
6725          --  which we have receiving stubs in Stubs. Subp_Number is an internal
6726          --  subprogram index. Subp_Dist_Name is the string used to call the
6727          --  subprogram by name, and Subp_Dist_Addr is the address of the proxy
6728          --  object, used in the context of calls through remote
6729          --  access-to-subprogram types.
6730
6731          ---------------------
6732          -- Append_Stubs_To --
6733          ---------------------
6734
6735          procedure Append_Stubs_To
6736            (RPC_Receiver_Cases : List_Id;
6737             Declaration        : Node_Id;
6738             Stubs              : Node_Id;
6739             Subp_Number        : Int;
6740             Subp_Dist_Name     : Entity_Id;
6741             Subp_Proxy_Addr    : Entity_Id)
6742          is
6743             Case_Stmts : List_Id;
6744          begin
6745             Case_Stmts := New_List (
6746               Make_Procedure_Call_Statement (Loc,
6747                 Name                   =>
6748                   New_Occurrence_Of (
6749                     Defining_Entity (Stubs), Loc),
6750                 Parameter_Associations =>
6751                   New_List (New_Occurrence_Of (Request, Loc))));
6752
6753             if Nkind (Specification (Declaration)) = N_Function_Specification
6754               or else not
6755                 Is_Asynchronous (Defining_Entity (Specification (Declaration)))
6756             then
6757                Append_To (Case_Stmts, Make_Simple_Return_Statement (Loc));
6758             end if;
6759
6760             Append_To (RPC_Receiver_Cases,
6761               Make_Case_Statement_Alternative (Loc,
6762                 Discrete_Choices =>
6763                    New_List (Make_Integer_Literal (Loc, Subp_Number)),
6764                 Statements       => Case_Stmts));
6765
6766             Append_To (Dispatch_On_Name,
6767               Make_Elsif_Part (Loc,
6768                 Condition =>
6769                   Make_Function_Call (Loc,
6770                     Name =>
6771                       New_Occurrence_Of (RTE (RE_Caseless_String_Eq), Loc),
6772                     Parameter_Associations => New_List (
6773                       New_Occurrence_Of (Subp_Id, Loc),
6774                       New_Occurrence_Of (Subp_Dist_Name, Loc))),
6775
6776                 Then_Statements => New_List (
6777                   Make_Assignment_Statement (Loc,
6778                     New_Occurrence_Of (Subp_Index, Loc),
6779                     Make_Integer_Literal (Loc, Subp_Number)))));
6780
6781             Append_To (Dispatch_On_Address,
6782               Make_Elsif_Part (Loc,
6783                 Condition =>
6784                   Make_Op_Eq (Loc,
6785                     Left_Opnd  => New_Occurrence_Of (Local_Address, Loc),
6786                     Right_Opnd => New_Occurrence_Of (Subp_Proxy_Addr, Loc)),
6787
6788                 Then_Statements => New_List (
6789                   Make_Assignment_Statement (Loc,
6790                     New_Occurrence_Of (Subp_Index, Loc),
6791                     Make_Integer_Literal (Loc, Subp_Number)))));
6792          end Append_Stubs_To;
6793
6794       --  Start of processing for Add_Receiving_Stubs_To_Declarations
6795
6796       begin
6797          --  Building receiving stubs consist in several operations:
6798
6799          --    - a package RPC receiver must be built. This subprogram
6800          --      will get a Subprogram_Id from the incoming stream
6801          --      and will dispatch the call to the right subprogram;
6802
6803          --    - a receiving stub for each subprogram visible in the package
6804          --      spec. This stub will read all the parameters from the stream,
6805          --      and put the result as well as the exception occurrence in the
6806          --      output stream;
6807
6808          --    - a dummy package with an empty spec and a body made of an
6809          --      elaboration part, whose job is to register the receiving
6810          --      part of this RCI package on the name server. This is done
6811          --      by calling System.Partition_Interface.Register_Receiving_Stub.
6812
6813          Build_RPC_Receiver_Body (
6814            RPC_Receiver => Pkg_RPC_Receiver,
6815            Request      => Request,
6816            Subp_Id      => Subp_Id,
6817            Subp_Index   => Subp_Index,
6818            Stmts        => Pkg_RPC_Receiver_Statements,
6819            Decl         => Pkg_RPC_Receiver_Body);
6820          Pkg_RPC_Receiver_Decls := Declarations (Pkg_RPC_Receiver_Body);
6821
6822          --  Extract local address information from the target reference:
6823          --  if non-null, that means that this is a reference that denotes
6824          --  one particular operation, and hence that the operation name
6825          --  must not be taken into account for dispatching.
6826
6827          Append_To (Pkg_RPC_Receiver_Decls,
6828            Make_Object_Declaration (Loc,
6829              Defining_Identifier => Is_Local,
6830              Object_Definition   =>
6831                New_Occurrence_Of (Standard_Boolean, Loc)));
6832
6833          Append_To (Pkg_RPC_Receiver_Decls,
6834            Make_Object_Declaration (Loc,
6835              Defining_Identifier => Local_Address,
6836              Object_Definition   =>
6837                New_Occurrence_Of (RTE (RE_Address), Loc)));
6838
6839          Append_To (Pkg_RPC_Receiver_Statements,
6840            Make_Procedure_Call_Statement (Loc,
6841              Name => New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
6842              Parameter_Associations => New_List (
6843                Make_Selected_Component (Loc,
6844                  Prefix        => Request,
6845                  Selector_Name => Name_Target),
6846                New_Occurrence_Of (Is_Local, Loc),
6847                New_Occurrence_Of (Local_Address, Loc))));
6848
6849          --  For each subprogram, the receiving stub will be built and a
6850          --  case statement will be made on the Subprogram_Id to dispatch
6851          --  to the right subprogram.
6852
6853          All_Calls_Remote_E := Boolean_Literals (
6854            Has_All_Calls_Remote (Defining_Entity (Pkg_Spec)));
6855
6856          Overload_Counter_Table.Reset;
6857          Reserve_NamingContext_Methods;
6858
6859          Current_Declaration := First (Visible_Declarations (Pkg_Spec));
6860          while Present (Current_Declaration) loop
6861             if Nkind (Current_Declaration) = N_Subprogram_Declaration
6862               and then Comes_From_Source (Current_Declaration)
6863             then
6864                declare
6865                   Loc : constant Source_Ptr := Sloc (Current_Declaration);
6866                   --  While specifically processing Current_Declaration, use
6867                   --  its Sloc as the location of all generated nodes.
6868
6869                   Subp_Def : constant Entity_Id :=
6870                                Defining_Unit_Name
6871                                  (Specification (Current_Declaration));
6872
6873                   Subp_Val : String_Id;
6874
6875                   Subp_Dist_Name : constant Entity_Id :=
6876                                      Make_Defining_Identifier (Loc,
6877                                        Chars =>
6878                                          New_External_Name
6879                                            (Related_Id   => Chars (Subp_Def),
6880                                             Suffix       => 'D',
6881                                             Suffix_Index => -1));
6882
6883                   Proxy_Object_Addr : Entity_Id;
6884
6885                begin
6886                   --  Build receiving stub
6887
6888                   Current_Stubs :=
6889                     Build_Subprogram_Receiving_Stubs
6890                       (Vis_Decl     => Current_Declaration,
6891                        Asynchronous =>
6892                          Nkind (Specification (Current_Declaration)) =
6893                              N_Procedure_Specification
6894                            and then Is_Asynchronous (Subp_Def));
6895
6896                   Append_To (Decls, Current_Stubs);
6897                   Analyze (Current_Stubs);
6898
6899                   --  Build RAS proxy
6900
6901                   Add_RAS_Proxy_And_Analyze (Decls,
6902                     Vis_Decl           => Current_Declaration,
6903                     All_Calls_Remote_E => All_Calls_Remote_E,
6904                     Proxy_Object_Addr  => Proxy_Object_Addr);
6905
6906                   --  Compute distribution identifier
6907
6908                   Assign_Subprogram_Identifier
6909                     (Subp_Def,
6910                      Current_Subprogram_Number,
6911                      Subp_Val);
6912
6913                   pragma Assert
6914                     (Current_Subprogram_Number = Get_Subprogram_Id (Subp_Def));
6915
6916                   Append_To (Decls,
6917                     Make_Object_Declaration (Loc,
6918                       Defining_Identifier => Subp_Dist_Name,
6919                       Constant_Present    => True,
6920                       Object_Definition   =>
6921                         New_Occurrence_Of (Standard_String, Loc),
6922                       Expression          =>
6923                         Make_String_Literal (Loc, Subp_Val)));
6924                   Analyze (Last (Decls));
6925
6926                   --  Add subprogram descriptor (RCI_Subp_Info) to the
6927                   --  subprograms table for this receiver. The aggregate
6928                   --  below must be kept consistent with the declaration
6929                   --  of type RCI_Subp_Info in System.Partition_Interface.
6930
6931                   Append_To (Subp_Info_List,
6932                     Make_Component_Association (Loc,
6933                       Choices => New_List (
6934                         Make_Integer_Literal (Loc, Current_Subprogram_Number)),
6935
6936                       Expression =>
6937                         Make_Aggregate (Loc,
6938                           Expressions => New_List (
6939                             Make_Attribute_Reference (Loc,
6940                               Prefix =>
6941                                 New_Occurrence_Of (Subp_Dist_Name, Loc),
6942                               Attribute_Name => Name_Address),
6943
6944                             Make_Attribute_Reference (Loc,
6945                               Prefix         =>
6946                                 New_Occurrence_Of (Subp_Dist_Name, Loc),
6947                               Attribute_Name => Name_Length),
6948
6949                             New_Occurrence_Of (Proxy_Object_Addr, Loc)))));
6950
6951                   Append_Stubs_To (Pkg_RPC_Receiver_Cases,
6952                     Declaration     => Current_Declaration,
6953                     Stubs           => Current_Stubs,
6954                     Subp_Number     => Current_Subprogram_Number,
6955                     Subp_Dist_Name  => Subp_Dist_Name,
6956                     Subp_Proxy_Addr => Proxy_Object_Addr);
6957                end;
6958
6959                Current_Subprogram_Number := Current_Subprogram_Number + 1;
6960             end if;
6961
6962             Next (Current_Declaration);
6963          end loop;
6964
6965          Append_To (Decls,
6966            Make_Object_Declaration (Loc,
6967              Defining_Identifier => Subp_Info_Array,
6968              Constant_Present    => True,
6969              Aliased_Present     => True,
6970              Object_Definition   =>
6971                Make_Subtype_Indication (Loc,
6972                  Subtype_Mark =>
6973                    New_Occurrence_Of (RTE (RE_RCI_Subp_Info_Array), Loc),
6974                  Constraint =>
6975                    Make_Index_Or_Discriminant_Constraint (Loc,
6976                      New_List (
6977                        Make_Range (Loc,
6978                          Low_Bound  =>
6979                            Make_Integer_Literal (Loc,
6980                              Intval => First_RCI_Subprogram_Id),
6981                          High_Bound =>
6982                            Make_Integer_Literal (Loc,
6983                              Intval =>
6984                                First_RCI_Subprogram_Id
6985                                + List_Length (Subp_Info_List) - 1)))))));
6986
6987          if Present (First (Subp_Info_List)) then
6988             Set_Expression (Last (Decls),
6989               Make_Aggregate (Loc,
6990                 Component_Associations => Subp_Info_List));
6991
6992             --  Generate the dispatch statement to determine the subprogram id
6993             --  of the called subprogram.
6994
6995             --  We first test whether the reference that was used to make the
6996             --  call was the base RCI reference (in which case Local_Address is
6997             --  zero, and the method identifier from the request must be used
6998             --  to determine which subprogram is called) or a reference
6999             --  identifying one particular subprogram (in which case
7000             --  Local_Address is the address of that subprogram, and the
7001             --  method name from the request is ignored). The latter occurs
7002             --  for the case of a call through a remote access-to-subprogram.
7003
7004             --  In each case, cascaded elsifs are used to determine the proper
7005             --  subprogram index. Using hash tables might be more efficient.
7006
7007             Append_To (Pkg_RPC_Receiver_Statements,
7008               Make_Implicit_If_Statement (Pkg_Spec,
7009                 Condition =>
7010                   Make_Op_Ne (Loc,
7011                     Left_Opnd  => New_Occurrence_Of (Local_Address, Loc),
7012                     Right_Opnd => New_Occurrence_Of
7013                                     (RTE (RE_Null_Address), Loc)),
7014
7015                 Then_Statements => New_List (
7016                   Make_Implicit_If_Statement (Pkg_Spec,
7017                     Condition       => New_Occurrence_Of (Standard_False, Loc),
7018                     Then_Statements => New_List (
7019                       Make_Null_Statement (Loc)),
7020                     Elsif_Parts     => Dispatch_On_Address)),
7021
7022                 Else_Statements => New_List (
7023                   Make_Implicit_If_Statement (Pkg_Spec,
7024                     Condition       => New_Occurrence_Of (Standard_False, Loc),
7025                     Then_Statements => New_List (Make_Null_Statement (Loc)),
7026                     Elsif_Parts     => Dispatch_On_Name))));
7027
7028          else
7029             --  For a degenerate RCI with no visible subprograms,
7030             --  Subp_Info_List has zero length, and the declaration is for an
7031             --  empty array, in which case no initialization aggregate must be
7032             --  generated. We do not generate a Dispatch_Statement either.
7033
7034             --  No initialization provided: remove CONSTANT so that the
7035             --  declaration is not an incomplete deferred constant.
7036
7037             Set_Constant_Present (Last (Decls), False);
7038          end if;
7039
7040          --  Analyze Subp_Info_Array declaration
7041
7042          Analyze (Last (Decls));
7043
7044          --  If we receive an invalid Subprogram_Id, it is best to do nothing
7045          --  rather than raising an exception since we do not want someone
7046          --  to crash a remote partition by sending invalid subprogram ids.
7047          --  This is consistent with the other parts of the case statement
7048          --  since even in presence of incorrect parameters in the stream,
7049          --  every exception will be caught and (if the subprogram is not an
7050          --  APC) put into the result stream and sent away.
7051
7052          Append_To (Pkg_RPC_Receiver_Cases,
7053            Make_Case_Statement_Alternative (Loc,
7054              Discrete_Choices => New_List (Make_Others_Choice (Loc)),
7055              Statements       => New_List (Make_Null_Statement (Loc))));
7056
7057          Append_To (Pkg_RPC_Receiver_Statements,
7058            Make_Case_Statement (Loc,
7059              Expression   => New_Occurrence_Of (Subp_Index, Loc),
7060              Alternatives => Pkg_RPC_Receiver_Cases));
7061
7062          --  Pkg_RPC_Receiver body is now complete: insert it into the tree and
7063          --  analyze it.
7064
7065          Append_To (Decls, Pkg_RPC_Receiver_Body);
7066          Analyze (Last (Decls));
7067
7068          Pkg_RPC_Receiver_Object :=
7069            Make_Object_Declaration (Loc,
7070              Defining_Identifier =>
7071                Make_Defining_Identifier (Loc, New_Internal_Name ('R')),
7072              Aliased_Present     => True,
7073              Object_Definition   => New_Occurrence_Of (RTE (RE_Servant), Loc));
7074          Append_To (Decls, Pkg_RPC_Receiver_Object);
7075          Analyze (Last (Decls));
7076
7077          Get_Library_Unit_Name_String (Pkg_Spec);
7078
7079          --  Name
7080
7081          Append_To (Register_Pkg_Actuals,
7082            Make_String_Literal (Loc,
7083              Strval => String_From_Name_Buffer));
7084
7085          --  Version
7086
7087          Append_To (Register_Pkg_Actuals,
7088            Make_Attribute_Reference (Loc,
7089              Prefix         =>
7090                New_Occurrence_Of
7091                  (Defining_Entity (Pkg_Spec), Loc),
7092              Attribute_Name => Name_Version));
7093
7094          --  Handler
7095
7096          Append_To (Register_Pkg_Actuals,
7097            Make_Attribute_Reference (Loc,
7098              Prefix          =>
7099                New_Occurrence_Of (Pkg_RPC_Receiver, Loc),
7100              Attribute_Name  => Name_Access));
7101
7102          --  Receiver
7103
7104          Append_To (Register_Pkg_Actuals,
7105            Make_Attribute_Reference (Loc,
7106              Prefix         =>
7107                New_Occurrence_Of (
7108                  Defining_Identifier (Pkg_RPC_Receiver_Object), Loc),
7109              Attribute_Name => Name_Access));
7110
7111          --  Subp_Info
7112
7113          Append_To (Register_Pkg_Actuals,
7114            Make_Attribute_Reference (Loc,
7115              Prefix         => New_Occurrence_Of (Subp_Info_Array, Loc),
7116              Attribute_Name => Name_Address));
7117
7118          --  Subp_Info_Len
7119
7120          Append_To (Register_Pkg_Actuals,
7121            Make_Attribute_Reference (Loc,
7122              Prefix         => New_Occurrence_Of (Subp_Info_Array, Loc),
7123              Attribute_Name => Name_Length));
7124
7125          --  Is_All_Calls_Remote
7126
7127          Append_To (Register_Pkg_Actuals,
7128            New_Occurrence_Of (All_Calls_Remote_E, Loc));
7129
7130          --  ???
7131
7132          Append_To (Stmts,
7133            Make_Procedure_Call_Statement (Loc,
7134              Name                   =>
7135                New_Occurrence_Of (RTE (RE_Register_Pkg_Receiving_Stub), Loc),
7136              Parameter_Associations => Register_Pkg_Actuals));
7137          Analyze (Last (Stmts));
7138       end Add_Receiving_Stubs_To_Declarations;
7139
7140       ---------------------------------
7141       -- Build_General_Calling_Stubs --
7142       ---------------------------------
7143
7144       procedure Build_General_Calling_Stubs
7145         (Decls                     : List_Id;
7146          Statements                : List_Id;
7147          Target_Object             : Node_Id;
7148          Subprogram_Id             : Node_Id;
7149          Asynchronous              : Node_Id   := Empty;
7150          Is_Known_Asynchronous     : Boolean   := False;
7151          Is_Known_Non_Asynchronous : Boolean   := False;
7152          Is_Function               : Boolean;
7153          Spec                      : Node_Id;
7154          Stub_Type                 : Entity_Id := Empty;
7155          RACW_Type                 : Entity_Id := Empty;
7156          Nod                       : Node_Id)
7157       is
7158          Loc : constant Source_Ptr := Sloc (Nod);
7159
7160          Arguments : Node_Id;
7161          --  Name of the named values list used to transmit parameters
7162          --  to the remote package
7163
7164          Request : Node_Id;
7165          --  The request object constructed by these stubs
7166
7167          Result : Node_Id;
7168          --  Name of the result named value (in non-APC cases) which get the
7169          --  result of the remote subprogram.
7170
7171          Result_TC : Node_Id;
7172          --  Typecode expression for the result of the request (void
7173          --  typecode for procedures).
7174
7175          Exception_Return_Parameter : Node_Id;
7176          --  Name of the parameter which will hold the exception sent by the
7177          --  remote subprogram.
7178
7179          Current_Parameter : Node_Id;
7180          --  Current parameter being handled
7181
7182          Ordered_Parameters_List : constant List_Id :=
7183                                      Build_Ordered_Parameters_List (Spec);
7184
7185          Asynchronous_P : Node_Id;
7186          --  A Boolean expression indicating whether this call is asynchronous
7187
7188          Asynchronous_Statements     : List_Id := No_List;
7189          Non_Asynchronous_Statements : List_Id := No_List;
7190          --  Statements specifics to the Asynchronous/Non-Asynchronous cases
7191
7192          Extra_Formal_Statements : constant List_Id := New_List;
7193          --  List of statements for extra formal parameters. It will appear
7194          --  after the regular statements for writing out parameters.
7195
7196          After_Statements : constant List_Id := New_List;
7197          --  Statements to be executed after call returns (to assign
7198          --  in out or out parameter values).
7199
7200          Etyp : Entity_Id;
7201          --  The type of the formal parameter being processed
7202
7203          Is_Controlling_Formal         : Boolean;
7204          Is_First_Controlling_Formal   : Boolean;
7205          First_Controlling_Formal_Seen : Boolean := False;
7206          --  Controlling formal parameters of distributed object primitives
7207          --  require special handling, and the first such parameter needs even
7208          --  more special handling.
7209
7210       begin
7211          --  ??? document general form of stub subprograms for the PolyORB case
7212          Request := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
7213
7214          Append_To (Decls,
7215            Make_Object_Declaration (Loc,
7216              Defining_Identifier => Request,
7217              Aliased_Present     => False,
7218              Object_Definition   =>
7219                  New_Occurrence_Of (RTE (RE_Request_Access), Loc)));
7220
7221          Result :=
7222            Make_Defining_Identifier (Loc,
7223              Chars => New_Internal_Name ('R'));
7224
7225          if Is_Function then
7226             Result_TC :=
7227               PolyORB_Support.Helpers.Build_TypeCode_Call
7228                 (Loc, Etype (Result_Definition (Spec)), Decls);
7229          else
7230             Result_TC := New_Occurrence_Of (RTE (RE_TC_Void), Loc);
7231          end if;
7232
7233          Append_To (Decls,
7234            Make_Object_Declaration (Loc,
7235              Defining_Identifier => Result,
7236              Aliased_Present     => False,
7237              Object_Definition   =>
7238                New_Occurrence_Of (RTE (RE_NamedValue), Loc),
7239              Expression =>
7240                Make_Aggregate (Loc,
7241                  Component_Associations => New_List (
7242                    Make_Component_Association (Loc,
7243                      Choices    => New_List (Make_Identifier (Loc, Name_Name)),
7244                      Expression =>
7245                        New_Occurrence_Of (RTE (RE_Result_Name), Loc)),
7246                    Make_Component_Association (Loc,
7247                      Choices => New_List (
7248                        Make_Identifier (Loc, Name_Argument)),
7249                      Expression =>
7250                        Make_Function_Call (Loc,
7251                          Name => New_Occurrence_Of (RTE (RE_Create_Any), Loc),
7252                          Parameter_Associations => New_List (Result_TC))),
7253                    Make_Component_Association (Loc,
7254                      Choices    => New_List (
7255                        Make_Identifier (Loc, Name_Arg_Modes)),
7256                      Expression => Make_Integer_Literal (Loc, 0))))));
7257
7258          if not Is_Known_Asynchronous then
7259             Exception_Return_Parameter :=
7260               Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
7261
7262             Append_To (Decls,
7263               Make_Object_Declaration (Loc,
7264                 Defining_Identifier => Exception_Return_Parameter,
7265                 Object_Definition   =>
7266                   New_Occurrence_Of (RTE (RE_Exception_Occurrence), Loc)));
7267
7268          else
7269             Exception_Return_Parameter := Empty;
7270          end if;
7271
7272          --  Initialize and fill in arguments list
7273
7274          Arguments :=
7275            Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
7276          Declare_Create_NVList (Loc, Arguments, Decls, Statements);
7277
7278          Current_Parameter := First (Ordered_Parameters_List);
7279          while Present (Current_Parameter) loop
7280             if Is_RACW_Controlling_Formal (Current_Parameter, Stub_Type) then
7281                Is_Controlling_Formal := True;
7282                Is_First_Controlling_Formal :=
7283                  not First_Controlling_Formal_Seen;
7284                First_Controlling_Formal_Seen := True;
7285
7286             else
7287                Is_Controlling_Formal := False;
7288                Is_First_Controlling_Formal := False;
7289             end if;
7290
7291             if Is_Controlling_Formal then
7292
7293                --  For a controlling formal argument, we send its reference
7294
7295                Etyp := RACW_Type;
7296
7297             else
7298                Etyp := Etype (Parameter_Type (Current_Parameter));
7299             end if;
7300
7301             --  The first controlling formal parameter is treated specially:
7302             --  it is used to set the target object of the call.
7303
7304             if not Is_First_Controlling_Formal then
7305                declare
7306                   Constrained : constant Boolean :=
7307                                   Is_Constrained (Etyp)
7308                                     or else Is_Elementary_Type (Etyp);
7309
7310                   Any : constant Entity_Id :=
7311                           Make_Defining_Identifier (Loc,
7312                             New_Internal_Name ('A'));
7313
7314                   Actual_Parameter : Node_Id :=
7315                                        New_Occurrence_Of (
7316                                          Defining_Identifier (
7317                                            Current_Parameter), Loc);
7318
7319                   Expr : Node_Id;
7320
7321                begin
7322                   if Is_Controlling_Formal then
7323
7324                      --  For a controlling formal parameter (other than the
7325                      --  first one), use the corresponding RACW. If the
7326                      --  parameter is not an anonymous access parameter, that
7327                      --  involves taking its 'Unrestricted_Access.
7328
7329                      if Nkind (Parameter_Type (Current_Parameter))
7330                        = N_Access_Definition
7331                      then
7332                         Actual_Parameter := OK_Convert_To
7333                           (Etyp, Actual_Parameter);
7334                      else
7335                         Actual_Parameter := OK_Convert_To (Etyp,
7336                           Make_Attribute_Reference (Loc,
7337                             Prefix         => Actual_Parameter,
7338                             Attribute_Name => Name_Unrestricted_Access));
7339                      end if;
7340
7341                   end if;
7342
7343                   if In_Present (Current_Parameter)
7344                     or else not Out_Present (Current_Parameter)
7345                     or else not Constrained
7346                     or else Is_Controlling_Formal
7347                   then
7348                      --  The parameter has an input value, is constrained at
7349                      --  runtime by an input value, or is a controlling formal
7350                      --  parameter (always passed as a reference) other than
7351                      --  the first one.
7352
7353                      Expr := PolyORB_Support.Helpers.Build_To_Any_Call
7354                                (Actual_Parameter, Decls);
7355
7356                   else
7357                      Expr := Make_Function_Call (Loc,
7358                        Name => New_Occurrence_Of (RTE (RE_Create_Any), Loc),
7359                        Parameter_Associations => New_List (
7360                          PolyORB_Support.Helpers.Build_TypeCode_Call
7361                            (Loc, Etyp, Decls)));
7362                   end if;
7363
7364                   Append_To (Decls,
7365                     Make_Object_Declaration (Loc,
7366                       Defining_Identifier => Any,
7367                       Aliased_Present     => False,
7368                       Object_Definition   =>
7369                         New_Occurrence_Of (RTE (RE_Any), Loc),
7370                       Expression          => Expr));
7371
7372                   Append_To (Statements,
7373                     Add_Parameter_To_NVList (Loc,
7374                       Parameter   => Current_Parameter,
7375                       NVList      => Arguments,
7376                       Constrained => Constrained,
7377                       Any         => Any));
7378
7379                   if Out_Present (Current_Parameter)
7380                     and then not Is_Controlling_Formal
7381                   then
7382                      Append_To (After_Statements,
7383                        Make_Assignment_Statement (Loc,
7384                          Name =>
7385                            New_Occurrence_Of (
7386                              Defining_Identifier (Current_Parameter), Loc),
7387                            Expression =>
7388                              PolyORB_Support.Helpers.Build_From_Any_Call
7389                                (Etype (Parameter_Type (Current_Parameter)),
7390                                 New_Occurrence_Of (Any, Loc),
7391                                 Decls)));
7392
7393                   end if;
7394                end;
7395             end if;
7396
7397             --  If the current parameter has a dynamic constrained status, then
7398             --  this status is transmitted as well.
7399             --  This should be done for accessibility as well ???
7400
7401             if Nkind (Parameter_Type (Current_Parameter)) /=
7402                                                     N_Access_Definition
7403               and then Need_Extra_Constrained (Current_Parameter)
7404             then
7405                --  In this block, we do not use the extra formal that has been
7406                --  created because it does not exist at the time of expansion
7407                --  when building calling stubs for remote access to subprogram
7408                --  types. We create an extra variable of this type and push it
7409                --  in the stream after the regular parameters.
7410
7411                declare
7412                   Extra_Any_Parameter : constant Entity_Id :=
7413                                           Make_Defining_Identifier
7414                                             (Loc, New_Internal_Name ('P'));
7415
7416                   Parameter_Exp : constant Node_Id :=
7417                      Make_Attribute_Reference (Loc,
7418                        Prefix         => New_Occurrence_Of (
7419                          Defining_Identifier (Current_Parameter), Loc),
7420                        Attribute_Name => Name_Constrained);
7421
7422                begin
7423                   Set_Etype (Parameter_Exp, Etype (Standard_Boolean));
7424
7425                   Append_To (Decls,
7426                     Make_Object_Declaration (Loc,
7427                       Defining_Identifier => Extra_Any_Parameter,
7428                       Aliased_Present     => False,
7429                       Object_Definition   =>
7430                         New_Occurrence_Of (RTE (RE_Any), Loc),
7431                       Expression          =>
7432                         PolyORB_Support.Helpers.Build_To_Any_Call
7433                           (Parameter_Exp, Decls)));
7434
7435                   Append_To (Extra_Formal_Statements,
7436                     Add_Parameter_To_NVList (Loc,
7437                       Parameter   => Extra_Any_Parameter,
7438                       NVList      => Arguments,
7439                       Constrained => True,
7440                       Any         => Extra_Any_Parameter));
7441                end;
7442             end if;
7443
7444             Next (Current_Parameter);
7445          end loop;
7446
7447          --  Append the formal statements list to the statements
7448
7449          Append_List_To (Statements, Extra_Formal_Statements);
7450
7451          Append_To (Statements,
7452            Make_Procedure_Call_Statement (Loc,
7453              Name =>
7454                New_Occurrence_Of (RTE (RE_Request_Create), Loc),
7455
7456              Parameter_Associations => New_List (
7457                Target_Object,
7458                Subprogram_Id,
7459                New_Occurrence_Of (Arguments, Loc),
7460                New_Occurrence_Of (Result, Loc),
7461                New_Occurrence_Of (RTE (RE_Nil_Exc_List), Loc))));
7462
7463          Append_To (Parameter_Associations (Last (Statements)),
7464                New_Occurrence_Of (Request, Loc));
7465
7466          pragma Assert
7467            (not (Is_Known_Non_Asynchronous and Is_Known_Asynchronous));
7468
7469          if Is_Known_Non_Asynchronous or Is_Known_Asynchronous then
7470             Asynchronous_P :=
7471               New_Occurrence_Of
7472                 (Boolean_Literals (Is_Known_Asynchronous), Loc);
7473
7474          else
7475             pragma Assert (Present (Asynchronous));
7476             Asynchronous_P := New_Copy_Tree (Asynchronous);
7477
7478             --  The expression node Asynchronous will be used to build an 'if'
7479             --  statement at the end of Build_General_Calling_Stubs: we need to
7480             --  make a copy here.
7481          end if;
7482
7483          Append_To (Parameter_Associations (Last (Statements)),
7484            Make_Indexed_Component (Loc,
7485              Prefix =>
7486                New_Occurrence_Of (
7487                  RTE (RE_Asynchronous_P_To_Sync_Scope), Loc),
7488              Expressions => New_List (Asynchronous_P)));
7489
7490          Append_To (Statements,
7491              Make_Procedure_Call_Statement (Loc,
7492                Name                   =>
7493                  New_Occurrence_Of (RTE (RE_Request_Invoke), Loc),
7494                Parameter_Associations => New_List (
7495                  New_Occurrence_Of (Request, Loc))));
7496
7497          Non_Asynchronous_Statements := New_List (Make_Null_Statement (Loc));
7498          Asynchronous_Statements := New_List (Make_Null_Statement (Loc));
7499
7500          if not Is_Known_Asynchronous then
7501
7502             --  Reraise an exception occurrence from the completed request.
7503             --  If the exception occurrence is empty, this is a no-op.
7504
7505             Append_To (Non_Asynchronous_Statements,
7506               Make_Procedure_Call_Statement (Loc,
7507                 Name                   =>
7508                   New_Occurrence_Of (RTE (RE_Request_Raise_Occurrence), Loc),
7509                 Parameter_Associations => New_List (
7510                   New_Occurrence_Of (Request, Loc))));
7511
7512             if Is_Function then
7513
7514                --  If this is a function call, read the value and return it
7515
7516                Append_To (Non_Asynchronous_Statements,
7517                  Make_Tag_Check (Loc,
7518                    Make_Simple_Return_Statement (Loc,
7519                      PolyORB_Support.Helpers.Build_From_Any_Call
7520                        (Etype (Result_Definition (Spec)),
7521                         Make_Selected_Component (Loc,
7522                           Prefix        => Result,
7523                           Selector_Name => Name_Argument),
7524                         Decls))));
7525             end if;
7526          end if;
7527
7528          Append_List_To (Non_Asynchronous_Statements, After_Statements);
7529
7530          if Is_Known_Asynchronous then
7531             Append_List_To (Statements, Asynchronous_Statements);
7532
7533          elsif Is_Known_Non_Asynchronous then
7534             Append_List_To (Statements, Non_Asynchronous_Statements);
7535
7536          else
7537             pragma Assert (Present (Asynchronous));
7538             Append_To (Statements,
7539               Make_Implicit_If_Statement (Nod,
7540                 Condition       => Asynchronous,
7541                 Then_Statements => Asynchronous_Statements,
7542                 Else_Statements => Non_Asynchronous_Statements));
7543          end if;
7544       end Build_General_Calling_Stubs;
7545
7546       -----------------------
7547       -- Build_Stub_Target --
7548       -----------------------
7549
7550       function Build_Stub_Target
7551         (Loc                   : Source_Ptr;
7552          Decls                 : List_Id;
7553          RCI_Locator           : Entity_Id;
7554          Controlling_Parameter : Entity_Id) return RPC_Target
7555       is
7556          Target_Info : RPC_Target (PCS_Kind => Name_PolyORB_DSA);
7557          Target_Reference : constant Entity_Id :=
7558                               Make_Defining_Identifier (Loc,
7559                                 New_Internal_Name ('T'));
7560       begin
7561          if Present (Controlling_Parameter) then
7562             Append_To (Decls,
7563               Make_Object_Declaration (Loc,
7564                 Defining_Identifier => Target_Reference,
7565
7566                 Object_Definition   =>
7567                   New_Occurrence_Of (RTE (RE_Object_Ref), Loc),
7568
7569                 Expression          =>
7570                   Make_Function_Call (Loc,
7571                     Name =>
7572                       New_Occurrence_Of (RTE (RE_Make_Ref), Loc),
7573                     Parameter_Associations => New_List (
7574                       Make_Selected_Component (Loc,
7575                         Prefix        => Controlling_Parameter,
7576                         Selector_Name => Name_Target)))));
7577
7578             --  Note: Controlling_Parameter has the same components as
7579             --  System.Partition_Interface.RACW_Stub_Type.
7580
7581             Target_Info.Object := New_Occurrence_Of (Target_Reference, Loc);
7582
7583          else
7584             Target_Info.Object :=
7585               Make_Selected_Component (Loc,
7586                 Prefix        => Make_Identifier (Loc, Chars (RCI_Locator)),
7587                 Selector_Name =>
7588                   Make_Identifier (Loc, Name_Get_RCI_Package_Ref));
7589          end if;
7590
7591          return Target_Info;
7592       end Build_Stub_Target;
7593
7594       ---------------------
7595       -- Build_Stub_Type --
7596       ---------------------
7597
7598       procedure Build_Stub_Type
7599         (RACW_Type         : Entity_Id;
7600          Stub_Type         : Entity_Id;
7601          Stub_Type_Decl    : out Node_Id;
7602          RPC_Receiver_Decl : out Node_Id)
7603       is
7604          Loc : constant Source_Ptr := Sloc (Stub_Type);
7605          pragma Warnings (Off);
7606          pragma Unreferenced (RACW_Type);
7607          pragma Warnings (On);
7608
7609       begin
7610          Stub_Type_Decl :=
7611            Make_Full_Type_Declaration (Loc,
7612              Defining_Identifier => Stub_Type,
7613              Type_Definition     =>
7614                Make_Record_Definition (Loc,
7615                  Tagged_Present  => True,
7616                  Limited_Present => True,
7617                  Component_List  =>
7618                    Make_Component_List (Loc,
7619                      Component_Items => New_List (
7620
7621                        Make_Component_Declaration (Loc,
7622                          Defining_Identifier =>
7623                            Make_Defining_Identifier (Loc, Name_Target),
7624                          Component_Definition =>
7625                            Make_Component_Definition (Loc,
7626                              Aliased_Present     => False,
7627                              Subtype_Indication  =>
7628                                New_Occurrence_Of (RTE (RE_Entity_Ptr), Loc))),
7629
7630                        Make_Component_Declaration (Loc,
7631                          Defining_Identifier =>
7632                            Make_Defining_Identifier (Loc, Name_Asynchronous),
7633
7634                          Component_Definition =>
7635                            Make_Component_Definition (Loc,
7636                              Aliased_Present    => False,
7637                              Subtype_Indication =>
7638                                New_Occurrence_Of (Standard_Boolean, Loc)))))));
7639
7640          RPC_Receiver_Decl :=
7641            Make_Object_Declaration (Loc,
7642              Defining_Identifier => Make_Defining_Identifier (Loc,
7643                                       New_Internal_Name ('R')),
7644              Aliased_Present     => True,
7645              Object_Definition   =>
7646                New_Occurrence_Of (RTE (RE_Servant), Loc));
7647       end Build_Stub_Type;
7648
7649       -----------------------------
7650       -- Build_RPC_Receiver_Body --
7651       -----------------------------
7652
7653       procedure Build_RPC_Receiver_Body
7654         (RPC_Receiver : Entity_Id;
7655          Request      : out Entity_Id;
7656          Subp_Id      : out Entity_Id;
7657          Subp_Index   : out Entity_Id;
7658          Stmts        : out List_Id;
7659          Decl         : out Node_Id)
7660       is
7661          Loc : constant Source_Ptr := Sloc (RPC_Receiver);
7662
7663          RPC_Receiver_Spec  : Node_Id;
7664          RPC_Receiver_Decls : List_Id;
7665
7666       begin
7667          Request := Make_Defining_Identifier (Loc, Name_R);
7668
7669          RPC_Receiver_Spec :=
7670            Build_RPC_Receiver_Specification
7671              (RPC_Receiver      => RPC_Receiver,
7672               Request_Parameter => Request);
7673
7674          Subp_Id    := Make_Defining_Identifier (Loc, Name_P);
7675          Subp_Index := Make_Defining_Identifier (Loc, Name_I);
7676
7677          RPC_Receiver_Decls := New_List (
7678            Make_Object_Renaming_Declaration (Loc,
7679              Defining_Identifier => Subp_Id,
7680              Subtype_Mark        => New_Occurrence_Of (Standard_String, Loc),
7681              Name                =>
7682                Make_Explicit_Dereference (Loc,
7683                  Prefix =>
7684                    Make_Selected_Component (Loc,
7685                      Prefix        => Request,
7686                      Selector_Name => Name_Operation))),
7687
7688            Make_Object_Declaration (Loc,
7689              Defining_Identifier => Subp_Index,
7690              Object_Definition   =>
7691                New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc),
7692              Expression          =>
7693                Make_Attribute_Reference (Loc,
7694                  Prefix         =>
7695                    New_Occurrence_Of (RTE (RE_Subprogram_Id), Loc),
7696                  Attribute_Name => Name_Last)));
7697
7698          Stmts := New_List;
7699
7700          Decl :=
7701            Make_Subprogram_Body (Loc,
7702              Specification              => RPC_Receiver_Spec,
7703              Declarations               => RPC_Receiver_Decls,
7704              Handled_Statement_Sequence =>
7705                Make_Handled_Sequence_Of_Statements (Loc,
7706                  Statements => Stmts));
7707       end Build_RPC_Receiver_Body;
7708
7709       --------------------------------------
7710       -- Build_Subprogram_Receiving_Stubs --
7711       --------------------------------------
7712
7713       function Build_Subprogram_Receiving_Stubs
7714         (Vis_Decl                 : Node_Id;
7715          Asynchronous             : Boolean;
7716          Dynamically_Asynchronous : Boolean   := False;
7717          Stub_Type                : Entity_Id := Empty;
7718          RACW_Type                : Entity_Id := Empty;
7719          Parent_Primitive         : Entity_Id := Empty) return Node_Id
7720       is
7721          Loc : constant Source_Ptr := Sloc (Vis_Decl);
7722
7723          Request_Parameter : constant Entity_Id :=
7724                                Make_Defining_Identifier (Loc,
7725                                  New_Internal_Name ('R'));
7726          --  Formal parameter for receiving stubs: a descriptor for an incoming
7727          --  request.
7728
7729          Outer_Decls : constant List_Id := New_List;
7730          --  At the outermost level, an NVList and Any's are declared for all
7731          --  parameters. The Dynamic_Async flag also needs to be declared there
7732          --  to be visible from the exception handling code.
7733
7734          Outer_Statements : constant List_Id := New_List;
7735          --  Statements that occur prior to the declaration of the actual
7736          --  parameter variables.
7737
7738          Outer_Extra_Formal_Statements : constant List_Id := New_List;
7739          --  Statements concerning extra formal parameters, prior to the
7740          --  declaration of the actual parameter variables.
7741
7742          Decls : constant List_Id := New_List;
7743          --  All the parameters will get declared before calling the real
7744          --  subprograms. Also the out parameters will be declared.
7745          --  At this level, parameters may be unconstrained.
7746
7747          Statements : constant List_Id := New_List;
7748
7749          After_Statements : constant List_Id := New_List;
7750          --  Statements to be executed after the subprogram call
7751
7752          Inner_Decls : List_Id := No_List;
7753          --  In case of a function, the inner declarations are needed since
7754          --  the result may be unconstrained.
7755
7756          Excep_Handlers : List_Id := No_List;
7757
7758          Parameter_List : constant List_Id := New_List;
7759          --  List of parameters to be passed to the subprogram
7760
7761          First_Controlling_Formal_Seen : Boolean := False;
7762
7763          Current_Parameter : Node_Id;
7764
7765          Ordered_Parameters_List : constant List_Id :=
7766                                      Build_Ordered_Parameters_List
7767                                        (Specification (Vis_Decl));
7768
7769          Arguments : constant Entity_Id :=
7770                        Make_Defining_Identifier (Loc,
7771                          New_Internal_Name ('A'));
7772          --  Name of the named values list used to retrieve parameters
7773
7774          Subp_Spec : Node_Id;
7775          --  Subprogram specification
7776
7777          Called_Subprogram : Node_Id;
7778          --  The subprogram to call
7779
7780       begin
7781          if Present (RACW_Type) then
7782             Called_Subprogram :=
7783               New_Occurrence_Of (Parent_Primitive, Loc);
7784          else
7785             Called_Subprogram :=
7786               New_Occurrence_Of
7787                 (Defining_Unit_Name (Specification (Vis_Decl)), Loc);
7788          end if;
7789
7790          Declare_Create_NVList (Loc, Arguments, Outer_Decls, Outer_Statements);
7791
7792          --  Loop through every parameter and get its value from the stream. If
7793          --  the parameter is unconstrained, then the parameter is read using
7794          --  'Input at the point of declaration.
7795
7796          Current_Parameter := First (Ordered_Parameters_List);
7797          while Present (Current_Parameter) loop
7798             declare
7799                Etyp        : Entity_Id;
7800                Constrained : Boolean;
7801                Any         : Entity_Id := Empty;
7802                Object      : constant Entity_Id :=
7803                                Make_Defining_Identifier (Loc,
7804                                  Chars => New_Internal_Name ('P'));
7805                Expr        : Node_Id   := Empty;
7806
7807                Is_Controlling_Formal : constant Boolean :=
7808                                          Is_RACW_Controlling_Formal
7809                                            (Current_Parameter, Stub_Type);
7810
7811                Is_First_Controlling_Formal : Boolean := False;
7812
7813                Need_Extra_Constrained : Boolean;
7814                --  True when an extra constrained actual is required
7815
7816             begin
7817                if Is_Controlling_Formal then
7818
7819                   --  Controlling formals in distributed object primitive
7820                   --  operations are handled specially:
7821                   --    - the first controlling formal is used as the
7822                   --      target of the call;
7823                   --    - the remaining controlling formals are transmitted
7824                   --      as RACWs.
7825
7826                   Etyp := RACW_Type;
7827                   Is_First_Controlling_Formal :=
7828                     not First_Controlling_Formal_Seen;
7829                   First_Controlling_Formal_Seen := True;
7830
7831                else
7832                   Etyp := Etype (Parameter_Type (Current_Parameter));
7833                end if;
7834
7835                Constrained :=
7836                  Is_Constrained (Etyp) or else Is_Elementary_Type (Etyp);
7837
7838                if not Is_First_Controlling_Formal then
7839                   Any :=
7840                     Make_Defining_Identifier (Loc,
7841                       Chars => New_Internal_Name ('A'));
7842
7843                   Append_To (Outer_Decls,
7844                     Make_Object_Declaration (Loc,
7845                       Defining_Identifier => Any,
7846                       Object_Definition   =>
7847                         New_Occurrence_Of (RTE (RE_Any), Loc),
7848                       Expression =>
7849                         Make_Function_Call (Loc,
7850                           Name => New_Occurrence_Of (RTE (RE_Create_Any), Loc),
7851                           Parameter_Associations => New_List (
7852                             PolyORB_Support.Helpers.Build_TypeCode_Call
7853                               (Loc, Etyp, Outer_Decls)))));
7854
7855                   Append_To (Outer_Statements,
7856                     Add_Parameter_To_NVList (Loc,
7857                       Parameter   => Current_Parameter,
7858                       NVList      => Arguments,
7859                       Constrained => Constrained,
7860                       Any         => Any));
7861                end if;
7862
7863                if Is_First_Controlling_Formal then
7864                   declare
7865                      Addr : constant Entity_Id :=
7866                               Make_Defining_Identifier (Loc,
7867                                 Chars => New_Internal_Name ('A'));
7868
7869                      Is_Local : constant Entity_Id :=
7870                                   Make_Defining_Identifier (Loc,
7871                                     Chars => New_Internal_Name ('L'));
7872
7873                   begin
7874                      --  Special case: obtain the first controlling formal
7875                      --  from the target of the remote call, instead of the
7876                      --  argument list.
7877
7878                      Append_To (Outer_Decls,
7879                        Make_Object_Declaration (Loc,
7880                          Defining_Identifier => Addr,
7881                          Object_Definition =>
7882                            New_Occurrence_Of (RTE (RE_Address), Loc)));
7883
7884                      Append_To (Outer_Decls,
7885                        Make_Object_Declaration (Loc,
7886                          Defining_Identifier => Is_Local,
7887                          Object_Definition =>
7888                            New_Occurrence_Of (Standard_Boolean, Loc)));
7889
7890                      Append_To (Outer_Statements,
7891                        Make_Procedure_Call_Statement (Loc,
7892                          Name =>
7893                            New_Occurrence_Of (RTE (RE_Get_Local_Address), Loc),
7894                          Parameter_Associations => New_List (
7895                            Make_Selected_Component (Loc,
7896                              Prefix =>
7897                                New_Occurrence_Of (
7898                                  Request_Parameter, Loc),
7899                              Selector_Name =>
7900                                Make_Identifier (Loc, Name_Target)),
7901                            New_Occurrence_Of (Is_Local, Loc),
7902                            New_Occurrence_Of (Addr, Loc))));
7903
7904                      Expr := Unchecked_Convert_To (RACW_Type,
7905                        New_Occurrence_Of (Addr, Loc));
7906                   end;
7907
7908                elsif In_Present (Current_Parameter)
7909                   or else not Out_Present (Current_Parameter)
7910                   or else not Constrained
7911                then
7912                   --  If an input parameter is constrained, then its reading is
7913                   --  deferred until the beginning of the subprogram body. If
7914                   --  it is unconstrained, then an expression is built for
7915                   --  the object declaration and the variable is set using
7916                   --  'Input instead of 'Read.
7917
7918                   Expr := PolyORB_Support.Helpers.Build_From_Any_Call (
7919                             Etyp, New_Occurrence_Of (Any, Loc), Decls);
7920
7921                   if Constrained then
7922                      Append_To (Statements,
7923                        Make_Assignment_Statement (Loc,
7924                          Name       => New_Occurrence_Of (Object, Loc),
7925                          Expression => Expr));
7926                      Expr := Empty;
7927                   else
7928                      null;
7929
7930                      --  Expr will be used to initialize (and constrain) the
7931                      --  parameter when it is declared.
7932                   end if;
7933
7934                end if;
7935
7936                Need_Extra_Constrained :=
7937                  Nkind (Parameter_Type (Current_Parameter)) /=
7938                                                          N_Access_Definition
7939                    and then
7940                      Ekind (Defining_Identifier (Current_Parameter)) /= E_Void
7941                    and then
7942                      Present (Extra_Constrained
7943                        (Defining_Identifier (Current_Parameter)));
7944
7945                --  We may not associate an extra constrained actual to a
7946                --  constant object, so if one is needed, declare the actual
7947                --  as a variable even if it won't be modified.
7948
7949                Build_Actual_Object_Declaration
7950                  (Object   => Object,
7951                   Etyp     => Etyp,
7952                   Variable => Need_Extra_Constrained
7953                                 or else Out_Present (Current_Parameter),
7954                   Expr     => Expr,
7955                   Decls    => Decls);
7956                Set_Etype (Object, Etyp);
7957
7958                --  An out parameter may be written back using a 'Write
7959                --  attribute instead of a 'Output because it has been
7960                --  constrained by the parameter given to the caller. Note that
7961                --  out controlling arguments in the case of a RACW are not put
7962                --  back in the stream because the pointer on them has not
7963                --  changed.
7964
7965                if Out_Present (Current_Parameter)
7966                  and then not Is_Controlling_Formal
7967                then
7968                   Append_To (After_Statements,
7969                     Make_Procedure_Call_Statement (Loc,
7970                       Name => New_Occurrence_Of (RTE (RE_Move_Any_Value), Loc),
7971                       Parameter_Associations => New_List (
7972                         New_Occurrence_Of (Any, Loc),
7973                         PolyORB_Support.Helpers.Build_To_Any_Call
7974                           (New_Occurrence_Of (Object, Loc), Decls))));
7975                end if;
7976
7977                --  For RACW controlling formals, the Etyp of Object is always
7978                --  an RACW, even if the parameter is not of an anonymous access
7979                --  type. In such case, we need to dereference it at call time.
7980
7981                if Is_Controlling_Formal then
7982                   if Nkind (Parameter_Type (Current_Parameter)) /=
7983                                                         N_Access_Definition
7984                   then
7985                      Append_To (Parameter_List,
7986                        Make_Parameter_Association (Loc,
7987                          Selector_Name             =>
7988                            New_Occurrence_Of
7989                              (Defining_Identifier (Current_Parameter), Loc),
7990                          Explicit_Actual_Parameter =>
7991                            Make_Explicit_Dereference (Loc,
7992                              Prefix =>
7993                                Unchecked_Convert_To (RACW_Type,
7994                                  OK_Convert_To (RTE (RE_Address),
7995                                    New_Occurrence_Of (Object, Loc))))));
7996
7997                   else
7998                      Append_To (Parameter_List,
7999                        Make_Parameter_Association (Loc,
8000                          Selector_Name             =>
8001                            New_Occurrence_Of
8002                              (Defining_Identifier (Current_Parameter), Loc),
8003
8004                          Explicit_Actual_Parameter =>
8005                            Unchecked_Convert_To (RACW_Type,
8006                              OK_Convert_To (RTE (RE_Address),
8007                                New_Occurrence_Of (Object, Loc)))));
8008                   end if;
8009
8010                else
8011                   Append_To (Parameter_List,
8012                     Make_Parameter_Association (Loc,
8013                       Selector_Name             =>
8014                         New_Occurrence_Of (
8015                           Defining_Identifier (Current_Parameter), Loc),
8016                       Explicit_Actual_Parameter =>
8017                         New_Occurrence_Of (Object, Loc)));
8018                end if;
8019
8020                --  If the current parameter needs an extra formal, then read it
8021                --  from the stream and set the corresponding semantic field in
8022                --  the variable. If the kind of the parameter identifier is
8023                --  E_Void, then this is a compiler generated parameter that
8024                --  doesn't need an extra constrained status.
8025
8026                --  The case of Extra_Accessibility should also be handled ???
8027
8028                if Need_Extra_Constrained then
8029                   declare
8030                      Extra_Parameter : constant Entity_Id :=
8031                                          Extra_Constrained
8032                                            (Defining_Identifier
8033                                              (Current_Parameter));
8034
8035                      Extra_Any : constant Entity_Id :=
8036                                    Make_Defining_Identifier (Loc,
8037                                      Chars => New_Internal_Name ('A'));
8038
8039                      Formal_Entity : constant Entity_Id :=
8040                                        Make_Defining_Identifier (Loc,
8041                                          Chars => Chars (Extra_Parameter));
8042
8043                      Formal_Type : constant Entity_Id :=
8044                                      Etype (Extra_Parameter);
8045
8046                   begin
8047                      Append_To (Outer_Decls,
8048                        Make_Object_Declaration (Loc,
8049                          Defining_Identifier => Extra_Any,
8050                          Object_Definition   =>
8051                            New_Occurrence_Of (RTE (RE_Any), Loc),
8052                          Expression =>
8053                            Make_Function_Call (Loc,
8054                              Name =>
8055                                New_Occurrence_Of (RTE (RE_Create_Any), Loc),
8056                              Parameter_Associations => New_List (
8057                                PolyORB_Support.Helpers.Build_TypeCode_Call
8058                                  (Loc, Formal_Type, Outer_Decls)))));
8059
8060                      Append_To (Outer_Extra_Formal_Statements,
8061                        Add_Parameter_To_NVList (Loc,
8062                          Parameter   => Extra_Parameter,
8063                          NVList      => Arguments,
8064                          Constrained => True,
8065                          Any         => Extra_Any));
8066
8067                      Append_To (Decls,
8068                        Make_Object_Declaration (Loc,
8069                          Defining_Identifier => Formal_Entity,
8070                          Object_Definition   =>
8071                            New_Occurrence_Of (Formal_Type, Loc)));
8072
8073                      Append_To (Statements,
8074                        Make_Assignment_Statement (Loc,
8075                          Name => New_Occurrence_Of (Formal_Entity, Loc),
8076                          Expression =>
8077                            PolyORB_Support.Helpers.Build_From_Any_Call
8078                              (Formal_Type,
8079                               New_Occurrence_Of (Extra_Any, Loc),
8080                               Decls)));
8081                      Set_Extra_Constrained (Object, Formal_Entity);
8082                   end;
8083                end if;
8084             end;
8085
8086             Next (Current_Parameter);
8087          end loop;
8088
8089          --  Extra Formals should go after all the other parameters
8090
8091          Append_List_To (Outer_Statements, Outer_Extra_Formal_Statements);
8092
8093          Append_To (Outer_Statements,
8094            Make_Procedure_Call_Statement (Loc,
8095              Name => New_Occurrence_Of (RTE (RE_Request_Arguments), Loc),
8096              Parameter_Associations => New_List (
8097                New_Occurrence_Of (Request_Parameter, Loc),
8098                New_Occurrence_Of (Arguments, Loc))));
8099
8100          if Nkind (Specification (Vis_Decl)) = N_Function_Specification then
8101
8102             --  The remote subprogram is a function: Build an inner block to be
8103             --  able to hold a potentially unconstrained result in a variable.
8104
8105             declare
8106                Etyp   : constant Entity_Id :=
8107                           Etype (Result_Definition (Specification (Vis_Decl)));
8108                Result : constant Node_Id   :=
8109                           Make_Defining_Identifier (Loc,
8110                             Chars => New_Internal_Name ('R'));
8111
8112             begin
8113                Inner_Decls := New_List (
8114                  Make_Object_Declaration (Loc,
8115                    Defining_Identifier => Result,
8116                    Constant_Present    => True,
8117                    Object_Definition   => New_Occurrence_Of (Etyp, Loc),
8118                    Expression          =>
8119                      Make_Function_Call (Loc,
8120                        Name                   => Called_Subprogram,
8121                        Parameter_Associations => Parameter_List)));
8122
8123                if Is_Class_Wide_Type (Etyp) then
8124
8125                   --  For a remote call to a function with a class-wide type,
8126                   --  check that the returned value satisfies the requirements
8127                   --  of (RM E.4(18)).
8128
8129                   Append_To (Inner_Decls,
8130                     Make_Transportable_Check (Loc,
8131                       New_Occurrence_Of (Result, Loc)));
8132
8133                end if;
8134
8135                Set_Etype (Result, Etyp);
8136                Append_To (After_Statements,
8137                  Make_Procedure_Call_Statement (Loc,
8138                    Name => New_Occurrence_Of (RTE (RE_Set_Result), Loc),
8139                    Parameter_Associations => New_List (
8140                      New_Occurrence_Of (Request_Parameter, Loc),
8141                      PolyORB_Support.Helpers.Build_To_Any_Call
8142                        (New_Occurrence_Of (Result, Loc), Decls))));
8143
8144                --  A DSA function does not have out or inout arguments
8145             end;
8146
8147             Append_To (Statements,
8148               Make_Block_Statement (Loc,
8149                 Declarations               => Inner_Decls,
8150                 Handled_Statement_Sequence =>
8151                   Make_Handled_Sequence_Of_Statements (Loc,
8152                     Statements => After_Statements)));
8153
8154          else
8155             --  The remote subprogram is a procedure. We do not need any inner
8156             --  block in this case. No specific processing is required here for
8157             --  the dynamically asynchronous case: the indication of whether
8158             --  call is asynchronous or not is managed by the Sync_Scope
8159             --  attibute of the request, and is handled entirely in the
8160             --  protocol layer.
8161
8162             Append_To (After_Statements,
8163               Make_Procedure_Call_Statement (Loc,
8164                 Name => New_Occurrence_Of (RTE (RE_Request_Set_Out), Loc),
8165                 Parameter_Associations => New_List (
8166                   New_Occurrence_Of (Request_Parameter, Loc))));
8167
8168             Append_To (Statements,
8169               Make_Procedure_Call_Statement (Loc,
8170                 Name                   => Called_Subprogram,
8171                 Parameter_Associations => Parameter_List));
8172
8173             Append_List_To (Statements, After_Statements);
8174          end if;
8175
8176          Subp_Spec :=
8177            Make_Procedure_Specification (Loc,
8178              Defining_Unit_Name       =>
8179                Make_Defining_Identifier (Loc, New_Internal_Name ('F')),
8180
8181              Parameter_Specifications => New_List (
8182                Make_Parameter_Specification (Loc,
8183                  Defining_Identifier => Request_Parameter,
8184                  Parameter_Type      =>
8185                    New_Occurrence_Of (RTE (RE_Request_Access), Loc))));
8186
8187          --  An exception raised during the execution of an incoming
8188          --  remote subprogram call and that needs to be sent back
8189          --  to the caller is propagated by the receiving stubs, and
8190          --  will be handled by the caller (the distribution runtime).
8191
8192          if Asynchronous and then not Dynamically_Asynchronous then
8193
8194             --  For an asynchronous procedure, add a null exception handler
8195
8196             Excep_Handlers := New_List (
8197               Make_Implicit_Exception_Handler (Loc,
8198                 Exception_Choices => New_List (Make_Others_Choice (Loc)),
8199                 Statements        => New_List (Make_Null_Statement (Loc))));
8200
8201          else
8202             --  In the other cases, if an exception is raised, then the
8203             --  exception occurrence is propagated.
8204
8205             null;
8206          end if;
8207
8208          Append_To (Outer_Statements,
8209            Make_Block_Statement (Loc,
8210              Declarations => Decls,
8211              Handled_Statement_Sequence =>
8212                Make_Handled_Sequence_Of_Statements (Loc,
8213                  Statements => Statements)));
8214
8215          return
8216            Make_Subprogram_Body (Loc,
8217              Specification              => Subp_Spec,
8218              Declarations               => Outer_Decls,
8219              Handled_Statement_Sequence =>
8220                Make_Handled_Sequence_Of_Statements (Loc,
8221                  Statements         => Outer_Statements,
8222                  Exception_Handlers => Excep_Handlers));
8223       end Build_Subprogram_Receiving_Stubs;
8224
8225       -------------
8226       -- Helpers --
8227       -------------
8228
8229       package body Helpers is
8230
8231          -----------------------
8232          -- Local Subprograms --
8233          -----------------------
8234
8235          function Find_Numeric_Representation
8236            (Typ : Entity_Id) return Entity_Id;
8237          --  Given a numeric type Typ, return the smallest integer or floating
8238          --  point type from Standard, or the smallest unsigned (modular) type
8239          --  from System.Unsigned_Types, whose range encompasses that of Typ.
8240
8241          function Make_Helper_Function_Name
8242            (Loc : Source_Ptr;
8243             Typ : Entity_Id;
8244             Nam : Name_Id) return Entity_Id;
8245          --  Return the name to be assigned for helper subprogram Nam of Typ
8246
8247          ------------------------------------------------------------
8248          -- Common subprograms for building various tree fragments --
8249          ------------------------------------------------------------
8250
8251          function Build_Get_Aggregate_Element
8252            (Loc : Source_Ptr;
8253             Any : Entity_Id;
8254             TC  : Node_Id;
8255             Idx : Node_Id) return Node_Id;
8256          --  Build a call to Get_Aggregate_Element on Any for typecode TC,
8257          --  returning the Idx'th element.
8258
8259          generic
8260             Subprogram : Entity_Id;
8261             --  Reference location for constructed nodes
8262
8263             Arry : Entity_Id;
8264             --  For 'Range and Etype
8265
8266             Indices : List_Id;
8267             --  For the construction of the innermost element expression
8268
8269             with procedure Add_Process_Element
8270               (Stmts   : List_Id;
8271                Any     : Entity_Id;
8272                Counter : Entity_Id;
8273                Datum   : Node_Id);
8274
8275          procedure Append_Array_Traversal
8276            (Stmts   : List_Id;
8277             Any     : Entity_Id;
8278             Counter : Entity_Id := Empty;
8279             Depth   : Pos       := 1);
8280          --  Build nested loop statements that iterate over the elements of an
8281          --  array Arry. The statement(s) built by Add_Process_Element are
8282          --  executed for each element; Indices is the list of indices to be
8283          --  used in the construction of the indexed component that denotes the
8284          --  current element. Subprogram is the entity for the subprogram for
8285          --  which this iterator is generated. The generated statements are
8286          --  appended to Stmts.
8287
8288          generic
8289             Rec : Entity_Id;
8290             --  The record entity being dealt with
8291
8292             with procedure Add_Process_Element
8293               (Stmts     : List_Id;
8294                Container : Node_Or_Entity_Id;
8295                Counter   : in out Int;
8296                Rec       : Entity_Id;
8297                Field     : Node_Id);
8298             --  Rec is the instance of the record type, or Empty.
8299             --  Field is either the N_Defining_Identifier for a component,
8300             --  or an N_Variant_Part.
8301
8302          procedure Append_Record_Traversal
8303            (Stmts     : List_Id;
8304             Clist     : Node_Id;
8305             Container : Node_Or_Entity_Id;
8306             Counter   : in out Int);
8307          --  Process component list Clist. Individual fields are passed
8308          --  to Field_Processing. Each variant part is also processed.
8309          --  Container is the outer Any (for From_Any/To_Any),
8310          --  the outer typecode (for TC) to which the operation applies.
8311
8312          -----------------------------
8313          -- Append_Record_Traversal --
8314          -----------------------------
8315
8316          procedure Append_Record_Traversal
8317            (Stmts     : List_Id;
8318             Clist     : Node_Id;
8319             Container : Node_Or_Entity_Id;
8320             Counter   : in out Int)
8321          is
8322             CI : List_Id;
8323             VP : Node_Id;
8324             --  Clist's Component_Items and Variant_Part
8325
8326             Item : Node_Id;
8327             Def  : Entity_Id;
8328
8329          begin
8330             if No (Clist) then
8331                return;
8332             end if;
8333
8334             CI := Component_Items (Clist);
8335             VP := Variant_Part (Clist);
8336
8337             Item := First (CI);
8338             while Present (Item) loop
8339                Def := Defining_Identifier (Item);
8340
8341                if not Is_Internal_Name (Chars (Def)) then
8342                   Add_Process_Element
8343                     (Stmts, Container, Counter, Rec, Def);
8344                end if;
8345
8346                Next (Item);
8347             end loop;
8348
8349             if Present (VP) then
8350                Add_Process_Element (Stmts, Container, Counter, Rec, VP);
8351             end if;
8352          end Append_Record_Traversal;
8353
8354          -------------------------
8355          -- Build_From_Any_Call --
8356          -------------------------
8357
8358          function Build_From_Any_Call
8359            (Typ   : Entity_Id;
8360             N     : Node_Id;
8361             Decls : List_Id) return Node_Id
8362          is
8363             Loc : constant Source_Ptr := Sloc (N);
8364
8365             U_Type : Entity_Id  := Underlying_Type (Typ);
8366
8367             Fnam    : Entity_Id := Empty;
8368             Lib_RE  : RE_Id := RE_Null;
8369             Result  : Node_Id;
8370
8371          begin
8372             --  First simple case where the From_Any function is present
8373             --  in the type's TSS.
8374
8375             Fnam := Find_Inherited_TSS (U_Type, TSS_From_Any);
8376
8377             if Sloc (U_Type) <= Standard_Location then
8378                U_Type := Base_Type (U_Type);
8379             end if;
8380
8381             --  Check first for Boolean and Character. These are enumeration
8382             --  types, but we treat them specially, since they may require
8383             --  special handling in the transfer protocol. However, this
8384             --  special handling only applies if they have standard
8385             --  representation, otherwise they are treated like any other
8386             --  enumeration type.
8387
8388             if Present (Fnam) then
8389                null;
8390
8391             elsif U_Type = Standard_Boolean then
8392                Lib_RE := RE_FA_B;
8393
8394             elsif U_Type = Standard_Character then
8395                Lib_RE := RE_FA_C;
8396
8397             elsif U_Type = Standard_Wide_Character then
8398                Lib_RE := RE_FA_WC;
8399
8400             elsif U_Type = Standard_Wide_Wide_Character then
8401                Lib_RE := RE_FA_WWC;
8402
8403             --  Floating point types
8404
8405             elsif U_Type = Standard_Short_Float then
8406                Lib_RE := RE_FA_SF;
8407
8408             elsif U_Type = Standard_Float then
8409                Lib_RE := RE_FA_F;
8410
8411             elsif U_Type = Standard_Long_Float then
8412                Lib_RE := RE_FA_LF;
8413
8414             elsif U_Type = Standard_Long_Long_Float then
8415                Lib_RE := RE_FA_LLF;
8416
8417             --  Integer types
8418
8419             elsif U_Type = Etype (Standard_Short_Short_Integer) then
8420                   Lib_RE := RE_FA_SSI;
8421
8422             elsif U_Type = Etype (Standard_Short_Integer) then
8423                Lib_RE := RE_FA_SI;
8424
8425             elsif U_Type = Etype (Standard_Integer) then
8426                Lib_RE := RE_FA_I;
8427
8428             elsif U_Type = Etype (Standard_Long_Integer) then
8429                Lib_RE := RE_FA_LI;
8430
8431             elsif U_Type = Etype (Standard_Long_Long_Integer) then
8432                Lib_RE := RE_FA_LLI;
8433
8434             --  Unsigned integer types
8435
8436             elsif U_Type = RTE (RE_Short_Short_Unsigned) then
8437                Lib_RE := RE_FA_SSU;
8438
8439             elsif U_Type = RTE (RE_Short_Unsigned) then
8440                Lib_RE := RE_FA_SU;
8441
8442             elsif U_Type = RTE (RE_Unsigned) then
8443                Lib_RE := RE_FA_U;
8444
8445             elsif U_Type = RTE (RE_Long_Unsigned) then
8446                Lib_RE := RE_FA_LU;
8447
8448             elsif U_Type = RTE (RE_Long_Long_Unsigned) then
8449                Lib_RE := RE_FA_LLU;
8450
8451             elsif U_Type = Standard_String then
8452                Lib_RE := RE_FA_String;
8453
8454             --  Special DSA types
8455
8456             elsif Is_RTE (U_Type, RE_Any_Container_Ptr) then
8457                Lib_RE := RE_FA_A;
8458
8459             --  Other (non-primitive) types
8460
8461             else
8462                declare
8463                   Decl : Entity_Id;
8464                   Typ  : Entity_Id := U_Type;
8465
8466                begin
8467                   --  For the subtype representing a generic actual type, go
8468                   --  to the base type.
8469
8470                   if Is_Generic_Actual_Type (Typ) then
8471                      Typ := Base_Type (Typ);
8472                   end if;
8473
8474                   Build_From_Any_Function (Loc, Typ, Decl, Fnam);
8475                   Append_To (Decls, Decl);
8476                end;
8477             end if;
8478
8479             --  Call the function
8480
8481             if Lib_RE /= RE_Null then
8482                pragma Assert (No (Fnam));
8483                Fnam := RTE (Lib_RE);
8484             end if;
8485
8486             Result :=
8487               Make_Function_Call (Loc,
8488                 Name                   => New_Occurrence_Of (Fnam, Loc),
8489                 Parameter_Associations => New_List (N));
8490
8491             --  We must set the type of Result, so the unchecked conversion
8492             --  from the underlying type to the base type is properly done.
8493
8494             Set_Etype (Result, U_Type);
8495
8496             return Unchecked_Convert_To (Typ, Result);
8497          end Build_From_Any_Call;
8498
8499          -----------------------------
8500          -- Build_From_Any_Function --
8501          -----------------------------
8502
8503          procedure Build_From_Any_Function
8504            (Loc  : Source_Ptr;
8505             Typ  : Entity_Id;
8506             Decl : out Node_Id;
8507             Fnam : out Entity_Id)
8508          is
8509             Spec  : Node_Id;
8510             Decls : constant List_Id := New_List;
8511             Stms  : constant List_Id := New_List;
8512
8513             Any_Parameter : constant Entity_Id :=
8514                               Make_Defining_Identifier (Loc,
8515                                 New_Internal_Name ('A'));
8516
8517             Use_Opaque_Representation : Boolean;
8518
8519          begin
8520             if Is_Itype (Typ) then
8521                Build_From_Any_Function
8522                   (Loc  => Loc,
8523                    Typ  => Etype (Typ),
8524                    Decl => Decl,
8525                    Fnam => Fnam);
8526                return;
8527             end if;
8528
8529             Fnam := Make_Helper_Function_Name (Loc, Typ, Name_From_Any);
8530
8531             Spec :=
8532               Make_Function_Specification (Loc,
8533                 Defining_Unit_Name => Fnam,
8534                 Parameter_Specifications => New_List (
8535                   Make_Parameter_Specification (Loc,
8536                     Defining_Identifier => Any_Parameter,
8537                     Parameter_Type => New_Occurrence_Of (RTE (RE_Any), Loc))),
8538                 Result_Definition => New_Occurrence_Of (Typ, Loc));
8539
8540             --  The RACW case is taken care of by Exp_Dist.Add_RACW_From_Any
8541
8542             pragma Assert
8543               (not (Is_Remote_Access_To_Class_Wide_Type (Typ)));
8544
8545             Use_Opaque_Representation := False;
8546
8547             if Has_Stream_Attribute_Definition
8548                  (Typ, TSS_Stream_Output, At_Any_Place => True)
8549               or else
8550                Has_Stream_Attribute_Definition
8551                  (Typ, TSS_Stream_Write, At_Any_Place => True)
8552             then
8553                --  If user-defined stream attributes are specified for this
8554                --  type, use them and transmit data as an opaque sequence of
8555                --  stream elements.
8556
8557                Use_Opaque_Representation := True;
8558
8559             elsif Is_Derived_Type (Typ) and then not Is_Tagged_Type (Typ) then
8560                Append_To (Stms,
8561                  Make_Simple_Return_Statement (Loc,
8562                    Expression =>
8563                      OK_Convert_To (Typ,
8564                        Build_From_Any_Call
8565                          (Root_Type (Typ),
8566                           New_Occurrence_Of (Any_Parameter, Loc),
8567                           Decls))));
8568
8569             elsif Is_Record_Type (Typ)
8570               and then not Is_Derived_Type (Typ)
8571               and then not Is_Tagged_Type (Typ)
8572             then
8573                if Nkind (Declaration_Node (Typ)) = N_Subtype_Declaration then
8574                   Append_To (Stms,
8575                     Make_Simple_Return_Statement (Loc,
8576                       Expression =>
8577                         Build_From_Any_Call
8578                           (Etype (Typ),
8579                            New_Occurrence_Of (Any_Parameter, Loc),
8580                            Decls)));
8581
8582                else
8583                   declare
8584                      Disc                      : Entity_Id := Empty;
8585                      Discriminant_Associations : List_Id;
8586                      Rdef                      : constant Node_Id :=
8587                                                    Type_Definition
8588                                                      (Declaration_Node (Typ));
8589                      Component_Counter         : Int := 0;
8590
8591                      --  The returned object
8592
8593                      Res : constant Entity_Id :=
8594                              Make_Defining_Identifier (Loc,
8595                                New_Internal_Name ('R'));
8596
8597                      Res_Definition : Node_Id := New_Occurrence_Of (Typ, Loc);
8598
8599                      procedure FA_Rec_Add_Process_Element
8600                        (Stmts   : List_Id;
8601                         Any     : Entity_Id;
8602                         Counter : in out Int;
8603                         Rec     : Entity_Id;
8604                         Field   : Node_Id);
8605
8606                      procedure FA_Append_Record_Traversal is
8607                         new Append_Record_Traversal
8608                           (Rec                 => Res,
8609                            Add_Process_Element => FA_Rec_Add_Process_Element);
8610
8611                      --------------------------------
8612                      -- FA_Rec_Add_Process_Element --
8613                      --------------------------------
8614
8615                      procedure FA_Rec_Add_Process_Element
8616                        (Stmts   : List_Id;
8617                         Any     : Entity_Id;
8618                         Counter : in out Int;
8619                         Rec     : Entity_Id;
8620                         Field   : Node_Id)
8621                      is
8622                      begin
8623                         if Nkind (Field) = N_Defining_Identifier then
8624
8625                            --  A regular component
8626
8627                            Append_To (Stmts,
8628                              Make_Assignment_Statement (Loc,
8629                                Name => Make_Selected_Component (Loc,
8630                                  Prefix        =>
8631                                    New_Occurrence_Of (Rec, Loc),
8632                                  Selector_Name =>
8633                                    New_Occurrence_Of (Field, Loc)),
8634                                Expression =>
8635                                  Build_From_Any_Call (Etype (Field),
8636                                    Build_Get_Aggregate_Element (Loc,
8637                                      Any => Any,
8638                                      TC  => Build_TypeCode_Call (Loc,
8639                                               Etype (Field), Decls),
8640                                      Idx => Make_Integer_Literal (Loc,
8641                                               Counter)),
8642                                    Decls)));
8643
8644                         else
8645                            --  A variant part
8646
8647                            declare
8648                               Variant        : Node_Id;
8649                               Struct_Counter : Int := 0;
8650
8651                               Block_Decls : constant List_Id := New_List;
8652                               Block_Stmts : constant List_Id := New_List;
8653                               VP_Stmts    : List_Id;
8654
8655                               Alt_List    : constant List_Id := New_List;
8656                               Choice_List : List_Id;
8657
8658                               Struct_Any : constant Entity_Id :=
8659                                              Make_Defining_Identifier (Loc,
8660                                                New_Internal_Name ('S'));
8661
8662                            begin
8663                               Append_To (Decls,
8664                                 Make_Object_Declaration (Loc,
8665                                   Defining_Identifier => Struct_Any,
8666                                   Constant_Present    => True,
8667                                   Object_Definition   =>
8668                                      New_Occurrence_Of (RTE (RE_Any), Loc),
8669                                   Expression          =>
8670                                     Make_Function_Call (Loc,
8671                                       Name =>
8672                                         New_Occurrence_Of
8673                                           (RTE (RE_Extract_Union_Value), Loc),
8674
8675                                       Parameter_Associations => New_List (
8676                                         Build_Get_Aggregate_Element (Loc,
8677                                           Any => Any,
8678                                           TC  =>
8679                                             Make_Function_Call (Loc,
8680                                               Name => New_Occurrence_Of (
8681                                                 RTE (RE_Any_Member_Type), Loc),
8682                                               Parameter_Associations =>
8683                                                 New_List (
8684                                                   New_Occurrence_Of (Any, Loc),
8685                                                   Make_Integer_Literal (Loc,
8686                                                     Intval => Counter))),
8687                                           Idx =>
8688                                             Make_Integer_Literal (Loc,
8689                                              Intval => Counter))))));
8690
8691                               Append_To (Stmts,
8692                                 Make_Block_Statement (Loc,
8693                                   Declarations => Block_Decls,
8694                                   Handled_Statement_Sequence =>
8695                                     Make_Handled_Sequence_Of_Statements (Loc,
8696                                       Statements => Block_Stmts)));
8697
8698                               Append_To (Block_Stmts,
8699                                 Make_Case_Statement (Loc,
8700                                     Expression =>
8701                                       Make_Selected_Component (Loc,
8702                                         Prefix        => Rec,
8703                                         Selector_Name => Chars (Name (Field))),
8704                                     Alternatives => Alt_List));
8705
8706                               Variant := First_Non_Pragma (Variants (Field));
8707                               while Present (Variant) loop
8708                                  Choice_List :=
8709                                    New_Copy_List_Tree
8710                                      (Discrete_Choices (Variant));
8711
8712                                  VP_Stmts := New_List;
8713
8714                                  --  Struct_Counter should be reset before
8715                                  --  handling a variant part. Indeed only one
8716                                  --  of the case statement alternatives will be
8717                                  --  executed at run-time, so the counter must
8718                                  --  start at 0 for every case statement.
8719
8720                                  Struct_Counter := 0;
8721
8722                                  FA_Append_Record_Traversal (
8723                                    Stmts     => VP_Stmts,
8724                                    Clist     => Component_List (Variant),
8725                                    Container => Struct_Any,
8726                                    Counter   => Struct_Counter);
8727
8728                                  Append_To (Alt_List,
8729                                    Make_Case_Statement_Alternative (Loc,
8730                                      Discrete_Choices => Choice_List,
8731                                      Statements       => VP_Stmts));
8732                                  Next_Non_Pragma (Variant);
8733                               end loop;
8734                            end;
8735                         end if;
8736
8737                         Counter := Counter + 1;
8738                      end FA_Rec_Add_Process_Element;
8739
8740                   begin
8741                      --  First all discriminants
8742
8743                      if Has_Discriminants (Typ) then
8744                         Discriminant_Associations := New_List;
8745
8746                         Disc := First_Discriminant (Typ);
8747                         while Present (Disc) loop
8748                            declare
8749                               Disc_Var_Name : constant Entity_Id :=
8750                                                 Make_Defining_Identifier (Loc,
8751                                                   Chars => Chars (Disc));
8752                               Disc_Type     : constant Entity_Id :=
8753                                                 Etype (Disc);
8754
8755                            begin
8756                               Append_To (Decls,
8757                                 Make_Object_Declaration (Loc,
8758                                   Defining_Identifier => Disc_Var_Name,
8759                                   Constant_Present    => True,
8760                                   Object_Definition   =>
8761                                     New_Occurrence_Of (Disc_Type, Loc),
8762
8763                                   Expression =>
8764                                     Build_From_Any_Call (Disc_Type,
8765                                       Build_Get_Aggregate_Element (Loc,
8766                                         Any => Any_Parameter,
8767                                         TC  => Build_TypeCode_Call
8768                                                  (Loc, Disc_Type, Decls),
8769                                         Idx => Make_Integer_Literal (Loc,
8770                                                Intval => Component_Counter)),
8771                                       Decls)));
8772
8773                               Component_Counter := Component_Counter + 1;
8774
8775                               Append_To (Discriminant_Associations,
8776                                 Make_Discriminant_Association (Loc,
8777                                   Selector_Names => New_List (
8778                                     New_Occurrence_Of (Disc, Loc)),
8779                                   Expression =>
8780                                     New_Occurrence_Of (Disc_Var_Name, Loc)));
8781                            end;
8782                            Next_Discriminant (Disc);
8783                         end loop;
8784
8785                         Res_Definition :=
8786                           Make_Subtype_Indication (Loc,
8787                             Subtype_Mark => Res_Definition,
8788                             Constraint   =>
8789                               Make_Index_Or_Discriminant_Constraint (Loc,
8790                                 Discriminant_Associations));
8791                      end if;
8792
8793                      --  Now we have all the discriminants in variables, we can
8794                      --  declared a constrained object. Note that we are not
8795                      --  initializing (non-discriminant) components directly in
8796                      --  the object declarations, because which fields to
8797                      --  initialize depends (at run time) on the discriminant
8798                      --  values.
8799
8800                      Append_To (Decls,
8801                        Make_Object_Declaration (Loc,
8802                          Defining_Identifier => Res,
8803                          Object_Definition   => Res_Definition));
8804
8805                      --  ... then all components
8806
8807                      FA_Append_Record_Traversal (Stms,
8808                        Clist     => Component_List (Rdef),
8809                        Container => Any_Parameter,
8810                        Counter   => Component_Counter);
8811
8812                      Append_To (Stms,
8813                        Make_Simple_Return_Statement (Loc,
8814                          Expression => New_Occurrence_Of (Res, Loc)));
8815                   end;
8816                end if;
8817
8818             elsif Is_Array_Type (Typ) then
8819                declare
8820                   Constrained : constant Boolean := Is_Constrained (Typ);
8821
8822                   procedure FA_Ary_Add_Process_Element
8823                     (Stmts   : List_Id;
8824                      Any     : Entity_Id;
8825                      Counter : Entity_Id;
8826                      Datum   : Node_Id);
8827                   --  Assign the current element (as identified by Counter) of
8828                   --  Any to the variable denoted by name Datum, and advance
8829                   --  Counter by 1. If Datum is not an Any, a call to From_Any
8830                   --  for its type is inserted.
8831
8832                   --------------------------------
8833                   -- FA_Ary_Add_Process_Element --
8834                   --------------------------------
8835
8836                   procedure FA_Ary_Add_Process_Element
8837                     (Stmts   : List_Id;
8838                      Any     : Entity_Id;
8839                      Counter : Entity_Id;
8840                      Datum   : Node_Id)
8841                   is
8842                      Assignment : constant Node_Id :=
8843                        Make_Assignment_Statement (Loc,
8844                          Name       => Datum,
8845                          Expression => Empty);
8846
8847                      Element_Any : Node_Id;
8848
8849                   begin
8850                      declare
8851                         Element_TC : Node_Id;
8852
8853                      begin
8854                         if Etype (Datum) = RTE (RE_Any) then
8855
8856                            --  When Datum is an Any the Etype field is not
8857                            --  sufficient to determine the typecode of Datum
8858                            --  (which can be a TC_SEQUENCE or TC_ARRAY
8859                            --  depending on the value of Constrained).
8860
8861                            --  Therefore we retrieve the typecode which has
8862                            --  been constructed in Append_Array_Traversal with
8863                            --  a call to Get_Any_Type.
8864
8865                            Element_TC :=
8866                              Make_Function_Call (Loc,
8867                                Name => New_Occurrence_Of (
8868                                  RTE (RE_Get_Any_Type), Loc),
8869                                Parameter_Associations => New_List (
8870                                  New_Occurrence_Of (Entity (Datum), Loc)));
8871                         else
8872                            --  For non Any Datum we simply construct a typecode
8873                            --  matching the Etype of the Datum.
8874
8875                            Element_TC := Build_TypeCode_Call
8876                               (Loc, Etype (Datum), Decls);
8877                         end if;
8878
8879                         Element_Any :=
8880                           Build_Get_Aggregate_Element (Loc,
8881                             Any => Any,
8882                             TC  => Element_TC,
8883                             Idx => New_Occurrence_Of (Counter, Loc));
8884                      end;
8885
8886                      --  Note: here we *prepend* statements to Stmts, so
8887                      --  we must do it in reverse order.
8888
8889                      Prepend_To (Stmts,
8890                        Make_Assignment_Statement (Loc,
8891                          Name =>
8892                            New_Occurrence_Of (Counter, Loc),
8893                          Expression =>
8894                            Make_Op_Add (Loc,
8895                              Left_Opnd  => New_Occurrence_Of (Counter, Loc),
8896                              Right_Opnd => Make_Integer_Literal (Loc, 1))));
8897
8898                      if Nkind (Datum) /= N_Attribute_Reference then
8899
8900                         --  We ignore the value of the length of each
8901                         --  dimension, since the target array has already
8902                         --  been constrained anyway.
8903
8904                         if Etype (Datum) /= RTE (RE_Any) then
8905                            Set_Expression (Assignment,
8906                               Build_From_Any_Call
8907                                 (Component_Type (Typ), Element_Any, Decls));
8908                         else
8909                            Set_Expression (Assignment, Element_Any);
8910                         end if;
8911
8912                         Prepend_To (Stmts, Assignment);
8913                      end if;
8914                   end FA_Ary_Add_Process_Element;
8915
8916                   ------------------------
8917                   -- Local Declarations --
8918                   ------------------------
8919
8920                   Counter : constant Entity_Id :=
8921                               Make_Defining_Identifier (Loc, Name_J);
8922
8923                   Initial_Counter_Value : Int := 0;
8924
8925                   Component_TC : constant Entity_Id :=
8926                                    Make_Defining_Identifier (Loc, Name_T);
8927
8928                   Res : constant Entity_Id :=
8929                           Make_Defining_Identifier (Loc, Name_R);
8930
8931                   procedure Append_From_Any_Array_Iterator is
8932                     new Append_Array_Traversal (
8933                       Subprogram => Fnam,
8934                       Arry       => Res,
8935                       Indices    => New_List,
8936                       Add_Process_Element => FA_Ary_Add_Process_Element);
8937
8938                   Res_Subtype_Indication : Node_Id :=
8939                                              New_Occurrence_Of (Typ, Loc);
8940
8941                begin
8942                   if not Constrained then
8943                      declare
8944                         Ndim : constant Int := Number_Dimensions (Typ);
8945                         Lnam : Name_Id;
8946                         Hnam : Name_Id;
8947                         Indx : Node_Id := First_Index (Typ);
8948                         Indt : Entity_Id;
8949
8950                         Ranges : constant List_Id := New_List;
8951
8952                      begin
8953                         for J in 1 .. Ndim loop
8954                            Lnam := New_External_Name ('L', J);
8955                            Hnam := New_External_Name ('H', J);
8956                            Indt := Etype (Indx);
8957
8958                            Append_To (Decls,
8959                              Make_Object_Declaration (Loc,
8960                                Defining_Identifier =>
8961                                  Make_Defining_Identifier (Loc, Lnam),
8962                                Constant_Present    => True,
8963                                Object_Definition   =>
8964                                  New_Occurrence_Of (Indt, Loc),
8965                                Expression          =>
8966                                  Build_From_Any_Call
8967                                    (Indt,
8968                                     Build_Get_Aggregate_Element (Loc,
8969                                       Any => Any_Parameter,
8970                                       TC  => Build_TypeCode_Call
8971                                                (Loc, Indt, Decls),
8972                                       Idx =>
8973                                         Make_Integer_Literal (Loc, J - 1)),
8974                                    Decls)));
8975
8976                            Append_To (Decls,
8977                              Make_Object_Declaration (Loc,
8978                                Defining_Identifier =>
8979                                  Make_Defining_Identifier (Loc, Hnam),
8980
8981                                Constant_Present => True,
8982
8983                                Object_Definition =>
8984                                  New_Occurrence_Of (Indt, Loc),
8985
8986                                Expression => Make_Attribute_Reference (Loc,
8987                                  Prefix         =>
8988                                    New_Occurrence_Of (Indt, Loc),
8989
8990                                  Attribute_Name => Name_Val,
8991
8992                                  Expressions    => New_List (
8993                                    Make_Op_Subtract (Loc,
8994                                      Left_Opnd =>
8995                                        Make_Op_Add (Loc,
8996                                          Left_Opnd =>
8997                                            OK_Convert_To (
8998                                              Standard_Long_Integer,
8999                                              Make_Identifier (Loc, Lnam)),
9000
9001                                          Right_Opnd =>
9002                                            OK_Convert_To (
9003                                              Standard_Long_Integer,
9004                                              Make_Function_Call (Loc,
9005                                                Name =>
9006                                                  New_Occurrence_Of (RTE (
9007                                                  RE_Get_Nested_Sequence_Length
9008                                                  ), Loc),
9009                                                Parameter_Associations =>
9010                                                  New_List (
9011                                                    New_Occurrence_Of (
9012                                                      Any_Parameter, Loc),
9013                                                    Make_Integer_Literal (Loc,
9014                                                      Intval => J))))),
9015
9016                                      Right_Opnd =>
9017                                        Make_Integer_Literal (Loc, 1))))));
9018
9019                            Append_To (Ranges,
9020                              Make_Range (Loc,
9021                                Low_Bound  => Make_Identifier (Loc, Lnam),
9022                                High_Bound => Make_Identifier (Loc, Hnam)));
9023
9024                            Next_Index (Indx);
9025                         end loop;
9026
9027                         --  Now we have all the necessary bound information:
9028                         --  apply the set of range constraints to the
9029                         --  (unconstrained) nominal subtype of Res.
9030
9031                         Initial_Counter_Value := Ndim;
9032                         Res_Subtype_Indication := Make_Subtype_Indication (Loc,
9033                           Subtype_Mark => Res_Subtype_Indication,
9034                           Constraint   =>
9035                             Make_Index_Or_Discriminant_Constraint (Loc,
9036                               Constraints => Ranges));
9037                      end;
9038                   end if;
9039
9040                   Append_To (Decls,
9041                     Make_Object_Declaration (Loc,
9042                       Defining_Identifier => Res,
9043                       Object_Definition => Res_Subtype_Indication));
9044                   Set_Etype (Res, Typ);
9045
9046                   Append_To (Decls,
9047                     Make_Object_Declaration (Loc,
9048                       Defining_Identifier => Counter,
9049                       Object_Definition =>
9050                         New_Occurrence_Of (RTE (RE_Long_Unsigned), Loc),
9051                       Expression =>
9052                         Make_Integer_Literal (Loc, Initial_Counter_Value)));
9053
9054                   Append_To (Decls,
9055                     Make_Object_Declaration (Loc,
9056                       Defining_Identifier => Component_TC,
9057                       Constant_Present    => True,
9058                       Object_Definition   =>
9059                         New_Occurrence_Of (RTE (RE_TypeCode), Loc),
9060                       Expression          =>
9061                         Build_TypeCode_Call (Loc,
9062                           Component_Type (Typ), Decls)));
9063
9064                   Append_From_Any_Array_Iterator
9065                     (Stms, Any_Parameter, Counter);
9066
9067                   Append_To (Stms,
9068                     Make_Simple_Return_Statement (Loc,
9069                       Expression => New_Occurrence_Of (Res, Loc)));
9070                end;
9071
9072             elsif Is_Integer_Type (Typ) or else Is_Unsigned_Type (Typ) then
9073                Append_To (Stms,
9074                  Make_Simple_Return_Statement (Loc,
9075                    Expression =>
9076                      Unchecked_Convert_To (Typ,
9077                        Build_From_Any_Call
9078                          (Find_Numeric_Representation (Typ),
9079                           New_Occurrence_Of (Any_Parameter, Loc),
9080                           Decls))));
9081
9082             else
9083                Use_Opaque_Representation := True;
9084             end if;
9085
9086             if Use_Opaque_Representation then
9087
9088                --  Default: type is represented as an opaque sequence of bytes
9089
9090                declare
9091                   Strm : constant Entity_Id :=
9092                            Make_Defining_Identifier (Loc,
9093                              Chars => New_Internal_Name ('S'));
9094                   Res  : constant Entity_Id :=
9095                            Make_Defining_Identifier (Loc,
9096                              Chars => New_Internal_Name ('R'));
9097
9098                begin
9099                   --  Strm : Buffer_Stream_Type;
9100
9101                   Append_To (Decls,
9102                     Make_Object_Declaration (Loc,
9103                       Defining_Identifier => Strm,
9104                       Aliased_Present     => True,
9105                       Object_Definition   =>
9106                         New_Occurrence_Of (RTE (RE_Buffer_Stream_Type), Loc)));
9107
9108                   --  Allocate_Buffer (Strm);
9109
9110                   Append_To (Stms,
9111                     Make_Procedure_Call_Statement (Loc,
9112                       Name =>
9113                         New_Occurrence_Of (RTE (RE_Allocate_Buffer), Loc),
9114                       Parameter_Associations => New_List (
9115                         New_Occurrence_Of (Strm, Loc))));
9116
9117                   --  Any_To_BS (Strm, A);
9118
9119                   Append_To (Stms,
9120                     Make_Procedure_Call_Statement (Loc,
9121                       Name => New_Occurrence_Of (RTE (RE_Any_To_BS), Loc),
9122                       Parameter_Associations => New_List (
9123                         New_Occurrence_Of (Any_Parameter, Loc),
9124                         New_Occurrence_Of (Strm, Loc))));
9125
9126                   if Transmit_As_Unconstrained (Typ) then
9127
9128                      --  declare
9129                      --     Res : constant T := T'Input (Strm);
9130                      --  begin
9131                      --     Release_Buffer (Strm);
9132                      --     return Res;
9133                      --  end;
9134
9135                      Append_To (Stms, Make_Block_Statement (Loc,
9136                        Declarations               => New_List (
9137                          Make_Object_Declaration (Loc,
9138                            Defining_Identifier => Res,
9139                            Constant_Present    => True,
9140                            Object_Definition   => New_Occurrence_Of (Typ, Loc),
9141                            Expression          =>
9142                              Make_Attribute_Reference (Loc,
9143                                Prefix         => New_Occurrence_Of (Typ, Loc),
9144                                Attribute_Name => Name_Input,
9145                                Expressions    => New_List (
9146                                  Make_Attribute_Reference (Loc,
9147                                    Prefix         =>
9148                                      New_Occurrence_Of (Strm, Loc),
9149                                    Attribute_Name => Name_Access))))),
9150
9151                        Handled_Statement_Sequence =>
9152                          Make_Handled_Sequence_Of_Statements (Loc,
9153                            Statements => New_List (
9154                              Make_Procedure_Call_Statement (Loc,
9155                                Name                   =>
9156                                  New_Occurrence_Of
9157                                    (RTE (RE_Release_Buffer), Loc),
9158                                Parameter_Associations =>
9159                                  New_List (New_Occurrence_Of (Strm, Loc))),
9160
9161                              Make_Simple_Return_Statement (Loc,
9162                                Expression => New_Occurrence_Of (Res, Loc))))));
9163
9164                   else
9165                      --  declare
9166                      --     Res : T;
9167                      --  begin
9168                      --     T'Read (Strm, Res);
9169                      --     Release_Buffer (Strm);
9170                      --     return Res;
9171                      --  end;
9172
9173                      Append_To (Stms, Make_Block_Statement (Loc,
9174                        Declarations               => New_List (
9175                          Make_Object_Declaration (Loc,
9176                            Defining_Identifier => Res,
9177                            Constant_Present    => False,
9178                            Object_Definition   =>
9179                              New_Occurrence_Of (Typ, Loc))),
9180
9181                        Handled_Statement_Sequence =>
9182                          Make_Handled_Sequence_Of_Statements (Loc,
9183                            Statements => New_List (
9184                              Make_Attribute_Reference (Loc,
9185                                Prefix         => New_Occurrence_Of (Typ, Loc),
9186                                Attribute_Name => Name_Read,
9187                                Expressions    => New_List (
9188                                  Make_Attribute_Reference (Loc,
9189                                    Prefix         =>
9190                                      New_Occurrence_Of (Strm, Loc),
9191                                    Attribute_Name => Name_Access),
9192                                  New_Occurrence_Of (Res, Loc))),
9193
9194                              Make_Procedure_Call_Statement (Loc,
9195                                Name                   =>
9196                                  New_Occurrence_Of
9197                                    (RTE (RE_Release_Buffer), Loc),
9198                                Parameter_Associations =>
9199                                  New_List (New_Occurrence_Of (Strm, Loc))),
9200
9201                              Make_Simple_Return_Statement (Loc,
9202                                Expression => New_Occurrence_Of (Res, Loc))))));
9203                   end if;
9204                end;
9205             end if;
9206
9207             Decl :=
9208               Make_Subprogram_Body (Loc,
9209                 Specification => Spec,
9210                 Declarations => Decls,
9211                 Handled_Statement_Sequence =>
9212                   Make_Handled_Sequence_Of_Statements (Loc,
9213                     Statements => Stms));
9214          end Build_From_Any_Function;
9215
9216          ---------------------------------
9217          -- Build_Get_Aggregate_Element --
9218          ---------------------------------
9219
9220          function Build_Get_Aggregate_Element
9221            (Loc : Source_Ptr;
9222             Any : Entity_Id;
9223             TC  : Node_Id;
9224             Idx : Node_Id) return Node_Id
9225          is
9226          begin
9227             return Make_Function_Call (Loc,
9228               Name =>
9229                 New_Occurrence_Of (RTE (RE_Get_Aggregate_Element), Loc),
9230               Parameter_Associations => New_List (
9231                 New_Occurrence_Of (Any, Loc),
9232                 TC,
9233                 Idx));
9234          end Build_Get_Aggregate_Element;
9235
9236          -------------------------
9237          -- Build_Reposiroty_Id --
9238          -------------------------
9239
9240          procedure Build_Name_And_Repository_Id
9241            (E           : Entity_Id;
9242             Name_Str    : out String_Id;
9243             Repo_Id_Str : out String_Id)
9244          is
9245          begin
9246             Start_String;
9247             Store_String_Chars ("DSA:");
9248             Get_Library_Unit_Name_String (Scope (E));
9249             Store_String_Chars
9250               (Name_Buffer (Name_Buffer'First ..
9251                Name_Buffer'First + Name_Len - 1));
9252             Store_String_Char ('.');
9253             Get_Name_String (Chars (E));
9254             Store_String_Chars
9255               (Name_Buffer (Name_Buffer'First ..
9256                Name_Buffer'First + Name_Len - 1));
9257             Store_String_Chars (":1.0");
9258             Repo_Id_Str := End_String;
9259             Name_Str    := String_From_Name_Buffer;
9260          end Build_Name_And_Repository_Id;
9261
9262          -----------------------
9263          -- Build_To_Any_Call --
9264          -----------------------
9265
9266          function Build_To_Any_Call
9267            (N     : Node_Id;
9268             Decls : List_Id) return Node_Id
9269          is
9270             Loc : constant Source_Ptr := Sloc (N);
9271
9272             Typ     : Entity_Id := Etype (N);
9273             U_Type  : Entity_Id;
9274             Fnam    : Entity_Id := Empty;
9275             Lib_RE  : RE_Id := RE_Null;
9276
9277          begin
9278             --  If N is a selected component, then maybe its Etype has not been
9279             --  set yet: try to use Etype of the selector_name in that case.
9280
9281             if No (Typ) and then Nkind (N) = N_Selected_Component then
9282                Typ := Etype (Selector_Name (N));
9283             end if;
9284             pragma Assert (Present (Typ));
9285
9286             --  Get full view for private type, completion for incomplete type
9287
9288             U_Type := Underlying_Type (Typ);
9289
9290             --  First simple case where the To_Any function is present in the
9291             --  type's TSS.
9292
9293             Fnam := Find_Inherited_TSS (U_Type, TSS_To_Any);
9294
9295             --  Check first for Boolean and Character. These are enumeration
9296             --  types, but we treat them specially, since they may require
9297             --  special handling in the transfer protocol. However, this
9298             --  special handling only applies if they have standard
9299             --  representation, otherwise they are treated like any other
9300             --  enumeration type.
9301
9302             if Sloc (U_Type) <= Standard_Location then
9303                U_Type := Base_Type (U_Type);
9304             end if;
9305
9306             if Present (Fnam) then
9307                null;
9308
9309             elsif U_Type = Standard_Boolean then
9310                Lib_RE := RE_TA_B;
9311
9312             elsif U_Type = Standard_Character then
9313                Lib_RE := RE_TA_C;
9314
9315             elsif U_Type = Standard_Wide_Character then
9316                Lib_RE := RE_TA_WC;
9317
9318             elsif U_Type = Standard_Wide_Wide_Character then
9319                Lib_RE := RE_TA_WWC;
9320
9321             --  Floating point types
9322
9323             elsif U_Type = Standard_Short_Float then
9324                Lib_RE := RE_TA_SF;
9325
9326             elsif U_Type = Standard_Float then
9327                Lib_RE := RE_TA_F;
9328
9329             elsif U_Type = Standard_Long_Float then
9330                Lib_RE := RE_TA_LF;
9331
9332             elsif U_Type = Standard_Long_Long_Float then
9333                Lib_RE := RE_TA_LLF;
9334
9335             --  Integer types
9336
9337             elsif U_Type = Etype (Standard_Short_Short_Integer) then
9338                   Lib_RE := RE_TA_SSI;
9339
9340             elsif U_Type = Etype (Standard_Short_Integer) then
9341                Lib_RE := RE_TA_SI;
9342
9343             elsif U_Type = Etype (Standard_Integer) then
9344                Lib_RE := RE_TA_I;
9345
9346             elsif U_Type = Etype (Standard_Long_Integer) then
9347                Lib_RE := RE_TA_LI;
9348
9349             elsif U_Type = Etype (Standard_Long_Long_Integer) then
9350                Lib_RE := RE_TA_LLI;
9351
9352             --  Unsigned integer types
9353
9354             elsif U_Type = RTE (RE_Short_Short_Unsigned) then
9355                Lib_RE := RE_TA_SSU;
9356
9357             elsif U_Type = RTE (RE_Short_Unsigned) then
9358                Lib_RE := RE_TA_SU;
9359
9360             elsif U_Type = RTE (RE_Unsigned) then
9361                Lib_RE := RE_TA_U;
9362
9363             elsif U_Type = RTE (RE_Long_Unsigned) then
9364                Lib_RE := RE_TA_LU;
9365
9366             elsif U_Type = RTE (RE_Long_Long_Unsigned) then
9367                Lib_RE := RE_TA_LLU;
9368
9369             elsif U_Type = Standard_String then
9370                Lib_RE := RE_TA_String;
9371
9372             --  Special DSA types
9373
9374             elsif Is_RTE (U_Type, RE_Any_Container_Ptr) then
9375                Lib_RE := RE_TA_A;
9376                U_Type := Typ;
9377
9378             elsif U_Type = Underlying_Type (RTE (RE_TypeCode)) then
9379
9380                --  No corresponding FA_TC ???
9381
9382                Lib_RE := RE_TA_TC;
9383
9384             --  Other (non-primitive) types
9385
9386             else
9387                declare
9388                   Decl : Entity_Id;
9389                begin
9390                   Build_To_Any_Function (Loc, U_Type, Decl, Fnam);
9391                   Append_To (Decls, Decl);
9392                end;
9393             end if;
9394
9395             --  Call the function
9396
9397             if Lib_RE /= RE_Null then
9398                pragma Assert (No (Fnam));
9399                Fnam := RTE (Lib_RE);
9400             end if;
9401
9402             return
9403                 Make_Function_Call (Loc,
9404                   Name                   => New_Occurrence_Of (Fnam, Loc),
9405                   Parameter_Associations =>
9406                     New_List (Unchecked_Convert_To (U_Type, N)));
9407          end Build_To_Any_Call;
9408
9409          ---------------------------
9410          -- Build_To_Any_Function --
9411          ---------------------------
9412
9413          procedure Build_To_Any_Function
9414            (Loc  : Source_Ptr;
9415             Typ  : Entity_Id;
9416             Decl : out Node_Id;
9417             Fnam : out Entity_Id)
9418          is
9419             Spec  : Node_Id;
9420             Decls : constant List_Id := New_List;
9421             Stms  : constant List_Id := New_List;
9422
9423             Expr_Parameter : constant Entity_Id :=
9424                                Make_Defining_Identifier (Loc, Name_E);
9425
9426             Any : constant Entity_Id :=
9427                     Make_Defining_Identifier (Loc, Name_A);
9428
9429             Any_Decl  : Node_Id;
9430             Result_TC : Node_Id := Build_TypeCode_Call (Loc, Typ, Decls);
9431
9432             Use_Opaque_Representation : Boolean;
9433             --  When True, use stream attributes and represent type as an
9434             --  opaque sequence of bytes.
9435
9436          begin
9437             if Is_Itype (Typ) then
9438                Build_To_Any_Function
9439                   (Loc  => Loc,
9440                   Typ  => Etype (Typ),
9441                   Decl => Decl,
9442                   Fnam => Fnam);
9443                return;
9444             end if;
9445
9446             Fnam := Make_Helper_Function_Name (Loc, Typ, Name_To_Any);
9447
9448             Spec :=
9449               Make_Function_Specification (Loc,
9450                 Defining_Unit_Name => Fnam,
9451                 Parameter_Specifications => New_List (
9452                   Make_Parameter_Specification (Loc,
9453                     Defining_Identifier => Expr_Parameter,
9454                     Parameter_Type => New_Occurrence_Of (Typ, Loc))),
9455                 Result_Definition  => New_Occurrence_Of (RTE (RE_Any), Loc));
9456             Set_Etype (Expr_Parameter, Typ);
9457
9458             Any_Decl :=
9459               Make_Object_Declaration (Loc,
9460                 Defining_Identifier => Any,
9461                 Object_Definition   => New_Occurrence_Of (RTE (RE_Any), Loc));
9462
9463             Use_Opaque_Representation := False;
9464
9465             if Has_Stream_Attribute_Definition
9466                  (Typ, TSS_Stream_Output, At_Any_Place => True)
9467               or else
9468                Has_Stream_Attribute_Definition
9469                  (Typ, TSS_Stream_Write,  At_Any_Place => True)
9470             then
9471                --  If user-defined stream attributes are specified for this
9472                --  type, use them and transmit data as an opaque sequence of
9473                --  stream elements.
9474
9475                Use_Opaque_Representation := True;
9476
9477             elsif Is_Derived_Type (Typ) and then not Is_Tagged_Type (Typ) then
9478
9479                --  Non-tagged derived type: convert to root type
9480
9481                declare
9482                   Rt_Type : constant Entity_Id := Root_Type (Typ);
9483                   Expr    : constant Node_Id :=
9484                               OK_Convert_To
9485                                 (Rt_Type,
9486                                  New_Occurrence_Of (Expr_Parameter, Loc));
9487                begin
9488                   Set_Expression (Any_Decl, Build_To_Any_Call (Expr, Decls));
9489                end;
9490
9491             elsif Is_Record_Type (Typ) and then not Is_Tagged_Type (Typ) then
9492
9493                --  Non-tagged record type
9494
9495                if Nkind (Declaration_Node (Typ)) = N_Subtype_Declaration then
9496                   declare
9497                      Rt_Type : constant Entity_Id := Etype (Typ);
9498                      Expr    : constant Node_Id :=
9499                                  OK_Convert_To (Rt_Type,
9500                                    New_Occurrence_Of (Expr_Parameter, Loc));
9501
9502                   begin
9503                      Set_Expression
9504                        (Any_Decl, Build_To_Any_Call (Expr, Decls));
9505                   end;
9506
9507                --  Comment needed here (and label on declare block ???)
9508
9509                else
9510                   declare
9511                      Disc     : Entity_Id := Empty;
9512                      Rdef     : constant Node_Id :=
9513                                   Type_Definition (Declaration_Node (Typ));
9514                      Counter  : Int := 0;
9515                      Elements : constant List_Id := New_List;
9516
9517                      procedure TA_Rec_Add_Process_Element
9518                        (Stmts     : List_Id;
9519                         Container : Node_Or_Entity_Id;
9520                         Counter   : in out Int;
9521                         Rec       : Entity_Id;
9522                         Field     : Node_Id);
9523                      --  Processing routine for traversal below
9524
9525                      procedure TA_Append_Record_Traversal is
9526                         new Append_Record_Traversal
9527                           (Rec                 => Expr_Parameter,
9528                            Add_Process_Element => TA_Rec_Add_Process_Element);
9529
9530                      --------------------------------
9531                      -- TA_Rec_Add_Process_Element --
9532                      --------------------------------
9533
9534                      procedure TA_Rec_Add_Process_Element
9535                        (Stmts     : List_Id;
9536                         Container : Node_Or_Entity_Id;
9537                         Counter   : in out Int;
9538                         Rec       : Entity_Id;
9539                         Field     : Node_Id)
9540                      is
9541                         Field_Ref : Node_Id;
9542
9543                      begin
9544                         if Nkind (Field) = N_Defining_Identifier then
9545
9546                            --  A regular component
9547
9548                            Field_Ref := Make_Selected_Component (Loc,
9549                              Prefix        => New_Occurrence_Of (Rec, Loc),
9550                              Selector_Name => New_Occurrence_Of (Field, Loc));
9551                            Set_Etype (Field_Ref, Etype (Field));
9552
9553                            Append_To (Stmts,
9554                              Make_Procedure_Call_Statement (Loc,
9555                                Name =>
9556                                  New_Occurrence_Of (
9557                                    RTE (RE_Add_Aggregate_Element), Loc),
9558                                Parameter_Associations => New_List (
9559                                  New_Occurrence_Of (Container, Loc),
9560                                  Build_To_Any_Call (Field_Ref, Decls))));
9561
9562                         else
9563                            --  A variant part
9564
9565                            Variant_Part : declare
9566                               Variant        : Node_Id;
9567                               Struct_Counter : Int := 0;
9568
9569                               Block_Decls : constant List_Id := New_List;
9570                               Block_Stmts : constant List_Id := New_List;
9571                               VP_Stmts    : List_Id;
9572
9573                               Alt_List    : constant List_Id := New_List;
9574                               Choice_List : List_Id;
9575
9576                               Union_Any : constant Entity_Id :=
9577                                             Make_Defining_Identifier (Loc,
9578                                               New_Internal_Name ('V'));
9579
9580                               Struct_Any : constant Entity_Id :=
9581                                              Make_Defining_Identifier (Loc,
9582                                                 New_Internal_Name ('S'));
9583
9584                               function Make_Discriminant_Reference
9585                                 return Node_Id;
9586                               --  Build reference to the discriminant for this
9587                               --  variant part.
9588
9589                               ---------------------------------
9590                               -- Make_Discriminant_Reference --
9591                               ---------------------------------
9592
9593                               function Make_Discriminant_Reference
9594                                 return Node_Id
9595                               is
9596                                  Nod : constant Node_Id :=
9597                                          Make_Selected_Component (Loc,
9598                                            Prefix        => Rec,
9599                                            Selector_Name =>
9600                                              Chars (Name (Field)));
9601                               begin
9602                                  Set_Etype (Nod, Etype (Name (Field)));
9603                                  return Nod;
9604                               end Make_Discriminant_Reference;
9605
9606                            --  Start of processing for Variant_Part
9607
9608                            begin
9609                               Append_To (Stmts,
9610                                 Make_Block_Statement (Loc,
9611                                   Declarations =>
9612                                     Block_Decls,
9613                                   Handled_Statement_Sequence =>
9614                                     Make_Handled_Sequence_Of_Statements (Loc,
9615                                       Statements => Block_Stmts)));
9616
9617                               --  Declare variant part aggregate (Union_Any).
9618                               --  Knowing the position of this VP in the
9619                               --  variant record, we can fetch the VP typecode
9620                               --  from Container.
9621
9622                               Append_To (Block_Decls,
9623                                 Make_Object_Declaration (Loc,
9624                                   Defining_Identifier => Union_Any,
9625                                   Object_Definition   =>
9626                                     New_Occurrence_Of (RTE (RE_Any), Loc),
9627                                   Expression =>
9628                                     Make_Function_Call (Loc,
9629                                       Name => New_Occurrence_Of (
9630                                                 RTE (RE_Create_Any), Loc),
9631                                       Parameter_Associations => New_List (
9632                                         Make_Function_Call (Loc,
9633                                           Name =>
9634                                             New_Occurrence_Of (
9635                                               RTE (RE_Any_Member_Type), Loc),
9636                                           Parameter_Associations => New_List (
9637                                             New_Occurrence_Of (Container, Loc),
9638                                             Make_Integer_Literal (Loc,
9639                                               Counter)))))));
9640
9641                               --  Declare inner struct aggregate (which
9642                               --  contains the components of this VP).
9643
9644                               Append_To (Block_Decls,
9645                                 Make_Object_Declaration (Loc,
9646                                   Defining_Identifier => Struct_Any,
9647                                   Object_Definition   =>
9648                                     New_Occurrence_Of (RTE (RE_Any), Loc),
9649                                   Expression =>
9650                                     Make_Function_Call (Loc,
9651                                       Name => New_Occurrence_Of (
9652                                         RTE (RE_Create_Any), Loc),
9653                                       Parameter_Associations => New_List (
9654                                         Make_Function_Call (Loc,
9655                                           Name =>
9656                                             New_Occurrence_Of (
9657                                               RTE (RE_Any_Member_Type), Loc),
9658                                           Parameter_Associations => New_List (
9659                                             New_Occurrence_Of (Union_Any, Loc),
9660                                             Make_Integer_Literal (Loc,
9661                                               Uint_1)))))));
9662
9663                               --  Build case statement
9664
9665                               Append_To (Block_Stmts,
9666                                 Make_Case_Statement (Loc,
9667                                   Expression   => Make_Discriminant_Reference,
9668                                   Alternatives => Alt_List));
9669
9670                               Variant := First_Non_Pragma (Variants (Field));
9671                               while Present (Variant) loop
9672                                  Choice_List := New_Copy_List_Tree
9673                                    (Discrete_Choices (Variant));
9674
9675                                  VP_Stmts := New_List;
9676
9677                                  --  Append discriminant val to union aggregate
9678
9679                                  Append_To (VP_Stmts,
9680                                     Make_Procedure_Call_Statement (Loc,
9681                                       Name =>
9682                                         New_Occurrence_Of (
9683                                           RTE (RE_Add_Aggregate_Element), Loc),
9684                                       Parameter_Associations => New_List (
9685                                         New_Occurrence_Of (Union_Any, Loc),
9686                                           Build_To_Any_Call
9687                                             (Make_Discriminant_Reference,
9688                                              Block_Decls))));
9689
9690                                  --  Populate inner struct aggregate
9691
9692                                  --  Struct_Counter should be reset before
9693                                  --  handling a variant part. Indeed only one
9694                                  --  of the case statement alternatives will be
9695                                  --  executed at run-time, so the counter must
9696                                  --  start at 0 for every case statement.
9697
9698                                  Struct_Counter := 0;
9699
9700                                  TA_Append_Record_Traversal (
9701                                    Stmts     => VP_Stmts,
9702                                    Clist     => Component_List (Variant),
9703                                    Container => Struct_Any,
9704                                    Counter   => Struct_Counter);
9705
9706                                  --  Append inner struct to union aggregate
9707
9708                                  Append_To (VP_Stmts,
9709                                    Make_Procedure_Call_Statement (Loc,
9710                                      Name =>
9711                                        New_Occurrence_Of (
9712                                          RTE (RE_Add_Aggregate_Element), Loc),
9713                                      Parameter_Associations => New_List (
9714                                        New_Occurrence_Of (Union_Any, Loc),
9715                                        New_Occurrence_Of (Struct_Any, Loc))));
9716
9717                                  --  Append union to outer aggregate
9718
9719                                  Append_To (VP_Stmts,
9720                                    Make_Procedure_Call_Statement (Loc,
9721                                      Name =>
9722                                        New_Occurrence_Of (
9723                                          RTE (RE_Add_Aggregate_Element), Loc),
9724                                        Parameter_Associations => New_List (
9725                                           New_Occurrence_Of (Container, Loc),
9726                                           New_Occurrence_Of
9727                                             (Union_Any, Loc))));
9728
9729                                  Append_To (Alt_List,
9730                                    Make_Case_Statement_Alternative (Loc,
9731                                      Discrete_Choices => Choice_List,
9732                                      Statements       => VP_Stmts));
9733
9734                                  Next_Non_Pragma (Variant);
9735                               end loop;
9736                            end Variant_Part;
9737                         end if;
9738
9739                         Counter := Counter + 1;
9740                      end TA_Rec_Add_Process_Element;
9741
9742                   begin
9743                      --  Records are encoded in a TC_STRUCT aggregate:
9744
9745                      --  -- Outer aggregate (TC_STRUCT)
9746                      --  | [discriminant1]
9747                      --  | [discriminant2]
9748                      --  | ...
9749                      --  |
9750                      --  | [component1]
9751                      --  | [component2]
9752                      --  | ...
9753
9754                      --  A component can be a common component or variant part
9755
9756                      --  A variant part is encoded as a TC_UNION aggregate:
9757
9758                      --  -- Variant Part Aggregate (TC_UNION)
9759                      --  | [discriminant choice for this Variant Part]
9760                      --  |
9761                      --  | -- Inner struct (TC_STRUCT)
9762                      --  | |  [component1]
9763                      --  | |  [component2]
9764                      --  | |  ...
9765
9766                      --  Let's start by building the outer aggregate. First we
9767                      --  construct Elements array containing all discriminants.
9768
9769                      if Has_Discriminants (Typ) then
9770                         Disc := First_Discriminant (Typ);
9771                         while Present (Disc) loop
9772                            declare
9773                               Discriminant : constant Entity_Id :=
9774                                                Make_Selected_Component (Loc,
9775                                                  Prefix        =>
9776                                                    Expr_Parameter,
9777                                                  Selector_Name =>
9778                                                    Chars (Disc));
9779
9780                            begin
9781                               Set_Etype (Discriminant, Etype (Disc));
9782
9783                               Append_To (Elements,
9784                                 Make_Component_Association (Loc,
9785                                   Choices => New_List (
9786                                     Make_Integer_Literal (Loc, Counter)),
9787                                   Expression =>
9788                                     Build_To_Any_Call (Discriminant, Decls)));
9789                            end;
9790
9791                            Counter := Counter + 1;
9792                            Next_Discriminant (Disc);
9793                         end loop;
9794
9795                      else
9796                         --  If there are no discriminants, we declare an empty
9797                         --  Elements array.
9798
9799                         declare
9800                            Dummy_Any : constant Entity_Id :=
9801                                          Make_Defining_Identifier (Loc,
9802                                            Chars => New_Internal_Name ('A'));
9803
9804                         begin
9805                            Append_To (Decls,
9806                              Make_Object_Declaration (Loc,
9807                                Defining_Identifier => Dummy_Any,
9808                                Object_Definition   =>
9809                                  New_Occurrence_Of (RTE (RE_Any), Loc)));
9810
9811                            Append_To (Elements,
9812                              Make_Component_Association (Loc,
9813                                Choices => New_List (
9814                                  Make_Range (Loc,
9815                                    Low_Bound  =>
9816                                      Make_Integer_Literal (Loc, 1),
9817                                    High_Bound =>
9818                                      Make_Integer_Literal (Loc, 0))),
9819                                Expression =>
9820                                  New_Occurrence_Of (Dummy_Any, Loc)));
9821                         end;
9822                      end if;
9823
9824                      --  We build the result aggregate with discriminants
9825                      --  as the first elements.
9826
9827                      Set_Expression (Any_Decl,
9828                        Make_Function_Call (Loc,
9829                          Name => New_Occurrence_Of (
9830                                    RTE (RE_Any_Aggregate_Build), Loc),
9831                          Parameter_Associations => New_List (
9832                            Result_TC,
9833                            Make_Aggregate (Loc,
9834                              Component_Associations => Elements))));
9835                      Result_TC := Empty;
9836
9837                      --  Then we append all the components to the result
9838                      --  aggregate.
9839
9840                      TA_Append_Record_Traversal (Stms,
9841                        Clist     => Component_List (Rdef),
9842                        Container => Any,
9843                        Counter   => Counter);
9844                   end;
9845                end if;
9846
9847             elsif Is_Array_Type (Typ) then
9848
9849                --  Constrained and unconstrained array types
9850
9851                declare
9852                   Constrained : constant Boolean := Is_Constrained (Typ);
9853
9854                   procedure TA_Ary_Add_Process_Element
9855                     (Stmts   : List_Id;
9856                      Any     : Entity_Id;
9857                      Counter : Entity_Id;
9858                      Datum   : Node_Id);
9859
9860                   --------------------------------
9861                   -- TA_Ary_Add_Process_Element --
9862                   --------------------------------
9863
9864                   procedure TA_Ary_Add_Process_Element
9865                     (Stmts   : List_Id;
9866                      Any     : Entity_Id;
9867                      Counter : Entity_Id;
9868                      Datum   : Node_Id)
9869                   is
9870                      pragma Warnings (Off);
9871                      pragma Unreferenced (Counter);
9872                      pragma Warnings (On);
9873
9874                      Element_Any : Node_Id;
9875
9876                   begin
9877                      if Etype (Datum) = RTE (RE_Any) then
9878                         Element_Any := Datum;
9879                      else
9880                         Element_Any := Build_To_Any_Call (Datum, Decls);
9881                      end if;
9882
9883                      Append_To (Stmts,
9884                        Make_Procedure_Call_Statement (Loc,
9885                          Name => New_Occurrence_Of (
9886                                    RTE (RE_Add_Aggregate_Element), Loc),
9887                          Parameter_Associations => New_List (
9888                            New_Occurrence_Of (Any, Loc),
9889                            Element_Any)));
9890                   end TA_Ary_Add_Process_Element;
9891
9892                   procedure Append_To_Any_Array_Iterator is
9893                     new Append_Array_Traversal (
9894                       Subprogram => Fnam,
9895                       Arry       => Expr_Parameter,
9896                       Indices    => New_List,
9897                       Add_Process_Element => TA_Ary_Add_Process_Element);
9898
9899                   Index : Node_Id;
9900
9901                begin
9902                   Set_Expression (Any_Decl,
9903                     Make_Function_Call (Loc,
9904                       Name =>
9905                         New_Occurrence_Of (RTE (RE_Create_Any), Loc),
9906                       Parameter_Associations => New_List (Result_TC)));
9907                   Result_TC := Empty;
9908
9909                   if not Constrained then
9910                      Index := First_Index (Typ);
9911                      for J in 1 .. Number_Dimensions (Typ) loop
9912                         Append_To (Stms,
9913                           Make_Procedure_Call_Statement (Loc,
9914                             Name =>
9915                               New_Occurrence_Of (
9916                                 RTE (RE_Add_Aggregate_Element), Loc),
9917                             Parameter_Associations => New_List (
9918                               New_Occurrence_Of (Any, Loc),
9919                               Build_To_Any_Call (
9920                                 OK_Convert_To (Etype (Index),
9921                                   Make_Attribute_Reference (Loc,
9922                                     Prefix         =>
9923                                       New_Occurrence_Of (Expr_Parameter, Loc),
9924                                     Attribute_Name => Name_First,
9925                                     Expressions    => New_List (
9926                                       Make_Integer_Literal (Loc, J)))),
9927                                 Decls))));
9928                         Next_Index (Index);
9929                      end loop;
9930                   end if;
9931
9932                   Append_To_Any_Array_Iterator (Stms, Any);
9933                end;
9934
9935             elsif Is_Integer_Type (Typ) or else Is_Unsigned_Type (Typ) then
9936
9937                --  Integer types
9938
9939                Set_Expression (Any_Decl,
9940                  Build_To_Any_Call (
9941                    OK_Convert_To (
9942                      Find_Numeric_Representation (Typ),
9943                      New_Occurrence_Of (Expr_Parameter, Loc)),
9944                    Decls));
9945
9946             else
9947                --  Default case, including tagged types: opaque representation
9948
9949                Use_Opaque_Representation := True;
9950             end if;
9951
9952             if Use_Opaque_Representation then
9953                declare
9954                   Strm : constant Entity_Id :=
9955                            Make_Defining_Identifier (Loc,
9956                              Chars => New_Internal_Name ('S'));
9957                   --  Stream used to store data representation produced by
9958                   --  stream attribute.
9959
9960                begin
9961                   --  Generate:
9962                   --    Strm : aliased Buffer_Stream_Type;
9963
9964                   Append_To (Decls,
9965                     Make_Object_Declaration (Loc,
9966                       Defining_Identifier =>
9967                         Strm,
9968                       Aliased_Present     =>
9969                         True,
9970                       Object_Definition   =>
9971                         New_Occurrence_Of (RTE (RE_Buffer_Stream_Type), Loc)));
9972
9973                   --  Generate:
9974                   --    Allocate_Buffer (Strm);
9975
9976                   Append_To (Stms,
9977                     Make_Procedure_Call_Statement (Loc,
9978                       Name =>
9979                         New_Occurrence_Of (RTE (RE_Allocate_Buffer), Loc),
9980                       Parameter_Associations => New_List (
9981                         New_Occurrence_Of (Strm, Loc))));
9982
9983                   --  Generate:
9984                   --    T'Output (Strm'Access, E);
9985
9986                   Append_To (Stms,
9987                       Make_Attribute_Reference (Loc,
9988                         Prefix         => New_Occurrence_Of (Typ, Loc),
9989                         Attribute_Name => Name_Output,
9990                         Expressions    => New_List (
9991                           Make_Attribute_Reference (Loc,
9992                             Prefix         => New_Occurrence_Of (Strm, Loc),
9993                             Attribute_Name => Name_Access),
9994                           New_Occurrence_Of (Expr_Parameter, Loc))));
9995
9996                   --  Generate:
9997                   --    BS_To_Any (Strm, A);
9998
9999                   Append_To (Stms,
10000                     Make_Procedure_Call_Statement (Loc,
10001                       Name => New_Occurrence_Of (RTE (RE_BS_To_Any), Loc),
10002                       Parameter_Associations => New_List (
10003                         New_Occurrence_Of (Strm, Loc),
10004                         New_Occurrence_Of (Any, Loc))));
10005
10006                   --  Generate:
10007                   --    Release_Buffer (Strm);
10008
10009                   Append_To (Stms,
10010                     Make_Procedure_Call_Statement (Loc,
10011                       Name => New_Occurrence_Of (RTE (RE_Release_Buffer), Loc),
10012                       Parameter_Associations => New_List (
10013                         New_Occurrence_Of (Strm, Loc))));
10014                end;
10015             end if;
10016
10017             Append_To (Decls, Any_Decl);
10018
10019             if Present (Result_TC) then
10020                Append_To (Stms,
10021                  Make_Procedure_Call_Statement (Loc,
10022                    Name => New_Occurrence_Of (RTE (RE_Set_TC), Loc),
10023                    Parameter_Associations => New_List (
10024                      New_Occurrence_Of (Any, Loc),
10025                      Result_TC)));
10026             end if;
10027
10028             Append_To (Stms,
10029               Make_Simple_Return_Statement (Loc,
10030                 Expression => New_Occurrence_Of (Any, Loc)));
10031
10032             Decl :=
10033               Make_Subprogram_Body (Loc,
10034                 Specification              => Spec,
10035                 Declarations               => Decls,
10036                 Handled_Statement_Sequence =>
10037                   Make_Handled_Sequence_Of_Statements (Loc,
10038                     Statements => Stms));
10039          end Build_To_Any_Function;
10040
10041          -------------------------
10042          -- Build_TypeCode_Call --
10043          -------------------------
10044
10045          function Build_TypeCode_Call
10046            (Loc   : Source_Ptr;
10047             Typ   : Entity_Id;
10048             Decls : List_Id) return Node_Id
10049          is
10050             U_Type : Entity_Id := Underlying_Type (Typ);
10051             --  The full view, if Typ is private; the completion,
10052             --  if Typ is incomplete.
10053
10054             Fnam   : Entity_Id := Empty;
10055             Lib_RE : RE_Id := RE_Null;
10056             Expr   : Node_Id;
10057
10058          begin
10059             --  Special case System.PolyORB.Interface.Any: its primitives have
10060             --  not been set yet, so can't call Find_Inherited_TSS.
10061
10062             if Typ = RTE (RE_Any) then
10063                Fnam := RTE (RE_TC_A);
10064
10065             else
10066                --  First simple case where the TypeCode is present
10067                --  in the type's TSS.
10068
10069                Fnam := Find_Inherited_TSS (U_Type, TSS_TypeCode);
10070             end if;
10071
10072             if No (Fnam) then
10073                if Sloc (U_Type) <= Standard_Location then
10074
10075                   --  Do not try to build alias typecodes for subtypes from
10076                   --  Standard.
10077
10078                   U_Type := Base_Type (U_Type);
10079                end if;
10080
10081                if U_Type = Standard_Boolean then
10082                   Lib_RE := RE_TC_B;
10083
10084                elsif U_Type = Standard_Character then
10085                   Lib_RE := RE_TC_C;
10086
10087                elsif U_Type = Standard_Wide_Character then
10088                   Lib_RE := RE_TC_WC;
10089
10090                elsif U_Type = Standard_Wide_Wide_Character then
10091                   Lib_RE := RE_TC_WWC;
10092
10093                --  Floating point types
10094
10095                elsif U_Type = Standard_Short_Float then
10096                   Lib_RE := RE_TC_SF;
10097
10098                elsif U_Type = Standard_Float then
10099                   Lib_RE := RE_TC_F;
10100
10101                elsif U_Type = Standard_Long_Float then
10102                   Lib_RE := RE_TC_LF;
10103
10104                elsif U_Type = Standard_Long_Long_Float then
10105                   Lib_RE := RE_TC_LLF;
10106
10107                --  Integer types (walk back to the base type)
10108
10109                elsif U_Type = Etype (Standard_Short_Short_Integer) then
10110                      Lib_RE := RE_TC_SSI;
10111
10112                elsif U_Type = Etype (Standard_Short_Integer) then
10113                   Lib_RE := RE_TC_SI;
10114
10115                elsif U_Type = Etype (Standard_Integer) then
10116                   Lib_RE := RE_TC_I;
10117
10118                elsif U_Type = Etype (Standard_Long_Integer) then
10119                   Lib_RE := RE_TC_LI;
10120
10121                elsif U_Type = Etype (Standard_Long_Long_Integer) then
10122                   Lib_RE := RE_TC_LLI;
10123
10124                --  Unsigned integer types
10125
10126                elsif U_Type = RTE (RE_Short_Short_Unsigned) then
10127                   Lib_RE := RE_TC_SSU;
10128
10129                elsif U_Type = RTE (RE_Short_Unsigned) then
10130                   Lib_RE := RE_TC_SU;
10131
10132                elsif U_Type = RTE (RE_Unsigned) then
10133                   Lib_RE := RE_TC_U;
10134
10135                elsif U_Type = RTE (RE_Long_Unsigned) then
10136                   Lib_RE := RE_TC_LU;
10137
10138                elsif U_Type = RTE (RE_Long_Long_Unsigned) then
10139                   Lib_RE := RE_TC_LLU;
10140
10141                elsif U_Type = Standard_String then
10142                   Lib_RE := RE_TC_String;
10143
10144                --  Special DSA types
10145
10146                elsif Is_RTE (U_Type, RE_Any_Container_Ptr) then
10147                   Lib_RE := RE_TC_A;
10148
10149                --  Other (non-primitive) types
10150
10151                else
10152                   declare
10153                      Decl : Entity_Id;
10154                   begin
10155                      Build_TypeCode_Function (Loc, U_Type, Decl, Fnam);
10156                      Append_To (Decls, Decl);
10157                   end;
10158                end if;
10159
10160                if Lib_RE /= RE_Null then
10161                   Fnam := RTE (Lib_RE);
10162                end if;
10163             end if;
10164
10165             --  Call the function
10166
10167             Expr :=
10168               Make_Function_Call (Loc, Name => New_Occurrence_Of (Fnam, Loc));
10169
10170             --  Allow Expr to be used as arg to Build_To_Any_Call immediately
10171
10172             Set_Etype (Expr, RTE (RE_TypeCode));
10173
10174             return Expr;
10175          end Build_TypeCode_Call;
10176
10177          -----------------------------
10178          -- Build_TypeCode_Function --
10179          -----------------------------
10180
10181          procedure Build_TypeCode_Function
10182            (Loc  : Source_Ptr;
10183             Typ  : Entity_Id;
10184             Decl : out Node_Id;
10185             Fnam : out Entity_Id)
10186          is
10187             Spec  : Node_Id;
10188             Decls : constant List_Id := New_List;
10189             Stms  : constant List_Id := New_List;
10190
10191             TCNam : constant Entity_Id :=
10192                       Make_Helper_Function_Name (Loc, Typ, Name_TypeCode);
10193
10194             Parameters : List_Id;
10195
10196             procedure Add_String_Parameter
10197               (S              : String_Id;
10198                Parameter_List : List_Id);
10199             --  Add a literal for S to Parameters
10200
10201             procedure Add_TypeCode_Parameter
10202               (TC_Node        : Node_Id;
10203                Parameter_List : List_Id);
10204             --  Add the typecode for Typ to Parameters
10205
10206             procedure Add_Long_Parameter
10207               (Expr_Node      : Node_Id;
10208                Parameter_List : List_Id);
10209             --  Add a signed long integer expression to Parameters
10210
10211             procedure Initialize_Parameter_List
10212               (Name_String    : String_Id;
10213                Repo_Id_String : String_Id;
10214                Parameter_List : out List_Id);
10215             --  Return a list that contains the first two parameters
10216             --  for a parameterized typecode: name and repository id.
10217
10218             function Make_Constructed_TypeCode
10219               (Kind       : Entity_Id;
10220                Parameters : List_Id) return Node_Id;
10221             --  Call TC_Build with the given kind and parameters
10222
10223             procedure Return_Constructed_TypeCode (Kind : Entity_Id);
10224             --  Make a return statement that calls TC_Build with the given
10225             --  typecode kind, and the constructed parameters list.
10226
10227             procedure Return_Alias_TypeCode (Base_TypeCode  : Node_Id);
10228             --  Return a typecode that is a TC_Alias for the given typecode
10229
10230             --------------------------
10231             -- Add_String_Parameter --
10232             --------------------------
10233
10234             procedure Add_String_Parameter
10235               (S              : String_Id;
10236                Parameter_List : List_Id)
10237             is
10238             begin
10239                Append_To (Parameter_List,
10240                  Make_Function_Call (Loc,
10241                    Name => New_Occurrence_Of (RTE (RE_TA_String), Loc),
10242                    Parameter_Associations => New_List (
10243                      Make_String_Literal (Loc, S))));
10244             end Add_String_Parameter;
10245
10246             ----------------------------
10247             -- Add_TypeCode_Parameter --
10248             ----------------------------
10249
10250             procedure Add_TypeCode_Parameter
10251               (TC_Node        : Node_Id;
10252                Parameter_List : List_Id)
10253             is
10254             begin
10255                Append_To (Parameter_List,
10256                  Make_Function_Call (Loc,
10257                    Name => New_Occurrence_Of (RTE (RE_TA_TC), Loc),
10258                    Parameter_Associations => New_List (TC_Node)));
10259             end Add_TypeCode_Parameter;
10260
10261             ------------------------
10262             -- Add_Long_Parameter --
10263             ------------------------
10264
10265             procedure Add_Long_Parameter
10266               (Expr_Node      : Node_Id;
10267                Parameter_List : List_Id)
10268             is
10269             begin
10270                Append_To (Parameter_List,
10271                  Make_Function_Call (Loc,
10272                    Name => New_Occurrence_Of (RTE (RE_TA_LI), Loc),
10273                    Parameter_Associations => New_List (Expr_Node)));
10274             end Add_Long_Parameter;
10275
10276             -------------------------------
10277             -- Initialize_Parameter_List --
10278             -------------------------------
10279
10280             procedure Initialize_Parameter_List
10281               (Name_String    : String_Id;
10282                Repo_Id_String : String_Id;
10283                Parameter_List : out List_Id)
10284             is
10285             begin
10286                Parameter_List := New_List;
10287                Add_String_Parameter (Name_String, Parameter_List);
10288                Add_String_Parameter (Repo_Id_String, Parameter_List);
10289             end Initialize_Parameter_List;
10290
10291             ---------------------------
10292             -- Return_Alias_TypeCode --
10293             ---------------------------
10294
10295             procedure Return_Alias_TypeCode
10296               (Base_TypeCode  : Node_Id)
10297             is
10298             begin
10299                Add_TypeCode_Parameter (Base_TypeCode, Parameters);
10300                Return_Constructed_TypeCode (RTE (RE_TC_Alias));
10301             end Return_Alias_TypeCode;
10302
10303             -------------------------------
10304             -- Make_Constructed_TypeCode --
10305             -------------------------------
10306
10307             function Make_Constructed_TypeCode
10308               (Kind       : Entity_Id;
10309                Parameters : List_Id) return Node_Id
10310             is
10311                Constructed_TC : constant Node_Id :=
10312                  Make_Function_Call (Loc,
10313                    Name =>
10314                      New_Occurrence_Of (RTE (RE_TC_Build), Loc),
10315                    Parameter_Associations => New_List (
10316                      New_Occurrence_Of (Kind, Loc),
10317                      Make_Aggregate (Loc,
10318                         Expressions => Parameters)));
10319             begin
10320                Set_Etype (Constructed_TC, RTE (RE_TypeCode));
10321                return Constructed_TC;
10322             end Make_Constructed_TypeCode;
10323
10324             ---------------------------------
10325             -- Return_Constructed_TypeCode --
10326             ---------------------------------
10327
10328             procedure Return_Constructed_TypeCode (Kind : Entity_Id) is
10329             begin
10330                Append_To (Stms,
10331                  Make_Simple_Return_Statement (Loc,
10332                    Expression =>
10333                      Make_Constructed_TypeCode (Kind, Parameters)));
10334             end Return_Constructed_TypeCode;
10335
10336             ------------------
10337             -- Record types --
10338             ------------------
10339
10340             procedure TC_Rec_Add_Process_Element
10341               (Params  : List_Id;
10342                Any     : Entity_Id;
10343                Counter : in out Int;
10344                Rec     : Entity_Id;
10345                Field   : Node_Id);
10346
10347             procedure TC_Append_Record_Traversal is
10348               new Append_Record_Traversal (
10349                 Rec                 => Empty,
10350                 Add_Process_Element => TC_Rec_Add_Process_Element);
10351
10352             --------------------------------
10353             -- TC_Rec_Add_Process_Element --
10354             --------------------------------
10355
10356             procedure TC_Rec_Add_Process_Element
10357               (Params  : List_Id;
10358                Any     : Entity_Id;
10359                Counter : in out Int;
10360                Rec     : Entity_Id;
10361                Field   : Node_Id)
10362             is
10363                pragma Warnings (Off);
10364                pragma Unreferenced (Any, Counter, Rec);
10365                pragma Warnings (On);
10366
10367             begin
10368                if Nkind (Field) = N_Defining_Identifier then
10369
10370                   --  A regular component
10371
10372                   Add_TypeCode_Parameter
10373                     (Build_TypeCode_Call (Loc, Etype (Field), Decls), Params);
10374                   Get_Name_String (Chars (Field));
10375                   Add_String_Parameter (String_From_Name_Buffer, Params);
10376
10377                else
10378
10379                   --  A variant part
10380
10381                   declare
10382                      Discriminant_Type : constant Entity_Id :=
10383                                            Etype (Name (Field));
10384
10385                      Is_Enum : constant Boolean :=
10386                                  Is_Enumeration_Type (Discriminant_Type);
10387
10388                      Union_TC_Params : List_Id;
10389
10390                      U_Name : constant Name_Id :=
10391                                 New_External_Name (Chars (Typ), 'V', -1);
10392
10393                      Name_Str         : String_Id;
10394                      Struct_TC_Params : List_Id;
10395
10396                      Variant : Node_Id;
10397                      Choice  : Node_Id;
10398                      Default : constant Node_Id :=
10399                                  Make_Integer_Literal (Loc, -1);
10400
10401                      Dummy_Counter : Int := 0;
10402
10403                      Choice_Index : Int := 0;
10404
10405                      procedure Add_Params_For_Variant_Components;
10406                      --  Add a struct TypeCode and a corresponding member name
10407                      --  to the union parameter list.
10408
10409                      --  Ordering of declarations is a complete mess in this
10410                      --  area, it is supposed to be types/variables, then
10411                      --  subprogram specs, then subprogram bodies ???
10412
10413                      ---------------------------------------
10414                      -- Add_Params_For_Variant_Components --
10415                      ---------------------------------------
10416
10417                      procedure Add_Params_For_Variant_Components
10418                      is
10419                         S_Name : constant Name_Id :=
10420                                    New_External_Name (U_Name, 'S', -1);
10421
10422                      begin
10423                         Get_Name_String (S_Name);
10424                         Name_Str := String_From_Name_Buffer;
10425                         Initialize_Parameter_List
10426                           (Name_Str, Name_Str, Struct_TC_Params);
10427
10428                         --  Build struct parameters
10429
10430                         TC_Append_Record_Traversal (Struct_TC_Params,
10431                           Component_List (Variant),
10432                           Empty,
10433                           Dummy_Counter);
10434
10435                         Add_TypeCode_Parameter
10436                           (Make_Constructed_TypeCode
10437                            (RTE (RE_TC_Struct), Struct_TC_Params),
10438                            Union_TC_Params);
10439
10440                         Add_String_Parameter (Name_Str, Union_TC_Params);
10441                      end Add_Params_For_Variant_Components;
10442
10443                   begin
10444                      Get_Name_String (U_Name);
10445                      Name_Str := String_From_Name_Buffer;
10446
10447                      Initialize_Parameter_List
10448                        (Name_Str, Name_Str, Union_TC_Params);
10449
10450                      --  Add union in enclosing parameter list
10451
10452                      Add_TypeCode_Parameter
10453                        (Make_Constructed_TypeCode
10454                         (RTE (RE_TC_Union), Union_TC_Params),
10455                         Params);
10456
10457                      Add_String_Parameter (Name_Str, Params);
10458
10459                      --  Build union parameters
10460
10461                      Add_TypeCode_Parameter
10462                        (Build_TypeCode_Call
10463                           (Loc, Discriminant_Type, Decls),
10464                         Union_TC_Params);
10465
10466                      Add_Long_Parameter (Default, Union_TC_Params);
10467
10468                      Variant := First_Non_Pragma (Variants (Field));
10469                      while Present (Variant) loop
10470                         Choice := First (Discrete_Choices (Variant));
10471                         while Present (Choice) loop
10472                            case Nkind (Choice) is
10473                               when N_Range =>
10474                                  declare
10475                                     L : constant Uint :=
10476                                           Expr_Value (Low_Bound (Choice));
10477                                     H : constant Uint :=
10478                                           Expr_Value (High_Bound (Choice));
10479                                     J : Uint := L;
10480                                     --  3.8.1(8) guarantees that the bounds of
10481                                     --  this range are static.
10482
10483                                     Expr : Node_Id;
10484
10485                                  begin
10486                                     while J <= H loop
10487                                        if Is_Enum then
10488                                           Expr := New_Occurrence_Of (
10489                                             Get_Enum_Lit_From_Pos (
10490                                               Discriminant_Type, J, Loc), Loc);
10491                                        else
10492                                           Expr :=
10493                                             Make_Integer_Literal (Loc, J);
10494                                        end if;
10495                                        Append_To (Union_TC_Params,
10496                                          Build_To_Any_Call (Expr, Decls));
10497
10498                                        Add_Params_For_Variant_Components;
10499                                        J := J + Uint_1;
10500                                     end loop;
10501                                  end;
10502
10503                               when N_Others_Choice =>
10504
10505                                  --  This variant possess a default choice.
10506                                  --  We must therefore set the default
10507                                  --  parameter to the current choice index. The
10508                                  --  default parameter is by construction the
10509                                  --  fourth in the Union_TC_Params list.
10510
10511                                  declare
10512                                     Default_Node : constant Node_Id :=
10513                                                      Pick (Union_TC_Params, 4);
10514
10515                                     New_Default_Node : constant Node_Id :=
10516                                       Make_Function_Call (Loc,
10517                                        Name =>
10518                                          New_Occurrence_Of
10519                                            (RTE (RE_TA_LI), Loc),
10520                                        Parameter_Associations =>
10521                                          New_List (
10522                                            Make_Integer_Literal
10523                                              (Loc, Choice_Index)));
10524                                  begin
10525                                     Insert_Before (
10526                                       Default_Node,
10527                                       New_Default_Node);
10528
10529                                     Remove (Default_Node);
10530                                  end;
10531
10532                                  --  Add a placeholder member label
10533                                  --  for the default case.
10534                                  --  It must be of the discriminant type.
10535
10536                                  declare
10537                                     Exp : constant Node_Id :=
10538                                       Make_Attribute_Reference (Loc,
10539                                        Prefix => New_Occurrence_Of
10540                                          (Discriminant_Type, Loc),
10541                                        Attribute_Name => Name_First);
10542                                  begin
10543                                     Set_Etype (Exp, Discriminant_Type);
10544                                     Append_To (Union_TC_Params,
10545                                       Build_To_Any_Call (Exp, Decls));
10546                                  end;
10547
10548                                  Add_Params_For_Variant_Components;
10549
10550                               when others =>
10551
10552                                  --  Case of an explicit choice
10553
10554                                  declare
10555                                     Exp : constant Node_Id :=
10556                                             New_Copy_Tree (Choice);
10557                                  begin
10558                                     Append_To (Union_TC_Params,
10559                                       Build_To_Any_Call (Exp, Decls));
10560                                  end;
10561
10562                                  Add_Params_For_Variant_Components;
10563                            end case;
10564
10565                            Next (Choice);
10566                            Choice_Index := Choice_Index + 1;
10567                         end loop;
10568
10569                         Next_Non_Pragma (Variant);
10570                      end loop;
10571                   end;
10572                end if;
10573             end TC_Rec_Add_Process_Element;
10574
10575             Type_Name_Str    : String_Id;
10576             Type_Repo_Id_Str : String_Id;
10577
10578          begin
10579             if Is_Itype (Typ) then
10580                Build_TypeCode_Function
10581                   (Loc  => Loc,
10582                   Typ  => Etype (Typ),
10583                   Decl => Decl,
10584                   Fnam => Fnam);
10585                return;
10586             end if;
10587
10588             Fnam := TCNam;
10589
10590             Spec :=
10591               Make_Function_Specification (Loc,
10592                 Defining_Unit_Name       => Fnam,
10593                 Parameter_Specifications => Empty_List,
10594                 Result_Definition        =>
10595                   New_Occurrence_Of (RTE (RE_TypeCode), Loc));
10596
10597             Build_Name_And_Repository_Id (Typ,
10598               Name_Str => Type_Name_Str, Repo_Id_Str => Type_Repo_Id_Str);
10599
10600             Initialize_Parameter_List
10601               (Type_Name_Str, Type_Repo_Id_Str, Parameters);
10602
10603             if Has_Stream_Attribute_Definition
10604                  (Typ, TSS_Stream_Output, At_Any_Place => True)
10605               or else
10606                Has_Stream_Attribute_Definition
10607                  (Typ, TSS_Stream_Write, At_Any_Place => True)
10608             then
10609                --  If user-defined stream attributes are specified for this
10610                --  type, use them and transmit data as an opaque sequence of
10611                --  stream elements.
10612
10613                Return_Alias_TypeCode
10614                  (New_Occurrence_Of (RTE (RE_TC_Opaque), Loc));
10615
10616             elsif Is_Derived_Type (Typ) and then not Is_Tagged_Type (Typ) then
10617                Return_Alias_TypeCode (
10618                  Build_TypeCode_Call (Loc, Etype (Typ), Decls));
10619
10620             elsif Is_Integer_Type (Typ) or else Is_Unsigned_Type (Typ) then
10621                Return_Alias_TypeCode (
10622                  Build_TypeCode_Call (Loc,
10623                    Find_Numeric_Representation (Typ), Decls));
10624
10625             elsif Is_Record_Type (Typ) and then not Is_Tagged_Type (Typ) then
10626
10627                --  Record typecodes are encoded as follows:
10628                --  -- TC_STRUCT
10629                --  |
10630                --  |  [Name]
10631                --  |  [Repository Id]
10632                --
10633                --  Then for each discriminant:
10634                --
10635                --  |  [Discriminant Type Code]
10636                --  |  [Discriminant Name]
10637                --  |  ...
10638                --
10639                --  Then for each component:
10640                --
10641                --  |  [Component Type Code]
10642                --  |  [Component Name]
10643                --  |  ...
10644                --
10645                --  Variants components type codes are encoded as follows:
10646                --  --  TC_UNION
10647                --  |
10648                --  |  [Name]
10649                --  |  [Repository Id]
10650                --  |  [Discriminant Type Code]
10651                --  |  [Index of Default Variant Part or -1 for no default]
10652                --
10653                --  Then for each Variant Part :
10654                --
10655                --  |  [VP Label]
10656                --  |
10657                --  |  -- TC_STRUCT
10658                --  |  | [Variant Part Name]
10659                --  |  | [Variant Part Repository Id]
10660                --  |  |
10661                --  |    Then for each VP component:
10662                --  |  | [VP component Typecode]
10663                --  |  | [VP component Name]
10664                --  |  | ...
10665                --  |  --
10666                --  |
10667                --  |  [VP Name]
10668
10669                if Nkind (Declaration_Node (Typ)) = N_Subtype_Declaration then
10670                   Return_Alias_TypeCode
10671                     (Build_TypeCode_Call (Loc, Etype (Typ), Decls));
10672
10673                else
10674                   declare
10675                      Disc : Entity_Id := Empty;
10676                      Rdef : constant Node_Id :=
10677                               Type_Definition (Declaration_Node (Typ));
10678                      Dummy_Counter : Int := 0;
10679
10680                   begin
10681                      --  Construct the discriminants typecodes
10682
10683                      if Has_Discriminants (Typ) then
10684                         Disc := First_Discriminant (Typ);
10685                      end if;
10686
10687                      while Present (Disc) loop
10688                         Add_TypeCode_Parameter (
10689                           Build_TypeCode_Call (Loc, Etype (Disc), Decls),
10690                           Parameters);
10691                         Get_Name_String (Chars (Disc));
10692                         Add_String_Parameter (
10693                           String_From_Name_Buffer,
10694                           Parameters);
10695                         Next_Discriminant (Disc);
10696                      end loop;
10697
10698                      --  then the components typecodes
10699
10700                      TC_Append_Record_Traversal
10701                        (Parameters, Component_List (Rdef),
10702                         Empty, Dummy_Counter);
10703                      Return_Constructed_TypeCode (RTE (RE_TC_Struct));
10704                   end;
10705                end if;
10706
10707             elsif Is_Array_Type (Typ) then
10708                declare
10709                   Ndim           : constant Pos := Number_Dimensions (Typ);
10710                   Inner_TypeCode : Node_Id;
10711                   Constrained    : constant Boolean := Is_Constrained (Typ);
10712                   Indx           : Node_Id          := First_Index (Typ);
10713
10714                begin
10715                   Inner_TypeCode :=
10716                     Build_TypeCode_Call (Loc, Component_Type (Typ), Decls);
10717
10718                   for J in 1 .. Ndim loop
10719                      if Constrained then
10720                         Inner_TypeCode := Make_Constructed_TypeCode
10721                           (RTE (RE_TC_Array), New_List (
10722                             Build_To_Any_Call (
10723                               OK_Convert_To (RTE (RE_Long_Unsigned),
10724                                 Make_Attribute_Reference (Loc,
10725                                   Prefix => New_Occurrence_Of (Typ, Loc),
10726                                   Attribute_Name => Name_Length,
10727                                   Expressions => New_List (
10728                                     Make_Integer_Literal (Loc,
10729                                       Intval => Ndim - J + 1)))),
10730                               Decls),
10731                             Build_To_Any_Call (Inner_TypeCode, Decls)));
10732
10733                      else
10734                         --  Unconstrained case: add low bound for each
10735                         --  dimension.
10736
10737                         Add_TypeCode_Parameter
10738                           (Build_TypeCode_Call (Loc, Etype (Indx), Decls),
10739                            Parameters);
10740                         Get_Name_String (New_External_Name ('L', J));
10741                         Add_String_Parameter (
10742                           String_From_Name_Buffer,
10743                           Parameters);
10744                         Next_Index (Indx);
10745
10746                         Inner_TypeCode := Make_Constructed_TypeCode
10747                           (RTE (RE_TC_Sequence), New_List (
10748                             Build_To_Any_Call (
10749                               OK_Convert_To (RTE (RE_Long_Unsigned),
10750                                 Make_Integer_Literal (Loc, 0)),
10751                               Decls),
10752                             Build_To_Any_Call (Inner_TypeCode, Decls)));
10753                      end if;
10754                   end loop;
10755
10756                   if Constrained then
10757                      Return_Alias_TypeCode (Inner_TypeCode);
10758                   else
10759                      Add_TypeCode_Parameter (Inner_TypeCode, Parameters);
10760                      Start_String;
10761                      Store_String_Char ('V');
10762                      Add_String_Parameter (End_String, Parameters);
10763                      Return_Constructed_TypeCode (RTE (RE_TC_Struct));
10764                   end if;
10765                end;
10766
10767             else
10768                --  Default: type is represented as an opaque sequence of bytes
10769
10770                Return_Alias_TypeCode
10771                  (New_Occurrence_Of (RTE (RE_TC_Opaque), Loc));
10772             end if;
10773
10774             Decl :=
10775               Make_Subprogram_Body (Loc,
10776                 Specification              => Spec,
10777                 Declarations               => Decls,
10778                 Handled_Statement_Sequence =>
10779                   Make_Handled_Sequence_Of_Statements (Loc,
10780                     Statements => Stms));
10781          end Build_TypeCode_Function;
10782
10783          ---------------------------------
10784          -- Find_Numeric_Representation --
10785          ---------------------------------
10786
10787          function Find_Numeric_Representation
10788            (Typ : Entity_Id) return Entity_Id
10789          is
10790             FST    : constant Entity_Id := First_Subtype (Typ);
10791             P_Size : constant Uint      := Esize (FST);
10792
10793          begin
10794             if Is_Unsigned_Type (Typ) then
10795                if P_Size <= Standard_Short_Short_Integer_Size then
10796                   return RTE (RE_Short_Short_Unsigned);
10797
10798                elsif P_Size <= Standard_Short_Integer_Size then
10799                   return RTE (RE_Short_Unsigned);
10800
10801                elsif P_Size <= Standard_Integer_Size then
10802                   return RTE (RE_Unsigned);
10803
10804                elsif P_Size <= Standard_Long_Integer_Size then
10805                   return RTE (RE_Long_Unsigned);
10806
10807                else
10808                   return RTE (RE_Long_Long_Unsigned);
10809                end if;
10810
10811             elsif Is_Integer_Type (Typ) then
10812                if P_Size <= Standard_Short_Short_Integer_Size then
10813                   return Standard_Short_Short_Integer;
10814
10815                elsif P_Size <= Standard_Short_Integer_Size then
10816                   return Standard_Short_Integer;
10817
10818                elsif P_Size <= Standard_Integer_Size then
10819                   return Standard_Integer;
10820
10821                elsif P_Size <= Standard_Long_Integer_Size then
10822                   return Standard_Long_Integer;
10823
10824                else
10825                   return Standard_Long_Long_Integer;
10826                end if;
10827
10828             elsif Is_Floating_Point_Type (Typ) then
10829                if P_Size <= Standard_Short_Float_Size then
10830                   return Standard_Short_Float;
10831
10832                elsif P_Size <= Standard_Float_Size then
10833                   return Standard_Float;
10834
10835                elsif P_Size <= Standard_Long_Float_Size then
10836                   return Standard_Long_Float;
10837
10838                else
10839                   return Standard_Long_Long_Float;
10840                end if;
10841
10842             else
10843                raise Program_Error;
10844             end if;
10845
10846             --  TBD: fixed point types???
10847             --  TBverified numeric types with a biased representation???
10848
10849          end Find_Numeric_Representation;
10850
10851          ---------------------------
10852          -- Append_Array_Traversal --
10853          ---------------------------
10854
10855          procedure Append_Array_Traversal
10856            (Stmts   : List_Id;
10857             Any     : Entity_Id;
10858             Counter : Entity_Id := Empty;
10859             Depth   : Pos       := 1)
10860          is
10861             Loc         : constant Source_Ptr := Sloc (Subprogram);
10862             Typ         : constant Entity_Id  := Etype (Arry);
10863             Constrained : constant Boolean    := Is_Constrained (Typ);
10864             Ndim        : constant Pos        := Number_Dimensions (Typ);
10865
10866             Inner_Any, Inner_Counter : Entity_Id;
10867
10868             Loop_Stm    : Node_Id;
10869             Inner_Stmts : constant List_Id := New_List;
10870
10871          begin
10872             if Depth > Ndim then
10873
10874                --  Processing for one element of an array
10875
10876                declare
10877                   Element_Expr : constant Node_Id :=
10878                                    Make_Indexed_Component (Loc,
10879                                      New_Occurrence_Of (Arry, Loc),
10880                                      Indices);
10881                begin
10882                   Set_Etype (Element_Expr, Component_Type (Typ));
10883                   Add_Process_Element (Stmts,
10884                     Any     => Any,
10885                     Counter => Counter,
10886                     Datum   => Element_Expr);
10887                end;
10888
10889                return;
10890             end if;
10891
10892             Append_To (Indices,
10893               Make_Identifier (Loc, New_External_Name ('L', Depth)));
10894
10895             if not Constrained or else Depth > 1 then
10896                Inner_Any := Make_Defining_Identifier (Loc,
10897                               New_External_Name ('A', Depth));
10898                Set_Etype (Inner_Any, RTE (RE_Any));
10899             else
10900                Inner_Any := Empty;
10901             end if;
10902
10903             if Present (Counter) then
10904                Inner_Counter := Make_Defining_Identifier (Loc,
10905                                   New_External_Name ('J', Depth));
10906             else
10907                Inner_Counter := Empty;
10908             end if;
10909
10910             declare
10911                Loop_Any : Node_Id := Inner_Any;
10912
10913             begin
10914                --  For the first dimension of a constrained array, we add
10915                --  elements directly in the corresponding Any; there is no
10916                --  intervening inner Any.
10917
10918                if No (Loop_Any) then
10919                   Loop_Any := Any;
10920                end if;
10921
10922                Append_Array_Traversal (Inner_Stmts,
10923                  Any     => Loop_Any,
10924                  Counter => Inner_Counter,
10925                  Depth   => Depth + 1);
10926             end;
10927
10928             Loop_Stm :=
10929               Make_Implicit_Loop_Statement (Subprogram,
10930                 Iteration_Scheme =>
10931                   Make_Iteration_Scheme (Loc,
10932                     Loop_Parameter_Specification =>
10933                       Make_Loop_Parameter_Specification (Loc,
10934                         Defining_Identifier =>
10935                           Make_Defining_Identifier (Loc,
10936                             Chars => New_External_Name ('L', Depth)),
10937
10938                         Discrete_Subtype_Definition =>
10939                           Make_Attribute_Reference (Loc,
10940                             Prefix         => New_Occurrence_Of (Arry, Loc),
10941                             Attribute_Name => Name_Range,
10942
10943                             Expressions => New_List (
10944                               Make_Integer_Literal (Loc, Depth))))),
10945                 Statements => Inner_Stmts);
10946
10947             declare
10948                Decls       : constant List_Id := New_List;
10949                Dimen_Stmts : constant List_Id := New_List;
10950                Length_Node : Node_Id;
10951
10952                Inner_Any_TypeCode : constant Entity_Id :=
10953                                       Make_Defining_Identifier (Loc,
10954                                         New_External_Name ('T', Depth));
10955
10956                Inner_Any_TypeCode_Expr : Node_Id;
10957
10958             begin
10959                if Depth = 1 then
10960                   if Constrained then
10961                      Inner_Any_TypeCode_Expr :=
10962                        Make_Function_Call (Loc,
10963                          Name => New_Occurrence_Of (RTE (RE_Get_TC), Loc),
10964                          Parameter_Associations => New_List (
10965                            New_Occurrence_Of (Any, Loc)));
10966                   else
10967                      Inner_Any_TypeCode_Expr :=
10968                        Make_Function_Call (Loc,
10969                          Name =>
10970                            New_Occurrence_Of (RTE (RE_Any_Member_Type), Loc),
10971                              Parameter_Associations => New_List (
10972                                New_Occurrence_Of (Any, Loc),
10973                                Make_Integer_Literal (Loc, Ndim)));
10974                   end if;
10975                else
10976                   Inner_Any_TypeCode_Expr :=
10977                     Make_Function_Call (Loc,
10978                       Name => New_Occurrence_Of (RTE (RE_Content_Type), Loc),
10979                       Parameter_Associations => New_List (
10980                         Make_Identifier (Loc,
10981                           Chars => New_External_Name ('T', Depth - 1))));
10982                end if;
10983
10984                Append_To (Decls,
10985                  Make_Object_Declaration (Loc,
10986                    Defining_Identifier => Inner_Any_TypeCode,
10987                    Constant_Present    => True,
10988                    Object_Definition   => New_Occurrence_Of (
10989                                             RTE (RE_TypeCode), Loc),
10990                    Expression          => Inner_Any_TypeCode_Expr));
10991
10992                if Present (Inner_Any) then
10993                   Append_To (Decls,
10994                     Make_Object_Declaration (Loc,
10995                       Defining_Identifier => Inner_Any,
10996                       Object_Definition   =>
10997                         New_Occurrence_Of (RTE (RE_Any), Loc),
10998                       Expression          =>
10999                         Make_Function_Call (Loc,
11000                           Name =>
11001                             New_Occurrence_Of (
11002                               RTE (RE_Create_Any), Loc),
11003                           Parameter_Associations => New_List (
11004                             New_Occurrence_Of (Inner_Any_TypeCode, Loc)))));
11005                end if;
11006
11007                if Present (Inner_Counter) then
11008                   Append_To (Decls,
11009                     Make_Object_Declaration (Loc,
11010                       Defining_Identifier => Inner_Counter,
11011                       Object_Definition   =>
11012                         New_Occurrence_Of (RTE (RE_Long_Unsigned), Loc),
11013                       Expression          =>
11014                         Make_Integer_Literal (Loc, 0)));
11015                end if;
11016
11017                if not Constrained then
11018                   Length_Node := Make_Attribute_Reference (Loc,
11019                         Prefix         => New_Occurrence_Of (Arry, Loc),
11020                         Attribute_Name => Name_Length,
11021                         Expressions    =>
11022                           New_List (Make_Integer_Literal (Loc, Depth)));
11023                   Set_Etype (Length_Node, RTE (RE_Long_Unsigned));
11024
11025                   Add_Process_Element (Dimen_Stmts,
11026                     Datum   => Length_Node,
11027                     Any     => Inner_Any,
11028                     Counter => Inner_Counter);
11029                end if;
11030
11031                --  Loop_Stm does appropriate processing for each element
11032                --  of Inner_Any.
11033
11034                Append_To (Dimen_Stmts, Loop_Stm);
11035
11036                --  Link outer and inner any
11037
11038                if Present (Inner_Any) then
11039                   Add_Process_Element (Dimen_Stmts,
11040                     Any     => Any,
11041                     Counter => Counter,
11042                     Datum   => New_Occurrence_Of (Inner_Any, Loc));
11043                end if;
11044
11045                Append_To (Stmts,
11046                  Make_Block_Statement (Loc,
11047                    Declarations =>
11048                      Decls,
11049                    Handled_Statement_Sequence =>
11050                      Make_Handled_Sequence_Of_Statements (Loc,
11051                        Statements => Dimen_Stmts)));
11052             end;
11053          end Append_Array_Traversal;
11054
11055          -------------------------------
11056          -- Make_Helper_Function_Name --
11057          -------------------------------
11058
11059          function Make_Helper_Function_Name
11060            (Loc : Source_Ptr;
11061             Typ : Entity_Id;
11062             Nam : Name_Id) return Entity_Id
11063          is
11064          begin
11065             declare
11066                Serial : Nat := 0;
11067                --  For tagged types, we use a canonical name so that it matches
11068                --  the primitive spec. For all other cases, we use a serialized
11069                --  name so that multiple generations of the same procedure do
11070                --  not clash.
11071
11072             begin
11073                if not Is_Tagged_Type (Typ) then
11074                   Serial := Increment_Serial_Number;
11075                end if;
11076
11077                --  Use prefixed underscore to avoid potential clash with used
11078                --  identifier (we use attribute names for Nam).
11079
11080                return
11081                  Make_Defining_Identifier (Loc,
11082                    Chars =>
11083                      New_External_Name
11084                        (Related_Id => Nam,
11085                         Suffix => ' ', Suffix_Index => Serial,
11086                         Prefix => '_'));
11087             end;
11088          end Make_Helper_Function_Name;
11089       end Helpers;
11090
11091       -----------------------------------
11092       -- Reserve_NamingContext_Methods --
11093       -----------------------------------
11094
11095       procedure Reserve_NamingContext_Methods is
11096          Str_Resolve : constant String := "resolve";
11097       begin
11098          Name_Buffer (1 .. Str_Resolve'Length) := Str_Resolve;
11099          Name_Len := Str_Resolve'Length;
11100          Overload_Counter_Table.Set (Name_Find, 1);
11101       end Reserve_NamingContext_Methods;
11102
11103    end PolyORB_Support;
11104
11105    -------------------------------
11106    -- RACW_Type_Is_Asynchronous --
11107    -------------------------------
11108
11109    procedure RACW_Type_Is_Asynchronous (RACW_Type : Entity_Id) is
11110       Asynchronous_Flag : constant Entity_Id :=
11111                             Asynchronous_Flags_Table.Get (RACW_Type);
11112    begin
11113       Replace (Expression (Parent (Asynchronous_Flag)),
11114         New_Occurrence_Of (Standard_True, Sloc (Asynchronous_Flag)));
11115    end RACW_Type_Is_Asynchronous;
11116
11117    -------------------------
11118    -- RCI_Package_Locator --
11119    -------------------------
11120
11121    function RCI_Package_Locator
11122      (Loc          : Source_Ptr;
11123       Package_Spec : Node_Id) return Node_Id
11124    is
11125       Inst     : Node_Id;
11126       Pkg_Name : String_Id;
11127
11128    begin
11129       Get_Library_Unit_Name_String (Package_Spec);
11130       Pkg_Name := String_From_Name_Buffer;
11131       Inst :=
11132         Make_Package_Instantiation (Loc,
11133           Defining_Unit_Name   =>
11134             Make_Defining_Identifier (Loc, New_Internal_Name ('R')),
11135           Name                 =>
11136             New_Occurrence_Of (RTE (RE_RCI_Locator), Loc),
11137           Generic_Associations => New_List (
11138             Make_Generic_Association (Loc,
11139               Selector_Name                     =>
11140                 Make_Identifier (Loc, Name_RCI_Name),
11141               Explicit_Generic_Actual_Parameter =>
11142                 Make_String_Literal (Loc,
11143                   Strval => Pkg_Name)),
11144             Make_Generic_Association (Loc,
11145               Selector_Name                     =>
11146                 Make_Identifier (Loc, Name_Version),
11147               Explicit_Generic_Actual_Parameter =>
11148                 Make_Attribute_Reference (Loc,
11149                   Prefix         =>
11150                     New_Occurrence_Of (Defining_Entity (Package_Spec), Loc),
11151                   Attribute_Name =>
11152                     Name_Version))));
11153
11154       RCI_Locator_Table.Set (Defining_Unit_Name (Package_Spec),
11155         Defining_Unit_Name (Inst));
11156       return Inst;
11157    end RCI_Package_Locator;
11158
11159    -----------------------------------------------
11160    -- Remote_Types_Tagged_Full_View_Encountered --
11161    -----------------------------------------------
11162
11163    procedure Remote_Types_Tagged_Full_View_Encountered
11164      (Full_View : Entity_Id)
11165    is
11166       Stub_Elements : constant Stub_Structure :=
11167                         Stubs_Table.Get (Full_View);
11168
11169    begin
11170       --  For an RACW encountered before the freeze point of its designated
11171       --  type, the stub type is generated at the point of the RACW declaration
11172       --  but the primitives are generated only once the designated type is
11173       --  frozen. That freeze can occur in another scope, for example when the
11174       --  RACW is declared in a nested package. In that case we need to
11175       --  reestablish the stub type's scope prior to generating its primitive
11176       --  operations.
11177
11178       if Stub_Elements /= Empty_Stub_Structure then
11179          declare
11180             Saved_Scope : constant Entity_Id := Current_Scope;
11181             Stubs_Scope : constant Entity_Id :=
11182                             Scope (Stub_Elements.Stub_Type);
11183
11184          begin
11185             if Current_Scope /= Stubs_Scope then
11186                Push_Scope (Stubs_Scope);
11187             end if;
11188
11189             Add_RACW_Primitive_Declarations_And_Bodies
11190               (Full_View,
11191                Stub_Elements.RPC_Receiver_Decl,
11192                Stub_Elements.Body_Decls);
11193
11194             if Current_Scope /= Saved_Scope then
11195                Pop_Scope;
11196             end if;
11197          end;
11198       end if;
11199    end Remote_Types_Tagged_Full_View_Encountered;
11200
11201    -------------------
11202    -- Scope_Of_Spec --
11203    -------------------
11204
11205    function Scope_Of_Spec (Spec : Node_Id) return Entity_Id is
11206       Unit_Name : Node_Id;
11207
11208    begin
11209       Unit_Name := Defining_Unit_Name (Spec);
11210       while Nkind (Unit_Name) /= N_Defining_Identifier loop
11211          Unit_Name := Defining_Identifier (Unit_Name);
11212       end loop;
11213
11214       return Unit_Name;
11215    end Scope_Of_Spec;
11216
11217    ----------------------
11218    -- Set_Renaming_TSS --
11219    ----------------------
11220
11221    procedure Set_Renaming_TSS
11222      (Typ     : Entity_Id;
11223       Nam     : Entity_Id;
11224       TSS_Nam : TSS_Name_Type)
11225    is
11226       Loc  : constant Source_Ptr := Sloc (Nam);
11227       Spec : constant Node_Id := Parent (Nam);
11228
11229       TSS_Node : constant Node_Id :=
11230                    Make_Subprogram_Renaming_Declaration (Loc,
11231                      Specification =>
11232                        Copy_Specification (Loc,
11233                          Spec     => Spec,
11234                          New_Name => Make_TSS_Name (Typ, TSS_Nam)),
11235                        Name => New_Occurrence_Of (Nam, Loc));
11236
11237       Snam : constant Entity_Id :=
11238                Defining_Unit_Name (Specification (TSS_Node));
11239
11240    begin
11241       if Nkind (Spec) = N_Function_Specification then
11242          Set_Ekind (Snam, E_Function);
11243          Set_Etype (Snam, Entity (Result_Definition (Spec)));
11244       else
11245          Set_Ekind (Snam, E_Procedure);
11246          Set_Etype (Snam, Standard_Void_Type);
11247       end if;
11248
11249       Set_TSS (Typ, Snam);
11250    end Set_Renaming_TSS;
11251
11252    ----------------------------------------------
11253    -- Specific_Add_Obj_RPC_Receiver_Completion --
11254    ----------------------------------------------
11255
11256    procedure Specific_Add_Obj_RPC_Receiver_Completion
11257      (Loc           : Source_Ptr;
11258       Decls         : List_Id;
11259       RPC_Receiver  : Entity_Id;
11260       Stub_Elements : Stub_Structure)
11261    is
11262    begin
11263       case Get_PCS_Name is
11264          when Name_PolyORB_DSA =>
11265             PolyORB_Support.Add_Obj_RPC_Receiver_Completion (Loc,
11266               Decls, RPC_Receiver, Stub_Elements);
11267          when others =>
11268             GARLIC_Support.Add_Obj_RPC_Receiver_Completion (Loc,
11269               Decls, RPC_Receiver, Stub_Elements);
11270       end case;
11271    end Specific_Add_Obj_RPC_Receiver_Completion;
11272
11273    --------------------------------
11274    -- Specific_Add_RACW_Features --
11275    --------------------------------
11276
11277    procedure Specific_Add_RACW_Features
11278      (RACW_Type         : Entity_Id;
11279       Desig             : Entity_Id;
11280       Stub_Type         : Entity_Id;
11281       Stub_Type_Access  : Entity_Id;
11282       RPC_Receiver_Decl : Node_Id;
11283       Body_Decls        : List_Id)
11284    is
11285    begin
11286       case Get_PCS_Name is
11287          when Name_PolyORB_DSA =>
11288             PolyORB_Support.Add_RACW_Features
11289               (RACW_Type,
11290                Desig,
11291                Stub_Type,
11292                Stub_Type_Access,
11293                RPC_Receiver_Decl,
11294                Body_Decls);
11295
11296          when others =>
11297             GARLIC_Support.Add_RACW_Features
11298               (RACW_Type,
11299                Stub_Type,
11300                Stub_Type_Access,
11301                RPC_Receiver_Decl,
11302                Body_Decls);
11303       end case;
11304    end Specific_Add_RACW_Features;
11305
11306    --------------------------------
11307    -- Specific_Add_RAST_Features --
11308    --------------------------------
11309
11310    procedure Specific_Add_RAST_Features
11311      (Vis_Decl : Node_Id;
11312       RAS_Type : Entity_Id)
11313    is
11314    begin
11315       case Get_PCS_Name is
11316          when Name_PolyORB_DSA =>
11317             PolyORB_Support.Add_RAST_Features (Vis_Decl, RAS_Type);
11318          when others =>
11319             GARLIC_Support.Add_RAST_Features (Vis_Decl, RAS_Type);
11320       end case;
11321    end Specific_Add_RAST_Features;
11322
11323    --------------------------------------------------
11324    -- Specific_Add_Receiving_Stubs_To_Declarations --
11325    --------------------------------------------------
11326
11327    procedure Specific_Add_Receiving_Stubs_To_Declarations
11328      (Pkg_Spec : Node_Id;
11329       Decls    : List_Id;
11330       Stmts    : List_Id)
11331    is
11332    begin
11333       case Get_PCS_Name is
11334          when Name_PolyORB_DSA =>
11335             PolyORB_Support.Add_Receiving_Stubs_To_Declarations
11336               (Pkg_Spec, Decls, Stmts);
11337          when others =>
11338             GARLIC_Support.Add_Receiving_Stubs_To_Declarations
11339               (Pkg_Spec, Decls, Stmts);
11340       end case;
11341    end Specific_Add_Receiving_Stubs_To_Declarations;
11342
11343    ------------------------------------------
11344    -- Specific_Build_General_Calling_Stubs --
11345    ------------------------------------------
11346
11347    procedure Specific_Build_General_Calling_Stubs
11348      (Decls                     : List_Id;
11349       Statements                : List_Id;
11350       Target                    : RPC_Target;
11351       Subprogram_Id             : Node_Id;
11352       Asynchronous              : Node_Id   := Empty;
11353       Is_Known_Asynchronous     : Boolean   := False;
11354       Is_Known_Non_Asynchronous : Boolean   := False;
11355       Is_Function               : Boolean;
11356       Spec                      : Node_Id;
11357       Stub_Type                 : Entity_Id := Empty;
11358       RACW_Type                 : Entity_Id := Empty;
11359       Nod                       : Node_Id)
11360    is
11361    begin
11362       case Get_PCS_Name is
11363          when Name_PolyORB_DSA =>
11364             PolyORB_Support.Build_General_Calling_Stubs
11365               (Decls,
11366                Statements,
11367                Target.Object,
11368                Subprogram_Id,
11369                Asynchronous,
11370                Is_Known_Asynchronous,
11371                Is_Known_Non_Asynchronous,
11372                Is_Function,
11373                Spec,
11374                Stub_Type,
11375                RACW_Type,
11376                Nod);
11377
11378          when others =>
11379             GARLIC_Support.Build_General_Calling_Stubs
11380               (Decls,
11381                Statements,
11382                Target.Partition,
11383                Target.RPC_Receiver,
11384                Subprogram_Id,
11385                Asynchronous,
11386                Is_Known_Asynchronous,
11387                Is_Known_Non_Asynchronous,
11388                Is_Function,
11389                Spec,
11390                Stub_Type,
11391                RACW_Type,
11392                Nod);
11393       end case;
11394    end Specific_Build_General_Calling_Stubs;
11395
11396    --------------------------------------
11397    -- Specific_Build_RPC_Receiver_Body --
11398    --------------------------------------
11399
11400    procedure Specific_Build_RPC_Receiver_Body
11401      (RPC_Receiver : Entity_Id;
11402       Request      : out Entity_Id;
11403       Subp_Id      : out Entity_Id;
11404       Subp_Index   : out Entity_Id;
11405       Stmts        : out List_Id;
11406       Decl         : out Node_Id)
11407    is
11408    begin
11409       case Get_PCS_Name is
11410          when Name_PolyORB_DSA =>
11411             PolyORB_Support.Build_RPC_Receiver_Body
11412               (RPC_Receiver,
11413                Request,
11414                Subp_Id,
11415                Subp_Index,
11416                Stmts,
11417                Decl);
11418
11419          when others =>
11420             GARLIC_Support.Build_RPC_Receiver_Body
11421               (RPC_Receiver,
11422                Request,
11423                Subp_Id,
11424                Subp_Index,
11425                Stmts,
11426                Decl);
11427       end case;
11428    end Specific_Build_RPC_Receiver_Body;
11429
11430    --------------------------------
11431    -- Specific_Build_Stub_Target --
11432    --------------------------------
11433
11434    function Specific_Build_Stub_Target
11435      (Loc                   : Source_Ptr;
11436       Decls                 : List_Id;
11437       RCI_Locator           : Entity_Id;
11438       Controlling_Parameter : Entity_Id) return RPC_Target
11439    is
11440    begin
11441       case Get_PCS_Name is
11442          when Name_PolyORB_DSA =>
11443             return PolyORB_Support.Build_Stub_Target (Loc,
11444                      Decls, RCI_Locator, Controlling_Parameter);
11445
11446          when others =>
11447             return GARLIC_Support.Build_Stub_Target (Loc,
11448                      Decls, RCI_Locator, Controlling_Parameter);
11449       end case;
11450    end Specific_Build_Stub_Target;
11451
11452    ------------------------------
11453    -- Specific_Build_Stub_Type --
11454    ------------------------------
11455
11456    procedure Specific_Build_Stub_Type
11457      (RACW_Type         : Entity_Id;
11458       Stub_Type         : Entity_Id;
11459       Stub_Type_Decl    : out Node_Id;
11460       RPC_Receiver_Decl : out Node_Id)
11461    is
11462    begin
11463       case Get_PCS_Name is
11464          when Name_PolyORB_DSA =>
11465             PolyORB_Support.Build_Stub_Type (
11466               RACW_Type, Stub_Type,
11467               Stub_Type_Decl, RPC_Receiver_Decl);
11468
11469          when others =>
11470             GARLIC_Support.Build_Stub_Type (
11471               RACW_Type, Stub_Type,
11472               Stub_Type_Decl, RPC_Receiver_Decl);
11473       end case;
11474    end Specific_Build_Stub_Type;
11475
11476    function Specific_Build_Subprogram_Receiving_Stubs
11477      (Vis_Decl                 : Node_Id;
11478       Asynchronous             : Boolean;
11479       Dynamically_Asynchronous : Boolean   := False;
11480       Stub_Type                : Entity_Id := Empty;
11481       RACW_Type                : Entity_Id := Empty;
11482       Parent_Primitive         : Entity_Id := Empty) return Node_Id
11483    is
11484    begin
11485       case Get_PCS_Name is
11486          when Name_PolyORB_DSA =>
11487             return PolyORB_Support.Build_Subprogram_Receiving_Stubs
11488                      (Vis_Decl,
11489                       Asynchronous,
11490                       Dynamically_Asynchronous,
11491                       Stub_Type,
11492                       RACW_Type,
11493                       Parent_Primitive);
11494
11495          when others =>
11496             return GARLIC_Support.Build_Subprogram_Receiving_Stubs
11497                      (Vis_Decl,
11498                       Asynchronous,
11499                       Dynamically_Asynchronous,
11500                       Stub_Type,
11501                       RACW_Type,
11502                       Parent_Primitive);
11503       end case;
11504    end Specific_Build_Subprogram_Receiving_Stubs;
11505
11506    -------------------------------
11507    -- Transmit_As_Unconstrained --
11508    -------------------------------
11509
11510    function Transmit_As_Unconstrained (Typ : Entity_Id) return Boolean is
11511    begin
11512       return
11513         not (Is_Elementary_Type (Typ) or else Is_Constrained (Typ))
11514           or else (Is_Access_Type (Typ) and then Can_Never_Be_Null (Typ));
11515    end Transmit_As_Unconstrained;
11516
11517    --------------------------
11518    -- Underlying_RACW_Type --
11519    --------------------------
11520
11521    function Underlying_RACW_Type (RAS_Typ : Entity_Id) return Entity_Id is
11522       Record_Type : Entity_Id;
11523
11524    begin
11525       if Ekind (RAS_Typ) = E_Record_Type then
11526          Record_Type := RAS_Typ;
11527       else
11528          pragma Assert (Present (Equivalent_Type (RAS_Typ)));
11529          Record_Type := Equivalent_Type (RAS_Typ);
11530       end if;
11531
11532       return
11533         Etype (Subtype_Indication
11534                 (Component_Definition
11535                   (First (Component_Items
11536                            (Component_List
11537                              (Type_Definition
11538                                (Declaration_Node (Record_Type))))))));
11539    end Underlying_RACW_Type;
11540
11541 end Exp_Dist;