PR target/16201
[official-gcc.git] / gcc / ada / exp_ch9.adb
blobdbd692dd980debb7cb201fc293ad14a49d328dfe
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 9 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2004, 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 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. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Ch3; use Exp_Ch3;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Dbug; use Exp_Dbug;
36 with Exp_Smem; use Exp_Smem;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Freeze; use Freeze;
40 with Hostparm;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Ch11; use Sem_Ch11;
52 with Sem_Elab; use Sem_Elab;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Sinfo; use Sinfo;
56 with Snames; use Snames;
57 with Stand; use Stand;
58 with Targparm; use Targparm;
59 with Tbuild; use Tbuild;
60 with Types; use Types;
61 with Uintp; use Uintp;
63 package body Exp_Ch9 is
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 function Actual_Index_Expression
70 (Sloc : Source_Ptr;
71 Ent : Entity_Id;
72 Index : Node_Id;
73 Tsk : Entity_Id) return Node_Id;
74 -- Compute the index position for an entry call. Tsk is the target
75 -- task. If the bounds of some entry family depend on discriminants,
76 -- the expression computed by this function uses the discriminants
77 -- of the target task.
79 function Index_Constant_Declaration
80 (N : Node_Id;
81 Index_Id : Entity_Id;
82 Prot : Entity_Id) return List_Id;
83 -- For an entry family and its barrier function, we define a local entity
84 -- that maps the index in the call into the entry index into the object:
86 -- I : constant Index_Type := Index_Type'Val (
87 -- E - <<index of first family member>> +
88 -- Protected_Entry_Index (Index_Type'Pos (Index_Type'First)));
90 procedure Add_Object_Pointer
91 (Decls : List_Id;
92 Pid : Entity_Id;
93 Loc : Source_Ptr);
94 -- Prepend an object pointer declaration to the declaration list
95 -- Decls. This object pointer is initialized to a type conversion
96 -- of the System.Address pointer passed to entry barrier functions
97 -- and entry body procedures.
99 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
100 -- Transform accept statement into a block with added exception handler.
101 -- Used both for simple accept statements and for accept alternatives in
102 -- select statements. Astat is the accept statement.
104 function Build_Barrier_Function
105 (N : Node_Id;
106 Ent : Entity_Id;
107 Pid : Node_Id) return Node_Id;
108 -- Build the function body returning the value of the barrier expression
109 -- for the specified entry body.
111 function Build_Barrier_Function_Specification
112 (Def_Id : Entity_Id;
113 Loc : Source_Ptr) return Node_Id;
114 -- Build a specification for a function implementing
115 -- the protected entry barrier of the specified entry body.
117 function Build_Corresponding_Record
118 (N : Node_Id;
119 Ctyp : Node_Id;
120 Loc : Source_Ptr) return Node_Id;
121 -- Common to tasks and protected types. Copy discriminant specifications,
122 -- build record declaration. N is the type declaration, Ctyp is the
123 -- concurrent entity (task type or protected type).
125 function Build_Entry_Count_Expression
126 (Concurrent_Type : Node_Id;
127 Component_List : List_Id;
128 Loc : Source_Ptr) return Node_Id;
129 -- Compute number of entries for concurrent object. This is a count of
130 -- simple entries, followed by an expression that computes the length
131 -- of the range of each entry family. A single array with that size is
132 -- allocated for each concurrent object of the type.
134 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
135 -- Build the function that translates the entry index in the call
136 -- (which depends on the size of entry families) into an index into the
137 -- Entry_Bodies_Array, to determine the body and barrier function used
138 -- in a protected entry call. A pointer to this function appears in every
139 -- protected object.
141 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
142 -- Build subprogram declaration for previous one
144 function Build_Protected_Entry
145 (N : Node_Id;
146 Ent : Entity_Id;
147 Pid : Node_Id) return Node_Id;
148 -- Build the procedure implementing the statement sequence of
149 -- the specified entry body.
151 function Build_Protected_Entry_Specification
152 (Def_Id : Entity_Id;
153 Ent_Id : Entity_Id;
154 Loc : Source_Ptr) return Node_Id;
155 -- Build a specification for a procedure implementing
156 -- the statement sequence of the specified entry body.
157 -- Add attributes associating it with the entry defining identifier
158 -- Ent_Id.
160 function Build_Protected_Subprogram_Body
161 (N : Node_Id;
162 Pid : Node_Id;
163 N_Op_Spec : Node_Id) return Node_Id;
164 -- This function is used to construct the protected version of a protected
165 -- subprogram. Its statement sequence first defers abortion, then locks
166 -- the associated protected object, and then enters a block that contains
167 -- a call to the unprotected version of the subprogram (for details, see
168 -- Build_Unprotected_Subprogram_Body). This block statement requires
169 -- a cleanup handler that unlocks the object in all cases.
170 -- (see Exp_Ch7.Expand_Cleanup_Actions).
172 function Build_Protected_Spec
173 (N : Node_Id;
174 Obj_Type : Entity_Id;
175 Unprotected : Boolean := False;
176 Ident : Entity_Id) return List_Id;
177 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
178 -- Subprogram_Type. Builds signature of protected subprogram, adding the
179 -- formal that corresponds to the object itself. For an access to protected
180 -- subprogram, there is no object type to specify, so the additional
181 -- parameter has type Address and mode In. An indirect call through such
182 -- a pointer converts the address to a reference to the actual object.
183 -- The object is a limited record and therefore a by_reference type.
185 function Build_Selected_Name
186 (Prefix, Selector : Name_Id;
187 Append_Char : Character := ' ') return Name_Id;
188 -- Build a name in the form of Prefix__Selector, with an optional
189 -- character appended. This is used for internal subprograms generated
190 -- for operations of protected types, including barrier functions.
191 -- For the subprograms generated for entry bodies and entry barriers,
192 -- the generated name includes a sequence number that makes names
193 -- unique in the presence of entry overloading. This is necessary
194 -- because entry body procedures and barrier functions all have the
195 -- same signature.
197 procedure Build_Simple_Entry_Call
198 (N : Node_Id;
199 Concval : Node_Id;
200 Ename : Node_Id;
201 Index : Node_Id);
202 -- Some comments here would be useful ???
204 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
205 -- This routine constructs a specification for the procedure that we will
206 -- build for the task body for task type T. The spec has the form:
208 -- procedure tnameB (_Task : access tnameV);
210 -- where name is the character name taken from the task type entity that
211 -- is passed as the argument to the procedure, and tnameV is the task
212 -- value type that is associated with the task type.
214 function Build_Unprotected_Subprogram_Body
215 (N : Node_Id;
216 Pid : Node_Id) return Node_Id;
217 -- This routine constructs the unprotected version of a protected
218 -- subprogram body, which is contains all of the code in the
219 -- original, unexpanded body. This is the version of the protected
220 -- subprogram that is called from all protected operations on the same
221 -- object, including the protected version of the same subprogram.
223 procedure Collect_Entry_Families
224 (Loc : Source_Ptr;
225 Cdecls : List_Id;
226 Current_Node : in out Node_Id;
227 Conctyp : Entity_Id);
228 -- For each entry family in a concurrent type, create an anonymous array
229 -- type of the right size, and add a component to the corresponding_record.
231 function Family_Offset
232 (Loc : Source_Ptr;
233 Hi : Node_Id;
234 Lo : Node_Id;
235 Ttyp : Entity_Id) return Node_Id;
236 -- Compute (Hi - Lo) for two entry family indices. Hi is the index in
237 -- an accept statement, or the upper bound in the discrete subtype of
238 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
239 -- the concurrent type of the entry.
241 function Family_Size
242 (Loc : Source_Ptr;
243 Hi : Node_Id;
244 Lo : Node_Id;
245 Ttyp : Entity_Id) return Node_Id;
246 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
247 -- a family, and handle properly the superflat case. This is equivalent
248 -- to the use of 'Length on the index type, but must use Family_Offset
249 -- to handle properly the case of bounds that depend on discriminants.
251 procedure Extract_Entry
252 (N : Node_Id;
253 Concval : out Node_Id;
254 Ename : out Node_Id;
255 Index : out Node_Id);
256 -- Given an entry call, returns the associated concurrent object,
257 -- the entry name, and the entry family index.
259 function Find_Task_Or_Protected_Pragma
260 (T : Node_Id;
261 P : Name_Id) return Node_Id;
262 -- Searches the task or protected definition T for the first occurrence
263 -- of the pragma whose name is given by P. The caller has ensured that
264 -- the pragma is present in the task definition. A special case is that
265 -- when P is Name_uPriority, the call will also find Interrupt_Priority.
266 -- ??? Should be implemented with the rep item chain mechanism.
268 procedure Update_Prival_Subtypes (N : Node_Id);
269 -- The actual subtypes of the privals will differ from the type of the
270 -- private declaration in the original protected type, if the protected
271 -- type has discriminants or if the prival has constrained components.
272 -- This is because the privals are generated out of sequence w.r.t. the
273 -- analysis of a protected body. After generating the bodies for protected
274 -- operations, we set correctly the type of all references to privals, by
275 -- means of a recursive tree traversal, which is heavy-handed but
276 -- correct.
278 -----------------------------
279 -- Actual_Index_Expression --
280 -----------------------------
282 function Actual_Index_Expression
283 (Sloc : Source_Ptr;
284 Ent : Entity_Id;
285 Index : Node_Id;
286 Tsk : Entity_Id) return Node_Id
288 Ttyp : constant Entity_Id := Etype (Tsk);
289 Expr : Node_Id;
290 Num : Node_Id;
291 Lo : Node_Id;
292 Hi : Node_Id;
293 Prev : Entity_Id;
294 S : Node_Id;
296 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
297 -- Compute difference between bounds of entry family
299 --------------------------
300 -- Actual_Family_Offset --
301 --------------------------
303 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
305 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
306 -- Replace a reference to a discriminant with a selected component
307 -- denoting the discriminant of the target task.
309 -----------------------------
310 -- Actual_Discriminant_Ref --
311 -----------------------------
313 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
314 Typ : constant Entity_Id := Etype (Bound);
315 B : Node_Id;
317 begin
318 if not Is_Entity_Name (Bound)
319 or else Ekind (Entity (Bound)) /= E_Discriminant
320 then
321 if Nkind (Bound) = N_Attribute_Reference then
322 return Bound;
323 else
324 B := New_Copy_Tree (Bound);
325 end if;
327 else
328 B :=
329 Make_Selected_Component (Sloc,
330 Prefix => New_Copy_Tree (Tsk),
331 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
333 Analyze_And_Resolve (B, Typ);
334 end if;
336 return
337 Make_Attribute_Reference (Sloc,
338 Attribute_Name => Name_Pos,
339 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
340 Expressions => New_List (B));
341 end Actual_Discriminant_Ref;
343 -- Start of processing for Actual_Family_Offset
345 begin
346 return
347 Make_Op_Subtract (Sloc,
348 Left_Opnd => Actual_Discriminant_Ref (Hi),
349 Right_Opnd => Actual_Discriminant_Ref (Lo));
350 end Actual_Family_Offset;
352 -- Start of processing for Actual_Index_Expression
354 begin
355 -- The queues of entries and entry families appear in textual
356 -- order in the associated record. The entry index is computed as
357 -- the sum of the number of queues for all entries that precede the
358 -- designated one, to which is added the index expression, if this
359 -- expression denotes a member of a family.
361 -- The following is a place holder for the count of simple entries
363 Num := Make_Integer_Literal (Sloc, 1);
365 -- We construct an expression which is a series of addition
366 -- operations. See comments in Entry_Index_Expression, which is
367 -- identical in structure.
369 if Present (Index) then
370 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
372 Expr :=
373 Make_Op_Add (Sloc,
374 Left_Opnd => Num,
376 Right_Opnd =>
377 Actual_Family_Offset (
378 Make_Attribute_Reference (Sloc,
379 Attribute_Name => Name_Pos,
380 Prefix => New_Reference_To (Base_Type (S), Sloc),
381 Expressions => New_List (Relocate_Node (Index))),
382 Type_Low_Bound (S)));
383 else
384 Expr := Num;
385 end if;
387 -- Now add lengths of preceding entries and entry families
389 Prev := First_Entity (Ttyp);
391 while Chars (Prev) /= Chars (Ent)
392 or else (Ekind (Prev) /= Ekind (Ent))
393 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
394 loop
395 if Ekind (Prev) = E_Entry then
396 Set_Intval (Num, Intval (Num) + 1);
398 elsif Ekind (Prev) = E_Entry_Family then
399 S :=
400 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
401 Lo := Type_Low_Bound (S);
402 Hi := Type_High_Bound (S);
404 Expr :=
405 Make_Op_Add (Sloc,
406 Left_Opnd => Expr,
407 Right_Opnd =>
408 Make_Op_Add (Sloc,
409 Left_Opnd =>
410 Actual_Family_Offset (Hi, Lo),
411 Right_Opnd =>
412 Make_Integer_Literal (Sloc, 1)));
414 -- Other components are anonymous types to be ignored
416 else
417 null;
418 end if;
420 Next_Entity (Prev);
421 end loop;
423 return Expr;
424 end Actual_Index_Expression;
426 ----------------------------------
427 -- Add_Discriminal_Declarations --
428 ----------------------------------
430 procedure Add_Discriminal_Declarations
431 (Decls : List_Id;
432 Typ : Entity_Id;
433 Name : Name_Id;
434 Loc : Source_Ptr)
436 D : Entity_Id;
438 begin
439 if Has_Discriminants (Typ) then
440 D := First_Discriminant (Typ);
442 while Present (D) loop
444 Prepend_To (Decls,
445 Make_Object_Renaming_Declaration (Loc,
446 Defining_Identifier => Discriminal (D),
447 Subtype_Mark => New_Reference_To (Etype (D), Loc),
448 Name =>
449 Make_Selected_Component (Loc,
450 Prefix => Make_Identifier (Loc, Name),
451 Selector_Name => Make_Identifier (Loc, Chars (D)))));
453 Next_Discriminant (D);
454 end loop;
455 end if;
456 end Add_Discriminal_Declarations;
458 ------------------------
459 -- Add_Object_Pointer --
460 ------------------------
462 procedure Add_Object_Pointer
463 (Decls : List_Id;
464 Pid : Entity_Id;
465 Loc : Source_Ptr)
467 Obj_Ptr : Node_Id;
469 begin
470 -- Prepend the declaration of _object. This must be first in the
471 -- declaration list, since it is used by the discriminal and
472 -- prival declarations.
473 -- ??? An attempt to make this a renaming was unsuccessful.
475 -- type poVP is access poV;
476 -- _object : poVP := poVP!O;
478 Obj_Ptr :=
479 Make_Defining_Identifier (Loc,
480 Chars =>
481 New_External_Name
482 (Chars (Corresponding_Record_Type (Pid)), 'P'));
484 Prepend_To (Decls,
485 Make_Object_Declaration (Loc,
486 Defining_Identifier =>
487 Make_Defining_Identifier (Loc, Name_uObject),
488 Object_Definition => New_Reference_To (Obj_Ptr, Loc),
489 Expression =>
490 Unchecked_Convert_To (Obj_Ptr,
491 Make_Identifier (Loc, Name_uO))));
493 Prepend_To (Decls,
494 Make_Full_Type_Declaration (Loc,
495 Defining_Identifier => Obj_Ptr,
496 Type_Definition => Make_Access_To_Object_Definition (Loc,
497 Subtype_Indication =>
498 New_Reference_To (Corresponding_Record_Type (Pid), Loc))));
499 end Add_Object_Pointer;
501 ------------------------------
502 -- Add_Private_Declarations --
503 ------------------------------
505 procedure Add_Private_Declarations
506 (Decls : List_Id;
507 Typ : Entity_Id;
508 Name : Name_Id;
509 Loc : Source_Ptr)
511 Def : constant Node_Id := Protected_Definition (Parent (Typ));
512 Body_Ent : constant Entity_Id := Corresponding_Body (Parent (Typ));
513 P : Node_Id;
514 Pdef : Entity_Id;
516 begin
517 pragma Assert (Nkind (Def) = N_Protected_Definition);
519 if Present (Private_Declarations (Def)) then
520 P := First (Private_Declarations (Def));
522 while Present (P) loop
523 if Nkind (P) = N_Component_Declaration then
524 Pdef := Defining_Identifier (P);
525 Prepend_To (Decls,
526 Make_Object_Renaming_Declaration (Loc,
527 Defining_Identifier => Prival (Pdef),
528 Subtype_Mark => New_Reference_To (Etype (Pdef), Loc),
529 Name =>
530 Make_Selected_Component (Loc,
531 Prefix => Make_Identifier (Loc, Name),
532 Selector_Name => Make_Identifier (Loc, Chars (Pdef)))));
533 end if;
534 Next (P);
535 end loop;
536 end if;
538 -- One more "prival" for the object itself, with the right protection
539 -- type.
541 declare
542 Protection_Type : RE_Id;
543 begin
544 if Has_Attach_Handler (Typ) then
545 if Restricted_Profile then
546 if Has_Entries (Typ) then
547 Protection_Type := RE_Protection_Entry;
548 else
549 Protection_Type := RE_Protection;
550 end if;
551 else
552 Protection_Type := RE_Static_Interrupt_Protection;
553 end if;
555 elsif Has_Interrupt_Handler (Typ) then
556 Protection_Type := RE_Dynamic_Interrupt_Protection;
558 elsif Has_Entries (Typ) then
559 if Abort_Allowed
560 or else Restriction_Active (No_Entry_Queue) = False
561 or else Number_Entries (Typ) > 1
562 then
563 Protection_Type := RE_Protection_Entries;
564 else
565 Protection_Type := RE_Protection_Entry;
566 end if;
568 else
569 Protection_Type := RE_Protection;
570 end if;
572 Prepend_To (Decls,
573 Make_Object_Renaming_Declaration (Loc,
574 Defining_Identifier => Object_Ref (Body_Ent),
575 Subtype_Mark => New_Reference_To (RTE (Protection_Type), Loc),
576 Name =>
577 Make_Selected_Component (Loc,
578 Prefix => Make_Identifier (Loc, Name),
579 Selector_Name => Make_Identifier (Loc, Name_uObject))));
580 end;
581 end Add_Private_Declarations;
583 -----------------------
584 -- Build_Accept_Body --
585 -----------------------
587 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
588 Loc : constant Source_Ptr := Sloc (Astat);
589 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
590 New_S : Node_Id;
591 Hand : Node_Id;
592 Call : Node_Id;
593 Ohandle : Node_Id;
595 begin
596 -- At the end of the statement sequence, Complete_Rendezvous is called.
597 -- A label skipping the Complete_Rendezvous, and all other
598 -- accept processing, has already been added for the expansion
599 -- of requeue statements.
601 Call := Build_Runtime_Call (Loc, RE_Complete_Rendezvous);
602 Insert_Before (Last (Statements (Stats)), Call);
603 Analyze (Call);
605 -- If exception handlers are present, then append Complete_Rendezvous
606 -- calls to the handlers, and construct the required outer block.
608 if Present (Exception_Handlers (Stats)) then
609 Hand := First (Exception_Handlers (Stats));
611 while Present (Hand) loop
612 Call := Build_Runtime_Call (Loc, RE_Complete_Rendezvous);
613 Append (Call, Statements (Hand));
614 Analyze (Call);
615 Next (Hand);
616 end loop;
618 New_S :=
619 Make_Handled_Sequence_Of_Statements (Loc,
620 Statements => New_List (
621 Make_Block_Statement (Loc,
622 Handled_Statement_Sequence => Stats)));
624 else
625 New_S := Stats;
626 end if;
628 -- At this stage we know that the new statement sequence does not
629 -- have an exception handler part, so we supply one to call
630 -- Exceptional_Complete_Rendezvous. This handler is
632 -- when all others =>
633 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
635 -- We handle Abort_Signal to make sure that we properly catch the abort
636 -- case and wake up the caller.
638 Ohandle := Make_Others_Choice (Loc);
639 Set_All_Others (Ohandle);
641 Set_Exception_Handlers (New_S,
642 New_List (
643 Make_Exception_Handler (Loc,
644 Exception_Choices => New_List (Ohandle),
646 Statements => New_List (
647 Make_Procedure_Call_Statement (Loc,
648 Name => New_Reference_To (
649 RTE (RE_Exceptional_Complete_Rendezvous), Loc),
650 Parameter_Associations => New_List (
651 Make_Function_Call (Loc,
652 Name => New_Reference_To (
653 RTE (RE_Get_GNAT_Exception), Loc))))))));
655 Set_Parent (New_S, Astat); -- temp parent for Analyze call
656 Analyze_Exception_Handlers (Exception_Handlers (New_S));
657 Expand_Exception_Handlers (New_S);
659 -- Exceptional_Complete_Rendezvous must be called with abort
660 -- still deferred, which is the case for a "when all others" handler.
662 return New_S;
663 end Build_Accept_Body;
665 -----------------------------------
666 -- Build_Activation_Chain_Entity --
667 -----------------------------------
669 procedure Build_Activation_Chain_Entity (N : Node_Id) is
670 P : Node_Id;
671 B : Node_Id;
672 Decls : List_Id;
674 begin
675 -- Loop to find enclosing construct containing activation chain variable
677 P := Parent (N);
679 while Nkind (P) /= N_Subprogram_Body
680 and then Nkind (P) /= N_Package_Declaration
681 and then Nkind (P) /= N_Package_Body
682 and then Nkind (P) /= N_Block_Statement
683 and then Nkind (P) /= N_Task_Body
684 loop
685 P := Parent (P);
686 end loop;
688 -- If we are in a package body, the activation chain variable is
689 -- allocated in the corresponding spec. First, we save the package
690 -- body node because we enter the new entity in its Declarations list.
692 B := P;
694 if Nkind (P) = N_Package_Body then
695 P := Unit_Declaration_Node (Corresponding_Spec (P));
696 Decls := Declarations (B);
698 elsif Nkind (P) = N_Package_Declaration then
699 Decls := Visible_Declarations (Specification (B));
701 else
702 Decls := Declarations (B);
703 end if;
705 -- If activation chain entity not already declared, declare it
707 if No (Activation_Chain_Entity (P)) then
708 Set_Activation_Chain_Entity
709 (P, Make_Defining_Identifier (Sloc (N), Name_uChain));
711 Prepend_To (Decls,
712 Make_Object_Declaration (Sloc (P),
713 Defining_Identifier => Activation_Chain_Entity (P),
714 Aliased_Present => True,
715 Object_Definition =>
716 New_Reference_To (RTE (RE_Activation_Chain), Sloc (P))));
718 Analyze (First (Decls));
719 end if;
720 end Build_Activation_Chain_Entity;
722 ----------------------------
723 -- Build_Barrier_Function --
724 ----------------------------
726 function Build_Barrier_Function
727 (N : Node_Id;
728 Ent : Entity_Id;
729 Pid : Node_Id) return Node_Id
731 Loc : constant Source_Ptr := Sloc (N);
732 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
733 Index_Spec : constant Node_Id := Entry_Index_Specification
734 (Ent_Formals);
735 Op_Decls : constant List_Id := New_List;
736 Bdef : Entity_Id;
737 Bspec : Node_Id;
739 begin
740 Bdef :=
741 Make_Defining_Identifier (Loc, Chars (Barrier_Function (Ent)));
742 Bspec := Build_Barrier_Function_Specification (Bdef, Loc);
744 -- <object pointer declaration>
745 -- <discriminant renamings>
746 -- <private object renamings>
747 -- Add discriminal and private renamings. These names have
748 -- already been used to expand references to discriminants
749 -- and private data.
751 Add_Discriminal_Declarations (Op_Decls, Pid, Name_uObject, Loc);
752 Add_Private_Declarations (Op_Decls, Pid, Name_uObject, Loc);
753 Add_Object_Pointer (Op_Decls, Pid, Loc);
755 -- If this is the barrier for an entry family, the entry index is
756 -- visible in the body of the barrier. Create a local variable that
757 -- converts the entry index (which is the last formal of the barrier
758 -- function) into the appropriate offset into the entry array. The
759 -- entry index constant must be set, as for the entry body, so that
760 -- local references to the entry index are correctly replaced with
761 -- the local variable. This parallels what is done for entry bodies.
763 if Present (Index_Spec) then
764 declare
765 Index_Id : constant Entity_Id := Defining_Identifier (Index_Spec);
766 Index_Con : constant Entity_Id :=
767 Make_Defining_Identifier (Loc,
768 Chars => New_Internal_Name ('J'));
770 begin
771 Set_Entry_Index_Constant (Index_Id, Index_Con);
772 Append_List_To (Op_Decls,
773 Index_Constant_Declaration (N, Index_Id, Pid));
774 end;
775 end if;
777 -- Note: the condition in the barrier function needs to be properly
778 -- processed for the C/Fortran boolean possibility, but this happens
779 -- automatically since the return statement does this normalization.
781 return
782 Make_Subprogram_Body (Loc,
783 Specification => Bspec,
784 Declarations => Op_Decls,
785 Handled_Statement_Sequence =>
786 Make_Handled_Sequence_Of_Statements (Loc,
787 Statements => New_List (
788 Make_Return_Statement (Loc,
789 Expression => Condition (Ent_Formals)))));
790 end Build_Barrier_Function;
792 ------------------------------------------
793 -- Build_Barrier_Function_Specification --
794 ------------------------------------------
796 function Build_Barrier_Function_Specification
797 (Def_Id : Entity_Id;
798 Loc : Source_Ptr) return Node_Id
800 begin
801 Set_Needs_Debug_Info (Def_Id);
802 return Make_Function_Specification (Loc,
803 Defining_Unit_Name => Def_Id,
804 Parameter_Specifications => New_List (
805 Make_Parameter_Specification (Loc,
806 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
807 Parameter_Type =>
808 New_Reference_To (RTE (RE_Address), Loc)),
810 Make_Parameter_Specification (Loc,
811 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uE),
812 Parameter_Type =>
813 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
815 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc));
816 end Build_Barrier_Function_Specification;
818 --------------------------
819 -- Build_Call_With_Task --
820 --------------------------
822 function Build_Call_With_Task
823 (N : Node_Id;
824 E : Entity_Id) return Node_Id
826 Loc : constant Source_Ptr := Sloc (N);
828 begin
829 return
830 Make_Function_Call (Loc,
831 Name => New_Reference_To (E, Loc),
832 Parameter_Associations => New_List (Concurrent_Ref (N)));
833 end Build_Call_With_Task;
835 --------------------------------
836 -- Build_Corresponding_Record --
837 --------------------------------
839 function Build_Corresponding_Record
840 (N : Node_Id;
841 Ctyp : Entity_Id;
842 Loc : Source_Ptr) return Node_Id
844 Rec_Ent : constant Entity_Id :=
845 Make_Defining_Identifier
846 (Loc, New_External_Name (Chars (Ctyp), 'V'));
847 Disc : Entity_Id;
848 Dlist : List_Id;
849 New_Disc : Entity_Id;
850 Cdecls : List_Id;
852 begin
853 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
854 Set_Ekind (Rec_Ent, E_Record_Type);
855 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
856 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
857 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
858 Set_Stored_Constraint (Rec_Ent, No_Elist);
859 Cdecls := New_List;
861 -- Use discriminals to create list of discriminants for record, and
862 -- create new discriminals for use in default expressions, etc. It is
863 -- worth noting that a task discriminant gives rise to 5 entities;
865 -- a) The original discriminant.
866 -- b) The discriminal for use in the task.
867 -- c) The discriminant of the corresponding record.
868 -- d) The discriminal for the init proc of the corresponding record.
869 -- e) The local variable that renames the discriminant in the procedure
870 -- for the task body.
872 -- In fact the discriminals b) are used in the renaming declarations
873 -- for e). See details in einfo (Handling of Discriminants).
875 if Present (Discriminant_Specifications (N)) then
876 Dlist := New_List;
877 Disc := First_Discriminant (Ctyp);
879 while Present (Disc) loop
880 New_Disc := CR_Discriminant (Disc);
882 Append_To (Dlist,
883 Make_Discriminant_Specification (Loc,
884 Defining_Identifier => New_Disc,
885 Discriminant_Type =>
886 New_Occurrence_Of (Etype (Disc), Loc),
887 Expression =>
888 New_Copy (Discriminant_Default_Value (Disc))));
890 Next_Discriminant (Disc);
891 end loop;
893 else
894 Dlist := No_List;
895 end if;
897 -- Now we can construct the record type declaration. Note that this
898 -- record is limited, reflecting the underlying limitedness of the
899 -- task or protected object that it represents, and ensuring for
900 -- example that it is properly passed by reference.
902 return
903 Make_Full_Type_Declaration (Loc,
904 Defining_Identifier => Rec_Ent,
905 Discriminant_Specifications => Dlist,
906 Type_Definition =>
907 Make_Record_Definition (Loc,
908 Component_List =>
909 Make_Component_List (Loc,
910 Component_Items => Cdecls),
911 Limited_Present => True));
912 end Build_Corresponding_Record;
914 ----------------------------------
915 -- Build_Entry_Count_Expression --
916 ----------------------------------
918 function Build_Entry_Count_Expression
919 (Concurrent_Type : Node_Id;
920 Component_List : List_Id;
921 Loc : Source_Ptr) return Node_Id
923 Eindx : Nat;
924 Ent : Entity_Id;
925 Ecount : Node_Id;
926 Comp : Node_Id;
927 Lo : Node_Id;
928 Hi : Node_Id;
929 Typ : Entity_Id;
931 begin
932 Ent := First_Entity (Concurrent_Type);
933 Eindx := 0;
935 -- Count number of non-family entries
937 while Present (Ent) loop
938 if Ekind (Ent) = E_Entry then
939 Eindx := Eindx + 1;
940 end if;
942 Next_Entity (Ent);
943 end loop;
945 Ecount := Make_Integer_Literal (Loc, Eindx);
947 -- Loop through entry families building the addition nodes
949 Ent := First_Entity (Concurrent_Type);
950 Comp := First (Component_List);
952 while Present (Ent) loop
953 if Ekind (Ent) = E_Entry_Family then
954 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
955 Next (Comp);
956 end loop;
958 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
959 Hi := Type_High_Bound (Typ);
960 Lo := Type_Low_Bound (Typ);
962 Ecount :=
963 Make_Op_Add (Loc,
964 Left_Opnd => Ecount,
965 Right_Opnd => Family_Size (Loc, Hi, Lo, Concurrent_Type));
966 end if;
968 Next_Entity (Ent);
969 end loop;
971 return Ecount;
972 end Build_Entry_Count_Expression;
974 ---------------------------
975 -- Build_Find_Body_Index --
976 ---------------------------
978 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
979 Loc : constant Source_Ptr := Sloc (Typ);
980 Ent : Entity_Id;
981 E_Typ : Entity_Id;
982 Has_F : Boolean := False;
983 Index : Nat;
984 If_St : Node_Id := Empty;
985 Lo : Node_Id;
986 Hi : Node_Id;
987 Decls : List_Id := New_List;
988 Ret : Node_Id;
989 Spec : Node_Id;
990 Siz : Node_Id := Empty;
992 procedure Add_If_Clause (Expr : Node_Id);
993 -- Add test for range of current entry
995 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
996 -- If a bound of an entry is given by a discriminant, retrieve the
997 -- actual value of the discriminant from the enclosing object.
999 -------------------
1000 -- Add_If_Clause --
1001 -------------------
1003 procedure Add_If_Clause (Expr : Node_Id) is
1004 Cond : Node_Id;
1005 Stats : constant List_Id :=
1006 New_List (
1007 Make_Return_Statement (Loc,
1008 Expression => Make_Integer_Literal (Loc, Index + 1)));
1010 begin
1011 -- Index for current entry body
1013 Index := Index + 1;
1015 -- Compute total length of entry queues so far
1017 if No (Siz) then
1018 Siz := Expr;
1019 else
1020 Siz :=
1021 Make_Op_Add (Loc,
1022 Left_Opnd => Siz,
1023 Right_Opnd => Expr);
1024 end if;
1026 Cond :=
1027 Make_Op_Le (Loc,
1028 Left_Opnd => Make_Identifier (Loc, Name_uE),
1029 Right_Opnd => Siz);
1031 -- Map entry queue indices in the range of the current family
1032 -- into the current index, that designates the entry body.
1034 if No (If_St) then
1035 If_St :=
1036 Make_Implicit_If_Statement (Typ,
1037 Condition => Cond,
1038 Then_Statements => Stats,
1039 Elsif_Parts => New_List);
1041 Ret := If_St;
1043 else
1044 Append (
1045 Make_Elsif_Part (Loc,
1046 Condition => Cond,
1047 Then_Statements => Stats),
1048 Elsif_Parts (If_St));
1049 end if;
1050 end Add_If_Clause;
1052 ------------------------------
1053 -- Convert_Discriminant_Ref --
1054 ------------------------------
1056 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
1057 B : Node_Id;
1059 begin
1060 if Is_Entity_Name (Bound)
1061 and then Ekind (Entity (Bound)) = E_Discriminant
1062 then
1063 B :=
1064 Make_Selected_Component (Loc,
1065 Prefix =>
1066 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
1067 Make_Explicit_Dereference (Loc,
1068 Make_Identifier (Loc, Name_uObject))),
1069 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
1070 Set_Etype (B, Etype (Entity (Bound)));
1071 else
1072 B := New_Copy_Tree (Bound);
1073 end if;
1075 return B;
1076 end Convert_Discriminant_Ref;
1078 -- Start of processing for Build_Find_Body_Index
1080 begin
1081 Spec := Build_Find_Body_Index_Spec (Typ);
1083 Ent := First_Entity (Typ);
1085 while Present (Ent) loop
1087 if Ekind (Ent) = E_Entry_Family then
1088 Has_F := True;
1089 exit;
1090 end if;
1092 Next_Entity (Ent);
1093 end loop;
1095 if not Has_F then
1097 -- If the protected type has no entry families, there is a one-one
1098 -- correspondence between entry queue and entry body.
1100 Ret :=
1101 Make_Return_Statement (Loc,
1102 Expression => Make_Identifier (Loc, Name_uE));
1104 else
1105 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
1106 -- the following:
1108 -- if E <= l1 then return 1;
1109 -- elsif E <= l1 + l2 then return 2;
1110 -- ...
1112 Index := 0;
1113 Siz := Empty;
1114 Ent := First_Entity (Typ);
1116 Add_Object_Pointer (Decls, Typ, Loc);
1118 while Present (Ent) loop
1120 if Ekind (Ent) = E_Entry then
1121 Add_If_Clause (Make_Integer_Literal (Loc, 1));
1123 elsif Ekind (Ent) = E_Entry_Family then
1125 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1126 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
1127 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
1128 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ));
1129 end if;
1131 Next_Entity (Ent);
1132 end loop;
1134 if Index = 1 then
1135 Decls := New_List;
1136 Ret :=
1137 Make_Return_Statement (Loc,
1138 Expression => Make_Integer_Literal (Loc, 1));
1140 elsif Nkind (Ret) = N_If_Statement then
1142 -- Ranges are in increasing order, so last one doesn't need a
1143 -- guard.
1145 declare
1146 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
1148 begin
1149 Remove (Nod);
1150 Set_Else_Statements (Ret, Then_Statements (Nod));
1151 end;
1152 end if;
1153 end if;
1155 return
1156 Make_Subprogram_Body (Loc,
1157 Specification => Spec,
1158 Declarations => Decls,
1159 Handled_Statement_Sequence =>
1160 Make_Handled_Sequence_Of_Statements (Loc,
1161 Statements => New_List (Ret)));
1162 end Build_Find_Body_Index;
1164 --------------------------------
1165 -- Build_Find_Body_Index_Spec --
1166 --------------------------------
1168 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
1169 Loc : constant Source_Ptr := Sloc (Typ);
1170 Id : constant Entity_Id :=
1171 Make_Defining_Identifier (Loc,
1172 Chars => New_External_Name (Chars (Typ), 'F'));
1173 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
1174 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
1176 begin
1177 return
1178 Make_Function_Specification (Loc,
1179 Defining_Unit_Name => Id,
1180 Parameter_Specifications => New_List (
1181 Make_Parameter_Specification (Loc,
1182 Defining_Identifier => Parm1,
1183 Parameter_Type =>
1184 New_Reference_To (RTE (RE_Address), Loc)),
1186 Make_Parameter_Specification (Loc,
1187 Defining_Identifier => Parm2,
1188 Parameter_Type =>
1189 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1190 Subtype_Mark => New_Occurrence_Of (
1191 RTE (RE_Protected_Entry_Index), Loc));
1192 end Build_Find_Body_Index_Spec;
1194 -------------------------
1195 -- Build_Master_Entity --
1196 -------------------------
1198 procedure Build_Master_Entity (E : Entity_Id) is
1199 Loc : constant Source_Ptr := Sloc (E);
1200 P : Node_Id;
1201 Decl : Node_Id;
1202 S : Entity_Id;
1204 begin
1205 S := Scope (E);
1207 -- Ada 2005 (AI-287): Do not set/get the has_master_entity reminder
1208 -- in internal scopes. Required for nested limited aggregates.
1210 if Ada_Version >= Ada_05 then
1211 while Is_Internal (S) loop
1212 S := Scope (S);
1213 end loop;
1214 end if;
1216 -- Nothing to do if we already built a master entity for this scope
1217 -- or if there is no task hierarchy.
1219 if Has_Master_Entity (S)
1220 or else Restriction_Active (No_Task_Hierarchy)
1221 then
1222 return;
1223 end if;
1225 -- Otherwise first build the master entity
1226 -- _Master : constant Master_Id := Current_Master.all;
1227 -- and insert it just before the current declaration
1229 Decl :=
1230 Make_Object_Declaration (Loc,
1231 Defining_Identifier =>
1232 Make_Defining_Identifier (Loc, Name_uMaster),
1233 Constant_Present => True,
1234 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
1235 Expression =>
1236 Make_Explicit_Dereference (Loc,
1237 New_Reference_To (RTE (RE_Current_Master), Loc)));
1239 P := Parent (E);
1240 Insert_Before (P, Decl);
1241 Analyze (Decl);
1243 -- Ada 2005 (AI-287): Set the has_master_entity reminder in the
1244 -- non-internal scope selected above.
1246 if Ada_Version >= Ada_05 then
1247 Set_Has_Master_Entity (S);
1248 else
1249 Set_Has_Master_Entity (Scope (E));
1250 end if;
1252 -- Now mark the containing scope as a task master
1254 while Nkind (P) /= N_Compilation_Unit loop
1255 P := Parent (P);
1257 -- If we fall off the top, we are at the outer level, and the
1258 -- environment task is our effective master, so nothing to mark.
1260 if Nkind (P) = N_Task_Body
1261 or else Nkind (P) = N_Block_Statement
1262 or else Nkind (P) = N_Subprogram_Body
1263 then
1264 Set_Is_Task_Master (P, True);
1265 return;
1267 elsif Nkind (Parent (P)) = N_Subunit then
1268 P := Corresponding_Stub (Parent (P));
1269 end if;
1270 end loop;
1271 end Build_Master_Entity;
1273 ---------------------------
1274 -- Build_Protected_Entry --
1275 ---------------------------
1277 function Build_Protected_Entry
1278 (N : Node_Id;
1279 Ent : Entity_Id;
1280 Pid : Node_Id) return Node_Id
1282 Loc : constant Source_Ptr := Sloc (N);
1283 Op_Decls : constant List_Id := New_List;
1284 Edef : Entity_Id;
1285 Espec : Node_Id;
1286 Op_Stats : List_Id;
1287 Ohandle : Node_Id;
1288 Complete : Node_Id;
1290 begin
1291 Edef :=
1292 Make_Defining_Identifier (Loc,
1293 Chars => Chars (Protected_Body_Subprogram (Ent)));
1294 Espec := Build_Protected_Entry_Specification (Edef, Empty, Loc);
1296 -- <object pointer declaration>
1297 -- Add object pointer declaration. This is needed by the
1298 -- discriminal and prival renamings, which should already
1299 -- have been inserted into the declaration list.
1301 Add_Object_Pointer (Op_Decls, Pid, Loc);
1303 if Abort_Allowed
1304 or else Restriction_Active (No_Entry_Queue) = False
1305 or else Number_Entries (Pid) > 1
1306 then
1307 Complete := New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
1308 else
1309 Complete :=
1310 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
1311 end if;
1313 Op_Stats := New_List (
1314 Make_Block_Statement (Loc,
1315 Declarations => Declarations (N),
1316 Handled_Statement_Sequence =>
1317 Handled_Statement_Sequence (N)),
1319 Make_Procedure_Call_Statement (Loc,
1320 Name => Complete,
1321 Parameter_Associations => New_List (
1322 Make_Attribute_Reference (Loc,
1323 Prefix =>
1324 Make_Selected_Component (Loc,
1325 Prefix =>
1326 Make_Identifier (Loc, Name_uObject),
1328 Selector_Name =>
1329 Make_Identifier (Loc, Name_uObject)),
1330 Attribute_Name => Name_Unchecked_Access))));
1332 if Restriction_Active (No_Exception_Handlers) then
1333 return
1334 Make_Subprogram_Body (Loc,
1335 Specification => Espec,
1336 Declarations => Op_Decls,
1337 Handled_Statement_Sequence =>
1338 Make_Handled_Sequence_Of_Statements (Loc, Op_Stats));
1340 else
1341 Ohandle := Make_Others_Choice (Loc);
1342 Set_All_Others (Ohandle);
1344 if Abort_Allowed
1345 or else Restriction_Active (No_Entry_Queue) = False
1346 or else Number_Entries (Pid) > 1
1347 then
1348 Complete :=
1349 New_Reference_To (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
1351 else
1352 Complete := New_Reference_To (
1353 RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
1354 end if;
1356 return
1357 Make_Subprogram_Body (Loc,
1358 Specification => Espec,
1359 Declarations => Op_Decls,
1360 Handled_Statement_Sequence =>
1361 Make_Handled_Sequence_Of_Statements (Loc,
1362 Statements => Op_Stats,
1363 Exception_Handlers => New_List (
1364 Make_Exception_Handler (Loc,
1365 Exception_Choices => New_List (Ohandle),
1367 Statements => New_List (
1368 Make_Procedure_Call_Statement (Loc,
1369 Name => Complete,
1370 Parameter_Associations => New_List (
1371 Make_Attribute_Reference (Loc,
1372 Prefix =>
1373 Make_Selected_Component (Loc,
1374 Prefix =>
1375 Make_Identifier (Loc, Name_uObject),
1376 Selector_Name =>
1377 Make_Identifier (Loc, Name_uObject)),
1378 Attribute_Name => Name_Unchecked_Access),
1380 Make_Function_Call (Loc,
1381 Name => New_Reference_To (
1382 RTE (RE_Get_GNAT_Exception), Loc)))))))));
1383 end if;
1384 end Build_Protected_Entry;
1386 -----------------------------------------
1387 -- Build_Protected_Entry_Specification --
1388 -----------------------------------------
1390 function Build_Protected_Entry_Specification
1391 (Def_Id : Entity_Id;
1392 Ent_Id : Entity_Id;
1393 Loc : Source_Ptr) return Node_Id
1395 P : Entity_Id;
1397 begin
1398 Set_Needs_Debug_Info (Def_Id);
1399 P := Make_Defining_Identifier (Loc, Name_uP);
1401 if Present (Ent_Id) then
1402 Append_Elmt (P, Accept_Address (Ent_Id));
1403 end if;
1405 return Make_Procedure_Specification (Loc,
1406 Defining_Unit_Name => Def_Id,
1407 Parameter_Specifications => New_List (
1408 Make_Parameter_Specification (Loc,
1409 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
1410 Parameter_Type =>
1411 New_Reference_To (RTE (RE_Address), Loc)),
1413 Make_Parameter_Specification (Loc,
1414 Defining_Identifier => P,
1415 Parameter_Type =>
1416 New_Reference_To (RTE (RE_Address), Loc)),
1418 Make_Parameter_Specification (Loc,
1419 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uE),
1420 Parameter_Type =>
1421 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
1422 end Build_Protected_Entry_Specification;
1424 --------------------------
1425 -- Build_Protected_Spec --
1426 --------------------------
1428 function Build_Protected_Spec
1429 (N : Node_Id;
1430 Obj_Type : Entity_Id;
1431 Unprotected : Boolean := False;
1432 Ident : Entity_Id) return List_Id
1434 Loc : constant Source_Ptr := Sloc (N);
1435 Formal : Entity_Id;
1436 New_Plist : List_Id;
1437 New_Param : Node_Id;
1439 begin
1440 New_Plist := New_List;
1441 Formal := First_Formal (Ident);
1443 while Present (Formal) loop
1444 New_Param :=
1445 Make_Parameter_Specification (Loc,
1446 Defining_Identifier =>
1447 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
1448 In_Present => In_Present (Parent (Formal)),
1449 Out_Present => Out_Present (Parent (Formal)),
1450 Parameter_Type =>
1451 New_Reference_To (Etype (Formal), Loc));
1453 if Unprotected then
1454 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
1455 end if;
1457 Append (New_Param, New_Plist);
1458 Next_Formal (Formal);
1459 end loop;
1461 -- If the subprogram is a procedure and the context is not an access
1462 -- to protected subprogram, the parameter is in-out. Otherwise it is
1463 -- an in parameter.
1465 Prepend_To (New_Plist,
1466 Make_Parameter_Specification (Loc,
1467 Defining_Identifier =>
1468 Make_Defining_Identifier (Loc, Name_uObject),
1469 In_Present => True,
1470 Out_Present =>
1471 (Etype (Ident) = Standard_Void_Type
1472 and then not Is_RTE (Obj_Type, RE_Address)),
1473 Parameter_Type => New_Reference_To (Obj_Type, Loc)));
1475 return New_Plist;
1476 end Build_Protected_Spec;
1478 ---------------------------------------
1479 -- Build_Protected_Sub_Specification --
1480 ---------------------------------------
1482 function Build_Protected_Sub_Specification
1483 (N : Node_Id;
1484 Prottyp : Entity_Id;
1485 Unprotected : Boolean := False) return Node_Id
1487 Loc : constant Source_Ptr := Sloc (N);
1488 Decl : Node_Id;
1489 Protnm : constant Name_Id := Chars (Prottyp);
1490 Ident : Entity_Id;
1491 Nam : Name_Id;
1492 New_Id : Entity_Id;
1493 New_Plist : List_Id;
1494 Append_Char : Character;
1495 New_Spec : Node_Id;
1497 begin
1498 if Ekind
1499 (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
1500 then
1501 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
1502 else
1503 Decl := N;
1504 end if;
1506 Ident := Defining_Unit_Name (Specification (Decl));
1507 Nam := Chars (Ident);
1509 New_Plist := Build_Protected_Spec
1510 (Decl, Corresponding_Record_Type (Prottyp),
1511 Unprotected, Ident);
1513 if Unprotected then
1514 Append_Char := 'N';
1515 else
1516 Append_Char := 'P';
1517 end if;
1519 New_Id :=
1520 Make_Defining_Identifier (Loc,
1521 Chars => Build_Selected_Name (Protnm, Nam, Append_Char));
1523 -- The unprotected operation carries the user code, and debugging
1524 -- information must be generated for it, even though this spec does
1525 -- not come from source. It is also convenient to allow gdb to step
1526 -- into the protected operation, even though it only contains lock/
1527 -- unlock calls.
1529 Set_Needs_Debug_Info (New_Id);
1531 if Nkind (Specification (Decl)) = N_Procedure_Specification then
1532 return
1533 Make_Procedure_Specification (Loc,
1534 Defining_Unit_Name => New_Id,
1535 Parameter_Specifications => New_Plist);
1537 else
1538 New_Spec :=
1539 Make_Function_Specification (Loc,
1540 Defining_Unit_Name => New_Id,
1541 Parameter_Specifications => New_Plist,
1542 Subtype_Mark => New_Copy (Subtype_Mark (Specification (Decl))));
1543 Set_Return_Present (Defining_Unit_Name (New_Spec));
1544 return New_Spec;
1545 end if;
1546 end Build_Protected_Sub_Specification;
1548 -------------------------------------
1549 -- Build_Protected_Subprogram_Body --
1550 -------------------------------------
1552 function Build_Protected_Subprogram_Body
1553 (N : Node_Id;
1554 Pid : Node_Id;
1555 N_Op_Spec : Node_Id) return Node_Id
1557 Loc : constant Source_Ptr := Sloc (N);
1558 Op_Spec : Node_Id;
1559 P_Op_Spec : Node_Id;
1560 Uactuals : List_Id;
1561 Pformal : Node_Id;
1562 Unprot_Call : Node_Id;
1563 Sub_Body : Node_Id;
1564 Lock_Name : Node_Id;
1565 Lock_Stmt : Node_Id;
1566 Service_Name : Node_Id;
1567 R : Node_Id;
1568 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
1569 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
1570 Stmts : List_Id;
1571 Object_Parm : Node_Id;
1572 Exc_Safe : Boolean;
1574 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
1575 -- Tell whether a given subprogram cannot raise an exception
1577 -----------------------
1578 -- Is_Exception_Safe --
1579 -----------------------
1581 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
1583 function Has_Side_Effect (N : Node_Id) return Boolean;
1584 -- Return True whenever encountering a subprogram call or a
1585 -- raise statement of any kind in the sequence of statements N
1587 ---------------------
1588 -- Has_Side_Effect --
1589 ---------------------
1591 -- What is this doing buried two levels down in exp_ch9. It
1592 -- seems like a generally useful function, and indeed there
1593 -- may be code duplication going on here ???
1595 function Has_Side_Effect (N : Node_Id) return Boolean is
1596 Stmt : Node_Id := N;
1597 Expr : Node_Id;
1599 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
1600 -- Indicate whether N is a subprogram call or a raise statement
1602 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
1603 begin
1604 return Nkind (N) = N_Procedure_Call_Statement
1605 or else Nkind (N) = N_Function_Call
1606 or else Nkind (N) = N_Raise_Statement
1607 or else Nkind (N) = N_Raise_Constraint_Error
1608 or else Nkind (N) = N_Raise_Program_Error
1609 or else Nkind (N) = N_Raise_Storage_Error;
1610 end Is_Call_Or_Raise;
1612 -- Start of processing for Has_Side_Effect
1614 begin
1615 while Present (Stmt) loop
1616 if Is_Call_Or_Raise (Stmt) then
1617 return True;
1618 end if;
1620 -- An object declaration can also contain a function call
1621 -- or a raise statement
1623 if Nkind (Stmt) = N_Object_Declaration then
1624 Expr := Expression (Stmt);
1626 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
1627 return True;
1628 end if;
1629 end if;
1631 Next (Stmt);
1632 end loop;
1634 return False;
1635 end Has_Side_Effect;
1637 -- Start of processing for Is_Exception_Safe
1639 begin
1640 -- If the checks handled by the back end are not disabled, we cannot
1641 -- ensure that no exception will be raised.
1643 if not Access_Checks_Suppressed (Empty)
1644 or else not Discriminant_Checks_Suppressed (Empty)
1645 or else not Range_Checks_Suppressed (Empty)
1646 or else not Index_Checks_Suppressed (Empty)
1647 or else Opt.Stack_Checking_Enabled
1648 then
1649 return False;
1650 end if;
1652 if Has_Side_Effect (First (Declarations (Subprogram)))
1653 or else
1654 Has_Side_Effect (
1655 First (Statements (Handled_Statement_Sequence (Subprogram))))
1656 then
1657 return False;
1658 else
1659 return True;
1660 end if;
1661 end Is_Exception_Safe;
1663 -- Start of processing for Build_Protected_Subprogram_Body
1665 begin
1666 Op_Spec := Specification (N);
1667 Exc_Safe := Is_Exception_Safe (N);
1669 P_Op_Spec :=
1670 Build_Protected_Sub_Specification (N,
1671 Pid, Unprotected => False);
1673 -- Build a list of the formal parameters of the protected
1674 -- version of the subprogram to use as the actual parameters
1675 -- of the unprotected version.
1677 Uactuals := New_List;
1678 Pformal := First (Parameter_Specifications (P_Op_Spec));
1680 while Present (Pformal) loop
1681 Append (
1682 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))),
1683 Uactuals);
1684 Next (Pformal);
1685 end loop;
1687 -- Make a call to the unprotected version of the subprogram
1688 -- built above for use by the protected version built below.
1690 if Nkind (Op_Spec) = N_Function_Specification then
1691 if Exc_Safe then
1692 R := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
1693 Unprot_Call :=
1694 Make_Object_Declaration (Loc,
1695 Defining_Identifier => R,
1696 Constant_Present => True,
1697 Object_Definition => New_Copy (Subtype_Mark (N_Op_Spec)),
1698 Expression =>
1699 Make_Function_Call (Loc,
1700 Name => Make_Identifier (Loc,
1701 Chars (Defining_Unit_Name (N_Op_Spec))),
1702 Parameter_Associations => Uactuals));
1703 Return_Stmt := Make_Return_Statement (Loc,
1704 Expression => New_Reference_To (R, Loc));
1706 else
1707 Unprot_Call := Make_Return_Statement (Loc,
1708 Expression => Make_Function_Call (Loc,
1709 Name =>
1710 Make_Identifier (Loc,
1711 Chars (Defining_Unit_Name (N_Op_Spec))),
1712 Parameter_Associations => Uactuals));
1713 end if;
1715 else
1716 Unprot_Call := Make_Procedure_Call_Statement (Loc,
1717 Name =>
1718 Make_Identifier (Loc,
1719 Chars (Defining_Unit_Name (N_Op_Spec))),
1720 Parameter_Associations => Uactuals);
1721 end if;
1723 -- Wrap call in block that will be covered by an at_end handler
1725 if not Exc_Safe then
1726 Unprot_Call := Make_Block_Statement (Loc,
1727 Handled_Statement_Sequence =>
1728 Make_Handled_Sequence_Of_Statements (Loc,
1729 Statements => New_List (Unprot_Call)));
1730 end if;
1732 -- Make the protected subprogram body. This locks the protected
1733 -- object and calls the unprotected version of the subprogram.
1735 -- If the protected object is controlled (i.e it has entries or
1736 -- needs finalization for interrupt handling), call Lock_Entries,
1737 -- except if the protected object follows the Ravenscar profile, in
1738 -- which case call Lock_Entry, otherwise call the simplified version,
1739 -- Lock.
1741 if Has_Entries (Pid)
1742 or else Has_Interrupt_Handler (Pid)
1743 or else (Has_Attach_Handler (Pid) and then not Restricted_Profile)
1744 then
1745 if Abort_Allowed
1746 or else Restriction_Active (No_Entry_Queue) = False
1747 or else Number_Entries (Pid) > 1
1748 then
1749 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
1750 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
1752 else
1753 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
1754 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
1755 end if;
1757 else
1758 Lock_Name := New_Reference_To (RTE (RE_Lock), Loc);
1759 Service_Name := New_Reference_To (RTE (RE_Unlock), Loc);
1760 end if;
1762 Object_Parm :=
1763 Make_Attribute_Reference (Loc,
1764 Prefix =>
1765 Make_Selected_Component (Loc,
1766 Prefix =>
1767 Make_Identifier (Loc, Name_uObject),
1768 Selector_Name =>
1769 Make_Identifier (Loc, Name_uObject)),
1770 Attribute_Name => Name_Unchecked_Access);
1772 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
1773 Name => Lock_Name,
1774 Parameter_Associations => New_List (Object_Parm));
1776 if Abort_Allowed then
1777 Stmts := New_List (
1778 Make_Procedure_Call_Statement (Loc,
1779 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
1780 Parameter_Associations => Empty_List),
1781 Lock_Stmt);
1783 else
1784 Stmts := New_List (Lock_Stmt);
1785 end if;
1787 if not Exc_Safe then
1788 Append (Unprot_Call, Stmts);
1789 else
1790 if Nkind (Op_Spec) = N_Function_Specification then
1791 Pre_Stmts := Stmts;
1792 Stmts := Empty_List;
1793 else
1794 Append (Unprot_Call, Stmts);
1795 end if;
1797 Append (
1798 Make_Procedure_Call_Statement (Loc,
1799 Name => Service_Name,
1800 Parameter_Associations =>
1801 New_List (New_Copy_Tree (Object_Parm))),
1802 Stmts);
1804 if Abort_Allowed then
1805 Append (
1806 Make_Procedure_Call_Statement (Loc,
1807 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
1808 Parameter_Associations => Empty_List),
1809 Stmts);
1810 end if;
1812 if Nkind (Op_Spec) = N_Function_Specification then
1813 Append (Return_Stmt, Stmts);
1814 Append (Make_Block_Statement (Loc,
1815 Declarations => New_List (Unprot_Call),
1816 Handled_Statement_Sequence =>
1817 Make_Handled_Sequence_Of_Statements (Loc,
1818 Statements => Stmts)), Pre_Stmts);
1819 Stmts := Pre_Stmts;
1820 end if;
1821 end if;
1823 Sub_Body :=
1824 Make_Subprogram_Body (Loc,
1825 Declarations => Empty_List,
1826 Specification => P_Op_Spec,
1827 Handled_Statement_Sequence =>
1828 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
1830 if not Exc_Safe then
1831 Set_Is_Protected_Subprogram_Body (Sub_Body);
1832 end if;
1834 return Sub_Body;
1835 end Build_Protected_Subprogram_Body;
1837 -------------------------------------
1838 -- Build_Protected_Subprogram_Call --
1839 -------------------------------------
1841 procedure Build_Protected_Subprogram_Call
1842 (N : Node_Id;
1843 Name : Node_Id;
1844 Rec : Node_Id;
1845 External : Boolean := True)
1847 Loc : constant Source_Ptr := Sloc (N);
1848 Sub : constant Entity_Id := Entity (Name);
1849 New_Sub : Node_Id;
1850 Params : List_Id;
1852 begin
1853 if External then
1854 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
1855 else
1856 New_Sub :=
1857 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
1858 end if;
1860 if Present (Parameter_Associations (N)) then
1861 Params := New_Copy_List_Tree (Parameter_Associations (N));
1862 else
1863 Params := New_List;
1864 end if;
1866 Prepend (Rec, Params);
1868 if Ekind (Sub) = E_Procedure then
1869 Rewrite (N,
1870 Make_Procedure_Call_Statement (Loc,
1871 Name => New_Sub,
1872 Parameter_Associations => Params));
1874 else
1875 pragma Assert (Ekind (Sub) = E_Function);
1876 Rewrite (N,
1877 Make_Function_Call (Loc,
1878 Name => New_Sub,
1879 Parameter_Associations => Params));
1880 end if;
1882 if External
1883 and then Nkind (Rec) = N_Unchecked_Type_Conversion
1884 and then Is_Entity_Name (Expression (Rec))
1885 and then Is_Shared_Passive (Entity (Expression (Rec)))
1886 then
1887 Add_Shared_Var_Lock_Procs (N);
1888 end if;
1889 end Build_Protected_Subprogram_Call;
1891 -------------------------
1892 -- Build_Selected_Name --
1893 -------------------------
1895 function Build_Selected_Name
1896 (Prefix, Selector : Name_Id;
1897 Append_Char : Character := ' ') return Name_Id
1899 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
1900 Select_Len : Natural;
1902 begin
1903 Get_Name_String (Selector);
1904 Select_Len := Name_Len;
1905 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
1906 Get_Name_String (Prefix);
1908 -- If scope is anonymous type, discard suffix to recover name of
1909 -- single protected object. Otherwise use protected type name.
1911 if Name_Buffer (Name_Len) = 'T' then
1912 Name_Len := Name_Len - 1;
1913 end if;
1915 Name_Buffer (Name_Len + 1) := '_';
1916 Name_Buffer (Name_Len + 2) := '_';
1918 Name_Len := Name_Len + 2;
1919 for J in 1 .. Select_Len loop
1920 Name_Len := Name_Len + 1;
1921 Name_Buffer (Name_Len) := Select_Buffer (J);
1922 end loop;
1924 if Append_Char /= ' ' then
1925 Name_Len := Name_Len + 1;
1926 Name_Buffer (Name_Len) := Append_Char;
1927 end if;
1929 return Name_Find;
1930 end Build_Selected_Name;
1932 -----------------------------
1933 -- Build_Simple_Entry_Call --
1934 -----------------------------
1936 -- A task entry call is converted to a call to Call_Simple
1938 -- declare
1939 -- P : parms := (parm, parm, parm);
1940 -- begin
1941 -- Call_Simple (acceptor-task, entry-index, P'Address);
1942 -- parm := P.param;
1943 -- parm := P.param;
1944 -- ...
1945 -- end;
1947 -- Here Pnn is an aggregate of the type constructed for the entry to hold
1948 -- the parameters, and the constructed aggregate value contains either the
1949 -- parameters or, in the case of non-elementary types, references to these
1950 -- parameters. Then the address of this aggregate is passed to the runtime
1951 -- routine, along with the task id value and the task entry index value.
1952 -- Pnn is only required if parameters are present.
1954 -- The assignments after the call are present only in the case of in-out
1955 -- or out parameters for elementary types, and are used to assign back the
1956 -- resulting values of such parameters.
1958 -- Note: the reason that we insert a block here is that in the context
1959 -- of selects, conditional entry calls etc. the entry call statement
1960 -- appears on its own, not as an element of a list.
1962 -- A protected entry call is converted to a Protected_Entry_Call:
1964 -- declare
1965 -- P : E1_Params := (param, param, param);
1966 -- Pnn : Boolean;
1967 -- Bnn : Communications_Block;
1969 -- declare
1970 -- P : E1_Params := (param, param, param);
1971 -- Bnn : Communications_Block;
1973 -- begin
1974 -- Protected_Entry_Call (
1975 -- Object => po._object'Access,
1976 -- E => <entry index>;
1977 -- Uninterpreted_Data => P'Address;
1978 -- Mode => Simple_Call;
1979 -- Block => Bnn);
1980 -- parm := P.param;
1981 -- parm := P.param;
1982 -- ...
1983 -- end;
1985 procedure Build_Simple_Entry_Call
1986 (N : Node_Id;
1987 Concval : Node_Id;
1988 Ename : Node_Id;
1989 Index : Node_Id)
1991 begin
1992 Expand_Call (N);
1994 -- Convert entry call to Call_Simple call
1996 declare
1997 Loc : constant Source_Ptr := Sloc (N);
1998 Parms : constant List_Id := Parameter_Associations (N);
1999 Stats : constant List_Id := New_List;
2000 Pdecl : Node_Id;
2001 Xdecl : Node_Id;
2002 Decls : List_Id;
2003 Conctyp : Node_Id;
2004 Ent : Entity_Id;
2005 Ent_Acc : Entity_Id;
2006 P : Entity_Id;
2007 X : Entity_Id;
2008 Plist : List_Id;
2009 Parm1 : Node_Id;
2010 Parm2 : Node_Id;
2011 Parm3 : Node_Id;
2012 Call : Node_Id;
2013 Actual : Node_Id;
2014 Formal : Node_Id;
2015 N_Node : Node_Id;
2016 N_Var : Node_Id;
2017 Comm_Name : Entity_Id;
2019 begin
2020 -- Simple entry and entry family cases merge here
2022 Ent := Entity (Ename);
2023 Ent_Acc := Entry_Parameters_Type (Ent);
2024 Conctyp := Etype (Concval);
2026 -- If prefix is an access type, dereference to obtain the task type
2028 if Is_Access_Type (Conctyp) then
2029 Conctyp := Designated_Type (Conctyp);
2030 end if;
2032 -- Special case for protected subprogram calls
2034 if Is_Protected_Type (Conctyp)
2035 and then Is_Subprogram (Entity (Ename))
2036 then
2037 if not Is_Eliminated (Entity (Ename)) then
2038 Build_Protected_Subprogram_Call
2039 (N, Ename, Convert_Concurrent (Concval, Conctyp));
2040 Analyze (N);
2041 end if;
2043 return;
2044 end if;
2046 -- First parameter is the Task_Id value from the task value or the
2047 -- Object from the protected object value, obtained by selecting
2048 -- the _Task_Id or _Object from the result of doing an unchecked
2049 -- conversion to convert the value to the corresponding record type.
2051 Parm1 := Concurrent_Ref (Concval);
2053 -- Second parameter is the entry index, computed by the routine
2054 -- provided for this purpose. The value of this expression is
2055 -- assigned to an intermediate variable to assure that any entry
2056 -- family index expressions are evaluated before the entry
2057 -- parameters.
2059 if Abort_Allowed
2060 or else Restriction_Active (No_Entry_Queue) = False
2061 or else not Is_Protected_Type (Conctyp)
2062 or else Number_Entries (Conctyp) > 1
2063 then
2064 X := Make_Defining_Identifier (Loc, Name_uX);
2066 Xdecl :=
2067 Make_Object_Declaration (Loc,
2068 Defining_Identifier => X,
2069 Object_Definition =>
2070 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
2071 Expression => Actual_Index_Expression (
2072 Loc, Entity (Ename), Index, Concval));
2074 Decls := New_List (Xdecl);
2075 Parm2 := New_Reference_To (X, Loc);
2077 else
2078 Xdecl := Empty;
2079 Decls := New_List;
2080 Parm2 := Empty;
2081 end if;
2083 -- The third parameter is the packaged parameters. If there are
2084 -- none, then it is just the null address, since nothing is passed
2086 if No (Parms) then
2087 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
2088 P := Empty;
2090 -- Case of parameters present, where third argument is the address
2091 -- of a packaged record containing the required parameter values.
2093 else
2094 -- First build a list of parameter values, which are
2095 -- references to objects of the parameter types.
2097 Plist := New_List;
2099 Actual := First_Actual (N);
2100 Formal := First_Formal (Ent);
2102 while Present (Actual) loop
2104 -- If it is a by_copy_type, copy it to a new variable. The
2105 -- packaged record has a field that points to this variable.
2107 if Is_By_Copy_Type (Etype (Actual)) then
2108 N_Node :=
2109 Make_Object_Declaration (Loc,
2110 Defining_Identifier =>
2111 Make_Defining_Identifier (Loc,
2112 Chars => New_Internal_Name ('J')),
2113 Aliased_Present => True,
2114 Object_Definition =>
2115 New_Reference_To (Etype (Formal), Loc));
2117 -- We have to make an assignment statement separate for
2118 -- the case of limited type. We can not assign it unless
2119 -- the Assignment_OK flag is set first.
2121 if Ekind (Formal) /= E_Out_Parameter then
2122 N_Var :=
2123 New_Reference_To (Defining_Identifier (N_Node), Loc);
2124 Set_Assignment_OK (N_Var);
2125 Append_To (Stats,
2126 Make_Assignment_Statement (Loc,
2127 Name => N_Var,
2128 Expression => Relocate_Node (Actual)));
2129 end if;
2131 Append (N_Node, Decls);
2133 Append_To (Plist,
2134 Make_Attribute_Reference (Loc,
2135 Attribute_Name => Name_Unchecked_Access,
2136 Prefix =>
2137 New_Reference_To (Defining_Identifier (N_Node), Loc)));
2138 else
2139 Append_To (Plist,
2140 Make_Reference (Loc, Prefix => Relocate_Node (Actual)));
2141 end if;
2143 Next_Actual (Actual);
2144 Next_Formal_With_Extras (Formal);
2145 end loop;
2147 -- Now build the declaration of parameters initialized with the
2148 -- aggregate containing this constructed parameter list.
2150 P := Make_Defining_Identifier (Loc, Name_uP);
2152 Pdecl :=
2153 Make_Object_Declaration (Loc,
2154 Defining_Identifier => P,
2155 Object_Definition =>
2156 New_Reference_To (Designated_Type (Ent_Acc), Loc),
2157 Expression =>
2158 Make_Aggregate (Loc, Expressions => Plist));
2160 Parm3 :=
2161 Make_Attribute_Reference (Loc,
2162 Attribute_Name => Name_Address,
2163 Prefix => New_Reference_To (P, Loc));
2165 Append (Pdecl, Decls);
2166 end if;
2168 -- Now we can create the call, case of protected type
2170 if Is_Protected_Type (Conctyp) then
2171 if Abort_Allowed
2172 or else Restriction_Active (No_Entry_Queue) = False
2173 or else Number_Entries (Conctyp) > 1
2174 then
2175 -- Change the type of the index declaration
2177 Set_Object_Definition (Xdecl,
2178 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
2180 -- Some additional declarations for protected entry calls
2182 if No (Decls) then
2183 Decls := New_List;
2184 end if;
2186 -- Bnn : Communications_Block;
2188 Comm_Name :=
2189 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
2191 Append_To (Decls,
2192 Make_Object_Declaration (Loc,
2193 Defining_Identifier => Comm_Name,
2194 Object_Definition =>
2195 New_Reference_To (RTE (RE_Communication_Block), Loc)));
2197 -- Some additional statements for protected entry calls
2199 -- Protected_Entry_Call (
2200 -- Object => po._object'Access,
2201 -- E => <entry index>;
2202 -- Uninterpreted_Data => P'Address;
2203 -- Mode => Simple_Call;
2204 -- Block => Bnn);
2206 Call :=
2207 Make_Procedure_Call_Statement (Loc,
2208 Name =>
2209 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
2211 Parameter_Associations => New_List (
2212 Make_Attribute_Reference (Loc,
2213 Attribute_Name => Name_Unchecked_Access,
2214 Prefix => Parm1),
2215 Parm2,
2216 Parm3,
2217 New_Reference_To (RTE (RE_Simple_Call), Loc),
2218 New_Occurrence_Of (Comm_Name, Loc)));
2220 else
2221 -- Protected_Single_Entry_Call (
2222 -- Object => po._object'Access,
2223 -- Uninterpreted_Data => P'Address;
2224 -- Mode => Simple_Call);
2226 Call :=
2227 Make_Procedure_Call_Statement (Loc,
2228 Name => New_Reference_To (
2229 RTE (RE_Protected_Single_Entry_Call), Loc),
2231 Parameter_Associations => New_List (
2232 Make_Attribute_Reference (Loc,
2233 Attribute_Name => Name_Unchecked_Access,
2234 Prefix => Parm1),
2235 Parm3,
2236 New_Reference_To (RTE (RE_Simple_Call), Loc)));
2237 end if;
2239 -- Case of task type
2241 else
2242 Call :=
2243 Make_Procedure_Call_Statement (Loc,
2244 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
2245 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
2247 end if;
2249 Append_To (Stats, Call);
2251 -- If there are out or in/out parameters by copy
2252 -- add assignment statements for the result values.
2254 if Present (Parms) then
2255 Actual := First_Actual (N);
2256 Formal := First_Formal (Ent);
2258 Set_Assignment_OK (Actual);
2259 while Present (Actual) loop
2260 if Is_By_Copy_Type (Etype (Actual))
2261 and then Ekind (Formal) /= E_In_Parameter
2262 then
2263 N_Node :=
2264 Make_Assignment_Statement (Loc,
2265 Name => New_Copy (Actual),
2266 Expression =>
2267 Make_Explicit_Dereference (Loc,
2268 Make_Selected_Component (Loc,
2269 Prefix => New_Reference_To (P, Loc),
2270 Selector_Name =>
2271 Make_Identifier (Loc, Chars (Formal)))));
2273 -- In all cases (including limited private types) we
2274 -- want the assignment to be valid.
2276 Set_Assignment_OK (Name (N_Node));
2278 -- If the call is the triggering alternative in an
2279 -- asynchronous select, or the entry_call alternative
2280 -- of a conditional entry call, the assignments for in-out
2281 -- parameters are incorporated into the statement list
2282 -- that follows, so that there are executed only if the
2283 -- entry call succeeds.
2285 if (Nkind (Parent (N)) = N_Triggering_Alternative
2286 and then N = Triggering_Statement (Parent (N)))
2287 or else
2288 (Nkind (Parent (N)) = N_Entry_Call_Alternative
2289 and then N = Entry_Call_Statement (Parent (N)))
2290 then
2291 if No (Statements (Parent (N))) then
2292 Set_Statements (Parent (N), New_List);
2293 end if;
2295 Prepend (N_Node, Statements (Parent (N)));
2297 else
2298 Insert_After (Call, N_Node);
2299 end if;
2300 end if;
2302 Next_Actual (Actual);
2303 Next_Formal_With_Extras (Formal);
2304 end loop;
2305 end if;
2307 -- Finally, create block and analyze it
2309 Rewrite (N,
2310 Make_Block_Statement (Loc,
2311 Declarations => Decls,
2312 Handled_Statement_Sequence =>
2313 Make_Handled_Sequence_Of_Statements (Loc,
2314 Statements => Stats)));
2316 Analyze (N);
2317 end;
2318 end Build_Simple_Entry_Call;
2320 --------------------------------
2321 -- Build_Task_Activation_Call --
2322 --------------------------------
2324 procedure Build_Task_Activation_Call (N : Node_Id) is
2325 Loc : constant Source_Ptr := Sloc (N);
2326 Chain : Entity_Id;
2327 Call : Node_Id;
2328 Name : Node_Id;
2329 P : Node_Id;
2331 begin
2332 -- Get the activation chain entity. Except in the case of a package
2333 -- body, this is in the node that w as passed. For a package body, we
2334 -- have to find the corresponding package declaration node.
2336 if Nkind (N) = N_Package_Body then
2337 P := Corresponding_Spec (N);
2339 loop
2340 P := Parent (P);
2341 exit when Nkind (P) = N_Package_Declaration;
2342 end loop;
2344 Chain := Activation_Chain_Entity (P);
2346 else
2347 Chain := Activation_Chain_Entity (N);
2348 end if;
2350 if Present (Chain) then
2351 if Restricted_Profile then
2352 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
2353 else
2354 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
2355 end if;
2357 Call :=
2358 Make_Procedure_Call_Statement (Loc,
2359 Name => Name,
2360 Parameter_Associations =>
2361 New_List (Make_Attribute_Reference (Loc,
2362 Prefix => New_Occurrence_Of (Chain, Loc),
2363 Attribute_Name => Name_Unchecked_Access)));
2365 if Nkind (N) = N_Package_Declaration then
2366 if Present (Corresponding_Body (N)) then
2367 null;
2369 elsif Present (Private_Declarations (Specification (N))) then
2370 Append (Call, Private_Declarations (Specification (N)));
2372 else
2373 Append (Call, Visible_Declarations (Specification (N)));
2374 end if;
2376 else
2377 if Present (Handled_Statement_Sequence (N)) then
2379 -- The call goes at the start of the statement sequence, but
2380 -- after the start of exception range label if one is present.
2382 declare
2383 Stm : Node_Id;
2385 begin
2386 Stm := First (Statements (Handled_Statement_Sequence (N)));
2388 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
2389 Next (Stm);
2390 end if;
2392 Insert_Before (Stm, Call);
2393 end;
2395 else
2396 Set_Handled_Statement_Sequence (N,
2397 Make_Handled_Sequence_Of_Statements (Loc,
2398 Statements => New_List (Call)));
2399 end if;
2400 end if;
2402 Analyze (Call);
2403 Check_Task_Activation (N);
2404 end if;
2405 end Build_Task_Activation_Call;
2407 -------------------------------
2408 -- Build_Task_Allocate_Block --
2409 -------------------------------
2411 procedure Build_Task_Allocate_Block
2412 (Actions : List_Id;
2413 N : Node_Id;
2414 Args : List_Id)
2416 T : constant Entity_Id := Entity (Expression (N));
2417 Init : constant Entity_Id := Base_Init_Proc (T);
2418 Loc : constant Source_Ptr := Sloc (N);
2419 Chain : constant Entity_Id :=
2420 Make_Defining_Identifier (Loc, Name_uChain);
2422 Blkent : Entity_Id;
2423 Block : Node_Id;
2425 begin
2426 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2428 Block :=
2429 Make_Block_Statement (Loc,
2430 Identifier => New_Reference_To (Blkent, Loc),
2431 Declarations => New_List (
2433 -- _Chain : Activation_Chain;
2435 Make_Object_Declaration (Loc,
2436 Defining_Identifier => Chain,
2437 Aliased_Present => True,
2438 Object_Definition =>
2439 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
2441 Handled_Statement_Sequence =>
2442 Make_Handled_Sequence_Of_Statements (Loc,
2444 Statements => New_List (
2446 -- Init (Args);
2448 Make_Procedure_Call_Statement (Loc,
2449 Name => New_Reference_To (Init, Loc),
2450 Parameter_Associations => Args),
2452 -- Activate_Tasks (_Chain);
2454 Make_Procedure_Call_Statement (Loc,
2455 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
2456 Parameter_Associations => New_List (
2457 Make_Attribute_Reference (Loc,
2458 Prefix => New_Reference_To (Chain, Loc),
2459 Attribute_Name => Name_Unchecked_Access))))),
2461 Has_Created_Identifier => True,
2462 Is_Task_Allocation_Block => True);
2464 Append_To (Actions,
2465 Make_Implicit_Label_Declaration (Loc,
2466 Defining_Identifier => Blkent,
2467 Label_Construct => Block));
2469 Append_To (Actions, Block);
2471 Set_Activation_Chain_Entity (Block, Chain);
2472 end Build_Task_Allocate_Block;
2474 -----------------------------------------------
2475 -- Build_Task_Allocate_Block_With_Init_Stmts --
2476 -----------------------------------------------
2478 procedure Build_Task_Allocate_Block_With_Init_Stmts
2479 (Actions : List_Id;
2480 N : Node_Id;
2481 Init_Stmts : List_Id)
2483 Loc : constant Source_Ptr := Sloc (N);
2484 Chain : constant Entity_Id :=
2485 Make_Defining_Identifier (Loc, Name_uChain);
2486 Blkent : Entity_Id;
2487 Block : Node_Id;
2489 begin
2490 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2492 Append_To (Init_Stmts,
2493 Make_Procedure_Call_Statement (Loc,
2494 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
2495 Parameter_Associations => New_List (
2496 Make_Attribute_Reference (Loc,
2497 Prefix => New_Reference_To (Chain, Loc),
2498 Attribute_Name => Name_Unchecked_Access))));
2500 Block :=
2501 Make_Block_Statement (Loc,
2502 Identifier => New_Reference_To (Blkent, Loc),
2503 Declarations => New_List (
2505 -- _Chain : Activation_Chain;
2507 Make_Object_Declaration (Loc,
2508 Defining_Identifier => Chain,
2509 Aliased_Present => True,
2510 Object_Definition =>
2511 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
2513 Handled_Statement_Sequence =>
2514 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
2516 Has_Created_Identifier => True,
2517 Is_Task_Allocation_Block => True);
2519 Append_To (Actions,
2520 Make_Implicit_Label_Declaration (Loc,
2521 Defining_Identifier => Blkent,
2522 Label_Construct => Block));
2524 Append_To (Actions, Block);
2526 Set_Activation_Chain_Entity (Block, Chain);
2527 end Build_Task_Allocate_Block_With_Init_Stmts;
2529 -----------------------------------
2530 -- Build_Task_Proc_Specification --
2531 -----------------------------------
2533 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
2534 Loc : constant Source_Ptr := Sloc (T);
2535 Nam : constant Name_Id := Chars (T);
2536 Tdec : constant Node_Id := Declaration_Node (T);
2537 Ent : Entity_Id;
2539 begin
2540 Ent :=
2541 Make_Defining_Identifier (Loc,
2542 Chars => New_External_Name (Nam, 'B'));
2543 Set_Is_Internal (Ent);
2545 -- Associate the procedure with the task, if this is the declaration
2546 -- (and not the body) of the procedure.
2548 if No (Task_Body_Procedure (Tdec)) then
2549 Set_Task_Body_Procedure (Tdec, Ent);
2550 end if;
2552 return
2553 Make_Procedure_Specification (Loc,
2554 Defining_Unit_Name => Ent,
2555 Parameter_Specifications =>
2556 New_List (
2557 Make_Parameter_Specification (Loc,
2558 Defining_Identifier =>
2559 Make_Defining_Identifier (Loc, Name_uTask),
2560 Parameter_Type =>
2561 Make_Access_Definition (Loc,
2562 Subtype_Mark =>
2563 New_Reference_To
2564 (Corresponding_Record_Type (T), Loc)))));
2565 end Build_Task_Proc_Specification;
2567 ---------------------------------------
2568 -- Build_Unprotected_Subprogram_Body --
2569 ---------------------------------------
2571 function Build_Unprotected_Subprogram_Body
2572 (N : Node_Id;
2573 Pid : Node_Id) return Node_Id
2575 Loc : constant Source_Ptr := Sloc (N);
2576 N_Op_Spec : Node_Id;
2577 Op_Decls : List_Id;
2579 begin
2580 -- Make an unprotected version of the subprogram for use
2581 -- within the same object, with a new name and an additional
2582 -- parameter representing the object.
2584 Op_Decls := Declarations (N);
2585 N_Op_Spec :=
2586 Build_Protected_Sub_Specification
2587 (N, Pid, Unprotected => True);
2589 return
2590 Make_Subprogram_Body (Loc,
2591 Specification => N_Op_Spec,
2592 Declarations => Op_Decls,
2593 Handled_Statement_Sequence =>
2594 Handled_Statement_Sequence (N));
2595 end Build_Unprotected_Subprogram_Body;
2597 ----------------------------
2598 -- Collect_Entry_Families --
2599 ----------------------------
2601 procedure Collect_Entry_Families
2602 (Loc : Source_Ptr;
2603 Cdecls : List_Id;
2604 Current_Node : in out Node_Id;
2605 Conctyp : Entity_Id)
2607 Efam : Entity_Id;
2608 Efam_Decl : Node_Id;
2609 Efam_Type : Entity_Id;
2611 begin
2612 Efam := First_Entity (Conctyp);
2614 while Present (Efam) loop
2616 if Ekind (Efam) = E_Entry_Family then
2617 Efam_Type :=
2618 Make_Defining_Identifier (Loc,
2619 Chars => New_Internal_Name ('F'));
2621 Efam_Decl :=
2622 Make_Full_Type_Declaration (Loc,
2623 Defining_Identifier => Efam_Type,
2624 Type_Definition =>
2625 Make_Unconstrained_Array_Definition (Loc,
2626 Subtype_Marks => (New_List (
2627 New_Occurrence_Of (
2628 Base_Type
2629 (Etype (Discrete_Subtype_Definition
2630 (Parent (Efam)))), Loc))),
2632 Component_Definition =>
2633 Make_Component_Definition (Loc,
2634 Aliased_Present => False,
2635 Subtype_Indication =>
2636 New_Reference_To (Standard_Character, Loc))));
2638 Insert_After (Current_Node, Efam_Decl);
2639 Current_Node := Efam_Decl;
2640 Analyze (Efam_Decl);
2642 Append_To (Cdecls,
2643 Make_Component_Declaration (Loc,
2644 Defining_Identifier =>
2645 Make_Defining_Identifier (Loc, Chars (Efam)),
2647 Component_Definition =>
2648 Make_Component_Definition (Loc,
2649 Aliased_Present => False,
2650 Subtype_Indication =>
2651 Make_Subtype_Indication (Loc,
2652 Subtype_Mark =>
2653 New_Occurrence_Of (Efam_Type, Loc),
2655 Constraint =>
2656 Make_Index_Or_Discriminant_Constraint (Loc,
2657 Constraints => New_List (
2658 New_Occurrence_Of
2659 (Etype (Discrete_Subtype_Definition
2660 (Parent (Efam))), Loc)))))));
2662 end if;
2664 Next_Entity (Efam);
2665 end loop;
2666 end Collect_Entry_Families;
2668 --------------------
2669 -- Concurrent_Ref --
2670 --------------------
2672 -- The expression returned for a reference to a concurrent
2673 -- object has the form:
2675 -- taskV!(name)._Task_Id
2677 -- for a task, and
2679 -- objectV!(name)._Object
2681 -- for a protected object. For the case of an access to a concurrent
2682 -- object, there is an extra explicit dereference:
2684 -- taskV!(name.all)._Task_Id
2685 -- objectV!(name.all)._Object
2687 -- here taskV and objectV are the types for the associated records, which
2688 -- contain the required _Task_Id and _Object fields for tasks and
2689 -- protected objects, respectively.
2691 -- For the case of a task type name, the expression is
2693 -- Self;
2695 -- i.e. a call to the Self function which returns precisely this Task_Id
2697 -- For the case of a protected type name, the expression is
2699 -- objectR
2701 -- which is a renaming of the _object field of the current object
2702 -- object record, passed into protected operations as a parameter.
2704 function Concurrent_Ref (N : Node_Id) return Node_Id is
2705 Loc : constant Source_Ptr := Sloc (N);
2706 Ntyp : constant Entity_Id := Etype (N);
2707 Dtyp : Entity_Id;
2708 Sel : Name_Id;
2710 function Is_Current_Task (T : Entity_Id) return Boolean;
2711 -- Check whether the reference is to the immediately enclosing task
2712 -- type, or to an outer one (rare but legal).
2714 ---------------------
2715 -- Is_Current_Task --
2716 ---------------------
2718 function Is_Current_Task (T : Entity_Id) return Boolean is
2719 Scop : Entity_Id;
2721 begin
2722 Scop := Current_Scope;
2723 while Present (Scop)
2724 and then Scop /= Standard_Standard
2725 loop
2727 if Scop = T then
2728 return True;
2730 elsif Is_Task_Type (Scop) then
2731 return False;
2733 -- If this is a procedure nested within the task type, we must
2734 -- assume that it can be called from an inner task, and therefore
2735 -- cannot treat it as a local reference.
2737 elsif Is_Overloadable (Scop)
2738 and then In_Open_Scopes (T)
2739 then
2740 return False;
2742 else
2743 Scop := Scope (Scop);
2744 end if;
2745 end loop;
2747 -- We know that we are within the task body, so should have
2748 -- found it in scope.
2750 raise Program_Error;
2751 end Is_Current_Task;
2753 -- Start of processing for Concurrent_Ref
2755 begin
2756 if Is_Access_Type (Ntyp) then
2757 Dtyp := Designated_Type (Ntyp);
2759 if Is_Protected_Type (Dtyp) then
2760 Sel := Name_uObject;
2761 else
2762 Sel := Name_uTask_Id;
2763 end if;
2765 return
2766 Make_Selected_Component (Loc,
2767 Prefix =>
2768 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
2769 Make_Explicit_Dereference (Loc, N)),
2770 Selector_Name => Make_Identifier (Loc, Sel));
2772 elsif Is_Entity_Name (N)
2773 and then Is_Concurrent_Type (Entity (N))
2774 then
2775 if Is_Task_Type (Entity (N)) then
2777 if Is_Current_Task (Entity (N)) then
2778 return
2779 Make_Function_Call (Loc,
2780 Name => New_Reference_To (RTE (RE_Self), Loc));
2782 else
2783 declare
2784 Decl : Node_Id;
2785 T_Self : constant Entity_Id
2786 := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
2787 T_Body : constant Node_Id
2788 := Parent (Corresponding_Body (Parent (Entity (N))));
2790 begin
2791 Decl := Make_Object_Declaration (Loc,
2792 Defining_Identifier => T_Self,
2793 Object_Definition =>
2794 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
2795 Expression =>
2796 Make_Function_Call (Loc,
2797 Name => New_Reference_To (RTE (RE_Self), Loc)));
2798 Prepend (Decl, Declarations (T_Body));
2799 Analyze (Decl);
2800 Set_Scope (T_Self, Entity (N));
2801 return New_Occurrence_Of (T_Self, Loc);
2802 end;
2803 end if;
2805 else
2806 pragma Assert (Is_Protected_Type (Entity (N)));
2807 return
2808 New_Reference_To (
2809 Object_Ref (Corresponding_Body (Parent (Base_Type (Ntyp)))),
2810 Loc);
2811 end if;
2813 else
2814 pragma Assert (Is_Concurrent_Type (Ntyp));
2816 if Is_Protected_Type (Ntyp) then
2817 Sel := Name_uObject;
2818 else
2819 Sel := Name_uTask_Id;
2820 end if;
2822 return
2823 Make_Selected_Component (Loc,
2824 Prefix =>
2825 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
2826 New_Copy_Tree (N)),
2827 Selector_Name => Make_Identifier (Loc, Sel));
2828 end if;
2829 end Concurrent_Ref;
2831 ------------------------
2832 -- Convert_Concurrent --
2833 ------------------------
2835 function Convert_Concurrent
2836 (N : Node_Id;
2837 Typ : Entity_Id) return Node_Id
2839 begin
2840 if not Is_Concurrent_Type (Typ) then
2841 return N;
2842 else
2843 return
2844 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2845 New_Copy_Tree (N));
2846 end if;
2847 end Convert_Concurrent;
2849 ----------------------------
2850 -- Entry_Index_Expression --
2851 ----------------------------
2853 function Entry_Index_Expression
2854 (Sloc : Source_Ptr;
2855 Ent : Entity_Id;
2856 Index : Node_Id;
2857 Ttyp : Entity_Id) return Node_Id
2859 Expr : Node_Id;
2860 Num : Node_Id;
2861 Lo : Node_Id;
2862 Hi : Node_Id;
2863 Prev : Entity_Id;
2864 S : Node_Id;
2866 begin
2867 -- The queues of entries and entry families appear in textual
2868 -- order in the associated record. The entry index is computed as
2869 -- the sum of the number of queues for all entries that precede the
2870 -- designated one, to which is added the index expression, if this
2871 -- expression denotes a member of a family.
2873 -- The following is a place holder for the count of simple entries
2875 Num := Make_Integer_Literal (Sloc, 1);
2877 -- We construct an expression which is a series of addition
2878 -- operations. The first operand is the number of single entries that
2879 -- precede this one, the second operand is the index value relative
2880 -- to the start of the referenced family, and the remaining operands
2881 -- are the lengths of the entry families that precede this entry, i.e.
2882 -- the constructed expression is:
2884 -- number_simple_entries +
2885 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
2886 -- family'length + ...
2888 -- where index-value is the given index value, and s is the index
2889 -- subtype (we have to use pos because the subtype might be an
2890 -- enumeration type preventing direct subtraction).
2891 -- Note that the task entry array is one-indexed.
2893 -- The upper bound of the entry family may be a discriminant, so we
2894 -- retrieve the lower bound explicitly to compute offset, rather than
2895 -- using the index subtype which may mention a discriminant.
2897 if Present (Index) then
2898 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
2900 Expr :=
2901 Make_Op_Add (Sloc,
2902 Left_Opnd => Num,
2904 Right_Opnd =>
2905 Family_Offset (
2906 Sloc,
2907 Make_Attribute_Reference (Sloc,
2908 Attribute_Name => Name_Pos,
2909 Prefix => New_Reference_To (Base_Type (S), Sloc),
2910 Expressions => New_List (Relocate_Node (Index))),
2911 Type_Low_Bound (S),
2912 Ttyp));
2913 else
2914 Expr := Num;
2915 end if;
2917 -- Now add lengths of preceding entries and entry families
2919 Prev := First_Entity (Ttyp);
2921 while Chars (Prev) /= Chars (Ent)
2922 or else (Ekind (Prev) /= Ekind (Ent))
2923 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
2924 loop
2925 if Ekind (Prev) = E_Entry then
2926 Set_Intval (Num, Intval (Num) + 1);
2928 elsif Ekind (Prev) = E_Entry_Family then
2929 S :=
2930 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
2931 Lo := Type_Low_Bound (S);
2932 Hi := Type_High_Bound (S);
2934 Expr :=
2935 Make_Op_Add (Sloc,
2936 Left_Opnd => Expr,
2937 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp));
2939 -- Other components are anonymous types to be ignored
2941 else
2942 null;
2943 end if;
2945 Next_Entity (Prev);
2946 end loop;
2948 return Expr;
2949 end Entry_Index_Expression;
2951 ---------------------------
2952 -- Establish_Task_Master --
2953 ---------------------------
2955 procedure Establish_Task_Master (N : Node_Id) is
2956 Call : Node_Id;
2958 begin
2959 if Restriction_Active (No_Task_Hierarchy) = False then
2960 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
2961 Prepend_To (Declarations (N), Call);
2962 Analyze (Call);
2963 end if;
2964 end Establish_Task_Master;
2966 --------------------------------
2967 -- Expand_Accept_Declarations --
2968 --------------------------------
2970 -- Part of the expansion of an accept statement involves the creation of
2971 -- a declaration that can be referenced from the statement sequence of
2972 -- the accept:
2974 -- Ann : Address;
2976 -- This declaration is inserted immediately before the accept statement
2977 -- and it is important that it be inserted before the statements of the
2978 -- statement sequence are analyzed. Thus it would be too late to create
2979 -- this declaration in the Expand_N_Accept_Statement routine, which is
2980 -- why there is a separate procedure to be called directly from Sem_Ch9.
2982 -- Ann is used to hold the address of the record containing the parameters
2983 -- (see Expand_N_Entry_Call for more details on how this record is built).
2984 -- References to the parameters do an unchecked conversion of this address
2985 -- to a pointer to the required record type, and then access the field that
2986 -- holds the value of the required parameter. The entity for the address
2987 -- variable is held as the top stack element (i.e. the last element) of the
2988 -- Accept_Address stack in the corresponding entry entity, and this element
2989 -- must be set in place before the statements are processed.
2991 -- The above description applies to the case of a stand alone accept
2992 -- statement, i.e. one not appearing as part of a select alternative.
2994 -- For the case of an accept that appears as part of a select alternative
2995 -- of a selective accept, we must still create the declaration right away,
2996 -- since Ann is needed immediately, but there is an important difference:
2998 -- The declaration is inserted before the selective accept, not before
2999 -- the accept statement (which is not part of a list anyway, and so would
3000 -- not accommodate inserted declarations)
3002 -- We only need one address variable for the entire selective accept. So
3003 -- the Ann declaration is created only for the first accept alternative,
3004 -- and subsequent accept alternatives reference the same Ann variable.
3006 -- We can distinguish the two cases by seeing whether the accept statement
3007 -- is part of a list. If not, then it must be in an accept alternative.
3009 -- To expand the requeue statement, a label is provided at the end of
3010 -- the accept statement or alternative of which it is a part, so that
3011 -- the statement can be skipped after the requeue is complete.
3012 -- This label is created here rather than during the expansion of the
3013 -- accept statement, because it will be needed by any requeue
3014 -- statements within the accept, which are expanded before the
3015 -- accept.
3017 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
3018 Loc : constant Source_Ptr := Sloc (N);
3019 Ann : Entity_Id := Empty;
3020 Adecl : Node_Id;
3021 Lab_Id : Node_Id;
3022 Lab : Node_Id;
3023 Ldecl : Node_Id;
3024 Ldecl2 : Node_Id;
3026 begin
3027 if Expander_Active then
3029 -- If we have no handled statement sequence, then build a dummy
3030 -- sequence consisting of a null statement. This is only done if
3031 -- pragma FIFO_Within_Priorities is specified. The issue here is
3032 -- that even a null accept body has an effect on the called task
3033 -- in terms of its position in the queue, so we cannot optimize
3034 -- the context switch away. However, if FIFO_Within_Priorities
3035 -- is not active, the optimization is legitimate, since we can
3036 -- say that our dispatching policy (i.e. the default dispatching
3037 -- policy) reorders the queue to be the same as just before the
3038 -- call. In the absence of a specified dispatching policy, we are
3039 -- allowed to modify queue orders for a given priority at will!
3041 if Opt.Task_Dispatching_Policy = 'F' and then
3042 not Present (Handled_Statement_Sequence (N))
3043 then
3044 Set_Handled_Statement_Sequence (N,
3045 Make_Handled_Sequence_Of_Statements (Loc,
3046 New_List (Make_Null_Statement (Loc))));
3047 end if;
3049 -- Create and declare two labels to be placed at the end of the
3050 -- accept statement. The first label is used to allow requeues to
3051 -- skip the remainder of entry processing. The second label is
3052 -- used to skip the remainder of entry processing if the rendezvous
3053 -- completes in the middle of the accept body.
3055 if Present (Handled_Statement_Sequence (N)) then
3056 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
3057 Set_Entity (Lab_Id,
3058 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
3059 Lab := Make_Label (Loc, Lab_Id);
3060 Ldecl :=
3061 Make_Implicit_Label_Declaration (Loc,
3062 Defining_Identifier => Entity (Lab_Id),
3063 Label_Construct => Lab);
3064 Append (Lab, Statements (Handled_Statement_Sequence (N)));
3066 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
3067 Set_Entity (Lab_Id,
3068 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
3069 Lab := Make_Label (Loc, Lab_Id);
3070 Ldecl2 :=
3071 Make_Implicit_Label_Declaration (Loc,
3072 Defining_Identifier => Entity (Lab_Id),
3073 Label_Construct => Lab);
3074 Append (Lab, Statements (Handled_Statement_Sequence (N)));
3076 else
3077 Ldecl := Empty;
3078 Ldecl2 := Empty;
3079 end if;
3081 -- Case of stand alone accept statement
3083 if Is_List_Member (N) then
3085 if Present (Handled_Statement_Sequence (N)) then
3086 Ann :=
3087 Make_Defining_Identifier (Loc,
3088 Chars => New_Internal_Name ('A'));
3090 Adecl :=
3091 Make_Object_Declaration (Loc,
3092 Defining_Identifier => Ann,
3093 Object_Definition =>
3094 New_Reference_To (RTE (RE_Address), Loc));
3096 Insert_Before (N, Adecl);
3097 Analyze (Adecl);
3099 Insert_Before (N, Ldecl);
3100 Analyze (Ldecl);
3102 Insert_Before (N, Ldecl2);
3103 Analyze (Ldecl2);
3104 end if;
3106 -- Case of accept statement which is in an accept alternative
3108 else
3109 declare
3110 Acc_Alt : constant Node_Id := Parent (N);
3111 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
3112 Alt : Node_Id;
3114 begin
3115 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
3116 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
3118 -- ??? Consider a single label for select statements
3120 if Present (Handled_Statement_Sequence (N)) then
3121 Prepend (Ldecl2,
3122 Statements (Handled_Statement_Sequence (N)));
3123 Analyze (Ldecl2);
3125 Prepend (Ldecl,
3126 Statements (Handled_Statement_Sequence (N)));
3127 Analyze (Ldecl);
3128 end if;
3130 -- Find first accept alternative of the selective accept. A
3131 -- valid selective accept must have at least one accept in it.
3133 Alt := First (Select_Alternatives (Sel_Acc));
3135 while Nkind (Alt) /= N_Accept_Alternative loop
3136 Next (Alt);
3137 end loop;
3139 -- If we are the first accept statement, then we have to
3140 -- create the Ann variable, as for the stand alone case,
3141 -- except that it is inserted before the selective accept.
3142 -- Similarly, a label for requeue expansion must be
3143 -- declared.
3145 if N = Accept_Statement (Alt) then
3146 Ann :=
3147 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3149 Adecl :=
3150 Make_Object_Declaration (Loc,
3151 Defining_Identifier => Ann,
3152 Object_Definition =>
3153 New_Reference_To (RTE (RE_Address), Loc));
3155 Insert_Before (Sel_Acc, Adecl);
3156 Analyze (Adecl);
3158 -- If we are not the first accept statement, then find the
3159 -- Ann variable allocated by the first accept and use it.
3161 else
3162 Ann :=
3163 Node (Last_Elmt (Accept_Address
3164 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
3165 end if;
3166 end;
3167 end if;
3169 -- Merge here with Ann either created or referenced, and Adecl
3170 -- pointing to the corresponding declaration. Remaining processing
3171 -- is the same for the two cases.
3173 if Present (Ann) then
3174 Append_Elmt (Ann, Accept_Address (Ent));
3175 Set_Needs_Debug_Info (Ann);
3176 end if;
3178 -- Create renaming declarations for the entry formals. Each
3179 -- reference to a formal becomes a dereference of a component
3180 -- of the parameter block, whose address is held in Ann.
3181 -- These declarations are eventually inserted into the accept
3182 -- block, and analyzed there so that they have the proper scope
3183 -- for gdb and do not conflict with other declarations.
3185 if Present (Parameter_Specifications (N))
3186 and then Present (Handled_Statement_Sequence (N))
3187 then
3188 declare
3189 Formal : Entity_Id;
3190 New_F : Entity_Id;
3191 Comp : Entity_Id;
3192 Decl : Node_Id;
3194 begin
3195 New_Scope (Ent);
3196 Formal := First_Formal (Ent);
3198 while Present (Formal) loop
3199 Comp := Entry_Component (Formal);
3200 New_F :=
3201 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
3202 Set_Etype (New_F, Etype (Formal));
3203 Set_Scope (New_F, Ent);
3204 Set_Needs_Debug_Info (New_F); -- That's the whole point.
3206 if Ekind (Formal) = E_In_Parameter then
3207 Set_Ekind (New_F, E_Constant);
3208 else
3209 Set_Ekind (New_F, E_Variable);
3210 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
3211 end if;
3213 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
3215 Decl :=
3216 Make_Object_Renaming_Declaration (Loc,
3217 Defining_Identifier => New_F,
3218 Subtype_Mark => New_Reference_To (Etype (Formal), Loc),
3219 Name =>
3220 Make_Explicit_Dereference (Loc,
3221 Make_Selected_Component (Loc,
3222 Prefix =>
3223 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
3224 New_Reference_To (Ann, Loc)),
3225 Selector_Name =>
3226 New_Reference_To (Comp, Loc))));
3228 if No (Declarations (N)) then
3229 Set_Declarations (N, New_List);
3230 end if;
3232 Append (Decl, Declarations (N));
3233 Set_Renamed_Object (Formal, New_F);
3234 Next_Formal (Formal);
3235 end loop;
3237 End_Scope;
3238 end;
3239 end if;
3240 end if;
3241 end Expand_Accept_Declarations;
3243 ---------------------------------------------
3244 -- Expand_Access_Protected_Subprogram_Type --
3245 ---------------------------------------------
3247 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
3248 Loc : constant Source_Ptr := Sloc (N);
3249 Comps : List_Id;
3250 T : constant Entity_Id := Defining_Identifier (N);
3251 D_T : constant Entity_Id := Designated_Type (T);
3252 D_T2 : constant Entity_Id := Make_Defining_Identifier
3253 (Loc, New_Internal_Name ('D'));
3254 E_T : constant Entity_Id := Make_Defining_Identifier
3255 (Loc, New_Internal_Name ('E'));
3256 P_List : constant List_Id := Build_Protected_Spec
3257 (N, RTE (RE_Address), False, D_T);
3258 Decl1 : Node_Id;
3259 Decl2 : Node_Id;
3260 Def1 : Node_Id;
3262 begin
3263 -- Create access to protected subprogram with full signature
3265 if Nkind (Type_Definition (N)) = N_Access_Function_Definition then
3266 Def1 :=
3267 Make_Access_Function_Definition (Loc,
3268 Parameter_Specifications => P_List,
3269 Subtype_Mark => New_Copy (Subtype_Mark (Type_Definition (N))));
3271 else
3272 Def1 :=
3273 Make_Access_Procedure_Definition (Loc,
3274 Parameter_Specifications => P_List);
3275 end if;
3277 Decl1 :=
3278 Make_Full_Type_Declaration (Loc,
3279 Defining_Identifier => D_T2,
3280 Type_Definition => Def1);
3282 Analyze (Decl1);
3283 Insert_After (N, Decl1);
3285 -- Create Equivalent_Type, a record with two components for an
3286 -- access to object and an access to subprogram.
3288 Comps := New_List (
3289 Make_Component_Declaration (Loc,
3290 Defining_Identifier =>
3291 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
3292 Component_Definition =>
3293 Make_Component_Definition (Loc,
3294 Aliased_Present => False,
3295 Subtype_Indication =>
3296 New_Occurrence_Of (RTE (RE_Address), Loc))),
3298 Make_Component_Declaration (Loc,
3299 Defining_Identifier =>
3300 Make_Defining_Identifier (Loc, New_Internal_Name ('S')),
3301 Component_Definition =>
3302 Make_Component_Definition (Loc,
3303 Aliased_Present => False,
3304 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
3306 Decl2 :=
3307 Make_Full_Type_Declaration (Loc,
3308 Defining_Identifier => E_T,
3309 Type_Definition =>
3310 Make_Record_Definition (Loc,
3311 Component_List =>
3312 Make_Component_List (Loc,
3313 Component_Items => Comps)));
3315 Analyze (Decl2);
3316 Insert_After (Decl1, Decl2);
3317 Set_Equivalent_Type (T, E_T);
3318 end Expand_Access_Protected_Subprogram_Type;
3320 --------------------------
3321 -- Expand_Entry_Barrier --
3322 --------------------------
3324 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
3325 Loc : constant Source_Ptr := Sloc (N);
3326 Prot : constant Entity_Id := Scope (Ent);
3327 Spec_Decl : constant Node_Id := Parent (Prot);
3328 Cond : constant Node_Id :=
3329 Condition (Entry_Body_Formal_Part (N));
3330 Func : Node_Id;
3331 B_F : Node_Id;
3332 Body_Decl : Node_Id;
3334 begin
3335 if No_Run_Time_Mode then
3336 Error_Msg_CRT ("entry barrier", N);
3337 return;
3338 end if;
3340 -- The body of the entry barrier must be analyzed in the context of
3341 -- the protected object, but its scope is external to it, just as any
3342 -- other unprotected version of a protected operation. The specification
3343 -- has been produced when the protected type declaration was elaborated.
3344 -- We build the body, insert it in the enclosing scope, but analyze it
3345 -- in the current context. A more uniform approach would be to treat a
3346 -- barrier just as a protected function, and discard the protected
3347 -- version of it because it is never called.
3349 if Expander_Active then
3350 B_F := Build_Barrier_Function (N, Ent, Prot);
3351 Func := Barrier_Function (Ent);
3352 Set_Corresponding_Spec (B_F, Func);
3354 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
3356 if Nkind (Parent (Body_Decl)) = N_Subunit then
3357 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
3358 end if;
3360 Insert_Before_And_Analyze (Body_Decl, B_F);
3362 Update_Prival_Subtypes (B_F);
3364 Set_Privals (Spec_Decl, N, Loc);
3365 Set_Discriminals (Spec_Decl);
3366 Set_Scope (Func, Scope (Prot));
3368 else
3369 Analyze_And_Resolve (Cond, Any_Boolean);
3370 end if;
3372 -- The Ravenscar profile restricts barriers to simple variables
3373 -- declared within the protected object. We also allow Boolean
3374 -- constants, since these appear in several published examples
3375 -- and are also allowed by the Aonix compiler.
3377 -- Note that after analysis variables in this context will be
3378 -- replaced by the corresponding prival, that is to say a renaming
3379 -- of a selected component of the form _Object.Var. If expansion is
3380 -- disabled, as within a generic, we check that the entity appears in
3381 -- the current scope.
3383 if Is_Entity_Name (Cond) then
3385 if Entity (Cond) = Standard_False
3386 or else
3387 Entity (Cond) = Standard_True
3388 then
3389 return;
3391 elsif not Expander_Active
3392 and then Scope (Entity (Cond)) = Current_Scope
3393 then
3394 return;
3396 -- Check for case of _object.all.field (note that the explicit
3397 -- dereference gets inserted by analyze/expand of _object.field)
3399 elsif Present (Renamed_Object (Entity (Cond)))
3400 and then
3401 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
3402 and then
3403 Chars
3404 (Prefix
3405 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
3406 then
3407 return;
3408 end if;
3409 end if;
3411 -- It is not a boolean variable or literal, so check the restriction
3413 Check_Restriction (Simple_Barriers, Cond);
3414 end Expand_Entry_Barrier;
3416 ------------------------------------
3417 -- Expand_Entry_Body_Declarations --
3418 ------------------------------------
3420 procedure Expand_Entry_Body_Declarations (N : Node_Id) is
3421 Loc : constant Source_Ptr := Sloc (N);
3422 Index_Spec : Node_Id;
3424 begin
3425 if Expander_Active then
3427 -- Expand entry bodies corresponding to entry families
3428 -- by assigning a placeholder for the constant that will
3429 -- be used to expand references to the entry index parameter.
3431 Index_Spec :=
3432 Entry_Index_Specification (Entry_Body_Formal_Part (N));
3434 if Present (Index_Spec) then
3435 Set_Entry_Index_Constant (
3436 Defining_Identifier (Index_Spec),
3437 Make_Defining_Identifier (Loc, New_Internal_Name ('J')));
3438 end if;
3439 end if;
3440 end Expand_Entry_Body_Declarations;
3442 ------------------------------
3443 -- Expand_N_Abort_Statement --
3444 ------------------------------
3446 -- Expand abort T1, T2, .. Tn; into:
3447 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
3449 procedure Expand_N_Abort_Statement (N : Node_Id) is
3450 Loc : constant Source_Ptr := Sloc (N);
3451 Tlist : constant List_Id := Names (N);
3452 Count : Nat;
3453 Aggr : Node_Id;
3454 Tasknm : Node_Id;
3456 begin
3457 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
3458 Count := 0;
3460 Tasknm := First (Tlist);
3462 while Present (Tasknm) loop
3463 Count := Count + 1;
3464 Append_To (Component_Associations (Aggr),
3465 Make_Component_Association (Loc,
3466 Choices => New_List (
3467 Make_Integer_Literal (Loc, Count)),
3468 Expression => Concurrent_Ref (Tasknm)));
3469 Next (Tasknm);
3470 end loop;
3472 Rewrite (N,
3473 Make_Procedure_Call_Statement (Loc,
3474 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
3475 Parameter_Associations => New_List (
3476 Make_Qualified_Expression (Loc,
3477 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
3478 Expression => Aggr))));
3480 Analyze (N);
3481 end Expand_N_Abort_Statement;
3483 -------------------------------
3484 -- Expand_N_Accept_Statement --
3485 -------------------------------
3487 -- This procedure handles expansion of accept statements that stand
3488 -- alone, i.e. they are not part of an accept alternative. The expansion
3489 -- of accept statement in accept alternatives is handled by the routines
3490 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
3491 -- following description applies only to stand alone accept statements.
3493 -- If there is no handled statement sequence, or only null statements,
3494 -- then this is called a trivial accept, and the expansion is:
3496 -- Accept_Trivial (entry-index)
3498 -- If there is a handled statement sequence, then the expansion is:
3500 -- Ann : Address;
3501 -- {Lnn : Label}
3503 -- begin
3504 -- begin
3505 -- Accept_Call (entry-index, Ann);
3506 -- Renaming_Declarations for formals
3507 -- <statement sequence from N_Accept_Statement node>
3508 -- Complete_Rendezvous;
3509 -- <<Lnn>>
3511 -- exception
3512 -- when ... =>
3513 -- <exception handler from N_Accept_Statement node>
3514 -- Complete_Rendezvous;
3515 -- when ... =>
3516 -- <exception handler from N_Accept_Statement node>
3517 -- Complete_Rendezvous;
3518 -- ...
3519 -- end;
3521 -- exception
3522 -- when all others =>
3523 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
3524 -- end;
3526 -- The first three declarations were already inserted ahead of the
3527 -- accept statement by the Expand_Accept_Declarations procedure, which
3528 -- was called directly from the semantics during analysis of the accept.
3529 -- statement, before analyzing its contained statements.
3531 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
3532 -- from possible expansion activity (the original source of course does
3533 -- not have any declarations associated with the accept statement, since
3534 -- an accept statement has no declarative part). In particular, if the
3535 -- expander is active, the first such declaration is the declaration of
3536 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
3538 -- The two blocks are merged into a single block if the inner block has
3539 -- no exception handlers, but otherwise two blocks are required, since
3540 -- exceptions might be raised in the exception handlers of the inner
3541 -- block, and Exceptional_Complete_Rendezvous must be called.
3543 procedure Expand_N_Accept_Statement (N : Node_Id) is
3544 Loc : constant Source_Ptr := Sloc (N);
3545 Stats : constant Node_Id := Handled_Statement_Sequence (N);
3546 Ename : constant Node_Id := Entry_Direct_Name (N);
3547 Eindx : constant Node_Id := Entry_Index (N);
3548 Eent : constant Entity_Id := Entity (Ename);
3549 Acstack : constant Elist_Id := Accept_Address (Eent);
3550 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
3551 Ttyp : constant Entity_Id := Etype (Scope (Eent));
3552 Blkent : Entity_Id;
3553 Call : Node_Id;
3554 Block : Node_Id;
3556 function Null_Statements (Stats : List_Id) return Boolean;
3557 -- Check for null statement sequence (i.e a list of labels and
3558 -- null statements)
3560 function Null_Statements (Stats : List_Id) return Boolean is
3561 Stmt : Node_Id;
3563 begin
3564 Stmt := First (Stats);
3565 while Nkind (Stmt) /= N_Empty
3566 and then (Nkind (Stmt) = N_Null_Statement
3567 or else
3568 Nkind (Stmt) = N_Label)
3569 loop
3570 Next (Stmt);
3571 end loop;
3573 return Nkind (Stmt) = N_Empty;
3574 end Null_Statements;
3576 -- Start of processing for Expand_N_Accept_Statement
3578 begin
3579 -- If accept statement is not part of a list, then its parent must be
3580 -- an accept alternative, and, as described above, we do not do any
3581 -- expansion for such accept statements at this level.
3583 if not Is_List_Member (N) then
3584 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
3585 return;
3587 -- Trivial accept case (no statement sequence, or null statements).
3588 -- If the accept statement has declarations, then just insert them
3589 -- before the procedure call.
3591 -- We avoid this optimization when FIFO_Within_Priorities is active,
3592 -- since it is not correct according to annex D semantics. The problem
3593 -- is that the call is required to reorder the acceptors position on
3594 -- its ready queue, even though there is nothing to be done. However,
3595 -- if no policy is specified, then we decide that our dispatching
3596 -- policy always reorders the queue right after the RV to look the
3597 -- way they were just before the RV. Since we are allowed to freely
3598 -- reorder same-priority queues (this is part of what dispatching
3599 -- policies are all about), the optimization is legitimate.
3601 elsif Opt.Task_Dispatching_Policy /= 'F'
3602 and then (No (Stats) or else Null_Statements (Statements (Stats)))
3603 then
3604 -- Remove declarations for renamings, because the parameter block
3605 -- will not be assigned.
3607 declare
3608 D : Node_Id;
3609 Next_D : Node_Id;
3611 begin
3612 D := First (Declarations (N));
3614 while Present (D) loop
3615 Next_D := Next (D);
3616 if Nkind (D) = N_Object_Renaming_Declaration then
3617 Remove (D);
3618 end if;
3620 D := Next_D;
3621 end loop;
3622 end;
3624 if Present (Declarations (N)) then
3625 Insert_Actions (N, Declarations (N));
3626 end if;
3628 Rewrite (N,
3629 Make_Procedure_Call_Statement (Loc,
3630 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
3631 Parameter_Associations => New_List (
3632 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
3634 Analyze (N);
3636 -- Discard Entry_Address that was created for it, so it will not be
3637 -- emitted if this accept statement is in the statement part of a
3638 -- delay alternative.
3640 if Present (Stats) then
3641 Remove_Last_Elmt (Acstack);
3642 end if;
3644 -- Case of statement sequence present
3646 else
3647 -- Construct the block, using the declarations from the accept
3648 -- statement if any to initialize the declarations of the block.
3650 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3651 Set_Ekind (Blkent, E_Block);
3652 Set_Etype (Blkent, Standard_Void_Type);
3653 Set_Scope (Blkent, Current_Scope);
3655 Block :=
3656 Make_Block_Statement (Loc,
3657 Identifier => New_Reference_To (Blkent, Loc),
3658 Declarations => Declarations (N),
3659 Handled_Statement_Sequence => Build_Accept_Body (N));
3661 -- Prepend call to Accept_Call to main statement sequence
3662 -- If the accept has exception handlers, the statement sequence
3663 -- is wrapped in a block. Insert call and renaming declarations
3664 -- in the declarations of the block, so they are elaborated before
3665 -- the handlers.
3667 Call :=
3668 Make_Procedure_Call_Statement (Loc,
3669 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
3670 Parameter_Associations => New_List (
3671 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
3672 New_Reference_To (Ann, Loc)));
3674 if Parent (Stats) = N then
3675 Prepend (Call, Statements (Stats));
3676 else
3677 Set_Declarations
3678 (Parent (Stats),
3679 New_List (Call));
3680 end if;
3682 Analyze (Call);
3684 New_Scope (Blkent);
3686 declare
3687 D : Node_Id;
3688 Next_D : Node_Id;
3689 Typ : Entity_Id;
3690 begin
3691 D := First (Declarations (N));
3693 while Present (D) loop
3694 Next_D := Next (D);
3696 if Nkind (D) = N_Object_Renaming_Declaration then
3697 -- The renaming declarations for the formals were
3698 -- created during analysis of the accept statement,
3699 -- and attached to the list of declarations. Place
3700 -- them now in the context of the accept block or
3701 -- subprogram.
3703 Remove (D);
3704 Typ := Entity (Subtype_Mark (D));
3705 Insert_After (Call, D);
3706 Analyze (D);
3708 -- If the formal is class_wide, it does not have an
3709 -- actual subtype. The analysis of the renaming declaration
3710 -- creates one, but we need to retain the class-wide
3711 -- nature of the entity.
3713 if Is_Class_Wide_Type (Typ) then
3714 Set_Etype (Defining_Identifier (D), Typ);
3715 end if;
3717 end if;
3719 D := Next_D;
3720 end loop;
3721 end;
3723 End_Scope;
3725 -- Replace the accept statement by the new block
3727 Rewrite (N, Block);
3728 Analyze (N);
3730 -- Last step is to unstack the Accept_Address value
3732 Remove_Last_Elmt (Acstack);
3733 end if;
3734 end Expand_N_Accept_Statement;
3736 ----------------------------------
3737 -- Expand_N_Asynchronous_Select --
3738 ----------------------------------
3740 -- This procedure assumes that the trigger statement is an entry call. A
3741 -- delay alternative should already have been expanded into an entry call
3742 -- to the appropriate delay object Wait entry.
3744 -- If the trigger is a task entry call, the select is implemented with
3745 -- a Task_Entry_Call:
3747 -- declare
3748 -- B : Boolean;
3749 -- C : Boolean;
3750 -- P : parms := (parm, parm, parm);
3752 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
3754 -- procedure _clean is
3755 -- begin
3756 -- ...
3757 -- Cancel_Task_Entry_Call (C);
3758 -- ...
3759 -- end _clean;
3761 -- begin
3762 -- Abort_Defer;
3763 -- Task_Entry_Call
3764 -- (acceptor-task,
3765 -- entry-index,
3766 -- P'Address,
3767 -- Asynchronous_Call,
3768 -- B);
3770 -- begin
3771 -- begin
3772 -- Abort_Undefer;
3773 -- abortable-part
3774 -- at end
3775 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions.
3776 -- end;
3778 -- exception
3779 -- when Abort_Signal => Abort_Undefer;
3780 -- end;
3781 -- parm := P.param;
3782 -- parm := P.param;
3783 -- ...
3784 -- if not C then
3785 -- triggered-statements
3786 -- end if;
3787 -- end;
3789 -- Note that Build_Simple_Entry_Call is used to expand the entry
3790 -- of the asynchronous entry call (by the
3791 -- Expand_N_Entry_Call_Statement procedure) as follows:
3793 -- declare
3794 -- P : parms := (parm, parm, parm);
3795 -- begin
3796 -- Call_Simple (acceptor-task, entry-index, P'Address);
3797 -- parm := P.param;
3798 -- parm := P.param;
3799 -- ...
3800 -- end;
3802 -- so the task at hand is to convert the latter expansion into the former
3804 -- If the trigger is a protected entry call, the select is
3805 -- implemented with Protected_Entry_Call:
3807 -- declare
3808 -- P : E1_Params := (param, param, param);
3809 -- Bnn : Communications_Block;
3811 -- begin
3812 -- declare
3813 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions.
3814 -- procedure _clean is
3815 -- begin
3816 -- ...
3817 -- if Enqueued (Bnn) then
3818 -- Cancel_Protected_Entry_Call (Bnn);
3819 -- end if;
3820 -- ...
3821 -- end _clean;
3823 -- begin
3824 -- begin
3825 -- Protected_Entry_Call (
3826 -- Object => po._object'Access,
3827 -- E => <entry index>;
3828 -- Uninterpreted_Data => P'Address;
3829 -- Mode => Asynchronous_Call;
3830 -- Block => Bnn);
3831 -- if Enqueued (Bnn) then
3832 -- <abortable part>
3833 -- end if;
3834 -- at end
3835 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions.
3836 -- end;
3838 -- exception
3839 -- when Abort_Signal =>
3840 -- Abort_Undefer;
3841 -- null;
3842 -- end;
3844 -- if not Cancelled (Bnn) then
3845 -- triggered statements
3846 -- end if;
3847 -- end;
3849 -- Build_Simple_Entry_Call is used to expand the all to a simple
3850 -- protected entry call:
3852 -- declare
3853 -- P : E1_Params := (param, param, param);
3854 -- Bnn : Communications_Block;
3856 -- begin
3857 -- Protected_Entry_Call (
3858 -- Object => po._object'Access,
3859 -- E => <entry index>;
3860 -- Uninterpreted_Data => P'Address;
3861 -- Mode => Simple_Call;
3862 -- Block => Bnn);
3863 -- parm := P.param;
3864 -- parm := P.param;
3865 -- ...
3866 -- end;
3868 -- The job is to convert this to the asynchronous form
3870 -- If the trigger is a delay statement, it will have been expanded into a
3871 -- call to one of the GNARL delay procedures. This routine will convert
3872 -- this into a protected entry call on a delay object and then continue
3873 -- processing as for a protected entry call trigger. This requires
3874 -- declaring a Delay_Block object and adding a pointer to this object to
3875 -- the parameter list of the delay procedure to form the parameter list of
3876 -- the entry call. This object is used by the runtime to queue the delay
3877 -- request.
3879 -- For a description of the use of P and the assignments after the
3880 -- call, see Expand_N_Entry_Call_Statement.
3882 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
3883 Loc : constant Source_Ptr := Sloc (N);
3884 Trig : constant Node_Id := Triggering_Alternative (N);
3885 Abrt : constant Node_Id := Abortable_Part (N);
3886 Tstats : constant List_Id := Statements (Trig);
3887 Astats : constant List_Id := Statements (Abrt);
3889 Ecall : Node_Id;
3890 Concval : Node_Id;
3891 Ename : Node_Id;
3892 Index : Node_Id;
3893 Hdle : List_Id;
3894 Decls : List_Id;
3895 Decl : Node_Id;
3896 Parms : List_Id;
3897 Parm : Node_Id;
3898 Call : Node_Id;
3899 Stmts : List_Id;
3900 Enqueue_Call : Node_Id;
3901 Stmt : Node_Id;
3902 B : Entity_Id;
3903 Pdef : Entity_Id;
3904 Dblock_Ent : Entity_Id;
3905 N_Orig : Node_Id;
3906 Abortable_Block : Node_Id;
3907 Cancel_Param : Entity_Id;
3908 Blkent : Entity_Id;
3909 Target_Undefer : RE_Id;
3910 Undefer_Args : List_Id := No_List;
3912 begin
3913 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3914 Ecall := Triggering_Statement (Trig);
3916 -- The arguments in the call may require dynamic allocation, and the
3917 -- call statement may have been transformed into a block. The block
3918 -- may contain additional declarations for internal entities, and the
3919 -- original call is found by sequential search.
3921 if Nkind (Ecall) = N_Block_Statement then
3922 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
3924 while Nkind (Ecall) /= N_Procedure_Call_Statement
3925 and then Nkind (Ecall) /= N_Entry_Call_Statement
3926 loop
3927 Next (Ecall);
3928 end loop;
3929 end if;
3931 -- If a delay was used as a trigger, it will have been expanded
3932 -- into a procedure call. Convert it to the appropriate sequence of
3933 -- statements, similar to what is done for a task entry call.
3934 -- Note that this currently supports only Duration, Real_Time.Time,
3935 -- and Calendar.Time.
3937 if Nkind (Ecall) = N_Procedure_Call_Statement then
3939 -- Add a Delay_Block object to the parameter list of the
3940 -- delay procedure to form the parameter list of the Wait
3941 -- entry call.
3943 Dblock_Ent := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
3945 Pdef := Entity (Name (Ecall));
3947 if Is_RTE (Pdef, RO_CA_Delay_For) then
3948 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
3950 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
3951 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
3953 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
3954 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
3955 end if;
3957 Append_To (Parameter_Associations (Ecall),
3958 Make_Attribute_Reference (Loc,
3959 Prefix => New_Reference_To (Dblock_Ent, Loc),
3960 Attribute_Name => Name_Unchecked_Access));
3962 -- Create the inner block to protect the abortable part
3964 Hdle := New_List (
3965 Make_Exception_Handler (Loc,
3966 Exception_Choices =>
3967 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
3968 Statements => New_List (
3969 Make_Procedure_Call_Statement (Loc,
3970 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)))));
3972 Prepend_To (Astats,
3973 Make_Procedure_Call_Statement (Loc,
3974 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
3976 Abortable_Block :=
3977 Make_Block_Statement (Loc,
3978 Identifier => New_Reference_To (Blkent, Loc),
3979 Handled_Statement_Sequence =>
3980 Make_Handled_Sequence_Of_Statements (Loc,
3981 Statements => Astats),
3982 Has_Created_Identifier => True,
3983 Is_Asynchronous_Call_Block => True);
3985 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
3987 Rewrite (Ecall,
3988 Make_Implicit_If_Statement (N,
3989 Condition => Make_Function_Call (Loc,
3990 Name => Enqueue_Call,
3991 Parameter_Associations => Parameter_Associations (Ecall)),
3992 Then_Statements =>
3993 New_List (Make_Block_Statement (Loc,
3994 Handled_Statement_Sequence =>
3995 Make_Handled_Sequence_Of_Statements (Loc,
3996 Statements => New_List (
3997 Make_Implicit_Label_Declaration (Loc,
3998 Defining_Identifier => Blkent,
3999 Label_Construct => Abortable_Block),
4000 Abortable_Block),
4001 Exception_Handlers => Hdle)))));
4003 Stmts := New_List (Ecall);
4005 -- Construct statement sequence for new block
4007 Append_To (Stmts,
4008 Make_Implicit_If_Statement (N,
4009 Condition => Make_Function_Call (Loc,
4010 Name => New_Reference_To (
4011 RTE (RE_Timed_Out), Loc),
4012 Parameter_Associations => New_List (
4013 Make_Attribute_Reference (Loc,
4014 Prefix => New_Reference_To (Dblock_Ent, Loc),
4015 Attribute_Name => Name_Unchecked_Access))),
4016 Then_Statements => Tstats));
4018 -- The result is the new block
4020 Set_Entry_Cancel_Parameter (Blkent, Dblock_Ent);
4022 Rewrite (N,
4023 Make_Block_Statement (Loc,
4024 Declarations => New_List (
4025 Make_Object_Declaration (Loc,
4026 Defining_Identifier => Dblock_Ent,
4027 Aliased_Present => True,
4028 Object_Definition => New_Reference_To (
4029 RTE (RE_Delay_Block), Loc))),
4031 Handled_Statement_Sequence =>
4032 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
4034 Analyze (N);
4035 return;
4037 else
4038 N_Orig := N;
4039 end if;
4041 Extract_Entry (Ecall, Concval, Ename, Index);
4042 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
4044 Stmts := Statements (Handled_Statement_Sequence (Ecall));
4045 Decls := Declarations (Ecall);
4047 if Is_Protected_Type (Etype (Concval)) then
4049 -- Get the declarations of the block expanded from the entry call
4051 Decl := First (Decls);
4052 while Present (Decl)
4053 and then (Nkind (Decl) /= N_Object_Declaration
4054 or else not Is_RTE
4055 (Etype (Object_Definition (Decl)), RE_Communication_Block))
4056 loop
4057 Next (Decl);
4058 end loop;
4060 pragma Assert (Present (Decl));
4061 Cancel_Param := Defining_Identifier (Decl);
4063 -- Change the mode of the Protected_Entry_Call call.
4064 -- Protected_Entry_Call (
4065 -- Object => po._object'Access,
4066 -- E => <entry index>;
4067 -- Uninterpreted_Data => P'Address;
4068 -- Mode => Asynchronous_Call;
4069 -- Block => Bnn);
4071 Stmt := First (Stmts);
4073 -- Skip assignments to temporaries created for in-out parameters.
4074 -- This makes unwarranted assumptions about the shape of the expanded
4075 -- tree for the call, and should be cleaned up ???
4077 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
4078 Next (Stmt);
4079 end loop;
4081 Call := Stmt;
4083 Parm := First (Parameter_Associations (Call));
4084 while Present (Parm)
4085 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
4086 loop
4087 Next (Parm);
4088 end loop;
4090 pragma Assert (Present (Parm));
4091 Rewrite (Parm, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
4092 Analyze (Parm);
4094 -- Append an if statement to execute the abortable part.
4095 -- if Enqueued (Bnn) then
4097 Append_To (Stmts,
4098 Make_Implicit_If_Statement (N,
4099 Condition => Make_Function_Call (Loc,
4100 Name => New_Reference_To (
4101 RTE (RE_Enqueued), Loc),
4102 Parameter_Associations => New_List (
4103 New_Reference_To (Cancel_Param, Loc))),
4104 Then_Statements => Astats));
4106 Abortable_Block :=
4107 Make_Block_Statement (Loc,
4108 Identifier => New_Reference_To (Blkent, Loc),
4109 Handled_Statement_Sequence =>
4110 Make_Handled_Sequence_Of_Statements (Loc,
4111 Statements => Stmts),
4112 Has_Created_Identifier => True,
4113 Is_Asynchronous_Call_Block => True);
4115 -- For the JVM call Update_Exception instead of Abort_Undefer.
4116 -- See 4jexcept.ads for an explanation.
4118 if Hostparm.Java_VM then
4119 Target_Undefer := RE_Update_Exception;
4120 Undefer_Args :=
4121 New_List (Make_Function_Call (Loc,
4122 Name => New_Occurrence_Of
4123 (RTE (RE_Current_Target_Exception), Loc)));
4124 else
4125 Target_Undefer := RE_Abort_Undefer;
4126 end if;
4128 Stmts := New_List (
4129 Make_Block_Statement (Loc,
4130 Handled_Statement_Sequence =>
4131 Make_Handled_Sequence_Of_Statements (Loc,
4132 Statements => New_List (
4133 Make_Implicit_Label_Declaration (Loc,
4134 Defining_Identifier => Blkent,
4135 Label_Construct => Abortable_Block),
4136 Abortable_Block),
4138 -- exception
4140 Exception_Handlers => New_List (
4141 Make_Exception_Handler (Loc,
4143 -- when Abort_Signal =>
4144 -- Abort_Undefer.all;
4146 Exception_Choices =>
4147 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
4148 Statements => New_List (
4149 Make_Procedure_Call_Statement (Loc,
4150 Name => New_Reference_To (
4151 RTE (Target_Undefer), Loc),
4152 Parameter_Associations => Undefer_Args)))))),
4154 -- if not Cancelled (Bnn) then
4155 -- triggered statements
4156 -- end if;
4158 Make_Implicit_If_Statement (N,
4159 Condition => Make_Op_Not (Loc,
4160 Right_Opnd =>
4161 Make_Function_Call (Loc,
4162 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
4163 Parameter_Associations => New_List (
4164 New_Occurrence_Of (Cancel_Param, Loc)))),
4165 Then_Statements => Tstats));
4167 -- Asynchronous task entry call
4169 else
4170 if No (Decls) then
4171 Decls := New_List;
4172 end if;
4174 B := Make_Defining_Identifier (Loc, Name_uB);
4176 -- Insert declaration of B in declarations of existing block
4178 Prepend_To (Decls,
4179 Make_Object_Declaration (Loc,
4180 Defining_Identifier => B,
4181 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
4183 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
4185 -- Insert declaration of C in declarations of existing block
4187 Prepend_To (Decls,
4188 Make_Object_Declaration (Loc,
4189 Defining_Identifier => Cancel_Param,
4190 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
4192 -- Remove and save the call to Call_Simple
4194 Stmt := First (Stmts);
4196 -- Skip assignments to temporaries created for in-out parameters.
4197 -- This makes unwarranted assumptions about the shape of the expanded
4198 -- tree for the call, and should be cleaned up ???
4200 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
4201 Next (Stmt);
4202 end loop;
4204 Call := Stmt;
4206 -- Create the inner block to protect the abortable part
4208 Hdle := New_List (
4209 Make_Exception_Handler (Loc,
4210 Exception_Choices =>
4211 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
4212 Statements => New_List (
4213 Make_Procedure_Call_Statement (Loc,
4214 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)))));
4216 Prepend_To (Astats,
4217 Make_Procedure_Call_Statement (Loc,
4218 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
4220 Abortable_Block :=
4221 Make_Block_Statement (Loc,
4222 Identifier => New_Reference_To (Blkent, Loc),
4223 Handled_Statement_Sequence =>
4224 Make_Handled_Sequence_Of_Statements (Loc,
4225 Statements => Astats),
4226 Has_Created_Identifier => True,
4227 Is_Asynchronous_Call_Block => True);
4229 Insert_After (Call,
4230 Make_Block_Statement (Loc,
4231 Handled_Statement_Sequence =>
4232 Make_Handled_Sequence_Of_Statements (Loc,
4233 Statements => New_List (
4234 Make_Implicit_Label_Declaration (Loc,
4235 Defining_Identifier => Blkent,
4236 Label_Construct => Abortable_Block),
4237 Abortable_Block),
4238 Exception_Handlers => Hdle)));
4240 -- Create new call statement
4242 Parms := Parameter_Associations (Call);
4243 Append_To (Parms, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
4244 Append_To (Parms, New_Reference_To (B, Loc));
4245 Rewrite (Call,
4246 Make_Procedure_Call_Statement (Loc,
4247 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
4248 Parameter_Associations => Parms));
4250 -- Construct statement sequence for new block
4252 Append_To (Stmts,
4253 Make_Implicit_If_Statement (N,
4254 Condition => Make_Op_Not (Loc,
4255 New_Reference_To (Cancel_Param, Loc)),
4256 Then_Statements => Tstats));
4258 -- Protected the call against abortion
4260 Prepend_To (Stmts,
4261 Make_Procedure_Call_Statement (Loc,
4262 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
4263 Parameter_Associations => Empty_List));
4264 end if;
4266 Set_Entry_Cancel_Parameter (Blkent, Cancel_Param);
4268 -- The result is the new block
4270 Rewrite (N_Orig,
4271 Make_Block_Statement (Loc,
4272 Declarations => Decls,
4273 Handled_Statement_Sequence =>
4274 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
4276 Analyze (N_Orig);
4277 end Expand_N_Asynchronous_Select;
4279 -------------------------------------
4280 -- Expand_N_Conditional_Entry_Call --
4281 -------------------------------------
4283 -- The conditional task entry call is converted to a call to
4284 -- Task_Entry_Call:
4286 -- declare
4287 -- B : Boolean;
4288 -- P : parms := (parm, parm, parm);
4290 -- begin
4291 -- Task_Entry_Call
4292 -- (acceptor-task,
4293 -- entry-index,
4294 -- P'Address,
4295 -- Conditional_Call,
4296 -- B);
4297 -- parm := P.param;
4298 -- parm := P.param;
4299 -- ...
4300 -- if B then
4301 -- normal-statements
4302 -- else
4303 -- else-statements
4304 -- end if;
4305 -- end;
4307 -- For a description of the use of P and the assignments after the
4308 -- call, see Expand_N_Entry_Call_Statement. Note that the entry call
4309 -- of the conditional entry call has already been expanded (by the
4310 -- Expand_N_Entry_Call_Statement procedure) as follows:
4312 -- declare
4313 -- P : parms := (parm, parm, parm);
4314 -- begin
4315 -- ... info for in-out parameters
4316 -- Call_Simple (acceptor-task, entry-index, P'Address);
4317 -- parm := P.param;
4318 -- parm := P.param;
4319 -- ...
4320 -- end;
4322 -- so the task at hand is to convert the latter expansion into the former
4324 -- The conditional protected entry call is converted to a call to
4325 -- Protected_Entry_Call:
4327 -- declare
4328 -- P : parms := (parm, parm, parm);
4329 -- Bnn : Communications_Block;
4331 -- begin
4332 -- Protected_Entry_Call (
4333 -- Object => po._object'Access,
4334 -- E => <entry index>;
4335 -- Uninterpreted_Data => P'Address;
4336 -- Mode => Conditional_Call;
4337 -- Block => Bnn);
4338 -- parm := P.param;
4339 -- parm := P.param;
4340 -- ...
4341 -- if Cancelled (Bnn) then
4342 -- else-statements
4343 -- else
4344 -- normal-statements
4345 -- end if;
4346 -- end;
4348 -- As for tasks, the entry call of the conditional entry call has
4349 -- already been expanded (by the Expand_N_Entry_Call_Statement procedure)
4350 -- as follows:
4352 -- declare
4353 -- P : E1_Params := (param, param, param);
4354 -- Bnn : Communications_Block;
4356 -- begin
4357 -- Protected_Entry_Call (
4358 -- Object => po._object'Access,
4359 -- E => <entry index>;
4360 -- Uninterpreted_Data => P'Address;
4361 -- Mode => Simple_Call;
4362 -- Block => Bnn);
4363 -- parm := P.param;
4364 -- parm := P.param;
4365 -- ...
4366 -- end;
4368 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
4369 Loc : constant Source_Ptr := Sloc (N);
4370 Alt : constant Node_Id := Entry_Call_Alternative (N);
4371 Blk : Node_Id := Entry_Call_Statement (Alt);
4372 Transient_Blk : Node_Id;
4374 Parms : List_Id;
4375 Parm : Node_Id;
4376 Call : Node_Id;
4377 Stmts : List_Id;
4378 B : Entity_Id;
4379 Decl : Node_Id;
4380 Stmt : Node_Id;
4382 begin
4383 -- As described above, The entry alternative is transformed into a
4384 -- block that contains the gnulli call, and possibly assignment
4385 -- statements for in-out parameters. The gnulli call may itself be
4386 -- rewritten into a transient block if some unconstrained parameters
4387 -- require it. We need to retrieve the call to complete its parameter
4388 -- list.
4390 Transient_Blk :=
4391 First_Real_Statement (Handled_Statement_Sequence (Blk));
4393 if Present (Transient_Blk)
4394 and then
4395 Nkind (Transient_Blk) = N_Block_Statement
4396 then
4397 Blk := Transient_Blk;
4398 end if;
4400 Stmts := Statements (Handled_Statement_Sequence (Blk));
4402 Stmt := First (Stmts);
4404 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
4405 Next (Stmt);
4406 end loop;
4408 Call := Stmt;
4410 Parms := Parameter_Associations (Call);
4412 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
4414 -- Substitute Conditional_Entry_Call for Simple_Call
4415 -- parameter.
4417 Parm := First (Parms);
4418 while Present (Parm)
4419 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
4420 loop
4421 Next (Parm);
4422 end loop;
4424 pragma Assert (Present (Parm));
4425 Rewrite (Parm, New_Reference_To (RTE (RE_Conditional_Call), Loc));
4427 Analyze (Parm);
4429 -- Find the Communication_Block parameter for the call
4430 -- to the Cancelled function.
4432 Decl := First (Declarations (Blk));
4433 while Present (Decl)
4434 and then not
4435 Is_RTE (Etype (Object_Definition (Decl)), RE_Communication_Block)
4436 loop
4437 Next (Decl);
4438 end loop;
4440 -- Add an if statement to execute the else part if the call
4441 -- does not succeed (as indicated by the Cancelled predicate).
4443 Append_To (Stmts,
4444 Make_Implicit_If_Statement (N,
4445 Condition => Make_Function_Call (Loc,
4446 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
4447 Parameter_Associations => New_List (
4448 New_Reference_To (Defining_Identifier (Decl), Loc))),
4449 Then_Statements => Else_Statements (N),
4450 Else_Statements => Statements (Alt)));
4452 else
4453 B := Make_Defining_Identifier (Loc, Name_uB);
4455 -- Insert declaration of B in declarations of existing block
4457 if No (Declarations (Blk)) then
4458 Set_Declarations (Blk, New_List);
4459 end if;
4461 Prepend_To (Declarations (Blk),
4462 Make_Object_Declaration (Loc,
4463 Defining_Identifier => B,
4464 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
4466 -- Create new call statement
4468 Append_To (Parms, New_Reference_To (RTE (RE_Conditional_Call), Loc));
4469 Append_To (Parms, New_Reference_To (B, Loc));
4471 Rewrite (Call,
4472 Make_Procedure_Call_Statement (Loc,
4473 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
4474 Parameter_Associations => Parms));
4476 -- Construct statement sequence for new block
4478 Append_To (Stmts,
4479 Make_Implicit_If_Statement (N,
4480 Condition => New_Reference_To (B, Loc),
4481 Then_Statements => Statements (Alt),
4482 Else_Statements => Else_Statements (N)));
4484 end if;
4486 -- The result is the new block
4488 Rewrite (N,
4489 Make_Block_Statement (Loc,
4490 Declarations => Declarations (Blk),
4491 Handled_Statement_Sequence =>
4492 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
4494 Analyze (N);
4495 end Expand_N_Conditional_Entry_Call;
4497 ---------------------------------------
4498 -- Expand_N_Delay_Relative_Statement --
4499 ---------------------------------------
4501 -- Delay statement is implemented as a procedure call to Delay_For
4502 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
4503 -- simple delays imposed by the use of Protected Objects.
4505 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
4506 Loc : constant Source_Ptr := Sloc (N);
4508 begin
4509 Rewrite (N,
4510 Make_Procedure_Call_Statement (Loc,
4511 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
4512 Parameter_Associations => New_List (Expression (N))));
4513 Analyze (N);
4514 end Expand_N_Delay_Relative_Statement;
4516 ------------------------------------
4517 -- Expand_N_Delay_Until_Statement --
4518 ------------------------------------
4520 -- Delay Until statement is implemented as a procedure call to
4521 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
4523 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
4524 Loc : constant Source_Ptr := Sloc (N);
4525 Typ : Entity_Id;
4527 begin
4528 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
4529 Typ := RTE (RO_CA_Delay_Until);
4530 else
4531 Typ := RTE (RO_RT_Delay_Until);
4532 end if;
4534 Rewrite (N,
4535 Make_Procedure_Call_Statement (Loc,
4536 Name => New_Reference_To (Typ, Loc),
4537 Parameter_Associations => New_List (Expression (N))));
4539 Analyze (N);
4540 end Expand_N_Delay_Until_Statement;
4542 -------------------------
4543 -- Expand_N_Entry_Body --
4544 -------------------------
4546 procedure Expand_N_Entry_Body (N : Node_Id) is
4547 Loc : constant Source_Ptr := Sloc (N);
4548 Dec : constant Node_Id := Parent (Current_Scope);
4549 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
4550 Index_Spec : constant Node_Id :=
4551 Entry_Index_Specification (Ent_Formals);
4552 Next_Op : Node_Id;
4553 First_Decl : constant Node_Id := First (Declarations (N));
4554 Index_Decl : List_Id;
4556 begin
4557 -- Add the renamings for private declarations and discriminants
4559 Add_Discriminal_Declarations
4560 (Declarations (N), Defining_Identifier (Dec), Name_uObject, Loc);
4561 Add_Private_Declarations
4562 (Declarations (N), Defining_Identifier (Dec), Name_uObject, Loc);
4564 if Present (Index_Spec) then
4565 Index_Decl :=
4566 Index_Constant_Declaration
4568 Defining_Identifier (Index_Spec), Defining_Identifier (Dec));
4570 -- If the entry has local declarations, insert index declaration
4571 -- before them, because the index may be used therein.
4573 if Present (First_Decl) then
4574 Insert_List_Before (First_Decl, Index_Decl);
4575 else
4576 Append_List_To (Declarations (N), Index_Decl);
4577 end if;
4578 end if;
4580 -- Associate privals and discriminals with the next protected
4581 -- operation body to be expanded. These are used to expand
4582 -- references to private data objects and discriminants,
4583 -- respectively.
4585 Next_Op := Next_Protected_Operation (N);
4587 if Present (Next_Op) then
4588 Set_Privals (Dec, Next_Op, Loc);
4589 Set_Discriminals (Dec);
4590 end if;
4591 end Expand_N_Entry_Body;
4593 -----------------------------------
4594 -- Expand_N_Entry_Call_Statement --
4595 -----------------------------------
4597 -- An entry call is expanded into GNARLI calls to implement
4598 -- a simple entry call (see Build_Simple_Entry_Call).
4600 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
4601 Concval : Node_Id;
4602 Ename : Node_Id;
4603 Index : Node_Id;
4605 begin
4606 if No_Run_Time_Mode then
4607 Error_Msg_CRT ("entry call", N);
4608 return;
4609 end if;
4611 -- If this entry call is part of an asynchronous select, don't
4612 -- expand it here; it will be expanded with the select statement.
4613 -- Don't expand timed entry calls either, as they are translated
4614 -- into asynchronous entry calls.
4616 -- ??? This whole approach is questionable; it may be better
4617 -- to go back to allowing the expansion to take place and then
4618 -- attempting to fix it up in Expand_N_Asynchronous_Select.
4619 -- The tricky part is figuring out whether the expanded
4620 -- call is on a task or protected entry.
4622 if (Nkind (Parent (N)) /= N_Triggering_Alternative
4623 or else N /= Triggering_Statement (Parent (N)))
4624 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
4625 or else N /= Entry_Call_Statement (Parent (N))
4626 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
4627 then
4628 Extract_Entry (N, Concval, Ename, Index);
4629 Build_Simple_Entry_Call (N, Concval, Ename, Index);
4630 end if;
4631 end Expand_N_Entry_Call_Statement;
4633 --------------------------------
4634 -- Expand_N_Entry_Declaration --
4635 --------------------------------
4637 -- If there are parameters, then first, each of the formals is marked
4638 -- by setting Is_Entry_Formal. Next a record type is built which is
4639 -- used to hold the parameter values. The name of this record type is
4640 -- entryP where entry is the name of the entry, with an additional
4641 -- corresponding access type called entryPA. The record type has matching
4642 -- components for each formal (the component names are the same as the
4643 -- formal names). For elementary types, the component type matches the
4644 -- formal type. For composite types, an access type is declared (with
4645 -- the name formalA) which designates the formal type, and the type of
4646 -- the component is this access type. Finally the Entry_Component of
4647 -- each formal is set to reference the corresponding record component.
4649 procedure Expand_N_Entry_Declaration (N : Node_Id) is
4650 Loc : constant Source_Ptr := Sloc (N);
4651 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
4652 Components : List_Id;
4653 Formal : Node_Id;
4654 Ftype : Entity_Id;
4655 Last_Decl : Node_Id;
4656 Component : Entity_Id;
4657 Ctype : Entity_Id;
4658 Decl : Node_Id;
4659 Rec_Ent : Entity_Id;
4660 Acc_Ent : Entity_Id;
4662 begin
4663 Formal := First_Formal (Entry_Ent);
4664 Last_Decl := N;
4666 -- Most processing is done only if parameters are present
4668 if Present (Formal) then
4669 Components := New_List;
4671 -- Loop through formals
4673 while Present (Formal) loop
4674 Set_Is_Entry_Formal (Formal);
4675 Component :=
4676 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
4677 Set_Entry_Component (Formal, Component);
4678 Set_Entry_Formal (Component, Formal);
4679 Ftype := Etype (Formal);
4681 -- Declare new access type and then append
4683 Ctype :=
4684 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4686 Decl :=
4687 Make_Full_Type_Declaration (Loc,
4688 Defining_Identifier => Ctype,
4689 Type_Definition =>
4690 Make_Access_To_Object_Definition (Loc,
4691 All_Present => True,
4692 Constant_Present => Ekind (Formal) = E_In_Parameter,
4693 Subtype_Indication => New_Reference_To (Ftype, Loc)));
4695 Insert_After (Last_Decl, Decl);
4696 Last_Decl := Decl;
4698 Append_To (Components,
4699 Make_Component_Declaration (Loc,
4700 Defining_Identifier => Component,
4701 Component_Definition =>
4702 Make_Component_Definition (Loc,
4703 Aliased_Present => False,
4704 Subtype_Indication => New_Reference_To (Ctype, Loc))));
4706 Next_Formal_With_Extras (Formal);
4707 end loop;
4709 -- Create the Entry_Parameter_Record declaration
4711 Rec_Ent :=
4712 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
4714 Decl :=
4715 Make_Full_Type_Declaration (Loc,
4716 Defining_Identifier => Rec_Ent,
4717 Type_Definition =>
4718 Make_Record_Definition (Loc,
4719 Component_List =>
4720 Make_Component_List (Loc,
4721 Component_Items => Components)));
4723 Insert_After (Last_Decl, Decl);
4724 Last_Decl := Decl;
4726 -- Construct and link in the corresponding access type
4728 Acc_Ent :=
4729 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4731 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
4733 Decl :=
4734 Make_Full_Type_Declaration (Loc,
4735 Defining_Identifier => Acc_Ent,
4736 Type_Definition =>
4737 Make_Access_To_Object_Definition (Loc,
4738 All_Present => True,
4739 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
4741 Insert_After (Last_Decl, Decl);
4742 Last_Decl := Decl;
4743 end if;
4744 end Expand_N_Entry_Declaration;
4746 -----------------------------
4747 -- Expand_N_Protected_Body --
4748 -----------------------------
4750 -- Protected bodies are expanded to the completion of the subprograms
4751 -- created for the corresponding protected type. These are a protected
4752 -- and unprotected version of each protected subprogram in the object,
4753 -- a function to calculate each entry barrier, and a procedure to
4754 -- execute the sequence of statements of each protected entry body.
4755 -- For example, for protected type ptype:
4757 -- function entB
4758 -- (O : System.Address;
4759 -- E : Protected_Entry_Index)
4760 -- return Boolean
4761 -- is
4762 -- <discriminant renamings>
4763 -- <private object renamings>
4764 -- begin
4765 -- return <barrier expression>;
4766 -- end entB;
4768 -- procedure pprocN (_object : in out poV;...) is
4769 -- <discriminant renamings>
4770 -- <private object renamings>
4771 -- begin
4772 -- <sequence of statements>
4773 -- end pprocN;
4775 -- procedure pproc (_object : in out poV;...) is
4776 -- procedure _clean is
4777 -- Pn : Boolean;
4778 -- begin
4779 -- ptypeS (_object, Pn);
4780 -- Unlock (_object._object'Access);
4781 -- Abort_Undefer.all;
4782 -- end _clean;
4784 -- begin
4785 -- Abort_Defer.all;
4786 -- Lock (_object._object'Access);
4787 -- pprocN (_object;...);
4788 -- at end
4789 -- _clean;
4790 -- end pproc;
4792 -- function pfuncN (_object : poV;...) return Return_Type is
4793 -- <discriminant renamings>
4794 -- <private object renamings>
4795 -- begin
4796 -- <sequence of statements>
4797 -- end pfuncN;
4799 -- function pfunc (_object : poV) return Return_Type is
4800 -- procedure _clean is
4801 -- begin
4802 -- Unlock (_object._object'Access);
4803 -- Abort_Undefer.all;
4804 -- end _clean;
4806 -- begin
4807 -- Abort_Defer.all;
4808 -- Lock (_object._object'Access);
4809 -- return pfuncN (_object);
4811 -- at end
4812 -- _clean;
4813 -- end pfunc;
4815 -- procedure entE
4816 -- (O : System.Address;
4817 -- P : System.Address;
4818 -- E : Protected_Entry_Index)
4819 -- is
4820 -- <discriminant renamings>
4821 -- <private object renamings>
4822 -- type poVP is access poV;
4823 -- _Object : ptVP := ptVP!(O);
4825 -- begin
4826 -- begin
4827 -- <statement sequence>
4828 -- Complete_Entry_Body (_Object._Object);
4829 -- exception
4830 -- when all others =>
4831 -- Exceptional_Complete_Entry_Body (
4832 -- _Object._Object, Get_GNAT_Exception);
4833 -- end;
4834 -- end entE;
4836 -- The type poV is the record created for the protected type to hold
4837 -- the state of the protected object.
4839 procedure Expand_N_Protected_Body (N : Node_Id) is
4840 Pid : constant Entity_Id := Corresponding_Spec (N);
4841 Has_Entries : Boolean := False;
4842 Op_Decl : Node_Id;
4843 Op_Body : Node_Id;
4844 Op_Id : Entity_Id;
4845 New_Op_Body : Node_Id;
4846 Current_Node : Node_Id;
4847 Num_Entries : Natural := 0;
4849 begin
4850 if No_Run_Time_Mode then
4851 Error_Msg_CRT ("protected body", N);
4852 return;
4853 end if;
4855 if Nkind (Parent (N)) = N_Subunit then
4857 -- This is the proper body corresponding to a stub. The declarations
4858 -- must be inserted at the point of the stub, which is in the decla-
4859 -- rative part of the parent unit.
4861 Current_Node := Corresponding_Stub (Parent (N));
4863 else
4864 Current_Node := N;
4865 end if;
4867 Op_Body := First (Declarations (N));
4869 -- The protected body is replaced with the bodies of its
4870 -- protected operations, and the declarations for internal objects
4871 -- that may have been created for entry family bounds.
4873 Rewrite (N, Make_Null_Statement (Sloc (N)));
4874 Analyze (N);
4876 while Present (Op_Body) loop
4877 case Nkind (Op_Body) is
4878 when N_Subprogram_Declaration =>
4879 null;
4881 when N_Subprogram_Body =>
4883 -- Exclude functions created to analyze defaults
4885 if not Is_Eliminated (Defining_Entity (Op_Body))
4886 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
4887 then
4888 New_Op_Body :=
4889 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
4891 Insert_After (Current_Node, New_Op_Body);
4892 Current_Node := New_Op_Body;
4893 Analyze (New_Op_Body);
4895 Update_Prival_Subtypes (New_Op_Body);
4897 -- Build the corresponding protected operation only if
4898 -- this is a visible operation of the type, or if it is
4899 -- an interrupt handler. Otherwise it is only callable
4900 -- from within the object, and the unprotected version
4901 -- is sufficient.
4903 if Present (Corresponding_Spec (Op_Body)) then
4904 Op_Decl :=
4905 Unit_Declaration_Node (Corresponding_Spec (Op_Body));
4907 if Nkind (Parent (Op_Decl)) = N_Protected_Definition
4908 and then
4909 (List_Containing (Op_Decl) =
4910 Visible_Declarations (Parent (Op_Decl))
4911 or else
4912 Is_Interrupt_Handler
4913 (Corresponding_Spec (Op_Body)))
4914 then
4915 New_Op_Body :=
4916 Build_Protected_Subprogram_Body (
4917 Op_Body, Pid, Specification (New_Op_Body));
4919 Insert_After (Current_Node, New_Op_Body);
4920 Analyze (New_Op_Body);
4921 end if;
4922 end if;
4923 end if;
4925 when N_Entry_Body =>
4926 Op_Id := Defining_Identifier (Op_Body);
4927 Has_Entries := True;
4928 Num_Entries := Num_Entries + 1;
4930 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
4932 Insert_After (Current_Node, New_Op_Body);
4933 Current_Node := New_Op_Body;
4934 Analyze (New_Op_Body);
4936 Update_Prival_Subtypes (New_Op_Body);
4938 when N_Implicit_Label_Declaration =>
4939 null;
4941 when N_Itype_Reference =>
4942 Insert_After (Current_Node, New_Copy (Op_Body));
4944 when N_Freeze_Entity =>
4945 New_Op_Body := New_Copy (Op_Body);
4947 if Present (Entity (Op_Body))
4948 and then Freeze_Node (Entity (Op_Body)) = Op_Body
4949 then
4950 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
4951 end if;
4953 Insert_After (Current_Node, New_Op_Body);
4954 Current_Node := New_Op_Body;
4955 Analyze (New_Op_Body);
4957 when N_Pragma =>
4958 New_Op_Body := New_Copy (Op_Body);
4959 Insert_After (Current_Node, New_Op_Body);
4960 Current_Node := New_Op_Body;
4961 Analyze (New_Op_Body);
4963 when N_Object_Declaration =>
4964 pragma Assert (not Comes_From_Source (Op_Body));
4965 New_Op_Body := New_Copy (Op_Body);
4966 Insert_After (Current_Node, New_Op_Body);
4967 Current_Node := New_Op_Body;
4968 Analyze (New_Op_Body);
4970 when others =>
4971 raise Program_Error;
4973 end case;
4975 Next (Op_Body);
4976 end loop;
4978 -- Finally, create the body of the function that maps an entry index
4979 -- into the corresponding body index, except when there is no entry,
4980 -- or in a ravenscar-like profile (no abort, no entry queue, 1 entry)
4982 if Has_Entries
4983 and then (Abort_Allowed
4984 or else Restriction_Active (No_Entry_Queue) = False
4985 or else Num_Entries > 1)
4986 then
4987 New_Op_Body := Build_Find_Body_Index (Pid);
4988 Insert_After (Current_Node, New_Op_Body);
4989 Analyze (New_Op_Body);
4990 end if;
4991 end Expand_N_Protected_Body;
4993 -----------------------------------------
4994 -- Expand_N_Protected_Type_Declaration --
4995 -----------------------------------------
4997 -- First we create a corresponding record type declaration used to
4998 -- represent values of this protected type.
4999 -- The general form of this type declaration is
5001 -- type poV (discriminants) is record
5002 -- _Object : aliased <kind>Protection
5003 -- [(<entry count> [, <handler count>])];
5004 -- [entry_family : array (bounds) of Void;]
5005 -- <private data fields>
5006 -- end record;
5008 -- The discriminants are present only if the corresponding protected
5009 -- type has discriminants, and they exactly mirror the protected type
5010 -- discriminants. The private data fields similarly mirror the
5011 -- private declarations of the protected type.
5013 -- The Object field is always present. It contains RTS specific data
5014 -- used to control the protected object. It is declared as Aliased
5015 -- so that it can be passed as a pointer to the RTS. This allows the
5016 -- protected record to be referenced within RTS data structures.
5017 -- An appropriate Protection type and discriminant are generated.
5019 -- The Service field is present for protected objects with entries. It
5020 -- contains sufficient information to allow the entry service procedure
5021 -- for this object to be called when the object is not known till runtime.
5023 -- One entry_family component is present for each entry family in the
5024 -- task definition (see Expand_N_Task_Type_Declaration).
5026 -- When a protected object is declared, an instance of the protected type
5027 -- value record is created. The elaboration of this declaration creates
5028 -- the correct bounds for the entry families, and also evaluates the
5029 -- priority expression if needed. The initialization routine for
5030 -- the protected type itself then calls Initialize_Protection with
5031 -- appropriate parameters to initialize the value of the Task_Id field.
5032 -- Install_Handlers may be also called if a pragma Attach_Handler applies.
5034 -- Note: this record is passed to the subprograms created by the
5035 -- expansion of protected subprograms and entries. It is an in parameter
5036 -- to protected functions and an in out parameter to procedures and
5037 -- entry bodies. The Entity_Id for this created record type is placed
5038 -- in the Corresponding_Record_Type field of the associated protected
5039 -- type entity.
5041 -- Next we create a procedure specifications for protected subprograms
5042 -- and entry bodies. For each protected subprograms two subprograms are
5043 -- created, an unprotected and a protected version. The unprotected
5044 -- version is called from within other operations of the same protected
5045 -- object.
5047 -- We also build the call to register the procedure if a pragma
5048 -- Interrupt_Handler applies.
5050 -- A single subprogram is created to service all entry bodies; it has an
5051 -- additional boolean out parameter indicating that the previous entry
5052 -- call made by the current task was serviced immediately, i.e. not by
5053 -- proxy. The O parameter contains a pointer to a record object of the
5054 -- type described above. An untyped interface is used here to allow this
5055 -- procedure to be called in places where the type of the object to be
5056 -- serviced is not known. This must be done, for example, when a call
5057 -- that may have been requeued is cancelled; the corresponding object
5058 -- must be serviced, but which object that is not known till runtime.
5060 -- procedure ptypeS
5061 -- (O : System.Address; P : out Boolean);
5062 -- procedure pprocN (_object : in out poV);
5063 -- procedure pproc (_object : in out poV);
5064 -- function pfuncN (_object : poV);
5065 -- function pfunc (_object : poV);
5066 -- ...
5068 -- Note that this must come after the record type declaration, since
5069 -- the specs refer to this type.
5071 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
5072 Loc : constant Source_Ptr := Sloc (N);
5073 Prottyp : constant Entity_Id := Defining_Identifier (N);
5074 Protnm : constant Name_Id := Chars (Prottyp);
5076 Pdef : constant Node_Id := Protected_Definition (N);
5077 -- This contains two lists; one for visible and one for private decls
5079 Rec_Decl : Node_Id;
5080 Cdecls : List_Id;
5081 Discr_Map : constant Elist_Id := New_Elmt_List;
5082 Priv : Node_Id;
5083 Pent : Entity_Id;
5084 New_Priv : Node_Id;
5085 Comp : Node_Id;
5086 Comp_Id : Entity_Id;
5087 Sub : Node_Id;
5088 Current_Node : Node_Id := N;
5089 Bdef : Entity_Id := Empty; -- avoid uninit warning
5090 Edef : Entity_Id := Empty; -- avoid uninit warning
5091 Entries_Aggr : Node_Id;
5092 Body_Id : Entity_Id;
5093 Body_Arr : Node_Id;
5094 E_Count : Int;
5095 Object_Comp : Node_Id;
5097 procedure Register_Handler;
5098 -- for a protected operation that is an interrupt handler, add the
5099 -- freeze action that will register it as such.
5101 ----------------------
5102 -- Register_Handler --
5103 ----------------------
5105 procedure Register_Handler is
5107 -- All semantic checks already done in Sem_Prag
5109 Prot_Proc : constant Entity_Id :=
5110 Defining_Unit_Name
5111 (Specification (Current_Node));
5113 Proc_Address : constant Node_Id :=
5114 Make_Attribute_Reference (Loc,
5115 Prefix => New_Reference_To (Prot_Proc, Loc),
5116 Attribute_Name => Name_Address);
5118 RTS_Call : constant Entity_Id :=
5119 Make_Procedure_Call_Statement (Loc,
5120 Name =>
5121 New_Reference_To (
5122 RTE (RE_Register_Interrupt_Handler), Loc),
5123 Parameter_Associations =>
5124 New_List (Proc_Address));
5125 begin
5126 Append_Freeze_Action (Prot_Proc, RTS_Call);
5127 end Register_Handler;
5129 -- Start of processing for Expand_N_Protected_Type_Declaration
5131 begin
5132 if Present (Corresponding_Record_Type (Prottyp)) then
5133 return;
5134 else
5135 Rec_Decl := Build_Corresponding_Record (N, Prottyp, Loc);
5136 Cdecls := Component_Items
5137 (Component_List (Type_Definition (Rec_Decl)));
5138 end if;
5140 Qualify_Entity_Names (N);
5142 -- If the type has discriminants, their occurrences in the declaration
5143 -- have been replaced by the corresponding discriminals. For components
5144 -- that are constrained by discriminants, their homologues in the
5145 -- corresponding record type must refer to the discriminants of that
5146 -- record, so we must apply a new renaming to subtypes_indications:
5148 -- protected discriminant => discriminal => record discriminant.
5149 -- This replacement is not applied to default expressions, for which
5150 -- the discriminal is correct.
5152 if Has_Discriminants (Prottyp) then
5153 declare
5154 Disc : Entity_Id;
5155 Decl : Node_Id;
5157 begin
5158 Disc := First_Discriminant (Prottyp);
5159 Decl := First (Discriminant_Specifications (Rec_Decl));
5161 while Present (Disc) loop
5162 Append_Elmt (Discriminal (Disc), Discr_Map);
5163 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
5164 Next_Discriminant (Disc);
5165 Next (Decl);
5166 end loop;
5167 end;
5168 end if;
5170 -- Fill in the component declarations
5172 -- Add components for entry families. For each entry family,
5173 -- create an anonymous type declaration with the same size, and
5174 -- analyze the type.
5176 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prottyp);
5178 -- Prepend the _Object field with the right type to the component
5179 -- list. We need to compute the number of entries, and in some cases
5180 -- the number of Attach_Handler pragmas.
5182 declare
5183 Ritem : Node_Id;
5184 Num_Attach_Handler : Int := 0;
5185 Protection_Subtype : Node_Id;
5186 Entry_Count_Expr : constant Node_Id :=
5187 Build_Entry_Count_Expression
5188 (Prottyp, Cdecls, Loc);
5190 begin
5191 if Has_Attach_Handler (Prottyp) then
5192 Ritem := First_Rep_Item (Prottyp);
5193 while Present (Ritem) loop
5194 if Nkind (Ritem) = N_Pragma
5195 and then Chars (Ritem) = Name_Attach_Handler
5196 then
5197 Num_Attach_Handler := Num_Attach_Handler + 1;
5198 end if;
5200 Next_Rep_Item (Ritem);
5201 end loop;
5203 if Restricted_Profile then
5204 if Has_Entries (Prottyp) then
5205 Protection_Subtype :=
5206 New_Reference_To (RTE (RE_Protection_Entry), Loc);
5207 else
5208 Protection_Subtype :=
5209 New_Reference_To (RTE (RE_Protection), Loc);
5210 end if;
5211 else
5212 Protection_Subtype :=
5213 Make_Subtype_Indication
5214 (Sloc => Loc,
5215 Subtype_Mark =>
5216 New_Reference_To
5217 (RTE (RE_Static_Interrupt_Protection), Loc),
5218 Constraint =>
5219 Make_Index_Or_Discriminant_Constraint (
5220 Sloc => Loc,
5221 Constraints => New_List (
5222 Entry_Count_Expr,
5223 Make_Integer_Literal (Loc, Num_Attach_Handler))));
5224 end if;
5226 elsif Has_Interrupt_Handler (Prottyp) then
5227 Protection_Subtype :=
5228 Make_Subtype_Indication (
5229 Sloc => Loc,
5230 Subtype_Mark => New_Reference_To
5231 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
5232 Constraint =>
5233 Make_Index_Or_Discriminant_Constraint (
5234 Sloc => Loc,
5235 Constraints => New_List (Entry_Count_Expr)));
5237 elsif Has_Entries (Prottyp) then
5238 if Abort_Allowed
5239 or else Restriction_Active (No_Entry_Queue) = False
5240 or else Number_Entries (Prottyp) > 1
5241 then
5242 Protection_Subtype :=
5243 Make_Subtype_Indication (
5244 Sloc => Loc,
5245 Subtype_Mark =>
5246 New_Reference_To (RTE (RE_Protection_Entries), Loc),
5247 Constraint =>
5248 Make_Index_Or_Discriminant_Constraint (
5249 Sloc => Loc,
5250 Constraints => New_List (Entry_Count_Expr)));
5252 else
5253 Protection_Subtype :=
5254 New_Reference_To (RTE (RE_Protection_Entry), Loc);
5255 end if;
5257 else
5258 Protection_Subtype := New_Reference_To (RTE (RE_Protection), Loc);
5259 end if;
5261 Object_Comp :=
5262 Make_Component_Declaration (Loc,
5263 Defining_Identifier =>
5264 Make_Defining_Identifier (Loc, Name_uObject),
5265 Component_Definition =>
5266 Make_Component_Definition (Loc,
5267 Aliased_Present => True,
5268 Subtype_Indication => Protection_Subtype));
5269 end;
5271 pragma Assert (Present (Pdef));
5273 -- Add private field components
5275 if Present (Private_Declarations (Pdef)) then
5276 Priv := First (Private_Declarations (Pdef));
5278 while Present (Priv) loop
5280 if Nkind (Priv) = N_Component_Declaration then
5281 Pent := Defining_Identifier (Priv);
5282 New_Priv :=
5283 Make_Component_Declaration (Loc,
5284 Defining_Identifier =>
5285 Make_Defining_Identifier (Sloc (Pent), Chars (Pent)),
5286 Component_Definition =>
5287 Make_Component_Definition (Sloc (Pent),
5288 Aliased_Present => False,
5289 Subtype_Indication =>
5290 New_Copy_Tree (Subtype_Indication
5291 (Component_Definition (Priv)),
5292 Discr_Map)),
5293 Expression => Expression (Priv));
5295 Append_To (Cdecls, New_Priv);
5297 elsif Nkind (Priv) = N_Subprogram_Declaration then
5299 -- Make the unprotected version of the subprogram available
5300 -- for expansion of intra object calls. There is need for
5301 -- a protected version only if the subprogram is an interrupt
5302 -- handler, otherwise this operation can only be called from
5303 -- within the body.
5305 Sub :=
5306 Make_Subprogram_Declaration (Loc,
5307 Specification =>
5308 Build_Protected_Sub_Specification
5309 (Priv, Prottyp, Unprotected => True));
5311 Insert_After (Current_Node, Sub);
5312 Analyze (Sub);
5314 Set_Protected_Body_Subprogram
5315 (Defining_Unit_Name (Specification (Priv)),
5316 Defining_Unit_Name (Specification (Sub)));
5318 Current_Node := Sub;
5319 if Is_Interrupt_Handler
5320 (Defining_Unit_Name (Specification (Priv)))
5321 then
5322 Sub :=
5323 Make_Subprogram_Declaration (Loc,
5324 Specification =>
5325 Build_Protected_Sub_Specification
5326 (Priv, Prottyp, Unprotected => False));
5328 Insert_After (Current_Node, Sub);
5329 Analyze (Sub);
5330 Current_Node := Sub;
5332 if not Restricted_Profile then
5333 Register_Handler;
5334 end if;
5335 end if;
5336 end if;
5338 Next (Priv);
5339 end loop;
5340 end if;
5342 -- Put the _Object component after the private component so that it
5343 -- be finalized early as required by 9.4 (20)
5345 Append_To (Cdecls, Object_Comp);
5347 Insert_After (Current_Node, Rec_Decl);
5348 Current_Node := Rec_Decl;
5350 -- Analyze the record declaration immediately after construction,
5351 -- because the initialization procedure is needed for single object
5352 -- declarations before the next entity is analyzed (the freeze call
5353 -- that generates this initialization procedure is found below).
5355 Analyze (Rec_Decl, Suppress => All_Checks);
5357 -- Collect pointers to entry bodies and their barriers, to be placed
5358 -- in the Entry_Bodies_Array for the type. For each entry/family we
5359 -- add an expression to the aggregate which is the initial value of
5360 -- this array. The array is declared after all protected subprograms.
5362 if Has_Entries (Prottyp) then
5363 Entries_Aggr :=
5364 Make_Aggregate (Loc, Expressions => New_List);
5366 else
5367 Entries_Aggr := Empty;
5368 end if;
5370 -- Build two new procedure specifications for each protected
5371 -- subprogram; one to call from outside the object and one to
5372 -- call from inside. Build a barrier function and an entry
5373 -- body action procedure specification for each protected entry.
5374 -- Initialize the entry body array. If subprogram is flagged as
5375 -- eliminated, do not generate any internal operations.
5377 E_Count := 0;
5379 Comp := First (Visible_Declarations (Pdef));
5381 while Present (Comp) loop
5382 if Nkind (Comp) = N_Subprogram_Declaration
5383 and then not Is_Eliminated (Defining_Entity (Comp))
5384 then
5385 Sub :=
5386 Make_Subprogram_Declaration (Loc,
5387 Specification =>
5388 Build_Protected_Sub_Specification
5389 (Comp, Prottyp, Unprotected => True));
5391 Insert_After (Current_Node, Sub);
5392 Analyze (Sub);
5394 Set_Protected_Body_Subprogram
5395 (Defining_Unit_Name (Specification (Comp)),
5396 Defining_Unit_Name (Specification (Sub)));
5398 -- Make the protected version of the subprogram available
5399 -- for expansion of external calls.
5401 Current_Node := Sub;
5403 Sub :=
5404 Make_Subprogram_Declaration (Loc,
5405 Specification =>
5406 Build_Protected_Sub_Specification
5407 (Comp, Prottyp, Unprotected => False));
5409 Insert_After (Current_Node, Sub);
5410 Analyze (Sub);
5411 Current_Node := Sub;
5413 -- If a pragma Interrupt_Handler applies, build and add
5414 -- a call to Register_Interrupt_Handler to the freezing actions
5415 -- of the protected version (Current_Node) of the subprogram:
5416 -- system.interrupts.register_interrupt_handler
5417 -- (prot_procP'address);
5419 if not Restricted_Profile
5420 and then Is_Interrupt_Handler
5421 (Defining_Unit_Name (Specification (Comp)))
5422 then
5423 Register_Handler;
5424 end if;
5426 elsif Nkind (Comp) = N_Entry_Declaration then
5427 E_Count := E_Count + 1;
5428 Comp_Id := Defining_Identifier (Comp);
5429 Set_Privals_Chain (Comp_Id, New_Elmt_List);
5430 Edef :=
5431 Make_Defining_Identifier (Loc,
5432 Build_Selected_Name
5433 (Protnm,
5434 New_External_Name (Chars (Comp_Id), Suffix_Index => -1),
5435 'E'));
5436 Sub :=
5437 Make_Subprogram_Declaration (Loc,
5438 Specification =>
5439 Build_Protected_Entry_Specification (Edef, Comp_Id, Loc));
5441 Insert_After (Current_Node, Sub);
5442 Analyze (Sub);
5444 Set_Protected_Body_Subprogram (
5445 Defining_Identifier (Comp),
5446 Defining_Unit_Name (Specification (Sub)));
5448 Current_Node := Sub;
5450 Bdef :=
5451 Make_Defining_Identifier (Loc,
5452 Build_Selected_Name
5453 (Protnm,
5454 New_External_Name (Chars (Comp_Id), Suffix_Index => -1),
5455 'B'));
5456 Sub :=
5457 Make_Subprogram_Declaration (Loc,
5458 Specification =>
5459 Build_Barrier_Function_Specification (Bdef, Loc));
5461 Insert_After (Current_Node, Sub);
5462 Analyze (Sub);
5463 Set_Protected_Body_Subprogram (Bdef, Bdef);
5464 Set_Barrier_Function (Comp_Id, Bdef);
5465 Set_Scope (Bdef, Scope (Comp_Id));
5466 Current_Node := Sub;
5468 -- Collect pointers to the protected subprogram and the barrier
5469 -- of the current entry, for insertion into Entry_Bodies_Array.
5471 Append (
5472 Make_Aggregate (Loc,
5473 Expressions => New_List (
5474 Make_Attribute_Reference (Loc,
5475 Prefix => New_Reference_To (Bdef, Loc),
5476 Attribute_Name => Name_Unrestricted_Access),
5477 Make_Attribute_Reference (Loc,
5478 Prefix => New_Reference_To (Edef, Loc),
5479 Attribute_Name => Name_Unrestricted_Access))),
5480 Expressions (Entries_Aggr));
5482 end if;
5484 Next (Comp);
5485 end loop;
5487 -- If there are some private entry declarations, expand it as if they
5488 -- were visible entries.
5490 if Present (Private_Declarations (Pdef)) then
5491 Comp := First (Private_Declarations (Pdef));
5493 while Present (Comp) loop
5494 if Nkind (Comp) = N_Entry_Declaration then
5495 E_Count := E_Count + 1;
5496 Comp_Id := Defining_Identifier (Comp);
5497 Set_Privals_Chain (Comp_Id, New_Elmt_List);
5498 Edef :=
5499 Make_Defining_Identifier (Loc,
5500 Build_Selected_Name
5501 (Protnm,
5502 New_External_Name (Chars (Comp_Id), Suffix_Index => -1),
5503 'E'));
5505 Sub :=
5506 Make_Subprogram_Declaration (Loc,
5507 Specification =>
5508 Build_Protected_Entry_Specification (Edef, Comp_Id, Loc));
5510 Insert_After (Current_Node, Sub);
5511 Analyze (Sub);
5513 Set_Protected_Body_Subprogram (
5514 Defining_Identifier (Comp),
5515 Defining_Unit_Name (Specification (Sub)));
5517 Current_Node := Sub;
5519 Bdef :=
5520 Make_Defining_Identifier (Loc,
5521 Build_Selected_Name
5522 (Protnm,
5523 New_External_Name (Chars (Comp_Id), Suffix_Index => -1),
5524 'B'));
5525 Sub :=
5526 Make_Subprogram_Declaration (Loc,
5527 Specification =>
5528 Build_Barrier_Function_Specification (Bdef, Loc));
5530 Insert_After (Current_Node, Sub);
5531 Analyze (Sub);
5532 Set_Protected_Body_Subprogram (Bdef, Bdef);
5533 Set_Barrier_Function (Comp_Id, Bdef);
5534 Set_Scope (Bdef, Scope (Comp_Id));
5535 Current_Node := Sub;
5537 -- Collect pointers to the protected subprogram and the
5538 -- barrier of the current entry, for insertion into
5539 -- Entry_Bodies_Array.
5541 Append (
5542 Make_Aggregate (Loc,
5543 Expressions => New_List (
5544 Make_Attribute_Reference (Loc,
5545 Prefix => New_Reference_To (Bdef, Loc),
5546 Attribute_Name => Name_Unrestricted_Access),
5547 Make_Attribute_Reference (Loc,
5548 Prefix => New_Reference_To (Edef, Loc),
5549 Attribute_Name => Name_Unrestricted_Access))),
5550 Expressions (Entries_Aggr));
5551 end if;
5553 Next (Comp);
5554 end loop;
5555 end if;
5557 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
5558 -- all protected subprograms have been collected.
5560 if Has_Entries (Prottyp) then
5561 Body_Id := Make_Defining_Identifier (Sloc (Prottyp),
5562 New_External_Name (Chars (Prottyp), 'A'));
5564 if Abort_Allowed
5565 or else Restriction_Active (No_Entry_Queue) = False
5566 or else E_Count > 1
5567 then
5568 Body_Arr := Make_Object_Declaration (Loc,
5569 Defining_Identifier => Body_Id,
5570 Aliased_Present => True,
5571 Object_Definition =>
5572 Make_Subtype_Indication (Loc,
5573 Subtype_Mark => New_Reference_To (
5574 RTE (RE_Protected_Entry_Body_Array), Loc),
5575 Constraint =>
5576 Make_Index_Or_Discriminant_Constraint (Loc,
5577 Constraints => New_List (
5578 Make_Range (Loc,
5579 Make_Integer_Literal (Loc, 1),
5580 Make_Integer_Literal (Loc, E_Count))))),
5581 Expression => Entries_Aggr);
5583 else
5584 Body_Arr := Make_Object_Declaration (Loc,
5585 Defining_Identifier => Body_Id,
5586 Aliased_Present => True,
5587 Object_Definition => New_Reference_To (RTE (RE_Entry_Body), Loc),
5588 Expression =>
5589 Make_Aggregate (Loc,
5590 Expressions => New_List (
5591 Make_Attribute_Reference (Loc,
5592 Prefix => New_Reference_To (Bdef, Loc),
5593 Attribute_Name => Name_Unrestricted_Access),
5594 Make_Attribute_Reference (Loc,
5595 Prefix => New_Reference_To (Edef, Loc),
5596 Attribute_Name => Name_Unrestricted_Access))));
5597 end if;
5599 -- A pointer to this array will be placed in the corresponding
5600 -- record by its initialization procedure, so this needs to be
5601 -- analyzed here.
5603 Insert_After (Current_Node, Body_Arr);
5604 Current_Node := Body_Arr;
5605 Analyze (Body_Arr);
5607 Set_Entry_Bodies_Array (Prottyp, Body_Id);
5609 -- Finally, build the function that maps an entry index into the
5610 -- corresponding body. A pointer to this function is placed in each
5611 -- object of the type. Except for a ravenscar-like profile (no abort,
5612 -- no entry queue, 1 entry)
5614 if Abort_Allowed
5615 or else Restriction_Active (No_Entry_Queue) = False
5616 or else E_Count > 1
5617 then
5618 Sub :=
5619 Make_Subprogram_Declaration (Loc,
5620 Specification => Build_Find_Body_Index_Spec (Prottyp));
5621 Insert_After (Current_Node, Sub);
5622 Analyze (Sub);
5623 end if;
5624 end if;
5625 end Expand_N_Protected_Type_Declaration;
5627 --------------------------------
5628 -- Expand_N_Requeue_Statement --
5629 --------------------------------
5631 -- A requeue statement is expanded into one of four GNARLI operations,
5632 -- depending on the source and destination (task or protected object).
5633 -- In addition, code must be generated to jump around the remainder of
5634 -- processing for the original entry and, if the destination is a
5635 -- (different) protected object, to attempt to service it.
5636 -- The following illustrates the various cases:
5638 -- procedure entE
5639 -- (O : System.Address;
5640 -- P : System.Address;
5641 -- E : Protected_Entry_Index)
5642 -- is
5643 -- <discriminant renamings>
5644 -- <private object renamings>
5645 -- type poVP is access poV;
5646 -- _Object : ptVP := ptVP!(O);
5648 -- begin
5649 -- begin
5650 -- <start of statement sequence for entry>
5652 -- -- Requeue from one protected entry body to another protected
5653 -- -- entry.
5655 -- Requeue_Protected_Entry (
5656 -- _object._object'Access,
5657 -- new._object'Access,
5658 -- E,
5659 -- Abort_Present);
5660 -- return;
5662 -- <some more of the statement sequence for entry>
5664 -- -- Requeue from an entry body to a task entry
5666 -- Requeue_Protected_To_Task_Entry (
5667 -- New._task_id,
5668 -- E,
5669 -- Abort_Present);
5670 -- return;
5672 -- <rest of statement sequence for entry>
5673 -- Complete_Entry_Body (_Object._Object);
5675 -- exception
5676 -- when all others =>
5677 -- Exceptional_Complete_Entry_Body (
5678 -- _Object._Object, Get_GNAT_Exception);
5679 -- end;
5680 -- end entE;
5682 -- Requeue of a task entry call to a task entry
5684 -- Accept_Call (E, Ann);
5685 -- <start of statement sequence for accept statement>
5686 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
5687 -- goto Lnn;
5688 -- <rest of statement sequence for accept statement>
5689 -- <<Lnn>>
5690 -- Complete_Rendezvous;
5692 -- exception
5693 -- when all others =>
5694 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
5696 -- Requeue of a task entry call to a protected entry
5698 -- Accept_Call (E, Ann);
5699 -- <start of statement sequence for accept statement>
5700 -- Requeue_Task_To_Protected_Entry (
5701 -- new._object'Access,
5702 -- E,
5703 -- Abort_Present);
5704 -- newS (new, Pnn);
5705 -- goto Lnn;
5706 -- <rest of statement sequence for accept statement>
5707 -- <<Lnn>>
5708 -- Complete_Rendezvous;
5710 -- exception
5711 -- when all others =>
5712 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
5714 -- Further details on these expansions can be found in
5715 -- Expand_N_Protected_Body and Expand_N_Accept_Statement.
5717 procedure Expand_N_Requeue_Statement (N : Node_Id) is
5718 Loc : constant Source_Ptr := Sloc (N);
5719 Acc_Stat : Node_Id;
5720 Concval : Node_Id;
5721 Ename : Node_Id;
5722 Index : Node_Id;
5723 Conctyp : Entity_Id;
5724 Oldtyp : Entity_Id;
5725 Lab_Node : Node_Id;
5726 Rcall : Node_Id;
5727 Abortable : Node_Id;
5728 Skip_Stat : Node_Id;
5729 Self_Param : Node_Id;
5730 New_Param : Node_Id;
5731 Params : List_Id;
5732 RTS_Call : Entity_Id;
5734 begin
5735 Abortable :=
5736 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc);
5738 -- Set up the target object
5740 Extract_Entry (N, Concval, Ename, Index);
5741 Conctyp := Etype (Concval);
5742 New_Param := Concurrent_Ref (Concval);
5744 -- The target entry index and abortable flag are the same for all cases
5746 Params := New_List (
5747 Entry_Index_Expression (Loc, Entity (Ename), Index, Conctyp),
5748 Abortable);
5750 -- Determine proper GNARLI call and required additional parameters
5751 -- Loop to find nearest enclosing task type or protected type
5753 Oldtyp := Current_Scope;
5754 loop
5755 if Is_Task_Type (Oldtyp) then
5756 if Is_Task_Type (Conctyp) then
5757 RTS_Call := RTE (RE_Requeue_Task_Entry);
5759 else
5760 pragma Assert (Is_Protected_Type (Conctyp));
5761 RTS_Call := RTE (RE_Requeue_Task_To_Protected_Entry);
5762 New_Param :=
5763 Make_Attribute_Reference (Loc,
5764 Prefix => New_Param,
5765 Attribute_Name => Name_Unchecked_Access);
5766 end if;
5768 Prepend (New_Param, Params);
5769 exit;
5771 elsif Is_Protected_Type (Oldtyp) then
5772 Self_Param :=
5773 Make_Attribute_Reference (Loc,
5774 Prefix => Concurrent_Ref (New_Occurrence_Of (Oldtyp, Loc)),
5775 Attribute_Name => Name_Unchecked_Access);
5777 if Is_Task_Type (Conctyp) then
5778 RTS_Call := RTE (RE_Requeue_Protected_To_Task_Entry);
5780 else
5781 pragma Assert (Is_Protected_Type (Conctyp));
5782 RTS_Call := RTE (RE_Requeue_Protected_Entry);
5783 New_Param :=
5784 Make_Attribute_Reference (Loc,
5785 Prefix => New_Param,
5786 Attribute_Name => Name_Unchecked_Access);
5787 end if;
5789 Prepend (New_Param, Params);
5790 Prepend (Self_Param, Params);
5791 exit;
5793 -- If neither task type or protected type, must be in some
5794 -- inner enclosing block, so move on out
5796 else
5797 Oldtyp := Scope (Oldtyp);
5798 end if;
5799 end loop;
5801 -- Create the GNARLI call
5803 Rcall := Make_Procedure_Call_Statement (Loc,
5804 Name =>
5805 New_Occurrence_Of (RTS_Call, Loc),
5806 Parameter_Associations => Params);
5808 Rewrite (N, Rcall);
5809 Analyze (N);
5811 if Is_Protected_Type (Oldtyp) then
5813 -- Build the return statement to skip the rest of the entry body
5815 Skip_Stat := Make_Return_Statement (Loc);
5817 else
5818 -- If the requeue is within a task, find the end label of the
5819 -- enclosing accept statement.
5821 Acc_Stat := Parent (N);
5822 while Nkind (Acc_Stat) /= N_Accept_Statement loop
5823 Acc_Stat := Parent (Acc_Stat);
5824 end loop;
5826 -- The last statement is the second label, used for completing the
5827 -- rendezvous the usual way.
5828 -- The label we are looking for is right before it.
5830 Lab_Node :=
5831 Prev (Last (Statements (Handled_Statement_Sequence (Acc_Stat))));
5833 pragma Assert (Nkind (Lab_Node) = N_Label);
5835 -- Build the goto statement to skip the rest of the accept
5836 -- statement.
5838 Skip_Stat :=
5839 Make_Goto_Statement (Loc,
5840 Name => New_Occurrence_Of (Entity (Identifier (Lab_Node)), Loc));
5841 end if;
5843 Set_Analyzed (Skip_Stat);
5845 Insert_After (N, Skip_Stat);
5846 end Expand_N_Requeue_Statement;
5848 -------------------------------
5849 -- Expand_N_Selective_Accept --
5850 -------------------------------
5852 procedure Expand_N_Selective_Accept (N : Node_Id) is
5853 Loc : constant Source_Ptr := Sloc (N);
5854 Alts : constant List_Id := Select_Alternatives (N);
5856 -- Note: in the below declarations a lot of new lists are allocated
5857 -- unconditionally which may well not end up being used. That's
5858 -- not a good idea since it wastes space gratuitously ???
5860 Accept_Case : List_Id;
5861 Accept_List : constant List_Id := New_List;
5863 Alt : Node_Id;
5864 Alt_List : constant List_Id := New_List;
5865 Alt_Stats : List_Id;
5866 Ann : Entity_Id := Empty;
5868 Block : Node_Id;
5869 Check_Guard : Boolean := True;
5871 Decls : constant List_Id := New_List;
5872 Stats : constant List_Id := New_List;
5873 Body_List : constant List_Id := New_List;
5874 Trailing_List : constant List_Id := New_List;
5876 Choices : List_Id;
5877 Else_Present : Boolean := False;
5878 Terminate_Alt : Node_Id := Empty;
5879 Select_Mode : Node_Id;
5881 Delay_Case : List_Id;
5882 Delay_Count : Integer := 0;
5883 Delay_Val : Entity_Id;
5884 Delay_Index : Entity_Id;
5885 Delay_Min : Entity_Id;
5886 Delay_Num : Int := 1;
5887 Delay_Alt_List : List_Id := New_List;
5888 Delay_List : constant List_Id := New_List;
5889 D : Entity_Id;
5890 M : Entity_Id;
5892 First_Delay : Boolean := True;
5893 Guard_Open : Entity_Id;
5895 End_Lab : Node_Id;
5896 Index : Int := 1;
5897 Lab : Node_Id;
5898 Num_Alts : Int;
5899 Num_Accept : Nat := 0;
5900 Proc : Node_Id;
5901 Q : Node_Id;
5902 Time_Type : Entity_Id;
5903 X : Node_Id;
5904 Select_Call : Node_Id;
5906 Qnam : constant Entity_Id :=
5907 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
5909 Xnam : constant Entity_Id :=
5910 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
5912 -----------------------
5913 -- Local subprograms --
5914 -----------------------
5916 function Accept_Or_Raise return List_Id;
5917 -- For the rare case where delay alternatives all have guards, and
5918 -- all of them are closed, it is still possible that there were open
5919 -- accept alternatives with no callers. We must reexamine the
5920 -- Accept_List, and execute a selective wait with no else if some
5921 -- accept is open. If none, we raise program_error.
5923 procedure Add_Accept (Alt : Node_Id);
5924 -- Process a single accept statement in a select alternative. Build
5925 -- procedure for body of accept, and add entry to dispatch table with
5926 -- expression for guard, in preparation for call to run time select.
5928 function Make_And_Declare_Label (Num : Int) return Node_Id;
5929 -- Manufacture a label using Num as a serial number and declare it.
5930 -- The declaration is appended to Decls. The label marks the trailing
5931 -- statements of an accept or delay alternative.
5933 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
5934 -- Build call to Selective_Wait runtime routine
5936 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
5937 -- Add code to compare value of delay with previous values, and
5938 -- generate case entry for trailing statements.
5940 procedure Process_Accept_Alternative
5941 (Alt : Node_Id;
5942 Index : Int;
5943 Proc : Node_Id);
5944 -- Add code to call corresponding procedure, and branch to
5945 -- trailing statements, if any.
5947 ---------------------
5948 -- Accept_Or_Raise --
5949 ---------------------
5951 function Accept_Or_Raise return List_Id is
5952 Cond : Node_Id;
5953 Stats : List_Id;
5954 J : constant Entity_Id := Make_Defining_Identifier (Loc,
5955 New_Internal_Name ('J'));
5957 begin
5958 -- We generate the following:
5960 -- for J in q'range loop
5961 -- if q(J).S /=null_task_entry then
5962 -- selective_wait (simple_mode,...);
5963 -- done := True;
5964 -- exit;
5965 -- end if;
5966 -- end loop;
5968 -- if no rendez_vous then
5969 -- raise program_error;
5970 -- end if;
5972 -- Note that the code needs to know that the selector name
5973 -- in an Accept_Alternative is named S.
5975 Cond := Make_Op_Ne (Loc,
5976 Left_Opnd =>
5977 Make_Selected_Component (Loc,
5978 Prefix => Make_Indexed_Component (Loc,
5979 Prefix => New_Reference_To (Qnam, Loc),
5980 Expressions => New_List (New_Reference_To (J, Loc))),
5981 Selector_Name => Make_Identifier (Loc, Name_S)),
5982 Right_Opnd =>
5983 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
5985 Stats := New_List (
5986 Make_Implicit_Loop_Statement (N,
5987 Identifier => Empty,
5988 Iteration_Scheme =>
5989 Make_Iteration_Scheme (Loc,
5990 Loop_Parameter_Specification =>
5991 Make_Loop_Parameter_Specification (Loc,
5992 Defining_Identifier => J,
5993 Discrete_Subtype_Definition =>
5994 Make_Attribute_Reference (Loc,
5995 Prefix => New_Reference_To (Qnam, Loc),
5996 Attribute_Name => Name_Range,
5997 Expressions => New_List (
5998 Make_Integer_Literal (Loc, 1))))),
6000 Statements => New_List (
6001 Make_Implicit_If_Statement (N,
6002 Condition => Cond,
6003 Then_Statements => New_List (
6004 Make_Select_Call (
6005 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
6006 Make_Exit_Statement (Loc))))));
6008 Append_To (Stats,
6009 Make_Raise_Program_Error (Loc,
6010 Condition => Make_Op_Eq (Loc,
6011 Left_Opnd => New_Reference_To (Xnam, Loc),
6012 Right_Opnd =>
6013 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
6014 Reason => PE_All_Guards_Closed));
6016 return Stats;
6017 end Accept_Or_Raise;
6019 ----------------
6020 -- Add_Accept --
6021 ----------------
6023 procedure Add_Accept (Alt : Node_Id) is
6024 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
6025 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
6026 Eent : constant Entity_Id := Entity (Ename);
6027 Index : constant Node_Id := Entry_Index (Acc_Stm);
6028 Null_Body : Node_Id;
6029 Proc_Body : Node_Id;
6030 PB_Ent : Entity_Id;
6031 Expr : Node_Id;
6032 Call : Node_Id;
6034 begin
6035 if No (Ann) then
6036 Ann := Node (Last_Elmt (Accept_Address (Eent)));
6037 end if;
6039 if Present (Condition (Alt)) then
6040 Expr :=
6041 Make_Conditional_Expression (Loc, New_List (
6042 Condition (Alt),
6043 Entry_Index_Expression (Loc, Eent, Index, Scope (Eent)),
6044 New_Reference_To (RTE (RE_Null_Task_Entry), Loc)));
6045 else
6046 Expr :=
6047 Entry_Index_Expression
6048 (Loc, Eent, Index, Scope (Eent));
6049 end if;
6051 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
6052 Null_Body := New_Reference_To (Standard_False, Loc);
6054 if Abort_Allowed then
6055 Call := Make_Procedure_Call_Statement (Loc,
6056 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc));
6057 Insert_Before (First (Statements (Handled_Statement_Sequence (
6058 Accept_Statement (Alt)))), Call);
6059 Analyze (Call);
6060 end if;
6062 PB_Ent :=
6063 Make_Defining_Identifier (Sloc (Ename),
6064 New_External_Name (Chars (Ename), 'A', Num_Accept));
6066 Set_Needs_Debug_Info (PB_Ent, Comes_From_Source (Alt));
6068 Proc_Body :=
6069 Make_Subprogram_Body (Loc,
6070 Specification =>
6071 Make_Procedure_Specification (Loc,
6072 Defining_Unit_Name => PB_Ent),
6073 Declarations => Declarations (Acc_Stm),
6074 Handled_Statement_Sequence =>
6075 Build_Accept_Body (Accept_Statement (Alt)));
6077 -- During the analysis of the body of the accept statement, any
6078 -- zero cost exception handler records were collected in the
6079 -- Accept_Handler_Records field of the N_Accept_Alternative
6080 -- node. This is where we move them to where they belong,
6081 -- namely the newly created procedure.
6083 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
6084 Append (Proc_Body, Body_List);
6086 else
6087 Null_Body := New_Reference_To (Standard_True, Loc);
6089 -- if accept statement has declarations, insert above, given
6090 -- that we are not creating a body for the accept.
6092 if Present (Declarations (Acc_Stm)) then
6093 Insert_Actions (N, Declarations (Acc_Stm));
6094 end if;
6095 end if;
6097 Append_To (Accept_List,
6098 Make_Aggregate (Loc, Expressions => New_List (Null_Body, Expr)));
6100 Num_Accept := Num_Accept + 1;
6101 end Add_Accept;
6103 ----------------------------
6104 -- Make_And_Declare_Label --
6105 ----------------------------
6107 function Make_And_Declare_Label (Num : Int) return Node_Id is
6108 Lab_Id : Node_Id;
6110 begin
6111 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
6112 Lab :=
6113 Make_Label (Loc, Lab_Id);
6115 Append_To (Decls,
6116 Make_Implicit_Label_Declaration (Loc,
6117 Defining_Identifier =>
6118 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
6119 Label_Construct => Lab));
6121 return Lab;
6122 end Make_And_Declare_Label;
6124 ----------------------
6125 -- Make_Select_Call --
6126 ----------------------
6128 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
6129 Params : constant List_Id := New_List;
6131 begin
6132 Append (
6133 Make_Attribute_Reference (Loc,
6134 Prefix => New_Reference_To (Qnam, Loc),
6135 Attribute_Name => Name_Unchecked_Access),
6136 Params);
6137 Append (Select_Mode, Params);
6138 Append (New_Reference_To (Ann, Loc), Params);
6139 Append (New_Reference_To (Xnam, Loc), Params);
6141 return
6142 Make_Procedure_Call_Statement (Loc,
6143 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
6144 Parameter_Associations => Params);
6145 end Make_Select_Call;
6147 --------------------------------
6148 -- Process_Accept_Alternative --
6149 --------------------------------
6151 procedure Process_Accept_Alternative
6152 (Alt : Node_Id;
6153 Index : Int;
6154 Proc : Node_Id)
6156 Choices : List_Id := No_List;
6157 Alt_Stats : List_Id;
6159 begin
6160 Adjust_Condition (Condition (Alt));
6161 Alt_Stats := No_List;
6163 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
6164 Choices := New_List (
6165 Make_Integer_Literal (Loc, Index));
6167 Alt_Stats := New_List (
6168 Make_Procedure_Call_Statement (Loc,
6169 Name => New_Reference_To (
6170 Defining_Unit_Name (Specification (Proc)), Loc)));
6171 end if;
6173 if Statements (Alt) /= Empty_List then
6175 if No (Alt_Stats) then
6177 -- Accept with no body, followed by trailing statements
6179 Choices := New_List (
6180 Make_Integer_Literal (Loc, Index));
6182 Alt_Stats := New_List;
6183 end if;
6185 -- After the call, if any, branch to to trailing statements.
6186 -- We create a label for each, as well as the corresponding
6187 -- label declaration.
6189 Lab := Make_And_Declare_Label (Index);
6190 Append_To (Alt_Stats,
6191 Make_Goto_Statement (Loc,
6192 Name => New_Copy (Identifier (Lab))));
6194 Append (Lab, Trailing_List);
6195 Append_List (Statements (Alt), Trailing_List);
6196 Append_To (Trailing_List,
6197 Make_Goto_Statement (Loc,
6198 Name => New_Copy (Identifier (End_Lab))));
6199 end if;
6201 if Present (Alt_Stats) then
6203 -- Procedure call. and/or trailing statements
6205 Append_To (Alt_List,
6206 Make_Case_Statement_Alternative (Loc,
6207 Discrete_Choices => Choices,
6208 Statements => Alt_Stats));
6209 end if;
6210 end Process_Accept_Alternative;
6212 -------------------------------
6213 -- Process_Delay_Alternative --
6214 -------------------------------
6216 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
6217 Choices : List_Id;
6218 Cond : Node_Id;
6219 Delay_Alt : List_Id;
6221 begin
6222 -- Deal with C/Fortran boolean as delay condition
6224 Adjust_Condition (Condition (Alt));
6226 -- Determine the smallest specified delay
6228 -- for each delay alternative generate:
6230 -- if guard-expression then
6231 -- Delay_Val := delay-expression;
6232 -- Guard_Open := True;
6233 -- if Delay_Val < Delay_Min then
6234 -- Delay_Min := Delay_Val;
6235 -- Delay_Index := Index;
6236 -- end if;
6237 -- end if;
6239 -- The enclosing if-statement is omitted if there is no guard
6241 if Delay_Count = 1
6242 or else First_Delay
6243 then
6244 First_Delay := False;
6246 Delay_Alt := New_List (
6247 Make_Assignment_Statement (Loc,
6248 Name => New_Reference_To (Delay_Min, Loc),
6249 Expression => Expression (Delay_Statement (Alt))));
6251 if Delay_Count > 1 then
6252 Append_To (Delay_Alt,
6253 Make_Assignment_Statement (Loc,
6254 Name => New_Reference_To (Delay_Index, Loc),
6255 Expression => Make_Integer_Literal (Loc, Index)));
6256 end if;
6258 else
6259 Delay_Alt := New_List (
6260 Make_Assignment_Statement (Loc,
6261 Name => New_Reference_To (Delay_Val, Loc),
6262 Expression => Expression (Delay_Statement (Alt))));
6264 if Time_Type = Standard_Duration then
6265 Cond :=
6266 Make_Op_Lt (Loc,
6267 Left_Opnd => New_Reference_To (Delay_Val, Loc),
6268 Right_Opnd => New_Reference_To (Delay_Min, Loc));
6270 else
6271 -- The scope of the time type must define a comparison
6272 -- operator. The scope itself may not be visible, so we
6273 -- construct a node with entity information to insure that
6274 -- semantic analysis can find the proper operator.
6276 Cond :=
6277 Make_Function_Call (Loc,
6278 Name => Make_Selected_Component (Loc,
6279 Prefix => New_Reference_To (Scope (Time_Type), Loc),
6280 Selector_Name =>
6281 Make_Operator_Symbol (Loc,
6282 Chars => Name_Op_Lt,
6283 Strval => No_String)),
6284 Parameter_Associations =>
6285 New_List (
6286 New_Reference_To (Delay_Val, Loc),
6287 New_Reference_To (Delay_Min, Loc)));
6289 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
6290 end if;
6292 Append_To (Delay_Alt,
6293 Make_Implicit_If_Statement (N,
6294 Condition => Cond,
6295 Then_Statements => New_List (
6296 Make_Assignment_Statement (Loc,
6297 Name => New_Reference_To (Delay_Min, Loc),
6298 Expression => New_Reference_To (Delay_Val, Loc)),
6300 Make_Assignment_Statement (Loc,
6301 Name => New_Reference_To (Delay_Index, Loc),
6302 Expression => Make_Integer_Literal (Loc, Index)))));
6303 end if;
6305 if Check_Guard then
6306 Append_To (Delay_Alt,
6307 Make_Assignment_Statement (Loc,
6308 Name => New_Reference_To (Guard_Open, Loc),
6309 Expression => New_Reference_To (Standard_True, Loc)));
6310 end if;
6312 if Present (Condition (Alt)) then
6313 Delay_Alt := New_List (
6314 Make_Implicit_If_Statement (N,
6315 Condition => Condition (Alt),
6316 Then_Statements => Delay_Alt));
6317 end if;
6319 Append_List (Delay_Alt, Delay_List);
6321 -- If the delay alternative has a statement part, add a
6322 -- choice to the case statements for delays.
6324 if Present (Statements (Alt)) then
6326 if Delay_Count = 1 then
6327 Append_List (Statements (Alt), Delay_Alt_List);
6329 else
6330 Choices := New_List (
6331 Make_Integer_Literal (Loc, Index));
6333 Append_To (Delay_Alt_List,
6334 Make_Case_Statement_Alternative (Loc,
6335 Discrete_Choices => Choices,
6336 Statements => Statements (Alt)));
6337 end if;
6339 elsif Delay_Count = 1 then
6341 -- If the single delay has no trailing statements, add a branch
6342 -- to the exit label to the selective wait.
6344 Delay_Alt_List := New_List (
6345 Make_Goto_Statement (Loc,
6346 Name => New_Copy (Identifier (End_Lab))));
6348 end if;
6349 end Process_Delay_Alternative;
6351 -- Start of processing for Expand_N_Selective_Accept
6353 begin
6354 -- First insert some declarations before the select. The first is:
6356 -- Ann : Address
6358 -- This variable holds the parameters passed to the accept body. This
6359 -- declaration has already been inserted by the time we get here by
6360 -- a call to Expand_Accept_Declarations made from the semantics when
6361 -- processing the first accept statement contained in the select. We
6362 -- can find this entity as Accept_Address (E), where E is any of the
6363 -- entries references by contained accept statements.
6365 -- The first step is to scan the list of Selective_Accept_Statements
6366 -- to find this entity, and also count the number of accepts, and
6367 -- determine if terminated, delay or else is present:
6369 Num_Alts := 0;
6371 Alt := First (Alts);
6372 while Present (Alt) loop
6374 if Nkind (Alt) = N_Accept_Alternative then
6375 Add_Accept (Alt);
6377 elsif Nkind (Alt) = N_Delay_Alternative then
6378 Delay_Count := Delay_Count + 1;
6380 -- If the delays are relative delays, the delay expressions have
6381 -- type Standard_Duration. Otherwise they must have some time type
6382 -- recognized by GNAT.
6384 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
6385 Time_Type := Standard_Duration;
6386 else
6387 Time_Type := Etype (Expression (Delay_Statement (Alt)));
6389 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
6390 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
6391 then
6392 null;
6393 else
6394 Error_Msg_NE (
6395 "& is not a time type ('R'M 9.6(6))",
6396 Expression (Delay_Statement (Alt)), Time_Type);
6397 Time_Type := Standard_Duration;
6398 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
6399 end if;
6400 end if;
6402 if No (Condition (Alt)) then
6404 -- This guard will always be open
6406 Check_Guard := False;
6407 end if;
6409 elsif Nkind (Alt) = N_Terminate_Alternative then
6410 Adjust_Condition (Condition (Alt));
6411 Terminate_Alt := Alt;
6412 end if;
6414 Num_Alts := Num_Alts + 1;
6415 Next (Alt);
6416 end loop;
6418 Else_Present := Present (Else_Statements (N));
6420 -- At the same time (see procedure Add_Accept) we build the accept list:
6422 -- Qnn : Accept_List (1 .. num-select) := (
6423 -- (null-body, entry-index),
6424 -- (null-body, entry-index),
6425 -- ..
6426 -- (null_body, entry-index));
6428 -- In the above declaration, null-body is True if the corresponding
6429 -- accept has no body, and false otherwise. The entry is either the
6430 -- entry index expression if there is no guard, or if a guard is
6431 -- present, then a conditional expression of the form:
6433 -- (if guard then entry-index else Null_Task_Entry)
6435 -- If a guard is statically known to be false, the entry can simply
6436 -- be omitted from the accept list.
6438 Q :=
6439 Make_Object_Declaration (Loc,
6440 Defining_Identifier => Qnam,
6441 Object_Definition =>
6442 New_Reference_To (RTE (RE_Accept_List), Loc),
6443 Aliased_Present => True,
6445 Expression =>
6446 Make_Qualified_Expression (Loc,
6447 Subtype_Mark =>
6448 New_Reference_To (RTE (RE_Accept_List), Loc),
6449 Expression =>
6450 Make_Aggregate (Loc, Expressions => Accept_List)));
6452 Append (Q, Decls);
6454 -- Then we declare the variable that holds the index for the accept
6455 -- that will be selected for service:
6457 -- Xnn : Select_Index;
6459 X :=
6460 Make_Object_Declaration (Loc,
6461 Defining_Identifier => Xnam,
6462 Object_Definition =>
6463 New_Reference_To (RTE (RE_Select_Index), Loc),
6464 Expression =>
6465 New_Reference_To (RTE (RE_No_Rendezvous), Loc));
6467 Append (X, Decls);
6469 -- After this follow procedure declarations for each accept body
6471 -- procedure Pnn is
6472 -- begin
6473 -- ...
6474 -- end;
6476 -- where the ... are statements from the corresponding procedure body.
6477 -- No parameters are involved, since the parameters are passed via Ann
6478 -- and the parameter references have already been expanded to be direct
6479 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
6480 -- any embedded tasking statements (which would normally be illegal in
6481 -- procedures, have been converted to calls to the tasking runtime so
6482 -- there is no problem in putting them into procedures.
6484 -- The original accept statement has been expanded into a block in
6485 -- the same fashion as for simple accepts (see Build_Accept_Body).
6487 -- Note: we don't really need to build these procedures for the case
6488 -- where no delay statement is present, but it is just as easy to
6489 -- build them unconditionally, and not significantly inefficient,
6490 -- since if they are short they will be inlined anyway.
6492 -- The procedure declarations have been assembled in Body_List
6494 -- If delays are present, we must compute the required delay.
6495 -- We first generate the declarations:
6497 -- Delay_Index : Boolean := 0;
6498 -- Delay_Min : Some_Time_Type.Time;
6499 -- Delay_Val : Some_Time_Type.Time;
6501 -- Delay_Index will be set to the index of the minimum delay, i.e. the
6502 -- active delay that is actually chosen as the basis for the possible
6503 -- delay if an immediate rendez-vous is not possible.
6505 -- In the most common case there is a single delay statement, and this
6506 -- is handled specially.
6508 if Delay_Count > 0 then
6510 -- Generate the required declarations
6512 Delay_Val :=
6513 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
6514 Delay_Index :=
6515 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
6516 Delay_Min :=
6517 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
6519 Append_To (Decls,
6520 Make_Object_Declaration (Loc,
6521 Defining_Identifier => Delay_Val,
6522 Object_Definition => New_Reference_To (Time_Type, Loc)));
6524 Append_To (Decls,
6525 Make_Object_Declaration (Loc,
6526 Defining_Identifier => Delay_Index,
6527 Object_Definition => New_Reference_To (Standard_Integer, Loc),
6528 Expression => Make_Integer_Literal (Loc, 0)));
6530 Append_To (Decls,
6531 Make_Object_Declaration (Loc,
6532 Defining_Identifier => Delay_Min,
6533 Object_Definition => New_Reference_To (Time_Type, Loc),
6534 Expression =>
6535 Unchecked_Convert_To (Time_Type,
6536 Make_Attribute_Reference (Loc,
6537 Prefix =>
6538 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
6539 Attribute_Name => Name_Last))));
6541 -- Create Duration and Delay_Mode objects used for passing a delay
6542 -- value to RTS
6544 D := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
6545 M := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
6547 declare
6548 Discr : Entity_Id;
6550 begin
6551 -- Note that these values are defined in s-osprim.ads and must
6552 -- be kept in sync:
6554 -- Relative : constant := 0;
6555 -- Absolute_Calendar : constant := 1;
6556 -- Absolute_RT : constant := 2;
6558 if Time_Type = Standard_Duration then
6559 Discr := Make_Integer_Literal (Loc, 0);
6561 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
6562 Discr := Make_Integer_Literal (Loc, 1);
6564 else
6565 pragma Assert
6566 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
6567 Discr := Make_Integer_Literal (Loc, 2);
6568 end if;
6570 Append_To (Decls,
6571 Make_Object_Declaration (Loc,
6572 Defining_Identifier => D,
6573 Object_Definition =>
6574 New_Reference_To (Standard_Duration, Loc)));
6576 Append_To (Decls,
6577 Make_Object_Declaration (Loc,
6578 Defining_Identifier => M,
6579 Object_Definition =>
6580 New_Reference_To (Standard_Integer, Loc),
6581 Expression => Discr));
6582 end;
6584 if Check_Guard then
6585 Guard_Open :=
6586 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
6588 Append_To (Decls,
6589 Make_Object_Declaration (Loc,
6590 Defining_Identifier => Guard_Open,
6591 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
6592 Expression => New_Reference_To (Standard_False, Loc)));
6593 end if;
6595 -- Delay_Count is zero, don't need M and D set (suppress warning)
6597 else
6598 M := Empty;
6599 D := Empty;
6600 end if;
6602 if Present (Terminate_Alt) then
6604 -- If the terminate alternative guard is False, use
6605 -- Simple_Mode; otherwise use Terminate_Mode.
6607 if Present (Condition (Terminate_Alt)) then
6608 Select_Mode := Make_Conditional_Expression (Loc,
6609 New_List (Condition (Terminate_Alt),
6610 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
6611 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
6612 else
6613 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
6614 end if;
6616 elsif Else_Present or Delay_Count > 0 then
6617 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
6619 else
6620 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
6621 end if;
6623 Select_Call := Make_Select_Call (Select_Mode);
6624 Append (Select_Call, Stats);
6626 -- Now generate code to act on the result. There is an entry
6627 -- in this case for each accept statement with a non-null body,
6628 -- followed by a branch to the statements that follow the Accept.
6629 -- In the absence of delay alternatives, we generate:
6631 -- case X is
6632 -- when No_Rendezvous => -- omitted if simple mode
6633 -- goto Lab0;
6635 -- when 1 =>
6636 -- P1n;
6637 -- goto Lab1;
6639 -- when 2 =>
6640 -- P2n;
6641 -- goto Lab2;
6643 -- when others =>
6644 -- goto Exit;
6645 -- end case;
6647 -- Lab0: Else_Statements;
6648 -- goto exit;
6650 -- Lab1: Trailing_Statements1;
6651 -- goto Exit;
6653 -- Lab2: Trailing_Statements2;
6654 -- goto Exit;
6655 -- ...
6656 -- Exit:
6658 -- Generate label for common exit
6660 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
6662 -- First entry is the default case, when no rendezvous is possible
6664 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
6666 if Else_Present then
6668 -- If no rendezvous is possible, the else part is executed
6670 Lab := Make_And_Declare_Label (0);
6671 Alt_Stats := New_List (
6672 Make_Goto_Statement (Loc,
6673 Name => New_Copy (Identifier (Lab))));
6675 Append (Lab, Trailing_List);
6676 Append_List (Else_Statements (N), Trailing_List);
6677 Append_To (Trailing_List,
6678 Make_Goto_Statement (Loc,
6679 Name => New_Copy (Identifier (End_Lab))));
6680 else
6681 Alt_Stats := New_List (
6682 Make_Goto_Statement (Loc,
6683 Name => New_Copy (Identifier (End_Lab))));
6684 end if;
6686 Append_To (Alt_List,
6687 Make_Case_Statement_Alternative (Loc,
6688 Discrete_Choices => Choices,
6689 Statements => Alt_Stats));
6691 -- We make use of the fact that Accept_Index is an integer type,
6692 -- and generate successive literals for entries for each accept.
6693 -- Only those for which there is a body or trailing statements are
6694 -- given a case entry.
6696 Alt := First (Select_Alternatives (N));
6697 Proc := First (Body_List);
6699 while Present (Alt) loop
6701 if Nkind (Alt) = N_Accept_Alternative then
6702 Process_Accept_Alternative (Alt, Index, Proc);
6703 Index := Index + 1;
6705 if Present
6706 (Handled_Statement_Sequence (Accept_Statement (Alt)))
6707 then
6708 Next (Proc);
6709 end if;
6711 elsif Nkind (Alt) = N_Delay_Alternative then
6712 Process_Delay_Alternative (Alt, Delay_Num);
6713 Delay_Num := Delay_Num + 1;
6714 end if;
6716 Next (Alt);
6717 end loop;
6719 -- An others choice is always added to the main case, as well
6720 -- as the delay case (to satisfy the compiler).
6722 Append_To (Alt_List,
6723 Make_Case_Statement_Alternative (Loc,
6724 Discrete_Choices =>
6725 New_List (Make_Others_Choice (Loc)),
6726 Statements =>
6727 New_List (Make_Goto_Statement (Loc,
6728 Name => New_Copy (Identifier (End_Lab))))));
6730 Accept_Case := New_List (
6731 Make_Case_Statement (Loc,
6732 Expression => New_Reference_To (Xnam, Loc),
6733 Alternatives => Alt_List));
6735 Append_List (Trailing_List, Accept_Case);
6736 Append (End_Lab, Accept_Case);
6737 Append_List (Body_List, Decls);
6739 -- Construct case statement for trailing statements of delay
6740 -- alternatives, if there are several of them.
6742 if Delay_Count > 1 then
6743 Append_To (Delay_Alt_List,
6744 Make_Case_Statement_Alternative (Loc,
6745 Discrete_Choices =>
6746 New_List (Make_Others_Choice (Loc)),
6747 Statements =>
6748 New_List (Make_Null_Statement (Loc))));
6750 Delay_Case := New_List (
6751 Make_Case_Statement (Loc,
6752 Expression => New_Reference_To (Delay_Index, Loc),
6753 Alternatives => Delay_Alt_List));
6754 else
6755 Delay_Case := Delay_Alt_List;
6756 end if;
6758 -- If there are no delay alternatives, we append the case statement
6759 -- to the statement list.
6761 if Delay_Count = 0 then
6762 Append_List (Accept_Case, Stats);
6764 -- Delay alternatives present
6766 else
6767 -- If delay alternatives are present we generate:
6769 -- find minimum delay.
6770 -- DX := minimum delay;
6771 -- M := <delay mode>;
6772 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
6773 -- DX, MX, X);
6775 -- if X = No_Rendezvous then
6776 -- case statement for delay statements.
6777 -- else
6778 -- case statement for accept alternatives.
6779 -- end if;
6781 declare
6782 Cases : Node_Id;
6783 Stmt : Node_Id;
6784 Parms : List_Id;
6785 Parm : Node_Id;
6786 Conv : Node_Id;
6788 begin
6789 -- The type of the delay expression is known to be legal
6791 if Time_Type = Standard_Duration then
6792 Conv := New_Reference_To (Delay_Min, Loc);
6794 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
6795 Conv := Make_Function_Call (Loc,
6796 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
6797 New_List (New_Reference_To (Delay_Min, Loc)));
6799 else
6800 pragma Assert
6801 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
6803 Conv := Make_Function_Call (Loc,
6804 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
6805 New_List (New_Reference_To (Delay_Min, Loc)));
6806 end if;
6808 Stmt := Make_Assignment_Statement (Loc,
6809 Name => New_Reference_To (D, Loc),
6810 Expression => Conv);
6812 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
6814 Parms := Parameter_Associations (Select_Call);
6815 Parm := First (Parms);
6817 while Present (Parm)
6818 and then Parm /= Select_Mode
6819 loop
6820 Next (Parm);
6821 end loop;
6823 pragma Assert (Present (Parm));
6824 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
6825 Analyze (Parm);
6827 -- Prepare two new parameters of Duration and Delay_Mode type
6828 -- which represent the value and the mode of the minimum delay.
6830 Next (Parm);
6831 Insert_After (Parm, New_Reference_To (M, Loc));
6832 Insert_After (Parm, New_Reference_To (D, Loc));
6834 -- Create a call to RTS
6836 Rewrite (Select_Call,
6837 Make_Procedure_Call_Statement (Loc,
6838 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
6839 Parameter_Associations => Parms));
6841 -- This new call should follow the calculation of the
6842 -- minimum delay.
6844 Insert_List_Before (Select_Call, Delay_List);
6846 if Check_Guard then
6847 Stmt :=
6848 Make_Implicit_If_Statement (N,
6849 Condition => New_Reference_To (Guard_Open, Loc),
6850 Then_Statements =>
6851 New_List (New_Copy_Tree (Stmt),
6852 New_Copy_Tree (Select_Call)),
6853 Else_Statements => Accept_Or_Raise);
6854 Rewrite (Select_Call, Stmt);
6855 else
6856 Insert_Before (Select_Call, Stmt);
6857 end if;
6859 Cases :=
6860 Make_Implicit_If_Statement (N,
6861 Condition => Make_Op_Eq (Loc,
6862 Left_Opnd => New_Reference_To (Xnam, Loc),
6863 Right_Opnd =>
6864 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
6866 Then_Statements => Delay_Case,
6867 Else_Statements => Accept_Case);
6869 Append (Cases, Stats);
6870 end;
6871 end if;
6873 -- Replace accept statement with appropriate block
6875 Block :=
6876 Make_Block_Statement (Loc,
6877 Declarations => Decls,
6878 Handled_Statement_Sequence =>
6879 Make_Handled_Sequence_Of_Statements (Loc,
6880 Statements => Stats));
6882 Rewrite (N, Block);
6883 Analyze (N);
6885 -- Note: have to worry more about abort deferral in above code ???
6887 -- Final step is to unstack the Accept_Address entries for all accept
6888 -- statements appearing in accept alternatives in the select statement
6890 Alt := First (Alts);
6891 while Present (Alt) loop
6892 if Nkind (Alt) = N_Accept_Alternative then
6893 Remove_Last_Elmt (Accept_Address
6894 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
6895 end if;
6897 Next (Alt);
6898 end loop;
6899 end Expand_N_Selective_Accept;
6901 --------------------------------------
6902 -- Expand_N_Single_Task_Declaration --
6903 --------------------------------------
6905 -- Single task declarations should never be present after semantic
6906 -- analysis, since we expect them to be replaced by a declaration of
6907 -- an anonymous task type, followed by a declaration of the task
6908 -- object. We include this routine to make sure that is happening!
6910 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
6911 begin
6912 raise Program_Error;
6913 end Expand_N_Single_Task_Declaration;
6915 ------------------------
6916 -- Expand_N_Task_Body --
6917 ------------------------
6919 -- Given a task body
6921 -- task body tname is
6922 -- <declarations>
6923 -- begin
6924 -- <statements>
6925 -- end x;
6927 -- This expansion routine converts it into a procedure and sets the
6928 -- elaboration flag for the procedure to true, to represent the fact
6929 -- that the task body is now elaborated:
6931 -- procedure tnameB (_Task : access tnameV) is
6932 -- discriminal : dtype renames _Task.discriminant;
6934 -- procedure _clean is
6935 -- begin
6936 -- Abort_Defer.all;
6937 -- Complete_Task;
6938 -- Abort_Undefer.all;
6939 -- return;
6940 -- end _clean;
6942 -- begin
6943 -- Abort_Undefer.all;
6944 -- <declarations>
6945 -- System.Task_Stages.Complete_Activation;
6946 -- <statements>
6947 -- at end
6948 -- _clean;
6949 -- end tnameB;
6951 -- tnameE := True;
6953 -- In addition, if the task body is an activator, then a call to
6954 -- activate tasks is added at the start of the statements, before
6955 -- the call to Complete_Activation, and if in addition the task is
6956 -- a master then it must be established as a master. These calls are
6957 -- inserted and analyzed in Expand_Cleanup_Actions, when the
6958 -- Handled_Sequence_Of_Statements is expanded.
6960 -- There is one discriminal declaration line generated for each
6961 -- discriminant that is present to provide an easy reference point
6962 -- for discriminant references inside the body (see Exp_Ch2.Expand_Name).
6964 -- Note on relationship to GNARLI definition. In the GNARLI definition,
6965 -- task body procedures have a profile (Arg : System.Address). That is
6966 -- needed because GNARLI has to use the same access-to-subprogram type
6967 -- for all task types. We depend here on knowing that in GNAT, passing
6968 -- an address argument by value is identical to passing a record value
6969 -- by access (in either case a single pointer is passed), so even though
6970 -- this procedure has the wrong profile. In fact it's all OK, since the
6971 -- callings sequence is identical.
6973 procedure Expand_N_Task_Body (N : Node_Id) is
6974 Loc : constant Source_Ptr := Sloc (N);
6975 Ttyp : constant Entity_Id := Corresponding_Spec (N);
6976 Call : Node_Id;
6977 New_N : Node_Id;
6979 begin
6980 -- Here we start the expansion by generating discriminal declarations
6982 Add_Discriminal_Declarations (Declarations (N), Ttyp, Name_uTask, Loc);
6984 -- Add a call to Abort_Undefer at the very beginning of the task
6985 -- body since this body is called with abort still deferred.
6987 if Abort_Allowed then
6988 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
6989 Insert_Before
6990 (First (Statements (Handled_Statement_Sequence (N))), Call);
6991 Analyze (Call);
6992 end if;
6994 -- The statement part has already been protected with an at_end and
6995 -- cleanup actions. The call to Complete_Activation must be placed
6996 -- at the head of the sequence of statements of that block. The
6997 -- declarations have been merged in this sequence of statements but
6998 -- the first real statement is accessible from the First_Real_Statement
6999 -- field (which was set for exactly this purpose).
7001 if Restricted_Profile then
7002 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
7003 else
7004 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
7005 end if;
7007 Insert_Before
7008 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
7009 Analyze (Call);
7011 New_N :=
7012 Make_Subprogram_Body (Loc,
7013 Specification => Build_Task_Proc_Specification (Ttyp),
7014 Declarations => Declarations (N),
7015 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
7017 -- If the task contains generic instantiations, cleanup actions
7018 -- are delayed until after instantiation. Transfer the activation
7019 -- chain to the subprogram, to insure that the activation call is
7020 -- properly generated. It the task body contains inner tasks, indicate
7021 -- that the subprogram is a task master.
7023 if Delay_Cleanups (Ttyp) then
7024 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
7025 Set_Is_Task_Master (New_N, Is_Task_Master (N));
7026 end if;
7028 Rewrite (N, New_N);
7029 Analyze (N);
7031 -- Set elaboration flag immediately after task body. If the body
7032 -- is a subunit, the flag is set in the declarative part that
7033 -- contains the stub.
7035 if Nkind (Parent (N)) /= N_Subunit then
7036 Insert_After (N,
7037 Make_Assignment_Statement (Loc,
7038 Name =>
7039 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
7040 Expression => New_Reference_To (Standard_True, Loc)));
7041 end if;
7042 end Expand_N_Task_Body;
7044 ------------------------------------
7045 -- Expand_N_Task_Type_Declaration --
7046 ------------------------------------
7048 -- We have several things to do. First we must create a Boolean flag used
7049 -- to mark if the body is elaborated yet. This variable gets set to True
7050 -- when the body of the task is elaborated (we can't rely on the normal
7051 -- ABE mechanism for the task body, since we need to pass an access to
7052 -- this elaboration boolean to the runtime routines).
7054 -- taskE : aliased Boolean := False;
7056 -- Next a variable is declared to hold the task stack size (either
7057 -- the default : Unspecified_Size, or a value that is set by a pragma
7058 -- Storage_Size). If the value of the pragma Storage_Size is static, then
7059 -- the variable is initialized with this value:
7061 -- taskZ : Size_Type := Unspecified_Size;
7062 -- or
7063 -- taskZ : Size_Type := Size_Type (size_expression);
7065 -- Next we create a corresponding record type declaration used to represent
7066 -- values of this task. The general form of this type declaration is
7068 -- type taskV (discriminants) is record
7069 -- _Task_Id : Task_Id;
7070 -- entry_family : array (bounds) of Void;
7071 -- _Priority : Integer := priority_expression;
7072 -- _Size : Size_Type := Size_Type (size_expression);
7073 -- _Task_Info : Task_Info_Type := task_info_expression;
7074 -- end record;
7076 -- The discriminants are present only if the corresponding task type has
7077 -- discriminants, and they exactly mirror the task type discriminants.
7079 -- The Id field is always present. It contains the Task_Id value, as
7080 -- set by the call to Create_Task. Note that although the task is
7081 -- limited, the task value record type is not limited, so there is no
7082 -- problem in passing this field as an out parameter to Create_Task.
7084 -- One entry_family component is present for each entry family in the
7085 -- task definition. The bounds correspond to the bounds of the entry
7086 -- family (which may depend on discriminants). The element type is
7087 -- void, since we only need the bounds information for determining
7088 -- the entry index. Note that the use of an anonymous array would
7089 -- normally be illegal in this context, but this is a parser check,
7090 -- and the semantics is quite prepared to handle such a case.
7092 -- The _Size field is present only if a Storage_Size pragma appears in
7093 -- the task definition. The expression captures the argument that was
7094 -- present in the pragma, and is used to override the task stack size
7095 -- otherwise associated with the task type.
7097 -- The _Priority field is present only if a Priority or Interrupt_Priority
7098 -- pragma appears in the task definition. The expression captures the
7099 -- argument that was present in the pragma, and is used to provide
7100 -- the Size parameter to the call to Create_Task.
7102 -- The _Task_Info field is present only if a Task_Info pragma appears in
7103 -- the task definition. The expression captures the argument that was
7104 -- present in the pragma, and is used to provide the Task_Image parameter
7105 -- to the call to Create_Task.
7107 -- When a task is declared, an instance of the task value record is
7108 -- created. The elaboration of this declaration creates the correct
7109 -- bounds for the entry families, and also evaluates the size, priority,
7110 -- and task_Info expressions if needed. The initialization routine for
7111 -- the task type itself then calls Create_Task with appropriate
7112 -- parameters to initialize the value of the Task_Id field.
7114 -- Note: the address of this record is passed as the "Discriminants"
7115 -- parameter for Create_Task. Since Create_Task merely passes this onto
7116 -- the body procedure, it does not matter that it does not quite match
7117 -- the GNARLI model of what is being passed (the record contains more
7118 -- than just the discriminants, but the discriminants can be found from
7119 -- the record value).
7121 -- The Entity_Id for this created record type is placed in the
7122 -- Corresponding_Record_Type field of the associated task type entity.
7124 -- Next we create a procedure specification for the task body procedure:
7126 -- procedure taskB (_Task : access taskV);
7128 -- Note that this must come after the record type declaration, since
7129 -- the spec refers to this type. It turns out that the initialization
7130 -- procedure for the value type references the task body spec, but that's
7131 -- fine, since it won't be generated till the freeze point for the type,
7132 -- which is certainly after the task body spec declaration.
7134 -- Finally, we set the task index value field of the entry attribute in
7135 -- the case of a simple entry.
7137 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
7138 Loc : constant Source_Ptr := Sloc (N);
7139 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
7140 Tasknm : constant Name_Id := Chars (Tasktyp);
7141 Taskdef : constant Node_Id := Task_Definition (N);
7143 Proc_Spec : Node_Id;
7144 Rec_Decl : Node_Id;
7145 Rec_Ent : Entity_Id;
7146 Cdecls : List_Id;
7147 Elab_Decl : Node_Id;
7148 Size_Decl : Node_Id;
7149 Body_Decl : Node_Id;
7150 Task_Size : Node_Id;
7151 Ent_Stack : Entity_Id;
7152 Decl_Stack : Node_Id;
7154 begin
7155 -- If already expanded, nothing to do
7157 if Present (Corresponding_Record_Type (Tasktyp)) then
7158 return;
7159 end if;
7161 -- Here we will do the expansion
7163 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
7164 Rec_Ent := Defining_Identifier (Rec_Decl);
7165 Cdecls := Component_Items (Component_List
7166 (Type_Definition (Rec_Decl)));
7168 Qualify_Entity_Names (N);
7170 -- First create the elaboration variable
7172 Elab_Decl :=
7173 Make_Object_Declaration (Loc,
7174 Defining_Identifier =>
7175 Make_Defining_Identifier (Sloc (Tasktyp),
7176 Chars => New_External_Name (Tasknm, 'E')),
7177 Aliased_Present => True,
7178 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
7179 Expression => New_Reference_To (Standard_False, Loc));
7180 Insert_After (N, Elab_Decl);
7182 -- Next create the declaration of the size variable (tasknmZ)
7184 Set_Storage_Size_Variable (Tasktyp,
7185 Make_Defining_Identifier (Sloc (Tasktyp),
7186 Chars => New_External_Name (Tasknm, 'Z')));
7188 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) and then
7189 Is_Static_Expression (Expression (First (
7190 Pragma_Argument_Associations (Find_Task_Or_Protected_Pragma (
7191 Taskdef, Name_Storage_Size)))))
7192 then
7193 Size_Decl :=
7194 Make_Object_Declaration (Loc,
7195 Defining_Identifier => Storage_Size_Variable (Tasktyp),
7196 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
7197 Expression =>
7198 Convert_To (RTE (RE_Size_Type),
7199 Relocate_Node (
7200 Expression (First (
7201 Pragma_Argument_Associations (
7202 Find_Task_Or_Protected_Pragma
7203 (Taskdef, Name_Storage_Size)))))));
7205 else
7206 Size_Decl :=
7207 Make_Object_Declaration (Loc,
7208 Defining_Identifier => Storage_Size_Variable (Tasktyp),
7209 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
7210 Expression => New_Reference_To (RTE (RE_Unspecified_Size), Loc));
7211 end if;
7213 Insert_After (Elab_Decl, Size_Decl);
7215 -- Next build the rest of the corresponding record declaration.
7216 -- This is done last, since the corresponding record initialization
7217 -- procedure will reference the previously created entities.
7219 -- Fill in the component declarations -- first the _Task_Id field
7221 Append_To (Cdecls,
7222 Make_Component_Declaration (Loc,
7223 Defining_Identifier =>
7224 Make_Defining_Identifier (Loc, Name_uTask_Id),
7225 Component_Definition =>
7226 Make_Component_Definition (Loc,
7227 Aliased_Present => False,
7228 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
7229 Loc))));
7231 -- Declare static ATCB (that is, created by the expander) if we
7232 -- are using the Restricted run time.
7234 if Restricted_Profile then
7235 Append_To (Cdecls,
7236 Make_Component_Declaration (Loc,
7237 Defining_Identifier =>
7238 Make_Defining_Identifier (Loc, Name_uATCB),
7240 Component_Definition =>
7241 Make_Component_Definition (Loc,
7242 Aliased_Present => True,
7243 Subtype_Indication => Make_Subtype_Indication (Loc,
7244 Subtype_Mark => New_Occurrence_Of
7245 (RTE (RE_Ada_Task_Control_Block), Loc),
7247 Constraint =>
7248 Make_Index_Or_Discriminant_Constraint (Loc,
7249 Constraints =>
7250 New_List (Make_Integer_Literal (Loc, 0)))))));
7252 end if;
7254 -- Declare static stack (that is, created by the expander) if we
7255 -- are using the Restricted run time on a bare board configuration.
7257 if Restricted_Profile
7258 and then Preallocated_Stacks_On_Target
7259 then
7260 -- First we need to extract the appropriate stack size
7262 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
7264 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
7265 Task_Size := Relocate_Node (
7266 Expression (First (
7267 Pragma_Argument_Associations (
7268 Find_Task_Or_Protected_Pragma
7269 (Taskdef, Name_Storage_Size)))));
7270 else
7271 Task_Size :=
7272 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
7273 end if;
7275 Decl_Stack := Make_Component_Declaration (Loc,
7276 Defining_Identifier => Ent_Stack,
7278 Component_Definition =>
7279 Make_Component_Definition (Loc,
7280 Aliased_Present => True,
7281 Subtype_Indication => Make_Subtype_Indication (Loc,
7282 Subtype_Mark =>
7283 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
7285 Constraint =>
7286 Make_Index_Or_Discriminant_Constraint (Loc,
7287 Constraints => New_List (Make_Range (Loc,
7288 Low_Bound => Make_Integer_Literal (Loc, 1),
7289 High_Bound => Convert_To (RTE (RE_Storage_Offset),
7290 Task_Size)))))));
7292 Append_To (Cdecls, Decl_Stack);
7294 -- The appropriate alignment for the stack is ensured by the
7295 -- run-time code in charge of task creation.
7297 end if;
7299 -- Add components for entry families
7301 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
7303 -- Add the _Priority component if a Priority pragma is present
7305 if Present (Taskdef) and then Has_Priority_Pragma (Taskdef) then
7306 declare
7307 Prag : constant Node_Id :=
7308 Find_Task_Or_Protected_Pragma (Taskdef, Name_Priority);
7309 Expr : Node_Id;
7311 begin
7312 Expr := First (Pragma_Argument_Associations (Prag));
7314 if Nkind (Expr) = N_Pragma_Argument_Association then
7315 Expr := Expression (Expr);
7316 end if;
7318 Expr := New_Copy_Tree (Expr);
7320 -- Add conversion to proper type to do range check if required
7321 -- Note that for runtime units, we allow out of range interrupt
7322 -- priority values to be used in a priority pragma. This is for
7323 -- the benefit of some versions of System.Interrupts which use
7324 -- a special server task with maximum interrupt priority.
7326 if Chars (Prag) = Name_Priority
7327 and then not GNAT_Mode
7328 then
7329 Rewrite (Expr, Convert_To (RTE (RE_Priority), Expr));
7330 else
7331 Rewrite (Expr, Convert_To (RTE (RE_Any_Priority), Expr));
7332 end if;
7334 Append_To (Cdecls,
7335 Make_Component_Declaration (Loc,
7336 Defining_Identifier =>
7337 Make_Defining_Identifier (Loc, Name_uPriority),
7338 Component_Definition =>
7339 Make_Component_Definition (Loc,
7340 Aliased_Present => False,
7341 Subtype_Indication => New_Reference_To (Standard_Integer,
7342 Loc)),
7343 Expression => Expr));
7344 end;
7345 end if;
7347 -- Add the _Task_Size component if a Storage_Size pragma is present
7349 if Present (Taskdef)
7350 and then Has_Storage_Size_Pragma (Taskdef)
7351 then
7352 Append_To (Cdecls,
7353 Make_Component_Declaration (Loc,
7354 Defining_Identifier =>
7355 Make_Defining_Identifier (Loc, Name_uSize),
7357 Component_Definition =>
7358 Make_Component_Definition (Loc,
7359 Aliased_Present => False,
7360 Subtype_Indication => New_Reference_To (RTE (RE_Size_Type),
7361 Loc)),
7363 Expression =>
7364 Convert_To (RTE (RE_Size_Type),
7365 Relocate_Node (
7366 Expression (First (
7367 Pragma_Argument_Associations (
7368 Find_Task_Or_Protected_Pragma
7369 (Taskdef, Name_Storage_Size))))))));
7370 end if;
7372 -- Add the _Task_Info component if a Task_Info pragma is present
7374 if Present (Taskdef) and then Has_Task_Info_Pragma (Taskdef) then
7375 Append_To (Cdecls,
7376 Make_Component_Declaration (Loc,
7377 Defining_Identifier =>
7378 Make_Defining_Identifier (Loc, Name_uTask_Info),
7380 Component_Definition =>
7381 Make_Component_Definition (Loc,
7382 Aliased_Present => False,
7383 Subtype_Indication =>
7384 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
7386 Expression => New_Copy (
7387 Expression (First (
7388 Pragma_Argument_Associations (
7389 Find_Task_Or_Protected_Pragma
7390 (Taskdef, Name_Task_Info)))))));
7391 end if;
7393 Insert_After (Size_Decl, Rec_Decl);
7395 -- Analyze the record declaration immediately after construction,
7396 -- because the initialization procedure is needed for single task
7397 -- declarations before the next entity is analyzed.
7399 Analyze (Rec_Decl);
7401 -- Create the declaration of the task body procedure
7403 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
7404 Body_Decl :=
7405 Make_Subprogram_Declaration (Loc,
7406 Specification => Proc_Spec);
7408 Insert_After (Rec_Decl, Body_Decl);
7410 -- The subprogram does not comes from source, so we have to indicate
7411 -- the need for debugging information explicitly.
7413 Set_Needs_Debug_Info
7414 (Defining_Entity (Proc_Spec), Comes_From_Source (Original_Node (N)));
7416 -- Now we can freeze the corresponding record. This needs manually
7417 -- freezing, since it is really part of the task type, and the task
7418 -- type is frozen at this stage. We of course need the initialization
7419 -- procedure for this corresponding record type and we won't get it
7420 -- in time if we don't freeze now.
7422 declare
7423 L : constant List_Id := Freeze_Entity (Rec_Ent, Loc);
7425 begin
7426 if Is_Non_Empty_List (L) then
7427 Insert_List_After (Body_Decl, L);
7428 end if;
7429 end;
7431 -- Complete the expansion of access types to the current task
7432 -- type, if any were declared.
7434 Expand_Previous_Access_Type (Tasktyp);
7435 end Expand_N_Task_Type_Declaration;
7437 -------------------------------
7438 -- Expand_N_Timed_Entry_Call --
7439 -------------------------------
7441 -- A timed entry call in normal case is not implemented using ATC
7442 -- mechanism anymore for efficiency reason.
7444 -- select
7445 -- T.E;
7446 -- S1;
7447 -- or
7448 -- Delay D;
7449 -- S2;
7450 -- end select;
7452 -- is expanded as follow:
7454 -- 1) When T.E is a task entry_call;
7456 -- declare
7457 -- B : Boolean;
7458 -- X : Task_Entry_Index := <entry index>;
7459 -- DX : Duration := To_Duration (D);
7460 -- M : Delay_Mode := <discriminant>;
7461 -- P : parms := (parm, parm, parm);
7463 -- begin
7464 -- Timed_Protected_Entry_Call (<acceptor-task>, X, P'Address,
7465 -- DX, M, B);
7466 -- if B then
7467 -- S1;
7468 -- else
7469 -- S2;
7470 -- end if;
7471 -- end;
7473 -- 2) When T.E is a protected entry_call;
7475 -- declare
7476 -- B : Boolean;
7477 -- X : Protected_Entry_Index := <entry index>;
7478 -- DX : Duration := To_Duration (D);
7479 -- M : Delay_Mode := <discriminant>;
7480 -- P : parms := (parm, parm, parm);
7482 -- begin
7483 -- Timed_Protected_Entry_Call (<object>'unchecked_access, X,
7484 -- P'Address, DX, M, B);
7485 -- if B then
7486 -- S1;
7487 -- else
7488 -- S2;
7489 -- end if;
7490 -- end;
7492 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
7493 Loc : constant Source_Ptr := Sloc (N);
7495 E_Call : Node_Id :=
7496 Entry_Call_Statement (Entry_Call_Alternative (N));
7497 E_Stats : constant List_Id :=
7498 Statements (Entry_Call_Alternative (N));
7499 D_Stat : constant Node_Id :=
7500 Delay_Statement (Delay_Alternative (N));
7501 D_Stats : constant List_Id :=
7502 Statements (Delay_Alternative (N));
7504 Stmts : List_Id;
7505 Stmt : Node_Id;
7506 Parms : List_Id;
7507 Parm : Node_Id;
7509 Concval : Node_Id;
7510 Ename : Node_Id;
7511 Index : Node_Id;
7513 Decls : List_Id;
7514 Disc : Node_Id;
7515 Conv : Node_Id;
7516 B : Entity_Id;
7517 D : Entity_Id;
7518 Dtyp : Entity_Id;
7519 M : Entity_Id;
7521 Call : Node_Id;
7522 Dummy : Node_Id;
7524 begin
7525 -- The arguments in the call may require dynamic allocation, and the
7526 -- call statement may have been transformed into a block. The block
7527 -- may contain additional declarations for internal entities, and the
7528 -- original call is found by sequential search.
7530 if Nkind (E_Call) = N_Block_Statement then
7531 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
7533 while Nkind (E_Call) /= N_Procedure_Call_Statement
7534 and then Nkind (E_Call) /= N_Entry_Call_Statement
7535 loop
7536 Next (E_Call);
7537 end loop;
7538 end if;
7540 -- Build an entry call using Simple_Entry_Call. We will use this as the
7541 -- base for creating appropriate calls.
7543 Extract_Entry (E_Call, Concval, Ename, Index);
7544 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
7546 Stmts := Statements (Handled_Statement_Sequence (E_Call));
7547 Decls := Declarations (E_Call);
7549 if No (Decls) then
7550 Decls := New_List;
7551 end if;
7553 Dtyp := Base_Type (Etype (Expression (D_Stat)));
7555 -- Use the type of the delay expression (Calendar or Real_Time)
7556 -- to generate the appropriate conversion.
7558 if Nkind (D_Stat) = N_Delay_Relative_Statement then
7559 Disc := Make_Integer_Literal (Loc, 0);
7560 Conv := Relocate_Node (Expression (D_Stat));
7562 elsif Is_RTE (Dtyp, RO_CA_Time) then
7563 Disc := Make_Integer_Literal (Loc, 1);
7564 Conv := Make_Function_Call (Loc,
7565 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
7566 New_List (New_Copy (Expression (D_Stat))));
7568 else pragma Assert (Is_RTE (Dtyp, RO_RT_Time));
7569 Disc := Make_Integer_Literal (Loc, 2);
7570 Conv := Make_Function_Call (Loc,
7571 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
7572 New_List (New_Copy (Expression (D_Stat))));
7573 end if;
7575 -- Create Duration and Delay_Mode objects for passing a delay value
7577 D := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
7578 M := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
7580 Append_To (Decls,
7581 Make_Object_Declaration (Loc,
7582 Defining_Identifier => D,
7583 Object_Definition => New_Reference_To (Standard_Duration, Loc)));
7585 Append_To (Decls,
7586 Make_Object_Declaration (Loc,
7587 Defining_Identifier => M,
7588 Object_Definition => New_Reference_To (Standard_Integer, Loc),
7589 Expression => Disc));
7591 B := Make_Defining_Identifier (Loc, Name_uB);
7593 -- Create a boolean object used for a return parameter
7595 Prepend_To (Decls,
7596 Make_Object_Declaration (Loc,
7597 Defining_Identifier => B,
7598 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7600 Stmt := First (Stmts);
7602 -- Skip assignments to temporaries created for in-out parameters.
7603 -- This makes unwarranted assumptions about the shape of the expanded
7604 -- tree for the call, and should be cleaned up ???
7606 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7607 Next (Stmt);
7608 end loop;
7610 -- Do the assignement at this stage only because the evaluation of the
7611 -- expression must not occur before (see ACVC C97302A).
7613 Insert_Before (Stmt,
7614 Make_Assignment_Statement (Loc,
7615 Name => New_Reference_To (D, Loc),
7616 Expression => Conv));
7618 Call := Stmt;
7620 Parms := Parameter_Associations (Call);
7622 -- For a protected type, we build a Timed_Protected_Entry_Call
7624 if Is_Protected_Type (Etype (Concval)) then
7626 -- Create a new call statement
7628 Parm := First (Parms);
7630 while Present (Parm)
7631 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
7632 loop
7633 Next (Parm);
7634 end loop;
7636 Dummy := Remove_Next (Next (Parm));
7638 -- Remove garbage is following the Cancel_Param if present
7640 Dummy := Next (Parm);
7642 -- Remove the mode of the Protected_Entry_Call call, then remove the
7643 -- Communication_Block of the Protected_Entry_Call call, and finally
7644 -- add Duration and a Delay_Mode parameter
7646 pragma Assert (Present (Parm));
7647 Rewrite (Parm, New_Reference_To (D, Loc));
7649 Rewrite (Dummy, New_Reference_To (M, Loc));
7651 -- Add a Boolean flag for successful entry call
7653 Append_To (Parms, New_Reference_To (B, Loc));
7655 if Abort_Allowed
7656 or else Restriction_Active (No_Entry_Queue) = False
7657 or else Number_Entries (Etype (Concval)) > 1
7658 then
7659 Rewrite (Call,
7660 Make_Procedure_Call_Statement (Loc,
7661 Name =>
7662 New_Reference_To (RTE (RE_Timed_Protected_Entry_Call), Loc),
7663 Parameter_Associations => Parms));
7665 else
7666 Parm := First (Parms);
7668 while Present (Parm)
7669 and then not Is_RTE (Etype (Parm), RE_Protected_Entry_Index)
7670 loop
7671 Next (Parm);
7672 end loop;
7674 Remove (Parm);
7676 Rewrite (Call,
7677 Make_Procedure_Call_Statement (Loc,
7678 Name => New_Reference_To (
7679 RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
7680 Parameter_Associations => Parms));
7681 end if;
7683 -- For the task case, build a Timed_Task_Entry_Call
7685 else
7686 -- Create a new call statement
7688 Append_To (Parms, New_Reference_To (D, Loc));
7689 Append_To (Parms, New_Reference_To (M, Loc));
7690 Append_To (Parms, New_Reference_To (B, Loc));
7692 Rewrite (Call,
7693 Make_Procedure_Call_Statement (Loc,
7694 Name => New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
7695 Parameter_Associations => Parms));
7697 end if;
7699 Append_To (Stmts,
7700 Make_Implicit_If_Statement (N,
7701 Condition => New_Reference_To (B, Loc),
7702 Then_Statements => E_Stats,
7703 Else_Statements => D_Stats));
7705 Rewrite (N,
7706 Make_Block_Statement (Loc,
7707 Declarations => Decls,
7708 Handled_Statement_Sequence =>
7709 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7711 Analyze (N);
7712 end Expand_N_Timed_Entry_Call;
7714 ----------------------------------------
7715 -- Expand_Protected_Body_Declarations --
7716 ----------------------------------------
7718 -- Part of the expansion of a protected body involves the creation of
7719 -- a declaration that can be referenced from the statement sequences of
7720 -- the entry bodies:
7722 -- A : Address;
7724 -- This declaration is inserted in the declarations of the service
7725 -- entries procedure for the protected body, and it is important that
7726 -- it be inserted before the statements of the entry body statement
7727 -- sequences are analyzed. Thus it would be too late to create this
7728 -- declaration in the Expand_N_Protected_Body routine, which is why
7729 -- there is a separate procedure to be called directly from Sem_Ch9.
7731 -- Ann is used to hold the address of the record containing the parameters
7732 -- (see Expand_N_Entry_Call for more details on how this record is built).
7733 -- References to the parameters do an unchecked conversion of this address
7734 -- to a pointer to the required record type, and then access the field that
7735 -- holds the value of the required parameter. The entity for the address
7736 -- variable is held as the top stack element (i.e. the last element) of the
7737 -- Accept_Address stack in the corresponding entry entity, and this element
7738 -- must be set in place before the statements are processed.
7740 -- No stack is needed for entry bodies, since they cannot be nested, but
7741 -- it is kept for consistency between protected and task entries. The
7742 -- stack will never contain more than one element. There is also only one
7743 -- such variable for a given protected body, but this is placed on the
7744 -- Accept_Address stack of all of the entries, again for consistency.
7746 -- To expand the requeue statement, a label is provided at the end of
7747 -- the loop in the entry service routine created by the expander (see
7748 -- Expand_N_Protected_Body for details), so that the statement can be
7749 -- skipped after the requeue is complete. This label is created during the
7750 -- expansion of the entry body, which will take place after the expansion
7751 -- of the requeue statements that it contains, so a placeholder defining
7752 -- identifier is associated with the task type here.
7754 -- Another label is provided following case statement created by the
7755 -- expander. This label is need for implementing return statement from
7756 -- entry body so that a return can be expanded as a goto to this label.
7757 -- This label is created during the expansion of the entry body, which
7758 -- will take place after the expansion of the return statements that it
7759 -- contains. Therefore, just like the label for expanding requeues, we
7760 -- need another placeholder for the label.
7762 procedure Expand_Protected_Body_Declarations
7763 (N : Node_Id;
7764 Spec_Id : Entity_Id)
7766 Op : Node_Id;
7768 begin
7769 if No_Run_Time_Mode then
7770 Error_Msg_CRT ("protected body", N);
7771 return;
7773 elsif Expander_Active then
7775 -- Associate privals with the first subprogram or entry
7776 -- body to be expanded. These are used to expand references
7777 -- to private data objects.
7779 Op := First_Protected_Operation (Declarations (N));
7781 if Present (Op) then
7782 Set_Discriminals (Parent (Spec_Id));
7783 Set_Privals (Parent (Spec_Id), Op, Sloc (N));
7784 end if;
7785 end if;
7786 end Expand_Protected_Body_Declarations;
7788 -------------------------
7789 -- External_Subprogram --
7790 -------------------------
7792 function External_Subprogram (E : Entity_Id) return Entity_Id is
7793 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
7794 Decl : constant Node_Id := Unit_Declaration_Node (E);
7796 begin
7797 -- If the protected operation is defined in the visible part of the
7798 -- protected type, or if it is an interrupt handler, the internal and
7799 -- external subprograms follow each other on the entity chain. If the
7800 -- operation is defined in the private part of the type, there is no
7801 -- need for a separate locking version of the operation, and internal
7802 -- calls use the protected_body_subprogram directly.
7804 if List_Containing (Decl) = Visible_Declarations (Parent (Decl))
7805 or else Is_Interrupt_Handler (E)
7806 then
7807 return Next_Entity (Subp);
7808 else
7809 return (Subp);
7810 end if;
7811 end External_Subprogram;
7813 -------------------
7814 -- Extract_Entry --
7815 -------------------
7817 procedure Extract_Entry
7818 (N : Node_Id;
7819 Concval : out Node_Id;
7820 Ename : out Node_Id;
7821 Index : out Node_Id)
7823 Nam : constant Node_Id := Name (N);
7825 begin
7826 -- For a simple entry, the name is a selected component, with the
7827 -- prefix being the task value, and the selector being the entry.
7829 if Nkind (Nam) = N_Selected_Component then
7830 Concval := Prefix (Nam);
7831 Ename := Selector_Name (Nam);
7832 Index := Empty;
7834 -- For a member of an entry family, the name is an indexed
7835 -- component where the prefix is a selected component,
7836 -- whose prefix in turn is the task value, and whose
7837 -- selector is the entry family. The single expression in
7838 -- the expressions list of the indexed component is the
7839 -- subscript for the family.
7841 else
7842 pragma Assert (Nkind (Nam) = N_Indexed_Component);
7843 Concval := Prefix (Prefix (Nam));
7844 Ename := Selector_Name (Prefix (Nam));
7845 Index := First (Expressions (Nam));
7846 end if;
7847 end Extract_Entry;
7849 -------------------
7850 -- Family_Offset --
7851 -------------------
7853 function Family_Offset
7854 (Loc : Source_Ptr;
7855 Hi : Node_Id;
7856 Lo : Node_Id;
7857 Ttyp : Entity_Id) return Node_Id
7859 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
7860 -- If one of the bounds is a reference to a discriminant, replace
7861 -- with corresponding discriminal of type. Within the body of a task
7862 -- retrieve the renamed discriminant by simple visibility, using its
7863 -- generated name. Within a protected object, find the original dis-
7864 -- criminant and replace it with the discriminal of the current prot-
7865 -- ected operation.
7867 ------------------------------
7868 -- Convert_Discriminant_Ref --
7869 ------------------------------
7871 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
7872 Loc : constant Source_Ptr := Sloc (Bound);
7873 B : Node_Id;
7874 D : Entity_Id;
7876 begin
7877 if Is_Entity_Name (Bound)
7878 and then Ekind (Entity (Bound)) = E_Discriminant
7879 then
7880 if Is_Task_Type (Ttyp)
7881 and then Has_Completion (Ttyp)
7882 then
7883 B := Make_Identifier (Loc, Chars (Entity (Bound)));
7884 Find_Direct_Name (B);
7886 elsif Is_Protected_Type (Ttyp) then
7887 D := First_Discriminant (Ttyp);
7889 while Chars (D) /= Chars (Entity (Bound)) loop
7890 Next_Discriminant (D);
7891 end loop;
7893 B := New_Reference_To (Discriminal (D), Loc);
7895 else
7896 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
7897 end if;
7899 elsif Nkind (Bound) = N_Attribute_Reference then
7900 return Bound;
7902 else
7903 B := New_Copy_Tree (Bound);
7904 end if;
7906 return
7907 Make_Attribute_Reference (Loc,
7908 Attribute_Name => Name_Pos,
7909 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
7910 Expressions => New_List (B));
7911 end Convert_Discriminant_Ref;
7913 -- Start of processing for Family_Offset
7915 begin
7916 return
7917 Make_Op_Subtract (Loc,
7918 Left_Opnd => Convert_Discriminant_Ref (Hi),
7919 Right_Opnd => Convert_Discriminant_Ref (Lo));
7920 end Family_Offset;
7922 -----------------
7923 -- Family_Size --
7924 -----------------
7926 function Family_Size
7927 (Loc : Source_Ptr;
7928 Hi : Node_Id;
7929 Lo : Node_Id;
7930 Ttyp : Entity_Id) return Node_Id
7932 Ityp : Entity_Id;
7934 begin
7935 if Is_Task_Type (Ttyp) then
7936 Ityp := RTE (RE_Task_Entry_Index);
7937 else
7938 Ityp := RTE (RE_Protected_Entry_Index);
7939 end if;
7941 return
7942 Make_Attribute_Reference (Loc,
7943 Prefix => New_Reference_To (Ityp, Loc),
7944 Attribute_Name => Name_Max,
7945 Expressions => New_List (
7946 Make_Op_Add (Loc,
7947 Left_Opnd =>
7948 Family_Offset (Loc, Hi, Lo, Ttyp),
7949 Right_Opnd =>
7950 Make_Integer_Literal (Loc, 1)),
7951 Make_Integer_Literal (Loc, 0)));
7952 end Family_Size;
7954 -----------------------------------
7955 -- Find_Task_Or_Protected_Pragma --
7956 -----------------------------------
7958 function Find_Task_Or_Protected_Pragma
7959 (T : Node_Id;
7960 P : Name_Id) return Node_Id
7962 N : Node_Id;
7964 begin
7965 N := First (Visible_Declarations (T));
7967 while Present (N) loop
7968 if Nkind (N) = N_Pragma then
7969 if Chars (N) = P then
7970 return N;
7972 elsif P = Name_Priority
7973 and then Chars (N) = Name_Interrupt_Priority
7974 then
7975 return N;
7977 else
7978 Next (N);
7979 end if;
7981 else
7982 Next (N);
7983 end if;
7984 end loop;
7986 N := First (Private_Declarations (T));
7988 while Present (N) loop
7989 if Nkind (N) = N_Pragma then
7990 if Chars (N) = P then
7991 return N;
7993 elsif P = Name_Priority
7994 and then Chars (N) = Name_Interrupt_Priority
7995 then
7996 return N;
7998 else
7999 Next (N);
8000 end if;
8002 else
8003 Next (N);
8004 end if;
8005 end loop;
8007 raise Program_Error;
8008 end Find_Task_Or_Protected_Pragma;
8010 -------------------------------
8011 -- First_Protected_Operation --
8012 -------------------------------
8014 function First_Protected_Operation (D : List_Id) return Node_Id is
8015 First_Op : Node_Id;
8017 begin
8018 First_Op := First (D);
8019 while Present (First_Op)
8020 and then Nkind (First_Op) /= N_Subprogram_Body
8021 and then Nkind (First_Op) /= N_Entry_Body
8022 loop
8023 Next (First_Op);
8024 end loop;
8026 return First_Op;
8027 end First_Protected_Operation;
8029 --------------------------------
8030 -- Index_Constant_Declaration --
8031 --------------------------------
8033 function Index_Constant_Declaration
8034 (N : Node_Id;
8035 Index_Id : Entity_Id;
8036 Prot : Entity_Id) return List_Id
8038 Loc : constant Source_Ptr := Sloc (N);
8039 Decls : constant List_Id := New_List;
8040 Index_Con : constant Entity_Id := Entry_Index_Constant (Index_Id);
8041 Index_Typ : Entity_Id;
8043 Hi : Node_Id := Type_High_Bound (Etype (Index_Id));
8044 Lo : Node_Id := Type_Low_Bound (Etype (Index_Id));
8046 function Replace_Discriminant (Bound : Node_Id) return Node_Id;
8047 -- The bounds of the entry index may depend on discriminants, so
8048 -- each declaration of an entry_index_constant must have its own
8049 -- subtype declaration, using the local renaming of the object discri-
8050 -- minant.
8052 --------------------------
8053 -- Replace_Discriminant --
8054 --------------------------
8056 function Replace_Discriminant (Bound : Node_Id) return Node_Id is
8057 begin
8058 if Nkind (Bound) = N_Identifier
8059 and then Ekind (Entity (Bound)) = E_Constant
8060 and then Present (Discriminal_Link (Entity (Bound)))
8061 then
8062 return Make_Identifier (Loc, Chars (Entity (Bound)));
8063 else
8064 return Duplicate_Subexpr (Bound);
8065 end if;
8066 end Replace_Discriminant;
8068 -- Start of processing for Index_Constant_Declaration
8070 begin
8071 Set_Discriminal_Link (Index_Con, Index_Id);
8073 if Is_Entity_Name (
8074 Original_Node (Discrete_Subtype_Definition (Parent (Index_Id))))
8075 then
8076 -- Simple case: entry family is given by a subtype mark, and index
8077 -- constant has the same type, no replacement needed.
8079 Index_Typ := Etype (Index_Id);
8081 else
8082 Hi := Replace_Discriminant (Hi);
8083 Lo := Replace_Discriminant (Lo);
8085 Index_Typ := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
8087 Append (
8088 Make_Subtype_Declaration (Loc,
8089 Defining_Identifier => Index_Typ,
8090 Subtype_Indication =>
8091 Make_Subtype_Indication (Loc,
8092 Subtype_Mark =>
8093 New_Occurrence_Of (Base_Type (Etype (Index_Id)), Loc),
8094 Constraint =>
8095 Make_Range_Constraint (Loc,
8096 Range_Expression => Make_Range (Loc, Lo, Hi)))),
8097 Decls);
8099 end if;
8101 Append (
8102 Make_Object_Declaration (Loc,
8103 Defining_Identifier => Index_Con,
8104 Constant_Present => True,
8105 Object_Definition => New_Occurrence_Of (Index_Typ, Loc),
8107 Expression =>
8108 Make_Attribute_Reference (Loc,
8109 Prefix => New_Reference_To (Index_Typ, Loc),
8110 Attribute_Name => Name_Val,
8112 Expressions => New_List (
8114 Make_Op_Add (Loc,
8115 Left_Opnd =>
8116 Make_Op_Subtract (Loc,
8117 Left_Opnd => Make_Identifier (Loc, Name_uE),
8118 Right_Opnd =>
8119 Entry_Index_Expression (Loc,
8120 Defining_Identifier (N), Empty, Prot)),
8122 Right_Opnd =>
8123 Make_Attribute_Reference (Loc,
8124 Prefix => New_Reference_To (Index_Typ, Loc),
8125 Attribute_Name => Name_Pos,
8126 Expressions => New_List (
8127 Make_Attribute_Reference (Loc,
8128 Prefix => New_Reference_To (Index_Typ, Loc),
8129 Attribute_Name => Name_First))))))),
8130 Decls);
8132 return Decls;
8133 end Index_Constant_Declaration;
8135 --------------------------------
8136 -- Make_Initialize_Protection --
8137 --------------------------------
8139 function Make_Initialize_Protection
8140 (Protect_Rec : Entity_Id) return List_Id
8142 Loc : constant Source_Ptr := Sloc (Protect_Rec);
8143 P_Arr : Entity_Id;
8144 Pdef : Node_Id;
8145 Pdec : Node_Id;
8146 Ptyp : constant Node_Id :=
8147 Corresponding_Concurrent_Type (Protect_Rec);
8148 Args : List_Id;
8149 L : constant List_Id := New_List;
8150 Has_Entry : constant Boolean := Has_Entries (Ptyp);
8151 Restricted : constant Boolean := Restricted_Profile;
8153 begin
8154 -- We may need two calls to properly initialize the object, one
8155 -- to Initialize_Protection, and possibly one to Install_Handlers
8156 -- if we have a pragma Attach_Handler.
8158 -- Get protected declaration. In the case of a task type declaration,
8159 -- this is simply the parent of the protected type entity.
8160 -- In the single protected object
8161 -- declaration, this parent will be the implicit type, and we can find
8162 -- the corresponding single protected object declaration by
8163 -- searching forward in the declaration list in the tree.
8164 -- ??? I am not sure that the test for N_Single_Protected_Declaration
8165 -- is needed here. Nodes of this type should have been removed
8166 -- during semantic analysis.
8168 Pdec := Parent (Ptyp);
8170 while Nkind (Pdec) /= N_Protected_Type_Declaration
8171 and then Nkind (Pdec) /= N_Single_Protected_Declaration
8172 loop
8173 Next (Pdec);
8174 end loop;
8176 -- Now we can find the object definition from this declaration
8178 Pdef := Protected_Definition (Pdec);
8180 -- Build the parameter list for the call. Note that _Init is the name
8181 -- of the formal for the object to be initialized, which is the task
8182 -- value record itself.
8184 Args := New_List;
8186 -- Object parameter. This is a pointer to the object of type
8187 -- Protection used by the GNARL to control the protected object.
8189 Append_To (Args,
8190 Make_Attribute_Reference (Loc,
8191 Prefix =>
8192 Make_Selected_Component (Loc,
8193 Prefix => Make_Identifier (Loc, Name_uInit),
8194 Selector_Name => Make_Identifier (Loc, Name_uObject)),
8195 Attribute_Name => Name_Unchecked_Access));
8197 -- Priority parameter. Set to Unspecified_Priority unless there is a
8198 -- priority pragma, in which case we take the value from the pragma,
8199 -- or there is an interrupt pragma and no priority pragma, and we
8200 -- set the ceiling to Interrupt_Priority'Last, an implementation-
8201 -- defined value, see D.3(10).
8203 if Present (Pdef)
8204 and then Has_Priority_Pragma (Pdef)
8205 then
8206 Append_To (Args,
8207 Duplicate_Subexpr_No_Checks
8208 (Expression
8209 (First
8210 (Pragma_Argument_Associations
8211 (Find_Task_Or_Protected_Pragma (Pdef, Name_Priority))))));
8213 elsif Has_Interrupt_Handler (Ptyp)
8214 or else Has_Attach_Handler (Ptyp)
8215 then
8216 -- When no priority is specified but an xx_Handler pragma is,
8217 -- we default to System.Interrupts.Default_Interrupt_Priority,
8218 -- see D.3(10).
8220 Append_To (Args,
8221 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
8223 else
8224 Append_To (Args,
8225 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
8226 end if;
8228 if Has_Entry
8229 or else Has_Interrupt_Handler (Ptyp)
8230 or else Has_Attach_Handler (Ptyp)
8231 then
8232 -- Compiler_Info parameter. This parameter allows entry body
8233 -- procedures and barrier functions to be called from the runtime.
8234 -- It is a pointer to the record generated by the compiler to
8235 -- represent the protected object.
8237 if Has_Entry or else not Restricted then
8238 Append_To (Args,
8239 Make_Attribute_Reference (Loc,
8240 Prefix => Make_Identifier (Loc, Name_uInit),
8241 Attribute_Name => Name_Address));
8242 end if;
8244 if Has_Entry then
8245 -- Entry_Bodies parameter. This is a pointer to an array of
8246 -- pointers to the entry body procedures and barrier functions
8247 -- of the object. If the protected type has no entries this
8248 -- object will not exist; in this case, pass a null.
8250 P_Arr := Entry_Bodies_Array (Ptyp);
8252 Append_To (Args,
8253 Make_Attribute_Reference (Loc,
8254 Prefix => New_Reference_To (P_Arr, Loc),
8255 Attribute_Name => Name_Unrestricted_Access));
8257 if Abort_Allowed
8258 or else Restriction_Active (No_Entry_Queue) = False
8259 or else Number_Entries (Ptyp) > 1
8260 then
8261 -- Find index mapping function (clumsy but ok for now)
8263 while Ekind (P_Arr) /= E_Function loop
8264 Next_Entity (P_Arr);
8265 end loop;
8267 Append_To (Args,
8268 Make_Attribute_Reference (Loc,
8269 Prefix =>
8270 New_Reference_To (P_Arr, Loc),
8271 Attribute_Name => Name_Unrestricted_Access));
8272 end if;
8274 elsif not Restricted then
8275 Append_To (Args, Make_Null (Loc));
8276 Append_To (Args, Make_Null (Loc));
8277 end if;
8279 if Abort_Allowed
8280 or else Restriction_Active (No_Entry_Queue) = False
8281 or else Number_Entries (Ptyp) > 1
8282 then
8283 Append_To (L,
8284 Make_Procedure_Call_Statement (Loc,
8285 Name => New_Reference_To (
8286 RTE (RE_Initialize_Protection_Entries), Loc),
8287 Parameter_Associations => Args));
8289 elsif not Has_Entry and then Restricted then
8290 Append_To (L,
8291 Make_Procedure_Call_Statement (Loc,
8292 Name => New_Reference_To (
8293 RTE (RE_Initialize_Protection), Loc),
8294 Parameter_Associations => Args));
8296 else
8297 Append_To (L,
8298 Make_Procedure_Call_Statement (Loc,
8299 Name => New_Reference_To (
8300 RTE (RE_Initialize_Protection_Entry), Loc),
8301 Parameter_Associations => Args));
8302 end if;
8304 else
8305 Append_To (L,
8306 Make_Procedure_Call_Statement (Loc,
8307 Name => New_Reference_To (RTE (RE_Initialize_Protection), Loc),
8308 Parameter_Associations => Args));
8309 end if;
8311 if Has_Attach_Handler (Ptyp) then
8313 -- We have a list of N Attach_Handler (ProcI, ExprI),
8314 -- and we have to make the following call:
8315 -- Install_Handlers (_object,
8316 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
8317 -- or, in the case of Ravenscar:
8318 -- Install_Handlers
8319 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
8321 declare
8322 Args : constant List_Id := New_List;
8323 Table : constant List_Id := New_List;
8324 Ritem : Node_Id := First_Rep_Item (Ptyp);
8326 begin
8327 if not Restricted then
8328 -- Appends the _object argument
8330 Append_To (Args,
8331 Make_Attribute_Reference (Loc,
8332 Prefix =>
8333 Make_Selected_Component (Loc,
8334 Prefix => Make_Identifier (Loc, Name_uInit),
8335 Selector_Name => Make_Identifier (Loc, Name_uObject)),
8336 Attribute_Name => Name_Unchecked_Access));
8337 end if;
8339 -- Build the Attach_Handler table argument
8341 while Present (Ritem) loop
8342 if Nkind (Ritem) = N_Pragma
8343 and then Chars (Ritem) = Name_Attach_Handler
8344 then
8345 declare
8346 Handler : constant Node_Id :=
8347 First (Pragma_Argument_Associations (Ritem));
8349 Interrupt : constant Node_Id := Next (Handler);
8350 Expr : constant Node_Id := Expression (Interrupt);
8352 begin
8353 Append_To (Table,
8354 Make_Aggregate (Loc, Expressions => New_List (
8355 Unchecked_Convert_To
8356 (RTE (RE_System_Interrupt_Id), Expr),
8357 Make_Attribute_Reference (Loc,
8358 Prefix => Make_Selected_Component (Loc,
8359 Make_Identifier (Loc, Name_uInit),
8360 Duplicate_Subexpr_No_Checks
8361 (Expression (Handler))),
8362 Attribute_Name => Name_Access))));
8363 end;
8364 end if;
8366 Next_Rep_Item (Ritem);
8367 end loop;
8369 -- Append the table argument we just built
8371 Append_To (Args, Make_Aggregate (Loc, Table));
8373 -- Append the Install_Handler call to the statements
8375 Append_To (L,
8376 Make_Procedure_Call_Statement (Loc,
8377 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
8378 Parameter_Associations => Args));
8379 end;
8380 end if;
8382 return L;
8383 end Make_Initialize_Protection;
8385 ---------------------------
8386 -- Make_Task_Create_Call --
8387 ---------------------------
8389 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
8390 Loc : constant Source_Ptr := Sloc (Task_Rec);
8391 Name : Node_Id;
8392 Tdef : Node_Id;
8393 Tdec : Node_Id;
8394 Ttyp : Node_Id;
8395 Tnam : Name_Id;
8396 Args : List_Id;
8397 Ecount : Node_Id;
8399 begin
8400 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
8401 Tnam := Chars (Ttyp);
8403 -- Get task declaration. In the case of a task type declaration, this
8404 -- is simply the parent of the task type entity. In the single task
8405 -- declaration, this parent will be the implicit type, and we can find
8406 -- the corresponding single task declaration by searching forward in
8407 -- the declaration list in the tree.
8408 -- ??? I am not sure that the test for N_Single_Task_Declaration
8409 -- is needed here. Nodes of this type should have been removed
8410 -- during semantic analysis.
8412 Tdec := Parent (Ttyp);
8414 while Nkind (Tdec) /= N_Task_Type_Declaration
8415 and then Nkind (Tdec) /= N_Single_Task_Declaration
8416 loop
8417 Next (Tdec);
8418 end loop;
8420 -- Now we can find the task definition from this declaration
8422 Tdef := Task_Definition (Tdec);
8424 -- Build the parameter list for the call. Note that _Init is the name
8425 -- of the formal for the object to be initialized, which is the task
8426 -- value record itself.
8428 Args := New_List;
8430 -- Priority parameter. Set to Unspecified_Priority unless there is a
8431 -- priority pragma, in which case we take the value from the pragma.
8433 if Present (Tdef) and then Has_Priority_Pragma (Tdef) then
8434 Append_To (Args,
8435 Make_Selected_Component (Loc,
8436 Prefix => Make_Identifier (Loc, Name_uInit),
8437 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
8438 else
8439 Append_To (Args,
8440 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
8441 end if;
8443 -- Optional Stack parameter
8445 if Restricted_Profile then
8447 -- If the stack has been preallocated by the expander then
8448 -- pass its address. Otherwise, pass a null address.
8450 if Preallocated_Stacks_On_Target then
8451 Append_To (Args,
8452 Make_Attribute_Reference (Loc,
8453 Prefix => Make_Selected_Component (Loc,
8454 Prefix => Make_Identifier (Loc, Name_uInit),
8455 Selector_Name =>
8456 Make_Identifier (Loc, Name_uStack)),
8457 Attribute_Name => Name_Address));
8459 else
8460 Append_To (Args,
8461 New_Reference_To (RTE (RE_Null_Address), Loc));
8462 end if;
8463 end if;
8465 -- Size parameter. If no Storage_Size pragma is present, then
8466 -- the size is taken from the taskZ variable for the type, which
8467 -- is either Unspecified_Size, or has been reset by the use of
8468 -- a Storage_Size attribute definition clause. If a pragma is
8469 -- present, then the size is taken from the _Size field of the
8470 -- task value record, which was set from the pragma value.
8472 if Present (Tdef)
8473 and then Has_Storage_Size_Pragma (Tdef)
8474 then
8475 Append_To (Args,
8476 Make_Selected_Component (Loc,
8477 Prefix => Make_Identifier (Loc, Name_uInit),
8478 Selector_Name => Make_Identifier (Loc, Name_uSize)));
8480 else
8481 Append_To (Args,
8482 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
8483 end if;
8485 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
8486 -- Task_Info pragma, in which case we take the value from the pragma.
8488 if Present (Tdef)
8489 and then Has_Task_Info_Pragma (Tdef)
8490 then
8491 Append_To (Args,
8492 Make_Selected_Component (Loc,
8493 Prefix => Make_Identifier (Loc, Name_uInit),
8494 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
8496 else
8497 Append_To (Args,
8498 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
8499 end if;
8501 if not Restricted_Profile then
8503 -- Number of entries. This is an expression of the form:
8505 -- n + _Init.a'Length + _Init.a'B'Length + ...
8507 -- where a,b... are the entry family names for the task definition
8509 Ecount := Build_Entry_Count_Expression (
8510 Ttyp,
8511 Component_Items (Component_List (
8512 Type_Definition (Parent (
8513 Corresponding_Record_Type (Ttyp))))),
8514 Loc);
8515 Append_To (Args, Ecount);
8517 -- Master parameter. This is a reference to the _Master parameter of
8518 -- the initialization procedure, except in the case of the pragma
8519 -- Restrictions (No_Task_Hierarchy) where the value is fixed to 3.
8520 -- See comments in System.Tasking.Initialization.Init_RTS for the
8521 -- value 3.
8523 if Restriction_Active (No_Task_Hierarchy) = False then
8524 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
8525 else
8526 Append_To (Args, Make_Integer_Literal (Loc, 3));
8527 end if;
8528 end if;
8530 -- State parameter. This is a pointer to the task body procedure. The
8531 -- required value is obtained by taking the address of the task body
8532 -- procedure and converting it (with an unchecked conversion) to the
8533 -- type required by the task kernel. For further details, see the
8534 -- description of Expand_Task_Body
8536 Append_To (Args,
8537 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
8538 Make_Attribute_Reference (Loc,
8539 Prefix =>
8540 New_Occurrence_Of (Get_Task_Body_Procedure (Ttyp), Loc),
8541 Attribute_Name => Name_Address)));
8543 -- Discriminants parameter. This is just the address of the task
8544 -- value record itself (which contains the discriminant values
8546 Append_To (Args,
8547 Make_Attribute_Reference (Loc,
8548 Prefix => Make_Identifier (Loc, Name_uInit),
8549 Attribute_Name => Name_Address));
8551 -- Elaborated parameter. This is an access to the elaboration Boolean
8553 Append_To (Args,
8554 Make_Attribute_Reference (Loc,
8555 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
8556 Attribute_Name => Name_Unchecked_Access));
8558 -- Chain parameter. This is a reference to the _Chain parameter of
8559 -- the initialization procedure.
8561 Append_To (Args, Make_Identifier (Loc, Name_uChain));
8563 -- Task name parameter. Take this from the _Task_Id parameter to the
8564 -- init call unless there is a Task_Name pragma, in which case we take
8565 -- the value from the pragma.
8567 if Present (Tdef)
8568 and then Has_Task_Name_Pragma (Tdef)
8569 then
8570 Append_To (Args,
8571 New_Copy (
8572 Expression (First (
8573 Pragma_Argument_Associations (
8574 Find_Task_Or_Protected_Pragma
8575 (Tdef, Name_Task_Name))))));
8577 else
8578 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
8579 end if;
8581 -- Created_Task parameter. This is the _Task_Id field of the task
8582 -- record value
8584 Append_To (Args,
8585 Make_Selected_Component (Loc,
8586 Prefix => Make_Identifier (Loc, Name_uInit),
8587 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
8589 if Restricted_Profile then
8590 Name := New_Reference_To (RTE (RE_Create_Restricted_Task), Loc);
8591 else
8592 Name := New_Reference_To (RTE (RE_Create_Task), Loc);
8593 end if;
8595 return Make_Procedure_Call_Statement (Loc,
8596 Name => Name, Parameter_Associations => Args);
8597 end Make_Task_Create_Call;
8599 ------------------------------
8600 -- Next_Protected_Operation --
8601 ------------------------------
8603 function Next_Protected_Operation (N : Node_Id) return Node_Id is
8604 Next_Op : Node_Id;
8606 begin
8607 Next_Op := Next (N);
8609 while Present (Next_Op)
8610 and then Nkind (Next_Op) /= N_Subprogram_Body
8611 and then Nkind (Next_Op) /= N_Entry_Body
8612 loop
8613 Next (Next_Op);
8614 end loop;
8616 return Next_Op;
8617 end Next_Protected_Operation;
8619 ----------------------
8620 -- Set_Discriminals --
8621 ----------------------
8623 procedure Set_Discriminals (Dec : Node_Id) is
8624 D : Entity_Id;
8625 Pdef : Entity_Id;
8626 D_Minal : Entity_Id;
8628 begin
8629 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
8630 Pdef := Defining_Identifier (Dec);
8632 if Has_Discriminants (Pdef) then
8633 D := First_Discriminant (Pdef);
8635 while Present (D) loop
8636 D_Minal :=
8637 Make_Defining_Identifier (Sloc (D),
8638 Chars => New_External_Name (Chars (D), 'D'));
8640 Set_Ekind (D_Minal, E_Constant);
8641 Set_Etype (D_Minal, Etype (D));
8642 Set_Scope (D_Minal, Pdef);
8643 Set_Discriminal (D, D_Minal);
8644 Set_Discriminal_Link (D_Minal, D);
8646 Next_Discriminant (D);
8647 end loop;
8648 end if;
8649 end Set_Discriminals;
8651 -----------------
8652 -- Set_Privals --
8653 -----------------
8655 procedure Set_Privals
8656 (Dec : Node_Id;
8657 Op : Node_Id;
8658 Loc : Source_Ptr)
8660 P_Decl : Node_Id;
8661 P_Id : Entity_Id;
8662 Priv : Entity_Id;
8663 Def : Node_Id;
8664 Body_Ent : Entity_Id;
8665 Prec_Decl : constant Node_Id :=
8666 Parent (Corresponding_Record_Type
8667 (Defining_Identifier (Dec)));
8668 Prec_Def : constant Entity_Id := Type_Definition (Prec_Decl);
8669 Obj_Decl : Node_Id;
8670 P_Subtype : Entity_Id;
8671 Assoc_L : constant Elist_Id := New_Elmt_List;
8672 Op_Id : Entity_Id;
8674 begin
8675 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
8676 pragma Assert
8677 (Nkind (Op) = N_Subprogram_Body or else Nkind (Op) = N_Entry_Body);
8679 Def := Protected_Definition (Dec);
8681 if Present (Private_Declarations (Def)) then
8683 P_Decl := First (Private_Declarations (Def));
8685 while Present (P_Decl) loop
8686 if Nkind (P_Decl) = N_Component_Declaration then
8687 P_Id := Defining_Identifier (P_Decl);
8688 Priv :=
8689 Make_Defining_Identifier (Loc,
8690 New_External_Name (Chars (P_Id), 'P'));
8692 Set_Ekind (Priv, E_Variable);
8693 Set_Etype (Priv, Etype (P_Id));
8694 Set_Scope (Priv, Scope (P_Id));
8695 Set_Esize (Priv, Esize (Etype (P_Id)));
8696 Set_Alignment (Priv, Alignment (Etype (P_Id)));
8698 -- If the type of the component is an itype, we must
8699 -- create a new itype for the corresponding prival in
8700 -- each protected operation, to avoid scoping problems.
8701 -- We create new itypes by copying the tree for the
8702 -- component definition.
8704 if Is_Itype (Etype (P_Id)) then
8705 Append_Elmt (P_Id, Assoc_L);
8706 Append_Elmt (Priv, Assoc_L);
8708 if Nkind (Op) = N_Entry_Body then
8709 Op_Id := Defining_Identifier (Op);
8710 else
8711 Op_Id := Defining_Unit_Name (Specification (Op));
8712 end if;
8714 Discard_Node
8715 (New_Copy_Tree (P_Decl, Assoc_L, New_Scope => Op_Id));
8716 end if;
8718 Set_Protected_Operation (P_Id, Op);
8719 Set_Prival (P_Id, Priv);
8720 end if;
8722 Next (P_Decl);
8723 end loop;
8724 end if;
8726 -- There is one more implicit private declaration: the object
8727 -- itself. A "prival" for this is attached to the protected
8728 -- body defining identifier.
8730 Body_Ent := Corresponding_Body (Dec);
8732 Priv :=
8733 Make_Defining_Identifier (Sloc (Body_Ent),
8734 Chars => New_External_Name (Chars (Body_Ent), 'R'));
8736 -- Set the Etype to the implicit subtype of Protection created when
8737 -- the protected type declaration was expanded. This node will not
8738 -- be analyzed until it is used as the defining identifier for the
8739 -- renaming declaration in the protected operation body, and it will
8740 -- be needed in the references expanded before that body is expanded.
8741 -- Since the Protection field is aliased, set Is_Aliased as well.
8743 Obj_Decl := First (Component_Items (Component_List (Prec_Def)));
8744 while Chars (Defining_Identifier (Obj_Decl)) /= Name_uObject loop
8745 Next (Obj_Decl);
8746 end loop;
8748 P_Subtype := Etype (Defining_Identifier (Obj_Decl));
8749 Set_Ekind (Priv, E_Variable);
8750 Set_Etype (Priv, P_Subtype);
8751 Set_Is_Aliased (Priv);
8752 Set_Object_Ref (Body_Ent, Priv);
8753 end Set_Privals;
8755 ----------------------------
8756 -- Update_Prival_Subtypes --
8757 ----------------------------
8759 procedure Update_Prival_Subtypes (N : Node_Id) is
8761 function Process (N : Node_Id) return Traverse_Result;
8762 -- Update the etype of occurrences of privals whose etype does not
8763 -- match the current Etype of the prival entity itself.
8765 procedure Update_Array_Bounds (E : Entity_Id);
8766 -- Itypes generated for array expressions may depend on the
8767 -- determinants of the protected object, and need to be processed
8768 -- separately because they are not attached to the tree.
8770 procedure Update_Index_Types (N : Node_Id);
8771 -- Similarly, update the types of expressions in indexed components
8772 -- which may depend on other discriminants.
8774 -------------
8775 -- Process --
8776 -------------
8778 function Process (N : Node_Id) return Traverse_Result is
8779 begin
8780 if Is_Entity_Name (N) then
8781 declare
8782 E : constant Entity_Id := Entity (N);
8784 begin
8785 if Present (E)
8786 and then (Ekind (E) = E_Constant
8787 or else Ekind (E) = E_Variable)
8788 and then Nkind (Parent (E)) = N_Object_Renaming_Declaration
8789 and then not Is_Scalar_Type (Etype (E))
8790 and then Etype (N) /= Etype (E)
8791 then
8792 Set_Etype (N, Etype (Entity (Original_Node (N))));
8793 Update_Index_Types (N);
8795 elsif Present (E)
8796 and then Ekind (E) = E_Constant
8797 and then Present (Discriminal_Link (E))
8798 then
8799 Set_Etype (N, Etype (E));
8800 end if;
8801 end;
8803 return OK;
8805 elsif Nkind (N) = N_Defining_Identifier
8806 or else Nkind (N) = N_Defining_Operator_Symbol
8807 or else Nkind (N) = N_Defining_Character_Literal
8808 then
8809 return Skip;
8811 elsif Nkind (N) = N_String_Literal then
8813 -- Array type, but bounds are constant
8815 return OK;
8817 elsif Nkind (N) = N_Object_Declaration
8818 and then Is_Itype (Etype (Defining_Identifier (N)))
8819 and then Is_Array_Type (Etype (Defining_Identifier (N)))
8820 then
8821 Update_Array_Bounds (Etype (Defining_Identifier (N)));
8822 return OK;
8824 -- For array components of discriminated records, use the
8825 -- base type directly, because it may depend indirectly
8826 -- on the discriminants of the protected type. Cleaner would
8827 -- be a systematic mechanism to compute actual subtypes of
8828 -- private components ???
8830 elsif Nkind (N) in N_Has_Etype
8831 and then Present (Etype (N))
8832 and then Is_Array_Type (Etype (N))
8833 and then Nkind (N) = N_Selected_Component
8834 and then Has_Discriminants (Etype (Prefix (N)))
8835 then
8836 Set_Etype (N, Base_Type (Etype (N)));
8837 Update_Index_Types (N);
8838 return OK;
8840 else
8841 if Nkind (N) in N_Has_Etype
8842 and then Present (Etype (N))
8843 and then Is_Itype (Etype (N)) then
8845 if Is_Array_Type (Etype (N)) then
8846 Update_Array_Bounds (Etype (N));
8848 elsif Is_Scalar_Type (Etype (N)) then
8849 Update_Prival_Subtypes (Type_Low_Bound (Etype (N)));
8850 Update_Prival_Subtypes (Type_High_Bound (Etype (N)));
8851 end if;
8852 end if;
8854 return OK;
8855 end if;
8856 end Process;
8858 -------------------------
8859 -- Update_Array_Bounds --
8860 -------------------------
8862 procedure Update_Array_Bounds (E : Entity_Id) is
8863 Ind : Node_Id;
8865 begin
8866 Ind := First_Index (E);
8868 while Present (Ind) loop
8869 Update_Prival_Subtypes (Type_Low_Bound (Etype (Ind)));
8870 Update_Prival_Subtypes (Type_High_Bound (Etype (Ind)));
8871 Next_Index (Ind);
8872 end loop;
8873 end Update_Array_Bounds;
8875 ------------------------
8876 -- Update_Index_Types --
8877 ------------------------
8879 procedure Update_Index_Types (N : Node_Id) is
8880 Indx1 : Node_Id;
8881 I_Typ : Node_Id;
8882 begin
8883 -- If the prefix has an actual subtype that is different
8884 -- from the nominal one, update the types of the indices,
8885 -- so that the proper constraints are applied. Do not
8886 -- apply this transformation to a packed array, where the
8887 -- index type is computed for a byte array and is different
8888 -- from the source index.
8890 if Nkind (Parent (N)) = N_Indexed_Component
8891 and then
8892 not Is_Bit_Packed_Array (Etype (Prefix (Parent (N))))
8893 then
8894 Indx1 := First (Expressions (Parent (N)));
8895 I_Typ := First_Index (Etype (N));
8897 while Present (Indx1) and then Present (I_Typ) loop
8899 if not Is_Entity_Name (Indx1) then
8900 Set_Etype (Indx1, Base_Type (Etype (I_Typ)));
8901 end if;
8903 Next (Indx1);
8904 Next_Index (I_Typ);
8905 end loop;
8906 end if;
8907 end Update_Index_Types;
8909 procedure Traverse is new Traverse_Proc;
8911 -- Start of processing for Update_Prival_Subtypes
8913 begin
8914 Traverse (N);
8915 end Update_Prival_Subtypes;
8917 end Exp_Ch9;