1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with 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;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
49 with Sem_Ch6; use 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
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
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
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
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
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
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_Wrapper_Body
136 Proc_Nam : Entity_Id;
138 Formals : List_Id) return Node_Id;
139 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
140 -- associated with a protected or task type. This is required to implement
141 -- dispatching calls through interfaces. Proc_Nam is the entry name to be
142 -- wrapped, Obj_Typ is the type of the newly added formal parameter to
143 -- handle object notation, Formals are the original entry formals that will
144 -- be explicitly replicated.
146 function Build_Wrapper_Spec
148 Proc_Nam : Entity_Id;
150 Formals : List_Id) return Node_Id;
151 -- Ada 2005 (AI-345): Build the specification of a primitive operation
152 -- associated with a protected or task type. This is required implement
153 -- dispatching calls through interfaces. Proc_Nam is the entry name to be
154 -- wrapped, Obj_Typ is the type of the newly added formal parameter to
155 -- handle object notation, Formals are the original entry formals that will
156 -- be explicitly replicated.
158 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
159 -- Build the function that translates the entry index in the call
160 -- (which depends on the size of entry families) into an index into the
161 -- Entry_Bodies_Array, to determine the body and barrier function used
162 -- in a protected entry call. A pointer to this function appears in every
165 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
166 -- Build subprogram declaration for previous one
168 function Build_Protected_Entry
171 Pid : Node_Id) return Node_Id;
172 -- Build the procedure implementing the statement sequence of
173 -- the specified entry body.
175 function Build_Protected_Entry_Specification
178 Loc : Source_Ptr) return Node_Id;
179 -- Build a specification for a procedure implementing
180 -- the statement sequence of the specified entry body.
181 -- Add attributes associating it with the entry defining identifier
184 function Build_Protected_Subprogram_Body
187 N_Op_Spec : Node_Id) return Node_Id;
188 -- This function is used to construct the protected version of a protected
189 -- subprogram. Its statement sequence first defers abort, then locks
190 -- the associated protected object, and then enters a block that contains
191 -- a call to the unprotected version of the subprogram (for details, see
192 -- Build_Unprotected_Subprogram_Body). This block statement requires
193 -- a cleanup handler that unlocks the object in all cases.
194 -- (see Exp_Ch7.Expand_Cleanup_Actions).
196 function Build_Protected_Spec
198 Obj_Type : Entity_Id;
199 Unprotected : Boolean := False;
200 Ident : Entity_Id) return List_Id;
201 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
202 -- Subprogram_Type. Builds signature of protected subprogram, adding the
203 -- formal that corresponds to the object itself. For an access to protected
204 -- subprogram, there is no object type to specify, so the additional
205 -- parameter has type Address and mode In. An indirect call through such
206 -- a pointer converts the address to a reference to the actual object.
207 -- The object is a limited record and therefore a by_reference type.
209 function Build_Selected_Name
210 (Prefix, Selector : Name_Id;
211 Append_Char : Character := ' ') return Name_Id;
212 -- Build a name in the form of Prefix__Selector, with an optional
213 -- character appended. This is used for internal subprograms generated
214 -- for operations of protected types, including barrier functions.
215 -- For the subprograms generated for entry bodies and entry barriers,
216 -- the generated name includes a sequence number that makes names
217 -- unique in the presence of entry overloading. This is necessary
218 -- because entry body procedures and barrier functions all have the
221 procedure Build_Simple_Entry_Call
226 -- Some comments here would be useful ???
228 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
229 -- This routine constructs a specification for the procedure that we will
230 -- build for the task body for task type T. The spec has the form:
232 -- procedure tnameB (_Task : access tnameV);
234 -- where name is the character name taken from the task type entity that
235 -- is passed as the argument to the procedure, and tnameV is the task
236 -- value type that is associated with the task type.
238 function Build_Unprotected_Subprogram_Body
240 Pid : Node_Id) return Node_Id;
241 -- This routine constructs the unprotected version of a protected
242 -- subprogram body, which is contains all of the code in the
243 -- original, unexpanded body. This is the version of the protected
244 -- subprogram that is called from all protected operations on the same
245 -- object, including the protected version of the same subprogram.
247 procedure Collect_Entry_Families
250 Current_Node : in out Node_Id;
251 Conctyp : Entity_Id);
252 -- For each entry family in a concurrent type, create an anonymous array
253 -- type of the right size, and add a component to the corresponding_record.
255 function Family_Offset
259 Ttyp : Entity_Id) return Node_Id;
260 -- Compute (Hi - Lo) for two entry family indices. Hi is the index in
261 -- an accept statement, or the upper bound in the discrete subtype of
262 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
263 -- the concurrent type of the entry.
269 Ttyp : Entity_Id) return Node_Id;
270 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
271 -- a family, and handle properly the superflat case. This is equivalent
272 -- to the use of 'Length on the index type, but must use Family_Offset
273 -- to handle properly the case of bounds that depend on discriminants.
275 procedure Extract_Entry
277 Concval : out Node_Id;
279 Index : out Node_Id);
280 -- Given an entry call, returns the associated concurrent object,
281 -- the entry name, and the entry family index.
283 function Find_Task_Or_Protected_Pragma
285 P : Name_Id) return Node_Id;
286 -- Searches the task or protected definition T for the first occurrence
287 -- of the pragma whose name is given by P. The caller has ensured that
288 -- the pragma is present in the task definition. A special case is that
289 -- when P is Name_uPriority, the call will also find Interrupt_Priority.
290 -- ??? Should be implemented with the rep item chain mechanism.
292 procedure Update_Prival_Subtypes (N : Node_Id);
293 -- The actual subtypes of the privals will differ from the type of the
294 -- private declaration in the original protected type, if the protected
295 -- type has discriminants or if the prival has constrained components.
296 -- This is because the privals are generated out of sequence w.r.t. the
297 -- analysis of a protected body. After generating the bodies for protected
298 -- operations, we set correctly the type of all references to privals, by
299 -- means of a recursive tree traversal, which is heavy-handed but
302 -----------------------------
303 -- Actual_Index_Expression --
304 -----------------------------
306 function Actual_Index_Expression
310 Tsk : Entity_Id) return Node_Id
312 Ttyp : constant Entity_Id := Etype (Tsk);
320 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
321 -- Compute difference between bounds of entry family
323 --------------------------
324 -- Actual_Family_Offset --
325 --------------------------
327 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
329 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
330 -- Replace a reference to a discriminant with a selected component
331 -- denoting the discriminant of the target task.
333 -----------------------------
334 -- Actual_Discriminant_Ref --
335 -----------------------------
337 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
338 Typ : constant Entity_Id := Etype (Bound);
342 if not Is_Entity_Name (Bound)
343 or else Ekind (Entity (Bound)) /= E_Discriminant
345 if Nkind (Bound) = N_Attribute_Reference then
348 B := New_Copy_Tree (Bound);
353 Make_Selected_Component (Sloc,
354 Prefix => New_Copy_Tree (Tsk),
355 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
357 Analyze_And_Resolve (B, Typ);
361 Make_Attribute_Reference (Sloc,
362 Attribute_Name => Name_Pos,
363 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
364 Expressions => New_List (B));
365 end Actual_Discriminant_Ref;
367 -- Start of processing for Actual_Family_Offset
371 Make_Op_Subtract (Sloc,
372 Left_Opnd => Actual_Discriminant_Ref (Hi),
373 Right_Opnd => Actual_Discriminant_Ref (Lo));
374 end Actual_Family_Offset;
376 -- Start of processing for Actual_Index_Expression
379 -- The queues of entries and entry families appear in textual
380 -- order in the associated record. The entry index is computed as
381 -- the sum of the number of queues for all entries that precede the
382 -- designated one, to which is added the index expression, if this
383 -- expression denotes a member of a family.
385 -- The following is a place holder for the count of simple entries
387 Num := Make_Integer_Literal (Sloc, 1);
389 -- We construct an expression which is a series of addition
390 -- operations. See comments in Entry_Index_Expression, which is
391 -- identical in structure.
393 if Present (Index) then
394 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
401 Actual_Family_Offset (
402 Make_Attribute_Reference (Sloc,
403 Attribute_Name => Name_Pos,
404 Prefix => New_Reference_To (Base_Type (S), Sloc),
405 Expressions => New_List (Relocate_Node (Index))),
406 Type_Low_Bound (S)));
411 -- Now add lengths of preceding entries and entry families
413 Prev := First_Entity (Ttyp);
415 while Chars (Prev) /= Chars (Ent)
416 or else (Ekind (Prev) /= Ekind (Ent))
417 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
419 if Ekind (Prev) = E_Entry then
420 Set_Intval (Num, Intval (Num) + 1);
422 elsif Ekind (Prev) = E_Entry_Family then
424 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
425 Lo := Type_Low_Bound (S);
426 Hi := Type_High_Bound (S);
434 Actual_Family_Offset (Hi, Lo),
436 Make_Integer_Literal (Sloc, 1)));
438 -- Other components are anonymous types to be ignored
448 end Actual_Index_Expression;
450 ----------------------------------
451 -- Add_Discriminal_Declarations --
452 ----------------------------------
454 procedure Add_Discriminal_Declarations
463 if Has_Discriminants (Typ) then
464 D := First_Discriminant (Typ);
466 while Present (D) loop
469 Make_Object_Renaming_Declaration (Loc,
470 Defining_Identifier => Discriminal (D),
471 Subtype_Mark => New_Reference_To (Etype (D), Loc),
473 Make_Selected_Component (Loc,
474 Prefix => Make_Identifier (Loc, Name),
475 Selector_Name => Make_Identifier (Loc, Chars (D)))));
477 Next_Discriminant (D);
480 end Add_Discriminal_Declarations;
482 ------------------------
483 -- Add_Object_Pointer --
484 ------------------------
486 procedure Add_Object_Pointer
494 -- Prepend the declaration of _object. This must be first in the
495 -- declaration list, since it is used by the discriminal and
496 -- prival declarations.
497 -- ??? An attempt to make this a renaming was unsuccessful.
499 -- type poVP is access poV;
500 -- _object : poVP := poVP!O;
503 Make_Defining_Identifier (Loc,
506 (Chars (Corresponding_Record_Type (Pid)), 'P'));
509 Make_Object_Declaration (Loc,
510 Defining_Identifier =>
511 Make_Defining_Identifier (Loc, Name_uObject),
512 Object_Definition => New_Reference_To (Obj_Ptr, Loc),
514 Unchecked_Convert_To (Obj_Ptr,
515 Make_Identifier (Loc, Name_uO))));
518 Make_Full_Type_Declaration (Loc,
519 Defining_Identifier => Obj_Ptr,
520 Type_Definition => Make_Access_To_Object_Definition (Loc,
521 Subtype_Indication =>
522 New_Reference_To (Corresponding_Record_Type (Pid), Loc))));
523 end Add_Object_Pointer;
525 ------------------------------
526 -- Add_Private_Declarations --
527 ------------------------------
529 procedure Add_Private_Declarations
535 Def : constant Node_Id := Protected_Definition (Parent (Typ));
536 Body_Ent : constant Entity_Id := Corresponding_Body (Parent (Typ));
541 pragma Assert (Nkind (Def) = N_Protected_Definition);
543 if Present (Private_Declarations (Def)) then
544 P := First (Private_Declarations (Def));
546 while Present (P) loop
547 if Nkind (P) = N_Component_Declaration then
548 Pdef := Defining_Identifier (P);
550 Make_Object_Renaming_Declaration (Loc,
551 Defining_Identifier => Prival (Pdef),
552 Subtype_Mark => New_Reference_To (Etype (Pdef), Loc),
554 Make_Selected_Component (Loc,
555 Prefix => Make_Identifier (Loc, Name),
556 Selector_Name => Make_Identifier (Loc, Chars (Pdef)))));
562 -- One more "prival" for the object itself, with the right protection
566 Protection_Type : RE_Id;
568 if Has_Attach_Handler (Typ) then
569 if Restricted_Profile then
570 if Has_Entries (Typ) then
571 Protection_Type := RE_Protection_Entry;
573 Protection_Type := RE_Protection;
576 Protection_Type := RE_Static_Interrupt_Protection;
579 elsif Has_Interrupt_Handler (Typ) then
580 Protection_Type := RE_Dynamic_Interrupt_Protection;
582 elsif Has_Entries (Typ) then
584 or else Restriction_Active (No_Entry_Queue) = False
585 or else Number_Entries (Typ) > 1
587 Protection_Type := RE_Protection_Entries;
589 Protection_Type := RE_Protection_Entry;
593 Protection_Type := RE_Protection;
597 Make_Object_Renaming_Declaration (Loc,
598 Defining_Identifier => Object_Ref (Body_Ent),
599 Subtype_Mark => New_Reference_To (RTE (Protection_Type), Loc),
601 Make_Selected_Component (Loc,
602 Prefix => Make_Identifier (Loc, Name),
603 Selector_Name => Make_Identifier (Loc, Name_uObject))));
605 end Add_Private_Declarations;
607 -----------------------
608 -- Build_Accept_Body --
609 -----------------------
611 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
612 Loc : constant Source_Ptr := Sloc (Astat);
613 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
620 -- At the end of the statement sequence, Complete_Rendezvous is called.
621 -- A label skipping the Complete_Rendezvous, and all other
622 -- accept processing, has already been added for the expansion
623 -- of requeue statements.
625 Call := Build_Runtime_Call (Loc, RE_Complete_Rendezvous);
626 Insert_Before (Last (Statements (Stats)), Call);
629 -- If exception handlers are present, then append Complete_Rendezvous
630 -- calls to the handlers, and construct the required outer block.
632 if Present (Exception_Handlers (Stats)) then
633 Hand := First (Exception_Handlers (Stats));
635 while Present (Hand) loop
636 Call := Build_Runtime_Call (Loc, RE_Complete_Rendezvous);
637 Append (Call, Statements (Hand));
643 Make_Handled_Sequence_Of_Statements (Loc,
644 Statements => New_List (
645 Make_Block_Statement (Loc,
646 Handled_Statement_Sequence => Stats)));
652 -- At this stage we know that the new statement sequence does not
653 -- have an exception handler part, so we supply one to call
654 -- Exceptional_Complete_Rendezvous. This handler is
656 -- when all others =>
657 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
659 -- We handle Abort_Signal to make sure that we properly catch the abort
660 -- case and wake up the caller.
662 Ohandle := Make_Others_Choice (Loc);
663 Set_All_Others (Ohandle);
665 Set_Exception_Handlers (New_S,
667 Make_Exception_Handler (Loc,
668 Exception_Choices => New_List (Ohandle),
670 Statements => New_List (
671 Make_Procedure_Call_Statement (Loc,
672 Name => New_Reference_To (
673 RTE (RE_Exceptional_Complete_Rendezvous), Loc),
674 Parameter_Associations => New_List (
675 Make_Function_Call (Loc,
676 Name => New_Reference_To (
677 RTE (RE_Get_GNAT_Exception), Loc))))))));
679 Set_Parent (New_S, Astat); -- temp parent for Analyze call
680 Analyze_Exception_Handlers (Exception_Handlers (New_S));
681 Expand_Exception_Handlers (New_S);
683 -- Exceptional_Complete_Rendezvous must be called with abort
684 -- still deferred, which is the case for a "when all others" handler.
687 end Build_Accept_Body;
689 -----------------------------------
690 -- Build_Activation_Chain_Entity --
691 -----------------------------------
693 procedure Build_Activation_Chain_Entity (N : Node_Id) is
699 -- Loop to find enclosing construct containing activation chain variable
703 while Nkind (P) /= N_Subprogram_Body
704 and then Nkind (P) /= N_Package_Declaration
705 and then Nkind (P) /= N_Package_Body
706 and then Nkind (P) /= N_Block_Statement
707 and then Nkind (P) /= N_Task_Body
712 -- If we are in a package body, the activation chain variable is
713 -- allocated in the corresponding spec. First, we save the package
714 -- body node because we enter the new entity in its Declarations list.
718 if Nkind (P) = N_Package_Body then
719 P := Unit_Declaration_Node (Corresponding_Spec (P));
720 Decls := Declarations (B);
722 elsif Nkind (P) = N_Package_Declaration then
723 Decls := Visible_Declarations (Specification (B));
726 Decls := Declarations (B);
729 -- If activation chain entity not already declared, declare it
731 if No (Activation_Chain_Entity (P)) then
732 Set_Activation_Chain_Entity
733 (P, Make_Defining_Identifier (Sloc (N), Name_uChain));
736 Make_Object_Declaration (Sloc (P),
737 Defining_Identifier => Activation_Chain_Entity (P),
738 Aliased_Present => True,
740 New_Reference_To (RTE (RE_Activation_Chain), Sloc (P))));
742 Analyze (First (Decls));
744 end Build_Activation_Chain_Entity;
746 ----------------------------
747 -- Build_Barrier_Function --
748 ----------------------------
750 function Build_Barrier_Function
753 Pid : Node_Id) return Node_Id
755 Loc : constant Source_Ptr := Sloc (N);
756 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
757 Index_Spec : constant Node_Id := Entry_Index_Specification
759 Op_Decls : constant List_Id := New_List;
765 Make_Defining_Identifier (Loc, Chars (Barrier_Function (Ent)));
766 Bspec := Build_Barrier_Function_Specification (Bdef, Loc);
768 -- <object pointer declaration>
769 -- <discriminant renamings>
770 -- <private object renamings>
771 -- Add discriminal and private renamings. These names have
772 -- already been used to expand references to discriminants
775 Add_Discriminal_Declarations (Op_Decls, Pid, Name_uObject, Loc);
776 Add_Private_Declarations (Op_Decls, Pid, Name_uObject, Loc);
777 Add_Object_Pointer (Op_Decls, Pid, Loc);
779 -- If this is the barrier for an entry family, the entry index is
780 -- visible in the body of the barrier. Create a local variable that
781 -- converts the entry index (which is the last formal of the barrier
782 -- function) into the appropriate offset into the entry array. The
783 -- entry index constant must be set, as for the entry body, so that
784 -- local references to the entry index are correctly replaced with
785 -- the local variable. This parallels what is done for entry bodies.
787 if Present (Index_Spec) then
789 Index_Id : constant Entity_Id := Defining_Identifier (Index_Spec);
790 Index_Con : constant Entity_Id :=
791 Make_Defining_Identifier (Loc,
792 Chars => New_Internal_Name ('J'));
795 Set_Entry_Index_Constant (Index_Id, Index_Con);
796 Append_List_To (Op_Decls,
797 Index_Constant_Declaration (N, Index_Id, Pid));
801 -- Note: the condition in the barrier function needs to be properly
802 -- processed for the C/Fortran boolean possibility, but this happens
803 -- automatically since the return statement does this normalization.
806 Make_Subprogram_Body (Loc,
807 Specification => Bspec,
808 Declarations => Op_Decls,
809 Handled_Statement_Sequence =>
810 Make_Handled_Sequence_Of_Statements (Loc,
811 Statements => New_List (
812 Make_Return_Statement (Loc,
813 Expression => Condition (Ent_Formals)))));
814 end Build_Barrier_Function;
816 ------------------------------------------
817 -- Build_Barrier_Function_Specification --
818 ------------------------------------------
820 function Build_Barrier_Function_Specification
822 Loc : Source_Ptr) return Node_Id
825 Set_Needs_Debug_Info (Def_Id);
826 return Make_Function_Specification (Loc,
827 Defining_Unit_Name => Def_Id,
828 Parameter_Specifications => New_List (
829 Make_Parameter_Specification (Loc,
830 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
832 New_Reference_To (RTE (RE_Address), Loc)),
834 Make_Parameter_Specification (Loc,
835 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uE),
837 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
839 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc));
840 end Build_Barrier_Function_Specification;
842 --------------------------
843 -- Build_Call_With_Task --
844 --------------------------
846 function Build_Call_With_Task
848 E : Entity_Id) return Node_Id
850 Loc : constant Source_Ptr := Sloc (N);
854 Make_Function_Call (Loc,
855 Name => New_Reference_To (E, Loc),
856 Parameter_Associations => New_List (Concurrent_Ref (N)));
857 end Build_Call_With_Task;
859 --------------------------------
860 -- Build_Corresponding_Record --
861 --------------------------------
863 function Build_Corresponding_Record
866 Loc : Source_Ptr) return Node_Id
868 Rec_Ent : constant Entity_Id :=
869 Make_Defining_Identifier
870 (Loc, New_External_Name (Chars (Ctyp), 'V'));
873 New_Disc : Entity_Id;
877 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
878 Set_Ekind (Rec_Ent, E_Record_Type);
879 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
880 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
881 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
882 Set_Stored_Constraint (Rec_Ent, No_Elist);
885 -- Use discriminals to create list of discriminants for record, and
886 -- create new discriminals for use in default expressions, etc. It is
887 -- worth noting that a task discriminant gives rise to 5 entities;
889 -- a) The original discriminant.
890 -- b) The discriminal for use in the task.
891 -- c) The discriminant of the corresponding record.
892 -- d) The discriminal for the init proc of the corresponding record.
893 -- e) The local variable that renames the discriminant in the procedure
894 -- for the task body.
896 -- In fact the discriminals b) are used in the renaming declarations
897 -- for e). See details in einfo (Handling of Discriminants).
899 if Present (Discriminant_Specifications (N)) then
901 Disc := First_Discriminant (Ctyp);
903 while Present (Disc) loop
904 New_Disc := CR_Discriminant (Disc);
907 Make_Discriminant_Specification (Loc,
908 Defining_Identifier => New_Disc,
910 New_Occurrence_Of (Etype (Disc), Loc),
912 New_Copy (Discriminant_Default_Value (Disc))));
914 Next_Discriminant (Disc);
921 -- Now we can construct the record type declaration. Note that this
922 -- record is "limited tagged". It is "limited" to reflect the underlying
923 -- limitedness of the task or protected object that it represents, and
924 -- ensuring for example that it is properly passed by reference. It is
925 -- "tagged" to give support to dispatching calls through interfaces (Ada
929 Make_Full_Type_Declaration (Loc,
930 Defining_Identifier => Rec_Ent,
931 Discriminant_Specifications => Dlist,
933 Make_Record_Definition (Loc,
935 Make_Component_List (Loc,
936 Component_Items => Cdecls),
937 Tagged_Present => Ada_Version >= Ada_05,
938 Limited_Present => True));
939 end Build_Corresponding_Record;
941 ----------------------------------
942 -- Build_Entry_Count_Expression --
943 ----------------------------------
945 function Build_Entry_Count_Expression
946 (Concurrent_Type : Node_Id;
947 Component_List : List_Id;
948 Loc : Source_Ptr) return Node_Id
959 Ent := First_Entity (Concurrent_Type);
962 -- Count number of non-family entries
964 while Present (Ent) loop
965 if Ekind (Ent) = E_Entry then
972 Ecount := Make_Integer_Literal (Loc, Eindx);
974 -- Loop through entry families building the addition nodes
976 Ent := First_Entity (Concurrent_Type);
977 Comp := First (Component_List);
979 while Present (Ent) loop
980 if Ekind (Ent) = E_Entry_Family then
981 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
985 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
986 Hi := Type_High_Bound (Typ);
987 Lo := Type_Low_Bound (Typ);
992 Right_Opnd => Family_Size (Loc, Hi, Lo, Concurrent_Type));
999 end Build_Entry_Count_Expression;
1001 ------------------------------
1002 -- Build_Wrapper_Body --
1003 ------------------------------
1005 function Build_Wrapper_Body
1007 Proc_Nam : Entity_Id;
1008 Obj_Typ : Entity_Id;
1009 Formals : List_Id) return Node_Id
1011 Actuals : List_Id := No_List;
1012 Body_Spec : Node_Id;
1014 First_Formal : Node_Id;
1018 Body_Spec := Build_Wrapper_Spec (Loc, Proc_Nam, Obj_Typ, Formals);
1020 -- If we did not generate the specification do have nothing else to do
1022 if Body_Spec = Empty then
1026 -- Map formals to actuals. Use the list built for the wrapper spec,
1027 -- skipping the object notation parameter.
1029 First_Formal := First (Parameter_Specifications (Body_Spec));
1031 Formal := First_Formal;
1034 if Present (Formal) then
1035 Actuals := New_List;
1037 while Present (Formal) loop
1039 Make_Identifier (Loc, Chars =>
1040 Chars (Defining_Identifier (Formal))));
1046 -- An access-to-variable first parameter will require an explicit
1047 -- dereference in the unchecked conversion. This case occurs when
1048 -- a protected entry wrapper must override an interface-level
1049 -- procedure with interface access as first parameter.
1051 -- SubprgName (O.all).Proc_Nam (Formal_1 .. Formal_N)
1053 if Nkind (Parameter_Type (First_Formal)) = N_Access_Definition then
1055 Make_Explicit_Dereference (Loc,
1057 Make_Identifier (Loc, Chars => Name_uO));
1060 Make_Identifier (Loc, Chars => Name_uO);
1063 if Ekind (Proc_Nam) = E_Function then
1065 Make_Subprogram_Body (Loc,
1066 Specification => Body_Spec,
1067 Declarations => Empty_List,
1068 Handled_Statement_Sequence =>
1069 Make_Handled_Sequence_Of_Statements (Loc,
1072 Make_Return_Statement (Loc,
1073 Make_Function_Call (Loc,
1075 Make_Selected_Component (Loc,
1077 Unchecked_Convert_To (
1078 Corresponding_Concurrent_Type (Obj_Typ),
1081 New_Reference_To (Proc_Nam, Loc)),
1082 Parameter_Associations => Actuals)))));
1085 Make_Subprogram_Body (Loc,
1086 Specification => Body_Spec,
1087 Declarations => Empty_List,
1088 Handled_Statement_Sequence =>
1089 Make_Handled_Sequence_Of_Statements (Loc,
1092 Make_Procedure_Call_Statement (Loc,
1094 Make_Selected_Component (Loc,
1096 Unchecked_Convert_To (
1097 Corresponding_Concurrent_Type (Obj_Typ),
1100 New_Reference_To (Proc_Nam, Loc)),
1101 Parameter_Associations => Actuals))));
1103 end Build_Wrapper_Body;
1105 ------------------------
1106 -- Build_Wrapper_Spec --
1107 ------------------------
1109 function Build_Wrapper_Spec
1111 Proc_Nam : Entity_Id;
1112 Obj_Typ : Entity_Id;
1113 Formals : List_Id) return Node_Id
1115 New_Name_Id : constant Entity_Id :=
1116 Make_Defining_Identifier (Loc, Chars (Proc_Nam));
1118 First_Param : Node_Id := Empty;
1120 Iface_Elmt : Elmt_Id := No_Elmt;
1121 New_Formals : List_Id;
1122 Obj_Param : Node_Id;
1123 Obj_Param_Typ : Node_Id;
1124 Iface_Prim_Op : Entity_Id;
1125 Iface_Prim_Op_Elmt : Elmt_Id;
1127 function Overriding_Possible
1128 (Iface_Prim_Op : Entity_Id;
1129 Proc_Nam : Entity_Id) return Boolean;
1130 -- Determine whether a primitive operation can be overriden by the
1131 -- wrapper. Iface_Prim_Op is the candidate primitive operation of an
1132 -- abstract interface type, Proc_Nam is the generated entry wrapper.
1134 function Replicate_Entry_Formals
1136 Formals : List_Id) return List_Id;
1137 -- An explicit parameter replication is required due to the
1138 -- Is_Entry_Formal flag being set for all the formals. The explicit
1139 -- replication removes the flag that would otherwise cause a different
1140 -- path of analysis.
1142 -------------------------
1143 -- Overriding_Possible --
1144 -------------------------
1146 function Overriding_Possible
1147 (Iface_Prim_Op : Entity_Id;
1148 Proc_Nam : Entity_Id) return Boolean
1150 Prim_Op_Spec : constant Node_Id := Parent (Iface_Prim_Op);
1151 Proc_Spec : constant Node_Id := Parent (Proc_Nam);
1153 Is_Access_To_Variable : Boolean;
1154 Is_Out_Present : Boolean;
1156 function Type_Conformant_Parameters
1157 (Prim_Op_Param_Specs : List_Id;
1158 Proc_Param_Specs : List_Id) return Boolean;
1159 -- Determine whether the parameters of the generated entry wrapper
1160 -- and those of a primitive operation are type conformant. During
1161 -- this check, the first parameter of the primitive operation is
1164 --------------------------------
1165 -- Type_Conformant_Parameters --
1166 --------------------------------
1168 function Type_Conformant_Parameters
1169 (Prim_Op_Param_Specs : List_Id;
1170 Proc_Param_Specs : List_Id) return Boolean
1172 Prim_Op_Param : Node_Id;
1173 Proc_Param : Node_Id;
1176 -- Skip the first parameter of the primitive operation
1178 Prim_Op_Param := Next (First (Prim_Op_Param_Specs));
1179 Proc_Param := First (Proc_Param_Specs);
1180 while Present (Prim_Op_Param)
1181 and then Present (Proc_Param)
1183 -- The two parameters must be mode conformant and have
1184 -- the exact same types.
1186 if Out_Present (Prim_Op_Param) /= Out_Present (Proc_Param)
1187 or else In_Present (Prim_Op_Param) /= In_Present (Proc_Param)
1188 or else Etype (Parameter_Type (Prim_Op_Param)) /=
1189 Etype (Parameter_Type (Proc_Param))
1194 Next (Prim_Op_Param);
1198 -- One of the lists is longer than the other
1200 if Present (Prim_Op_Param) or else Present (Proc_Param) then
1205 end Type_Conformant_Parameters;
1207 -- Start of processing for Overriding_Possible
1210 if Chars (Iface_Prim_Op) /= Chars (Proc_Nam) then
1214 -- Special check for protected procedures: If an inherited subprogram
1215 -- is implemented by a protected procedure or an entry, then the
1216 -- first parameter of the inherited subprogram shall be of mode OUT
1217 -- or IN OUT, or an access-to-variable parameter.
1219 if Ekind (Iface_Prim_Op) = E_Procedure then
1222 Present (Parameter_Specifications (Prim_Op_Spec))
1224 Out_Present (First (Parameter_Specifications (Prim_Op_Spec)));
1226 Is_Access_To_Variable :=
1227 Present (Parameter_Specifications (Prim_Op_Spec))
1229 Nkind (Parameter_Type
1231 (Parameter_Specifications (Prim_Op_Spec))))
1232 = N_Access_Definition;
1234 if not Is_Out_Present
1235 and then not Is_Access_To_Variable
1241 return Type_Conformant_Parameters (
1242 Parameter_Specifications (Prim_Op_Spec),
1243 Parameter_Specifications (Proc_Spec));
1245 end Overriding_Possible;
1247 -----------------------------
1248 -- Replicate_Entry_Formals --
1249 -----------------------------
1251 function Replicate_Entry_Formals
1253 Formals : List_Id) return List_Id
1255 New_Formals : constant List_Id := New_List;
1259 Formal := First (Formals);
1261 if Present (Formal) then
1262 while Present (Formal) loop
1264 -- Create an explicit copy of the entry parameter
1266 Append_To (New_Formals,
1267 Make_Parameter_Specification (Loc,
1268 Defining_Identifier =>
1269 Make_Defining_Identifier (Loc,
1270 Chars => Chars (Defining_Identifier (Formal))),
1271 In_Present => In_Present (Formal),
1272 Out_Present => Out_Present (Formal),
1273 Parameter_Type => New_Reference_To (Etype (
1274 Parameter_Type (Formal)), Loc)));
1281 end Replicate_Entry_Formals;
1283 -- Start of processing for Build_Wrapper_Spec
1286 -- The mode is determined by the first parameter of the interface-level
1287 -- procedure that the current entry is trying to override.
1289 pragma Assert (Present (Abstract_Interfaces
1290 (Corresponding_Record_Type (Scope (Proc_Nam)))));
1293 First_Elmt (Abstract_Interfaces
1294 (Corresponding_Record_Type (Scope (Proc_Nam))));
1296 -- We must examine all the protected operations of the implemented
1297 -- interfaces in order to discover a possible overriding candidate.
1299 Examine_Interfaces : while Present (Iface_Elmt) loop
1300 Iface := Node (Iface_Elmt);
1302 if Present (Primitive_Operations (Iface)) then
1303 Iface_Prim_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
1305 while Present (Iface_Prim_Op_Elmt) loop
1306 Iface_Prim_Op := Node (Iface_Prim_Op_Elmt);
1308 -- The current primitive operation can be overriden by the
1309 -- generated entry wrapper.
1311 if Overriding_Possible (Iface_Prim_Op, Proc_Nam) then
1313 First (Parameter_Specifications (Parent (Iface_Prim_Op)));
1315 exit Examine_Interfaces;
1318 Next_Elmt (Iface_Prim_Op_Elmt);
1322 Next_Elmt (Iface_Elmt);
1323 end loop Examine_Interfaces;
1325 -- Return if no interface primitive can be overriden
1327 if not Present (First_Param) then
1331 New_Formals := Replicate_Entry_Formals (Loc, Formals);
1333 -- ??? Certain source packages contain protected or task types that do
1334 -- not implement any interfaces and are compiled with the -gnat05
1335 -- switch. In this case, a default first parameter is created.
1337 if Present (First_Param) then
1338 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
1340 Make_Access_Definition (Loc,
1342 New_Reference_To (Obj_Typ, Loc));
1344 Obj_Param_Typ := New_Reference_To (Obj_Typ, Loc);
1348 Make_Parameter_Specification (Loc,
1349 Defining_Identifier =>
1350 Make_Defining_Identifier (Loc, Name_uO),
1351 In_Present => In_Present (First_Param),
1352 Out_Present => Out_Present (First_Param),
1353 Parameter_Type => Obj_Param_Typ);
1357 Make_Parameter_Specification (Loc,
1358 Defining_Identifier =>
1359 Make_Defining_Identifier (Loc, Name_uO),
1361 Out_Present => True,
1362 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
1365 Prepend_To (New_Formals, Obj_Param);
1367 -- Minimum decoration needed to catch the entity in
1368 -- Sem_Ch6.Override_Dispatching_Operation
1370 if Ekind (Proc_Nam) = E_Procedure
1371 or else Ekind (Proc_Nam) = E_Entry
1373 Set_Ekind (New_Name_Id, E_Procedure);
1375 Make_Procedure_Specification (Loc,
1376 Defining_Unit_Name => New_Name_Id,
1377 Parameter_Specifications => New_Formals);
1379 else pragma Assert (Ekind (Proc_Nam) = E_Function);
1380 Set_Ekind (New_Name_Id, E_Function);
1382 Make_Function_Specification (Loc,
1383 Defining_Unit_Name => New_Name_Id,
1384 Parameter_Specifications => New_Formals,
1385 Subtype_Mark => New_Copy (Subtype_Mark (Parent (Proc_Nam))));
1387 end Build_Wrapper_Spec;
1389 ---------------------------
1390 -- Build_Find_Body_Index --
1391 ---------------------------
1393 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
1394 Loc : constant Source_Ptr := Sloc (Typ);
1397 Has_F : Boolean := False;
1399 If_St : Node_Id := Empty;
1402 Decls : List_Id := New_List;
1405 Siz : Node_Id := Empty;
1407 procedure Add_If_Clause (Expr : Node_Id);
1408 -- Add test for range of current entry
1410 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
1411 -- If a bound of an entry is given by a discriminant, retrieve the
1412 -- actual value of the discriminant from the enclosing object.
1418 procedure Add_If_Clause (Expr : Node_Id) is
1420 Stats : constant List_Id :=
1422 Make_Return_Statement (Loc,
1423 Expression => Make_Integer_Literal (Loc, Index + 1)));
1426 -- Index for current entry body
1430 -- Compute total length of entry queues so far
1438 Right_Opnd => Expr);
1443 Left_Opnd => Make_Identifier (Loc, Name_uE),
1446 -- Map entry queue indices in the range of the current family
1447 -- into the current index, that designates the entry body.
1451 Make_Implicit_If_Statement (Typ,
1453 Then_Statements => Stats,
1454 Elsif_Parts => New_List);
1460 Make_Elsif_Part (Loc,
1462 Then_Statements => Stats),
1463 Elsif_Parts (If_St));
1467 ------------------------------
1468 -- Convert_Discriminant_Ref --
1469 ------------------------------
1471 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
1475 if Is_Entity_Name (Bound)
1476 and then Ekind (Entity (Bound)) = E_Discriminant
1479 Make_Selected_Component (Loc,
1481 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
1482 Make_Explicit_Dereference (Loc,
1483 Make_Identifier (Loc, Name_uObject))),
1484 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
1485 Set_Etype (B, Etype (Entity (Bound)));
1487 B := New_Copy_Tree (Bound);
1491 end Convert_Discriminant_Ref;
1493 -- Start of processing for Build_Find_Body_Index
1496 Spec := Build_Find_Body_Index_Spec (Typ);
1498 Ent := First_Entity (Typ);
1500 while Present (Ent) loop
1502 if Ekind (Ent) = E_Entry_Family then
1512 -- If the protected type has no entry families, there is a one-one
1513 -- correspondence between entry queue and entry body.
1516 Make_Return_Statement (Loc,
1517 Expression => Make_Identifier (Loc, Name_uE));
1520 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
1523 -- if E <= l1 then return 1;
1524 -- elsif E <= l1 + l2 then return 2;
1529 Ent := First_Entity (Typ);
1531 Add_Object_Pointer (Decls, Typ, Loc);
1533 while Present (Ent) loop
1535 if Ekind (Ent) = E_Entry then
1536 Add_If_Clause (Make_Integer_Literal (Loc, 1));
1538 elsif Ekind (Ent) = E_Entry_Family then
1540 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1541 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
1542 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
1543 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ));
1552 Make_Return_Statement (Loc,
1553 Expression => Make_Integer_Literal (Loc, 1));
1555 elsif Nkind (Ret) = N_If_Statement then
1557 -- Ranges are in increasing order, so last one doesn't need a
1561 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
1565 Set_Else_Statements (Ret, Then_Statements (Nod));
1571 Make_Subprogram_Body (Loc,
1572 Specification => Spec,
1573 Declarations => Decls,
1574 Handled_Statement_Sequence =>
1575 Make_Handled_Sequence_Of_Statements (Loc,
1576 Statements => New_List (Ret)));
1577 end Build_Find_Body_Index;
1579 --------------------------------
1580 -- Build_Find_Body_Index_Spec --
1581 --------------------------------
1583 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
1584 Loc : constant Source_Ptr := Sloc (Typ);
1585 Id : constant Entity_Id :=
1586 Make_Defining_Identifier (Loc,
1587 Chars => New_External_Name (Chars (Typ), 'F'));
1588 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
1589 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
1593 Make_Function_Specification (Loc,
1594 Defining_Unit_Name => Id,
1595 Parameter_Specifications => New_List (
1596 Make_Parameter_Specification (Loc,
1597 Defining_Identifier => Parm1,
1599 New_Reference_To (RTE (RE_Address), Loc)),
1601 Make_Parameter_Specification (Loc,
1602 Defining_Identifier => Parm2,
1604 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1605 Subtype_Mark => New_Occurrence_Of (
1606 RTE (RE_Protected_Entry_Index), Loc));
1607 end Build_Find_Body_Index_Spec;
1609 -------------------------
1610 -- Build_Master_Entity --
1611 -------------------------
1613 procedure Build_Master_Entity (E : Entity_Id) is
1614 Loc : constant Source_Ptr := Sloc (E);
1622 -- Ada 2005 (AI-287): Do not set/get the has_master_entity reminder
1623 -- in internal scopes. Required for nested limited aggregates.
1625 if Ada_Version >= Ada_05 then
1626 while Is_Internal (S) loop
1631 -- Nothing to do if we already built a master entity for this scope
1632 -- or if there is no task hierarchy.
1634 if Has_Master_Entity (S)
1635 or else Restriction_Active (No_Task_Hierarchy)
1640 -- Otherwise first build the master entity
1641 -- _Master : constant Master_Id := Current_Master.all;
1642 -- and insert it just before the current declaration
1645 Make_Object_Declaration (Loc,
1646 Defining_Identifier =>
1647 Make_Defining_Identifier (Loc, Name_uMaster),
1648 Constant_Present => True,
1649 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
1651 Make_Explicit_Dereference (Loc,
1652 New_Reference_To (RTE (RE_Current_Master), Loc)));
1655 Insert_Before (P, Decl);
1658 -- Ada 2005 (AI-287): Set the has_master_entity reminder in the
1659 -- non-internal scope selected above.
1661 if Ada_Version >= Ada_05 then
1662 Set_Has_Master_Entity (S);
1664 Set_Has_Master_Entity (Scope (E));
1667 -- Now mark the containing scope as a task master
1669 while Nkind (P) /= N_Compilation_Unit loop
1672 -- If we fall off the top, we are at the outer level, and the
1673 -- environment task is our effective master, so nothing to mark.
1675 if Nkind (P) = N_Task_Body
1676 or else Nkind (P) = N_Block_Statement
1677 or else Nkind (P) = N_Subprogram_Body
1679 Set_Is_Task_Master (P, True);
1682 elsif Nkind (Parent (P)) = N_Subunit then
1683 P := Corresponding_Stub (Parent (P));
1686 end Build_Master_Entity;
1688 ---------------------------
1689 -- Build_Protected_Entry --
1690 ---------------------------
1692 function Build_Protected_Entry
1695 Pid : Node_Id) return Node_Id
1697 Loc : constant Source_Ptr := Sloc (N);
1698 Op_Decls : constant List_Id := New_List;
1707 Make_Defining_Identifier (Loc,
1708 Chars => Chars (Protected_Body_Subprogram (Ent)));
1709 Espec := Build_Protected_Entry_Specification (Edef, Empty, Loc);
1711 -- <object pointer declaration>
1712 -- Add object pointer declaration. This is needed by the
1713 -- discriminal and prival renamings, which should already
1714 -- have been inserted into the declaration list.
1716 Add_Object_Pointer (Op_Decls, Pid, Loc);
1719 or else Restriction_Active (No_Entry_Queue) = False
1720 or else Number_Entries (Pid) > 1
1722 Complete := New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
1725 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
1728 Op_Stats := New_List (
1729 Make_Block_Statement (Loc,
1730 Declarations => Declarations (N),
1731 Handled_Statement_Sequence =>
1732 Handled_Statement_Sequence (N)),
1734 Make_Procedure_Call_Statement (Loc,
1736 Parameter_Associations => New_List (
1737 Make_Attribute_Reference (Loc,
1739 Make_Selected_Component (Loc,
1741 Make_Identifier (Loc, Name_uObject),
1744 Make_Identifier (Loc, Name_uObject)),
1745 Attribute_Name => Name_Unchecked_Access))));
1747 if Restriction_Active (No_Exception_Handlers) then
1749 Make_Subprogram_Body (Loc,
1750 Specification => Espec,
1751 Declarations => Op_Decls,
1752 Handled_Statement_Sequence =>
1753 Make_Handled_Sequence_Of_Statements (Loc, Op_Stats));
1756 Ohandle := Make_Others_Choice (Loc);
1757 Set_All_Others (Ohandle);
1760 or else Restriction_Active (No_Entry_Queue) = False
1761 or else Number_Entries (Pid) > 1
1764 New_Reference_To (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
1767 Complete := New_Reference_To (
1768 RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
1772 Make_Subprogram_Body (Loc,
1773 Specification => Espec,
1774 Declarations => Op_Decls,
1775 Handled_Statement_Sequence =>
1776 Make_Handled_Sequence_Of_Statements (Loc,
1777 Statements => Op_Stats,
1778 Exception_Handlers => New_List (
1779 Make_Exception_Handler (Loc,
1780 Exception_Choices => New_List (Ohandle),
1782 Statements => New_List (
1783 Make_Procedure_Call_Statement (Loc,
1785 Parameter_Associations => New_List (
1786 Make_Attribute_Reference (Loc,
1788 Make_Selected_Component (Loc,
1790 Make_Identifier (Loc, Name_uObject),
1792 Make_Identifier (Loc, Name_uObject)),
1793 Attribute_Name => Name_Unchecked_Access),
1795 Make_Function_Call (Loc,
1796 Name => New_Reference_To (
1797 RTE (RE_Get_GNAT_Exception), Loc)))))))));
1799 end Build_Protected_Entry;
1801 -----------------------------------------
1802 -- Build_Protected_Entry_Specification --
1803 -----------------------------------------
1805 function Build_Protected_Entry_Specification
1806 (Def_Id : Entity_Id;
1808 Loc : Source_Ptr) return Node_Id
1813 Set_Needs_Debug_Info (Def_Id);
1814 P := Make_Defining_Identifier (Loc, Name_uP);
1816 if Present (Ent_Id) then
1817 Append_Elmt (P, Accept_Address (Ent_Id));
1820 return Make_Procedure_Specification (Loc,
1821 Defining_Unit_Name => Def_Id,
1822 Parameter_Specifications => New_List (
1823 Make_Parameter_Specification (Loc,
1824 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
1826 New_Reference_To (RTE (RE_Address), Loc)),
1828 Make_Parameter_Specification (Loc,
1829 Defining_Identifier => P,
1831 New_Reference_To (RTE (RE_Address), Loc)),
1833 Make_Parameter_Specification (Loc,
1834 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uE),
1836 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
1837 end Build_Protected_Entry_Specification;
1839 --------------------------
1840 -- Build_Protected_Spec --
1841 --------------------------
1843 function Build_Protected_Spec
1845 Obj_Type : Entity_Id;
1846 Unprotected : Boolean := False;
1847 Ident : Entity_Id) return List_Id
1849 Loc : constant Source_Ptr := Sloc (N);
1851 New_Plist : List_Id;
1852 New_Param : Node_Id;
1855 New_Plist := New_List;
1856 Formal := First_Formal (Ident);
1858 while Present (Formal) loop
1860 Make_Parameter_Specification (Loc,
1861 Defining_Identifier =>
1862 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
1863 In_Present => In_Present (Parent (Formal)),
1864 Out_Present => Out_Present (Parent (Formal)),
1866 New_Reference_To (Etype (Formal), Loc));
1869 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
1872 Append (New_Param, New_Plist);
1873 Next_Formal (Formal);
1876 -- If the subprogram is a procedure and the context is not an access
1877 -- to protected subprogram, the parameter is in-out. Otherwise it is
1880 Prepend_To (New_Plist,
1881 Make_Parameter_Specification (Loc,
1882 Defining_Identifier =>
1883 Make_Defining_Identifier (Loc, Name_uObject),
1886 (Etype (Ident) = Standard_Void_Type
1887 and then not Is_RTE (Obj_Type, RE_Address)),
1888 Parameter_Type => New_Reference_To (Obj_Type, Loc)));
1891 end Build_Protected_Spec;
1893 ---------------------------------------
1894 -- Build_Protected_Sub_Specification --
1895 ---------------------------------------
1897 function Build_Protected_Sub_Specification
1899 Prottyp : Entity_Id;
1900 Unprotected : Boolean := False) return Node_Id
1902 Loc : constant Source_Ptr := Sloc (N);
1904 Protnm : constant Name_Id := Chars (Prottyp);
1908 New_Plist : List_Id;
1909 Append_Char : Character;
1914 (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
1916 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
1921 Ident := Defining_Unit_Name (Specification (Decl));
1922 Nam := Chars (Ident);
1924 New_Plist := Build_Protected_Spec
1925 (Decl, Corresponding_Record_Type (Prottyp),
1926 Unprotected, Ident);
1931 -- Ada 2005 (AI-345): The protected version no longer uses 'P'
1932 -- as suffix in order to make it a primitive operation
1934 if Ada_Version >= Ada_05 then
1942 Make_Defining_Identifier (Loc,
1943 Chars => Build_Selected_Name (Protnm, Nam, Append_Char));
1945 -- The unprotected operation carries the user code, and debugging
1946 -- information must be generated for it, even though this spec does
1947 -- not come from source. It is also convenient to allow gdb to step
1948 -- into the protected operation, even though it only contains lock/
1951 Set_Needs_Debug_Info (New_Id);
1953 if Nkind (Specification (Decl)) = N_Procedure_Specification then
1955 Make_Procedure_Specification (Loc,
1956 Defining_Unit_Name => New_Id,
1957 Parameter_Specifications => New_Plist);
1961 Make_Function_Specification (Loc,
1962 Defining_Unit_Name => New_Id,
1963 Parameter_Specifications => New_Plist,
1964 Subtype_Mark => New_Copy (Subtype_Mark (Specification (Decl))));
1965 Set_Return_Present (Defining_Unit_Name (New_Spec));
1968 end Build_Protected_Sub_Specification;
1970 -------------------------------------
1971 -- Build_Protected_Subprogram_Body --
1972 -------------------------------------
1974 function Build_Protected_Subprogram_Body
1977 N_Op_Spec : Node_Id) return Node_Id
1979 Loc : constant Source_Ptr := Sloc (N);
1981 P_Op_Spec : Node_Id;
1984 Unprot_Call : Node_Id;
1986 Lock_Name : Node_Id;
1987 Lock_Stmt : Node_Id;
1988 Service_Name : Node_Id;
1990 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
1991 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
1993 Object_Parm : Node_Id;
1996 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
1997 -- Tell whether a given subprogram cannot raise an exception
1999 -----------------------
2000 -- Is_Exception_Safe --
2001 -----------------------
2003 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
2005 function Has_Side_Effect (N : Node_Id) return Boolean;
2006 -- Return True whenever encountering a subprogram call or a
2007 -- raise statement of any kind in the sequence of statements N
2009 ---------------------
2010 -- Has_Side_Effect --
2011 ---------------------
2013 -- What is this doing buried two levels down in exp_ch9. It
2014 -- seems like a generally useful function, and indeed there
2015 -- may be code duplication going on here ???
2017 function Has_Side_Effect (N : Node_Id) return Boolean is
2018 Stmt : Node_Id := N;
2021 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
2022 -- Indicate whether N is a subprogram call or a raise statement
2024 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
2026 return Nkind (N) = N_Procedure_Call_Statement
2027 or else Nkind (N) = N_Function_Call
2028 or else Nkind (N) = N_Raise_Statement
2029 or else Nkind (N) = N_Raise_Constraint_Error
2030 or else Nkind (N) = N_Raise_Program_Error
2031 or else Nkind (N) = N_Raise_Storage_Error;
2032 end Is_Call_Or_Raise;
2034 -- Start of processing for Has_Side_Effect
2037 while Present (Stmt) loop
2038 if Is_Call_Or_Raise (Stmt) then
2042 -- An object declaration can also contain a function call
2043 -- or a raise statement
2045 if Nkind (Stmt) = N_Object_Declaration then
2046 Expr := Expression (Stmt);
2048 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
2057 end Has_Side_Effect;
2059 -- Start of processing for Is_Exception_Safe
2062 -- If the checks handled by the back end are not disabled, we cannot
2063 -- ensure that no exception will be raised.
2065 if not Access_Checks_Suppressed (Empty)
2066 or else not Discriminant_Checks_Suppressed (Empty)
2067 or else not Range_Checks_Suppressed (Empty)
2068 or else not Index_Checks_Suppressed (Empty)
2069 or else Opt.Stack_Checking_Enabled
2074 if Has_Side_Effect (First (Declarations (Subprogram)))
2077 First (Statements (Handled_Statement_Sequence (Subprogram))))
2083 end Is_Exception_Safe;
2085 -- Start of processing for Build_Protected_Subprogram_Body
2088 Op_Spec := Specification (N);
2089 Exc_Safe := Is_Exception_Safe (N);
2092 Build_Protected_Sub_Specification (N,
2093 Pid, Unprotected => False);
2095 -- Build a list of the formal parameters of the protected
2096 -- version of the subprogram to use as the actual parameters
2097 -- of the unprotected version.
2099 Uactuals := New_List;
2100 Pformal := First (Parameter_Specifications (P_Op_Spec));
2102 while Present (Pformal) loop
2104 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))),
2109 -- Make a call to the unprotected version of the subprogram
2110 -- built above for use by the protected version built below.
2112 if Nkind (Op_Spec) = N_Function_Specification then
2114 R := Make_Defining_Identifier (Loc, New_Internal_Name ('R'));
2116 Make_Object_Declaration (Loc,
2117 Defining_Identifier => R,
2118 Constant_Present => True,
2119 Object_Definition => New_Copy (Subtype_Mark (N_Op_Spec)),
2121 Make_Function_Call (Loc,
2122 Name => Make_Identifier (Loc,
2123 Chars (Defining_Unit_Name (N_Op_Spec))),
2124 Parameter_Associations => Uactuals));
2125 Return_Stmt := Make_Return_Statement (Loc,
2126 Expression => New_Reference_To (R, Loc));
2129 Unprot_Call := Make_Return_Statement (Loc,
2130 Expression => Make_Function_Call (Loc,
2132 Make_Identifier (Loc,
2133 Chars (Defining_Unit_Name (N_Op_Spec))),
2134 Parameter_Associations => Uactuals));
2138 Unprot_Call := Make_Procedure_Call_Statement (Loc,
2140 Make_Identifier (Loc,
2141 Chars (Defining_Unit_Name (N_Op_Spec))),
2142 Parameter_Associations => Uactuals);
2145 -- Wrap call in block that will be covered by an at_end handler
2147 if not Exc_Safe then
2148 Unprot_Call := Make_Block_Statement (Loc,
2149 Handled_Statement_Sequence =>
2150 Make_Handled_Sequence_Of_Statements (Loc,
2151 Statements => New_List (Unprot_Call)));
2154 -- Make the protected subprogram body. This locks the protected
2155 -- object and calls the unprotected version of the subprogram.
2157 -- If the protected object is controlled (i.e it has entries or
2158 -- needs finalization for interrupt handling), call Lock_Entries,
2159 -- except if the protected object follows the Ravenscar profile, in
2160 -- which case call Lock_Entry, otherwise call the simplified version,
2163 if Has_Entries (Pid)
2164 or else Has_Interrupt_Handler (Pid)
2165 or else (Has_Attach_Handler (Pid) and then not Restricted_Profile)
2168 or else Restriction_Active (No_Entry_Queue) = False
2169 or else Number_Entries (Pid) > 1
2171 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
2172 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
2175 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
2176 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
2180 Lock_Name := New_Reference_To (RTE (RE_Lock), Loc);
2181 Service_Name := New_Reference_To (RTE (RE_Unlock), Loc);
2185 Make_Attribute_Reference (Loc,
2187 Make_Selected_Component (Loc,
2189 Make_Identifier (Loc, Name_uObject),
2191 Make_Identifier (Loc, Name_uObject)),
2192 Attribute_Name => Name_Unchecked_Access);
2194 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
2196 Parameter_Associations => New_List (Object_Parm));
2198 if Abort_Allowed then
2200 Make_Procedure_Call_Statement (Loc,
2201 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
2202 Parameter_Associations => Empty_List),
2206 Stmts := New_List (Lock_Stmt);
2209 if not Exc_Safe then
2210 Append (Unprot_Call, Stmts);
2212 if Nkind (Op_Spec) = N_Function_Specification then
2214 Stmts := Empty_List;
2216 Append (Unprot_Call, Stmts);
2220 Make_Procedure_Call_Statement (Loc,
2221 Name => Service_Name,
2222 Parameter_Associations =>
2223 New_List (New_Copy_Tree (Object_Parm))),
2226 if Abort_Allowed then
2228 Make_Procedure_Call_Statement (Loc,
2229 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
2230 Parameter_Associations => Empty_List),
2234 if Nkind (Op_Spec) = N_Function_Specification then
2235 Append (Return_Stmt, Stmts);
2236 Append (Make_Block_Statement (Loc,
2237 Declarations => New_List (Unprot_Call),
2238 Handled_Statement_Sequence =>
2239 Make_Handled_Sequence_Of_Statements (Loc,
2240 Statements => Stmts)), Pre_Stmts);
2246 Make_Subprogram_Body (Loc,
2247 Declarations => Empty_List,
2248 Specification => P_Op_Spec,
2249 Handled_Statement_Sequence =>
2250 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
2252 if not Exc_Safe then
2253 Set_Is_Protected_Subprogram_Body (Sub_Body);
2257 end Build_Protected_Subprogram_Body;
2259 -------------------------------------
2260 -- Build_Protected_Subprogram_Call --
2261 -------------------------------------
2263 procedure Build_Protected_Subprogram_Call
2267 External : Boolean := True)
2269 Loc : constant Source_Ptr := Sloc (N);
2270 Sub : constant Entity_Id := Entity (Name);
2276 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
2279 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
2282 if Present (Parameter_Associations (N)) then
2283 Params := New_Copy_List_Tree (Parameter_Associations (N));
2288 Prepend (Rec, Params);
2290 if Ekind (Sub) = E_Procedure then
2292 Make_Procedure_Call_Statement (Loc,
2294 Parameter_Associations => Params));
2297 pragma Assert (Ekind (Sub) = E_Function);
2299 Make_Function_Call (Loc,
2301 Parameter_Associations => Params));
2305 and then Nkind (Rec) = N_Unchecked_Type_Conversion
2306 and then Is_Entity_Name (Expression (Rec))
2307 and then Is_Shared_Passive (Entity (Expression (Rec)))
2309 Add_Shared_Var_Lock_Procs (N);
2311 end Build_Protected_Subprogram_Call;
2313 -------------------------
2314 -- Build_Selected_Name --
2315 -------------------------
2317 function Build_Selected_Name
2318 (Prefix, Selector : Name_Id;
2319 Append_Char : Character := ' ') return Name_Id
2321 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
2322 Select_Len : Natural;
2325 Get_Name_String (Selector);
2326 Select_Len := Name_Len;
2327 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
2328 Get_Name_String (Prefix);
2330 -- If scope is anonymous type, discard suffix to recover name of
2331 -- single protected object. Otherwise use protected type name.
2333 if Name_Buffer (Name_Len) = 'T' then
2334 Name_Len := Name_Len - 1;
2337 Name_Buffer (Name_Len + 1) := '_';
2338 Name_Buffer (Name_Len + 2) := '_';
2340 Name_Len := Name_Len + 2;
2341 for J in 1 .. Select_Len loop
2342 Name_Len := Name_Len + 1;
2343 Name_Buffer (Name_Len) := Select_Buffer (J);
2346 if Append_Char /= ' ' then
2347 Name_Len := Name_Len + 1;
2348 Name_Buffer (Name_Len) := Append_Char;
2352 end Build_Selected_Name;
2354 -----------------------------
2355 -- Build_Simple_Entry_Call --
2356 -----------------------------
2358 -- A task entry call is converted to a call to Call_Simple
2361 -- P : parms := (parm, parm, parm);
2363 -- Call_Simple (acceptor-task, entry-index, P'Address);
2369 -- Here Pnn is an aggregate of the type constructed for the entry to hold
2370 -- the parameters, and the constructed aggregate value contains either the
2371 -- parameters or, in the case of non-elementary types, references to these
2372 -- parameters. Then the address of this aggregate is passed to the runtime
2373 -- routine, along with the task id value and the task entry index value.
2374 -- Pnn is only required if parameters are present.
2376 -- The assignments after the call are present only in the case of in-out
2377 -- or out parameters for elementary types, and are used to assign back the
2378 -- resulting values of such parameters.
2380 -- Note: the reason that we insert a block here is that in the context
2381 -- of selects, conditional entry calls etc. the entry call statement
2382 -- appears on its own, not as an element of a list.
2384 -- A protected entry call is converted to a Protected_Entry_Call:
2387 -- P : E1_Params := (param, param, param);
2389 -- Bnn : Communications_Block;
2392 -- P : E1_Params := (param, param, param);
2393 -- Bnn : Communications_Block;
2396 -- Protected_Entry_Call (
2397 -- Object => po._object'Access,
2398 -- E => <entry index>;
2399 -- Uninterpreted_Data => P'Address;
2400 -- Mode => Simple_Call;
2407 procedure Build_Simple_Entry_Call
2416 -- Convert entry call to Call_Simple call
2419 Loc : constant Source_Ptr := Sloc (N);
2420 Parms : constant List_Id := Parameter_Associations (N);
2421 Stats : constant List_Id := New_List;
2427 Ent_Acc : Entity_Id;
2439 Comm_Name : Entity_Id;
2442 -- Simple entry and entry family cases merge here
2444 Ent := Entity (Ename);
2445 Ent_Acc := Entry_Parameters_Type (Ent);
2446 Conctyp := Etype (Concval);
2448 -- If prefix is an access type, dereference to obtain the task type
2450 if Is_Access_Type (Conctyp) then
2451 Conctyp := Designated_Type (Conctyp);
2454 -- Special case for protected subprogram calls
2456 if Is_Protected_Type (Conctyp)
2457 and then Is_Subprogram (Entity (Ename))
2459 if not Is_Eliminated (Entity (Ename)) then
2460 Build_Protected_Subprogram_Call
2461 (N, Ename, Convert_Concurrent (Concval, Conctyp));
2468 -- First parameter is the Task_Id value from the task value or the
2469 -- Object from the protected object value, obtained by selecting
2470 -- the _Task_Id or _Object from the result of doing an unchecked
2471 -- conversion to convert the value to the corresponding record type.
2473 Parm1 := Concurrent_Ref (Concval);
2475 -- Second parameter is the entry index, computed by the routine
2476 -- provided for this purpose. The value of this expression is
2477 -- assigned to an intermediate variable to assure that any entry
2478 -- family index expressions are evaluated before the entry
2482 or else Restriction_Active (No_Entry_Queue) = False
2483 or else not Is_Protected_Type (Conctyp)
2484 or else Number_Entries (Conctyp) > 1
2486 X := Make_Defining_Identifier (Loc, Name_uX);
2489 Make_Object_Declaration (Loc,
2490 Defining_Identifier => X,
2491 Object_Definition =>
2492 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
2493 Expression => Actual_Index_Expression (
2494 Loc, Entity (Ename), Index, Concval));
2496 Decls := New_List (Xdecl);
2497 Parm2 := New_Reference_To (X, Loc);
2505 -- The third parameter is the packaged parameters. If there are
2506 -- none, then it is just the null address, since nothing is passed
2509 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
2512 -- Case of parameters present, where third argument is the address
2513 -- of a packaged record containing the required parameter values.
2516 -- First build a list of parameter values, which are
2517 -- references to objects of the parameter types.
2521 Actual := First_Actual (N);
2522 Formal := First_Formal (Ent);
2524 while Present (Actual) loop
2526 -- If it is a by_copy_type, copy it to a new variable. The
2527 -- packaged record has a field that points to this variable.
2529 if Is_By_Copy_Type (Etype (Actual)) then
2531 Make_Object_Declaration (Loc,
2532 Defining_Identifier =>
2533 Make_Defining_Identifier (Loc,
2534 Chars => New_Internal_Name ('J')),
2535 Aliased_Present => True,
2536 Object_Definition =>
2537 New_Reference_To (Etype (Formal), Loc));
2539 -- We have to make an assignment statement separate for
2540 -- the case of limited type. We can not assign it unless
2541 -- the Assignment_OK flag is set first.
2543 if Ekind (Formal) /= E_Out_Parameter then
2545 New_Reference_To (Defining_Identifier (N_Node), Loc);
2546 Set_Assignment_OK (N_Var);
2548 Make_Assignment_Statement (Loc,
2550 Expression => Relocate_Node (Actual)));
2553 Append (N_Node, Decls);
2556 Make_Attribute_Reference (Loc,
2557 Attribute_Name => Name_Unchecked_Access,
2559 New_Reference_To (Defining_Identifier (N_Node), Loc)));
2562 Make_Reference (Loc, Prefix => Relocate_Node (Actual)));
2565 Next_Actual (Actual);
2566 Next_Formal_With_Extras (Formal);
2569 -- Now build the declaration of parameters initialized with the
2570 -- aggregate containing this constructed parameter list.
2572 P := Make_Defining_Identifier (Loc, Name_uP);
2575 Make_Object_Declaration (Loc,
2576 Defining_Identifier => P,
2577 Object_Definition =>
2578 New_Reference_To (Designated_Type (Ent_Acc), Loc),
2580 Make_Aggregate (Loc, Expressions => Plist));
2583 Make_Attribute_Reference (Loc,
2584 Attribute_Name => Name_Address,
2585 Prefix => New_Reference_To (P, Loc));
2587 Append (Pdecl, Decls);
2590 -- Now we can create the call, case of protected type
2592 if Is_Protected_Type (Conctyp) then
2594 or else Restriction_Active (No_Entry_Queue) = False
2595 or else Number_Entries (Conctyp) > 1
2597 -- Change the type of the index declaration
2599 Set_Object_Definition (Xdecl,
2600 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
2602 -- Some additional declarations for protected entry calls
2608 -- Bnn : Communications_Block;
2611 Make_Defining_Identifier (Loc, New_Internal_Name ('B'));
2614 Make_Object_Declaration (Loc,
2615 Defining_Identifier => Comm_Name,
2616 Object_Definition =>
2617 New_Reference_To (RTE (RE_Communication_Block), Loc)));
2619 -- Some additional statements for protected entry calls
2621 -- Protected_Entry_Call (
2622 -- Object => po._object'Access,
2623 -- E => <entry index>;
2624 -- Uninterpreted_Data => P'Address;
2625 -- Mode => Simple_Call;
2629 Make_Procedure_Call_Statement (Loc,
2631 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
2633 Parameter_Associations => New_List (
2634 Make_Attribute_Reference (Loc,
2635 Attribute_Name => Name_Unchecked_Access,
2639 New_Reference_To (RTE (RE_Simple_Call), Loc),
2640 New_Occurrence_Of (Comm_Name, Loc)));
2643 -- Protected_Single_Entry_Call (
2644 -- Object => po._object'Access,
2645 -- Uninterpreted_Data => P'Address;
2646 -- Mode => Simple_Call);
2649 Make_Procedure_Call_Statement (Loc,
2650 Name => New_Reference_To (
2651 RTE (RE_Protected_Single_Entry_Call), Loc),
2653 Parameter_Associations => New_List (
2654 Make_Attribute_Reference (Loc,
2655 Attribute_Name => Name_Unchecked_Access,
2658 New_Reference_To (RTE (RE_Simple_Call), Loc)));
2661 -- Case of task type
2665 Make_Procedure_Call_Statement (Loc,
2666 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
2667 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
2671 Append_To (Stats, Call);
2673 -- If there are out or in/out parameters by copy
2674 -- add assignment statements for the result values.
2676 if Present (Parms) then
2677 Actual := First_Actual (N);
2678 Formal := First_Formal (Ent);
2680 Set_Assignment_OK (Actual);
2681 while Present (Actual) loop
2682 if Is_By_Copy_Type (Etype (Actual))
2683 and then Ekind (Formal) /= E_In_Parameter
2686 Make_Assignment_Statement (Loc,
2687 Name => New_Copy (Actual),
2689 Make_Explicit_Dereference (Loc,
2690 Make_Selected_Component (Loc,
2691 Prefix => New_Reference_To (P, Loc),
2693 Make_Identifier (Loc, Chars (Formal)))));
2695 -- In all cases (including limited private types) we
2696 -- want the assignment to be valid.
2698 Set_Assignment_OK (Name (N_Node));
2700 -- If the call is the triggering alternative in an
2701 -- asynchronous select, or the entry_call alternative
2702 -- of a conditional entry call, the assignments for in-out
2703 -- parameters are incorporated into the statement list
2704 -- that follows, so that there are executed only if the
2705 -- entry call succeeds.
2707 if (Nkind (Parent (N)) = N_Triggering_Alternative
2708 and then N = Triggering_Statement (Parent (N)))
2710 (Nkind (Parent (N)) = N_Entry_Call_Alternative
2711 and then N = Entry_Call_Statement (Parent (N)))
2713 if No (Statements (Parent (N))) then
2714 Set_Statements (Parent (N), New_List);
2717 Prepend (N_Node, Statements (Parent (N)));
2720 Insert_After (Call, N_Node);
2724 Next_Actual (Actual);
2725 Next_Formal_With_Extras (Formal);
2729 -- Finally, create block and analyze it
2732 Make_Block_Statement (Loc,
2733 Declarations => Decls,
2734 Handled_Statement_Sequence =>
2735 Make_Handled_Sequence_Of_Statements (Loc,
2736 Statements => Stats)));
2740 end Build_Simple_Entry_Call;
2742 --------------------------------
2743 -- Build_Task_Activation_Call --
2744 --------------------------------
2746 procedure Build_Task_Activation_Call (N : Node_Id) is
2747 Loc : constant Source_Ptr := Sloc (N);
2754 -- Get the activation chain entity. Except in the case of a package
2755 -- body, this is in the node that w as passed. For a package body, we
2756 -- have to find the corresponding package declaration node.
2758 if Nkind (N) = N_Package_Body then
2759 P := Corresponding_Spec (N);
2763 exit when Nkind (P) = N_Package_Declaration;
2766 Chain := Activation_Chain_Entity (P);
2769 Chain := Activation_Chain_Entity (N);
2772 if Present (Chain) then
2773 if Restricted_Profile then
2774 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
2776 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
2780 Make_Procedure_Call_Statement (Loc,
2782 Parameter_Associations =>
2783 New_List (Make_Attribute_Reference (Loc,
2784 Prefix => New_Occurrence_Of (Chain, Loc),
2785 Attribute_Name => Name_Unchecked_Access)));
2787 if Nkind (N) = N_Package_Declaration then
2788 if Present (Corresponding_Body (N)) then
2791 elsif Present (Private_Declarations (Specification (N))) then
2792 Append (Call, Private_Declarations (Specification (N)));
2795 Append (Call, Visible_Declarations (Specification (N)));
2799 if Present (Handled_Statement_Sequence (N)) then
2801 -- The call goes at the start of the statement sequence, but
2802 -- after the start of exception range label if one is present.
2808 Stm := First (Statements (Handled_Statement_Sequence (N)));
2810 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
2814 Insert_Before (Stm, Call);
2818 Set_Handled_Statement_Sequence (N,
2819 Make_Handled_Sequence_Of_Statements (Loc,
2820 Statements => New_List (Call)));
2825 Check_Task_Activation (N);
2827 end Build_Task_Activation_Call;
2829 -------------------------------
2830 -- Build_Task_Allocate_Block --
2831 -------------------------------
2833 procedure Build_Task_Allocate_Block
2838 T : constant Entity_Id := Entity (Expression (N));
2839 Init : constant Entity_Id := Base_Init_Proc (T);
2840 Loc : constant Source_Ptr := Sloc (N);
2841 Chain : constant Entity_Id :=
2842 Make_Defining_Identifier (Loc, Name_uChain);
2848 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2851 Make_Block_Statement (Loc,
2852 Identifier => New_Reference_To (Blkent, Loc),
2853 Declarations => New_List (
2855 -- _Chain : Activation_Chain;
2857 Make_Object_Declaration (Loc,
2858 Defining_Identifier => Chain,
2859 Aliased_Present => True,
2860 Object_Definition =>
2861 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
2863 Handled_Statement_Sequence =>
2864 Make_Handled_Sequence_Of_Statements (Loc,
2866 Statements => New_List (
2870 Make_Procedure_Call_Statement (Loc,
2871 Name => New_Reference_To (Init, Loc),
2872 Parameter_Associations => Args),
2874 -- Activate_Tasks (_Chain);
2876 Make_Procedure_Call_Statement (Loc,
2877 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
2878 Parameter_Associations => New_List (
2879 Make_Attribute_Reference (Loc,
2880 Prefix => New_Reference_To (Chain, Loc),
2881 Attribute_Name => Name_Unchecked_Access))))),
2883 Has_Created_Identifier => True,
2884 Is_Task_Allocation_Block => True);
2887 Make_Implicit_Label_Declaration (Loc,
2888 Defining_Identifier => Blkent,
2889 Label_Construct => Block));
2891 Append_To (Actions, Block);
2893 Set_Activation_Chain_Entity (Block, Chain);
2894 end Build_Task_Allocate_Block;
2896 -----------------------------------------------
2897 -- Build_Task_Allocate_Block_With_Init_Stmts --
2898 -----------------------------------------------
2900 procedure Build_Task_Allocate_Block_With_Init_Stmts
2903 Init_Stmts : List_Id)
2905 Loc : constant Source_Ptr := Sloc (N);
2906 Chain : constant Entity_Id :=
2907 Make_Defining_Identifier (Loc, Name_uChain);
2912 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
2914 Append_To (Init_Stmts,
2915 Make_Procedure_Call_Statement (Loc,
2916 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
2917 Parameter_Associations => New_List (
2918 Make_Attribute_Reference (Loc,
2919 Prefix => New_Reference_To (Chain, Loc),
2920 Attribute_Name => Name_Unchecked_Access))));
2923 Make_Block_Statement (Loc,
2924 Identifier => New_Reference_To (Blkent, Loc),
2925 Declarations => New_List (
2927 -- _Chain : Activation_Chain;
2929 Make_Object_Declaration (Loc,
2930 Defining_Identifier => Chain,
2931 Aliased_Present => True,
2932 Object_Definition =>
2933 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
2935 Handled_Statement_Sequence =>
2936 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
2938 Has_Created_Identifier => True,
2939 Is_Task_Allocation_Block => True);
2942 Make_Implicit_Label_Declaration (Loc,
2943 Defining_Identifier => Blkent,
2944 Label_Construct => Block));
2946 Append_To (Actions, Block);
2948 Set_Activation_Chain_Entity (Block, Chain);
2949 end Build_Task_Allocate_Block_With_Init_Stmts;
2951 -----------------------------------
2952 -- Build_Task_Proc_Specification --
2953 -----------------------------------
2955 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
2956 Loc : constant Source_Ptr := Sloc (T);
2957 Nam : constant Name_Id := Chars (T);
2962 Make_Defining_Identifier (Loc,
2963 Chars => New_External_Name (Nam, 'B'));
2964 Set_Is_Internal (Ent);
2966 -- Associate the procedure with the task, if this is the declaration
2967 -- (and not the body) of the procedure.
2969 if No (Task_Body_Procedure (T)) then
2970 Set_Task_Body_Procedure (T, Ent);
2974 Make_Procedure_Specification (Loc,
2975 Defining_Unit_Name => Ent,
2976 Parameter_Specifications =>
2978 Make_Parameter_Specification (Loc,
2979 Defining_Identifier =>
2980 Make_Defining_Identifier (Loc, Name_uTask),
2982 Make_Access_Definition (Loc,
2985 (Corresponding_Record_Type (T), Loc)))));
2986 end Build_Task_Proc_Specification;
2988 ---------------------------------------
2989 -- Build_Unprotected_Subprogram_Body --
2990 ---------------------------------------
2992 function Build_Unprotected_Subprogram_Body
2994 Pid : Node_Id) return Node_Id
2996 Loc : constant Source_Ptr := Sloc (N);
2997 N_Op_Spec : Node_Id;
3001 -- Make an unprotected version of the subprogram for use
3002 -- within the same object, with a new name and an additional
3003 -- parameter representing the object.
3005 Op_Decls := Declarations (N);
3007 Build_Protected_Sub_Specification
3008 (N, Pid, Unprotected => True);
3011 Make_Subprogram_Body (Loc,
3012 Specification => N_Op_Spec,
3013 Declarations => Op_Decls,
3014 Handled_Statement_Sequence =>
3015 Handled_Statement_Sequence (N));
3016 end Build_Unprotected_Subprogram_Body;
3018 ----------------------------
3019 -- Collect_Entry_Families --
3020 ----------------------------
3022 procedure Collect_Entry_Families
3025 Current_Node : in out Node_Id;
3026 Conctyp : Entity_Id)
3029 Efam_Decl : Node_Id;
3030 Efam_Type : Entity_Id;
3033 Efam := First_Entity (Conctyp);
3035 while Present (Efam) loop
3037 if Ekind (Efam) = E_Entry_Family then
3039 Make_Defining_Identifier (Loc,
3040 Chars => New_Internal_Name ('F'));
3043 Make_Full_Type_Declaration (Loc,
3044 Defining_Identifier => Efam_Type,
3046 Make_Unconstrained_Array_Definition (Loc,
3047 Subtype_Marks => (New_List (
3050 (Etype (Discrete_Subtype_Definition
3051 (Parent (Efam)))), Loc))),
3053 Component_Definition =>
3054 Make_Component_Definition (Loc,
3055 Aliased_Present => False,
3056 Subtype_Indication =>
3057 New_Reference_To (Standard_Character, Loc))));
3059 Insert_After (Current_Node, Efam_Decl);
3060 Current_Node := Efam_Decl;
3061 Analyze (Efam_Decl);
3064 Make_Component_Declaration (Loc,
3065 Defining_Identifier =>
3066 Make_Defining_Identifier (Loc, Chars (Efam)),
3068 Component_Definition =>
3069 Make_Component_Definition (Loc,
3070 Aliased_Present => False,
3071 Subtype_Indication =>
3072 Make_Subtype_Indication (Loc,
3074 New_Occurrence_Of (Efam_Type, Loc),
3077 Make_Index_Or_Discriminant_Constraint (Loc,
3078 Constraints => New_List (
3080 (Etype (Discrete_Subtype_Definition
3081 (Parent (Efam))), Loc)))))));
3087 end Collect_Entry_Families;
3089 --------------------
3090 -- Concurrent_Ref --
3091 --------------------
3093 -- The expression returned for a reference to a concurrent
3094 -- object has the form:
3096 -- taskV!(name)._Task_Id
3100 -- objectV!(name)._Object
3102 -- for a protected object. For the case of an access to a concurrent
3103 -- object, there is an extra explicit dereference:
3105 -- taskV!(name.all)._Task_Id
3106 -- objectV!(name.all)._Object
3108 -- here taskV and objectV are the types for the associated records, which
3109 -- contain the required _Task_Id and _Object fields for tasks and
3110 -- protected objects, respectively.
3112 -- For the case of a task type name, the expression is
3116 -- i.e. a call to the Self function which returns precisely this Task_Id
3118 -- For the case of a protected type name, the expression is
3122 -- which is a renaming of the _object field of the current object
3123 -- object record, passed into protected operations as a parameter.
3125 function Concurrent_Ref (N : Node_Id) return Node_Id is
3126 Loc : constant Source_Ptr := Sloc (N);
3127 Ntyp : constant Entity_Id := Etype (N);
3131 function Is_Current_Task (T : Entity_Id) return Boolean;
3132 -- Check whether the reference is to the immediately enclosing task
3133 -- type, or to an outer one (rare but legal).
3135 ---------------------
3136 -- Is_Current_Task --
3137 ---------------------
3139 function Is_Current_Task (T : Entity_Id) return Boolean is
3143 Scop := Current_Scope;
3144 while Present (Scop)
3145 and then Scop /= Standard_Standard
3151 elsif Is_Task_Type (Scop) then
3154 -- If this is a procedure nested within the task type, we must
3155 -- assume that it can be called from an inner task, and therefore
3156 -- cannot treat it as a local reference.
3158 elsif Is_Overloadable (Scop)
3159 and then In_Open_Scopes (T)
3164 Scop := Scope (Scop);
3168 -- We know that we are within the task body, so should have
3169 -- found it in scope.
3171 raise Program_Error;
3172 end Is_Current_Task;
3174 -- Start of processing for Concurrent_Ref
3177 if Is_Access_Type (Ntyp) then
3178 Dtyp := Designated_Type (Ntyp);
3180 if Is_Protected_Type (Dtyp) then
3181 Sel := Name_uObject;
3183 Sel := Name_uTask_Id;
3187 Make_Selected_Component (Loc,
3189 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
3190 Make_Explicit_Dereference (Loc, N)),
3191 Selector_Name => Make_Identifier (Loc, Sel));
3193 elsif Is_Entity_Name (N)
3194 and then Is_Concurrent_Type (Entity (N))
3196 if Is_Task_Type (Entity (N)) then
3198 if Is_Current_Task (Entity (N)) then
3200 Make_Function_Call (Loc,
3201 Name => New_Reference_To (RTE (RE_Self), Loc));
3206 T_Self : constant Entity_Id
3207 := Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
3208 T_Body : constant Node_Id
3209 := Parent (Corresponding_Body (Parent (Entity (N))));
3212 Decl := Make_Object_Declaration (Loc,
3213 Defining_Identifier => T_Self,
3214 Object_Definition =>
3215 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
3217 Make_Function_Call (Loc,
3218 Name => New_Reference_To (RTE (RE_Self), Loc)));
3219 Prepend (Decl, Declarations (T_Body));
3221 Set_Scope (T_Self, Entity (N));
3222 return New_Occurrence_Of (T_Self, Loc);
3227 pragma Assert (Is_Protected_Type (Entity (N)));
3230 Object_Ref (Corresponding_Body (Parent (Base_Type (Ntyp)))),
3235 pragma Assert (Is_Concurrent_Type (Ntyp));
3237 if Is_Protected_Type (Ntyp) then
3238 Sel := Name_uObject;
3240 Sel := Name_uTask_Id;
3244 Make_Selected_Component (Loc,
3246 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
3248 Selector_Name => Make_Identifier (Loc, Sel));
3252 ------------------------
3253 -- Convert_Concurrent --
3254 ------------------------
3256 function Convert_Concurrent
3258 Typ : Entity_Id) return Node_Id
3261 if not Is_Concurrent_Type (Typ) then
3265 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
3268 end Convert_Concurrent;
3270 ----------------------------
3271 -- Entry_Index_Expression --
3272 ----------------------------
3274 function Entry_Index_Expression
3278 Ttyp : Entity_Id) return Node_Id
3288 -- The queues of entries and entry families appear in textual
3289 -- order in the associated record. The entry index is computed as
3290 -- the sum of the number of queues for all entries that precede the
3291 -- designated one, to which is added the index expression, if this
3292 -- expression denotes a member of a family.
3294 -- The following is a place holder for the count of simple entries
3296 Num := Make_Integer_Literal (Sloc, 1);
3298 -- We construct an expression which is a series of addition
3299 -- operations. The first operand is the number of single entries that
3300 -- precede this one, the second operand is the index value relative
3301 -- to the start of the referenced family, and the remaining operands
3302 -- are the lengths of the entry families that precede this entry, i.e.
3303 -- the constructed expression is:
3305 -- number_simple_entries +
3306 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
3307 -- family'length + ...
3309 -- where index-value is the given index value, and s is the index
3310 -- subtype (we have to use pos because the subtype might be an
3311 -- enumeration type preventing direct subtraction).
3312 -- Note that the task entry array is one-indexed.
3314 -- The upper bound of the entry family may be a discriminant, so we
3315 -- retrieve the lower bound explicitly to compute offset, rather than
3316 -- using the index subtype which may mention a discriminant.
3318 if Present (Index) then
3319 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
3328 Make_Attribute_Reference (Sloc,
3329 Attribute_Name => Name_Pos,
3330 Prefix => New_Reference_To (Base_Type (S), Sloc),
3331 Expressions => New_List (Relocate_Node (Index))),
3338 -- Now add lengths of preceding entries and entry families
3340 Prev := First_Entity (Ttyp);
3342 while Chars (Prev) /= Chars (Ent)
3343 or else (Ekind (Prev) /= Ekind (Ent))
3344 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
3346 if Ekind (Prev) = E_Entry then
3347 Set_Intval (Num, Intval (Num) + 1);
3349 elsif Ekind (Prev) = E_Entry_Family then
3351 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
3352 Lo := Type_Low_Bound (S);
3353 Hi := Type_High_Bound (S);
3358 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp));
3360 -- Other components are anonymous types to be ignored
3370 end Entry_Index_Expression;
3372 ---------------------------
3373 -- Establish_Task_Master --
3374 ---------------------------
3376 procedure Establish_Task_Master (N : Node_Id) is
3380 if Restriction_Active (No_Task_Hierarchy) = False then
3381 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
3382 Prepend_To (Declarations (N), Call);
3385 end Establish_Task_Master;
3387 --------------------------------
3388 -- Expand_Accept_Declarations --
3389 --------------------------------
3391 -- Part of the expansion of an accept statement involves the creation of
3392 -- a declaration that can be referenced from the statement sequence of
3397 -- This declaration is inserted immediately before the accept statement
3398 -- and it is important that it be inserted before the statements of the
3399 -- statement sequence are analyzed. Thus it would be too late to create
3400 -- this declaration in the Expand_N_Accept_Statement routine, which is
3401 -- why there is a separate procedure to be called directly from Sem_Ch9.
3403 -- Ann is used to hold the address of the record containing the parameters
3404 -- (see Expand_N_Entry_Call for more details on how this record is built).
3405 -- References to the parameters do an unchecked conversion of this address
3406 -- to a pointer to the required record type, and then access the field that
3407 -- holds the value of the required parameter. The entity for the address
3408 -- variable is held as the top stack element (i.e. the last element) of the
3409 -- Accept_Address stack in the corresponding entry entity, and this element
3410 -- must be set in place before the statements are processed.
3412 -- The above description applies to the case of a stand alone accept
3413 -- statement, i.e. one not appearing as part of a select alternative.
3415 -- For the case of an accept that appears as part of a select alternative
3416 -- of a selective accept, we must still create the declaration right away,
3417 -- since Ann is needed immediately, but there is an important difference:
3419 -- The declaration is inserted before the selective accept, not before
3420 -- the accept statement (which is not part of a list anyway, and so would
3421 -- not accommodate inserted declarations)
3423 -- We only need one address variable for the entire selective accept. So
3424 -- the Ann declaration is created only for the first accept alternative,
3425 -- and subsequent accept alternatives reference the same Ann variable.
3427 -- We can distinguish the two cases by seeing whether the accept statement
3428 -- is part of a list. If not, then it must be in an accept alternative.
3430 -- To expand the requeue statement, a label is provided at the end of
3431 -- the accept statement or alternative of which it is a part, so that
3432 -- the statement can be skipped after the requeue is complete.
3433 -- This label is created here rather than during the expansion of the
3434 -- accept statement, because it will be needed by any requeue
3435 -- statements within the accept, which are expanded before the
3438 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
3439 Loc : constant Source_Ptr := Sloc (N);
3440 Ann : Entity_Id := Empty;
3448 if Expander_Active then
3450 -- If we have no handled statement sequence, then build a dummy
3451 -- sequence consisting of a null statement. This is only done if
3452 -- pragma FIFO_Within_Priorities is specified. The issue here is
3453 -- that even a null accept body has an effect on the called task
3454 -- in terms of its position in the queue, so we cannot optimize
3455 -- the context switch away. However, if FIFO_Within_Priorities
3456 -- is not active, the optimization is legitimate, since we can
3457 -- say that our dispatching policy (i.e. the default dispatching
3458 -- policy) reorders the queue to be the same as just before the
3459 -- call. In the absence of a specified dispatching policy, we are
3460 -- allowed to modify queue orders for a given priority at will!
3462 if Opt.Task_Dispatching_Policy = 'F' and then
3463 not Present (Handled_Statement_Sequence (N))
3465 Set_Handled_Statement_Sequence (N,
3466 Make_Handled_Sequence_Of_Statements (Loc,
3467 New_List (Make_Null_Statement (Loc))));
3470 -- Create and declare two labels to be placed at the end of the
3471 -- accept statement. The first label is used to allow requeues to
3472 -- skip the remainder of entry processing. The second label is
3473 -- used to skip the remainder of entry processing if the rendezvous
3474 -- completes in the middle of the accept body.
3476 if Present (Handled_Statement_Sequence (N)) then
3477 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
3479 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
3480 Lab := Make_Label (Loc, Lab_Id);
3482 Make_Implicit_Label_Declaration (Loc,
3483 Defining_Identifier => Entity (Lab_Id),
3484 Label_Construct => Lab);
3485 Append (Lab, Statements (Handled_Statement_Sequence (N)));
3487 Lab_Id := Make_Identifier (Loc, New_Internal_Name ('L'));
3489 Make_Defining_Identifier (Loc, Chars (Lab_Id)));
3490 Lab := Make_Label (Loc, Lab_Id);
3492 Make_Implicit_Label_Declaration (Loc,
3493 Defining_Identifier => Entity (Lab_Id),
3494 Label_Construct => Lab);
3495 Append (Lab, Statements (Handled_Statement_Sequence (N)));
3502 -- Case of stand alone accept statement
3504 if Is_List_Member (N) then
3506 if Present (Handled_Statement_Sequence (N)) then
3508 Make_Defining_Identifier (Loc,
3509 Chars => New_Internal_Name ('A'));
3512 Make_Object_Declaration (Loc,
3513 Defining_Identifier => Ann,
3514 Object_Definition =>
3515 New_Reference_To (RTE (RE_Address), Loc));
3517 Insert_Before (N, Adecl);
3520 Insert_Before (N, Ldecl);
3523 Insert_Before (N, Ldecl2);
3527 -- Case of accept statement which is in an accept alternative
3531 Acc_Alt : constant Node_Id := Parent (N);
3532 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
3536 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
3537 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
3539 -- ??? Consider a single label for select statements
3541 if Present (Handled_Statement_Sequence (N)) then
3543 Statements (Handled_Statement_Sequence (N)));
3547 Statements (Handled_Statement_Sequence (N)));
3551 -- Find first accept alternative of the selective accept. A
3552 -- valid selective accept must have at least one accept in it.
3554 Alt := First (Select_Alternatives (Sel_Acc));
3556 while Nkind (Alt) /= N_Accept_Alternative loop
3560 -- If we are the first accept statement, then we have to
3561 -- create the Ann variable, as for the stand alone case,
3562 -- except that it is inserted before the selective accept.
3563 -- Similarly, a label for requeue expansion must be
3566 if N = Accept_Statement (Alt) then
3568 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
3571 Make_Object_Declaration (Loc,
3572 Defining_Identifier => Ann,
3573 Object_Definition =>
3574 New_Reference_To (RTE (RE_Address), Loc));
3576 Insert_Before (Sel_Acc, Adecl);
3579 -- If we are not the first accept statement, then find the
3580 -- Ann variable allocated by the first accept and use it.
3584 Node (Last_Elmt (Accept_Address
3585 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
3590 -- Merge here with Ann either created or referenced, and Adecl
3591 -- pointing to the corresponding declaration. Remaining processing
3592 -- is the same for the two cases.
3594 if Present (Ann) then
3595 Append_Elmt (Ann, Accept_Address (Ent));
3596 Set_Needs_Debug_Info (Ann);
3599 -- Create renaming declarations for the entry formals. Each
3600 -- reference to a formal becomes a dereference of a component
3601 -- of the parameter block, whose address is held in Ann.
3602 -- These declarations are eventually inserted into the accept
3603 -- block, and analyzed there so that they have the proper scope
3604 -- for gdb and do not conflict with other declarations.
3606 if Present (Parameter_Specifications (N))
3607 and then Present (Handled_Statement_Sequence (N))
3617 Formal := First_Formal (Ent);
3619 while Present (Formal) loop
3620 Comp := Entry_Component (Formal);
3622 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
3623 Set_Etype (New_F, Etype (Formal));
3624 Set_Scope (New_F, Ent);
3625 Set_Needs_Debug_Info (New_F); -- That's the whole point.
3627 if Ekind (Formal) = E_In_Parameter then
3628 Set_Ekind (New_F, E_Constant);
3630 Set_Ekind (New_F, E_Variable);
3631 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
3634 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
3637 Make_Object_Renaming_Declaration (Loc,
3638 Defining_Identifier => New_F,
3639 Subtype_Mark => New_Reference_To (Etype (Formal), Loc),
3641 Make_Explicit_Dereference (Loc,
3642 Make_Selected_Component (Loc,
3644 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
3645 New_Reference_To (Ann, Loc)),
3647 New_Reference_To (Comp, Loc))));
3649 if No (Declarations (N)) then
3650 Set_Declarations (N, New_List);
3653 Append (Decl, Declarations (N));
3654 Set_Renamed_Object (Formal, New_F);
3655 Next_Formal (Formal);
3662 end Expand_Accept_Declarations;
3664 ---------------------------------------------
3665 -- Expand_Access_Protected_Subprogram_Type --
3666 ---------------------------------------------
3668 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
3669 Loc : constant Source_Ptr := Sloc (N);
3671 T : constant Entity_Id := Defining_Identifier (N);
3672 D_T : constant Entity_Id := Designated_Type (T);
3673 D_T2 : constant Entity_Id := Make_Defining_Identifier
3674 (Loc, New_Internal_Name ('D'));
3675 E_T : constant Entity_Id := Make_Defining_Identifier
3676 (Loc, New_Internal_Name ('E'));
3677 P_List : constant List_Id := Build_Protected_Spec
3678 (N, RTE (RE_Address), False, D_T);
3684 -- Create access to protected subprogram with full signature
3686 if Nkind (Type_Definition (N)) = N_Access_Function_Definition then
3688 Make_Access_Function_Definition (Loc,
3689 Parameter_Specifications => P_List,
3690 Subtype_Mark => New_Copy (Subtype_Mark (Type_Definition (N))));
3694 Make_Access_Procedure_Definition (Loc,
3695 Parameter_Specifications => P_List);
3699 Make_Full_Type_Declaration (Loc,
3700 Defining_Identifier => D_T2,
3701 Type_Definition => Def1);
3704 Insert_After (N, Decl1);
3706 -- Create Equivalent_Type, a record with two components for an
3707 -- access to object and an access to subprogram.
3710 Make_Component_Declaration (Loc,
3711 Defining_Identifier =>
3712 Make_Defining_Identifier (Loc, New_Internal_Name ('P')),
3713 Component_Definition =>
3714 Make_Component_Definition (Loc,
3715 Aliased_Present => False,
3716 Subtype_Indication =>
3717 New_Occurrence_Of (RTE (RE_Address), Loc))),
3719 Make_Component_Declaration (Loc,
3720 Defining_Identifier =>
3721 Make_Defining_Identifier (Loc, New_Internal_Name ('S')),
3722 Component_Definition =>
3723 Make_Component_Definition (Loc,
3724 Aliased_Present => False,
3725 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
3728 Make_Full_Type_Declaration (Loc,
3729 Defining_Identifier => E_T,
3731 Make_Record_Definition (Loc,
3733 Make_Component_List (Loc,
3734 Component_Items => Comps)));
3737 Insert_After (Decl1, Decl2);
3738 Set_Equivalent_Type (T, E_T);
3739 end Expand_Access_Protected_Subprogram_Type;
3741 --------------------------
3742 -- Expand_Entry_Barrier --
3743 --------------------------
3745 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
3746 Loc : constant Source_Ptr := Sloc (N);
3747 Prot : constant Entity_Id := Scope (Ent);
3748 Spec_Decl : constant Node_Id := Parent (Prot);
3749 Cond : constant Node_Id :=
3750 Condition (Entry_Body_Formal_Part (N));
3753 Body_Decl : Node_Id;
3756 if No_Run_Time_Mode then
3757 Error_Msg_CRT ("entry barrier", N);
3761 -- The body of the entry barrier must be analyzed in the context of
3762 -- the protected object, but its scope is external to it, just as any
3763 -- other unprotected version of a protected operation. The specification
3764 -- has been produced when the protected type declaration was elaborated.
3765 -- We build the body, insert it in the enclosing scope, but analyze it
3766 -- in the current context. A more uniform approach would be to treat a
3767 -- barrier just as a protected function, and discard the protected
3768 -- version of it because it is never called.
3770 if Expander_Active then
3771 B_F := Build_Barrier_Function (N, Ent, Prot);
3772 Func := Barrier_Function (Ent);
3773 Set_Corresponding_Spec (B_F, Func);
3775 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
3777 if Nkind (Parent (Body_Decl)) = N_Subunit then
3778 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
3781 Insert_Before_And_Analyze (Body_Decl, B_F);
3783 Update_Prival_Subtypes (B_F);
3785 Set_Privals (Spec_Decl, N, Loc);
3786 Set_Discriminals (Spec_Decl);
3787 Set_Scope (Func, Scope (Prot));
3790 Analyze_And_Resolve (Cond, Any_Boolean);
3793 -- The Ravenscar profile restricts barriers to simple variables
3794 -- declared within the protected object. We also allow Boolean
3795 -- constants, since these appear in several published examples
3796 -- and are also allowed by the Aonix compiler.
3798 -- Note that after analysis variables in this context will be
3799 -- replaced by the corresponding prival, that is to say a renaming
3800 -- of a selected component of the form _Object.Var. If expansion is
3801 -- disabled, as within a generic, we check that the entity appears in
3802 -- the current scope.
3804 if Is_Entity_Name (Cond) then
3806 if Entity (Cond) = Standard_False
3808 Entity (Cond) = Standard_True
3812 elsif not Expander_Active
3813 and then Scope (Entity (Cond)) = Current_Scope
3817 -- Check for case of _object.all.field (note that the explicit
3818 -- dereference gets inserted by analyze/expand of _object.field)
3820 elsif Present (Renamed_Object (Entity (Cond)))
3822 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
3826 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
3832 -- It is not a boolean variable or literal, so check the restriction
3834 Check_Restriction (Simple_Barriers, Cond);
3835 end Expand_Entry_Barrier;
3837 ------------------------------------
3838 -- Expand_Entry_Body_Declarations --
3839 ------------------------------------
3841 procedure Expand_Entry_Body_Declarations (N : Node_Id) is
3842 Loc : constant Source_Ptr := Sloc (N);
3843 Index_Spec : Node_Id;
3846 if Expander_Active then
3848 -- Expand entry bodies corresponding to entry families
3849 -- by assigning a placeholder for the constant that will
3850 -- be used to expand references to the entry index parameter.
3853 Entry_Index_Specification (Entry_Body_Formal_Part (N));
3855 if Present (Index_Spec) then
3856 Set_Entry_Index_Constant (
3857 Defining_Identifier (Index_Spec),
3858 Make_Defining_Identifier (Loc, New_Internal_Name ('J')));
3861 end Expand_Entry_Body_Declarations;
3863 ------------------------------
3864 -- Expand_N_Abort_Statement --
3865 ------------------------------
3867 -- Expand abort T1, T2, .. Tn; into:
3868 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
3870 procedure Expand_N_Abort_Statement (N : Node_Id) is
3871 Loc : constant Source_Ptr := Sloc (N);
3872 Tlist : constant List_Id := Names (N);
3878 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
3881 Tasknm := First (Tlist);
3883 while Present (Tasknm) loop
3885 Append_To (Component_Associations (Aggr),
3886 Make_Component_Association (Loc,
3887 Choices => New_List (
3888 Make_Integer_Literal (Loc, Count)),
3889 Expression => Concurrent_Ref (Tasknm)));
3894 Make_Procedure_Call_Statement (Loc,
3895 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
3896 Parameter_Associations => New_List (
3897 Make_Qualified_Expression (Loc,
3898 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
3899 Expression => Aggr))));
3902 end Expand_N_Abort_Statement;
3904 -------------------------------
3905 -- Expand_N_Accept_Statement --
3906 -------------------------------
3908 -- This procedure handles expansion of accept statements that stand
3909 -- alone, i.e. they are not part of an accept alternative. The expansion
3910 -- of accept statement in accept alternatives is handled by the routines
3911 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
3912 -- following description applies only to stand alone accept statements.
3914 -- If there is no handled statement sequence, or only null statements,
3915 -- then this is called a trivial accept, and the expansion is:
3917 -- Accept_Trivial (entry-index)
3919 -- If there is a handled statement sequence, then the expansion is:
3926 -- Accept_Call (entry-index, Ann);
3927 -- Renaming_Declarations for formals
3928 -- <statement sequence from N_Accept_Statement node>
3929 -- Complete_Rendezvous;
3934 -- <exception handler from N_Accept_Statement node>
3935 -- Complete_Rendezvous;
3937 -- <exception handler from N_Accept_Statement node>
3938 -- Complete_Rendezvous;
3943 -- when all others =>
3944 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
3947 -- The first three declarations were already inserted ahead of the
3948 -- accept statement by the Expand_Accept_Declarations procedure, which
3949 -- was called directly from the semantics during analysis of the accept.
3950 -- statement, before analyzing its contained statements.
3952 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
3953 -- from possible expansion activity (the original source of course does
3954 -- not have any declarations associated with the accept statement, since
3955 -- an accept statement has no declarative part). In particular, if the
3956 -- expander is active, the first such declaration is the declaration of
3957 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
3959 -- The two blocks are merged into a single block if the inner block has
3960 -- no exception handlers, but otherwise two blocks are required, since
3961 -- exceptions might be raised in the exception handlers of the inner
3962 -- block, and Exceptional_Complete_Rendezvous must be called.
3964 procedure Expand_N_Accept_Statement (N : Node_Id) is
3965 Loc : constant Source_Ptr := Sloc (N);
3966 Stats : constant Node_Id := Handled_Statement_Sequence (N);
3967 Ename : constant Node_Id := Entry_Direct_Name (N);
3968 Eindx : constant Node_Id := Entry_Index (N);
3969 Eent : constant Entity_Id := Entity (Ename);
3970 Acstack : constant Elist_Id := Accept_Address (Eent);
3971 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
3972 Ttyp : constant Entity_Id := Etype (Scope (Eent));
3977 function Null_Statements (Stats : List_Id) return Boolean;
3978 -- Check for null statement sequence (i.e a list of labels and
3981 function Null_Statements (Stats : List_Id) return Boolean is
3985 Stmt := First (Stats);
3986 while Nkind (Stmt) /= N_Empty
3987 and then (Nkind (Stmt) = N_Null_Statement
3989 Nkind (Stmt) = N_Label)
3994 return Nkind (Stmt) = N_Empty;
3995 end Null_Statements;
3997 -- Start of processing for Expand_N_Accept_Statement
4000 -- If accept statement is not part of a list, then its parent must be
4001 -- an accept alternative, and, as described above, we do not do any
4002 -- expansion for such accept statements at this level.
4004 if not Is_List_Member (N) then
4005 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
4008 -- Trivial accept case (no statement sequence, or null statements).
4009 -- If the accept statement has declarations, then just insert them
4010 -- before the procedure call.
4012 -- We avoid this optimization when FIFO_Within_Priorities is active,
4013 -- since it is not correct according to annex D semantics. The problem
4014 -- is that the call is required to reorder the acceptors position on
4015 -- its ready queue, even though there is nothing to be done. However,
4016 -- if no policy is specified, then we decide that our dispatching
4017 -- policy always reorders the queue right after the RV to look the
4018 -- way they were just before the RV. Since we are allowed to freely
4019 -- reorder same-priority queues (this is part of what dispatching
4020 -- policies are all about), the optimization is legitimate.
4022 elsif Opt.Task_Dispatching_Policy /= 'F'
4023 and then (No (Stats) or else Null_Statements (Statements (Stats)))
4025 -- Remove declarations for renamings, because the parameter block
4026 -- will not be assigned.
4033 D := First (Declarations (N));
4035 while Present (D) loop
4037 if Nkind (D) = N_Object_Renaming_Declaration then
4045 if Present (Declarations (N)) then
4046 Insert_Actions (N, Declarations (N));
4050 Make_Procedure_Call_Statement (Loc,
4051 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
4052 Parameter_Associations => New_List (
4053 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
4057 -- Discard Entry_Address that was created for it, so it will not be
4058 -- emitted if this accept statement is in the statement part of a
4059 -- delay alternative.
4061 if Present (Stats) then
4062 Remove_Last_Elmt (Acstack);
4065 -- Case of statement sequence present
4068 -- Construct the block, using the declarations from the accept
4069 -- statement if any to initialize the declarations of the block.
4071 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4072 Set_Ekind (Blkent, E_Block);
4073 Set_Etype (Blkent, Standard_Void_Type);
4074 Set_Scope (Blkent, Current_Scope);
4077 Make_Block_Statement (Loc,
4078 Identifier => New_Reference_To (Blkent, Loc),
4079 Declarations => Declarations (N),
4080 Handled_Statement_Sequence => Build_Accept_Body (N));
4082 -- Prepend call to Accept_Call to main statement sequence
4083 -- If the accept has exception handlers, the statement sequence
4084 -- is wrapped in a block. Insert call and renaming declarations
4085 -- in the declarations of the block, so they are elaborated before
4089 Make_Procedure_Call_Statement (Loc,
4090 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
4091 Parameter_Associations => New_List (
4092 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
4093 New_Reference_To (Ann, Loc)));
4095 if Parent (Stats) = N then
4096 Prepend (Call, Statements (Stats));
4112 D := First (Declarations (N));
4114 while Present (D) loop
4117 if Nkind (D) = N_Object_Renaming_Declaration then
4118 -- The renaming declarations for the formals were
4119 -- created during analysis of the accept statement,
4120 -- and attached to the list of declarations. Place
4121 -- them now in the context of the accept block or
4125 Typ := Entity (Subtype_Mark (D));
4126 Insert_After (Call, D);
4129 -- If the formal is class_wide, it does not have an
4130 -- actual subtype. The analysis of the renaming declaration
4131 -- creates one, but we need to retain the class-wide
4132 -- nature of the entity.
4134 if Is_Class_Wide_Type (Typ) then
4135 Set_Etype (Defining_Identifier (D), Typ);
4146 -- Replace the accept statement by the new block
4151 -- Last step is to unstack the Accept_Address value
4153 Remove_Last_Elmt (Acstack);
4155 end Expand_N_Accept_Statement;
4157 ----------------------------------
4158 -- Expand_N_Asynchronous_Select --
4159 ----------------------------------
4161 -- This procedure assumes that the trigger statement is an entry call. A
4162 -- delay alternative should already have been expanded into an entry call
4163 -- to the appropriate delay object Wait entry.
4165 -- If the trigger is a task entry call, the select is implemented with
4166 -- a Task_Entry_Call:
4171 -- P : parms := (parm, parm, parm);
4173 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
4175 -- procedure _clean is
4178 -- Cancel_Task_Entry_Call (C);
4188 -- Asynchronous_Call,
4196 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions.
4200 -- when Abort_Signal => Abort_Undefer;
4206 -- triggered-statements
4210 -- Note that Build_Simple_Entry_Call is used to expand the entry
4211 -- of the asynchronous entry call (by the
4212 -- Expand_N_Entry_Call_Statement procedure) as follows:
4215 -- P : parms := (parm, parm, parm);
4217 -- Call_Simple (acceptor-task, entry-index, P'Address);
4223 -- so the task at hand is to convert the latter expansion into the former
4225 -- If the trigger is a protected entry call, the select is
4226 -- implemented with Protected_Entry_Call:
4229 -- P : E1_Params := (param, param, param);
4230 -- Bnn : Communications_Block;
4234 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions.
4235 -- procedure _clean is
4238 -- if Enqueued (Bnn) then
4239 -- Cancel_Protected_Entry_Call (Bnn);
4246 -- Protected_Entry_Call (
4247 -- Object => po._object'Access,
4248 -- E => <entry index>;
4249 -- Uninterpreted_Data => P'Address;
4250 -- Mode => Asynchronous_Call;
4252 -- if Enqueued (Bnn) then
4256 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions.
4260 -- when Abort_Signal =>
4265 -- if not Cancelled (Bnn) then
4266 -- triggered statements
4270 -- Build_Simple_Entry_Call is used to expand the all to a simple
4271 -- protected entry call:
4274 -- P : E1_Params := (param, param, param);
4275 -- Bnn : Communications_Block;
4278 -- Protected_Entry_Call (
4279 -- Object => po._object'Access,
4280 -- E => <entry index>;
4281 -- Uninterpreted_Data => P'Address;
4282 -- Mode => Simple_Call;
4289 -- The job is to convert this to the asynchronous form
4291 -- If the trigger is a delay statement, it will have been expanded into a
4292 -- call to one of the GNARL delay procedures. This routine will convert
4293 -- this into a protected entry call on a delay object and then continue
4294 -- processing as for a protected entry call trigger. This requires
4295 -- declaring a Delay_Block object and adding a pointer to this object to
4296 -- the parameter list of the delay procedure to form the parameter list of
4297 -- the entry call. This object is used by the runtime to queue the delay
4300 -- For a description of the use of P and the assignments after the
4301 -- call, see Expand_N_Entry_Call_Statement.
4303 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
4304 Loc : constant Source_Ptr := Sloc (N);
4305 Trig : constant Node_Id := Triggering_Alternative (N);
4306 Abrt : constant Node_Id := Abortable_Part (N);
4307 Tstats : constant List_Id := Statements (Trig);
4308 Astats : constant List_Id := Statements (Abrt);
4321 Enqueue_Call : Node_Id;
4325 Dblock_Ent : Entity_Id;
4327 Abortable_Block : Node_Id;
4328 Cancel_Param : Entity_Id;
4330 Target_Undefer : RE_Id;
4331 Undefer_Args : List_Id := No_List;
4334 Blkent := Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
4335 Ecall := Triggering_Statement (Trig);
4337 -- The arguments in the call may require dynamic allocation, and the
4338 -- call statement may have been transformed into a block. The block
4339 -- may contain additional declarations for internal entities, and the
4340 -- original call is found by sequential search.
4342 if Nkind (Ecall) = N_Block_Statement then
4343 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
4345 while Nkind (Ecall) /= N_Procedure_Call_Statement
4346 and then Nkind (Ecall) /= N_Entry_Call_Statement
4352 -- If a delay was used as a trigger, it will have been expanded
4353 -- into a procedure call. Convert it to the appropriate sequence of
4354 -- statements, similar to what is done for a task entry call.
4355 -- Note that this currently supports only Duration, Real_Time.Time,
4356 -- and Calendar.Time.
4358 if Nkind (Ecall) = N_Procedure_Call_Statement then
4360 -- Add a Delay_Block object to the parameter list of the
4361 -- delay procedure to form the parameter list of the Wait
4364 Dblock_Ent := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
4366 Pdef := Entity (Name (Ecall));
4368 if Is_RTE (Pdef, RO_CA_Delay_For) then
4369 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
4371 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
4372 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
4374 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
4375 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
4378 Append_To (Parameter_Associations (Ecall),
4379 Make_Attribute_Reference (Loc,
4380 Prefix => New_Reference_To (Dblock_Ent, Loc),
4381 Attribute_Name => Name_Unchecked_Access));
4383 -- Create the inner block to protect the abortable part
4386 Make_Exception_Handler (Loc,
4387 Exception_Choices =>
4388 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
4389 Statements => New_List (
4390 Make_Procedure_Call_Statement (Loc,
4391 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)))));
4394 Make_Procedure_Call_Statement (Loc,
4395 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
4398 Make_Block_Statement (Loc,
4399 Identifier => New_Reference_To (Blkent, Loc),
4400 Handled_Statement_Sequence =>
4401 Make_Handled_Sequence_Of_Statements (Loc,
4402 Statements => Astats),
4403 Has_Created_Identifier => True,
4404 Is_Asynchronous_Call_Block => True);
4406 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
4409 Make_Implicit_If_Statement (N,
4410 Condition => Make_Function_Call (Loc,
4411 Name => Enqueue_Call,
4412 Parameter_Associations => Parameter_Associations (Ecall)),
4414 New_List (Make_Block_Statement (Loc,
4415 Handled_Statement_Sequence =>
4416 Make_Handled_Sequence_Of_Statements (Loc,
4417 Statements => New_List (
4418 Make_Implicit_Label_Declaration (Loc,
4419 Defining_Identifier => Blkent,
4420 Label_Construct => Abortable_Block),
4422 Exception_Handlers => Hdle)))));
4424 Stmts := New_List (Ecall);
4426 -- Construct statement sequence for new block
4429 Make_Implicit_If_Statement (N,
4430 Condition => Make_Function_Call (Loc,
4431 Name => New_Reference_To (
4432 RTE (RE_Timed_Out), Loc),
4433 Parameter_Associations => New_List (
4434 Make_Attribute_Reference (Loc,
4435 Prefix => New_Reference_To (Dblock_Ent, Loc),
4436 Attribute_Name => Name_Unchecked_Access))),
4437 Then_Statements => Tstats));
4439 -- The result is the new block
4441 Set_Entry_Cancel_Parameter (Blkent, Dblock_Ent);
4444 Make_Block_Statement (Loc,
4445 Declarations => New_List (
4446 Make_Object_Declaration (Loc,
4447 Defining_Identifier => Dblock_Ent,
4448 Aliased_Present => True,
4449 Object_Definition => New_Reference_To (
4450 RTE (RE_Delay_Block), Loc))),
4452 Handled_Statement_Sequence =>
4453 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
4462 Extract_Entry (Ecall, Concval, Ename, Index);
4463 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
4465 Stmts := Statements (Handled_Statement_Sequence (Ecall));
4466 Decls := Declarations (Ecall);
4468 if Is_Protected_Type (Etype (Concval)) then
4470 -- Get the declarations of the block expanded from the entry call
4472 Decl := First (Decls);
4473 while Present (Decl)
4474 and then (Nkind (Decl) /= N_Object_Declaration
4476 (Etype (Object_Definition (Decl)), RE_Communication_Block))
4481 pragma Assert (Present (Decl));
4482 Cancel_Param := Defining_Identifier (Decl);
4484 -- Change the mode of the Protected_Entry_Call call.
4485 -- Protected_Entry_Call (
4486 -- Object => po._object'Access,
4487 -- E => <entry index>;
4488 -- Uninterpreted_Data => P'Address;
4489 -- Mode => Asynchronous_Call;
4492 Stmt := First (Stmts);
4494 -- Skip assignments to temporaries created for in-out parameters.
4495 -- This makes unwarranted assumptions about the shape of the expanded
4496 -- tree for the call, and should be cleaned up ???
4498 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
4504 Parm := First (Parameter_Associations (Call));
4505 while Present (Parm)
4506 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
4511 pragma Assert (Present (Parm));
4512 Rewrite (Parm, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
4515 -- Append an if statement to execute the abortable part.
4516 -- if Enqueued (Bnn) then
4519 Make_Implicit_If_Statement (N,
4520 Condition => Make_Function_Call (Loc,
4521 Name => New_Reference_To (
4522 RTE (RE_Enqueued), Loc),
4523 Parameter_Associations => New_List (
4524 New_Reference_To (Cancel_Param, Loc))),
4525 Then_Statements => Astats));
4528 Make_Block_Statement (Loc,
4529 Identifier => New_Reference_To (Blkent, Loc),
4530 Handled_Statement_Sequence =>
4531 Make_Handled_Sequence_Of_Statements (Loc,
4532 Statements => Stmts),
4533 Has_Created_Identifier => True,
4534 Is_Asynchronous_Call_Block => True);
4536 -- For the JVM call Update_Exception instead of Abort_Undefer.
4537 -- See 4jexcept.ads for an explanation.
4539 if Hostparm.Java_VM then
4540 Target_Undefer := RE_Update_Exception;
4542 New_List (Make_Function_Call (Loc,
4543 Name => New_Occurrence_Of
4544 (RTE (RE_Current_Target_Exception), Loc)));
4546 Target_Undefer := RE_Abort_Undefer;
4550 Make_Block_Statement (Loc,
4551 Handled_Statement_Sequence =>
4552 Make_Handled_Sequence_Of_Statements (Loc,
4553 Statements => New_List (
4554 Make_Implicit_Label_Declaration (Loc,
4555 Defining_Identifier => Blkent,
4556 Label_Construct => Abortable_Block),
4561 Exception_Handlers => New_List (
4562 Make_Exception_Handler (Loc,
4564 -- when Abort_Signal =>
4565 -- Abort_Undefer.all;
4567 Exception_Choices =>
4568 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
4569 Statements => New_List (
4570 Make_Procedure_Call_Statement (Loc,
4571 Name => New_Reference_To (
4572 RTE (Target_Undefer), Loc),
4573 Parameter_Associations => Undefer_Args)))))),
4575 -- if not Cancelled (Bnn) then
4576 -- triggered statements
4579 Make_Implicit_If_Statement (N,
4580 Condition => Make_Op_Not (Loc,
4582 Make_Function_Call (Loc,
4583 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
4584 Parameter_Associations => New_List (
4585 New_Occurrence_Of (Cancel_Param, Loc)))),
4586 Then_Statements => Tstats));
4588 -- Asynchronous task entry call
4595 B := Make_Defining_Identifier (Loc, Name_uB);
4597 -- Insert declaration of B in declarations of existing block
4600 Make_Object_Declaration (Loc,
4601 Defining_Identifier => B,
4602 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
4604 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
4606 -- Insert declaration of C in declarations of existing block
4609 Make_Object_Declaration (Loc,
4610 Defining_Identifier => Cancel_Param,
4611 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
4613 -- Remove and save the call to Call_Simple
4615 Stmt := First (Stmts);
4617 -- Skip assignments to temporaries created for in-out parameters.
4618 -- This makes unwarranted assumptions about the shape of the expanded
4619 -- tree for the call, and should be cleaned up ???
4621 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
4627 -- Create the inner block to protect the abortable part
4630 Make_Exception_Handler (Loc,
4631 Exception_Choices =>
4632 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
4633 Statements => New_List (
4634 Make_Procedure_Call_Statement (Loc,
4635 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)))));
4638 Make_Procedure_Call_Statement (Loc,
4639 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
4642 Make_Block_Statement (Loc,
4643 Identifier => New_Reference_To (Blkent, Loc),
4644 Handled_Statement_Sequence =>
4645 Make_Handled_Sequence_Of_Statements (Loc,
4646 Statements => Astats),
4647 Has_Created_Identifier => True,
4648 Is_Asynchronous_Call_Block => True);
4651 Make_Block_Statement (Loc,
4652 Handled_Statement_Sequence =>
4653 Make_Handled_Sequence_Of_Statements (Loc,
4654 Statements => New_List (
4655 Make_Implicit_Label_Declaration (Loc,
4656 Defining_Identifier => Blkent,
4657 Label_Construct => Abortable_Block),
4659 Exception_Handlers => Hdle)));
4661 -- Create new call statement
4663 Parms := Parameter_Associations (Call);
4664 Append_To (Parms, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
4665 Append_To (Parms, New_Reference_To (B, Loc));
4667 Make_Procedure_Call_Statement (Loc,
4668 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
4669 Parameter_Associations => Parms));
4671 -- Construct statement sequence for new block
4674 Make_Implicit_If_Statement (N,
4675 Condition => Make_Op_Not (Loc,
4676 New_Reference_To (Cancel_Param, Loc)),
4677 Then_Statements => Tstats));
4679 -- Protected the call against abort
4682 Make_Procedure_Call_Statement (Loc,
4683 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
4684 Parameter_Associations => Empty_List));
4687 Set_Entry_Cancel_Parameter (Blkent, Cancel_Param);
4689 -- The result is the new block
4692 Make_Block_Statement (Loc,
4693 Declarations => Decls,
4694 Handled_Statement_Sequence =>
4695 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
4698 end Expand_N_Asynchronous_Select;
4700 -------------------------------------
4701 -- Expand_N_Conditional_Entry_Call --
4702 -------------------------------------
4704 -- The conditional task entry call is converted to a call to
4709 -- P : parms := (parm, parm, parm);
4716 -- Conditional_Call,
4722 -- normal-statements
4728 -- For a description of the use of P and the assignments after the
4729 -- call, see Expand_N_Entry_Call_Statement. Note that the entry call
4730 -- of the conditional entry call has already been expanded (by the
4731 -- Expand_N_Entry_Call_Statement procedure) as follows:
4734 -- P : parms := (parm, parm, parm);
4736 -- ... info for in-out parameters
4737 -- Call_Simple (acceptor-task, entry-index, P'Address);
4743 -- so the task at hand is to convert the latter expansion into the former
4745 -- The conditional protected entry call is converted to a call to
4746 -- Protected_Entry_Call:
4749 -- P : parms := (parm, parm, parm);
4750 -- Bnn : Communications_Block;
4753 -- Protected_Entry_Call (
4754 -- Object => po._object'Access,
4755 -- E => <entry index>;
4756 -- Uninterpreted_Data => P'Address;
4757 -- Mode => Conditional_Call;
4762 -- if Cancelled (Bnn) then
4765 -- normal-statements
4769 -- As for tasks, the entry call of the conditional entry call has
4770 -- already been expanded (by the Expand_N_Entry_Call_Statement procedure)
4774 -- P : E1_Params := (param, param, param);
4775 -- Bnn : Communications_Block;
4778 -- Protected_Entry_Call (
4779 -- Object => po._object'Access,
4780 -- E => <entry index>;
4781 -- Uninterpreted_Data => P'Address;
4782 -- Mode => Simple_Call;
4789 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
4790 Loc : constant Source_Ptr := Sloc (N);
4791 Alt : constant Node_Id := Entry_Call_Alternative (N);
4792 Blk : Node_Id := Entry_Call_Statement (Alt);
4793 Transient_Blk : Node_Id;
4804 -- As described above, The entry alternative is transformed into a
4805 -- block that contains the gnulli call, and possibly assignment
4806 -- statements for in-out parameters. The gnulli call may itself be
4807 -- rewritten into a transient block if some unconstrained parameters
4808 -- require it. We need to retrieve the call to complete its parameter
4812 First_Real_Statement (Handled_Statement_Sequence (Blk));
4814 if Present (Transient_Blk)
4816 Nkind (Transient_Blk) = N_Block_Statement
4818 Blk := Transient_Blk;
4821 Stmts := Statements (Handled_Statement_Sequence (Blk));
4823 Stmt := First (Stmts);
4825 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
4831 Parms := Parameter_Associations (Call);
4833 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
4835 -- Substitute Conditional_Entry_Call for Simple_Call
4838 Parm := First (Parms);
4839 while Present (Parm)
4840 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
4845 pragma Assert (Present (Parm));
4846 Rewrite (Parm, New_Reference_To (RTE (RE_Conditional_Call), Loc));
4850 -- Find the Communication_Block parameter for the call
4851 -- to the Cancelled function.
4853 Decl := First (Declarations (Blk));
4854 while Present (Decl)
4856 Is_RTE (Etype (Object_Definition (Decl)), RE_Communication_Block)
4861 -- Add an if statement to execute the else part if the call
4862 -- does not succeed (as indicated by the Cancelled predicate).
4865 Make_Implicit_If_Statement (N,
4866 Condition => Make_Function_Call (Loc,
4867 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
4868 Parameter_Associations => New_List (
4869 New_Reference_To (Defining_Identifier (Decl), Loc))),
4870 Then_Statements => Else_Statements (N),
4871 Else_Statements => Statements (Alt)));
4874 B := Make_Defining_Identifier (Loc, Name_uB);
4876 -- Insert declaration of B in declarations of existing block
4878 if No (Declarations (Blk)) then
4879 Set_Declarations (Blk, New_List);
4882 Prepend_To (Declarations (Blk),
4883 Make_Object_Declaration (Loc,
4884 Defining_Identifier => B,
4885 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
4887 -- Create new call statement
4889 Append_To (Parms, New_Reference_To (RTE (RE_Conditional_Call), Loc));
4890 Append_To (Parms, New_Reference_To (B, Loc));
4893 Make_Procedure_Call_Statement (Loc,
4894 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
4895 Parameter_Associations => Parms));
4897 -- Construct statement sequence for new block
4900 Make_Implicit_If_Statement (N,
4901 Condition => New_Reference_To (B, Loc),
4902 Then_Statements => Statements (Alt),
4903 Else_Statements => Else_Statements (N)));
4907 -- The result is the new block
4910 Make_Block_Statement (Loc,
4911 Declarations => Declarations (Blk),
4912 Handled_Statement_Sequence =>
4913 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
4916 end Expand_N_Conditional_Entry_Call;
4918 ---------------------------------------
4919 -- Expand_N_Delay_Relative_Statement --
4920 ---------------------------------------
4922 -- Delay statement is implemented as a procedure call to Delay_For
4923 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
4924 -- simple delays imposed by the use of Protected Objects.
4926 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
4927 Loc : constant Source_Ptr := Sloc (N);
4931 Make_Procedure_Call_Statement (Loc,
4932 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
4933 Parameter_Associations => New_List (Expression (N))));
4935 end Expand_N_Delay_Relative_Statement;
4937 ------------------------------------
4938 -- Expand_N_Delay_Until_Statement --
4939 ------------------------------------
4941 -- Delay Until statement is implemented as a procedure call to
4942 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
4944 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
4945 Loc : constant Source_Ptr := Sloc (N);
4949 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
4950 Typ := RTE (RO_CA_Delay_Until);
4952 Typ := RTE (RO_RT_Delay_Until);
4956 Make_Procedure_Call_Statement (Loc,
4957 Name => New_Reference_To (Typ, Loc),
4958 Parameter_Associations => New_List (Expression (N))));
4961 end Expand_N_Delay_Until_Statement;
4963 -------------------------
4964 -- Expand_N_Entry_Body --
4965 -------------------------
4967 procedure Expand_N_Entry_Body (N : Node_Id) is
4968 Loc : constant Source_Ptr := Sloc (N);
4969 Dec : constant Node_Id := Parent (Current_Scope);
4970 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
4971 Index_Spec : constant Node_Id :=
4972 Entry_Index_Specification (Ent_Formals);
4974 First_Decl : constant Node_Id := First (Declarations (N));
4975 Index_Decl : List_Id;
4978 -- Add the renamings for private declarations and discriminants
4980 Add_Discriminal_Declarations
4981 (Declarations (N), Defining_Identifier (Dec), Name_uObject, Loc);
4982 Add_Private_Declarations
4983 (Declarations (N), Defining_Identifier (Dec), Name_uObject, Loc);
4985 if Present (Index_Spec) then
4987 Index_Constant_Declaration
4989 Defining_Identifier (Index_Spec), Defining_Identifier (Dec));
4991 -- If the entry has local declarations, insert index declaration
4992 -- before them, because the index may be used therein.
4994 if Present (First_Decl) then
4995 Insert_List_Before (First_Decl, Index_Decl);
4997 Append_List_To (Declarations (N), Index_Decl);
5001 -- Associate privals and discriminals with the next protected
5002 -- operation body to be expanded. These are used to expand
5003 -- references to private data objects and discriminants,
5006 Next_Op := Next_Protected_Operation (N);
5008 if Present (Next_Op) then
5009 Set_Privals (Dec, Next_Op, Loc);
5010 Set_Discriminals (Dec);
5012 end Expand_N_Entry_Body;
5014 -----------------------------------
5015 -- Expand_N_Entry_Call_Statement --
5016 -----------------------------------
5018 -- An entry call is expanded into GNARLI calls to implement
5019 -- a simple entry call (see Build_Simple_Entry_Call).
5021 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
5027 if No_Run_Time_Mode then
5028 Error_Msg_CRT ("entry call", N);
5032 -- If this entry call is part of an asynchronous select, don't
5033 -- expand it here; it will be expanded with the select statement.
5034 -- Don't expand timed entry calls either, as they are translated
5035 -- into asynchronous entry calls.
5037 -- ??? This whole approach is questionable; it may be better
5038 -- to go back to allowing the expansion to take place and then
5039 -- attempting to fix it up in Expand_N_Asynchronous_Select.
5040 -- The tricky part is figuring out whether the expanded
5041 -- call is on a task or protected entry.
5043 if (Nkind (Parent (N)) /= N_Triggering_Alternative
5044 or else N /= Triggering_Statement (Parent (N)))
5045 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
5046 or else N /= Entry_Call_Statement (Parent (N))
5047 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
5049 Extract_Entry (N, Concval, Ename, Index);
5050 Build_Simple_Entry_Call (N, Concval, Ename, Index);
5052 end Expand_N_Entry_Call_Statement;
5054 --------------------------------
5055 -- Expand_N_Entry_Declaration --
5056 --------------------------------
5058 -- If there are parameters, then first, each of the formals is marked
5059 -- by setting Is_Entry_Formal. Next a record type is built which is
5060 -- used to hold the parameter values. The name of this record type is
5061 -- entryP where entry is the name of the entry, with an additional
5062 -- corresponding access type called entryPA. The record type has matching
5063 -- components for each formal (the component names are the same as the
5064 -- formal names). For elementary types, the component type matches the
5065 -- formal type. For composite types, an access type is declared (with
5066 -- the name formalA) which designates the formal type, and the type of
5067 -- the component is this access type. Finally the Entry_Component of
5068 -- each formal is set to reference the corresponding record component.
5070 procedure Expand_N_Entry_Declaration (N : Node_Id) is
5071 Loc : constant Source_Ptr := Sloc (N);
5072 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
5073 Components : List_Id;
5076 Last_Decl : Node_Id;
5077 Component : Entity_Id;
5080 Rec_Ent : Entity_Id;
5081 Acc_Ent : Entity_Id;
5084 Formal := First_Formal (Entry_Ent);
5087 -- Most processing is done only if parameters are present
5089 if Present (Formal) then
5090 Components := New_List;
5092 -- Loop through formals
5094 while Present (Formal) loop
5095 Set_Is_Entry_Formal (Formal);
5097 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
5098 Set_Entry_Component (Formal, Component);
5099 Set_Entry_Formal (Component, Formal);
5100 Ftype := Etype (Formal);
5102 -- Declare new access type and then append
5105 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5108 Make_Full_Type_Declaration (Loc,
5109 Defining_Identifier => Ctype,
5111 Make_Access_To_Object_Definition (Loc,
5112 All_Present => True,
5113 Constant_Present => Ekind (Formal) = E_In_Parameter,
5114 Subtype_Indication => New_Reference_To (Ftype, Loc)));
5116 Insert_After (Last_Decl, Decl);
5119 Append_To (Components,
5120 Make_Component_Declaration (Loc,
5121 Defining_Identifier => Component,
5122 Component_Definition =>
5123 Make_Component_Definition (Loc,
5124 Aliased_Present => False,
5125 Subtype_Indication => New_Reference_To (Ctype, Loc))));
5127 Next_Formal_With_Extras (Formal);
5130 -- Create the Entry_Parameter_Record declaration
5133 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
5136 Make_Full_Type_Declaration (Loc,
5137 Defining_Identifier => Rec_Ent,
5139 Make_Record_Definition (Loc,
5141 Make_Component_List (Loc,
5142 Component_Items => Components)));
5144 Insert_After (Last_Decl, Decl);
5147 -- Construct and link in the corresponding access type
5150 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
5152 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
5155 Make_Full_Type_Declaration (Loc,
5156 Defining_Identifier => Acc_Ent,
5158 Make_Access_To_Object_Definition (Loc,
5159 All_Present => True,
5160 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
5162 Insert_After (Last_Decl, Decl);
5165 end Expand_N_Entry_Declaration;
5167 -----------------------------
5168 -- Expand_N_Protected_Body --
5169 -----------------------------
5171 -- Protected bodies are expanded to the completion of the subprograms
5172 -- created for the corresponding protected type. These are a protected
5173 -- and unprotected version of each protected subprogram in the object,
5174 -- a function to calculate each entry barrier, and a procedure to
5175 -- execute the sequence of statements of each protected entry body.
5176 -- For example, for protected type ptype:
5179 -- (O : System.Address;
5180 -- E : Protected_Entry_Index)
5183 -- <discriminant renamings>
5184 -- <private object renamings>
5186 -- return <barrier expression>;
5189 -- procedure pprocN (_object : in out poV;...) is
5190 -- <discriminant renamings>
5191 -- <private object renamings>
5193 -- <sequence of statements>
5196 -- procedure pproc (_object : in out poV;...) is
5197 -- procedure _clean is
5200 -- ptypeS (_object, Pn);
5201 -- Unlock (_object._object'Access);
5202 -- Abort_Undefer.all;
5207 -- Lock (_object._object'Access);
5208 -- pprocN (_object;...);
5213 -- function pfuncN (_object : poV;...) return Return_Type is
5214 -- <discriminant renamings>
5215 -- <private object renamings>
5217 -- <sequence of statements>
5220 -- function pfunc (_object : poV) return Return_Type is
5221 -- procedure _clean is
5223 -- Unlock (_object._object'Access);
5224 -- Abort_Undefer.all;
5229 -- Lock (_object._object'Access);
5230 -- return pfuncN (_object);
5237 -- (O : System.Address;
5238 -- P : System.Address;
5239 -- E : Protected_Entry_Index)
5241 -- <discriminant renamings>
5242 -- <private object renamings>
5243 -- type poVP is access poV;
5244 -- _Object : ptVP := ptVP!(O);
5248 -- <statement sequence>
5249 -- Complete_Entry_Body (_Object._Object);
5251 -- when all others =>
5252 -- Exceptional_Complete_Entry_Body (
5253 -- _Object._Object, Get_GNAT_Exception);
5257 -- The type poV is the record created for the protected type to hold
5258 -- the state of the protected object.
5260 procedure Expand_N_Protected_Body (N : Node_Id) is
5261 Loc : constant Source_Ptr := Sloc (N);
5262 Pid : constant Entity_Id := Corresponding_Spec (N);
5263 Has_Entries : Boolean := False;
5267 New_Op_Body : Node_Id;
5268 Current_Node : Node_Id;
5269 Num_Entries : Natural := 0;
5272 if No_Run_Time_Mode then
5273 Error_Msg_CRT ("protected body", N);
5277 if Nkind (Parent (N)) = N_Subunit then
5279 -- This is the proper body corresponding to a stub. The declarations
5280 -- must be inserted at the point of the stub, which is in the decla-
5281 -- rative part of the parent unit.
5283 Current_Node := Corresponding_Stub (Parent (N));
5289 Op_Body := First (Declarations (N));
5291 -- The protected body is replaced with the bodies of its
5292 -- protected operations, and the declarations for internal objects
5293 -- that may have been created for entry family bounds.
5295 Rewrite (N, Make_Null_Statement (Sloc (N)));
5298 while Present (Op_Body) loop
5299 case Nkind (Op_Body) is
5300 when N_Subprogram_Declaration =>
5303 when N_Subprogram_Body =>
5305 -- Exclude functions created to analyze defaults
5307 if not Is_Eliminated (Defining_Entity (Op_Body))
5308 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
5311 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
5313 Insert_After (Current_Node, New_Op_Body);
5314 Current_Node := New_Op_Body;
5315 Analyze (New_Op_Body);
5317 Update_Prival_Subtypes (New_Op_Body);
5319 -- Build the corresponding protected operation only if
5320 -- this is a visible operation of the type, or if it is
5321 -- an interrupt handler. Otherwise it is only callable
5322 -- from within the object, and the unprotected version
5325 if Present (Corresponding_Spec (Op_Body)) then
5327 Unit_Declaration_Node (Corresponding_Spec (Op_Body));
5329 if Nkind (Parent (Op_Decl)) = N_Protected_Definition
5331 (List_Containing (Op_Decl) =
5332 Visible_Declarations (Parent (Op_Decl))
5334 Is_Interrupt_Handler
5335 (Corresponding_Spec (Op_Body)))
5338 Build_Protected_Subprogram_Body (
5339 Op_Body, Pid, Specification (New_Op_Body));
5341 Insert_After (Current_Node, New_Op_Body);
5342 Analyze (New_Op_Body);
5347 when N_Entry_Body =>
5348 Op_Id := Defining_Identifier (Op_Body);
5349 Has_Entries := True;
5350 Num_Entries := Num_Entries + 1;
5352 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
5354 Insert_After (Current_Node, New_Op_Body);
5355 Current_Node := New_Op_Body;
5356 Analyze (New_Op_Body);
5358 Update_Prival_Subtypes (New_Op_Body);
5360 when N_Implicit_Label_Declaration =>
5363 when N_Itype_Reference =>
5364 Insert_After (Current_Node, New_Copy (Op_Body));
5366 when N_Freeze_Entity =>
5367 New_Op_Body := New_Copy (Op_Body);
5369 if Present (Entity (Op_Body))
5370 and then Freeze_Node (Entity (Op_Body)) = Op_Body
5372 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
5375 Insert_After (Current_Node, New_Op_Body);
5376 Current_Node := New_Op_Body;
5377 Analyze (New_Op_Body);
5380 New_Op_Body := New_Copy (Op_Body);
5381 Insert_After (Current_Node, New_Op_Body);
5382 Current_Node := New_Op_Body;
5383 Analyze (New_Op_Body);
5385 when N_Object_Declaration =>
5386 pragma Assert (not Comes_From_Source (Op_Body));
5387 New_Op_Body := New_Copy (Op_Body);
5388 Insert_After (Current_Node, New_Op_Body);
5389 Current_Node := New_Op_Body;
5390 Analyze (New_Op_Body);
5393 raise Program_Error;
5400 -- Finally, create the body of the function that maps an entry index
5401 -- into the corresponding body index, except when there is no entry,
5402 -- or in a ravenscar-like profile (no abort, no entry queue, 1 entry)
5405 and then (Abort_Allowed
5406 or else Restriction_Active (No_Entry_Queue) = False
5407 or else Num_Entries > 1)
5409 New_Op_Body := Build_Find_Body_Index (Pid);
5410 Insert_After (Current_Node, New_Op_Body);
5411 Current_Node := New_Op_Body;
5412 Analyze (New_Op_Body);
5415 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies
5416 -- after the protected body. At this point the entry specs have been
5417 -- created, frozen and included in the dispatch table for the
5420 pragma Assert (Present (Corresponding_Record_Type (Pid)));
5422 if Ada_Version >= Ada_05
5423 and then Present (Protected_Definition (Parent (Pid)))
5424 and then Present (Abstract_Interfaces
5425 (Corresponding_Record_Type (Pid)))
5428 Vis_Decl : Node_Id :=
5429 First (Visible_Declarations
5430 (Protected_Definition (Parent (Pid))));
5431 Wrap_Body : Node_Id;
5434 -- Examine the visible declarations of the protected type,
5435 -- looking for an entry declaration. We do not consider
5436 -- entry families since they can not have dispatching
5437 -- operations, thus they do not need entry wrappers.
5439 while Present (Vis_Decl) loop
5440 if Nkind (Vis_Decl) = N_Entry_Declaration then
5442 Build_Wrapper_Body (Loc,
5443 Proc_Nam => Defining_Identifier (Vis_Decl),
5444 Obj_Typ => Corresponding_Record_Type (Pid),
5445 Formals => Parameter_Specifications (Vis_Decl));
5447 if Wrap_Body /= Empty then
5448 Insert_After (Current_Node, Wrap_Body);
5449 Current_Node := Wrap_Body;
5451 Analyze (Wrap_Body);
5454 elsif Nkind (Vis_Decl) = N_Subprogram_Declaration then
5456 Build_Wrapper_Body (Loc,
5457 Proc_Nam => Defining_Unit_Name
5458 (Specification (Vis_Decl)),
5459 Obj_Typ => Corresponding_Record_Type (Pid),
5460 Formals => Parameter_Specifications
5461 (Specification (Vis_Decl)));
5463 if Wrap_Body /= Empty then
5464 Insert_After (Current_Node, Wrap_Body);
5465 Current_Node := Wrap_Body;
5467 Analyze (Wrap_Body);
5475 end Expand_N_Protected_Body;
5477 -----------------------------------------
5478 -- Expand_N_Protected_Type_Declaration --
5479 -----------------------------------------
5481 -- First we create a corresponding record type declaration used to
5482 -- represent values of this protected type.
5483 -- The general form of this type declaration is
5485 -- type poV (discriminants) is record
5486 -- _Object : aliased <kind>Protection
5487 -- [(<entry count> [, <handler count>])];
5488 -- [entry_family : array (bounds) of Void;]
5489 -- <private data fields>
5492 -- The discriminants are present only if the corresponding protected
5493 -- type has discriminants, and they exactly mirror the protected type
5494 -- discriminants. The private data fields similarly mirror the
5495 -- private declarations of the protected type.
5497 -- The Object field is always present. It contains RTS specific data
5498 -- used to control the protected object. It is declared as Aliased
5499 -- so that it can be passed as a pointer to the RTS. This allows the
5500 -- protected record to be referenced within RTS data structures.
5501 -- An appropriate Protection type and discriminant are generated.
5503 -- The Service field is present for protected objects with entries. It
5504 -- contains sufficient information to allow the entry service procedure
5505 -- for this object to be called when the object is not known till runtime.
5507 -- One entry_family component is present for each entry family in the
5508 -- task definition (see Expand_N_Task_Type_Declaration).
5510 -- When a protected object is declared, an instance of the protected type
5511 -- value record is created. The elaboration of this declaration creates
5512 -- the correct bounds for the entry families, and also evaluates the
5513 -- priority expression if needed. The initialization routine for
5514 -- the protected type itself then calls Initialize_Protection with
5515 -- appropriate parameters to initialize the value of the Task_Id field.
5516 -- Install_Handlers may be also called if a pragma Attach_Handler applies.
5518 -- Note: this record is passed to the subprograms created by the
5519 -- expansion of protected subprograms and entries. It is an in parameter
5520 -- to protected functions and an in out parameter to procedures and
5521 -- entry bodies. The Entity_Id for this created record type is placed
5522 -- in the Corresponding_Record_Type field of the associated protected
5525 -- Next we create a procedure specifications for protected subprograms
5526 -- and entry bodies. For each protected subprograms two subprograms are
5527 -- created, an unprotected and a protected version. The unprotected
5528 -- version is called from within other operations of the same protected
5531 -- We also build the call to register the procedure if a pragma
5532 -- Interrupt_Handler applies.
5534 -- A single subprogram is created to service all entry bodies; it has an
5535 -- additional boolean out parameter indicating that the previous entry
5536 -- call made by the current task was serviced immediately, i.e. not by
5537 -- proxy. The O parameter contains a pointer to a record object of the
5538 -- type described above. An untyped interface is used here to allow this
5539 -- procedure to be called in places where the type of the object to be
5540 -- serviced is not known. This must be done, for example, when a call
5541 -- that may have been requeued is cancelled; the corresponding object
5542 -- must be serviced, but which object that is not known till runtime.
5545 -- (O : System.Address; P : out Boolean);
5546 -- procedure pprocN (_object : in out poV);
5547 -- procedure pproc (_object : in out poV);
5548 -- function pfuncN (_object : poV);
5549 -- function pfunc (_object : poV);
5552 -- Note that this must come after the record type declaration, since
5553 -- the specs refer to this type.
5555 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
5556 Loc : constant Source_Ptr := Sloc (N);
5557 Prottyp : constant Entity_Id := Defining_Identifier (N);
5558 Protnm : constant Name_Id := Chars (Prottyp);
5560 Pdef : constant Node_Id := Protected_Definition (N);
5561 -- This contains two lists; one for visible and one for private decls
5565 Discr_Map : constant Elist_Id := New_Elmt_List;
5570 Comp_Id : Entity_Id;
5572 Current_Node : Node_Id := N;
5573 Bdef : Entity_Id := Empty; -- avoid uninit warning
5574 Edef : Entity_Id := Empty; -- avoid uninit warning
5575 Entries_Aggr : Node_Id;
5576 Body_Id : Entity_Id;
5579 Object_Comp : Node_Id;
5581 procedure Register_Handler;
5582 -- for a protected operation that is an interrupt handler, add the
5583 -- freeze action that will register it as such.
5585 ----------------------
5586 -- Register_Handler --
5587 ----------------------
5589 procedure Register_Handler is
5591 -- All semantic checks already done in Sem_Prag
5593 Prot_Proc : constant Entity_Id :=
5595 (Specification (Current_Node));
5597 Proc_Address : constant Node_Id :=
5598 Make_Attribute_Reference (Loc,
5599 Prefix => New_Reference_To (Prot_Proc, Loc),
5600 Attribute_Name => Name_Address);
5602 RTS_Call : constant Entity_Id :=
5603 Make_Procedure_Call_Statement (Loc,
5606 RTE (RE_Register_Interrupt_Handler), Loc),
5607 Parameter_Associations =>
5608 New_List (Proc_Address));
5610 Append_Freeze_Action (Prot_Proc, RTS_Call);
5611 end Register_Handler;
5613 -- Start of processing for Expand_N_Protected_Type_Declaration
5616 if Present (Corresponding_Record_Type (Prottyp)) then
5619 Rec_Decl := Build_Corresponding_Record (N, Prottyp, Loc);
5620 Cdecls := Component_Items
5621 (Component_List (Type_Definition (Rec_Decl)));
5624 -- Ada 2005 (AI-345): Propagate the attribute that contains the list
5625 -- of implemented interfaces.
5627 Set_Interface_List (Type_Definition (Rec_Decl), Interface_List (N));
5629 Qualify_Entity_Names (N);
5631 -- If the type has discriminants, their occurrences in the declaration
5632 -- have been replaced by the corresponding discriminals. For components
5633 -- that are constrained by discriminants, their homologues in the
5634 -- corresponding record type must refer to the discriminants of that
5635 -- record, so we must apply a new renaming to subtypes_indications:
5637 -- protected discriminant => discriminal => record discriminant.
5638 -- This replacement is not applied to default expressions, for which
5639 -- the discriminal is correct.
5641 if Has_Discriminants (Prottyp) then
5647 Disc := First_Discriminant (Prottyp);
5648 Decl := First (Discriminant_Specifications (Rec_Decl));
5650 while Present (Disc) loop
5651 Append_Elmt (Discriminal (Disc), Discr_Map);
5652 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
5653 Next_Discriminant (Disc);
5659 -- Fill in the component declarations
5661 -- Add components for entry families. For each entry family,
5662 -- create an anonymous type declaration with the same size, and
5663 -- analyze the type.
5665 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prottyp);
5667 -- Prepend the _Object field with the right type to the component
5668 -- list. We need to compute the number of entries, and in some cases
5669 -- the number of Attach_Handler pragmas.
5673 Num_Attach_Handler : Int := 0;
5674 Protection_Subtype : Node_Id;
5675 Entry_Count_Expr : constant Node_Id :=
5676 Build_Entry_Count_Expression
5677 (Prottyp, Cdecls, Loc);
5680 if Has_Attach_Handler (Prottyp) then
5681 Ritem := First_Rep_Item (Prottyp);
5682 while Present (Ritem) loop
5683 if Nkind (Ritem) = N_Pragma
5684 and then Chars (Ritem) = Name_Attach_Handler
5686 Num_Attach_Handler := Num_Attach_Handler + 1;
5689 Next_Rep_Item (Ritem);
5692 if Restricted_Profile then
5693 if Has_Entries (Prottyp) then
5694 Protection_Subtype :=
5695 New_Reference_To (RTE (RE_Protection_Entry), Loc);
5697 Protection_Subtype :=
5698 New_Reference_To (RTE (RE_Protection), Loc);
5701 Protection_Subtype :=
5702 Make_Subtype_Indication
5706 (RTE (RE_Static_Interrupt_Protection), Loc),
5708 Make_Index_Or_Discriminant_Constraint (
5710 Constraints => New_List (
5712 Make_Integer_Literal (Loc, Num_Attach_Handler))));
5715 elsif Has_Interrupt_Handler (Prottyp) then
5716 Protection_Subtype :=
5717 Make_Subtype_Indication (
5719 Subtype_Mark => New_Reference_To
5720 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
5722 Make_Index_Or_Discriminant_Constraint (
5724 Constraints => New_List (Entry_Count_Expr)));
5726 elsif Has_Entries (Prottyp) then
5728 or else Restriction_Active (No_Entry_Queue) = False
5729 or else Number_Entries (Prottyp) > 1
5731 Protection_Subtype :=
5732 Make_Subtype_Indication (
5735 New_Reference_To (RTE (RE_Protection_Entries), Loc),
5737 Make_Index_Or_Discriminant_Constraint (
5739 Constraints => New_List (Entry_Count_Expr)));
5742 Protection_Subtype :=
5743 New_Reference_To (RTE (RE_Protection_Entry), Loc);
5747 Protection_Subtype := New_Reference_To (RTE (RE_Protection), Loc);
5751 Make_Component_Declaration (Loc,
5752 Defining_Identifier =>
5753 Make_Defining_Identifier (Loc, Name_uObject),
5754 Component_Definition =>
5755 Make_Component_Definition (Loc,
5756 Aliased_Present => True,
5757 Subtype_Indication => Protection_Subtype));
5760 pragma Assert (Present (Pdef));
5762 -- Add private field components
5764 if Present (Private_Declarations (Pdef)) then
5765 Priv := First (Private_Declarations (Pdef));
5767 while Present (Priv) loop
5769 if Nkind (Priv) = N_Component_Declaration then
5770 Pent := Defining_Identifier (Priv);
5772 Make_Component_Declaration (Loc,
5773 Defining_Identifier =>
5774 Make_Defining_Identifier (Sloc (Pent), Chars (Pent)),
5775 Component_Definition =>
5776 Make_Component_Definition (Sloc (Pent),
5777 Aliased_Present => False,
5778 Subtype_Indication =>
5779 New_Copy_Tree (Subtype_Indication
5780 (Component_Definition (Priv)),
5782 Expression => Expression (Priv));
5784 Append_To (Cdecls, New_Priv);
5786 elsif Nkind (Priv) = N_Subprogram_Declaration then
5788 -- Make the unprotected version of the subprogram available
5789 -- for expansion of intra object calls. There is need for
5790 -- a protected version only if the subprogram is an interrupt
5791 -- handler, otherwise this operation can only be called from
5795 Make_Subprogram_Declaration (Loc,
5797 Build_Protected_Sub_Specification
5798 (Priv, Prottyp, Unprotected => True));
5800 Insert_After (Current_Node, Sub);
5803 Set_Protected_Body_Subprogram
5804 (Defining_Unit_Name (Specification (Priv)),
5805 Defining_Unit_Name (Specification (Sub)));
5807 Current_Node := Sub;
5808 if Is_Interrupt_Handler
5809 (Defining_Unit_Name (Specification (Priv)))
5812 Make_Subprogram_Declaration (Loc,
5814 Build_Protected_Sub_Specification
5815 (Priv, Prottyp, Unprotected => False));
5817 Insert_After (Current_Node, Sub);
5819 Current_Node := Sub;
5821 if not Restricted_Profile then
5831 -- Put the _Object component after the private component so that it
5832 -- be finalized early as required by 9.4 (20)
5834 Append_To (Cdecls, Object_Comp);
5836 Insert_After (Current_Node, Rec_Decl);
5837 Current_Node := Rec_Decl;
5839 -- Analyze the record declaration immediately after construction,
5840 -- because the initialization procedure is needed for single object
5841 -- declarations before the next entity is analyzed (the freeze call
5842 -- that generates this initialization procedure is found below).
5844 Analyze (Rec_Decl, Suppress => All_Checks);
5846 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
5847 -- the corresponding record is frozen
5849 if Ada_Version >= Ada_05
5850 and then Present (Visible_Declarations (Pdef))
5851 and then Present (Corresponding_Record_Type
5852 (Defining_Identifier (Parent (Pdef))))
5853 and then Present (Abstract_Interfaces
5854 (Corresponding_Record_Type
5855 (Defining_Identifier (Parent (Pdef)))))
5858 Current_Node : Node_Id := Rec_Decl;
5860 Wrap_Spec : Node_Id;
5864 -- Examine the visible declarations of the protected type, looking
5865 -- for declarations of entries, and subprograms. We do not
5866 -- consider entry families since they can not have dispatching
5867 -- operations, thus they do not need entry wrappers.
5869 Vis_Decl := First (Visible_Declarations (Pdef));
5871 while Present (Vis_Decl) loop
5875 if Nkind (Vis_Decl) = N_Entry_Declaration
5876 and then not Present (Discrete_Subtype_Definition (Vis_Decl))
5879 Build_Wrapper_Spec (Loc,
5880 Proc_Nam => Defining_Identifier (Vis_Decl),
5881 Obj_Typ => Defining_Identifier (Rec_Decl),
5882 Formals => Parameter_Specifications (Vis_Decl));
5884 elsif Nkind (Vis_Decl) = N_Subprogram_Declaration then
5886 Build_Wrapper_Spec (Loc,
5887 Proc_Nam => Defining_Unit_Name
5888 (Specification (Vis_Decl)),
5889 Obj_Typ => Defining_Identifier (Rec_Decl),
5890 Formals => Parameter_Specifications
5891 (Specification (Vis_Decl)));
5895 if Wrap_Spec /= Empty then
5896 New_N := Make_Subprogram_Declaration (Loc,
5897 Specification => Wrap_Spec);
5899 Insert_After (Current_Node, New_N);
5900 Current_Node := New_N;
5910 -- Collect pointers to entry bodies and their barriers, to be placed
5911 -- in the Entry_Bodies_Array for the type. For each entry/family we
5912 -- add an expression to the aggregate which is the initial value of
5913 -- this array. The array is declared after all protected subprograms.
5915 if Has_Entries (Prottyp) then
5917 Make_Aggregate (Loc, Expressions => New_List);
5920 Entries_Aggr := Empty;
5923 -- Build two new procedure specifications for each protected
5924 -- subprogram; one to call from outside the object and one to
5925 -- call from inside. Build a barrier function and an entry
5926 -- body action procedure specification for each protected entry.
5927 -- Initialize the entry body array. If subprogram is flagged as
5928 -- eliminated, do not generate any internal operations.
5932 Comp := First (Visible_Declarations (Pdef));
5934 while Present (Comp) loop
5935 if Nkind (Comp) = N_Subprogram_Declaration
5936 and then not Is_Eliminated (Defining_Entity (Comp))
5939 Make_Subprogram_Declaration (Loc,
5941 Build_Protected_Sub_Specification
5942 (Comp, Prottyp, Unprotected => True));
5944 Insert_After (Current_Node, Sub);
5947 Set_Protected_Body_Subprogram
5948 (Defining_Unit_Name (Specification (Comp)),
5949 Defining_Unit_Name (Specification (Sub)));
5951 -- Make the protected version of the subprogram available
5952 -- for expansion of external calls.
5954 Current_Node := Sub;
5957 Make_Subprogram_Declaration (Loc,
5959 Build_Protected_Sub_Specification
5960 (Comp, Prottyp, Unprotected => False));
5962 Insert_After (Current_Node, Sub);
5964 Current_Node := Sub;
5966 -- If a pragma Interrupt_Handler applies, build and add
5967 -- a call to Register_Interrupt_Handler to the freezing actions
5968 -- of the protected version (Current_Node) of the subprogram:
5969 -- system.interrupts.register_interrupt_handler
5970 -- (prot_procP'address);
5972 if not Restricted_Profile
5973 and then Is_Interrupt_Handler
5974 (Defining_Unit_Name (Specification (Comp)))
5979 elsif Nkind (Comp) = N_Entry_Declaration then
5980 E_Count := E_Count + 1;
5981 Comp_Id := Defining_Identifier (Comp);
5982 Set_Privals_Chain (Comp_Id, New_Elmt_List);
5984 Make_Defining_Identifier (Loc,
5987 New_External_Name (Chars (Comp_Id), Suffix_Index => -1),
5990 Make_Subprogram_Declaration (Loc,
5992 Build_Protected_Entry_Specification (Edef, Comp_Id, Loc));
5994 Insert_After (Current_Node, Sub);
5997 Set_Protected_Body_Subprogram (
5998 Defining_Identifier (Comp),
5999 Defining_Unit_Name (Specification (Sub)));
6001 Current_Node := Sub;
6004 Make_Defining_Identifier (Loc,
6007 New_External_Name (Chars (Comp_Id), Suffix_Index => -1),
6010 Make_Subprogram_Declaration (Loc,
6012 Build_Barrier_Function_Specification (Bdef, Loc));
6014 Insert_After (Current_Node, Sub);
6016 Set_Protected_Body_Subprogram (Bdef, Bdef);
6017 Set_Barrier_Function (Comp_Id, Bdef);
6018 Set_Scope (Bdef, Scope (Comp_Id));
6019 Current_Node := Sub;
6021 -- Collect pointers to the protected subprogram and the barrier
6022 -- of the current entry, for insertion into Entry_Bodies_Array.
6025 Make_Aggregate (Loc,
6026 Expressions => New_List (
6027 Make_Attribute_Reference (Loc,
6028 Prefix => New_Reference_To (Bdef, Loc),
6029 Attribute_Name => Name_Unrestricted_Access),
6030 Make_Attribute_Reference (Loc,
6031 Prefix => New_Reference_To (Edef, Loc),
6032 Attribute_Name => Name_Unrestricted_Access))),
6033 Expressions (Entries_Aggr));
6040 -- If there are some private entry declarations, expand it as if they
6041 -- were visible entries.
6043 if Present (Private_Declarations (Pdef)) then
6044 Comp := First (Private_Declarations (Pdef));
6046 while Present (Comp) loop
6047 if Nkind (Comp) = N_Entry_Declaration then
6048 E_Count := E_Count + 1;
6049 Comp_Id := Defining_Identifier (Comp);
6050 Set_Privals_Chain (Comp_Id, New_Elmt_List);
6052 Make_Defining_Identifier (Loc,
6055 New_External_Name (Chars (Comp_Id), Suffix_Index => -1),
6059 Make_Subprogram_Declaration (Loc,
6061 Build_Protected_Entry_Specification (Edef, Comp_Id, Loc));
6063 Insert_After (Current_Node, Sub);
6066 Set_Protected_Body_Subprogram (
6067 Defining_Identifier (Comp),
6068 Defining_Unit_Name (Specification (Sub)));
6070 Current_Node := Sub;
6073 Make_Defining_Identifier (Loc,
6076 New_External_Name (Chars (Comp_Id), Suffix_Index => -1),
6079 Make_Subprogram_Declaration (Loc,
6081 Build_Barrier_Function_Specification (Bdef, Loc));
6083 Insert_After (Current_Node, Sub);
6085 Set_Protected_Body_Subprogram (Bdef, Bdef);
6086 Set_Barrier_Function (Comp_Id, Bdef);
6087 Set_Scope (Bdef, Scope (Comp_Id));
6088 Current_Node := Sub;
6090 -- Collect pointers to the protected subprogram and the
6091 -- barrier of the current entry, for insertion into
6092 -- Entry_Bodies_Array.
6095 Make_Aggregate (Loc,
6096 Expressions => New_List (
6097 Make_Attribute_Reference (Loc,
6098 Prefix => New_Reference_To (Bdef, Loc),
6099 Attribute_Name => Name_Unrestricted_Access),
6100 Make_Attribute_Reference (Loc,
6101 Prefix => New_Reference_To (Edef, Loc),
6102 Attribute_Name => Name_Unrestricted_Access))),
6103 Expressions (Entries_Aggr));
6110 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
6111 -- all protected subprograms have been collected.
6113 if Has_Entries (Prottyp) then
6114 Body_Id := Make_Defining_Identifier (Sloc (Prottyp),
6115 New_External_Name (Chars (Prottyp), 'A'));
6118 or else Restriction_Active (No_Entry_Queue) = False
6121 Body_Arr := Make_Object_Declaration (Loc,
6122 Defining_Identifier => Body_Id,
6123 Aliased_Present => True,
6124 Object_Definition =>
6125 Make_Subtype_Indication (Loc,
6126 Subtype_Mark => New_Reference_To (
6127 RTE (RE_Protected_Entry_Body_Array), Loc),
6129 Make_Index_Or_Discriminant_Constraint (Loc,
6130 Constraints => New_List (
6132 Make_Integer_Literal (Loc, 1),
6133 Make_Integer_Literal (Loc, E_Count))))),
6134 Expression => Entries_Aggr);
6137 Body_Arr := Make_Object_Declaration (Loc,
6138 Defining_Identifier => Body_Id,
6139 Aliased_Present => True,
6140 Object_Definition => New_Reference_To (RTE (RE_Entry_Body), Loc),
6142 Make_Aggregate (Loc,
6143 Expressions => New_List (
6144 Make_Attribute_Reference (Loc,
6145 Prefix => New_Reference_To (Bdef, Loc),
6146 Attribute_Name => Name_Unrestricted_Access),
6147 Make_Attribute_Reference (Loc,
6148 Prefix => New_Reference_To (Edef, Loc),
6149 Attribute_Name => Name_Unrestricted_Access))));
6152 -- A pointer to this array will be placed in the corresponding
6153 -- record by its initialization procedure, so this needs to be
6156 Insert_After (Current_Node, Body_Arr);
6157 Current_Node := Body_Arr;
6160 Set_Entry_Bodies_Array (Prottyp, Body_Id);
6162 -- Finally, build the function that maps an entry index into the
6163 -- corresponding body. A pointer to this function is placed in each
6164 -- object of the type. Except for a ravenscar-like profile (no abort,
6165 -- no entry queue, 1 entry)
6168 or else Restriction_Active (No_Entry_Queue) = False
6172 Make_Subprogram_Declaration (Loc,
6173 Specification => Build_Find_Body_Index_Spec (Prottyp));
6174 Insert_After (Current_Node, Sub);
6178 end Expand_N_Protected_Type_Declaration;
6180 --------------------------------
6181 -- Expand_N_Requeue_Statement --
6182 --------------------------------
6184 -- A requeue statement is expanded into one of four GNARLI operations,
6185 -- depending on the source and destination (task or protected object).
6186 -- In addition, code must be generated to jump around the remainder of
6187 -- processing for the original entry and, if the destination is a
6188 -- (different) protected object, to attempt to service it.
6189 -- The following illustrates the various cases:
6192 -- (O : System.Address;
6193 -- P : System.Address;
6194 -- E : Protected_Entry_Index)
6196 -- <discriminant renamings>
6197 -- <private object renamings>
6198 -- type poVP is access poV;
6199 -- _Object : ptVP := ptVP!(O);
6203 -- <start of statement sequence for entry>
6205 -- -- Requeue from one protected entry body to another protected
6208 -- Requeue_Protected_Entry (
6209 -- _object._object'Access,
6210 -- new._object'Access,
6215 -- <some more of the statement sequence for entry>
6217 -- -- Requeue from an entry body to a task entry
6219 -- Requeue_Protected_To_Task_Entry (
6225 -- <rest of statement sequence for entry>
6226 -- Complete_Entry_Body (_Object._Object);
6229 -- when all others =>
6230 -- Exceptional_Complete_Entry_Body (
6231 -- _Object._Object, Get_GNAT_Exception);
6235 -- Requeue of a task entry call to a task entry
6237 -- Accept_Call (E, Ann);
6238 -- <start of statement sequence for accept statement>
6239 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
6241 -- <rest of statement sequence for accept statement>
6243 -- Complete_Rendezvous;
6246 -- when all others =>
6247 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6249 -- Requeue of a task entry call to a protected entry
6251 -- Accept_Call (E, Ann);
6252 -- <start of statement sequence for accept statement>
6253 -- Requeue_Task_To_Protected_Entry (
6254 -- new._object'Access,
6259 -- <rest of statement sequence for accept statement>
6261 -- Complete_Rendezvous;
6264 -- when all others =>
6265 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6267 -- Further details on these expansions can be found in
6268 -- Expand_N_Protected_Body and Expand_N_Accept_Statement.
6270 procedure Expand_N_Requeue_Statement (N : Node_Id) is
6271 Loc : constant Source_Ptr := Sloc (N);
6276 Conctyp : Entity_Id;
6280 Abortable : Node_Id;
6281 Skip_Stat : Node_Id;
6282 Self_Param : Node_Id;
6283 New_Param : Node_Id;
6285 RTS_Call : Entity_Id;
6289 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc);
6291 -- Set up the target object
6293 Extract_Entry (N, Concval, Ename, Index);
6294 Conctyp := Etype (Concval);
6295 New_Param := Concurrent_Ref (Concval);
6297 -- The target entry index and abortable flag are the same for all cases
6299 Params := New_List (
6300 Entry_Index_Expression (Loc, Entity (Ename), Index, Conctyp),
6303 -- Determine proper GNARLI call and required additional parameters
6304 -- Loop to find nearest enclosing task type or protected type
6306 Oldtyp := Current_Scope;
6308 if Is_Task_Type (Oldtyp) then
6309 if Is_Task_Type (Conctyp) then
6310 RTS_Call := RTE (RE_Requeue_Task_Entry);
6313 pragma Assert (Is_Protected_Type (Conctyp));
6314 RTS_Call := RTE (RE_Requeue_Task_To_Protected_Entry);
6316 Make_Attribute_Reference (Loc,
6317 Prefix => New_Param,
6318 Attribute_Name => Name_Unchecked_Access);
6321 Prepend (New_Param, Params);
6324 elsif Is_Protected_Type (Oldtyp) then
6326 Make_Attribute_Reference (Loc,
6327 Prefix => Concurrent_Ref (New_Occurrence_Of (Oldtyp, Loc)),
6328 Attribute_Name => Name_Unchecked_Access);
6330 if Is_Task_Type (Conctyp) then
6331 RTS_Call := RTE (RE_Requeue_Protected_To_Task_Entry);
6334 pragma Assert (Is_Protected_Type (Conctyp));
6335 RTS_Call := RTE (RE_Requeue_Protected_Entry);
6337 Make_Attribute_Reference (Loc,
6338 Prefix => New_Param,
6339 Attribute_Name => Name_Unchecked_Access);
6342 Prepend (New_Param, Params);
6343 Prepend (Self_Param, Params);
6346 -- If neither task type or protected type, must be in some
6347 -- inner enclosing block, so move on out
6350 Oldtyp := Scope (Oldtyp);
6354 -- Create the GNARLI call
6356 Rcall := Make_Procedure_Call_Statement (Loc,
6358 New_Occurrence_Of (RTS_Call, Loc),
6359 Parameter_Associations => Params);
6364 if Is_Protected_Type (Oldtyp) then
6366 -- Build the return statement to skip the rest of the entry body
6368 Skip_Stat := Make_Return_Statement (Loc);
6371 -- If the requeue is within a task, find the end label of the
6372 -- enclosing accept statement.
6374 Acc_Stat := Parent (N);
6375 while Nkind (Acc_Stat) /= N_Accept_Statement loop
6376 Acc_Stat := Parent (Acc_Stat);
6379 -- The last statement is the second label, used for completing the
6380 -- rendezvous the usual way.
6381 -- The label we are looking for is right before it.
6384 Prev (Last (Statements (Handled_Statement_Sequence (Acc_Stat))));
6386 pragma Assert (Nkind (Lab_Node) = N_Label);
6388 -- Build the goto statement to skip the rest of the accept
6392 Make_Goto_Statement (Loc,
6393 Name => New_Occurrence_Of (Entity (Identifier (Lab_Node)), Loc));
6396 Set_Analyzed (Skip_Stat);
6398 Insert_After (N, Skip_Stat);
6399 end Expand_N_Requeue_Statement;
6401 -------------------------------
6402 -- Expand_N_Selective_Accept --
6403 -------------------------------
6405 procedure Expand_N_Selective_Accept (N : Node_Id) is
6406 Loc : constant Source_Ptr := Sloc (N);
6407 Alts : constant List_Id := Select_Alternatives (N);
6409 -- Note: in the below declarations a lot of new lists are allocated
6410 -- unconditionally which may well not end up being used. That's
6411 -- not a good idea since it wastes space gratuitously ???
6413 Accept_Case : List_Id;
6414 Accept_List : constant List_Id := New_List;
6417 Alt_List : constant List_Id := New_List;
6418 Alt_Stats : List_Id;
6419 Ann : Entity_Id := Empty;
6422 Check_Guard : Boolean := True;
6424 Decls : constant List_Id := New_List;
6425 Stats : constant List_Id := New_List;
6426 Body_List : constant List_Id := New_List;
6427 Trailing_List : constant List_Id := New_List;
6430 Else_Present : Boolean := False;
6431 Terminate_Alt : Node_Id := Empty;
6432 Select_Mode : Node_Id;
6434 Delay_Case : List_Id;
6435 Delay_Count : Integer := 0;
6436 Delay_Val : Entity_Id;
6437 Delay_Index : Entity_Id;
6438 Delay_Min : Entity_Id;
6439 Delay_Num : Int := 1;
6440 Delay_Alt_List : List_Id := New_List;
6441 Delay_List : constant List_Id := New_List;
6445 First_Delay : Boolean := True;
6446 Guard_Open : Entity_Id;
6452 Num_Accept : Nat := 0;
6455 Time_Type : Entity_Id;
6457 Select_Call : Node_Id;
6459 Qnam : constant Entity_Id :=
6460 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
6462 Xnam : constant Entity_Id :=
6463 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
6465 -----------------------
6466 -- Local subprograms --
6467 -----------------------
6469 function Accept_Or_Raise return List_Id;
6470 -- For the rare case where delay alternatives all have guards, and
6471 -- all of them are closed, it is still possible that there were open
6472 -- accept alternatives with no callers. We must reexamine the
6473 -- Accept_List, and execute a selective wait with no else if some
6474 -- accept is open. If none, we raise program_error.
6476 procedure Add_Accept (Alt : Node_Id);
6477 -- Process a single accept statement in a select alternative. Build
6478 -- procedure for body of accept, and add entry to dispatch table with
6479 -- expression for guard, in preparation for call to run time select.
6481 function Make_And_Declare_Label (Num : Int) return Node_Id;
6482 -- Manufacture a label using Num as a serial number and declare it.
6483 -- The declaration is appended to Decls. The label marks the trailing
6484 -- statements of an accept or delay alternative.
6486 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
6487 -- Build call to Selective_Wait runtime routine
6489 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
6490 -- Add code to compare value of delay with previous values, and
6491 -- generate case entry for trailing statements.
6493 procedure Process_Accept_Alternative
6497 -- Add code to call corresponding procedure, and branch to
6498 -- trailing statements, if any.
6500 ---------------------
6501 -- Accept_Or_Raise --
6502 ---------------------
6504 function Accept_Or_Raise return List_Id is
6507 J : constant Entity_Id := Make_Defining_Identifier (Loc,
6508 New_Internal_Name ('J'));
6511 -- We generate the following:
6513 -- for J in q'range loop
6514 -- if q(J).S /=null_task_entry then
6515 -- selective_wait (simple_mode,...);
6521 -- if no rendez_vous then
6522 -- raise program_error;
6525 -- Note that the code needs to know that the selector name
6526 -- in an Accept_Alternative is named S.
6528 Cond := Make_Op_Ne (Loc,
6530 Make_Selected_Component (Loc,
6531 Prefix => Make_Indexed_Component (Loc,
6532 Prefix => New_Reference_To (Qnam, Loc),
6533 Expressions => New_List (New_Reference_To (J, Loc))),
6534 Selector_Name => Make_Identifier (Loc, Name_S)),
6536 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
6539 Make_Implicit_Loop_Statement (N,
6540 Identifier => Empty,
6542 Make_Iteration_Scheme (Loc,
6543 Loop_Parameter_Specification =>
6544 Make_Loop_Parameter_Specification (Loc,
6545 Defining_Identifier => J,
6546 Discrete_Subtype_Definition =>
6547 Make_Attribute_Reference (Loc,
6548 Prefix => New_Reference_To (Qnam, Loc),
6549 Attribute_Name => Name_Range,
6550 Expressions => New_List (
6551 Make_Integer_Literal (Loc, 1))))),
6553 Statements => New_List (
6554 Make_Implicit_If_Statement (N,
6556 Then_Statements => New_List (
6558 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
6559 Make_Exit_Statement (Loc))))));
6562 Make_Raise_Program_Error (Loc,
6563 Condition => Make_Op_Eq (Loc,
6564 Left_Opnd => New_Reference_To (Xnam, Loc),
6566 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
6567 Reason => PE_All_Guards_Closed));
6570 end Accept_Or_Raise;
6576 procedure Add_Accept (Alt : Node_Id) is
6577 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
6578 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
6579 Eent : constant Entity_Id := Entity (Ename);
6580 Index : constant Node_Id := Entry_Index (Acc_Stm);
6581 Null_Body : Node_Id;
6582 Proc_Body : Node_Id;
6589 Ann := Node (Last_Elmt (Accept_Address (Eent)));
6592 if Present (Condition (Alt)) then
6594 Make_Conditional_Expression (Loc, New_List (
6596 Entry_Index_Expression (Loc, Eent, Index, Scope (Eent)),
6597 New_Reference_To (RTE (RE_Null_Task_Entry), Loc)));
6600 Entry_Index_Expression
6601 (Loc, Eent, Index, Scope (Eent));
6604 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
6605 Null_Body := New_Reference_To (Standard_False, Loc);
6607 if Abort_Allowed then
6608 Call := Make_Procedure_Call_Statement (Loc,
6609 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc));
6610 Insert_Before (First (Statements (Handled_Statement_Sequence (
6611 Accept_Statement (Alt)))), Call);
6616 Make_Defining_Identifier (Sloc (Ename),
6617 New_External_Name (Chars (Ename), 'A', Num_Accept));
6619 Set_Needs_Debug_Info (PB_Ent, Comes_From_Source (Alt));
6622 Make_Subprogram_Body (Loc,
6624 Make_Procedure_Specification (Loc,
6625 Defining_Unit_Name => PB_Ent),
6626 Declarations => Declarations (Acc_Stm),
6627 Handled_Statement_Sequence =>
6628 Build_Accept_Body (Accept_Statement (Alt)));
6630 -- During the analysis of the body of the accept statement, any
6631 -- zero cost exception handler records were collected in the
6632 -- Accept_Handler_Records field of the N_Accept_Alternative
6633 -- node. This is where we move them to where they belong,
6634 -- namely the newly created procedure.
6636 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
6637 Append (Proc_Body, Body_List);
6640 Null_Body := New_Reference_To (Standard_True, Loc);
6642 -- if accept statement has declarations, insert above, given
6643 -- that we are not creating a body for the accept.
6645 if Present (Declarations (Acc_Stm)) then
6646 Insert_Actions (N, Declarations (Acc_Stm));
6650 Append_To (Accept_List,
6651 Make_Aggregate (Loc, Expressions => New_List (Null_Body, Expr)));
6653 Num_Accept := Num_Accept + 1;
6656 ----------------------------
6657 -- Make_And_Declare_Label --
6658 ----------------------------
6660 function Make_And_Declare_Label (Num : Int) return Node_Id is
6664 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
6666 Make_Label (Loc, Lab_Id);
6669 Make_Implicit_Label_Declaration (Loc,
6670 Defining_Identifier =>
6671 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
6672 Label_Construct => Lab));
6675 end Make_And_Declare_Label;
6677 ----------------------
6678 -- Make_Select_Call --
6679 ----------------------
6681 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
6682 Params : constant List_Id := New_List;
6686 Make_Attribute_Reference (Loc,
6687 Prefix => New_Reference_To (Qnam, Loc),
6688 Attribute_Name => Name_Unchecked_Access),
6690 Append (Select_Mode, Params);
6691 Append (New_Reference_To (Ann, Loc), Params);
6692 Append (New_Reference_To (Xnam, Loc), Params);
6695 Make_Procedure_Call_Statement (Loc,
6696 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
6697 Parameter_Associations => Params);
6698 end Make_Select_Call;
6700 --------------------------------
6701 -- Process_Accept_Alternative --
6702 --------------------------------
6704 procedure Process_Accept_Alternative
6709 Choices : List_Id := No_List;
6710 Alt_Stats : List_Id;
6713 Adjust_Condition (Condition (Alt));
6714 Alt_Stats := No_List;
6716 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
6717 Choices := New_List (
6718 Make_Integer_Literal (Loc, Index));
6720 Alt_Stats := New_List (
6721 Make_Procedure_Call_Statement (Loc,
6722 Name => New_Reference_To (
6723 Defining_Unit_Name (Specification (Proc)), Loc)));
6726 if Statements (Alt) /= Empty_List then
6728 if No (Alt_Stats) then
6730 -- Accept with no body, followed by trailing statements
6732 Choices := New_List (
6733 Make_Integer_Literal (Loc, Index));
6735 Alt_Stats := New_List;
6738 -- After the call, if any, branch to to trailing statements.
6739 -- We create a label for each, as well as the corresponding
6740 -- label declaration.
6742 Lab := Make_And_Declare_Label (Index);
6743 Append_To (Alt_Stats,
6744 Make_Goto_Statement (Loc,
6745 Name => New_Copy (Identifier (Lab))));
6747 Append (Lab, Trailing_List);
6748 Append_List (Statements (Alt), Trailing_List);
6749 Append_To (Trailing_List,
6750 Make_Goto_Statement (Loc,
6751 Name => New_Copy (Identifier (End_Lab))));
6754 if Present (Alt_Stats) then
6756 -- Procedure call. and/or trailing statements
6758 Append_To (Alt_List,
6759 Make_Case_Statement_Alternative (Loc,
6760 Discrete_Choices => Choices,
6761 Statements => Alt_Stats));
6763 end Process_Accept_Alternative;
6765 -------------------------------
6766 -- Process_Delay_Alternative --
6767 -------------------------------
6769 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
6772 Delay_Alt : List_Id;
6775 -- Deal with C/Fortran boolean as delay condition
6777 Adjust_Condition (Condition (Alt));
6779 -- Determine the smallest specified delay
6781 -- for each delay alternative generate:
6783 -- if guard-expression then
6784 -- Delay_Val := delay-expression;
6785 -- Guard_Open := True;
6786 -- if Delay_Val < Delay_Min then
6787 -- Delay_Min := Delay_Val;
6788 -- Delay_Index := Index;
6792 -- The enclosing if-statement is omitted if there is no guard
6797 First_Delay := False;
6799 Delay_Alt := New_List (
6800 Make_Assignment_Statement (Loc,
6801 Name => New_Reference_To (Delay_Min, Loc),
6802 Expression => Expression (Delay_Statement (Alt))));
6804 if Delay_Count > 1 then
6805 Append_To (Delay_Alt,
6806 Make_Assignment_Statement (Loc,
6807 Name => New_Reference_To (Delay_Index, Loc),
6808 Expression => Make_Integer_Literal (Loc, Index)));
6812 Delay_Alt := New_List (
6813 Make_Assignment_Statement (Loc,
6814 Name => New_Reference_To (Delay_Val, Loc),
6815 Expression => Expression (Delay_Statement (Alt))));
6817 if Time_Type = Standard_Duration then
6820 Left_Opnd => New_Reference_To (Delay_Val, Loc),
6821 Right_Opnd => New_Reference_To (Delay_Min, Loc));
6824 -- The scope of the time type must define a comparison
6825 -- operator. The scope itself may not be visible, so we
6826 -- construct a node with entity information to insure that
6827 -- semantic analysis can find the proper operator.
6830 Make_Function_Call (Loc,
6831 Name => Make_Selected_Component (Loc,
6832 Prefix => New_Reference_To (Scope (Time_Type), Loc),
6834 Make_Operator_Symbol (Loc,
6835 Chars => Name_Op_Lt,
6836 Strval => No_String)),
6837 Parameter_Associations =>
6839 New_Reference_To (Delay_Val, Loc),
6840 New_Reference_To (Delay_Min, Loc)));
6842 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
6845 Append_To (Delay_Alt,
6846 Make_Implicit_If_Statement (N,
6848 Then_Statements => New_List (
6849 Make_Assignment_Statement (Loc,
6850 Name => New_Reference_To (Delay_Min, Loc),
6851 Expression => New_Reference_To (Delay_Val, Loc)),
6853 Make_Assignment_Statement (Loc,
6854 Name => New_Reference_To (Delay_Index, Loc),
6855 Expression => Make_Integer_Literal (Loc, Index)))));
6859 Append_To (Delay_Alt,
6860 Make_Assignment_Statement (Loc,
6861 Name => New_Reference_To (Guard_Open, Loc),
6862 Expression => New_Reference_To (Standard_True, Loc)));
6865 if Present (Condition (Alt)) then
6866 Delay_Alt := New_List (
6867 Make_Implicit_If_Statement (N,
6868 Condition => Condition (Alt),
6869 Then_Statements => Delay_Alt));
6872 Append_List (Delay_Alt, Delay_List);
6874 -- If the delay alternative has a statement part, add a
6875 -- choice to the case statements for delays.
6877 if Present (Statements (Alt)) then
6879 if Delay_Count = 1 then
6880 Append_List (Statements (Alt), Delay_Alt_List);
6883 Choices := New_List (
6884 Make_Integer_Literal (Loc, Index));
6886 Append_To (Delay_Alt_List,
6887 Make_Case_Statement_Alternative (Loc,
6888 Discrete_Choices => Choices,
6889 Statements => Statements (Alt)));
6892 elsif Delay_Count = 1 then
6894 -- If the single delay has no trailing statements, add a branch
6895 -- to the exit label to the selective wait.
6897 Delay_Alt_List := New_List (
6898 Make_Goto_Statement (Loc,
6899 Name => New_Copy (Identifier (End_Lab))));
6902 end Process_Delay_Alternative;
6904 -- Start of processing for Expand_N_Selective_Accept
6907 -- First insert some declarations before the select. The first is:
6911 -- This variable holds the parameters passed to the accept body. This
6912 -- declaration has already been inserted by the time we get here by
6913 -- a call to Expand_Accept_Declarations made from the semantics when
6914 -- processing the first accept statement contained in the select. We
6915 -- can find this entity as Accept_Address (E), where E is any of the
6916 -- entries references by contained accept statements.
6918 -- The first step is to scan the list of Selective_Accept_Statements
6919 -- to find this entity, and also count the number of accepts, and
6920 -- determine if terminated, delay or else is present:
6924 Alt := First (Alts);
6925 while Present (Alt) loop
6927 if Nkind (Alt) = N_Accept_Alternative then
6930 elsif Nkind (Alt) = N_Delay_Alternative then
6931 Delay_Count := Delay_Count + 1;
6933 -- If the delays are relative delays, the delay expressions have
6934 -- type Standard_Duration. Otherwise they must have some time type
6935 -- recognized by GNAT.
6937 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
6938 Time_Type := Standard_Duration;
6940 Time_Type := Etype (Expression (Delay_Statement (Alt)));
6942 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
6943 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
6948 "& is not a time type ('R'M 9.6(6))",
6949 Expression (Delay_Statement (Alt)), Time_Type);
6950 Time_Type := Standard_Duration;
6951 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
6955 if No (Condition (Alt)) then
6957 -- This guard will always be open
6959 Check_Guard := False;
6962 elsif Nkind (Alt) = N_Terminate_Alternative then
6963 Adjust_Condition (Condition (Alt));
6964 Terminate_Alt := Alt;
6967 Num_Alts := Num_Alts + 1;
6971 Else_Present := Present (Else_Statements (N));
6973 -- At the same time (see procedure Add_Accept) we build the accept list:
6975 -- Qnn : Accept_List (1 .. num-select) := (
6976 -- (null-body, entry-index),
6977 -- (null-body, entry-index),
6979 -- (null_body, entry-index));
6981 -- In the above declaration, null-body is True if the corresponding
6982 -- accept has no body, and false otherwise. The entry is either the
6983 -- entry index expression if there is no guard, or if a guard is
6984 -- present, then a conditional expression of the form:
6986 -- (if guard then entry-index else Null_Task_Entry)
6988 -- If a guard is statically known to be false, the entry can simply
6989 -- be omitted from the accept list.
6992 Make_Object_Declaration (Loc,
6993 Defining_Identifier => Qnam,
6994 Object_Definition =>
6995 New_Reference_To (RTE (RE_Accept_List), Loc),
6996 Aliased_Present => True,
6999 Make_Qualified_Expression (Loc,
7001 New_Reference_To (RTE (RE_Accept_List), Loc),
7003 Make_Aggregate (Loc, Expressions => Accept_List)));
7007 -- Then we declare the variable that holds the index for the accept
7008 -- that will be selected for service:
7010 -- Xnn : Select_Index;
7013 Make_Object_Declaration (Loc,
7014 Defining_Identifier => Xnam,
7015 Object_Definition =>
7016 New_Reference_To (RTE (RE_Select_Index), Loc),
7018 New_Reference_To (RTE (RE_No_Rendezvous), Loc));
7022 -- After this follow procedure declarations for each accept body
7029 -- where the ... are statements from the corresponding procedure body.
7030 -- No parameters are involved, since the parameters are passed via Ann
7031 -- and the parameter references have already been expanded to be direct
7032 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
7033 -- any embedded tasking statements (which would normally be illegal in
7034 -- procedures, have been converted to calls to the tasking runtime so
7035 -- there is no problem in putting them into procedures.
7037 -- The original accept statement has been expanded into a block in
7038 -- the same fashion as for simple accepts (see Build_Accept_Body).
7040 -- Note: we don't really need to build these procedures for the case
7041 -- where no delay statement is present, but it is just as easy to
7042 -- build them unconditionally, and not significantly inefficient,
7043 -- since if they are short they will be inlined anyway.
7045 -- The procedure declarations have been assembled in Body_List
7047 -- If delays are present, we must compute the required delay.
7048 -- We first generate the declarations:
7050 -- Delay_Index : Boolean := 0;
7051 -- Delay_Min : Some_Time_Type.Time;
7052 -- Delay_Val : Some_Time_Type.Time;
7054 -- Delay_Index will be set to the index of the minimum delay, i.e. the
7055 -- active delay that is actually chosen as the basis for the possible
7056 -- delay if an immediate rendez-vous is not possible.
7058 -- In the most common case there is a single delay statement, and this
7059 -- is handled specially.
7061 if Delay_Count > 0 then
7063 -- Generate the required declarations
7066 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
7068 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
7070 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
7073 Make_Object_Declaration (Loc,
7074 Defining_Identifier => Delay_Val,
7075 Object_Definition => New_Reference_To (Time_Type, Loc)));
7078 Make_Object_Declaration (Loc,
7079 Defining_Identifier => Delay_Index,
7080 Object_Definition => New_Reference_To (Standard_Integer, Loc),
7081 Expression => Make_Integer_Literal (Loc, 0)));
7084 Make_Object_Declaration (Loc,
7085 Defining_Identifier => Delay_Min,
7086 Object_Definition => New_Reference_To (Time_Type, Loc),
7088 Unchecked_Convert_To (Time_Type,
7089 Make_Attribute_Reference (Loc,
7091 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
7092 Attribute_Name => Name_Last))));
7094 -- Create Duration and Delay_Mode objects used for passing a delay
7097 D := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
7098 M := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
7104 -- Note that these values are defined in s-osprim.ads and must
7107 -- Relative : constant := 0;
7108 -- Absolute_Calendar : constant := 1;
7109 -- Absolute_RT : constant := 2;
7111 if Time_Type = Standard_Duration then
7112 Discr := Make_Integer_Literal (Loc, 0);
7114 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
7115 Discr := Make_Integer_Literal (Loc, 1);
7119 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
7120 Discr := Make_Integer_Literal (Loc, 2);
7124 Make_Object_Declaration (Loc,
7125 Defining_Identifier => D,
7126 Object_Definition =>
7127 New_Reference_To (Standard_Duration, Loc)));
7130 Make_Object_Declaration (Loc,
7131 Defining_Identifier => M,
7132 Object_Definition =>
7133 New_Reference_To (Standard_Integer, Loc),
7134 Expression => Discr));
7139 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
7142 Make_Object_Declaration (Loc,
7143 Defining_Identifier => Guard_Open,
7144 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
7145 Expression => New_Reference_To (Standard_False, Loc)));
7148 -- Delay_Count is zero, don't need M and D set (suppress warning)
7155 if Present (Terminate_Alt) then
7157 -- If the terminate alternative guard is False, use
7158 -- Simple_Mode; otherwise use Terminate_Mode.
7160 if Present (Condition (Terminate_Alt)) then
7161 Select_Mode := Make_Conditional_Expression (Loc,
7162 New_List (Condition (Terminate_Alt),
7163 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
7164 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
7166 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
7169 elsif Else_Present or Delay_Count > 0 then
7170 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
7173 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
7176 Select_Call := Make_Select_Call (Select_Mode);
7177 Append (Select_Call, Stats);
7179 -- Now generate code to act on the result. There is an entry
7180 -- in this case for each accept statement with a non-null body,
7181 -- followed by a branch to the statements that follow the Accept.
7182 -- In the absence of delay alternatives, we generate:
7185 -- when No_Rendezvous => -- omitted if simple mode
7200 -- Lab0: Else_Statements;
7203 -- Lab1: Trailing_Statements1;
7206 -- Lab2: Trailing_Statements2;
7211 -- Generate label for common exit
7213 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
7215 -- First entry is the default case, when no rendezvous is possible
7217 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
7219 if Else_Present then
7221 -- If no rendezvous is possible, the else part is executed
7223 Lab := Make_And_Declare_Label (0);
7224 Alt_Stats := New_List (
7225 Make_Goto_Statement (Loc,
7226 Name => New_Copy (Identifier (Lab))));
7228 Append (Lab, Trailing_List);
7229 Append_List (Else_Statements (N), Trailing_List);
7230 Append_To (Trailing_List,
7231 Make_Goto_Statement (Loc,
7232 Name => New_Copy (Identifier (End_Lab))));
7234 Alt_Stats := New_List (
7235 Make_Goto_Statement (Loc,
7236 Name => New_Copy (Identifier (End_Lab))));
7239 Append_To (Alt_List,
7240 Make_Case_Statement_Alternative (Loc,
7241 Discrete_Choices => Choices,
7242 Statements => Alt_Stats));
7244 -- We make use of the fact that Accept_Index is an integer type,
7245 -- and generate successive literals for entries for each accept.
7246 -- Only those for which there is a body or trailing statements are
7247 -- given a case entry.
7249 Alt := First (Select_Alternatives (N));
7250 Proc := First (Body_List);
7252 while Present (Alt) loop
7254 if Nkind (Alt) = N_Accept_Alternative then
7255 Process_Accept_Alternative (Alt, Index, Proc);
7259 (Handled_Statement_Sequence (Accept_Statement (Alt)))
7264 elsif Nkind (Alt) = N_Delay_Alternative then
7265 Process_Delay_Alternative (Alt, Delay_Num);
7266 Delay_Num := Delay_Num + 1;
7272 -- An others choice is always added to the main case, as well
7273 -- as the delay case (to satisfy the compiler).
7275 Append_To (Alt_List,
7276 Make_Case_Statement_Alternative (Loc,
7278 New_List (Make_Others_Choice (Loc)),
7280 New_List (Make_Goto_Statement (Loc,
7281 Name => New_Copy (Identifier (End_Lab))))));
7283 Accept_Case := New_List (
7284 Make_Case_Statement (Loc,
7285 Expression => New_Reference_To (Xnam, Loc),
7286 Alternatives => Alt_List));
7288 Append_List (Trailing_List, Accept_Case);
7289 Append (End_Lab, Accept_Case);
7290 Append_List (Body_List, Decls);
7292 -- Construct case statement for trailing statements of delay
7293 -- alternatives, if there are several of them.
7295 if Delay_Count > 1 then
7296 Append_To (Delay_Alt_List,
7297 Make_Case_Statement_Alternative (Loc,
7299 New_List (Make_Others_Choice (Loc)),
7301 New_List (Make_Null_Statement (Loc))));
7303 Delay_Case := New_List (
7304 Make_Case_Statement (Loc,
7305 Expression => New_Reference_To (Delay_Index, Loc),
7306 Alternatives => Delay_Alt_List));
7308 Delay_Case := Delay_Alt_List;
7311 -- If there are no delay alternatives, we append the case statement
7312 -- to the statement list.
7314 if Delay_Count = 0 then
7315 Append_List (Accept_Case, Stats);
7317 -- Delay alternatives present
7320 -- If delay alternatives are present we generate:
7322 -- find minimum delay.
7323 -- DX := minimum delay;
7324 -- M := <delay mode>;
7325 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
7328 -- if X = No_Rendezvous then
7329 -- case statement for delay statements.
7331 -- case statement for accept alternatives.
7342 -- The type of the delay expression is known to be legal
7344 if Time_Type = Standard_Duration then
7345 Conv := New_Reference_To (Delay_Min, Loc);
7347 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
7348 Conv := Make_Function_Call (Loc,
7349 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
7350 New_List (New_Reference_To (Delay_Min, Loc)));
7354 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
7356 Conv := Make_Function_Call (Loc,
7357 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
7358 New_List (New_Reference_To (Delay_Min, Loc)));
7361 Stmt := Make_Assignment_Statement (Loc,
7362 Name => New_Reference_To (D, Loc),
7363 Expression => Conv);
7365 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
7367 Parms := Parameter_Associations (Select_Call);
7368 Parm := First (Parms);
7370 while Present (Parm)
7371 and then Parm /= Select_Mode
7376 pragma Assert (Present (Parm));
7377 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
7380 -- Prepare two new parameters of Duration and Delay_Mode type
7381 -- which represent the value and the mode of the minimum delay.
7384 Insert_After (Parm, New_Reference_To (M, Loc));
7385 Insert_After (Parm, New_Reference_To (D, Loc));
7387 -- Create a call to RTS
7389 Rewrite (Select_Call,
7390 Make_Procedure_Call_Statement (Loc,
7391 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
7392 Parameter_Associations => Parms));
7394 -- This new call should follow the calculation of the
7397 Insert_List_Before (Select_Call, Delay_List);
7401 Make_Implicit_If_Statement (N,
7402 Condition => New_Reference_To (Guard_Open, Loc),
7404 New_List (New_Copy_Tree (Stmt),
7405 New_Copy_Tree (Select_Call)),
7406 Else_Statements => Accept_Or_Raise);
7407 Rewrite (Select_Call, Stmt);
7409 Insert_Before (Select_Call, Stmt);
7413 Make_Implicit_If_Statement (N,
7414 Condition => Make_Op_Eq (Loc,
7415 Left_Opnd => New_Reference_To (Xnam, Loc),
7417 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
7419 Then_Statements => Delay_Case,
7420 Else_Statements => Accept_Case);
7422 Append (Cases, Stats);
7426 -- Replace accept statement with appropriate block
7429 Make_Block_Statement (Loc,
7430 Declarations => Decls,
7431 Handled_Statement_Sequence =>
7432 Make_Handled_Sequence_Of_Statements (Loc,
7433 Statements => Stats));
7438 -- Note: have to worry more about abort deferral in above code ???
7440 -- Final step is to unstack the Accept_Address entries for all accept
7441 -- statements appearing in accept alternatives in the select statement
7443 Alt := First (Alts);
7444 while Present (Alt) loop
7445 if Nkind (Alt) = N_Accept_Alternative then
7446 Remove_Last_Elmt (Accept_Address
7447 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
7452 end Expand_N_Selective_Accept;
7454 --------------------------------------
7455 -- Expand_N_Single_Task_Declaration --
7456 --------------------------------------
7458 -- Single task declarations should never be present after semantic
7459 -- analysis, since we expect them to be replaced by a declaration of
7460 -- an anonymous task type, followed by a declaration of the task
7461 -- object. We include this routine to make sure that is happening!
7463 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
7465 raise Program_Error;
7466 end Expand_N_Single_Task_Declaration;
7468 ------------------------
7469 -- Expand_N_Task_Body --
7470 ------------------------
7472 -- Given a task body
7474 -- task body tname is
7480 -- This expansion routine converts it into a procedure and sets the
7481 -- elaboration flag for the procedure to true, to represent the fact
7482 -- that the task body is now elaborated:
7484 -- procedure tnameB (_Task : access tnameV) is
7485 -- discriminal : dtype renames _Task.discriminant;
7487 -- procedure _clean is
7491 -- Abort_Undefer.all;
7496 -- Abort_Undefer.all;
7498 -- System.Task_Stages.Complete_Activation;
7506 -- In addition, if the task body is an activator, then a call to
7507 -- activate tasks is added at the start of the statements, before
7508 -- the call to Complete_Activation, and if in addition the task is
7509 -- a master then it must be established as a master. These calls are
7510 -- inserted and analyzed in Expand_Cleanup_Actions, when the
7511 -- Handled_Sequence_Of_Statements is expanded.
7513 -- There is one discriminal declaration line generated for each
7514 -- discriminant that is present to provide an easy reference point
7515 -- for discriminant references inside the body (see Exp_Ch2.Expand_Name).
7517 -- Note on relationship to GNARLI definition. In the GNARLI definition,
7518 -- task body procedures have a profile (Arg : System.Address). That is
7519 -- needed because GNARLI has to use the same access-to-subprogram type
7520 -- for all task types. We depend here on knowing that in GNAT, passing
7521 -- an address argument by value is identical to passing a record value
7522 -- by access (in either case a single pointer is passed), so even though
7523 -- this procedure has the wrong profile. In fact it's all OK, since the
7524 -- callings sequence is identical.
7526 procedure Expand_N_Task_Body (N : Node_Id) is
7527 Loc : constant Source_Ptr := Sloc (N);
7528 Ttyp : constant Entity_Id := Corresponding_Spec (N);
7533 -- Here we start the expansion by generating discriminal declarations
7535 Add_Discriminal_Declarations (Declarations (N), Ttyp, Name_uTask, Loc);
7537 -- Add a call to Abort_Undefer at the very beginning of the task
7538 -- body since this body is called with abort still deferred.
7540 if Abort_Allowed then
7541 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
7543 (First (Statements (Handled_Statement_Sequence (N))), Call);
7547 -- The statement part has already been protected with an at_end and
7548 -- cleanup actions. The call to Complete_Activation must be placed
7549 -- at the head of the sequence of statements of that block. The
7550 -- declarations have been merged in this sequence of statements but
7551 -- the first real statement is accessible from the First_Real_Statement
7552 -- field (which was set for exactly this purpose).
7554 if Restricted_Profile then
7555 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
7557 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
7561 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
7565 Make_Subprogram_Body (Loc,
7566 Specification => Build_Task_Proc_Specification (Ttyp),
7567 Declarations => Declarations (N),
7568 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
7570 -- If the task contains generic instantiations, cleanup actions
7571 -- are delayed until after instantiation. Transfer the activation
7572 -- chain to the subprogram, to insure that the activation call is
7573 -- properly generated. It the task body contains inner tasks, indicate
7574 -- that the subprogram is a task master.
7576 if Delay_Cleanups (Ttyp) then
7577 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
7578 Set_Is_Task_Master (New_N, Is_Task_Master (N));
7584 -- Set elaboration flag immediately after task body. If the body
7585 -- is a subunit, the flag is set in the declarative part that
7586 -- contains the stub.
7588 if Nkind (Parent (N)) /= N_Subunit then
7590 Make_Assignment_Statement (Loc,
7592 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
7593 Expression => New_Reference_To (Standard_True, Loc)));
7596 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies
7597 -- after the task body. At this point the entry specs have been
7598 -- created, frozen and included in the dispatch table for the task
7601 pragma Assert (Present (Corresponding_Record_Type (Ttyp)));
7603 if Ada_Version >= Ada_05
7604 and then Present (Task_Definition (Parent (Ttyp)))
7605 and then Present (Abstract_Interfaces
7606 (Corresponding_Record_Type (Ttyp)))
7609 Current_Node : Node_Id;
7610 Vis_Decl : Node_Id :=
7611 First (Visible_Declarations (Task_Definition (Parent (Ttyp))));
7612 Wrap_Body : Node_Id;
7615 if Nkind (Parent (N)) = N_Subunit then
7616 Current_Node := Corresponding_Stub (Parent (N));
7621 -- Examine the visible declarations of the task type,
7622 -- looking for an entry declaration. We do not consider
7623 -- entry families since they can not have dispatching
7624 -- operations, thus they do not need entry wrappers.
7626 while Present (Vis_Decl) loop
7627 if Nkind (Vis_Decl) = N_Entry_Declaration
7628 and then Ekind (Defining_Identifier (Vis_Decl)) = E_Entry
7631 -- Create the specification of the wrapper
7634 Build_Wrapper_Body (Loc,
7635 Proc_Nam => Defining_Identifier (Vis_Decl),
7636 Obj_Typ => Corresponding_Record_Type (Ttyp),
7637 Formals => Parameter_Specifications (Vis_Decl));
7639 if Wrap_Body /= Empty then
7640 Insert_After (Current_Node, Wrap_Body);
7641 Current_Node := Wrap_Body;
7643 Analyze (Wrap_Body);
7651 end Expand_N_Task_Body;
7653 ------------------------------------
7654 -- Expand_N_Task_Type_Declaration --
7655 ------------------------------------
7657 -- We have several things to do. First we must create a Boolean flag used
7658 -- to mark if the body is elaborated yet. This variable gets set to True
7659 -- when the body of the task is elaborated (we can't rely on the normal
7660 -- ABE mechanism for the task body, since we need to pass an access to
7661 -- this elaboration boolean to the runtime routines).
7663 -- taskE : aliased Boolean := False;
7665 -- Next a variable is declared to hold the task stack size (either
7666 -- the default : Unspecified_Size, or a value that is set by a pragma
7667 -- Storage_Size). If the value of the pragma Storage_Size is static, then
7668 -- the variable is initialized with this value:
7670 -- taskZ : Size_Type := Unspecified_Size;
7672 -- taskZ : Size_Type := Size_Type (size_expression);
7674 -- Next we create a corresponding record type declaration used to represent
7675 -- values of this task. The general form of this type declaration is
7677 -- type taskV (discriminants) is record
7678 -- _Task_Id : Task_Id;
7679 -- entry_family : array (bounds) of Void;
7680 -- _Priority : Integer := priority_expression;
7681 -- _Size : Size_Type := Size_Type (size_expression);
7682 -- _Task_Info : Task_Info_Type := task_info_expression;
7685 -- The discriminants are present only if the corresponding task type has
7686 -- discriminants, and they exactly mirror the task type discriminants.
7688 -- The Id field is always present. It contains the Task_Id value, as
7689 -- set by the call to Create_Task. Note that although the task is
7690 -- limited, the task value record type is not limited, so there is no
7691 -- problem in passing this field as an out parameter to Create_Task.
7693 -- One entry_family component is present for each entry family in the
7694 -- task definition. The bounds correspond to the bounds of the entry
7695 -- family (which may depend on discriminants). The element type is
7696 -- void, since we only need the bounds information for determining
7697 -- the entry index. Note that the use of an anonymous array would
7698 -- normally be illegal in this context, but this is a parser check,
7699 -- and the semantics is quite prepared to handle such a case.
7701 -- The _Size field is present only if a Storage_Size pragma appears in
7702 -- the task definition. The expression captures the argument that was
7703 -- present in the pragma, and is used to override the task stack size
7704 -- otherwise associated with the task type.
7706 -- The _Priority field is present only if a Priority or Interrupt_Priority
7707 -- pragma appears in the task definition. The expression captures the
7708 -- argument that was present in the pragma, and is used to provide
7709 -- the Size parameter to the call to Create_Task.
7711 -- The _Task_Info field is present only if a Task_Info pragma appears in
7712 -- the task definition. The expression captures the argument that was
7713 -- present in the pragma, and is used to provide the Task_Image parameter
7714 -- to the call to Create_Task.
7716 -- When a task is declared, an instance of the task value record is
7717 -- created. The elaboration of this declaration creates the correct
7718 -- bounds for the entry families, and also evaluates the size, priority,
7719 -- and task_Info expressions if needed. The initialization routine for
7720 -- the task type itself then calls Create_Task with appropriate
7721 -- parameters to initialize the value of the Task_Id field.
7723 -- Note: the address of this record is passed as the "Discriminants"
7724 -- parameter for Create_Task. Since Create_Task merely passes this onto
7725 -- the body procedure, it does not matter that it does not quite match
7726 -- the GNARLI model of what is being passed (the record contains more
7727 -- than just the discriminants, but the discriminants can be found from
7728 -- the record value).
7730 -- The Entity_Id for this created record type is placed in the
7731 -- Corresponding_Record_Type field of the associated task type entity.
7733 -- Next we create a procedure specification for the task body procedure:
7735 -- procedure taskB (_Task : access taskV);
7737 -- Note that this must come after the record type declaration, since
7738 -- the spec refers to this type. It turns out that the initialization
7739 -- procedure for the value type references the task body spec, but that's
7740 -- fine, since it won't be generated till the freeze point for the type,
7741 -- which is certainly after the task body spec declaration.
7743 -- Finally, we set the task index value field of the entry attribute in
7744 -- the case of a simple entry.
7746 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
7747 Loc : constant Source_Ptr := Sloc (N);
7748 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
7749 Tasknm : constant Name_Id := Chars (Tasktyp);
7750 Taskdef : constant Node_Id := Task_Definition (N);
7752 Proc_Spec : Node_Id;
7754 Rec_Ent : Entity_Id;
7756 Elab_Decl : Node_Id;
7757 Size_Decl : Node_Id;
7758 Body_Decl : Node_Id;
7759 Task_Size : Node_Id;
7760 Ent_Stack : Entity_Id;
7761 Decl_Stack : Node_Id;
7764 -- If already expanded, nothing to do
7766 if Present (Corresponding_Record_Type (Tasktyp)) then
7770 -- Here we will do the expansion
7772 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
7774 -- Ada 2005 (AI-345): Propagate the attribute that contains the list
7775 -- of implemented interfaces.
7777 Set_Interface_List (Type_Definition (Rec_Decl), Interface_List (N));
7779 Rec_Ent := Defining_Identifier (Rec_Decl);
7780 Cdecls := Component_Items (Component_List
7781 (Type_Definition (Rec_Decl)));
7783 Qualify_Entity_Names (N);
7785 -- First create the elaboration variable
7788 Make_Object_Declaration (Loc,
7789 Defining_Identifier =>
7790 Make_Defining_Identifier (Sloc (Tasktyp),
7791 Chars => New_External_Name (Tasknm, 'E')),
7792 Aliased_Present => True,
7793 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
7794 Expression => New_Reference_To (Standard_False, Loc));
7795 Insert_After (N, Elab_Decl);
7797 -- Next create the declaration of the size variable (tasknmZ)
7799 Set_Storage_Size_Variable (Tasktyp,
7800 Make_Defining_Identifier (Sloc (Tasktyp),
7801 Chars => New_External_Name (Tasknm, 'Z')));
7803 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) and then
7804 Is_Static_Expression (Expression (First (
7805 Pragma_Argument_Associations (Find_Task_Or_Protected_Pragma (
7806 Taskdef, Name_Storage_Size)))))
7809 Make_Object_Declaration (Loc,
7810 Defining_Identifier => Storage_Size_Variable (Tasktyp),
7811 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
7813 Convert_To (RTE (RE_Size_Type),
7816 Pragma_Argument_Associations (
7817 Find_Task_Or_Protected_Pragma
7818 (Taskdef, Name_Storage_Size)))))));
7822 Make_Object_Declaration (Loc,
7823 Defining_Identifier => Storage_Size_Variable (Tasktyp),
7824 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
7825 Expression => New_Reference_To (RTE (RE_Unspecified_Size), Loc));
7828 Insert_After (Elab_Decl, Size_Decl);
7830 -- Next build the rest of the corresponding record declaration.
7831 -- This is done last, since the corresponding record initialization
7832 -- procedure will reference the previously created entities.
7834 -- Fill in the component declarations -- first the _Task_Id field
7837 Make_Component_Declaration (Loc,
7838 Defining_Identifier =>
7839 Make_Defining_Identifier (Loc, Name_uTask_Id),
7840 Component_Definition =>
7841 Make_Component_Definition (Loc,
7842 Aliased_Present => False,
7843 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
7846 -- Declare static ATCB (that is, created by the expander) if we
7847 -- are using the Restricted run time.
7849 if Restricted_Profile then
7851 Make_Component_Declaration (Loc,
7852 Defining_Identifier =>
7853 Make_Defining_Identifier (Loc, Name_uATCB),
7855 Component_Definition =>
7856 Make_Component_Definition (Loc,
7857 Aliased_Present => True,
7858 Subtype_Indication => Make_Subtype_Indication (Loc,
7859 Subtype_Mark => New_Occurrence_Of
7860 (RTE (RE_Ada_Task_Control_Block), Loc),
7863 Make_Index_Or_Discriminant_Constraint (Loc,
7865 New_List (Make_Integer_Literal (Loc, 0)))))));
7869 -- Declare static stack (that is, created by the expander) if we
7870 -- are using the Restricted run time on a bare board configuration.
7872 if Restricted_Profile
7873 and then Preallocated_Stacks_On_Target
7875 -- First we need to extract the appropriate stack size
7877 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
7879 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
7880 Task_Size := Relocate_Node (
7882 Pragma_Argument_Associations (
7883 Find_Task_Or_Protected_Pragma
7884 (Taskdef, Name_Storage_Size)))));
7887 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
7890 Decl_Stack := Make_Component_Declaration (Loc,
7891 Defining_Identifier => Ent_Stack,
7893 Component_Definition =>
7894 Make_Component_Definition (Loc,
7895 Aliased_Present => True,
7896 Subtype_Indication => Make_Subtype_Indication (Loc,
7898 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
7901 Make_Index_Or_Discriminant_Constraint (Loc,
7902 Constraints => New_List (Make_Range (Loc,
7903 Low_Bound => Make_Integer_Literal (Loc, 1),
7904 High_Bound => Convert_To (RTE (RE_Storage_Offset),
7907 Append_To (Cdecls, Decl_Stack);
7909 -- The appropriate alignment for the stack is ensured by the
7910 -- run-time code in charge of task creation.
7914 -- Add components for entry families
7916 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
7918 -- Add the _Priority component if a Priority pragma is present
7920 if Present (Taskdef) and then Has_Priority_Pragma (Taskdef) then
7922 Prag : constant Node_Id :=
7923 Find_Task_Or_Protected_Pragma (Taskdef, Name_Priority);
7927 Expr := First (Pragma_Argument_Associations (Prag));
7929 if Nkind (Expr) = N_Pragma_Argument_Association then
7930 Expr := Expression (Expr);
7933 Expr := New_Copy_Tree (Expr);
7935 -- Add conversion to proper type to do range check if required
7936 -- Note that for runtime units, we allow out of range interrupt
7937 -- priority values to be used in a priority pragma. This is for
7938 -- the benefit of some versions of System.Interrupts which use
7939 -- a special server task with maximum interrupt priority.
7941 if Chars (Prag) = Name_Priority
7942 and then not GNAT_Mode
7944 Rewrite (Expr, Convert_To (RTE (RE_Priority), Expr));
7946 Rewrite (Expr, Convert_To (RTE (RE_Any_Priority), Expr));
7950 Make_Component_Declaration (Loc,
7951 Defining_Identifier =>
7952 Make_Defining_Identifier (Loc, Name_uPriority),
7953 Component_Definition =>
7954 Make_Component_Definition (Loc,
7955 Aliased_Present => False,
7956 Subtype_Indication => New_Reference_To (Standard_Integer,
7958 Expression => Expr));
7962 -- Add the _Task_Size component if a Storage_Size pragma is present
7964 if Present (Taskdef)
7965 and then Has_Storage_Size_Pragma (Taskdef)
7968 Make_Component_Declaration (Loc,
7969 Defining_Identifier =>
7970 Make_Defining_Identifier (Loc, Name_uSize),
7972 Component_Definition =>
7973 Make_Component_Definition (Loc,
7974 Aliased_Present => False,
7975 Subtype_Indication => New_Reference_To (RTE (RE_Size_Type),
7979 Convert_To (RTE (RE_Size_Type),
7982 Pragma_Argument_Associations (
7983 Find_Task_Or_Protected_Pragma
7984 (Taskdef, Name_Storage_Size))))))));
7987 -- Add the _Task_Info component if a Task_Info pragma is present
7989 if Present (Taskdef) and then Has_Task_Info_Pragma (Taskdef) then
7991 Make_Component_Declaration (Loc,
7992 Defining_Identifier =>
7993 Make_Defining_Identifier (Loc, Name_uTask_Info),
7995 Component_Definition =>
7996 Make_Component_Definition (Loc,
7997 Aliased_Present => False,
7998 Subtype_Indication =>
7999 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
8001 Expression => New_Copy (
8003 Pragma_Argument_Associations (
8004 Find_Task_Or_Protected_Pragma
8005 (Taskdef, Name_Task_Info)))))));
8008 Insert_After (Size_Decl, Rec_Decl);
8010 -- Analyze the record declaration immediately after construction,
8011 -- because the initialization procedure is needed for single task
8012 -- declarations before the next entity is analyzed.
8016 -- Create the declaration of the task body procedure
8018 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
8020 Make_Subprogram_Declaration (Loc,
8021 Specification => Proc_Spec);
8023 Insert_After (Rec_Decl, Body_Decl);
8025 -- The subprogram does not comes from source, so we have to indicate
8026 -- the need for debugging information explicitly.
8028 Set_Needs_Debug_Info
8029 (Defining_Entity (Proc_Spec), Comes_From_Source (Original_Node (N)));
8031 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs
8032 -- before the corresponding record has been frozen.
8034 if Ada_Version >= Ada_05
8035 and then Present (Taskdef)
8036 and then Present (Corresponding_Record_Type
8037 (Defining_Identifier (Parent (Taskdef))))
8038 and then Present (Abstract_Interfaces
8039 (Corresponding_Record_Type
8040 (Defining_Identifier (Parent (Taskdef)))))
8043 Current_Node : Node_Id := Rec_Decl;
8044 Vis_Decl : Node_Id := First (Visible_Declarations (Taskdef));
8045 Wrap_Spec : Node_Id;
8049 -- Examine the visible declarations of the task type,
8050 -- looking for an entry declaration. We do not consider
8051 -- entry families since they can not have dispatching
8052 -- operations, thus they do not need entry wrappers.
8054 while Present (Vis_Decl) loop
8055 if Nkind (Vis_Decl) = N_Entry_Declaration
8056 and then Ekind (Defining_Identifier (Vis_Decl)) = E_Entry
8059 Build_Wrapper_Spec (Loc,
8060 Proc_Nam => Defining_Identifier (Vis_Decl),
8061 Obj_Typ => Etype (Rec_Ent),
8062 Formals => Parameter_Specifications (Vis_Decl));
8064 if Wrap_Spec /= Empty then
8066 Make_Subprogram_Declaration (Loc,
8067 Specification => Wrap_Spec);
8069 Insert_After (Current_Node, New_N);
8070 Current_Node := New_N;
8081 -- Ada 2005 (AI-345): We must defer freezing to allow further
8082 -- declaration of primitive subprograms covering task interfaces
8084 if Ada_Version <= Ada_95 then
8086 -- Now we can freeze the corresponding record. This needs manually
8087 -- freezing, since it is really part of the task type, and the task
8088 -- type is frozen at this stage. We of course need the initialization
8089 -- procedure for this corresponding record type and we won't get it
8090 -- in time if we don't freeze now.
8093 L : constant List_Id := Freeze_Entity (Rec_Ent, Loc);
8096 if Is_Non_Empty_List (L) then
8097 Insert_List_After (Body_Decl, L);
8102 -- Complete the expansion of access types to the current task
8103 -- type, if any were declared.
8105 Expand_Previous_Access_Type (Tasktyp);
8106 end Expand_N_Task_Type_Declaration;
8108 -------------------------------
8109 -- Expand_N_Timed_Entry_Call --
8110 -------------------------------
8112 -- A timed entry call in normal case is not implemented using ATC
8113 -- mechanism anymore for efficiency reason.
8123 -- is expanded as follow:
8125 -- 1) When T.E is a task entry_call;
8129 -- X : Task_Entry_Index := <entry index>;
8130 -- DX : Duration := To_Duration (D);
8131 -- M : Delay_Mode := <discriminant>;
8132 -- P : parms := (parm, parm, parm);
8135 -- Timed_Protected_Entry_Call (<acceptor-task>, X, P'Address,
8144 -- 2) When T.E is a protected entry_call;
8148 -- X : Protected_Entry_Index := <entry index>;
8149 -- DX : Duration := To_Duration (D);
8150 -- M : Delay_Mode := <discriminant>;
8151 -- P : parms := (parm, parm, parm);
8154 -- Timed_Protected_Entry_Call (<object>'unchecked_access, X,
8155 -- P'Address, DX, M, B);
8163 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
8164 Loc : constant Source_Ptr := Sloc (N);
8167 Entry_Call_Statement (Entry_Call_Alternative (N));
8168 E_Stats : constant List_Id :=
8169 Statements (Entry_Call_Alternative (N));
8170 D_Stat : constant Node_Id :=
8171 Delay_Statement (Delay_Alternative (N));
8172 D_Stats : constant List_Id :=
8173 Statements (Delay_Alternative (N));
8196 -- The arguments in the call may require dynamic allocation, and the
8197 -- call statement may have been transformed into a block. The block
8198 -- may contain additional declarations for internal entities, and the
8199 -- original call is found by sequential search.
8201 if Nkind (E_Call) = N_Block_Statement then
8202 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
8204 while Nkind (E_Call) /= N_Procedure_Call_Statement
8205 and then Nkind (E_Call) /= N_Entry_Call_Statement
8211 -- Build an entry call using Simple_Entry_Call. We will use this as the
8212 -- base for creating appropriate calls.
8214 Extract_Entry (E_Call, Concval, Ename, Index);
8215 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
8217 Stmts := Statements (Handled_Statement_Sequence (E_Call));
8218 Decls := Declarations (E_Call);
8224 Dtyp := Base_Type (Etype (Expression (D_Stat)));
8226 -- Use the type of the delay expression (Calendar or Real_Time)
8227 -- to generate the appropriate conversion.
8229 if Nkind (D_Stat) = N_Delay_Relative_Statement then
8230 Disc := Make_Integer_Literal (Loc, 0);
8231 Conv := Relocate_Node (Expression (D_Stat));
8233 elsif Is_RTE (Dtyp, RO_CA_Time) then
8234 Disc := Make_Integer_Literal (Loc, 1);
8235 Conv := Make_Function_Call (Loc,
8236 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
8237 New_List (New_Copy (Expression (D_Stat))));
8239 else pragma Assert (Is_RTE (Dtyp, RO_RT_Time));
8240 Disc := Make_Integer_Literal (Loc, 2);
8241 Conv := Make_Function_Call (Loc,
8242 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
8243 New_List (New_Copy (Expression (D_Stat))));
8246 -- Create Duration and Delay_Mode objects for passing a delay value
8248 D := Make_Defining_Identifier (Loc, New_Internal_Name ('D'));
8249 M := Make_Defining_Identifier (Loc, New_Internal_Name ('M'));
8252 Make_Object_Declaration (Loc,
8253 Defining_Identifier => D,
8254 Object_Definition => New_Reference_To (Standard_Duration, Loc)));
8257 Make_Object_Declaration (Loc,
8258 Defining_Identifier => M,
8259 Object_Definition => New_Reference_To (Standard_Integer, Loc),
8260 Expression => Disc));
8262 B := Make_Defining_Identifier (Loc, Name_uB);
8264 -- Create a boolean object used for a return parameter
8267 Make_Object_Declaration (Loc,
8268 Defining_Identifier => B,
8269 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
8271 Stmt := First (Stmts);
8273 -- Skip assignments to temporaries created for in-out parameters.
8274 -- This makes unwarranted assumptions about the shape of the expanded
8275 -- tree for the call, and should be cleaned up ???
8277 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
8281 -- Do the assignement at this stage only because the evaluation of the
8282 -- expression must not occur before (see ACVC C97302A).
8284 Insert_Before (Stmt,
8285 Make_Assignment_Statement (Loc,
8286 Name => New_Reference_To (D, Loc),
8287 Expression => Conv));
8291 Parms := Parameter_Associations (Call);
8293 -- For a protected type, we build a Timed_Protected_Entry_Call
8295 if Is_Protected_Type (Etype (Concval)) then
8297 -- Create a new call statement
8299 Parm := First (Parms);
8301 while Present (Parm)
8302 and then not Is_RTE (Etype (Parm), RE_Call_Modes)
8307 Dummy := Remove_Next (Next (Parm));
8309 -- Remove garbage is following the Cancel_Param if present
8311 Dummy := Next (Parm);
8313 -- Remove the mode of the Protected_Entry_Call call, then remove the
8314 -- Communication_Block of the Protected_Entry_Call call, and finally
8315 -- add Duration and a Delay_Mode parameter
8317 pragma Assert (Present (Parm));
8318 Rewrite (Parm, New_Reference_To (D, Loc));
8320 Rewrite (Dummy, New_Reference_To (M, Loc));
8322 -- Add a Boolean flag for successful entry call
8324 Append_To (Parms, New_Reference_To (B, Loc));
8327 or else Restriction_Active (No_Entry_Queue) = False
8328 or else Number_Entries (Etype (Concval)) > 1
8331 Make_Procedure_Call_Statement (Loc,
8333 New_Reference_To (RTE (RE_Timed_Protected_Entry_Call), Loc),
8334 Parameter_Associations => Parms));
8337 Parm := First (Parms);
8339 while Present (Parm)
8340 and then not Is_RTE (Etype (Parm), RE_Protected_Entry_Index)
8348 Make_Procedure_Call_Statement (Loc,
8349 Name => New_Reference_To (
8350 RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
8351 Parameter_Associations => Parms));
8354 -- For the task case, build a Timed_Task_Entry_Call
8357 -- Create a new call statement
8359 Append_To (Parms, New_Reference_To (D, Loc));
8360 Append_To (Parms, New_Reference_To (M, Loc));
8361 Append_To (Parms, New_Reference_To (B, Loc));
8364 Make_Procedure_Call_Statement (Loc,
8365 Name => New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
8366 Parameter_Associations => Parms));
8371 Make_Implicit_If_Statement (N,
8372 Condition => New_Reference_To (B, Loc),
8373 Then_Statements => E_Stats,
8374 Else_Statements => D_Stats));
8377 Make_Block_Statement (Loc,
8378 Declarations => Decls,
8379 Handled_Statement_Sequence =>
8380 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8383 end Expand_N_Timed_Entry_Call;
8385 ----------------------------------------
8386 -- Expand_Protected_Body_Declarations --
8387 ----------------------------------------
8389 -- Part of the expansion of a protected body involves the creation of
8390 -- a declaration that can be referenced from the statement sequences of
8391 -- the entry bodies:
8395 -- This declaration is inserted in the declarations of the service
8396 -- entries procedure for the protected body, and it is important that
8397 -- it be inserted before the statements of the entry body statement
8398 -- sequences are analyzed. Thus it would be too late to create this
8399 -- declaration in the Expand_N_Protected_Body routine, which is why
8400 -- there is a separate procedure to be called directly from Sem_Ch9.
8402 -- Ann is used to hold the address of the record containing the parameters
8403 -- (see Expand_N_Entry_Call for more details on how this record is built).
8404 -- References to the parameters do an unchecked conversion of this address
8405 -- to a pointer to the required record type, and then access the field that
8406 -- holds the value of the required parameter. The entity for the address
8407 -- variable is held as the top stack element (i.e. the last element) of the
8408 -- Accept_Address stack in the corresponding entry entity, and this element
8409 -- must be set in place before the statements are processed.
8411 -- No stack is needed for entry bodies, since they cannot be nested, but
8412 -- it is kept for consistency between protected and task entries. The
8413 -- stack will never contain more than one element. There is also only one
8414 -- such variable for a given protected body, but this is placed on the
8415 -- Accept_Address stack of all of the entries, again for consistency.
8417 -- To expand the requeue statement, a label is provided at the end of
8418 -- the loop in the entry service routine created by the expander (see
8419 -- Expand_N_Protected_Body for details), so that the statement can be
8420 -- skipped after the requeue is complete. This label is created during the
8421 -- expansion of the entry body, which will take place after the expansion
8422 -- of the requeue statements that it contains, so a placeholder defining
8423 -- identifier is associated with the task type here.
8425 -- Another label is provided following case statement created by the
8426 -- expander. This label is need for implementing return statement from
8427 -- entry body so that a return can be expanded as a goto to this label.
8428 -- This label is created during the expansion of the entry body, which
8429 -- will take place after the expansion of the return statements that it
8430 -- contains. Therefore, just like the label for expanding requeues, we
8431 -- need another placeholder for the label.
8433 procedure Expand_Protected_Body_Declarations
8435 Spec_Id : Entity_Id)
8440 if No_Run_Time_Mode then
8441 Error_Msg_CRT ("protected body", N);
8444 elsif Expander_Active then
8446 -- Associate privals with the first subprogram or entry
8447 -- body to be expanded. These are used to expand references
8448 -- to private data objects.
8450 Op := First_Protected_Operation (Declarations (N));
8452 if Present (Op) then
8453 Set_Discriminals (Parent (Spec_Id));
8454 Set_Privals (Parent (Spec_Id), Op, Sloc (N));
8457 end Expand_Protected_Body_Declarations;
8459 -------------------------
8460 -- External_Subprogram --
8461 -------------------------
8463 function External_Subprogram (E : Entity_Id) return Entity_Id is
8464 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
8465 Decl : constant Node_Id := Unit_Declaration_Node (E);
8468 -- If the protected operation is defined in the visible part of the
8469 -- protected type, or if it is an interrupt handler, the internal and
8470 -- external subprograms follow each other on the entity chain. If the
8471 -- operation is defined in the private part of the type, there is no
8472 -- need for a separate locking version of the operation, and internal
8473 -- calls use the protected_body_subprogram directly.
8475 if List_Containing (Decl) = Visible_Declarations (Parent (Decl))
8476 or else Is_Interrupt_Handler (E)
8478 return Next_Entity (Subp);
8482 end External_Subprogram;
8488 procedure Extract_Entry
8490 Concval : out Node_Id;
8491 Ename : out Node_Id;
8492 Index : out Node_Id)
8494 Nam : constant Node_Id := Name (N);
8497 -- For a simple entry, the name is a selected component, with the
8498 -- prefix being the task value, and the selector being the entry.
8500 if Nkind (Nam) = N_Selected_Component then
8501 Concval := Prefix (Nam);
8502 Ename := Selector_Name (Nam);
8505 -- For a member of an entry family, the name is an indexed
8506 -- component where the prefix is a selected component,
8507 -- whose prefix in turn is the task value, and whose
8508 -- selector is the entry family. The single expression in
8509 -- the expressions list of the indexed component is the
8510 -- subscript for the family.
8513 pragma Assert (Nkind (Nam) = N_Indexed_Component);
8514 Concval := Prefix (Prefix (Nam));
8515 Ename := Selector_Name (Prefix (Nam));
8516 Index := First (Expressions (Nam));
8524 function Family_Offset
8528 Ttyp : Entity_Id) return Node_Id
8530 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
8531 -- If one of the bounds is a reference to a discriminant, replace
8532 -- with corresponding discriminal of type. Within the body of a task
8533 -- retrieve the renamed discriminant by simple visibility, using its
8534 -- generated name. Within a protected object, find the original dis-
8535 -- criminant and replace it with the discriminal of the current prot-
8538 ------------------------------
8539 -- Convert_Discriminant_Ref --
8540 ------------------------------
8542 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
8543 Loc : constant Source_Ptr := Sloc (Bound);
8548 if Is_Entity_Name (Bound)
8549 and then Ekind (Entity (Bound)) = E_Discriminant
8551 if Is_Task_Type (Ttyp)
8552 and then Has_Completion (Ttyp)
8554 B := Make_Identifier (Loc, Chars (Entity (Bound)));
8555 Find_Direct_Name (B);
8557 elsif Is_Protected_Type (Ttyp) then
8558 D := First_Discriminant (Ttyp);
8560 while Chars (D) /= Chars (Entity (Bound)) loop
8561 Next_Discriminant (D);
8564 B := New_Reference_To (Discriminal (D), Loc);
8567 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
8570 elsif Nkind (Bound) = N_Attribute_Reference then
8574 B := New_Copy_Tree (Bound);
8578 Make_Attribute_Reference (Loc,
8579 Attribute_Name => Name_Pos,
8580 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
8581 Expressions => New_List (B));
8582 end Convert_Discriminant_Ref;
8584 -- Start of processing for Family_Offset
8588 Make_Op_Subtract (Loc,
8589 Left_Opnd => Convert_Discriminant_Ref (Hi),
8590 Right_Opnd => Convert_Discriminant_Ref (Lo));
8597 function Family_Size
8601 Ttyp : Entity_Id) return Node_Id
8606 if Is_Task_Type (Ttyp) then
8607 Ityp := RTE (RE_Task_Entry_Index);
8609 Ityp := RTE (RE_Protected_Entry_Index);
8613 Make_Attribute_Reference (Loc,
8614 Prefix => New_Reference_To (Ityp, Loc),
8615 Attribute_Name => Name_Max,
8616 Expressions => New_List (
8619 Family_Offset (Loc, Hi, Lo, Ttyp),
8621 Make_Integer_Literal (Loc, 1)),
8622 Make_Integer_Literal (Loc, 0)));
8625 -----------------------------------
8626 -- Find_Task_Or_Protected_Pragma --
8627 -----------------------------------
8629 function Find_Task_Or_Protected_Pragma
8631 P : Name_Id) return Node_Id
8636 N := First (Visible_Declarations (T));
8638 while Present (N) loop
8639 if Nkind (N) = N_Pragma then
8640 if Chars (N) = P then
8643 elsif P = Name_Priority
8644 and then Chars (N) = Name_Interrupt_Priority
8657 N := First (Private_Declarations (T));
8659 while Present (N) loop
8660 if Nkind (N) = N_Pragma then
8661 if Chars (N) = P then
8664 elsif P = Name_Priority
8665 and then Chars (N) = Name_Interrupt_Priority
8678 raise Program_Error;
8679 end Find_Task_Or_Protected_Pragma;
8681 -------------------------------
8682 -- First_Protected_Operation --
8683 -------------------------------
8685 function First_Protected_Operation (D : List_Id) return Node_Id is
8689 First_Op := First (D);
8690 while Present (First_Op)
8691 and then Nkind (First_Op) /= N_Subprogram_Body
8692 and then Nkind (First_Op) /= N_Entry_Body
8698 end First_Protected_Operation;
8700 --------------------------------
8701 -- Index_Constant_Declaration --
8702 --------------------------------
8704 function Index_Constant_Declaration
8706 Index_Id : Entity_Id;
8707 Prot : Entity_Id) return List_Id
8709 Loc : constant Source_Ptr := Sloc (N);
8710 Decls : constant List_Id := New_List;
8711 Index_Con : constant Entity_Id := Entry_Index_Constant (Index_Id);
8712 Index_Typ : Entity_Id;
8714 Hi : Node_Id := Type_High_Bound (Etype (Index_Id));
8715 Lo : Node_Id := Type_Low_Bound (Etype (Index_Id));
8717 function Replace_Discriminant (Bound : Node_Id) return Node_Id;
8718 -- The bounds of the entry index may depend on discriminants, so
8719 -- each declaration of an entry_index_constant must have its own
8720 -- subtype declaration, using the local renaming of the object discri-
8723 --------------------------
8724 -- Replace_Discriminant --
8725 --------------------------
8727 function Replace_Discriminant (Bound : Node_Id) return Node_Id is
8729 if Nkind (Bound) = N_Identifier
8730 and then Ekind (Entity (Bound)) = E_Constant
8731 and then Present (Discriminal_Link (Entity (Bound)))
8733 return Make_Identifier (Loc, Chars (Entity (Bound)));
8735 return Duplicate_Subexpr (Bound);
8737 end Replace_Discriminant;
8739 -- Start of processing for Index_Constant_Declaration
8742 Set_Discriminal_Link (Index_Con, Index_Id);
8745 Original_Node (Discrete_Subtype_Definition (Parent (Index_Id))))
8747 -- Simple case: entry family is given by a subtype mark, and index
8748 -- constant has the same type, no replacement needed.
8750 Index_Typ := Etype (Index_Id);
8753 Hi := Replace_Discriminant (Hi);
8754 Lo := Replace_Discriminant (Lo);
8756 Index_Typ := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
8759 Make_Subtype_Declaration (Loc,
8760 Defining_Identifier => Index_Typ,
8761 Subtype_Indication =>
8762 Make_Subtype_Indication (Loc,
8764 New_Occurrence_Of (Base_Type (Etype (Index_Id)), Loc),
8766 Make_Range_Constraint (Loc,
8767 Range_Expression => Make_Range (Loc, Lo, Hi)))),
8773 Make_Object_Declaration (Loc,
8774 Defining_Identifier => Index_Con,
8775 Constant_Present => True,
8776 Object_Definition => New_Occurrence_Of (Index_Typ, Loc),
8779 Make_Attribute_Reference (Loc,
8780 Prefix => New_Reference_To (Index_Typ, Loc),
8781 Attribute_Name => Name_Val,
8783 Expressions => New_List (
8787 Make_Op_Subtract (Loc,
8788 Left_Opnd => Make_Identifier (Loc, Name_uE),
8790 Entry_Index_Expression (Loc,
8791 Defining_Identifier (N), Empty, Prot)),
8794 Make_Attribute_Reference (Loc,
8795 Prefix => New_Reference_To (Index_Typ, Loc),
8796 Attribute_Name => Name_Pos,
8797 Expressions => New_List (
8798 Make_Attribute_Reference (Loc,
8799 Prefix => New_Reference_To (Index_Typ, Loc),
8800 Attribute_Name => Name_First))))))),
8804 end Index_Constant_Declaration;
8806 --------------------------------
8807 -- Make_Initialize_Protection --
8808 --------------------------------
8810 function Make_Initialize_Protection
8811 (Protect_Rec : Entity_Id) return List_Id
8813 Loc : constant Source_Ptr := Sloc (Protect_Rec);
8817 Ptyp : constant Node_Id :=
8818 Corresponding_Concurrent_Type (Protect_Rec);
8820 L : constant List_Id := New_List;
8821 Has_Entry : constant Boolean := Has_Entries (Ptyp);
8822 Restricted : constant Boolean := Restricted_Profile;
8825 -- We may need two calls to properly initialize the object, one
8826 -- to Initialize_Protection, and possibly one to Install_Handlers
8827 -- if we have a pragma Attach_Handler.
8829 -- Get protected declaration. In the case of a task type declaration,
8830 -- this is simply the parent of the protected type entity.
8831 -- In the single protected object
8832 -- declaration, this parent will be the implicit type, and we can find
8833 -- the corresponding single protected object declaration by
8834 -- searching forward in the declaration list in the tree.
8835 -- ??? I am not sure that the test for N_Single_Protected_Declaration
8836 -- is needed here. Nodes of this type should have been removed
8837 -- during semantic analysis.
8839 Pdec := Parent (Ptyp);
8841 while Nkind (Pdec) /= N_Protected_Type_Declaration
8842 and then Nkind (Pdec) /= N_Single_Protected_Declaration
8847 -- Now we can find the object definition from this declaration
8849 Pdef := Protected_Definition (Pdec);
8851 -- Build the parameter list for the call. Note that _Init is the name
8852 -- of the formal for the object to be initialized, which is the task
8853 -- value record itself.
8857 -- Object parameter. This is a pointer to the object of type
8858 -- Protection used by the GNARL to control the protected object.
8861 Make_Attribute_Reference (Loc,
8863 Make_Selected_Component (Loc,
8864 Prefix => Make_Identifier (Loc, Name_uInit),
8865 Selector_Name => Make_Identifier (Loc, Name_uObject)),
8866 Attribute_Name => Name_Unchecked_Access));
8868 -- Priority parameter. Set to Unspecified_Priority unless there is a
8869 -- priority pragma, in which case we take the value from the pragma,
8870 -- or there is an interrupt pragma and no priority pragma, and we
8871 -- set the ceiling to Interrupt_Priority'Last, an implementation-
8872 -- defined value, see D.3(10).
8875 and then Has_Priority_Pragma (Pdef)
8878 Duplicate_Subexpr_No_Checks
8881 (Pragma_Argument_Associations
8882 (Find_Task_Or_Protected_Pragma (Pdef, Name_Priority))))));
8884 elsif Has_Interrupt_Handler (Ptyp)
8885 or else Has_Attach_Handler (Ptyp)
8887 -- When no priority is specified but an xx_Handler pragma is,
8888 -- we default to System.Interrupts.Default_Interrupt_Priority,
8892 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
8896 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
8900 or else Has_Interrupt_Handler (Ptyp)
8901 or else Has_Attach_Handler (Ptyp)
8903 -- Compiler_Info parameter. This parameter allows entry body
8904 -- procedures and barrier functions to be called from the runtime.
8905 -- It is a pointer to the record generated by the compiler to
8906 -- represent the protected object.
8908 if Has_Entry or else not Restricted then
8910 Make_Attribute_Reference (Loc,
8911 Prefix => Make_Identifier (Loc, Name_uInit),
8912 Attribute_Name => Name_Address));
8916 -- Entry_Bodies parameter. This is a pointer to an array of
8917 -- pointers to the entry body procedures and barrier functions
8918 -- of the object. If the protected type has no entries this
8919 -- object will not exist; in this case, pass a null.
8921 P_Arr := Entry_Bodies_Array (Ptyp);
8924 Make_Attribute_Reference (Loc,
8925 Prefix => New_Reference_To (P_Arr, Loc),
8926 Attribute_Name => Name_Unrestricted_Access));
8929 or else Restriction_Active (No_Entry_Queue) = False
8930 or else Number_Entries (Ptyp) > 1
8932 -- Find index mapping function (clumsy but ok for now)
8934 while Ekind (P_Arr) /= E_Function loop
8935 Next_Entity (P_Arr);
8939 Make_Attribute_Reference (Loc,
8941 New_Reference_To (P_Arr, Loc),
8942 Attribute_Name => Name_Unrestricted_Access));
8945 elsif not Restricted then
8946 Append_To (Args, Make_Null (Loc));
8947 Append_To (Args, Make_Null (Loc));
8951 or else Restriction_Active (No_Entry_Queue) = False
8952 or else Number_Entries (Ptyp) > 1
8955 Make_Procedure_Call_Statement (Loc,
8956 Name => New_Reference_To (
8957 RTE (RE_Initialize_Protection_Entries), Loc),
8958 Parameter_Associations => Args));
8960 elsif not Has_Entry and then Restricted then
8962 Make_Procedure_Call_Statement (Loc,
8963 Name => New_Reference_To (
8964 RTE (RE_Initialize_Protection), Loc),
8965 Parameter_Associations => Args));
8969 Make_Procedure_Call_Statement (Loc,
8970 Name => New_Reference_To (
8971 RTE (RE_Initialize_Protection_Entry), Loc),
8972 Parameter_Associations => Args));
8977 Make_Procedure_Call_Statement (Loc,
8978 Name => New_Reference_To (RTE (RE_Initialize_Protection), Loc),
8979 Parameter_Associations => Args));
8982 if Has_Attach_Handler (Ptyp) then
8984 -- We have a list of N Attach_Handler (ProcI, ExprI),
8985 -- and we have to make the following call:
8986 -- Install_Handlers (_object,
8987 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
8988 -- or, in the case of Ravenscar:
8990 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
8993 Args : constant List_Id := New_List;
8994 Table : constant List_Id := New_List;
8995 Ritem : Node_Id := First_Rep_Item (Ptyp);
8998 if not Restricted then
8999 -- Appends the _object argument
9002 Make_Attribute_Reference (Loc,
9004 Make_Selected_Component (Loc,
9005 Prefix => Make_Identifier (Loc, Name_uInit),
9006 Selector_Name => Make_Identifier (Loc, Name_uObject)),
9007 Attribute_Name => Name_Unchecked_Access));
9010 -- Build the Attach_Handler table argument
9012 while Present (Ritem) loop
9013 if Nkind (Ritem) = N_Pragma
9014 and then Chars (Ritem) = Name_Attach_Handler
9017 Handler : constant Node_Id :=
9018 First (Pragma_Argument_Associations (Ritem));
9020 Interrupt : constant Node_Id := Next (Handler);
9021 Expr : constant Node_Id := Expression (Interrupt);
9025 Make_Aggregate (Loc, Expressions => New_List (
9026 Unchecked_Convert_To
9027 (RTE (RE_System_Interrupt_Id), Expr),
9028 Make_Attribute_Reference (Loc,
9029 Prefix => Make_Selected_Component (Loc,
9030 Make_Identifier (Loc, Name_uInit),
9031 Duplicate_Subexpr_No_Checks
9032 (Expression (Handler))),
9033 Attribute_Name => Name_Access))));
9037 Next_Rep_Item (Ritem);
9040 -- Append the table argument we just built
9042 Append_To (Args, Make_Aggregate (Loc, Table));
9044 -- Append the Install_Handler call to the statements
9047 Make_Procedure_Call_Statement (Loc,
9048 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
9049 Parameter_Associations => Args));
9054 end Make_Initialize_Protection;
9056 ---------------------------
9057 -- Make_Task_Create_Call --
9058 ---------------------------
9060 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
9061 Loc : constant Source_Ptr := Sloc (Task_Rec);
9071 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
9072 Tnam := Chars (Ttyp);
9074 -- Get task declaration. In the case of a task type declaration, this
9075 -- is simply the parent of the task type entity. In the single task
9076 -- declaration, this parent will be the implicit type, and we can find
9077 -- the corresponding single task declaration by searching forward in
9078 -- the declaration list in the tree.
9079 -- ??? I am not sure that the test for N_Single_Task_Declaration
9080 -- is needed here. Nodes of this type should have been removed
9081 -- during semantic analysis.
9083 Tdec := Parent (Ttyp);
9085 while Nkind (Tdec) /= N_Task_Type_Declaration
9086 and then Nkind (Tdec) /= N_Single_Task_Declaration
9091 -- Now we can find the task definition from this declaration
9093 Tdef := Task_Definition (Tdec);
9095 -- Build the parameter list for the call. Note that _Init is the name
9096 -- of the formal for the object to be initialized, which is the task
9097 -- value record itself.
9101 -- Priority parameter. Set to Unspecified_Priority unless there is a
9102 -- priority pragma, in which case we take the value from the pragma.
9104 if Present (Tdef) and then Has_Priority_Pragma (Tdef) then
9106 Make_Selected_Component (Loc,
9107 Prefix => Make_Identifier (Loc, Name_uInit),
9108 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
9111 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
9114 -- Optional Stack parameter
9116 if Restricted_Profile then
9118 -- If the stack has been preallocated by the expander then
9119 -- pass its address. Otherwise, pass a null address.
9121 if Preallocated_Stacks_On_Target then
9123 Make_Attribute_Reference (Loc,
9124 Prefix => Make_Selected_Component (Loc,
9125 Prefix => Make_Identifier (Loc, Name_uInit),
9127 Make_Identifier (Loc, Name_uStack)),
9128 Attribute_Name => Name_Address));
9132 New_Reference_To (RTE (RE_Null_Address), Loc));
9136 -- Size parameter. If no Storage_Size pragma is present, then
9137 -- the size is taken from the taskZ variable for the type, which
9138 -- is either Unspecified_Size, or has been reset by the use of
9139 -- a Storage_Size attribute definition clause. If a pragma is
9140 -- present, then the size is taken from the _Size field of the
9141 -- task value record, which was set from the pragma value.
9144 and then Has_Storage_Size_Pragma (Tdef)
9147 Make_Selected_Component (Loc,
9148 Prefix => Make_Identifier (Loc, Name_uInit),
9149 Selector_Name => Make_Identifier (Loc, Name_uSize)));
9153 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
9156 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
9157 -- Task_Info pragma, in which case we take the value from the pragma.
9160 and then Has_Task_Info_Pragma (Tdef)
9163 Make_Selected_Component (Loc,
9164 Prefix => Make_Identifier (Loc, Name_uInit),
9165 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
9169 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
9172 if not Restricted_Profile then
9174 -- Number of entries. This is an expression of the form:
9176 -- n + _Init.a'Length + _Init.a'B'Length + ...
9178 -- where a,b... are the entry family names for the task definition
9180 Ecount := Build_Entry_Count_Expression (
9182 Component_Items (Component_List (
9183 Type_Definition (Parent (
9184 Corresponding_Record_Type (Ttyp))))),
9186 Append_To (Args, Ecount);
9188 -- Master parameter. This is a reference to the _Master parameter of
9189 -- the initialization procedure, except in the case of the pragma
9190 -- Restrictions (No_Task_Hierarchy) where the value is fixed to 3.
9191 -- See comments in System.Tasking.Initialization.Init_RTS for the
9194 if Restriction_Active (No_Task_Hierarchy) = False then
9195 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
9197 Append_To (Args, Make_Integer_Literal (Loc, 3));
9201 -- State parameter. This is a pointer to the task body procedure. The
9202 -- required value is obtained by taking the address of the task body
9203 -- procedure and converting it (with an unchecked conversion) to the
9204 -- type required by the task kernel. For further details, see the
9205 -- description of Expand_Task_Body
9208 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
9209 Make_Attribute_Reference (Loc,
9211 New_Occurrence_Of (Get_Task_Body_Procedure (Ttyp), Loc),
9212 Attribute_Name => Name_Address)));
9214 -- Discriminants parameter. This is just the address of the task
9215 -- value record itself (which contains the discriminant values
9218 Make_Attribute_Reference (Loc,
9219 Prefix => Make_Identifier (Loc, Name_uInit),
9220 Attribute_Name => Name_Address));
9222 -- Elaborated parameter. This is an access to the elaboration Boolean
9225 Make_Attribute_Reference (Loc,
9226 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
9227 Attribute_Name => Name_Unchecked_Access));
9229 -- Chain parameter. This is a reference to the _Chain parameter of
9230 -- the initialization procedure.
9232 Append_To (Args, Make_Identifier (Loc, Name_uChain));
9234 -- Task name parameter. Take this from the _Task_Id parameter to the
9235 -- init call unless there is a Task_Name pragma, in which case we take
9236 -- the value from the pragma.
9239 and then Has_Task_Name_Pragma (Tdef)
9244 Pragma_Argument_Associations (
9245 Find_Task_Or_Protected_Pragma
9246 (Tdef, Name_Task_Name))))));
9249 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
9252 -- Created_Task parameter. This is the _Task_Id field of the task
9256 Make_Selected_Component (Loc,
9257 Prefix => Make_Identifier (Loc, Name_uInit),
9258 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
9260 if Restricted_Profile then
9261 Name := New_Reference_To (RTE (RE_Create_Restricted_Task), Loc);
9263 Name := New_Reference_To (RTE (RE_Create_Task), Loc);
9266 return Make_Procedure_Call_Statement (Loc,
9267 Name => Name, Parameter_Associations => Args);
9268 end Make_Task_Create_Call;
9270 ------------------------------
9271 -- Next_Protected_Operation --
9272 ------------------------------
9274 function Next_Protected_Operation (N : Node_Id) return Node_Id is
9278 Next_Op := Next (N);
9280 while Present (Next_Op)
9281 and then Nkind (Next_Op) /= N_Subprogram_Body
9282 and then Nkind (Next_Op) /= N_Entry_Body
9288 end Next_Protected_Operation;
9290 ----------------------
9291 -- Set_Discriminals --
9292 ----------------------
9294 procedure Set_Discriminals (Dec : Node_Id) is
9297 D_Minal : Entity_Id;
9300 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
9301 Pdef := Defining_Identifier (Dec);
9303 if Has_Discriminants (Pdef) then
9304 D := First_Discriminant (Pdef);
9306 while Present (D) loop
9308 Make_Defining_Identifier (Sloc (D),
9309 Chars => New_External_Name (Chars (D), 'D'));
9311 Set_Ekind (D_Minal, E_Constant);
9312 Set_Etype (D_Minal, Etype (D));
9313 Set_Scope (D_Minal, Pdef);
9314 Set_Discriminal (D, D_Minal);
9315 Set_Discriminal_Link (D_Minal, D);
9317 Next_Discriminant (D);
9320 end Set_Discriminals;
9326 procedure Set_Privals
9335 Body_Ent : Entity_Id;
9336 Prec_Decl : constant Node_Id :=
9337 Parent (Corresponding_Record_Type
9338 (Defining_Identifier (Dec)));
9339 Prec_Def : constant Entity_Id := Type_Definition (Prec_Decl);
9341 P_Subtype : Entity_Id;
9342 Assoc_L : constant Elist_Id := New_Elmt_List;
9346 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
9348 (Nkind (Op) = N_Subprogram_Body or else Nkind (Op) = N_Entry_Body);
9350 Def := Protected_Definition (Dec);
9352 if Present (Private_Declarations (Def)) then
9354 P_Decl := First (Private_Declarations (Def));
9356 while Present (P_Decl) loop
9357 if Nkind (P_Decl) = N_Component_Declaration then
9358 P_Id := Defining_Identifier (P_Decl);
9360 Make_Defining_Identifier (Loc,
9361 New_External_Name (Chars (P_Id), 'P'));
9363 Set_Ekind (Priv, E_Variable);
9364 Set_Etype (Priv, Etype (P_Id));
9365 Set_Scope (Priv, Scope (P_Id));
9366 Set_Esize (Priv, Esize (Etype (P_Id)));
9367 Set_Alignment (Priv, Alignment (Etype (P_Id)));
9369 -- If the type of the component is an itype, we must
9370 -- create a new itype for the corresponding prival in
9371 -- each protected operation, to avoid scoping problems.
9372 -- We create new itypes by copying the tree for the
9373 -- component definition.
9375 if Is_Itype (Etype (P_Id)) then
9376 Append_Elmt (P_Id, Assoc_L);
9377 Append_Elmt (Priv, Assoc_L);
9379 if Nkind (Op) = N_Entry_Body then
9380 Op_Id := Defining_Identifier (Op);
9382 Op_Id := Defining_Unit_Name (Specification (Op));
9386 (New_Copy_Tree (P_Decl, Assoc_L, New_Scope => Op_Id));
9389 Set_Protected_Operation (P_Id, Op);
9390 Set_Prival (P_Id, Priv);
9397 -- There is one more implicit private declaration: the object
9398 -- itself. A "prival" for this is attached to the protected
9399 -- body defining identifier.
9401 Body_Ent := Corresponding_Body (Dec);
9404 Make_Defining_Identifier (Sloc (Body_Ent),
9405 Chars => New_External_Name (Chars (Body_Ent), 'R'));
9407 -- Set the Etype to the implicit subtype of Protection created when
9408 -- the protected type declaration was expanded. This node will not
9409 -- be analyzed until it is used as the defining identifier for the
9410 -- renaming declaration in the protected operation body, and it will
9411 -- be needed in the references expanded before that body is expanded.
9412 -- Since the Protection field is aliased, set Is_Aliased as well.
9414 Obj_Decl := First (Component_Items (Component_List (Prec_Def)));
9415 while Chars (Defining_Identifier (Obj_Decl)) /= Name_uObject loop
9419 P_Subtype := Etype (Defining_Identifier (Obj_Decl));
9420 Set_Ekind (Priv, E_Variable);
9421 Set_Etype (Priv, P_Subtype);
9422 Set_Is_Aliased (Priv);
9423 Set_Object_Ref (Body_Ent, Priv);
9426 ----------------------------
9427 -- Update_Prival_Subtypes --
9428 ----------------------------
9430 procedure Update_Prival_Subtypes (N : Node_Id) is
9432 function Process (N : Node_Id) return Traverse_Result;
9433 -- Update the etype of occurrences of privals whose etype does not
9434 -- match the current Etype of the prival entity itself.
9436 procedure Update_Array_Bounds (E : Entity_Id);
9437 -- Itypes generated for array expressions may depend on the
9438 -- determinants of the protected object, and need to be processed
9439 -- separately because they are not attached to the tree.
9441 procedure Update_Index_Types (N : Node_Id);
9442 -- Similarly, update the types of expressions in indexed components
9443 -- which may depend on other discriminants.
9449 function Process (N : Node_Id) return Traverse_Result is
9451 if Is_Entity_Name (N) then
9453 E : constant Entity_Id := Entity (N);
9457 and then (Ekind (E) = E_Constant
9458 or else Ekind (E) = E_Variable)
9459 and then Nkind (Parent (E)) = N_Object_Renaming_Declaration
9460 and then not Is_Scalar_Type (Etype (E))
9461 and then Etype (N) /= Etype (E)
9463 Set_Etype (N, Etype (Entity (Original_Node (N))));
9464 Update_Index_Types (N);
9467 and then Ekind (E) = E_Constant
9468 and then Present (Discriminal_Link (E))
9470 Set_Etype (N, Etype (E));
9476 elsif Nkind (N) = N_Defining_Identifier
9477 or else Nkind (N) = N_Defining_Operator_Symbol
9478 or else Nkind (N) = N_Defining_Character_Literal
9482 elsif Nkind (N) = N_String_Literal then
9484 -- Array type, but bounds are constant
9488 elsif Nkind (N) = N_Object_Declaration
9489 and then Is_Itype (Etype (Defining_Identifier (N)))
9490 and then Is_Array_Type (Etype (Defining_Identifier (N)))
9492 Update_Array_Bounds (Etype (Defining_Identifier (N)));
9495 -- For array components of discriminated records, use the
9496 -- base type directly, because it may depend indirectly
9497 -- on the discriminants of the protected type. Cleaner would
9498 -- be a systematic mechanism to compute actual subtypes of
9499 -- private components ???
9501 elsif Nkind (N) in N_Has_Etype
9502 and then Present (Etype (N))
9503 and then Is_Array_Type (Etype (N))
9504 and then Nkind (N) = N_Selected_Component
9505 and then Has_Discriminants (Etype (Prefix (N)))
9507 Set_Etype (N, Base_Type (Etype (N)));
9508 Update_Index_Types (N);
9512 if Nkind (N) in N_Has_Etype
9513 and then Present (Etype (N))
9514 and then Is_Itype (Etype (N)) then
9516 if Is_Array_Type (Etype (N)) then
9517 Update_Array_Bounds (Etype (N));
9519 elsif Is_Scalar_Type (Etype (N)) then
9520 Update_Prival_Subtypes (Type_Low_Bound (Etype (N)));
9521 Update_Prival_Subtypes (Type_High_Bound (Etype (N)));
9529 -------------------------
9530 -- Update_Array_Bounds --
9531 -------------------------
9533 procedure Update_Array_Bounds (E : Entity_Id) is
9537 Ind := First_Index (E);
9539 while Present (Ind) loop
9540 Update_Prival_Subtypes (Type_Low_Bound (Etype (Ind)));
9541 Update_Prival_Subtypes (Type_High_Bound (Etype (Ind)));
9544 end Update_Array_Bounds;
9546 ------------------------
9547 -- Update_Index_Types --
9548 ------------------------
9550 procedure Update_Index_Types (N : Node_Id) is
9554 -- If the prefix has an actual subtype that is different
9555 -- from the nominal one, update the types of the indices,
9556 -- so that the proper constraints are applied. Do not
9557 -- apply this transformation to a packed array, where the
9558 -- index type is computed for a byte array and is different
9559 -- from the source index.
9561 if Nkind (Parent (N)) = N_Indexed_Component
9563 not Is_Bit_Packed_Array (Etype (Prefix (Parent (N))))
9565 Indx1 := First (Expressions (Parent (N)));
9566 I_Typ := First_Index (Etype (N));
9568 while Present (Indx1) and then Present (I_Typ) loop
9570 if not Is_Entity_Name (Indx1) then
9571 Set_Etype (Indx1, Base_Type (Etype (I_Typ)));
9578 end Update_Index_Types;
9580 procedure Traverse is new Traverse_Proc;
9582 -- Start of processing for Update_Prival_Subtypes
9586 end Update_Prival_Subtypes;