1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2001, 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 -- Full_Qualified_Name --
110 -------------------------
112 function Full_Qualified_Name
(E
: Entity_Id
) return String_Id
is
113 Ent
: Entity_Id
:= E
;
114 Parent_Name
: String_Id
:= No_String
;
117 -- Deals properly with child units
119 if Nkind
(Ent
) = N_Defining_Program_Unit_Name
then
120 Ent
:= Defining_Identifier
(Ent
);
123 -- Compute recursively the qualification. Only "Standard" has no scope.
125 if Present
(Scope
(Scope
(Ent
))) then
126 Parent_Name
:= Full_Qualified_Name
(Scope
(Ent
));
129 -- Every entity should have a name except some expanded blocks
130 -- don't bother about those.
132 if Chars
(Ent
) = No_Name
then
136 -- Add a period between Name and qualification
138 if Parent_Name
/= No_String
then
139 Start_String
(Parent_Name
);
140 Store_String_Char
(Get_Char_Code
('.'));
146 -- Generates the entity name in upper case
148 Get_Name_String
(Chars
(Ent
));
149 Set_Casing
(All_Lower_Case
);
150 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
152 end Full_Qualified_Name
;
154 -----------------------
155 -- Get_Subprogram_Id --
156 -----------------------
158 function Get_Subprogram_Id
(E
: Entity_Id
) return Int
is
159 Current_Declaration
: Node_Id
;
164 (Is_Remote_Call_Interface
(Scope
(E
))
166 (Nkind
(Parent
(E
)) = N_Procedure_Specification
168 Nkind
(Parent
(E
)) = N_Function_Specification
));
170 Current_Declaration
:=
171 First
(Visible_Declarations
172 (Package_Specification_Of_Scope
(Scope
(E
))));
174 while Current_Declaration
/= Empty
loop
175 if Nkind
(Current_Declaration
) = N_Subprogram_Declaration
176 and then Comes_From_Source
(Current_Declaration
)
178 if Defining_Unit_Name
179 (Specification
(Current_Declaration
)) = E
184 Result
:= Result
+ 1;
187 Next
(Current_Declaration
);
190 -- Error if we do not find it
193 end Get_Subprogram_Id
;
195 ------------------------
196 -- Is_All_Remote_Call --
197 ------------------------
199 function Is_All_Remote_Call
(N
: Node_Id
) return Boolean is
203 if (Nkind
(N
) = N_Function_Call
204 or else Nkind
(N
) = N_Procedure_Call_Statement
)
205 and then Nkind
(Name
(N
)) in N_Has_Entity
206 and then Is_Remote_Call_Interface
(Entity
(Name
(N
)))
207 and then Has_All_Calls_Remote
(Scope
(Entity
(Name
(N
))))
208 and then Comes_From_Source
(N
)
210 Par
:= Parent
(Entity
(Name
(N
)));
213 and then (Nkind
(Par
) /= N_Package_Specification
214 or else Is_Wrapper_Package
(Defining_Entity
(Par
)))
219 if Present
(Par
) then
221 not Scope_Within_Or_Same
(Current_Scope
, Defining_Entity
(Par
));
228 end Is_All_Remote_Call
;
230 ------------------------------------
231 -- Package_Specification_Of_Scope --
232 ------------------------------------
234 function Package_Specification_Of_Scope
(E
: Entity_Id
) return Node_Id
is
235 N
: Node_Id
:= Parent
(E
);
237 while Nkind
(N
) /= N_Package_Specification
loop
242 end Package_Specification_Of_Scope
;
244 --------------------------
245 -- Process_Partition_ID --
246 --------------------------
248 procedure Process_Partition_Id
(N
: Node_Id
) is
249 Loc
: constant Source_Ptr
:= Sloc
(N
);
253 Get_Pt_Id_Call
: Node_Id
;
254 Prefix_String
: String_Id
;
255 Typ
: constant Entity_Id
:= Etype
(N
);
258 Ety
:= Entity
(Prefix
(N
));
260 -- In case prefix is not a library unit entity, get the entity
263 while (Present
(Scope
(Ety
))
264 and then Scope
(Ety
) /= Standard_Standard
)
265 and not Is_Child_Unit
(Ety
)
270 Nd
:= Enclosing_Lib_Unit_Node
(N
);
272 -- Retrieve the proper function to call.
274 if Is_Remote_Call_Interface
(Ety
) then
275 Get_Pt_Id
:= New_Occurrence_Of
276 (RTE
(RE_Get_Active_Partition_Id
), Loc
);
278 elsif Is_Shared_Passive
(Ety
) then
279 Get_Pt_Id
:= New_Occurrence_Of
280 (RTE
(RE_Get_Passive_Partition_Id
), Loc
);
283 Get_Pt_Id
:= New_Occurrence_Of
284 (RTE
(RE_Get_Local_Partition_Id
), Loc
);
287 -- Get and store the String_Id corresponding to the name of the
288 -- library unit whose Partition_Id is needed
290 Get_Unit_Name_String
(Get_Unit_Name
(Unit_Declaration_Node
(Ety
)));
292 -- Remove seven last character ("(spec)" or " (body)").
293 -- (this is a bit nasty, should have interface for this ???)
295 Name_Len
:= Name_Len
- 7;
298 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
299 Prefix_String
:= End_String
;
301 -- Build the function call which will replace the attribute
303 if Is_Remote_Call_Interface
(Ety
)
304 or else Is_Shared_Passive
(Ety
)
307 Make_Function_Call
(Loc
,
309 Parameter_Associations
=>
310 New_List
(Make_String_Literal
(Loc
, Prefix_String
)));
313 Get_Pt_Id_Call
:= Make_Function_Call
(Loc
, Get_Pt_Id
);
317 -- Replace the attribute node by a conversion of the function call
318 -- to the target type.
320 Rewrite
(N
, Convert_To
(Typ
, Get_Pt_Id_Call
));
321 Analyze_And_Resolve
(N
, Typ
);
323 end Process_Partition_Id
;
325 ----------------------------------
326 -- Process_Remote_AST_Attribute --
327 ----------------------------------
329 procedure Process_Remote_AST_Attribute
331 New_Type
: Entity_Id
)
333 Loc
: constant Source_Ptr
:= Sloc
(N
);
334 Remote_Subp
: Entity_Id
;
335 Tick_Access_Conv_Call
: Node_Id
;
336 Remote_Subp_Decl
: Node_Id
;
338 RS_Pkg_Specif
: Node_Id
;
339 RS_Pkg_E
: Entity_Id
;
340 RAS_Pkg_E
: Entity_Id
;
341 RAS_Type
: Entity_Id
;
345 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
356 elsif Ekind
(New_Type
) = E_Record_Type
then
357 RAS_Type
:= New_Type
;
360 -- If the remote type has not been constructed yet, create
361 -- it and its attributes now.
363 Attribute_Subp
:= TSS
(New_Type
, Name_uRAS_Access
);
365 if No
(Attribute_Subp
) then
366 Add_RAST_Features
(Parent
(New_Type
));
369 RAS_Type
:= Equivalent_Type
(New_Type
);
372 RAS_Name
:= Chars
(RAS_Type
);
373 RAS_Decl
:= Parent
(RAS_Type
);
374 Attribute_Subp
:= TSS
(RAS_Type
, Name_uRAS_Access
);
376 RAS_Pkg_E
:= Defining_Entity
(Parent
(RAS_Decl
));
377 Remote_Subp_Decl
:= Unit_Declaration_Node
(Remote_Subp
);
379 if Nkind
(Remote_Subp_Decl
) = N_Subprogram_Body
then
380 Remote_Subp
:= Corresponding_Spec
(Remote_Subp_Decl
);
381 Remote_Subp_Decl
:= Unit_Declaration_Node
(Remote_Subp
);
384 RS_Pkg_Specif
:= Parent
(Remote_Subp_Decl
);
385 RS_Pkg_E
:= Defining_Entity
(RS_Pkg_Specif
);
387 Subp_Id
:= Get_Subprogram_Id
(Remote_Subp
);
389 if Ekind
(Remote_Subp
) = E_Procedure
390 and then Is_Asynchronous
(Remote_Subp
)
392 Async_E
:= Standard_True
;
394 Async_E
:= Standard_False
;
397 -- Right now, we do not call the Name_uAddress_Resolver subprogram,
398 -- which means that we end up with a Null_Address value in the ras
399 -- field: each dereference of an RAS will go through the PCS, which
400 -- is authorized but potentially not very efficient ???
402 Parameter
:= New_Occurrence_Of
(RTE
(RE_Null_Address
), Loc
);
404 Tick_Access_Conv_Call
:=
405 Make_Function_Call
(Loc
,
406 Name
=> New_Occurrence_Of
(Attribute_Subp
, Loc
),
407 Parameter_Associations
=>
410 Make_String_Literal
(Loc
, Full_Qualified_Name
(RS_Pkg_E
)),
411 Make_Integer_Literal
(Loc
, Subp_Id
),
412 New_Occurrence_Of
(Async_E
, Loc
)));
414 Rewrite
(N
, Tick_Access_Conv_Call
);
415 Analyze_And_Resolve
(N
, RAS_Type
);
417 end Process_Remote_AST_Attribute
;
419 ------------------------------------
420 -- Process_Remote_AST_Declaration --
421 ------------------------------------
423 procedure Process_Remote_AST_Declaration
(N
: Node_Id
) is
424 Loc
: constant Source_Ptr
:= Sloc
(N
);
425 User_Type
: constant Node_Id
:= Defining_Identifier
(N
);
426 Fat_Type
: constant Entity_Id
:=
427 Make_Defining_Identifier
428 (Loc
, Chars
(User_Type
));
429 New_Type_Decl
: Node_Id
;
432 -- We add a record type declaration for the equivalent fat pointer type
435 Make_Full_Type_Declaration
(Loc
,
436 Defining_Identifier
=> Fat_Type
,
438 Make_Record_Definition
(Loc
,
440 Make_Component_List
(Loc
,
441 Component_Items
=> New_List
(
443 Make_Component_Declaration
(Loc
,
444 Defining_Identifier
=>
445 Make_Defining_Identifier
(Loc
,
447 Subtype_Indication
=>
449 (RTE
(RE_Unsigned_64
), Loc
)),
451 Make_Component_Declaration
(Loc
,
452 Defining_Identifier
=>
453 Make_Defining_Identifier
(Loc
,
454 Chars
=> Name_Origin
),
455 Subtype_Indication
=>
460 Make_Component_Declaration
(Loc
,
461 Defining_Identifier
=>
462 Make_Defining_Identifier
(Loc
,
463 Chars
=> Name_Receiver
),
464 Subtype_Indication
=>
466 (RTE
(RE_Unsigned_64
), Loc
)),
468 Make_Component_Declaration
(Loc
,
469 Defining_Identifier
=>
470 Make_Defining_Identifier
(Loc
,
471 Chars
=> Name_Subp_Id
),
472 Subtype_Indication
=>
477 Make_Component_Declaration
(Loc
,
478 Defining_Identifier
=>
479 Make_Defining_Identifier
(Loc
,
480 Chars
=> Name_Async
),
481 Subtype_Indication
=>
486 Insert_After
(N
, New_Type_Decl
);
487 Set_Equivalent_Type
(User_Type
, Fat_Type
);
488 Set_Corresponding_Remote_Type
(Fat_Type
, User_Type
);
490 -- The reason we suppress the initialization procedure is that we know
491 -- that no initialization is required (even if Initialize_Scalars mode
492 -- is active), and there are order of elaboration problems if we do try
493 -- to generate an Init_Proc for this created record type.
495 Set_Suppress_Init_Proc
(Fat_Type
);
497 if Expander_Active
then
498 Add_RAST_Features
(Parent
(User_Type
));
501 end Process_Remote_AST_Declaration
;
503 -----------------------
504 -- RAS_E_Dereference --
505 -----------------------
507 procedure RAS_E_Dereference
(Pref
: Node_Id
) is
508 Loc
: constant Source_Ptr
:= Sloc
(Pref
);
510 New_Type
: constant Entity_Id
:= Etype
(Pref
);
511 RAS
: constant Entity_Id
:=
512 Corresponding_Remote_Type
(New_Type
);
513 RAS_Decl
: constant Node_Id
:= Parent
(RAS
);
514 Explicit_Deref
: constant Node_Id
:= Parent
(Pref
);
515 Deref_Subp_Call
: constant Node_Id
:= Parent
(Explicit_Deref
);
516 Deref_Proc
: Entity_Id
;
520 if Nkind
(Deref_Subp_Call
) = N_Procedure_Call_Statement
then
521 Params
:= Parameter_Associations
(Deref_Subp_Call
);
523 if Present
(Params
) then
524 Prepend
(Pref
, Params
);
526 Params
:= New_List
(Pref
);
529 elsif Nkind
(Deref_Subp_Call
) = N_Indexed_Component
then
531 Params
:= Expressions
(Deref_Subp_Call
);
533 if Present
(Params
) then
534 Prepend
(Pref
, Params
);
536 Params
:= New_List
(Pref
);
540 -- Context is not a call.
545 Deref_Proc
:= TSS
(New_Type
, Name_uRAS_Dereference
);
547 if not Expander_Active
then
550 elsif No
(Deref_Proc
) then
551 Add_RAST_Features
(RAS_Decl
);
552 Deref_Proc
:= TSS
(New_Type
, Name_uRAS_Dereference
);
555 if Ekind
(Deref_Proc
) = E_Function
then
557 Make_Function_Call
(Loc
,
558 Name
=> New_Occurrence_Of
(Deref_Proc
, Loc
),
559 Parameter_Associations
=> Params
);
563 Make_Procedure_Call_Statement
(Loc
,
564 Name
=> New_Occurrence_Of
(Deref_Proc
, Loc
),
565 Parameter_Associations
=> Params
);
568 Rewrite
(Deref_Subp_Call
, Call_Node
);
569 Analyze
(Deref_Subp_Call
);
570 end RAS_E_Dereference
;
572 ------------------------------
573 -- Remote_AST_E_Dereference --
574 ------------------------------
576 function Remote_AST_E_Dereference
(P
: Node_Id
) return Boolean
578 ET
: constant Entity_Id
:= Etype
(P
);
581 -- Perform the changes only on original dereferences, and only if
582 -- we are generating code.
584 if Comes_From_Source
(P
)
585 and then Is_Record_Type
(ET
)
586 and then (Is_Remote_Call_Interface
(ET
)
587 or else Is_Remote_Types
(ET
))
588 and then Present
(Corresponding_Remote_Type
(ET
))
589 and then (Nkind
(Parent
(Parent
(P
))) = N_Procedure_Call_Statement
590 or else Nkind
(Parent
(Parent
(P
))) = N_Indexed_Component
)
591 and then Expander_Active
593 RAS_E_Dereference
(P
);
598 end Remote_AST_E_Dereference
;
600 ------------------------------
601 -- Remote_AST_I_Dereference --
602 ------------------------------
604 function Remote_AST_I_Dereference
(P
: Node_Id
) return Boolean
606 ET
: constant Entity_Id
:= Etype
(P
);
610 if Comes_From_Source
(P
)
611 and then (Is_Remote_Call_Interface
(ET
)
612 or else Is_Remote_Types
(ET
))
613 and then Present
(Corresponding_Remote_Type
(ET
))
614 and then Ekind
(Entity
(P
)) /= E_Function
617 Make_Explicit_Dereference
(Sloc
(P
),
618 Prefix
=> Relocate_Node
(P
));
621 RAS_E_Dereference
(Prefix
(P
));
626 end Remote_AST_I_Dereference
;
628 ---------------------------
629 -- Remote_AST_Null_Value --
630 ---------------------------
632 function Remote_AST_Null_Value
637 Loc
: constant Source_Ptr
:= Sloc
(N
);
638 Target_Type
: Entity_Id
;
641 if not Expander_Active
then
644 elsif Ekind
(Typ
) = E_Access_Subprogram_Type
645 and then (Is_Remote_Call_Interface
(Typ
)
646 or else Is_Remote_Types
(Typ
))
647 and then Comes_From_Source
(N
)
648 and then Expander_Active
650 -- Any null that comes from source and is of the RAS type must
651 -- be expanded, except if expansion is not active (nothing
652 -- gets expanded into the equivalent record type).
654 Target_Type
:= Equivalent_Type
(Typ
);
656 elsif Ekind
(Typ
) = E_Record_Type
657 and then Present
(Corresponding_Remote_Type
(Typ
))
659 -- This is a record type representing a RAS type, this must be
665 -- We do not have to handle this case
673 Expressions
=> New_List
(
674 Make_Integer_Literal
(Loc
, 0), -- Ras
675 Make_Integer_Literal
(Loc
, 0), -- Origin
676 Make_Integer_Literal
(Loc
, 0), -- Receiver
677 Make_Integer_Literal
(Loc
, 0), -- Subp_Id
678 New_Occurrence_Of
(Standard_False
, Loc
)))); -- Asyn
679 Analyze_And_Resolve
(N
, Target_Type
);
681 end Remote_AST_Null_Value
;