2002-04-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / ada / sem_dist.adb
blob08217891fcce2f85d7a762f73c773bcbf3c19d11
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ D I S T --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
11 -- --
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. --
22 -- --
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). --
25 -- --
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;
37 with Opt; use Opt;
38 with Rtsfind; use Rtsfind;
39 with Sem; use Sem;
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
70 begin
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));
82 end if;
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
90 null;
91 elsif Nkind (U) = N_Package_Body then
92 Error_Msg_N
93 ("Specification file expected from command line", U);
94 else
95 Expand_Calling_Stubs_Bodies (Exp);
96 end if;
98 else
100 if Is_Shared_Passive (Spec) then
101 Build_Passive_Partition_Stub (Exp);
102 else
103 Expand_Receiving_Stubs_Bodies (Exp);
104 end if;
106 end if;
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;
117 begin
118 -- Deals properly with child units
120 if Nkind (Ent) = N_Defining_Program_Unit_Name then
121 Ent := Defining_Identifier (Ent);
122 end if;
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));
128 end if;
130 -- Every entity should have a name except some expanded blocks
131 -- don't bother about those.
133 if Chars (Ent) = No_Name then
134 return Parent_Name;
135 end if;
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 ('.'));
143 else
144 Start_String;
145 end if;
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));
152 return End_String;
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;
161 Result : Int := 0;
163 begin
164 pragma Assert
165 (Is_Remote_Call_Interface (Scope (E))
166 and then
167 (Nkind (Parent (E)) = N_Procedure_Specification
168 or else
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)
178 then
179 if Defining_Unit_Name
180 (Specification (Current_Declaration)) = E
181 then
182 return Result;
183 end if;
185 Result := Result + 1;
186 end if;
188 Next (Current_Declaration);
189 end loop;
191 -- Error if we do not find it
193 raise Program_Error;
194 end Get_Subprogram_Id;
196 ------------------------
197 -- Is_All_Remote_Call --
198 ------------------------
200 function Is_All_Remote_Call (N : Node_Id) return Boolean is
201 Par : Node_Id;
203 begin
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)
210 then
211 Par := Parent (Entity (Name (N)));
213 while Present (Par)
214 and then (Nkind (Par) /= N_Package_Specification
215 or else Is_Wrapper_Package (Defining_Entity (Par)))
216 loop
217 Par := Parent (Par);
218 end loop;
220 if Present (Par) then
221 return
222 not Scope_Within_Or_Same (Current_Scope, Defining_Entity (Par));
223 else
224 return False;
225 end if;
226 else
227 return False;
228 end if;
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);
237 begin
238 while Nkind (N) /= N_Package_Specification loop
239 N := Parent (N);
240 end loop;
242 return N;
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);
251 Ety : Entity_Id;
252 Nd : Node_Id;
253 Get_Pt_Id : Node_Id;
254 Get_Pt_Id_Call : Node_Id;
255 Prefix_String : String_Id;
256 Typ : constant Entity_Id := Etype (N);
258 begin
259 Ety := Entity (Prefix (N));
261 -- In case prefix is not a library unit entity, get the entity
262 -- of library unit.
264 while (Present (Scope (Ety))
265 and then Scope (Ety) /= Standard_Standard)
266 and not Is_Child_Unit (Ety)
267 loop
268 Ety := Scope (Ety);
269 end loop;
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);
283 else
284 Get_Pt_Id := New_Occurrence_Of
285 (RTE (RE_Get_Local_Partition_Id), Loc);
286 end if;
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;
298 Start_String;
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)
306 then
307 Get_Pt_Id_Call :=
308 Make_Function_Call (Loc,
309 Name => Get_Pt_Id,
310 Parameter_Associations =>
311 New_List (Make_String_Literal (Loc, Prefix_String)));
313 else
314 Get_Pt_Id_Call := Make_Function_Call (Loc, Get_Pt_Id);
316 end if;
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
331 (N : Node_Id;
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;
338 RAS_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;
343 RAS_Name : Name_Id;
344 Async_E : Entity_Id;
345 Subp_Id : Int;
346 Attribute_Subp : Entity_Id;
347 Parameter : Node_Id;
349 begin
350 -- Check if we have to expand the access attribute
352 Remote_Subp := Entity (Prefix (N));
354 if not Expander_Active then
355 return;
357 elsif Ekind (New_Type) = E_Record_Type then
358 RAS_Type := New_Type;
360 else
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));
368 end if;
370 RAS_Type := Equivalent_Type (New_Type);
371 end if;
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);
383 end if;
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)
392 then
393 Async_E := Standard_True;
394 else
395 Async_E := Standard_False;
396 end if;
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 =>
409 New_List (
410 Parameter,
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;
432 begin
433 -- We add a record type declaration for the equivalent fat pointer type
435 New_Type_Decl :=
436 Make_Full_Type_Declaration (Loc,
437 Defining_Identifier => Fat_Type,
438 Type_Definition =>
439 Make_Record_Definition (Loc,
440 Component_List =>
441 Make_Component_List (Loc,
442 Component_Items => New_List (
444 Make_Component_Declaration (Loc,
445 Defining_Identifier =>
446 Make_Defining_Identifier (Loc,
447 Chars => Name_Ras),
448 Subtype_Indication =>
449 New_Occurrence_Of
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 =>
457 New_Reference_To
458 (Standard_Integer,
459 Loc)),
461 Make_Component_Declaration (Loc,
462 Defining_Identifier =>
463 Make_Defining_Identifier (Loc,
464 Chars => Name_Receiver),
465 Subtype_Indication =>
466 New_Reference_To
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 =>
474 New_Reference_To
475 (Standard_Natural,
476 Loc)),
478 Make_Component_Declaration (Loc,
479 Defining_Identifier =>
480 Make_Defining_Identifier (Loc,
481 Chars => Name_Async),
482 Subtype_Indication =>
483 New_Reference_To
484 (Standard_Boolean,
485 Loc))))));
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));
500 end if;
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);
510 Call_Node : Node_Id;
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;
518 Params : List_Id;
520 begin
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);
526 else
527 Params := New_List (Pref);
528 end if;
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);
536 else
537 Params := New_List (Pref);
538 end if;
540 else
541 -- Context is not a call.
543 return;
544 end if;
546 Deref_Proc := TSS (New_Type, Name_uRAS_Dereference);
548 if not Expander_Active then
549 return;
551 elsif No (Deref_Proc) then
552 Add_RAST_Features (RAS_Decl);
553 Deref_Proc := TSS (New_Type, Name_uRAS_Dereference);
554 end if;
556 if Ekind (Deref_Proc) = E_Function then
557 Call_Node :=
558 Make_Function_Call (Loc,
559 Name => New_Occurrence_Of (Deref_Proc, Loc),
560 Parameter_Associations => Params);
562 else
563 Call_Node :=
564 Make_Procedure_Call_Statement (Loc,
565 Name => New_Occurrence_Of (Deref_Proc, Loc),
566 Parameter_Associations => Params);
567 end if;
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);
581 begin
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
593 then
594 RAS_E_Dereference (P);
595 return True;
596 else
597 return False;
598 end if;
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);
608 Deref : Node_Id;
609 begin
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
616 then
617 Deref :=
618 Make_Explicit_Dereference (Sloc (P),
619 Prefix => Relocate_Node (P));
620 Rewrite (P, Deref);
621 Set_Etype (P, ET);
622 RAS_E_Dereference (Prefix (P));
623 return True;
624 end if;
626 return False;
627 end Remote_AST_I_Dereference;
629 ---------------------------
630 -- Remote_AST_Null_Value --
631 ---------------------------
633 function Remote_AST_Null_Value
634 (N : Node_Id;
635 Typ : Entity_Id)
636 return Boolean
638 Loc : constant Source_Ptr := Sloc (N);
639 Target_Type : Entity_Id;
641 begin
642 if not Expander_Active then
643 return False;
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
650 then
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))
659 then
660 -- This is a record type representing a RAS type, this must be
661 -- expanded.
663 Target_Type := Typ;
665 else
666 -- We do not have to handle this case
668 return False;
670 end if;
672 Rewrite (N,
673 Make_Aggregate (Loc,
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);
681 return True;
682 end Remote_AST_Null_Value;
684 end Sem_Dist;