1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Exp_Ch3; use Exp_Ch3;
32 with Exp_Ch6; use Exp_Ch6;
33 with Exp_Ch11; use Exp_Ch11;
34 with Exp_Dbug; use Exp_Dbug;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Sel; use Exp_Sel;
37 with Exp_Smem; use Exp_Smem;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch8; use Sem_Ch8;
54 with Sem_Ch11; use Sem_Ch11;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Targparm; use Targparm;
64 with Tbuild; use Tbuild;
65 with Uintp; use Uintp;
67 package body Exp_Ch9 is
69 -- The following constant establishes the upper bound for the index of
70 -- an entry family. It is used to limit the allocated size of protected
71 -- types with defaulted discriminant of an integer type, when the bound
72 -- of some entry family depends on a discriminant. The limitation to
73 -- entry families of 128K should be reasonable in all cases, and is a
74 -- documented implementation restriction.
76 Entry_Family_Bound : constant Int := 2**16;
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 function Actual_Index_Expression
86 Tsk : Entity_Id) return Node_Id;
87 -- Compute the index position for an entry call. Tsk is the target task. If
88 -- the bounds of some entry family depend on discriminants, the expression
89 -- computed by this function uses the discriminants of the target task.
91 procedure Add_Object_Pointer
95 -- Prepend an object pointer declaration to the declaration list Decls.
96 -- This object pointer is initialized to a type conversion of the System.
97 -- Address pointer passed to entry barrier functions and entry body
100 procedure Add_Formal_Renamings
105 -- Create renaming declarations for the formals, inside the procedure that
106 -- implements an entry body. The renamings make the original names of the
107 -- formals accessible to gdb, and serve no other purpose.
108 -- Spec is the specification of the procedure being built.
109 -- Decls is the list of declarations to be enhanced.
110 -- Ent is the entity for the original entry body.
112 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
113 -- Transform accept statement into a block with added exception handler.
114 -- Used both for simple accept statements and for accept alternatives in
115 -- select statements. Astat is the accept statement.
117 function Build_Barrier_Function
120 Pid : Node_Id) return Node_Id;
121 -- Build the function body returning the value of the barrier expression
122 -- for the specified entry body.
124 function Build_Barrier_Function_Specification
126 Def_Id : Entity_Id) return Node_Id;
127 -- Build a specification for a function implementing the protected entry
128 -- barrier of the specified entry body.
130 function Build_Corresponding_Record
133 Loc : Source_Ptr) return Node_Id;
134 -- Common to tasks and protected types. Copy discriminant specifications,
135 -- build record declaration. N is the type declaration, Ctyp is the
136 -- concurrent entity (task type or protected type).
138 function Build_Entry_Count_Expression
139 (Concurrent_Type : Node_Id;
140 Component_List : List_Id;
141 Loc : Source_Ptr) return Node_Id;
142 -- Compute number of entries for concurrent object. This is a count of
143 -- simple entries, followed by an expression that computes the length
144 -- of the range of each entry family. A single array with that size is
145 -- allocated for each concurrent object of the type.
147 function Build_Parameter_Block
151 Decls : List_Id) return Entity_Id;
152 -- Generate an access type for each actual parameter in the list Actuals.
153 -- Create an encapsulating record that contains all the actuals and return
154 -- its type. Generate:
155 -- type Ann1 is access all <actual1-type>
157 -- type AnnN is access all <actualN-type>
158 -- type Pnn is record
164 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id);
165 -- Build body of wrapper procedure for an entry or entry family that has
166 -- pre/postconditions. The body gathers the PPC's and expands them in the
167 -- usual way, and performs the entry call itself. This way preconditions
168 -- are evaluated before the call is queued. E is the entry in question,
169 -- and Decl is the enclosing synchronized type declaration at whose
170 -- freeze point the generated body is analyzed.
172 function Build_Renamed_Formal_Declaration
176 Renamed_Formal : Node_Id) return Node_Id;
177 -- Create a renaming declaration for a formal, within a protected entry
178 -- body or an accept body. The renamed object is a component of the
179 -- parameter block that is a parameter in the entry call.
181 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
182 -- does not dereference the corresponding component to prevent an illegal
183 -- use of the incomplete type (AI05-0151).
185 procedure Build_Wrapper_Bodies
189 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
190 -- record of a concurrent type. N is the insertion node where all bodies
191 -- will be placed. This routine builds the bodies of the subprograms which
192 -- serve as an indirection mechanism to overriding primitives of concurrent
193 -- types, entries and protected procedures. Any new body is analyzed.
195 procedure Build_Wrapper_Specs
199 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
200 -- record of a concurrent type. N is the insertion node where all specs
201 -- will be placed. This routine builds the specs of the subprograms which
202 -- serve as an indirection mechanism to overriding primitives of concurrent
203 -- types, entries and protected procedures. Any new spec is analyzed.
205 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
206 -- Build the function that translates the entry index in the call
207 -- (which depends on the size of entry families) into an index into the
208 -- Entry_Bodies_Array, to determine the body and barrier function used
209 -- in a protected entry call. A pointer to this function appears in every
212 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
213 -- Build subprogram declaration for previous one
215 function Build_Protected_Entry
218 Pid : Node_Id) return Node_Id;
219 -- Build the procedure implementing the statement sequence of the specified
222 function Build_Protected_Entry_Specification
225 Ent_Id : Entity_Id) return Node_Id;
226 -- Build a specification for the procedure implementing the statements of
227 -- the specified entry body. Add attributes associating it with the entry
228 -- defining identifier Ent_Id.
230 function Build_Protected_Spec
232 Obj_Type : Entity_Id;
234 Unprotected : Boolean := False) return List_Id;
235 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
236 -- Subprogram_Type. Builds signature of protected subprogram, adding the
237 -- formal that corresponds to the object itself. For an access to protected
238 -- subprogram, there is no object type to specify, so the parameter has
239 -- type Address and mode In. An indirect call through such a pointer will
240 -- convert the address to a reference to the actual object. The object is
241 -- a limited record and therefore a by_reference type.
243 function Build_Protected_Subprogram_Body
246 N_Op_Spec : Node_Id) return Node_Id;
247 -- This function is used to construct the protected version of a protected
248 -- subprogram. Its statement sequence first defers abort, then locks
249 -- the associated protected object, and then enters a block that contains
250 -- a call to the unprotected version of the subprogram (for details, see
251 -- Build_Unprotected_Subprogram_Body). This block statement requires
252 -- a cleanup handler that unlocks the object in all cases.
253 -- (see Exp_Ch7.Expand_Cleanup_Actions).
255 function Build_Selected_Name
257 Selector : Entity_Id;
258 Append_Char : Character := ' ') return Name_Id;
259 -- Build a name in the form of Prefix__Selector, with an optional
260 -- character appended. This is used for internal subprograms generated
261 -- for operations of protected types, including barrier functions.
262 -- For the subprograms generated for entry bodies and entry barriers,
263 -- the generated name includes a sequence number that makes names
264 -- unique in the presence of entry overloading. This is necessary
265 -- because entry body procedures and barrier functions all have the
268 procedure Build_Simple_Entry_Call
273 -- Some comments here would be useful ???
275 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
276 -- This routine constructs a specification for the procedure that we will
277 -- build for the task body for task type T. The spec has the form:
279 -- procedure tnameB (_Task : access tnameV);
281 -- where name is the character name taken from the task type entity that
282 -- is passed as the argument to the procedure, and tnameV is the task
283 -- value type that is associated with the task type.
285 function Build_Unprotected_Subprogram_Body
287 Pid : Node_Id) return Node_Id;
288 -- This routine constructs the unprotected version of a protected
289 -- subprogram body, which is contains all of the code in the
290 -- original, unexpanded body. This is the version of the protected
291 -- subprogram that is called from all protected operations on the same
292 -- object, including the protected version of the same subprogram.
294 procedure Collect_Entry_Families
297 Current_Node : in out Node_Id;
298 Conctyp : Entity_Id);
299 -- For each entry family in a concurrent type, create an anonymous array
300 -- type of the right size, and add a component to the corresponding_record.
302 function Concurrent_Object
303 (Spec_Id : Entity_Id;
304 Conc_Typ : Entity_Id) return Entity_Id;
305 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
306 -- the entity associated with the concurrent object in the Protected_Body_
307 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
308 -- denotes formal parameter _O, _object or _task.
310 function Copy_Result_Type (Res : Node_Id) return Node_Id;
311 -- Copy the result type of a function specification, when building the
312 -- internal operation corresponding to a protected function, or when
313 -- expanding an access to protected function. If the result is an anonymous
314 -- access to subprogram itself, we need to create a new signature with the
315 -- same parameter names and the same resolved types, but with new entities
318 procedure Debug_Private_Data_Declarations (Decls : List_Id);
319 -- Decls is a list which may contain the declarations created by Install_
320 -- Private_Data_Declarations. All generated entities are marked as needing
321 -- debug info and debug nodes are manually generation where necessary. This
322 -- step of the expansion must to be done after private data has been moved
323 -- to its final resting scope to ensure proper visibility of debug objects.
325 function Family_Offset
330 Cap : Boolean) return Node_Id;
331 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in
332 -- an accept statement, or the upper bound in the discrete subtype of
333 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
334 -- the concurrent type of the entry. If Cap is true, the result is
335 -- capped according to Entry_Family_Bound.
342 Cap : Boolean) return Node_Id;
343 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
344 -- a family, and handle properly the superflat case. This is equivalent
345 -- to the use of 'Length on the index type, but must use Family_Offset
346 -- to handle properly the case of bounds that depend on discriminants.
347 -- If Cap is true, the result is capped according to Entry_Family_Bound.
349 procedure Extract_Dispatching_Call
351 Call_Ent : out Entity_Id;
352 Object : out Entity_Id;
353 Actuals : out List_Id;
354 Formals : out List_Id);
355 -- Given a dispatching call, extract the entity of the name of the call,
356 -- its actual dispatching object, its actual parameters and the formal
357 -- parameters of the overridden interface-level version. If the type of
358 -- the dispatching object is an access type then an explicit dereference
359 -- is returned in Object.
361 procedure Extract_Entry
363 Concval : out Node_Id;
365 Index : out Node_Id);
366 -- Given an entry call, returns the associated concurrent object,
367 -- the entry name, and the entry family index.
369 function Find_Task_Or_Protected_Pragma
371 P : Name_Id) return Node_Id;
372 -- Searches the task or protected definition T for the first occurrence
373 -- of the pragma whose name is given by P. The caller has ensured that
374 -- the pragma is present in the task definition. A special case is that
375 -- when P is Name_uPriority, the call will also find Interrupt_Priority.
376 -- ??? Should be implemented with the rep item chain mechanism.
378 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
379 -- Given a subprogram identifier, return the entity which is associated
380 -- with the protection entry index in the Protected_Body_Subprogram or the
381 -- Task_Body_Procedure of Spec_Id. The returned entity denotes formal
384 function Is_Potentially_Large_Family
385 (Base_Index : Entity_Id;
388 Hi : Node_Id) return Boolean;
390 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
391 -- Determine whether Id is a function or a procedure and is marked as a
392 -- private primitive.
394 function Null_Statements (Stats : List_Id) return Boolean;
395 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
396 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as
397 -- well to still count as null. Returns True for a null sequence. The
398 -- argument is the list of statements from the DO-END sequence.
400 function Parameter_Block_Pack
406 Stmts : List_Id) return Entity_Id;
407 -- Set the components of the generated parameter block with the values of
408 -- the actual parameters. Generate aliased temporaries to capture the
409 -- values for types that are passed by copy. Otherwise generate a reference
410 -- to the actual's value. Return the address of the aggregate block.
412 -- Jnn1 : alias <formal-type1>;
413 -- Jnn1 := <actual1>;
416 -- Jnn1'unchecked_access;
417 -- <actual2>'reference;
420 function Parameter_Block_Unpack
424 Formals : List_Id) return List_Id;
425 -- Retrieve the values of the components from the parameter block and
426 -- assign then to the original actual parameters. Generate:
427 -- <actual1> := P.<formal1>;
429 -- <actualN> := P.<formalN>;
431 function Trivial_Accept_OK return Boolean;
432 -- If there is no DO-END block for an accept, or if the DO-END block has
433 -- only null statements, then it is possible to do the Rendezvous with much
434 -- less overhead using the Accept_Trivial routine in the run-time library.
435 -- However, this is not always a valid optimization. Whether it is valid or
436 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
437 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
438 -- a rescheduling is required, so this optimization is not allowed. This
439 -- function returns True if the optimization is permitted.
441 -----------------------------
442 -- Actual_Index_Expression --
443 -----------------------------
445 function Actual_Index_Expression
449 Tsk : Entity_Id) return Node_Id
451 Ttyp : constant Entity_Id := Etype (Tsk);
459 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
460 -- Compute difference between bounds of entry family
462 --------------------------
463 -- Actual_Family_Offset --
464 --------------------------
466 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
468 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
469 -- Replace a reference to a discriminant with a selected component
470 -- denoting the discriminant of the target task.
472 -----------------------------
473 -- Actual_Discriminant_Ref --
474 -----------------------------
476 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
477 Typ : constant Entity_Id := Etype (Bound);
481 if not Is_Entity_Name (Bound)
482 or else Ekind (Entity (Bound)) /= E_Discriminant
484 if Nkind (Bound) = N_Attribute_Reference then
487 B := New_Copy_Tree (Bound);
492 Make_Selected_Component (Sloc,
493 Prefix => New_Copy_Tree (Tsk),
494 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
496 Analyze_And_Resolve (B, Typ);
500 Make_Attribute_Reference (Sloc,
501 Attribute_Name => Name_Pos,
502 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
503 Expressions => New_List (B));
504 end Actual_Discriminant_Ref;
506 -- Start of processing for Actual_Family_Offset
510 Make_Op_Subtract (Sloc,
511 Left_Opnd => Actual_Discriminant_Ref (Hi),
512 Right_Opnd => Actual_Discriminant_Ref (Lo));
513 end Actual_Family_Offset;
515 -- Start of processing for Actual_Index_Expression
518 -- The queues of entries and entry families appear in textual order in
519 -- the associated record. The entry index is computed as the sum of the
520 -- number of queues for all entries that precede the designated one, to
521 -- which is added the index expression, if this expression denotes a
522 -- member of a family.
524 -- The following is a place holder for the count of simple entries
526 Num := Make_Integer_Literal (Sloc, 1);
528 -- We construct an expression which is a series of addition operations.
529 -- See comments in Entry_Index_Expression, which is identical in
532 if Present (Index) then
533 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
540 Actual_Family_Offset (
541 Make_Attribute_Reference (Sloc,
542 Attribute_Name => Name_Pos,
543 Prefix => New_Reference_To (Base_Type (S), Sloc),
544 Expressions => New_List (Relocate_Node (Index))),
545 Type_Low_Bound (S)));
550 -- Now add lengths of preceding entries and entry families
552 Prev := First_Entity (Ttyp);
554 while Chars (Prev) /= Chars (Ent)
555 or else (Ekind (Prev) /= Ekind (Ent))
556 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
558 if Ekind (Prev) = E_Entry then
559 Set_Intval (Num, Intval (Num) + 1);
561 elsif Ekind (Prev) = E_Entry_Family then
563 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
565 -- The need for the following full view retrieval stems from
566 -- this complex case of nested generics and tasking:
569 -- type Formal_Index is range <>;
572 -- type Index is private;
579 -- type Index is new Formal_Index range 1 .. 10;
582 -- package body Outer is
584 -- entry Fam (Index); -- (2)
587 -- package body Inner is -- (3)
595 -- We are currently building the index expression for the entry
596 -- call "T.E" (1). Part of the expansion must mention the range
597 -- of the discrete type "Index" (2) of entry family "Fam".
598 -- However only the private view of type "Index" is available to
599 -- the inner generic (3) because there was no prior mention of
600 -- the type inside "Inner". This visibility requirement is
601 -- implicit and cannot be detected during the construction of
602 -- the generic trees and needs special handling.
605 and then Is_Private_Type (S)
606 and then Present (Full_View (S))
611 Lo := Type_Low_Bound (S);
612 Hi := Type_High_Bound (S);
620 Actual_Family_Offset (Hi, Lo),
622 Make_Integer_Literal (Sloc, 1)));
624 -- Other components are anonymous types to be ignored
634 end Actual_Index_Expression;
636 --------------------------
637 -- Add_Formal_Renamings --
638 --------------------------
640 procedure Add_Formal_Renamings
646 Ptr : constant Entity_Id :=
648 (Next (First (Parameter_Specifications (Spec))));
649 -- The name of the formal that holds the address of the parameter block
656 Renamed_Formal : Node_Id;
659 Formal := First_Formal (Ent);
660 while Present (Formal) loop
661 Comp := Entry_Component (Formal);
663 Make_Defining_Identifier (Sloc (Formal),
664 Chars => Chars (Formal));
665 Set_Etype (New_F, Etype (Formal));
666 Set_Scope (New_F, Ent);
668 -- Now we set debug info needed on New_F even though it does not
669 -- come from source, so that the debugger will get the right
670 -- information for these generated names.
672 Set_Debug_Info_Needed (New_F);
674 if Ekind (Formal) = E_In_Parameter then
675 Set_Ekind (New_F, E_Constant);
677 Set_Ekind (New_F, E_Variable);
678 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
681 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
684 Make_Selected_Component (Loc,
686 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
687 Make_Identifier (Loc, Chars (Ptr))),
688 Selector_Name => New_Reference_To (Comp, Loc));
691 Build_Renamed_Formal_Declaration
692 (New_F, Formal, Comp, Renamed_Formal);
694 Append (Decl, Decls);
695 Set_Renamed_Object (Formal, New_F);
696 Next_Formal (Formal);
698 end Add_Formal_Renamings;
700 ------------------------
701 -- Add_Object_Pointer --
702 ------------------------
704 procedure Add_Object_Pointer
706 Conc_Typ : Entity_Id;
709 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
714 -- Create the renaming declaration for the Protection object of a
715 -- protected type. _Object is used by Complete_Entry_Body.
716 -- ??? An attempt to make this a renaming was unsuccessful.
718 -- Build the entity for the access type
721 Make_Defining_Identifier (Loc,
722 New_External_Name (Chars (Rec_Typ), 'P'));
725 -- _object : poVP := poVP!O;
728 Make_Object_Declaration (Loc,
729 Defining_Identifier =>
730 Make_Defining_Identifier (Loc, Name_uObject),
732 New_Reference_To (Obj_Ptr, Loc),
734 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
735 Set_Debug_Info_Needed (Defining_Identifier (Decl));
736 Prepend_To (Decls, Decl);
739 -- type poVP is access poV;
742 Make_Full_Type_Declaration (Loc,
743 Defining_Identifier =>
746 Make_Access_To_Object_Definition (Loc,
747 Subtype_Indication =>
748 New_Reference_To (Rec_Typ, Loc)));
749 Set_Debug_Info_Needed (Defining_Identifier (Decl));
750 Prepend_To (Decls, Decl);
751 end Add_Object_Pointer;
753 -----------------------
754 -- Build_Accept_Body --
755 -----------------------
757 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
758 Loc : constant Source_Ptr := Sloc (Astat);
759 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
766 -- At the end of the statement sequence, Complete_Rendezvous is called.
767 -- A label skipping the Complete_Rendezvous, and all other accept
768 -- processing, has already been added for the expansion of requeue
769 -- statements. The Sloc is copied from the last statement since it
770 -- is really part of this last statement.
774 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
775 Insert_Before (Last (Statements (Stats)), Call);
778 -- If exception handlers are present, then append Complete_Rendezvous
779 -- calls to the handlers, and construct the required outer block. As
780 -- above, the Sloc is copied from the last statement in the sequence.
782 if Present (Exception_Handlers (Stats)) then
783 Hand := First (Exception_Handlers (Stats));
784 while Present (Hand) loop
787 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
788 Append (Call, Statements (Hand));
794 Make_Handled_Sequence_Of_Statements (Loc,
795 Statements => New_List (
796 Make_Block_Statement (Loc,
797 Handled_Statement_Sequence => Stats)));
803 -- At this stage we know that the new statement sequence does not
804 -- have an exception handler part, so we supply one to call
805 -- Exceptional_Complete_Rendezvous. This handler is
807 -- when all others =>
808 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
810 -- We handle Abort_Signal to make sure that we properly catch the abort
811 -- case and wake up the caller.
813 Ohandle := Make_Others_Choice (Loc);
814 Set_All_Others (Ohandle);
816 Set_Exception_Handlers (New_S,
818 Make_Implicit_Exception_Handler (Loc,
819 Exception_Choices => New_List (Ohandle),
821 Statements => New_List (
822 Make_Procedure_Call_Statement (Sloc (Stats),
823 Name => New_Reference_To (
824 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
825 Parameter_Associations => New_List (
826 Make_Function_Call (Sloc (Stats),
827 Name => New_Reference_To (
828 RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
830 Set_Parent (New_S, Astat); -- temp parent for Analyze call
831 Analyze_Exception_Handlers (Exception_Handlers (New_S));
832 Expand_Exception_Handlers (New_S);
834 -- Exceptional_Complete_Rendezvous must be called with abort
835 -- still deferred, which is the case for a "when all others" handler.
838 end Build_Accept_Body;
840 -----------------------------------
841 -- Build_Activation_Chain_Entity --
842 -----------------------------------
844 procedure Build_Activation_Chain_Entity (N : Node_Id) is
845 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
846 -- Determine whether an extended return statement has an activation
849 --------------------------
850 -- Has_Activation_Chain --
851 --------------------------
853 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
857 Decl := First (Return_Object_Declarations (Stmt));
858 while Present (Decl) loop
859 if Nkind (Decl) = N_Object_Declaration
860 and then Chars (Defining_Identifier (Decl)) = Name_uChain
869 end Has_Activation_Chain;
876 -- Start of processing for Build_Activation_Chain_Entity
879 -- Traverse the parent chain looking for an enclosing construct which
880 -- contains an activation chain variable. The construct is either a
881 -- body, a block, or an extended return.
885 while not Nkind_In (Par, N_Block_Statement,
887 N_Extended_Return_Statement,
889 N_Package_Declaration,
896 -- When the enclosing construct is a package body, the activation chain
897 -- variable is declared in the body, but the Activation_Chain_Entity is
898 -- attached to the spec.
900 if Nkind (Par) = N_Package_Body then
901 Decls := Declarations (Par);
902 Par := Unit_Declaration_Node (Corresponding_Spec (Par));
904 elsif Nkind (Par) = N_Package_Declaration then
905 Decls := Visible_Declarations (Specification (Par));
907 elsif Nkind (Par) = N_Extended_Return_Statement then
908 Decls := Return_Object_Declarations (Par);
911 Decls := Declarations (Par);
914 -- If an activation chain entity has not been declared already, create
917 if Nkind (Par) = N_Extended_Return_Statement
918 or else No (Activation_Chain_Entity (Par))
920 -- Since extended return statements do not store the entity of the
921 -- chain, examine the return object declarations to avoid creating
924 if Nkind (Par) = N_Extended_Return_Statement
925 and then Has_Activation_Chain (Par)
935 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
937 -- Note: An extended return statement is not really a task
938 -- activator, but it does have an activation chain on which to
939 -- store the tasks temporarily. On successful return, the tasks
940 -- on this chain are moved to the chain passed in by the caller.
941 -- We do not build an Activation_Chain_Entity for an extended
942 -- return statement, because we do not want to build a call to
943 -- Activate_Tasks. Task activation is the responsibility of the
946 if Nkind (Par) /= N_Extended_Return_Statement then
947 Set_Activation_Chain_Entity (Par, Chain);
951 Make_Object_Declaration (Sloc (Par),
952 Defining_Identifier => Chain,
953 Aliased_Present => True,
955 New_Reference_To (RTE (RE_Activation_Chain), Sloc (Par)));
957 Prepend_To (Decls, Decl);
961 end Build_Activation_Chain_Entity;
963 ----------------------------
964 -- Build_Barrier_Function --
965 ----------------------------
967 function Build_Barrier_Function
970 Pid : Node_Id) return Node_Id
972 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
973 Cond : constant Node_Id := Condition (Ent_Formals);
974 Loc : constant Source_Ptr := Sloc (Cond);
975 Func_Id : constant Entity_Id := Barrier_Function (Ent);
976 Op_Decls : constant List_Id := New_List;
981 -- Add a declaration for the Protection object, renaming declarations
982 -- for the discriminals and privals and finally a declaration for the
983 -- entry family index (if applicable).
985 Install_Private_Data_Declarations (Sloc (N),
991 Family => Ekind (Ent) = E_Entry_Family);
993 -- If compiling with -fpreserve-control-flow, make sure we insert an
994 -- IF statement so that the back-end knows to generate a conditional
995 -- branch instruction, even if the condition is just the name of a
998 if Opt.Suppress_Control_Flow_Optimizations then
999 Stmt := Make_Implicit_If_Statement (Cond,
1001 Then_Statements => New_List (
1002 Make_Simple_Return_Statement (Loc,
1003 New_Occurrence_Of (Standard_True, Loc))),
1004 Else_Statements => New_List (
1005 Make_Simple_Return_Statement (Loc,
1006 New_Occurrence_Of (Standard_False, Loc))));
1009 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1012 -- Note: the condition in the barrier function needs to be properly
1013 -- processed for the C/Fortran boolean possibility, but this happens
1014 -- automatically since the return statement does this normalization.
1017 Make_Subprogram_Body (Loc,
1019 Build_Barrier_Function_Specification (Loc,
1020 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1021 Declarations => Op_Decls,
1022 Handled_Statement_Sequence =>
1023 Make_Handled_Sequence_Of_Statements (Loc,
1024 Statements => New_List (Stmt)));
1025 Set_Is_Entry_Barrier_Function (Func_Body);
1028 end Build_Barrier_Function;
1030 ------------------------------------------
1031 -- Build_Barrier_Function_Specification --
1032 ------------------------------------------
1034 function Build_Barrier_Function_Specification
1036 Def_Id : Entity_Id) return Node_Id
1039 Set_Debug_Info_Needed (Def_Id);
1041 return Make_Function_Specification (Loc,
1042 Defining_Unit_Name => Def_Id,
1043 Parameter_Specifications => New_List (
1044 Make_Parameter_Specification (Loc,
1045 Defining_Identifier =>
1046 Make_Defining_Identifier (Loc, Name_uO),
1048 New_Reference_To (RTE (RE_Address), Loc)),
1050 Make_Parameter_Specification (Loc,
1051 Defining_Identifier =>
1052 Make_Defining_Identifier (Loc, Name_uE),
1054 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1056 Result_Definition =>
1057 New_Reference_To (Standard_Boolean, Loc));
1058 end Build_Barrier_Function_Specification;
1060 --------------------------
1061 -- Build_Call_With_Task --
1062 --------------------------
1064 function Build_Call_With_Task
1066 E : Entity_Id) return Node_Id
1068 Loc : constant Source_Ptr := Sloc (N);
1071 Make_Function_Call (Loc,
1072 Name => New_Reference_To (E, Loc),
1073 Parameter_Associations => New_List (Concurrent_Ref (N)));
1074 end Build_Call_With_Task;
1076 -----------------------------
1077 -- Build_Class_Wide_Master --
1078 -----------------------------
1080 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1081 Loc : constant Source_Ptr := Sloc (Typ);
1082 Master_Id : Entity_Id;
1083 Master_Scope : Entity_Id;
1085 Related_Node : Node_Id;
1089 -- Nothing to do if there is no task hierarchy
1091 if Restriction_Active (No_Task_Hierarchy) then
1095 -- Find the declaration that created the access type. It is either a
1096 -- type declaration, or an object declaration with an access definition,
1097 -- in which case the type is anonymous.
1099 if Is_Itype (Typ) then
1100 Related_Node := Associated_Node_For_Itype (Typ);
1102 Related_Node := Parent (Typ);
1105 Master_Scope := Find_Master_Scope (Typ);
1107 -- Nothing to do if the master scope already contains a _master entity.
1108 -- The only exception to this is the following scenario:
1111 -- Transient_Scope_1
1114 -- Transient_Scope_2
1117 -- In this case the source scope is marked as having the master entity
1118 -- even though the actual declaration appears inside an inner scope. If
1119 -- the second transient scope requires a _master, it cannot use the one
1120 -- already declared because the entity is not visible.
1122 Name_Id := Make_Identifier (Loc, Name_uMaster);
1124 if not Has_Master_Entity (Master_Scope)
1125 or else No (Current_Entity_In_Scope (Name_Id))
1128 Master_Decl : Node_Id;
1131 Set_Has_Master_Entity (Master_Scope);
1134 -- _master : constant Integer := Current_Master.all;
1137 Make_Object_Declaration (Loc,
1138 Defining_Identifier =>
1139 Make_Defining_Identifier (Loc, Name_uMaster),
1140 Constant_Present => True,
1141 Object_Definition =>
1142 New_Reference_To (Standard_Integer, Loc),
1144 Make_Explicit_Dereference (Loc,
1145 New_Reference_To (RTE (RE_Current_Master), Loc)));
1147 Insert_Action (Related_Node, Master_Decl);
1148 Analyze (Master_Decl);
1150 -- Mark the containing scope as a task master. Masters associated
1151 -- with return statements are already marked at this stage (see
1152 -- Analyze_Subprogram_Body).
1154 if Ekind (Current_Scope) /= E_Return_Statement then
1156 Par : Node_Id := Related_Node;
1159 while Nkind (Par) /= N_Compilation_Unit loop
1160 Par := Parent (Par);
1162 -- If we fall off the top, we are at the outer level, and
1163 -- the environment task is our effective master, so
1166 if Nkind_In (Par, N_Block_Statement,
1170 Set_Is_Task_Master (Par);
1180 Make_Defining_Identifier (Loc,
1181 New_External_Name (Chars (Typ), 'M'));
1184 -- Mnn renames _master;
1187 Make_Object_Renaming_Declaration (Loc,
1188 Defining_Identifier => Master_Id,
1189 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
1192 Insert_Before (Related_Node, Ren_Decl);
1195 Set_Master_Id (Typ, Master_Id);
1196 end Build_Class_Wide_Master;
1198 --------------------------------
1199 -- Build_Corresponding_Record --
1200 --------------------------------
1202 function Build_Corresponding_Record
1205 Loc : Source_Ptr) return Node_Id
1207 Rec_Ent : constant Entity_Id :=
1208 Make_Defining_Identifier
1209 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1212 New_Disc : Entity_Id;
1216 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1217 Set_Ekind (Rec_Ent, E_Record_Type);
1218 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1219 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1220 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1221 Set_Stored_Constraint (Rec_Ent, No_Elist);
1224 -- Use discriminals to create list of discriminants for record, and
1225 -- create new discriminals for use in default expressions, etc. It is
1226 -- worth noting that a task discriminant gives rise to 5 entities;
1228 -- a) The original discriminant.
1229 -- b) The discriminal for use in the task.
1230 -- c) The discriminant of the corresponding record.
1231 -- d) The discriminal for the init proc of the corresponding record.
1232 -- e) The local variable that renames the discriminant in the procedure
1233 -- for the task body.
1235 -- In fact the discriminals b) are used in the renaming declarations
1236 -- for e). See details in einfo (Handling of Discriminants).
1238 if Present (Discriminant_Specifications (N)) then
1240 Disc := First_Discriminant (Ctyp);
1242 while Present (Disc) loop
1243 New_Disc := CR_Discriminant (Disc);
1246 Make_Discriminant_Specification (Loc,
1247 Defining_Identifier => New_Disc,
1248 Discriminant_Type =>
1249 New_Occurrence_Of (Etype (Disc), Loc),
1251 New_Copy (Discriminant_Default_Value (Disc))));
1253 Next_Discriminant (Disc);
1260 -- Now we can construct the record type declaration. Note that this
1261 -- record is "limited tagged". It is "limited" to reflect the underlying
1262 -- limitedness of the task or protected object that it represents, and
1263 -- ensuring for example that it is properly passed by reference. It is
1264 -- "tagged" to give support to dispatching calls through interfaces. We
1265 -- propagate here the list of interfaces covered by the concurrent type
1266 -- (Ada 2005: AI-345).
1269 Make_Full_Type_Declaration (Loc,
1270 Defining_Identifier => Rec_Ent,
1271 Discriminant_Specifications => Dlist,
1273 Make_Record_Definition (Loc,
1275 Make_Component_List (Loc,
1276 Component_Items => Cdecls),
1278 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1279 Interface_List => Interface_List (N),
1280 Limited_Present => True));
1281 end Build_Corresponding_Record;
1283 ----------------------------------
1284 -- Build_Entry_Count_Expression --
1285 ----------------------------------
1287 function Build_Entry_Count_Expression
1288 (Concurrent_Type : Node_Id;
1289 Component_List : List_Id;
1290 Loc : Source_Ptr) return Node_Id
1302 -- Count number of non-family entries
1305 Ent := First_Entity (Concurrent_Type);
1306 while Present (Ent) loop
1307 if Ekind (Ent) = E_Entry then
1314 Ecount := Make_Integer_Literal (Loc, Eindx);
1316 -- Loop through entry families building the addition nodes
1318 Ent := First_Entity (Concurrent_Type);
1319 Comp := First (Component_List);
1320 while Present (Ent) loop
1321 if Ekind (Ent) = E_Entry_Family then
1322 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1326 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1327 Hi := Type_High_Bound (Typ);
1328 Lo := Type_Low_Bound (Typ);
1329 Large := Is_Potentially_Large_Family
1330 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1333 Left_Opnd => Ecount,
1334 Right_Opnd => Family_Size
1335 (Loc, Hi, Lo, Concurrent_Type, Large));
1342 end Build_Entry_Count_Expression;
1344 -----------------------
1345 -- Build_Entry_Names --
1346 -----------------------
1348 function Build_Entry_Names (Conc_Typ : Entity_Id) return Node_Id is
1349 Loc : constant Source_Ptr := Sloc (Conc_Typ);
1355 Typ : Entity_Id := Conc_Typ;
1357 procedure Build_Entry_Family_Name (Id : Entity_Id);
1359 -- for Lnn in Family_Low .. Family_High loop
1362 -- (_init._object <or> _init._task_id,
1364 -- new String ("<Entry name>(" & Lnn'Img & ")"));
1366 -- Note that the bounds of the range may reference discriminants. The
1367 -- above construct is added directly to the statements of the block.
1369 procedure Build_Entry_Name (Id : Entity_Id);
1373 -- (_init._object <or>_init._task_id,
1375 -- new String ("<Entry name>");
1376 -- The above construct is added directly to the statements of the block.
1378 function Build_Set_Entry_Name_Call (Arg3 : Node_Id) return Node_Id;
1379 -- Generate the call to the runtime routine Set_Entry_Name with actuals
1380 -- _init._task_id or _init._object, Inn and Arg3.
1382 procedure Increment_Index (Stmts : List_Id);
1383 -- Generate the following and add it to Stmts
1386 -----------------------------
1387 -- Build_Entry_Family_Name --
1388 -----------------------------
1390 procedure Build_Entry_Family_Name (Id : Entity_Id) is
1391 Def : constant Node_Id :=
1392 Discrete_Subtype_Definition (Parent (Id));
1393 L_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
1394 L_Stmts : constant List_Id := New_List;
1397 function Build_Range (Def : Node_Id) return Node_Id;
1398 -- Given a discrete subtype definition of an entry family, generate a
1399 -- range node which covers the range of Def's type.
1405 function Build_Range (Def : Node_Id) return Node_Id is
1406 High : Node_Id := Type_High_Bound (Etype (Def));
1407 Low : Node_Id := Type_Low_Bound (Etype (Def));
1410 -- If a bound references a discriminant, generate an identifier
1411 -- with the same name. Resolution will map it to the formals of
1414 if Is_Entity_Name (Low)
1415 and then Ekind (Entity (Low)) = E_Discriminant
1417 Low := Make_Identifier (Loc, Chars (Low));
1419 Low := New_Copy_Tree (Low);
1422 if Is_Entity_Name (High)
1423 and then Ekind (Entity (High)) = E_Discriminant
1425 High := Make_Identifier (Loc, Chars (High));
1427 High := New_Copy_Tree (High);
1433 High_Bound => High);
1436 -- Start of processing for Build_Entry_Family_Name
1439 Get_Name_String (Chars (Id));
1441 -- Add a leading '('
1443 Add_Char_To_Name_Buffer ('(');
1446 -- new String'("<Entry name>(" & Lnn'Img & ")");
1448 -- This is an implicit heap allocation, and Comes_From_Source is
1449 -- False, which ensures that it will get flagged as a violation of
1450 -- No_Implicit_Heap_Allocations when that restriction applies.
1453 Make_Allocator (Loc,
1454 Make_Qualified_Expression (Loc,
1456 New_Reference_To (Standard_String, Loc),
1458 Make_Op_Concat (Loc,
1460 Make_Op_Concat (Loc,
1462 Make_String_Literal (Loc,
1463 Strval => String_From_Name_Buffer),
1465 Make_Attribute_Reference (Loc,
1467 New_Reference_To (L_Id, Loc),
1468 Attribute_Name => Name_Img)),
1470 Make_String_Literal (Loc,
1473 Increment_Index (L_Stmts);
1474 Append_To (L_Stmts, Build_Set_Entry_Name_Call (Val));
1477 -- for Lnn in Family_Low .. Family_High loop
1480 -- (_init._object <or> _init._task_id, Inn, <Val>);
1484 Make_Loop_Statement (Loc,
1486 Make_Iteration_Scheme (Loc,
1487 Loop_Parameter_Specification =>
1488 Make_Loop_Parameter_Specification (Loc,
1489 Defining_Identifier => L_Id,
1490 Discrete_Subtype_Definition => Build_Range (Def))),
1491 Statements => L_Stmts,
1492 End_Label => Empty));
1493 end Build_Entry_Family_Name;
1495 ----------------------
1496 -- Build_Entry_Name --
1497 ----------------------
1499 procedure Build_Entry_Name (Id : Entity_Id) is
1503 Get_Name_String (Chars (Id));
1505 -- This is an implicit heap allocation, and Comes_From_Source is
1506 -- False, which ensures that it will get flagged as a violation of
1507 -- No_Implicit_Heap_Allocations when that restriction applies.
1510 Make_Allocator (Loc,
1511 Make_Qualified_Expression (Loc,
1513 New_Reference_To (Standard_String, Loc),
1515 Make_String_Literal (Loc,
1516 String_From_Name_Buffer)));
1518 Increment_Index (B_Stmts);
1519 Append_To (B_Stmts, Build_Set_Entry_Name_Call (Val));
1520 end Build_Entry_Name;
1522 -------------------------------
1523 -- Build_Set_Entry_Name_Call --
1524 -------------------------------
1526 function Build_Set_Entry_Name_Call (Arg3 : Node_Id) return Node_Id is
1531 -- Determine the proper name for the first argument and the RTS
1534 if Is_Protected_Type (Typ) then
1535 Arg1 := Name_uObject;
1536 Proc := RO_PE_Set_Entry_Name;
1538 else pragma Assert (Is_Task_Type (Typ));
1539 Arg1 := Name_uTask_Id;
1540 Proc := RO_TS_Set_Entry_Name;
1544 -- Set_Entry_Name (_init.Arg1, Inn, Arg3);
1547 Make_Procedure_Call_Statement (Loc,
1549 New_Reference_To (RTE (Proc), Loc),
1550 Parameter_Associations => New_List (
1551 Make_Selected_Component (Loc, -- _init._object
1552 Prefix => -- _init._task_id
1553 Make_Identifier (Loc, Name_uInit),
1555 Make_Identifier (Loc, Arg1)),
1556 New_Reference_To (Index, Loc), -- Inn
1558 end Build_Set_Entry_Name_Call;
1560 ---------------------
1561 -- Increment_Index --
1562 ---------------------
1564 procedure Increment_Index (Stmts : List_Id) is
1570 Make_Assignment_Statement (Loc,
1572 New_Reference_To (Index, Loc),
1576 New_Reference_To (Index, Loc),
1578 Make_Integer_Literal (Loc, 1))));
1579 end Increment_Index;
1581 -- Start of processing for Build_Entry_Names
1584 -- Retrieve the original concurrent type
1586 if Is_Concurrent_Record_Type (Typ) then
1587 Typ := Corresponding_Concurrent_Type (Typ);
1590 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
1592 -- Nothing to do if the type has no entries
1594 if not Has_Entries (Typ) then
1598 -- Avoid generating entry names for a protected type with only one entry
1600 if Is_Protected_Type (Typ)
1601 and then Find_Protection_Type (Typ) /= RTE (RE_Protection_Entries)
1606 Index := Make_Temporary (Loc, 'I');
1608 -- Step 1: Generate the declaration of the index variable:
1609 -- Inn : Protected_Entry_Index := 0;
1611 -- Inn : Task_Entry_Index := 0;
1613 if Is_Protected_Type (Typ) then
1614 Index_Typ := RE_Protected_Entry_Index;
1616 Index_Typ := RE_Task_Entry_Index;
1619 B_Decls := New_List;
1621 Make_Object_Declaration (Loc,
1622 Defining_Identifier => Index,
1623 Object_Definition => New_Reference_To (RTE (Index_Typ), Loc),
1624 Expression => Make_Integer_Literal (Loc, 0)));
1626 B_Stmts := New_List;
1628 -- Step 2: Generate a call to Set_Entry_Name for each entry and entry
1631 Comp := First_Entity (Typ);
1632 while Present (Comp) loop
1633 if Ekind (Comp) = E_Entry then
1634 Build_Entry_Name (Comp);
1636 elsif Ekind (Comp) = E_Entry_Family then
1637 Build_Entry_Family_Name (Comp);
1643 -- Step 3: Wrap the statements in a block
1646 Make_Block_Statement (Loc,
1647 Declarations => B_Decls,
1648 Handled_Statement_Sequence =>
1649 Make_Handled_Sequence_Of_Statements (Loc,
1650 Statements => B_Stmts));
1651 end Build_Entry_Names;
1653 ---------------------------
1654 -- Build_Parameter_Block --
1655 ---------------------------
1657 function Build_Parameter_Block
1661 Decls : List_Id) return Entity_Id
1667 Has_Comp : Boolean := False;
1671 Actual := First (Actuals);
1673 Formal := Defining_Identifier (First (Formals));
1675 while Present (Actual) loop
1676 if not Is_Controlling_Actual (Actual) then
1679 -- type Ann is access all <actual-type>
1681 Comp_Nam := Make_Temporary (Loc, 'A');
1684 Make_Full_Type_Declaration (Loc,
1685 Defining_Identifier => Comp_Nam,
1687 Make_Access_To_Object_Definition (Loc,
1688 All_Present => True,
1689 Constant_Present => Ekind (Formal) = E_In_Parameter,
1690 Subtype_Indication =>
1691 New_Reference_To (Etype (Actual), Loc))));
1697 Make_Component_Declaration (Loc,
1698 Defining_Identifier =>
1699 Make_Defining_Identifier (Loc, Chars (Formal)),
1700 Component_Definition =>
1701 Make_Component_Definition (Loc,
1704 Subtype_Indication =>
1705 New_Reference_To (Comp_Nam, Loc))));
1710 Next_Actual (Actual);
1711 Next_Formal_With_Extras (Formal);
1714 Rec_Nam := Make_Temporary (Loc, 'P');
1719 -- type Pnn is record
1724 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1725 -- the original parameter names and Ann1 .. AnnN are the access to
1729 Make_Full_Type_Declaration (Loc,
1730 Defining_Identifier =>
1733 Make_Record_Definition (Loc,
1735 Make_Component_List (Loc, Comps))));
1738 -- type Pnn is null record;
1741 Make_Full_Type_Declaration (Loc,
1742 Defining_Identifier =>
1745 Make_Record_Definition (Loc,
1746 Null_Present => True,
1747 Component_List => Empty)));
1751 end Build_Parameter_Block;
1753 --------------------------------------
1754 -- Build_Renamed_Formal_Declaration --
1755 --------------------------------------
1757 function Build_Renamed_Formal_Declaration
1761 Renamed_Formal : Node_Id) return Node_Id
1763 Loc : constant Source_Ptr := Sloc (New_F);
1767 -- If the formal is a tagged incomplete type, it is already passed
1768 -- by reference, so it is sufficient to rename the pointer component
1769 -- that corresponds to the actual. Otherwise we need to dereference
1770 -- the pointer component to obtain the actual.
1772 if Is_Incomplete_Type (Etype (Formal))
1773 and then Is_Tagged_Type (Etype (Formal))
1776 Make_Object_Renaming_Declaration (Loc,
1777 Defining_Identifier => New_F,
1778 Subtype_Mark => New_Reference_To (Etype (Comp), Loc),
1779 Name => Renamed_Formal);
1783 Make_Object_Renaming_Declaration (Loc,
1784 Defining_Identifier => New_F,
1785 Subtype_Mark => New_Reference_To (Etype (Formal), Loc),
1787 Make_Explicit_Dereference (Loc, Renamed_Formal));
1791 end Build_Renamed_Formal_Declaration;
1793 -----------------------
1794 -- Build_PPC_Wrapper --
1795 -----------------------
1797 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id) is
1798 Loc : constant Source_Ptr := Sloc (E);
1799 Synch_Type : constant Entity_Id := Scope (E);
1801 Wrapper_Id : constant Entity_Id :=
1802 Make_Defining_Identifier (Loc,
1803 Chars => New_External_Name (Chars (E), 'E'));
1804 -- the wrapper procedure name
1806 Wrapper_Body : Node_Id;
1808 Synch_Id : constant Entity_Id :=
1809 Make_Defining_Identifier (Loc,
1810 Chars => New_External_Name (Chars (Scope (E)), 'A'));
1811 -- The parameter that designates the synchronized object in the call
1813 Actuals : constant List_Id := New_List;
1814 -- The actuals in the entry call
1816 Decls : constant List_Id := New_List;
1818 Entry_Call : Node_Id;
1819 Entry_Name : Node_Id;
1822 -- The specification of the wrapper procedure
1826 -- Only build the wrapper if entry has pre/postconditions.
1827 -- Should this be done unconditionally instead ???
1833 P := Spec_PPC_List (Contract (E));
1838 -- Transfer ppc pragmas to the declarations of the wrapper
1840 while Present (P) loop
1841 if Pragma_Name (P) = Name_Precondition
1842 or else Pragma_Name (P) = Name_Postcondition
1844 Append (Relocate_Node (P), Decls);
1845 Set_Analyzed (Last (Decls), False);
1848 P := Next_Pragma (P);
1852 -- First formal is synchronized object
1855 Make_Parameter_Specification (Loc,
1856 Defining_Identifier => Synch_Id,
1857 Out_Present => True,
1859 Parameter_Type => New_Occurrence_Of (Scope (E), Loc)));
1862 Make_Selected_Component (Loc,
1863 Prefix => New_Occurrence_Of (Synch_Id, Loc),
1864 Selector_Name => New_Occurrence_Of (E, Loc));
1866 -- If entity is entry family, second formal is the corresponding index,
1867 -- and entry name is an indexed component.
1869 if Ekind (E) = E_Entry_Family then
1871 Index : constant Entity_Id :=
1872 Make_Defining_Identifier (Loc, Name_I);
1875 Make_Parameter_Specification (Loc,
1876 Defining_Identifier => Index,
1878 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1881 Make_Indexed_Component (Loc,
1882 Prefix => Entry_Name,
1883 Expressions => New_List (New_Occurrence_Of (Index, Loc)));
1888 Make_Procedure_Call_Statement (Loc,
1890 Parameter_Associations => Actuals);
1892 -- Now add formals that match those of the entry, and build actuals for
1893 -- the nested entry call.
1897 New_Form : Entity_Id;
1898 Parm_Spec : Node_Id;
1901 Form := First_Formal (E);
1902 while Present (Form) loop
1903 New_Form := Make_Defining_Identifier (Loc, Chars (Form));
1905 Make_Parameter_Specification (Loc,
1906 Defining_Identifier => New_Form,
1907 Out_Present => Out_Present (Parent (Form)),
1908 In_Present => In_Present (Parent (Form)),
1909 Parameter_Type => New_Occurrence_Of (Etype (Form), Loc));
1911 Append (Parm_Spec, Specs);
1912 Append (New_Occurrence_Of (New_Form, Loc), Actuals);
1917 -- Add renaming declarations for the discriminants of the enclosing
1918 -- type, which may be visible in the preconditions.
1920 if Has_Discriminants (Synch_Type) then
1926 D := First_Discriminant (Synch_Type);
1927 while Present (D) loop
1929 Make_Object_Renaming_Declaration (Loc,
1930 Defining_Identifier =>
1931 Make_Defining_Identifier (Loc, Chars (D)),
1932 Subtype_Mark => New_Reference_To (Etype (D), Loc),
1934 Make_Selected_Component (Loc,
1935 Prefix => New_Reference_To (Synch_Id, Loc),
1936 Selector_Name => Make_Identifier (Loc, Chars (D))));
1937 Prepend (Decl, Decls);
1938 Next_Discriminant (D);
1943 Set_PPC_Wrapper (E, Wrapper_Id);
1945 Make_Subprogram_Body (Loc,
1947 Make_Procedure_Specification (Loc,
1948 Defining_Unit_Name => Wrapper_Id,
1949 Parameter_Specifications => Specs),
1950 Declarations => Decls,
1951 Handled_Statement_Sequence =>
1952 Make_Handled_Sequence_Of_Statements (Loc,
1953 Statements => New_List (Entry_Call)));
1955 -- The wrapper body is analyzed when the enclosing type is frozen
1957 Append_Freeze_Action (Defining_Entity (Decl), Wrapper_Body);
1958 end Build_PPC_Wrapper;
1960 --------------------------
1961 -- Build_Wrapper_Bodies --
1962 --------------------------
1964 procedure Build_Wrapper_Bodies
1969 Rec_Typ : Entity_Id;
1971 function Build_Wrapper_Body
1973 Subp_Id : Entity_Id;
1974 Obj_Typ : Entity_Id;
1975 Formals : List_Id) return Node_Id;
1976 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
1977 -- associated with a protected or task type. Subp_Id is the subprogram
1978 -- name which will be wrapped. Obj_Typ is the type of the new formal
1979 -- parameter which handles dispatching and object notation. Formals are
1980 -- the original formals of Subp_Id which will be explicitly replicated.
1982 ------------------------
1983 -- Build_Wrapper_Body --
1984 ------------------------
1986 function Build_Wrapper_Body
1988 Subp_Id : Entity_Id;
1989 Obj_Typ : Entity_Id;
1990 Formals : List_Id) return Node_Id
1992 Body_Spec : Node_Id;
1995 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
1997 -- The subprogram is not overriding or is not a primitive declared
1998 -- between two views.
2000 if No (Body_Spec) then
2005 Actuals : List_Id := No_List;
2007 First_Form : Node_Id;
2012 -- Map formals to actuals. Use the list built for the wrapper
2013 -- spec, skipping the object notation parameter.
2015 First_Form := First (Parameter_Specifications (Body_Spec));
2017 Formal := First_Form;
2020 if Present (Formal) then
2021 Actuals := New_List;
2022 while Present (Formal) loop
2024 Make_Identifier (Loc,
2025 Chars => Chars (Defining_Identifier (Formal))));
2030 -- Special processing for primitives declared between a private
2031 -- type and its completion: the wrapper needs a properly typed
2032 -- parameter if the wrapped operation has a controlling first
2033 -- parameter. Note that this might not be the case for a function
2034 -- with a controlling result.
2036 if Is_Private_Primitive_Subprogram (Subp_Id) then
2037 if No (Actuals) then
2038 Actuals := New_List;
2041 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2042 Prepend_To (Actuals,
2043 Unchecked_Convert_To
2044 (Corresponding_Concurrent_Type (Obj_Typ),
2045 Make_Identifier (Loc, Name_uO)));
2048 Prepend_To (Actuals,
2049 Make_Identifier (Loc,
2050 Chars => Chars (Defining_Identifier (First_Form))));
2053 Nam := New_Reference_To (Subp_Id, Loc);
2055 -- An access-to-variable object parameter requires an explicit
2056 -- dereference in the unchecked conversion. This case occurs
2057 -- when a protected entry wrapper must override an interface
2058 -- level procedure with interface access as first parameter.
2060 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2062 if Nkind (Parameter_Type (First_Form)) =
2066 Make_Explicit_Dereference (Loc,
2067 Prefix => Make_Identifier (Loc, Name_uO));
2069 Conv_Id := Make_Identifier (Loc, Name_uO);
2073 Make_Selected_Component (Loc,
2075 Unchecked_Convert_To
2076 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2077 Selector_Name => New_Reference_To (Subp_Id, Loc));
2080 -- Create the subprogram body. For a function, the call to the
2081 -- actual subprogram has to be converted to the corresponding
2082 -- record if it is a controlling result.
2084 if Ekind (Subp_Id) = E_Function then
2090 Make_Function_Call (Loc,
2092 Parameter_Associations => Actuals);
2094 if Has_Controlling_Result (Subp_Id) then
2096 Unchecked_Convert_To
2097 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2101 Make_Subprogram_Body (Loc,
2102 Specification => Body_Spec,
2103 Declarations => Empty_List,
2104 Handled_Statement_Sequence =>
2105 Make_Handled_Sequence_Of_Statements (Loc,
2106 Statements => New_List (
2107 Make_Simple_Return_Statement (Loc, Res))));
2112 Make_Subprogram_Body (Loc,
2113 Specification => Body_Spec,
2114 Declarations => Empty_List,
2115 Handled_Statement_Sequence =>
2116 Make_Handled_Sequence_Of_Statements (Loc,
2117 Statements => New_List (
2118 Make_Procedure_Call_Statement (Loc,
2120 Parameter_Associations => Actuals))));
2123 end Build_Wrapper_Body;
2125 -- Start of processing for Build_Wrapper_Bodies
2128 if Is_Concurrent_Type (Typ) then
2129 Rec_Typ := Corresponding_Record_Type (Typ);
2134 -- Generate wrapper bodies for a concurrent type which implements an
2137 if Present (Interfaces (Rec_Typ)) then
2139 Insert_Nod : Node_Id;
2141 Prim_Elmt : Elmt_Id;
2142 Prim_Decl : Node_Id;
2144 Wrap_Body : Node_Id;
2145 Wrap_Id : Entity_Id;
2150 -- Examine all primitive operations of the corresponding record
2151 -- type, looking for wrapper specs. Generate bodies in order to
2154 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2155 while Present (Prim_Elmt) loop
2156 Prim := Node (Prim_Elmt);
2158 if (Ekind (Prim) = E_Function
2159 or else Ekind (Prim) = E_Procedure)
2160 and then Is_Primitive_Wrapper (Prim)
2162 Subp := Wrapped_Entity (Prim);
2163 Prim_Decl := Parent (Parent (Prim));
2166 Build_Wrapper_Body (Loc,
2169 Formals => Parameter_Specifications (Parent (Subp)));
2170 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2172 Set_Corresponding_Spec (Wrap_Body, Prim);
2173 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2175 Insert_After (Insert_Nod, Wrap_Body);
2176 Insert_Nod := Wrap_Body;
2178 Analyze (Wrap_Body);
2181 Next_Elmt (Prim_Elmt);
2185 end Build_Wrapper_Bodies;
2187 ------------------------
2188 -- Build_Wrapper_Spec --
2189 ------------------------
2191 function Build_Wrapper_Spec
2192 (Subp_Id : Entity_Id;
2193 Obj_Typ : Entity_Id;
2194 Formals : List_Id) return Node_Id
2196 Loc : constant Source_Ptr := Sloc (Subp_Id);
2197 First_Param : Node_Id;
2199 Iface_Elmt : Elmt_Id;
2200 Iface_Op : Entity_Id;
2201 Iface_Op_Elmt : Elmt_Id;
2203 function Overriding_Possible
2204 (Iface_Op : Entity_Id;
2205 Wrapper : Entity_Id) return Boolean;
2206 -- Determine whether a primitive operation can be overridden by Wrapper.
2207 -- Iface_Op is the candidate primitive operation of an interface type,
2208 -- Wrapper is the generated entry wrapper.
2210 function Replicate_Formals
2212 Formals : List_Id) return List_Id;
2213 -- An explicit parameter replication is required due to the Is_Entry_
2214 -- Formal flag being set for all the formals of an entry. The explicit
2215 -- replication removes the flag that would otherwise cause a different
2216 -- path of analysis.
2218 -------------------------
2219 -- Overriding_Possible --
2220 -------------------------
2222 function Overriding_Possible
2223 (Iface_Op : Entity_Id;
2224 Wrapper : Entity_Id) return Boolean
2226 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2227 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2229 function Type_Conformant_Parameters
2230 (Iface_Op_Params : List_Id;
2231 Wrapper_Params : List_Id) return Boolean;
2232 -- Determine whether the parameters of the generated entry wrapper
2233 -- and those of a primitive operation are type conformant. During
2234 -- this check, the first parameter of the primitive operation is
2235 -- skipped if it is a controlling argument: protected functions
2236 -- may have a controlling result.
2238 --------------------------------
2239 -- Type_Conformant_Parameters --
2240 --------------------------------
2242 function Type_Conformant_Parameters
2243 (Iface_Op_Params : List_Id;
2244 Wrapper_Params : List_Id) return Boolean
2246 Iface_Op_Param : Node_Id;
2247 Iface_Op_Typ : Entity_Id;
2248 Wrapper_Param : Node_Id;
2249 Wrapper_Typ : Entity_Id;
2252 -- Skip the first (controlling) parameter of primitive operation
2254 Iface_Op_Param := First (Iface_Op_Params);
2256 if Present (First_Formal (Iface_Op))
2257 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2259 Iface_Op_Param := Next (Iface_Op_Param);
2262 Wrapper_Param := First (Wrapper_Params);
2263 while Present (Iface_Op_Param)
2264 and then Present (Wrapper_Param)
2266 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2267 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2269 -- The two parameters must be mode conformant
2271 if not Conforming_Types
2272 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2277 Next (Iface_Op_Param);
2278 Next (Wrapper_Param);
2281 -- One of the lists is longer than the other
2283 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2288 end Type_Conformant_Parameters;
2290 -- Start of processing for Overriding_Possible
2293 if Chars (Iface_Op) /= Chars (Wrapper) then
2297 -- If an inherited subprogram is implemented by a protected procedure
2298 -- or an entry, then the first parameter of the inherited subprogram
2299 -- shall be of mode OUT or IN OUT, or access-to-variable parameter.
2301 if Ekind (Iface_Op) = E_Procedure
2302 and then Present (Parameter_Specifications (Iface_Op_Spec))
2305 Obj_Param : constant Node_Id :=
2306 First (Parameter_Specifications (Iface_Op_Spec));
2308 if not Out_Present (Obj_Param)
2309 and then Nkind (Parameter_Type (Obj_Param)) /=
2318 Type_Conformant_Parameters (
2319 Parameter_Specifications (Iface_Op_Spec),
2320 Parameter_Specifications (Wrapper_Spec));
2321 end Overriding_Possible;
2323 -----------------------
2324 -- Replicate_Formals --
2325 -----------------------
2327 function Replicate_Formals
2329 Formals : List_Id) return List_Id
2331 New_Formals : constant List_Id := New_List;
2333 Param_Type : Node_Id;
2336 Formal := First (Formals);
2338 -- Skip the object parameter when dealing with primitives declared
2339 -- between two views.
2341 if Is_Private_Primitive_Subprogram (Subp_Id)
2342 and then not Has_Controlling_Result (Subp_Id)
2344 Formal := Next (Formal);
2347 while Present (Formal) loop
2349 -- Create an explicit copy of the entry parameter
2351 -- When creating the wrapper subprogram for a primitive operation
2352 -- of a protected interface we must construct an equivalent
2353 -- signature to that of the overriding operation. For regular
2354 -- parameters we can just use the type of the formal, but for
2355 -- access to subprogram parameters we need to reanalyze the
2356 -- parameter type to create local entities for the signature of
2357 -- the subprogram type. Using the entities of the overriding
2358 -- subprogram will result in out-of-scope errors in the back-end.
2360 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2361 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2364 New_Reference_To (Etype (Parameter_Type (Formal)), Loc);
2367 Append_To (New_Formals,
2368 Make_Parameter_Specification (Loc,
2369 Defining_Identifier =>
2370 Make_Defining_Identifier (Loc,
2371 Chars => Chars (Defining_Identifier (Formal))),
2372 In_Present => In_Present (Formal),
2373 Out_Present => Out_Present (Formal),
2374 Parameter_Type => Param_Type));
2380 end Replicate_Formals;
2382 -- Start of processing for Build_Wrapper_Spec
2385 -- There is no point in building wrappers for non-tagged concurrent
2388 pragma Assert (Is_Tagged_Type (Obj_Typ));
2390 -- An entry or a protected procedure can override a routine where the
2391 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2392 -- type. Since the wrapper must have the exact same signature as that of
2393 -- the overridden subprogram, we try to find the overriding candidate
2394 -- and use its controlling formal.
2396 First_Param := Empty;
2398 -- Check every implemented interface
2400 if Present (Interfaces (Obj_Typ)) then
2401 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2402 Search : while Present (Iface_Elmt) loop
2403 Iface := Node (Iface_Elmt);
2405 -- Check every interface primitive
2407 if Present (Primitive_Operations (Iface)) then
2408 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2409 while Present (Iface_Op_Elmt) loop
2410 Iface_Op := Node (Iface_Op_Elmt);
2412 -- Ignore predefined primitives
2414 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2415 Iface_Op := Ultimate_Alias (Iface_Op);
2417 -- The current primitive operation can be overridden by
2418 -- the generated entry wrapper.
2420 if Overriding_Possible (Iface_Op, Subp_Id) then
2422 First (Parameter_Specifications (Parent (Iface_Op)));
2428 Next_Elmt (Iface_Op_Elmt);
2432 Next_Elmt (Iface_Elmt);
2436 -- Ada 2012 (AI05-0090-1): If no interface primitive is covered by
2437 -- this subprogram and this is not a primitive declared between two
2438 -- views then force the generation of a wrapper. As an optimization,
2439 -- previous versions of the frontend avoid generating the wrapper;
2440 -- however, the wrapper facilitates locating and reporting an error
2441 -- when a duplicate declaration is found later. See example in
2445 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2448 (Corresponding_Concurrent_Type (Obj_Typ))
2451 Make_Parameter_Specification (Loc,
2452 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2454 Out_Present => False,
2455 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2457 -- For entries and procedures of protected types the mode of
2458 -- the controlling argument must be in-out.
2462 Make_Parameter_Specification (Loc,
2463 Defining_Identifier =>
2464 Make_Defining_Identifier (Loc,
2467 Out_Present => (Ekind (Subp_Id) /= E_Function),
2468 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2473 Wrapper_Id : constant Entity_Id :=
2474 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2475 New_Formals : List_Id;
2476 Obj_Param : Node_Id;
2477 Obj_Param_Typ : Entity_Id;
2480 -- Minimum decoration is needed to catch the entity in
2481 -- Sem_Ch6.Override_Dispatching_Operation.
2483 if Ekind (Subp_Id) = E_Function then
2484 Set_Ekind (Wrapper_Id, E_Function);
2486 Set_Ekind (Wrapper_Id, E_Procedure);
2489 Set_Is_Primitive_Wrapper (Wrapper_Id);
2490 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2491 Set_Is_Private_Primitive (Wrapper_Id,
2492 Is_Private_Primitive_Subprogram (Subp_Id));
2494 -- Process the formals
2496 New_Formals := Replicate_Formals (Loc, Formals);
2498 -- A function with a controlling result and no first controlling
2499 -- formal needs no additional parameter.
2501 if Has_Controlling_Result (Subp_Id)
2503 (No (First_Formal (Subp_Id))
2504 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2508 -- Routine Subp_Id has been found to override an interface primitive.
2509 -- If the interface operation has an access parameter, create a copy
2510 -- of it, with the same null exclusion indicator if present.
2512 elsif Present (First_Param) then
2513 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2515 Make_Access_Definition (Loc,
2517 New_Reference_To (Obj_Typ, Loc));
2518 Set_Null_Exclusion_Present (Obj_Param_Typ,
2519 Null_Exclusion_Present (Parameter_Type (First_Param)));
2522 Obj_Param_Typ := New_Reference_To (Obj_Typ, Loc);
2526 Make_Parameter_Specification (Loc,
2527 Defining_Identifier =>
2528 Make_Defining_Identifier (Loc,
2530 In_Present => In_Present (First_Param),
2531 Out_Present => Out_Present (First_Param),
2532 Parameter_Type => Obj_Param_Typ);
2534 Prepend_To (New_Formals, Obj_Param);
2536 -- If we are dealing with a primitive declared between two views,
2537 -- implemented by a synchronized operation, we need to create
2538 -- a default parameter. The mode of the parameter must match that
2539 -- of the primitive operation.
2542 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2544 Make_Parameter_Specification (Loc,
2545 Defining_Identifier =>
2546 Make_Defining_Identifier (Loc, Name_uO),
2547 In_Present => In_Present (Parent (First_Entity (Subp_Id))),
2548 Out_Present => Ekind (Subp_Id) /= E_Function,
2549 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2550 Prepend_To (New_Formals, Obj_Param);
2553 -- Build the final spec. If it is a function with a controlling
2554 -- result, it is a primitive operation of the corresponding
2555 -- record type, so mark the spec accordingly.
2557 if Ekind (Subp_Id) = E_Function then
2562 if Has_Controlling_Result (Subp_Id) then
2565 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2567 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2571 Make_Function_Specification (Loc,
2572 Defining_Unit_Name => Wrapper_Id,
2573 Parameter_Specifications => New_Formals,
2574 Result_Definition => Res_Def);
2578 Make_Procedure_Specification (Loc,
2579 Defining_Unit_Name => Wrapper_Id,
2580 Parameter_Specifications => New_Formals);
2583 end Build_Wrapper_Spec;
2585 -------------------------
2586 -- Build_Wrapper_Specs --
2587 -------------------------
2589 procedure Build_Wrapper_Specs
2595 Rec_Typ : Entity_Id;
2596 procedure Scan_Declarations (L : List_Id);
2597 -- Common processing for visible and private declarations
2598 -- of a protected type.
2600 procedure Scan_Declarations (L : List_Id) is
2602 Wrap_Decl : Node_Id;
2603 Wrap_Spec : Node_Id;
2611 while Present (Decl) loop
2614 if Nkind (Decl) = N_Entry_Declaration
2615 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2619 (Subp_Id => Defining_Identifier (Decl),
2621 Formals => Parameter_Specifications (Decl));
2623 elsif Nkind (Decl) = N_Subprogram_Declaration then
2626 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2629 Parameter_Specifications (Specification (Decl)));
2632 if Present (Wrap_Spec) then
2634 Make_Subprogram_Declaration (Loc,
2635 Specification => Wrap_Spec);
2637 Insert_After (N, Wrap_Decl);
2640 Analyze (Wrap_Decl);
2645 end Scan_Declarations;
2647 -- start of processing for Build_Wrapper_Specs
2650 if Is_Protected_Type (Typ) then
2651 Def := Protected_Definition (Parent (Typ));
2652 else pragma Assert (Is_Task_Type (Typ));
2653 Def := Task_Definition (Parent (Typ));
2656 Rec_Typ := Corresponding_Record_Type (Typ);
2658 -- Generate wrapper specs for a concurrent type which implements an
2659 -- interface. Operations in both the visible and private parts may
2660 -- implement progenitor operations.
2662 if Present (Interfaces (Rec_Typ))
2663 and then Present (Def)
2665 Scan_Declarations (Visible_Declarations (Def));
2666 Scan_Declarations (Private_Declarations (Def));
2668 end Build_Wrapper_Specs;
2670 ---------------------------
2671 -- Build_Find_Body_Index --
2672 ---------------------------
2674 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2675 Loc : constant Source_Ptr := Sloc (Typ);
2678 Has_F : Boolean := False;
2680 If_St : Node_Id := Empty;
2683 Decls : List_Id := New_List;
2686 Siz : Node_Id := Empty;
2688 procedure Add_If_Clause (Expr : Node_Id);
2689 -- Add test for range of current entry
2691 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2692 -- If a bound of an entry is given by a discriminant, retrieve the
2693 -- actual value of the discriminant from the enclosing object.
2699 procedure Add_If_Clause (Expr : Node_Id) is
2701 Stats : constant List_Id :=
2703 Make_Simple_Return_Statement (Loc,
2704 Expression => Make_Integer_Literal (Loc, Index + 1)));
2707 -- Index for current entry body
2711 -- Compute total length of entry queues so far
2719 Right_Opnd => Expr);
2724 Left_Opnd => Make_Identifier (Loc, Name_uE),
2727 -- Map entry queue indexes in the range of the current family
2728 -- into the current index, that designates the entry body.
2732 Make_Implicit_If_Statement (Typ,
2734 Then_Statements => Stats,
2735 Elsif_Parts => New_List);
2741 Make_Elsif_Part (Loc,
2743 Then_Statements => Stats),
2744 Elsif_Parts (If_St));
2748 ------------------------------
2749 -- Convert_Discriminant_Ref --
2750 ------------------------------
2752 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2756 if Is_Entity_Name (Bound)
2757 and then Ekind (Entity (Bound)) = E_Discriminant
2760 Make_Selected_Component (Loc,
2762 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2763 Make_Explicit_Dereference (Loc,
2764 Make_Identifier (Loc, Name_uObject))),
2765 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2766 Set_Etype (B, Etype (Entity (Bound)));
2768 B := New_Copy_Tree (Bound);
2772 end Convert_Discriminant_Ref;
2774 -- Start of processing for Build_Find_Body_Index
2777 Spec := Build_Find_Body_Index_Spec (Typ);
2779 Ent := First_Entity (Typ);
2780 while Present (Ent) loop
2781 if Ekind (Ent) = E_Entry_Family then
2791 -- If the protected type has no entry families, there is a one-one
2792 -- correspondence between entry queue and entry body.
2795 Make_Simple_Return_Statement (Loc,
2796 Expression => Make_Identifier (Loc, Name_uE));
2799 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2802 -- if E <= l1 then return 1;
2803 -- elsif E <= l1 + l2 then return 2;
2808 Ent := First_Entity (Typ);
2810 Add_Object_Pointer (Loc, Typ, Decls);
2812 while Present (Ent) loop
2813 if Ekind (Ent) = E_Entry then
2814 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2816 elsif Ekind (Ent) = E_Entry_Family then
2817 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2818 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2819 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2820 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2829 Make_Simple_Return_Statement (Loc,
2830 Expression => Make_Integer_Literal (Loc, 1));
2832 elsif Nkind (Ret) = N_If_Statement then
2834 -- Ranges are in increasing order, so last one doesn't need guard
2837 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2840 Set_Else_Statements (Ret, Then_Statements (Nod));
2846 Make_Subprogram_Body (Loc,
2847 Specification => Spec,
2848 Declarations => Decls,
2849 Handled_Statement_Sequence =>
2850 Make_Handled_Sequence_Of_Statements (Loc,
2851 Statements => New_List (Ret)));
2852 end Build_Find_Body_Index;
2854 --------------------------------
2855 -- Build_Find_Body_Index_Spec --
2856 --------------------------------
2858 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2859 Loc : constant Source_Ptr := Sloc (Typ);
2860 Id : constant Entity_Id :=
2861 Make_Defining_Identifier (Loc,
2862 Chars => New_External_Name (Chars (Typ), 'F'));
2863 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2864 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2868 Make_Function_Specification (Loc,
2869 Defining_Unit_Name => Id,
2870 Parameter_Specifications => New_List (
2871 Make_Parameter_Specification (Loc,
2872 Defining_Identifier => Parm1,
2874 New_Reference_To (RTE (RE_Address), Loc)),
2876 Make_Parameter_Specification (Loc,
2877 Defining_Identifier => Parm2,
2879 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
2880 Result_Definition => New_Occurrence_Of (
2881 RTE (RE_Protected_Entry_Index), Loc));
2882 end Build_Find_Body_Index_Spec;
2884 -------------------------
2885 -- Build_Master_Entity --
2886 -------------------------
2888 procedure Build_Master_Entity
2890 Use_Current : Boolean := False)
2892 Loc : constant Source_Ptr := Sloc (Id);
2894 Master_Decl : Node_Id;
2895 Master_Scop : Entity_Id;
2899 Master_Scop := Current_Scope;
2901 Master_Scop := Find_Master_Scope (Id);
2904 -- Do not create a master if the enclosing scope already has one or if
2905 -- there is no task hierarchy.
2907 if Has_Master_Entity (Master_Scop)
2908 or else Restriction_Active (No_Task_Hierarchy)
2913 -- Determine the proper context to insert the master
2915 if Is_Access_Type (Id) and then Is_Itype (Id) then
2916 Context := Associated_Node_For_Itype (Id);
2918 Context := Parent (Id);
2921 -- Create a master, generate:
2922 -- _Master : constant Master_Id := Current_Master.all;
2925 Make_Object_Declaration (Loc,
2926 Defining_Identifier =>
2927 Make_Defining_Identifier (Loc, Name_uMaster),
2928 Constant_Present => True,
2929 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
2931 Make_Explicit_Dereference (Loc,
2932 New_Reference_To (RTE (RE_Current_Master), Loc)));
2934 Insert_Before (Context, Master_Decl);
2935 Analyze (Master_Decl);
2937 -- Mark the enclosing scope and its associated construct as being task
2940 Set_Has_Master_Entity (Master_Scop);
2942 while Nkind (Context) /= N_Compilation_Unit loop
2943 Context := Parent (Context);
2945 -- If we fall off the top, we are at the outer level, and the
2946 -- environment task is our effective master, so nothing to mark.
2948 if Nkind_In (Context, N_Block_Statement,
2952 Set_Is_Task_Master (Context, True);
2955 elsif Nkind (Parent (Context)) = N_Subunit then
2956 Context := Corresponding_Stub (Parent (Context));
2959 end Build_Master_Entity;
2961 ---------------------------
2962 -- Build_Master_Renaming --
2963 ---------------------------
2965 procedure Build_Master_Renaming (N : Node_Id; Typ : Entity_Id) is
2966 Loc : constant Source_Ptr := Sloc (N);
2967 Master_Decl : Node_Id;
2968 Master_Id : Entity_Id;
2971 -- Nothing to do if there is no task hierarchy
2973 if Restriction_Active (No_Task_Hierarchy) then
2978 Make_Defining_Identifier (Loc,
2979 New_External_Name (Chars (Typ), 'M'));
2982 Make_Object_Renaming_Declaration (Loc,
2983 Defining_Identifier => Master_Id,
2984 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
2985 Name => Make_Identifier (Loc, Name_uMaster));
2987 Insert_Before (N, Master_Decl);
2988 Analyze (Master_Decl);
2990 Set_Master_Id (Typ, Master_Id);
2991 end Build_Master_Renaming;
2993 -----------------------------------------
2994 -- Build_Private_Protected_Declaration --
2995 -----------------------------------------
2997 function Build_Private_Protected_Declaration
2998 (N : Node_Id) return Entity_Id
3000 Loc : constant Source_Ptr := Sloc (N);
3001 Body_Id : constant Entity_Id := Defining_Entity (N);
3006 Spec_Id : Entity_Id;
3009 Formal := First_Formal (Body_Id);
3011 -- The protected operation always has at least one formal, namely the
3012 -- object itself, but it is only placed in the parameter list if
3013 -- expansion is enabled.
3015 if Present (Formal) or else Expander_Active then
3016 Plist := Copy_Parameter_List (Body_Id);
3021 if Nkind (Specification (N)) = N_Procedure_Specification then
3023 Make_Procedure_Specification (Loc,
3024 Defining_Unit_Name =>
3025 Make_Defining_Identifier (Sloc (Body_Id),
3026 Chars => Chars (Body_Id)),
3027 Parameter_Specifications =>
3031 Make_Function_Specification (Loc,
3032 Defining_Unit_Name =>
3033 Make_Defining_Identifier (Sloc (Body_Id),
3034 Chars => Chars (Body_Id)),
3035 Parameter_Specifications => Plist,
3036 Result_Definition =>
3037 New_Occurrence_Of (Etype (Body_Id), Loc));
3040 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3041 Insert_Before (N, Decl);
3042 Spec_Id := Defining_Unit_Name (New_Spec);
3044 -- Indicate that the entity comes from source, to ensure that cross-
3045 -- reference information is properly generated. The body itself is
3046 -- rewritten during expansion, and the body entity will not appear in
3047 -- calls to the operation.
3049 Set_Comes_From_Source (Spec_Id, True);
3051 Set_Has_Completion (Spec_Id);
3052 Set_Convention (Spec_Id, Convention_Protected);
3054 end Build_Private_Protected_Declaration;
3056 ---------------------------
3057 -- Build_Protected_Entry --
3058 ---------------------------
3060 function Build_Protected_Entry
3063 Pid : Node_Id) return Node_Id
3065 Loc : constant Source_Ptr := Sloc (N);
3067 Decls : constant List_Id := Declarations (N);
3068 End_Lab : constant Node_Id :=
3069 End_Label (Handled_Statement_Sequence (N));
3070 End_Loc : constant Source_Ptr :=
3071 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3072 -- Used for the generated call to Complete_Entry_Body
3074 Han_Loc : Source_Ptr;
3075 -- Used for the exception handler, inserted at end of the body
3077 Op_Decls : constant List_Id := New_List;
3085 -- Set the source location on the exception handler only when debugging
3086 -- the expanded code (see Make_Implicit_Exception_Handler).
3088 if Debug_Generated_Code then
3091 -- Otherwise the inserted code should not be visible to the debugger
3094 Han_Loc := No_Location;
3098 Make_Defining_Identifier (Loc,
3099 Chars => Chars (Protected_Body_Subprogram (Ent)));
3101 Build_Protected_Entry_Specification (Loc, Edef, Empty);
3103 -- Add the following declarations:
3104 -- type poVP is access poV;
3105 -- _object : poVP := poVP (_O);
3107 -- where _O is the formal parameter associated with the concurrent
3108 -- object. These declarations are needed for Complete_Entry_Body.
3110 Add_Object_Pointer (Loc, Pid, Op_Decls);
3112 -- Add renamings for all formals, the Protection object, discriminals,
3113 -- privals and the entry index constant for use by debugger.
3115 Add_Formal_Renamings (Espec, Op_Decls, Ent, Loc);
3116 Debug_Private_Data_Declarations (Decls);
3118 case Corresponding_Runtime_Package (Pid) is
3119 when System_Tasking_Protected_Objects_Entries =>
3121 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
3123 when System_Tasking_Protected_Objects_Single_Entry =>
3125 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
3128 raise Program_Error;
3131 Op_Stats := New_List (
3132 Make_Block_Statement (Loc,
3133 Declarations => Decls,
3134 Handled_Statement_Sequence =>
3135 Handled_Statement_Sequence (N)),
3137 Make_Procedure_Call_Statement (End_Loc,
3139 Parameter_Associations => New_List (
3140 Make_Attribute_Reference (End_Loc,
3142 Make_Selected_Component (End_Loc,
3143 Prefix => Make_Identifier (End_Loc, Name_uObject),
3144 Selector_Name => Make_Identifier (End_Loc, Name_uObject)),
3145 Attribute_Name => Name_Unchecked_Access))));
3147 -- When exceptions can not be propagated, we never need to call
3148 -- Exception_Complete_Entry_Body
3150 if No_Exception_Handlers_Set then
3152 Make_Subprogram_Body (Loc,
3153 Specification => Espec,
3154 Declarations => Op_Decls,
3155 Handled_Statement_Sequence =>
3156 Make_Handled_Sequence_Of_Statements (Loc,
3157 Statements => Op_Stats,
3158 End_Label => End_Lab));
3161 Ohandle := Make_Others_Choice (Loc);
3162 Set_All_Others (Ohandle);
3164 case Corresponding_Runtime_Package (Pid) is
3165 when System_Tasking_Protected_Objects_Entries =>
3168 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3170 when System_Tasking_Protected_Objects_Single_Entry =>
3173 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3176 raise Program_Error;
3179 -- Establish link between subprogram body entity and source entry
3181 Set_Corresponding_Protected_Entry (Edef, Ent);
3183 -- Create body of entry procedure. The renaming declarations are
3184 -- placed ahead of the block that contains the actual entry body.
3187 Make_Subprogram_Body (Loc,
3188 Specification => Espec,
3189 Declarations => Op_Decls,
3190 Handled_Statement_Sequence =>
3191 Make_Handled_Sequence_Of_Statements (Loc,
3192 Statements => Op_Stats,
3193 End_Label => End_Lab,
3194 Exception_Handlers => New_List (
3195 Make_Implicit_Exception_Handler (Han_Loc,
3196 Exception_Choices => New_List (Ohandle),
3198 Statements => New_List (
3199 Make_Procedure_Call_Statement (Han_Loc,
3201 Parameter_Associations => New_List (
3202 Make_Attribute_Reference (Han_Loc,
3204 Make_Selected_Component (Han_Loc,
3206 Make_Identifier (Han_Loc, Name_uObject),
3208 Make_Identifier (Han_Loc, Name_uObject)),
3209 Attribute_Name => Name_Unchecked_Access),
3211 Make_Function_Call (Han_Loc,
3212 Name => New_Reference_To (
3213 RTE (RE_Get_GNAT_Exception), Loc)))))))));
3215 end Build_Protected_Entry;
3217 -----------------------------------------
3218 -- Build_Protected_Entry_Specification --
3219 -----------------------------------------
3221 function Build_Protected_Entry_Specification
3224 Ent_Id : Entity_Id) return Node_Id
3226 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3229 Set_Debug_Info_Needed (Def_Id);
3231 if Present (Ent_Id) then
3232 Append_Elmt (P, Accept_Address (Ent_Id));
3236 Make_Procedure_Specification (Loc,
3237 Defining_Unit_Name => Def_Id,
3238 Parameter_Specifications => New_List (
3239 Make_Parameter_Specification (Loc,
3240 Defining_Identifier =>
3241 Make_Defining_Identifier (Loc, Name_uO),
3243 New_Reference_To (RTE (RE_Address), Loc)),
3245 Make_Parameter_Specification (Loc,
3246 Defining_Identifier => P,
3248 New_Reference_To (RTE (RE_Address), Loc)),
3250 Make_Parameter_Specification (Loc,
3251 Defining_Identifier =>
3252 Make_Defining_Identifier (Loc, Name_uE),
3254 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
3255 end Build_Protected_Entry_Specification;
3257 --------------------------
3258 -- Build_Protected_Spec --
3259 --------------------------
3261 function Build_Protected_Spec
3263 Obj_Type : Entity_Id;
3265 Unprotected : Boolean := False) return List_Id
3267 Loc : constant Source_Ptr := Sloc (N);
3270 New_Plist : List_Id;
3271 New_Param : Node_Id;
3274 New_Plist := New_List;
3276 Formal := First_Formal (Ident);
3277 while Present (Formal) loop
3279 Make_Parameter_Specification (Loc,
3280 Defining_Identifier =>
3281 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
3282 In_Present => In_Present (Parent (Formal)),
3283 Out_Present => Out_Present (Parent (Formal)),
3284 Parameter_Type => New_Reference_To (Etype (Formal), Loc));
3287 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
3290 Append (New_Param, New_Plist);
3291 Next_Formal (Formal);
3294 -- If the subprogram is a procedure and the context is not an access
3295 -- to protected subprogram, the parameter is in-out. Otherwise it is
3299 Make_Parameter_Specification (Loc,
3300 Defining_Identifier =>
3301 Make_Defining_Identifier (Loc, Name_uObject),
3304 (Etype (Ident) = Standard_Void_Type
3305 and then not Is_RTE (Obj_Type, RE_Address)),
3307 New_Reference_To (Obj_Type, Loc));
3308 Set_Debug_Info_Needed (Defining_Identifier (Decl));
3309 Prepend_To (New_Plist, Decl);
3312 end Build_Protected_Spec;
3314 ---------------------------------------
3315 -- Build_Protected_Sub_Specification --
3316 ---------------------------------------
3318 function Build_Protected_Sub_Specification
3320 Prot_Typ : Entity_Id;
3321 Mode : Subprogram_Protection_Mode) return Node_Id
3323 Loc : constant Source_Ptr := Sloc (N);
3327 New_Plist : List_Id;
3330 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
3331 (Dispatching_Mode => ' ',
3332 Protected_Mode => 'P',
3333 Unprotected_Mode => 'N');
3336 if Ekind (Defining_Unit_Name (Specification (N))) =
3339 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
3344 Def_Id := Defining_Unit_Name (Specification (Decl));
3347 Build_Protected_Spec
3348 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
3349 Mode = Unprotected_Mode);
3351 Make_Defining_Identifier (Loc,
3352 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
3354 -- The unprotected operation carries the user code, and debugging
3355 -- information must be generated for it, even though this spec does
3356 -- not come from source. It is also convenient to allow gdb to step
3357 -- into the protected operation, even though it only contains lock/
3360 Set_Debug_Info_Needed (New_Id);
3362 -- If a pragma Eliminate applies to the source entity, the internal
3363 -- subprograms will be eliminated as well.
3365 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
3367 if Nkind (Specification (Decl)) = N_Procedure_Specification then
3369 Make_Procedure_Specification (Loc,
3370 Defining_Unit_Name => New_Id,
3371 Parameter_Specifications => New_Plist);
3373 -- Create a new specification for the anonymous subprogram type
3377 Make_Function_Specification (Loc,
3378 Defining_Unit_Name => New_Id,
3379 Parameter_Specifications => New_Plist,
3380 Result_Definition =>
3381 Copy_Result_Type (Result_Definition (Specification (Decl))));
3383 Set_Return_Present (Defining_Unit_Name (New_Spec));
3387 end Build_Protected_Sub_Specification;
3389 -------------------------------------
3390 -- Build_Protected_Subprogram_Body --
3391 -------------------------------------
3393 function Build_Protected_Subprogram_Body
3396 N_Op_Spec : Node_Id) return Node_Id
3398 Loc : constant Source_Ptr := Sloc (N);
3400 P_Op_Spec : Node_Id;
3403 Unprot_Call : Node_Id;
3405 Lock_Name : Node_Id;
3406 Lock_Stmt : Node_Id;
3407 Service_Name : Node_Id;
3409 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
3410 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
3412 Object_Parm : Node_Id;
3416 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
3417 -- Tell whether a given subprogram cannot raise an exception
3419 -----------------------
3420 -- Is_Exception_Safe --
3421 -----------------------
3423 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
3425 function Has_Side_Effect (N : Node_Id) return Boolean;
3426 -- Return True whenever encountering a subprogram call or raise
3427 -- statement of any kind in the sequence of statements
3429 ---------------------
3430 -- Has_Side_Effect --
3431 ---------------------
3433 -- What is this doing buried two levels down in exp_ch9. It seems
3434 -- like a generally useful function, and indeed there may be code
3435 -- duplication going on here ???
3437 function Has_Side_Effect (N : Node_Id) return Boolean is
3441 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
3442 -- Indicate whether N is a subprogram call or a raise statement
3444 ----------------------
3445 -- Is_Call_Or_Raise --
3446 ----------------------
3448 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
3450 return Nkind_In (N, N_Procedure_Call_Statement,
3453 N_Raise_Constraint_Error,
3454 N_Raise_Program_Error,
3455 N_Raise_Storage_Error);
3456 end Is_Call_Or_Raise;
3458 -- Start of processing for Has_Side_Effect
3462 while Present (Stmt) loop
3463 if Is_Call_Or_Raise (Stmt) then
3467 -- An object declaration can also contain a function call
3468 -- or a raise statement
3470 if Nkind (Stmt) = N_Object_Declaration then
3471 Expr := Expression (Stmt);
3473 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
3482 end Has_Side_Effect;
3484 -- Start of processing for Is_Exception_Safe
3487 -- If the checks handled by the back end are not disabled, we cannot
3488 -- ensure that no exception will be raised.
3490 if not Access_Checks_Suppressed (Empty)
3491 or else not Discriminant_Checks_Suppressed (Empty)
3492 or else not Range_Checks_Suppressed (Empty)
3493 or else not Index_Checks_Suppressed (Empty)
3494 or else Opt.Stack_Checking_Enabled
3499 if Has_Side_Effect (First (Declarations (Subprogram)))
3502 First (Statements (Handled_Statement_Sequence (Subprogram))))
3508 end Is_Exception_Safe;
3510 -- Start of processing for Build_Protected_Subprogram_Body
3513 Op_Spec := Specification (N);
3514 Exc_Safe := Is_Exception_Safe (N);
3517 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
3519 -- Build a list of the formal parameters of the protected version of
3520 -- the subprogram to use as the actual parameters of the unprotected
3523 Uactuals := New_List;
3524 Pformal := First (Parameter_Specifications (P_Op_Spec));
3525 while Present (Pformal) loop
3526 Append_To (Uactuals,
3527 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
3531 -- Make a call to the unprotected version of the subprogram built above
3532 -- for use by the protected version built below.
3534 if Nkind (Op_Spec) = N_Function_Specification then
3536 R := Make_Temporary (Loc, 'R');
3538 Make_Object_Declaration (Loc,
3539 Defining_Identifier => R,
3540 Constant_Present => True,
3541 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
3543 Make_Function_Call (Loc,
3544 Name => Make_Identifier (Loc,
3545 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3546 Parameter_Associations => Uactuals));
3549 Make_Simple_Return_Statement (Loc,
3550 Expression => New_Reference_To (R, Loc));
3553 Unprot_Call := Make_Simple_Return_Statement (Loc,
3554 Expression => Make_Function_Call (Loc,
3556 Make_Identifier (Loc,
3557 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3558 Parameter_Associations => Uactuals));
3561 Lock_Kind := RE_Lock_Read_Only;
3565 Make_Procedure_Call_Statement (Loc,
3567 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
3568 Parameter_Associations => Uactuals);
3570 Lock_Kind := RE_Lock;
3573 -- Wrap call in block that will be covered by an at_end handler
3575 if not Exc_Safe then
3576 Unprot_Call := Make_Block_Statement (Loc,
3577 Handled_Statement_Sequence =>
3578 Make_Handled_Sequence_Of_Statements (Loc,
3579 Statements => New_List (Unprot_Call)));
3582 -- Make the protected subprogram body. This locks the protected
3583 -- object and calls the unprotected version of the subprogram.
3585 case Corresponding_Runtime_Package (Pid) is
3586 when System_Tasking_Protected_Objects_Entries =>
3587 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
3588 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
3590 when System_Tasking_Protected_Objects_Single_Entry =>
3591 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
3592 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
3594 when System_Tasking_Protected_Objects =>
3595 Lock_Name := New_Reference_To (RTE (Lock_Kind), Loc);
3596 Service_Name := New_Reference_To (RTE (RE_Unlock), Loc);
3599 raise Program_Error;
3603 Make_Attribute_Reference (Loc,
3605 Make_Selected_Component (Loc,
3606 Prefix => Make_Identifier (Loc, Name_uObject),
3607 Selector_Name => Make_Identifier (Loc, Name_uObject)),
3608 Attribute_Name => Name_Unchecked_Access);
3610 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
3612 Parameter_Associations => New_List (Object_Parm));
3614 if Abort_Allowed then
3616 Make_Procedure_Call_Statement (Loc,
3617 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
3618 Parameter_Associations => Empty_List),
3622 Stmts := New_List (Lock_Stmt);
3625 if not Exc_Safe then
3626 Append (Unprot_Call, Stmts);
3628 if Nkind (Op_Spec) = N_Function_Specification then
3630 Stmts := Empty_List;
3632 Append (Unprot_Call, Stmts);
3636 Make_Procedure_Call_Statement (Loc,
3637 Name => Service_Name,
3638 Parameter_Associations =>
3639 New_List (New_Copy_Tree (Object_Parm))),
3642 if Abort_Allowed then
3644 Make_Procedure_Call_Statement (Loc,
3645 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
3646 Parameter_Associations => Empty_List),
3650 if Nkind (Op_Spec) = N_Function_Specification then
3651 Append (Return_Stmt, Stmts);
3652 Append (Make_Block_Statement (Loc,
3653 Declarations => New_List (Unprot_Call),
3654 Handled_Statement_Sequence =>
3655 Make_Handled_Sequence_Of_Statements (Loc,
3656 Statements => Stmts)), Pre_Stmts);
3662 Make_Subprogram_Body (Loc,
3663 Declarations => Empty_List,
3664 Specification => P_Op_Spec,
3665 Handled_Statement_Sequence =>
3666 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
3668 if not Exc_Safe then
3669 Set_Is_Protected_Subprogram_Body (Sub_Body);
3673 end Build_Protected_Subprogram_Body;
3675 -------------------------------------
3676 -- Build_Protected_Subprogram_Call --
3677 -------------------------------------
3679 procedure Build_Protected_Subprogram_Call
3683 External : Boolean := True)
3685 Loc : constant Source_Ptr := Sloc (N);
3686 Sub : constant Entity_Id := Entity (Name);
3692 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
3695 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
3698 if Present (Parameter_Associations (N)) then
3699 Params := New_Copy_List_Tree (Parameter_Associations (N));
3704 -- If the type is an untagged derived type, convert to the root type,
3705 -- which is the one on which the operations are defined.
3707 if Nkind (Rec) = N_Unchecked_Type_Conversion
3708 and then not Is_Tagged_Type (Etype (Rec))
3709 and then Is_Derived_Type (Etype (Rec))
3711 Set_Etype (Rec, Root_Type (Etype (Rec)));
3712 Set_Subtype_Mark (Rec,
3713 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
3716 Prepend (Rec, Params);
3718 if Ekind (Sub) = E_Procedure then
3720 Make_Procedure_Call_Statement (Loc,
3722 Parameter_Associations => Params));
3725 pragma Assert (Ekind (Sub) = E_Function);
3727 Make_Function_Call (Loc,
3729 Parameter_Associations => Params));
3733 and then Nkind (Rec) = N_Unchecked_Type_Conversion
3734 and then Is_Entity_Name (Expression (Rec))
3735 and then Is_Shared_Passive (Entity (Expression (Rec)))
3737 Add_Shared_Var_Lock_Procs (N);
3739 end Build_Protected_Subprogram_Call;
3741 -------------------------
3742 -- Build_Selected_Name --
3743 -------------------------
3745 function Build_Selected_Name
3746 (Prefix : Entity_Id;
3747 Selector : Entity_Id;
3748 Append_Char : Character := ' ') return Name_Id
3750 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
3751 Select_Len : Natural;
3754 Get_Name_String (Chars (Selector));
3755 Select_Len := Name_Len;
3756 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
3757 Get_Name_String (Chars (Prefix));
3759 -- If scope is anonymous type, discard suffix to recover name of
3760 -- single protected object. Otherwise use protected type name.
3762 if Name_Buffer (Name_Len) = 'T' then
3763 Name_Len := Name_Len - 1;
3766 Add_Str_To_Name_Buffer ("__");
3767 for J in 1 .. Select_Len loop
3768 Add_Char_To_Name_Buffer (Select_Buffer (J));
3771 -- Now add the Append_Char if specified. The encoding to follow
3772 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
3773 -- then the entity is associated to a protected type subprogram.
3774 -- Otherwise, it is a protected type entry. For each case, the
3775 -- encoding to follow for the suffix is documented in exp_dbug.ads.
3777 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
3779 if Append_Char /= ' ' then
3780 if Append_Char = 'P' or Append_Char = 'N' then
3781 Add_Char_To_Name_Buffer (Append_Char);
3784 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
3785 return New_External_Name (Name_Find, ' ', -1);
3790 end Build_Selected_Name;
3792 -----------------------------
3793 -- Build_Simple_Entry_Call --
3794 -----------------------------
3796 -- A task entry call is converted to a call to Call_Simple
3799 -- P : parms := (parm, parm, parm);
3801 -- Call_Simple (acceptor-task, entry-index, P'Address);
3807 -- Here Pnn is an aggregate of the type constructed for the entry to hold
3808 -- the parameters, and the constructed aggregate value contains either the
3809 -- parameters or, in the case of non-elementary types, references to these
3810 -- parameters. Then the address of this aggregate is passed to the runtime
3811 -- routine, along with the task id value and the task entry index value.
3812 -- Pnn is only required if parameters are present.
3814 -- The assignments after the call are present only in the case of in-out
3815 -- or out parameters for elementary types, and are used to assign back the
3816 -- resulting values of such parameters.
3818 -- Note: the reason that we insert a block here is that in the context
3819 -- of selects, conditional entry calls etc. the entry call statement
3820 -- appears on its own, not as an element of a list.
3822 -- A protected entry call is converted to a Protected_Entry_Call:
3825 -- P : E1_Params := (param, param, param);
3827 -- Bnn : Communications_Block;
3830 -- P : E1_Params := (param, param, param);
3831 -- Bnn : Communications_Block;
3834 -- Protected_Entry_Call (
3835 -- Object => po._object'Access,
3836 -- E => <entry index>;
3837 -- Uninterpreted_Data => P'Address;
3838 -- Mode => Simple_Call;
3845 procedure Build_Simple_Entry_Call
3854 -- If call has been inlined, nothing left to do
3856 if Nkind (N) = N_Block_Statement then
3860 -- Convert entry call to Call_Simple call
3863 Loc : constant Source_Ptr := Sloc (N);
3864 Parms : constant List_Id := Parameter_Associations (N);
3865 Stats : constant List_Id := New_List;
3868 Comm_Name : Entity_Id;
3872 Ent_Acc : Entity_Id;
3874 Iface_Tag : Entity_Id;
3875 Iface_Typ : Entity_Id;
3888 -- Simple entry and entry family cases merge here
3890 Ent := Entity (Ename);
3891 Ent_Acc := Entry_Parameters_Type (Ent);
3892 Conctyp := Etype (Concval);
3894 -- If prefix is an access type, dereference to obtain the task type
3896 if Is_Access_Type (Conctyp) then
3897 Conctyp := Designated_Type (Conctyp);
3900 -- Special case for protected subprogram calls
3902 if Is_Protected_Type (Conctyp)
3903 and then Is_Subprogram (Entity (Ename))
3905 if not Is_Eliminated (Entity (Ename)) then
3906 Build_Protected_Subprogram_Call
3907 (N, Ename, Convert_Concurrent (Concval, Conctyp));
3914 -- First parameter is the Task_Id value from the task value or the
3915 -- Object from the protected object value, obtained by selecting
3916 -- the _Task_Id or _Object from the result of doing an unchecked
3917 -- conversion to convert the value to the corresponding record type.
3919 if Nkind (Concval) = N_Function_Call
3920 and then Is_Task_Type (Conctyp)
3921 and then Ada_Version >= Ada_2005
3924 ExpR : constant Node_Id := Relocate_Node (Concval);
3925 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
3930 Make_Object_Declaration (Loc,
3931 Defining_Identifier => Obj,
3932 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
3933 Expression => ExpR);
3934 Set_Etype (Obj, Conctyp);
3935 Decls := New_List (Decl);
3936 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
3943 Parm1 := Concurrent_Ref (Concval);
3945 -- Second parameter is the entry index, computed by the routine
3946 -- provided for this purpose. The value of this expression is
3947 -- assigned to an intermediate variable to assure that any entry
3948 -- family index expressions are evaluated before the entry
3952 or else Restriction_Active (No_Entry_Queue) = False
3953 or else not Is_Protected_Type (Conctyp)
3954 or else Number_Entries (Conctyp) > 1
3955 or else (Has_Attach_Handler (Conctyp)
3956 and then not Restricted_Profile)
3958 X := Make_Defining_Identifier (Loc, Name_uX);
3961 Make_Object_Declaration (Loc,
3962 Defining_Identifier => X,
3963 Object_Definition =>
3964 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
3965 Expression => Actual_Index_Expression (
3966 Loc, Entity (Ename), Index, Concval));
3968 Append_To (Decls, Xdecl);
3969 Parm2 := New_Reference_To (X, Loc);
3976 -- The third parameter is the packaged parameters. If there are
3977 -- none, then it is just the null address, since nothing is passed.
3980 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
3983 -- Case of parameters present, where third argument is the address
3984 -- of a packaged record containing the required parameter values.
3987 -- First build a list of parameter values, which are references to
3988 -- objects of the parameter types.
3992 Actual := First_Actual (N);
3993 Formal := First_Formal (Ent);
3995 while Present (Actual) loop
3997 -- If it is a by_copy_type, copy it to a new variable. The
3998 -- packaged record has a field that points to this variable.
4000 if Is_By_Copy_Type (Etype (Actual)) then
4002 Make_Object_Declaration (Loc,
4003 Defining_Identifier => Make_Temporary (Loc, 'J'),
4004 Aliased_Present => True,
4005 Object_Definition =>
4006 New_Reference_To (Etype (Formal), Loc));
4008 -- Mark the object as not needing initialization since the
4009 -- initialization is performed separately, avoiding errors
4010 -- on cases such as formals of null-excluding access types.
4012 Set_No_Initialization (N_Node);
4014 -- We must make an assignment statement separate for the
4015 -- case of limited type. We cannot assign it unless the
4016 -- Assignment_OK flag is set first. An out formal of an
4017 -- access type must also be initialized from the actual,
4018 -- as stated in RM 6.4.1 (13).
4020 if Ekind (Formal) /= E_Out_Parameter
4021 or else Is_Access_Type (Etype (Formal))
4024 New_Reference_To (Defining_Identifier (N_Node), Loc);
4025 Set_Assignment_OK (N_Var);
4027 Make_Assignment_Statement (Loc,
4029 Expression => Relocate_Node (Actual)));
4032 Append (N_Node, Decls);
4035 Make_Attribute_Reference (Loc,
4036 Attribute_Name => Name_Unchecked_Access,
4038 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4040 -- If it is a VM_By_Copy_Actual, copy it to a new variable
4042 elsif Is_VM_By_Copy_Actual (Actual) then
4044 Make_Object_Declaration (Loc,
4045 Defining_Identifier => Make_Temporary (Loc, 'J'),
4046 Aliased_Present => True,
4047 Object_Definition =>
4048 New_Reference_To (Etype (Formal), Loc),
4049 Expression => New_Copy_Tree (Actual));
4050 Set_Assignment_OK (N_Node);
4052 Append (N_Node, Decls);
4055 Make_Attribute_Reference (Loc,
4056 Attribute_Name => Name_Unchecked_Access,
4058 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4061 -- Interface class-wide formal
4063 if Ada_Version >= Ada_2005
4064 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4065 and then Is_Interface (Etype (Formal))
4067 Iface_Typ := Etype (Etype (Formal));
4070 -- formal_iface_type! (actual.iface_tag)'reference
4073 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4074 pragma Assert (Present (Iface_Tag));
4077 Make_Reference (Loc,
4078 Unchecked_Convert_To (Iface_Typ,
4079 Make_Selected_Component (Loc,
4081 Relocate_Node (Actual),
4083 New_Reference_To (Iface_Tag, Loc)))));
4089 Make_Reference (Loc, Relocate_Node (Actual)));
4093 Next_Actual (Actual);
4094 Next_Formal_With_Extras (Formal);
4097 -- Now build the declaration of parameters initialized with the
4098 -- aggregate containing this constructed parameter list.
4100 P := Make_Defining_Identifier (Loc, Name_uP);
4103 Make_Object_Declaration (Loc,
4104 Defining_Identifier => P,
4105 Object_Definition =>
4106 New_Reference_To (Designated_Type (Ent_Acc), Loc),
4108 Make_Aggregate (Loc, Expressions => Plist));
4111 Make_Attribute_Reference (Loc,
4112 Prefix => New_Reference_To (P, Loc),
4113 Attribute_Name => Name_Address);
4115 Append (Pdecl, Decls);
4118 -- Now we can create the call, case of protected type
4120 if Is_Protected_Type (Conctyp) then
4121 case Corresponding_Runtime_Package (Conctyp) is
4122 when System_Tasking_Protected_Objects_Entries =>
4124 -- Change the type of the index declaration
4126 Set_Object_Definition (Xdecl,
4127 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
4129 -- Some additional declarations for protected entry calls
4135 -- Bnn : Communications_Block;
4137 Comm_Name := Make_Temporary (Loc, 'B');
4140 Make_Object_Declaration (Loc,
4141 Defining_Identifier => Comm_Name,
4142 Object_Definition =>
4143 New_Reference_To (RTE (RE_Communication_Block), Loc)));
4145 -- Some additional statements for protected entry calls
4147 -- Protected_Entry_Call (
4148 -- Object => po._object'Access,
4149 -- E => <entry index>;
4150 -- Uninterpreted_Data => P'Address;
4151 -- Mode => Simple_Call;
4155 Make_Procedure_Call_Statement (Loc,
4157 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
4159 Parameter_Associations => New_List (
4160 Make_Attribute_Reference (Loc,
4161 Attribute_Name => Name_Unchecked_Access,
4165 New_Reference_To (RTE (RE_Simple_Call), Loc),
4166 New_Occurrence_Of (Comm_Name, Loc)));
4168 when System_Tasking_Protected_Objects_Single_Entry =>
4169 -- Protected_Single_Entry_Call (
4170 -- Object => po._object'Access,
4171 -- Uninterpreted_Data => P'Address;
4172 -- Mode => Simple_Call);
4175 Make_Procedure_Call_Statement (Loc,
4176 Name => New_Reference_To (
4177 RTE (RE_Protected_Single_Entry_Call), Loc),
4179 Parameter_Associations => New_List (
4180 Make_Attribute_Reference (Loc,
4181 Attribute_Name => Name_Unchecked_Access,
4184 New_Reference_To (RTE (RE_Simple_Call), Loc)));
4187 raise Program_Error;
4190 -- Case of task type
4194 Make_Procedure_Call_Statement (Loc,
4195 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
4196 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4200 Append_To (Stats, Call);
4202 -- If there are out or in/out parameters by copy add assignment
4203 -- statements for the result values.
4205 if Present (Parms) then
4206 Actual := First_Actual (N);
4207 Formal := First_Formal (Ent);
4209 Set_Assignment_OK (Actual);
4210 while Present (Actual) loop
4211 if (Is_By_Copy_Type (Etype (Actual))
4212 or else Is_VM_By_Copy_Actual (Actual))
4213 and then Ekind (Formal) /= E_In_Parameter
4216 Make_Assignment_Statement (Loc,
4217 Name => New_Copy (Actual),
4219 Make_Explicit_Dereference (Loc,
4220 Make_Selected_Component (Loc,
4221 Prefix => New_Reference_To (P, Loc),
4223 Make_Identifier (Loc, Chars (Formal)))));
4225 -- In all cases (including limited private types) we want
4226 -- the assignment to be valid.
4228 Set_Assignment_OK (Name (N_Node));
4230 -- If the call is the triggering alternative in an
4231 -- asynchronous select, or the entry_call alternative of a
4232 -- conditional entry call, the assignments for in-out
4233 -- parameters are incorporated into the statement list that
4234 -- follows, so that there are executed only if the entry
4237 if (Nkind (Parent (N)) = N_Triggering_Alternative
4238 and then N = Triggering_Statement (Parent (N)))
4240 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4241 and then N = Entry_Call_Statement (Parent (N)))
4243 if No (Statements (Parent (N))) then
4244 Set_Statements (Parent (N), New_List);
4247 Prepend (N_Node, Statements (Parent (N)));
4250 Insert_After (Call, N_Node);
4254 Next_Actual (Actual);
4255 Next_Formal_With_Extras (Formal);
4259 -- Finally, create block and analyze it
4262 Make_Block_Statement (Loc,
4263 Declarations => Decls,
4264 Handled_Statement_Sequence =>
4265 Make_Handled_Sequence_Of_Statements (Loc,
4266 Statements => Stats)));
4270 end Build_Simple_Entry_Call;
4272 --------------------------------
4273 -- Build_Task_Activation_Call --
4274 --------------------------------
4276 procedure Build_Task_Activation_Call (N : Node_Id) is
4277 Loc : constant Source_Ptr := Sloc (N);
4284 -- Get the activation chain entity. Except in the case of a package
4285 -- body, this is in the node that was passed. For a package body, we
4286 -- have to find the corresponding package declaration node.
4288 if Nkind (N) = N_Package_Body then
4289 P := Corresponding_Spec (N);
4292 exit when Nkind (P) = N_Package_Declaration;
4295 Chain := Activation_Chain_Entity (P);
4298 Chain := Activation_Chain_Entity (N);
4301 if Present (Chain) then
4302 if Restricted_Profile then
4303 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
4305 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
4309 Make_Procedure_Call_Statement (Loc,
4311 Parameter_Associations =>
4312 New_List (Make_Attribute_Reference (Loc,
4313 Prefix => New_Occurrence_Of (Chain, Loc),
4314 Attribute_Name => Name_Unchecked_Access)));
4316 if Nkind (N) = N_Package_Declaration then
4317 if Present (Corresponding_Body (N)) then
4320 elsif Present (Private_Declarations (Specification (N))) then
4321 Append (Call, Private_Declarations (Specification (N)));
4324 Append (Call, Visible_Declarations (Specification (N)));
4328 if Present (Handled_Statement_Sequence (N)) then
4330 -- The call goes at the start of the statement sequence
4331 -- after the start of exception range label if one is present.
4337 Stm := First (Statements (Handled_Statement_Sequence (N)));
4339 -- A special case, skip exception range label if one is
4340 -- present (from front end zcx processing).
4342 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
4346 -- Another special case, if the first statement is a block
4347 -- from optimization of a local raise to a goto, then the
4348 -- call goes inside this block.
4350 if Nkind (Stm) = N_Block_Statement
4351 and then Exception_Junk (Stm)
4354 First (Statements (Handled_Statement_Sequence (Stm)));
4357 -- Insertion point is after any exception label pushes,
4358 -- since we want it covered by any local handlers.
4360 while Nkind (Stm) in N_Push_xxx_Label loop
4364 -- Now we have the proper insertion point
4366 Insert_Before (Stm, Call);
4370 Set_Handled_Statement_Sequence (N,
4371 Make_Handled_Sequence_Of_Statements (Loc,
4372 Statements => New_List (Call)));
4377 Check_Task_Activation (N);
4379 end Build_Task_Activation_Call;
4381 -------------------------------
4382 -- Build_Task_Allocate_Block --
4383 -------------------------------
4385 procedure Build_Task_Allocate_Block
4390 T : constant Entity_Id := Entity (Expression (N));
4391 Init : constant Entity_Id := Base_Init_Proc (T);
4392 Loc : constant Source_Ptr := Sloc (N);
4393 Chain : constant Entity_Id :=
4394 Make_Defining_Identifier (Loc, Name_uChain);
4395 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4400 Make_Block_Statement (Loc,
4401 Identifier => New_Reference_To (Blkent, Loc),
4402 Declarations => New_List (
4404 -- _Chain : Activation_Chain;
4406 Make_Object_Declaration (Loc,
4407 Defining_Identifier => Chain,
4408 Aliased_Present => True,
4409 Object_Definition =>
4410 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
4412 Handled_Statement_Sequence =>
4413 Make_Handled_Sequence_Of_Statements (Loc,
4415 Statements => New_List (
4419 Make_Procedure_Call_Statement (Loc,
4420 Name => New_Reference_To (Init, Loc),
4421 Parameter_Associations => Args),
4423 -- Activate_Tasks (_Chain);
4425 Make_Procedure_Call_Statement (Loc,
4426 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
4427 Parameter_Associations => New_List (
4428 Make_Attribute_Reference (Loc,
4429 Prefix => New_Reference_To (Chain, Loc),
4430 Attribute_Name => Name_Unchecked_Access))))),
4432 Has_Created_Identifier => True,
4433 Is_Task_Allocation_Block => True);
4436 Make_Implicit_Label_Declaration (Loc,
4437 Defining_Identifier => Blkent,
4438 Label_Construct => Block));
4440 Append_To (Actions, Block);
4442 Set_Activation_Chain_Entity (Block, Chain);
4443 end Build_Task_Allocate_Block;
4445 -----------------------------------------------
4446 -- Build_Task_Allocate_Block_With_Init_Stmts --
4447 -----------------------------------------------
4449 procedure Build_Task_Allocate_Block_With_Init_Stmts
4452 Init_Stmts : List_Id)
4454 Loc : constant Source_Ptr := Sloc (N);
4455 Chain : constant Entity_Id :=
4456 Make_Defining_Identifier (Loc, Name_uChain);
4457 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4461 Append_To (Init_Stmts,
4462 Make_Procedure_Call_Statement (Loc,
4463 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
4464 Parameter_Associations => New_List (
4465 Make_Attribute_Reference (Loc,
4466 Prefix => New_Reference_To (Chain, Loc),
4467 Attribute_Name => Name_Unchecked_Access))));
4470 Make_Block_Statement (Loc,
4471 Identifier => New_Reference_To (Blkent, Loc),
4472 Declarations => New_List (
4474 -- _Chain : Activation_Chain;
4476 Make_Object_Declaration (Loc,
4477 Defining_Identifier => Chain,
4478 Aliased_Present => True,
4479 Object_Definition =>
4480 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
4482 Handled_Statement_Sequence =>
4483 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
4485 Has_Created_Identifier => True,
4486 Is_Task_Allocation_Block => True);
4489 Make_Implicit_Label_Declaration (Loc,
4490 Defining_Identifier => Blkent,
4491 Label_Construct => Block));
4493 Append_To (Actions, Block);
4495 Set_Activation_Chain_Entity (Block, Chain);
4496 end Build_Task_Allocate_Block_With_Init_Stmts;
4498 -----------------------------------
4499 -- Build_Task_Proc_Specification --
4500 -----------------------------------
4502 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
4503 Loc : constant Source_Ptr := Sloc (T);
4504 Spec_Id : Entity_Id;
4507 -- Case of explicit task type, suffix TB
4509 if Comes_From_Source (T) then
4511 Make_Defining_Identifier (Loc,
4512 Chars => New_External_Name (Chars (T), "TB"));
4514 -- Case of anonymous task type, suffix B
4518 Make_Defining_Identifier (Loc,
4519 Chars => New_External_Name (Chars (T), 'B'));
4522 Set_Is_Internal (Spec_Id);
4524 -- Associate the procedure with the task, if this is the declaration
4525 -- (and not the body) of the procedure.
4527 if No (Task_Body_Procedure (T)) then
4528 Set_Task_Body_Procedure (T, Spec_Id);
4532 Make_Procedure_Specification (Loc,
4533 Defining_Unit_Name => Spec_Id,
4534 Parameter_Specifications => New_List (
4535 Make_Parameter_Specification (Loc,
4536 Defining_Identifier =>
4537 Make_Defining_Identifier (Loc, Name_uTask),
4539 Make_Access_Definition (Loc,
4541 New_Reference_To (Corresponding_Record_Type (T), Loc)))));
4542 end Build_Task_Proc_Specification;
4544 ---------------------------------------
4545 -- Build_Unprotected_Subprogram_Body --
4546 ---------------------------------------
4548 function Build_Unprotected_Subprogram_Body
4550 Pid : Node_Id) return Node_Id
4552 Decls : constant List_Id := Declarations (N);
4555 -- Add renamings for the Protection object, discriminals, privals and
4556 -- the entry index constant for use by debugger.
4558 Debug_Private_Data_Declarations (Decls);
4560 -- Make an unprotected version of the subprogram for use within the same
4561 -- object, with a new name and an additional parameter representing the
4565 Make_Subprogram_Body (Sloc (N),
4567 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
4568 Declarations => Decls,
4569 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
4570 end Build_Unprotected_Subprogram_Body;
4572 ----------------------------
4573 -- Collect_Entry_Families --
4574 ----------------------------
4576 procedure Collect_Entry_Families
4579 Current_Node : in out Node_Id;
4580 Conctyp : Entity_Id)
4583 Efam_Decl : Node_Id;
4584 Efam_Type : Entity_Id;
4587 Efam := First_Entity (Conctyp);
4588 while Present (Efam) loop
4589 if Ekind (Efam) = E_Entry_Family then
4590 Efam_Type := Make_Temporary (Loc, 'F');
4595 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
4597 Bas_Decl : Node_Id := Empty;
4602 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
4604 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
4605 Bas := Make_Temporary (Loc, 'B');
4608 Make_Subtype_Declaration (Loc,
4609 Defining_Identifier => Bas,
4610 Subtype_Indication =>
4611 Make_Subtype_Indication (Loc,
4613 New_Occurrence_Of (Standard_Integer, Loc),
4615 Make_Range_Constraint (Loc,
4616 Range_Expression => Make_Range (Loc,
4617 Make_Integer_Literal
4618 (Loc, -Entry_Family_Bound),
4619 Make_Integer_Literal
4620 (Loc, Entry_Family_Bound - 1)))));
4622 Insert_After (Current_Node, Bas_Decl);
4623 Current_Node := Bas_Decl;
4628 Make_Full_Type_Declaration (Loc,
4629 Defining_Identifier => Efam_Type,
4631 Make_Unconstrained_Array_Definition (Loc,
4633 (New_List (New_Occurrence_Of (Bas, Loc))),
4635 Component_Definition =>
4636 Make_Component_Definition (Loc,
4637 Aliased_Present => False,
4638 Subtype_Indication =>
4639 New_Reference_To (Standard_Character, Loc))));
4642 Insert_After (Current_Node, Efam_Decl);
4643 Current_Node := Efam_Decl;
4644 Analyze (Efam_Decl);
4647 Make_Component_Declaration (Loc,
4648 Defining_Identifier =>
4649 Make_Defining_Identifier (Loc, Chars (Efam)),
4651 Component_Definition =>
4652 Make_Component_Definition (Loc,
4653 Aliased_Present => False,
4654 Subtype_Indication =>
4655 Make_Subtype_Indication (Loc,
4657 New_Occurrence_Of (Efam_Type, Loc),
4660 Make_Index_Or_Discriminant_Constraint (Loc,
4661 Constraints => New_List (
4663 (Etype (Discrete_Subtype_Definition
4664 (Parent (Efam))), Loc)))))));
4670 end Collect_Entry_Families;
4672 -----------------------
4673 -- Concurrent_Object --
4674 -----------------------
4676 function Concurrent_Object
4677 (Spec_Id : Entity_Id;
4678 Conc_Typ : Entity_Id) return Entity_Id
4681 -- Parameter _O or _object
4683 if Is_Protected_Type (Conc_Typ) then
4684 return First_Formal (Protected_Body_Subprogram (Spec_Id));
4689 pragma Assert (Is_Task_Type (Conc_Typ));
4690 return First_Formal (Task_Body_Procedure (Conc_Typ));
4692 end Concurrent_Object;
4694 ----------------------
4695 -- Copy_Result_Type --
4696 ----------------------
4698 function Copy_Result_Type (Res : Node_Id) return Node_Id is
4699 New_Res : constant Node_Id := New_Copy_Tree (Res);
4704 -- If the result type is an access_to_subprogram, we must create
4705 -- new entities for its spec.
4707 if Nkind (New_Res) = N_Access_Definition
4708 and then Present (Access_To_Subprogram_Definition (New_Res))
4710 -- Provide new entities for the formals
4712 Par_Spec := First (Parameter_Specifications
4713 (Access_To_Subprogram_Definition (New_Res)));
4714 while Present (Par_Spec) loop
4715 Formal := Defining_Identifier (Par_Spec);
4716 Set_Defining_Identifier (Par_Spec,
4717 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
4723 end Copy_Result_Type;
4725 --------------------
4726 -- Concurrent_Ref --
4727 --------------------
4729 -- The expression returned for a reference to a concurrent object has the
4732 -- taskV!(name)._Task_Id
4736 -- objectV!(name)._Object
4738 -- for a protected object. For the case of an access to a concurrent
4739 -- object, there is an extra explicit dereference:
4741 -- taskV!(name.all)._Task_Id
4742 -- objectV!(name.all)._Object
4744 -- here taskV and objectV are the types for the associated records, which
4745 -- contain the required _Task_Id and _Object fields for tasks and protected
4746 -- objects, respectively.
4748 -- For the case of a task type name, the expression is
4752 -- i.e. a call to the Self function which returns precisely this Task_Id
4754 -- For the case of a protected type name, the expression is
4758 -- which is a renaming of the _object field of the current object
4759 -- record, passed into protected operations as a parameter.
4761 function Concurrent_Ref (N : Node_Id) return Node_Id is
4762 Loc : constant Source_Ptr := Sloc (N);
4763 Ntyp : constant Entity_Id := Etype (N);
4767 function Is_Current_Task (T : Entity_Id) return Boolean;
4768 -- Check whether the reference is to the immediately enclosing task
4769 -- type, or to an outer one (rare but legal).
4771 ---------------------
4772 -- Is_Current_Task --
4773 ---------------------
4775 function Is_Current_Task (T : Entity_Id) return Boolean is
4779 Scop := Current_Scope;
4780 while Present (Scop)
4781 and then Scop /= Standard_Standard
4787 elsif Is_Task_Type (Scop) then
4790 -- If this is a procedure nested within the task type, we must
4791 -- assume that it can be called from an inner task, and therefore
4792 -- cannot treat it as a local reference.
4794 elsif Is_Overloadable (Scop)
4795 and then In_Open_Scopes (T)
4800 Scop := Scope (Scop);
4804 -- We know that we are within the task body, so should have found it
4807 raise Program_Error;
4808 end Is_Current_Task;
4810 -- Start of processing for Concurrent_Ref
4813 if Is_Access_Type (Ntyp) then
4814 Dtyp := Designated_Type (Ntyp);
4816 if Is_Protected_Type (Dtyp) then
4817 Sel := Name_uObject;
4819 Sel := Name_uTask_Id;
4823 Make_Selected_Component (Loc,
4825 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
4826 Make_Explicit_Dereference (Loc, N)),
4827 Selector_Name => Make_Identifier (Loc, Sel));
4829 elsif Is_Entity_Name (N)
4830 and then Is_Concurrent_Type (Entity (N))
4832 if Is_Task_Type (Entity (N)) then
4834 if Is_Current_Task (Entity (N)) then
4836 Make_Function_Call (Loc,
4837 Name => New_Reference_To (RTE (RE_Self), Loc));
4842 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
4843 T_Body : constant Node_Id :=
4844 Parent (Corresponding_Body (Parent (Entity (N))));
4848 Make_Object_Declaration (Loc,
4849 Defining_Identifier => T_Self,
4850 Object_Definition =>
4851 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
4853 Make_Function_Call (Loc,
4854 Name => New_Reference_To (RTE (RE_Self), Loc)));
4855 Prepend (Decl, Declarations (T_Body));
4857 Set_Scope (T_Self, Entity (N));
4858 return New_Occurrence_Of (T_Self, Loc);
4863 pragma Assert (Is_Protected_Type (Entity (N)));
4866 New_Reference_To (Find_Protection_Object (Current_Scope), Loc);
4870 if Is_Protected_Type (Ntyp) then
4871 Sel := Name_uObject;
4873 elsif Is_Task_Type (Ntyp) then
4874 Sel := Name_uTask_Id;
4877 raise Program_Error;
4881 Make_Selected_Component (Loc,
4883 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
4885 Selector_Name => Make_Identifier (Loc, Sel));
4889 ------------------------
4890 -- Convert_Concurrent --
4891 ------------------------
4893 function Convert_Concurrent
4895 Typ : Entity_Id) return Node_Id
4898 if not Is_Concurrent_Type (Typ) then
4902 Unchecked_Convert_To
4903 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
4905 end Convert_Concurrent;
4907 -------------------------------------
4908 -- Debug_Private_Data_Declarations --
4909 -------------------------------------
4911 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
4912 Debug_Nod : Node_Id;
4916 Decl := First (Decls);
4917 while Present (Decl)
4918 and then not Comes_From_Source (Decl)
4920 -- Declaration for concurrent entity _object and its access type,
4921 -- along with the entry index subtype:
4922 -- type prot_typVP is access prot_typV;
4923 -- _object : prot_typVP := prot_typV (_O);
4924 -- subtype Jnn is <Type of Index> range Low .. High;
4926 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
4927 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4929 -- Declaration for the Protection object, discriminals, privals and
4930 -- entry index constant:
4931 -- conc_typR : protection_typ renames _object._object;
4932 -- discr_nameD : discr_typ renames _object.discr_name;
4933 -- discr_nameD : discr_typ renames _task.discr_name;
4934 -- prival_name : comp_typ renames _object.comp_name;
4935 -- J : constant Jnn :=
4936 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
4938 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
4939 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4940 Debug_Nod := Debug_Renaming_Declaration (Decl);
4942 if Present (Debug_Nod) then
4943 Insert_After (Decl, Debug_Nod);
4949 end Debug_Private_Data_Declarations;
4951 ----------------------------
4952 -- Entry_Index_Expression --
4953 ----------------------------
4955 function Entry_Index_Expression
4959 Ttyp : Entity_Id) return Node_Id
4969 -- The queues of entries and entry families appear in textual order in
4970 -- the associated record. The entry index is computed as the sum of the
4971 -- number of queues for all entries that precede the designated one, to
4972 -- which is added the index expression, if this expression denotes a
4973 -- member of a family.
4975 -- The following is a place holder for the count of simple entries
4977 Num := Make_Integer_Literal (Sloc, 1);
4979 -- We construct an expression which is a series of addition operations.
4980 -- The first operand is the number of single entries that precede this
4981 -- one, the second operand is the index value relative to the start of
4982 -- the referenced family, and the remaining operands are the lengths of
4983 -- the entry families that precede this entry, i.e. the constructed
4986 -- number_simple_entries +
4987 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
4988 -- family'length + ...
4990 -- where index-value is the given index value, and s is the index
4991 -- subtype (we have to use pos because the subtype might be an
4992 -- enumeration type preventing direct subtraction). Note that the task
4993 -- entry array is one-indexed.
4995 -- The upper bound of the entry family may be a discriminant, so we
4996 -- retrieve the lower bound explicitly to compute offset, rather than
4997 -- using the index subtype which may mention a discriminant.
4999 if Present (Index) then
5000 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5009 Make_Attribute_Reference (Sloc,
5010 Attribute_Name => Name_Pos,
5011 Prefix => New_Reference_To (Base_Type (S), Sloc),
5012 Expressions => New_List (Relocate_Node (Index))),
5020 -- Now add lengths of preceding entries and entry families
5022 Prev := First_Entity (Ttyp);
5024 while Chars (Prev) /= Chars (Ent)
5025 or else (Ekind (Prev) /= Ekind (Ent))
5026 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5028 if Ekind (Prev) = E_Entry then
5029 Set_Intval (Num, Intval (Num) + 1);
5031 elsif Ekind (Prev) = E_Entry_Family then
5033 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5034 Lo := Type_Low_Bound (S);
5035 Hi := Type_High_Bound (S);
5040 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5042 -- Other components are anonymous types to be ignored
5052 end Entry_Index_Expression;
5054 ---------------------------
5055 -- Establish_Task_Master --
5056 ---------------------------
5058 procedure Establish_Task_Master (N : Node_Id) is
5061 if Restriction_Active (No_Task_Hierarchy) = False then
5062 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5063 Prepend_To (Declarations (N), Call);
5066 end Establish_Task_Master;
5068 --------------------------------
5069 -- Expand_Accept_Declarations --
5070 --------------------------------
5072 -- Part of the expansion of an accept statement involves the creation of
5073 -- a declaration that can be referenced from the statement sequence of
5078 -- This declaration is inserted immediately before the accept statement
5079 -- and it is important that it be inserted before the statements of the
5080 -- statement sequence are analyzed. Thus it would be too late to create
5081 -- this declaration in the Expand_N_Accept_Statement routine, which is
5082 -- why there is a separate procedure to be called directly from Sem_Ch9.
5084 -- Ann is used to hold the address of the record containing the parameters
5085 -- (see Expand_N_Entry_Call for more details on how this record is built).
5086 -- References to the parameters do an unchecked conversion of this address
5087 -- to a pointer to the required record type, and then access the field that
5088 -- holds the value of the required parameter. The entity for the address
5089 -- variable is held as the top stack element (i.e. the last element) of the
5090 -- Accept_Address stack in the corresponding entry entity, and this element
5091 -- must be set in place before the statements are processed.
5093 -- The above description applies to the case of a stand alone accept
5094 -- statement, i.e. one not appearing as part of a select alternative.
5096 -- For the case of an accept that appears as part of a select alternative
5097 -- of a selective accept, we must still create the declaration right away,
5098 -- since Ann is needed immediately, but there is an important difference:
5100 -- The declaration is inserted before the selective accept, not before
5101 -- the accept statement (which is not part of a list anyway, and so would
5102 -- not accommodate inserted declarations)
5104 -- We only need one address variable for the entire selective accept. So
5105 -- the Ann declaration is created only for the first accept alternative,
5106 -- and subsequent accept alternatives reference the same Ann variable.
5108 -- We can distinguish the two cases by seeing whether the accept statement
5109 -- is part of a list. If not, then it must be in an accept alternative.
5111 -- To expand the requeue statement, a label is provided at the end of the
5112 -- accept statement or alternative of which it is a part, so that the
5113 -- statement can be skipped after the requeue is complete. This label is
5114 -- created here rather than during the expansion of the accept statement,
5115 -- because it will be needed by any requeue statements within the accept,
5116 -- which are expanded before the accept.
5118 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5119 Loc : constant Source_Ptr := Sloc (N);
5120 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5121 Ann : Entity_Id := Empty;
5129 if Full_Expander_Active then
5131 -- If we have no handled statement sequence, we may need to build
5132 -- a dummy sequence consisting of a null statement. This can be
5133 -- skipped if the trivial accept optimization is permitted.
5135 if not Trivial_Accept_OK
5137 (No (Stats) or else Null_Statements (Statements (Stats)))
5139 Set_Handled_Statement_Sequence (N,
5140 Make_Handled_Sequence_Of_Statements (Loc,
5141 Statements => New_List (Make_Null_Statement (Loc))));
5144 -- Create and declare two labels to be placed at the end of the
5145 -- accept statement. The first label is used to allow requeues to
5146 -- skip the remainder of entry processing. The second label is used
5147 -- to skip the remainder of entry processing if the rendezvous
5148 -- completes in the middle of the accept body.
5150 if Present (Handled_Statement_Sequence (N)) then
5155 Ent := Make_Temporary (Loc, 'L');
5156 Lab_Id := New_Reference_To (Ent, Loc);
5157 Lab := Make_Label (Loc, Lab_Id);
5159 Make_Implicit_Label_Declaration (Loc,
5160 Defining_Identifier => Ent,
5161 Label_Construct => Lab);
5162 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5164 Ent := Make_Temporary (Loc, 'L');
5165 Lab_Id := New_Reference_To (Ent, Loc);
5166 Lab := Make_Label (Loc, Lab_Id);
5168 Make_Implicit_Label_Declaration (Loc,
5169 Defining_Identifier => Ent,
5170 Label_Construct => Lab);
5171 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5179 -- Case of stand alone accept statement
5181 if Is_List_Member (N) then
5183 if Present (Handled_Statement_Sequence (N)) then
5184 Ann := Make_Temporary (Loc, 'A');
5187 Make_Object_Declaration (Loc,
5188 Defining_Identifier => Ann,
5189 Object_Definition =>
5190 New_Reference_To (RTE (RE_Address), Loc));
5192 Insert_Before (N, Adecl);
5195 Insert_Before (N, Ldecl);
5198 Insert_Before (N, Ldecl2);
5202 -- Case of accept statement which is in an accept alternative
5206 Acc_Alt : constant Node_Id := Parent (N);
5207 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5211 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5212 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5214 -- ??? Consider a single label for select statements
5216 if Present (Handled_Statement_Sequence (N)) then
5218 Statements (Handled_Statement_Sequence (N)));
5222 Statements (Handled_Statement_Sequence (N)));
5226 -- Find first accept alternative of the selective accept. A
5227 -- valid selective accept must have at least one accept in it.
5229 Alt := First (Select_Alternatives (Sel_Acc));
5231 while Nkind (Alt) /= N_Accept_Alternative loop
5235 -- If we are the first accept statement, then we have to create
5236 -- the Ann variable, as for the stand alone case, except that
5237 -- it is inserted before the selective accept. Similarly, a
5238 -- label for requeue expansion must be declared.
5240 if N = Accept_Statement (Alt) then
5241 Ann := Make_Temporary (Loc, 'A');
5243 Make_Object_Declaration (Loc,
5244 Defining_Identifier => Ann,
5245 Object_Definition =>
5246 New_Reference_To (RTE (RE_Address), Loc));
5248 Insert_Before (Sel_Acc, Adecl);
5251 -- If we are not the first accept statement, then find the Ann
5252 -- variable allocated by the first accept and use it.
5256 Node (Last_Elmt (Accept_Address
5257 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5262 -- Merge here with Ann either created or referenced, and Adecl
5263 -- pointing to the corresponding declaration. Remaining processing
5264 -- is the same for the two cases.
5266 if Present (Ann) then
5267 Append_Elmt (Ann, Accept_Address (Ent));
5268 Set_Debug_Info_Needed (Ann);
5271 -- Create renaming declarations for the entry formals. Each reference
5272 -- to a formal becomes a dereference of a component of the parameter
5273 -- block, whose address is held in Ann. These declarations are
5274 -- eventually inserted into the accept block, and analyzed there so
5275 -- that they have the proper scope for gdb and do not conflict with
5276 -- other declarations.
5278 if Present (Parameter_Specifications (N))
5279 and then Present (Handled_Statement_Sequence (N))
5286 Renamed_Formal : Node_Id;
5290 Formal := First_Formal (Ent);
5292 while Present (Formal) loop
5293 Comp := Entry_Component (Formal);
5295 Make_Defining_Identifier (Loc, Chars (Formal));
5297 Set_Etype (New_F, Etype (Formal));
5298 Set_Scope (New_F, Ent);
5300 -- Now we set debug info needed on New_F even though it does
5301 -- not come from source, so that the debugger will get the
5302 -- right information for these generated names.
5304 Set_Debug_Info_Needed (New_F);
5306 if Ekind (Formal) = E_In_Parameter then
5307 Set_Ekind (New_F, E_Constant);
5309 Set_Ekind (New_F, E_Variable);
5310 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5313 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5316 Make_Selected_Component (Loc,
5318 Unchecked_Convert_To (
5319 Entry_Parameters_Type (Ent),
5320 New_Reference_To (Ann, Loc)),
5322 New_Reference_To (Comp, Loc));
5325 Build_Renamed_Formal_Declaration
5326 (New_F, Formal, Comp, Renamed_Formal);
5328 if No (Declarations (N)) then
5329 Set_Declarations (N, New_List);
5332 Append (Decl, Declarations (N));
5333 Set_Renamed_Object (Formal, New_F);
5334 Next_Formal (Formal);
5341 end Expand_Accept_Declarations;
5343 ---------------------------------------------
5344 -- Expand_Access_Protected_Subprogram_Type --
5345 ---------------------------------------------
5347 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5348 Loc : constant Source_Ptr := Sloc (N);
5350 T : constant Entity_Id := Defining_Identifier (N);
5351 D_T : constant Entity_Id := Designated_Type (T);
5352 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
5353 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
5354 P_List : constant List_Id := Build_Protected_Spec
5355 (N, RTE (RE_Address), D_T, False);
5361 -- Create access to subprogram with full signature
5363 if Etype (D_T) /= Standard_Void_Type then
5365 Make_Access_Function_Definition (Loc,
5366 Parameter_Specifications => P_List,
5367 Result_Definition =>
5368 Copy_Result_Type (Result_Definition (Type_Definition (N))));
5372 Make_Access_Procedure_Definition (Loc,
5373 Parameter_Specifications => P_List);
5377 Make_Full_Type_Declaration (Loc,
5378 Defining_Identifier => D_T2,
5379 Type_Definition => Def1);
5381 Insert_After (N, Decl1);
5384 -- Associate the access to subprogram with its original access to
5385 -- protected subprogram type. Needed by the backend to know that this
5386 -- type corresponds with an access to protected subprogram type.
5388 Set_Original_Access_Type (D_T2, T);
5390 -- Create Equivalent_Type, a record with two components for an access to
5391 -- object and an access to subprogram.
5394 Make_Component_Declaration (Loc,
5395 Defining_Identifier => Make_Temporary (Loc, 'P'),
5396 Component_Definition =>
5397 Make_Component_Definition (Loc,
5398 Aliased_Present => False,
5399 Subtype_Indication =>
5400 New_Occurrence_Of (RTE (RE_Address), Loc))),
5402 Make_Component_Declaration (Loc,
5403 Defining_Identifier => Make_Temporary (Loc, 'S'),
5404 Component_Definition =>
5405 Make_Component_Definition (Loc,
5406 Aliased_Present => False,
5407 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
5410 Make_Full_Type_Declaration (Loc,
5411 Defining_Identifier => E_T,
5413 Make_Record_Definition (Loc,
5415 Make_Component_List (Loc, Component_Items => Comps)));
5417 Insert_After (Decl1, Decl2);
5419 Set_Equivalent_Type (T, E_T);
5420 end Expand_Access_Protected_Subprogram_Type;
5422 --------------------------
5423 -- Expand_Entry_Barrier --
5424 --------------------------
5426 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
5427 Cond : constant Node_Id :=
5428 Condition (Entry_Body_Formal_Part (N));
5429 Prot : constant Entity_Id := Scope (Ent);
5430 Spec_Decl : constant Node_Id := Parent (Prot);
5433 Body_Decl : Node_Id;
5436 if No_Run_Time_Mode then
5437 Error_Msg_CRT ("entry barrier", N);
5441 -- The body of the entry barrier must be analyzed in the context of the
5442 -- protected object, but its scope is external to it, just as any other
5443 -- unprotected version of a protected operation. The specification has
5444 -- been produced when the protected type declaration was elaborated. We
5445 -- build the body, insert it in the enclosing scope, but analyze it in
5446 -- the current context. A more uniform approach would be to treat the
5447 -- barrier just as a protected function, and discard the protected
5448 -- version of it because it is never called.
5450 if Full_Expander_Active then
5451 B_F := Build_Barrier_Function (N, Ent, Prot);
5452 Func := Barrier_Function (Ent);
5453 Set_Corresponding_Spec (B_F, Func);
5455 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
5457 if Nkind (Parent (Body_Decl)) = N_Subunit then
5458 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
5461 Insert_Before_And_Analyze (Body_Decl, B_F);
5463 Set_Discriminals (Spec_Decl);
5464 Set_Scope (Func, Scope (Prot));
5467 Analyze_And_Resolve (Cond, Any_Boolean);
5470 -- The Ravenscar profile restricts barriers to simple variables declared
5471 -- within the protected object. We also allow Boolean constants, since
5472 -- these appear in several published examples and are also allowed by
5473 -- the Aonix compiler.
5475 -- Note that after analysis variables in this context will be replaced
5476 -- by the corresponding prival, that is to say a renaming of a selected
5477 -- component of the form _Object.Var. If expansion is disabled, as
5478 -- within a generic, we check that the entity appears in the current
5481 if Is_Entity_Name (Cond) then
5483 -- A small optimization of useless renamings. If the scope of the
5484 -- entity of the condition is not the barrier function, then the
5485 -- condition does not reference any of the generated renamings
5486 -- within the function.
5488 if Full_Expander_Active
5489 and then Scope (Entity (Cond)) /= Func
5491 Set_Declarations (B_F, Empty_List);
5494 if Entity (Cond) = Standard_False
5496 Entity (Cond) = Standard_True
5500 elsif not Expander_Active
5501 and then Scope (Entity (Cond)) = Current_Scope
5505 -- Check for case of _object.all.field (note that the explicit
5506 -- dereference gets inserted by analyze/expand of _object.field)
5508 elsif Present (Renamed_Object (Entity (Cond)))
5510 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
5514 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
5520 -- It is not a boolean variable or literal, so check the restriction
5522 Check_Restriction (Simple_Barriers, Cond);
5523 end Expand_Entry_Barrier;
5525 ------------------------------
5526 -- Expand_N_Abort_Statement --
5527 ------------------------------
5529 -- Expand abort T1, T2, .. Tn; into:
5530 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
5532 procedure Expand_N_Abort_Statement (N : Node_Id) is
5533 Loc : constant Source_Ptr := Sloc (N);
5534 Tlist : constant List_Id := Names (N);
5540 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
5543 Tasknm := First (Tlist);
5545 while Present (Tasknm) loop
5548 -- A task interface class-wide type object is being aborted.
5549 -- Retrieve its _task_id by calling a dispatching routine.
5551 if Ada_Version >= Ada_2005
5552 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
5553 and then Is_Interface (Etype (Tasknm))
5554 and then Is_Task_Interface (Etype (Tasknm))
5556 Append_To (Component_Associations (Aggr),
5557 Make_Component_Association (Loc,
5558 Choices => New_List (
5559 Make_Integer_Literal (Loc, Count)),
5562 -- Task_Id (Tasknm._disp_get_task_id)
5564 Make_Unchecked_Type_Conversion (Loc,
5566 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
5568 Make_Selected_Component (Loc,
5569 Prefix => New_Copy_Tree (Tasknm),
5571 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
5574 Append_To (Component_Associations (Aggr),
5575 Make_Component_Association (Loc,
5576 Choices => New_List (
5577 Make_Integer_Literal (Loc, Count)),
5578 Expression => Concurrent_Ref (Tasknm)));
5585 Make_Procedure_Call_Statement (Loc,
5586 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
5587 Parameter_Associations => New_List (
5588 Make_Qualified_Expression (Loc,
5589 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
5590 Expression => Aggr))));
5593 end Expand_N_Abort_Statement;
5595 -------------------------------
5596 -- Expand_N_Accept_Statement --
5597 -------------------------------
5599 -- This procedure handles expansion of accept statements that stand
5600 -- alone, i.e. they are not part of an accept alternative. The expansion
5601 -- of accept statement in accept alternatives is handled by the routines
5602 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
5603 -- following description applies only to stand alone accept statements.
5605 -- If there is no handled statement sequence, or only null statements,
5606 -- then this is called a trivial accept, and the expansion is:
5608 -- Accept_Trivial (entry-index)
5610 -- If there is a handled statement sequence, then the expansion is:
5617 -- Accept_Call (entry-index, Ann);
5618 -- Renaming_Declarations for formals
5619 -- <statement sequence from N_Accept_Statement node>
5620 -- Complete_Rendezvous;
5625 -- <exception handler from N_Accept_Statement node>
5626 -- Complete_Rendezvous;
5628 -- <exception handler from N_Accept_Statement node>
5629 -- Complete_Rendezvous;
5634 -- when all others =>
5635 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
5638 -- The first three declarations were already inserted ahead of the accept
5639 -- statement by the Expand_Accept_Declarations procedure, which was called
5640 -- directly from the semantics during analysis of the accept statement,
5641 -- before analyzing its contained statements.
5643 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
5644 -- from possible expansion activity (the original source of course does
5645 -- not have any declarations associated with the accept statement, since
5646 -- an accept statement has no declarative part). In particular, if the
5647 -- expander is active, the first such declaration is the declaration of
5648 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
5650 -- The two blocks are merged into a single block if the inner block has
5651 -- no exception handlers, but otherwise two blocks are required, since
5652 -- exceptions might be raised in the exception handlers of the inner
5653 -- block, and Exceptional_Complete_Rendezvous must be called.
5655 procedure Expand_N_Accept_Statement (N : Node_Id) is
5656 Loc : constant Source_Ptr := Sloc (N);
5657 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5658 Ename : constant Node_Id := Entry_Direct_Name (N);
5659 Eindx : constant Node_Id := Entry_Index (N);
5660 Eent : constant Entity_Id := Entity (Ename);
5661 Acstack : constant Elist_Id := Accept_Address (Eent);
5662 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
5663 Ttyp : constant Entity_Id := Etype (Scope (Eent));
5668 -- Start of processing for Expand_N_Accept_Statement
5671 -- If accept statement is not part of a list, then its parent must be
5672 -- an accept alternative, and, as described above, we do not do any
5673 -- expansion for such accept statements at this level.
5675 if not Is_List_Member (N) then
5676 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
5679 -- Trivial accept case (no statement sequence, or null statements).
5680 -- If the accept statement has declarations, then just insert them
5681 -- before the procedure call.
5683 elsif Trivial_Accept_OK
5684 and then (No (Stats) or else Null_Statements (Statements (Stats)))
5686 -- Remove declarations for renamings, because the parameter block
5687 -- will not be assigned.
5694 D := First (Declarations (N));
5696 while Present (D) loop
5698 if Nkind (D) = N_Object_Renaming_Declaration then
5706 if Present (Declarations (N)) then
5707 Insert_Actions (N, Declarations (N));
5711 Make_Procedure_Call_Statement (Loc,
5712 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
5713 Parameter_Associations => New_List (
5714 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
5718 -- Discard Entry_Address that was created for it, so it will not be
5719 -- emitted if this accept statement is in the statement part of a
5720 -- delay alternative.
5722 if Present (Stats) then
5723 Remove_Last_Elmt (Acstack);
5726 -- Case of statement sequence present
5729 -- Construct the block, using the declarations from the accept
5730 -- statement if any to initialize the declarations of the block.
5732 Blkent := Make_Temporary (Loc, 'A');
5733 Set_Ekind (Blkent, E_Block);
5734 Set_Etype (Blkent, Standard_Void_Type);
5735 Set_Scope (Blkent, Current_Scope);
5738 Make_Block_Statement (Loc,
5739 Identifier => New_Reference_To (Blkent, Loc),
5740 Declarations => Declarations (N),
5741 Handled_Statement_Sequence => Build_Accept_Body (N));
5743 -- For the analysis of the generated declarations, the parent node
5744 -- must be properly set.
5746 Set_Parent (Block, Parent (N));
5748 -- Prepend call to Accept_Call to main statement sequence If the
5749 -- accept has exception handlers, the statement sequence is wrapped
5750 -- in a block. Insert call and renaming declarations in the
5751 -- declarations of the block, so they are elaborated before the
5755 Make_Procedure_Call_Statement (Loc,
5756 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
5757 Parameter_Associations => New_List (
5758 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
5759 New_Reference_To (Ann, Loc)));
5761 if Parent (Stats) = N then
5762 Prepend (Call, Statements (Stats));
5771 Push_Scope (Blkent);
5779 D := First (Declarations (N));
5780 while Present (D) loop
5783 if Nkind (D) = N_Object_Renaming_Declaration then
5785 -- The renaming declarations for the formals were created
5786 -- during analysis of the accept statement, and attached to
5787 -- the list of declarations. Place them now in the context
5788 -- of the accept block or subprogram.
5791 Typ := Entity (Subtype_Mark (D));
5792 Insert_After (Call, D);
5795 -- If the formal is class_wide, it does not have an actual
5796 -- subtype. The analysis of the renaming declaration creates
5797 -- one, but we need to retain the class-wide nature of the
5800 if Is_Class_Wide_Type (Typ) then
5801 Set_Etype (Defining_Identifier (D), Typ);
5812 -- Replace the accept statement by the new block
5817 -- Last step is to unstack the Accept_Address value
5819 Remove_Last_Elmt (Acstack);
5821 end Expand_N_Accept_Statement;
5823 ----------------------------------
5824 -- Expand_N_Asynchronous_Select --
5825 ----------------------------------
5827 -- This procedure assumes that the trigger statement is an entry call or
5828 -- a dispatching procedure call. A delay alternative should already have
5829 -- been expanded into an entry call to the appropriate delay object Wait
5832 -- If the trigger is a task entry call, the select is implemented with
5833 -- a Task_Entry_Call:
5838 -- P : parms := (parm, parm, parm);
5840 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
5842 -- procedure _clean is
5845 -- Cancel_Task_Entry_Call (C);
5852 -- (<acceptor-task>, -- Acceptor
5853 -- <entry-index>, -- E
5854 -- P'Address, -- Uninterpreted_Data
5855 -- Asynchronous_Call, -- Mode
5856 -- B); -- Rendezvous_Successful
5863 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5866 -- when Abort_Signal => Abort_Undefer;
5873 -- <triggered-statements>
5877 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
5878 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
5882 -- P : parms := (parm, parm, parm);
5884 -- Call_Simple (acceptor-task, entry-index, P'Address);
5890 -- so the task at hand is to convert the latter expansion into the former
5892 -- If the trigger is a protected entry call, the select is implemented
5893 -- with Protected_Entry_Call:
5896 -- P : E1_Params := (param, param, param);
5897 -- Bnn : Communications_Block;
5902 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
5904 -- procedure _clean is
5907 -- if Enqueued (Bnn) then
5908 -- Cancel_Protected_Entry_Call (Bnn);
5915 -- Protected_Entry_Call
5916 -- (po._object'Access, -- Object
5917 -- <entry index>, -- E
5918 -- P'Address, -- Uninterpreted_Data
5919 -- Asynchronous_Call, -- Mode
5922 -- if Enqueued (Bnn) then
5926 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
5929 -- when Abort_Signal => Abort_Undefer;
5932 -- if not Cancelled (Bnn) then
5933 -- <triggered-statements>
5937 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
5941 -- P : E1_Params := (param, param, param);
5942 -- Bnn : Communications_Block;
5945 -- Protected_Entry_Call
5946 -- (po._object'Access, -- Object
5947 -- <entry index>, -- E
5948 -- P'Address, -- Uninterpreted_Data
5949 -- Simple_Call, -- Mode
5956 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
5960 -- B : Boolean := False;
5961 -- Bnn : Communication_Block;
5962 -- C : Ada.Tags.Prim_Op_Kind;
5963 -- D : System.Storage_Elements.Dummy_Communication_Block;
5964 -- K : Ada.Tags.Tagged_Kind :=
5965 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
5966 -- P : Parameters := (Param1 .. ParamN);
5971 -- if K = Ada.Tags.TK_Limited_Tagged then
5972 -- <dispatching-call>;
5973 -- <triggering-statements>;
5977 -- Ada.Tags.Get_Offset_Index
5978 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
5980 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
5982 -- if C = POK_Protected_Entry then
5984 -- procedure _clean is
5986 -- if Enqueued (Bnn) then
5987 -- Cancel_Protected_Entry_Call (Bnn);
5993 -- _Disp_Asynchronous_Select
5994 -- (<object>, S, P'Address, D, B);
5995 -- Bnn := Communication_Block (D);
5997 -- Param1 := P.Param1;
5999 -- ParamN := P.ParamN;
6001 -- if Enqueued (Bnn) then
6002 -- <abortable-statements>
6005 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6008 -- when Abort_Signal => Abort_Undefer;
6011 -- if not Cancelled (Bnn) then
6012 -- <triggering-statements>
6015 -- elsif C = POK_Task_Entry then
6017 -- procedure _clean is
6019 -- Cancel_Task_Entry_Call (U);
6025 -- _Disp_Asynchronous_Select
6026 -- (<object>, S, P'Address, D, B);
6027 -- Bnn := Communication_Bloc (D);
6029 -- Param1 := P.Param1;
6031 -- ParamN := P.ParamN;
6036 -- <abortable-statements>
6038 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6041 -- when Abort_Signal => Abort_Undefer;
6045 -- <triggering-statements>
6050 -- <dispatching-call>;
6051 -- <triggering-statements>
6056 -- The job is to convert this to the asynchronous form
6058 -- If the trigger is a delay statement, it will have been expanded into a
6059 -- call to one of the GNARL delay procedures. This routine will convert
6060 -- this into a protected entry call on a delay object and then continue
6061 -- processing as for a protected entry call trigger. This requires
6062 -- declaring a Delay_Block object and adding a pointer to this object to
6063 -- the parameter list of the delay procedure to form the parameter list of
6064 -- the entry call. This object is used by the runtime to queue the delay
6067 -- For a description of the use of P and the assignments after the call,
6068 -- see Expand_N_Entry_Call_Statement.
6070 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6071 Loc : constant Source_Ptr := Sloc (N);
6072 Abrt : constant Node_Id := Abortable_Part (N);
6073 Astats : constant List_Id := Statements (Abrt);
6074 Trig : constant Node_Id := Triggering_Alternative (N);
6075 Tstats : constant List_Id := Statements (Trig);
6077 Abort_Block_Ent : Entity_Id;
6078 Abortable_Block : Node_Id;
6080 Blk_Ent : Entity_Id;
6081 Blk_Typ : Entity_Id;
6083 Call_Ent : Entity_Id;
6084 Cancel_Param : Entity_Id;
6085 Cleanup_Block : Node_Id;
6086 Cleanup_Block_Ent : Entity_Id;
6087 Cleanup_Stmts : List_Id;
6088 Conc_Typ_Stmts : List_Id;
6090 Dblock_Ent : Entity_Id;
6095 Enqueue_Call : Node_Id;
6098 Handler_Stmt : Node_Id;
6100 Lim_Typ_Stmts : List_Id;
6106 ProtE_Stmts : List_Id;
6107 ProtP_Stmts : List_Id;
6110 TaskE_Stmts : List_Id;
6112 B : Entity_Id; -- Call status flag
6113 Bnn : Entity_Id; -- Communication block
6114 C : Entity_Id; -- Call kind
6115 K : Entity_Id; -- Tagged kind
6116 P : Entity_Id; -- Parameter block
6117 S : Entity_Id; -- Primitive operation slot
6118 T : Entity_Id; -- Additional status flag
6121 Process_Statements_For_Controlled_Objects (Trig);
6122 Process_Statements_For_Controlled_Objects (Abrt);
6124 Blk_Ent := Make_Temporary (Loc, 'A');
6125 Ecall := Triggering_Statement (Trig);
6127 -- The arguments in the call may require dynamic allocation, and the
6128 -- call statement may have been transformed into a block. The block
6129 -- may contain additional declarations for internal entities, and the
6130 -- original call is found by sequential search.
6132 if Nkind (Ecall) = N_Block_Statement then
6133 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6134 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6135 N_Entry_Call_Statement)
6141 -- This is either a dispatching call or a delay statement used as a
6142 -- trigger which was expanded into a procedure call.
6144 if Nkind (Ecall) = N_Procedure_Call_Statement then
6145 if Ada_Version >= Ada_2005
6147 (No (Original_Node (Ecall))
6148 or else not Nkind_In (Original_Node (Ecall),
6149 N_Delay_Relative_Statement,
6150 N_Delay_Until_Statement))
6152 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6157 -- Call status flag processing, generate:
6158 -- B : Boolean := False;
6160 B := Build_B (Loc, Decls);
6162 -- Communication block processing, generate:
6163 -- Bnn : Communication_Block;
6165 Bnn := Make_Temporary (Loc, 'B');
6167 Make_Object_Declaration (Loc,
6168 Defining_Identifier => Bnn,
6169 Object_Definition =>
6170 New_Reference_To (RTE (RE_Communication_Block), Loc)));
6172 -- Call kind processing, generate:
6173 -- C : Ada.Tags.Prim_Op_Kind;
6175 C := Build_C (Loc, Decls);
6177 -- Tagged kind processing, generate:
6178 -- K : Ada.Tags.Tagged_Kind :=
6179 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6181 -- Dummy communication block, generate:
6182 -- D : Dummy_Communication_Block;
6185 Make_Object_Declaration (Loc,
6186 Defining_Identifier =>
6187 Make_Defining_Identifier (Loc, Name_uD),
6188 Object_Definition =>
6190 RTE (RE_Dummy_Communication_Block), Loc)));
6192 K := Build_K (Loc, Decls, Obj);
6194 -- Parameter block processing
6196 Blk_Typ := Build_Parameter_Block
6197 (Loc, Actuals, Formals, Decls);
6198 P := Parameter_Block_Pack
6199 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6201 -- Dispatch table slot processing, generate:
6204 S := Build_S (Loc, Decls);
6206 -- Additional status flag processing, generate:
6209 T := Make_Temporary (Loc, 'T');
6211 Make_Object_Declaration (Loc,
6212 Defining_Identifier => T,
6213 Object_Definition =>
6214 New_Reference_To (Standard_Boolean, Loc)));
6216 ------------------------------
6217 -- Protected entry handling --
6218 ------------------------------
6221 -- Param1 := P.Param1;
6223 -- ParamN := P.ParamN;
6225 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6228 -- Bnn := Communication_Block (D);
6230 Prepend_To (Cleanup_Stmts,
6231 Make_Assignment_Statement (Loc,
6233 New_Reference_To (Bnn, Loc),
6235 Make_Unchecked_Type_Conversion (Loc,
6237 New_Reference_To (RTE (RE_Communication_Block), Loc),
6238 Expression => Make_Identifier (Loc, Name_uD))));
6241 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6243 Prepend_To (Cleanup_Stmts,
6244 Make_Procedure_Call_Statement (Loc,
6247 Find_Prim_Op (Etype (Etype (Obj)),
6248 Name_uDisp_Asynchronous_Select),
6250 Parameter_Associations =>
6252 New_Copy_Tree (Obj), -- <object>
6253 New_Reference_To (S, Loc), -- S
6254 Make_Attribute_Reference (Loc, -- P'Address
6256 New_Reference_To (P, Loc),
6259 Make_Identifier (Loc, Name_uD), -- D
6260 New_Reference_To (B, Loc)))); -- B
6263 -- if Enqueued (Bnn) then
6264 -- <abortable-statements>
6267 Append_To (Cleanup_Stmts,
6268 Make_If_Statement (Loc,
6270 Make_Function_Call (Loc,
6272 New_Reference_To (RTE (RE_Enqueued), Loc),
6273 Parameter_Associations =>
6275 New_Reference_To (Bnn, Loc))),
6278 New_Copy_List_Tree (Astats)));
6280 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6281 -- will then generate a _clean for the communication block Bnn.
6285 -- procedure _clean is
6287 -- if Enqueued (Bnn) then
6288 -- Cancel_Protected_Entry_Call (Bnn);
6297 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
6299 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
6301 -- Wrap the cleanup block in an exception handling block
6307 -- when Abort_Signal => Abort_Undefer;
6310 Abort_Block_Ent := Make_Temporary (Loc, 'A');
6313 Make_Implicit_Label_Declaration (Loc,
6314 Defining_Identifier =>
6318 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
6321 -- if not Cancelled (Bnn) then
6322 -- <triggering-statements>
6325 Append_To (ProtE_Stmts,
6326 Make_If_Statement (Loc,
6330 Make_Function_Call (Loc,
6332 New_Reference_To (RTE (RE_Cancelled), Loc),
6333 Parameter_Associations =>
6335 New_Reference_To (Bnn, Loc)))),
6338 New_Copy_List_Tree (Tstats)));
6340 -------------------------
6341 -- Task entry handling --
6342 -------------------------
6345 -- Param1 := P.Param1;
6347 -- ParamN := P.ParamN;
6349 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6352 -- Bnn := Communication_Block (D);
6354 Append_To (TaskE_Stmts,
6355 Make_Assignment_Statement (Loc,
6357 New_Reference_To (Bnn, Loc),
6359 Make_Unchecked_Type_Conversion (Loc,
6361 New_Reference_To (RTE (RE_Communication_Block), Loc),
6362 Expression => Make_Identifier (Loc, Name_uD))));
6365 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6367 Prepend_To (TaskE_Stmts,
6368 Make_Procedure_Call_Statement (Loc,
6371 Find_Prim_Op (Etype (Etype (Obj)),
6372 Name_uDisp_Asynchronous_Select),
6374 Parameter_Associations =>
6376 New_Copy_Tree (Obj), -- <object>
6377 New_Reference_To (S, Loc), -- S
6378 Make_Attribute_Reference (Loc, -- P'Address
6380 New_Reference_To (P, Loc),
6383 Make_Identifier (Loc, Name_uD), -- D
6384 New_Reference_To (B, Loc)))); -- B
6389 Prepend_To (TaskE_Stmts,
6390 Make_Procedure_Call_Statement (Loc,
6392 New_Reference_To (RTE (RE_Abort_Defer), Loc),
6393 Parameter_Associations =>
6398 -- <abortable-statements>
6400 Cleanup_Stmts := New_Copy_List_Tree (Astats);
6402 Prepend_To (Cleanup_Stmts,
6403 Make_Procedure_Call_Statement (Loc,
6405 New_Reference_To (RTE (RE_Abort_Undefer), Loc),
6406 Parameter_Associations =>
6409 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6410 -- will generate a _clean for the additional status flag.
6414 -- procedure _clean is
6416 -- Cancel_Task_Entry_Call (U);
6424 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
6426 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
6428 -- Wrap the cleanup block in an exception handling block
6434 -- when Abort_Signal => Abort_Undefer;
6437 Abort_Block_Ent := Make_Temporary (Loc, 'A');
6439 Append_To (TaskE_Stmts,
6440 Make_Implicit_Label_Declaration (Loc,
6441 Defining_Identifier => Abort_Block_Ent));
6443 Append_To (TaskE_Stmts,
6445 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
6449 -- <triggering-statements>
6452 Append_To (TaskE_Stmts,
6453 Make_If_Statement (Loc,
6457 New_Reference_To (T, Loc)),
6460 New_Copy_List_Tree (Tstats)));
6462 ----------------------------------
6463 -- Protected procedure handling --
6464 ----------------------------------
6467 -- <dispatching-call>;
6468 -- <triggering-statements>
6470 ProtP_Stmts := New_Copy_List_Tree (Tstats);
6471 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
6474 -- S := Ada.Tags.Get_Offset_Index
6475 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
6478 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
6481 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6483 Append_To (Conc_Typ_Stmts,
6484 Make_Procedure_Call_Statement (Loc,
6487 Find_Prim_Op (Etype (Etype (Obj)),
6488 Name_uDisp_Get_Prim_Op_Kind),
6490 Parameter_Associations =>
6492 New_Copy_Tree (Obj),
6493 New_Reference_To (S, Loc),
6494 New_Reference_To (C, Loc))));
6497 -- if C = POK_Procedure_Entry then
6499 -- elsif C = POK_Task_Entry then
6505 Append_To (Conc_Typ_Stmts,
6506 Make_If_Statement (Loc,
6510 New_Reference_To (C, Loc),
6512 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
6519 Make_Elsif_Part (Loc,
6523 New_Reference_To (C, Loc),
6525 New_Reference_To (RTE (RE_POK_Task_Entry), Loc)),
6534 -- <dispatching-call>;
6535 -- <triggering-statements>
6537 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
6538 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
6541 -- if K = Ada.Tags.TK_Limited_Tagged then
6548 Make_If_Statement (Loc,
6552 New_Reference_To (K, Loc),
6554 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
6563 Make_Block_Statement (Loc,
6566 Handled_Statement_Sequence =>
6567 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6572 -- Delay triggering statement processing
6575 -- Add a Delay_Block object to the parameter list of the delay
6576 -- procedure to form the parameter list of the Wait entry call.
6578 Dblock_Ent := Make_Temporary (Loc, 'D');
6580 Pdef := Entity (Name (Ecall));
6582 if Is_RTE (Pdef, RO_CA_Delay_For) then
6584 New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
6586 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
6588 New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
6590 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
6591 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
6594 Append_To (Parameter_Associations (Ecall),
6595 Make_Attribute_Reference (Loc,
6596 Prefix => New_Reference_To (Dblock_Ent, Loc),
6597 Attribute_Name => Name_Unchecked_Access));
6599 -- Create the inner block to protect the abortable part
6601 Hdle := New_List (Build_Abort_Block_Handler (Loc));
6604 Make_Procedure_Call_Statement (Loc,
6605 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
6608 Make_Block_Statement (Loc,
6609 Identifier => New_Reference_To (Blk_Ent, Loc),
6610 Handled_Statement_Sequence =>
6611 Make_Handled_Sequence_Of_Statements (Loc,
6612 Statements => Astats),
6613 Has_Created_Identifier => True,
6614 Is_Asynchronous_Call_Block => True);
6616 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
6619 Make_Implicit_If_Statement (N,
6620 Condition => Make_Function_Call (Loc,
6621 Name => Enqueue_Call,
6622 Parameter_Associations => Parameter_Associations (Ecall)),
6624 New_List (Make_Block_Statement (Loc,
6625 Handled_Statement_Sequence =>
6626 Make_Handled_Sequence_Of_Statements (Loc,
6627 Statements => New_List (
6628 Make_Implicit_Label_Declaration (Loc,
6629 Defining_Identifier => Blk_Ent,
6630 Label_Construct => Abortable_Block),
6632 Exception_Handlers => Hdle)))));
6634 Stmts := New_List (Ecall);
6636 -- Construct statement sequence for new block
6639 Make_Implicit_If_Statement (N,
6640 Condition => Make_Function_Call (Loc,
6641 Name => New_Reference_To (
6642 RTE (RE_Timed_Out), Loc),
6643 Parameter_Associations => New_List (
6644 Make_Attribute_Reference (Loc,
6645 Prefix => New_Reference_To (Dblock_Ent, Loc),
6646 Attribute_Name => Name_Unchecked_Access))),
6647 Then_Statements => Tstats));
6649 -- The result is the new block
6651 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
6654 Make_Block_Statement (Loc,
6655 Declarations => New_List (
6656 Make_Object_Declaration (Loc,
6657 Defining_Identifier => Dblock_Ent,
6658 Aliased_Present => True,
6659 Object_Definition => New_Reference_To (
6660 RTE (RE_Delay_Block), Loc))),
6662 Handled_Statement_Sequence =>
6663 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6673 Extract_Entry (Ecall, Concval, Ename, Index);
6674 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
6676 Stmts := Statements (Handled_Statement_Sequence (Ecall));
6677 Decls := Declarations (Ecall);
6679 if Is_Protected_Type (Etype (Concval)) then
6681 -- Get the declarations of the block expanded from the entry call
6683 Decl := First (Decls);
6684 while Present (Decl)
6686 (Nkind (Decl) /= N_Object_Declaration
6687 or else not Is_RTE (Etype (Object_Definition (Decl)),
6688 RE_Communication_Block))
6693 pragma Assert (Present (Decl));
6694 Cancel_Param := Defining_Identifier (Decl);
6696 -- Change the mode of the Protected_Entry_Call call
6698 -- Protected_Entry_Call (
6699 -- Object => po._object'Access,
6700 -- E => <entry index>;
6701 -- Uninterpreted_Data => P'Address;
6702 -- Mode => Asynchronous_Call;
6705 Stmt := First (Stmts);
6707 -- Skip assignments to temporaries created for in-out parameters
6709 -- This makes unwarranted assumptions about the shape of the expanded
6710 -- tree for the call, and should be cleaned up ???
6712 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
6718 Param := First (Parameter_Associations (Call));
6719 while Present (Param)
6720 and then not Is_RTE (Etype (Param), RE_Call_Modes)
6725 pragma Assert (Present (Param));
6726 Rewrite (Param, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
6729 -- Append an if statement to execute the abortable part
6732 -- if Enqueued (Bnn) then
6735 Make_Implicit_If_Statement (N,
6736 Condition => Make_Function_Call (Loc,
6737 Name => New_Reference_To (RTE (RE_Enqueued), Loc),
6738 Parameter_Associations => New_List (
6739 New_Reference_To (Cancel_Param, Loc))),
6740 Then_Statements => Astats));
6743 Make_Block_Statement (Loc,
6744 Identifier => New_Reference_To (Blk_Ent, Loc),
6745 Handled_Statement_Sequence =>
6746 Make_Handled_Sequence_Of_Statements (Loc,
6747 Statements => Stmts),
6748 Has_Created_Identifier => True,
6749 Is_Asynchronous_Call_Block => True);
6751 -- For the VM call Update_Exception instead of Abort_Undefer.
6752 -- See 4jexcept.ads for an explanation.
6754 if VM_Target = No_VM then
6755 if Exception_Mechanism = Back_End_Exceptions then
6757 -- Aborts are not deferred at beginning of exception handlers
6760 Handler_Stmt := Make_Null_Statement (Loc);
6763 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
6764 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
6765 Parameter_Associations => No_List);
6768 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
6769 Name => New_Reference_To (RTE (RE_Update_Exception), Loc),
6770 Parameter_Associations => New_List (
6771 Make_Function_Call (Loc,
6772 Name => New_Occurrence_Of
6773 (RTE (RE_Current_Target_Exception), Loc))));
6777 Make_Block_Statement (Loc,
6778 Handled_Statement_Sequence =>
6779 Make_Handled_Sequence_Of_Statements (Loc,
6780 Statements => New_List (
6781 Make_Implicit_Label_Declaration (Loc,
6782 Defining_Identifier => Blk_Ent,
6783 Label_Construct => Abortable_Block),
6788 Exception_Handlers => New_List (
6789 Make_Implicit_Exception_Handler (Loc,
6791 -- when Abort_Signal =>
6792 -- Abort_Undefer.all;
6794 Exception_Choices =>
6795 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
6796 Statements => New_List (Handler_Stmt))))),
6798 -- if not Cancelled (Bnn) then
6799 -- triggered statements
6802 Make_Implicit_If_Statement (N,
6803 Condition => Make_Op_Not (Loc,
6805 Make_Function_Call (Loc,
6806 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
6807 Parameter_Associations => New_List (
6808 New_Occurrence_Of (Cancel_Param, Loc)))),
6809 Then_Statements => Tstats));
6811 -- Asynchronous task entry call
6818 B := Make_Defining_Identifier (Loc, Name_uB);
6820 -- Insert declaration of B in declarations of existing block
6823 Make_Object_Declaration (Loc,
6824 Defining_Identifier => B,
6825 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
6827 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
6829 -- Insert declaration of C in declarations of existing block
6832 Make_Object_Declaration (Loc,
6833 Defining_Identifier => Cancel_Param,
6834 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
6836 -- Remove and save the call to Call_Simple
6838 Stmt := First (Stmts);
6840 -- Skip assignments to temporaries created for in-out parameters.
6841 -- This makes unwarranted assumptions about the shape of the expanded
6842 -- tree for the call, and should be cleaned up ???
6844 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
6850 -- Create the inner block to protect the abortable part
6852 Hdle := New_List (Build_Abort_Block_Handler (Loc));
6855 Make_Procedure_Call_Statement (Loc,
6856 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
6859 Make_Block_Statement (Loc,
6860 Identifier => New_Reference_To (Blk_Ent, Loc),
6861 Handled_Statement_Sequence =>
6862 Make_Handled_Sequence_Of_Statements (Loc,
6863 Statements => Astats),
6864 Has_Created_Identifier => True,
6865 Is_Asynchronous_Call_Block => True);
6868 Make_Block_Statement (Loc,
6869 Handled_Statement_Sequence =>
6870 Make_Handled_Sequence_Of_Statements (Loc,
6871 Statements => New_List (
6872 Make_Implicit_Label_Declaration (Loc,
6873 Defining_Identifier =>
6878 Exception_Handlers => Hdle)));
6880 -- Create new call statement
6882 Params := Parameter_Associations (Call);
6885 New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
6887 New_Reference_To (B, Loc));
6890 Make_Procedure_Call_Statement (Loc,
6892 New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
6893 Parameter_Associations => Params));
6895 -- Construct statement sequence for new block
6898 Make_Implicit_If_Statement (N,
6901 New_Reference_To (Cancel_Param, Loc)),
6902 Then_Statements => Tstats));
6904 -- Protected the call against abort
6907 Make_Procedure_Call_Statement (Loc,
6908 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
6909 Parameter_Associations => Empty_List));
6912 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
6914 -- The result is the new block
6917 Make_Block_Statement (Loc,
6918 Declarations => Decls,
6919 Handled_Statement_Sequence =>
6920 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
6923 end Expand_N_Asynchronous_Select;
6925 -------------------------------------
6926 -- Expand_N_Conditional_Entry_Call --
6927 -------------------------------------
6929 -- The conditional task entry call is converted to a call to
6934 -- P : parms := (parm, parm, parm);
6938 -- (<acceptor-task>, -- Acceptor
6939 -- <entry-index>, -- E
6940 -- P'Address, -- Uninterpreted_Data
6941 -- Conditional_Call, -- Mode
6942 -- B); -- Rendezvous_Successful
6947 -- normal-statements
6953 -- For a description of the use of P and the assignments after the call,
6954 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
6955 -- conditional entry call has already been expanded (by the Expand_N_Entry
6956 -- _Call_Statement procedure) as follows:
6959 -- P : parms := (parm, parm, parm);
6961 -- ... info for in-out parameters
6962 -- Call_Simple (acceptor-task, entry-index, P'Address);
6968 -- so the task at hand is to convert the latter expansion into the former
6970 -- The conditional protected entry call is converted to a call to
6971 -- Protected_Entry_Call:
6974 -- P : parms := (parm, parm, parm);
6975 -- Bnn : Communications_Block;
6978 -- Protected_Entry_Call
6979 -- (po._object'Access, -- Object
6980 -- <entry index>, -- E
6981 -- P'Address, -- Uninterpreted_Data
6982 -- Conditional_Call, -- Mode
6987 -- if Cancelled (Bnn) then
6990 -- normal-statements
6994 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
6998 -- B : Boolean := False;
6999 -- C : Ada.Tags.Prim_Op_Kind;
7000 -- K : Ada.Tags.Tagged_Kind :=
7001 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7002 -- P : Parameters := (Param1 .. ParamN);
7006 -- if K = Ada.Tags.TK_Limited_Tagged then
7007 -- <dispatching-call>;
7008 -- <triggering-statements>
7012 -- Ada.Tags.Get_Offset_Index
7013 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7015 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7017 -- if C = POK_Protected_Entry
7018 -- or else C = POK_Task_Entry
7020 -- Param1 := P.Param1;
7022 -- ParamN := P.ParamN;
7026 -- if C = POK_Procedure
7027 -- or else C = POK_Protected_Procedure
7028 -- or else C = POK_Task_Procedure
7030 -- <dispatching-call>;
7033 -- <triggering-statements>
7035 -- <else-statements>
7040 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7041 Loc : constant Source_Ptr := Sloc (N);
7042 Alt : constant Node_Id := Entry_Call_Alternative (N);
7043 Blk : Node_Id := Entry_Call_Statement (Alt);
7046 Blk_Typ : Entity_Id;
7048 Call_Ent : Entity_Id;
7049 Conc_Typ_Stmts : List_Id;
7053 Lim_Typ_Stmts : List_Id;
7060 Transient_Blk : Node_Id;
7063 B : Entity_Id; -- Call status flag
7064 C : Entity_Id; -- Call kind
7065 K : Entity_Id; -- Tagged kind
7066 P : Entity_Id; -- Parameter block
7067 S : Entity_Id; -- Primitive operation slot
7070 Process_Statements_For_Controlled_Objects (N);
7072 if Ada_Version >= Ada_2005
7073 and then Nkind (Blk) = N_Procedure_Call_Statement
7075 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7080 -- Call status flag processing, generate:
7081 -- B : Boolean := False;
7083 B := Build_B (Loc, Decls);
7085 -- Call kind processing, generate:
7086 -- C : Ada.Tags.Prim_Op_Kind;
7088 C := Build_C (Loc, Decls);
7090 -- Tagged kind processing, generate:
7091 -- K : Ada.Tags.Tagged_Kind :=
7092 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7094 K := Build_K (Loc, Decls, Obj);
7096 -- Parameter block processing
7098 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7099 P := Parameter_Block_Pack
7100 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7102 -- Dispatch table slot processing, generate:
7105 S := Build_S (Loc, Decls);
7108 -- S := Ada.Tags.Get_Offset_Index
7109 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7112 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7115 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7117 Append_To (Conc_Typ_Stmts,
7118 Make_Procedure_Call_Statement (Loc,
7121 Find_Prim_Op (Etype (Etype (Obj)),
7122 Name_uDisp_Conditional_Select),
7124 Parameter_Associations =>
7126 New_Copy_Tree (Obj), -- <object>
7127 New_Reference_To (S, Loc), -- S
7128 Make_Attribute_Reference (Loc, -- P'Address
7130 New_Reference_To (P, Loc),
7133 New_Reference_To (C, Loc), -- C
7134 New_Reference_To (B, Loc)))); -- B
7137 -- if C = POK_Protected_Entry
7138 -- or else C = POK_Task_Entry
7140 -- Param1 := P.Param1;
7142 -- ParamN := P.ParamN;
7145 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7147 -- Generate the if statement only when the packed parameters need
7148 -- explicit assignments to their corresponding actuals.
7150 if Present (Unpack) then
7151 Append_To (Conc_Typ_Stmts,
7152 Make_If_Statement (Loc,
7159 New_Reference_To (C, Loc),
7161 New_Reference_To (RTE (
7162 RE_POK_Protected_Entry), Loc)),
7166 New_Reference_To (C, Loc),
7168 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
7176 -- if C = POK_Procedure
7177 -- or else C = POK_Protected_Procedure
7178 -- or else C = POK_Task_Procedure
7180 -- <dispatching-call>
7182 -- <normal-statements>
7184 -- <else-statements>
7187 N_Stats := New_Copy_List_Tree (Statements (Alt));
7189 Prepend_To (N_Stats,
7190 Make_If_Statement (Loc,
7196 New_Reference_To (C, Loc),
7198 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
7205 New_Reference_To (C, Loc),
7207 New_Reference_To (RTE (
7208 RE_POK_Protected_Procedure), Loc)),
7213 New_Reference_To (C, Loc),
7215 New_Reference_To (RTE (
7216 RE_POK_Task_Procedure), Loc)))),
7221 Append_To (Conc_Typ_Stmts,
7222 Make_If_Statement (Loc,
7223 Condition => New_Reference_To (B, Loc),
7224 Then_Statements => N_Stats,
7225 Else_Statements => Else_Statements (N)));
7228 -- <dispatching-call>;
7229 -- <triggering-statements>
7231 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
7232 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7235 -- if K = Ada.Tags.TK_Limited_Tagged then
7242 Make_If_Statement (Loc,
7246 New_Reference_To (K, Loc),
7248 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
7257 Make_Block_Statement (Loc,
7260 Handled_Statement_Sequence =>
7261 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7263 -- As described above, The entry alternative is transformed into a
7264 -- block that contains the gnulli call, and possibly assignment
7265 -- statements for in-out parameters. The gnulli call may itself be
7266 -- rewritten into a transient block if some unconstrained parameters
7267 -- require it. We need to retrieve the call to complete its parameter
7272 First_Real_Statement (Handled_Statement_Sequence (Blk));
7274 if Present (Transient_Blk)
7275 and then Nkind (Transient_Blk) = N_Block_Statement
7277 Blk := Transient_Blk;
7280 Stmts := Statements (Handled_Statement_Sequence (Blk));
7281 Stmt := First (Stmts);
7282 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7287 Params := Parameter_Associations (Call);
7289 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7291 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7293 Param := First (Params);
7294 while Present (Param)
7295 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7300 pragma Assert (Present (Param));
7301 Rewrite (Param, New_Reference_To (RTE (RE_Conditional_Call), Loc));
7305 -- Find the Communication_Block parameter for the call to the
7306 -- Cancelled function.
7308 Decl := First (Declarations (Blk));
7309 while Present (Decl)
7310 and then not Is_RTE (Etype (Object_Definition (Decl)),
7311 RE_Communication_Block)
7316 -- Add an if statement to execute the else part if the call
7317 -- does not succeed (as indicated by the Cancelled predicate).
7320 Make_Implicit_If_Statement (N,
7321 Condition => Make_Function_Call (Loc,
7322 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
7323 Parameter_Associations => New_List (
7324 New_Reference_To (Defining_Identifier (Decl), Loc))),
7325 Then_Statements => Else_Statements (N),
7326 Else_Statements => Statements (Alt)));
7329 B := Make_Defining_Identifier (Loc, Name_uB);
7331 -- Insert declaration of B in declarations of existing block
7333 if No (Declarations (Blk)) then
7334 Set_Declarations (Blk, New_List);
7337 Prepend_To (Declarations (Blk),
7338 Make_Object_Declaration (Loc,
7339 Defining_Identifier => B,
7340 Object_Definition =>
7341 New_Reference_To (Standard_Boolean, Loc)));
7343 -- Create new call statement
7346 New_Reference_To (RTE (RE_Conditional_Call), Loc));
7347 Append_To (Params, New_Reference_To (B, Loc));
7350 Make_Procedure_Call_Statement (Loc,
7351 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
7352 Parameter_Associations => Params));
7354 -- Construct statement sequence for new block
7357 Make_Implicit_If_Statement (N,
7358 Condition => New_Reference_To (B, Loc),
7359 Then_Statements => Statements (Alt),
7360 Else_Statements => Else_Statements (N)));
7363 -- The result is the new block
7366 Make_Block_Statement (Loc,
7367 Declarations => Declarations (Blk),
7368 Handled_Statement_Sequence =>
7369 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7373 end Expand_N_Conditional_Entry_Call;
7375 ---------------------------------------
7376 -- Expand_N_Delay_Relative_Statement --
7377 ---------------------------------------
7379 -- Delay statement is implemented as a procedure call to Delay_For
7380 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
7381 -- simple delays imposed by the use of Protected Objects.
7383 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
7384 Loc : constant Source_Ptr := Sloc (N);
7387 Make_Procedure_Call_Statement (Loc,
7388 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
7389 Parameter_Associations => New_List (Expression (N))));
7391 end Expand_N_Delay_Relative_Statement;
7393 ------------------------------------
7394 -- Expand_N_Delay_Until_Statement --
7395 ------------------------------------
7397 -- Delay Until statement is implemented as a procedure call to
7398 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
7400 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
7401 Loc : constant Source_Ptr := Sloc (N);
7405 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
7406 Typ := RTE (RO_CA_Delay_Until);
7408 Typ := RTE (RO_RT_Delay_Until);
7412 Make_Procedure_Call_Statement (Loc,
7413 Name => New_Reference_To (Typ, Loc),
7414 Parameter_Associations => New_List (Expression (N))));
7417 end Expand_N_Delay_Until_Statement;
7419 -------------------------
7420 -- Expand_N_Entry_Body --
7421 -------------------------
7423 procedure Expand_N_Entry_Body (N : Node_Id) is
7425 -- Associate discriminals with the next protected operation body to be
7428 if Present (Next_Protected_Operation (N)) then
7429 Set_Discriminals (Parent (Current_Scope));
7431 end Expand_N_Entry_Body;
7433 -----------------------------------
7434 -- Expand_N_Entry_Call_Statement --
7435 -----------------------------------
7437 -- An entry call is expanded into GNARLI calls to implement a simple entry
7438 -- call (see Build_Simple_Entry_Call).
7440 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
7446 if No_Run_Time_Mode then
7447 Error_Msg_CRT ("entry call", N);
7451 -- If this entry call is part of an asynchronous select, don't expand it
7452 -- here; it will be expanded with the select statement. Don't expand
7453 -- timed entry calls either, as they are translated into asynchronous
7456 -- ??? This whole approach is questionable; it may be better to go back
7457 -- to allowing the expansion to take place and then attempting to fix it
7458 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
7459 -- whether the expanded call is on a task or protected entry.
7461 if (Nkind (Parent (N)) /= N_Triggering_Alternative
7462 or else N /= Triggering_Statement (Parent (N)))
7463 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
7464 or else N /= Entry_Call_Statement (Parent (N))
7465 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
7467 Extract_Entry (N, Concval, Ename, Index);
7468 Build_Simple_Entry_Call (N, Concval, Ename, Index);
7470 end Expand_N_Entry_Call_Statement;
7472 --------------------------------
7473 -- Expand_N_Entry_Declaration --
7474 --------------------------------
7476 -- If there are parameters, then first, each of the formals is marked by
7477 -- setting Is_Entry_Formal. Next a record type is built which is used to
7478 -- hold the parameter values. The name of this record type is entryP where
7479 -- entry is the name of the entry, with an additional corresponding access
7480 -- type called entryPA. The record type has matching components for each
7481 -- formal (the component names are the same as the formal names). For
7482 -- elementary types, the component type matches the formal type. For
7483 -- composite types, an access type is declared (with the name formalA)
7484 -- which designates the formal type, and the type of the component is this
7485 -- access type. Finally the Entry_Component of each formal is set to
7486 -- reference the corresponding record component.
7488 procedure Expand_N_Entry_Declaration (N : Node_Id) is
7489 Loc : constant Source_Ptr := Sloc (N);
7490 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
7491 Components : List_Id;
7494 Last_Decl : Node_Id;
7495 Component : Entity_Id;
7498 Rec_Ent : Entity_Id;
7499 Acc_Ent : Entity_Id;
7502 Formal := First_Formal (Entry_Ent);
7505 -- Most processing is done only if parameters are present
7507 if Present (Formal) then
7508 Components := New_List;
7510 -- Loop through formals
7512 while Present (Formal) loop
7513 Set_Is_Entry_Formal (Formal);
7515 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
7516 Set_Entry_Component (Formal, Component);
7517 Set_Entry_Formal (Component, Formal);
7518 Ftype := Etype (Formal);
7520 -- Declare new access type and then append
7522 Ctype := Make_Temporary (Loc, 'A');
7525 Make_Full_Type_Declaration (Loc,
7526 Defining_Identifier => Ctype,
7528 Make_Access_To_Object_Definition (Loc,
7529 All_Present => True,
7530 Constant_Present => Ekind (Formal) = E_In_Parameter,
7531 Subtype_Indication => New_Reference_To (Ftype, Loc)));
7533 Insert_After (Last_Decl, Decl);
7536 Append_To (Components,
7537 Make_Component_Declaration (Loc,
7538 Defining_Identifier => Component,
7539 Component_Definition =>
7540 Make_Component_Definition (Loc,
7541 Aliased_Present => False,
7542 Subtype_Indication => New_Reference_To (Ctype, Loc))));
7544 Next_Formal_With_Extras (Formal);
7547 -- Create the Entry_Parameter_Record declaration
7549 Rec_Ent := Make_Temporary (Loc, 'P');
7552 Make_Full_Type_Declaration (Loc,
7553 Defining_Identifier => Rec_Ent,
7555 Make_Record_Definition (Loc,
7557 Make_Component_List (Loc,
7558 Component_Items => Components)));
7560 Insert_After (Last_Decl, Decl);
7563 -- Construct and link in the corresponding access type
7565 Acc_Ent := Make_Temporary (Loc, 'A');
7567 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
7570 Make_Full_Type_Declaration (Loc,
7571 Defining_Identifier => Acc_Ent,
7573 Make_Access_To_Object_Definition (Loc,
7574 All_Present => True,
7575 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
7577 Insert_After (Last_Decl, Decl);
7579 end Expand_N_Entry_Declaration;
7581 -----------------------------
7582 -- Expand_N_Protected_Body --
7583 -----------------------------
7585 -- Protected bodies are expanded to the completion of the subprograms
7586 -- created for the corresponding protected type. These are a protected and
7587 -- unprotected version of each protected subprogram in the object, a
7588 -- function to calculate each entry barrier, and a procedure to execute the
7589 -- sequence of statements of each protected entry body. For example, for
7590 -- protected type ptype:
7593 -- (O : System.Address;
7594 -- E : Protected_Entry_Index)
7597 -- <discriminant renamings>
7598 -- <private object renamings>
7600 -- return <barrier expression>;
7603 -- procedure pprocN (_object : in out poV;...) is
7604 -- <discriminant renamings>
7605 -- <private object renamings>
7607 -- <sequence of statements>
7610 -- procedure pprocP (_object : in out poV;...) is
7611 -- procedure _clean is
7614 -- ptypeS (_object, Pn);
7615 -- Unlock (_object._object'Access);
7616 -- Abort_Undefer.all;
7621 -- Lock (_object._object'Access);
7622 -- pprocN (_object;...);
7627 -- function pfuncN (_object : poV;...) return Return_Type is
7628 -- <discriminant renamings>
7629 -- <private object renamings>
7631 -- <sequence of statements>
7634 -- function pfuncP (_object : poV) return Return_Type is
7635 -- procedure _clean is
7637 -- Unlock (_object._object'Access);
7638 -- Abort_Undefer.all;
7643 -- Lock (_object._object'Access);
7644 -- return pfuncN (_object);
7651 -- (O : System.Address;
7652 -- P : System.Address;
7653 -- E : Protected_Entry_Index)
7655 -- <discriminant renamings>
7656 -- <private object renamings>
7657 -- type poVP is access poV;
7658 -- _Object : ptVP := ptVP!(O);
7662 -- <statement sequence>
7663 -- Complete_Entry_Body (_Object._Object);
7665 -- when all others =>
7666 -- Exceptional_Complete_Entry_Body (
7667 -- _Object._Object, Get_GNAT_Exception);
7671 -- The type poV is the record created for the protected type to hold
7672 -- the state of the protected object.
7674 procedure Expand_N_Protected_Body (N : Node_Id) is
7675 Loc : constant Source_Ptr := Sloc (N);
7676 Pid : constant Entity_Id := Corresponding_Spec (N);
7678 Current_Node : Node_Id;
7679 Disp_Op_Body : Node_Id;
7680 New_Op_Body : Node_Id;
7681 Num_Entries : Natural := 0;
7685 function Build_Dispatching_Subprogram_Body
7688 Prot_Bod : Node_Id) return Node_Id;
7689 -- Build a dispatching version of the protected subprogram body. The
7690 -- newly generated subprogram contains a call to the original protected
7691 -- body. The following code is generated:
7693 -- function <protected-function-name> (Param1 .. ParamN) return
7696 -- return <protected-function-name>P (Param1 .. ParamN);
7697 -- end <protected-function-name>;
7701 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
7703 -- <protected-procedure-name>P (Param1 .. ParamN);
7704 -- end <protected-procedure-name>
7706 ---------------------------------------
7707 -- Build_Dispatching_Subprogram_Body --
7708 ---------------------------------------
7710 function Build_Dispatching_Subprogram_Body
7713 Prot_Bod : Node_Id) return Node_Id
7715 Loc : constant Source_Ptr := Sloc (N);
7722 -- Generate a specification without a letter suffix in order to
7723 -- override an interface function or procedure.
7725 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
7727 -- The formal parameters become the actuals of the protected function
7728 -- or procedure call.
7730 Actuals := New_List;
7731 Formal := First (Parameter_Specifications (Spec));
7732 while Present (Formal) loop
7734 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
7739 if Nkind (Spec) = N_Procedure_Specification then
7742 Make_Procedure_Call_Statement (Loc,
7744 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
7745 Parameter_Associations => Actuals));
7747 pragma Assert (Nkind (Spec) = N_Function_Specification);
7751 Make_Simple_Return_Statement (Loc,
7753 Make_Function_Call (Loc,
7755 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
7756 Parameter_Associations => Actuals)));
7760 Make_Subprogram_Body (Loc,
7761 Declarations => Empty_List,
7762 Specification => Spec,
7763 Handled_Statement_Sequence =>
7764 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
7765 end Build_Dispatching_Subprogram_Body;
7767 -- Start of processing for Expand_N_Protected_Body
7770 if No_Run_Time_Mode then
7771 Error_Msg_CRT ("protected body", N);
7775 -- This is the proper body corresponding to a stub. The declarations
7776 -- must be inserted at the point of the stub, which in turn is in the
7777 -- declarative part of the parent unit.
7779 if Nkind (Parent (N)) = N_Subunit then
7780 Current_Node := Corresponding_Stub (Parent (N));
7785 Op_Body := First (Declarations (N));
7787 -- The protected body is replaced with the bodies of its
7788 -- protected operations, and the declarations for internal objects
7789 -- that may have been created for entry family bounds.
7791 Rewrite (N, Make_Null_Statement (Sloc (N)));
7794 while Present (Op_Body) loop
7795 case Nkind (Op_Body) is
7796 when N_Subprogram_Declaration =>
7799 when N_Subprogram_Body =>
7801 -- Do not create bodies for eliminated operations
7803 if not Is_Eliminated (Defining_Entity (Op_Body))
7804 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
7807 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
7809 Insert_After (Current_Node, New_Op_Body);
7810 Current_Node := New_Op_Body;
7811 Analyze (New_Op_Body);
7813 -- Build the corresponding protected operation. It may
7814 -- appear that this is needed only if this is a visible
7815 -- operation of the type, or if it is an interrupt handler,
7816 -- and this was the strategy used previously in GNAT.
7817 -- However, the operation may be exported through a 'Access
7818 -- to an external caller. This is the common idiom in code
7819 -- that uses the Ada 2005 Timing_Events package. As a result
7820 -- we need to produce the protected body for both visible
7821 -- and private operations, as well as operations that only
7822 -- have a body in the source, and for which we create a
7823 -- declaration in the protected body itself.
7825 if Present (Corresponding_Spec (Op_Body)) then
7827 Build_Protected_Subprogram_Body (
7828 Op_Body, Pid, Specification (New_Op_Body));
7830 Insert_After (Current_Node, New_Op_Body);
7831 Analyze (New_Op_Body);
7833 Current_Node := New_Op_Body;
7835 -- Generate an overriding primitive operation body for
7836 -- this subprogram if the protected type implements an
7839 if Ada_Version >= Ada_2005
7841 Present (Interfaces (Corresponding_Record_Type (Pid)))
7844 Build_Dispatching_Subprogram_Body
7845 (Op_Body, Pid, New_Op_Body);
7847 Insert_After (Current_Node, Disp_Op_Body);
7848 Analyze (Disp_Op_Body);
7850 Current_Node := Disp_Op_Body;
7855 when N_Entry_Body =>
7856 Op_Id := Defining_Identifier (Op_Body);
7857 Num_Entries := Num_Entries + 1;
7859 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
7861 Insert_After (Current_Node, New_Op_Body);
7862 Current_Node := New_Op_Body;
7863 Analyze (New_Op_Body);
7865 when N_Implicit_Label_Declaration =>
7868 when N_Itype_Reference =>
7869 Insert_After (Current_Node, New_Copy (Op_Body));
7871 when N_Freeze_Entity =>
7872 New_Op_Body := New_Copy (Op_Body);
7874 if Present (Entity (Op_Body))
7875 and then Freeze_Node (Entity (Op_Body)) = Op_Body
7877 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
7880 Insert_After (Current_Node, New_Op_Body);
7881 Current_Node := New_Op_Body;
7882 Analyze (New_Op_Body);
7885 New_Op_Body := New_Copy (Op_Body);
7886 Insert_After (Current_Node, New_Op_Body);
7887 Current_Node := New_Op_Body;
7888 Analyze (New_Op_Body);
7890 when N_Object_Declaration =>
7891 pragma Assert (not Comes_From_Source (Op_Body));
7892 New_Op_Body := New_Copy (Op_Body);
7893 Insert_After (Current_Node, New_Op_Body);
7894 Current_Node := New_Op_Body;
7895 Analyze (New_Op_Body);
7898 raise Program_Error;
7905 -- Finally, create the body of the function that maps an entry index
7906 -- into the corresponding body index, except when there is no entry, or
7907 -- in a Ravenscar-like profile.
7909 if Corresponding_Runtime_Package (Pid) =
7910 System_Tasking_Protected_Objects_Entries
7912 New_Op_Body := Build_Find_Body_Index (Pid);
7913 Insert_After (Current_Node, New_Op_Body);
7914 Current_Node := New_Op_Body;
7915 Analyze (New_Op_Body);
7918 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
7919 -- protected body. At this point all wrapper specs have been created,
7920 -- frozen and included in the dispatch table for the protected type.
7922 if Ada_Version >= Ada_2005 then
7923 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
7925 end Expand_N_Protected_Body;
7927 -----------------------------------------
7928 -- Expand_N_Protected_Type_Declaration --
7929 -----------------------------------------
7931 -- First we create a corresponding record type declaration used to
7932 -- represent values of this protected type.
7933 -- The general form of this type declaration is
7935 -- type poV (discriminants) is record
7936 -- _Object : aliased <kind>Protection
7937 -- [(<entry count> [, <handler count>])];
7938 -- [entry_family : array (bounds) of Void;]
7939 -- <private data fields>
7942 -- The discriminants are present only if the corresponding protected type
7943 -- has discriminants, and they exactly mirror the protected type
7944 -- discriminants. The private data fields similarly mirror the private
7945 -- declarations of the protected type.
7947 -- The Object field is always present. It contains RTS specific data used
7948 -- to control the protected object. It is declared as Aliased so that it
7949 -- can be passed as a pointer to the RTS. This allows the protected record
7950 -- to be referenced within RTS data structures. An appropriate Protection
7951 -- type and discriminant are generated.
7953 -- The Service field is present for protected objects with entries. It
7954 -- contains sufficient information to allow the entry service procedure for
7955 -- this object to be called when the object is not known till runtime.
7957 -- One entry_family component is present for each entry family in the
7958 -- task definition (see Expand_N_Task_Type_Declaration).
7960 -- When a protected object is declared, an instance of the protected type
7961 -- value record is created. The elaboration of this declaration creates the
7962 -- correct bounds for the entry families, and also evaluates the priority
7963 -- expression if needed. The initialization routine for the protected type
7964 -- itself then calls Initialize_Protection with appropriate parameters to
7965 -- initialize the value of the Task_Id field. Install_Handlers may be also
7966 -- called if a pragma Attach_Handler applies.
7968 -- Note: this record is passed to the subprograms created by the expansion
7969 -- of protected subprograms and entries. It is an in parameter to protected
7970 -- functions and an in out parameter to procedures and entry bodies. The
7971 -- Entity_Id for this created record type is placed in the
7972 -- Corresponding_Record_Type field of the associated protected type entity.
7974 -- Next we create a procedure specifications for protected subprograms and
7975 -- entry bodies. For each protected subprograms two subprograms are
7976 -- created, an unprotected and a protected version. The unprotected version
7977 -- is called from within other operations of the same protected object.
7979 -- We also build the call to register the procedure if a pragma
7980 -- Interrupt_Handler applies.
7982 -- A single subprogram is created to service all entry bodies; it has an
7983 -- additional boolean out parameter indicating that the previous entry call
7984 -- made by the current task was serviced immediately, i.e. not by proxy.
7985 -- The O parameter contains a pointer to a record object of the type
7986 -- described above. An untyped interface is used here to allow this
7987 -- procedure to be called in places where the type of the object to be
7988 -- serviced is not known. This must be done, for example, when a call that
7989 -- may have been requeued is cancelled; the corresponding object must be
7990 -- serviced, but which object that is not known till runtime.
7993 -- (O : System.Address; P : out Boolean);
7994 -- procedure pprocN (_object : in out poV);
7995 -- procedure pproc (_object : in out poV);
7996 -- function pfuncN (_object : poV);
7997 -- function pfunc (_object : poV);
8000 -- Note that this must come after the record type declaration, since
8001 -- the specs refer to this type.
8003 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8004 Loc : constant Source_Ptr := Sloc (N);
8005 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8007 Pdef : constant Node_Id := Protected_Definition (N);
8008 -- This contains two lists; one for visible and one for private decls
8012 Discr_Map : constant Elist_Id := New_Elmt_List;
8016 Comp_Id : Entity_Id;
8018 Current_Node : Node_Id := N;
8019 Bdef : Entity_Id := Empty; -- avoid uninit warning
8020 Edef : Entity_Id := Empty; -- avoid uninit warning
8021 Entries_Aggr : Node_Id;
8022 Body_Id : Entity_Id;
8025 Object_Comp : Node_Id;
8027 procedure Check_Inlining (Subp : Entity_Id);
8028 -- If the original operation has a pragma Inline, propagate the flag
8029 -- to the internal body, for possible inlining later on. The source
8030 -- operation is invisible to the back-end and is never actually called.
8032 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8033 -- When compiling under the Ravenscar profile, private components must
8034 -- have a static size, or else a protected object will require heap
8035 -- allocation, violating the corresponding restriction. It is preferable
8036 -- to make this check here, because it provides a better error message
8037 -- than the back-end, which refers to the object as a whole.
8039 procedure Register_Handler;
8040 -- For a protected operation that is an interrupt handler, add the
8041 -- freeze action that will register it as such.
8043 --------------------
8044 -- Check_Inlining --
8045 --------------------
8047 procedure Check_Inlining (Subp : Entity_Id) is
8049 if Is_Inlined (Subp) then
8050 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8051 Set_Is_Inlined (Subp, False);
8055 ---------------------------------
8056 -- Check_Static_Component_Size --
8057 ---------------------------------
8059 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8060 Typ : constant Entity_Id := Etype (Comp);
8064 if Is_Scalar_Type (Typ) then
8067 elsif Is_Array_Type (Typ) then
8068 return Compile_Time_Known_Bounds (Typ);
8070 elsif Is_Record_Type (Typ) then
8071 C := First_Component (Typ);
8072 while Present (C) loop
8073 if not Static_Component_Size (C) then
8082 -- Any other types will be checked by the back-end
8087 end Static_Component_Size;
8089 ----------------------
8090 -- Register_Handler --
8091 ----------------------
8093 procedure Register_Handler is
8095 -- All semantic checks already done in Sem_Prag
8097 Prot_Proc : constant Entity_Id :=
8099 (Specification (Current_Node));
8101 Proc_Address : constant Node_Id :=
8102 Make_Attribute_Reference (Loc,
8103 Prefix => New_Reference_To (Prot_Proc, Loc),
8104 Attribute_Name => Name_Address);
8106 RTS_Call : constant Entity_Id :=
8107 Make_Procedure_Call_Statement (Loc,
8110 RTE (RE_Register_Interrupt_Handler), Loc),
8111 Parameter_Associations =>
8112 New_List (Proc_Address));
8114 Append_Freeze_Action (Prot_Proc, RTS_Call);
8115 end Register_Handler;
8117 -- Start of processing for Expand_N_Protected_Type_Declaration
8120 if Present (Corresponding_Record_Type (Prot_Typ)) then
8123 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8126 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8128 Qualify_Entity_Names (N);
8130 -- If the type has discriminants, their occurrences in the declaration
8131 -- have been replaced by the corresponding discriminals. For components
8132 -- that are constrained by discriminants, their homologues in the
8133 -- corresponding record type must refer to the discriminants of that
8134 -- record, so we must apply a new renaming to subtypes_indications:
8136 -- protected discriminant => discriminal => record discriminant
8138 -- This replacement is not applied to default expressions, for which
8139 -- the discriminal is correct.
8141 if Has_Discriminants (Prot_Typ) then
8147 Disc := First_Discriminant (Prot_Typ);
8148 Decl := First (Discriminant_Specifications (Rec_Decl));
8149 while Present (Disc) loop
8150 Append_Elmt (Discriminal (Disc), Discr_Map);
8151 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
8152 Next_Discriminant (Disc);
8158 -- Fill in the component declarations
8160 -- Add components for entry families. For each entry family, create an
8161 -- anonymous type declaration with the same size, and analyze the type.
8163 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
8165 -- Prepend the _Object field with the right type to the component list.
8166 -- We need to compute the number of entries, and in some cases the
8167 -- number of Attach_Handler pragmas.
8171 Num_Attach_Handler : Int := 0;
8172 Protection_Subtype : Node_Id;
8173 Entry_Count_Expr : constant Node_Id :=
8174 Build_Entry_Count_Expression
8175 (Prot_Typ, Cdecls, Loc);
8178 -- Could this be simplified using Corresponding_Runtime_Package???
8180 if Has_Attach_Handler (Prot_Typ) then
8181 Ritem := First_Rep_Item (Prot_Typ);
8182 while Present (Ritem) loop
8183 if Nkind (Ritem) = N_Pragma
8184 and then Pragma_Name (Ritem) = Name_Attach_Handler
8186 Num_Attach_Handler := Num_Attach_Handler + 1;
8189 Next_Rep_Item (Ritem);
8192 if Restricted_Profile then
8193 if Has_Entries (Prot_Typ) then
8194 Protection_Subtype :=
8195 New_Reference_To (RTE (RE_Protection_Entry), Loc);
8197 Protection_Subtype :=
8198 New_Reference_To (RTE (RE_Protection), Loc);
8201 Protection_Subtype :=
8202 Make_Subtype_Indication
8206 (RTE (RE_Static_Interrupt_Protection), Loc),
8208 Make_Index_Or_Discriminant_Constraint (
8210 Constraints => New_List (
8212 Make_Integer_Literal (Loc, Num_Attach_Handler))));
8215 elsif Has_Interrupt_Handler (Prot_Typ)
8216 and then not Restriction_Active (No_Dynamic_Attachment)
8218 Protection_Subtype :=
8219 Make_Subtype_Indication (
8221 Subtype_Mark => New_Reference_To
8222 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
8224 Make_Index_Or_Discriminant_Constraint (
8226 Constraints => New_List (Entry_Count_Expr)));
8228 -- Type has explicit entries or generated primitive entry wrappers
8230 elsif Has_Entries (Prot_Typ)
8231 or else (Ada_Version >= Ada_2005
8232 and then Present (Interface_List (N)))
8234 case Corresponding_Runtime_Package (Prot_Typ) is
8235 when System_Tasking_Protected_Objects_Entries =>
8236 Protection_Subtype :=
8237 Make_Subtype_Indication (Loc,
8239 New_Reference_To (RTE (RE_Protection_Entries), Loc),
8241 Make_Index_Or_Discriminant_Constraint (
8243 Constraints => New_List (Entry_Count_Expr)));
8245 when System_Tasking_Protected_Objects_Single_Entry =>
8246 Protection_Subtype :=
8247 New_Reference_To (RTE (RE_Protection_Entry), Loc);
8250 raise Program_Error;
8254 Protection_Subtype := New_Reference_To (RTE (RE_Protection), Loc);
8258 Make_Component_Declaration (Loc,
8259 Defining_Identifier =>
8260 Make_Defining_Identifier (Loc, Name_uObject),
8261 Component_Definition =>
8262 Make_Component_Definition (Loc,
8263 Aliased_Present => True,
8264 Subtype_Indication => Protection_Subtype));
8267 pragma Assert (Present (Pdef));
8269 -- Add private field components
8271 if Present (Private_Declarations (Pdef)) then
8272 Priv := First (Private_Declarations (Pdef));
8274 while Present (Priv) loop
8276 if Nkind (Priv) = N_Component_Declaration then
8277 if not Static_Component_Size (Defining_Identifier (Priv)) then
8279 -- When compiling for a restricted profile, the private
8280 -- components must have a static size. If not, this is an
8281 -- error for a single protected declaration, and rates a
8282 -- warning on a protected type declaration.
8284 if not Comes_From_Source (Prot_Typ) then
8285 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
8287 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
8288 Error_Msg_N ("component has non-static size?", Priv);
8290 ("\creation of protected object of type& will violate"
8291 & " restriction No_Implicit_Heap_Allocations?",
8296 -- The component definition consists of a subtype indication,
8297 -- or (in Ada 2005) an access definition. Make a copy of the
8298 -- proper definition.
8301 Old_Comp : constant Node_Id := Component_Definition (Priv);
8302 Oent : constant Entity_Id := Defining_Identifier (Priv);
8304 Nent : constant Entity_Id :=
8305 Make_Defining_Identifier (Sloc (Oent),
8306 Chars => Chars (Oent));
8309 if Present (Subtype_Indication (Old_Comp)) then
8311 Make_Component_Definition (Sloc (Oent),
8312 Aliased_Present => False,
8313 Subtype_Indication =>
8314 New_Copy_Tree (Subtype_Indication (Old_Comp),
8318 Make_Component_Definition (Sloc (Oent),
8319 Aliased_Present => False,
8320 Access_Definition =>
8321 New_Copy_Tree (Access_Definition (Old_Comp),
8326 Make_Component_Declaration (Loc,
8327 Defining_Identifier => Nent,
8328 Component_Definition => New_Comp,
8329 Expression => Expression (Priv));
8331 Set_Has_Per_Object_Constraint (Nent,
8332 Has_Per_Object_Constraint (Oent));
8334 Append_To (Cdecls, New_Priv);
8337 elsif Nkind (Priv) = N_Subprogram_Declaration then
8339 -- Make the unprotected version of the subprogram available
8340 -- for expansion of intra object calls. There is need for
8341 -- a protected version only if the subprogram is an interrupt
8342 -- handler, otherwise this operation can only be called from
8346 Make_Subprogram_Declaration (Loc,
8348 Build_Protected_Sub_Specification
8349 (Priv, Prot_Typ, Unprotected_Mode));
8351 Insert_After (Current_Node, Sub);
8354 Set_Protected_Body_Subprogram
8355 (Defining_Unit_Name (Specification (Priv)),
8356 Defining_Unit_Name (Specification (Sub)));
8357 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
8358 Current_Node := Sub;
8361 Make_Subprogram_Declaration (Loc,
8363 Build_Protected_Sub_Specification
8364 (Priv, Prot_Typ, Protected_Mode));
8366 Insert_After (Current_Node, Sub);
8368 Current_Node := Sub;
8370 if Is_Interrupt_Handler
8371 (Defining_Unit_Name (Specification (Priv)))
8373 if not Restricted_Profile then
8383 -- Put the _Object component after the private component so that it
8384 -- be finalized early as required by 9.4 (20)
8386 Append_To (Cdecls, Object_Comp);
8388 Insert_After (Current_Node, Rec_Decl);
8389 Current_Node := Rec_Decl;
8391 -- Analyze the record declaration immediately after construction,
8392 -- because the initialization procedure is needed for single object
8393 -- declarations before the next entity is analyzed (the freeze call
8394 -- that generates this initialization procedure is found below).
8396 Analyze (Rec_Decl, Suppress => All_Checks);
8398 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
8399 -- the corresponding record is frozen. If any wrappers are generated,
8400 -- Current_Node is updated accordingly.
8402 if Ada_Version >= Ada_2005 then
8403 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
8406 -- Collect pointers to entry bodies and their barriers, to be placed
8407 -- in the Entry_Bodies_Array for the type. For each entry/family we
8408 -- add an expression to the aggregate which is the initial value of
8409 -- this array. The array is declared after all protected subprograms.
8411 if Has_Entries (Prot_Typ) then
8412 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
8414 Entries_Aggr := Empty;
8417 -- Build two new procedure specifications for each protected subprogram;
8418 -- one to call from outside the object and one to call from inside.
8419 -- Build a barrier function and an entry body action procedure
8420 -- specification for each protected entry. Initialize the entry body
8421 -- array. If subprogram is flagged as eliminated, do not generate any
8422 -- internal operations.
8426 Comp := First (Visible_Declarations (Pdef));
8428 while Present (Comp) loop
8429 if Nkind (Comp) = N_Subprogram_Declaration then
8431 Make_Subprogram_Declaration (Loc,
8433 Build_Protected_Sub_Specification
8434 (Comp, Prot_Typ, Unprotected_Mode));
8436 Insert_After (Current_Node, Sub);
8439 Set_Protected_Body_Subprogram
8440 (Defining_Unit_Name (Specification (Comp)),
8441 Defining_Unit_Name (Specification (Sub)));
8442 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
8444 -- Make the protected version of the subprogram available for
8445 -- expansion of external calls.
8447 Current_Node := Sub;
8450 Make_Subprogram_Declaration (Loc,
8452 Build_Protected_Sub_Specification
8453 (Comp, Prot_Typ, Protected_Mode));
8455 Insert_After (Current_Node, Sub);
8458 Current_Node := Sub;
8460 -- Generate an overriding primitive operation specification for
8461 -- this subprogram if the protected type implements an interface.
8463 if Ada_Version >= Ada_2005
8465 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
8468 Make_Subprogram_Declaration (Loc,
8470 Build_Protected_Sub_Specification
8471 (Comp, Prot_Typ, Dispatching_Mode));
8473 Insert_After (Current_Node, Sub);
8476 Current_Node := Sub;
8479 -- If a pragma Interrupt_Handler applies, build and add a call to
8480 -- Register_Interrupt_Handler to the freezing actions of the
8481 -- protected version (Current_Node) of the subprogram:
8483 -- system.interrupts.register_interrupt_handler
8484 -- (prot_procP'address);
8486 if not Restricted_Profile
8487 and then Is_Interrupt_Handler
8488 (Defining_Unit_Name (Specification (Comp)))
8493 elsif Nkind (Comp) = N_Entry_Declaration then
8494 E_Count := E_Count + 1;
8495 Comp_Id := Defining_Identifier (Comp);
8498 Make_Defining_Identifier (Loc,
8499 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
8501 Make_Subprogram_Declaration (Loc,
8503 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
8505 Insert_After (Current_Node, Sub);
8508 -- Build wrapper procedure for pre/postconditions
8510 Build_PPC_Wrapper (Comp_Id, N);
8512 Set_Protected_Body_Subprogram
8513 (Defining_Identifier (Comp),
8514 Defining_Unit_Name (Specification (Sub)));
8516 Current_Node := Sub;
8519 Make_Defining_Identifier (Loc,
8520 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'B'));
8522 Make_Subprogram_Declaration (Loc,
8524 Build_Barrier_Function_Specification (Loc, Bdef));
8526 Insert_After (Current_Node, Sub);
8528 Set_Protected_Body_Subprogram (Bdef, Bdef);
8529 Set_Barrier_Function (Comp_Id, Bdef);
8530 Set_Scope (Bdef, Scope (Comp_Id));
8531 Current_Node := Sub;
8533 -- Collect pointers to the protected subprogram and the barrier
8534 -- of the current entry, for insertion into Entry_Bodies_Array.
8537 Make_Aggregate (Loc,
8538 Expressions => New_List (
8539 Make_Attribute_Reference (Loc,
8540 Prefix => New_Reference_To (Bdef, Loc),
8541 Attribute_Name => Name_Unrestricted_Access),
8542 Make_Attribute_Reference (Loc,
8543 Prefix => New_Reference_To (Edef, Loc),
8544 Attribute_Name => Name_Unrestricted_Access))),
8545 Expressions (Entries_Aggr));
8552 -- If there are some private entry declarations, expand it as if they
8553 -- were visible entries.
8555 if Present (Private_Declarations (Pdef)) then
8556 Comp := First (Private_Declarations (Pdef));
8557 while Present (Comp) loop
8558 if Nkind (Comp) = N_Entry_Declaration then
8559 E_Count := E_Count + 1;
8560 Comp_Id := Defining_Identifier (Comp);
8563 Make_Defining_Identifier (Loc,
8564 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
8566 Make_Subprogram_Declaration (Loc,
8568 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
8570 Insert_After (Current_Node, Sub);
8573 Set_Protected_Body_Subprogram
8574 (Defining_Identifier (Comp),
8575 Defining_Unit_Name (Specification (Sub)));
8577 Current_Node := Sub;
8580 Make_Defining_Identifier (Loc,
8581 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
8584 Make_Subprogram_Declaration (Loc,
8586 Build_Barrier_Function_Specification (Loc, Bdef));
8588 Insert_After (Current_Node, Sub);
8590 Set_Protected_Body_Subprogram (Bdef, Bdef);
8591 Set_Barrier_Function (Comp_Id, Bdef);
8592 Set_Scope (Bdef, Scope (Comp_Id));
8593 Current_Node := Sub;
8595 -- Collect pointers to the protected subprogram and the barrier
8596 -- of the current entry, for insertion into Entry_Bodies_Array.
8598 Append_To (Expressions (Entries_Aggr),
8599 Make_Aggregate (Loc,
8600 Expressions => New_List (
8601 Make_Attribute_Reference (Loc,
8602 Prefix => New_Reference_To (Bdef, Loc),
8603 Attribute_Name => Name_Unrestricted_Access),
8604 Make_Attribute_Reference (Loc,
8605 Prefix => New_Reference_To (Edef, Loc),
8606 Attribute_Name => Name_Unrestricted_Access))));
8613 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
8614 -- all protected subprograms have been collected.
8616 if Has_Entries (Prot_Typ) then
8618 Make_Defining_Identifier (Sloc (Prot_Typ),
8619 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
8621 case Corresponding_Runtime_Package (Prot_Typ) is
8622 when System_Tasking_Protected_Objects_Entries =>
8623 Body_Arr := Make_Object_Declaration (Loc,
8624 Defining_Identifier => Body_Id,
8625 Aliased_Present => True,
8626 Object_Definition =>
8627 Make_Subtype_Indication (Loc,
8628 Subtype_Mark => New_Reference_To (
8629 RTE (RE_Protected_Entry_Body_Array), Loc),
8631 Make_Index_Or_Discriminant_Constraint (Loc,
8632 Constraints => New_List (
8634 Make_Integer_Literal (Loc, 1),
8635 Make_Integer_Literal (Loc, E_Count))))),
8636 Expression => Entries_Aggr);
8638 when System_Tasking_Protected_Objects_Single_Entry =>
8639 Body_Arr := Make_Object_Declaration (Loc,
8640 Defining_Identifier => Body_Id,
8641 Aliased_Present => True,
8642 Object_Definition => New_Reference_To
8643 (RTE (RE_Entry_Body), Loc),
8645 Make_Aggregate (Loc,
8646 Expressions => New_List (
8647 Make_Attribute_Reference (Loc,
8648 Prefix => New_Reference_To (Bdef, Loc),
8649 Attribute_Name => Name_Unrestricted_Access),
8650 Make_Attribute_Reference (Loc,
8651 Prefix => New_Reference_To (Edef, Loc),
8652 Attribute_Name => Name_Unrestricted_Access))));
8655 raise Program_Error;
8658 -- A pointer to this array will be placed in the corresponding record
8659 -- by its initialization procedure so this needs to be analyzed here.
8661 Insert_After (Current_Node, Body_Arr);
8662 Current_Node := Body_Arr;
8665 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
8667 -- Finally, build the function that maps an entry index into the
8668 -- corresponding body. A pointer to this function is placed in each
8669 -- object of the type. Except for a ravenscar-like profile (no abort,
8670 -- no entry queue, 1 entry)
8672 if Corresponding_Runtime_Package (Prot_Typ) =
8673 System_Tasking_Protected_Objects_Entries
8676 Make_Subprogram_Declaration (Loc,
8677 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
8678 Insert_After (Current_Node, Sub);
8682 end Expand_N_Protected_Type_Declaration;
8684 --------------------------------
8685 -- Expand_N_Requeue_Statement --
8686 --------------------------------
8688 -- A non-dispatching requeue statement is expanded into one of four GNARLI
8689 -- operations, depending on the source and destination (task or protected
8690 -- object). A dispatching requeue statement is expanded into a call to the
8691 -- predefined primitive _Disp_Requeue. In addition, code is generated to
8692 -- jump around the remainder of processing for the original entry and, if
8693 -- the destination is (different) protected object, to attempt to service
8694 -- it. The following illustrates the various cases:
8697 -- (O : System.Address;
8698 -- P : System.Address;
8699 -- E : Protected_Entry_Index)
8701 -- <discriminant renamings>
8702 -- <private object renamings>
8703 -- type poVP is access poV;
8704 -- _object : ptVP := ptVP!(O);
8708 -- <start of statement sequence for entry>
8710 -- -- Requeue from one protected entry body to another protected
8713 -- Requeue_Protected_Entry (
8714 -- _object._object'Access,
8715 -- new._object'Access,
8720 -- <some more of the statement sequence for entry>
8722 -- -- Requeue from an entry body to a task entry
8724 -- Requeue_Protected_To_Task_Entry (
8730 -- <rest of statement sequence for entry>
8731 -- Complete_Entry_Body (_object._object);
8734 -- when all others =>
8735 -- Exceptional_Complete_Entry_Body (
8736 -- _object._object, Get_GNAT_Exception);
8740 -- Requeue of a task entry call to a task entry
8742 -- Accept_Call (E, Ann);
8743 -- <start of statement sequence for accept statement>
8744 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
8746 -- <rest of statement sequence for accept statement>
8748 -- Complete_Rendezvous;
8751 -- when all others =>
8752 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8754 -- Requeue of a task entry call to a protected entry
8756 -- Accept_Call (E, Ann);
8757 -- <start of statement sequence for accept statement>
8758 -- Requeue_Task_To_Protected_Entry (
8759 -- new._object'Access,
8764 -- <rest of statement sequence for accept statement>
8766 -- Complete_Rendezvous;
8769 -- when all others =>
8770 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8772 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
8773 -- marked by pragma Implemented (XXX, By_Entry).
8775 -- The requeue is inside a protected entry:
8778 -- (O : System.Address;
8779 -- P : System.Address;
8780 -- E : Protected_Entry_Index)
8782 -- <discriminant renamings>
8783 -- <private object renamings>
8784 -- type poVP is access poV;
8785 -- _object : ptVP := ptVP!(O);
8789 -- <start of statement sequence for entry>
8792 -- (<interface class-wide object>,
8795 -- Ada.Tags.Get_Offset_Index
8797 -- <interface dispatch table index of target entry>),
8801 -- <rest of statement sequence for entry>
8802 -- Complete_Entry_Body (_object._object);
8805 -- when all others =>
8806 -- Exceptional_Complete_Entry_Body (
8807 -- _object._object, Get_GNAT_Exception);
8811 -- The requeue is inside a task entry:
8813 -- Accept_Call (E, Ann);
8814 -- <start of statement sequence for accept statement>
8816 -- (<interface class-wide object>,
8819 -- Ada.Tags.Get_Offset_Index
8821 -- <interface dispatch table index of target entrt>),
8825 -- <rest of statement sequence for accept statement>
8827 -- Complete_Rendezvous;
8830 -- when all others =>
8831 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
8833 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
8834 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
8835 -- statement is replaced by a dispatching call with actual parameters taken
8836 -- from the inner-most accept statement or entry body.
8838 -- Target.Primitive (Param1, ..., ParamN);
8840 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
8841 -- marked by pragma Implemented (XXX, By_Any) or not marked at all.
8844 -- S : constant Offset_Index :=
8845 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
8846 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
8849 -- if C = POK_Protected_Entry
8850 -- or else C = POK_Task_Entry
8852 -- <statements for dispatching requeue>
8854 -- elsif C = POK_Protected_Procedure then
8855 -- <dispatching call equivalent>
8858 -- raise Program_Error;
8862 procedure Expand_N_Requeue_Statement (N : Node_Id) is
8863 Loc : constant Source_Ptr := Sloc (N);
8864 Conc_Typ : Entity_Id;
8868 Old_Typ : Entity_Id;
8870 function Build_Dispatching_Call_Equivalent return Node_Id;
8871 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
8872 -- the form Concval.Ename. It is statically known that Ename is allowed
8873 -- to be implemented by a protected procedure. Create a dispatching call
8874 -- equivalent of Concval.Ename taking the actual parameters from the
8875 -- inner-most accept statement or entry body.
8877 function Build_Dispatching_Requeue return Node_Id;
8878 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
8879 -- the form Concval.Ename. It is statically known that Ename is allowed
8880 -- to be implemented by a protected or a task entry. Create a call to
8881 -- primitive _Disp_Requeue which handles the low-level actions.
8883 function Build_Dispatching_Requeue_To_Any return Node_Id;
8884 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
8885 -- the form Concval.Ename. Ename is either marked by pragma Implemented
8886 -- (XXX, By_Any) or not marked at all. Create a block which determines
8887 -- at runtime whether Ename denotes an entry or a procedure and perform
8888 -- the appropriate kind of dispatching select.
8890 function Build_Normal_Requeue return Node_Id;
8891 -- N denotes a non-dispatching requeue statement to either a task or a
8892 -- protected entry. Build the appropriate runtime call to perform the
8895 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
8896 -- For a protected entry, create a return statement to skip the rest of
8897 -- the entry body. Otherwise, create a goto statement to skip the rest
8898 -- of a task accept statement. The lookup for the enclosing entry body
8899 -- or accept statement starts from Search.
8901 ---------------------------------------
8902 -- Build_Dispatching_Call_Equivalent --
8903 ---------------------------------------
8905 function Build_Dispatching_Call_Equivalent return Node_Id is
8906 Call_Ent : constant Entity_Id := Entity (Ename);
8907 Obj : constant Node_Id := Original_Node (Concval);
8914 -- Climb the parent chain looking for the inner-most entry body or
8915 -- accept statement.
8918 while Present (Acc_Ent)
8919 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
8922 Acc_Ent := Parent (Acc_Ent);
8925 -- A requeue statement should be housed inside an entry body or an
8926 -- accept statement at some level. If this is not the case, then the
8927 -- tree is malformed.
8929 pragma Assert (Present (Acc_Ent));
8931 -- Recover the list of formal parameters
8933 if Nkind (Acc_Ent) = N_Entry_Body then
8934 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
8937 Formals := Parameter_Specifications (Acc_Ent);
8939 -- Create the actual parameters for the dispatching call. These are
8940 -- simply copies of the entry body or accept statement formals in the
8941 -- same order as they appear.
8945 if Present (Formals) then
8946 Actuals := New_List;
8947 Formal := First (Formals);
8948 while Present (Formal) loop
8950 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8956 -- Obj.Call_Ent (Actuals);
8959 Make_Procedure_Call_Statement (Loc,
8961 Make_Selected_Component (Loc,
8962 Prefix => Make_Identifier (Loc, Chars (Obj)),
8963 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
8965 Parameter_Associations => Actuals);
8966 end Build_Dispatching_Call_Equivalent;
8968 -------------------------------
8969 -- Build_Dispatching_Requeue --
8970 -------------------------------
8972 function Build_Dispatching_Requeue return Node_Id is
8973 Params : constant List_Id := New_List;
8976 -- Process the "with abort" parameter
8979 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
8981 -- Process the entry wrapper's position in the primary dispatch
8982 -- table parameter. Generate:
8984 -- Ada.Tags.Get_Offset_Index
8985 -- (Ada.Tags.Tag (Concval),
8986 -- <interface dispatch table position of Ename>)
8988 if Tagged_Type_Expansion then
8990 Make_Function_Call (Loc,
8991 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
8992 Parameter_Associations => New_List (
8993 Unchecked_Convert_To (RTE (RE_Tag), Concval),
8994 Make_Integer_Literal (Loc, DT_Position (Entity (Ename))))));
9000 Make_Function_Call (Loc,
9001 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9003 Parameter_Associations => New_List (
9007 Make_Attribute_Reference (Loc,
9009 Attribute_Name => Name_Tag),
9013 Make_Attribute_Reference (Loc,
9014 Prefix => New_Reference_To (Etype (Concval), Loc),
9015 Attribute_Name => Name_Tag),
9019 Make_Integer_Literal (Loc, DT_Position (Entity (Ename))))));
9022 -- Specific actuals for protected to XXX requeue
9024 if Is_Protected_Type (Old_Typ) then
9026 Make_Attribute_Reference (Loc, -- _object'Address
9028 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9029 Attribute_Name => Name_Address));
9031 Prepend_To (Params, -- True
9032 New_Reference_To (Standard_True, Loc));
9034 -- Specific actuals for task to XXX requeue
9037 pragma Assert (Is_Task_Type (Old_Typ));
9039 Prepend_To (Params, -- null
9040 New_Reference_To (RTE (RE_Null_Address), Loc));
9042 Prepend_To (Params, -- False
9043 New_Reference_To (Standard_False, Loc));
9046 -- Add the object parameter
9048 Prepend_To (Params, New_Copy_Tree (Concval));
9051 -- _Disp_Requeue (<Params>);
9054 Make_Procedure_Call_Statement (Loc,
9055 Name => Make_Identifier (Loc, Name_uDisp_Requeue),
9056 Parameter_Associations => Params);
9057 end Build_Dispatching_Requeue;
9059 --------------------------------------
9060 -- Build_Dispatching_Requeue_To_Any --
9061 --------------------------------------
9063 function Build_Dispatching_Requeue_To_Any return Node_Id is
9064 Call_Ent : constant Entity_Id := Entity (Ename);
9065 Obj : constant Node_Id := Original_Node (Concval);
9066 Skip : constant Node_Id := Build_Skip_Statement (N);
9076 -- Dispatch table slot processing, generate:
9079 S := Build_S (Loc, Decls);
9081 -- Call kind processing, generate:
9082 -- C : Ada.Tags.Prim_Op_Kind;
9084 C := Build_C (Loc, Decls);
9087 -- S := Ada.Tags.Get_Offset_Index
9088 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
9090 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
9093 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
9096 Make_Procedure_Call_Statement (Loc,
9099 Find_Prim_Op (Etype (Etype (Obj)),
9100 Name_uDisp_Get_Prim_Op_Kind),
9102 Parameter_Associations => New_List (
9103 New_Copy_Tree (Obj),
9104 New_Reference_To (S, Loc),
9105 New_Reference_To (C, Loc))));
9109 -- if C = POK_Protected_Entry
9110 -- or else C = POK_Task_Entry
9113 Make_If_Statement (Loc,
9119 New_Reference_To (C, Loc),
9121 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
9126 New_Reference_To (C, Loc),
9128 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
9130 -- Dispatching requeue equivalent
9132 Then_Statements => New_List (
9133 Build_Dispatching_Requeue,
9136 -- elsif C = POK_Protected_Procedure then
9138 Elsif_Parts => New_List (
9139 Make_Elsif_Part (Loc,
9143 New_Reference_To (C, Loc),
9146 RTE (RE_POK_Protected_Procedure), Loc)),
9148 -- Dispatching call equivalent
9150 Then_Statements => New_List (
9151 Build_Dispatching_Call_Equivalent))),
9154 -- raise Program_Error;
9157 Else_Statements => New_List (
9158 Make_Raise_Program_Error (Loc,
9159 Reason => PE_Explicit_Raise))));
9161 -- Wrap everything into a block
9164 Make_Block_Statement (Loc,
9165 Declarations => Decls,
9166 Handled_Statement_Sequence =>
9167 Make_Handled_Sequence_Of_Statements (Loc,
9168 Statements => Stmts));
9169 end Build_Dispatching_Requeue_To_Any;
9171 --------------------------
9172 -- Build_Normal_Requeue --
9173 --------------------------
9175 function Build_Normal_Requeue return Node_Id is
9176 Params : constant List_Id := New_List;
9181 -- Process the "with abort" parameter
9184 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9186 -- Add the index expression to the parameters. It is common among all
9190 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
9192 if Is_Protected_Type (Old_Typ) then
9194 Self_Param : Node_Id;
9198 Make_Attribute_Reference (Loc,
9200 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9202 Name_Unchecked_Access);
9204 -- Protected to protected requeue
9206 if Is_Protected_Type (Conc_Typ) then
9209 RTE (RE_Requeue_Protected_Entry), Loc);
9212 Make_Attribute_Reference (Loc,
9214 Concurrent_Ref (Concval),
9216 Name_Unchecked_Access);
9218 -- Protected to task requeue
9220 else pragma Assert (Is_Task_Type (Conc_Typ));
9223 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
9225 Param := Concurrent_Ref (Concval);
9228 Prepend_To (Params, Param);
9229 Prepend_To (Params, Self_Param);
9232 else pragma Assert (Is_Task_Type (Old_Typ));
9234 -- Task to protected requeue
9236 if Is_Protected_Type (Conc_Typ) then
9239 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
9242 Make_Attribute_Reference (Loc,
9244 Concurrent_Ref (Concval),
9246 Name_Unchecked_Access);
9248 -- Task to task requeue
9250 else pragma Assert (Is_Task_Type (Conc_Typ));
9252 New_Reference_To (RTE (RE_Requeue_Task_Entry), Loc);
9254 Param := Concurrent_Ref (Concval);
9257 Prepend_To (Params, Param);
9261 Make_Procedure_Call_Statement (Loc,
9263 Parameter_Associations => Params);
9264 end Build_Normal_Requeue;
9266 --------------------------
9267 -- Build_Skip_Statement --
9268 --------------------------
9270 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
9271 Skip_Stmt : Node_Id;
9274 -- Build a return statement to skip the rest of the entire body
9276 if Is_Protected_Type (Old_Typ) then
9277 Skip_Stmt := Make_Simple_Return_Statement (Loc);
9279 -- If the requeue is within a task, find the end label of the
9280 -- enclosing accept statement and create a goto statement to it.
9288 -- Climb the parent chain looking for the enclosing accept
9291 Acc := Parent (Search);
9293 and then Nkind (Acc) /= N_Accept_Statement
9295 Acc := Parent (Acc);
9298 -- The last statement is the second label used for completing
9299 -- the rendezvous the usual way. The label we are looking for
9300 -- is right before it.
9303 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
9305 pragma Assert (Nkind (Label) = N_Label);
9307 -- Generate a goto statement to skip the rest of the accept
9310 Make_Goto_Statement (Loc,
9312 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
9316 Set_Analyzed (Skip_Stmt);
9319 end Build_Skip_Statement;
9321 -- Start of processing for Expand_N_Requeue_Statement
9324 -- Extract the components of the entry call
9326 Extract_Entry (N, Concval, Ename, Index);
9327 Conc_Typ := Etype (Concval);
9329 -- Examine the scope stack in order to find nearest enclosing protected
9330 -- or task type. This will constitute our invocation source.
9332 Old_Typ := Current_Scope;
9333 while Present (Old_Typ)
9334 and then not Is_Protected_Type (Old_Typ)
9335 and then not Is_Task_Type (Old_Typ)
9337 Old_Typ := Scope (Old_Typ);
9340 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
9341 -- Concval.Ename where the type of Concval is class-wide concurrent
9344 if Ada_Version >= Ada_2012
9345 and then Present (Concval)
9346 and then Is_Class_Wide_Type (Conc_Typ)
9347 and then Is_Concurrent_Interface (Conc_Typ)
9350 Has_Impl : Boolean := False;
9351 Impl_Kind : Name_Id := No_Name;
9354 -- Check whether the Ename is flagged by pragma Implemented
9356 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
9358 Impl_Kind := Implementation_Kind (Entity (Ename));
9361 -- The procedure_or_entry_NAME is guaranteed to be overridden by
9362 -- an entry. Create a call to predefined primitive _Disp_Requeue.
9365 and then Impl_Kind = Name_By_Entry
9367 Rewrite (N, Build_Dispatching_Requeue);
9369 Insert_After (N, Build_Skip_Statement (N));
9371 -- The procedure_or_entry_NAME is guaranteed to be overridden by
9372 -- a protected procedure. In this case the requeue is transformed
9373 -- into a dispatching call.
9376 and then Impl_Kind = Name_By_Protected_Procedure
9378 Rewrite (N, Build_Dispatching_Call_Equivalent);
9381 -- The procedure_or_entry_NAME's implementation kind is either
9382 -- By_Any or pragma Implemented was not applied at all. In this
9383 -- case a runtime test determines whether Ename denotes an entry
9384 -- or a protected procedure and performs the appropriate call.
9387 Rewrite (N, Build_Dispatching_Requeue_To_Any);
9392 -- Processing for regular (non-dispatching) requeues
9395 Rewrite (N, Build_Normal_Requeue);
9397 Insert_After (N, Build_Skip_Statement (N));
9399 end Expand_N_Requeue_Statement;
9401 -------------------------------
9402 -- Expand_N_Selective_Accept --
9403 -------------------------------
9405 procedure Expand_N_Selective_Accept (N : Node_Id) is
9406 Loc : constant Source_Ptr := Sloc (N);
9407 Alts : constant List_Id := Select_Alternatives (N);
9409 -- Note: in the below declarations a lot of new lists are allocated
9410 -- unconditionally which may well not end up being used. That's
9411 -- not a good idea since it wastes space gratuitously ???
9413 Accept_Case : List_Id;
9414 Accept_List : constant List_Id := New_List;
9417 Alt_List : constant List_Id := New_List;
9418 Alt_Stats : List_Id;
9419 Ann : Entity_Id := Empty;
9422 Check_Guard : Boolean := True;
9424 Decls : constant List_Id := New_List;
9425 Stats : constant List_Id := New_List;
9426 Body_List : constant List_Id := New_List;
9427 Trailing_List : constant List_Id := New_List;
9430 Else_Present : Boolean := False;
9431 Terminate_Alt : Node_Id := Empty;
9432 Select_Mode : Node_Id;
9434 Delay_Case : List_Id;
9435 Delay_Count : Integer := 0;
9436 Delay_Val : Entity_Id;
9437 Delay_Index : Entity_Id;
9438 Delay_Min : Entity_Id;
9439 Delay_Num : Int := 1;
9440 Delay_Alt_List : List_Id := New_List;
9441 Delay_List : constant List_Id := New_List;
9445 First_Delay : Boolean := True;
9446 Guard_Open : Entity_Id;
9452 Num_Accept : Nat := 0;
9455 Time_Type : Entity_Id;
9457 Select_Call : Node_Id;
9459 Qnam : constant Entity_Id :=
9460 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
9462 Xnam : constant Entity_Id :=
9463 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
9465 -----------------------
9466 -- Local subprograms --
9467 -----------------------
9469 function Accept_Or_Raise return List_Id;
9470 -- For the rare case where delay alternatives all have guards, and
9471 -- all of them are closed, it is still possible that there were open
9472 -- accept alternatives with no callers. We must reexamine the
9473 -- Accept_List, and execute a selective wait with no else if some
9474 -- accept is open. If none, we raise program_error.
9476 procedure Add_Accept (Alt : Node_Id);
9477 -- Process a single accept statement in a select alternative. Build
9478 -- procedure for body of accept, and add entry to dispatch table with
9479 -- expression for guard, in preparation for call to run time select.
9481 function Make_And_Declare_Label (Num : Int) return Node_Id;
9482 -- Manufacture a label using Num as a serial number and declare it.
9483 -- The declaration is appended to Decls. The label marks the trailing
9484 -- statements of an accept or delay alternative.
9486 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
9487 -- Build call to Selective_Wait runtime routine
9489 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
9490 -- Add code to compare value of delay with previous values, and
9491 -- generate case entry for trailing statements.
9493 procedure Process_Accept_Alternative
9497 -- Add code to call corresponding procedure, and branch to
9498 -- trailing statements, if any.
9500 ---------------------
9501 -- Accept_Or_Raise --
9502 ---------------------
9504 function Accept_Or_Raise return List_Id is
9507 J : constant Entity_Id := Make_Temporary (Loc, 'J');
9510 -- We generate the following:
9512 -- for J in q'range loop
9513 -- if q(J).S /=null_task_entry then
9514 -- selective_wait (simple_mode,...);
9520 -- if no rendez_vous then
9521 -- raise program_error;
9524 -- Note that the code needs to know that the selector name
9525 -- in an Accept_Alternative is named S.
9527 Cond := Make_Op_Ne (Loc,
9529 Make_Selected_Component (Loc,
9531 Make_Indexed_Component (Loc,
9532 Prefix => New_Reference_To (Qnam, Loc),
9533 Expressions => New_List (New_Reference_To (J, Loc))),
9534 Selector_Name => Make_Identifier (Loc, Name_S)),
9536 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
9539 Make_Implicit_Loop_Statement (N,
9540 Identifier => Empty,
9542 Make_Iteration_Scheme (Loc,
9543 Loop_Parameter_Specification =>
9544 Make_Loop_Parameter_Specification (Loc,
9545 Defining_Identifier => J,
9546 Discrete_Subtype_Definition =>
9547 Make_Attribute_Reference (Loc,
9548 Prefix => New_Reference_To (Qnam, Loc),
9549 Attribute_Name => Name_Range,
9550 Expressions => New_List (
9551 Make_Integer_Literal (Loc, 1))))),
9553 Statements => New_List (
9554 Make_Implicit_If_Statement (N,
9556 Then_Statements => New_List (
9558 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
9559 Make_Exit_Statement (Loc))))));
9562 Make_Raise_Program_Error (Loc,
9563 Condition => Make_Op_Eq (Loc,
9564 Left_Opnd => New_Reference_To (Xnam, Loc),
9566 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
9567 Reason => PE_All_Guards_Closed));
9570 end Accept_Or_Raise;
9576 procedure Add_Accept (Alt : Node_Id) is
9577 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
9578 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
9579 Eloc : constant Source_Ptr := Sloc (Ename);
9580 Eent : constant Entity_Id := Entity (Ename);
9581 Index : constant Node_Id := Entry_Index (Acc_Stm);
9582 Null_Body : Node_Id;
9583 Proc_Body : Node_Id;
9590 Ann := Node (Last_Elmt (Accept_Address (Eent)));
9593 if Present (Condition (Alt)) then
9595 Make_Conditional_Expression (Eloc, New_List (
9597 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
9598 New_Reference_To (RTE (RE_Null_Task_Entry), Eloc)));
9601 Entry_Index_Expression
9602 (Eloc, Eent, Index, Scope (Eent));
9605 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
9606 Null_Body := New_Reference_To (Standard_False, Eloc);
9608 if Abort_Allowed then
9609 Call := Make_Procedure_Call_Statement (Eloc,
9610 Name => New_Reference_To (RTE (RE_Abort_Undefer), Eloc));
9611 Insert_Before (First (Statements (Handled_Statement_Sequence (
9612 Accept_Statement (Alt)))), Call);
9617 Make_Defining_Identifier (Eloc,
9618 New_External_Name (Chars (Ename), 'A', Num_Accept));
9620 if Comes_From_Source (Alt) then
9621 Set_Debug_Info_Needed (PB_Ent);
9625 Make_Subprogram_Body (Eloc,
9627 Make_Procedure_Specification (Eloc,
9628 Defining_Unit_Name => PB_Ent),
9629 Declarations => Declarations (Acc_Stm),
9630 Handled_Statement_Sequence =>
9631 Build_Accept_Body (Accept_Statement (Alt)));
9633 -- During the analysis of the body of the accept statement, any
9634 -- zero cost exception handler records were collected in the
9635 -- Accept_Handler_Records field of the N_Accept_Alternative node.
9636 -- This is where we move them to where they belong, namely the
9637 -- newly created procedure.
9639 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
9640 Append (Proc_Body, Body_List);
9643 Null_Body := New_Reference_To (Standard_True, Eloc);
9645 -- if accept statement has declarations, insert above, given that
9646 -- we are not creating a body for the accept.
9648 if Present (Declarations (Acc_Stm)) then
9649 Insert_Actions (N, Declarations (Acc_Stm));
9653 Append_To (Accept_List,
9654 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
9656 Num_Accept := Num_Accept + 1;
9659 ----------------------------
9660 -- Make_And_Declare_Label --
9661 ----------------------------
9663 function Make_And_Declare_Label (Num : Int) return Node_Id is
9667 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
9669 Make_Label (Loc, Lab_Id);
9672 Make_Implicit_Label_Declaration (Loc,
9673 Defining_Identifier =>
9674 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
9675 Label_Construct => Lab));
9678 end Make_And_Declare_Label;
9680 ----------------------
9681 -- Make_Select_Call --
9682 ----------------------
9684 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
9685 Params : constant List_Id := New_List;
9689 Make_Attribute_Reference (Loc,
9690 Prefix => New_Reference_To (Qnam, Loc),
9691 Attribute_Name => Name_Unchecked_Access),
9693 Append (Select_Mode, Params);
9694 Append (New_Reference_To (Ann, Loc), Params);
9695 Append (New_Reference_To (Xnam, Loc), Params);
9698 Make_Procedure_Call_Statement (Loc,
9699 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
9700 Parameter_Associations => Params);
9701 end Make_Select_Call;
9703 --------------------------------
9704 -- Process_Accept_Alternative --
9705 --------------------------------
9707 procedure Process_Accept_Alternative
9712 Choices : List_Id := No_List;
9713 Alt_Stats : List_Id;
9716 Adjust_Condition (Condition (Alt));
9717 Alt_Stats := No_List;
9719 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
9720 Choices := New_List (
9721 Make_Integer_Literal (Loc, Index));
9723 Alt_Stats := New_List (
9724 Make_Procedure_Call_Statement (Sloc (Proc),
9725 Name => New_Reference_To (
9726 Defining_Unit_Name (Specification (Proc)), Sloc (Proc))));
9729 if Statements (Alt) /= Empty_List then
9731 if No (Alt_Stats) then
9733 -- Accept with no body, followed by trailing statements
9735 Choices := New_List (
9736 Make_Integer_Literal (Loc, Index));
9738 Alt_Stats := New_List;
9741 -- After the call, if any, branch to trailing statements. We
9742 -- create a label for each, as well as the corresponding label
9745 Lab := Make_And_Declare_Label (Index);
9746 Append_To (Alt_Stats,
9747 Make_Goto_Statement (Loc,
9748 Name => New_Copy (Identifier (Lab))));
9750 Append (Lab, Trailing_List);
9751 Append_List (Statements (Alt), Trailing_List);
9752 Append_To (Trailing_List,
9753 Make_Goto_Statement (Loc,
9754 Name => New_Copy (Identifier (End_Lab))));
9757 if Present (Alt_Stats) then
9759 -- Procedure call. and/or trailing statements
9761 Append_To (Alt_List,
9762 Make_Case_Statement_Alternative (Loc,
9763 Discrete_Choices => Choices,
9764 Statements => Alt_Stats));
9766 end Process_Accept_Alternative;
9768 -------------------------------
9769 -- Process_Delay_Alternative --
9770 -------------------------------
9772 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
9775 Delay_Alt : List_Id;
9778 -- Deal with C/Fortran boolean as delay condition
9780 Adjust_Condition (Condition (Alt));
9782 -- Determine the smallest specified delay
9784 -- for each delay alternative generate:
9786 -- if guard-expression then
9787 -- Delay_Val := delay-expression;
9788 -- Guard_Open := True;
9789 -- if Delay_Val < Delay_Min then
9790 -- Delay_Min := Delay_Val;
9791 -- Delay_Index := Index;
9795 -- The enclosing if-statement is omitted if there is no guard
9800 First_Delay := False;
9802 Delay_Alt := New_List (
9803 Make_Assignment_Statement (Loc,
9804 Name => New_Reference_To (Delay_Min, Loc),
9805 Expression => Expression (Delay_Statement (Alt))));
9807 if Delay_Count > 1 then
9808 Append_To (Delay_Alt,
9809 Make_Assignment_Statement (Loc,
9810 Name => New_Reference_To (Delay_Index, Loc),
9811 Expression => Make_Integer_Literal (Loc, Index)));
9815 Delay_Alt := New_List (
9816 Make_Assignment_Statement (Loc,
9817 Name => New_Reference_To (Delay_Val, Loc),
9818 Expression => Expression (Delay_Statement (Alt))));
9820 if Time_Type = Standard_Duration then
9823 Left_Opnd => New_Reference_To (Delay_Val, Loc),
9824 Right_Opnd => New_Reference_To (Delay_Min, Loc));
9827 -- The scope of the time type must define a comparison
9828 -- operator. The scope itself may not be visible, so we
9829 -- construct a node with entity information to insure that
9830 -- semantic analysis can find the proper operator.
9833 Make_Function_Call (Loc,
9834 Name => Make_Selected_Component (Loc,
9835 Prefix => New_Reference_To (Scope (Time_Type), Loc),
9837 Make_Operator_Symbol (Loc,
9838 Chars => Name_Op_Lt,
9839 Strval => No_String)),
9840 Parameter_Associations =>
9842 New_Reference_To (Delay_Val, Loc),
9843 New_Reference_To (Delay_Min, Loc)));
9845 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
9848 Append_To (Delay_Alt,
9849 Make_Implicit_If_Statement (N,
9851 Then_Statements => New_List (
9852 Make_Assignment_Statement (Loc,
9853 Name => New_Reference_To (Delay_Min, Loc),
9854 Expression => New_Reference_To (Delay_Val, Loc)),
9856 Make_Assignment_Statement (Loc,
9857 Name => New_Reference_To (Delay_Index, Loc),
9858 Expression => Make_Integer_Literal (Loc, Index)))));
9862 Append_To (Delay_Alt,
9863 Make_Assignment_Statement (Loc,
9864 Name => New_Reference_To (Guard_Open, Loc),
9865 Expression => New_Reference_To (Standard_True, Loc)));
9868 if Present (Condition (Alt)) then
9869 Delay_Alt := New_List (
9870 Make_Implicit_If_Statement (N,
9871 Condition => Condition (Alt),
9872 Then_Statements => Delay_Alt));
9875 Append_List (Delay_Alt, Delay_List);
9877 -- If the delay alternative has a statement part, add choice to the
9878 -- case statements for delays.
9880 if Present (Statements (Alt)) then
9882 if Delay_Count = 1 then
9883 Append_List (Statements (Alt), Delay_Alt_List);
9886 Choices := New_List (
9887 Make_Integer_Literal (Loc, Index));
9889 Append_To (Delay_Alt_List,
9890 Make_Case_Statement_Alternative (Loc,
9891 Discrete_Choices => Choices,
9892 Statements => Statements (Alt)));
9895 elsif Delay_Count = 1 then
9897 -- If the single delay has no trailing statements, add a branch
9898 -- to the exit label to the selective wait.
9900 Delay_Alt_List := New_List (
9901 Make_Goto_Statement (Loc,
9902 Name => New_Copy (Identifier (End_Lab))));
9905 end Process_Delay_Alternative;
9907 -- Start of processing for Expand_N_Selective_Accept
9910 Process_Statements_For_Controlled_Objects (N);
9912 -- First insert some declarations before the select. The first is:
9916 -- This variable holds the parameters passed to the accept body. This
9917 -- declaration has already been inserted by the time we get here by
9918 -- a call to Expand_Accept_Declarations made from the semantics when
9919 -- processing the first accept statement contained in the select. We
9920 -- can find this entity as Accept_Address (E), where E is any of the
9921 -- entries references by contained accept statements.
9923 -- The first step is to scan the list of Selective_Accept_Statements
9924 -- to find this entity, and also count the number of accepts, and
9925 -- determine if terminated, delay or else is present:
9929 Alt := First (Alts);
9930 while Present (Alt) loop
9931 Process_Statements_For_Controlled_Objects (Alt);
9933 if Nkind (Alt) = N_Accept_Alternative then
9936 elsif Nkind (Alt) = N_Delay_Alternative then
9937 Delay_Count := Delay_Count + 1;
9939 -- If the delays are relative delays, the delay expressions have
9940 -- type Standard_Duration. Otherwise they must have some time type
9941 -- recognized by GNAT.
9943 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
9944 Time_Type := Standard_Duration;
9946 Time_Type := Etype (Expression (Delay_Statement (Alt)));
9948 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
9949 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
9954 "& is not a time type (RM 9.6(6))",
9955 Expression (Delay_Statement (Alt)), Time_Type);
9956 Time_Type := Standard_Duration;
9957 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
9961 if No (Condition (Alt)) then
9963 -- This guard will always be open
9965 Check_Guard := False;
9968 elsif Nkind (Alt) = N_Terminate_Alternative then
9969 Adjust_Condition (Condition (Alt));
9970 Terminate_Alt := Alt;
9973 Num_Alts := Num_Alts + 1;
9977 Else_Present := Present (Else_Statements (N));
9979 -- At the same time (see procedure Add_Accept) we build the accept list:
9981 -- Qnn : Accept_List (1 .. num-select) := (
9982 -- (null-body, entry-index),
9983 -- (null-body, entry-index),
9985 -- (null_body, entry-index));
9987 -- In the above declaration, null-body is True if the corresponding
9988 -- accept has no body, and false otherwise. The entry is either the
9989 -- entry index expression if there is no guard, or if a guard is
9990 -- present, then a conditional expression of the form:
9992 -- (if guard then entry-index else Null_Task_Entry)
9994 -- If a guard is statically known to be false, the entry can simply
9995 -- be omitted from the accept list.
9998 Make_Object_Declaration (Loc,
9999 Defining_Identifier => Qnam,
10000 Object_Definition =>
10001 New_Reference_To (RTE (RE_Accept_List), Loc),
10002 Aliased_Present => True,
10005 Make_Qualified_Expression (Loc,
10007 New_Reference_To (RTE (RE_Accept_List), Loc),
10009 Make_Aggregate (Loc, Expressions => Accept_List)));
10013 -- Then we declare the variable that holds the index for the accept
10014 -- that will be selected for service:
10016 -- Xnn : Select_Index;
10019 Make_Object_Declaration (Loc,
10020 Defining_Identifier => Xnam,
10021 Object_Definition =>
10022 New_Reference_To (RTE (RE_Select_Index), Loc),
10024 New_Reference_To (RTE (RE_No_Rendezvous), Loc));
10028 -- After this follow procedure declarations for each accept body
10030 -- procedure Pnn is
10035 -- where the ... are statements from the corresponding procedure body.
10036 -- No parameters are involved, since the parameters are passed via Ann
10037 -- and the parameter references have already been expanded to be direct
10038 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10039 -- any embedded tasking statements (which would normally be illegal in
10040 -- procedures), have been converted to calls to the tasking runtime so
10041 -- there is no problem in putting them into procedures.
10043 -- The original accept statement has been expanded into a block in
10044 -- the same fashion as for simple accepts (see Build_Accept_Body).
10046 -- Note: we don't really need to build these procedures for the case
10047 -- where no delay statement is present, but it is just as easy to
10048 -- build them unconditionally, and not significantly inefficient,
10049 -- since if they are short they will be inlined anyway.
10051 -- The procedure declarations have been assembled in Body_List
10053 -- If delays are present, we must compute the required delay.
10054 -- We first generate the declarations:
10056 -- Delay_Index : Boolean := 0;
10057 -- Delay_Min : Some_Time_Type.Time;
10058 -- Delay_Val : Some_Time_Type.Time;
10060 -- Delay_Index will be set to the index of the minimum delay, i.e. the
10061 -- active delay that is actually chosen as the basis for the possible
10062 -- delay if an immediate rendez-vous is not possible.
10064 -- In the most common case there is a single delay statement, and this
10065 -- is handled specially.
10067 if Delay_Count > 0 then
10069 -- Generate the required declarations
10072 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
10074 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
10076 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
10079 Make_Object_Declaration (Loc,
10080 Defining_Identifier => Delay_Val,
10081 Object_Definition => New_Reference_To (Time_Type, Loc)));
10084 Make_Object_Declaration (Loc,
10085 Defining_Identifier => Delay_Index,
10086 Object_Definition => New_Reference_To (Standard_Integer, Loc),
10087 Expression => Make_Integer_Literal (Loc, 0)));
10090 Make_Object_Declaration (Loc,
10091 Defining_Identifier => Delay_Min,
10092 Object_Definition => New_Reference_To (Time_Type, Loc),
10094 Unchecked_Convert_To (Time_Type,
10095 Make_Attribute_Reference (Loc,
10097 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
10098 Attribute_Name => Name_Last))));
10100 -- Create Duration and Delay_Mode objects used for passing a delay
10103 D := Make_Temporary (Loc, 'D');
10104 M := Make_Temporary (Loc, 'M');
10110 -- Note that these values are defined in s-osprim.ads and must
10111 -- be kept in sync:
10113 -- Relative : constant := 0;
10114 -- Absolute_Calendar : constant := 1;
10115 -- Absolute_RT : constant := 2;
10117 if Time_Type = Standard_Duration then
10118 Discr := Make_Integer_Literal (Loc, 0);
10120 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10121 Discr := Make_Integer_Literal (Loc, 1);
10125 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
10126 Discr := Make_Integer_Literal (Loc, 2);
10130 Make_Object_Declaration (Loc,
10131 Defining_Identifier => D,
10132 Object_Definition =>
10133 New_Reference_To (Standard_Duration, Loc)));
10136 Make_Object_Declaration (Loc,
10137 Defining_Identifier => M,
10138 Object_Definition =>
10139 New_Reference_To (Standard_Integer, Loc),
10140 Expression => Discr));
10143 if Check_Guard then
10145 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
10148 Make_Object_Declaration (Loc,
10149 Defining_Identifier => Guard_Open,
10150 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
10151 Expression => New_Reference_To (Standard_False, Loc)));
10154 -- Delay_Count is zero, don't need M and D set (suppress warning)
10161 if Present (Terminate_Alt) then
10163 -- If the terminate alternative guard is False, use
10164 -- Simple_Mode; otherwise use Terminate_Mode.
10166 if Present (Condition (Terminate_Alt)) then
10167 Select_Mode := Make_Conditional_Expression (Loc,
10168 New_List (Condition (Terminate_Alt),
10169 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
10170 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
10172 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
10175 elsif Else_Present or Delay_Count > 0 then
10176 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
10179 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
10182 Select_Call := Make_Select_Call (Select_Mode);
10183 Append (Select_Call, Stats);
10185 -- Now generate code to act on the result. There is an entry
10186 -- in this case for each accept statement with a non-null body,
10187 -- followed by a branch to the statements that follow the Accept.
10188 -- In the absence of delay alternatives, we generate:
10191 -- when No_Rendezvous => -- omitted if simple mode
10206 -- Lab0: Else_Statements;
10209 -- Lab1: Trailing_Statements1;
10212 -- Lab2: Trailing_Statements2;
10217 -- Generate label for common exit
10219 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
10221 -- First entry is the default case, when no rendezvous is possible
10223 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
10225 if Else_Present then
10227 -- If no rendezvous is possible, the else part is executed
10229 Lab := Make_And_Declare_Label (0);
10230 Alt_Stats := New_List (
10231 Make_Goto_Statement (Loc,
10232 Name => New_Copy (Identifier (Lab))));
10234 Append (Lab, Trailing_List);
10235 Append_List (Else_Statements (N), Trailing_List);
10236 Append_To (Trailing_List,
10237 Make_Goto_Statement (Loc,
10238 Name => New_Copy (Identifier (End_Lab))));
10240 Alt_Stats := New_List (
10241 Make_Goto_Statement (Loc,
10242 Name => New_Copy (Identifier (End_Lab))));
10245 Append_To (Alt_List,
10246 Make_Case_Statement_Alternative (Loc,
10247 Discrete_Choices => Choices,
10248 Statements => Alt_Stats));
10250 -- We make use of the fact that Accept_Index is an integer type, and
10251 -- generate successive literals for entries for each accept. Only those
10252 -- for which there is a body or trailing statements get a case entry.
10254 Alt := First (Select_Alternatives (N));
10255 Proc := First (Body_List);
10256 while Present (Alt) loop
10258 if Nkind (Alt) = N_Accept_Alternative then
10259 Process_Accept_Alternative (Alt, Index, Proc);
10260 Index := Index + 1;
10263 (Handled_Statement_Sequence (Accept_Statement (Alt)))
10268 elsif Nkind (Alt) = N_Delay_Alternative then
10269 Process_Delay_Alternative (Alt, Delay_Num);
10270 Delay_Num := Delay_Num + 1;
10276 -- An others choice is always added to the main case, as well
10277 -- as the delay case (to satisfy the compiler).
10279 Append_To (Alt_List,
10280 Make_Case_Statement_Alternative (Loc,
10281 Discrete_Choices =>
10282 New_List (Make_Others_Choice (Loc)),
10284 New_List (Make_Goto_Statement (Loc,
10285 Name => New_Copy (Identifier (End_Lab))))));
10287 Accept_Case := New_List (
10288 Make_Case_Statement (Loc,
10289 Expression => New_Reference_To (Xnam, Loc),
10290 Alternatives => Alt_List));
10292 Append_List (Trailing_List, Accept_Case);
10293 Append (End_Lab, Accept_Case);
10294 Append_List (Body_List, Decls);
10296 -- Construct case statement for trailing statements of delay
10297 -- alternatives, if there are several of them.
10299 if Delay_Count > 1 then
10300 Append_To (Delay_Alt_List,
10301 Make_Case_Statement_Alternative (Loc,
10302 Discrete_Choices =>
10303 New_List (Make_Others_Choice (Loc)),
10305 New_List (Make_Null_Statement (Loc))));
10307 Delay_Case := New_List (
10308 Make_Case_Statement (Loc,
10309 Expression => New_Reference_To (Delay_Index, Loc),
10310 Alternatives => Delay_Alt_List));
10312 Delay_Case := Delay_Alt_List;
10315 -- If there are no delay alternatives, we append the case statement
10316 -- to the statement list.
10318 if Delay_Count = 0 then
10319 Append_List (Accept_Case, Stats);
10321 -- Delay alternatives present
10324 -- If delay alternatives are present we generate:
10326 -- find minimum delay.
10327 -- DX := minimum delay;
10328 -- M := <delay mode>;
10329 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
10332 -- if X = No_Rendezvous then
10333 -- case statement for delay statements.
10335 -- case statement for accept alternatives.
10346 -- The type of the delay expression is known to be legal
10348 if Time_Type = Standard_Duration then
10349 Conv := New_Reference_To (Delay_Min, Loc);
10351 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10352 Conv := Make_Function_Call (Loc,
10353 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
10354 New_List (New_Reference_To (Delay_Min, Loc)));
10358 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
10360 Conv := Make_Function_Call (Loc,
10361 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
10362 New_List (New_Reference_To (Delay_Min, Loc)));
10365 Stmt := Make_Assignment_Statement (Loc,
10366 Name => New_Reference_To (D, Loc),
10367 Expression => Conv);
10369 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
10371 Parms := Parameter_Associations (Select_Call);
10372 Parm := First (Parms);
10374 while Present (Parm)
10375 and then Parm /= Select_Mode
10380 pragma Assert (Present (Parm));
10381 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
10384 -- Prepare two new parameters of Duration and Delay_Mode type
10385 -- which represent the value and the mode of the minimum delay.
10388 Insert_After (Parm, New_Reference_To (M, Loc));
10389 Insert_After (Parm, New_Reference_To (D, Loc));
10391 -- Create a call to RTS
10393 Rewrite (Select_Call,
10394 Make_Procedure_Call_Statement (Loc,
10395 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
10396 Parameter_Associations => Parms));
10398 -- This new call should follow the calculation of the minimum
10401 Insert_List_Before (Select_Call, Delay_List);
10403 if Check_Guard then
10405 Make_Implicit_If_Statement (N,
10406 Condition => New_Reference_To (Guard_Open, Loc),
10408 New_List (New_Copy_Tree (Stmt),
10409 New_Copy_Tree (Select_Call)),
10410 Else_Statements => Accept_Or_Raise);
10411 Rewrite (Select_Call, Stmt);
10413 Insert_Before (Select_Call, Stmt);
10417 Make_Implicit_If_Statement (N,
10418 Condition => Make_Op_Eq (Loc,
10419 Left_Opnd => New_Reference_To (Xnam, Loc),
10421 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
10423 Then_Statements => Delay_Case,
10424 Else_Statements => Accept_Case);
10426 Append (Cases, Stats);
10430 -- Replace accept statement with appropriate block
10433 Make_Block_Statement (Loc,
10434 Declarations => Decls,
10435 Handled_Statement_Sequence =>
10436 Make_Handled_Sequence_Of_Statements (Loc,
10437 Statements => Stats));
10439 Rewrite (N, Block);
10442 -- Note: have to worry more about abort deferral in above code ???
10444 -- Final step is to unstack the Accept_Address entries for all accept
10445 -- statements appearing in accept alternatives in the select statement
10447 Alt := First (Alts);
10448 while Present (Alt) loop
10449 if Nkind (Alt) = N_Accept_Alternative then
10450 Remove_Last_Elmt (Accept_Address
10451 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
10456 end Expand_N_Selective_Accept;
10458 --------------------------------------
10459 -- Expand_N_Single_Task_Declaration --
10460 --------------------------------------
10462 -- Single task declarations should never be present after semantic
10463 -- analysis, since we expect them to be replaced by a declaration of an
10464 -- anonymous task type, followed by a declaration of the task object. We
10465 -- include this routine to make sure that is happening!
10467 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
10469 raise Program_Error;
10470 end Expand_N_Single_Task_Declaration;
10472 ------------------------
10473 -- Expand_N_Task_Body --
10474 ------------------------
10476 -- Given a task body
10478 -- task body tname is
10484 -- This expansion routine converts it into a procedure and sets the
10485 -- elaboration flag for the procedure to true, to represent the fact
10486 -- that the task body is now elaborated:
10488 -- procedure tnameB (_Task : access tnameV) is
10489 -- discriminal : dtype renames _Task.discriminant;
10491 -- procedure _clean is
10493 -- Abort_Defer.all;
10495 -- Abort_Undefer.all;
10500 -- Abort_Undefer.all;
10502 -- System.Task_Stages.Complete_Activation;
10510 -- In addition, if the task body is an activator, then a call to activate
10511 -- tasks is added at the start of the statements, before the call to
10512 -- Complete_Activation, and if in addition the task is a master then it
10513 -- must be established as a master. These calls are inserted and analyzed
10514 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
10517 -- There is one discriminal declaration line generated for each
10518 -- discriminant that is present to provide an easy reference point for
10519 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
10521 -- Note on relationship to GNARLI definition. In the GNARLI definition,
10522 -- task body procedures have a profile (Arg : System.Address). That is
10523 -- needed because GNARLI has to use the same access-to-subprogram type
10524 -- for all task types. We depend here on knowing that in GNAT, passing
10525 -- an address argument by value is identical to passing a record value
10526 -- by access (in either case a single pointer is passed), so even though
10527 -- this procedure has the wrong profile. In fact it's all OK, since the
10528 -- callings sequence is identical.
10530 procedure Expand_N_Task_Body (N : Node_Id) is
10531 Loc : constant Source_Ptr := Sloc (N);
10532 Ttyp : constant Entity_Id := Corresponding_Spec (N);
10536 Insert_Nod : Node_Id;
10537 -- Used to determine the proper location of wrapper body insertions
10540 -- Add renaming declarations for discriminals and a declaration for the
10541 -- entry family index (if applicable).
10543 Install_Private_Data_Declarations
10544 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
10546 -- Add a call to Abort_Undefer at the very beginning of the task
10547 -- body since this body is called with abort still deferred.
10549 if Abort_Allowed then
10550 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10552 (First (Statements (Handled_Statement_Sequence (N))), Call);
10556 -- The statement part has already been protected with an at_end and
10557 -- cleanup actions. The call to Complete_Activation must be placed
10558 -- at the head of the sequence of statements of that block. The
10559 -- declarations have been merged in this sequence of statements but
10560 -- the first real statement is accessible from the First_Real_Statement
10561 -- field (which was set for exactly this purpose).
10563 if Restricted_Profile then
10564 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
10566 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
10570 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
10574 Make_Subprogram_Body (Loc,
10575 Specification => Build_Task_Proc_Specification (Ttyp),
10576 Declarations => Declarations (N),
10577 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
10579 -- If the task contains generic instantiations, cleanup actions are
10580 -- delayed until after instantiation. Transfer the activation chain to
10581 -- the subprogram, to insure that the activation call is properly
10582 -- generated. It the task body contains inner tasks, indicate that the
10583 -- subprogram is a task master.
10585 if Delay_Cleanups (Ttyp) then
10586 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
10587 Set_Is_Task_Master (New_N, Is_Task_Master (N));
10590 Rewrite (N, New_N);
10593 -- Set elaboration flag immediately after task body. If the body is a
10594 -- subunit, the flag is set in the declarative part containing the stub.
10596 if Nkind (Parent (N)) /= N_Subunit then
10598 Make_Assignment_Statement (Loc,
10600 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
10601 Expression => New_Reference_To (Standard_True, Loc)));
10604 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
10605 -- the task body. At this point all wrapper specs have been created,
10606 -- frozen and included in the dispatch table for the task type.
10608 if Ada_Version >= Ada_2005 then
10609 if Nkind (Parent (N)) = N_Subunit then
10610 Insert_Nod := Corresponding_Stub (Parent (N));
10615 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
10617 end Expand_N_Task_Body;
10619 ------------------------------------
10620 -- Expand_N_Task_Type_Declaration --
10621 ------------------------------------
10623 -- We have several things to do. First we must create a Boolean flag used
10624 -- to mark if the body is elaborated yet. This variable gets set to True
10625 -- when the body of the task is elaborated (we can't rely on the normal
10626 -- ABE mechanism for the task body, since we need to pass an access to
10627 -- this elaboration boolean to the runtime routines).
10629 -- taskE : aliased Boolean := False;
10631 -- Next a variable is declared to hold the task stack size (either the
10632 -- default : Unspecified_Size, or a value that is set by a pragma
10633 -- Storage_Size). If the value of the pragma Storage_Size is static, then
10634 -- the variable is initialized with this value:
10636 -- taskZ : Size_Type := Unspecified_Size;
10638 -- taskZ : Size_Type := Size_Type (size_expression);
10640 -- Note: No variable is needed to hold the task relative deadline since
10641 -- its value would never be static because the parameter is of a private
10642 -- type (Ada.Real_Time.Time_Span).
10644 -- Next we create a corresponding record type declaration used to represent
10645 -- values of this task. The general form of this type declaration is
10647 -- type taskV (discriminants) is record
10648 -- _Task_Id : Task_Id;
10649 -- entry_family : array (bounds) of Void;
10650 -- _Priority : Integer := priority_expression;
10651 -- _Size : Size_Type := size_expression;
10652 -- _Task_Info : Task_Info_Type := task_info_expression;
10653 -- _CPU : Integer := cpu_range_expression;
10654 -- _Relative_Deadline : Time_Span := time_span_expression;
10655 -- _Domain : Dispatching_Domain := dd_expression;
10658 -- The discriminants are present only if the corresponding task type has
10659 -- discriminants, and they exactly mirror the task type discriminants.
10661 -- The Id field is always present. It contains the Task_Id value, as set by
10662 -- the call to Create_Task. Note that although the task is limited, the
10663 -- task value record type is not limited, so there is no problem in passing
10664 -- this field as an out parameter to Create_Task.
10666 -- One entry_family component is present for each entry family in the task
10667 -- definition. The bounds correspond to the bounds of the entry family
10668 -- (which may depend on discriminants). The element type is void, since we
10669 -- only need the bounds information for determining the entry index. Note
10670 -- that the use of an anonymous array would normally be illegal in this
10671 -- context, but this is a parser check, and the semantics is quite prepared
10672 -- to handle such a case.
10674 -- The _Size field is present only if a Storage_Size pragma appears in the
10675 -- task definition. The expression captures the argument that was present
10676 -- in the pragma, and is used to override the task stack size otherwise
10677 -- associated with the task type.
10679 -- The _Priority field is present only if a Priority or Interrupt_Priority
10680 -- pragma appears in the task definition. The expression captures the
10681 -- argument that was present in the pragma, and is used to provide the Size
10682 -- parameter to the call to Create_Task.
10684 -- The _Task_Info field is present only if a Task_Info pragma appears in
10685 -- the task definition. The expression captures the argument that was
10686 -- present in the pragma, and is used to provide the Task_Image parameter
10687 -- to the call to Create_Task.
10689 -- The _CPU field is present only if a CPU pragma appears in the task
10690 -- definition. The expression captures the argument that was present in
10691 -- the pragma, and is used to provide the CPU parameter to the call to
10694 -- The _Relative_Deadline field is present only if a Relative_Deadline
10695 -- pragma appears in the task definition. The expression captures the
10696 -- argument that was present in the pragma, and is used to provide the
10697 -- Relative_Deadline parameter to the call to Create_Task.
10699 -- The _Domain field is present only if a Dispatching_Domain pragma or
10700 -- aspect appears in the task definition. The expression captures the
10701 -- argument that was present in the pragma or aspect, and is used to
10702 -- provide the Dispatching_Domain parameter to the call to Create_Task.
10704 -- When a task is declared, an instance of the task value record is
10705 -- created. The elaboration of this declaration creates the correct bounds
10706 -- for the entry families, and also evaluates the size, priority, and
10707 -- task_Info expressions if needed. The initialization routine for the task
10708 -- type itself then calls Create_Task with appropriate parameters to
10709 -- initialize the value of the Task_Id field.
10711 -- Note: the address of this record is passed as the "Discriminants"
10712 -- parameter for Create_Task. Since Create_Task merely passes this onto the
10713 -- body procedure, it does not matter that it does not quite match the
10714 -- GNARLI model of what is being passed (the record contains more than just
10715 -- the discriminants, but the discriminants can be found from the record
10718 -- The Entity_Id for this created record type is placed in the
10719 -- Corresponding_Record_Type field of the associated task type entity.
10721 -- Next we create a procedure specification for the task body procedure:
10723 -- procedure taskB (_Task : access taskV);
10725 -- Note that this must come after the record type declaration, since
10726 -- the spec refers to this type. It turns out that the initialization
10727 -- procedure for the value type references the task body spec, but that's
10728 -- fine, since it won't be generated till the freeze point for the type,
10729 -- which is certainly after the task body spec declaration.
10731 -- Finally, we set the task index value field of the entry attribute in
10732 -- the case of a simple entry.
10734 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
10735 Loc : constant Source_Ptr := Sloc (N);
10736 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
10737 Tasknm : constant Name_Id := Chars (Tasktyp);
10738 Taskdef : constant Node_Id := Task_Definition (N);
10740 Proc_Spec : Node_Id;
10741 Rec_Decl : Node_Id;
10742 Rec_Ent : Entity_Id;
10744 Elab_Decl : Node_Id;
10745 Size_Decl : Node_Id;
10746 Body_Decl : Node_Id;
10747 Task_Size : Node_Id;
10748 Ent_Stack : Entity_Id;
10749 Decl_Stack : Node_Id;
10752 -- If already expanded, nothing to do
10754 if Present (Corresponding_Record_Type (Tasktyp)) then
10758 -- Here we will do the expansion
10760 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
10762 Rec_Ent := Defining_Identifier (Rec_Decl);
10763 Cdecls := Component_Items (Component_List
10764 (Type_Definition (Rec_Decl)));
10766 Qualify_Entity_Names (N);
10768 -- First create the elaboration variable
10771 Make_Object_Declaration (Loc,
10772 Defining_Identifier =>
10773 Make_Defining_Identifier (Sloc (Tasktyp),
10774 Chars => New_External_Name (Tasknm, 'E')),
10775 Aliased_Present => True,
10776 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
10777 Expression => New_Reference_To (Standard_False, Loc));
10778 Insert_After (N, Elab_Decl);
10780 -- Next create the declaration of the size variable (tasknmZ)
10782 Set_Storage_Size_Variable (Tasktyp,
10783 Make_Defining_Identifier (Sloc (Tasktyp),
10784 Chars => New_External_Name (Tasknm, 'Z')));
10786 if Present (Taskdef)
10787 and then Has_Storage_Size_Pragma (Taskdef)
10789 Is_Static_Expression
10791 (First (Pragma_Argument_Associations
10792 (Find_Task_Or_Protected_Pragma
10793 (Taskdef, Name_Storage_Size)))))
10796 Make_Object_Declaration (Loc,
10797 Defining_Identifier => Storage_Size_Variable (Tasktyp),
10798 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
10800 Convert_To (RTE (RE_Size_Type),
10802 (Expression (First (Pragma_Argument_Associations
10803 (Find_Task_Or_Protected_Pragma
10804 (Taskdef, Name_Storage_Size)))))));
10808 Make_Object_Declaration (Loc,
10809 Defining_Identifier => Storage_Size_Variable (Tasktyp),
10810 Object_Definition =>
10811 New_Reference_To (RTE (RE_Size_Type), Loc),
10813 New_Reference_To (RTE (RE_Unspecified_Size), Loc));
10816 Insert_After (Elab_Decl, Size_Decl);
10818 -- Next build the rest of the corresponding record declaration. This is
10819 -- done last, since the corresponding record initialization procedure
10820 -- will reference the previously created entities.
10822 -- Fill in the component declarations -- first the _Task_Id field
10825 Make_Component_Declaration (Loc,
10826 Defining_Identifier =>
10827 Make_Defining_Identifier (Loc, Name_uTask_Id),
10828 Component_Definition =>
10829 Make_Component_Definition (Loc,
10830 Aliased_Present => False,
10831 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
10834 -- Declare static ATCB (that is, created by the expander) if we are
10835 -- using the Restricted run time.
10837 if Restricted_Profile then
10839 Make_Component_Declaration (Loc,
10840 Defining_Identifier =>
10841 Make_Defining_Identifier (Loc, Name_uATCB),
10843 Component_Definition =>
10844 Make_Component_Definition (Loc,
10845 Aliased_Present => True,
10846 Subtype_Indication => Make_Subtype_Indication (Loc,
10848 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
10851 Make_Index_Or_Discriminant_Constraint (Loc,
10853 New_List (Make_Integer_Literal (Loc, 0)))))));
10857 -- Declare static stack (that is, created by the expander) if we are
10858 -- using the Restricted run time on a bare board configuration.
10860 if Restricted_Profile
10861 and then Preallocated_Stacks_On_Target
10863 -- First we need to extract the appropriate stack size
10865 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
10867 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
10869 Expr_N : constant Node_Id :=
10870 Expression (First (
10871 Pragma_Argument_Associations (
10872 Find_Task_Or_Protected_Pragma
10873 (Taskdef, Name_Storage_Size))));
10874 Etyp : constant Entity_Id := Etype (Expr_N);
10875 P : constant Node_Id := Parent (Expr_N);
10878 -- The stack is defined inside the corresponding record.
10879 -- Therefore if the size of the stack is set by means of
10880 -- a discriminant, we must reference the discriminant of the
10881 -- corresponding record type.
10883 if Nkind (Expr_N) in N_Has_Entity
10884 and then Present (Discriminal_Link (Entity (Expr_N)))
10888 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
10890 Set_Parent (Task_Size, P);
10891 Set_Etype (Task_Size, Etyp);
10892 Set_Analyzed (Task_Size);
10895 Task_Size := Relocate_Node (Expr_N);
10901 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
10904 Decl_Stack := Make_Component_Declaration (Loc,
10905 Defining_Identifier => Ent_Stack,
10907 Component_Definition =>
10908 Make_Component_Definition (Loc,
10909 Aliased_Present => True,
10910 Subtype_Indication => Make_Subtype_Indication (Loc,
10912 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
10915 Make_Index_Or_Discriminant_Constraint (Loc,
10916 Constraints => New_List (Make_Range (Loc,
10917 Low_Bound => Make_Integer_Literal (Loc, 1),
10918 High_Bound => Convert_To (RTE (RE_Storage_Offset),
10921 Append_To (Cdecls, Decl_Stack);
10923 -- The appropriate alignment for the stack is ensured by the run-time
10924 -- code in charge of task creation.
10928 -- Add components for entry families
10930 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
10932 -- Add the _Priority component if a Priority pragma is present
10934 if Present (Taskdef) and then Has_Pragma_Priority (Taskdef) then
10936 Prag : constant Node_Id :=
10937 Find_Task_Or_Protected_Pragma (Taskdef, Name_Priority);
10941 Expr := First (Pragma_Argument_Associations (Prag));
10943 if Nkind (Expr) = N_Pragma_Argument_Association then
10944 Expr := Expression (Expr);
10947 Expr := New_Copy_Tree (Expr);
10949 -- Add conversion to proper type to do range check if required
10950 -- Note that for runtime units, we allow out of range interrupt
10951 -- priority values to be used in a priority pragma. This is for
10952 -- the benefit of some versions of System.Interrupts which use
10953 -- a special server task with maximum interrupt priority.
10955 if Pragma_Name (Prag) = Name_Priority
10956 and then not GNAT_Mode
10958 Rewrite (Expr, Convert_To (RTE (RE_Priority), Expr));
10960 Rewrite (Expr, Convert_To (RTE (RE_Any_Priority), Expr));
10964 Make_Component_Declaration (Loc,
10965 Defining_Identifier =>
10966 Make_Defining_Identifier (Loc, Name_uPriority),
10967 Component_Definition =>
10968 Make_Component_Definition (Loc,
10969 Aliased_Present => False,
10970 Subtype_Indication => New_Reference_To (Standard_Integer,
10972 Expression => Expr));
10976 -- Add the _Task_Size component if a Storage_Size pragma is present
10978 if Present (Taskdef)
10979 and then Has_Storage_Size_Pragma (Taskdef)
10982 Make_Component_Declaration (Loc,
10983 Defining_Identifier =>
10984 Make_Defining_Identifier (Loc, Name_uSize),
10986 Component_Definition =>
10987 Make_Component_Definition (Loc,
10988 Aliased_Present => False,
10989 Subtype_Indication => New_Reference_To (RTE (RE_Size_Type),
10993 Convert_To (RTE (RE_Size_Type),
10995 Expression (First (
10996 Pragma_Argument_Associations (
10997 Find_Task_Or_Protected_Pragma
10998 (Taskdef, Name_Storage_Size))))))));
11001 -- Add the _Task_Info component if a Task_Info pragma is present
11003 if Present (Taskdef) and then Has_Task_Info_Pragma (Taskdef) then
11005 Make_Component_Declaration (Loc,
11006 Defining_Identifier =>
11007 Make_Defining_Identifier (Loc, Name_uTask_Info),
11009 Component_Definition =>
11010 Make_Component_Definition (Loc,
11011 Aliased_Present => False,
11012 Subtype_Indication =>
11013 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
11015 Expression => New_Copy (
11016 Expression (First (
11017 Pragma_Argument_Associations (
11018 Find_Task_Or_Protected_Pragma
11019 (Taskdef, Name_Task_Info)))))));
11022 -- Add the _CPU component if a CPU pragma is present
11024 if Present (Taskdef) and then Has_Pragma_CPU (Taskdef) then
11026 Make_Component_Declaration (Loc,
11027 Defining_Identifier =>
11028 Make_Defining_Identifier (Loc, Name_uCPU),
11030 Component_Definition =>
11031 Make_Component_Definition (Loc,
11032 Aliased_Present => False,
11033 Subtype_Indication =>
11034 New_Reference_To (RTE (RE_CPU_Range), Loc)),
11036 Expression => New_Copy (
11037 Expression (First (
11038 Pragma_Argument_Associations (
11039 Find_Task_Or_Protected_Pragma
11040 (Taskdef, Name_CPU)))))));
11043 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
11044 -- present. If we are using a restricted run time this component will
11045 -- not be added (deadlines are not allowed by the Ravenscar profile).
11047 if not Restricted_Profile
11048 and then Present (Taskdef)
11049 and then Has_Relative_Deadline_Pragma (Taskdef)
11052 Make_Component_Declaration (Loc,
11053 Defining_Identifier =>
11054 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
11056 Component_Definition =>
11057 Make_Component_Definition (Loc,
11058 Aliased_Present => False,
11059 Subtype_Indication =>
11060 New_Reference_To (RTE (RE_Time_Span), Loc)),
11063 Convert_To (RTE (RE_Time_Span),
11065 Expression (First (
11066 Pragma_Argument_Associations (
11067 Find_Task_Or_Protected_Pragma
11068 (Taskdef, Name_Relative_Deadline))))))));
11071 -- Add the _Dispatching_Domain component if a Dispatching_Domain pragma
11072 -- or aspect is present. If we are using a restricted run time this
11073 -- component will not be added (dispatching domains are not allowed by
11074 -- the Ravenscar profile).
11076 if not Restricted_Profile
11077 and then Present (Taskdef)
11078 and then Has_Pragma_Dispatching_Domain (Taskdef)
11081 Make_Component_Declaration (Loc,
11082 Defining_Identifier =>
11083 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
11085 Component_Definition =>
11086 Make_Component_Definition (Loc,
11087 Aliased_Present => False,
11088 Subtype_Indication =>
11090 (RTE (RE_Dispatching_Domain_Access), Loc)),
11093 Unchecked_Convert_To (RTE (RE_Dispatching_Domain_Access),
11097 (Pragma_Argument_Associations
11098 (Find_Task_Or_Protected_Pragma
11099 (Taskdef, Name_Dispatching_Domain))))))));
11102 Insert_After (Size_Decl, Rec_Decl);
11104 -- Analyze the record declaration immediately after construction,
11105 -- because the initialization procedure is needed for single task
11106 -- declarations before the next entity is analyzed.
11108 Analyze (Rec_Decl);
11110 -- Create the declaration of the task body procedure
11112 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
11114 Make_Subprogram_Declaration (Loc,
11115 Specification => Proc_Spec);
11117 Insert_After (Rec_Decl, Body_Decl);
11119 -- The subprogram does not comes from source, so we have to indicate the
11120 -- need for debugging information explicitly.
11122 if Comes_From_Source (Original_Node (N)) then
11123 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
11126 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
11127 -- the corresponding record has been frozen.
11129 if Ada_Version >= Ada_2005 then
11130 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
11133 -- Ada 2005 (AI-345): We must defer freezing to allow further
11134 -- declaration of primitive subprograms covering task interfaces
11136 if Ada_Version <= Ada_95 then
11138 -- Now we can freeze the corresponding record. This needs manually
11139 -- freezing, since it is really part of the task type, and the task
11140 -- type is frozen at this stage. We of course need the initialization
11141 -- procedure for this corresponding record type and we won't get it
11142 -- in time if we don't freeze now.
11145 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
11147 if Is_Non_Empty_List (L) then
11148 Insert_List_After (Body_Decl, L);
11153 -- Complete the expansion of access types to the current task type, if
11154 -- any were declared.
11156 Expand_Previous_Access_Type (Tasktyp);
11158 -- Create wrappers for entries that have pre/postconditions
11164 Ent := First_Entity (Tasktyp);
11165 while Present (Ent) loop
11166 if Ekind_In (Ent, E_Entry, E_Entry_Family)
11167 and then Present (Spec_PPC_List (Contract (Ent)))
11169 Build_PPC_Wrapper (Ent, N);
11175 end Expand_N_Task_Type_Declaration;
11177 -------------------------------
11178 -- Expand_N_Timed_Entry_Call --
11179 -------------------------------
11181 -- A timed entry call in normal case is not implemented using ATC mechanism
11182 -- anymore for efficiency reason.
11192 -- is expanded as follow:
11194 -- 1) When T.E is a task entry_call;
11198 -- X : Task_Entry_Index := <entry index>;
11199 -- DX : Duration := To_Duration (D);
11200 -- M : Delay_Mode := <discriminant>;
11201 -- P : parms := (parm, parm, parm);
11204 -- Timed_Protected_Entry_Call
11205 -- (<acceptor-task>, X, P'Address, DX, M, B);
11213 -- 2) When T.E is a protected entry_call;
11217 -- X : Protected_Entry_Index := <entry index>;
11218 -- DX : Duration := To_Duration (D);
11219 -- M : Delay_Mode := <discriminant>;
11220 -- P : parms := (parm, parm, parm);
11223 -- Timed_Protected_Entry_Call
11224 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
11232 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call;
11235 -- B : Boolean := False;
11236 -- C : Ada.Tags.Prim_Op_Kind;
11237 -- DX : Duration := To_Duration (D)
11238 -- K : Ada.Tags.Tagged_Kind :=
11239 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
11240 -- M : Integer :=...;
11241 -- P : Parameters := (Param1 .. ParamN);
11245 -- if K = Ada.Tags.TK_Limited_Tagged then
11246 -- <dispatching-call>;
11247 -- <triggering-statements>
11251 -- Ada.Tags.Get_Offset_Index
11252 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
11254 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
11256 -- if C = POK_Protected_Entry
11257 -- or else C = POK_Task_Entry
11259 -- Param1 := P.Param1;
11261 -- ParamN := P.ParamN;
11265 -- if C = POK_Procedure
11266 -- or else C = POK_Protected_Procedure
11267 -- or else C = POK_Task_Procedure
11269 -- <dispatching-call>;
11272 -- <triggering-statements>
11274 -- <timed-statements>
11279 -- The triggering statement and the sequence of timed statements have not
11280 -- been analyzed yet (see Analyzed_Timed_Entry_Call). They may contain
11281 -- local declarations, and therefore the copies that are made during
11282 -- expansion must be disjoint, as for any other inlining.
11284 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
11285 Loc : constant Source_Ptr := Sloc (N);
11287 E_Call : Node_Id :=
11288 Entry_Call_Statement (Entry_Call_Alternative (N));
11289 E_Stats : constant List_Id :=
11290 Statements (Entry_Call_Alternative (N));
11291 D_Stat : Node_Id :=
11292 Delay_Statement (Delay_Alternative (N));
11293 D_Stats : constant List_Id :=
11294 Statements (Delay_Alternative (N));
11297 Blk_Typ : Entity_Id;
11299 Call_Ent : Entity_Id;
11300 Conc_Typ_Stmts : List_Id;
11304 D_Type : Entity_Id;
11310 Is_Disp_Select : Boolean;
11311 Lim_Typ_Stmts : List_Id;
11320 B : Entity_Id; -- Call status flag
11321 C : Entity_Id; -- Call kind
11322 D : Entity_Id; -- Delay
11323 K : Entity_Id; -- Tagged kind
11324 M : Entity_Id; -- Delay mode
11325 P : Entity_Id; -- Parameter block
11326 S : Entity_Id; -- Primitive operation slot
11329 -- Under the Ravenscar profile, timed entry calls are excluded. An error
11330 -- was already reported on spec, so do not attempt to expand the call.
11332 if Restriction_Active (No_Select_Statements) then
11336 Process_Statements_For_Controlled_Objects (Entry_Call_Alternative (N));
11337 Process_Statements_For_Controlled_Objects (Delay_Alternative (N));
11339 -- The arguments in the call may require dynamic allocation, and the
11340 -- call statement may have been transformed into a block. The block
11341 -- may contain additional declarations for internal entities, and the
11342 -- original call is found by sequential search.
11344 if Nkind (E_Call) = N_Block_Statement then
11345 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
11346 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
11347 N_Entry_Call_Statement)
11354 Ada_Version >= Ada_2005
11355 and then Nkind (E_Call) = N_Procedure_Call_Statement;
11357 if Is_Disp_Select then
11358 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
11364 -- B : Boolean := False;
11366 B := Build_B (Loc, Decls);
11369 -- C : Ada.Tags.Prim_Op_Kind;
11371 C := Build_C (Loc, Decls);
11373 -- Because the analysis of all statements was disabled, manually
11374 -- analyze the delay statement.
11377 D_Stat := Original_Node (D_Stat);
11380 -- Build an entry call using Simple_Entry_Call
11382 Extract_Entry (E_Call, Concval, Ename, Index);
11383 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
11385 Decls := Declarations (E_Call);
11386 Stmts := Statements (Handled_Statement_Sequence (E_Call));
11395 B := Make_Defining_Identifier (Loc, Name_uB);
11398 Make_Object_Declaration (Loc,
11399 Defining_Identifier => B,
11400 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
11403 -- Duration and mode processing
11405 D_Type := Base_Type (Etype (Expression (D_Stat)));
11407 -- Use the type of the delay expression (Calendar or Real_Time) to
11408 -- generate the appropriate conversion.
11410 if Nkind (D_Stat) = N_Delay_Relative_Statement then
11411 D_Disc := Make_Integer_Literal (Loc, 0);
11412 D_Conv := Relocate_Node (Expression (D_Stat));
11414 elsif Is_RTE (D_Type, RO_CA_Time) then
11415 D_Disc := Make_Integer_Literal (Loc, 1);
11417 Make_Function_Call (Loc,
11418 Name => New_Reference_To (RTE (RO_CA_To_Duration), Loc),
11419 Parameter_Associations =>
11420 New_List (New_Copy (Expression (D_Stat))));
11422 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
11423 D_Disc := Make_Integer_Literal (Loc, 2);
11425 Make_Function_Call (Loc,
11426 Name => New_Reference_To (RTE (RO_RT_To_Duration), Loc),
11427 Parameter_Associations =>
11428 New_List (New_Copy (Expression (D_Stat))));
11431 D := Make_Temporary (Loc, 'D');
11437 Make_Object_Declaration (Loc,
11438 Defining_Identifier => D,
11439 Object_Definition => New_Reference_To (Standard_Duration, Loc)));
11441 M := Make_Temporary (Loc, 'M');
11444 -- M : Integer := (0 | 1 | 2);
11447 Make_Object_Declaration (Loc,
11448 Defining_Identifier => M,
11449 Object_Definition => New_Reference_To (Standard_Integer, Loc),
11450 Expression => D_Disc));
11452 -- Do the assignment at this stage only because the evaluation of the
11453 -- expression must not occur before (see ACVC C97302A).
11456 Make_Assignment_Statement (Loc,
11457 Name => New_Reference_To (D, Loc),
11458 Expression => D_Conv));
11460 -- Parameter block processing
11462 -- Manually create the parameter block for dispatching calls. In the
11463 -- case of entries, the block has already been created during the call
11464 -- to Build_Simple_Entry_Call.
11466 if Is_Disp_Select then
11468 -- Tagged kind processing, generate:
11469 -- K : Ada.Tags.Tagged_Kind :=
11470 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
11472 K := Build_K (Loc, Decls, Obj);
11474 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
11476 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
11478 -- Dispatch table slot processing, generate:
11481 S := Build_S (Loc, Decls);
11484 -- S := Ada.Tags.Get_Offset_Index
11485 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
11488 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
11491 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
11493 -- where Obj is the controlling formal parameter, S is the dispatch
11494 -- table slot number of the dispatching operation, P is the wrapped
11495 -- parameter block, D is the duration, M is the duration mode, C is
11496 -- the call kind and B is the call status.
11498 Params := New_List;
11500 Append_To (Params, New_Copy_Tree (Obj));
11501 Append_To (Params, New_Reference_To (S, Loc));
11503 Make_Attribute_Reference (Loc,
11504 Prefix => New_Reference_To (P, Loc),
11505 Attribute_Name => Name_Address));
11506 Append_To (Params, New_Reference_To (D, Loc));
11507 Append_To (Params, New_Reference_To (M, Loc));
11508 Append_To (Params, New_Reference_To (C, Loc));
11509 Append_To (Params, New_Reference_To (B, Loc));
11511 Append_To (Conc_Typ_Stmts,
11512 Make_Procedure_Call_Statement (Loc,
11516 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
11517 Parameter_Associations => Params));
11520 -- if C = POK_Protected_Entry
11521 -- or else C = POK_Task_Entry
11523 -- Param1 := P.Param1;
11525 -- ParamN := P.ParamN;
11528 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
11530 -- Generate the if statement only when the packed parameters need
11531 -- explicit assignments to their corresponding actuals.
11533 if Present (Unpack) then
11534 Append_To (Conc_Typ_Stmts,
11535 Make_If_Statement (Loc,
11541 Left_Opnd => New_Reference_To (C, Loc),
11544 (RTE (RE_POK_Protected_Entry), Loc)),
11548 Left_Opnd => New_Reference_To (C, Loc),
11550 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
11552 Then_Statements => Unpack));
11558 -- if C = POK_Procedure
11559 -- or else C = POK_Protected_Procedure
11560 -- or else C = POK_Task_Procedure
11562 -- <dispatching-call>
11564 -- <triggering-statements>
11566 -- <timed-statements>
11569 N_Stats := Copy_Separate_List (E_Stats);
11571 Prepend_To (N_Stats,
11572 Make_If_Statement (Loc,
11578 Left_Opnd => New_Reference_To (C, Loc),
11580 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
11586 Left_Opnd => New_Reference_To (C, Loc),
11588 New_Reference_To (RTE (
11589 RE_POK_Protected_Procedure), Loc)),
11592 Left_Opnd => New_Reference_To (C, Loc),
11595 (RTE (RE_POK_Task_Procedure), Loc)))),
11597 Then_Statements => New_List (E_Call)));
11599 Append_To (Conc_Typ_Stmts,
11600 Make_If_Statement (Loc,
11601 Condition => New_Reference_To (B, Loc),
11602 Then_Statements => N_Stats,
11603 Else_Statements => D_Stats));
11606 -- <dispatching-call>;
11607 -- <triggering-statements>
11609 Lim_Typ_Stmts := Copy_Separate_List (E_Stats);
11610 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (E_Call));
11613 -- if K = Ada.Tags.TK_Limited_Tagged then
11620 Make_If_Statement (Loc,
11623 Left_Opnd => New_Reference_To (K, Loc),
11625 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
11626 Then_Statements => Lim_Typ_Stmts,
11627 Else_Statements => Conc_Typ_Stmts));
11630 -- Skip assignments to temporaries created for in-out parameters.
11631 -- This makes unwarranted assumptions about the shape of the expanded
11632 -- tree for the call, and should be cleaned up ???
11634 Stmt := First (Stmts);
11635 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
11639 -- Do the assignment at this stage only because the evaluation
11640 -- of the expression must not occur before (see ACVC C97302A).
11642 Insert_Before (Stmt,
11643 Make_Assignment_Statement (Loc,
11644 Name => New_Reference_To (D, Loc),
11645 Expression => D_Conv));
11648 Params := Parameter_Associations (Call);
11650 -- For a protected type, we build a Timed_Protected_Entry_Call
11652 if Is_Protected_Type (Etype (Concval)) then
11654 -- Create a new call statement
11656 Param := First (Params);
11657 while Present (Param)
11658 and then not Is_RTE (Etype (Param), RE_Call_Modes)
11663 Dummy := Remove_Next (Next (Param));
11665 -- Remove garbage is following the Cancel_Param if present
11667 Dummy := Next (Param);
11669 -- Remove the mode of the Protected_Entry_Call call, then remove
11670 -- the Communication_Block of the Protected_Entry_Call call, and
11671 -- finally add Duration and a Delay_Mode parameter
11673 pragma Assert (Present (Param));
11674 Rewrite (Param, New_Reference_To (D, Loc));
11676 Rewrite (Dummy, New_Reference_To (M, Loc));
11678 -- Add a Boolean flag for successful entry call
11680 Append_To (Params, New_Reference_To (B, Loc));
11682 case Corresponding_Runtime_Package (Etype (Concval)) is
11683 when System_Tasking_Protected_Objects_Entries =>
11685 Make_Procedure_Call_Statement (Loc,
11688 (RTE (RE_Timed_Protected_Entry_Call), Loc),
11689 Parameter_Associations => Params));
11691 when System_Tasking_Protected_Objects_Single_Entry =>
11692 Param := First (Params);
11693 while Present (Param)
11695 Is_RTE (Etype (Param), RE_Protected_Entry_Index)
11703 Make_Procedure_Call_Statement (Loc,
11706 (RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
11707 Parameter_Associations => Params));
11710 raise Program_Error;
11713 -- For the task case, build a Timed_Task_Entry_Call
11716 -- Create a new call statement
11718 Append_To (Params, New_Reference_To (D, Loc));
11719 Append_To (Params, New_Reference_To (M, Loc));
11720 Append_To (Params, New_Reference_To (B, Loc));
11723 Make_Procedure_Call_Statement (Loc,
11725 New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
11726 Parameter_Associations => Params));
11730 Make_Implicit_If_Statement (N,
11731 Condition => New_Reference_To (B, Loc),
11732 Then_Statements => E_Stats,
11733 Else_Statements => D_Stats));
11737 Make_Block_Statement (Loc,
11738 Declarations => Decls,
11739 Handled_Statement_Sequence =>
11740 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
11743 end Expand_N_Timed_Entry_Call;
11745 ----------------------------------------
11746 -- Expand_Protected_Body_Declarations --
11747 ----------------------------------------
11749 procedure Expand_Protected_Body_Declarations
11751 Spec_Id : Entity_Id)
11754 if No_Run_Time_Mode then
11755 Error_Msg_CRT ("protected body", N);
11758 elsif Full_Expander_Active then
11760 -- Associate discriminals with the first subprogram or entry body to
11763 if Present (First_Protected_Operation (Declarations (N))) then
11764 Set_Discriminals (Parent (Spec_Id));
11767 end Expand_Protected_Body_Declarations;
11769 -------------------------
11770 -- External_Subprogram --
11771 -------------------------
11773 function External_Subprogram (E : Entity_Id) return Entity_Id is
11774 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
11777 -- The internal and external subprograms follow each other on the entity
11778 -- chain. Note that previously private operations had no separate
11779 -- external subprogram. We now create one in all cases, because a
11780 -- private operation may actually appear in an external call, through
11781 -- a 'Access reference used for a callback.
11783 -- If the operation is a function that returns an anonymous access type,
11784 -- the corresponding itype appears before the operation, and must be
11787 -- This mechanism is fragile, there should be a real link between the
11788 -- two versions of the operation, but there is no place to put it ???
11790 if Is_Access_Type (Next_Entity (Subp)) then
11791 return Next_Entity (Next_Entity (Subp));
11793 return Next_Entity (Subp);
11795 end External_Subprogram;
11797 ------------------------------
11798 -- Extract_Dispatching_Call --
11799 ------------------------------
11801 procedure Extract_Dispatching_Call
11803 Call_Ent : out Entity_Id;
11804 Object : out Entity_Id;
11805 Actuals : out List_Id;
11806 Formals : out List_Id)
11808 Call_Nam : Node_Id;
11811 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
11813 if Present (Original_Node (N)) then
11814 Call_Nam := Name (Original_Node (N));
11816 Call_Nam := Name (N);
11819 -- Retrieve the name of the dispatching procedure. It contains the
11820 -- dispatch table slot number.
11823 case Nkind (Call_Nam) is
11824 when N_Identifier =>
11827 when N_Selected_Component =>
11828 Call_Nam := Selector_Name (Call_Nam);
11831 raise Program_Error;
11836 Actuals := Parameter_Associations (N);
11837 Call_Ent := Entity (Call_Nam);
11838 Formals := Parameter_Specifications (Parent (Call_Ent));
11839 Object := First (Actuals);
11841 if Present (Original_Node (Object)) then
11842 Object := Original_Node (Object);
11845 -- If the type of the dispatching object is an access type then return
11846 -- an explicit dereference.
11848 if Is_Access_Type (Etype (Object)) then
11849 Object := Make_Explicit_Dereference (Sloc (N), Object);
11852 end Extract_Dispatching_Call;
11854 -------------------
11855 -- Extract_Entry --
11856 -------------------
11858 procedure Extract_Entry
11860 Concval : out Node_Id;
11861 Ename : out Node_Id;
11862 Index : out Node_Id)
11864 Nam : constant Node_Id := Name (N);
11867 -- For a simple entry, the name is a selected component, with the
11868 -- prefix being the task value, and the selector being the entry.
11870 if Nkind (Nam) = N_Selected_Component then
11871 Concval := Prefix (Nam);
11872 Ename := Selector_Name (Nam);
11875 -- For a member of an entry family, the name is an indexed component
11876 -- where the prefix is a selected component, whose prefix in turn is
11877 -- the task value, and whose selector is the entry family. The single
11878 -- expression in the expressions list of the indexed component is the
11879 -- subscript for the family.
11881 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
11882 Concval := Prefix (Prefix (Nam));
11883 Ename := Selector_Name (Prefix (Nam));
11884 Index := First (Expressions (Nam));
11888 -------------------
11889 -- Family_Offset --
11890 -------------------
11892 function Family_Offset
11897 Cap : Boolean) return Node_Id
11903 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
11904 -- If one of the bounds is a reference to a discriminant, replace with
11905 -- corresponding discriminal of type. Within the body of a task retrieve
11906 -- the renamed discriminant by simple visibility, using its generated
11907 -- name. Within a protected object, find the original discriminant and
11908 -- replace it with the discriminal of the current protected operation.
11910 ------------------------------
11911 -- Convert_Discriminant_Ref --
11912 ------------------------------
11914 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
11915 Loc : constant Source_Ptr := Sloc (Bound);
11920 if Is_Entity_Name (Bound)
11921 and then Ekind (Entity (Bound)) = E_Discriminant
11923 if Is_Task_Type (Ttyp)
11924 and then Has_Completion (Ttyp)
11926 B := Make_Identifier (Loc, Chars (Entity (Bound)));
11927 Find_Direct_Name (B);
11929 elsif Is_Protected_Type (Ttyp) then
11930 D := First_Discriminant (Ttyp);
11931 while Chars (D) /= Chars (Entity (Bound)) loop
11932 Next_Discriminant (D);
11935 B := New_Reference_To (Discriminal (D), Loc);
11938 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
11941 elsif Nkind (Bound) = N_Attribute_Reference then
11945 B := New_Copy_Tree (Bound);
11949 Make_Attribute_Reference (Loc,
11950 Attribute_Name => Name_Pos,
11951 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
11952 Expressions => New_List (B));
11953 end Convert_Discriminant_Ref;
11955 -- Start of processing for Family_Offset
11958 Real_Hi := Convert_Discriminant_Ref (Hi);
11959 Real_Lo := Convert_Discriminant_Ref (Lo);
11962 if Is_Task_Type (Ttyp) then
11963 Ityp := RTE (RE_Task_Entry_Index);
11965 Ityp := RTE (RE_Protected_Entry_Index);
11969 Make_Attribute_Reference (Loc,
11970 Prefix => New_Reference_To (Ityp, Loc),
11971 Attribute_Name => Name_Min,
11972 Expressions => New_List (
11974 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
11977 Make_Attribute_Reference (Loc,
11978 Prefix => New_Reference_To (Ityp, Loc),
11979 Attribute_Name => Name_Max,
11980 Expressions => New_List (
11982 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
11985 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
11992 function Family_Size
11997 Cap : Boolean) return Node_Id
12002 if Is_Task_Type (Ttyp) then
12003 Ityp := RTE (RE_Task_Entry_Index);
12005 Ityp := RTE (RE_Protected_Entry_Index);
12009 Make_Attribute_Reference (Loc,
12010 Prefix => New_Reference_To (Ityp, Loc),
12011 Attribute_Name => Name_Max,
12012 Expressions => New_List (
12015 Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
12017 Make_Integer_Literal (Loc, 1)),
12018 Make_Integer_Literal (Loc, 0)));
12021 -----------------------
12022 -- Find_Master_Scope --
12023 -----------------------
12025 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
12029 -- In Ada 2005, the master is the innermost enclosing scope that is not
12030 -- transient. If the enclosing block is the rewriting of a call or the
12031 -- scope is an extended return statement this is valid master. The
12032 -- master in an extended return is only used within the return, and is
12033 -- subsequently overwritten in Move_Activation_Chain, but it must exist
12034 -- now before that overwriting occurs.
12038 if Ada_Version >= Ada_2005 then
12039 while Is_Internal (S) loop
12040 if Nkind (Parent (S)) = N_Block_Statement
12042 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
12046 elsif Ekind (S) = E_Return_Statement then
12056 end Find_Master_Scope;
12058 -----------------------------------
12059 -- Find_Task_Or_Protected_Pragma --
12060 -----------------------------------
12062 function Find_Task_Or_Protected_Pragma
12064 P : Name_Id) return Node_Id
12069 N := First (Visible_Declarations (T));
12070 while Present (N) loop
12071 if Nkind (N) = N_Pragma then
12072 if Pragma_Name (N) = P then
12075 elsif P = Name_Priority
12076 and then Pragma_Name (N) = Name_Interrupt_Priority
12089 N := First (Private_Declarations (T));
12090 while Present (N) loop
12091 if Nkind (N) = N_Pragma then
12092 if Pragma_Name (N) = P then
12095 elsif P = Name_Priority
12096 and then Pragma_Name (N) = Name_Interrupt_Priority
12109 raise Program_Error;
12110 end Find_Task_Or_Protected_Pragma;
12112 -------------------------------
12113 -- First_Protected_Operation --
12114 -------------------------------
12116 function First_Protected_Operation (D : List_Id) return Node_Id is
12117 First_Op : Node_Id;
12120 First_Op := First (D);
12121 while Present (First_Op)
12122 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
12128 end First_Protected_Operation;
12130 ---------------------------------------
12131 -- Install_Private_Data_Declarations --
12132 ---------------------------------------
12134 procedure Install_Private_Data_Declarations
12136 Spec_Id : Entity_Id;
12137 Conc_Typ : Entity_Id;
12138 Body_Nod : Node_Id;
12140 Barrier : Boolean := False;
12141 Family : Boolean := False)
12143 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
12146 Insert_Node : Node_Id := Empty;
12147 Obj_Ent : Entity_Id;
12149 procedure Add (Decl : Node_Id);
12150 -- Add a single declaration after Insert_Node. If this is the first
12151 -- addition, Decl is added to the front of Decls and it becomes the
12154 function Replace_Bound (Bound : Node_Id) return Node_Id;
12155 -- The bounds of an entry index may depend on discriminants, create a
12156 -- reference to the corresponding prival. Otherwise return a duplicate
12157 -- of the original bound.
12163 procedure Add (Decl : Node_Id) is
12165 if No (Insert_Node) then
12166 Prepend_To (Decls, Decl);
12168 Insert_After (Insert_Node, Decl);
12171 Insert_Node := Decl;
12174 --------------------------
12175 -- Replace_Discriminant --
12176 --------------------------
12178 function Replace_Bound (Bound : Node_Id) return Node_Id is
12180 if Nkind (Bound) = N_Identifier
12181 and then Is_Discriminal (Entity (Bound))
12183 return Make_Identifier (Loc, Chars (Entity (Bound)));
12185 return Duplicate_Subexpr (Bound);
12189 -- Start of processing for Install_Private_Data_Declarations
12192 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
12193 -- formal parameter _O, _object or _task depending on the context.
12195 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
12197 -- Special processing of _O for barrier functions, protected entries
12204 (Ekind (Spec_Id) = E_Entry
12205 or else Ekind (Spec_Id) = E_Entry_Family))
12208 Conc_Rec : constant Entity_Id :=
12209 Corresponding_Record_Type (Conc_Typ);
12210 Typ_Id : constant Entity_Id :=
12211 Make_Defining_Identifier (Loc,
12212 New_External_Name (Chars (Conc_Rec), 'P'));
12215 -- type prot_typVP is access prot_typV;
12218 Make_Full_Type_Declaration (Loc,
12219 Defining_Identifier => Typ_Id,
12221 Make_Access_To_Object_Definition (Loc,
12222 Subtype_Indication =>
12223 New_Reference_To (Conc_Rec, Loc)));
12227 -- _object : prot_typVP := prot_typV (_O);
12230 Make_Object_Declaration (Loc,
12231 Defining_Identifier =>
12232 Make_Defining_Identifier (Loc, Name_uObject),
12233 Object_Definition => New_Reference_To (Typ_Id, Loc),
12235 Unchecked_Convert_To (Typ_Id,
12236 New_Reference_To (Obj_Ent, Loc)));
12239 -- Set the reference to the concurrent object
12241 Obj_Ent := Defining_Identifier (Decl);
12245 -- Step 2: Create the Protection object and build its declaration for
12246 -- any protected entry (family) of subprogram.
12248 if Is_Protected then
12250 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
12254 Set_Protection_Object (Spec_Id, Prot_Ent);
12256 -- Determine the proper protection type
12258 if Has_Attach_Handler (Conc_Typ)
12259 and then not Restricted_Profile
12260 and then not Restriction_Active (No_Dynamic_Attachment)
12262 Prot_Typ := RE_Static_Interrupt_Protection;
12264 elsif Has_Interrupt_Handler (Conc_Typ)
12265 and then not Restriction_Active (No_Dynamic_Attachment)
12267 Prot_Typ := RE_Dynamic_Interrupt_Protection;
12269 -- The type has explicit entries or generated primitive entry
12272 elsif Has_Entries (Conc_Typ)
12274 (Ada_Version >= Ada_2005
12275 and then Present (Interface_List (Parent (Conc_Typ))))
12277 case Corresponding_Runtime_Package (Conc_Typ) is
12278 when System_Tasking_Protected_Objects_Entries =>
12279 Prot_Typ := RE_Protection_Entries;
12281 when System_Tasking_Protected_Objects_Single_Entry =>
12282 Prot_Typ := RE_Protection_Entry;
12285 raise Program_Error;
12289 Prot_Typ := RE_Protection;
12293 -- conc_typR : protection_typ renames _object._object;
12296 Make_Object_Renaming_Declaration (Loc,
12297 Defining_Identifier => Prot_Ent,
12299 New_Reference_To (RTE (Prot_Typ), Loc),
12301 Make_Selected_Component (Loc,
12302 Prefix => New_Reference_To (Obj_Ent, Loc),
12303 Selector_Name => Make_Identifier (Loc, Name_uObject)));
12308 -- Step 3: Add discriminant renamings (if any)
12310 if Has_Discriminants (Conc_Typ) then
12315 D := First_Discriminant (Conc_Typ);
12316 while Present (D) loop
12318 -- Adjust the source location
12320 Set_Sloc (Discriminal (D), Loc);
12323 -- discr_name : discr_typ renames _object.discr_name;
12325 -- discr_name : discr_typ renames _task.discr_name;
12328 Make_Object_Renaming_Declaration (Loc,
12329 Defining_Identifier => Discriminal (D),
12330 Subtype_Mark => New_Reference_To (Etype (D), Loc),
12332 Make_Selected_Component (Loc,
12333 Prefix => New_Reference_To (Obj_Ent, Loc),
12334 Selector_Name => Make_Identifier (Loc, Chars (D))));
12337 Next_Discriminant (D);
12342 -- Step 4: Add private component renamings (if any)
12344 if Is_Protected then
12345 Def := Protected_Definition (Parent (Conc_Typ));
12347 if Present (Private_Declarations (Def)) then
12350 Comp_Id : Entity_Id;
12351 Decl_Id : Entity_Id;
12354 Comp := First (Private_Declarations (Def));
12355 while Present (Comp) loop
12356 if Nkind (Comp) = N_Component_Declaration then
12357 Comp_Id := Defining_Identifier (Comp);
12359 Make_Defining_Identifier (Loc, Chars (Comp_Id));
12361 -- Minimal decoration
12363 if Ekind (Spec_Id) = E_Function then
12364 Set_Ekind (Decl_Id, E_Constant);
12366 Set_Ekind (Decl_Id, E_Variable);
12369 Set_Prival (Comp_Id, Decl_Id);
12370 Set_Prival_Link (Decl_Id, Comp_Id);
12371 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
12374 -- comp_name : comp_typ renames _object.comp_name;
12377 Make_Object_Renaming_Declaration (Loc,
12378 Defining_Identifier => Decl_Id,
12380 New_Reference_To (Etype (Comp_Id), Loc),
12382 Make_Selected_Component (Loc,
12384 New_Reference_To (Obj_Ent, Loc),
12386 Make_Identifier (Loc, Chars (Comp_Id))));
12396 -- Step 5: Add the declaration of the entry index and the associated
12397 -- type for barrier functions and entry families.
12399 if (Barrier and then Family)
12400 or else Ekind (Spec_Id) = E_Entry_Family
12403 E : constant Entity_Id := Index_Object (Spec_Id);
12404 Index : constant Entity_Id :=
12405 Defining_Identifier (
12406 Entry_Index_Specification (
12407 Entry_Body_Formal_Part (Body_Nod)));
12408 Index_Con : constant Entity_Id :=
12409 Make_Defining_Identifier (Loc, Chars (Index));
12411 Index_Typ : Entity_Id;
12415 -- Minimal decoration
12417 Set_Ekind (Index_Con, E_Constant);
12418 Set_Entry_Index_Constant (Index, Index_Con);
12419 Set_Discriminal_Link (Index_Con, Index);
12421 -- Retrieve the bounds of the entry family
12423 High := Type_High_Bound (Etype (Index));
12424 Low := Type_Low_Bound (Etype (Index));
12426 -- In the simple case the entry family is given by a subtype
12427 -- mark and the index constant has the same type.
12429 if Is_Entity_Name (Original_Node (
12430 Discrete_Subtype_Definition (Parent (Index))))
12432 Index_Typ := Etype (Index);
12434 -- Otherwise a new subtype declaration is required
12437 High := Replace_Bound (High);
12438 Low := Replace_Bound (Low);
12440 Index_Typ := Make_Temporary (Loc, 'J');
12443 -- subtype Jnn is <Etype of Index> range Low .. High;
12446 Make_Subtype_Declaration (Loc,
12447 Defining_Identifier => Index_Typ,
12448 Subtype_Indication =>
12449 Make_Subtype_Indication (Loc,
12451 New_Reference_To (Base_Type (Etype (Index)), Loc),
12453 Make_Range_Constraint (Loc,
12454 Range_Expression =>
12455 Make_Range (Loc, Low, High))));
12459 Set_Etype (Index_Con, Index_Typ);
12461 -- Create the object which designates the index:
12462 -- J : constant Jnn :=
12463 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
12465 -- where Jnn is the subtype created above or the original type of
12466 -- the index, _E is a formal of the protected body subprogram and
12467 -- <index expr> is the index of the first family member.
12470 Make_Object_Declaration (Loc,
12471 Defining_Identifier => Index_Con,
12472 Constant_Present => True,
12473 Object_Definition =>
12474 New_Reference_To (Index_Typ, Loc),
12477 Make_Attribute_Reference (Loc,
12479 New_Reference_To (Index_Typ, Loc),
12480 Attribute_Name => Name_Val,
12482 Expressions => New_List (
12486 Make_Op_Subtract (Loc,
12488 New_Reference_To (E, Loc),
12490 Entry_Index_Expression (Loc,
12491 Defining_Identifier (Body_Nod),
12495 Make_Attribute_Reference (Loc,
12497 New_Reference_To (Index_Typ, Loc),
12498 Attribute_Name => Name_Pos,
12499 Expressions => New_List (
12500 Make_Attribute_Reference (Loc,
12502 New_Reference_To (Index_Typ, Loc),
12503 Attribute_Name => Name_First)))))));
12507 end Install_Private_Data_Declarations;
12509 ---------------------------------
12510 -- Is_Potentially_Large_Family --
12511 ---------------------------------
12513 function Is_Potentially_Large_Family
12514 (Base_Index : Entity_Id;
12515 Conctyp : Entity_Id;
12517 Hi : Node_Id) return Boolean
12520 return Scope (Base_Index) = Standard_Standard
12521 and then Base_Index = Base_Type (Standard_Integer)
12522 and then Has_Discriminants (Conctyp)
12524 (Discriminant_Default_Value (First_Discriminant (Conctyp)))
12526 (Denotes_Discriminant (Lo, True)
12527 or else Denotes_Discriminant (Hi, True));
12528 end Is_Potentially_Large_Family;
12530 -------------------------------------
12531 -- Is_Private_Primitive_Subprogram --
12532 -------------------------------------
12534 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
12537 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
12538 and then Is_Private_Primitive (Id);
12539 end Is_Private_Primitive_Subprogram;
12545 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
12546 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
12547 Formal : Entity_Id;
12550 Formal := First_Formal (Bod_Subp);
12551 while Present (Formal) loop
12553 -- Look for formal parameter _E
12555 if Chars (Formal) = Name_uE then
12559 Next_Formal (Formal);
12562 -- A protected body subprogram should always have the parameter in
12565 raise Program_Error;
12568 --------------------------------
12569 -- Make_Initialize_Protection --
12570 --------------------------------
12572 function Make_Initialize_Protection
12573 (Protect_Rec : Entity_Id) return List_Id
12575 Loc : constant Source_Ptr := Sloc (Protect_Rec);
12579 Ptyp : constant Node_Id :=
12580 Corresponding_Concurrent_Type (Protect_Rec);
12582 L : constant List_Id := New_List;
12583 Has_Entry : constant Boolean := Has_Entries (Ptyp);
12584 Restricted : constant Boolean := Restricted_Profile;
12587 -- We may need two calls to properly initialize the object, one to
12588 -- Initialize_Protection, and possibly one to Install_Handlers if we
12589 -- have a pragma Attach_Handler.
12591 -- Get protected declaration. In the case of a task type declaration,
12592 -- this is simply the parent of the protected type entity. In the single
12593 -- protected object declaration, this parent will be the implicit type,
12594 -- and we can find the corresponding single protected object declaration
12595 -- by searching forward in the declaration list in the tree.
12597 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
12598 -- of this type should have been removed during semantic analysis.
12600 Pdec := Parent (Ptyp);
12601 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
12602 N_Single_Protected_Declaration)
12607 -- Now we can find the object definition from this declaration
12609 Pdef := Protected_Definition (Pdec);
12611 -- Build the parameter list for the call. Note that _Init is the name
12612 -- of the formal for the object to be initialized, which is the task
12613 -- value record itself.
12617 -- Object parameter. This is a pointer to the object of type
12618 -- Protection used by the GNARL to control the protected object.
12621 Make_Attribute_Reference (Loc,
12623 Make_Selected_Component (Loc,
12624 Prefix => Make_Identifier (Loc, Name_uInit),
12625 Selector_Name => Make_Identifier (Loc, Name_uObject)),
12626 Attribute_Name => Name_Unchecked_Access));
12628 -- Priority parameter. Set to Unspecified_Priority unless there is a
12629 -- priority pragma, in which case we take the value from the pragma,
12630 -- or there is an interrupt pragma and no priority pragma, and we
12631 -- set the ceiling to Interrupt_Priority'Last, an implementation-
12632 -- defined value, see D.3(10).
12635 and then Has_Pragma_Priority (Pdef)
12638 Prio : constant Node_Id :=
12641 (Pragma_Argument_Associations
12642 (Find_Task_Or_Protected_Pragma
12643 (Pdef, Name_Priority))));
12647 -- If priority is a static expression, then we can duplicate it
12648 -- with no problem and simply append it to the argument list.
12650 if Is_Static_Expression (Prio) then
12652 Duplicate_Subexpr_No_Checks (Prio));
12654 -- Otherwise, the priority may be a per-object expression, if it
12655 -- depends on a discriminant of the type. In this case, create
12656 -- local variable to capture the expression. Note that it is
12657 -- really necessary to create this variable explicitly. It might
12658 -- be thought that removing side effects would the appropriate
12659 -- approach, but that could generate declarations improperly
12660 -- placed in the enclosing scope.
12662 -- Note: Use System.Any_Priority as the expected type for the
12663 -- non-static priority expression, in case the expression has not
12664 -- been analyzed yet (as occurs for example with pragma
12665 -- Interrupt_Priority).
12668 Temp := Make_Temporary (Loc, 'R', Prio);
12670 Make_Object_Declaration (Loc,
12671 Defining_Identifier => Temp,
12672 Object_Definition =>
12673 New_Occurrence_Of (RTE (RE_Any_Priority), Loc),
12674 Expression => Relocate_Node (Prio)));
12676 Append_To (Args, New_Occurrence_Of (Temp, Loc));
12680 -- When no priority is specified but an xx_Handler pragma is, we default
12681 -- to System.Interrupts.Default_Interrupt_Priority, see D.3(10).
12683 elsif Has_Attach_Handler (Ptyp)
12684 or else Has_Interrupt_Handler (Ptyp)
12687 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
12689 -- Normal case, no priority or xx_Handler specified, default priority
12693 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
12696 -- Test for Compiler_Info parameter. This parameter allows entry body
12697 -- procedures and barrier functions to be called from the runtime. It
12698 -- is a pointer to the record generated by the compiler to represent
12699 -- the protected object.
12701 -- A protected type without entries that covers an interface and
12702 -- overrides the abstract routines with protected procedures is
12703 -- considered equivalent to a protected type with entries in the
12704 -- context of dispatching select statements.
12707 or else Has_Interfaces (Protect_Rec)
12709 ((Has_Attach_Handler (Ptyp) or else Has_Interrupt_Handler (Ptyp))
12710 and then not Restriction_Active (No_Dynamic_Attachment))
12713 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
12715 Called_Subp : RE_Id;
12719 when System_Tasking_Protected_Objects_Entries =>
12720 Called_Subp := RE_Initialize_Protection_Entries;
12722 when System_Tasking_Protected_Objects =>
12723 Called_Subp := RE_Initialize_Protection;
12725 when System_Tasking_Protected_Objects_Single_Entry =>
12726 Called_Subp := RE_Initialize_Protection_Entry;
12729 raise Program_Error;
12733 or else not Restricted
12734 or else Has_Interfaces (Protect_Rec)
12737 Make_Attribute_Reference (Loc,
12738 Prefix => Make_Identifier (Loc, Name_uInit),
12739 Attribute_Name => Name_Address));
12742 -- Entry_Bodies parameter. This is a pointer to an array of
12743 -- pointers to the entry body procedures and barrier functions of
12744 -- the object. If the protected type has no entries this object
12745 -- will not exist, in this case, pass a null.
12748 P_Arr := Entry_Bodies_Array (Ptyp);
12751 Make_Attribute_Reference (Loc,
12752 Prefix => New_Reference_To (P_Arr, Loc),
12753 Attribute_Name => Name_Unrestricted_Access));
12755 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
12757 -- Find index mapping function (clumsy but ok for now)
12759 while Ekind (P_Arr) /= E_Function loop
12760 Next_Entity (P_Arr);
12764 Make_Attribute_Reference (Loc,
12765 Prefix => New_Reference_To (P_Arr, Loc),
12766 Attribute_Name => Name_Unrestricted_Access));
12768 -- Build_Entry_Names generation flag. When set to true, the
12769 -- runtime will allocate an array to hold the string names
12770 -- of protected entries.
12772 if not Restricted_Profile then
12773 if Entry_Names_OK then
12775 New_Reference_To (Standard_True, Loc));
12778 New_Reference_To (Standard_False, Loc));
12783 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
12784 Append_To (Args, Make_Null (Loc));
12786 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
12787 Append_To (Args, Make_Null (Loc));
12788 Append_To (Args, Make_Null (Loc));
12789 Append_To (Args, New_Reference_To (Standard_False, Loc));
12793 Make_Procedure_Call_Statement (Loc,
12794 Name => New_Reference_To (RTE (Called_Subp), Loc),
12795 Parameter_Associations => Args));
12799 Make_Procedure_Call_Statement (Loc,
12800 Name => New_Reference_To (RTE (RE_Initialize_Protection), Loc),
12801 Parameter_Associations => Args));
12804 if Has_Attach_Handler (Ptyp) then
12806 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
12807 -- make the following call:
12809 -- Install_Handlers (_object,
12810 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
12812 -- or, in the case of Ravenscar:
12814 -- Install_Restricted_Handlers
12815 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
12818 Args : constant List_Id := New_List;
12819 Table : constant List_Id := New_List;
12820 Ritem : Node_Id := First_Rep_Item (Ptyp);
12823 -- Build the Attach_Handler table argument
12825 while Present (Ritem) loop
12826 if Nkind (Ritem) = N_Pragma
12827 and then Pragma_Name (Ritem) = Name_Attach_Handler
12830 Handler : constant Node_Id :=
12831 First (Pragma_Argument_Associations (Ritem));
12833 Interrupt : constant Node_Id := Next (Handler);
12834 Expr : constant Node_Id := Expression (Interrupt);
12838 Make_Aggregate (Loc, Expressions => New_List (
12839 Unchecked_Convert_To
12840 (RTE (RE_System_Interrupt_Id), Expr),
12841 Make_Attribute_Reference (Loc,
12842 Prefix => Make_Selected_Component (Loc,
12843 Make_Identifier (Loc, Name_uInit),
12844 Duplicate_Subexpr_No_Checks
12845 (Expression (Handler))),
12846 Attribute_Name => Name_Access))));
12850 Next_Rep_Item (Ritem);
12853 -- Append the table argument we just built
12855 Append_To (Args, Make_Aggregate (Loc, Table));
12857 -- Append the Install_Handlers (or Install_Restricted_Handlers)
12858 -- call to the statements.
12861 -- Call a simplified version of Install_Handlers to be used
12862 -- when the Ravenscar restrictions are in effect
12863 -- (Install_Restricted_Handlers).
12866 Make_Procedure_Call_Statement (Loc,
12869 (RTE (RE_Install_Restricted_Handlers), Loc),
12870 Parameter_Associations => Args));
12873 -- First, prepends the _object argument
12876 Make_Attribute_Reference (Loc,
12878 Make_Selected_Component (Loc,
12879 Prefix => Make_Identifier (Loc, Name_uInit),
12880 Selector_Name => Make_Identifier (Loc, Name_uObject)),
12881 Attribute_Name => Name_Unchecked_Access));
12883 -- Then, insert call to Install_Handlers
12886 Make_Procedure_Call_Statement (Loc,
12887 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
12888 Parameter_Associations => Args));
12894 end Make_Initialize_Protection;
12896 ---------------------------
12897 -- Make_Task_Create_Call --
12898 ---------------------------
12900 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
12901 Loc : constant Source_Ptr := Sloc (Task_Rec);
12911 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
12912 Tnam := Chars (Ttyp);
12914 -- Get task declaration. In the case of a task type declaration, this is
12915 -- simply the parent of the task type entity. In the single task
12916 -- declaration, this parent will be the implicit type, and we can find
12917 -- the corresponding single task declaration by searching forward in the
12918 -- declaration list in the tree.
12920 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
12921 -- this type should have been removed during semantic analysis.
12923 Tdec := Parent (Ttyp);
12924 while not Nkind_In (Tdec, N_Task_Type_Declaration,
12925 N_Single_Task_Declaration)
12930 -- Now we can find the task definition from this declaration
12932 Tdef := Task_Definition (Tdec);
12934 -- Build the parameter list for the call. Note that _Init is the name
12935 -- of the formal for the object to be initialized, which is the task
12936 -- value record itself.
12940 -- Priority parameter. Set to Unspecified_Priority unless there is a
12941 -- priority pragma, in which case we take the value from the pragma.
12943 if Present (Tdef) and then Has_Pragma_Priority (Tdef) then
12945 Make_Selected_Component (Loc,
12946 Prefix => Make_Identifier (Loc, Name_uInit),
12947 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
12950 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
12953 -- Optional Stack parameter
12955 if Restricted_Profile then
12957 -- If the stack has been preallocated by the expander then
12958 -- pass its address. Otherwise, pass a null address.
12960 if Preallocated_Stacks_On_Target then
12962 Make_Attribute_Reference (Loc,
12964 Make_Selected_Component (Loc,
12965 Prefix => Make_Identifier (Loc, Name_uInit),
12966 Selector_Name => Make_Identifier (Loc, Name_uStack)),
12967 Attribute_Name => Name_Address));
12971 New_Reference_To (RTE (RE_Null_Address), Loc));
12975 -- Size parameter. If no Storage_Size pragma is present, then
12976 -- the size is taken from the taskZ variable for the type, which
12977 -- is either Unspecified_Size, or has been reset by the use of
12978 -- a Storage_Size attribute definition clause. If a pragma is
12979 -- present, then the size is taken from the _Size field of the
12980 -- task value record, which was set from the pragma value.
12983 and then Has_Storage_Size_Pragma (Tdef)
12986 Make_Selected_Component (Loc,
12987 Prefix => Make_Identifier (Loc, Name_uInit),
12988 Selector_Name => Make_Identifier (Loc, Name_uSize)));
12992 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
12995 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
12996 -- Task_Info pragma, in which case we take the value from the pragma.
12999 and then Has_Task_Info_Pragma (Tdef)
13002 Make_Selected_Component (Loc,
13003 Prefix => Make_Identifier (Loc, Name_uInit),
13004 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
13008 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
13011 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU pragma,
13012 -- in which case we take the value from the pragma. The parameter is
13013 -- passed as an Integer because in the case of unspecified CPU the
13014 -- value is not in the range of CPU_Range.
13016 if Present (Tdef) and then Has_Pragma_CPU (Tdef) then
13018 Convert_To (Standard_Integer,
13019 Make_Selected_Component (Loc,
13020 Prefix => Make_Identifier (Loc, Name_uInit),
13021 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
13025 New_Reference_To (RTE (RE_Unspecified_CPU), Loc));
13028 if not Restricted_Profile then
13030 -- Deadline parameter. If no Relative_Deadline pragma is present,
13031 -- then the deadline is Time_Span_Zero. If a pragma is present, then
13032 -- the deadline is taken from the _Relative_Deadline field of the
13033 -- task value record, which was set from the pragma value. Note that
13034 -- this parameter must not be generated for the restricted profiles
13035 -- since Ravenscar does not allow deadlines.
13037 -- Case where pragma Relative_Deadline applies: use given value
13039 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
13041 Make_Selected_Component (Loc,
13043 Make_Identifier (Loc, Name_uInit),
13045 Make_Identifier (Loc, Name_uRelative_Deadline)));
13047 -- No pragma Relative_Deadline apply to the task
13051 New_Reference_To (RTE (RE_Time_Span_Zero), Loc));
13054 -- Dispatching_Domain parameter. If no Dispatching_Domain pragma or
13055 -- aspect is present, then the dispatching domain is null. If a
13056 -- pragma or aspect is present, then the dispatching domain is taken
13057 -- from the _Dispatching_Domain field of the task value record,
13058 -- which was set from the pragma value. Note that this parameter
13059 -- must not be generated for the restricted profiles since Ravenscar
13060 -- does not allow dispatching domains.
13062 -- Case where pragma or aspect Dispatching_Domain applies: use given
13065 if Present (Tdef) and then Has_Pragma_Dispatching_Domain (Tdef) then
13067 Make_Selected_Component (Loc,
13069 Make_Identifier (Loc, Name_uInit),
13071 Make_Identifier (Loc, Name_uDispatching_Domain)));
13073 -- No pragma or aspect Dispatching_Domain apply to the task
13076 Append_To (Args, Make_Null (Loc));
13079 -- Number of entries. This is an expression of the form:
13081 -- n + _Init.a'Length + _Init.a'B'Length + ...
13083 -- where a,b... are the entry family names for the task definition
13086 Build_Entry_Count_Expression
13091 (Parent (Corresponding_Record_Type (Ttyp))))),
13093 Append_To (Args, Ecount);
13095 -- Master parameter. This is a reference to the _Master parameter of
13096 -- the initialization procedure, except in the case of the pragma
13097 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
13098 -- System.Tasking.Library_Task_Level.
13100 if Restriction_Active (No_Task_Hierarchy) = False then
13101 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
13104 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
13108 -- State parameter. This is a pointer to the task body procedure. The
13109 -- required value is obtained by taking 'Unrestricted_Access of the task
13110 -- body procedure and converting it (with an unchecked conversion) to
13111 -- the type required by the task kernel. For further details, see the
13112 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
13113 -- than 'Address in order to avoid creating trampolines.
13116 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
13117 Subp_Ptr_Typ : constant Node_Id :=
13118 Create_Itype (E_Access_Subprogram_Type, Tdec);
13119 Ref : constant Node_Id := Make_Itype_Reference (Loc);
13122 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
13123 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
13125 -- Be sure to freeze a reference to the access-to-subprogram type,
13126 -- otherwise gigi will complain that it's in the wrong scope, because
13127 -- it's actually inside the init procedure for the record type that
13128 -- corresponds to the task type.
13130 -- This processing is causing a crash in the .NET/JVM back ends that
13131 -- is not yet understood, so skip it in these cases ???
13133 if VM_Target = No_VM then
13134 Set_Itype (Ref, Subp_Ptr_Typ);
13135 Append_Freeze_Action (Task_Rec, Ref);
13138 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
13139 Make_Qualified_Expression (Loc,
13140 Subtype_Mark => New_Reference_To (Subp_Ptr_Typ, Loc),
13142 Make_Attribute_Reference (Loc,
13144 New_Occurrence_Of (Body_Proc, Loc),
13145 Attribute_Name => Name_Unrestricted_Access))));
13147 -- For the .NET/JVM cases revert to the original code below ???
13151 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
13152 Make_Attribute_Reference (Loc,
13154 New_Occurrence_Of (Body_Proc, Loc),
13155 Attribute_Name => Name_Address)));
13159 -- Discriminants parameter. This is just the address of the task
13160 -- value record itself (which contains the discriminant values
13163 Make_Attribute_Reference (Loc,
13164 Prefix => Make_Identifier (Loc, Name_uInit),
13165 Attribute_Name => Name_Address));
13167 -- Elaborated parameter. This is an access to the elaboration Boolean
13170 Make_Attribute_Reference (Loc,
13171 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
13172 Attribute_Name => Name_Unchecked_Access));
13174 -- Chain parameter. This is a reference to the _Chain parameter of
13175 -- the initialization procedure.
13177 Append_To (Args, Make_Identifier (Loc, Name_uChain));
13179 -- Task name parameter. Take this from the _Task_Id parameter to the
13180 -- init call unless there is a Task_Name pragma, in which case we take
13181 -- the value from the pragma.
13184 and then Has_Task_Name_Pragma (Tdef)
13186 -- Copy expression in full, because it may be dynamic and have
13192 (Pragma_Argument_Associations
13193 (Find_Task_Or_Protected_Pragma
13194 (Tdef, Name_Task_Name))))));
13197 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
13200 -- Created_Task parameter. This is the _Task_Id field of the task
13204 Make_Selected_Component (Loc,
13205 Prefix => Make_Identifier (Loc, Name_uInit),
13206 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
13208 -- Build_Entry_Names generation flag. When set to true, the runtime
13209 -- will allocate an array to hold the string names of task entries.
13211 if not Restricted_Profile then
13212 if Has_Entries (Ttyp)
13213 and then Entry_Names_OK
13215 Append_To (Args, New_Reference_To (Standard_True, Loc));
13217 Append_To (Args, New_Reference_To (Standard_False, Loc));
13221 if Restricted_Profile then
13222 Name := New_Reference_To (RTE (RE_Create_Restricted_Task), Loc);
13224 Name := New_Reference_To (RTE (RE_Create_Task), Loc);
13228 Make_Procedure_Call_Statement (Loc,
13230 Parameter_Associations => Args);
13231 end Make_Task_Create_Call;
13233 ------------------------------
13234 -- Next_Protected_Operation --
13235 ------------------------------
13237 function Next_Protected_Operation (N : Node_Id) return Node_Id is
13241 Next_Op := Next (N);
13242 while Present (Next_Op)
13243 and then not Nkind_In (Next_Op, N_Subprogram_Body, N_Entry_Body)
13249 end Next_Protected_Operation;
13251 ---------------------
13252 -- Null_Statements --
13253 ---------------------
13255 function Null_Statements (Stats : List_Id) return Boolean is
13259 Stmt := First (Stats);
13260 while Nkind (Stmt) /= N_Empty
13261 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
13263 (Nkind (Stmt) = N_Pragma
13264 and then (Pragma_Name (Stmt) = Name_Unreferenced
13266 Pragma_Name (Stmt) = Name_Unmodified
13268 Pragma_Name (Stmt) = Name_Warnings)))
13273 return Nkind (Stmt) = N_Empty;
13274 end Null_Statements;
13276 --------------------------
13277 -- Parameter_Block_Pack --
13278 --------------------------
13280 function Parameter_Block_Pack
13282 Blk_Typ : Entity_Id;
13286 Stmts : List_Id) return Node_Id
13288 Actual : Entity_Id;
13289 Expr : Node_Id := Empty;
13290 Formal : Entity_Id;
13291 Has_Param : Boolean := False;
13294 Temp_Asn : Node_Id;
13295 Temp_Nam : Node_Id;
13298 Actual := First (Actuals);
13299 Formal := Defining_Identifier (First (Formals));
13300 Params := New_List;
13302 while Present (Actual) loop
13303 if Is_By_Copy_Type (Etype (Actual)) then
13305 -- Jnn : aliased <formal-type>
13307 Temp_Nam := Make_Temporary (Loc, 'J');
13310 Make_Object_Declaration (Loc,
13313 Defining_Identifier =>
13315 Object_Definition =>
13316 New_Reference_To (Etype (Formal), Loc)));
13318 if Ekind (Formal) /= E_Out_Parameter then
13324 New_Reference_To (Temp_Nam, Loc);
13326 Set_Assignment_OK (Temp_Asn);
13329 Make_Assignment_Statement (Loc,
13333 New_Copy_Tree (Actual)));
13337 -- Jnn'unchecked_access
13340 Make_Attribute_Reference (Loc,
13342 Name_Unchecked_Access,
13344 New_Reference_To (Temp_Nam, Loc)));
13348 -- The controlling parameter is omitted
13351 if not Is_Controlling_Actual (Actual) then
13353 Make_Reference (Loc, New_Copy_Tree (Actual)));
13359 Next_Actual (Actual);
13360 Next_Formal_With_Extras (Formal);
13364 Expr := Make_Aggregate (Loc, Params);
13369 -- J1'unchecked_access;
13370 -- <actual2>'reference;
13373 P := Make_Temporary (Loc, 'P');
13376 Make_Object_Declaration (Loc,
13377 Defining_Identifier =>
13379 Object_Definition =>
13380 New_Reference_To (Blk_Typ, Loc),
13385 end Parameter_Block_Pack;
13387 ----------------------------
13388 -- Parameter_Block_Unpack --
13389 ----------------------------
13391 function Parameter_Block_Unpack
13395 Formals : List_Id) return List_Id
13397 Actual : Entity_Id;
13399 Formal : Entity_Id;
13400 Has_Asnmt : Boolean := False;
13401 Result : constant List_Id := New_List;
13404 Actual := First (Actuals);
13405 Formal := Defining_Identifier (First (Formals));
13406 while Present (Actual) loop
13407 if Is_By_Copy_Type (Etype (Actual))
13408 and then Ekind (Formal) /= E_In_Parameter
13411 -- <actual> := P.<formal>;
13414 Make_Assignment_Statement (Loc,
13418 Make_Explicit_Dereference (Loc,
13419 Make_Selected_Component (Loc,
13421 New_Reference_To (P, Loc),
13423 Make_Identifier (Loc, Chars (Formal)))));
13425 Set_Assignment_OK (Name (Asnmt));
13426 Append_To (Result, Asnmt);
13431 Next_Actual (Actual);
13432 Next_Formal_With_Extras (Formal);
13438 return New_List (Make_Null_Statement (Loc));
13440 end Parameter_Block_Unpack;
13442 ----------------------
13443 -- Set_Discriminals --
13444 ----------------------
13446 procedure Set_Discriminals (Dec : Node_Id) is
13449 D_Minal : Entity_Id;
13452 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
13453 Pdef := Defining_Identifier (Dec);
13455 if Has_Discriminants (Pdef) then
13456 D := First_Discriminant (Pdef);
13457 while Present (D) loop
13459 Make_Defining_Identifier (Sloc (D),
13460 Chars => New_External_Name (Chars (D), 'D'));
13462 Set_Ekind (D_Minal, E_Constant);
13463 Set_Etype (D_Minal, Etype (D));
13464 Set_Scope (D_Minal, Pdef);
13465 Set_Discriminal (D, D_Minal);
13466 Set_Discriminal_Link (D_Minal, D);
13468 Next_Discriminant (D);
13471 end Set_Discriminals;
13473 -----------------------
13474 -- Trivial_Accept_OK --
13475 -----------------------
13477 function Trivial_Accept_OK return Boolean is
13479 case Opt.Task_Dispatching_Policy is
13481 -- If we have the default task dispatching policy in effect, we can
13482 -- definitely do the optimization (one way of looking at this is to
13483 -- think of the formal definition of the default policy being allowed
13484 -- to run any task it likes after a rendezvous, so even if notionally
13485 -- a full rescheduling occurs, we can say that our dispatching policy
13486 -- (i.e. the default dispatching policy) reorders the queue to be the
13487 -- same as just before the call.
13492 -- FIFO_Within_Priorities certainly does not permit this
13493 -- optimization since the Rendezvous is a scheduling action that may
13494 -- require some other task to be run.
13499 -- For now, disallow the optimization for all other policies. This
13500 -- may be over-conservative, but it is certainly not incorrect.
13506 end Trivial_Accept_OK;