1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004, Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Dist; use Exp_Dist;
32 with Exp_Tss; use Exp_Tss;
33 with Nlists; use Nlists;
34 with Nmake; use Nmake;
35 with Namet; use Namet;
37 with Rtsfind; use Rtsfind;
39 with Sem_Res; use Sem_Res;
40 with Sem_Util; use Sem_Util;
41 with Sinfo; use Sinfo;
42 with Snames; use Snames;
43 with Stand; use Stand;
44 with Stringt; use Stringt;
45 with Tbuild; use Tbuild;
46 with Uname; use Uname;
48 package body Sem_Dist is
50 -----------------------
51 -- Local Subprograms --
52 -----------------------
54 procedure RAS_E_Dereference (Pref : Node_Id);
55 -- Handles explicit dereference of Remote Access to Subprograms.
57 function Full_Qualified_Name (E : Entity_Id) return String_Id;
58 -- returns the full qualified name of the entity in lower case.
60 -------------------------
61 -- Add_Stub_Constructs --
62 -------------------------
64 procedure Add_Stub_Constructs (N : Node_Id) is
65 U : constant Node_Id := Unit (N);
66 Spec : Entity_Id := Empty;
67 Exp : Node_Id := U; -- Unit that will be expanded
70 pragma Assert (Distribution_Stub_Mode /= No_Stubs);
72 if Nkind (U) = N_Package_Declaration then
73 Spec := Defining_Entity (Specification (U));
75 elsif Nkind (U) = N_Package_Body then
76 Spec := Corresponding_Spec (U);
78 else pragma Assert (Nkind (U) = N_Package_Instantiation);
79 Exp := Instance_Spec (U);
80 Spec := Defining_Entity (Specification (Exp));
83 pragma Assert (Is_Shared_Passive (Spec)
84 or else Is_Remote_Call_Interface (Spec));
86 if Distribution_Stub_Mode = Generate_Caller_Stub_Body then
88 if Is_Shared_Passive (Spec) then
90 elsif Nkind (U) = N_Package_Body then
92 ("Specification file expected from command line", U);
94 Expand_Calling_Stubs_Bodies (Exp);
99 if Is_Shared_Passive (Spec) then
100 Build_Passive_Partition_Stub (Exp);
102 Expand_Receiving_Stubs_Bodies (Exp);
106 end Add_Stub_Constructs;
108 ---------------------------------------
109 -- Build_RAS_Primitive_Specification --
110 ---------------------------------------
112 function Build_RAS_Primitive_Specification
113 (Subp_Spec : Node_Id;
114 Remote_Object_Type : Node_Id) return Node_Id
116 Loc : constant Source_Ptr := Sloc (Subp_Spec);
118 Primitive_Spec : constant Node_Id :=
119 Copy_Specification (Loc,
121 New_Name => Name_Call);
123 Subtype_Mark_For_Self : Node_Id;
126 if No (Parameter_Specifications (Primitive_Spec)) then
127 Set_Parameter_Specifications (Primitive_Spec, New_List);
130 if Nkind (Remote_Object_Type) in N_Entity then
131 Subtype_Mark_For_Self :=
132 New_Occurrence_Of (Remote_Object_Type, Loc);
134 Subtype_Mark_For_Self := Remote_Object_Type;
138 Parameter_Specifications (Primitive_Spec),
139 Make_Parameter_Specification (Loc,
140 Defining_Identifier =>
141 Make_Defining_Identifier (Loc, Name_uS),
143 Make_Access_Definition (Loc,
145 Subtype_Mark_For_Self)));
147 -- Trick later semantic analysis into considering this
148 -- operation as a primitive (dispatching) operation of
149 -- tagged type Obj_Type.
151 Set_Comes_From_Source (
152 Defining_Unit_Name (Primitive_Spec), True);
154 return Primitive_Spec;
155 end Build_RAS_Primitive_Specification;
157 -------------------------
158 -- Full_Qualified_Name --
159 -------------------------
161 function Full_Qualified_Name (E : Entity_Id) return String_Id is
162 Ent : Entity_Id := E;
163 Parent_Name : String_Id := No_String;
166 -- Deals properly with child units
168 if Nkind (Ent) = N_Defining_Program_Unit_Name then
169 Ent := Defining_Identifier (Ent);
172 -- Compute recursively the qualification. Only "Standard" has no scope.
174 if Present (Scope (Scope (Ent))) then
175 Parent_Name := Full_Qualified_Name (Scope (Ent));
178 -- Every entity should have a name except some expanded blocks
179 -- don't bother about those.
181 if Chars (Ent) = No_Name then
185 -- Add a period between Name and qualification
187 if Parent_Name /= No_String then
188 Start_String (Parent_Name);
189 Store_String_Char (Get_Char_Code ('.'));
195 -- Generates the entity name in upper case
197 Get_Name_String (Chars (Ent));
198 Set_Casing (All_Lower_Case);
199 Store_String_Chars (Name_Buffer (1 .. Name_Len));
201 end Full_Qualified_Name;
203 ------------------------
204 -- Is_All_Remote_Call --
205 ------------------------
207 function Is_All_Remote_Call (N : Node_Id) return Boolean is
211 if (Nkind (N) = N_Function_Call
212 or else Nkind (N) = N_Procedure_Call_Statement)
213 and then Nkind (Name (N)) in N_Has_Entity
214 and then Is_Remote_Call_Interface (Entity (Name (N)))
215 and then Has_All_Calls_Remote (Scope (Entity (Name (N))))
216 and then Comes_From_Source (N)
218 Par := Parent (Entity (Name (N)));
221 and then (Nkind (Par) /= N_Package_Specification
222 or else Is_Wrapper_Package (Defining_Entity (Par)))
227 if Present (Par) then
229 not Scope_Within_Or_Same (Current_Scope, Defining_Entity (Par));
236 end Is_All_Remote_Call;
238 ------------------------------------
239 -- Package_Specification_Of_Scope --
240 ------------------------------------
242 function Package_Specification_Of_Scope (E : Entity_Id) return Node_Id is
243 N : Node_Id := Parent (E);
245 while Nkind (N) /= N_Package_Specification loop
250 end Package_Specification_Of_Scope;
252 --------------------------
253 -- Process_Partition_ID --
254 --------------------------
256 procedure Process_Partition_Id (N : Node_Id) is
257 Loc : constant Source_Ptr := Sloc (N);
260 Get_Pt_Id_Call : Node_Id;
261 Prefix_String : String_Id;
262 Typ : constant Entity_Id := Etype (N);
265 Ety := Entity (Prefix (N));
267 -- In case prefix is not a library unit entity, get the entity
270 while (Present (Scope (Ety))
271 and then Scope (Ety) /= Standard_Standard)
272 and not Is_Child_Unit (Ety)
277 -- Retrieve the proper function to call.
279 if Is_Remote_Call_Interface (Ety) then
280 Get_Pt_Id := New_Occurrence_Of
281 (RTE (RE_Get_Active_Partition_Id), Loc);
283 elsif Is_Shared_Passive (Ety) then
284 Get_Pt_Id := New_Occurrence_Of
285 (RTE (RE_Get_Passive_Partition_Id), Loc);
288 Get_Pt_Id := New_Occurrence_Of
289 (RTE (RE_Get_Local_Partition_Id), Loc);
292 -- Get and store the String_Id corresponding to the name of the
293 -- library unit whose Partition_Id is needed
295 Get_Unit_Name_String (Get_Unit_Name (Unit_Declaration_Node (Ety)));
297 -- Remove seven last character ("(spec)" or " (body)").
298 -- (this is a bit nasty, should have interface for this ???)
300 Name_Len := Name_Len - 7;
303 Store_String_Chars (Name_Buffer (1 .. Name_Len));
304 Prefix_String := End_String;
306 -- Build the function call which will replace the attribute
308 if Is_Remote_Call_Interface (Ety)
309 or else Is_Shared_Passive (Ety)
312 Make_Function_Call (Loc,
314 Parameter_Associations =>
315 New_List (Make_String_Literal (Loc, Prefix_String)));
318 Get_Pt_Id_Call := Make_Function_Call (Loc, Get_Pt_Id);
322 -- Replace the attribute node by a conversion of the function call
323 -- to the target type.
325 Rewrite (N, Convert_To (Typ, Get_Pt_Id_Call));
326 Analyze_And_Resolve (N, Typ);
327 end Process_Partition_Id;
329 ----------------------------------
330 -- Process_Remote_AST_Attribute --
331 ----------------------------------
333 procedure Process_Remote_AST_Attribute
335 New_Type : Entity_Id)
337 Loc : constant Source_Ptr := Sloc (N);
338 Remote_Subp : Entity_Id;
339 Tick_Access_Conv_Call : Node_Id;
340 Remote_Subp_Decl : Node_Id;
341 RS_Pkg_Specif : Node_Id;
342 RS_Pkg_E : Entity_Id;
343 RAS_Type : Entity_Id := New_Type;
345 All_Calls_Remote_E : Entity_Id;
346 Attribute_Subp : Entity_Id;
349 -- Check if we have to expand the access attribute
351 Remote_Subp := Entity (Prefix (N));
353 if not Expander_Active then
357 if Ekind (RAS_Type) /= E_Record_Type then
358 RAS_Type := Equivalent_Type (RAS_Type);
361 Attribute_Subp := TSS (RAS_Type, TSS_RAS_Access);
362 pragma Assert (Present (Attribute_Subp));
363 Remote_Subp_Decl := Unit_Declaration_Node (Remote_Subp);
365 if Nkind (Remote_Subp_Decl) = N_Subprogram_Body then
366 Remote_Subp := Corresponding_Spec (Remote_Subp_Decl);
367 Remote_Subp_Decl := Unit_Declaration_Node (Remote_Subp);
370 RS_Pkg_Specif := Parent (Remote_Subp_Decl);
371 RS_Pkg_E := Defining_Entity (RS_Pkg_Specif);
374 Boolean_Literals (Ekind (Remote_Subp) = E_Procedure
375 and then Is_Asynchronous (Remote_Subp));
377 All_Calls_Remote_E :=
378 Boolean_Literals (Has_All_Calls_Remote (RS_Pkg_E));
380 Tick_Access_Conv_Call :=
381 Make_Function_Call (Loc,
382 Name => New_Occurrence_Of (Attribute_Subp, Loc),
383 Parameter_Associations =>
385 Make_String_Literal (Loc, Full_Qualified_Name (RS_Pkg_E)),
386 Build_Subprogram_Id (Loc, Remote_Subp),
387 New_Occurrence_Of (Async_E, Loc),
388 New_Occurrence_Of (All_Calls_Remote_E, Loc)));
390 Rewrite (N, Tick_Access_Conv_Call);
391 Analyze_And_Resolve (N, RAS_Type);
392 end Process_Remote_AST_Attribute;
394 ------------------------------------
395 -- Process_Remote_AST_Declaration --
396 ------------------------------------
398 procedure Process_Remote_AST_Declaration (N : Node_Id) is
399 Loc : constant Source_Ptr := Sloc (N);
400 User_Type : constant Node_Id := Defining_Identifier (N);
401 Scop : constant Entity_Id := Scope (User_Type);
402 Is_RCI : constant Boolean :=
403 Is_Remote_Call_Interface (Scop);
404 Is_RT : constant Boolean :=
405 Is_Remote_Types (Scop);
406 Type_Def : constant Node_Id := Type_Definition (N);
409 Is_Degenerate : Boolean;
410 -- True iff this RAS has an access formal parameter (see
411 -- Exp_Dist.Add_RAS_Dereference_TSS for details).
413 Subpkg : constant Entity_Id :=
414 Make_Defining_Identifier
415 (Loc, New_Internal_Name ('S'));
416 Subpkg_Decl : Node_Id;
417 Vis_Decls : constant List_Id := New_List;
418 Priv_Decls : constant List_Id := New_List;
420 Obj_Type : constant Entity_Id :=
421 Make_Defining_Identifier
422 (Loc, New_External_Name (
423 Chars (User_Type), 'R'));
426 Full_Obj_Type : constant Entity_Id :=
427 Make_Defining_Identifier
428 (Loc, Chars (Obj_Type));
430 RACW_Type : constant Entity_Id :=
431 Make_Defining_Identifier
432 (Loc, New_External_Name (
433 Chars (User_Type), 'P'));
435 Fat_Type : constant Entity_Id :=
436 Make_Defining_Identifier
437 (Loc, Chars (User_Type));
438 Fat_Type_Decl : Node_Id;
442 -- The tagged private type, primitive operation and RACW
443 -- type associated with a RAS need to all be declared in
444 -- a subpackage of the one that contains the RAS declaration,
445 -- because the primitive of the object type, and the associated
446 -- primitive of the stub type, need to be dispatching operations
447 -- of these types, and the profile of the RAS might contain
448 -- tagged types declared in the same scope.
450 Append_To (Vis_Decls,
451 Make_Private_Type_Declaration (Loc,
452 Defining_Identifier => Obj_Type,
453 Abstract_Present => True,
454 Tagged_Present => True,
455 Limited_Present => True));
457 Append_To (Priv_Decls,
458 Make_Full_Type_Declaration (Loc,
459 Defining_Identifier =>
462 Make_Record_Definition (Loc,
463 Abstract_Present => True,
464 Tagged_Present => True,
465 Limited_Present => True,
466 Null_Present => True,
467 Component_List => Empty)));
469 Is_Degenerate := False;
470 Parameter := First (Parameter_Specifications (Type_Def));
471 Parameters : while Present (Parameter) loop
472 if Nkind (Parameter_Type (Parameter)) = N_Access_Definition then
473 Error_Msg_N ("formal parameter& has anonymous access type?",
474 Defining_Identifier (Parameter));
475 Is_Degenerate := True;
481 if Is_Degenerate then
483 "remote access-to-subprogram type& can only be null?",
484 Defining_Identifier (Parameter), User_Type);
485 -- The only legal value for a RAS with a formal parameter of an
486 -- anonymous access type is null, because it cannot be
487 -- subtype-Conformant with any legal remote subprogram declaration.
488 -- In this case, we cannot generate a corresponding primitive
492 Append_To (Vis_Decls,
493 Make_Abstract_Subprogram_Declaration (Loc,
494 Specification => Build_RAS_Primitive_Specification (
495 Subp_Spec => Type_Def,
496 Remote_Object_Type => Obj_Type)));
499 Append_To (Vis_Decls,
500 Make_Full_Type_Declaration (Loc,
501 Defining_Identifier => RACW_Type,
503 Make_Access_To_Object_Definition (Loc,
505 Subtype_Indication =>
506 Make_Attribute_Reference (Loc,
508 New_Occurrence_Of (Obj_Type, Loc),
511 Set_Is_Remote_Call_Interface (RACW_Type, Is_RCI);
512 Set_Is_Remote_Types (RACW_Type, Is_RT);
513 -- ??? Object RPC receiver generation should be bypassed for this
514 -- RACW type, since actually calls will be received by the package
515 -- RPC receiver for the designated RCI subprogram.
518 Make_Package_Declaration (Loc,
519 Make_Package_Specification (Loc,
520 Defining_Unit_Name =>
522 Visible_Declarations =>
524 Private_Declarations =>
527 New_Occurrence_Of (Subpkg, Loc)));
528 Set_Is_Remote_Call_Interface (Subpkg, Is_RCI);
529 Set_Is_Remote_Types (Subpkg, Is_RT);
530 Insert_After_And_Analyze (N, Subpkg_Decl);
532 -- Many parts of the analyzer and expander expect
533 -- that the fat pointer type used to implement remote
534 -- access to subprogram types be a record.
535 -- Note: The structure of this type must be kept consistent
536 -- with the code generated by Remote_AST_Null_Value for the
537 -- corresponding 'null' expression.
539 Fat_Type_Decl := Make_Full_Type_Declaration (Loc,
540 Defining_Identifier => Fat_Type,
542 Make_Record_Definition (Loc,
544 Make_Component_List (Loc,
545 Component_Items => New_List (
546 Make_Component_Declaration (Loc,
547 Defining_Identifier =>
548 Make_Defining_Identifier (Loc, Name_Ras),
549 Component_Definition =>
550 Make_Component_Definition (Loc,
553 Subtype_Indication =>
554 New_Occurrence_Of (RACW_Type, Loc)))))));
555 Set_Equivalent_Type (User_Type, Fat_Type);
556 Set_Corresponding_Remote_Type (Fat_Type, User_Type);
557 Insert_After_And_Analyze (Subpkg_Decl, Fat_Type_Decl);
559 -- The reason we suppress the initialization procedure is that we know
560 -- that no initialization is required (even if Initialize_Scalars mode
561 -- is active), and there are order of elaboration problems if we do try
562 -- to generate an init proc for this created record type.
564 Set_Suppress_Init_Proc (Fat_Type);
566 if Expander_Active then
567 Add_RAST_Features (Parent (User_Type));
569 end Process_Remote_AST_Declaration;
571 -----------------------
572 -- RAS_E_Dereference --
573 -----------------------
575 procedure RAS_E_Dereference (Pref : Node_Id) is
576 Loc : constant Source_Ptr := Sloc (Pref);
578 New_Type : constant Entity_Id := Etype (Pref);
579 Explicit_Deref : constant Node_Id := Parent (Pref);
580 Deref_Subp_Call : constant Node_Id := Parent (Explicit_Deref);
581 Deref_Proc : Entity_Id;
585 if Nkind (Deref_Subp_Call) = N_Procedure_Call_Statement then
586 Params := Parameter_Associations (Deref_Subp_Call);
588 if Present (Params) then
589 Prepend (Pref, Params);
591 Params := New_List (Pref);
594 elsif Nkind (Deref_Subp_Call) = N_Indexed_Component then
596 Params := Expressions (Deref_Subp_Call);
598 if Present (Params) then
599 Prepend (Pref, Params);
601 Params := New_List (Pref);
605 -- Context is not a call.
610 if not Expander_Active then
614 Deref_Proc := TSS (New_Type, TSS_RAS_Dereference);
615 pragma Assert (Present (Deref_Proc));
617 if Ekind (Deref_Proc) = E_Function then
619 Make_Function_Call (Loc,
620 Name => New_Occurrence_Of (Deref_Proc, Loc),
621 Parameter_Associations => Params);
625 Make_Procedure_Call_Statement (Loc,
626 Name => New_Occurrence_Of (Deref_Proc, Loc),
627 Parameter_Associations => Params);
630 Rewrite (Deref_Subp_Call, Call_Node);
631 Analyze (Deref_Subp_Call);
632 end RAS_E_Dereference;
634 ------------------------------
635 -- Remote_AST_E_Dereference --
636 ------------------------------
638 function Remote_AST_E_Dereference (P : Node_Id) return Boolean is
639 ET : constant Entity_Id := Etype (P);
642 -- Perform the changes only on original dereferences, and only if
643 -- we are generating code.
645 if Comes_From_Source (P)
646 and then Is_Record_Type (ET)
647 and then (Is_Remote_Call_Interface (ET)
648 or else Is_Remote_Types (ET))
649 and then Present (Corresponding_Remote_Type (ET))
650 and then (Nkind (Parent (Parent (P))) = N_Procedure_Call_Statement
651 or else Nkind (Parent (Parent (P))) = N_Indexed_Component)
652 and then Expander_Active
654 RAS_E_Dereference (P);
659 end Remote_AST_E_Dereference;
661 ------------------------------
662 -- Remote_AST_I_Dereference --
663 ------------------------------
665 function Remote_AST_I_Dereference (P : Node_Id) return Boolean is
666 ET : constant Entity_Id := Etype (P);
670 if Comes_From_Source (P)
671 and then (Is_Remote_Call_Interface (ET)
672 or else Is_Remote_Types (ET))
673 and then Present (Corresponding_Remote_Type (ET))
674 and then Ekind (Entity (P)) /= E_Function
677 Make_Explicit_Dereference (Sloc (P),
678 Prefix => Relocate_Node (P));
681 RAS_E_Dereference (Prefix (P));
686 end Remote_AST_I_Dereference;
688 ---------------------------
689 -- Remote_AST_Null_Value --
690 ---------------------------
692 function Remote_AST_Null_Value
694 Typ : Entity_Id) return Boolean
696 Loc : constant Source_Ptr := Sloc (N);
697 Target_Type : Entity_Id;
700 if not Expander_Active then
703 elsif Ekind (Typ) = E_Access_Subprogram_Type
704 and then (Is_Remote_Call_Interface (Typ)
705 or else Is_Remote_Types (Typ))
706 and then Comes_From_Source (N)
707 and then Expander_Active
709 -- Any null that comes from source and is of the RAS type must
710 -- be expanded, except if expansion is not active (nothing
711 -- gets expanded into the equivalent record type).
713 Target_Type := Equivalent_Type (Typ);
715 elsif Ekind (Typ) = E_Record_Type
716 and then Present (Corresponding_Remote_Type (Typ))
718 -- This is a record type representing a RAS type, this must be
724 -- We do not have to handle this case
732 Component_Associations => New_List (
733 Make_Component_Association (Loc,
734 Choices => New_List (
735 Make_Identifier (Loc, Name_Ras)),
738 Analyze_And_Resolve (N, Target_Type);
740 end Remote_AST_Null_Value;