1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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_Ch9; use Sem_Ch9;
55 with Sem_Ch11; use Sem_Ch11;
56 with Sem_Elab; use Sem_Elab;
57 with Sem_Eval; use Sem_Eval;
58 with Sem_Res; use Sem_Res;
59 with Sem_Util; use Sem_Util;
60 with Sinfo; use Sinfo;
61 with Snames; use Snames;
62 with Stand; use Stand;
63 with Stringt; use Stringt;
64 with Targparm; use Targparm;
65 with Tbuild; use Tbuild;
66 with Uintp; use Uintp;
68 package body Exp_Ch9 is
70 -- The following constant establishes the upper bound for the index of
71 -- an entry family. It is used to limit the allocated size of protected
72 -- types with defaulted discriminant of an integer type, when the bound
73 -- of some entry family depends on a discriminant. The limitation to entry
74 -- families of 128K should be reasonable in all cases, and is a documented
75 -- implementation restriction.
77 Entry_Family_Bound : constant Pos := 2**16;
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 function Actual_Index_Expression
87 Tsk : Entity_Id) return Node_Id;
88 -- Compute the index position for an entry call. Tsk is the target task. If
89 -- the bounds of some entry family depend on discriminants, the expression
90 -- computed by this function uses the discriminants of the target task.
92 procedure Add_Object_Pointer
96 -- Prepend an object pointer declaration to the declaration list Decls.
97 -- This object pointer is initialized to a type conversion of the System.
98 -- Address pointer passed to entry barrier functions and entry body
101 procedure Add_Formal_Renamings
106 -- Create renaming declarations for the formals, inside the procedure that
107 -- implements an entry body. The renamings make the original names of the
108 -- formals accessible to gdb, and serve no other purpose.
109 -- Spec is the specification of the procedure being built.
110 -- Decls is the list of declarations to be enhanced.
111 -- Ent is the entity for the original entry body.
113 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
114 -- Transform accept statement into a block with added exception handler.
115 -- Used both for simple accept statements and for accept alternatives in
116 -- select statements. Astat is the accept statement.
118 function Build_Barrier_Function
121 Pid : Node_Id) return Node_Id;
122 -- Build the function body returning the value of the barrier expression
123 -- for the specified entry body.
125 function Build_Barrier_Function_Specification
127 Def_Id : Entity_Id) return Node_Id;
128 -- Build a specification for a function implementing the protected entry
129 -- barrier of the specified entry body.
131 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id);
132 -- Build the body of a wrapper procedure for an entry or entry family that
133 -- has contract cases, preconditions, or postconditions. The body gathers
134 -- the executable contract items and expands them in the usual way, and
135 -- performs the entry call itself. This way preconditions are evaluated
136 -- before the call is queued. E is the entry in question, and Decl is the
137 -- enclosing synchronized type declaration at whose freeze point the
138 -- generated body is analyzed.
140 function Build_Corresponding_Record
143 Loc : Source_Ptr) return Node_Id;
144 -- Common to tasks and protected types. Copy discriminant specifications,
145 -- build record declaration. N is the type declaration, Ctyp is the
146 -- concurrent entity (task type or protected type).
148 function Build_Dispatching_Tag_Check
150 N : Node_Id) return Node_Id;
151 -- Utility to create the tree to check whether the dispatching call in
152 -- a timed entry call, a conditional entry call, or an asynchronous
153 -- transfer of control is a call to a primitive of a non-synchronized type.
154 -- K is the temporary that holds the tagged kind of the target object, and
155 -- N is the enclosing construct.
157 function Build_Entry_Count_Expression
158 (Concurrent_Type : Node_Id;
159 Component_List : List_Id;
160 Loc : Source_Ptr) return Node_Id;
161 -- Compute number of entries for concurrent object. This is a count of
162 -- simple entries, followed by an expression that computes the length
163 -- of the range of each entry family. A single array with that size is
164 -- allocated for each concurrent object of the type.
166 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
167 -- Build the function that translates the entry index in the call
168 -- (which depends on the size of entry families) into an index into the
169 -- Entry_Bodies_Array, to determine the body and barrier function used
170 -- in a protected entry call. A pointer to this function appears in every
173 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
174 -- Build subprogram declaration for previous one
176 function Build_Lock_Free_Protected_Subprogram_Body
179 Unprot_Spec : Node_Id) return Node_Id;
180 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
181 -- the subprogram specification of the unprotected version of N. Transform
182 -- N such that it invokes the unprotected version of the body.
184 function Build_Lock_Free_Unprotected_Subprogram_Body
186 Prot_Typ : Node_Id) return Node_Id;
187 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
188 -- of N where the original statements of N are synchronized through atomic
189 -- actions such as compare and exchange. Prior to invoking this routine, it
190 -- has been established that N can be implemented in a lock-free fashion.
192 function Build_Parameter_Block
196 Decls : List_Id) return Entity_Id;
197 -- Generate an access type for each actual parameter in the list Actuals.
198 -- Create an encapsulating record that contains all the actuals and return
199 -- its type. Generate:
200 -- type Ann1 is access all <actual1-type>
202 -- type AnnN is access all <actualN-type>
203 -- type Pnn is record
209 function Build_Protected_Entry
212 Pid : Node_Id) return Node_Id;
213 -- Build the procedure implementing the statement sequence of the specified
216 function Build_Protected_Entry_Specification
219 Ent_Id : Entity_Id) return Node_Id;
220 -- Build a specification for the procedure implementing the statements of
221 -- the specified entry body. Add attributes associating it with the entry
222 -- defining identifier Ent_Id.
224 function Build_Protected_Spec
226 Obj_Type : Entity_Id;
228 Unprotected : Boolean := False) return List_Id;
229 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
230 -- Subprogram_Type. Builds signature of protected subprogram, adding the
231 -- formal that corresponds to the object itself. For an access to protected
232 -- subprogram, there is no object type to specify, so the parameter has
233 -- type Address and mode In. An indirect call through such a pointer will
234 -- convert the address to a reference to the actual object. The object is
235 -- a limited record and therefore a by_reference type.
237 function Build_Protected_Subprogram_Body
240 N_Op_Spec : Node_Id) return Node_Id;
241 -- This function is used to construct the protected version of a protected
242 -- subprogram. Its statement sequence first defers abort, then locks the
243 -- associated protected object, and then enters a block that contains a
244 -- call to the unprotected version of the subprogram (for details, see
245 -- Build_Unprotected_Subprogram_Body). This block statement requires a
246 -- cleanup handler that unlocks the object in all cases. For details,
247 -- see Exp_Ch7.Expand_Cleanup_Actions.
249 function Build_Renamed_Formal_Declaration
253 Renamed_Formal : Node_Id) return Node_Id;
254 -- Create a renaming declaration for a formal, within a protected entry
255 -- body or an accept body. The renamed object is a component of the
256 -- parameter block that is a parameter in the entry call.
258 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
259 -- does not dereference the corresponding component to prevent an illegal
260 -- use of the incomplete type (AI05-0151).
262 function Build_Selected_Name
264 Selector : Entity_Id;
265 Append_Char : Character := ' ') return Name_Id;
266 -- Build a name in the form of Prefix__Selector, with an optional character
267 -- appended. This is used for internal subprograms generated for operations
268 -- of protected types, including barrier functions. For the subprograms
269 -- generated for entry bodies and entry barriers, the generated name
270 -- includes a sequence number that makes names unique in the presence of
271 -- entry overloading. This is necessary because entry body procedures and
272 -- barrier functions all have the same signature.
274 procedure Build_Simple_Entry_Call
279 -- Some comments here would be useful ???
281 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
282 -- This routine constructs a specification for the procedure that we will
283 -- build for the task body for task type T. The spec has the form:
285 -- procedure tnameB (_Task : access tnameV);
287 -- where name is the character name taken from the task type entity that
288 -- is passed as the argument to the procedure, and tnameV is the task
289 -- value type that is associated with the task type.
291 function Build_Unprotected_Subprogram_Body
293 Pid : Node_Id) return Node_Id;
294 -- This routine constructs the unprotected version of a protected
295 -- subprogram body, which is contains all of the code in the original,
296 -- unexpanded body. This is the version of the protected subprogram that is
297 -- called from all protected operations on the same object, including the
298 -- protected version of the same subprogram.
300 procedure Build_Wrapper_Bodies
304 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
305 -- record of a concurrent type. N is the insertion node where all bodies
306 -- will be placed. This routine builds the bodies of the subprograms which
307 -- serve as an indirection mechanism to overriding primitives of concurrent
308 -- types, entries and protected procedures. Any new body is analyzed.
310 procedure Build_Wrapper_Specs
314 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
315 -- record of a concurrent type. N is the insertion node where all specs
316 -- will be placed. This routine builds the specs of the subprograms which
317 -- serve as an indirection mechanism to overriding primitives of concurrent
318 -- types, entries and protected procedures. Any new spec is analyzed.
320 procedure Collect_Entry_Families
323 Current_Node : in out Node_Id;
324 Conctyp : Entity_Id);
325 -- For each entry family in a concurrent type, create an anonymous array
326 -- type of the right size, and add a component to the corresponding_record.
328 function Concurrent_Object
329 (Spec_Id : Entity_Id;
330 Conc_Typ : Entity_Id) return Entity_Id;
331 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
332 -- the entity associated with the concurrent object in the Protected_Body_
333 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
334 -- denotes formal parameter _O, _object or _task.
336 function Copy_Result_Type (Res : Node_Id) return Node_Id;
337 -- Copy the result type of a function specification, when building the
338 -- internal operation corresponding to a protected function, or when
339 -- expanding an access to protected function. If the result is an anonymous
340 -- access to subprogram itself, we need to create a new signature with the
341 -- same parameter names and the same resolved types, but with new entities
344 procedure Debug_Private_Data_Declarations (Decls : List_Id);
345 -- Decls is a list which may contain the declarations created by Install_
346 -- Private_Data_Declarations. All generated entities are marked as needing
347 -- debug info and debug nodes are manually generation where necessary. This
348 -- step of the expansion must to be done after private data has been moved
349 -- to its final resting scope to ensure proper visibility of debug objects.
351 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
352 -- If control flow optimizations are suppressed, and Alt is an accept,
353 -- delay, or entry call alternative with no trailing statements, insert
354 -- a null trailing statement with the given Loc (which is the sloc of
355 -- the accept, delay, or entry call statement). There might not be any
356 -- generated code for the accept, delay, or entry call itself (the effect
357 -- of these statements is part of the general processsing done for the
358 -- enclosing selective accept, timed entry call, or asynchronous select),
359 -- and the null statement is there to carry the sloc of that statement to
360 -- the back-end for trace-based coverage analysis purposes.
362 procedure Extract_Dispatching_Call
364 Call_Ent : out Entity_Id;
365 Object : out Entity_Id;
366 Actuals : out List_Id;
367 Formals : out List_Id);
368 -- Given a dispatching call, extract the entity of the name of the call,
369 -- its actual dispatching object, its actual parameters and the formal
370 -- parameters of the overridden interface-level version. If the type of
371 -- the dispatching object is an access type then an explicit dereference
372 -- is returned in Object.
374 procedure Extract_Entry
376 Concval : out Node_Id;
378 Index : out Node_Id);
379 -- Given an entry call, returns the associated concurrent object, the entry
380 -- name, and the entry family index.
382 function Family_Offset
387 Cap : Boolean) return Node_Id;
388 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
389 -- accept statement, or the upper bound in the discrete subtype of an entry
390 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
391 -- type of the entry. If Cap is true, the result is capped according to
392 -- Entry_Family_Bound.
399 Cap : Boolean) return Node_Id;
400 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
401 -- family, and handle properly the superflat case. This is equivalent to
402 -- the use of 'Length on the index type, but must use Family_Offset to
403 -- handle properly the case of bounds that depend on discriminants. If
404 -- Cap is true, the result is capped according to Entry_Family_Bound.
406 procedure Find_Enclosing_Context
408 Context : out Node_Id;
409 Context_Id : out Entity_Id;
410 Context_Decls : out List_Id);
411 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
412 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
413 -- nearest enclosing body, block, package, or return statement and return
414 -- its constituents. Context is the enclosing construct, Context_Id is
415 -- the scope of Context_Id and Context_Decls is the declarative list of
418 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
419 -- Given a subprogram identifier, return the entity which is associated
420 -- with the protection entry index in the Protected_Body_Subprogram or
421 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
424 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
425 -- Tell whether a given subprogram cannot raise an exception
427 function Is_Potentially_Large_Family
428 (Base_Index : Entity_Id;
431 Hi : Node_Id) return Boolean;
433 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
434 -- Determine whether Id is a function or a procedure and is marked as a
435 -- private primitive.
437 function Null_Statements (Stats : List_Id) return Boolean;
438 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
439 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
440 -- to still count as null. Returns True for a null sequence. The argument
441 -- is the list of statements from the DO-END sequence.
443 function Parameter_Block_Pack
449 Stmts : List_Id) return Entity_Id;
450 -- Set the components of the generated parameter block with the values
451 -- of the actual parameters. Generate aliased temporaries to capture the
452 -- values for types that are passed by copy. Otherwise generate a reference
453 -- to the actual's value. Return the address of the aggregate block.
455 -- Jnn1 : alias <formal-type1>;
456 -- Jnn1 := <actual1>;
459 -- Jnn1'unchecked_access;
460 -- <actual2>'reference;
463 function Parameter_Block_Unpack
467 Formals : List_Id) return List_Id;
468 -- Retrieve the values of the components from the parameter block and
469 -- assign then to the original actual parameters. Generate:
470 -- <actual1> := P.<formal1>;
472 -- <actualN> := P.<formalN>;
474 function Trivial_Accept_OK return Boolean;
475 -- If there is no DO-END block for an accept, or if the DO-END block has
476 -- only null statements, then it is possible to do the Rendezvous with much
477 -- less overhead using the Accept_Trivial routine in the run-time library.
478 -- However, this is not always a valid optimization. Whether it is valid or
479 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
480 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
481 -- a rescheduling is required, so this optimization is not allowed. This
482 -- function returns True if the optimization is permitted.
484 -----------------------------
485 -- Actual_Index_Expression --
486 -----------------------------
488 function Actual_Index_Expression
492 Tsk : Entity_Id) return Node_Id
494 Ttyp : constant Entity_Id := Etype (Tsk);
502 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
503 -- Compute difference between bounds of entry family
505 --------------------------
506 -- Actual_Family_Offset --
507 --------------------------
509 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
511 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
512 -- Replace a reference to a discriminant with a selected component
513 -- denoting the discriminant of the target task.
515 -----------------------------
516 -- Actual_Discriminant_Ref --
517 -----------------------------
519 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
520 Typ : constant Entity_Id := Etype (Bound);
524 if not Is_Entity_Name (Bound)
525 or else Ekind (Entity (Bound)) /= E_Discriminant
527 if Nkind (Bound) = N_Attribute_Reference then
530 B := New_Copy_Tree (Bound);
535 Make_Selected_Component (Sloc,
536 Prefix => New_Copy_Tree (Tsk),
537 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
539 Analyze_And_Resolve (B, Typ);
543 Make_Attribute_Reference (Sloc,
544 Attribute_Name => Name_Pos,
545 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
546 Expressions => New_List (B));
547 end Actual_Discriminant_Ref;
549 -- Start of processing for Actual_Family_Offset
553 Make_Op_Subtract (Sloc,
554 Left_Opnd => Actual_Discriminant_Ref (Hi),
555 Right_Opnd => Actual_Discriminant_Ref (Lo));
556 end Actual_Family_Offset;
558 -- Start of processing for Actual_Index_Expression
561 -- The queues of entries and entry families appear in textual order in
562 -- the associated record. The entry index is computed as the sum of the
563 -- number of queues for all entries that precede the designated one, to
564 -- which is added the index expression, if this expression denotes a
565 -- member of a family.
567 -- The following is a place holder for the count of simple entries
569 Num := Make_Integer_Literal (Sloc, 1);
571 -- We construct an expression which is a series of addition operations.
572 -- See comments in Entry_Index_Expression, which is identical in
575 if Present (Index) then
576 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
582 Actual_Family_Offset (
583 Make_Attribute_Reference (Sloc,
584 Attribute_Name => Name_Pos,
585 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
586 Expressions => New_List (Relocate_Node (Index))),
587 Type_Low_Bound (S)));
592 -- Now add lengths of preceding entries and entry families
594 Prev := First_Entity (Ttyp);
595 while Chars (Prev) /= Chars (Ent)
596 or else (Ekind (Prev) /= Ekind (Ent))
597 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
599 if Ekind (Prev) = E_Entry then
600 Set_Intval (Num, Intval (Num) + 1);
602 elsif Ekind (Prev) = E_Entry_Family then
604 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
606 -- The need for the following full view retrieval stems from this
607 -- complex case of nested generics and tasking:
610 -- type Formal_Index is range <>;
613 -- type Index is private;
620 -- type Index is new Formal_Index range 1 .. 10;
623 -- package body Outer is
625 -- entry Fam (Index); -- (2)
628 -- package body Inner is -- (3)
636 -- We are currently building the index expression for the entry
637 -- call "T.E" (1). Part of the expansion must mention the range
638 -- of the discrete type "Index" (2) of entry family "Fam".
640 -- However only the private view of type "Index" is available to
641 -- the inner generic (3) because there was no prior mention of
642 -- the type inside "Inner". This visibility requirement is
643 -- implicit and cannot be detected during the construction of
644 -- the generic trees and needs special handling.
647 and then Is_Private_Type (S)
648 and then Present (Full_View (S))
653 Lo := Type_Low_Bound (S);
654 Hi := Type_High_Bound (S);
661 Left_Opnd => Actual_Family_Offset (Hi, Lo),
662 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
664 -- Other components are anonymous types to be ignored
674 end Actual_Index_Expression;
676 --------------------------
677 -- Add_Formal_Renamings --
678 --------------------------
680 procedure Add_Formal_Renamings
686 Ptr : constant Entity_Id :=
688 (Next (First (Parameter_Specifications (Spec))));
689 -- The name of the formal that holds the address of the parameter block
696 Renamed_Formal : Node_Id;
699 Formal := First_Formal (Ent);
700 while Present (Formal) loop
701 Comp := Entry_Component (Formal);
703 Make_Defining_Identifier (Sloc (Formal),
704 Chars => Chars (Formal));
705 Set_Etype (New_F, Etype (Formal));
706 Set_Scope (New_F, Ent);
708 -- Now we set debug info needed on New_F even though it does not come
709 -- from source, so that the debugger will get the right information
710 -- for these generated names.
712 Set_Debug_Info_Needed (New_F);
714 if Ekind (Formal) = E_In_Parameter then
715 Set_Ekind (New_F, E_Constant);
717 Set_Ekind (New_F, E_Variable);
718 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
721 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
724 Make_Selected_Component (Loc,
726 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
727 Make_Identifier (Loc, Chars (Ptr))),
728 Selector_Name => New_Occurrence_Of (Comp, Loc));
731 Build_Renamed_Formal_Declaration
732 (New_F, Formal, Comp, Renamed_Formal);
734 Append (Decl, Decls);
735 Set_Renamed_Object (Formal, New_F);
736 Next_Formal (Formal);
738 end Add_Formal_Renamings;
740 ------------------------
741 -- Add_Object_Pointer --
742 ------------------------
744 procedure Add_Object_Pointer
746 Conc_Typ : Entity_Id;
749 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
754 -- Create the renaming declaration for the Protection object of a
755 -- protected type. _Object is used by Complete_Entry_Body.
756 -- ??? An attempt to make this a renaming was unsuccessful.
758 -- Build the entity for the access type
761 Make_Defining_Identifier (Loc,
762 New_External_Name (Chars (Rec_Typ), 'P'));
765 -- _object : poVP := poVP!O;
768 Make_Object_Declaration (Loc,
769 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
770 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
772 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
773 Set_Debug_Info_Needed (Defining_Identifier (Decl));
774 Prepend_To (Decls, Decl);
777 -- type poVP is access poV;
780 Make_Full_Type_Declaration (Loc,
781 Defining_Identifier =>
784 Make_Access_To_Object_Definition (Loc,
785 Subtype_Indication =>
786 New_Occurrence_Of (Rec_Typ, Loc)));
787 Set_Debug_Info_Needed (Defining_Identifier (Decl));
788 Prepend_To (Decls, Decl);
789 end Add_Object_Pointer;
791 -----------------------
792 -- Build_Accept_Body --
793 -----------------------
795 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
796 Loc : constant Source_Ptr := Sloc (Astat);
797 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
804 -- At the end of the statement sequence, Complete_Rendezvous is called.
805 -- A label skipping the Complete_Rendezvous, and all other accept
806 -- processing, has already been added for the expansion of requeue
807 -- statements. The Sloc is copied from the last statement since it
808 -- is really part of this last statement.
812 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
813 Insert_Before (Last (Statements (Stats)), Call);
816 -- If exception handlers are present, then append Complete_Rendezvous
817 -- calls to the handlers, and construct the required outer block. As
818 -- above, the Sloc is copied from the last statement in the sequence.
820 if Present (Exception_Handlers (Stats)) then
821 Hand := First (Exception_Handlers (Stats));
822 while Present (Hand) loop
825 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
826 Append (Call, Statements (Hand));
832 Make_Handled_Sequence_Of_Statements (Loc,
833 Statements => New_List (
834 Make_Block_Statement (Loc,
835 Handled_Statement_Sequence => Stats)));
841 -- At this stage we know that the new statement sequence does
842 -- not have an exception handler part, so we supply one to call
843 -- Exceptional_Complete_Rendezvous. This handler is
845 -- when all others =>
846 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
848 -- We handle Abort_Signal to make sure that we properly catch the abort
849 -- case and wake up the caller.
851 Ohandle := Make_Others_Choice (Loc);
852 Set_All_Others (Ohandle);
854 Set_Exception_Handlers (New_S,
856 Make_Implicit_Exception_Handler (Loc,
857 Exception_Choices => New_List (Ohandle),
859 Statements => New_List (
860 Make_Procedure_Call_Statement (Sloc (Stats),
861 Name => New_Occurrence_Of (
862 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
863 Parameter_Associations => New_List (
864 Make_Function_Call (Sloc (Stats),
867 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
869 Set_Parent (New_S, Astat); -- temp parent for Analyze call
870 Analyze_Exception_Handlers (Exception_Handlers (New_S));
871 Expand_Exception_Handlers (New_S);
873 -- Exceptional_Complete_Rendezvous must be called with abort still
874 -- deferred, which is the case for a "when all others" handler.
877 end Build_Accept_Body;
879 -----------------------------------
880 -- Build_Activation_Chain_Entity --
881 -----------------------------------
883 procedure Build_Activation_Chain_Entity (N : Node_Id) is
884 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
885 -- Determine whether an extended return statement has activation chain
887 --------------------------
888 -- Has_Activation_Chain --
889 --------------------------
891 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
895 Decl := First (Return_Object_Declarations (Stmt));
896 while Present (Decl) loop
897 if Nkind (Decl) = N_Object_Declaration
898 and then Chars (Defining_Identifier (Decl)) = Name_uChain
907 end Has_Activation_Chain;
912 Context_Id : Entity_Id;
915 -- Start of processing for Build_Activation_Chain_Entity
918 -- Activation chain is never used for sequential elaboration policy, see
919 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
921 if Partition_Elaboration_Policy = 'S' then
925 Find_Enclosing_Context (N, Context, Context_Id, Decls);
927 -- If activation chain entity has not been declared already, create one
929 if Nkind (Context) = N_Extended_Return_Statement
930 or else No (Activation_Chain_Entity (Context))
932 -- Since extended return statements do not store the entity of the
933 -- chain, examine the return object declarations to avoid creating
936 if Nkind (Context) = N_Extended_Return_Statement
937 and then Has_Activation_Chain (Context)
943 Loc : constant Source_Ptr := Sloc (Context);
948 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
950 -- Note: An extended return statement is not really a task
951 -- activator, but it does have an activation chain on which to
952 -- store the tasks temporarily. On successful return, the tasks
953 -- on this chain are moved to the chain passed in by the caller.
954 -- We do not build an Activation_Chain_Entity for an extended
955 -- return statement, because we do not want to build a call to
956 -- Activate_Tasks. Task activation is the responsibility of the
959 if Nkind (Context) /= N_Extended_Return_Statement then
960 Set_Activation_Chain_Entity (Context, Chain);
964 Make_Object_Declaration (Loc,
965 Defining_Identifier => Chain,
966 Aliased_Present => True,
968 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
970 Prepend_To (Decls, Decl);
972 -- Ensure that _chain appears in the proper scope of the context
974 if Context_Id /= Current_Scope then
975 Push_Scope (Context_Id);
983 end Build_Activation_Chain_Entity;
985 ----------------------------
986 -- Build_Barrier_Function --
987 ----------------------------
989 function Build_Barrier_Function
992 Pid : Node_Id) return Node_Id
994 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
995 Cond : constant Node_Id := Condition (Ent_Formals);
996 Loc : constant Source_Ptr := Sloc (Cond);
997 Func_Id : constant Entity_Id := Barrier_Function (Ent);
998 Op_Decls : constant List_Id := New_List;
1000 Func_Body : Node_Id;
1003 -- Add a declaration for the Protection object, renaming declarations
1004 -- for the discriminals and privals and finally a declaration for the
1005 -- entry family index (if applicable).
1007 Install_Private_Data_Declarations (Sloc (N),
1013 Family => Ekind (Ent) = E_Entry_Family);
1015 -- If compiling with -fpreserve-control-flow, make sure we insert an
1016 -- IF statement so that the back-end knows to generate a conditional
1017 -- branch instruction, even if the condition is just the name of a
1018 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1019 -- such redundant IF statements under -fpreserve-control-flow
1020 -- (whether coming from this routine, or directly from source).
1022 if Opt.Suppress_Control_Flow_Optimizations then
1024 Make_Implicit_If_Statement (Cond,
1026 Then_Statements => New_List (
1027 Make_Simple_Return_Statement (Loc,
1028 New_Occurrence_Of (Standard_True, Loc))),
1030 Else_Statements => New_List (
1031 Make_Simple_Return_Statement (Loc,
1032 New_Occurrence_Of (Standard_False, Loc))));
1035 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1038 -- Note: the condition in the barrier function needs to be properly
1039 -- processed for the C/Fortran boolean possibility, but this happens
1040 -- automatically since the return statement does this normalization.
1043 Make_Subprogram_Body (Loc,
1045 Build_Barrier_Function_Specification (Loc,
1046 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1047 Declarations => Op_Decls,
1048 Handled_Statement_Sequence =>
1049 Make_Handled_Sequence_Of_Statements (Loc,
1050 Statements => New_List (Stmt)));
1051 Set_Is_Entry_Barrier_Function (Func_Body);
1054 end Build_Barrier_Function;
1056 ------------------------------------------
1057 -- Build_Barrier_Function_Specification --
1058 ------------------------------------------
1060 function Build_Barrier_Function_Specification
1062 Def_Id : Entity_Id) return Node_Id
1065 Set_Debug_Info_Needed (Def_Id);
1068 Make_Function_Specification (Loc,
1069 Defining_Unit_Name => Def_Id,
1070 Parameter_Specifications => New_List (
1071 Make_Parameter_Specification (Loc,
1072 Defining_Identifier =>
1073 Make_Defining_Identifier (Loc, Name_uO),
1075 New_Occurrence_Of (RTE (RE_Address), Loc)),
1077 Make_Parameter_Specification (Loc,
1078 Defining_Identifier =>
1079 Make_Defining_Identifier (Loc, Name_uE),
1081 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1083 Result_Definition =>
1084 New_Occurrence_Of (Standard_Boolean, Loc));
1085 end Build_Barrier_Function_Specification;
1087 --------------------------
1088 -- Build_Call_With_Task --
1089 --------------------------
1091 function Build_Call_With_Task
1093 E : Entity_Id) return Node_Id
1095 Loc : constant Source_Ptr := Sloc (N);
1098 Make_Function_Call (Loc,
1099 Name => New_Occurrence_Of (E, Loc),
1100 Parameter_Associations => New_List (Concurrent_Ref (N)));
1101 end Build_Call_With_Task;
1103 -----------------------------
1104 -- Build_Class_Wide_Master --
1105 -----------------------------
1107 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1108 Loc : constant Source_Ptr := Sloc (Typ);
1109 Master_Decl : Node_Id;
1110 Master_Id : Entity_Id;
1111 Master_Scope : Entity_Id;
1113 Related_Node : Node_Id;
1117 -- Nothing to do if there is no task hierarchy
1119 if Restriction_Active (No_Task_Hierarchy) then
1123 -- Find the declaration that created the access type, which is either a
1124 -- type declaration, or an object declaration with an access definition,
1125 -- in which case the type is anonymous.
1127 if Is_Itype (Typ) then
1128 Related_Node := Associated_Node_For_Itype (Typ);
1130 Related_Node := Parent (Typ);
1133 Master_Scope := Find_Master_Scope (Typ);
1135 -- Nothing to do if the master scope already contains a _master entity.
1136 -- The only exception to this is the following scenario:
1139 -- Transient_Scope_1
1142 -- Transient_Scope_2
1145 -- In this case the source scope is marked as having the master entity
1146 -- even though the actual declaration appears inside an inner scope. If
1147 -- the second transient scope requires a _master, it cannot use the one
1148 -- already declared because the entity is not visible.
1150 Name_Id := Make_Identifier (Loc, Name_uMaster);
1151 Master_Decl := Empty;
1153 if not Has_Master_Entity (Master_Scope)
1154 or else No (Current_Entity_In_Scope (Name_Id))
1157 Set_Has_Master_Entity (Master_Scope);
1160 -- _master : constant Integer := Current_Master.all;
1163 Make_Object_Declaration (Loc,
1164 Defining_Identifier =>
1165 Make_Defining_Identifier (Loc, Name_uMaster),
1166 Constant_Present => True,
1167 Object_Definition =>
1168 New_Occurrence_Of (Standard_Integer, Loc),
1170 Make_Explicit_Dereference (Loc,
1171 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1173 Insert_Action (Find_Hook_Context (Related_Node), Master_Decl);
1174 Analyze (Master_Decl);
1176 -- Mark the containing scope as a task master. Masters associated
1177 -- with return statements are already marked at this stage (see
1178 -- Analyze_Subprogram_Body).
1180 if Ekind (Current_Scope) /= E_Return_Statement then
1182 Par : Node_Id := Related_Node;
1185 while Nkind (Par) /= N_Compilation_Unit loop
1186 Par := Parent (Par);
1188 -- If we fall off the top, we are at the outer level,
1189 -- and the environment task is our effective master,
1190 -- so nothing to mark.
1192 if Nkind_In (Par, N_Block_Statement,
1196 Set_Is_Task_Master (Par);
1206 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1209 -- typeMnn renames _master;
1212 Make_Object_Renaming_Declaration (Loc,
1213 Defining_Identifier => Master_Id,
1214 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1217 -- If the master is declared locally, add the renaming declaration
1218 -- immediately after it, to prevent access-before-elaboration in the
1221 if Present (Master_Decl) then
1222 Insert_After (Master_Decl, Ren_Decl);
1226 Insert_Action (Related_Node, Ren_Decl);
1229 Set_Master_Id (Typ, Master_Id);
1230 end Build_Class_Wide_Master;
1232 ----------------------------
1233 -- Build_Contract_Wrapper --
1234 ----------------------------
1236 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id) is
1237 Conc_Typ : constant Entity_Id := Scope (E);
1238 Loc : constant Source_Ptr := Sloc (E);
1240 procedure Add_Discriminant_Renamings
1241 (Obj_Id : Entity_Id;
1243 -- Add renaming declarations for all discriminants of concurrent type
1244 -- Conc_Typ. Obj_Id is the entity of the wrapper formal parameter which
1245 -- represents the concurrent object.
1247 procedure Add_Matching_Formals
1249 Actuals : in out List_Id);
1250 -- Add formal parameters that match those of entry E to list Formals.
1251 -- The routine also adds matching actuals for the new formals to list
1254 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id);
1255 -- Relocate pragma Prag to list To. The routine creates a new list if
1256 -- To does not exist.
1258 --------------------------------
1259 -- Add_Discriminant_Renamings --
1260 --------------------------------
1262 procedure Add_Discriminant_Renamings
1263 (Obj_Id : Entity_Id;
1269 -- Inspect the discriminants of the concurrent type and generate a
1270 -- renaming for each one.
1272 if Has_Discriminants (Conc_Typ) then
1273 Discr := First_Discriminant (Conc_Typ);
1274 while Present (Discr) loop
1276 Make_Object_Renaming_Declaration (Loc,
1277 Defining_Identifier =>
1278 Make_Defining_Identifier (Loc, Chars (Discr)),
1280 New_Occurrence_Of (Etype (Discr), Loc),
1282 Make_Selected_Component (Loc,
1283 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1285 Make_Identifier (Loc, Chars (Discr)))));
1287 Next_Discriminant (Discr);
1290 end Add_Discriminant_Renamings;
1292 --------------------------
1293 -- Add_Matching_Formals --
1294 --------------------------
1296 procedure Add_Matching_Formals
1298 Actuals : in out List_Id)
1301 New_Formal : Entity_Id;
1304 -- Inspect the formal parameters of the entry and generate a new
1305 -- matching formal with the same name for the wrapper. A reference
1306 -- to the new formal becomes an actual in the entry call.
1308 Formal := First_Formal (E);
1309 while Present (Formal) loop
1310 New_Formal := Make_Defining_Identifier (Loc, Chars (Formal));
1312 Make_Parameter_Specification (Loc,
1313 Defining_Identifier => New_Formal,
1314 In_Present => In_Present (Parent (Formal)),
1315 Out_Present => Out_Present (Parent (Formal)),
1317 New_Occurrence_Of (Etype (Formal), Loc)));
1319 if No (Actuals) then
1320 Actuals := New_List;
1323 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1324 Next_Formal (Formal);
1326 end Add_Matching_Formals;
1328 ---------------------
1329 -- Transfer_Pragma --
1330 ---------------------
1332 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id) is
1340 New_Prag := Relocate_Node (Prag);
1342 Set_Analyzed (New_Prag, False);
1343 Append (New_Prag, To);
1344 end Transfer_Pragma;
1348 Items : constant Node_Id := Contract (E);
1349 Actuals : List_Id := No_List;
1352 Decls : List_Id := No_List;
1354 Has_Pragma : Boolean := False;
1355 Index_Id : Entity_Id;
1358 Wrapper_Id : Entity_Id;
1360 -- Start of processing for Build_Contract_Wrapper
1363 -- This routine generates a specialized wrapper for a protected or task
1364 -- entry [family] which implements precondition/postcondition semantics.
1365 -- Preconditions and case guards of contract cases are checked before
1366 -- the protected action or rendezvous takes place. Postconditions and
1367 -- consequences of contract cases are checked after the protected action
1368 -- or rendezvous takes place. The structure of the generated wrapper is
1371 -- procedure Wrapper
1372 -- (Obj_Id : Conc_Typ; -- concurrent object
1373 -- [Index : Index_Typ;] -- index of entry family
1374 -- [Formal_1 : ...; -- parameters of original entry
1377 -- [Discr_1 : ... renames Obj_Id.Discr_1; -- discriminant
1378 -- Discr_N : ... renames Obj_Id.Discr_N;] -- renamings
1380 -- <precondition checks>
1381 -- <case guard checks>
1383 -- procedure _Postconditions is
1385 -- <postcondition checks>
1386 -- <consequence checks>
1387 -- end _Postconditions;
1390 -- Entry_Call (Obj_Id, [Index,] [Formal_1, Formal_N]);
1394 -- Create the wrapper only when the entry has at least one executable
1395 -- contract item such as contract cases, precondition or postcondition.
1397 if Present (Items) then
1399 -- Inspect the list of pre/postconditions and transfer all available
1400 -- pragmas to the declarative list of the wrapper.
1402 Prag := Pre_Post_Conditions (Items);
1403 while Present (Prag) loop
1404 if Nam_In (Pragma_Name (Prag), Name_Postcondition,
1406 and then Is_Checked (Prag)
1409 Transfer_Pragma (Prag, To => Decls);
1412 Prag := Next_Pragma (Prag);
1415 -- Inspect the list of test/contract cases and transfer only contract
1416 -- cases pragmas to the declarative part of the wrapper.
1418 Prag := Contract_Test_Cases (Items);
1419 while Present (Prag) loop
1420 if Pragma_Name (Prag) = Name_Contract_Cases
1421 and then Is_Checked (Prag)
1424 Transfer_Pragma (Prag, To => Decls);
1427 Prag := Next_Pragma (Prag);
1431 -- The entry lacks executable contract items and a wrapper is not needed
1433 if not Has_Pragma then
1437 -- Create the profile of the wrapper. The first formal parameter is the
1438 -- concurrent object.
1441 Make_Defining_Identifier (Loc,
1442 Chars => New_External_Name (Chars (Conc_Typ), 'A'));
1444 Formals := New_List (
1445 Make_Parameter_Specification (Loc,
1446 Defining_Identifier => Obj_Id,
1447 Out_Present => True,
1449 Parameter_Type => New_Occurrence_Of (Conc_Typ, Loc)));
1451 -- Construct the call to the original entry. The call will be gradually
1452 -- augmented with an optional entry index and extra parameters.
1455 Make_Selected_Component (Loc,
1456 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1457 Selector_Name => New_Occurrence_Of (E, Loc));
1459 -- When creating a wrapper for an entry family, the second formal is the
1462 if Ekind (E) = E_Entry_Family then
1463 Index_Id := Make_Defining_Identifier (Loc, Name_I);
1466 Make_Parameter_Specification (Loc,
1467 Defining_Identifier => Index_Id,
1469 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1471 -- The call to the original entry becomes an indexed component to
1472 -- accommodate the entry index.
1475 Make_Indexed_Component (Loc,
1477 Expressions => New_List (New_Occurrence_Of (Index_Id, Loc)));
1480 -- Add formal parameters to match those of the entry and build actuals
1481 -- for the entry call.
1483 Add_Matching_Formals (Formals, Actuals);
1486 Make_Procedure_Call_Statement (Loc,
1488 Parameter_Associations => Actuals);
1490 -- Add renaming declarations for the discriminants of the enclosing type
1491 -- as the various contract items may reference them.
1493 Add_Discriminant_Renamings (Obj_Id, Decls);
1496 Make_Defining_Identifier (Loc, New_External_Name (Chars (E), 'E'));
1497 Set_Contract_Wrapper (E, Wrapper_Id);
1498 Set_Is_Entry_Wrapper (Wrapper_Id);
1500 -- The wrapper body is analyzed when the enclosing type is frozen
1502 Append_Freeze_Action (Defining_Entity (Decl),
1503 Make_Subprogram_Body (Loc,
1505 Make_Procedure_Specification (Loc,
1506 Defining_Unit_Name => Wrapper_Id,
1507 Parameter_Specifications => Formals),
1508 Declarations => Decls,
1509 Handled_Statement_Sequence =>
1510 Make_Handled_Sequence_Of_Statements (Loc,
1511 Statements => New_List (Call))));
1512 end Build_Contract_Wrapper;
1514 --------------------------------
1515 -- Build_Corresponding_Record --
1516 --------------------------------
1518 function Build_Corresponding_Record
1521 Loc : Source_Ptr) return Node_Id
1523 Rec_Ent : constant Entity_Id :=
1524 Make_Defining_Identifier
1525 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1528 New_Disc : Entity_Id;
1532 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1533 Set_Ekind (Rec_Ent, E_Record_Type);
1534 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1535 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1536 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1537 Set_Stored_Constraint (Rec_Ent, No_Elist);
1540 -- Use discriminals to create list of discriminants for record, and
1541 -- create new discriminals for use in default expressions, etc. It is
1542 -- worth noting that a task discriminant gives rise to 5 entities;
1544 -- a) The original discriminant.
1545 -- b) The discriminal for use in the task.
1546 -- c) The discriminant of the corresponding record.
1547 -- d) The discriminal for the init proc of the corresponding record.
1548 -- e) The local variable that renames the discriminant in the procedure
1549 -- for the task body.
1551 -- In fact the discriminals b) are used in the renaming declarations
1552 -- for e). See details in einfo (Handling of Discriminants).
1554 if Present (Discriminant_Specifications (N)) then
1556 Disc := First_Discriminant (Ctyp);
1558 while Present (Disc) loop
1559 New_Disc := CR_Discriminant (Disc);
1562 Make_Discriminant_Specification (Loc,
1563 Defining_Identifier => New_Disc,
1564 Discriminant_Type =>
1565 New_Occurrence_Of (Etype (Disc), Loc),
1567 New_Copy (Discriminant_Default_Value (Disc))));
1569 Next_Discriminant (Disc);
1576 -- Now we can construct the record type declaration. Note that this
1577 -- record is "limited tagged". It is "limited" to reflect the underlying
1578 -- limitedness of the task or protected object that it represents, and
1579 -- ensuring for example that it is properly passed by reference. It is
1580 -- "tagged" to give support to dispatching calls through interfaces. We
1581 -- propagate here the list of interfaces covered by the concurrent type
1582 -- (Ada 2005: AI-345).
1585 Make_Full_Type_Declaration (Loc,
1586 Defining_Identifier => Rec_Ent,
1587 Discriminant_Specifications => Dlist,
1589 Make_Record_Definition (Loc,
1591 Make_Component_List (Loc, Component_Items => Cdecls),
1593 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1594 Interface_List => Interface_List (N),
1595 Limited_Present => True));
1596 end Build_Corresponding_Record;
1598 ---------------------------------
1599 -- Build_Dispatching_Tag_Check --
1600 ---------------------------------
1602 function Build_Dispatching_Tag_Check
1604 N : Node_Id) return Node_Id
1606 Loc : constant Source_Ptr := Sloc (N);
1613 New_Occurrence_Of (K, Loc),
1615 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1619 New_Occurrence_Of (K, Loc),
1621 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1622 end Build_Dispatching_Tag_Check;
1624 ----------------------------------
1625 -- Build_Entry_Count_Expression --
1626 ----------------------------------
1628 function Build_Entry_Count_Expression
1629 (Concurrent_Type : Node_Id;
1630 Component_List : List_Id;
1631 Loc : Source_Ptr) return Node_Id
1643 -- Count number of non-family entries
1646 Ent := First_Entity (Concurrent_Type);
1647 while Present (Ent) loop
1648 if Ekind (Ent) = E_Entry then
1655 Ecount := Make_Integer_Literal (Loc, Eindx);
1657 -- Loop through entry families building the addition nodes
1659 Ent := First_Entity (Concurrent_Type);
1660 Comp := First (Component_List);
1661 while Present (Ent) loop
1662 if Ekind (Ent) = E_Entry_Family then
1663 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1667 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1668 Hi := Type_High_Bound (Typ);
1669 Lo := Type_Low_Bound (Typ);
1670 Large := Is_Potentially_Large_Family
1671 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1674 Left_Opnd => Ecount,
1676 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1683 end Build_Entry_Count_Expression;
1685 -----------------------
1686 -- Build_Entry_Names --
1687 -----------------------
1689 procedure Build_Entry_Names
1691 Obj_Typ : Entity_Id;
1694 Loc : constant Source_Ptr := Sloc (Obj_Ref);
1695 Data : Entity_Id := Empty;
1696 Index : Entity_Id := Empty;
1697 Typ : Entity_Id := Obj_Typ;
1699 procedure Build_Entry_Name (Comp_Id : Entity_Id);
1700 -- Given an entry [family], create a static string which denotes the
1701 -- name of Comp_Id and assign it to the underlying data structure which
1702 -- contains the entry names of a concurrent object.
1704 function Object_Reference return Node_Id;
1705 -- Return a reference to field _object or _task_id depending on the
1706 -- concurrent object being processed.
1708 ----------------------
1709 -- Build_Entry_Name --
1710 ----------------------
1712 procedure Build_Entry_Name (Comp_Id : Entity_Id) is
1713 function Build_Range (Def : Node_Id) return Node_Id;
1714 -- Given a discrete subtype definition of an entry family, generate a
1715 -- range node which covers the range of Def's type.
1717 procedure Create_Index_And_Data;
1718 -- Generate the declarations of variables Index and Data. Subsequent
1719 -- calls do nothing.
1721 function Increment_Index return Node_Id;
1722 -- Increment the index used in the assignment of string names to the
1725 function Name_Declaration (Def_Id : Entity_Id) return Node_Id;
1726 -- Given the name of a temporary variable, create the following
1727 -- declaration for it:
1729 -- Def_Id : aliased constant String := <String_Name_From_Buffer>;
1731 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id;
1732 -- Given the name of a temporary variable, place it in the array of
1733 -- string names. Generate:
1735 -- Data (Index) := Def_Id'Unchecked_Access;
1741 function Build_Range (Def : Node_Id) return Node_Id is
1742 High : Node_Id := Type_High_Bound (Etype (Def));
1743 Low : Node_Id := Type_Low_Bound (Etype (Def));
1746 -- If a bound references a discriminant, generate an identifier
1747 -- with the same name. Resolution will map it to the formals of
1750 if Is_Entity_Name (Low)
1751 and then Ekind (Entity (Low)) = E_Discriminant
1754 Make_Selected_Component (Loc,
1755 Prefix => New_Copy_Tree (Obj_Ref),
1756 Selector_Name => Make_Identifier (Loc, Chars (Low)));
1758 Low := New_Copy_Tree (Low);
1761 if Is_Entity_Name (High)
1762 and then Ekind (Entity (High)) = E_Discriminant
1765 Make_Selected_Component (Loc,
1766 Prefix => New_Copy_Tree (Obj_Ref),
1767 Selector_Name => Make_Identifier (Loc, Chars (High)));
1769 High := New_Copy_Tree (High);
1775 High_Bound => High);
1778 ---------------------------
1779 -- Create_Index_And_Data --
1780 ---------------------------
1782 procedure Create_Index_And_Data is
1784 if No (Index) and then No (Data) then
1791 if Is_Protected_Type (Typ) then
1792 Count := RO_PE_Number_Of_Entries;
1793 Data_Typ := RE_Protected_Entry_Names_Array;
1795 Count := RO_ST_Number_Of_Entries;
1796 Data_Typ := RE_Task_Entry_Names_Array;
1799 -- Step 1: Generate the declaration of the index variable:
1801 -- Index : Entry_Index := 1;
1803 Index := Make_Temporary (Loc, 'I');
1806 Make_Object_Declaration (Loc,
1807 Defining_Identifier => Index,
1808 Object_Definition =>
1809 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1810 Expression => Make_Integer_Literal (Loc, 1)));
1812 -- Step 2: Generate the declaration of an array to house all
1815 -- Size : constant Entry_Index := <Count> (Obj_Ref);
1816 -- Data : aliased <Data_Typ> := (1 .. Size => null);
1818 Size := Make_Temporary (Loc, 'S');
1821 Make_Object_Declaration (Loc,
1822 Defining_Identifier => Size,
1823 Constant_Present => True,
1824 Object_Definition =>
1825 New_Occurrence_Of (RTE (RE_Entry_Index), Loc),
1827 Make_Function_Call (Loc,
1829 New_Occurrence_Of (RTE (Count), Loc),
1830 Parameter_Associations =>
1831 New_List (Object_Reference))));
1833 Data := Make_Temporary (Loc, 'A');
1836 Make_Object_Declaration (Loc,
1837 Defining_Identifier => Data,
1838 Aliased_Present => True,
1839 Object_Definition =>
1840 New_Occurrence_Of (RTE (Data_Typ), Loc),
1842 Make_Aggregate (Loc,
1843 Component_Associations => New_List (
1844 Make_Component_Association (Loc,
1845 Choices => New_List (
1848 Make_Integer_Literal (Loc, 1),
1850 New_Occurrence_Of (Size, Loc))),
1851 Expression => Make_Null (Loc))))));
1854 end Create_Index_And_Data;
1856 ---------------------
1857 -- Increment_Index --
1858 ---------------------
1860 function Increment_Index return Node_Id is
1863 Make_Assignment_Statement (Loc,
1864 Name => New_Occurrence_Of (Index, Loc),
1867 Left_Opnd => New_Occurrence_Of (Index, Loc),
1868 Right_Opnd => Make_Integer_Literal (Loc, 1)));
1869 end Increment_Index;
1871 ----------------------
1872 -- Name_Declaration --
1873 ----------------------
1875 function Name_Declaration (Def_Id : Entity_Id) return Node_Id is
1878 Make_Object_Declaration (Loc,
1879 Defining_Identifier => Def_Id,
1880 Aliased_Present => True,
1881 Constant_Present => True,
1882 Object_Definition =>
1883 New_Occurrence_Of (Standard_String, Loc),
1885 Make_String_Literal (Loc, String_From_Name_Buffer));
1886 end Name_Declaration;
1888 --------------------
1889 -- Set_Entry_Name --
1890 --------------------
1892 function Set_Entry_Name (Def_Id : Entity_Id) return Node_Id is
1895 Make_Assignment_Statement (Loc,
1897 Make_Indexed_Component (Loc,
1898 Prefix => New_Occurrence_Of (Data, Loc),
1899 Expressions => New_List (New_Occurrence_Of (Index, Loc))),
1902 Make_Attribute_Reference (Loc,
1903 Prefix => New_Occurrence_Of (Def_Id, Loc),
1904 Attribute_Name => Name_Unchecked_Access));
1909 Temp_Id : Entity_Id;
1912 -- Start of processing for Build_Entry_Name
1915 if Ekind (Comp_Id) = E_Entry_Family then
1916 Subt_Def := Discrete_Subtype_Definition (Parent (Comp_Id));
1918 Create_Index_And_Data;
1920 -- Step 1: Create the string name of the entry family.
1922 -- Temp : aliased constant String := "name ()";
1924 Temp_Id := Make_Temporary (Loc, 'S');
1925 Get_Name_String (Chars (Comp_Id));
1926 Add_Char_To_Name_Buffer (' ');
1927 Add_Char_To_Name_Buffer ('(');
1928 Add_Char_To_Name_Buffer (')');
1930 Append_To (Stmts, Name_Declaration (Temp_Id));
1933 -- for Member in Family_Low .. Family_High loop
1934 -- Set_Entry_Name (...);
1935 -- Index := Index + 1;
1939 Make_Loop_Statement (Loc,
1941 Make_Iteration_Scheme (Loc,
1942 Loop_Parameter_Specification =>
1943 Make_Loop_Parameter_Specification (Loc,
1944 Defining_Identifier =>
1945 Make_Temporary (Loc, 'L'),
1946 Discrete_Subtype_Definition =>
1947 Build_Range (Subt_Def))),
1949 Statements => New_List (
1950 Set_Entry_Name (Temp_Id),
1952 End_Label => Empty));
1957 Create_Index_And_Data;
1959 -- Step 1: Create the string name of the entry. Generate:
1960 -- Temp : aliased constant String := "name";
1962 Temp_Id := Make_Temporary (Loc, 'S');
1963 Get_Name_String (Chars (Comp_Id));
1965 Append_To (Stmts, Name_Declaration (Temp_Id));
1967 -- Step 2: Associate the string name with the underlying data
1970 Append_To (Stmts, Set_Entry_Name (Temp_Id));
1971 Append_To (Stmts, Increment_Index);
1973 end Build_Entry_Name;
1975 ----------------------
1976 -- Object_Reference --
1977 ----------------------
1979 function Object_Reference return Node_Id is
1980 Conc_Typ : constant Entity_Id := Corresponding_Record_Type (Typ);
1985 if Is_Protected_Type (Typ) then
1986 Field := Name_uObject;
1988 Field := Name_uTask_Id;
1992 Make_Selected_Component (Loc,
1994 Unchecked_Convert_To (Conc_Typ, New_Copy_Tree (Obj_Ref)),
1995 Selector_Name => Make_Identifier (Loc, Field));
1997 if Is_Protected_Type (Typ) then
1999 Make_Attribute_Reference (Loc,
2001 Attribute_Name => Name_Unchecked_Access);
2005 end Object_Reference;
2012 -- Start of processing for Build_Entry_Names
2015 -- Retrieve the original concurrent type
2017 if Is_Concurrent_Record_Type (Typ) then
2018 Typ := Corresponding_Concurrent_Type (Typ);
2021 pragma Assert (Is_Concurrent_Type (Typ));
2023 -- Nothing to do if the type has no entries
2025 if not Has_Entries (Typ) then
2029 -- Avoid generating entry names for a protected type with only one entry
2031 if Is_Protected_Type (Typ)
2032 and then Find_Protection_Type (Base_Type (Typ)) /=
2033 RTE (RE_Protection_Entries)
2038 -- Step 1: Populate the array with statically generated strings denoting
2039 -- entries and entry family names.
2041 Comp := First_Entity (Typ);
2042 while Present (Comp) loop
2043 if Comes_From_Source (Comp)
2044 and then Ekind_In (Comp, E_Entry, E_Entry_Family)
2046 Build_Entry_Name (Comp);
2052 -- Step 2: Associate the array with the related concurrent object:
2054 -- Set_Entry_Names (Obj_Ref, <Data>'Unchecked_Access);
2056 if Present (Data) then
2057 if Is_Protected_Type (Typ) then
2058 Proc := RO_PE_Set_Entry_Names;
2060 Proc := RO_ST_Set_Entry_Names;
2064 Make_Procedure_Call_Statement (Loc,
2065 Name => New_Occurrence_Of (RTE (Proc), Loc),
2066 Parameter_Associations => New_List (
2068 Make_Attribute_Reference (Loc,
2069 Prefix => New_Occurrence_Of (Data, Loc),
2070 Attribute_Name => Name_Unchecked_Access))));
2072 end Build_Entry_Names;
2074 ---------------------------
2075 -- Build_Parameter_Block --
2076 ---------------------------
2078 function Build_Parameter_Block
2082 Decls : List_Id) return Entity_Id
2088 Has_Comp : Boolean := False;
2092 Actual := First (Actuals);
2094 Formal := Defining_Identifier (First (Formals));
2096 while Present (Actual) loop
2097 if not Is_Controlling_Actual (Actual) then
2100 -- type Ann is access all <actual-type>
2102 Comp_Nam := Make_Temporary (Loc, 'A');
2103 Set_Is_Param_Block_Component_Type (Comp_Nam);
2106 Make_Full_Type_Declaration (Loc,
2107 Defining_Identifier => Comp_Nam,
2109 Make_Access_To_Object_Definition (Loc,
2110 All_Present => True,
2111 Constant_Present => Ekind (Formal) = E_In_Parameter,
2112 Subtype_Indication =>
2113 New_Occurrence_Of (Etype (Actual), Loc))));
2119 Make_Component_Declaration (Loc,
2120 Defining_Identifier =>
2121 Make_Defining_Identifier (Loc, Chars (Formal)),
2122 Component_Definition =>
2123 Make_Component_Definition (Loc,
2126 Subtype_Indication =>
2127 New_Occurrence_Of (Comp_Nam, Loc))));
2132 Next_Actual (Actual);
2133 Next_Formal_With_Extras (Formal);
2136 Rec_Nam := Make_Temporary (Loc, 'P');
2141 -- type Pnn is record
2146 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
2147 -- the original parameter names and Ann1 .. AnnN are the access to
2151 Make_Full_Type_Declaration (Loc,
2152 Defining_Identifier =>
2155 Make_Record_Definition (Loc,
2157 Make_Component_List (Loc, Comps))));
2160 -- type Pnn is null record;
2163 Make_Full_Type_Declaration (Loc,
2164 Defining_Identifier =>
2167 Make_Record_Definition (Loc,
2168 Null_Present => True,
2169 Component_List => Empty)));
2173 end Build_Parameter_Block;
2175 --------------------------------------
2176 -- Build_Renamed_Formal_Declaration --
2177 --------------------------------------
2179 function Build_Renamed_Formal_Declaration
2183 Renamed_Formal : Node_Id) return Node_Id
2185 Loc : constant Source_Ptr := Sloc (New_F);
2189 -- If the formal is a tagged incomplete type, it is already passed
2190 -- by reference, so it is sufficient to rename the pointer component
2191 -- that corresponds to the actual. Otherwise we need to dereference
2192 -- the pointer component to obtain the actual.
2194 if Is_Incomplete_Type (Etype (Formal))
2195 and then Is_Tagged_Type (Etype (Formal))
2198 Make_Object_Renaming_Declaration (Loc,
2199 Defining_Identifier => New_F,
2200 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
2201 Name => Renamed_Formal);
2205 Make_Object_Renaming_Declaration (Loc,
2206 Defining_Identifier => New_F,
2207 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
2209 Make_Explicit_Dereference (Loc, Renamed_Formal));
2213 end Build_Renamed_Formal_Declaration;
2215 --------------------------
2216 -- Build_Wrapper_Bodies --
2217 --------------------------
2219 procedure Build_Wrapper_Bodies
2224 Rec_Typ : Entity_Id;
2226 function Build_Wrapper_Body
2228 Subp_Id : Entity_Id;
2229 Obj_Typ : Entity_Id;
2230 Formals : List_Id) return Node_Id;
2231 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
2232 -- associated with a protected or task type. Subp_Id is the subprogram
2233 -- name which will be wrapped. Obj_Typ is the type of the new formal
2234 -- parameter which handles dispatching and object notation. Formals are
2235 -- the original formals of Subp_Id which will be explicitly replicated.
2237 ------------------------
2238 -- Build_Wrapper_Body --
2239 ------------------------
2241 function Build_Wrapper_Body
2243 Subp_Id : Entity_Id;
2244 Obj_Typ : Entity_Id;
2245 Formals : List_Id) return Node_Id
2247 Body_Spec : Node_Id;
2250 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2252 -- The subprogram is not overriding or is not a primitive declared
2253 -- between two views.
2255 if No (Body_Spec) then
2260 Actuals : List_Id := No_List;
2262 First_Form : Node_Id;
2267 -- Map formals to actuals. Use the list built for the wrapper
2268 -- spec, skipping the object notation parameter.
2270 First_Form := First (Parameter_Specifications (Body_Spec));
2272 Formal := First_Form;
2275 if Present (Formal) then
2276 Actuals := New_List;
2277 while Present (Formal) loop
2279 Make_Identifier (Loc,
2280 Chars => Chars (Defining_Identifier (Formal))));
2285 -- Special processing for primitives declared between a private
2286 -- type and its completion: the wrapper needs a properly typed
2287 -- parameter if the wrapped operation has a controlling first
2288 -- parameter. Note that this might not be the case for a function
2289 -- with a controlling result.
2291 if Is_Private_Primitive_Subprogram (Subp_Id) then
2292 if No (Actuals) then
2293 Actuals := New_List;
2296 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2297 Prepend_To (Actuals,
2298 Unchecked_Convert_To
2299 (Corresponding_Concurrent_Type (Obj_Typ),
2300 Make_Identifier (Loc, Name_uO)));
2303 Prepend_To (Actuals,
2304 Make_Identifier (Loc,
2305 Chars => Chars (Defining_Identifier (First_Form))));
2308 Nam := New_Occurrence_Of (Subp_Id, Loc);
2310 -- An access-to-variable object parameter requires an explicit
2311 -- dereference in the unchecked conversion. This case occurs
2312 -- when a protected entry wrapper must override an interface
2313 -- level procedure with interface access as first parameter.
2315 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2317 if Nkind (Parameter_Type (First_Form)) =
2321 Make_Explicit_Dereference (Loc,
2322 Prefix => Make_Identifier (Loc, Name_uO));
2324 Conv_Id := Make_Identifier (Loc, Name_uO);
2328 Make_Selected_Component (Loc,
2330 Unchecked_Convert_To
2331 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2332 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
2335 -- Create the subprogram body. For a function, the call to the
2336 -- actual subprogram has to be converted to the corresponding
2337 -- record if it is a controlling result.
2339 if Ekind (Subp_Id) = E_Function then
2345 Make_Function_Call (Loc,
2347 Parameter_Associations => Actuals);
2349 if Has_Controlling_Result (Subp_Id) then
2351 Unchecked_Convert_To
2352 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2356 Make_Subprogram_Body (Loc,
2357 Specification => Body_Spec,
2358 Declarations => Empty_List,
2359 Handled_Statement_Sequence =>
2360 Make_Handled_Sequence_Of_Statements (Loc,
2361 Statements => New_List (
2362 Make_Simple_Return_Statement (Loc, Res))));
2367 Make_Subprogram_Body (Loc,
2368 Specification => Body_Spec,
2369 Declarations => Empty_List,
2370 Handled_Statement_Sequence =>
2371 Make_Handled_Sequence_Of_Statements (Loc,
2372 Statements => New_List (
2373 Make_Procedure_Call_Statement (Loc,
2375 Parameter_Associations => Actuals))));
2378 end Build_Wrapper_Body;
2380 -- Start of processing for Build_Wrapper_Bodies
2383 if Is_Concurrent_Type (Typ) then
2384 Rec_Typ := Corresponding_Record_Type (Typ);
2389 -- Generate wrapper bodies for a concurrent type which implements an
2392 if Present (Interfaces (Rec_Typ)) then
2394 Insert_Nod : Node_Id;
2396 Prim_Elmt : Elmt_Id;
2397 Prim_Decl : Node_Id;
2399 Wrap_Body : Node_Id;
2400 Wrap_Id : Entity_Id;
2405 -- Examine all primitive operations of the corresponding record
2406 -- type, looking for wrapper specs. Generate bodies in order to
2409 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2410 while Present (Prim_Elmt) loop
2411 Prim := Node (Prim_Elmt);
2413 if (Ekind (Prim) = E_Function
2414 or else Ekind (Prim) = E_Procedure)
2415 and then Is_Primitive_Wrapper (Prim)
2417 Subp := Wrapped_Entity (Prim);
2418 Prim_Decl := Parent (Parent (Prim));
2421 Build_Wrapper_Body (Loc,
2424 Formals => Parameter_Specifications (Parent (Subp)));
2425 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2427 Set_Corresponding_Spec (Wrap_Body, Prim);
2428 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2430 Insert_After (Insert_Nod, Wrap_Body);
2431 Insert_Nod := Wrap_Body;
2433 Analyze (Wrap_Body);
2436 Next_Elmt (Prim_Elmt);
2440 end Build_Wrapper_Bodies;
2442 ------------------------
2443 -- Build_Wrapper_Spec --
2444 ------------------------
2446 function Build_Wrapper_Spec
2447 (Subp_Id : Entity_Id;
2448 Obj_Typ : Entity_Id;
2449 Formals : List_Id) return Node_Id
2451 function Overriding_Possible
2452 (Iface_Op : Entity_Id;
2453 Wrapper : Entity_Id) return Boolean;
2454 -- Determine whether a primitive operation can be overridden by Wrapper.
2455 -- Iface_Op is the candidate primitive operation of an interface type,
2456 -- Wrapper is the generated entry wrapper.
2458 function Replicate_Formals
2460 Formals : List_Id) return List_Id;
2461 -- An explicit parameter replication is required due to the Is_Entry_
2462 -- Formal flag being set for all the formals of an entry. The explicit
2463 -- replication removes the flag that would otherwise cause a different
2464 -- path of analysis.
2466 -------------------------
2467 -- Overriding_Possible --
2468 -------------------------
2470 function Overriding_Possible
2471 (Iface_Op : Entity_Id;
2472 Wrapper : Entity_Id) return Boolean
2474 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2475 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2477 function Type_Conformant_Parameters
2478 (Iface_Op_Params : List_Id;
2479 Wrapper_Params : List_Id) return Boolean;
2480 -- Determine whether the parameters of the generated entry wrapper
2481 -- and those of a primitive operation are type conformant. During
2482 -- this check, the first parameter of the primitive operation is
2483 -- skipped if it is a controlling argument: protected functions
2484 -- may have a controlling result.
2486 --------------------------------
2487 -- Type_Conformant_Parameters --
2488 --------------------------------
2490 function Type_Conformant_Parameters
2491 (Iface_Op_Params : List_Id;
2492 Wrapper_Params : List_Id) return Boolean
2494 Iface_Op_Param : Node_Id;
2495 Iface_Op_Typ : Entity_Id;
2496 Wrapper_Param : Node_Id;
2497 Wrapper_Typ : Entity_Id;
2500 -- Skip the first (controlling) parameter of primitive operation
2502 Iface_Op_Param := First (Iface_Op_Params);
2504 if Present (First_Formal (Iface_Op))
2505 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2507 Iface_Op_Param := Next (Iface_Op_Param);
2510 Wrapper_Param := First (Wrapper_Params);
2511 while Present (Iface_Op_Param)
2512 and then Present (Wrapper_Param)
2514 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2515 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2517 -- The two parameters must be mode conformant
2519 if not Conforming_Types
2520 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2525 Next (Iface_Op_Param);
2526 Next (Wrapper_Param);
2529 -- One of the lists is longer than the other
2531 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2536 end Type_Conformant_Parameters;
2538 -- Start of processing for Overriding_Possible
2541 if Chars (Iface_Op) /= Chars (Wrapper) then
2545 -- If an inherited subprogram is implemented by a protected procedure
2546 -- or an entry, then the first parameter of the inherited subprogram
2547 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
2549 if Ekind (Iface_Op) = E_Procedure
2550 and then Present (Parameter_Specifications (Iface_Op_Spec))
2553 Obj_Param : constant Node_Id :=
2554 First (Parameter_Specifications (Iface_Op_Spec));
2556 if not Out_Present (Obj_Param)
2557 and then Nkind (Parameter_Type (Obj_Param)) /=
2566 Type_Conformant_Parameters
2567 (Parameter_Specifications (Iface_Op_Spec),
2568 Parameter_Specifications (Wrapper_Spec));
2569 end Overriding_Possible;
2571 -----------------------
2572 -- Replicate_Formals --
2573 -----------------------
2575 function Replicate_Formals
2577 Formals : List_Id) return List_Id
2579 New_Formals : constant List_Id := New_List;
2581 Param_Type : Node_Id;
2584 Formal := First (Formals);
2586 -- Skip the object parameter when dealing with primitives declared
2587 -- between two views.
2589 if Is_Private_Primitive_Subprogram (Subp_Id)
2590 and then not Has_Controlling_Result (Subp_Id)
2592 Formal := Next (Formal);
2595 while Present (Formal) loop
2597 -- Create an explicit copy of the entry parameter
2599 -- When creating the wrapper subprogram for a primitive operation
2600 -- of a protected interface we must construct an equivalent
2601 -- signature to that of the overriding operation. For regular
2602 -- parameters we can just use the type of the formal, but for
2603 -- access to subprogram parameters we need to reanalyze the
2604 -- parameter type to create local entities for the signature of
2605 -- the subprogram type. Using the entities of the overriding
2606 -- subprogram will result in out-of-scope errors in the back-end.
2608 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2609 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2612 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2615 Append_To (New_Formals,
2616 Make_Parameter_Specification (Loc,
2617 Defining_Identifier =>
2618 Make_Defining_Identifier (Loc,
2619 Chars => Chars (Defining_Identifier (Formal))),
2620 In_Present => In_Present (Formal),
2621 Out_Present => Out_Present (Formal),
2622 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2623 Parameter_Type => Param_Type));
2629 end Replicate_Formals;
2633 Loc : constant Source_Ptr := Sloc (Subp_Id);
2634 First_Param : Node_Id := Empty;
2636 Iface_Elmt : Elmt_Id;
2637 Iface_Op : Entity_Id;
2638 Iface_Op_Elmt : Elmt_Id;
2639 Overridden_Subp : Entity_Id;
2641 -- Start of processing for Build_Wrapper_Spec
2644 -- No point in building wrappers for untagged concurrent types
2646 pragma Assert (Is_Tagged_Type (Obj_Typ));
2648 -- Check if this subprogram has a profile that matches some interface
2651 Check_Synchronized_Overriding (Subp_Id, Overridden_Subp);
2653 if Present (Overridden_Subp) then
2655 First (Parameter_Specifications (Parent (Overridden_Subp)));
2657 -- An entry or a protected procedure can override a routine where the
2658 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2659 -- type. Since the wrapper must have the exact same signature as that of
2660 -- the overridden subprogram, we try to find the overriding candidate
2661 -- and use its controlling formal.
2663 -- Check every implemented interface
2665 elsif Present (Interfaces (Obj_Typ)) then
2666 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2667 Search : while Present (Iface_Elmt) loop
2668 Iface := Node (Iface_Elmt);
2670 -- Check every interface primitive
2672 if Present (Primitive_Operations (Iface)) then
2673 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2674 while Present (Iface_Op_Elmt) loop
2675 Iface_Op := Node (Iface_Op_Elmt);
2677 -- Ignore predefined primitives
2679 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2680 Iface_Op := Ultimate_Alias (Iface_Op);
2682 -- The current primitive operation can be overridden by
2683 -- the generated entry wrapper.
2685 if Overriding_Possible (Iface_Op, Subp_Id) then
2687 First (Parameter_Specifications (Parent (Iface_Op)));
2693 Next_Elmt (Iface_Op_Elmt);
2697 Next_Elmt (Iface_Elmt);
2701 -- Do not generate the wrapper if no interface primitive is covered by
2702 -- the subprogram and it is not a primitive declared between two views
2703 -- (see Process_Full_View).
2706 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2712 Wrapper_Id : constant Entity_Id :=
2713 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2714 New_Formals : List_Id;
2715 Obj_Param : Node_Id;
2716 Obj_Param_Typ : Entity_Id;
2719 -- Minimum decoration is needed to catch the entity in
2720 -- Sem_Ch6.Override_Dispatching_Operation.
2722 if Ekind (Subp_Id) = E_Function then
2723 Set_Ekind (Wrapper_Id, E_Function);
2725 Set_Ekind (Wrapper_Id, E_Procedure);
2728 Set_Is_Primitive_Wrapper (Wrapper_Id);
2729 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2730 Set_Is_Private_Primitive (Wrapper_Id,
2731 Is_Private_Primitive_Subprogram (Subp_Id));
2733 -- Process the formals
2735 New_Formals := Replicate_Formals (Loc, Formals);
2737 -- A function with a controlling result and no first controlling
2738 -- formal needs no additional parameter.
2740 if Has_Controlling_Result (Subp_Id)
2742 (No (First_Formal (Subp_Id))
2743 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2747 -- Routine Subp_Id has been found to override an interface primitive.
2748 -- If the interface operation has an access parameter, create a copy
2749 -- of it, with the same null exclusion indicator if present.
2751 elsif Present (First_Param) then
2752 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2754 Make_Access_Definition (Loc,
2756 New_Occurrence_Of (Obj_Typ, Loc),
2757 Null_Exclusion_Present =>
2758 Null_Exclusion_Present (Parameter_Type (First_Param)),
2760 Constant_Present (Parameter_Type (First_Param)));
2762 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2766 Make_Parameter_Specification (Loc,
2767 Defining_Identifier =>
2768 Make_Defining_Identifier (Loc,
2770 In_Present => In_Present (First_Param),
2771 Out_Present => Out_Present (First_Param),
2772 Parameter_Type => Obj_Param_Typ);
2774 Prepend_To (New_Formals, Obj_Param);
2776 -- If we are dealing with a primitive declared between two views,
2777 -- implemented by a synchronized operation, we need to create
2778 -- a default parameter. The mode of the parameter must match that
2779 -- of the primitive operation.
2782 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2785 Make_Parameter_Specification (Loc,
2786 Defining_Identifier =>
2787 Make_Defining_Identifier (Loc, Name_uO),
2789 In_Present (Parent (First_Entity (Subp_Id))),
2790 Out_Present => Ekind (Subp_Id) /= E_Function,
2791 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2793 Prepend_To (New_Formals, Obj_Param);
2796 -- Build the final spec. If it is a function with a controlling
2797 -- result, it is a primitive operation of the corresponding
2798 -- record type, so mark the spec accordingly.
2800 if Ekind (Subp_Id) = E_Function then
2805 if Has_Controlling_Result (Subp_Id) then
2808 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2810 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2814 Make_Function_Specification (Loc,
2815 Defining_Unit_Name => Wrapper_Id,
2816 Parameter_Specifications => New_Formals,
2817 Result_Definition => Res_Def);
2821 Make_Procedure_Specification (Loc,
2822 Defining_Unit_Name => Wrapper_Id,
2823 Parameter_Specifications => New_Formals);
2826 end Build_Wrapper_Spec;
2828 -------------------------
2829 -- Build_Wrapper_Specs --
2830 -------------------------
2832 procedure Build_Wrapper_Specs
2838 Rec_Typ : Entity_Id;
2839 procedure Scan_Declarations (L : List_Id);
2840 -- Common processing for visible and private declarations
2841 -- of a protected type.
2843 procedure Scan_Declarations (L : List_Id) is
2845 Wrap_Decl : Node_Id;
2846 Wrap_Spec : Node_Id;
2854 while Present (Decl) loop
2857 if Nkind (Decl) = N_Entry_Declaration
2858 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2862 (Subp_Id => Defining_Identifier (Decl),
2864 Formals => Parameter_Specifications (Decl));
2866 elsif Nkind (Decl) = N_Subprogram_Declaration then
2869 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2872 Parameter_Specifications (Specification (Decl)));
2875 if Present (Wrap_Spec) then
2877 Make_Subprogram_Declaration (Loc,
2878 Specification => Wrap_Spec);
2880 Insert_After (N, Wrap_Decl);
2883 Analyze (Wrap_Decl);
2888 end Scan_Declarations;
2890 -- start of processing for Build_Wrapper_Specs
2893 if Is_Protected_Type (Typ) then
2894 Def := Protected_Definition (Parent (Typ));
2895 else pragma Assert (Is_Task_Type (Typ));
2896 Def := Task_Definition (Parent (Typ));
2899 Rec_Typ := Corresponding_Record_Type (Typ);
2901 -- Generate wrapper specs for a concurrent type which implements an
2902 -- interface. Operations in both the visible and private parts may
2903 -- implement progenitor operations.
2905 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2906 Scan_Declarations (Visible_Declarations (Def));
2907 Scan_Declarations (Private_Declarations (Def));
2909 end Build_Wrapper_Specs;
2911 ---------------------------
2912 -- Build_Find_Body_Index --
2913 ---------------------------
2915 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2916 Loc : constant Source_Ptr := Sloc (Typ);
2919 Has_F : Boolean := False;
2921 If_St : Node_Id := Empty;
2924 Decls : List_Id := New_List;
2927 Siz : Node_Id := Empty;
2929 procedure Add_If_Clause (Expr : Node_Id);
2930 -- Add test for range of current entry
2932 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2933 -- If a bound of an entry is given by a discriminant, retrieve the
2934 -- actual value of the discriminant from the enclosing object.
2940 procedure Add_If_Clause (Expr : Node_Id) is
2942 Stats : constant List_Id :=
2944 Make_Simple_Return_Statement (Loc,
2945 Expression => Make_Integer_Literal (Loc, Index + 1)));
2948 -- Index for current entry body
2952 -- Compute total length of entry queues so far
2960 Right_Opnd => Expr);
2965 Left_Opnd => Make_Identifier (Loc, Name_uE),
2968 -- Map entry queue indexes in the range of the current family
2969 -- into the current index, that designates the entry body.
2973 Make_Implicit_If_Statement (Typ,
2975 Then_Statements => Stats,
2976 Elsif_Parts => New_List);
2980 Append_To (Elsif_Parts (If_St),
2981 Make_Elsif_Part (Loc,
2983 Then_Statements => Stats));
2987 ------------------------------
2988 -- Convert_Discriminant_Ref --
2989 ------------------------------
2991 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2995 if Is_Entity_Name (Bound)
2996 and then Ekind (Entity (Bound)) = E_Discriminant
2999 Make_Selected_Component (Loc,
3001 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
3002 Make_Explicit_Dereference (Loc,
3003 Make_Identifier (Loc, Name_uObject))),
3004 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
3005 Set_Etype (B, Etype (Entity (Bound)));
3007 B := New_Copy_Tree (Bound);
3011 end Convert_Discriminant_Ref;
3013 -- Start of processing for Build_Find_Body_Index
3016 Spec := Build_Find_Body_Index_Spec (Typ);
3018 Ent := First_Entity (Typ);
3019 while Present (Ent) loop
3020 if Ekind (Ent) = E_Entry_Family then
3030 -- If the protected type has no entry families, there is a one-one
3031 -- correspondence between entry queue and entry body.
3034 Make_Simple_Return_Statement (Loc,
3035 Expression => Make_Identifier (Loc, Name_uE));
3038 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
3041 -- if E <= l1 then return 1;
3042 -- elsif E <= l1 + l2 then return 2;
3047 Ent := First_Entity (Typ);
3049 Add_Object_Pointer (Loc, Typ, Decls);
3051 while Present (Ent) loop
3052 if Ekind (Ent) = E_Entry then
3053 Add_If_Clause (Make_Integer_Literal (Loc, 1));
3055 elsif Ekind (Ent) = E_Entry_Family then
3056 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
3057 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
3058 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
3059 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
3068 Make_Simple_Return_Statement (Loc,
3069 Expression => Make_Integer_Literal (Loc, 1));
3071 elsif Nkind (Ret) = N_If_Statement then
3073 -- Ranges are in increasing order, so last one doesn't need guard
3076 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
3079 Set_Else_Statements (Ret, Then_Statements (Nod));
3085 Make_Subprogram_Body (Loc,
3086 Specification => Spec,
3087 Declarations => Decls,
3088 Handled_Statement_Sequence =>
3089 Make_Handled_Sequence_Of_Statements (Loc,
3090 Statements => New_List (Ret)));
3091 end Build_Find_Body_Index;
3093 --------------------------------
3094 -- Build_Find_Body_Index_Spec --
3095 --------------------------------
3097 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
3098 Loc : constant Source_Ptr := Sloc (Typ);
3099 Id : constant Entity_Id :=
3100 Make_Defining_Identifier (Loc,
3101 Chars => New_External_Name (Chars (Typ), 'F'));
3102 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
3103 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
3107 Make_Function_Specification (Loc,
3108 Defining_Unit_Name => Id,
3109 Parameter_Specifications => New_List (
3110 Make_Parameter_Specification (Loc,
3111 Defining_Identifier => Parm1,
3113 New_Occurrence_Of (RTE (RE_Address), Loc)),
3115 Make_Parameter_Specification (Loc,
3116 Defining_Identifier => Parm2,
3118 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
3120 Result_Definition => New_Occurrence_Of (
3121 RTE (RE_Protected_Entry_Index), Loc));
3122 end Build_Find_Body_Index_Spec;
3124 -----------------------------------------------
3125 -- Build_Lock_Free_Protected_Subprogram_Body --
3126 -----------------------------------------------
3128 function Build_Lock_Free_Protected_Subprogram_Body
3131 Unprot_Spec : Node_Id) return Node_Id
3133 Actuals : constant List_Id := New_List;
3134 Loc : constant Source_Ptr := Sloc (N);
3135 Spec : constant Node_Id := Specification (N);
3136 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
3138 Prot_Spec : Node_Id;
3142 -- Create the protected version of the body
3145 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
3147 -- Build the actual parameters which appear in the call to the
3148 -- unprotected version of the body.
3150 Formal := First (Parameter_Specifications (Prot_Spec));
3151 while Present (Formal) loop
3153 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
3158 -- Function case, generate:
3159 -- return <Unprot_Func_Call>;
3161 if Nkind (Spec) = N_Function_Specification then
3163 Make_Simple_Return_Statement (Loc,
3165 Make_Function_Call (Loc,
3167 Make_Identifier (Loc, Chars (Unprot_Id)),
3168 Parameter_Associations => Actuals));
3170 -- Procedure case, call the unprotected version
3174 Make_Procedure_Call_Statement (Loc,
3176 Make_Identifier (Loc, Chars (Unprot_Id)),
3177 Parameter_Associations => Actuals);
3181 Make_Subprogram_Body (Loc,
3182 Declarations => Empty_List,
3183 Specification => Prot_Spec,
3184 Handled_Statement_Sequence =>
3185 Make_Handled_Sequence_Of_Statements (Loc,
3186 Statements => New_List (Stmt)));
3187 end Build_Lock_Free_Protected_Subprogram_Body;
3189 -------------------------------------------------
3190 -- Build_Lock_Free_Unprotected_Subprogram_Body --
3191 -------------------------------------------------
3193 -- Procedures which meet the lock-free implementation requirements and
3194 -- reference a unique scalar component Comp are expanded in the following
3197 -- procedure P (...) is
3198 -- Expected_Comp : constant Comp_Type :=
3200 -- (System.Atomic_Primitives.Lock_Free_Read_N
3201 -- (_Object.Comp'Address));
3205 -- <original declarations before the object renaming declaration
3208 -- Desired_Comp : Comp_Type := Expected_Comp;
3209 -- Comp : Comp_Type renames Desired_Comp;
3211 -- <original delarations after the object renaming declaration
3215 -- <original statements>
3216 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3217 -- (_Object.Comp'Address,
3218 -- Interfaces.Unsigned_N (Expected_Comp),
3219 -- Interfaces.Unsigned_N (Desired_Comp));
3224 -- Each return and raise statement of P is transformed into an atomic
3227 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3228 -- (_Object.Comp'Address,
3229 -- Interfaces.Unsigned_N (Expected_Comp),
3230 -- Interfaces.Unsigned_N (Desired_Comp));
3232 -- <original statement>
3237 -- Functions which meet the lock-free implementation requirements and
3238 -- reference a unique scalar component Comp are expanded in the following
3241 -- function F (...) return ... is
3242 -- <original declarations before the object renaming declaration
3245 -- Expected_Comp : constant Comp_Type :=
3247 -- (System.Atomic_Primitives.Lock_Free_Read_N
3248 -- (_Object.Comp'Address));
3249 -- Comp : Comp_Type renames Expected_Comp;
3251 -- <original delarations after the object renaming declaration of
3255 -- <original statements>
3258 function Build_Lock_Free_Unprotected_Subprogram_Body
3260 Prot_Typ : Node_Id) return Node_Id
3262 function Referenced_Component (N : Node_Id) return Entity_Id;
3263 -- Subprograms which meet the lock-free implementation criteria are
3264 -- allowed to reference only one unique component. Return the prival
3265 -- of the said component.
3267 --------------------------
3268 -- Referenced_Component --
3269 --------------------------
3271 function Referenced_Component (N : Node_Id) return Entity_Id is
3274 Source_Comp : Entity_Id := Empty;
3277 -- Find the unique source component which N references in its
3280 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3282 Element : Lock_Free_Subprogram renames
3283 Lock_Free_Subprogram_Table.Table (Index);
3285 if Element.Sub_Body = N then
3286 Source_Comp := Element.Comp_Id;
3292 if No (Source_Comp) then
3296 -- Find the prival which corresponds to the source component within
3297 -- the declarations of N.
3299 Decl := First (Declarations (N));
3300 while Present (Decl) loop
3302 -- Privals appear as object renamings
3304 if Nkind (Decl) = N_Object_Renaming_Declaration then
3305 Comp := Defining_Identifier (Decl);
3307 if Present (Prival_Link (Comp))
3308 and then Prival_Link (Comp) = Source_Comp
3318 end Referenced_Component;
3322 Comp : constant Entity_Id := Referenced_Component (N);
3323 Loc : constant Source_Ptr := Sloc (N);
3324 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
3325 Decls : List_Id := Declarations (N);
3327 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3330 -- Add renamings for the protection object, discriminals, privals, and
3331 -- the entry index constant for use by debugger.
3333 Debug_Private_Data_Declarations (Decls);
3335 -- Perform the lock-free expansion when the subprogram references a
3336 -- protected component.
3338 if Present (Comp) then
3339 Protected_Component_Ref : declare
3340 Comp_Decl : constant Node_Id := Parent (Comp);
3341 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
3342 Comp_Type : constant Entity_Id := Etype (Comp);
3344 Is_Procedure : constant Boolean :=
3345 Ekind (Corresponding_Spec (N)) = E_Procedure;
3346 -- Indicates if N is a protected procedure body
3348 Block_Decls : List_Id;
3349 Try_Write : Entity_Id;
3350 Desired_Comp : Entity_Id;
3353 Label_Id : Entity_Id := Empty;
3355 Expected_Comp : Entity_Id;
3358 New_Copy_List (Statements (Hand_Stmt_Seq));
3360 Unsigned : Entity_Id;
3362 function Process_Node (N : Node_Id) return Traverse_Result;
3363 -- Transform a single node if it is a return statement, a raise
3364 -- statement or a reference to Comp.
3366 procedure Process_Stmts (Stmts : List_Id);
3367 -- Given a statement sequence Stmts, wrap any return or raise
3368 -- statements in the following manner:
3370 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3371 -- (_Object.Comp'Address,
3372 -- Interfaces.Unsigned_N (Expected_Comp),
3373 -- Interfaces.Unsigned_N (Desired_Comp))
3384 function Process_Node (N : Node_Id) return Traverse_Result is
3386 procedure Wrap_Statement (Stmt : Node_Id);
3387 -- Wrap an arbitrary statement inside an if statement where the
3388 -- condition does an atomic check on the state of the object.
3390 --------------------
3391 -- Wrap_Statement --
3392 --------------------
3394 procedure Wrap_Statement (Stmt : Node_Id) is
3396 -- The first time through, create the declaration of a label
3397 -- which is used to skip the remainder of source statements
3398 -- if the state of the object has changed.
3400 if No (Label_Id) then
3402 Make_Identifier (Loc, New_External_Name ('L', 0));
3403 Set_Entity (Label_Id,
3404 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3408 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3409 -- (_Object.Comp'Address,
3410 -- Interfaces.Unsigned_N (Expected_Comp),
3411 -- Interfaces.Unsigned_N (Desired_Comp))
3419 Make_Implicit_If_Statement (N,
3421 Make_Function_Call (Loc,
3423 New_Occurrence_Of (Try_Write, Loc),
3424 Parameter_Associations => New_List (
3425 Make_Attribute_Reference (Loc,
3426 Prefix => Relocate_Node (Comp_Sel_Nam),
3427 Attribute_Name => Name_Address),
3429 Unchecked_Convert_To (Unsigned,
3430 New_Occurrence_Of (Expected_Comp, Loc)),
3432 Unchecked_Convert_To (Unsigned,
3433 New_Occurrence_Of (Desired_Comp, Loc)))),
3435 Then_Statements => New_List (Relocate_Node (Stmt)),
3437 Else_Statements => New_List (
3438 Make_Goto_Statement (Loc,
3440 New_Occurrence_Of (Entity (Label_Id), Loc)))));
3443 -- Start of processing for Process_Node
3446 -- Wrap each return and raise statement that appear inside a
3447 -- procedure. Skip the last return statement which is added by
3448 -- default since it is transformed into an exit statement.
3451 and then ((Nkind (N) = N_Simple_Return_Statement
3452 and then N /= Last (Stmts))
3453 or else Nkind (N) = N_Extended_Return_Statement
3454 or else (Nkind_In (N, N_Raise_Constraint_Error,
3455 N_Raise_Program_Error,
3457 N_Raise_Storage_Error)
3458 and then Comes_From_Source (N)))
3466 Set_Analyzed (N, False);
3471 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3477 procedure Process_Stmts (Stmts : List_Id) is
3480 Stmt := First (Stmts);
3481 while Present (Stmt) loop
3482 Process_Nodes (Stmt);
3487 -- Start of processing for Protected_Component_Ref
3490 -- Get the type size
3492 if Known_Static_Esize (Comp_Type) then
3493 Typ_Size := UI_To_Int (Esize (Comp_Type));
3495 -- If the Esize (Object_Size) is unknown at compile time, look at
3496 -- the RM_Size (Value_Size) since it may have been set by an
3497 -- explicit representation clause.
3499 elsif Known_Static_RM_Size (Comp_Type) then
3500 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3502 -- Should not happen since this has already been checked in
3503 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3506 raise Program_Error;
3509 -- Retrieve all relevant atomic routines and types
3513 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3514 Read := RTE (RE_Lock_Free_Read_8);
3515 Unsigned := RTE (RE_Uint8);
3518 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3519 Read := RTE (RE_Lock_Free_Read_16);
3520 Unsigned := RTE (RE_Uint16);
3523 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3524 Read := RTE (RE_Lock_Free_Read_32);
3525 Unsigned := RTE (RE_Uint32);
3528 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3529 Read := RTE (RE_Lock_Free_Read_64);
3530 Unsigned := RTE (RE_Uint64);
3533 raise Program_Error;
3537 -- Expected_Comp : constant Comp_Type :=
3539 -- (System.Atomic_Primitives.Lock_Free_Read_N
3540 -- (_Object.Comp'Address));
3543 Make_Defining_Identifier (Loc,
3544 New_External_Name (Chars (Comp), Suffix => "_saved"));
3547 Make_Object_Declaration (Loc,
3548 Defining_Identifier => Expected_Comp,
3549 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3550 Constant_Present => True,
3552 Unchecked_Convert_To (Comp_Type,
3553 Make_Function_Call (Loc,
3554 Name => New_Occurrence_Of (Read, Loc),
3555 Parameter_Associations => New_List (
3556 Make_Attribute_Reference (Loc,
3557 Prefix => Relocate_Node (Comp_Sel_Nam),
3558 Attribute_Name => Name_Address)))));
3560 -- Protected procedures
3562 if Is_Procedure then
3563 -- Move the original declarations inside the generated block
3565 Block_Decls := Decls;
3567 -- Reset the declarations list of the protected procedure to
3568 -- contain only Decl.
3570 Decls := New_List (Decl);
3573 -- Desired_Comp : Comp_Type := Expected_Comp;
3576 Make_Defining_Identifier (Loc,
3577 New_External_Name (Chars (Comp), Suffix => "_current"));
3579 -- Insert the declarations of Expected_Comp and Desired_Comp in
3580 -- the block declarations right before the renaming of the
3581 -- protected component.
3583 Insert_Before (Comp_Decl,
3584 Make_Object_Declaration (Loc,
3585 Defining_Identifier => Desired_Comp,
3586 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3588 New_Occurrence_Of (Expected_Comp, Loc)));
3590 -- Protected function
3593 Desired_Comp := Expected_Comp;
3595 -- Insert the declaration of Expected_Comp in the function
3596 -- declarations right before the renaming of the protected
3599 Insert_Before (Comp_Decl, Decl);
3602 -- Rewrite the protected component renaming declaration to be a
3603 -- renaming of Desired_Comp.
3606 -- Comp : Comp_Type renames Desired_Comp;
3609 Make_Object_Renaming_Declaration (Loc,
3610 Defining_Identifier =>
3611 Defining_Identifier (Comp_Decl),
3613 New_Occurrence_Of (Comp_Type, Loc),
3615 New_Occurrence_Of (Desired_Comp, Loc)));
3617 -- Wrap any return or raise statements in Stmts in same the manner
3618 -- described in Process_Stmts.
3620 Process_Stmts (Stmts);
3623 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3624 -- (_Object.Comp'Address,
3625 -- Interfaces.Unsigned_N (Expected_Comp),
3626 -- Interfaces.Unsigned_N (Desired_Comp))
3628 if Is_Procedure then
3630 Make_Exit_Statement (Loc,
3632 Make_Function_Call (Loc,
3634 New_Occurrence_Of (Try_Write, Loc),
3635 Parameter_Associations => New_List (
3636 Make_Attribute_Reference (Loc,
3637 Prefix => Relocate_Node (Comp_Sel_Nam),
3638 Attribute_Name => Name_Address),
3640 Unchecked_Convert_To (Unsigned,
3641 New_Occurrence_Of (Expected_Comp, Loc)),
3643 Unchecked_Convert_To (Unsigned,
3644 New_Occurrence_Of (Desired_Comp, Loc)))));
3646 -- Small optimization: transform the default return statement
3647 -- of a procedure into the atomic exit statement.
3649 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3650 Rewrite (Last (Stmts), Stmt);
3652 Append_To (Stmts, Stmt);
3656 -- Create the declaration of the label used to skip the rest of
3657 -- the source statements when the object state changes.
3659 if Present (Label_Id) then
3660 Label := Make_Label (Loc, Label_Id);
3662 Make_Implicit_Label_Declaration (Loc,
3663 Defining_Identifier => Entity (Label_Id),
3664 Label_Construct => Label));
3665 Append_To (Stmts, Label);
3677 if Is_Procedure then
3680 Make_Loop_Statement (Loc,
3681 Statements => New_List (
3682 Make_Block_Statement (Loc,
3683 Declarations => Block_Decls,
3684 Handled_Statement_Sequence =>
3685 Make_Handled_Sequence_Of_Statements (Loc,
3686 Statements => Stmts))),
3687 End_Label => Empty));
3691 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3692 end Protected_Component_Ref;
3695 -- Make an unprotected version of the subprogram for use within the same
3696 -- object, with new name and extra parameter representing the object.
3699 Make_Subprogram_Body (Loc,
3701 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3702 Declarations => Decls,
3703 Handled_Statement_Sequence => Hand_Stmt_Seq);
3704 end Build_Lock_Free_Unprotected_Subprogram_Body;
3706 -------------------------
3707 -- Build_Master_Entity --
3708 -------------------------
3710 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3711 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3713 Context_Id : Entity_Id;
3719 if Is_Itype (Obj_Or_Typ) then
3720 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3722 Par := Parent (Obj_Or_Typ);
3725 -- When creating a master for a record component which is either a task
3726 -- or access-to-task, the enclosing record is the master scope and the
3727 -- proper insertion point is the component list.
3729 if Is_Record_Type (Current_Scope) then
3731 Context_Id := Current_Scope;
3732 Decls := List_Containing (Context);
3734 -- Default case for object declarations and access types. Note that the
3735 -- context is updated to the nearest enclosing body, block, package, or
3736 -- return statement.
3739 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3742 -- Do not create a master if one already exists or there is no task
3745 if Has_Master_Entity (Context_Id)
3746 or else Restriction_Active (No_Task_Hierarchy)
3751 -- Create a master, generate:
3752 -- _Master : constant Master_Id := Current_Master.all;
3755 Make_Object_Declaration (Loc,
3756 Defining_Identifier =>
3757 Make_Defining_Identifier (Loc, Name_uMaster),
3758 Constant_Present => True,
3759 Object_Definition => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3761 Make_Explicit_Dereference (Loc,
3762 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
3764 -- The master is inserted at the start of the declarative list of the
3767 Prepend_To (Decls, Decl);
3769 -- In certain cases where transient scopes are involved, the immediate
3770 -- scope is not always the proper master scope. Ensure that the master
3771 -- declaration and entity appear in the same context.
3773 if Context_Id /= Current_Scope then
3774 Push_Scope (Context_Id);
3781 -- Mark the enclosing scope and its associated construct as being task
3784 Set_Has_Master_Entity (Context_Id);
3786 while Present (Context)
3787 and then Nkind (Context) /= N_Compilation_Unit
3789 if Nkind_In (Context, N_Block_Statement,
3793 Set_Is_Task_Master (Context);
3796 elsif Nkind (Parent (Context)) = N_Subunit then
3797 Context := Corresponding_Stub (Parent (Context));
3800 Context := Parent (Context);
3802 end Build_Master_Entity;
3804 ---------------------------
3805 -- Build_Master_Renaming --
3806 ---------------------------
3808 procedure Build_Master_Renaming
3809 (Ptr_Typ : Entity_Id;
3810 Ins_Nod : Node_Id := Empty)
3812 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3814 Master_Decl : Node_Id;
3815 Master_Id : Entity_Id;
3818 -- Nothing to do if there is no task hierarchy
3820 if Restriction_Active (No_Task_Hierarchy) then
3824 -- Determine the proper context to insert the master renaming
3826 if Present (Ins_Nod) then
3828 elsif Is_Itype (Ptr_Typ) then
3829 Context := Associated_Node_For_Itype (Ptr_Typ);
3831 Context := Parent (Ptr_Typ);
3835 -- <Ptr_Typ>M : Master_Id renames _Master;
3838 Make_Defining_Identifier (Loc,
3839 New_External_Name (Chars (Ptr_Typ), 'M'));
3842 Make_Object_Renaming_Declaration (Loc,
3843 Defining_Identifier => Master_Id,
3844 Subtype_Mark => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3845 Name => Make_Identifier (Loc, Name_uMaster));
3847 Insert_Action (Context, Master_Decl);
3849 -- The renamed master now services the access type
3851 Set_Master_Id (Ptr_Typ, Master_Id);
3852 end Build_Master_Renaming;
3854 -----------------------------------------
3855 -- Build_Private_Protected_Declaration --
3856 -----------------------------------------
3858 function Build_Private_Protected_Declaration
3859 (N : Node_Id) return Entity_Id
3861 Loc : constant Source_Ptr := Sloc (N);
3862 Body_Id : constant Entity_Id := Defining_Entity (N);
3867 Spec_Id : Entity_Id;
3870 Formal := First_Formal (Body_Id);
3872 -- The protected operation always has at least one formal, namely the
3873 -- object itself, but it is only placed in the parameter list if
3874 -- expansion is enabled.
3876 if Present (Formal) or else Expander_Active then
3877 Plist := Copy_Parameter_List (Body_Id);
3882 if Nkind (Specification (N)) = N_Procedure_Specification then
3884 Make_Procedure_Specification (Loc,
3885 Defining_Unit_Name =>
3886 Make_Defining_Identifier (Sloc (Body_Id),
3887 Chars => Chars (Body_Id)),
3888 Parameter_Specifications =>
3892 Make_Function_Specification (Loc,
3893 Defining_Unit_Name =>
3894 Make_Defining_Identifier (Sloc (Body_Id),
3895 Chars => Chars (Body_Id)),
3896 Parameter_Specifications => Plist,
3897 Result_Definition =>
3898 New_Occurrence_Of (Etype (Body_Id), Loc));
3901 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3902 Insert_Before (N, Decl);
3903 Spec_Id := Defining_Unit_Name (New_Spec);
3905 -- Indicate that the entity comes from source, to ensure that cross-
3906 -- reference information is properly generated. The body itself is
3907 -- rewritten during expansion, and the body entity will not appear in
3908 -- calls to the operation.
3910 Set_Comes_From_Source (Spec_Id, True);
3912 Set_Has_Completion (Spec_Id);
3913 Set_Convention (Spec_Id, Convention_Protected);
3915 end Build_Private_Protected_Declaration;
3917 ---------------------------
3918 -- Build_Protected_Entry --
3919 ---------------------------
3921 function Build_Protected_Entry
3924 Pid : Node_Id) return Node_Id
3926 Bod_Decls : constant List_Id := New_List;
3927 Decls : constant List_Id := Declarations (N);
3928 End_Lab : constant Node_Id :=
3929 End_Label (Handled_Statement_Sequence (N));
3930 End_Loc : constant Source_Ptr :=
3931 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3932 -- Used for the generated call to Complete_Entry_Body
3934 Loc : constant Source_Ptr := Sloc (N);
3938 Bod_Stmts : List_Id;
3942 EH_Loc : Source_Ptr;
3943 -- Used for the exception handler, inserted at end of the body
3946 -- Set the source location on the exception handler only when debugging
3947 -- the expanded code (see Make_Implicit_Exception_Handler).
3949 if Debug_Generated_Code then
3952 -- Otherwise the inserted code should not be visible to the debugger
3955 EH_Loc := No_Location;
3959 Make_Defining_Identifier (Loc,
3960 Chars => Chars (Protected_Body_Subprogram (Ent)));
3961 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3963 -- Add the following declarations:
3965 -- type poVP is access poV;
3966 -- _object : poVP := poVP (_O);
3968 -- where _O is the formal parameter associated with the concurrent
3969 -- object. These declarations are needed for Complete_Entry_Body.
3971 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3973 -- Add renamings for all formals, the Protection object, discriminals,
3974 -- privals and the entry index constant for use by debugger.
3976 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3977 Debug_Private_Data_Declarations (Decls);
3979 -- Put the declarations and the statements from the entry
3983 Make_Block_Statement (Loc,
3984 Declarations => Decls,
3985 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3987 case Corresponding_Runtime_Package (Pid) is
3988 when System_Tasking_Protected_Objects_Entries =>
3989 Append_To (Bod_Stmts,
3990 Make_Procedure_Call_Statement (End_Loc,
3992 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
3993 Parameter_Associations => New_List (
3994 Make_Attribute_Reference (End_Loc,
3996 Make_Selected_Component (End_Loc,
3998 Make_Identifier (End_Loc, Name_uObject),
4000 Make_Identifier (End_Loc, Name_uObject)),
4001 Attribute_Name => Name_Unchecked_Access))));
4003 when System_Tasking_Protected_Objects_Single_Entry =>
4005 -- Historically, a call to Complete_Single_Entry_Body was
4006 -- inserted, but it was a null procedure.
4011 raise Program_Error;
4014 -- When exceptions can not be propagated, we never need to call
4015 -- Exception_Complete_Entry_Body.
4017 if No_Exception_Handlers_Set then
4019 Make_Subprogram_Body (Loc,
4020 Specification => Bod_Spec,
4021 Declarations => Bod_Decls,
4022 Handled_Statement_Sequence =>
4023 Make_Handled_Sequence_Of_Statements (Loc,
4024 Statements => Bod_Stmts,
4025 End_Label => End_Lab));
4028 Ohandle := Make_Others_Choice (Loc);
4029 Set_All_Others (Ohandle);
4031 case Corresponding_Runtime_Package (Pid) is
4032 when System_Tasking_Protected_Objects_Entries =>
4035 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
4037 when System_Tasking_Protected_Objects_Single_Entry =>
4040 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
4043 raise Program_Error;
4046 -- Establish link between subprogram body entity and source entry
4048 Set_Corresponding_Protected_Entry (Bod_Id, Ent);
4050 -- Create body of entry procedure. The renaming declarations are
4051 -- placed ahead of the block that contains the actual entry body.
4054 Make_Subprogram_Body (Loc,
4055 Specification => Bod_Spec,
4056 Declarations => Bod_Decls,
4057 Handled_Statement_Sequence =>
4058 Make_Handled_Sequence_Of_Statements (Loc,
4059 Statements => Bod_Stmts,
4060 End_Label => End_Lab,
4061 Exception_Handlers => New_List (
4062 Make_Implicit_Exception_Handler (EH_Loc,
4063 Exception_Choices => New_List (Ohandle),
4065 Statements => New_List (
4066 Make_Procedure_Call_Statement (EH_Loc,
4068 Parameter_Associations => New_List (
4069 Make_Attribute_Reference (EH_Loc,
4071 Make_Selected_Component (EH_Loc,
4073 Make_Identifier (EH_Loc, Name_uObject),
4075 Make_Identifier (EH_Loc, Name_uObject)),
4076 Attribute_Name => Name_Unchecked_Access),
4078 Make_Function_Call (EH_Loc,
4081 (RTE (RE_Get_GNAT_Exception), Loc)))))))));
4083 end Build_Protected_Entry;
4085 -----------------------------------------
4086 -- Build_Protected_Entry_Specification --
4087 -----------------------------------------
4089 function Build_Protected_Entry_Specification
4092 Ent_Id : Entity_Id) return Node_Id
4094 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
4097 Set_Debug_Info_Needed (Def_Id);
4099 if Present (Ent_Id) then
4100 Append_Elmt (P, Accept_Address (Ent_Id));
4104 Make_Procedure_Specification (Loc,
4105 Defining_Unit_Name => Def_Id,
4106 Parameter_Specifications => New_List (
4107 Make_Parameter_Specification (Loc,
4108 Defining_Identifier =>
4109 Make_Defining_Identifier (Loc, Name_uO),
4111 New_Occurrence_Of (RTE (RE_Address), Loc)),
4113 Make_Parameter_Specification (Loc,
4114 Defining_Identifier => P,
4116 New_Occurrence_Of (RTE (RE_Address), Loc)),
4118 Make_Parameter_Specification (Loc,
4119 Defining_Identifier =>
4120 Make_Defining_Identifier (Loc, Name_uE),
4122 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
4123 end Build_Protected_Entry_Specification;
4125 --------------------------
4126 -- Build_Protected_Spec --
4127 --------------------------
4129 function Build_Protected_Spec
4131 Obj_Type : Entity_Id;
4133 Unprotected : Boolean := False) return List_Id
4135 Loc : constant Source_Ptr := Sloc (N);
4138 New_Plist : List_Id;
4139 New_Param : Node_Id;
4142 New_Plist := New_List;
4144 Formal := First_Formal (Ident);
4145 while Present (Formal) loop
4147 Make_Parameter_Specification (Loc,
4148 Defining_Identifier =>
4149 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
4150 Aliased_Present => Aliased_Present (Parent (Formal)),
4151 In_Present => In_Present (Parent (Formal)),
4152 Out_Present => Out_Present (Parent (Formal)),
4153 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc));
4156 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
4159 Append (New_Param, New_Plist);
4160 Next_Formal (Formal);
4163 -- If the subprogram is a procedure and the context is not an access
4164 -- to protected subprogram, the parameter is in-out. Otherwise it is
4168 Make_Parameter_Specification (Loc,
4169 Defining_Identifier =>
4170 Make_Defining_Identifier (Loc, Name_uObject),
4173 (Etype (Ident) = Standard_Void_Type
4174 and then not Is_RTE (Obj_Type, RE_Address)),
4176 New_Occurrence_Of (Obj_Type, Loc));
4177 Set_Debug_Info_Needed (Defining_Identifier (Decl));
4178 Prepend_To (New_Plist, Decl);
4181 end Build_Protected_Spec;
4183 ---------------------------------------
4184 -- Build_Protected_Sub_Specification --
4185 ---------------------------------------
4187 function Build_Protected_Sub_Specification
4189 Prot_Typ : Entity_Id;
4190 Mode : Subprogram_Protection_Mode) return Node_Id
4192 Loc : constant Source_Ptr := Sloc (N);
4196 New_Plist : List_Id;
4199 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
4200 (Dispatching_Mode => ' ',
4201 Protected_Mode => 'P',
4202 Unprotected_Mode => 'N');
4205 if Ekind (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
4207 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
4212 Def_Id := Defining_Unit_Name (Specification (Decl));
4215 Build_Protected_Spec
4216 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
4217 Mode = Unprotected_Mode);
4219 Make_Defining_Identifier (Loc,
4220 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
4222 -- Reference the original nondispatching subprogram since the analysis
4223 -- of the object.operation notation may need its original name (see
4224 -- Sem_Ch4.Names_Match).
4226 if Mode = Dispatching_Mode then
4227 Set_Ekind (New_Id, Ekind (Def_Id));
4228 Set_Original_Protected_Subprogram (New_Id, Def_Id);
4231 -- The unprotected operation carries the user code, and debugging
4232 -- information must be generated for it, even though this spec does
4233 -- not come from source. It is also convenient to allow gdb to step
4234 -- into the protected operation, even though it only contains lock/
4237 Set_Debug_Info_Needed (New_Id);
4239 -- If a pragma Eliminate applies to the source entity, the internal
4240 -- subprograms will be eliminated as well.
4242 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
4244 if Nkind (Specification (Decl)) = N_Procedure_Specification then
4246 Make_Procedure_Specification (Loc,
4247 Defining_Unit_Name => New_Id,
4248 Parameter_Specifications => New_Plist);
4250 -- Create a new specification for the anonymous subprogram type
4254 Make_Function_Specification (Loc,
4255 Defining_Unit_Name => New_Id,
4256 Parameter_Specifications => New_Plist,
4257 Result_Definition =>
4258 Copy_Result_Type (Result_Definition (Specification (Decl))));
4260 Set_Return_Present (Defining_Unit_Name (New_Spec));
4264 end Build_Protected_Sub_Specification;
4266 -------------------------------------
4267 -- Build_Protected_Subprogram_Body --
4268 -------------------------------------
4270 function Build_Protected_Subprogram_Body
4273 N_Op_Spec : Node_Id) return Node_Id
4275 Loc : constant Source_Ptr := Sloc (N);
4277 P_Op_Spec : Node_Id;
4280 Unprot_Call : Node_Id;
4282 Lock_Name : Node_Id;
4283 Lock_Stmt : Node_Id;
4285 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4286 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4288 Object_Parm : Node_Id;
4293 Op_Spec := Specification (N);
4294 Exc_Safe := Is_Exception_Safe (N);
4297 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4299 -- Build a list of the formal parameters of the protected version of
4300 -- the subprogram to use as the actual parameters of the unprotected
4303 Uactuals := New_List;
4304 Pformal := First (Parameter_Specifications (P_Op_Spec));
4305 while Present (Pformal) loop
4306 Append_To (Uactuals,
4307 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4311 -- Make a call to the unprotected version of the subprogram built above
4312 -- for use by the protected version built below.
4314 if Nkind (Op_Spec) = N_Function_Specification then
4316 R := Make_Temporary (Loc, 'R');
4319 Make_Object_Declaration (Loc,
4320 Defining_Identifier => R,
4321 Constant_Present => True,
4322 Object_Definition =>
4323 New_Copy (Result_Definition (N_Op_Spec)),
4325 Make_Function_Call (Loc,
4327 Make_Identifier (Loc,
4328 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4329 Parameter_Associations => Uactuals));
4332 Make_Simple_Return_Statement (Loc,
4333 Expression => New_Occurrence_Of (R, Loc));
4337 Make_Simple_Return_Statement (Loc,
4339 Make_Function_Call (Loc,
4341 Make_Identifier (Loc,
4342 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4343 Parameter_Associations => Uactuals));
4346 Lock_Kind := RE_Lock_Read_Only;
4350 Make_Procedure_Call_Statement (Loc,
4352 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4353 Parameter_Associations => Uactuals);
4355 Lock_Kind := RE_Lock;
4358 -- Wrap call in block that will be covered by an at_end handler
4360 if not Exc_Safe then
4362 Make_Block_Statement (Loc,
4363 Handled_Statement_Sequence =>
4364 Make_Handled_Sequence_Of_Statements (Loc,
4365 Statements => New_List (Unprot_Call)));
4368 -- Make the protected subprogram body. This locks the protected
4369 -- object and calls the unprotected version of the subprogram.
4371 case Corresponding_Runtime_Package (Pid) is
4372 when System_Tasking_Protected_Objects_Entries =>
4373 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
4375 when System_Tasking_Protected_Objects_Single_Entry =>
4376 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
4378 when System_Tasking_Protected_Objects =>
4379 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
4382 raise Program_Error;
4386 Make_Attribute_Reference (Loc,
4388 Make_Selected_Component (Loc,
4389 Prefix => Make_Identifier (Loc, Name_uObject),
4390 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4391 Attribute_Name => Name_Unchecked_Access);
4394 Make_Procedure_Call_Statement (Loc,
4396 Parameter_Associations => New_List (Object_Parm));
4398 if Abort_Allowed then
4400 Build_Runtime_Call (Loc, RE_Abort_Defer),
4404 Stmts := New_List (Lock_Stmt);
4407 if not Exc_Safe then
4408 Append (Unprot_Call, Stmts);
4410 if Nkind (Op_Spec) = N_Function_Specification then
4412 Stmts := Empty_List;
4414 Append (Unprot_Call, Stmts);
4417 -- Historical note: Previously, call to the cleanup was inserted
4418 -- here. This is now done by Build_Protected_Subprogram_Call_Cleanup,
4419 -- which is also shared by the 'not Exc_Safe' path.
4421 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
4423 if Nkind (Op_Spec) = N_Function_Specification then
4424 Append_To (Stmts, Return_Stmt);
4425 Append_To (Pre_Stmts,
4426 Make_Block_Statement (Loc,
4427 Declarations => New_List (Unprot_Call),
4428 Handled_Statement_Sequence =>
4429 Make_Handled_Sequence_Of_Statements (Loc,
4430 Statements => Stmts)));
4436 Make_Subprogram_Body (Loc,
4437 Declarations => Empty_List,
4438 Specification => P_Op_Spec,
4439 Handled_Statement_Sequence =>
4440 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4442 -- Mark this subprogram as a protected subprogram body so that the
4443 -- cleanup will be inserted. This is done only in the 'not Exc_Safe'
4444 -- path as otherwise the cleanup has already been inserted.
4446 if not Exc_Safe then
4447 Set_Is_Protected_Subprogram_Body (Sub_Body);
4451 end Build_Protected_Subprogram_Body;
4453 -------------------------------------
4454 -- Build_Protected_Subprogram_Call --
4455 -------------------------------------
4457 procedure Build_Protected_Subprogram_Call
4461 External : Boolean := True)
4463 Loc : constant Source_Ptr := Sloc (N);
4464 Sub : constant Entity_Id := Entity (Name);
4470 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4473 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4476 if Present (Parameter_Associations (N)) then
4477 Params := New_Copy_List_Tree (Parameter_Associations (N));
4482 -- If the type is an untagged derived type, convert to the root type,
4483 -- which is the one on which the operations are defined.
4485 if Nkind (Rec) = N_Unchecked_Type_Conversion
4486 and then not Is_Tagged_Type (Etype (Rec))
4487 and then Is_Derived_Type (Etype (Rec))
4489 Set_Etype (Rec, Root_Type (Etype (Rec)));
4490 Set_Subtype_Mark (Rec,
4491 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4494 Prepend (Rec, Params);
4496 if Ekind (Sub) = E_Procedure then
4498 Make_Procedure_Call_Statement (Loc,
4500 Parameter_Associations => Params));
4503 pragma Assert (Ekind (Sub) = E_Function);
4505 Make_Function_Call (Loc,
4507 Parameter_Associations => Params));
4509 -- Preserve type of call for subsequent processing (required for
4510 -- call to Wrap_Transient_Expression in the case of a shared passive
4513 Set_Etype (N, Etype (New_Sub));
4517 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4518 and then Is_Entity_Name (Expression (Rec))
4519 and then Is_Shared_Passive (Entity (Expression (Rec)))
4521 Add_Shared_Var_Lock_Procs (N);
4523 end Build_Protected_Subprogram_Call;
4525 ---------------------------------------------
4526 -- Build_Protected_Subprogram_Call_Cleanup --
4527 ---------------------------------------------
4529 procedure Build_Protected_Subprogram_Call_Cleanup
4538 -- If the associated protected object has entries, a protected
4539 -- procedure has to service entry queues. In this case generate:
4541 -- Service_Entries (_object._object'Access);
4543 if Nkind (Op_Spec) = N_Procedure_Specification
4544 and then Has_Entries (Conc_Typ)
4546 case Corresponding_Runtime_Package (Conc_Typ) is
4547 when System_Tasking_Protected_Objects_Entries =>
4548 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4550 when System_Tasking_Protected_Objects_Single_Entry =>
4551 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4554 raise Program_Error;
4558 Make_Procedure_Call_Statement (Loc,
4560 Parameter_Associations => New_List (
4561 Make_Attribute_Reference (Loc,
4563 Make_Selected_Component (Loc,
4564 Prefix => Make_Identifier (Loc, Name_uObject),
4565 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4566 Attribute_Name => Name_Unchecked_Access))));
4570 -- Unlock (_object._object'Access);
4572 case Corresponding_Runtime_Package (Conc_Typ) is
4573 when System_Tasking_Protected_Objects_Entries =>
4574 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4576 when System_Tasking_Protected_Objects_Single_Entry =>
4577 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4579 when System_Tasking_Protected_Objects =>
4580 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4583 raise Program_Error;
4587 Make_Procedure_Call_Statement (Loc,
4589 Parameter_Associations => New_List (
4590 Make_Attribute_Reference (Loc,
4592 Make_Selected_Component (Loc,
4593 Prefix => Make_Identifier (Loc, Name_uObject),
4594 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4595 Attribute_Name => Name_Unchecked_Access))));
4601 if Abort_Allowed then
4602 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
4604 end Build_Protected_Subprogram_Call_Cleanup;
4606 -------------------------
4607 -- Build_Selected_Name --
4608 -------------------------
4610 function Build_Selected_Name
4611 (Prefix : Entity_Id;
4612 Selector : Entity_Id;
4613 Append_Char : Character := ' ') return Name_Id
4615 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4616 Select_Len : Natural;
4619 Get_Name_String (Chars (Selector));
4620 Select_Len := Name_Len;
4621 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4622 Get_Name_String (Chars (Prefix));
4624 -- If scope is anonymous type, discard suffix to recover name of
4625 -- single protected object. Otherwise use protected type name.
4627 if Name_Buffer (Name_Len) = 'T' then
4628 Name_Len := Name_Len - 1;
4631 Add_Str_To_Name_Buffer ("__");
4632 for J in 1 .. Select_Len loop
4633 Add_Char_To_Name_Buffer (Select_Buffer (J));
4636 -- Now add the Append_Char if specified. The encoding to follow
4637 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4638 -- then the entity is associated to a protected type subprogram.
4639 -- Otherwise, it is a protected type entry. For each case, the
4640 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4642 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4644 if Append_Char /= ' ' then
4645 if Append_Char = 'P' or Append_Char = 'N' then
4646 Add_Char_To_Name_Buffer (Append_Char);
4649 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4650 return New_External_Name (Name_Find, ' ', -1);
4655 end Build_Selected_Name;
4657 -----------------------------
4658 -- Build_Simple_Entry_Call --
4659 -----------------------------
4661 -- A task entry call is converted to a call to Call_Simple
4664 -- P : parms := (parm, parm, parm);
4666 -- Call_Simple (acceptor-task, entry-index, P'Address);
4672 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4673 -- the parameters, and the constructed aggregate value contains either the
4674 -- parameters or, in the case of non-elementary types, references to these
4675 -- parameters. Then the address of this aggregate is passed to the runtime
4676 -- routine, along with the task id value and the task entry index value.
4677 -- Pnn is only required if parameters are present.
4679 -- The assignments after the call are present only in the case of in-out
4680 -- or out parameters for elementary types, and are used to assign back the
4681 -- resulting values of such parameters.
4683 -- Note: the reason that we insert a block here is that in the context
4684 -- of selects, conditional entry calls etc. the entry call statement
4685 -- appears on its own, not as an element of a list.
4687 -- A protected entry call is converted to a Protected_Entry_Call:
4690 -- P : E1_Params := (param, param, param);
4692 -- Bnn : Communications_Block;
4695 -- P : E1_Params := (param, param, param);
4696 -- Bnn : Communications_Block;
4699 -- Protected_Entry_Call (
4700 -- Object => po._object'Access,
4701 -- E => <entry index>;
4702 -- Uninterpreted_Data => P'Address;
4703 -- Mode => Simple_Call;
4710 procedure Build_Simple_Entry_Call
4719 -- If call has been inlined, nothing left to do
4721 if Nkind (N) = N_Block_Statement then
4725 -- Convert entry call to Call_Simple call
4728 Loc : constant Source_Ptr := Sloc (N);
4729 Parms : constant List_Id := Parameter_Associations (N);
4730 Stats : constant List_Id := New_List;
4733 Comm_Name : Entity_Id;
4737 Ent_Acc : Entity_Id;
4739 Iface_Tag : Entity_Id;
4740 Iface_Typ : Entity_Id;
4753 -- Simple entry and entry family cases merge here
4755 Ent := Entity (Ename);
4756 Ent_Acc := Entry_Parameters_Type (Ent);
4757 Conctyp := Etype (Concval);
4759 -- If prefix is an access type, dereference to obtain the task type
4761 if Is_Access_Type (Conctyp) then
4762 Conctyp := Designated_Type (Conctyp);
4765 -- Special case for protected subprogram calls
4767 if Is_Protected_Type (Conctyp)
4768 and then Is_Subprogram (Entity (Ename))
4770 if not Is_Eliminated (Entity (Ename)) then
4771 Build_Protected_Subprogram_Call
4772 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4779 -- First parameter is the Task_Id value from the task value or the
4780 -- Object from the protected object value, obtained by selecting
4781 -- the _Task_Id or _Object from the result of doing an unchecked
4782 -- conversion to convert the value to the corresponding record type.
4784 if Nkind (Concval) = N_Function_Call
4785 and then Is_Task_Type (Conctyp)
4786 and then Ada_Version >= Ada_2005
4789 ExpR : constant Node_Id := Relocate_Node (Concval);
4790 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4795 Make_Object_Declaration (Loc,
4796 Defining_Identifier => Obj,
4797 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4798 Expression => ExpR);
4799 Set_Etype (Obj, Conctyp);
4800 Decls := New_List (Decl);
4801 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4808 Parm1 := Concurrent_Ref (Concval);
4810 -- Second parameter is the entry index, computed by the routine
4811 -- provided for this purpose. The value of this expression is
4812 -- assigned to an intermediate variable to assure that any entry
4813 -- family index expressions are evaluated before the entry
4816 if not Is_Protected_Type (Conctyp)
4818 Corresponding_Runtime_Package (Conctyp) =
4819 System_Tasking_Protected_Objects_Entries
4821 X := Make_Defining_Identifier (Loc, Name_uX);
4824 Make_Object_Declaration (Loc,
4825 Defining_Identifier => X,
4826 Object_Definition =>
4827 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4828 Expression => Actual_Index_Expression (
4829 Loc, Entity (Ename), Index, Concval));
4831 Append_To (Decls, Xdecl);
4832 Parm2 := New_Occurrence_Of (X, Loc);
4839 -- The third parameter is the packaged parameters. If there are
4840 -- none, then it is just the null address, since nothing is passed.
4843 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4846 -- Case of parameters present, where third argument is the address
4847 -- of a packaged record containing the required parameter values.
4850 -- First build a list of parameter values, which are references to
4851 -- objects of the parameter types.
4855 Actual := First_Actual (N);
4856 Formal := First_Formal (Ent);
4857 while Present (Actual) loop
4859 -- If it is a by-copy type, copy it to a new variable. The
4860 -- packaged record has a field that points to this variable.
4862 if Is_By_Copy_Type (Etype (Actual)) then
4864 Make_Object_Declaration (Loc,
4865 Defining_Identifier => Make_Temporary (Loc, 'J'),
4866 Aliased_Present => True,
4867 Object_Definition =>
4868 New_Occurrence_Of (Etype (Formal), Loc));
4870 -- Mark the object as not needing initialization since the
4871 -- initialization is performed separately, avoiding errors
4872 -- on cases such as formals of null-excluding access types.
4874 Set_No_Initialization (N_Node);
4876 -- We must make a separate assignment statement for the
4877 -- case of limited types. We cannot assign it unless the
4878 -- Assignment_OK flag is set first. An out formal of an
4879 -- access type or whose type has a Default_Value must also
4880 -- be initialized from the actual (see RM 6.4.1 (13-13.1)),
4881 -- but no constraint, predicate, or null-exclusion check is
4882 -- applied before the call.
4884 if Ekind (Formal) /= E_Out_Parameter
4885 or else Is_Access_Type (Etype (Formal))
4887 (Is_Scalar_Type (Etype (Formal))
4889 Present (Default_Aspect_Value (Etype (Formal))))
4892 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4893 Set_Assignment_OK (N_Var);
4895 Make_Assignment_Statement (Loc,
4897 Expression => Relocate_Node (Actual)));
4899 -- Mark the object as internal, so we don't later reset
4900 -- No_Initialization flag in Default_Initialize_Object,
4901 -- which would lead to needless default initialization.
4902 -- We don't set this outside the if statement, because
4903 -- out scalar parameters without Default_Value do require
4904 -- default initialization if Initialize_Scalars applies.
4906 Set_Is_Internal (Defining_Identifier (N_Node));
4908 -- If actual is an out parameter of a null-excluding
4909 -- access type, there is access check on entry, so set
4910 -- Suppress_Assignment_Checks on the generated statement
4911 -- that assigns the actual to the parameter block
4913 Set_Suppress_Assignment_Checks (Last (Stats));
4916 Append (N_Node, Decls);
4919 Make_Attribute_Reference (Loc,
4920 Attribute_Name => Name_Unchecked_Access,
4923 (Defining_Identifier (N_Node), Loc)));
4926 -- Interface class-wide formal
4928 if Ada_Version >= Ada_2005
4929 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4930 and then Is_Interface (Etype (Formal))
4932 Iface_Typ := Etype (Etype (Formal));
4935 -- formal_iface_type! (actual.iface_tag)'reference
4938 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4939 pragma Assert (Present (Iface_Tag));
4942 Make_Reference (Loc,
4943 Unchecked_Convert_To (Iface_Typ,
4944 Make_Selected_Component (Loc,
4946 Relocate_Node (Actual),
4948 New_Occurrence_Of (Iface_Tag, Loc)))));
4954 Make_Reference (Loc, Relocate_Node (Actual)));
4958 Next_Actual (Actual);
4959 Next_Formal_With_Extras (Formal);
4962 -- Now build the declaration of parameters initialized with the
4963 -- aggregate containing this constructed parameter list.
4965 P := Make_Defining_Identifier (Loc, Name_uP);
4968 Make_Object_Declaration (Loc,
4969 Defining_Identifier => P,
4970 Object_Definition =>
4971 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4973 Make_Aggregate (Loc, Expressions => Plist));
4976 Make_Attribute_Reference (Loc,
4977 Prefix => New_Occurrence_Of (P, Loc),
4978 Attribute_Name => Name_Address);
4980 Append (Pdecl, Decls);
4983 -- Now we can create the call, case of protected type
4985 if Is_Protected_Type (Conctyp) then
4986 case Corresponding_Runtime_Package (Conctyp) is
4987 when System_Tasking_Protected_Objects_Entries =>
4989 -- Change the type of the index declaration
4991 Set_Object_Definition (Xdecl,
4992 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4994 -- Some additional declarations for protected entry calls
5000 -- Bnn : Communications_Block;
5002 Comm_Name := Make_Temporary (Loc, 'B');
5005 Make_Object_Declaration (Loc,
5006 Defining_Identifier => Comm_Name,
5007 Object_Definition =>
5009 (RTE (RE_Communication_Block), Loc)));
5011 -- Some additional statements for protected entry calls
5013 -- Protected_Entry_Call (
5014 -- Object => po._object'Access,
5015 -- E => <entry index>;
5016 -- Uninterpreted_Data => P'Address;
5017 -- Mode => Simple_Call;
5021 Make_Procedure_Call_Statement (Loc,
5023 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
5025 Parameter_Associations => New_List (
5026 Make_Attribute_Reference (Loc,
5027 Attribute_Name => Name_Unchecked_Access,
5031 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
5032 New_Occurrence_Of (Comm_Name, Loc)));
5034 when System_Tasking_Protected_Objects_Single_Entry =>
5035 -- Protected_Single_Entry_Call (
5036 -- Object => po._object'Access,
5037 -- Uninterpreted_Data => P'Address);
5040 Make_Procedure_Call_Statement (Loc,
5043 (RTE (RE_Protected_Single_Entry_Call), Loc),
5045 Parameter_Associations => New_List (
5046 Make_Attribute_Reference (Loc,
5047 Attribute_Name => Name_Unchecked_Access,
5052 raise Program_Error;
5055 -- Case of task type
5059 Make_Procedure_Call_Statement (Loc,
5061 New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
5062 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
5066 Append_To (Stats, Call);
5068 -- If there are out or in/out parameters by copy add assignment
5069 -- statements for the result values.
5071 if Present (Parms) then
5072 Actual := First_Actual (N);
5073 Formal := First_Formal (Ent);
5075 Set_Assignment_OK (Actual);
5076 while Present (Actual) loop
5077 if Is_By_Copy_Type (Etype (Actual))
5078 and then Ekind (Formal) /= E_In_Parameter
5081 Make_Assignment_Statement (Loc,
5082 Name => New_Copy (Actual),
5084 Make_Explicit_Dereference (Loc,
5085 Make_Selected_Component (Loc,
5086 Prefix => New_Occurrence_Of (P, Loc),
5088 Make_Identifier (Loc, Chars (Formal)))));
5090 -- In all cases (including limited private types) we want
5091 -- the assignment to be valid.
5093 Set_Assignment_OK (Name (N_Node));
5095 -- If the call is the triggering alternative in an
5096 -- asynchronous select, or the entry_call alternative of a
5097 -- conditional entry call, the assignments for in-out
5098 -- parameters are incorporated into the statement list that
5099 -- follows, so that there are executed only if the entry
5102 if (Nkind (Parent (N)) = N_Triggering_Alternative
5103 and then N = Triggering_Statement (Parent (N)))
5105 (Nkind (Parent (N)) = N_Entry_Call_Alternative
5106 and then N = Entry_Call_Statement (Parent (N)))
5108 if No (Statements (Parent (N))) then
5109 Set_Statements (Parent (N), New_List);
5112 Prepend (N_Node, Statements (Parent (N)));
5115 Insert_After (Call, N_Node);
5119 Next_Actual (Actual);
5120 Next_Formal_With_Extras (Formal);
5124 -- Finally, create block and analyze it
5127 Make_Block_Statement (Loc,
5128 Declarations => Decls,
5129 Handled_Statement_Sequence =>
5130 Make_Handled_Sequence_Of_Statements (Loc,
5131 Statements => Stats)));
5135 end Build_Simple_Entry_Call;
5137 --------------------------------
5138 -- Build_Task_Activation_Call --
5139 --------------------------------
5141 procedure Build_Task_Activation_Call (N : Node_Id) is
5142 Loc : constant Source_Ptr := Sloc (N);
5149 -- For sequential elaboration policy, all the tasks will be activated at
5150 -- the end of the elaboration.
5152 if Partition_Elaboration_Policy = 'S' then
5156 -- Get the activation chain entity. Except in the case of a package
5157 -- body, this is in the node that was passed. For a package body, we
5158 -- have to find the corresponding package declaration node.
5160 if Nkind (N) = N_Package_Body then
5161 P := Corresponding_Spec (N);
5164 exit when Nkind (P) = N_Package_Declaration;
5167 Chain := Activation_Chain_Entity (P);
5170 Chain := Activation_Chain_Entity (N);
5173 if Present (Chain) then
5174 if Restricted_Profile then
5175 Name := New_Occurrence_Of
5176 (RTE (RE_Activate_Restricted_Tasks), Loc);
5178 Name := New_Occurrence_Of
5179 (RTE (RE_Activate_Tasks), Loc);
5183 Make_Procedure_Call_Statement (Loc,
5185 Parameter_Associations =>
5186 New_List (Make_Attribute_Reference (Loc,
5187 Prefix => New_Occurrence_Of (Chain, Loc),
5188 Attribute_Name => Name_Unchecked_Access)));
5190 if Nkind (N) = N_Package_Declaration then
5191 if Present (Corresponding_Body (N)) then
5194 elsif Present (Private_Declarations (Specification (N))) then
5195 Append (Call, Private_Declarations (Specification (N)));
5198 Append (Call, Visible_Declarations (Specification (N)));
5202 if Present (Handled_Statement_Sequence (N)) then
5204 -- The call goes at the start of the statement sequence after
5205 -- the start of exception range label if one is present.
5211 Stm := First (Statements (Handled_Statement_Sequence (N)));
5213 -- A special case, skip exception range label if one is
5214 -- present (from front end zcx processing).
5216 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
5220 -- Another special case, if the first statement is a block
5221 -- from optimization of a local raise to a goto, then the
5222 -- call goes inside this block.
5224 if Nkind (Stm) = N_Block_Statement
5225 and then Exception_Junk (Stm)
5228 First (Statements (Handled_Statement_Sequence (Stm)));
5231 -- Insertion point is after any exception label pushes,
5232 -- since we want it covered by any local handlers.
5234 while Nkind (Stm) in N_Push_xxx_Label loop
5238 -- Now we have the proper insertion point
5240 Insert_Before (Stm, Call);
5244 Set_Handled_Statement_Sequence (N,
5245 Make_Handled_Sequence_Of_Statements (Loc,
5246 Statements => New_List (Call)));
5251 Check_Task_Activation (N);
5253 end Build_Task_Activation_Call;
5255 -------------------------------
5256 -- Build_Task_Allocate_Block --
5257 -------------------------------
5259 procedure Build_Task_Allocate_Block
5264 T : constant Entity_Id := Entity (Expression (N));
5265 Init : constant Entity_Id := Base_Init_Proc (T);
5266 Loc : constant Source_Ptr := Sloc (N);
5267 Chain : constant Entity_Id :=
5268 Make_Defining_Identifier (Loc, Name_uChain);
5269 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5274 Make_Block_Statement (Loc,
5275 Identifier => New_Occurrence_Of (Blkent, Loc),
5276 Declarations => New_List (
5278 -- _Chain : Activation_Chain;
5280 Make_Object_Declaration (Loc,
5281 Defining_Identifier => Chain,
5282 Aliased_Present => True,
5283 Object_Definition =>
5284 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5286 Handled_Statement_Sequence =>
5287 Make_Handled_Sequence_Of_Statements (Loc,
5289 Statements => New_List (
5293 Make_Procedure_Call_Statement (Loc,
5294 Name => New_Occurrence_Of (Init, Loc),
5295 Parameter_Associations => Args),
5297 -- Activate_Tasks (_Chain);
5299 Make_Procedure_Call_Statement (Loc,
5300 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
5301 Parameter_Associations => New_List (
5302 Make_Attribute_Reference (Loc,
5303 Prefix => New_Occurrence_Of (Chain, Loc),
5304 Attribute_Name => Name_Unchecked_Access))))),
5306 Has_Created_Identifier => True,
5307 Is_Task_Allocation_Block => True);
5310 Make_Implicit_Label_Declaration (Loc,
5311 Defining_Identifier => Blkent,
5312 Label_Construct => Block));
5314 Append_To (Actions, Block);
5316 Set_Activation_Chain_Entity (Block, Chain);
5317 end Build_Task_Allocate_Block;
5319 -----------------------------------------------
5320 -- Build_Task_Allocate_Block_With_Init_Stmts --
5321 -----------------------------------------------
5323 procedure Build_Task_Allocate_Block_With_Init_Stmts
5326 Init_Stmts : List_Id)
5328 Loc : constant Source_Ptr := Sloc (N);
5329 Chain : constant Entity_Id :=
5330 Make_Defining_Identifier (Loc, Name_uChain);
5331 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
5335 Append_To (Init_Stmts,
5336 Make_Procedure_Call_Statement (Loc,
5337 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
5338 Parameter_Associations => New_List (
5339 Make_Attribute_Reference (Loc,
5340 Prefix => New_Occurrence_Of (Chain, Loc),
5341 Attribute_Name => Name_Unchecked_Access))));
5344 Make_Block_Statement (Loc,
5345 Identifier => New_Occurrence_Of (Blkent, Loc),
5346 Declarations => New_List (
5348 -- _Chain : Activation_Chain;
5350 Make_Object_Declaration (Loc,
5351 Defining_Identifier => Chain,
5352 Aliased_Present => True,
5353 Object_Definition =>
5354 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5356 Handled_Statement_Sequence =>
5357 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5359 Has_Created_Identifier => True,
5360 Is_Task_Allocation_Block => True);
5363 Make_Implicit_Label_Declaration (Loc,
5364 Defining_Identifier => Blkent,
5365 Label_Construct => Block));
5367 Append_To (Actions, Block);
5369 Set_Activation_Chain_Entity (Block, Chain);
5370 end Build_Task_Allocate_Block_With_Init_Stmts;
5372 -----------------------------------
5373 -- Build_Task_Proc_Specification --
5374 -----------------------------------
5376 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5377 Loc : constant Source_Ptr := Sloc (T);
5378 Spec_Id : Entity_Id;
5381 -- Case of explicit task type, suffix TB
5383 if Comes_From_Source (T) then
5385 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
5387 -- Case of anonymous task type, suffix B
5391 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
5394 Set_Is_Internal (Spec_Id);
5396 -- Associate the procedure with the task, if this is the declaration
5397 -- (and not the body) of the procedure.
5399 if No (Task_Body_Procedure (T)) then
5400 Set_Task_Body_Procedure (T, Spec_Id);
5404 Make_Procedure_Specification (Loc,
5405 Defining_Unit_Name => Spec_Id,
5406 Parameter_Specifications => New_List (
5407 Make_Parameter_Specification (Loc,
5408 Defining_Identifier =>
5409 Make_Defining_Identifier (Loc, Name_uTask),
5411 Make_Access_Definition (Loc,
5413 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
5414 end Build_Task_Proc_Specification;
5416 ---------------------------------------
5417 -- Build_Unprotected_Subprogram_Body --
5418 ---------------------------------------
5420 function Build_Unprotected_Subprogram_Body
5422 Pid : Node_Id) return Node_Id
5424 Decls : constant List_Id := Declarations (N);
5427 -- Add renamings for the Protection object, discriminals, privals, and
5428 -- the entry index constant for use by debugger.
5430 Debug_Private_Data_Declarations (Decls);
5432 -- Make an unprotected version of the subprogram for use within the same
5433 -- object, with a new name and an additional parameter representing the
5437 Make_Subprogram_Body (Sloc (N),
5439 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5440 Declarations => Decls,
5441 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5442 end Build_Unprotected_Subprogram_Body;
5444 ----------------------------
5445 -- Collect_Entry_Families --
5446 ----------------------------
5448 procedure Collect_Entry_Families
5451 Current_Node : in out Node_Id;
5452 Conctyp : Entity_Id)
5455 Efam_Decl : Node_Id;
5456 Efam_Type : Entity_Id;
5459 Efam := First_Entity (Conctyp);
5460 while Present (Efam) loop
5461 if Ekind (Efam) = E_Entry_Family then
5462 Efam_Type := Make_Temporary (Loc, 'F');
5467 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5469 Bas_Decl : Node_Id := Empty;
5474 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5476 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5477 Bas := Make_Temporary (Loc, 'B');
5480 Make_Subtype_Declaration (Loc,
5481 Defining_Identifier => Bas,
5482 Subtype_Indication =>
5483 Make_Subtype_Indication (Loc,
5485 New_Occurrence_Of (Standard_Integer, Loc),
5487 Make_Range_Constraint (Loc,
5488 Range_Expression => Make_Range (Loc,
5489 Make_Integer_Literal
5490 (Loc, -Entry_Family_Bound),
5491 Make_Integer_Literal
5492 (Loc, Entry_Family_Bound - 1)))));
5494 Insert_After (Current_Node, Bas_Decl);
5495 Current_Node := Bas_Decl;
5500 Make_Full_Type_Declaration (Loc,
5501 Defining_Identifier => Efam_Type,
5503 Make_Unconstrained_Array_Definition (Loc,
5505 (New_List (New_Occurrence_Of (Bas, Loc))),
5507 Component_Definition =>
5508 Make_Component_Definition (Loc,
5509 Aliased_Present => False,
5510 Subtype_Indication =>
5511 New_Occurrence_Of (Standard_Character, Loc))));
5514 Insert_After (Current_Node, Efam_Decl);
5515 Current_Node := Efam_Decl;
5516 Analyze (Efam_Decl);
5519 Make_Component_Declaration (Loc,
5520 Defining_Identifier =>
5521 Make_Defining_Identifier (Loc, Chars (Efam)),
5523 Component_Definition =>
5524 Make_Component_Definition (Loc,
5525 Aliased_Present => False,
5526 Subtype_Indication =>
5527 Make_Subtype_Indication (Loc,
5529 New_Occurrence_Of (Efam_Type, Loc),
5532 Make_Index_Or_Discriminant_Constraint (Loc,
5533 Constraints => New_List (
5535 (Etype (Discrete_Subtype_Definition
5536 (Parent (Efam))), Loc)))))));
5542 end Collect_Entry_Families;
5544 -----------------------
5545 -- Concurrent_Object --
5546 -----------------------
5548 function Concurrent_Object
5549 (Spec_Id : Entity_Id;
5550 Conc_Typ : Entity_Id) return Entity_Id
5553 -- Parameter _O or _object
5555 if Is_Protected_Type (Conc_Typ) then
5556 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5561 pragma Assert (Is_Task_Type (Conc_Typ));
5562 return First_Formal (Task_Body_Procedure (Conc_Typ));
5564 end Concurrent_Object;
5566 ----------------------
5567 -- Copy_Result_Type --
5568 ----------------------
5570 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5571 New_Res : constant Node_Id := New_Copy_Tree (Res);
5576 -- If the result type is an access_to_subprogram, we must create new
5577 -- entities for its spec.
5579 if Nkind (New_Res) = N_Access_Definition
5580 and then Present (Access_To_Subprogram_Definition (New_Res))
5582 -- Provide new entities for the formals
5584 Par_Spec := First (Parameter_Specifications
5585 (Access_To_Subprogram_Definition (New_Res)));
5586 while Present (Par_Spec) loop
5587 Formal := Defining_Identifier (Par_Spec);
5588 Set_Defining_Identifier (Par_Spec,
5589 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5595 end Copy_Result_Type;
5597 --------------------
5598 -- Concurrent_Ref --
5599 --------------------
5601 -- The expression returned for a reference to a concurrent object has the
5604 -- taskV!(name)._Task_Id
5608 -- objectV!(name)._Object
5610 -- for a protected object. For the case of an access to a concurrent
5611 -- object, there is an extra explicit dereference:
5613 -- taskV!(name.all)._Task_Id
5614 -- objectV!(name.all)._Object
5616 -- here taskV and objectV are the types for the associated records, which
5617 -- contain the required _Task_Id and _Object fields for tasks and protected
5618 -- objects, respectively.
5620 -- For the case of a task type name, the expression is
5624 -- i.e. a call to the Self function which returns precisely this Task_Id
5626 -- For the case of a protected type name, the expression is
5630 -- which is a renaming of the _object field of the current object
5631 -- record, passed into protected operations as a parameter.
5633 function Concurrent_Ref (N : Node_Id) return Node_Id is
5634 Loc : constant Source_Ptr := Sloc (N);
5635 Ntyp : constant Entity_Id := Etype (N);
5639 function Is_Current_Task (T : Entity_Id) return Boolean;
5640 -- Check whether the reference is to the immediately enclosing task
5641 -- type, or to an outer one (rare but legal).
5643 ---------------------
5644 -- Is_Current_Task --
5645 ---------------------
5647 function Is_Current_Task (T : Entity_Id) return Boolean is
5651 Scop := Current_Scope;
5652 while Present (Scop) and then Scop /= Standard_Standard loop
5656 elsif Is_Task_Type (Scop) then
5659 -- If this is a procedure nested within the task type, we must
5660 -- assume that it can be called from an inner task, and therefore
5661 -- cannot treat it as a local reference.
5663 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5667 Scop := Scope (Scop);
5671 -- We know that we are within the task body, so should have found it
5674 raise Program_Error;
5675 end Is_Current_Task;
5677 -- Start of processing for Concurrent_Ref
5680 if Is_Access_Type (Ntyp) then
5681 Dtyp := Designated_Type (Ntyp);
5683 if Is_Protected_Type (Dtyp) then
5684 Sel := Name_uObject;
5686 Sel := Name_uTask_Id;
5690 Make_Selected_Component (Loc,
5692 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5693 Make_Explicit_Dereference (Loc, N)),
5694 Selector_Name => Make_Identifier (Loc, Sel));
5696 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5697 if Is_Task_Type (Entity (N)) then
5699 if Is_Current_Task (Entity (N)) then
5701 Make_Function_Call (Loc,
5702 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5707 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5708 T_Body : constant Node_Id :=
5709 Parent (Corresponding_Body (Parent (Entity (N))));
5713 Make_Object_Declaration (Loc,
5714 Defining_Identifier => T_Self,
5715 Object_Definition =>
5716 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5718 Make_Function_Call (Loc,
5719 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5720 Prepend (Decl, Declarations (T_Body));
5722 Set_Scope (T_Self, Entity (N));
5723 return New_Occurrence_Of (T_Self, Loc);
5728 pragma Assert (Is_Protected_Type (Entity (N)));
5731 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5735 if Is_Protected_Type (Ntyp) then
5736 Sel := Name_uObject;
5737 elsif Is_Task_Type (Ntyp) then
5738 Sel := Name_uTask_Id;
5740 raise Program_Error;
5744 Make_Selected_Component (Loc,
5746 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5748 Selector_Name => Make_Identifier (Loc, Sel));
5752 ------------------------
5753 -- Convert_Concurrent --
5754 ------------------------
5756 function Convert_Concurrent
5758 Typ : Entity_Id) return Node_Id
5761 if not Is_Concurrent_Type (Typ) then
5765 Unchecked_Convert_To
5766 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5768 end Convert_Concurrent;
5770 -------------------------------------
5771 -- Debug_Private_Data_Declarations --
5772 -------------------------------------
5774 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5775 Debug_Nod : Node_Id;
5779 Decl := First (Decls);
5780 while Present (Decl) and then not Comes_From_Source (Decl) loop
5782 -- Declaration for concurrent entity _object and its access type,
5783 -- along with the entry index subtype:
5784 -- type prot_typVP is access prot_typV;
5785 -- _object : prot_typVP := prot_typV (_O);
5786 -- subtype Jnn is <Type of Index> range Low .. High;
5788 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5789 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5791 -- Declaration for the Protection object, discriminals, privals, and
5792 -- entry index constant:
5793 -- conc_typR : protection_typ renames _object._object;
5794 -- discr_nameD : discr_typ renames _object.discr_name;
5795 -- discr_nameD : discr_typ renames _task.discr_name;
5796 -- prival_name : comp_typ renames _object.comp_name;
5797 -- J : constant Jnn :=
5798 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5800 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5801 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5802 Debug_Nod := Debug_Renaming_Declaration (Decl);
5804 if Present (Debug_Nod) then
5805 Insert_After (Decl, Debug_Nod);
5811 end Debug_Private_Data_Declarations;
5813 ------------------------------
5814 -- Ensure_Statement_Present --
5815 ------------------------------
5817 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5821 if Opt.Suppress_Control_Flow_Optimizations
5822 and then Is_Empty_List (Statements (Alt))
5824 Stmt := Make_Null_Statement (Loc);
5826 -- Mark NULL statement as coming from source so that it is not
5827 -- eliminated by GIGI.
5829 -- Another covert channel. If this is a requirement, it must be
5830 -- documented in sinfo/einfo ???
5832 Set_Comes_From_Source (Stmt, True);
5834 Set_Statements (Alt, New_List (Stmt));
5836 end Ensure_Statement_Present;
5838 ----------------------------
5839 -- Entry_Index_Expression --
5840 ----------------------------
5842 function Entry_Index_Expression
5846 Ttyp : Entity_Id) return Node_Id
5856 -- The queues of entries and entry families appear in textual order in
5857 -- the associated record. The entry index is computed as the sum of the
5858 -- number of queues for all entries that precede the designated one, to
5859 -- which is added the index expression, if this expression denotes a
5860 -- member of a family.
5862 -- The following is a place holder for the count of simple entries
5864 Num := Make_Integer_Literal (Sloc, 1);
5866 -- We construct an expression which is a series of addition operations.
5867 -- The first operand is the number of single entries that precede this
5868 -- one, the second operand is the index value relative to the start of
5869 -- the referenced family, and the remaining operands are the lengths of
5870 -- the entry families that precede this entry, i.e. the constructed
5873 -- number_simple_entries +
5874 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5875 -- family'length + ...
5877 -- where index-value is the given index value, and s is the index
5878 -- subtype (we have to use pos because the subtype might be an
5879 -- enumeration type preventing direct subtraction). Note that the task
5880 -- entry array is one-indexed.
5882 -- The upper bound of the entry family may be a discriminant, so we
5883 -- retrieve the lower bound explicitly to compute offset, rather than
5884 -- using the index subtype which may mention a discriminant.
5886 if Present (Index) then
5887 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5895 Make_Attribute_Reference (Sloc,
5896 Attribute_Name => Name_Pos,
5897 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5898 Expressions => New_List (Relocate_Node (Index))),
5906 -- Now add lengths of preceding entries and entry families
5908 Prev := First_Entity (Ttyp);
5909 while Chars (Prev) /= Chars (Ent)
5910 or else (Ekind (Prev) /= Ekind (Ent))
5911 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5913 if Ekind (Prev) = E_Entry then
5914 Set_Intval (Num, Intval (Num) + 1);
5916 elsif Ekind (Prev) = E_Entry_Family then
5917 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5918 Lo := Type_Low_Bound (S);
5919 Hi := Type_High_Bound (S);
5924 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5926 -- Other components are anonymous types to be ignored
5936 end Entry_Index_Expression;
5938 ---------------------------
5939 -- Establish_Task_Master --
5940 ---------------------------
5942 procedure Establish_Task_Master (N : Node_Id) is
5946 if Restriction_Active (No_Task_Hierarchy) = False then
5947 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5949 -- The block may have no declarations (and nevertheless be a task
5950 -- master) if it contains a call that may return an object that
5953 if No (Declarations (N)) then
5954 Set_Declarations (N, New_List (Call));
5956 Prepend_To (Declarations (N), Call);
5961 end Establish_Task_Master;
5963 --------------------------------
5964 -- Expand_Accept_Declarations --
5965 --------------------------------
5967 -- Part of the expansion of an accept statement involves the creation of
5968 -- a declaration that can be referenced from the statement sequence of
5973 -- This declaration is inserted immediately before the accept statement
5974 -- and it is important that it be inserted before the statements of the
5975 -- statement sequence are analyzed. Thus it would be too late to create
5976 -- this declaration in the Expand_N_Accept_Statement routine, which is
5977 -- why there is a separate procedure to be called directly from Sem_Ch9.
5979 -- Ann is used to hold the address of the record containing the parameters
5980 -- (see Expand_N_Entry_Call for more details on how this record is built).
5981 -- References to the parameters do an unchecked conversion of this address
5982 -- to a pointer to the required record type, and then access the field that
5983 -- holds the value of the required parameter. The entity for the address
5984 -- variable is held as the top stack element (i.e. the last element) of the
5985 -- Accept_Address stack in the corresponding entry entity, and this element
5986 -- must be set in place before the statements are processed.
5988 -- The above description applies to the case of a stand alone accept
5989 -- statement, i.e. one not appearing as part of a select alternative.
5991 -- For the case of an accept that appears as part of a select alternative
5992 -- of a selective accept, we must still create the declaration right away,
5993 -- since Ann is needed immediately, but there is an important difference:
5995 -- The declaration is inserted before the selective accept, not before
5996 -- the accept statement (which is not part of a list anyway, and so would
5997 -- not accommodate inserted declarations)
5999 -- We only need one address variable for the entire selective accept. So
6000 -- the Ann declaration is created only for the first accept alternative,
6001 -- and subsequent accept alternatives reference the same Ann variable.
6003 -- We can distinguish the two cases by seeing whether the accept statement
6004 -- is part of a list. If not, then it must be in an accept alternative.
6006 -- To expand the requeue statement, a label is provided at the end of the
6007 -- accept statement or alternative of which it is a part, so that the
6008 -- statement can be skipped after the requeue is complete. This label is
6009 -- created here rather than during the expansion of the accept statement,
6010 -- because it will be needed by any requeue statements within the accept,
6011 -- which are expanded before the accept.
6013 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
6014 Loc : constant Source_Ptr := Sloc (N);
6015 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6016 Ann : Entity_Id := Empty;
6023 if Expander_Active then
6025 -- If we have no handled statement sequence, we may need to build
6026 -- a dummy sequence consisting of a null statement. This can be
6027 -- skipped if the trivial accept optimization is permitted.
6029 if not Trivial_Accept_OK
6030 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6032 Set_Handled_Statement_Sequence (N,
6033 Make_Handled_Sequence_Of_Statements (Loc,
6034 Statements => New_List (Make_Null_Statement (Loc))));
6037 -- Create and declare two labels to be placed at the end of the
6038 -- accept statement. The first label is used to allow requeues to
6039 -- skip the remainder of entry processing. The second label is used
6040 -- to skip the remainder of entry processing if the rendezvous
6041 -- completes in the middle of the accept body.
6043 if Present (Handled_Statement_Sequence (N)) then
6048 Ent := Make_Temporary (Loc, 'L');
6049 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
6051 Make_Implicit_Label_Declaration (Loc,
6052 Defining_Identifier => Ent,
6053 Label_Construct => Lab);
6054 Append (Lab, Statements (Handled_Statement_Sequence (N)));
6056 Ent := Make_Temporary (Loc, 'L');
6057 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
6059 Make_Implicit_Label_Declaration (Loc,
6060 Defining_Identifier => Ent,
6061 Label_Construct => Lab);
6062 Append (Lab, Statements (Handled_Statement_Sequence (N)));
6070 -- Case of stand alone accept statement
6072 if Is_List_Member (N) then
6074 if Present (Handled_Statement_Sequence (N)) then
6075 Ann := Make_Temporary (Loc, 'A');
6078 Make_Object_Declaration (Loc,
6079 Defining_Identifier => Ann,
6080 Object_Definition =>
6081 New_Occurrence_Of (RTE (RE_Address), Loc));
6083 Insert_Before_And_Analyze (N, Adecl);
6084 Insert_Before_And_Analyze (N, Ldecl);
6085 Insert_Before_And_Analyze (N, Ldecl2);
6088 -- Case of accept statement which is in an accept alternative
6092 Acc_Alt : constant Node_Id := Parent (N);
6093 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
6097 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
6098 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
6100 -- ??? Consider a single label for select statements
6102 if Present (Handled_Statement_Sequence (N)) then
6104 Statements (Handled_Statement_Sequence (N)));
6108 Statements (Handled_Statement_Sequence (N)));
6112 -- Find first accept alternative of the selective accept. A
6113 -- valid selective accept must have at least one accept in it.
6115 Alt := First (Select_Alternatives (Sel_Acc));
6117 while Nkind (Alt) /= N_Accept_Alternative loop
6121 -- If this is the first accept statement, then we have to
6122 -- create the Ann variable, as for the stand alone case, except
6123 -- that it is inserted before the selective accept. Similarly,
6124 -- a label for requeue expansion must be declared.
6126 if N = Accept_Statement (Alt) then
6127 Ann := Make_Temporary (Loc, 'A');
6129 Make_Object_Declaration (Loc,
6130 Defining_Identifier => Ann,
6131 Object_Definition =>
6132 New_Occurrence_Of (RTE (RE_Address), Loc));
6134 Insert_Before_And_Analyze (Sel_Acc, Adecl);
6136 -- If this is not the first accept statement, then find the Ann
6137 -- variable allocated by the first accept and use it.
6141 Node (Last_Elmt (Accept_Address
6142 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
6147 -- Merge here with Ann either created or referenced, and Adecl
6148 -- pointing to the corresponding declaration. Remaining processing
6149 -- is the same for the two cases.
6151 if Present (Ann) then
6152 Append_Elmt (Ann, Accept_Address (Ent));
6153 Set_Debug_Info_Needed (Ann);
6156 -- Create renaming declarations for the entry formals. Each reference
6157 -- to a formal becomes a dereference of a component of the parameter
6158 -- block, whose address is held in Ann. These declarations are
6159 -- eventually inserted into the accept block, and analyzed there so
6160 -- that they have the proper scope for gdb and do not conflict with
6161 -- other declarations.
6163 if Present (Parameter_Specifications (N))
6164 and then Present (Handled_Statement_Sequence (N))
6171 Renamed_Formal : Node_Id;
6175 Formal := First_Formal (Ent);
6177 while Present (Formal) loop
6178 Comp := Entry_Component (Formal);
6179 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
6181 Set_Etype (New_F, Etype (Formal));
6182 Set_Scope (New_F, Ent);
6184 -- Now we set debug info needed on New_F even though it does
6185 -- not come from source, so that the debugger will get the
6186 -- right information for these generated names.
6188 Set_Debug_Info_Needed (New_F);
6190 if Ekind (Formal) = E_In_Parameter then
6191 Set_Ekind (New_F, E_Constant);
6193 Set_Ekind (New_F, E_Variable);
6194 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
6197 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
6200 Make_Selected_Component (Loc,
6202 Unchecked_Convert_To (
6203 Entry_Parameters_Type (Ent),
6204 New_Occurrence_Of (Ann, Loc)),
6206 New_Occurrence_Of (Comp, Loc));
6209 Build_Renamed_Formal_Declaration
6210 (New_F, Formal, Comp, Renamed_Formal);
6212 if No (Declarations (N)) then
6213 Set_Declarations (N, New_List);
6216 Append (Decl, Declarations (N));
6217 Set_Renamed_Object (Formal, New_F);
6218 Next_Formal (Formal);
6225 end Expand_Accept_Declarations;
6227 ---------------------------------------------
6228 -- Expand_Access_Protected_Subprogram_Type --
6229 ---------------------------------------------
6231 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
6232 Loc : constant Source_Ptr := Sloc (N);
6233 T : constant Entity_Id := Defining_Identifier (N);
6234 D_T : constant Entity_Id := Designated_Type (T);
6235 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
6236 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
6237 P_List : constant List_Id :=
6238 Build_Protected_Spec (N, RTE (RE_Address), D_T, False);
6246 -- Create access to subprogram with full signature
6248 if Etype (D_T) /= Standard_Void_Type then
6250 Make_Access_Function_Definition (Loc,
6251 Parameter_Specifications => P_List,
6252 Result_Definition =>
6253 Copy_Result_Type (Result_Definition (Type_Definition (N))));
6257 Make_Access_Procedure_Definition (Loc,
6258 Parameter_Specifications => P_List);
6262 Make_Full_Type_Declaration (Loc,
6263 Defining_Identifier => D_T2,
6264 Type_Definition => Def1);
6266 -- Declare the new types before the original one since the latter will
6267 -- refer to them through the Equivalent_Type slot.
6269 Insert_Before_And_Analyze (N, Decl1);
6271 -- Associate the access to subprogram with its original access to
6272 -- protected subprogram type. Needed by the backend to know that this
6273 -- type corresponds with an access to protected subprogram type.
6275 Set_Original_Access_Type (D_T2, T);
6277 -- Create Equivalent_Type, a record with two components for an access to
6278 -- object and an access to subprogram.
6281 Make_Component_Declaration (Loc,
6282 Defining_Identifier => Make_Temporary (Loc, 'P'),
6283 Component_Definition =>
6284 Make_Component_Definition (Loc,
6285 Aliased_Present => False,
6286 Subtype_Indication =>
6287 New_Occurrence_Of (RTE (RE_Address), Loc))),
6289 Make_Component_Declaration (Loc,
6290 Defining_Identifier => Make_Temporary (Loc, 'S'),
6291 Component_Definition =>
6292 Make_Component_Definition (Loc,
6293 Aliased_Present => False,
6294 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
6297 Make_Full_Type_Declaration (Loc,
6298 Defining_Identifier => E_T,
6300 Make_Record_Definition (Loc,
6302 Make_Component_List (Loc, Component_Items => Comps)));
6304 Insert_Before_And_Analyze (N, Decl2);
6305 Set_Equivalent_Type (T, E_T);
6306 end Expand_Access_Protected_Subprogram_Type;
6308 --------------------------
6309 -- Expand_Entry_Barrier --
6310 --------------------------
6312 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
6313 Cond : constant Node_Id :=
6314 Condition (Entry_Body_Formal_Part (N));
6315 Prot : constant Entity_Id := Scope (Ent);
6316 Spec_Decl : constant Node_Id := Parent (Prot);
6317 Func : Entity_Id := Empty;
6319 Body_Decl : Node_Id;
6321 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
6322 -- Check whether entity in Barrier is external to protected type.
6323 -- If so, barrier may not be properly synchronized.
6325 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
6326 -- Check whether N follows the Pure_Barriers restriction. Return OK if
6329 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean;
6330 -- Check whether entity name N denotes a component of the protected
6331 -- object. This is used to check the Simple_Barrier restriction.
6333 ----------------------
6334 -- Is_Global_Entity --
6335 ----------------------
6337 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
6342 if Is_Entity_Name (N) and then Present (Entity (N)) then
6346 if Ekind (E) = E_Variable then
6348 -- If the variable is local to the barrier function generated
6349 -- during expansion, it is ok. If expansion is not performed,
6350 -- then Func is Empty so this test cannot succeed.
6352 if Scope (E) = Func then
6355 -- A protected call from a barrier to another object is ok
6357 elsif Ekind (Etype (E)) = E_Protected_Type then
6360 -- If the variable is within the package body we consider
6361 -- this safe. This is a common (if dubious) idiom.
6363 elsif S = Scope (Prot)
6364 and then Ekind_In (S, E_Package, E_Generic_Package)
6365 and then Nkind (Parent (E)) = N_Object_Declaration
6366 and then Nkind (Parent (Parent (E))) = N_Package_Body
6371 Error_Msg_N ("potentially unsynchronized barrier??", N);
6372 Error_Msg_N ("\& should be private component of type??", N);
6378 end Is_Global_Entity;
6380 procedure Check_Unprotected_Barrier is
6381 new Traverse_Proc (Is_Global_Entity);
6383 ----------------------------
6384 -- Is_Simple_Barrier_Name --
6385 ----------------------------
6387 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean is
6391 -- Check for case of _object.all.field (note that the explicit
6392 -- dereference gets inserted by analyze/expand of _object.field).
6394 if Expander_Active then
6395 Renamed := Renamed_Object (Entity (N));
6399 and then Nkind (Renamed) = N_Selected_Component
6400 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
6402 return Scope (Entity (N)) = Current_Scope;
6404 end Is_Simple_Barrier_Name;
6406 ---------------------
6407 -- Is_Pure_Barrier --
6408 ---------------------
6410 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
6413 when N_Expanded_Name |
6415 if No (Entity (N)) then
6419 case Ekind (Entity (N)) is
6424 E_Enumeration_Literal =>
6430 -- A variable in the protected type is expanded as a
6433 if Is_Simple_Barrier_Name (N) then
6441 when N_Integer_Literal |
6443 N_Character_Literal =>
6448 if Ekind (Entity (N)) = E_Operator then
6452 when N_Short_Circuit =>
6460 end Is_Pure_Barrier;
6462 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6464 -- Start of processing for Expand_Entry_Barrier
6467 if No_Run_Time_Mode then
6468 Error_Msg_CRT ("entry barrier", N);
6472 -- The body of the entry barrier must be analyzed in the context of the
6473 -- protected object, but its scope is external to it, just as any other
6474 -- unprotected version of a protected operation. The specification has
6475 -- been produced when the protected type declaration was elaborated. We
6476 -- build the body, insert it in the enclosing scope, but analyze it in
6477 -- the current context. A more uniform approach would be to treat the
6478 -- barrier just as a protected function, and discard the protected
6479 -- version of it because it is never called.
6481 if Expander_Active then
6482 B_F := Build_Barrier_Function (N, Ent, Prot);
6483 Func := Barrier_Function (Ent);
6484 Set_Corresponding_Spec (B_F, Func);
6486 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
6488 if Nkind (Parent (Body_Decl)) = N_Subunit then
6489 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
6492 Insert_Before_And_Analyze (Body_Decl, B_F);
6494 Set_Discriminals (Spec_Decl);
6495 Set_Scope (Func, Scope (Prot));
6498 Analyze_And_Resolve (Cond, Any_Boolean);
6501 -- Check Pure_Barriers restriction
6503 if Check_Pure_Barriers (Cond) = Abandon then
6504 Check_Restriction (Pure_Barriers, Cond);
6507 -- The Ravenscar profile restricts barriers to simple variables declared
6508 -- within the protected object. We also allow Boolean constants, since
6509 -- these appear in several published examples and are also allowed by
6512 -- Note that after analysis variables in this context will be replaced
6513 -- by the corresponding prival, that is to say a renaming of a selected
6514 -- component of the form _Object.Var. If expansion is disabled, as
6515 -- within a generic, we check that the entity appears in the current
6518 if Is_Entity_Name (Cond) then
6520 -- A small optimization of useless renamings. If the scope of the
6521 -- entity of the condition is not the barrier function, then the
6522 -- condition does not reference any of the generated renamings
6523 -- within the function.
6525 if Expander_Active and then Scope (Entity (Cond)) /= Func then
6526 Set_Declarations (B_F, Empty_List);
6529 if Entity (Cond) = Standard_False
6531 Entity (Cond) = Standard_True
6535 elsif Is_Simple_Barrier_Name (Cond) then
6540 -- It is not a boolean variable or literal, so check the restriction.
6541 -- Note that it is safe to be calling Check_Restriction from here, even
6542 -- though this is part of the expander, since Expand_Entry_Barrier is
6543 -- called from Sem_Ch9 even in -gnatc mode.
6545 Check_Restriction (Simple_Barriers, Cond);
6547 -- Emit warning if barrier contains global entities and is thus
6548 -- potentially unsynchronized.
6550 Check_Unprotected_Barrier (Cond);
6551 end Expand_Entry_Barrier;
6553 ------------------------------
6554 -- Expand_N_Abort_Statement --
6555 ------------------------------
6557 -- Expand abort T1, T2, .. Tn; into:
6558 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6560 procedure Expand_N_Abort_Statement (N : Node_Id) is
6561 Loc : constant Source_Ptr := Sloc (N);
6562 Tlist : constant List_Id := Names (N);
6568 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6571 Tasknm := First (Tlist);
6573 while Present (Tasknm) loop
6576 -- A task interface class-wide type object is being aborted. Retrieve
6577 -- its _task_id by calling a dispatching routine.
6579 if Ada_Version >= Ada_2005
6580 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6581 and then Is_Interface (Etype (Tasknm))
6582 and then Is_Task_Interface (Etype (Tasknm))
6584 Append_To (Component_Associations (Aggr),
6585 Make_Component_Association (Loc,
6586 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6589 -- Task_Id (Tasknm._disp_get_task_id)
6591 Make_Unchecked_Type_Conversion (Loc,
6593 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6595 Make_Selected_Component (Loc,
6596 Prefix => New_Copy_Tree (Tasknm),
6598 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6601 Append_To (Component_Associations (Aggr),
6602 Make_Component_Association (Loc,
6603 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6604 Expression => Concurrent_Ref (Tasknm)));
6611 Make_Procedure_Call_Statement (Loc,
6612 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6613 Parameter_Associations => New_List (
6614 Make_Qualified_Expression (Loc,
6615 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6616 Expression => Aggr))));
6619 end Expand_N_Abort_Statement;
6621 -------------------------------
6622 -- Expand_N_Accept_Statement --
6623 -------------------------------
6625 -- This procedure handles expansion of accept statements that stand alone,
6626 -- i.e. they are not part of an accept alternative. The expansion of
6627 -- accept statement in accept alternatives is handled by the routines
6628 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6629 -- following description applies only to stand alone accept statements.
6631 -- If there is no handled statement sequence, or only null statements, then
6632 -- this is called a trivial accept, and the expansion is:
6634 -- Accept_Trivial (entry-index)
6636 -- If there is a handled statement sequence, then the expansion is:
6643 -- Accept_Call (entry-index, Ann);
6644 -- Renaming_Declarations for formals
6645 -- <statement sequence from N_Accept_Statement node>
6646 -- Complete_Rendezvous;
6651 -- <exception handler from N_Accept_Statement node>
6652 -- Complete_Rendezvous;
6654 -- <exception handler from N_Accept_Statement node>
6655 -- Complete_Rendezvous;
6660 -- when all others =>
6661 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6664 -- The first three declarations were already inserted ahead of the accept
6665 -- statement by the Expand_Accept_Declarations procedure, which was called
6666 -- directly from the semantics during analysis of the accept statement,
6667 -- before analyzing its contained statements.
6669 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6670 -- from possible expansion activity (the original source of course does
6671 -- not have any declarations associated with the accept statement, since
6672 -- an accept statement has no declarative part). In particular, if the
6673 -- expander is active, the first such declaration is the declaration of
6674 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6676 -- The two blocks are merged into a single block if the inner block has
6677 -- no exception handlers, but otherwise two blocks are required, since
6678 -- exceptions might be raised in the exception handlers of the inner
6679 -- block, and Exceptional_Complete_Rendezvous must be called.
6681 procedure Expand_N_Accept_Statement (N : Node_Id) is
6682 Loc : constant Source_Ptr := Sloc (N);
6683 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6684 Ename : constant Node_Id := Entry_Direct_Name (N);
6685 Eindx : constant Node_Id := Entry_Index (N);
6686 Eent : constant Entity_Id := Entity (Ename);
6687 Acstack : constant Elist_Id := Accept_Address (Eent);
6688 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6689 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6695 -- If the accept statement is not part of a list, then its parent must
6696 -- be an accept alternative, and, as described above, we do not do any
6697 -- expansion for such accept statements at this level.
6699 if not Is_List_Member (N) then
6700 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6703 -- Trivial accept case (no statement sequence, or null statements).
6704 -- If the accept statement has declarations, then just insert them
6705 -- before the procedure call.
6707 elsif Trivial_Accept_OK
6708 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6710 -- Remove declarations for renamings, because the parameter block
6711 -- will not be assigned.
6718 D := First (Declarations (N));
6719 while Present (D) loop
6721 if Nkind (D) = N_Object_Renaming_Declaration then
6729 if Present (Declarations (N)) then
6730 Insert_Actions (N, Declarations (N));
6734 Make_Procedure_Call_Statement (Loc,
6735 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6736 Parameter_Associations => New_List (
6737 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6741 -- Discard Entry_Address that was created for it, so it will not be
6742 -- emitted if this accept statement is in the statement part of a
6743 -- delay alternative.
6745 if Present (Stats) then
6746 Remove_Last_Elmt (Acstack);
6749 -- Case of statement sequence present
6752 -- Construct the block, using the declarations from the accept
6753 -- statement if any to initialize the declarations of the block.
6755 Blkent := Make_Temporary (Loc, 'A');
6756 Set_Ekind (Blkent, E_Block);
6757 Set_Etype (Blkent, Standard_Void_Type);
6758 Set_Scope (Blkent, Current_Scope);
6761 Make_Block_Statement (Loc,
6762 Identifier => New_Occurrence_Of (Blkent, Loc),
6763 Declarations => Declarations (N),
6764 Handled_Statement_Sequence => Build_Accept_Body (N));
6766 -- For the analysis of the generated declarations, the parent node
6767 -- must be properly set.
6769 Set_Parent (Block, Parent (N));
6771 -- Prepend call to Accept_Call to main statement sequence If the
6772 -- accept has exception handlers, the statement sequence is wrapped
6773 -- in a block. Insert call and renaming declarations in the
6774 -- declarations of the block, so they are elaborated before the
6778 Make_Procedure_Call_Statement (Loc,
6779 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6780 Parameter_Associations => New_List (
6781 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6782 New_Occurrence_Of (Ann, Loc)));
6784 if Parent (Stats) = N then
6785 Prepend (Call, Statements (Stats));
6787 Set_Declarations (Parent (Stats), New_List (Call));
6792 Push_Scope (Blkent);
6800 D := First (Declarations (N));
6801 while Present (D) loop
6804 if Nkind (D) = N_Object_Renaming_Declaration then
6806 -- The renaming declarations for the formals were created
6807 -- during analysis of the accept statement, and attached to
6808 -- the list of declarations. Place them now in the context
6809 -- of the accept block or subprogram.
6812 Typ := Entity (Subtype_Mark (D));
6813 Insert_After (Call, D);
6816 -- If the formal is class_wide, it does not have an actual
6817 -- subtype. The analysis of the renaming declaration creates
6818 -- one, but we need to retain the class-wide nature of the
6821 if Is_Class_Wide_Type (Typ) then
6822 Set_Etype (Defining_Identifier (D), Typ);
6833 -- Replace the accept statement by the new block
6838 -- Last step is to unstack the Accept_Address value
6840 Remove_Last_Elmt (Acstack);
6842 end Expand_N_Accept_Statement;
6844 ----------------------------------
6845 -- Expand_N_Asynchronous_Select --
6846 ----------------------------------
6848 -- This procedure assumes that the trigger statement is an entry call or
6849 -- a dispatching procedure call. A delay alternative should already have
6850 -- been expanded into an entry call to the appropriate delay object Wait
6853 -- If the trigger is a task entry call, the select is implemented with
6854 -- a Task_Entry_Call:
6859 -- P : parms := (parm, parm, parm);
6861 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6863 -- procedure _clean is
6866 -- Cancel_Task_Entry_Call (C);
6873 -- (<acceptor-task>, -- Acceptor
6874 -- <entry-index>, -- E
6875 -- P'Address, -- Uninterpreted_Data
6876 -- Asynchronous_Call, -- Mode
6877 -- B); -- Rendezvous_Successful
6884 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6887 -- when Abort_Signal => Abort_Undefer;
6894 -- <triggered-statements>
6898 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6899 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6903 -- P : parms := (parm, parm, parm);
6905 -- Call_Simple (acceptor-task, entry-index, P'Address);
6911 -- so the task at hand is to convert the latter expansion into the former
6913 -- If the trigger is a protected entry call, the select is implemented
6914 -- with Protected_Entry_Call:
6917 -- P : E1_Params := (param, param, param);
6918 -- Bnn : Communications_Block;
6923 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6925 -- procedure _clean is
6928 -- if Enqueued (Bnn) then
6929 -- Cancel_Protected_Entry_Call (Bnn);
6936 -- Protected_Entry_Call
6937 -- (po._object'Access, -- Object
6938 -- <entry index>, -- E
6939 -- P'Address, -- Uninterpreted_Data
6940 -- Asynchronous_Call, -- Mode
6943 -- if Enqueued (Bnn) then
6947 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6950 -- when Abort_Signal => Abort_Undefer;
6953 -- if not Cancelled (Bnn) then
6954 -- <triggered-statements>
6958 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6962 -- P : E1_Params := (param, param, param);
6963 -- Bnn : Communications_Block;
6966 -- Protected_Entry_Call
6967 -- (po._object'Access, -- Object
6968 -- <entry index>, -- E
6969 -- P'Address, -- Uninterpreted_Data
6970 -- Simple_Call, -- Mode
6977 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6981 -- B : Boolean := False;
6982 -- Bnn : Communication_Block;
6983 -- C : Ada.Tags.Prim_Op_Kind;
6984 -- D : System.Storage_Elements.Dummy_Communication_Block;
6985 -- K : Ada.Tags.Tagged_Kind :=
6986 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6987 -- P : Parameters := (Param1 .. ParamN);
6992 -- if K = Ada.Tags.TK_Limited_Tagged
6993 -- or else K = Ada.Tags.TK_Tagged
6995 -- <dispatching-call>;
6996 -- <triggering-statements>;
7000 -- Ada.Tags.Get_Offset_Index
7001 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7003 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7005 -- if C = POK_Protected_Entry then
7007 -- procedure _clean is
7009 -- if Enqueued (Bnn) then
7010 -- Cancel_Protected_Entry_Call (Bnn);
7016 -- _Disp_Asynchronous_Select
7017 -- (<object>, S, P'Address, D, B);
7018 -- Bnn := Communication_Block (D);
7020 -- Param1 := P.Param1;
7022 -- ParamN := P.ParamN;
7024 -- if Enqueued (Bnn) then
7025 -- <abortable-statements>
7028 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
7031 -- when Abort_Signal => Abort_Undefer;
7034 -- if not Cancelled (Bnn) then
7035 -- <triggering-statements>
7038 -- elsif C = POK_Task_Entry then
7040 -- procedure _clean is
7042 -- Cancel_Task_Entry_Call (U);
7048 -- _Disp_Asynchronous_Select
7049 -- (<object>, S, P'Address, D, B);
7050 -- Bnn := Communication_Bloc (D);
7052 -- Param1 := P.Param1;
7054 -- ParamN := P.ParamN;
7059 -- <abortable-statements>
7061 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
7064 -- when Abort_Signal => Abort_Undefer;
7068 -- <triggering-statements>
7073 -- <dispatching-call>;
7074 -- <triggering-statements>
7079 -- The job is to convert this to the asynchronous form
7081 -- If the trigger is a delay statement, it will have been expanded into
7082 -- a call to one of the GNARL delay procedures. This routine will convert
7083 -- this into a protected entry call on a delay object and then continue
7084 -- processing as for a protected entry call trigger. This requires
7085 -- declaring a Delay_Block object and adding a pointer to this object to
7086 -- the parameter list of the delay procedure to form the parameter list of
7087 -- the entry call. This object is used by the runtime to queue the delay
7090 -- For a description of the use of P and the assignments after the call,
7091 -- see Expand_N_Entry_Call_Statement.
7093 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
7094 Loc : constant Source_Ptr := Sloc (N);
7095 Abrt : constant Node_Id := Abortable_Part (N);
7096 Trig : constant Node_Id := Triggering_Alternative (N);
7098 Abort_Block_Ent : Entity_Id;
7099 Abortable_Block : Node_Id;
7102 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
7103 Blk_Typ : Entity_Id;
7105 Call_Ent : Entity_Id;
7106 Cancel_Param : Entity_Id;
7107 Cleanup_Block : Node_Id;
7108 Cleanup_Block_Ent : Entity_Id;
7109 Cleanup_Stmts : List_Id;
7110 Conc_Typ_Stmts : List_Id;
7112 Dblock_Ent : Entity_Id;
7117 Enqueue_Call : Node_Id;
7120 Handler_Stmt : Node_Id;
7122 Lim_Typ_Stmts : List_Id;
7128 ProtE_Stmts : List_Id;
7129 ProtP_Stmts : List_Id;
7132 TaskE_Stmts : List_Id;
7135 B : Entity_Id; -- Call status flag
7136 Bnn : Entity_Id; -- Communication block
7137 C : Entity_Id; -- Call kind
7138 K : Entity_Id; -- Tagged kind
7139 P : Entity_Id; -- Parameter block
7140 S : Entity_Id; -- Primitive operation slot
7141 T : Entity_Id; -- Additional status flag
7143 procedure Rewrite_Abortable_Part;
7144 -- If the trigger is a dispatching call, the expansion inserts multiple
7145 -- copies of the abortable part. This is both inefficient, and may lead
7146 -- to duplicate definitions that the back-end will reject, when the
7147 -- abortable part includes loops. This procedure rewrites the abortable
7148 -- part into a call to a generated procedure.
7150 ----------------------------
7151 -- Rewrite_Abortable_Part --
7152 ----------------------------
7154 procedure Rewrite_Abortable_Part is
7155 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
7160 Make_Subprogram_Body (Loc,
7162 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
7163 Declarations => New_List,
7164 Handled_Statement_Sequence =>
7165 Make_Handled_Sequence_Of_Statements (Loc, Astats));
7166 Insert_Before (N, Decl);
7169 -- Rewrite abortable part into a call to this procedure.
7173 Make_Procedure_Call_Statement (Loc,
7174 Name => New_Occurrence_Of (Proc, Loc)));
7175 end Rewrite_Abortable_Part;
7177 -- Start of processing for Expand_N_Asynchronous_Select
7180 -- Asynchronous select is not supported on restricted runtimes. Don't
7183 if Restricted_Profile then
7187 Process_Statements_For_Controlled_Objects (Trig);
7188 Process_Statements_For_Controlled_Objects (Abrt);
7190 Ecall := Triggering_Statement (Trig);
7192 Ensure_Statement_Present (Sloc (Ecall), Trig);
7194 -- Retrieve Astats and Tstats now because the finalization machinery may
7195 -- wrap them in blocks.
7197 Astats := Statements (Abrt);
7198 Tstats := Statements (Trig);
7200 -- The arguments in the call may require dynamic allocation, and the
7201 -- call statement may have been transformed into a block. The block
7202 -- may contain additional declarations for internal entities, and the
7203 -- original call is found by sequential search.
7205 if Nkind (Ecall) = N_Block_Statement then
7206 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
7207 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
7208 N_Entry_Call_Statement)
7214 -- This is either a dispatching call or a delay statement used as a
7215 -- trigger which was expanded into a procedure call.
7217 if Nkind (Ecall) = N_Procedure_Call_Statement then
7218 if Ada_Version >= Ada_2005
7220 (No (Original_Node (Ecall))
7221 or else not Nkind_In (Original_Node (Ecall),
7222 N_Delay_Relative_Statement,
7223 N_Delay_Until_Statement))
7225 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
7227 Rewrite_Abortable_Part;
7231 -- Call status flag processing, generate:
7232 -- B : Boolean := False;
7234 B := Build_B (Loc, Decls);
7236 -- Communication block processing, generate:
7237 -- Bnn : Communication_Block;
7239 Bnn := Make_Temporary (Loc, 'B');
7241 Make_Object_Declaration (Loc,
7242 Defining_Identifier => Bnn,
7243 Object_Definition =>
7244 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
7246 -- Call kind processing, generate:
7247 -- C : Ada.Tags.Prim_Op_Kind;
7249 C := Build_C (Loc, Decls);
7251 -- Tagged kind processing, generate:
7252 -- K : Ada.Tags.Tagged_Kind :=
7253 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7255 -- Dummy communication block, generate:
7256 -- D : Dummy_Communication_Block;
7259 Make_Object_Declaration (Loc,
7260 Defining_Identifier =>
7261 Make_Defining_Identifier (Loc, Name_uD),
7262 Object_Definition =>
7264 (RTE (RE_Dummy_Communication_Block), Loc)));
7266 K := Build_K (Loc, Decls, Obj);
7268 -- Parameter block processing
7270 Blk_Typ := Build_Parameter_Block
7271 (Loc, Actuals, Formals, Decls);
7272 P := Parameter_Block_Pack
7273 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7275 -- Dispatch table slot processing, generate:
7278 S := Build_S (Loc, Decls);
7280 -- Additional status flag processing, generate:
7283 T := Make_Temporary (Loc, 'T');
7285 Make_Object_Declaration (Loc,
7286 Defining_Identifier => T,
7287 Object_Definition =>
7288 New_Occurrence_Of (Standard_Boolean, Loc)));
7290 ------------------------------
7291 -- Protected entry handling --
7292 ------------------------------
7295 -- Param1 := P.Param1;
7297 -- ParamN := P.ParamN;
7299 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7302 -- Bnn := Communication_Block (D);
7304 Prepend_To (Cleanup_Stmts,
7305 Make_Assignment_Statement (Loc,
7306 Name => New_Occurrence_Of (Bnn, Loc),
7308 Make_Unchecked_Type_Conversion (Loc,
7310 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7311 Expression => Make_Identifier (Loc, Name_uD))));
7314 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7316 Prepend_To (Cleanup_Stmts,
7317 Make_Procedure_Call_Statement (Loc,
7321 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7323 Parameter_Associations =>
7325 New_Copy_Tree (Obj), -- <object>
7326 New_Occurrence_Of (S, Loc), -- S
7327 Make_Attribute_Reference (Loc, -- P'Address
7328 Prefix => New_Occurrence_Of (P, Loc),
7329 Attribute_Name => Name_Address),
7330 Make_Identifier (Loc, Name_uD), -- D
7331 New_Occurrence_Of (B, Loc)))); -- B
7334 -- if Enqueued (Bnn) then
7335 -- <abortable-statements>
7338 Append_To (Cleanup_Stmts,
7339 Make_Implicit_If_Statement (N,
7341 Make_Function_Call (Loc,
7343 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7344 Parameter_Associations =>
7345 New_List (New_Occurrence_Of (Bnn, Loc))),
7348 New_Copy_List_Tree (Astats)));
7350 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7351 -- will then generate a _clean for the communication block Bnn.
7355 -- procedure _clean is
7357 -- if Enqueued (Bnn) then
7358 -- Cancel_Protected_Entry_Call (Bnn);
7367 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7369 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7371 -- Wrap the cleanup block in an exception handling block
7377 -- when Abort_Signal => Abort_Undefer;
7380 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7383 Make_Implicit_Label_Declaration (Loc,
7384 Defining_Identifier => Abort_Block_Ent),
7387 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7390 -- if not Cancelled (Bnn) then
7391 -- <triggering-statements>
7394 Append_To (ProtE_Stmts,
7395 Make_Implicit_If_Statement (N,
7399 Make_Function_Call (Loc,
7401 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7402 Parameter_Associations =>
7403 New_List (New_Occurrence_Of (Bnn, Loc)))),
7406 New_Copy_List_Tree (Tstats)));
7408 -------------------------
7409 -- Task entry handling --
7410 -------------------------
7413 -- Param1 := P.Param1;
7415 -- ParamN := P.ParamN;
7417 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7420 -- Bnn := Communication_Block (D);
7422 Append_To (TaskE_Stmts,
7423 Make_Assignment_Statement (Loc,
7425 New_Occurrence_Of (Bnn, Loc),
7427 Make_Unchecked_Type_Conversion (Loc,
7429 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7430 Expression => Make_Identifier (Loc, Name_uD))));
7433 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7435 Prepend_To (TaskE_Stmts,
7436 Make_Procedure_Call_Statement (Loc,
7439 Find_Prim_Op (Etype (Etype (Obj)),
7440 Name_uDisp_Asynchronous_Select),
7443 Parameter_Associations => New_List (
7444 New_Copy_Tree (Obj), -- <object>
7445 New_Occurrence_Of (S, Loc), -- S
7446 Make_Attribute_Reference (Loc, -- P'Address
7447 Prefix => New_Occurrence_Of (P, Loc),
7448 Attribute_Name => Name_Address),
7449 Make_Identifier (Loc, Name_uD), -- D
7450 New_Occurrence_Of (B, Loc)))); -- B
7455 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7459 -- <abortable-statements>
7461 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7464 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7466 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7467 -- will generate a _clean for the additional status flag.
7471 -- procedure _clean is
7473 -- Cancel_Task_Entry_Call (U);
7481 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7483 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7485 -- Wrap the cleanup block in an exception handling block
7491 -- when Abort_Signal => Abort_Undefer;
7494 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7496 Append_To (TaskE_Stmts,
7497 Make_Implicit_Label_Declaration (Loc,
7498 Defining_Identifier => Abort_Block_Ent));
7500 Append_To (TaskE_Stmts,
7502 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7506 -- <triggering-statements>
7509 Append_To (TaskE_Stmts,
7510 Make_Implicit_If_Statement (N,
7512 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7515 New_Copy_List_Tree (Tstats)));
7517 ----------------------------------
7518 -- Protected procedure handling --
7519 ----------------------------------
7522 -- <dispatching-call>;
7523 -- <triggering-statements>
7525 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7526 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7529 -- S := Ada.Tags.Get_Offset_Index
7530 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7533 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7536 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7538 Append_To (Conc_Typ_Stmts,
7539 Make_Procedure_Call_Statement (Loc,
7542 (Find_Prim_Op (Etype (Etype (Obj)),
7543 Name_uDisp_Get_Prim_Op_Kind),
7545 Parameter_Associations =>
7547 New_Copy_Tree (Obj),
7548 New_Occurrence_Of (S, Loc),
7549 New_Occurrence_Of (C, Loc))));
7552 -- if C = POK_Procedure_Entry then
7554 -- elsif C = POK_Task_Entry then
7560 Append_To (Conc_Typ_Stmts,
7561 Make_Implicit_If_Statement (N,
7565 New_Occurrence_Of (C, Loc),
7567 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7574 Make_Elsif_Part (Loc,
7578 New_Occurrence_Of (C, Loc),
7580 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7589 -- <dispatching-call>;
7590 -- <triggering-statements>
7592 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7593 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7596 -- if K = Ada.Tags.TK_Limited_Tagged
7597 -- or else K = Ada.Tags.TK_Tagged
7605 Make_Implicit_If_Statement (N,
7606 Condition => Build_Dispatching_Tag_Check (K, N),
7607 Then_Statements => Lim_Typ_Stmts,
7608 Else_Statements => Conc_Typ_Stmts));
7611 Make_Block_Statement (Loc,
7614 Handled_Statement_Sequence =>
7615 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7620 -- Delay triggering statement processing
7623 -- Add a Delay_Block object to the parameter list of the delay
7624 -- procedure to form the parameter list of the Wait entry call.
7626 Dblock_Ent := Make_Temporary (Loc, 'D');
7628 Pdef := Entity (Name (Ecall));
7630 if Is_RTE (Pdef, RO_CA_Delay_For) then
7632 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7634 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7636 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7638 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7639 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7642 Append_To (Parameter_Associations (Ecall),
7643 Make_Attribute_Reference (Loc,
7644 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7645 Attribute_Name => Name_Unchecked_Access));
7647 -- Create the inner block to protect the abortable part
7649 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7651 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7654 Make_Block_Statement (Loc,
7655 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7656 Handled_Statement_Sequence =>
7657 Make_Handled_Sequence_Of_Statements (Loc,
7658 Statements => Astats),
7659 Has_Created_Identifier => True,
7660 Is_Asynchronous_Call_Block => True);
7662 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7665 Make_Implicit_If_Statement (N,
7667 Make_Function_Call (Loc,
7668 Name => Enqueue_Call,
7669 Parameter_Associations => Parameter_Associations (Ecall)),
7671 New_List (Make_Block_Statement (Loc,
7672 Handled_Statement_Sequence =>
7673 Make_Handled_Sequence_Of_Statements (Loc,
7674 Statements => New_List (
7675 Make_Implicit_Label_Declaration (Loc,
7676 Defining_Identifier => Blk_Ent,
7677 Label_Construct => Abortable_Block),
7679 Exception_Handlers => Hdle)))));
7681 Stmts := New_List (Ecall);
7683 -- Construct statement sequence for new block
7686 Make_Implicit_If_Statement (N,
7688 Make_Function_Call (Loc,
7689 Name => New_Occurrence_Of (
7690 RTE (RE_Timed_Out), Loc),
7691 Parameter_Associations => New_List (
7692 Make_Attribute_Reference (Loc,
7693 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7694 Attribute_Name => Name_Unchecked_Access))),
7695 Then_Statements => Tstats));
7697 -- The result is the new block
7699 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7702 Make_Block_Statement (Loc,
7703 Declarations => New_List (
7704 Make_Object_Declaration (Loc,
7705 Defining_Identifier => Dblock_Ent,
7706 Aliased_Present => True,
7707 Object_Definition =>
7708 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7710 Handled_Statement_Sequence =>
7711 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7721 Extract_Entry (Ecall, Concval, Ename, Index);
7722 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7724 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7725 Decls := Declarations (Ecall);
7727 if Is_Protected_Type (Etype (Concval)) then
7729 -- Get the declarations of the block expanded from the entry call
7731 Decl := First (Decls);
7732 while Present (Decl)
7733 and then (Nkind (Decl) /= N_Object_Declaration
7734 or else not Is_RTE (Etype (Object_Definition (Decl)),
7735 RE_Communication_Block))
7740 pragma Assert (Present (Decl));
7741 Cancel_Param := Defining_Identifier (Decl);
7743 -- Change the mode of the Protected_Entry_Call call
7745 -- Protected_Entry_Call (
7746 -- Object => po._object'Access,
7747 -- E => <entry index>;
7748 -- Uninterpreted_Data => P'Address;
7749 -- Mode => Asynchronous_Call;
7752 -- Skip assignments to temporaries created for in-out parameters
7754 -- This makes unwarranted assumptions about the shape of the expanded
7755 -- tree for the call, and should be cleaned up ???
7757 Stmt := First (Stmts);
7758 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7764 Param := First (Parameter_Associations (Call));
7765 while Present (Param)
7766 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7771 pragma Assert (Present (Param));
7772 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7775 -- Append an if statement to execute the abortable part
7778 -- if Enqueued (Bnn) then
7781 Make_Implicit_If_Statement (N,
7783 Make_Function_Call (Loc,
7784 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7785 Parameter_Associations => New_List (
7786 New_Occurrence_Of (Cancel_Param, Loc))),
7787 Then_Statements => Astats));
7790 Make_Block_Statement (Loc,
7791 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7792 Handled_Statement_Sequence =>
7793 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7794 Has_Created_Identifier => True,
7795 Is_Asynchronous_Call_Block => True);
7797 -- Aborts are not deferred at beginning of exception handlers in
7800 if ZCX_Exceptions then
7801 Handler_Stmt := Make_Null_Statement (Loc);
7804 Handler_Stmt := Build_Runtime_Call (Loc, RE_Abort_Undefer);
7808 Make_Block_Statement (Loc,
7809 Handled_Statement_Sequence =>
7810 Make_Handled_Sequence_Of_Statements (Loc,
7811 Statements => New_List (
7812 Make_Implicit_Label_Declaration (Loc,
7813 Defining_Identifier => Blk_Ent,
7814 Label_Construct => Abortable_Block),
7819 Exception_Handlers => New_List (
7820 Make_Implicit_Exception_Handler (Loc,
7822 -- when Abort_Signal =>
7823 -- Abort_Undefer.all;
7825 Exception_Choices =>
7826 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7827 Statements => New_List (Handler_Stmt))))),
7829 -- if not Cancelled (Bnn) then
7830 -- triggered statements
7833 Make_Implicit_If_Statement (N,
7834 Condition => Make_Op_Not (Loc,
7836 Make_Function_Call (Loc,
7837 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7838 Parameter_Associations => New_List (
7839 New_Occurrence_Of (Cancel_Param, Loc)))),
7840 Then_Statements => Tstats));
7842 -- Asynchronous task entry call
7849 B := Make_Defining_Identifier (Loc, Name_uB);
7851 -- Insert declaration of B in declarations of existing block
7854 Make_Object_Declaration (Loc,
7855 Defining_Identifier => B,
7856 Object_Definition =>
7857 New_Occurrence_Of (Standard_Boolean, Loc)));
7859 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7861 -- Insert declaration of C in declarations of existing block
7864 Make_Object_Declaration (Loc,
7865 Defining_Identifier => Cancel_Param,
7866 Object_Definition =>
7867 New_Occurrence_Of (Standard_Boolean, Loc)));
7869 -- Remove and save the call to Call_Simple
7871 Stmt := First (Stmts);
7873 -- Skip assignments to temporaries created for in-out parameters.
7874 -- This makes unwarranted assumptions about the shape of the expanded
7875 -- tree for the call, and should be cleaned up ???
7877 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7883 -- Create the inner block to protect the abortable part
7885 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7887 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7890 Make_Block_Statement (Loc,
7891 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7892 Handled_Statement_Sequence =>
7893 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7894 Has_Created_Identifier => True,
7895 Is_Asynchronous_Call_Block => True);
7898 Make_Block_Statement (Loc,
7899 Handled_Statement_Sequence =>
7900 Make_Handled_Sequence_Of_Statements (Loc,
7901 Statements => New_List (
7902 Make_Implicit_Label_Declaration (Loc,
7903 Defining_Identifier => Blk_Ent,
7904 Label_Construct => Abortable_Block),
7906 Exception_Handlers => Hdle)));
7908 -- Create new call statement
7910 Params := Parameter_Associations (Call);
7913 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7914 Append_To (Params, New_Occurrence_Of (B, Loc));
7917 Make_Procedure_Call_Statement (Loc,
7918 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7919 Parameter_Associations => Params));
7921 -- Construct statement sequence for new block
7924 Make_Implicit_If_Statement (N,
7926 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7927 Then_Statements => Tstats));
7929 -- Protected the call against abort
7931 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7934 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7936 -- The result is the new block
7939 Make_Block_Statement (Loc,
7940 Declarations => Decls,
7941 Handled_Statement_Sequence =>
7942 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7945 end Expand_N_Asynchronous_Select;
7947 -------------------------------------
7948 -- Expand_N_Conditional_Entry_Call --
7949 -------------------------------------
7951 -- The conditional task entry call is converted to a call to
7956 -- P : parms := (parm, parm, parm);
7960 -- (<acceptor-task>, -- Acceptor
7961 -- <entry-index>, -- E
7962 -- P'Address, -- Uninterpreted_Data
7963 -- Conditional_Call, -- Mode
7964 -- B); -- Rendezvous_Successful
7969 -- normal-statements
7975 -- For a description of the use of P and the assignments after the call,
7976 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7977 -- conditional entry call has already been expanded (by the Expand_N_Entry
7978 -- _Call_Statement procedure) as follows:
7981 -- P : parms := (parm, parm, parm);
7983 -- ... info for in-out parameters
7984 -- Call_Simple (acceptor-task, entry-index, P'Address);
7990 -- so the task at hand is to convert the latter expansion into the former
7992 -- The conditional protected entry call is converted to a call to
7993 -- Protected_Entry_Call:
7996 -- P : parms := (parm, parm, parm);
7997 -- Bnn : Communications_Block;
8000 -- Protected_Entry_Call
8001 -- (po._object'Access, -- Object
8002 -- <entry index>, -- E
8003 -- P'Address, -- Uninterpreted_Data
8004 -- Conditional_Call, -- Mode
8009 -- if Cancelled (Bnn) then
8012 -- normal-statements
8016 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
8020 -- B : Boolean := False;
8021 -- C : Ada.Tags.Prim_Op_Kind;
8022 -- K : Ada.Tags.Tagged_Kind :=
8023 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
8024 -- P : Parameters := (Param1 .. ParamN);
8028 -- if K = Ada.Tags.TK_Limited_Tagged
8029 -- or else K = Ada.Tags.TK_Tagged
8031 -- <dispatching-call>;
8032 -- <triggering-statements>
8036 -- Ada.Tags.Get_Offset_Index
8037 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
8039 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
8041 -- if C = POK_Protected_Entry
8042 -- or else C = POK_Task_Entry
8044 -- Param1 := P.Param1;
8046 -- ParamN := P.ParamN;
8050 -- if C = POK_Procedure
8051 -- or else C = POK_Protected_Procedure
8052 -- or else C = POK_Task_Procedure
8054 -- <dispatching-call>;
8057 -- <triggering-statements>
8059 -- <else-statements>
8064 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
8065 Loc : constant Source_Ptr := Sloc (N);
8066 Alt : constant Node_Id := Entry_Call_Alternative (N);
8067 Blk : Node_Id := Entry_Call_Statement (Alt);
8070 Blk_Typ : Entity_Id;
8072 Call_Ent : Entity_Id;
8073 Conc_Typ_Stmts : List_Id;
8077 Lim_Typ_Stmts : List_Id;
8084 Transient_Blk : Node_Id;
8087 B : Entity_Id; -- Call status flag
8088 C : Entity_Id; -- Call kind
8089 K : Entity_Id; -- Tagged kind
8090 P : Entity_Id; -- Parameter block
8091 S : Entity_Id; -- Primitive operation slot
8094 Process_Statements_For_Controlled_Objects (N);
8096 if Ada_Version >= Ada_2005
8097 and then Nkind (Blk) = N_Procedure_Call_Statement
8099 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
8104 -- Call status flag processing, generate:
8105 -- B : Boolean := False;
8107 B := Build_B (Loc, Decls);
8109 -- Call kind processing, generate:
8110 -- C : Ada.Tags.Prim_Op_Kind;
8112 C := Build_C (Loc, Decls);
8114 -- Tagged kind processing, generate:
8115 -- K : Ada.Tags.Tagged_Kind :=
8116 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
8118 K := Build_K (Loc, Decls, Obj);
8120 -- Parameter block processing
8122 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
8123 P := Parameter_Block_Pack
8124 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
8126 -- Dispatch table slot processing, generate:
8129 S := Build_S (Loc, Decls);
8132 -- S := Ada.Tags.Get_Offset_Index
8133 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
8136 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
8139 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
8141 Append_To (Conc_Typ_Stmts,
8142 Make_Procedure_Call_Statement (Loc,
8145 Find_Prim_Op (Etype (Etype (Obj)),
8146 Name_uDisp_Conditional_Select),
8148 Parameter_Associations =>
8150 New_Copy_Tree (Obj), -- <object>
8151 New_Occurrence_Of (S, Loc), -- S
8152 Make_Attribute_Reference (Loc, -- P'Address
8153 Prefix => New_Occurrence_Of (P, Loc),
8154 Attribute_Name => Name_Address),
8155 New_Occurrence_Of (C, Loc), -- C
8156 New_Occurrence_Of (B, Loc)))); -- B
8159 -- if C = POK_Protected_Entry
8160 -- or else C = POK_Task_Entry
8162 -- Param1 := P.Param1;
8164 -- ParamN := P.ParamN;
8167 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
8169 -- Generate the if statement only when the packed parameters need
8170 -- explicit assignments to their corresponding actuals.
8172 if Present (Unpack) then
8173 Append_To (Conc_Typ_Stmts,
8174 Make_Implicit_If_Statement (N,
8180 New_Occurrence_Of (C, Loc),
8182 New_Occurrence_Of (RTE (
8183 RE_POK_Protected_Entry), Loc)),
8188 New_Occurrence_Of (C, Loc),
8190 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
8192 Then_Statements => Unpack));
8197 -- if C = POK_Procedure
8198 -- or else C = POK_Protected_Procedure
8199 -- or else C = POK_Task_Procedure
8201 -- <dispatching-call>
8203 -- <normal-statements>
8205 -- <else-statements>
8208 N_Stats := New_Copy_List_Tree (Statements (Alt));
8210 Prepend_To (N_Stats,
8211 Make_Implicit_If_Statement (N,
8217 New_Occurrence_Of (C, Loc),
8219 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
8226 New_Occurrence_Of (C, Loc),
8228 New_Occurrence_Of (RTE (
8229 RE_POK_Protected_Procedure), Loc)),
8234 New_Occurrence_Of (C, Loc),
8236 New_Occurrence_Of (RTE (
8237 RE_POK_Task_Procedure), Loc)))),
8242 Append_To (Conc_Typ_Stmts,
8243 Make_Implicit_If_Statement (N,
8244 Condition => New_Occurrence_Of (B, Loc),
8245 Then_Statements => N_Stats,
8246 Else_Statements => Else_Statements (N)));
8249 -- <dispatching-call>;
8250 -- <triggering-statements>
8252 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
8253 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
8256 -- if K = Ada.Tags.TK_Limited_Tagged
8257 -- or else K = Ada.Tags.TK_Tagged
8265 Make_Implicit_If_Statement (N,
8266 Condition => Build_Dispatching_Tag_Check (K, N),
8267 Then_Statements => Lim_Typ_Stmts,
8268 Else_Statements => Conc_Typ_Stmts));
8271 Make_Block_Statement (Loc,
8274 Handled_Statement_Sequence =>
8275 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8277 -- As described above, the entry alternative is transformed into a
8278 -- block that contains the gnulli call, and possibly assignment
8279 -- statements for in-out parameters. The gnulli call may itself be
8280 -- rewritten into a transient block if some unconstrained parameters
8281 -- require it. We need to retrieve the call to complete its parameter
8286 First_Real_Statement (Handled_Statement_Sequence (Blk));
8288 if Present (Transient_Blk)
8289 and then Nkind (Transient_Blk) = N_Block_Statement
8291 Blk := Transient_Blk;
8294 Stmts := Statements (Handled_Statement_Sequence (Blk));
8295 Stmt := First (Stmts);
8296 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
8301 Params := Parameter_Associations (Call);
8303 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
8305 -- Substitute Conditional_Entry_Call for Simple_Call parameter
8307 Param := First (Params);
8308 while Present (Param)
8309 and then not Is_RTE (Etype (Param), RE_Call_Modes)
8314 pragma Assert (Present (Param));
8316 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8320 -- Find the Communication_Block parameter for the call to the
8321 -- Cancelled function.
8323 Decl := First (Declarations (Blk));
8324 while Present (Decl)
8325 and then not Is_RTE (Etype (Object_Definition (Decl)),
8326 RE_Communication_Block)
8331 -- Add an if statement to execute the else part if the call
8332 -- does not succeed (as indicated by the Cancelled predicate).
8335 Make_Implicit_If_Statement (N,
8336 Condition => Make_Function_Call (Loc,
8337 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
8338 Parameter_Associations => New_List (
8339 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8340 Then_Statements => Else_Statements (N),
8341 Else_Statements => Statements (Alt)));
8344 B := Make_Defining_Identifier (Loc, Name_uB);
8346 -- Insert declaration of B in declarations of existing block
8348 if No (Declarations (Blk)) then
8349 Set_Declarations (Blk, New_List);
8352 Prepend_To (Declarations (Blk),
8353 Make_Object_Declaration (Loc,
8354 Defining_Identifier => B,
8355 Object_Definition =>
8356 New_Occurrence_Of (Standard_Boolean, Loc)));
8358 -- Create new call statement
8361 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8362 Append_To (Params, New_Occurrence_Of (B, Loc));
8365 Make_Procedure_Call_Statement (Loc,
8366 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8367 Parameter_Associations => Params));
8369 -- Construct statement sequence for new block
8372 Make_Implicit_If_Statement (N,
8373 Condition => New_Occurrence_Of (B, Loc),
8374 Then_Statements => Statements (Alt),
8375 Else_Statements => Else_Statements (N)));
8378 -- The result is the new block
8381 Make_Block_Statement (Loc,
8382 Declarations => Declarations (Blk),
8383 Handled_Statement_Sequence =>
8384 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8388 end Expand_N_Conditional_Entry_Call;
8390 ---------------------------------------
8391 -- Expand_N_Delay_Relative_Statement --
8392 ---------------------------------------
8394 -- Delay statement is implemented as a procedure call to Delay_For
8395 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8396 -- simple delays imposed by the use of Protected Objects.
8398 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8399 Loc : constant Source_Ptr := Sloc (N);
8403 -- Try to use System.Relative_Delays.Delay_For only if available. This
8404 -- is the implementation used on restricted platforms when Ada.Calendar
8405 -- is not available.
8407 if RTE_Available (RO_RD_Delay_For) then
8408 Proc := RTE (RO_RD_Delay_For);
8410 -- Otherwise, use Ada.Calendar.Delays.Delay_For and emit an error
8411 -- message if not available.
8414 Proc := RTE (RO_CA_Delay_For);
8418 Make_Procedure_Call_Statement (Loc,
8419 Name => New_Occurrence_Of (Proc, Loc),
8420 Parameter_Associations => New_List (Expression (N))));
8422 end Expand_N_Delay_Relative_Statement;
8424 ------------------------------------
8425 -- Expand_N_Delay_Until_Statement --
8426 ------------------------------------
8428 -- Delay Until statement is implemented as a procedure call to
8429 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8431 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8432 Loc : constant Source_Ptr := Sloc (N);
8436 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8437 Typ := RTE (RO_CA_Delay_Until);
8439 Typ := RTE (RO_RT_Delay_Until);
8443 Make_Procedure_Call_Statement (Loc,
8444 Name => New_Occurrence_Of (Typ, Loc),
8445 Parameter_Associations => New_List (Expression (N))));
8448 end Expand_N_Delay_Until_Statement;
8450 -------------------------
8451 -- Expand_N_Entry_Body --
8452 -------------------------
8454 procedure Expand_N_Entry_Body (N : Node_Id) is
8456 -- Associate discriminals with the next protected operation body to be
8459 if Present (Next_Protected_Operation (N)) then
8460 Set_Discriminals (Parent (Current_Scope));
8462 end Expand_N_Entry_Body;
8464 -----------------------------------
8465 -- Expand_N_Entry_Call_Statement --
8466 -----------------------------------
8468 -- An entry call is expanded into GNARLI calls to implement a simple entry
8469 -- call (see Build_Simple_Entry_Call).
8471 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8477 if No_Run_Time_Mode then
8478 Error_Msg_CRT ("entry call", N);
8482 -- If this entry call is part of an asynchronous select, don't expand it
8483 -- here; it will be expanded with the select statement. Don't expand
8484 -- timed entry calls either, as they are translated into asynchronous
8487 -- ??? This whole approach is questionable; it may be better to go back
8488 -- to allowing the expansion to take place and then attempting to fix it
8489 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8490 -- whether the expanded call is on a task or protected entry.
8492 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8493 or else N /= Triggering_Statement (Parent (N)))
8494 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8495 or else N /= Entry_Call_Statement (Parent (N))
8496 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8498 Extract_Entry (N, Concval, Ename, Index);
8499 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8501 end Expand_N_Entry_Call_Statement;
8503 --------------------------------
8504 -- Expand_N_Entry_Declaration --
8505 --------------------------------
8507 -- If there are parameters, then first, each of the formals is marked by
8508 -- setting Is_Entry_Formal. Next a record type is built which is used to
8509 -- hold the parameter values. The name of this record type is entryP where
8510 -- entry is the name of the entry, with an additional corresponding access
8511 -- type called entryPA. The record type has matching components for each
8512 -- formal (the component names are the same as the formal names). For
8513 -- elementary types, the component type matches the formal type. For
8514 -- composite types, an access type is declared (with the name formalA)
8515 -- which designates the formal type, and the type of the component is this
8516 -- access type. Finally the Entry_Component of each formal is set to
8517 -- reference the corresponding record component.
8519 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8520 Loc : constant Source_Ptr := Sloc (N);
8521 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8522 Components : List_Id;
8525 Last_Decl : Node_Id;
8526 Component : Entity_Id;
8529 Rec_Ent : Entity_Id;
8530 Acc_Ent : Entity_Id;
8533 Formal := First_Formal (Entry_Ent);
8536 -- Most processing is done only if parameters are present
8538 if Present (Formal) then
8539 Components := New_List;
8541 -- Loop through formals
8543 while Present (Formal) loop
8544 Set_Is_Entry_Formal (Formal);
8546 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8547 Set_Entry_Component (Formal, Component);
8548 Set_Entry_Formal (Component, Formal);
8549 Ftype := Etype (Formal);
8551 -- Declare new access type and then append
8553 Ctype := Make_Temporary (Loc, 'A');
8554 Set_Is_Param_Block_Component_Type (Ctype);
8557 Make_Full_Type_Declaration (Loc,
8558 Defining_Identifier => Ctype,
8560 Make_Access_To_Object_Definition (Loc,
8561 All_Present => True,
8562 Constant_Present => Ekind (Formal) = E_In_Parameter,
8563 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8565 Insert_After (Last_Decl, Decl);
8568 Append_To (Components,
8569 Make_Component_Declaration (Loc,
8570 Defining_Identifier => Component,
8571 Component_Definition =>
8572 Make_Component_Definition (Loc,
8573 Aliased_Present => False,
8574 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8576 Next_Formal_With_Extras (Formal);
8579 -- Create the Entry_Parameter_Record declaration
8581 Rec_Ent := Make_Temporary (Loc, 'P');
8584 Make_Full_Type_Declaration (Loc,
8585 Defining_Identifier => Rec_Ent,
8587 Make_Record_Definition (Loc,
8589 Make_Component_List (Loc,
8590 Component_Items => Components)));
8592 Insert_After (Last_Decl, Decl);
8595 -- Construct and link in the corresponding access type
8597 Acc_Ent := Make_Temporary (Loc, 'A');
8599 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8602 Make_Full_Type_Declaration (Loc,
8603 Defining_Identifier => Acc_Ent,
8605 Make_Access_To_Object_Definition (Loc,
8606 All_Present => True,
8607 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8609 Insert_After (Last_Decl, Decl);
8611 end Expand_N_Entry_Declaration;
8613 -----------------------------
8614 -- Expand_N_Protected_Body --
8615 -----------------------------
8617 -- Protected bodies are expanded to the completion of the subprograms
8618 -- created for the corresponding protected type. These are a protected and
8619 -- unprotected version of each protected subprogram in the object, a
8620 -- function to calculate each entry barrier, and a procedure to execute the
8621 -- sequence of statements of each protected entry body. For example, for
8622 -- protected type ptype:
8625 -- (O : System.Address;
8626 -- E : Protected_Entry_Index)
8629 -- <discriminant renamings>
8630 -- <private object renamings>
8632 -- return <barrier expression>;
8635 -- procedure pprocN (_object : in out poV;...) is
8636 -- <discriminant renamings>
8637 -- <private object renamings>
8639 -- <sequence of statements>
8642 -- procedure pprocP (_object : in out poV;...) is
8643 -- procedure _clean is
8646 -- ptypeS (_object, Pn);
8647 -- Unlock (_object._object'Access);
8648 -- Abort_Undefer.all;
8653 -- Lock (_object._object'Access);
8654 -- pprocN (_object;...);
8659 -- function pfuncN (_object : poV;...) return Return_Type is
8660 -- <discriminant renamings>
8661 -- <private object renamings>
8663 -- <sequence of statements>
8666 -- function pfuncP (_object : poV) return Return_Type is
8667 -- procedure _clean is
8669 -- Unlock (_object._object'Access);
8670 -- Abort_Undefer.all;
8675 -- Lock (_object._object'Access);
8676 -- return pfuncN (_object);
8683 -- (O : System.Address;
8684 -- P : System.Address;
8685 -- E : Protected_Entry_Index)
8687 -- <discriminant renamings>
8688 -- <private object renamings>
8689 -- type poVP is access poV;
8690 -- _Object : ptVP := ptVP!(O);
8694 -- <statement sequence>
8695 -- Complete_Entry_Body (_Object._Object);
8697 -- when all others =>
8698 -- Exceptional_Complete_Entry_Body (
8699 -- _Object._Object, Get_GNAT_Exception);
8703 -- The type poV is the record created for the protected type to hold
8704 -- the state of the protected object.
8706 procedure Expand_N_Protected_Body (N : Node_Id) is
8707 Loc : constant Source_Ptr := Sloc (N);
8708 Pid : constant Entity_Id := Corresponding_Spec (N);
8710 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8711 -- This flag indicates whether the lock free implementation is active
8713 Current_Node : Node_Id;
8714 Disp_Op_Body : Node_Id;
8715 New_Op_Body : Node_Id;
8719 function Build_Dispatching_Subprogram_Body
8722 Prot_Bod : Node_Id) return Node_Id;
8723 -- Build a dispatching version of the protected subprogram body. The
8724 -- newly generated subprogram contains a call to the original protected
8725 -- body. The following code is generated:
8727 -- function <protected-function-name> (Param1 .. ParamN) return
8730 -- return <protected-function-name>P (Param1 .. ParamN);
8731 -- end <protected-function-name>;
8735 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8737 -- <protected-procedure-name>P (Param1 .. ParamN);
8738 -- end <protected-procedure-name>
8740 ---------------------------------------
8741 -- Build_Dispatching_Subprogram_Body --
8742 ---------------------------------------
8744 function Build_Dispatching_Subprogram_Body
8747 Prot_Bod : Node_Id) return Node_Id
8749 Loc : constant Source_Ptr := Sloc (N);
8756 -- Generate a specification without a letter suffix in order to
8757 -- override an interface function or procedure.
8759 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8761 -- The formal parameters become the actuals of the protected function
8762 -- or procedure call.
8764 Actuals := New_List;
8765 Formal := First (Parameter_Specifications (Spec));
8766 while Present (Formal) loop
8768 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8772 if Nkind (Spec) = N_Procedure_Specification then
8775 Make_Procedure_Call_Statement (Loc,
8777 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8778 Parameter_Associations => Actuals));
8781 pragma Assert (Nkind (Spec) = N_Function_Specification);
8785 Make_Simple_Return_Statement (Loc,
8787 Make_Function_Call (Loc,
8789 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8790 Parameter_Associations => Actuals)));
8794 Make_Subprogram_Body (Loc,
8795 Declarations => Empty_List,
8796 Specification => Spec,
8797 Handled_Statement_Sequence =>
8798 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8799 end Build_Dispatching_Subprogram_Body;
8801 -- Start of processing for Expand_N_Protected_Body
8804 if No_Run_Time_Mode then
8805 Error_Msg_CRT ("protected body", N);
8809 -- This is the proper body corresponding to a stub. The declarations
8810 -- must be inserted at the point of the stub, which in turn is in the
8811 -- declarative part of the parent unit.
8813 if Nkind (Parent (N)) = N_Subunit then
8814 Current_Node := Corresponding_Stub (Parent (N));
8819 Op_Body := First (Declarations (N));
8821 -- The protected body is replaced with the bodies of its
8822 -- protected operations, and the declarations for internal objects
8823 -- that may have been created for entry family bounds.
8825 Rewrite (N, Make_Null_Statement (Sloc (N)));
8828 while Present (Op_Body) loop
8829 case Nkind (Op_Body) is
8830 when N_Subprogram_Declaration =>
8833 when N_Subprogram_Body =>
8835 -- Do not create bodies for eliminated operations
8837 if not Is_Eliminated (Defining_Entity (Op_Body))
8838 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8840 if Lock_Free_Active then
8842 Build_Lock_Free_Unprotected_Subprogram_Body
8846 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8849 Insert_After (Current_Node, New_Op_Body);
8850 Current_Node := New_Op_Body;
8851 Analyze (New_Op_Body);
8853 -- Build the corresponding protected operation. It may
8854 -- appear that this is needed only if this is a visible
8855 -- operation of the type, or if it is an interrupt handler,
8856 -- and this was the strategy used previously in GNAT.
8858 -- However, the operation may be exported through a 'Access
8859 -- to an external caller. This is the common idiom in code
8860 -- that uses the Ada 2005 Timing_Events package. As a result
8861 -- we need to produce the protected body for both visible
8862 -- and private operations, as well as operations that only
8863 -- have a body in the source, and for which we create a
8864 -- declaration in the protected body itself.
8866 if Present (Corresponding_Spec (Op_Body)) then
8867 if Lock_Free_Active then
8869 Build_Lock_Free_Protected_Subprogram_Body
8870 (Op_Body, Pid, Specification (New_Op_Body));
8873 Build_Protected_Subprogram_Body
8874 (Op_Body, Pid, Specification (New_Op_Body));
8877 Insert_After (Current_Node, New_Op_Body);
8878 Analyze (New_Op_Body);
8880 Current_Node := New_Op_Body;
8882 -- Generate an overriding primitive operation body for
8883 -- this subprogram if the protected type implements an
8886 if Ada_Version >= Ada_2005
8888 Present (Interfaces (Corresponding_Record_Type (Pid)))
8891 Build_Dispatching_Subprogram_Body
8892 (Op_Body, Pid, New_Op_Body);
8894 Insert_After (Current_Node, Disp_Op_Body);
8895 Analyze (Disp_Op_Body);
8897 Current_Node := Disp_Op_Body;
8902 when N_Entry_Body =>
8903 Op_Id := Defining_Identifier (Op_Body);
8904 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8906 Insert_After (Current_Node, New_Op_Body);
8907 Current_Node := New_Op_Body;
8908 Analyze (New_Op_Body);
8910 when N_Implicit_Label_Declaration =>
8913 when N_Itype_Reference =>
8914 Insert_After (Current_Node, New_Copy (Op_Body));
8916 when N_Freeze_Entity =>
8917 New_Op_Body := New_Copy (Op_Body);
8919 if Present (Entity (Op_Body))
8920 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8922 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8925 Insert_After (Current_Node, New_Op_Body);
8926 Current_Node := New_Op_Body;
8927 Analyze (New_Op_Body);
8930 New_Op_Body := New_Copy (Op_Body);
8931 Insert_After (Current_Node, New_Op_Body);
8932 Current_Node := New_Op_Body;
8933 Analyze (New_Op_Body);
8935 when N_Object_Declaration =>
8936 pragma Assert (not Comes_From_Source (Op_Body));
8937 New_Op_Body := New_Copy (Op_Body);
8938 Insert_After (Current_Node, New_Op_Body);
8939 Current_Node := New_Op_Body;
8940 Analyze (New_Op_Body);
8943 raise Program_Error;
8950 -- Finally, create the body of the function that maps an entry index
8951 -- into the corresponding body index, except when there is no entry, or
8952 -- in a Ravenscar-like profile.
8954 if Corresponding_Runtime_Package (Pid) =
8955 System_Tasking_Protected_Objects_Entries
8957 New_Op_Body := Build_Find_Body_Index (Pid);
8958 Insert_After (Current_Node, New_Op_Body);
8959 Current_Node := New_Op_Body;
8960 Analyze (New_Op_Body);
8963 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8964 -- protected body. At this point all wrapper specs have been created,
8965 -- frozen and included in the dispatch table for the protected type.
8967 if Ada_Version >= Ada_2005 then
8968 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8970 end Expand_N_Protected_Body;
8972 -----------------------------------------
8973 -- Expand_N_Protected_Type_Declaration --
8974 -----------------------------------------
8976 -- First we create a corresponding record type declaration used to
8977 -- represent values of this protected type.
8978 -- The general form of this type declaration is
8980 -- type poV (discriminants) is record
8981 -- _Object : aliased <kind>Protection
8982 -- [(<entry count> [, <handler count>])];
8983 -- [entry_family : array (bounds) of Void;]
8984 -- <private data fields>
8987 -- The discriminants are present only if the corresponding protected type
8988 -- has discriminants, and they exactly mirror the protected type
8989 -- discriminants. The private data fields similarly mirror the private
8990 -- declarations of the protected type.
8992 -- The Object field is always present. It contains RTS specific data used
8993 -- to control the protected object. It is declared as Aliased so that it
8994 -- can be passed as a pointer to the RTS. This allows the protected record
8995 -- to be referenced within RTS data structures. An appropriate Protection
8996 -- type and discriminant are generated.
8998 -- The Service field is present for protected objects with entries. It
8999 -- contains sufficient information to allow the entry service procedure for
9000 -- this object to be called when the object is not known till runtime.
9002 -- One entry_family component is present for each entry family in the
9003 -- task definition (see Expand_N_Task_Type_Declaration).
9005 -- When a protected object is declared, an instance of the protected type
9006 -- value record is created. The elaboration of this declaration creates the
9007 -- correct bounds for the entry families, and also evaluates the priority
9008 -- expression if needed. The initialization routine for the protected type
9009 -- itself then calls Initialize_Protection with appropriate parameters to
9010 -- initialize the value of the Task_Id field. Install_Handlers may be also
9011 -- called if a pragma Attach_Handler applies.
9013 -- Note: this record is passed to the subprograms created by the expansion
9014 -- of protected subprograms and entries. It is an in parameter to protected
9015 -- functions and an in out parameter to procedures and entry bodies. The
9016 -- Entity_Id for this created record type is placed in the
9017 -- Corresponding_Record_Type field of the associated protected type entity.
9019 -- Next we create a procedure specifications for protected subprograms and
9020 -- entry bodies. For each protected subprograms two subprograms are
9021 -- created, an unprotected and a protected version. The unprotected version
9022 -- is called from within other operations of the same protected object.
9024 -- We also build the call to register the procedure if a pragma
9025 -- Interrupt_Handler applies.
9027 -- A single subprogram is created to service all entry bodies; it has an
9028 -- additional boolean out parameter indicating that the previous entry call
9029 -- made by the current task was serviced immediately, i.e. not by proxy.
9030 -- The O parameter contains a pointer to a record object of the type
9031 -- described above. An untyped interface is used here to allow this
9032 -- procedure to be called in places where the type of the object to be
9033 -- serviced is not known. This must be done, for example, when a call that
9034 -- may have been requeued is cancelled; the corresponding object must be
9035 -- serviced, but which object that is not known till runtime.
9038 -- (O : System.Address; P : out Boolean);
9039 -- procedure pprocN (_object : in out poV);
9040 -- procedure pproc (_object : in out poV);
9041 -- function pfuncN (_object : poV);
9042 -- function pfunc (_object : poV);
9045 -- Note that this must come after the record type declaration, since
9046 -- the specs refer to this type.
9048 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
9049 Discr_Map : constant Elist_Id := New_Elmt_List;
9050 Loc : constant Source_Ptr := Sloc (N);
9051 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
9053 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
9054 -- This flag indicates whether the lock free implementation is active
9056 Pdef : constant Node_Id := Protected_Definition (N);
9057 -- This contains two lists; one for visible and one for private decls
9059 Current_Node : Node_Id := N;
9061 Entries_Aggr : Node_Id;
9063 procedure Check_Inlining (Subp : Entity_Id);
9064 -- If the original operation has a pragma Inline, propagate the flag
9065 -- to the internal body, for possible inlining later on. The source
9066 -- operation is invisible to the back-end and is never actually called.
9068 function Discriminated_Size (Comp : Entity_Id) return Boolean;
9069 -- If a component size is not static then a warning will be emitted
9070 -- in Ravenscar or other restricted contexts. When a component is non-
9071 -- static because of a discriminant constraint we can specialize the
9072 -- warning by mentioning discriminants explicitly.
9074 procedure Expand_Entry_Declaration (Decl : Node_Id);
9075 -- Create the entry barrier and the procedure body for entry declaration
9076 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
9078 function Static_Component_Size (Comp : Entity_Id) return Boolean;
9079 -- When compiling under the Ravenscar profile, private components must
9080 -- have a static size, or else a protected object will require heap
9081 -- allocation, violating the corresponding restriction. It is preferable
9082 -- to make this check here, because it provides a better error message
9083 -- than the back-end, which refers to the object as a whole.
9085 procedure Register_Handler;
9086 -- For a protected operation that is an interrupt handler, add the
9087 -- freeze action that will register it as such.
9089 --------------------
9090 -- Check_Inlining --
9091 --------------------
9093 procedure Check_Inlining (Subp : Entity_Id) is
9095 if Is_Inlined (Subp) then
9096 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
9097 Set_Is_Inlined (Subp, False);
9101 ------------------------
9102 -- Discriminated_Size --
9103 ------------------------
9105 function Discriminated_Size (Comp : Entity_Id) return Boolean is
9106 Typ : constant Entity_Id := Etype (Comp);
9109 function Non_Static_Bound (Bound : Node_Id) return Boolean;
9110 -- Check whether the bound of an index is non-static and does denote
9111 -- a discriminant, in which case any protected object of the type
9112 -- will have a non-static size.
9114 ----------------------
9115 -- Non_Static_Bound --
9116 ----------------------
9118 function Non_Static_Bound (Bound : Node_Id) return Boolean is
9120 if Is_OK_Static_Expression (Bound) then
9123 elsif Is_Entity_Name (Bound)
9124 and then Present (Discriminal_Link (Entity (Bound)))
9131 end Non_Static_Bound;
9133 -- Start of processing for Discriminated_Size
9136 if not Is_Array_Type (Typ) then
9140 if Ekind (Typ) = E_Array_Subtype then
9141 Index := First_Index (Typ);
9142 while Present (Index) loop
9143 if Non_Static_Bound (Low_Bound (Index))
9144 or else Non_Static_Bound (High_Bound (Index))
9156 end Discriminated_Size;
9158 ---------------------------
9159 -- Static_Component_Size --
9160 ---------------------------
9162 function Static_Component_Size (Comp : Entity_Id) return Boolean is
9163 Typ : constant Entity_Id := Etype (Comp);
9167 if Is_Scalar_Type (Typ) then
9170 elsif Is_Array_Type (Typ) then
9171 return Compile_Time_Known_Bounds (Typ);
9173 elsif Is_Record_Type (Typ) then
9174 C := First_Component (Typ);
9175 while Present (C) loop
9176 if not Static_Component_Size (C) then
9185 -- Any other type will be checked by the back-end
9190 end Static_Component_Size;
9192 ------------------------------
9193 -- Expand_Entry_Declaration --
9194 ------------------------------
9196 procedure Expand_Entry_Declaration (Decl : Node_Id) is
9197 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
9203 E_Count := E_Count + 1;
9205 -- Create the protected body subprogram
9208 Make_Defining_Identifier (Loc,
9209 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
9210 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
9213 Make_Subprogram_Declaration (Loc,
9215 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
9217 Insert_After (Current_Node, Subp);
9218 Current_Node := Subp;
9222 -- Build a wrapper procedure to handle contract cases, preconditions,
9223 -- and postconditions.
9225 Build_Contract_Wrapper (Ent_Id, N);
9227 -- Create the barrier function
9230 Make_Defining_Identifier (Loc,
9231 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
9232 Set_Barrier_Function (Ent_Id, Bar_Id);
9235 Make_Subprogram_Declaration (Loc,
9237 Build_Barrier_Function_Specification (Loc, Bar_Id));
9238 Set_Is_Entry_Barrier_Function (Subp);
9240 Insert_After (Current_Node, Subp);
9241 Current_Node := Subp;
9245 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
9246 Set_Scope (Bar_Id, Scope (Ent_Id));
9248 -- Collect pointers to the protected subprogram and the barrier
9249 -- of the current entry, for insertion into Entry_Bodies_Array.
9251 Append_To (Expressions (Entries_Aggr),
9252 Make_Aggregate (Loc,
9253 Expressions => New_List (
9254 Make_Attribute_Reference (Loc,
9255 Prefix => New_Occurrence_Of (Bar_Id, Loc),
9256 Attribute_Name => Name_Unrestricted_Access),
9257 Make_Attribute_Reference (Loc,
9258 Prefix => New_Occurrence_Of (Bod_Id, Loc),
9259 Attribute_Name => Name_Unrestricted_Access))));
9260 end Expand_Entry_Declaration;
9262 ----------------------
9263 -- Register_Handler --
9264 ----------------------
9266 procedure Register_Handler is
9268 -- All semantic checks already done in Sem_Prag
9270 Prot_Proc : constant Entity_Id :=
9271 Defining_Unit_Name (Specification (Current_Node));
9273 Proc_Address : constant Node_Id :=
9274 Make_Attribute_Reference (Loc,
9276 New_Occurrence_Of (Prot_Proc, Loc),
9277 Attribute_Name => Name_Address);
9279 RTS_Call : constant Entity_Id :=
9280 Make_Procedure_Call_Statement (Loc,
9283 (RTE (RE_Register_Interrupt_Handler), Loc),
9284 Parameter_Associations => New_List (Proc_Address));
9286 Append_Freeze_Action (Prot_Proc, RTS_Call);
9287 end Register_Handler;
9292 Body_Id : Entity_Id;
9298 Object_Comp : Node_Id;
9303 -- Start of processing for Expand_N_Protected_Type_Declaration
9306 if Present (Corresponding_Record_Type (Prot_Typ)) then
9309 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
9312 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
9314 Qualify_Entity_Names (N);
9316 -- If the type has discriminants, their occurrences in the declaration
9317 -- have been replaced by the corresponding discriminals. For components
9318 -- that are constrained by discriminants, their homologues in the
9319 -- corresponding record type must refer to the discriminants of that
9320 -- record, so we must apply a new renaming to subtypes_indications:
9322 -- protected discriminant => discriminal => record discriminant
9324 -- This replacement is not applied to default expressions, for which
9325 -- the discriminal is correct.
9327 if Has_Discriminants (Prot_Typ) then
9333 Disc := First_Discriminant (Prot_Typ);
9334 Decl := First (Discriminant_Specifications (Rec_Decl));
9335 while Present (Disc) loop
9336 Append_Elmt (Discriminal (Disc), Discr_Map);
9337 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
9338 Next_Discriminant (Disc);
9344 -- Fill in the component declarations
9346 -- Add components for entry families. For each entry family, create an
9347 -- anonymous type declaration with the same size, and analyze the type.
9349 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9351 pragma Assert (Present (Pdef));
9353 Insert_After (Current_Node, Rec_Decl);
9354 Current_Node := Rec_Decl;
9356 -- Add private field components
9358 if Present (Private_Declarations (Pdef)) then
9359 Priv := First (Private_Declarations (Pdef));
9360 while Present (Priv) loop
9361 if Nkind (Priv) = N_Component_Declaration then
9362 if not Static_Component_Size (Defining_Identifier (Priv)) then
9364 -- When compiling for a restricted profile, the private
9365 -- components must have a static size. If not, this is an
9366 -- error for a single protected declaration, and rates a
9367 -- warning on a protected type declaration.
9369 if not Comes_From_Source (Prot_Typ) then
9371 -- It's ok to be checking this restriction at expansion
9372 -- time, because this is only for the restricted profile,
9373 -- which is not subject to strict RM conformance, so it
9374 -- is OK to miss this check in -gnatc mode.
9376 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9378 (No_Implicit_Protected_Object_Allocations, Priv);
9380 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9381 if not Discriminated_Size (Defining_Identifier (Priv))
9383 -- Any object of the type will be non-static.
9385 Error_Msg_N ("component has non-static size??", Priv);
9387 ("\creation of protected object of type& will "
9388 & "violate restriction "
9389 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9392 -- Object will be non-static if discriminants are.
9395 ("creation of protected object of type& with "
9396 & "non-static discriminants will violate"
9397 & " restriction No_Implicit_Heap_Allocations??",
9401 -- Likewise for No_Implicit_Protected_Object_Allocations
9403 elsif Restriction_Active
9404 (No_Implicit_Protected_Object_Allocations)
9406 if not Discriminated_Size (Defining_Identifier (Priv))
9408 -- Any object of the type will be non-static.
9410 Error_Msg_N ("component has non-static size??", Priv);
9412 ("\creation of protected object of type& will "
9413 & "violate restriction "
9414 & "No_Implicit_Protected_Object_Allocations??",
9417 -- Object will be non-static if discriminants are.
9420 ("creation of protected object of type& with "
9421 & "non-static discriminants will violate "
9423 & "No_Implicit_Protected_Object_Allocations??",
9429 -- The component definition consists of a subtype indication,
9430 -- or (in Ada 2005) an access definition. Make a copy of the
9431 -- proper definition.
9434 Old_Comp : constant Node_Id := Component_Definition (Priv);
9435 Oent : constant Entity_Id := Defining_Identifier (Priv);
9436 Nent : constant Entity_Id :=
9437 Make_Defining_Identifier (Sloc (Oent),
9438 Chars => Chars (Oent));
9442 if Present (Subtype_Indication (Old_Comp)) then
9444 Make_Component_Definition (Sloc (Oent),
9445 Aliased_Present => False,
9446 Subtype_Indication =>
9448 (Subtype_Indication (Old_Comp), Discr_Map));
9451 Make_Component_Definition (Sloc (Oent),
9452 Aliased_Present => False,
9453 Access_Definition =>
9455 (Access_Definition (Old_Comp), Discr_Map));
9459 Make_Component_Declaration (Loc,
9460 Defining_Identifier => Nent,
9461 Component_Definition => New_Comp,
9462 Expression => Expression (Priv));
9464 Set_Has_Per_Object_Constraint (Nent,
9465 Has_Per_Object_Constraint (Oent));
9467 Append_To (Cdecls, New_Priv);
9470 elsif Nkind (Priv) = N_Subprogram_Declaration then
9472 -- Make the unprotected version of the subprogram available
9473 -- for expansion of intra object calls. There is need for
9474 -- a protected version only if the subprogram is an interrupt
9475 -- handler, otherwise this operation can only be called from
9479 Make_Subprogram_Declaration (Loc,
9481 Build_Protected_Sub_Specification
9482 (Priv, Prot_Typ, Unprotected_Mode));
9484 Insert_After (Current_Node, Sub);
9487 Set_Protected_Body_Subprogram
9488 (Defining_Unit_Name (Specification (Priv)),
9489 Defining_Unit_Name (Specification (Sub)));
9490 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9491 Current_Node := Sub;
9494 Make_Subprogram_Declaration (Loc,
9496 Build_Protected_Sub_Specification
9497 (Priv, Prot_Typ, Protected_Mode));
9499 Insert_After (Current_Node, Sub);
9501 Current_Node := Sub;
9503 if Is_Interrupt_Handler
9504 (Defining_Unit_Name (Specification (Priv)))
9506 if not Restricted_Profile then
9516 -- Except for the lock-free implementation, append the _Object field
9517 -- with the right type to the component list. We need to compute the
9518 -- number of entries, and in some cases the number of Attach_Handler
9521 if not Lock_Free_Active then
9523 Entry_Count_Expr : constant Node_Id :=
9524 Build_Entry_Count_Expression
9525 (Prot_Typ, Cdecls, Loc);
9526 Num_Attach_Handler : Nat := 0;
9527 Protection_Subtype : Node_Id;
9531 if Has_Attach_Handler (Prot_Typ) then
9532 Ritem := First_Rep_Item (Prot_Typ);
9533 while Present (Ritem) loop
9534 if Nkind (Ritem) = N_Pragma
9535 and then Pragma_Name (Ritem) = Name_Attach_Handler
9537 Num_Attach_Handler := Num_Attach_Handler + 1;
9540 Next_Rep_Item (Ritem);
9544 -- Determine the proper protection type. There are two special
9545 -- cases: 1) when the protected type has dynamic interrupt
9546 -- handlers, and 2) when it has static handlers and we use a
9547 -- restricted profile.
9549 if Has_Attach_Handler (Prot_Typ)
9550 and then not Restricted_Profile
9552 Protection_Subtype :=
9553 Make_Subtype_Indication (Loc,
9556 (RTE (RE_Static_Interrupt_Protection), Loc),
9558 Make_Index_Or_Discriminant_Constraint (Loc,
9559 Constraints => New_List (
9561 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9563 elsif Has_Interrupt_Handler (Prot_Typ)
9564 and then not Restriction_Active (No_Dynamic_Attachment)
9566 Protection_Subtype :=
9567 Make_Subtype_Indication (Loc,
9570 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9572 Make_Index_Or_Discriminant_Constraint (Loc,
9573 Constraints => New_List (Entry_Count_Expr)));
9576 case Corresponding_Runtime_Package (Prot_Typ) is
9577 when System_Tasking_Protected_Objects_Entries =>
9578 Protection_Subtype :=
9579 Make_Subtype_Indication (Loc,
9582 (RTE (RE_Protection_Entries), Loc),
9584 Make_Index_Or_Discriminant_Constraint (Loc,
9585 Constraints => New_List (Entry_Count_Expr)));
9587 when System_Tasking_Protected_Objects_Single_Entry =>
9588 Protection_Subtype :=
9589 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9591 when System_Tasking_Protected_Objects =>
9592 Protection_Subtype :=
9593 New_Occurrence_Of (RTE (RE_Protection), Loc);
9596 raise Program_Error;
9601 Make_Component_Declaration (Loc,
9602 Defining_Identifier =>
9603 Make_Defining_Identifier (Loc, Name_uObject),
9604 Component_Definition =>
9605 Make_Component_Definition (Loc,
9606 Aliased_Present => True,
9607 Subtype_Indication => Protection_Subtype));
9610 -- Put the _Object component after the private component so that it
9611 -- be finalized early as required by 9.4 (20)
9613 Append_To (Cdecls, Object_Comp);
9616 -- Analyze the record declaration immediately after construction,
9617 -- because the initialization procedure is needed for single object
9618 -- declarations before the next entity is analyzed (the freeze call
9619 -- that generates this initialization procedure is found below).
9621 Analyze (Rec_Decl, Suppress => All_Checks);
9623 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9624 -- the corresponding record is frozen. If any wrappers are generated,
9625 -- Current_Node is updated accordingly.
9627 if Ada_Version >= Ada_2005 then
9628 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9631 -- Collect pointers to entry bodies and their barriers, to be placed
9632 -- in the Entry_Bodies_Array for the type. For each entry/family we
9633 -- add an expression to the aggregate which is the initial value of
9634 -- this array. The array is declared after all protected subprograms.
9636 if Has_Entries (Prot_Typ) then
9637 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9639 Entries_Aggr := Empty;
9642 -- Build two new procedure specifications for each protected subprogram;
9643 -- one to call from outside the object and one to call from inside.
9644 -- Build a barrier function and an entry body action procedure
9645 -- specification for each protected entry. Initialize the entry body
9646 -- array. If subprogram is flagged as eliminated, do not generate any
9647 -- internal operations.
9650 Comp := First (Visible_Declarations (Pdef));
9651 while Present (Comp) loop
9652 if Nkind (Comp) = N_Subprogram_Declaration then
9654 Make_Subprogram_Declaration (Loc,
9656 Build_Protected_Sub_Specification
9657 (Comp, Prot_Typ, Unprotected_Mode));
9659 Insert_After (Current_Node, Sub);
9662 Set_Protected_Body_Subprogram
9663 (Defining_Unit_Name (Specification (Comp)),
9664 Defining_Unit_Name (Specification (Sub)));
9665 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9667 -- Make the protected version of the subprogram available for
9668 -- expansion of external calls.
9670 Current_Node := Sub;
9673 Make_Subprogram_Declaration (Loc,
9675 Build_Protected_Sub_Specification
9676 (Comp, Prot_Typ, Protected_Mode));
9678 Insert_After (Current_Node, Sub);
9681 Current_Node := Sub;
9683 -- Generate an overriding primitive operation specification for
9684 -- this subprogram if the protected type implements an interface
9685 -- and Build_Wrapper_Spec did not generate its wrapper.
9687 if Ada_Version >= Ada_2005
9689 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9692 Found : Boolean := False;
9693 Prim_Elmt : Elmt_Id;
9699 (Primitive_Operations
9700 (Corresponding_Record_Type (Prot_Typ)));
9702 while Present (Prim_Elmt) loop
9703 Prim_Op := Node (Prim_Elmt);
9705 if Is_Primitive_Wrapper (Prim_Op)
9706 and then Wrapped_Entity (Prim_Op) =
9707 Defining_Entity (Specification (Comp))
9713 Next_Elmt (Prim_Elmt);
9718 Make_Subprogram_Declaration (Loc,
9720 Build_Protected_Sub_Specification
9721 (Comp, Prot_Typ, Dispatching_Mode));
9723 Insert_After (Current_Node, Sub);
9726 Current_Node := Sub;
9731 -- If a pragma Interrupt_Handler applies, build and add a call to
9732 -- Register_Interrupt_Handler to the freezing actions of the
9733 -- protected version (Current_Node) of the subprogram:
9735 -- system.interrupts.register_interrupt_handler
9736 -- (prot_procP'address);
9738 if not Restricted_Profile
9739 and then Is_Interrupt_Handler
9740 (Defining_Unit_Name (Specification (Comp)))
9745 elsif Nkind (Comp) = N_Entry_Declaration then
9746 Expand_Entry_Declaration (Comp);
9752 -- If there are some private entry declarations, expand it as if they
9753 -- were visible entries.
9755 if Present (Private_Declarations (Pdef)) then
9756 Comp := First (Private_Declarations (Pdef));
9757 while Present (Comp) loop
9758 if Nkind (Comp) = N_Entry_Declaration then
9759 Expand_Entry_Declaration (Comp);
9766 -- Create the declaration of an array object which contains the values
9767 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9768 -- type. This object is later passed to the appropriate protected object
9769 -- initialization routine.
9771 if Has_Entries (Prot_Typ)
9772 and then Corresponding_Runtime_Package (Prot_Typ) =
9773 System_Tasking_Protected_Objects_Entries
9780 Maxes_Id : Entity_Id;
9781 Need_Array : Boolean := False;
9784 -- First check if there is any Max_Queue_Length pragma
9786 Item := First_Entity (Prot_Typ);
9787 while Present (Item) loop
9788 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9796 -- Gather the Max_Queue_Length values of all entries in a list. A
9797 -- value of zero indicates that the entry has no limitation on its
9802 Item := First_Entity (Prot_Typ);
9804 while Present (Item) loop
9805 if Is_Entry (Item) then
9808 Make_Integer_Literal
9809 (Loc, Get_Max_Queue_Length (Item)));
9815 -- Create the declaration of the array object. Generate:
9817 -- Maxes_Id : aliased Protected_Entry_Queue_Max_Array
9818 -- (1 .. Count) := (..., ...);
9821 Make_Defining_Identifier (Loc,
9822 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9825 Make_Object_Declaration (Loc,
9826 Defining_Identifier => Maxes_Id,
9827 Aliased_Present => True,
9828 Constant_Present => True,
9829 Object_Definition =>
9830 Make_Subtype_Indication (Loc,
9833 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9835 Make_Index_Or_Discriminant_Constraint (Loc,
9836 Constraints => New_List (
9838 Make_Integer_Literal (Loc, 1),
9839 Make_Integer_Literal (Loc, Count))))),
9840 Expression => Make_Aggregate (Loc, Maxes));
9842 -- A pointer to this array will be placed in the corresponding
9843 -- record by its initialization procedure so this needs to be
9846 Insert_After (Current_Node, Max_Vals);
9847 Current_Node := Max_Vals;
9850 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9855 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9856 -- all protected subprograms have been collected.
9858 if Has_Entries (Prot_Typ) then
9860 Make_Defining_Identifier (Sloc (Prot_Typ),
9861 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9863 case Corresponding_Runtime_Package (Prot_Typ) is
9864 when System_Tasking_Protected_Objects_Entries =>
9865 Expr := Entries_Aggr;
9867 Make_Subtype_Indication (Loc,
9870 (RTE (RE_Protected_Entry_Body_Array), Loc),
9872 Make_Index_Or_Discriminant_Constraint (Loc,
9873 Constraints => New_List (
9875 Make_Integer_Literal (Loc, 1),
9876 Make_Integer_Literal (Loc, E_Count)))));
9878 when System_Tasking_Protected_Objects_Single_Entry =>
9879 Expr := Remove_Head (Expressions (Entries_Aggr));
9880 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9883 raise Program_Error;
9887 Make_Object_Declaration (Loc,
9888 Defining_Identifier => Body_Id,
9889 Aliased_Present => True,
9890 Object_Definition => Obj_Def,
9892 Constant_Present => True);
9894 -- A pointer to this array will be placed in the corresponding record
9895 -- by its initialization procedure so this needs to be analyzed here.
9897 Insert_After (Current_Node, Body_Arr);
9898 Current_Node := Body_Arr;
9901 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9903 -- Finally, build the function that maps an entry index into the
9904 -- corresponding body. A pointer to this function is placed in each
9905 -- object of the type. Except for a ravenscar-like profile (no abort,
9906 -- no entry queue, 1 entry)
9908 if Corresponding_Runtime_Package (Prot_Typ) =
9909 System_Tasking_Protected_Objects_Entries
9912 Make_Subprogram_Declaration (Loc,
9913 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9915 Insert_After (Current_Node, Sub);
9919 end Expand_N_Protected_Type_Declaration;
9921 --------------------------------
9922 -- Expand_N_Requeue_Statement --
9923 --------------------------------
9925 -- A nondispatching requeue statement is expanded into one of four GNARLI
9926 -- operations, depending on the source and destination (task or protected
9927 -- object). A dispatching requeue statement is expanded into a call to the
9928 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9929 -- jump around the remainder of processing for the original entry and, if
9930 -- the destination is (different) protected object, to attempt to service
9931 -- it. The following illustrates the various cases:
9934 -- (O : System.Address;
9935 -- P : System.Address;
9936 -- E : Protected_Entry_Index)
9938 -- <discriminant renamings>
9939 -- <private object renamings>
9940 -- type poVP is access poV;
9941 -- _object : ptVP := ptVP!(O);
9945 -- <start of statement sequence for entry>
9947 -- -- Requeue from one protected entry body to another protected
9950 -- Requeue_Protected_Entry (
9951 -- _object._object'Access,
9952 -- new._object'Access,
9957 -- <some more of the statement sequence for entry>
9959 -- -- Requeue from an entry body to a task entry
9961 -- Requeue_Protected_To_Task_Entry (
9967 -- <rest of statement sequence for entry>
9968 -- Complete_Entry_Body (_object._object);
9971 -- when all others =>
9972 -- Exceptional_Complete_Entry_Body (
9973 -- _object._object, Get_GNAT_Exception);
9977 -- Requeue of a task entry call to a task entry
9979 -- Accept_Call (E, Ann);
9980 -- <start of statement sequence for accept statement>
9981 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9983 -- <rest of statement sequence for accept statement>
9985 -- Complete_Rendezvous;
9988 -- when all others =>
9989 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9991 -- Requeue of a task entry call to a protected entry
9993 -- Accept_Call (E, Ann);
9994 -- <start of statement sequence for accept statement>
9995 -- Requeue_Task_To_Protected_Entry (
9996 -- new._object'Access,
10001 -- <rest of statement sequence for accept statement>
10003 -- Complete_Rendezvous;
10006 -- when all others =>
10007 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
10009 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10010 -- marked by pragma Implemented (XXX, By_Entry).
10012 -- The requeue is inside a protected entry:
10015 -- (O : System.Address;
10016 -- P : System.Address;
10017 -- E : Protected_Entry_Index)
10019 -- <discriminant renamings>
10020 -- <private object renamings>
10021 -- type poVP is access poV;
10022 -- _object : ptVP := ptVP!(O);
10026 -- <start of statement sequence for entry>
10029 -- (<interface class-wide object>,
10031 -- _object'Address,
10032 -- Ada.Tags.Get_Offset_Index
10034 -- <interface dispatch table index of target entry>),
10038 -- <rest of statement sequence for entry>
10039 -- Complete_Entry_Body (_object._object);
10042 -- when all others =>
10043 -- Exceptional_Complete_Entry_Body (
10044 -- _object._object, Get_GNAT_Exception);
10048 -- The requeue is inside a task entry:
10050 -- Accept_Call (E, Ann);
10051 -- <start of statement sequence for accept statement>
10053 -- (<interface class-wide object>,
10056 -- Ada.Tags.Get_Offset_Index
10058 -- <interface dispatch table index of target entrt>),
10060 -- newS (new, Pnn);
10062 -- <rest of statement sequence for accept statement>
10064 -- Complete_Rendezvous;
10067 -- when all others =>
10068 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
10070 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10071 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
10072 -- statement is replaced by a dispatching call with actual parameters taken
10073 -- from the inner-most accept statement or entry body.
10075 -- Target.Primitive (Param1, ..., ParamN);
10077 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
10078 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
10082 -- S : constant Offset_Index :=
10083 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
10084 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
10087 -- if C = POK_Protected_Entry
10088 -- or else C = POK_Task_Entry
10090 -- <statements for dispatching requeue>
10092 -- elsif C = POK_Protected_Procedure then
10093 -- <dispatching call equivalent>
10096 -- raise Program_Error;
10100 procedure Expand_N_Requeue_Statement (N : Node_Id) is
10101 Loc : constant Source_Ptr := Sloc (N);
10102 Conc_Typ : Entity_Id;
10106 Old_Typ : Entity_Id;
10108 function Build_Dispatching_Call_Equivalent return Node_Id;
10109 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10110 -- the form Concval.Ename. It is statically known that Ename is allowed
10111 -- to be implemented by a protected procedure. Create a dispatching call
10112 -- equivalent of Concval.Ename taking the actual parameters from the
10113 -- inner-most accept statement or entry body.
10115 function Build_Dispatching_Requeue return Node_Id;
10116 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10117 -- the form Concval.Ename. It is statically known that Ename is allowed
10118 -- to be implemented by a protected or a task entry. Create a call to
10119 -- primitive _Disp_Requeue which handles the low-level actions.
10121 function Build_Dispatching_Requeue_To_Any return Node_Id;
10122 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
10123 -- the form Concval.Ename. Ename is either marked by pragma Implemented
10124 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
10125 -- determines at runtime whether Ename denotes an entry or a procedure
10126 -- and perform the appropriate kind of dispatching select.
10128 function Build_Normal_Requeue return Node_Id;
10129 -- N denotes a nondispatching requeue statement to either a task or a
10130 -- protected entry. Build the appropriate runtime call to perform the
10133 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
10134 -- For a protected entry, create a return statement to skip the rest of
10135 -- the entry body. Otherwise, create a goto statement to skip the rest
10136 -- of a task accept statement. The lookup for the enclosing entry body
10137 -- or accept statement starts from Search.
10139 ---------------------------------------
10140 -- Build_Dispatching_Call_Equivalent --
10141 ---------------------------------------
10143 function Build_Dispatching_Call_Equivalent return Node_Id is
10144 Call_Ent : constant Entity_Id := Entity (Ename);
10145 Obj : constant Node_Id := Original_Node (Concval);
10152 -- Climb the parent chain looking for the inner-most entry body or
10153 -- accept statement.
10156 while Present (Acc_Ent)
10157 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
10160 Acc_Ent := Parent (Acc_Ent);
10163 -- A requeue statement should be housed inside an entry body or an
10164 -- accept statement at some level. If this is not the case, then the
10165 -- tree is malformed.
10167 pragma Assert (Present (Acc_Ent));
10169 -- Recover the list of formal parameters
10171 if Nkind (Acc_Ent) = N_Entry_Body then
10172 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
10175 Formals := Parameter_Specifications (Acc_Ent);
10177 -- Create the actual parameters for the dispatching call. These are
10178 -- simply copies of the entry body or accept statement formals in the
10179 -- same order as they appear.
10181 Actuals := No_List;
10183 if Present (Formals) then
10184 Actuals := New_List;
10185 Formal := First (Formals);
10186 while Present (Formal) loop
10187 Append_To (Actuals,
10188 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
10194 -- Obj.Call_Ent (Actuals);
10197 Make_Procedure_Call_Statement (Loc,
10199 Make_Selected_Component (Loc,
10200 Prefix => Make_Identifier (Loc, Chars (Obj)),
10201 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
10203 Parameter_Associations => Actuals);
10204 end Build_Dispatching_Call_Equivalent;
10206 -------------------------------
10207 -- Build_Dispatching_Requeue --
10208 -------------------------------
10210 function Build_Dispatching_Requeue return Node_Id is
10211 Params : constant List_Id := New_List;
10214 -- Process the "with abort" parameter
10216 Prepend_To (Params,
10217 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10219 -- Process the entry wrapper's position in the primary dispatch
10220 -- table parameter. Generate:
10222 -- Ada.Tags.Get_Entry_Index
10223 -- (T => To_Tag_Ptr (Obj'Address).all,
10225 -- Ada.Tags.Get_Offset_Index
10226 -- (Ada.Tags.Tag (Concval),
10227 -- <interface dispatch table position of Ename>));
10229 -- Note that Obj'Address is recursively expanded into a call to
10230 -- Base_Address (Obj).
10232 if Tagged_Type_Expansion then
10233 Prepend_To (Params,
10234 Make_Function_Call (Loc,
10235 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
10236 Parameter_Associations => New_List (
10238 Make_Explicit_Dereference (Loc,
10239 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
10240 Make_Attribute_Reference (Loc,
10241 Prefix => New_Copy_Tree (Concval),
10242 Attribute_Name => Name_Address))),
10244 Make_Function_Call (Loc,
10245 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
10246 Parameter_Associations => New_List (
10247 Unchecked_Convert_To (RTE (RE_Tag), Concval),
10248 Make_Integer_Literal (Loc,
10249 DT_Position (Entity (Ename))))))));
10254 Prepend_To (Params,
10255 Make_Function_Call (Loc,
10256 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
10257 Parameter_Associations => New_List (
10259 Make_Attribute_Reference (Loc,
10261 Attribute_Name => Name_Tag),
10263 Make_Function_Call (Loc,
10264 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
10266 Parameter_Associations => New_List (
10270 Make_Attribute_Reference (Loc,
10272 Attribute_Name => Name_Tag),
10276 Make_Attribute_Reference (Loc,
10277 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
10278 Attribute_Name => Name_Tag),
10282 Make_Integer_Literal (Loc,
10283 DT_Position (Entity (Ename))))))));
10286 -- Specific actuals for protected to XXX requeue
10288 if Is_Protected_Type (Old_Typ) then
10289 Prepend_To (Params,
10290 Make_Attribute_Reference (Loc, -- _object'Address
10292 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10293 Attribute_Name => Name_Address));
10295 Prepend_To (Params, -- True
10296 New_Occurrence_Of (Standard_True, Loc));
10298 -- Specific actuals for task to XXX requeue
10301 pragma Assert (Is_Task_Type (Old_Typ));
10303 Prepend_To (Params, -- null
10304 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
10306 Prepend_To (Params, -- False
10307 New_Occurrence_Of (Standard_False, Loc));
10310 -- Add the object parameter
10312 Prepend_To (Params, New_Copy_Tree (Concval));
10315 -- _Disp_Requeue (<Params>);
10317 -- Find entity for Disp_Requeue operation, which belongs to
10318 -- the type and may not be directly visible.
10325 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
10326 while Present (Elmt) loop
10328 exit when Chars (Op) = Name_uDisp_Requeue;
10333 Make_Procedure_Call_Statement (Loc,
10334 Name => New_Occurrence_Of (Op, Loc),
10335 Parameter_Associations => Params);
10337 end Build_Dispatching_Requeue;
10339 --------------------------------------
10340 -- Build_Dispatching_Requeue_To_Any --
10341 --------------------------------------
10343 function Build_Dispatching_Requeue_To_Any return Node_Id is
10344 Call_Ent : constant Entity_Id := Entity (Ename);
10345 Obj : constant Node_Id := Original_Node (Concval);
10346 Skip : constant Node_Id := Build_Skip_Statement (N);
10356 -- Dispatch table slot processing, generate:
10359 S := Build_S (Loc, Decls);
10361 -- Call kind processing, generate:
10362 -- C : Ada.Tags.Prim_Op_Kind;
10364 C := Build_C (Loc, Decls);
10367 -- S := Ada.Tags.Get_Offset_Index
10368 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10370 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10373 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10376 Make_Procedure_Call_Statement (Loc,
10378 New_Occurrence_Of (
10379 Find_Prim_Op (Etype (Etype (Obj)),
10380 Name_uDisp_Get_Prim_Op_Kind),
10382 Parameter_Associations => New_List (
10383 New_Copy_Tree (Obj),
10384 New_Occurrence_Of (S, Loc),
10385 New_Occurrence_Of (C, Loc))));
10389 -- if C = POK_Protected_Entry
10390 -- or else C = POK_Task_Entry
10393 Make_Implicit_If_Statement (N,
10399 New_Occurrence_Of (C, Loc),
10401 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10406 New_Occurrence_Of (C, Loc),
10408 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10410 -- Dispatching requeue equivalent
10412 Then_Statements => New_List (
10413 Build_Dispatching_Requeue,
10416 -- elsif C = POK_Protected_Procedure then
10418 Elsif_Parts => New_List (
10419 Make_Elsif_Part (Loc,
10423 New_Occurrence_Of (C, Loc),
10425 New_Occurrence_Of (
10426 RTE (RE_POK_Protected_Procedure), Loc)),
10428 -- Dispatching call equivalent
10430 Then_Statements => New_List (
10431 Build_Dispatching_Call_Equivalent))),
10434 -- raise Program_Error;
10437 Else_Statements => New_List (
10438 Make_Raise_Program_Error (Loc,
10439 Reason => PE_Explicit_Raise))));
10441 -- Wrap everything into a block
10444 Make_Block_Statement (Loc,
10445 Declarations => Decls,
10446 Handled_Statement_Sequence =>
10447 Make_Handled_Sequence_Of_Statements (Loc,
10448 Statements => Stmts));
10449 end Build_Dispatching_Requeue_To_Any;
10451 --------------------------
10452 -- Build_Normal_Requeue --
10453 --------------------------
10455 function Build_Normal_Requeue return Node_Id is
10456 Params : constant List_Id := New_List;
10461 -- Process the "with abort" parameter
10463 Prepend_To (Params,
10464 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10466 -- Add the index expression to the parameters. It is common among all
10469 Prepend_To (Params,
10470 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10472 if Is_Protected_Type (Old_Typ) then
10474 Self_Param : Node_Id;
10478 Make_Attribute_Reference (Loc,
10480 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10482 Name_Unchecked_Access);
10484 -- Protected to protected requeue
10486 if Is_Protected_Type (Conc_Typ) then
10488 New_Occurrence_Of (
10489 RTE (RE_Requeue_Protected_Entry), Loc);
10492 Make_Attribute_Reference (Loc,
10494 Concurrent_Ref (Concval),
10496 Name_Unchecked_Access);
10498 -- Protected to task requeue
10500 else pragma Assert (Is_Task_Type (Conc_Typ));
10502 New_Occurrence_Of (
10503 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10505 Param := Concurrent_Ref (Concval);
10508 Prepend_To (Params, Param);
10509 Prepend_To (Params, Self_Param);
10512 else pragma Assert (Is_Task_Type (Old_Typ));
10514 -- Task to protected requeue
10516 if Is_Protected_Type (Conc_Typ) then
10518 New_Occurrence_Of (
10519 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10522 Make_Attribute_Reference (Loc,
10524 Concurrent_Ref (Concval),
10526 Name_Unchecked_Access);
10528 -- Task to task requeue
10530 else pragma Assert (Is_Task_Type (Conc_Typ));
10532 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10534 Param := Concurrent_Ref (Concval);
10537 Prepend_To (Params, Param);
10541 Make_Procedure_Call_Statement (Loc,
10543 Parameter_Associations => Params);
10544 end Build_Normal_Requeue;
10546 --------------------------
10547 -- Build_Skip_Statement --
10548 --------------------------
10550 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10551 Skip_Stmt : Node_Id;
10554 -- Build a return statement to skip the rest of the entire body
10556 if Is_Protected_Type (Old_Typ) then
10557 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10559 -- If the requeue is within a task, find the end label of the
10560 -- enclosing accept statement and create a goto statement to it.
10568 -- Climb the parent chain looking for the enclosing accept
10571 Acc := Parent (Search);
10572 while Present (Acc)
10573 and then Nkind (Acc) /= N_Accept_Statement
10575 Acc := Parent (Acc);
10578 -- The last statement is the second label used for completing
10579 -- the rendezvous the usual way. The label we are looking for
10580 -- is right before it.
10583 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10585 pragma Assert (Nkind (Label) = N_Label);
10587 -- Generate a goto statement to skip the rest of the accept
10590 Make_Goto_Statement (Loc,
10592 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10596 Set_Analyzed (Skip_Stmt);
10599 end Build_Skip_Statement;
10601 -- Start of processing for Expand_N_Requeue_Statement
10604 -- Extract the components of the entry call
10606 Extract_Entry (N, Concval, Ename, Index);
10607 Conc_Typ := Etype (Concval);
10609 -- If the prefix is an access to class-wide type, dereference to get
10610 -- object and entry type.
10612 if Is_Access_Type (Conc_Typ) then
10613 Conc_Typ := Designated_Type (Conc_Typ);
10615 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10616 Analyze_And_Resolve (Concval, Conc_Typ);
10619 -- Examine the scope stack in order to find nearest enclosing protected
10620 -- or task type. This will constitute our invocation source.
10622 Old_Typ := Current_Scope;
10623 while Present (Old_Typ)
10624 and then not Is_Protected_Type (Old_Typ)
10625 and then not Is_Task_Type (Old_Typ)
10627 Old_Typ := Scope (Old_Typ);
10630 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10631 -- Concval.Ename where the type of Concval is class-wide concurrent
10634 if Ada_Version >= Ada_2012
10635 and then Present (Concval)
10636 and then Is_Class_Wide_Type (Conc_Typ)
10637 and then Is_Concurrent_Interface (Conc_Typ)
10640 Has_Impl : Boolean := False;
10641 Impl_Kind : Name_Id := No_Name;
10644 -- Check whether the Ename is flagged by pragma Implemented
10646 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10648 Impl_Kind := Implementation_Kind (Entity (Ename));
10651 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10652 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10654 if Has_Impl and then Impl_Kind = Name_By_Entry then
10655 Rewrite (N, Build_Dispatching_Requeue);
10657 Insert_After (N, Build_Skip_Statement (N));
10659 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10660 -- a protected procedure. In this case the requeue is transformed
10661 -- into a dispatching call.
10664 and then Impl_Kind = Name_By_Protected_Procedure
10666 Rewrite (N, Build_Dispatching_Call_Equivalent);
10669 -- The procedure_or_entry_NAME's implementation kind is either
10670 -- By_Any, Optional, or pragma Implemented was not applied at all.
10671 -- In this case a runtime test determines whether Ename denotes an
10672 -- entry or a protected procedure and performs the appropriate
10676 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10681 -- Processing for regular (nondispatching) requeues
10684 Rewrite (N, Build_Normal_Requeue);
10686 Insert_After (N, Build_Skip_Statement (N));
10688 end Expand_N_Requeue_Statement;
10690 -------------------------------
10691 -- Expand_N_Selective_Accept --
10692 -------------------------------
10694 procedure Expand_N_Selective_Accept (N : Node_Id) is
10695 Loc : constant Source_Ptr := Sloc (N);
10696 Alts : constant List_Id := Select_Alternatives (N);
10698 -- Note: in the below declarations a lot of new lists are allocated
10699 -- unconditionally which may well not end up being used. That's not
10700 -- a good idea since it wastes space gratuitously ???
10702 Accept_Case : List_Id;
10703 Accept_List : constant List_Id := New_List;
10706 Alt_List : constant List_Id := New_List;
10707 Alt_Stats : List_Id;
10708 Ann : Entity_Id := Empty;
10710 Check_Guard : Boolean := True;
10712 Decls : constant List_Id := New_List;
10713 Stats : constant List_Id := New_List;
10714 Body_List : constant List_Id := New_List;
10715 Trailing_List : constant List_Id := New_List;
10718 Else_Present : Boolean := False;
10719 Terminate_Alt : Node_Id := Empty;
10720 Select_Mode : Node_Id;
10722 Delay_Case : List_Id;
10723 Delay_Count : Integer := 0;
10724 Delay_Val : Entity_Id;
10725 Delay_Index : Entity_Id;
10726 Delay_Min : Entity_Id;
10727 Delay_Num : Pos := 1;
10728 Delay_Alt_List : List_Id := New_List;
10729 Delay_List : constant List_Id := New_List;
10733 First_Delay : Boolean := True;
10734 Guard_Open : Entity_Id;
10740 Num_Accept : Nat := 0;
10742 Time_Type : Entity_Id;
10743 Select_Call : Node_Id;
10745 Qnam : constant Entity_Id :=
10746 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10748 Xnam : constant Entity_Id :=
10749 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10751 -----------------------
10752 -- Local subprograms --
10753 -----------------------
10755 function Accept_Or_Raise return List_Id;
10756 -- For the rare case where delay alternatives all have guards, and
10757 -- all of them are closed, it is still possible that there were open
10758 -- accept alternatives with no callers. We must reexamine the
10759 -- Accept_List, and execute a selective wait with no else if some
10760 -- accept is open. If none, we raise program_error.
10762 procedure Add_Accept (Alt : Node_Id);
10763 -- Process a single accept statement in a select alternative. Build
10764 -- procedure for body of accept, and add entry to dispatch table with
10765 -- expression for guard, in preparation for call to run time select.
10767 function Make_And_Declare_Label (Num : Int) return Node_Id;
10768 -- Manufacture a label using Num as a serial number and declare it.
10769 -- The declaration is appended to Decls. The label marks the trailing
10770 -- statements of an accept or delay alternative.
10772 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10773 -- Build call to Selective_Wait runtime routine
10775 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10776 -- Add code to compare value of delay with previous values, and
10777 -- generate case entry for trailing statements.
10779 procedure Process_Accept_Alternative
10783 -- Add code to call corresponding procedure, and branch to
10784 -- trailing statements, if any.
10786 ---------------------
10787 -- Accept_Or_Raise --
10788 ---------------------
10790 function Accept_Or_Raise return List_Id is
10793 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10796 -- We generate the following:
10798 -- for J in q'range loop
10799 -- if q(J).S /=null_task_entry then
10800 -- selective_wait (simple_mode,...);
10806 -- if no rendez_vous then
10807 -- raise program_error;
10810 -- Note that the code needs to know that the selector name
10811 -- in an Accept_Alternative is named S.
10813 Cond := Make_Op_Ne (Loc,
10815 Make_Selected_Component (Loc,
10817 Make_Indexed_Component (Loc,
10818 Prefix => New_Occurrence_Of (Qnam, Loc),
10819 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10820 Selector_Name => Make_Identifier (Loc, Name_S)),
10822 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10824 Stats := New_List (
10825 Make_Implicit_Loop_Statement (N,
10826 Iteration_Scheme =>
10827 Make_Iteration_Scheme (Loc,
10828 Loop_Parameter_Specification =>
10829 Make_Loop_Parameter_Specification (Loc,
10830 Defining_Identifier => J,
10831 Discrete_Subtype_Definition =>
10832 Make_Attribute_Reference (Loc,
10833 Prefix => New_Occurrence_Of (Qnam, Loc),
10834 Attribute_Name => Name_Range,
10835 Expressions => New_List (
10836 Make_Integer_Literal (Loc, 1))))),
10838 Statements => New_List (
10839 Make_Implicit_If_Statement (N,
10841 Then_Statements => New_List (
10843 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10844 Make_Exit_Statement (Loc))))));
10847 Make_Raise_Program_Error (Loc,
10848 Condition => Make_Op_Eq (Loc,
10849 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10851 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10852 Reason => PE_All_Guards_Closed));
10855 end Accept_Or_Raise;
10861 procedure Add_Accept (Alt : Node_Id) is
10862 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10863 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10864 Eloc : constant Source_Ptr := Sloc (Ename);
10865 Eent : constant Entity_Id := Entity (Ename);
10866 Index : constant Node_Id := Entry_Index (Acc_Stm);
10867 Null_Body : Node_Id;
10868 Proc_Body : Node_Id;
10869 PB_Ent : Entity_Id;
10875 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10878 if Present (Condition (Alt)) then
10880 Make_If_Expression (Eloc, New_List (
10882 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10883 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10886 Entry_Index_Expression
10887 (Eloc, Eent, Index, Scope (Eent));
10890 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10891 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10893 -- Always add call to Abort_Undefer when generating code, since
10894 -- this is what the runtime expects (abort deferred in
10895 -- Selective_Wait). In CodePeer mode this only confuses the
10896 -- analysis with unknown calls, so don't do it.
10898 if not CodePeer_Mode then
10899 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10901 (First (Statements (Handled_Statement_Sequence
10902 (Accept_Statement (Alt)))),
10908 Make_Defining_Identifier (Eloc,
10909 New_External_Name (Chars (Ename), 'A', Num_Accept));
10911 if Comes_From_Source (Alt) then
10912 Set_Debug_Info_Needed (PB_Ent);
10916 Make_Subprogram_Body (Eloc,
10918 Make_Procedure_Specification (Eloc,
10919 Defining_Unit_Name => PB_Ent),
10920 Declarations => Declarations (Acc_Stm),
10921 Handled_Statement_Sequence =>
10922 Build_Accept_Body (Accept_Statement (Alt)));
10924 -- During the analysis of the body of the accept statement, any
10925 -- zero cost exception handler records were collected in the
10926 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10927 -- This is where we move them to where they belong, namely the
10928 -- newly created procedure.
10930 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10931 Append (Proc_Body, Body_List);
10934 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10936 -- if accept statement has declarations, insert above, given that
10937 -- we are not creating a body for the accept.
10939 if Present (Declarations (Acc_Stm)) then
10940 Insert_Actions (N, Declarations (Acc_Stm));
10944 Append_To (Accept_List,
10945 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10947 Num_Accept := Num_Accept + 1;
10950 ----------------------------
10951 -- Make_And_Declare_Label --
10952 ----------------------------
10954 function Make_And_Declare_Label (Num : Int) return Node_Id is
10958 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10960 Make_Label (Loc, Lab_Id);
10963 Make_Implicit_Label_Declaration (Loc,
10964 Defining_Identifier =>
10965 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10966 Label_Construct => Lab));
10969 end Make_And_Declare_Label;
10971 ----------------------
10972 -- Make_Select_Call --
10973 ----------------------
10975 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10976 Params : constant List_Id := New_List;
10980 Make_Attribute_Reference (Loc,
10981 Prefix => New_Occurrence_Of (Qnam, Loc),
10982 Attribute_Name => Name_Unchecked_Access));
10983 Append_To (Params, Select_Mode);
10984 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10985 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10988 Make_Procedure_Call_Statement (Loc,
10989 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10990 Parameter_Associations => Params);
10991 end Make_Select_Call;
10993 --------------------------------
10994 -- Process_Accept_Alternative --
10995 --------------------------------
10997 procedure Process_Accept_Alternative
11002 Astmt : constant Node_Id := Accept_Statement (Alt);
11003 Alt_Stats : List_Id;
11006 Adjust_Condition (Condition (Alt));
11008 -- Accept with body
11010 if Present (Handled_Statement_Sequence (Astmt)) then
11013 Make_Procedure_Call_Statement (Sloc (Proc),
11016 (Defining_Unit_Name (Specification (Proc)),
11019 -- Accept with no body (followed by trailing statements)
11022 Alt_Stats := Empty_List;
11025 Ensure_Statement_Present (Sloc (Astmt), Alt);
11027 -- After the call, if any, branch to trailing statements, if any.
11028 -- We create a label for each, as well as the corresponding label
11031 if not Is_Empty_List (Statements (Alt)) then
11032 Lab := Make_And_Declare_Label (Index);
11033 Append (Lab, Trailing_List);
11034 Append_List (Statements (Alt), Trailing_List);
11035 Append_To (Trailing_List,
11036 Make_Goto_Statement (Loc,
11037 Name => New_Copy (Identifier (End_Lab))));
11043 Append_To (Alt_Stats,
11044 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
11046 Append_To (Alt_List,
11047 Make_Case_Statement_Alternative (Loc,
11048 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
11049 Statements => Alt_Stats));
11050 end Process_Accept_Alternative;
11052 -------------------------------
11053 -- Process_Delay_Alternative --
11054 -------------------------------
11056 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
11057 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
11059 Delay_Alt : List_Id;
11062 -- Deal with C/Fortran boolean as delay condition
11064 Adjust_Condition (Condition (Alt));
11066 -- Determine the smallest specified delay
11068 -- for each delay alternative generate:
11070 -- if guard-expression then
11071 -- Delay_Val := delay-expression;
11072 -- Guard_Open := True;
11073 -- if Delay_Val < Delay_Min then
11074 -- Delay_Min := Delay_Val;
11075 -- Delay_Index := Index;
11079 -- The enclosing if-statement is omitted if there is no guard
11081 if Delay_Count = 1 or else First_Delay then
11082 First_Delay := False;
11084 Delay_Alt := New_List (
11085 Make_Assignment_Statement (Loc,
11086 Name => New_Occurrence_Of (Delay_Min, Loc),
11087 Expression => Expression (Delay_Statement (Alt))));
11089 if Delay_Count > 1 then
11090 Append_To (Delay_Alt,
11091 Make_Assignment_Statement (Loc,
11092 Name => New_Occurrence_Of (Delay_Index, Loc),
11093 Expression => Make_Integer_Literal (Loc, Index)));
11097 Delay_Alt := New_List (
11098 Make_Assignment_Statement (Loc,
11099 Name => New_Occurrence_Of (Delay_Val, Loc),
11100 Expression => Expression (Delay_Statement (Alt))));
11102 if Time_Type = Standard_Duration then
11105 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
11106 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
11109 -- The scope of the time type must define a comparison
11110 -- operator. The scope itself may not be visible, so we
11111 -- construct a node with entity information to insure that
11112 -- semantic analysis can find the proper operator.
11115 Make_Function_Call (Loc,
11116 Name => Make_Selected_Component (Loc,
11118 New_Occurrence_Of (Scope (Time_Type), Loc),
11120 Make_Operator_Symbol (Loc,
11121 Chars => Name_Op_Lt,
11122 Strval => No_String)),
11123 Parameter_Associations =>
11125 New_Occurrence_Of (Delay_Val, Loc),
11126 New_Occurrence_Of (Delay_Min, Loc)));
11128 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
11131 Append_To (Delay_Alt,
11132 Make_Implicit_If_Statement (N,
11134 Then_Statements => New_List (
11135 Make_Assignment_Statement (Loc,
11136 Name => New_Occurrence_Of (Delay_Min, Loc),
11137 Expression => New_Occurrence_Of (Delay_Val, Loc)),
11139 Make_Assignment_Statement (Loc,
11140 Name => New_Occurrence_Of (Delay_Index, Loc),
11141 Expression => Make_Integer_Literal (Loc, Index)))));
11144 if Check_Guard then
11145 Append_To (Delay_Alt,
11146 Make_Assignment_Statement (Loc,
11147 Name => New_Occurrence_Of (Guard_Open, Loc),
11148 Expression => New_Occurrence_Of (Standard_True, Loc)));
11151 if Present (Condition (Alt)) then
11152 Delay_Alt := New_List (
11153 Make_Implicit_If_Statement (N,
11154 Condition => Condition (Alt),
11155 Then_Statements => Delay_Alt));
11158 Append_List (Delay_Alt, Delay_List);
11160 Ensure_Statement_Present (Dloc, Alt);
11162 -- If the delay alternative has a statement part, add choice to the
11163 -- case statements for delays.
11165 if not Is_Empty_List (Statements (Alt)) then
11167 if Delay_Count = 1 then
11168 Append_List (Statements (Alt), Delay_Alt_List);
11171 Append_To (Delay_Alt_List,
11172 Make_Case_Statement_Alternative (Loc,
11173 Discrete_Choices => New_List (
11174 Make_Integer_Literal (Loc, Index)),
11175 Statements => Statements (Alt)));
11178 elsif Delay_Count = 1 then
11180 -- If the single delay has no trailing statements, add a branch
11181 -- to the exit label to the selective wait.
11183 Delay_Alt_List := New_List (
11184 Make_Goto_Statement (Loc,
11185 Name => New_Copy (Identifier (End_Lab))));
11188 end Process_Delay_Alternative;
11190 -- Start of processing for Expand_N_Selective_Accept
11193 Process_Statements_For_Controlled_Objects (N);
11195 -- First insert some declarations before the select. The first is:
11199 -- This variable holds the parameters passed to the accept body. This
11200 -- declaration has already been inserted by the time we get here by
11201 -- a call to Expand_Accept_Declarations made from the semantics when
11202 -- processing the first accept statement contained in the select. We
11203 -- can find this entity as Accept_Address (E), where E is any of the
11204 -- entries references by contained accept statements.
11206 -- The first step is to scan the list of Selective_Accept_Statements
11207 -- to find this entity, and also count the number of accepts, and
11208 -- determine if terminated, delay or else is present:
11212 Alt := First (Alts);
11213 while Present (Alt) loop
11214 Process_Statements_For_Controlled_Objects (Alt);
11216 if Nkind (Alt) = N_Accept_Alternative then
11219 elsif Nkind (Alt) = N_Delay_Alternative then
11220 Delay_Count := Delay_Count + 1;
11222 -- If the delays are relative delays, the delay expressions have
11223 -- type Standard_Duration. Otherwise they must have some time type
11224 -- recognized by GNAT.
11226 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
11227 Time_Type := Standard_Duration;
11229 Time_Type := Etype (Expression (Delay_Statement (Alt)));
11231 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
11232 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
11237 "& is not a time type (RM 9.6(6))",
11238 Expression (Delay_Statement (Alt)), Time_Type);
11239 Time_Type := Standard_Duration;
11240 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
11244 if No (Condition (Alt)) then
11246 -- This guard will always be open
11248 Check_Guard := False;
11251 elsif Nkind (Alt) = N_Terminate_Alternative then
11252 Adjust_Condition (Condition (Alt));
11253 Terminate_Alt := Alt;
11256 Num_Alts := Num_Alts + 1;
11260 Else_Present := Present (Else_Statements (N));
11262 -- At the same time (see procedure Add_Accept) we build the accept list:
11264 -- Qnn : Accept_List (1 .. num-select) := (
11265 -- (null-body, entry-index),
11266 -- (null-body, entry-index),
11268 -- (null_body, entry-index));
11270 -- In the above declaration, null-body is True if the corresponding
11271 -- accept has no body, and false otherwise. The entry is either the
11272 -- entry index expression if there is no guard, or if a guard is
11273 -- present, then an if expression of the form:
11275 -- (if guard then entry-index else Null_Task_Entry)
11277 -- If a guard is statically known to be false, the entry can simply
11278 -- be omitted from the accept list.
11281 Make_Object_Declaration (Loc,
11282 Defining_Identifier => Qnam,
11283 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11284 Aliased_Present => True,
11286 Make_Qualified_Expression (Loc,
11288 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11290 Make_Aggregate (Loc, Expressions => Accept_List))));
11292 -- Then we declare the variable that holds the index for the accept
11293 -- that will be selected for service:
11295 -- Xnn : Select_Index;
11298 Make_Object_Declaration (Loc,
11299 Defining_Identifier => Xnam,
11300 Object_Definition =>
11301 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
11303 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
11305 -- After this follow procedure declarations for each accept body
11307 -- procedure Pnn is
11312 -- where the ... are statements from the corresponding procedure body.
11313 -- No parameters are involved, since the parameters are passed via Ann
11314 -- and the parameter references have already been expanded to be direct
11315 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
11316 -- any embedded tasking statements (which would normally be illegal in
11317 -- procedures), have been converted to calls to the tasking runtime so
11318 -- there is no problem in putting them into procedures.
11320 -- The original accept statement has been expanded into a block in
11321 -- the same fashion as for simple accepts (see Build_Accept_Body).
11323 -- Note: we don't really need to build these procedures for the case
11324 -- where no delay statement is present, but it is just as easy to
11325 -- build them unconditionally, and not significantly inefficient,
11326 -- since if they are short they will be inlined anyway.
11328 -- The procedure declarations have been assembled in Body_List
11330 -- If delays are present, we must compute the required delay.
11331 -- We first generate the declarations:
11333 -- Delay_Index : Boolean := 0;
11334 -- Delay_Min : Some_Time_Type.Time;
11335 -- Delay_Val : Some_Time_Type.Time;
11337 -- Delay_Index will be set to the index of the minimum delay, i.e. the
11338 -- active delay that is actually chosen as the basis for the possible
11339 -- delay if an immediate rendez-vous is not possible.
11341 -- In the most common case there is a single delay statement, and this
11342 -- is handled specially.
11344 if Delay_Count > 0 then
11346 -- Generate the required declarations
11349 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11351 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11353 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11356 Make_Object_Declaration (Loc,
11357 Defining_Identifier => Delay_Val,
11358 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11361 Make_Object_Declaration (Loc,
11362 Defining_Identifier => Delay_Index,
11363 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11364 Expression => Make_Integer_Literal (Loc, 0)));
11367 Make_Object_Declaration (Loc,
11368 Defining_Identifier => Delay_Min,
11369 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11371 Unchecked_Convert_To (Time_Type,
11372 Make_Attribute_Reference (Loc,
11374 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11375 Attribute_Name => Name_Last))));
11377 -- Create Duration and Delay_Mode objects used for passing a delay
11380 D := Make_Temporary (Loc, 'D');
11381 M := Make_Temporary (Loc, 'M');
11387 -- Note that these values are defined in s-osprim.ads and must
11388 -- be kept in sync:
11390 -- Relative : constant := 0;
11391 -- Absolute_Calendar : constant := 1;
11392 -- Absolute_RT : constant := 2;
11394 if Time_Type = Standard_Duration then
11395 Discr := Make_Integer_Literal (Loc, 0);
11397 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11398 Discr := Make_Integer_Literal (Loc, 1);
11402 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11403 Discr := Make_Integer_Literal (Loc, 2);
11407 Make_Object_Declaration (Loc,
11408 Defining_Identifier => D,
11409 Object_Definition =>
11410 New_Occurrence_Of (Standard_Duration, Loc)));
11413 Make_Object_Declaration (Loc,
11414 Defining_Identifier => M,
11415 Object_Definition =>
11416 New_Occurrence_Of (Standard_Integer, Loc),
11417 Expression => Discr));
11420 if Check_Guard then
11422 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11425 Make_Object_Declaration (Loc,
11426 Defining_Identifier => Guard_Open,
11427 Object_Definition =>
11428 New_Occurrence_Of (Standard_Boolean, Loc),
11430 New_Occurrence_Of (Standard_False, Loc)));
11433 -- Delay_Count is zero, don't need M and D set (suppress warning)
11440 if Present (Terminate_Alt) then
11442 -- If the terminate alternative guard is False, use
11443 -- Simple_Mode; otherwise use Terminate_Mode.
11445 if Present (Condition (Terminate_Alt)) then
11446 Select_Mode := Make_If_Expression (Loc,
11447 New_List (Condition (Terminate_Alt),
11448 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11449 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11451 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11454 elsif Else_Present or Delay_Count > 0 then
11455 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11458 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11461 Select_Call := Make_Select_Call (Select_Mode);
11462 Append (Select_Call, Stats);
11464 -- Now generate code to act on the result. There is an entry
11465 -- in this case for each accept statement with a non-null body,
11466 -- followed by a branch to the statements that follow the Accept.
11467 -- In the absence of delay alternatives, we generate:
11470 -- when No_Rendezvous => -- omitted if simple mode
11485 -- Lab0: Else_Statements;
11488 -- Lab1: Trailing_Statements1;
11491 -- Lab2: Trailing_Statements2;
11496 -- Generate label for common exit
11498 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11500 -- First entry is the default case, when no rendezvous is possible
11502 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11504 if Else_Present then
11506 -- If no rendezvous is possible, the else part is executed
11508 Lab := Make_And_Declare_Label (0);
11509 Alt_Stats := New_List (
11510 Make_Goto_Statement (Loc,
11511 Name => New_Copy (Identifier (Lab))));
11513 Append (Lab, Trailing_List);
11514 Append_List (Else_Statements (N), Trailing_List);
11515 Append_To (Trailing_List,
11516 Make_Goto_Statement (Loc,
11517 Name => New_Copy (Identifier (End_Lab))));
11519 Alt_Stats := New_List (
11520 Make_Goto_Statement (Loc,
11521 Name => New_Copy (Identifier (End_Lab))));
11524 Append_To (Alt_List,
11525 Make_Case_Statement_Alternative (Loc,
11526 Discrete_Choices => Choices,
11527 Statements => Alt_Stats));
11529 -- We make use of the fact that Accept_Index is an integer type, and
11530 -- generate successive literals for entries for each accept. Only those
11531 -- for which there is a body or trailing statements get a case entry.
11533 Alt := First (Select_Alternatives (N));
11534 Proc := First (Body_List);
11535 while Present (Alt) loop
11537 if Nkind (Alt) = N_Accept_Alternative then
11538 Process_Accept_Alternative (Alt, Index, Proc);
11539 Index := Index + 1;
11542 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11547 elsif Nkind (Alt) = N_Delay_Alternative then
11548 Process_Delay_Alternative (Alt, Delay_Num);
11549 Delay_Num := Delay_Num + 1;
11555 -- An others choice is always added to the main case, as well
11556 -- as the delay case (to satisfy the compiler).
11558 Append_To (Alt_List,
11559 Make_Case_Statement_Alternative (Loc,
11560 Discrete_Choices =>
11561 New_List (Make_Others_Choice (Loc)),
11563 New_List (Make_Goto_Statement (Loc,
11564 Name => New_Copy (Identifier (End_Lab))))));
11566 Accept_Case := New_List (
11567 Make_Case_Statement (Loc,
11568 Expression => New_Occurrence_Of (Xnam, Loc),
11569 Alternatives => Alt_List));
11571 Append_List (Trailing_List, Accept_Case);
11572 Append_List (Body_List, Decls);
11574 -- Construct case statement for trailing statements of delay
11575 -- alternatives, if there are several of them.
11577 if Delay_Count > 1 then
11578 Append_To (Delay_Alt_List,
11579 Make_Case_Statement_Alternative (Loc,
11580 Discrete_Choices =>
11581 New_List (Make_Others_Choice (Loc)),
11583 New_List (Make_Null_Statement (Loc))));
11585 Delay_Case := New_List (
11586 Make_Case_Statement (Loc,
11587 Expression => New_Occurrence_Of (Delay_Index, Loc),
11588 Alternatives => Delay_Alt_List));
11590 Delay_Case := Delay_Alt_List;
11593 -- If there are no delay alternatives, we append the case statement
11594 -- to the statement list.
11596 if Delay_Count = 0 then
11597 Append_List (Accept_Case, Stats);
11599 -- Delay alternatives present
11602 -- If delay alternatives are present we generate:
11604 -- find minimum delay.
11605 -- DX := minimum delay;
11606 -- M := <delay mode>;
11607 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11610 -- if X = No_Rendezvous then
11611 -- case statement for delay statements.
11613 -- case statement for accept alternatives.
11624 -- The type of the delay expression is known to be legal
11626 if Time_Type = Standard_Duration then
11627 Conv := New_Occurrence_Of (Delay_Min, Loc);
11629 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11630 Conv := Make_Function_Call (Loc,
11631 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11632 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11636 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11638 Conv := Make_Function_Call (Loc,
11639 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11640 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11643 Stmt := Make_Assignment_Statement (Loc,
11644 Name => New_Occurrence_Of (D, Loc),
11645 Expression => Conv);
11647 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11649 Parms := Parameter_Associations (Select_Call);
11651 Parm := First (Parms);
11652 while Present (Parm) and then Parm /= Select_Mode loop
11656 pragma Assert (Present (Parm));
11657 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11660 -- Prepare two new parameters of Duration and Delay_Mode type
11661 -- which represent the value and the mode of the minimum delay.
11664 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11665 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11667 -- Create a call to RTS
11669 Rewrite (Select_Call,
11670 Make_Procedure_Call_Statement (Loc,
11671 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11672 Parameter_Associations => Parms));
11674 -- This new call should follow the calculation of the minimum
11677 Insert_List_Before (Select_Call, Delay_List);
11679 if Check_Guard then
11681 Make_Implicit_If_Statement (N,
11682 Condition => New_Occurrence_Of (Guard_Open, Loc),
11683 Then_Statements => New_List (
11684 New_Copy_Tree (Stmt),
11685 New_Copy_Tree (Select_Call)),
11686 Else_Statements => Accept_Or_Raise);
11687 Rewrite (Select_Call, Stmt);
11689 Insert_Before (Select_Call, Stmt);
11693 Make_Implicit_If_Statement (N,
11694 Condition => Make_Op_Eq (Loc,
11695 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11697 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11699 Then_Statements => Delay_Case,
11700 Else_Statements => Accept_Case);
11702 Append (Cases, Stats);
11706 Append (End_Lab, Stats);
11708 -- Replace accept statement with appropriate block
11711 Make_Block_Statement (Loc,
11712 Declarations => Decls,
11713 Handled_Statement_Sequence =>
11714 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11717 -- Note: have to worry more about abort deferral in above code ???
11719 -- Final step is to unstack the Accept_Address entries for all accept
11720 -- statements appearing in accept alternatives in the select statement
11722 Alt := First (Alts);
11723 while Present (Alt) loop
11724 if Nkind (Alt) = N_Accept_Alternative then
11725 Remove_Last_Elmt (Accept_Address
11726 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11731 end Expand_N_Selective_Accept;
11733 -------------------------------------------
11734 -- Expand_N_Single_Protected_Declaration --
11735 -------------------------------------------
11737 -- A single protected declaration should never be present after semantic
11738 -- analysis because it is transformed into a protected type declaration
11739 -- and an accompanying anonymous object. This routine ensures that the
11740 -- transformation takes place.
11742 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11744 raise Program_Error;
11745 end Expand_N_Single_Protected_Declaration;
11747 --------------------------------------
11748 -- Expand_N_Single_Task_Declaration --
11749 --------------------------------------
11751 -- A single task declaration should never be present after semantic
11752 -- analysis because it is transformed into a task type declaration and
11753 -- an accompanying anonymous object. This routine ensures that the
11754 -- transformation takes place.
11756 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11758 raise Program_Error;
11759 end Expand_N_Single_Task_Declaration;
11761 ------------------------
11762 -- Expand_N_Task_Body --
11763 ------------------------
11765 -- Given a task body
11767 -- task body tname is
11773 -- This expansion routine converts it into a procedure and sets the
11774 -- elaboration flag for the procedure to true, to represent the fact
11775 -- that the task body is now elaborated:
11777 -- procedure tnameB (_Task : access tnameV) is
11778 -- discriminal : dtype renames _Task.discriminant;
11780 -- procedure _clean is
11782 -- Abort_Defer.all;
11784 -- Abort_Undefer.all;
11789 -- Abort_Undefer.all;
11791 -- System.Task_Stages.Complete_Activation;
11799 -- In addition, if the task body is an activator, then a call to activate
11800 -- tasks is added at the start of the statements, before the call to
11801 -- Complete_Activation, and if in addition the task is a master then it
11802 -- must be established as a master. These calls are inserted and analyzed
11803 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11806 -- There is one discriminal declaration line generated for each
11807 -- discriminant that is present to provide an easy reference point for
11808 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11810 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11811 -- task body procedures have a profile (Arg : System.Address). That is
11812 -- needed because GNARLI has to use the same access-to-subprogram type
11813 -- for all task types. We depend here on knowing that in GNAT, passing
11814 -- an address argument by value is identical to passing a record value
11815 -- by access (in either case a single pointer is passed), so even though
11816 -- this procedure has the wrong profile. In fact it's all OK, since the
11817 -- callings sequence is identical.
11819 procedure Expand_N_Task_Body (N : Node_Id) is
11820 Loc : constant Source_Ptr := Sloc (N);
11821 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11825 Insert_Nod : Node_Id;
11826 -- Used to determine the proper location of wrapper body insertions
11829 -- if no task body procedure, means we had an error in configurable
11830 -- run-time mode, and there is no point in proceeding further.
11832 if No (Task_Body_Procedure (Ttyp)) then
11836 -- Add renaming declarations for discriminals and a declaration for the
11837 -- entry family index (if applicable).
11839 Install_Private_Data_Declarations
11840 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11842 -- Add a call to Abort_Undefer at the very beginning of the task
11843 -- body since this body is called with abort still deferred.
11845 if Abort_Allowed then
11846 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11848 (First (Statements (Handled_Statement_Sequence (N))), Call);
11852 -- The statement part has already been protected with an at_end and
11853 -- cleanup actions. The call to Complete_Activation must be placed
11854 -- at the head of the sequence of statements of that block. The
11855 -- declarations have been merged in this sequence of statements but
11856 -- the first real statement is accessible from the First_Real_Statement
11857 -- field (which was set for exactly this purpose).
11859 if Restricted_Profile then
11860 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11862 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11866 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11870 Make_Subprogram_Body (Loc,
11871 Specification => Build_Task_Proc_Specification (Ttyp),
11872 Declarations => Declarations (N),
11873 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11874 Set_Is_Task_Body_Procedure (New_N);
11876 -- If the task contains generic instantiations, cleanup actions are
11877 -- delayed until after instantiation. Transfer the activation chain to
11878 -- the subprogram, to insure that the activation call is properly
11879 -- generated. It the task body contains inner tasks, indicate that the
11880 -- subprogram is a task master.
11882 if Delay_Cleanups (Ttyp) then
11883 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11884 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11887 Rewrite (N, New_N);
11890 -- Set elaboration flag immediately after task body. If the body is a
11891 -- subunit, the flag is set in the declarative part containing the stub.
11893 if Nkind (Parent (N)) /= N_Subunit then
11895 Make_Assignment_Statement (Loc,
11897 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11898 Expression => New_Occurrence_Of (Standard_True, Loc)));
11901 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11902 -- the task body. At this point all wrapper specs have been created,
11903 -- frozen and included in the dispatch table for the task type.
11905 if Ada_Version >= Ada_2005 then
11906 if Nkind (Parent (N)) = N_Subunit then
11907 Insert_Nod := Corresponding_Stub (Parent (N));
11912 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11914 end Expand_N_Task_Body;
11916 ------------------------------------
11917 -- Expand_N_Task_Type_Declaration --
11918 ------------------------------------
11920 -- We have several things to do. First we must create a Boolean flag used
11921 -- to mark if the body is elaborated yet. This variable gets set to True
11922 -- when the body of the task is elaborated (we can't rely on the normal
11923 -- ABE mechanism for the task body, since we need to pass an access to
11924 -- this elaboration boolean to the runtime routines).
11926 -- taskE : aliased Boolean := False;
11928 -- Next a variable is declared to hold the task stack size (either the
11929 -- default : Unspecified_Size, or a value that is set by a pragma
11930 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11931 -- the variable is initialized with this value:
11933 -- taskZ : Size_Type := Unspecified_Size;
11935 -- taskZ : Size_Type := Size_Type (size_expression);
11937 -- Note: No variable is needed to hold the task relative deadline since
11938 -- its value would never be static because the parameter is of a private
11939 -- type (Ada.Real_Time.Time_Span).
11941 -- Next we create a corresponding record type declaration used to represent
11942 -- values of this task. The general form of this type declaration is
11944 -- type taskV (discriminants) is record
11945 -- _Task_Id : Task_Id;
11946 -- entry_family : array (bounds) of Void;
11947 -- _Priority : Integer := priority_expression;
11948 -- _Size : Size_Type := size_expression;
11949 -- _Task_Info : Task_Info_Type := task_info_expression;
11950 -- _CPU : Integer := cpu_range_expression;
11951 -- _Relative_Deadline : Time_Span := time_span_expression;
11952 -- _Domain : Dispatching_Domain := dd_expression;
11955 -- The discriminants are present only if the corresponding task type has
11956 -- discriminants, and they exactly mirror the task type discriminants.
11958 -- The Id field is always present. It contains the Task_Id value, as set by
11959 -- the call to Create_Task. Note that although the task is limited, the
11960 -- task value record type is not limited, so there is no problem in passing
11961 -- this field as an out parameter to Create_Task.
11963 -- One entry_family component is present for each entry family in the task
11964 -- definition. The bounds correspond to the bounds of the entry family
11965 -- (which may depend on discriminants). The element type is void, since we
11966 -- only need the bounds information for determining the entry index. Note
11967 -- that the use of an anonymous array would normally be illegal in this
11968 -- context, but this is a parser check, and the semantics is quite prepared
11969 -- to handle such a case.
11971 -- The _Size field is present only if a Storage_Size pragma appears in the
11972 -- task definition. The expression captures the argument that was present
11973 -- in the pragma, and is used to override the task stack size otherwise
11974 -- associated with the task type.
11976 -- The _Priority field is present only if the task entity has a Priority or
11977 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11978 -- definition clause). It will be filled at the freeze point, when the
11979 -- record init proc is built, to capture the expression of the rep item
11980 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11981 -- here since aspect evaluations are delayed till the freeze point.
11983 -- The _Task_Info field is present only if a Task_Info pragma appears in
11984 -- the task definition. The expression captures the argument that was
11985 -- present in the pragma, and is used to provide the Task_Image parameter
11986 -- to the call to Create_Task.
11988 -- The _CPU field is present only if the task entity has a CPU rep item
11989 -- (pragma, aspect specification or attribute definition clause). It will
11990 -- be filled at the freeze point, when the record init proc is built, to
11991 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11992 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11993 -- are delayed till the freeze point.
11995 -- The _Relative_Deadline field is present only if a Relative_Deadline
11996 -- pragma appears in the task definition. The expression captures the
11997 -- argument that was present in the pragma, and is used to provide the
11998 -- Relative_Deadline parameter to the call to Create_Task.
12000 -- The _Domain field is present only if the task entity has a
12001 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
12002 -- definition clause). It will be filled at the freeze point, when the
12003 -- record init proc is built, to capture the expression of the rep item
12004 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
12005 -- here since aspect evaluations are delayed till the freeze point.
12007 -- When a task is declared, an instance of the task value record is
12008 -- created. The elaboration of this declaration creates the correct bounds
12009 -- for the entry families, and also evaluates the size, priority, and
12010 -- task_Info expressions if needed. The initialization routine for the task
12011 -- type itself then calls Create_Task with appropriate parameters to
12012 -- initialize the value of the Task_Id field.
12014 -- Note: the address of this record is passed as the "Discriminants"
12015 -- parameter for Create_Task. Since Create_Task merely passes this onto the
12016 -- body procedure, it does not matter that it does not quite match the
12017 -- GNARLI model of what is being passed (the record contains more than just
12018 -- the discriminants, but the discriminants can be found from the record
12021 -- The Entity_Id for this created record type is placed in the
12022 -- Corresponding_Record_Type field of the associated task type entity.
12024 -- Next we create a procedure specification for the task body procedure:
12026 -- procedure taskB (_Task : access taskV);
12028 -- Note that this must come after the record type declaration, since
12029 -- the spec refers to this type. It turns out that the initialization
12030 -- procedure for the value type references the task body spec, but that's
12031 -- fine, since it won't be generated till the freeze point for the type,
12032 -- which is certainly after the task body spec declaration.
12034 -- Finally, we set the task index value field of the entry attribute in
12035 -- the case of a simple entry.
12037 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
12038 Loc : constant Source_Ptr := Sloc (N);
12039 TaskId : constant Entity_Id := Defining_Identifier (N);
12040 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
12041 Tasknm : constant Name_Id := Chars (Tasktyp);
12042 Taskdef : constant Node_Id := Task_Definition (N);
12044 Body_Decl : Node_Id;
12046 Decl_Stack : Node_Id;
12047 Elab_Decl : Node_Id;
12048 Ent_Stack : Entity_Id;
12049 Proc_Spec : Node_Id;
12050 Rec_Decl : Node_Id;
12051 Rec_Ent : Entity_Id;
12052 Size_Decl : Entity_Id;
12053 Task_Size : Node_Id;
12055 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
12056 -- Searches the task definition T for the first occurrence of the pragma
12057 -- Relative Deadline. The caller has ensured that the pragma is present
12058 -- in the task definition. Note that this routine cannot be implemented
12059 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
12060 -- not chained because their expansion into a procedure call statement
12061 -- would cause a break in the chain.
12063 ----------------------------------
12064 -- Get_Relative_Deadline_Pragma --
12065 ----------------------------------
12067 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
12071 N := First (Visible_Declarations (T));
12072 while Present (N) loop
12073 if Nkind (N) = N_Pragma
12074 and then Pragma_Name (N) = Name_Relative_Deadline
12082 N := First (Private_Declarations (T));
12083 while Present (N) loop
12084 if Nkind (N) = N_Pragma
12085 and then Pragma_Name (N) = Name_Relative_Deadline
12093 raise Program_Error;
12094 end Get_Relative_Deadline_Pragma;
12096 -- Start of processing for Expand_N_Task_Type_Declaration
12099 -- If already expanded, nothing to do
12101 if Present (Corresponding_Record_Type (Tasktyp)) then
12105 -- Here we will do the expansion
12107 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
12109 Rec_Ent := Defining_Identifier (Rec_Decl);
12110 Cdecls := Component_Items (Component_List
12111 (Type_Definition (Rec_Decl)));
12113 Qualify_Entity_Names (N);
12115 -- First create the elaboration variable
12118 Make_Object_Declaration (Loc,
12119 Defining_Identifier =>
12120 Make_Defining_Identifier (Sloc (Tasktyp),
12121 Chars => New_External_Name (Tasknm, 'E')),
12122 Aliased_Present => True,
12123 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
12124 Expression => New_Occurrence_Of (Standard_False, Loc));
12126 Insert_After (N, Elab_Decl);
12128 -- Next create the declaration of the size variable (tasknmZ)
12130 Set_Storage_Size_Variable (Tasktyp,
12131 Make_Defining_Identifier (Sloc (Tasktyp),
12132 Chars => New_External_Name (Tasknm, 'Z')));
12134 if Present (Taskdef)
12135 and then Has_Storage_Size_Pragma (Taskdef)
12137 Is_OK_Static_Expression
12139 (First (Pragma_Argument_Associations
12140 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
12143 Make_Object_Declaration (Loc,
12144 Defining_Identifier => Storage_Size_Variable (Tasktyp),
12145 Object_Definition =>
12146 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
12148 Convert_To (RTE (RE_Size_Type),
12150 (Expression (First (Pragma_Argument_Associations
12152 (TaskId, Name_Storage_Size)))))));
12156 Make_Object_Declaration (Loc,
12157 Defining_Identifier => Storage_Size_Variable (Tasktyp),
12158 Object_Definition =>
12159 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
12161 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
12164 Insert_After (Elab_Decl, Size_Decl);
12166 -- Next build the rest of the corresponding record declaration. This is
12167 -- done last, since the corresponding record initialization procedure
12168 -- will reference the previously created entities.
12170 -- Fill in the component declarations -- first the _Task_Id field
12173 Make_Component_Declaration (Loc,
12174 Defining_Identifier =>
12175 Make_Defining_Identifier (Loc, Name_uTask_Id),
12176 Component_Definition =>
12177 Make_Component_Definition (Loc,
12178 Aliased_Present => False,
12179 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
12182 -- Declare static ATCB (that is, created by the expander) if we are
12183 -- using the Restricted run time.
12185 if Restricted_Profile then
12187 Make_Component_Declaration (Loc,
12188 Defining_Identifier =>
12189 Make_Defining_Identifier (Loc, Name_uATCB),
12191 Component_Definition =>
12192 Make_Component_Definition (Loc,
12193 Aliased_Present => True,
12194 Subtype_Indication => Make_Subtype_Indication (Loc,
12196 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
12199 Make_Index_Or_Discriminant_Constraint (Loc,
12201 New_List (Make_Integer_Literal (Loc, 0)))))));
12205 -- Declare static stack (that is, created by the expander) if we are
12206 -- using the Restricted run time on a bare board configuration.
12208 if Restricted_Profile and then Preallocated_Stacks_On_Target then
12210 -- First we need to extract the appropriate stack size
12212 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
12214 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12216 Expr_N : constant Node_Id :=
12217 Expression (First (
12218 Pragma_Argument_Associations (
12219 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
12220 Etyp : constant Entity_Id := Etype (Expr_N);
12221 P : constant Node_Id := Parent (Expr_N);
12224 -- The stack is defined inside the corresponding record.
12225 -- Therefore if the size of the stack is set by means of
12226 -- a discriminant, we must reference the discriminant of the
12227 -- corresponding record type.
12229 if Nkind (Expr_N) in N_Has_Entity
12230 and then Present (Discriminal_Link (Entity (Expr_N)))
12234 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
12236 Set_Parent (Task_Size, P);
12237 Set_Etype (Task_Size, Etyp);
12238 Set_Analyzed (Task_Size);
12241 Task_Size := Relocate_Node (Expr_N);
12247 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
12250 Decl_Stack := Make_Component_Declaration (Loc,
12251 Defining_Identifier => Ent_Stack,
12253 Component_Definition =>
12254 Make_Component_Definition (Loc,
12255 Aliased_Present => True,
12256 Subtype_Indication => Make_Subtype_Indication (Loc,
12258 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
12261 Make_Index_Or_Discriminant_Constraint (Loc,
12262 Constraints => New_List (Make_Range (Loc,
12263 Low_Bound => Make_Integer_Literal (Loc, 1),
12264 High_Bound => Convert_To (RTE (RE_Storage_Offset),
12267 Append_To (Cdecls, Decl_Stack);
12269 -- The appropriate alignment for the stack is ensured by the run-time
12270 -- code in charge of task creation.
12274 -- Add components for entry families
12276 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
12278 -- Add the _Priority component if a Interrupt_Priority or Priority rep
12279 -- item is present.
12281 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
12283 Make_Component_Declaration (Loc,
12284 Defining_Identifier =>
12285 Make_Defining_Identifier (Loc, Name_uPriority),
12286 Component_Definition =>
12287 Make_Component_Definition (Loc,
12288 Aliased_Present => False,
12289 Subtype_Indication =>
12290 New_Occurrence_Of (Standard_Integer, Loc))));
12293 -- Add the _Size component if a Storage_Size pragma is present
12295 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12297 Make_Component_Declaration (Loc,
12298 Defining_Identifier =>
12299 Make_Defining_Identifier (Loc, Name_uSize),
12301 Component_Definition =>
12302 Make_Component_Definition (Loc,
12303 Aliased_Present => False,
12304 Subtype_Indication =>
12305 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
12308 Convert_To (RTE (RE_Size_Type),
12310 Expression (First (
12311 Pragma_Argument_Associations (
12312 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
12315 -- Add the _Task_Info component if a Task_Info pragma is present
12317 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
12319 Make_Component_Declaration (Loc,
12320 Defining_Identifier =>
12321 Make_Defining_Identifier (Loc, Name_uTask_Info),
12323 Component_Definition =>
12324 Make_Component_Definition (Loc,
12325 Aliased_Present => False,
12326 Subtype_Indication =>
12327 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12329 Expression => New_Copy (
12330 Expression (First (
12331 Pragma_Argument_Associations (
12333 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12336 -- Add the _CPU component if a CPU rep item is present
12338 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12340 Make_Component_Declaration (Loc,
12341 Defining_Identifier =>
12342 Make_Defining_Identifier (Loc, Name_uCPU),
12344 Component_Definition =>
12345 Make_Component_Definition (Loc,
12346 Aliased_Present => False,
12347 Subtype_Indication =>
12348 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12351 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12352 -- present. If we are using a restricted run time this component will
12353 -- not be added (deadlines are not allowed by the Ravenscar profile).
12355 if not Restricted_Profile
12356 and then Present (Taskdef)
12357 and then Has_Relative_Deadline_Pragma (Taskdef)
12360 Make_Component_Declaration (Loc,
12361 Defining_Identifier =>
12362 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12364 Component_Definition =>
12365 Make_Component_Definition (Loc,
12366 Aliased_Present => False,
12367 Subtype_Indication =>
12368 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12371 Convert_To (RTE (RE_Time_Span),
12373 Expression (First (
12374 Pragma_Argument_Associations (
12375 Get_Relative_Deadline_Pragma (Taskdef))))))));
12378 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12379 -- item is present. If we are using a restricted run time this component
12380 -- will not be added (dispatching domains are not allowed by the
12381 -- Ravenscar profile).
12383 if not Restricted_Profile
12386 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12389 Make_Component_Declaration (Loc,
12390 Defining_Identifier =>
12391 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12393 Component_Definition =>
12394 Make_Component_Definition (Loc,
12395 Aliased_Present => False,
12396 Subtype_Indication =>
12398 (RTE (RE_Dispatching_Domain_Access), Loc))));
12401 Insert_After (Size_Decl, Rec_Decl);
12403 -- Analyze the record declaration immediately after construction,
12404 -- because the initialization procedure is needed for single task
12405 -- declarations before the next entity is analyzed.
12407 Analyze (Rec_Decl);
12409 -- Create the declaration of the task body procedure
12411 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12413 Make_Subprogram_Declaration (Loc,
12414 Specification => Proc_Spec);
12415 Set_Is_Task_Body_Procedure (Body_Decl);
12417 Insert_After (Rec_Decl, Body_Decl);
12419 -- The subprogram does not comes from source, so we have to indicate the
12420 -- need for debugging information explicitly.
12422 if Comes_From_Source (Original_Node (N)) then
12423 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12426 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12427 -- the corresponding record has been frozen.
12429 if Ada_Version >= Ada_2005 then
12430 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12433 -- Ada 2005 (AI-345): We must defer freezing to allow further
12434 -- declaration of primitive subprograms covering task interfaces
12436 if Ada_Version <= Ada_95 then
12438 -- Now we can freeze the corresponding record. This needs manually
12439 -- freezing, since it is really part of the task type, and the task
12440 -- type is frozen at this stage. We of course need the initialization
12441 -- procedure for this corresponding record type and we won't get it
12442 -- in time if we don't freeze now.
12445 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
12447 if Is_Non_Empty_List (L) then
12448 Insert_List_After (Body_Decl, L);
12453 -- Complete the expansion of access types to the current task type, if
12454 -- any were declared.
12456 Expand_Previous_Access_Type (Tasktyp);
12458 -- Create wrappers for entries that have contract cases, preconditions
12459 -- and postconditions.
12465 Ent := First_Entity (Tasktyp);
12466 while Present (Ent) loop
12467 if Ekind_In (Ent, E_Entry, E_Entry_Family) then
12468 Build_Contract_Wrapper (Ent, N);
12474 end Expand_N_Task_Type_Declaration;
12476 -------------------------------
12477 -- Expand_N_Timed_Entry_Call --
12478 -------------------------------
12480 -- A timed entry call in normal case is not implemented using ATC mechanism
12481 -- anymore for efficiency reason.
12491 -- is expanded as follows:
12493 -- 1) When T.E is a task entry_call;
12497 -- X : Task_Entry_Index := <entry index>;
12498 -- DX : Duration := To_Duration (D);
12499 -- M : Delay_Mode := <discriminant>;
12500 -- P : parms := (parm, parm, parm);
12503 -- Timed_Protected_Entry_Call
12504 -- (<acceptor-task>, X, P'Address, DX, M, B);
12512 -- 2) When T.E is a protected entry_call;
12516 -- X : Protected_Entry_Index := <entry index>;
12517 -- DX : Duration := To_Duration (D);
12518 -- M : Delay_Mode := <discriminant>;
12519 -- P : parms := (parm, parm, parm);
12522 -- Timed_Protected_Entry_Call
12523 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12531 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12532 -- is no delay and the triggering statements are executed. We first
12533 -- determine the kind of the triggering call and then execute a
12534 -- synchronized operation or a direct call.
12537 -- B : Boolean := False;
12538 -- C : Ada.Tags.Prim_Op_Kind;
12539 -- DX : Duration := To_Duration (D)
12540 -- K : Ada.Tags.Tagged_Kind :=
12541 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12542 -- M : Integer :=...;
12543 -- P : Parameters := (Param1 .. ParamN);
12547 -- if K = Ada.Tags.TK_Limited_Tagged
12548 -- or else K = Ada.Tags.TK_Tagged
12550 -- <dispatching-call>;
12555 -- Ada.Tags.Get_Offset_Index
12556 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12558 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12560 -- if C = POK_Protected_Entry
12561 -- or else C = POK_Task_Entry
12563 -- Param1 := P.Param1;
12565 -- ParamN := P.ParamN;
12569 -- if C = POK_Procedure
12570 -- or else C = POK_Protected_Procedure
12571 -- or else C = POK_Task_Procedure
12573 -- <dispatching-call>;
12579 -- <triggering-statements>
12581 -- <timed-statements>
12585 -- The triggering statement and the sequence of timed statements have not
12586 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12587 -- global references if within an instantiation.
12589 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12590 Loc : constant Source_Ptr := Sloc (N);
12593 Blk_Typ : Entity_Id;
12595 Call_Ent : Entity_Id;
12596 Conc_Typ_Stmts : List_Id;
12598 D_Alt : constant Node_Id := Delay_Alternative (N);
12601 D_Stat : Node_Id := Delay_Statement (D_Alt);
12603 D_Type : Entity_Id;
12606 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12607 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12612 Is_Disp_Select : Boolean;
12613 Lim_Typ_Stmts : List_Id;
12622 B : Entity_Id; -- Call status flag
12623 C : Entity_Id; -- Call kind
12624 D : Entity_Id; -- Delay
12625 K : Entity_Id; -- Tagged kind
12626 M : Entity_Id; -- Delay mode
12627 P : Entity_Id; -- Parameter block
12628 S : Entity_Id; -- Primitive operation slot
12630 -- Start of processing for Expand_N_Timed_Entry_Call
12633 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12634 -- was already reported on spec, so do not attempt to expand the call.
12636 if Restriction_Active (No_Select_Statements) then
12640 Process_Statements_For_Controlled_Objects (E_Alt);
12641 Process_Statements_For_Controlled_Objects (D_Alt);
12643 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12645 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12646 -- may wrap them in blocks.
12648 E_Stats := Statements (E_Alt);
12649 D_Stats := Statements (D_Alt);
12651 -- The arguments in the call may require dynamic allocation, and the
12652 -- call statement may have been transformed into a block. The block
12653 -- may contain additional declarations for internal entities, and the
12654 -- original call is found by sequential search.
12656 if Nkind (E_Call) = N_Block_Statement then
12657 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12658 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12659 N_Entry_Call_Statement)
12666 Ada_Version >= Ada_2005
12667 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12669 if Is_Disp_Select then
12670 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12676 -- B : Boolean := False;
12678 B := Build_B (Loc, Decls);
12681 -- C : Ada.Tags.Prim_Op_Kind;
12683 C := Build_C (Loc, Decls);
12685 -- Because the analysis of all statements was disabled, manually
12686 -- analyze the delay statement.
12689 D_Stat := Original_Node (D_Stat);
12692 -- Build an entry call using Simple_Entry_Call
12694 Extract_Entry (E_Call, Concval, Ename, Index);
12695 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12697 Decls := Declarations (E_Call);
12698 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12707 B := Make_Defining_Identifier (Loc, Name_uB);
12710 Make_Object_Declaration (Loc,
12711 Defining_Identifier => B,
12712 Object_Definition =>
12713 New_Occurrence_Of (Standard_Boolean, Loc)));
12716 -- Duration and mode processing
12718 D_Type := Base_Type (Etype (Expression (D_Stat)));
12720 -- Use the type of the delay expression (Calendar or Real_Time) to
12721 -- generate the appropriate conversion.
12723 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12724 D_Disc := Make_Integer_Literal (Loc, 0);
12725 D_Conv := Relocate_Node (Expression (D_Stat));
12727 elsif Is_RTE (D_Type, RO_CA_Time) then
12728 D_Disc := Make_Integer_Literal (Loc, 1);
12730 Make_Function_Call (Loc,
12731 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12732 Parameter_Associations =>
12733 New_List (New_Copy (Expression (D_Stat))));
12735 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12736 D_Disc := Make_Integer_Literal (Loc, 2);
12738 Make_Function_Call (Loc,
12739 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12740 Parameter_Associations =>
12741 New_List (New_Copy (Expression (D_Stat))));
12744 D := Make_Temporary (Loc, 'D');
12750 Make_Object_Declaration (Loc,
12751 Defining_Identifier => D,
12752 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12754 M := Make_Temporary (Loc, 'M');
12757 -- M : Integer := (0 | 1 | 2);
12760 Make_Object_Declaration (Loc,
12761 Defining_Identifier => M,
12762 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12763 Expression => D_Disc));
12765 -- Do the assignment at this stage only because the evaluation of the
12766 -- expression must not occur before (see ACVC C97302A).
12769 Make_Assignment_Statement (Loc,
12770 Name => New_Occurrence_Of (D, Loc),
12771 Expression => D_Conv));
12773 -- Parameter block processing
12775 -- Manually create the parameter block for dispatching calls. In the
12776 -- case of entries, the block has already been created during the call
12777 -- to Build_Simple_Entry_Call.
12779 if Is_Disp_Select then
12781 -- Tagged kind processing, generate:
12782 -- K : Ada.Tags.Tagged_Kind :=
12783 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12785 K := Build_K (Loc, Decls, Obj);
12787 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12789 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12791 -- Dispatch table slot processing, generate:
12794 S := Build_S (Loc, Decls);
12797 -- S := Ada.Tags.Get_Offset_Index
12798 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12801 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12804 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12806 -- where Obj is the controlling formal parameter, S is the dispatch
12807 -- table slot number of the dispatching operation, P is the wrapped
12808 -- parameter block, D is the duration, M is the duration mode, C is
12809 -- the call kind and B is the call status.
12811 Params := New_List;
12813 Append_To (Params, New_Copy_Tree (Obj));
12814 Append_To (Params, New_Occurrence_Of (S, Loc));
12816 Make_Attribute_Reference (Loc,
12817 Prefix => New_Occurrence_Of (P, Loc),
12818 Attribute_Name => Name_Address));
12819 Append_To (Params, New_Occurrence_Of (D, Loc));
12820 Append_To (Params, New_Occurrence_Of (M, Loc));
12821 Append_To (Params, New_Occurrence_Of (C, Loc));
12822 Append_To (Params, New_Occurrence_Of (B, Loc));
12824 Append_To (Conc_Typ_Stmts,
12825 Make_Procedure_Call_Statement (Loc,
12829 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12830 Parameter_Associations => Params));
12833 -- if C = POK_Protected_Entry
12834 -- or else C = POK_Task_Entry
12836 -- Param1 := P.Param1;
12838 -- ParamN := P.ParamN;
12841 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12843 -- Generate the if statement only when the packed parameters need
12844 -- explicit assignments to their corresponding actuals.
12846 if Present (Unpack) then
12847 Append_To (Conc_Typ_Stmts,
12848 Make_Implicit_If_Statement (N,
12854 Left_Opnd => New_Occurrence_Of (C, Loc),
12857 (RTE (RE_POK_Protected_Entry), Loc)),
12861 Left_Opnd => New_Occurrence_Of (C, Loc),
12863 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12865 Then_Statements => Unpack));
12871 -- if C = POK_Procedure
12872 -- or else C = POK_Protected_Procedure
12873 -- or else C = POK_Task_Procedure
12875 -- <dispatching-call>
12879 N_Stats := New_List (
12880 Make_Implicit_If_Statement (N,
12885 Left_Opnd => New_Occurrence_Of (C, Loc),
12887 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12893 Left_Opnd => New_Occurrence_Of (C, Loc),
12895 New_Occurrence_Of (RTE (
12896 RE_POK_Protected_Procedure), Loc)),
12899 Left_Opnd => New_Occurrence_Of (C, Loc),
12902 (RTE (RE_POK_Task_Procedure), Loc)))),
12904 Then_Statements => New_List (E_Call)));
12906 Append_To (Conc_Typ_Stmts,
12907 Make_Implicit_If_Statement (N,
12908 Condition => New_Occurrence_Of (B, Loc),
12909 Then_Statements => N_Stats));
12912 -- <dispatching-call>;
12916 New_List (New_Copy_Tree (E_Call),
12917 Make_Assignment_Statement (Loc,
12918 Name => New_Occurrence_Of (B, Loc),
12919 Expression => New_Occurrence_Of (Standard_True, Loc)));
12922 -- if K = Ada.Tags.TK_Limited_Tagged
12923 -- or else K = Ada.Tags.TK_Tagged
12931 Make_Implicit_If_Statement (N,
12932 Condition => Build_Dispatching_Tag_Check (K, N),
12933 Then_Statements => Lim_Typ_Stmts,
12934 Else_Statements => Conc_Typ_Stmts));
12939 -- <triggering-statements>
12941 -- <timed-statements>
12945 Make_Implicit_If_Statement (N,
12946 Condition => New_Occurrence_Of (B, Loc),
12947 Then_Statements => E_Stats,
12948 Else_Statements => D_Stats));
12951 -- Simple case of a nondispatching trigger. Skip assignments to
12952 -- temporaries created for in-out parameters.
12954 -- This makes unwarranted assumptions about the shape of the expanded
12955 -- tree for the call, and should be cleaned up ???
12957 Stmt := First (Stmts);
12958 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12962 -- Do the assignment at this stage only because the evaluation
12963 -- of the expression must not occur before (see ACVC C97302A).
12965 Insert_Before (Stmt,
12966 Make_Assignment_Statement (Loc,
12967 Name => New_Occurrence_Of (D, Loc),
12968 Expression => D_Conv));
12971 Params := Parameter_Associations (Call);
12973 -- For a protected type, we build a Timed_Protected_Entry_Call
12975 if Is_Protected_Type (Etype (Concval)) then
12977 -- Create a new call statement
12979 Param := First (Params);
12980 while Present (Param)
12981 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12986 Dummy := Remove_Next (Next (Param));
12988 -- Remove garbage is following the Cancel_Param if present
12990 Dummy := Next (Param);
12992 -- Remove the mode of the Protected_Entry_Call call, then remove
12993 -- the Communication_Block of the Protected_Entry_Call call, and
12994 -- finally add Duration and a Delay_Mode parameter
12996 pragma Assert (Present (Param));
12997 Rewrite (Param, New_Occurrence_Of (D, Loc));
12999 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
13001 -- Add a Boolean flag for successful entry call
13003 Append_To (Params, New_Occurrence_Of (B, Loc));
13005 case Corresponding_Runtime_Package (Etype (Concval)) is
13006 when System_Tasking_Protected_Objects_Entries =>
13008 Make_Procedure_Call_Statement (Loc,
13011 (RTE (RE_Timed_Protected_Entry_Call), Loc),
13012 Parameter_Associations => Params));
13015 raise Program_Error;
13018 -- For the task case, build a Timed_Task_Entry_Call
13021 -- Create a new call statement
13023 Append_To (Params, New_Occurrence_Of (D, Loc));
13024 Append_To (Params, New_Occurrence_Of (M, Loc));
13025 Append_To (Params, New_Occurrence_Of (B, Loc));
13028 Make_Procedure_Call_Statement (Loc,
13030 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
13031 Parameter_Associations => Params));
13035 Make_Implicit_If_Statement (N,
13036 Condition => New_Occurrence_Of (B, Loc),
13037 Then_Statements => E_Stats,
13038 Else_Statements => D_Stats));
13042 Make_Block_Statement (Loc,
13043 Declarations => Decls,
13044 Handled_Statement_Sequence =>
13045 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
13048 end Expand_N_Timed_Entry_Call;
13050 ----------------------------------------
13051 -- Expand_Protected_Body_Declarations --
13052 ----------------------------------------
13054 procedure Expand_Protected_Body_Declarations
13056 Spec_Id : Entity_Id)
13059 if No_Run_Time_Mode then
13060 Error_Msg_CRT ("protected body", N);
13063 elsif Expander_Active then
13065 -- Associate discriminals with the first subprogram or entry body to
13068 if Present (First_Protected_Operation (Declarations (N))) then
13069 Set_Discriminals (Parent (Spec_Id));
13072 end Expand_Protected_Body_Declarations;
13074 -------------------------
13075 -- External_Subprogram --
13076 -------------------------
13078 function External_Subprogram (E : Entity_Id) return Entity_Id is
13079 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
13082 -- The internal and external subprograms follow each other on the entity
13083 -- chain. Note that previously private operations had no separate
13084 -- external subprogram. We now create one in all cases, because a
13085 -- private operation may actually appear in an external call, through
13086 -- a 'Access reference used for a callback.
13088 -- If the operation is a function that returns an anonymous access type,
13089 -- the corresponding itype appears before the operation, and must be
13092 -- This mechanism is fragile, there should be a real link between the
13093 -- two versions of the operation, but there is no place to put it ???
13095 if Is_Access_Type (Next_Entity (Subp)) then
13096 return Next_Entity (Next_Entity (Subp));
13098 return Next_Entity (Subp);
13100 end External_Subprogram;
13102 ------------------------------
13103 -- Extract_Dispatching_Call --
13104 ------------------------------
13106 procedure Extract_Dispatching_Call
13108 Call_Ent : out Entity_Id;
13109 Object : out Entity_Id;
13110 Actuals : out List_Id;
13111 Formals : out List_Id)
13113 Call_Nam : Node_Id;
13116 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
13118 if Present (Original_Node (N)) then
13119 Call_Nam := Name (Original_Node (N));
13121 Call_Nam := Name (N);
13124 -- Retrieve the name of the dispatching procedure. It contains the
13125 -- dispatch table slot number.
13128 case Nkind (Call_Nam) is
13129 when N_Identifier =>
13132 when N_Selected_Component =>
13133 Call_Nam := Selector_Name (Call_Nam);
13136 raise Program_Error;
13141 Actuals := Parameter_Associations (N);
13142 Call_Ent := Entity (Call_Nam);
13143 Formals := Parameter_Specifications (Parent (Call_Ent));
13144 Object := First (Actuals);
13146 if Present (Original_Node (Object)) then
13147 Object := Original_Node (Object);
13150 -- If the type of the dispatching object is an access type then return
13151 -- an explicit dereference.
13153 if Is_Access_Type (Etype (Object)) then
13154 Object := Make_Explicit_Dereference (Sloc (N), Object);
13157 end Extract_Dispatching_Call;
13159 -------------------
13160 -- Extract_Entry --
13161 -------------------
13163 procedure Extract_Entry
13165 Concval : out Node_Id;
13166 Ename : out Node_Id;
13167 Index : out Node_Id)
13169 Nam : constant Node_Id := Name (N);
13172 -- For a simple entry, the name is a selected component, with the
13173 -- prefix being the task value, and the selector being the entry.
13175 if Nkind (Nam) = N_Selected_Component then
13176 Concval := Prefix (Nam);
13177 Ename := Selector_Name (Nam);
13180 -- For a member of an entry family, the name is an indexed component
13181 -- where the prefix is a selected component, whose prefix in turn is
13182 -- the task value, and whose selector is the entry family. The single
13183 -- expression in the expressions list of the indexed component is the
13184 -- subscript for the family.
13186 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
13187 Concval := Prefix (Prefix (Nam));
13188 Ename := Selector_Name (Prefix (Nam));
13189 Index := First (Expressions (Nam));
13192 -- Through indirection, the type may actually be a limited view of a
13193 -- concurrent type. When compiling a call, the non-limited view of the
13194 -- type is visible.
13196 if From_Limited_With (Etype (Concval)) then
13197 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
13201 -------------------
13202 -- Family_Offset --
13203 -------------------
13205 function Family_Offset
13210 Cap : Boolean) return Node_Id
13216 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
13217 -- If one of the bounds is a reference to a discriminant, replace with
13218 -- corresponding discriminal of type. Within the body of a task retrieve
13219 -- the renamed discriminant by simple visibility, using its generated
13220 -- name. Within a protected object, find the original discriminant and
13221 -- replace it with the discriminal of the current protected operation.
13223 ------------------------------
13224 -- Convert_Discriminant_Ref --
13225 ------------------------------
13227 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
13228 Loc : constant Source_Ptr := Sloc (Bound);
13233 if Is_Entity_Name (Bound)
13234 and then Ekind (Entity (Bound)) = E_Discriminant
13236 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
13237 B := Make_Identifier (Loc, Chars (Entity (Bound)));
13238 Find_Direct_Name (B);
13240 elsif Is_Protected_Type (Ttyp) then
13241 D := First_Discriminant (Ttyp);
13242 while Chars (D) /= Chars (Entity (Bound)) loop
13243 Next_Discriminant (D);
13246 B := New_Occurrence_Of (Discriminal (D), Loc);
13249 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
13252 elsif Nkind (Bound) = N_Attribute_Reference then
13256 B := New_Copy_Tree (Bound);
13260 Make_Attribute_Reference (Loc,
13261 Attribute_Name => Name_Pos,
13262 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
13263 Expressions => New_List (B));
13264 end Convert_Discriminant_Ref;
13266 -- Start of processing for Family_Offset
13269 Real_Hi := Convert_Discriminant_Ref (Hi);
13270 Real_Lo := Convert_Discriminant_Ref (Lo);
13273 if Is_Task_Type (Ttyp) then
13274 Ityp := RTE (RE_Task_Entry_Index);
13276 Ityp := RTE (RE_Protected_Entry_Index);
13280 Make_Attribute_Reference (Loc,
13281 Prefix => New_Occurrence_Of (Ityp, Loc),
13282 Attribute_Name => Name_Min,
13283 Expressions => New_List (
13285 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
13288 Make_Attribute_Reference (Loc,
13289 Prefix => New_Occurrence_Of (Ityp, Loc),
13290 Attribute_Name => Name_Max,
13291 Expressions => New_List (
13293 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
13296 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
13303 function Family_Size
13308 Cap : Boolean) return Node_Id
13313 if Is_Task_Type (Ttyp) then
13314 Ityp := RTE (RE_Task_Entry_Index);
13316 Ityp := RTE (RE_Protected_Entry_Index);
13320 Make_Attribute_Reference (Loc,
13321 Prefix => New_Occurrence_Of (Ityp, Loc),
13322 Attribute_Name => Name_Max,
13323 Expressions => New_List (
13325 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13326 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13327 Make_Integer_Literal (Loc, 0)));
13330 ----------------------------
13331 -- Find_Enclosing_Context --
13332 ----------------------------
13334 procedure Find_Enclosing_Context
13336 Context : out Node_Id;
13337 Context_Id : out Entity_Id;
13338 Context_Decls : out List_Id)
13341 -- Traverse the parent chain looking for an enclosing body, block,
13342 -- package or return statement.
13344 Context := Parent (N);
13345 while Present (Context) loop
13346 if Nkind_In (Context, N_Entry_Body,
13347 N_Extended_Return_Statement,
13349 N_Package_Declaration,
13355 -- Do not consider block created to protect a list of statements with
13356 -- an Abort_Defer / Abort_Undefer_Direct pair.
13358 elsif Nkind (Context) = N_Block_Statement
13359 and then not Is_Abort_Block (Context)
13364 Context := Parent (Context);
13367 pragma Assert (Present (Context));
13369 -- Extract the constituents of the context
13371 if Nkind (Context) = N_Extended_Return_Statement then
13372 Context_Decls := Return_Object_Declarations (Context);
13373 Context_Id := Return_Statement_Entity (Context);
13375 -- Package declarations and bodies use a common library-level activation
13376 -- chain or task master, therefore return the package declaration as the
13377 -- proper carrier for the appropriate flag.
13379 elsif Nkind (Context) = N_Package_Body then
13380 Context_Decls := Declarations (Context);
13381 Context_Id := Corresponding_Spec (Context);
13382 Context := Parent (Context_Id);
13384 if Nkind (Context) = N_Defining_Program_Unit_Name then
13385 Context := Parent (Parent (Context));
13387 Context := Parent (Context);
13390 elsif Nkind (Context) = N_Package_Declaration then
13391 Context_Decls := Visible_Declarations (Specification (Context));
13392 Context_Id := Defining_Unit_Name (Specification (Context));
13394 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13395 Context_Id := Defining_Identifier (Context_Id);
13399 if Nkind (Context) = N_Block_Statement then
13400 Context_Id := Entity (Identifier (Context));
13402 elsif Nkind (Context) = N_Entry_Body then
13403 Context_Id := Defining_Identifier (Context);
13405 elsif Nkind (Context) = N_Subprogram_Body then
13406 if Present (Corresponding_Spec (Context)) then
13407 Context_Id := Corresponding_Spec (Context);
13409 Context_Id := Defining_Unit_Name (Specification (Context));
13411 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13412 Context_Id := Defining_Identifier (Context_Id);
13416 elsif Nkind (Context) = N_Task_Body then
13417 Context_Id := Corresponding_Spec (Context);
13420 raise Program_Error;
13423 Context_Decls := Declarations (Context);
13426 pragma Assert (Present (Context_Id));
13427 pragma Assert (Present (Context_Decls));
13428 end Find_Enclosing_Context;
13430 -----------------------
13431 -- Find_Master_Scope --
13432 -----------------------
13434 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13438 -- In Ada 2005, the master is the innermost enclosing scope that is not
13439 -- transient. If the enclosing block is the rewriting of a call or the
13440 -- scope is an extended return statement this is valid master. The
13441 -- master in an extended return is only used within the return, and is
13442 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13443 -- now before that overwriting occurs.
13447 if Ada_Version >= Ada_2005 then
13448 while Is_Internal (S) loop
13449 if Nkind (Parent (S)) = N_Block_Statement
13451 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
13455 elsif Ekind (S) = E_Return_Statement then
13465 end Find_Master_Scope;
13467 -------------------------------
13468 -- First_Protected_Operation --
13469 -------------------------------
13471 function First_Protected_Operation (D : List_Id) return Node_Id is
13472 First_Op : Node_Id;
13475 First_Op := First (D);
13476 while Present (First_Op)
13477 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
13483 end First_Protected_Operation;
13485 ---------------------------------------
13486 -- Install_Private_Data_Declarations --
13487 ---------------------------------------
13489 procedure Install_Private_Data_Declarations
13491 Spec_Id : Entity_Id;
13492 Conc_Typ : Entity_Id;
13493 Body_Nod : Node_Id;
13495 Barrier : Boolean := False;
13496 Family : Boolean := False)
13498 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13501 Insert_Node : Node_Id := Empty;
13502 Obj_Ent : Entity_Id;
13504 procedure Add (Decl : Node_Id);
13505 -- Add a single declaration after Insert_Node. If this is the first
13506 -- addition, Decl is added to the front of Decls and it becomes the
13509 function Replace_Bound (Bound : Node_Id) return Node_Id;
13510 -- The bounds of an entry index may depend on discriminants, create a
13511 -- reference to the corresponding prival. Otherwise return a duplicate
13512 -- of the original bound.
13518 procedure Add (Decl : Node_Id) is
13520 if No (Insert_Node) then
13521 Prepend_To (Decls, Decl);
13523 Insert_After (Insert_Node, Decl);
13526 Insert_Node := Decl;
13529 --------------------------
13530 -- Replace_Discriminant --
13531 --------------------------
13533 function Replace_Bound (Bound : Node_Id) return Node_Id is
13535 if Nkind (Bound) = N_Identifier
13536 and then Is_Discriminal (Entity (Bound))
13538 return Make_Identifier (Loc, Chars (Entity (Bound)));
13540 return Duplicate_Subexpr (Bound);
13544 -- Start of processing for Install_Private_Data_Declarations
13547 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13548 -- formal parameter _O, _object or _task depending on the context.
13550 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13552 -- Special processing of _O for barrier functions, protected entries
13559 (Ekind (Spec_Id) = E_Entry
13560 or else Ekind (Spec_Id) = E_Entry_Family))
13563 Conc_Rec : constant Entity_Id :=
13564 Corresponding_Record_Type (Conc_Typ);
13565 Typ_Id : constant Entity_Id :=
13566 Make_Defining_Identifier (Loc,
13567 New_External_Name (Chars (Conc_Rec), 'P'));
13570 -- type prot_typVP is access prot_typV;
13573 Make_Full_Type_Declaration (Loc,
13574 Defining_Identifier => Typ_Id,
13576 Make_Access_To_Object_Definition (Loc,
13577 Subtype_Indication =>
13578 New_Occurrence_Of (Conc_Rec, Loc)));
13582 -- _object : prot_typVP := prot_typV (_O);
13585 Make_Object_Declaration (Loc,
13586 Defining_Identifier =>
13587 Make_Defining_Identifier (Loc, Name_uObject),
13588 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13590 Unchecked_Convert_To (Typ_Id,
13591 New_Occurrence_Of (Obj_Ent, Loc)));
13594 -- Set the reference to the concurrent object
13596 Obj_Ent := Defining_Identifier (Decl);
13600 -- Step 2: Create the Protection object and build its declaration for
13601 -- any protected entry (family) of subprogram. Note for the lock-free
13602 -- implementation, the Protection object is not needed anymore.
13604 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13606 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13610 Set_Protection_Object (Spec_Id, Prot_Ent);
13612 -- Determine the proper protection type
13614 if Has_Attach_Handler (Conc_Typ)
13615 and then not Restricted_Profile
13617 Prot_Typ := RE_Static_Interrupt_Protection;
13619 elsif Has_Interrupt_Handler (Conc_Typ)
13620 and then not Restriction_Active (No_Dynamic_Attachment)
13622 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13625 case Corresponding_Runtime_Package (Conc_Typ) is
13626 when System_Tasking_Protected_Objects_Entries =>
13627 Prot_Typ := RE_Protection_Entries;
13629 when System_Tasking_Protected_Objects_Single_Entry =>
13630 Prot_Typ := RE_Protection_Entry;
13632 when System_Tasking_Protected_Objects =>
13633 Prot_Typ := RE_Protection;
13636 raise Program_Error;
13641 -- conc_typR : protection_typ renames _object._object;
13644 Make_Object_Renaming_Declaration (Loc,
13645 Defining_Identifier => Prot_Ent,
13647 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13649 Make_Selected_Component (Loc,
13650 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13651 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13656 -- Step 3: Add discriminant renamings (if any)
13658 if Has_Discriminants (Conc_Typ) then
13663 D := First_Discriminant (Conc_Typ);
13664 while Present (D) loop
13666 -- Adjust the source location
13668 Set_Sloc (Discriminal (D), Loc);
13671 -- discr_name : discr_typ renames _object.discr_name;
13673 -- discr_name : discr_typ renames _task.discr_name;
13676 Make_Object_Renaming_Declaration (Loc,
13677 Defining_Identifier => Discriminal (D),
13678 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13680 Make_Selected_Component (Loc,
13681 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13682 Selector_Name => Make_Identifier (Loc, Chars (D))));
13685 Next_Discriminant (D);
13690 -- Step 4: Add private component renamings (if any)
13692 if Is_Protected then
13693 Def := Protected_Definition (Parent (Conc_Typ));
13695 if Present (Private_Declarations (Def)) then
13698 Comp_Id : Entity_Id;
13699 Decl_Id : Entity_Id;
13702 Comp := First (Private_Declarations (Def));
13703 while Present (Comp) loop
13704 if Nkind (Comp) = N_Component_Declaration then
13705 Comp_Id := Defining_Identifier (Comp);
13707 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13709 -- Minimal decoration
13711 if Ekind (Spec_Id) = E_Function then
13712 Set_Ekind (Decl_Id, E_Constant);
13714 Set_Ekind (Decl_Id, E_Variable);
13717 Set_Prival (Comp_Id, Decl_Id);
13718 Set_Prival_Link (Decl_Id, Comp_Id);
13719 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13722 -- comp_name : comp_typ renames _object.comp_name;
13725 Make_Object_Renaming_Declaration (Loc,
13726 Defining_Identifier => Decl_Id,
13728 New_Occurrence_Of (Etype (Comp_Id), Loc),
13730 Make_Selected_Component (Loc,
13732 New_Occurrence_Of (Obj_Ent, Loc),
13734 Make_Identifier (Loc, Chars (Comp_Id))));
13744 -- Step 5: Add the declaration of the entry index and the associated
13745 -- type for barrier functions and entry families.
13747 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13749 E : constant Entity_Id := Index_Object (Spec_Id);
13750 Index : constant Entity_Id :=
13751 Defining_Identifier
13752 (Entry_Index_Specification
13753 (Entry_Body_Formal_Part (Body_Nod)));
13754 Index_Con : constant Entity_Id :=
13755 Make_Defining_Identifier (Loc, Chars (Index));
13757 Index_Typ : Entity_Id;
13761 -- Minimal decoration
13763 Set_Ekind (Index_Con, E_Constant);
13764 Set_Entry_Index_Constant (Index, Index_Con);
13765 Set_Discriminal_Link (Index_Con, Index);
13767 -- Retrieve the bounds of the entry family
13769 High := Type_High_Bound (Etype (Index));
13770 Low := Type_Low_Bound (Etype (Index));
13772 -- In the simple case the entry family is given by a subtype
13773 -- mark and the index constant has the same type.
13775 if Is_Entity_Name (Original_Node (
13776 Discrete_Subtype_Definition (Parent (Index))))
13778 Index_Typ := Etype (Index);
13780 -- Otherwise a new subtype declaration is required
13783 High := Replace_Bound (High);
13784 Low := Replace_Bound (Low);
13786 Index_Typ := Make_Temporary (Loc, 'J');
13789 -- subtype Jnn is <Etype of Index> range Low .. High;
13792 Make_Subtype_Declaration (Loc,
13793 Defining_Identifier => Index_Typ,
13794 Subtype_Indication =>
13795 Make_Subtype_Indication (Loc,
13797 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13799 Make_Range_Constraint (Loc,
13800 Range_Expression =>
13801 Make_Range (Loc, Low, High))));
13805 Set_Etype (Index_Con, Index_Typ);
13807 -- Create the object which designates the index:
13808 -- J : constant Jnn :=
13809 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13811 -- where Jnn is the subtype created above or the original type of
13812 -- the index, _E is a formal of the protected body subprogram and
13813 -- <index expr> is the index of the first family member.
13816 Make_Object_Declaration (Loc,
13817 Defining_Identifier => Index_Con,
13818 Constant_Present => True,
13819 Object_Definition =>
13820 New_Occurrence_Of (Index_Typ, Loc),
13823 Make_Attribute_Reference (Loc,
13825 New_Occurrence_Of (Index_Typ, Loc),
13826 Attribute_Name => Name_Val,
13828 Expressions => New_List (
13832 Make_Op_Subtract (Loc,
13833 Left_Opnd => New_Occurrence_Of (E, Loc),
13835 Entry_Index_Expression (Loc,
13836 Defining_Identifier (Body_Nod),
13840 Make_Attribute_Reference (Loc,
13842 New_Occurrence_Of (Index_Typ, Loc),
13843 Attribute_Name => Name_Pos,
13844 Expressions => New_List (
13845 Make_Attribute_Reference (Loc,
13847 New_Occurrence_Of (Index_Typ, Loc),
13848 Attribute_Name => Name_First)))))));
13852 end Install_Private_Data_Declarations;
13854 -----------------------
13855 -- Is_Exception_Safe --
13856 -----------------------
13858 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13860 function Has_Side_Effect (N : Node_Id) return Boolean;
13861 -- Return True whenever encountering a subprogram call or raise
13862 -- statement of any kind in the sequence of statements
13864 ---------------------
13865 -- Has_Side_Effect --
13866 ---------------------
13868 -- What is this doing buried two levels down in exp_ch9. It seems like a
13869 -- generally useful function, and indeed there may be code duplication
13870 -- going on here ???
13872 function Has_Side_Effect (N : Node_Id) return Boolean is
13876 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13877 -- Indicate whether N is a subprogram call or a raise statement
13879 ----------------------
13880 -- Is_Call_Or_Raise --
13881 ----------------------
13883 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13885 return Nkind_In (N, N_Procedure_Call_Statement,
13888 N_Raise_Constraint_Error,
13889 N_Raise_Program_Error,
13890 N_Raise_Storage_Error);
13891 end Is_Call_Or_Raise;
13893 -- Start of processing for Has_Side_Effect
13897 while Present (Stmt) loop
13898 if Is_Call_Or_Raise (Stmt) then
13902 -- An object declaration can also contain a function call or a
13903 -- raise statement.
13905 if Nkind (Stmt) = N_Object_Declaration then
13906 Expr := Expression (Stmt);
13908 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13917 end Has_Side_Effect;
13919 -- Start of processing for Is_Exception_Safe
13922 -- When exceptions can't be propagated, the subprogram returns normally
13924 if No_Exception_Handlers_Set then
13928 -- If the checks handled by the back end are not disabled, we cannot
13929 -- ensure that no exception will be raised.
13931 if not Access_Checks_Suppressed (Empty)
13932 or else not Discriminant_Checks_Suppressed (Empty)
13933 or else not Range_Checks_Suppressed (Empty)
13934 or else not Index_Checks_Suppressed (Empty)
13935 or else Opt.Stack_Checking_Enabled
13940 if Has_Side_Effect (First (Declarations (Subprogram)))
13943 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13949 end Is_Exception_Safe;
13951 ---------------------------------
13952 -- Is_Potentially_Large_Family --
13953 ---------------------------------
13955 function Is_Potentially_Large_Family
13956 (Base_Index : Entity_Id;
13957 Conctyp : Entity_Id;
13959 Hi : Node_Id) return Boolean
13962 return Scope (Base_Index) = Standard_Standard
13963 and then Base_Index = Base_Type (Standard_Integer)
13964 and then Has_Discriminants (Conctyp)
13966 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13968 (Denotes_Discriminant (Lo, True)
13970 Denotes_Discriminant (Hi, True));
13971 end Is_Potentially_Large_Family;
13973 -------------------------------------
13974 -- Is_Private_Primitive_Subprogram --
13975 -------------------------------------
13977 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13980 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13981 and then Is_Private_Primitive (Id);
13982 end Is_Private_Primitive_Subprogram;
13988 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13989 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13990 Formal : Entity_Id;
13993 Formal := First_Formal (Bod_Subp);
13994 while Present (Formal) loop
13996 -- Look for formal parameter _E
13998 if Chars (Formal) = Name_uE then
14002 Next_Formal (Formal);
14005 -- A protected body subprogram should always have the parameter in
14008 raise Program_Error;
14011 --------------------------------
14012 -- Make_Initialize_Protection --
14013 --------------------------------
14015 function Make_Initialize_Protection
14016 (Protect_Rec : Entity_Id) return List_Id
14018 Loc : constant Source_Ptr := Sloc (Protect_Rec);
14021 Ptyp : constant Node_Id :=
14022 Corresponding_Concurrent_Type (Protect_Rec);
14024 L : constant List_Id := New_List;
14025 Has_Entry : constant Boolean := Has_Entries (Ptyp);
14026 Prio_Type : Entity_Id;
14027 Prio_Var : Entity_Id := Empty;
14028 Restricted : constant Boolean := Restricted_Profile;
14031 -- We may need two calls to properly initialize the object, one to
14032 -- Initialize_Protection, and possibly one to Install_Handlers if we
14033 -- have a pragma Attach_Handler.
14035 -- Get protected declaration. In the case of a task type declaration,
14036 -- this is simply the parent of the protected type entity. In the single
14037 -- protected object declaration, this parent will be the implicit type,
14038 -- and we can find the corresponding single protected object declaration
14039 -- by searching forward in the declaration list in the tree.
14041 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
14042 -- of this type should have been removed during semantic analysis.
14044 Pdec := Parent (Ptyp);
14045 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
14046 N_Single_Protected_Declaration)
14051 -- Build the parameter list for the call. Note that _Init is the name
14052 -- of the formal for the object to be initialized, which is the task
14053 -- value record itself.
14057 -- For lock-free implementation, skip initializations of the Protection
14060 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14062 -- Object parameter. This is a pointer to the object of type
14063 -- Protection used by the GNARL to control the protected object.
14066 Make_Attribute_Reference (Loc,
14068 Make_Selected_Component (Loc,
14069 Prefix => Make_Identifier (Loc, Name_uInit),
14070 Selector_Name => Make_Identifier (Loc, Name_uObject)),
14071 Attribute_Name => Name_Unchecked_Access));
14073 -- Priority parameter. Set to Unspecified_Priority unless there is a
14074 -- Priority rep item, in which case we take the value from the pragma
14075 -- or attribute definition clause, or there is an Interrupt_Priority
14076 -- rep item and no Priority rep item, and we set the ceiling to
14077 -- Interrupt_Priority'Last, an implementation-defined value, see
14080 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
14082 Prio_Clause : constant Node_Id :=
14084 (Ptyp, Name_Priority, Check_Parents => False);
14091 if Nkind (Prio_Clause) = N_Pragma then
14094 (First (Pragma_Argument_Associations (Prio_Clause)));
14096 -- Get_Rep_Item returns either priority pragma.
14098 if Pragma_Name (Prio_Clause) = Name_Priority then
14099 Prio_Type := RTE (RE_Any_Priority);
14101 Prio_Type := RTE (RE_Interrupt_Priority);
14104 -- Attribute definition clause Priority
14107 if Chars (Prio_Clause) = Name_Priority then
14108 Prio_Type := RTE (RE_Any_Priority);
14110 Prio_Type := RTE (RE_Interrupt_Priority);
14113 Prio := Expression (Prio_Clause);
14116 -- Always create a locale variable to capture the priority.
14117 -- The priority is also passed to Install_Restriced_Handlers.
14118 -- Note that it is really necessary to create this variable
14119 -- explicitly. It might be thought that removing side effects
14120 -- would the appropriate approach, but that could generate
14121 -- declarations improperly placed in the enclosing scope.
14123 Prio_Var := Make_Temporary (Loc, 'R', Prio);
14125 Make_Object_Declaration (Loc,
14126 Defining_Identifier => Prio_Var,
14127 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
14128 Expression => Relocate_Node (Prio)));
14130 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14133 -- When no priority is specified but an xx_Handler pragma is, we
14134 -- default to System.Interrupts.Default_Interrupt_Priority, see
14137 elsif Has_Attach_Handler (Ptyp)
14138 or else Has_Interrupt_Handler (Ptyp)
14141 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
14143 -- Normal case, no priority or xx_Handler specified, default priority
14147 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14150 -- Test for Compiler_Info parameter. This parameter allows entry body
14151 -- procedures and barrier functions to be called from the runtime. It
14152 -- is a pointer to the record generated by the compiler to represent
14153 -- the protected object.
14155 -- A protected type without entries that covers an interface and
14156 -- overrides the abstract routines with protected procedures is
14157 -- considered equivalent to a protected type with entries in the
14158 -- context of dispatching select statements.
14160 -- Protected types with interrupt handlers (when not using a
14161 -- restricted profile) are also considered equivalent to protected
14162 -- types with entries.
14164 -- The types which are used (Static_Interrupt_Protection and
14165 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
14168 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
14170 Called_Subp : RE_Id;
14174 when System_Tasking_Protected_Objects_Entries =>
14175 Called_Subp := RE_Initialize_Protection_Entries;
14177 -- Argument Compiler_Info
14180 Make_Attribute_Reference (Loc,
14181 Prefix => Make_Identifier (Loc, Name_uInit),
14182 Attribute_Name => Name_Address));
14184 when System_Tasking_Protected_Objects_Single_Entry =>
14185 Called_Subp := RE_Initialize_Protection_Entry;
14187 -- Argument Compiler_Info
14190 Make_Attribute_Reference (Loc,
14191 Prefix => Make_Identifier (Loc, Name_uInit),
14192 Attribute_Name => Name_Address));
14194 when System_Tasking_Protected_Objects =>
14195 Called_Subp := RE_Initialize_Protection;
14198 raise Program_Error;
14201 -- Entry_Queue_Maxes parameter. This is an access to an array of
14202 -- naturals representing the entry queue maximums for each entry
14203 -- in the protected type. Zero represents no max. The access is
14204 -- null if there is no limit for all entries (usual case).
14207 and then Pkg_Id = System_Tasking_Protected_Objects_Entries
14209 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
14211 Make_Attribute_Reference (Loc,
14214 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
14215 Attribute_Name => Name_Unrestricted_Access));
14217 Append_To (Args, Make_Null (Loc));
14220 -- Edge cases exist where entry initialization functions are
14221 -- called, but no entries exist, so null is appended.
14223 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14224 Append_To (Args, Make_Null (Loc));
14227 -- Entry_Bodies parameter. This is a pointer to an array of
14228 -- pointers to the entry body procedures and barrier functions of
14229 -- the object. If the protected type has no entries this object
14230 -- will not exist, in this case, pass a null (it can happen when
14231 -- there are protected interrupt handlers or interfaces).
14234 P_Arr := Entry_Bodies_Array (Ptyp);
14236 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
14237 -- multiple entries).
14240 Make_Attribute_Reference (Loc,
14241 Prefix => New_Occurrence_Of (P_Arr, Loc),
14242 Attribute_Name => Name_Unrestricted_Access));
14244 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
14246 -- Find index mapping function (clumsy but ok for now)
14248 while Ekind (P_Arr) /= E_Function loop
14249 Next_Entity (P_Arr);
14253 Make_Attribute_Reference (Loc,
14254 Prefix => New_Occurrence_Of (P_Arr, Loc),
14255 Attribute_Name => Name_Unrestricted_Access));
14258 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
14260 -- This is the case where we have a protected object with
14261 -- interfaces and no entries, and the single entry restriction
14262 -- is in effect. We pass a null pointer for the entry
14263 -- parameter because there is no actual entry.
14265 Append_To (Args, Make_Null (Loc));
14267 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14269 -- This is the case where we have a protected object with no
14271 -- - either interrupt handlers with non restricted profile,
14273 -- Note that the types which are used for interrupt handlers
14274 -- (Static/Dynamic_Interrupt_Protection) are derived from
14275 -- Protection_Entries. We pass two null pointers because there
14276 -- is no actual entry, and the initialization procedure needs
14277 -- both Entry_Bodies and Find_Body_Index.
14279 Append_To (Args, Make_Null (Loc));
14280 Append_To (Args, Make_Null (Loc));
14284 Make_Procedure_Call_Statement (Loc,
14286 New_Occurrence_Of (RTE (Called_Subp), Loc),
14287 Parameter_Associations => Args));
14291 if Has_Attach_Handler (Ptyp) then
14293 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
14294 -- make the following call:
14296 -- Install_Handlers (_object,
14297 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14299 -- or, in the case of Ravenscar:
14301 -- Install_Restricted_Handlers
14302 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
14305 Args : constant List_Id := New_List;
14306 Table : constant List_Id := New_List;
14307 Ritem : Node_Id := First_Rep_Item (Ptyp);
14310 -- Build the Priority parameter (only for ravenscar)
14314 -- Priority comes from a pragma
14316 if Present (Prio_Var) then
14317 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14319 -- Priority is the default one
14324 (RTE (RE_Default_Interrupt_Priority), Loc));
14328 -- Build the Attach_Handler table argument
14330 while Present (Ritem) loop
14331 if Nkind (Ritem) = N_Pragma
14332 and then Pragma_Name (Ritem) = Name_Attach_Handler
14335 Handler : constant Node_Id :=
14336 First (Pragma_Argument_Associations (Ritem));
14338 Interrupt : constant Node_Id := Next (Handler);
14339 Expr : constant Node_Id := Expression (Interrupt);
14343 Make_Aggregate (Loc, Expressions => New_List (
14344 Unchecked_Convert_To
14345 (RTE (RE_System_Interrupt_Id), Expr),
14346 Make_Attribute_Reference (Loc,
14348 Make_Selected_Component (Loc,
14350 Make_Identifier (Loc, Name_uInit),
14352 Duplicate_Subexpr_No_Checks
14353 (Expression (Handler))),
14354 Attribute_Name => Name_Access))));
14358 Next_Rep_Item (Ritem);
14361 -- Append the table argument we just built
14363 Append_To (Args, Make_Aggregate (Loc, Table));
14365 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14366 -- call to the statements.
14369 -- Call a simplified version of Install_Handlers to be used
14370 -- when the Ravenscar restrictions are in effect
14371 -- (Install_Restricted_Handlers).
14374 Make_Procedure_Call_Statement (Loc,
14377 (RTE (RE_Install_Restricted_Handlers), Loc),
14378 Parameter_Associations => Args));
14381 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14383 -- First, prepends the _object argument
14386 Make_Attribute_Reference (Loc,
14388 Make_Selected_Component (Loc,
14389 Prefix => Make_Identifier (Loc, Name_uInit),
14391 Make_Identifier (Loc, Name_uObject)),
14392 Attribute_Name => Name_Unchecked_Access));
14395 -- Then, insert call to Install_Handlers
14398 Make_Procedure_Call_Statement (Loc,
14400 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14401 Parameter_Associations => Args));
14407 end Make_Initialize_Protection;
14409 ---------------------------
14410 -- Make_Task_Create_Call --
14411 ---------------------------
14413 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14414 Loc : constant Source_Ptr := Sloc (Task_Rec);
14424 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14425 Tnam := Chars (Ttyp);
14427 -- Get task declaration. In the case of a task type declaration, this is
14428 -- simply the parent of the task type entity. In the single task
14429 -- declaration, this parent will be the implicit type, and we can find
14430 -- the corresponding single task declaration by searching forward in the
14431 -- declaration list in the tree.
14433 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14434 -- this type should have been removed during semantic analysis.
14436 Tdec := Parent (Ttyp);
14437 while not Nkind_In (Tdec, N_Task_Type_Declaration,
14438 N_Single_Task_Declaration)
14443 -- Now we can find the task definition from this declaration
14445 Tdef := Task_Definition (Tdec);
14447 -- Build the parameter list for the call. Note that _Init is the name
14448 -- of the formal for the object to be initialized, which is the task
14449 -- value record itself.
14453 -- Priority parameter. Set to Unspecified_Priority unless there is a
14454 -- Priority rep item, in which case we take the value from the rep item.
14456 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14458 Make_Selected_Component (Loc,
14459 Prefix => Make_Identifier (Loc, Name_uInit),
14460 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14463 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14466 -- Optional Stack parameter
14468 if Restricted_Profile then
14470 -- If the stack has been preallocated by the expander then
14471 -- pass its address. Otherwise, pass a null address.
14473 if Preallocated_Stacks_On_Target then
14475 Make_Attribute_Reference (Loc,
14477 Make_Selected_Component (Loc,
14478 Prefix => Make_Identifier (Loc, Name_uInit),
14479 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14480 Attribute_Name => Name_Address));
14484 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14488 -- Size parameter. If no Storage_Size pragma is present, then
14489 -- the size is taken from the taskZ variable for the type, which
14490 -- is either Unspecified_Size, or has been reset by the use of
14491 -- a Storage_Size attribute definition clause. If a pragma is
14492 -- present, then the size is taken from the _Size field of the
14493 -- task value record, which was set from the pragma value.
14495 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14497 Make_Selected_Component (Loc,
14498 Prefix => Make_Identifier (Loc, Name_uInit),
14499 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14503 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14506 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14507 -- Task_Info pragma, in which case we take the value from the pragma.
14509 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14511 Make_Selected_Component (Loc,
14512 Prefix => Make_Identifier (Loc, Name_uInit),
14513 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14517 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14520 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14521 -- in which case we take the value from the rep item. The parameter is
14522 -- passed as an Integer because in the case of unspecified CPU the
14523 -- value is not in the range of CPU_Range.
14525 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14527 Convert_To (Standard_Integer,
14528 Make_Selected_Component (Loc,
14529 Prefix => Make_Identifier (Loc, Name_uInit),
14530 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14533 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14536 if not Restricted_Profile then
14538 -- Deadline parameter. If no Relative_Deadline pragma is present,
14539 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14540 -- the deadline is taken from the _Relative_Deadline field of the
14541 -- task value record, which was set from the pragma value. Note that
14542 -- this parameter must not be generated for the restricted profiles
14543 -- since Ravenscar does not allow deadlines.
14545 -- Case where pragma Relative_Deadline applies: use given value
14547 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14549 Make_Selected_Component (Loc,
14550 Prefix => Make_Identifier (Loc, Name_uInit),
14552 Make_Identifier (Loc, Name_uRelative_Deadline)));
14554 -- No pragma Relative_Deadline apply to the task
14558 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14561 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14562 -- present, then the dispatching domain is null. If a rep item is
14563 -- present, then the dispatching domain is taken from the
14564 -- _Dispatching_Domain field of the task value record, which was set
14565 -- from the rep item value.
14567 -- Case where Dispatching_Domain rep item applies: use given value
14570 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14573 Make_Selected_Component (Loc,
14575 Make_Identifier (Loc, Name_uInit),
14577 Make_Identifier (Loc, Name_uDispatching_Domain)));
14579 -- No pragma or aspect Dispatching_Domain applies to the task
14582 Append_To (Args, Make_Null (Loc));
14585 -- Number of entries. This is an expression of the form:
14587 -- n + _Init.a'Length + _Init.a'B'Length + ...
14589 -- where a,b... are the entry family names for the task definition
14592 Build_Entry_Count_Expression
14597 (Parent (Corresponding_Record_Type (Ttyp))))),
14599 Append_To (Args, Ecount);
14601 -- Master parameter. This is a reference to the _Master parameter of
14602 -- the initialization procedure, except in the case of the pragma
14603 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14604 -- System.Tasking.Library_Task_Level.
14606 if Restriction_Active (No_Task_Hierarchy) = False then
14607 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14610 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14614 -- State parameter. This is a pointer to the task body procedure. The
14615 -- required value is obtained by taking 'Unrestricted_Access of the task
14616 -- body procedure and converting it (with an unchecked conversion) to
14617 -- the type required by the task kernel. For further details, see the
14618 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14619 -- than 'Address in order to avoid creating trampolines.
14622 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14623 Subp_Ptr_Typ : constant Node_Id :=
14624 Create_Itype (E_Access_Subprogram_Type, Tdec);
14625 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14628 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14629 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14631 -- Be sure to freeze a reference to the access-to-subprogram type,
14632 -- otherwise gigi will complain that it's in the wrong scope, because
14633 -- it's actually inside the init procedure for the record type that
14634 -- corresponds to the task type.
14636 Set_Itype (Ref, Subp_Ptr_Typ);
14637 Append_Freeze_Action (Task_Rec, Ref);
14640 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14641 Make_Qualified_Expression (Loc,
14642 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14644 Make_Attribute_Reference (Loc,
14645 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14646 Attribute_Name => Name_Unrestricted_Access))));
14649 -- Discriminants parameter. This is just the address of the task
14650 -- value record itself (which contains the discriminant values
14653 Make_Attribute_Reference (Loc,
14654 Prefix => Make_Identifier (Loc, Name_uInit),
14655 Attribute_Name => Name_Address));
14657 -- Elaborated parameter. This is an access to the elaboration Boolean
14660 Make_Attribute_Reference (Loc,
14661 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14662 Attribute_Name => Name_Unchecked_Access));
14664 -- Add Chain parameter (not done for sequential elaboration policy, see
14665 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14667 if Partition_Elaboration_Policy /= 'S' then
14668 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14671 -- Task name parameter. Take this from the _Task_Id parameter to the
14672 -- init call unless there is a Task_Name pragma, in which case we take
14673 -- the value from the pragma.
14675 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14676 -- Copy expression in full, because it may be dynamic and have
14683 (Pragma_Argument_Associations
14685 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14688 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14691 -- Created_Task parameter. This is the _Task_Id field of the task
14695 Make_Selected_Component (Loc,
14696 Prefix => Make_Identifier (Loc, Name_uInit),
14697 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14703 if Restricted_Profile then
14704 if Partition_Elaboration_Policy = 'S' then
14705 Create_RE := RE_Create_Restricted_Task_Sequential;
14707 Create_RE := RE_Create_Restricted_Task;
14710 Create_RE := RE_Create_Task;
14713 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14717 Make_Procedure_Call_Statement (Loc,
14719 Parameter_Associations => Args);
14720 end Make_Task_Create_Call;
14722 ------------------------------
14723 -- Next_Protected_Operation --
14724 ------------------------------
14726 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14730 -- Check whether there is a subsequent body for a protected operation
14731 -- in the current protected body. In Ada2012 that includes expression
14732 -- functions that are completions.
14734 Next_Op := Next (N);
14735 while Present (Next_Op)
14736 and then not Nkind_In (Next_Op,
14737 N_Subprogram_Body, N_Entry_Body, N_Expression_Function)
14743 end Next_Protected_Operation;
14745 ---------------------
14746 -- Null_Statements --
14747 ---------------------
14749 function Null_Statements (Stats : List_Id) return Boolean is
14753 Stmt := First (Stats);
14754 while Nkind (Stmt) /= N_Empty
14755 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14757 (Nkind (Stmt) = N_Pragma
14759 Nam_In (Pragma_Name (Stmt), Name_Unreferenced,
14766 return Nkind (Stmt) = N_Empty;
14767 end Null_Statements;
14769 --------------------------
14770 -- Parameter_Block_Pack --
14771 --------------------------
14773 function Parameter_Block_Pack
14775 Blk_Typ : Entity_Id;
14779 Stmts : List_Id) return Node_Id
14781 Actual : Entity_Id;
14782 Expr : Node_Id := Empty;
14783 Formal : Entity_Id;
14784 Has_Param : Boolean := False;
14787 Temp_Asn : Node_Id;
14788 Temp_Nam : Node_Id;
14791 Actual := First (Actuals);
14792 Formal := Defining_Identifier (First (Formals));
14793 Params := New_List;
14794 while Present (Actual) loop
14795 if Is_By_Copy_Type (Etype (Actual)) then
14797 -- Jnn : aliased <formal-type>
14799 Temp_Nam := Make_Temporary (Loc, 'J');
14802 Make_Object_Declaration (Loc,
14803 Aliased_Present => True,
14804 Defining_Identifier => Temp_Nam,
14805 Object_Definition =>
14806 New_Occurrence_Of (Etype (Formal), Loc)));
14808 if Ekind (Formal) /= E_Out_Parameter then
14814 New_Occurrence_Of (Temp_Nam, Loc);
14816 Set_Assignment_OK (Temp_Asn);
14819 Make_Assignment_Statement (Loc,
14821 Expression => New_Copy_Tree (Actual)));
14825 -- Jnn'unchecked_access
14828 Make_Attribute_Reference (Loc,
14829 Attribute_Name => Name_Unchecked_Access,
14830 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14834 -- The controlling parameter is omitted
14837 if not Is_Controlling_Actual (Actual) then
14839 Make_Reference (Loc, New_Copy_Tree (Actual)));
14845 Next_Actual (Actual);
14846 Next_Formal_With_Extras (Formal);
14850 Expr := Make_Aggregate (Loc, Params);
14855 -- J1'unchecked_access;
14856 -- <actual2>'reference;
14859 P := Make_Temporary (Loc, 'P');
14862 Make_Object_Declaration (Loc,
14863 Defining_Identifier => P,
14864 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14865 Expression => Expr));
14868 end Parameter_Block_Pack;
14870 ----------------------------
14871 -- Parameter_Block_Unpack --
14872 ----------------------------
14874 function Parameter_Block_Unpack
14878 Formals : List_Id) return List_Id
14880 Actual : Entity_Id;
14882 Formal : Entity_Id;
14883 Has_Asnmt : Boolean := False;
14884 Result : constant List_Id := New_List;
14887 Actual := First (Actuals);
14888 Formal := Defining_Identifier (First (Formals));
14889 while Present (Actual) loop
14890 if Is_By_Copy_Type (Etype (Actual))
14891 and then Ekind (Formal) /= E_In_Parameter
14894 -- <actual> := P.<formal>;
14897 Make_Assignment_Statement (Loc,
14901 Make_Explicit_Dereference (Loc,
14902 Make_Selected_Component (Loc,
14904 New_Occurrence_Of (P, Loc),
14906 Make_Identifier (Loc, Chars (Formal)))));
14908 Set_Assignment_OK (Name (Asnmt));
14909 Append_To (Result, Asnmt);
14914 Next_Actual (Actual);
14915 Next_Formal_With_Extras (Formal);
14921 return New_List (Make_Null_Statement (Loc));
14923 end Parameter_Block_Unpack;
14925 ----------------------
14926 -- Set_Discriminals --
14927 ----------------------
14929 procedure Set_Discriminals (Dec : Node_Id) is
14932 D_Minal : Entity_Id;
14935 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14936 Pdef := Defining_Identifier (Dec);
14938 if Has_Discriminants (Pdef) then
14939 D := First_Discriminant (Pdef);
14940 while Present (D) loop
14942 Make_Defining_Identifier (Sloc (D),
14943 Chars => New_External_Name (Chars (D), 'D'));
14945 Set_Ekind (D_Minal, E_Constant);
14946 Set_Etype (D_Minal, Etype (D));
14947 Set_Scope (D_Minal, Pdef);
14948 Set_Discriminal (D, D_Minal);
14949 Set_Discriminal_Link (D_Minal, D);
14951 Next_Discriminant (D);
14954 end Set_Discriminals;
14956 -----------------------
14957 -- Trivial_Accept_OK --
14958 -----------------------
14960 function Trivial_Accept_OK return Boolean is
14962 case Opt.Task_Dispatching_Policy is
14964 -- If we have the default task dispatching policy in effect, we can
14965 -- definitely do the optimization (one way of looking at this is to
14966 -- think of the formal definition of the default policy being allowed
14967 -- to run any task it likes after a rendezvous, so even if notionally
14968 -- a full rescheduling occurs, we can say that our dispatching policy
14969 -- (i.e. the default dispatching policy) reorders the queue to be the
14970 -- same as just before the call.
14975 -- FIFO_Within_Priorities certainly does not permit this
14976 -- optimization since the Rendezvous is a scheduling action that may
14977 -- require some other task to be run.
14982 -- For now, disallow the optimization for all other policies. This
14983 -- may be over-conservative, but it is certainly not incorrect.
14989 end Trivial_Accept_OK;