OSDN Git Service

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