1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 with Atree
; use Atree
;
29 with Casing
; use Casing
;
30 with Einfo
; use Einfo
;
31 with Errout
; use Errout
;
32 with Exp_Dist
; use Exp_Dist
;
33 with Exp_Tss
; use Exp_Tss
;
34 with Nlists
; use Nlists
;
35 with Nmake
; use Nmake
;
36 with Namet
; use Namet
;
38 with Rtsfind
; use Rtsfind
;
40 with Sem_Res
; use Sem_Res
;
41 with Sem_Util
; use Sem_Util
;
42 with Sinfo
; use Sinfo
;
43 with Snames
; use Snames
;
44 with Stand
; use Stand
;
45 with Stringt
; use Stringt
;
46 with Tbuild
; use Tbuild
;
47 with Uname
; use Uname
;
49 package body Sem_Dist
is
51 -----------------------
52 -- Local Subprograms --
53 -----------------------
55 procedure RAS_E_Dereference
(Pref
: Node_Id
);
56 -- Handles explicit dereference of Remote Access to Subprograms.
58 function Full_Qualified_Name
(E
: Entity_Id
) return String_Id
;
59 -- returns the full qualified name of the entity in lower case.
61 -------------------------
62 -- Add_Stub_Constructs --
63 -------------------------
65 procedure Add_Stub_Constructs
(N
: Node_Id
) is
66 U
: constant Node_Id
:= Unit
(N
);
67 Spec
: Entity_Id
:= Empty
;
68 Exp
: Node_Id
:= U
; -- Unit that will be expanded
71 pragma Assert
(Distribution_Stub_Mode
/= No_Stubs
);
73 if Nkind
(U
) = N_Package_Declaration
then
74 Spec
:= Defining_Entity
(Specification
(U
));
76 elsif Nkind
(U
) = N_Package_Body
then
77 Spec
:= Corresponding_Spec
(U
);
79 else pragma Assert
(Nkind
(U
) = N_Package_Instantiation
);
80 Exp
:= Instance_Spec
(U
);
81 Spec
:= Defining_Entity
(Specification
(Exp
));
84 pragma Assert
(Is_Shared_Passive
(Spec
)
85 or else Is_Remote_Call_Interface
(Spec
));
87 if Distribution_Stub_Mode
= Generate_Caller_Stub_Body
then
89 if Is_Shared_Passive
(Spec
) then
91 elsif Nkind
(U
) = N_Package_Body
then
93 ("Specification file expected from command line", U
);
95 Expand_Calling_Stubs_Bodies
(Exp
);
100 if Is_Shared_Passive
(Spec
) then
101 Build_Passive_Partition_Stub
(Exp
);
103 Expand_Receiving_Stubs_Bodies
(Exp
);
107 end Add_Stub_Constructs
;
109 -------------------------
110 -- Full_Qualified_Name --
111 -------------------------
113 function Full_Qualified_Name
(E
: Entity_Id
) return String_Id
is
114 Ent
: Entity_Id
:= E
;
115 Parent_Name
: String_Id
:= No_String
;
118 -- Deals properly with child units
120 if Nkind
(Ent
) = N_Defining_Program_Unit_Name
then
121 Ent
:= Defining_Identifier
(Ent
);
124 -- Compute recursively the qualification. Only "Standard" has no scope.
126 if Present
(Scope
(Scope
(Ent
))) then
127 Parent_Name
:= Full_Qualified_Name
(Scope
(Ent
));
130 -- Every entity should have a name except some expanded blocks
131 -- don't bother about those.
133 if Chars
(Ent
) = No_Name
then
137 -- Add a period between Name and qualification
139 if Parent_Name
/= No_String
then
140 Start_String
(Parent_Name
);
141 Store_String_Char
(Get_Char_Code
('.'));
147 -- Generates the entity name in upper case
149 Get_Name_String
(Chars
(Ent
));
150 Set_Casing
(All_Lower_Case
);
151 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
153 end Full_Qualified_Name
;
155 -----------------------
156 -- Get_Subprogram_Id --
157 -----------------------
159 function Get_Subprogram_Id
(E
: Entity_Id
) return Int
is
160 Current_Declaration
: Node_Id
;
165 (Is_Remote_Call_Interface
(Scope
(E
))
167 (Nkind
(Parent
(E
)) = N_Procedure_Specification
169 Nkind
(Parent
(E
)) = N_Function_Specification
));
171 Current_Declaration
:=
172 First
(Visible_Declarations
173 (Package_Specification_Of_Scope
(Scope
(E
))));
175 while Current_Declaration
/= Empty
loop
176 if Nkind
(Current_Declaration
) = N_Subprogram_Declaration
177 and then Comes_From_Source
(Current_Declaration
)
179 if Defining_Unit_Name
180 (Specification
(Current_Declaration
)) = E
185 Result
:= Result
+ 1;
188 Next
(Current_Declaration
);
191 -- Error if we do not find it
194 end Get_Subprogram_Id
;
196 ------------------------
197 -- Is_All_Remote_Call --
198 ------------------------
200 function Is_All_Remote_Call
(N
: Node_Id
) return Boolean is
204 if (Nkind
(N
) = N_Function_Call
205 or else Nkind
(N
) = N_Procedure_Call_Statement
)
206 and then Nkind
(Name
(N
)) in N_Has_Entity
207 and then Is_Remote_Call_Interface
(Entity
(Name
(N
)))
208 and then Has_All_Calls_Remote
(Scope
(Entity
(Name
(N
))))
209 and then Comes_From_Source
(N
)
211 Par
:= Parent
(Entity
(Name
(N
)));
214 and then (Nkind
(Par
) /= N_Package_Specification
215 or else Is_Wrapper_Package
(Defining_Entity
(Par
)))
220 if Present
(Par
) then
222 not Scope_Within_Or_Same
(Current_Scope
, Defining_Entity
(Par
));
229 end Is_All_Remote_Call
;
231 ------------------------------------
232 -- Package_Specification_Of_Scope --
233 ------------------------------------
235 function Package_Specification_Of_Scope
(E
: Entity_Id
) return Node_Id
is
236 N
: Node_Id
:= Parent
(E
);
238 while Nkind
(N
) /= N_Package_Specification
loop
243 end Package_Specification_Of_Scope
;
245 --------------------------
246 -- Process_Partition_ID --
247 --------------------------
249 procedure Process_Partition_Id
(N
: Node_Id
) is
250 Loc
: constant Source_Ptr
:= Sloc
(N
);
254 Get_Pt_Id_Call
: Node_Id
;
255 Prefix_String
: String_Id
;
256 Typ
: constant Entity_Id
:= Etype
(N
);
259 Ety
:= Entity
(Prefix
(N
));
261 -- In case prefix is not a library unit entity, get the entity
264 while (Present
(Scope
(Ety
))
265 and then Scope
(Ety
) /= Standard_Standard
)
266 and not Is_Child_Unit
(Ety
)
271 Nd
:= Enclosing_Lib_Unit_Node
(N
);
273 -- Retrieve the proper function to call.
275 if Is_Remote_Call_Interface
(Ety
) then
276 Get_Pt_Id
:= New_Occurrence_Of
277 (RTE
(RE_Get_Active_Partition_Id
), Loc
);
279 elsif Is_Shared_Passive
(Ety
) then
280 Get_Pt_Id
:= New_Occurrence_Of
281 (RTE
(RE_Get_Passive_Partition_Id
), Loc
);
284 Get_Pt_Id
:= New_Occurrence_Of
285 (RTE
(RE_Get_Local_Partition_Id
), Loc
);
288 -- Get and store the String_Id corresponding to the name of the
289 -- library unit whose Partition_Id is needed
291 Get_Unit_Name_String
(Get_Unit_Name
(Unit_Declaration_Node
(Ety
)));
293 -- Remove seven last character ("(spec)" or " (body)").
294 -- (this is a bit nasty, should have interface for this ???)
296 Name_Len
:= Name_Len
- 7;
299 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
300 Prefix_String
:= End_String
;
302 -- Build the function call which will replace the attribute
304 if Is_Remote_Call_Interface
(Ety
)
305 or else Is_Shared_Passive
(Ety
)
308 Make_Function_Call
(Loc
,
310 Parameter_Associations
=>
311 New_List
(Make_String_Literal
(Loc
, Prefix_String
)));
314 Get_Pt_Id_Call
:= Make_Function_Call
(Loc
, Get_Pt_Id
);
318 -- Replace the attribute node by a conversion of the function call
319 -- to the target type.
321 Rewrite
(N
, Convert_To
(Typ
, Get_Pt_Id_Call
));
322 Analyze_And_Resolve
(N
, Typ
);
324 end Process_Partition_Id
;
326 ----------------------------------
327 -- Process_Remote_AST_Attribute --
328 ----------------------------------
330 procedure Process_Remote_AST_Attribute
332 New_Type
: Entity_Id
)
334 Loc
: constant Source_Ptr
:= Sloc
(N
);
335 Remote_Subp
: Entity_Id
;
336 Tick_Access_Conv_Call
: Node_Id
;
337 Remote_Subp_Decl
: Node_Id
;
339 RS_Pkg_Specif
: Node_Id
;
340 RS_Pkg_E
: Entity_Id
;
341 RAS_Pkg_E
: Entity_Id
;
342 RAS_Type
: Entity_Id
;
346 Attribute_Subp
: Entity_Id
;
350 -- Check if we have to expand the access attribute
352 Remote_Subp
:= Entity
(Prefix
(N
));
354 if not Expander_Active
then
357 elsif Ekind
(New_Type
) = E_Record_Type
then
358 RAS_Type
:= New_Type
;
361 -- If the remote type has not been constructed yet, create
362 -- it and its attributes now.
364 Attribute_Subp
:= TSS
(New_Type
, Name_uRAS_Access
);
366 if No
(Attribute_Subp
) then
367 Add_RAST_Features
(Parent
(New_Type
));
370 RAS_Type
:= Equivalent_Type
(New_Type
);
373 RAS_Name
:= Chars
(RAS_Type
);
374 RAS_Decl
:= Parent
(RAS_Type
);
375 Attribute_Subp
:= TSS
(RAS_Type
, Name_uRAS_Access
);
377 RAS_Pkg_E
:= Defining_Entity
(Parent
(RAS_Decl
));
378 Remote_Subp_Decl
:= Unit_Declaration_Node
(Remote_Subp
);
380 if Nkind
(Remote_Subp_Decl
) = N_Subprogram_Body
then
381 Remote_Subp
:= Corresponding_Spec
(Remote_Subp_Decl
);
382 Remote_Subp_Decl
:= Unit_Declaration_Node
(Remote_Subp
);
385 RS_Pkg_Specif
:= Parent
(Remote_Subp_Decl
);
386 RS_Pkg_E
:= Defining_Entity
(RS_Pkg_Specif
);
388 Subp_Id
:= Get_Subprogram_Id
(Remote_Subp
);
390 if Ekind
(Remote_Subp
) = E_Procedure
391 and then Is_Asynchronous
(Remote_Subp
)
393 Async_E
:= Standard_True
;
395 Async_E
:= Standard_False
;
398 -- Right now, we do not call the Name_uAddress_Resolver subprogram,
399 -- which means that we end up with a Null_Address value in the ras
400 -- field: each dereference of an RAS will go through the PCS, which
401 -- is authorized but potentially not very efficient ???
403 Parameter
:= New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
);
405 Tick_Access_Conv_Call
:=
406 Make_Function_Call
(Loc
,
407 Name
=> New_Occurrence_Of
(Attribute_Subp
, Loc
),
408 Parameter_Associations
=>
411 Make_String_Literal
(Loc
, Full_Qualified_Name
(RS_Pkg_E
)),
412 Make_Integer_Literal
(Loc
, Subp_Id
),
413 New_Occurrence_Of
(Async_E
, Loc
)));
415 Rewrite
(N
, Tick_Access_Conv_Call
);
416 Analyze_And_Resolve
(N
, RAS_Type
);
418 end Process_Remote_AST_Attribute
;
420 ------------------------------------
421 -- Process_Remote_AST_Declaration --
422 ------------------------------------
424 procedure Process_Remote_AST_Declaration
(N
: Node_Id
) is
425 Loc
: constant Source_Ptr
:= Sloc
(N
);
426 User_Type
: constant Node_Id
:= Defining_Identifier
(N
);
427 Fat_Type
: constant Entity_Id
:=
428 Make_Defining_Identifier
429 (Loc
, Chars
(User_Type
));
430 New_Type_Decl
: Node_Id
;
433 -- We add a record type declaration for the equivalent fat pointer type
436 Make_Full_Type_Declaration
(Loc
,
437 Defining_Identifier
=> Fat_Type
,
439 Make_Record_Definition
(Loc
,
441 Make_Component_List
(Loc
,
442 Component_Items
=> New_List
(
444 Make_Component_Declaration
(Loc
,
445 Defining_Identifier
=>
446 Make_Defining_Identifier
(Loc
,
448 Subtype_Indication
=>
450 (RTE
(RE_Unsigned_64
), Loc
)),
452 Make_Component_Declaration
(Loc
,
453 Defining_Identifier
=>
454 Make_Defining_Identifier
(Loc
,
455 Chars
=> Name_Origin
),
456 Subtype_Indication
=>
461 Make_Component_Declaration
(Loc
,
462 Defining_Identifier
=>
463 Make_Defining_Identifier
(Loc
,
464 Chars
=> Name_Receiver
),
465 Subtype_Indication
=>
467 (RTE
(RE_Unsigned_64
), Loc
)),
469 Make_Component_Declaration
(Loc
,
470 Defining_Identifier
=>
471 Make_Defining_Identifier
(Loc
,
472 Chars
=> Name_Subp_Id
),
473 Subtype_Indication
=>
478 Make_Component_Declaration
(Loc
,
479 Defining_Identifier
=>
480 Make_Defining_Identifier
(Loc
,
481 Chars
=> Name_Async
),
482 Subtype_Indication
=>
487 Insert_After
(N
, New_Type_Decl
);
488 Set_Equivalent_Type
(User_Type
, Fat_Type
);
489 Set_Corresponding_Remote_Type
(Fat_Type
, User_Type
);
491 -- The reason we suppress the initialization procedure is that we know
492 -- that no initialization is required (even if Initialize_Scalars mode
493 -- is active), and there are order of elaboration problems if we do try
494 -- to generate an Init_Proc for this created record type.
496 Set_Suppress_Init_Proc
(Fat_Type
);
498 if Expander_Active
then
499 Add_RAST_Features
(Parent
(User_Type
));
502 end Process_Remote_AST_Declaration
;
504 -----------------------
505 -- RAS_E_Dereference --
506 -----------------------
508 procedure RAS_E_Dereference
(Pref
: Node_Id
) is
509 Loc
: constant Source_Ptr
:= Sloc
(Pref
);
511 New_Type
: constant Entity_Id
:= Etype
(Pref
);
512 RAS
: constant Entity_Id
:=
513 Corresponding_Remote_Type
(New_Type
);
514 RAS_Decl
: constant Node_Id
:= Parent
(RAS
);
515 Explicit_Deref
: constant Node_Id
:= Parent
(Pref
);
516 Deref_Subp_Call
: constant Node_Id
:= Parent
(Explicit_Deref
);
517 Deref_Proc
: Entity_Id
;
521 if Nkind
(Deref_Subp_Call
) = N_Procedure_Call_Statement
then
522 Params
:= Parameter_Associations
(Deref_Subp_Call
);
524 if Present
(Params
) then
525 Prepend
(Pref
, Params
);
527 Params
:= New_List
(Pref
);
530 elsif Nkind
(Deref_Subp_Call
) = N_Indexed_Component
then
532 Params
:= Expressions
(Deref_Subp_Call
);
534 if Present
(Params
) then
535 Prepend
(Pref
, Params
);
537 Params
:= New_List
(Pref
);
541 -- Context is not a call.
546 Deref_Proc
:= TSS
(New_Type
, Name_uRAS_Dereference
);
548 if not Expander_Active
then
551 elsif No
(Deref_Proc
) then
552 Add_RAST_Features
(RAS_Decl
);
553 Deref_Proc
:= TSS
(New_Type
, Name_uRAS_Dereference
);
556 if Ekind
(Deref_Proc
) = E_Function
then
558 Make_Function_Call
(Loc
,
559 Name
=> New_Occurrence_Of
(Deref_Proc
, Loc
),
560 Parameter_Associations
=> Params
);
564 Make_Procedure_Call_Statement
(Loc
,
565 Name
=> New_Occurrence_Of
(Deref_Proc
, Loc
),
566 Parameter_Associations
=> Params
);
569 Rewrite
(Deref_Subp_Call
, Call_Node
);
570 Analyze
(Deref_Subp_Call
);
571 end RAS_E_Dereference
;
573 ------------------------------
574 -- Remote_AST_E_Dereference --
575 ------------------------------
577 function Remote_AST_E_Dereference
(P
: Node_Id
) return Boolean
579 ET
: constant Entity_Id
:= Etype
(P
);
582 -- Perform the changes only on original dereferences, and only if
583 -- we are generating code.
585 if Comes_From_Source
(P
)
586 and then Is_Record_Type
(ET
)
587 and then (Is_Remote_Call_Interface
(ET
)
588 or else Is_Remote_Types
(ET
))
589 and then Present
(Corresponding_Remote_Type
(ET
))
590 and then (Nkind
(Parent
(Parent
(P
))) = N_Procedure_Call_Statement
591 or else Nkind
(Parent
(Parent
(P
))) = N_Indexed_Component
)
592 and then Expander_Active
594 RAS_E_Dereference
(P
);
599 end Remote_AST_E_Dereference
;
601 ------------------------------
602 -- Remote_AST_I_Dereference --
603 ------------------------------
605 function Remote_AST_I_Dereference
(P
: Node_Id
) return Boolean
607 ET
: constant Entity_Id
:= Etype
(P
);
611 if Comes_From_Source
(P
)
612 and then (Is_Remote_Call_Interface
(ET
)
613 or else Is_Remote_Types
(ET
))
614 and then Present
(Corresponding_Remote_Type
(ET
))
615 and then Ekind
(Entity
(P
)) /= E_Function
618 Make_Explicit_Dereference
(Sloc
(P
),
619 Prefix
=> Relocate_Node
(P
));
622 RAS_E_Dereference
(Prefix
(P
));
627 end Remote_AST_I_Dereference
;
629 ---------------------------
630 -- Remote_AST_Null_Value --
631 ---------------------------
633 function Remote_AST_Null_Value
638 Loc
: constant Source_Ptr
:= Sloc
(N
);
639 Target_Type
: Entity_Id
;
642 if not Expander_Active
then
645 elsif Ekind
(Typ
) = E_Access_Subprogram_Type
646 and then (Is_Remote_Call_Interface
(Typ
)
647 or else Is_Remote_Types
(Typ
))
648 and then Comes_From_Source
(N
)
649 and then Expander_Active
651 -- Any null that comes from source and is of the RAS type must
652 -- be expanded, except if expansion is not active (nothing
653 -- gets expanded into the equivalent record type).
655 Target_Type
:= Equivalent_Type
(Typ
);
657 elsif Ekind
(Typ
) = E_Record_Type
658 and then Present
(Corresponding_Remote_Type
(Typ
))
660 -- This is a record type representing a RAS type, this must be
666 -- We do not have to handle this case
674 Expressions
=> New_List
(
675 Make_Integer_Literal
(Loc
, 0), -- Ras
676 Make_Integer_Literal
(Loc
, 0), -- Origin
677 Make_Integer_Literal
(Loc
, 0), -- Receiver
678 Make_Integer_Literal
(Loc
, 0), -- Subp_Id
679 New_Occurrence_Of
(Standard_False
, Loc
)))); -- Asyn
680 Analyze_And_Resolve
(N
, Target_Type
);
682 end Remote_AST_Null_Value
;