t-linux64: Delete the 32-bit multilib that uses software floating point emulation.
[official-gcc.git] / gcc / ada / exp_ch9.adb
blobe0ea3219cff50e07f6362ecca774bac6ff54d357
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 9 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
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;
41 with Hostparm;
42 with Itypes; use Itypes;
43 with Namet; use Namet;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Restrict; use Restrict;
48 with Rident; use Rident;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
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
74 -- entry families of 128K should be reasonable in all cases, and is a
75 -- documented implementation restriction.
77 Entry_Family_Bound : constant Int := 2**16;
79 -----------------------
80 -- Local Subprograms --
81 -----------------------
83 function Actual_Index_Expression
84 (Sloc : Source_Ptr;
85 Ent : Entity_Id;
86 Index : Node_Id;
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
93 (Loc : Source_Ptr;
94 Conc_Typ : Entity_Id;
95 Decls : List_Id);
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
99 -- procedures.
101 procedure Add_Formal_Renamings
102 (Spec : Node_Id;
103 Decls : List_Id;
104 Ent : Entity_Id;
105 Loc : Source_Ptr);
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
119 (N : Node_Id;
120 Ent : Entity_Id;
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
126 (Loc : Source_Ptr;
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 function Build_Corresponding_Record
132 (N : Node_Id;
133 Ctyp : Node_Id;
134 Loc : Source_Ptr) return Node_Id;
135 -- Common to tasks and protected types. Copy discriminant specifications,
136 -- build record declaration. N is the type declaration, Ctyp is the
137 -- concurrent entity (task type or protected type).
139 function Build_Entry_Count_Expression
140 (Concurrent_Type : Node_Id;
141 Component_List : List_Id;
142 Loc : Source_Ptr) return Node_Id;
143 -- Compute number of entries for concurrent object. This is a count of
144 -- simple entries, followed by an expression that computes the length
145 -- of the range of each entry family. A single array with that size is
146 -- allocated for each concurrent object of the type.
148 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
149 -- Build the function that translates the entry index in the call
150 -- (which depends on the size of entry families) into an index into the
151 -- Entry_Bodies_Array, to determine the body and barrier function used
152 -- in a protected entry call. A pointer to this function appears in every
153 -- protected object.
155 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
156 -- Build subprogram declaration for previous one
158 function Build_Lock_Free_Protected_Subprogram_Body
159 (N : Node_Id;
160 Prot_Typ : Node_Id;
161 Unprot_Spec : Node_Id) return Node_Id;
162 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
163 -- the subprogram specification of the unprotected version of N. Transform
164 -- N such that it invokes the unprotected version of the body.
166 function Build_Lock_Free_Unprotected_Subprogram_Body
167 (N : Node_Id;
168 Prot_Typ : Node_Id) return Node_Id;
169 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
170 -- of N where the original statements of N are synchronized through atomic
171 -- actions such as compare and exchange. Prior to invoking this routine, it
172 -- has been established that N can be implemented in a lock-free fashion.
174 function Build_Parameter_Block
175 (Loc : Source_Ptr;
176 Actuals : List_Id;
177 Formals : List_Id;
178 Decls : List_Id) return Entity_Id;
179 -- Generate an access type for each actual parameter in the list Actuals.
180 -- Create an encapsulating record that contains all the actuals and return
181 -- its type. Generate:
182 -- type Ann1 is access all <actual1-type>
183 -- ...
184 -- type AnnN is access all <actualN-type>
185 -- type Pnn is record
186 -- <formal1> : Ann1;
187 -- ...
188 -- <formalN> : AnnN;
189 -- end record;
191 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id);
192 -- Build body of wrapper procedure for an entry or entry family that has
193 -- pre/postconditions. The body gathers the PPC's and expands them in the
194 -- usual way, and performs the entry call itself. This way preconditions
195 -- are evaluated before the call is queued. E is the entry in question,
196 -- and Decl is the enclosing synchronized type declaration at whose
197 -- freeze point the generated body is analyzed.
199 function Build_Protected_Entry
200 (N : Node_Id;
201 Ent : Entity_Id;
202 Pid : Node_Id) return Node_Id;
203 -- Build the procedure implementing the statement sequence of the specified
204 -- entry body.
206 function Build_Protected_Entry_Specification
207 (Loc : Source_Ptr;
208 Def_Id : Entity_Id;
209 Ent_Id : Entity_Id) return Node_Id;
210 -- Build a specification for the procedure implementing the statements of
211 -- the specified entry body. Add attributes associating it with the entry
212 -- defining identifier Ent_Id.
214 function Build_Protected_Spec
215 (N : Node_Id;
216 Obj_Type : Entity_Id;
217 Ident : Entity_Id;
218 Unprotected : Boolean := False) return List_Id;
219 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
220 -- Subprogram_Type. Builds signature of protected subprogram, adding the
221 -- formal that corresponds to the object itself. For an access to protected
222 -- subprogram, there is no object type to specify, so the parameter has
223 -- type Address and mode In. An indirect call through such a pointer will
224 -- convert the address to a reference to the actual object. The object is
225 -- a limited record and therefore a by_reference type.
227 function Build_Protected_Subprogram_Body
228 (N : Node_Id;
229 Pid : Node_Id;
230 N_Op_Spec : Node_Id) return Node_Id;
231 -- This function is used to construct the protected version of a protected
232 -- subprogram. Its statement sequence first defers abort, then locks
233 -- the associated protected object, and then enters a block that contains
234 -- a call to the unprotected version of the subprogram (for details, see
235 -- Build_Unprotected_Subprogram_Body). This block statement requires
236 -- a cleanup handler that unlocks the object in all cases.
237 -- (see Exp_Ch7.Expand_Cleanup_Actions).
239 function Build_Renamed_Formal_Declaration
240 (New_F : Entity_Id;
241 Formal : Entity_Id;
242 Comp : Entity_Id;
243 Renamed_Formal : Node_Id) return Node_Id;
244 -- Create a renaming declaration for a formal, within a protected entry
245 -- body or an accept body. The renamed object is a component of the
246 -- parameter block that is a parameter in the entry call.
248 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
249 -- does not dereference the corresponding component to prevent an illegal
250 -- use of the incomplete type (AI05-0151).
252 function Build_Selected_Name
253 (Prefix : Entity_Id;
254 Selector : Entity_Id;
255 Append_Char : Character := ' ') return Name_Id;
256 -- Build a name in the form of Prefix__Selector, with an optional
257 -- character appended. This is used for internal subprograms generated
258 -- for operations of protected types, including barrier functions.
259 -- For the subprograms generated for entry bodies and entry barriers,
260 -- the generated name includes a sequence number that makes names
261 -- unique in the presence of entry overloading. This is necessary
262 -- because entry body procedures and barrier functions all have the
263 -- same signature.
265 procedure Build_Simple_Entry_Call
266 (N : Node_Id;
267 Concval : Node_Id;
268 Ename : Node_Id;
269 Index : Node_Id);
270 -- Some comments here would be useful ???
272 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
273 -- This routine constructs a specification for the procedure that we will
274 -- build for the task body for task type T. The spec has the form:
276 -- procedure tnameB (_Task : access tnameV);
278 -- where name is the character name taken from the task type entity that
279 -- is passed as the argument to the procedure, and tnameV is the task
280 -- value type that is associated with the task type.
282 function Build_Unprotected_Subprogram_Body
283 (N : Node_Id;
284 Pid : Node_Id) return Node_Id;
285 -- This routine constructs the unprotected version of a protected
286 -- subprogram body, which is contains all of the code in the
287 -- original, unexpanded body. This is the version of the protected
288 -- subprogram that is called from all protected operations on the same
289 -- object, including the protected version of the same subprogram.
291 procedure Build_Wrapper_Bodies
292 (Loc : Source_Ptr;
293 Typ : Entity_Id;
294 N : Node_Id);
295 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
296 -- record of a concurrent type. N is the insertion node where all bodies
297 -- will be placed. This routine builds the bodies of the subprograms which
298 -- serve as an indirection mechanism to overriding primitives of concurrent
299 -- types, entries and protected procedures. Any new body is analyzed.
301 procedure Build_Wrapper_Specs
302 (Loc : Source_Ptr;
303 Typ : Entity_Id;
304 N : in out Node_Id);
305 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
306 -- record of a concurrent type. N is the insertion node where all specs
307 -- will be placed. This routine builds the specs of the subprograms which
308 -- serve as an indirection mechanism to overriding primitives of concurrent
309 -- types, entries and protected procedures. Any new spec is analyzed.
311 procedure Collect_Entry_Families
312 (Loc : Source_Ptr;
313 Cdecls : List_Id;
314 Current_Node : in out Node_Id;
315 Conctyp : Entity_Id);
316 -- For each entry family in a concurrent type, create an anonymous array
317 -- type of the right size, and add a component to the corresponding_record.
319 function Concurrent_Object
320 (Spec_Id : Entity_Id;
321 Conc_Typ : Entity_Id) return Entity_Id;
322 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
323 -- the entity associated with the concurrent object in the Protected_Body_
324 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
325 -- denotes formal parameter _O, _object or _task.
327 function Copy_Result_Type (Res : Node_Id) return Node_Id;
328 -- Copy the result type of a function specification, when building the
329 -- internal operation corresponding to a protected function, or when
330 -- expanding an access to protected function. If the result is an anonymous
331 -- access to subprogram itself, we need to create a new signature with the
332 -- same parameter names and the same resolved types, but with new entities
333 -- for the formals.
335 procedure Debug_Private_Data_Declarations (Decls : List_Id);
336 -- Decls is a list which may contain the declarations created by Install_
337 -- Private_Data_Declarations. All generated entities are marked as needing
338 -- debug info and debug nodes are manually generation where necessary. This
339 -- step of the expansion must to be done after private data has been moved
340 -- to its final resting scope to ensure proper visibility of debug objects.
342 procedure Extract_Dispatching_Call
343 (N : Node_Id;
344 Call_Ent : out Entity_Id;
345 Object : out Entity_Id;
346 Actuals : out List_Id;
347 Formals : out List_Id);
348 -- Given a dispatching call, extract the entity of the name of the call,
349 -- its actual dispatching object, its actual parameters and the formal
350 -- parameters of the overridden interface-level version. If the type of
351 -- the dispatching object is an access type then an explicit dereference
352 -- is returned in Object.
354 procedure Extract_Entry
355 (N : Node_Id;
356 Concval : out Node_Id;
357 Ename : out Node_Id;
358 Index : out Node_Id);
359 -- Given an entry call, returns the associated concurrent object,
360 -- the entry name, and the entry family index.
362 function Family_Offset
363 (Loc : Source_Ptr;
364 Hi : Node_Id;
365 Lo : Node_Id;
366 Ttyp : Entity_Id;
367 Cap : Boolean) return Node_Id;
368 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in
369 -- an accept statement, or the upper bound in the discrete subtype of
370 -- an entry declaration. Lo is the corresponding lower bound. Ttyp is
371 -- the concurrent type of the entry. If Cap is true, the result is
372 -- capped according to Entry_Family_Bound.
374 function Family_Size
375 (Loc : Source_Ptr;
376 Hi : Node_Id;
377 Lo : Node_Id;
378 Ttyp : Entity_Id;
379 Cap : Boolean) return Node_Id;
380 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in
381 -- a family, and handle properly the superflat case. This is equivalent
382 -- to the use of 'Length on the index type, but must use Family_Offset
383 -- to handle properly the case of bounds that depend on discriminants.
384 -- If Cap is true, the result is capped according to Entry_Family_Bound.
386 procedure Find_Enclosing_Context
387 (N : Node_Id;
388 Context : out Node_Id;
389 Context_Id : out Entity_Id;
390 Context_Decls : out List_Id);
391 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
392 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
393 -- nearest enclosing body, block, package or return statement and return
394 -- its constituents. Context is the enclosing construct, Context_Id is
395 -- the scope of Context_Id and Context_Decls is the declarative list of
396 -- Context.
398 function Find_Task_Or_Protected_Pragma
399 (T : Node_Id;
400 P : Name_Id) return Node_Id;
401 -- Searches the task or protected definition T for the first occurrence
402 -- of the pragma whose name is given by P. The caller has ensured that
403 -- the pragma is present in the task definition. A special case is that
404 -- when P is Name_uPriority, the call will also find Interrupt_Priority.
405 -- ??? Should be implemented with the rep item chain mechanism.
407 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
408 -- Given a subprogram identifier, return the entity which is associated
409 -- with the protection entry index in the Protected_Body_Subprogram or the
410 -- Task_Body_Procedure of Spec_Id. The returned entity denotes formal
411 -- parameter _E.
413 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean;
414 -- Tell whether a given subprogram cannot raise an exception
416 function Is_Potentially_Large_Family
417 (Base_Index : Entity_Id;
418 Conctyp : Entity_Id;
419 Lo : Node_Id;
420 Hi : Node_Id) return Boolean;
422 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
423 -- Determine whether Id is a function or a procedure and is marked as a
424 -- private primitive.
426 function Null_Statements (Stats : List_Id) return Boolean;
427 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
428 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as
429 -- well to still count as null. Returns True for a null sequence. The
430 -- argument is the list of statements from the DO-END sequence.
432 function Parameter_Block_Pack
433 (Loc : Source_Ptr;
434 Blk_Typ : Entity_Id;
435 Actuals : List_Id;
436 Formals : List_Id;
437 Decls : List_Id;
438 Stmts : List_Id) return Entity_Id;
439 -- Set the components of the generated parameter block with the values of
440 -- the actual parameters. Generate aliased temporaries to capture the
441 -- values for types that are passed by copy. Otherwise generate a reference
442 -- to the actual's value. Return the address of the aggregate block.
443 -- Generate:
444 -- Jnn1 : alias <formal-type1>;
445 -- Jnn1 := <actual1>;
446 -- ...
447 -- P : Blk_Typ := (
448 -- Jnn1'unchecked_access;
449 -- <actual2>'reference;
450 -- ...);
452 function Parameter_Block_Unpack
453 (Loc : Source_Ptr;
454 P : Entity_Id;
455 Actuals : List_Id;
456 Formals : List_Id) return List_Id;
457 -- Retrieve the values of the components from the parameter block and
458 -- assign then to the original actual parameters. Generate:
459 -- <actual1> := P.<formal1>;
460 -- ...
461 -- <actualN> := P.<formalN>;
463 function Trivial_Accept_OK return Boolean;
464 -- If there is no DO-END block for an accept, or if the DO-END block has
465 -- only null statements, then it is possible to do the Rendezvous with much
466 -- less overhead using the Accept_Trivial routine in the run-time library.
467 -- However, this is not always a valid optimization. Whether it is valid or
468 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
469 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
470 -- a rescheduling is required, so this optimization is not allowed. This
471 -- function returns True if the optimization is permitted.
473 -----------------------------
474 -- Actual_Index_Expression --
475 -----------------------------
477 function Actual_Index_Expression
478 (Sloc : Source_Ptr;
479 Ent : Entity_Id;
480 Index : Node_Id;
481 Tsk : Entity_Id) return Node_Id
483 Ttyp : constant Entity_Id := Etype (Tsk);
484 Expr : Node_Id;
485 Num : Node_Id;
486 Lo : Node_Id;
487 Hi : Node_Id;
488 Prev : Entity_Id;
489 S : Node_Id;
491 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
492 -- Compute difference between bounds of entry family
494 --------------------------
495 -- Actual_Family_Offset --
496 --------------------------
498 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
500 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
501 -- Replace a reference to a discriminant with a selected component
502 -- denoting the discriminant of the target task.
504 -----------------------------
505 -- Actual_Discriminant_Ref --
506 -----------------------------
508 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
509 Typ : constant Entity_Id := Etype (Bound);
510 B : Node_Id;
512 begin
513 if not Is_Entity_Name (Bound)
514 or else Ekind (Entity (Bound)) /= E_Discriminant
515 then
516 if Nkind (Bound) = N_Attribute_Reference then
517 return Bound;
518 else
519 B := New_Copy_Tree (Bound);
520 end if;
522 else
523 B :=
524 Make_Selected_Component (Sloc,
525 Prefix => New_Copy_Tree (Tsk),
526 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
528 Analyze_And_Resolve (B, Typ);
529 end if;
531 return
532 Make_Attribute_Reference (Sloc,
533 Attribute_Name => Name_Pos,
534 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
535 Expressions => New_List (B));
536 end Actual_Discriminant_Ref;
538 -- Start of processing for Actual_Family_Offset
540 begin
541 return
542 Make_Op_Subtract (Sloc,
543 Left_Opnd => Actual_Discriminant_Ref (Hi),
544 Right_Opnd => Actual_Discriminant_Ref (Lo));
545 end Actual_Family_Offset;
547 -- Start of processing for Actual_Index_Expression
549 begin
550 -- The queues of entries and entry families appear in textual order in
551 -- the associated record. The entry index is computed as the sum of the
552 -- number of queues for all entries that precede the designated one, to
553 -- which is added the index expression, if this expression denotes a
554 -- member of a family.
556 -- The following is a place holder for the count of simple entries
558 Num := Make_Integer_Literal (Sloc, 1);
560 -- We construct an expression which is a series of addition operations.
561 -- See comments in Entry_Index_Expression, which is identical in
562 -- structure.
564 if Present (Index) then
565 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
567 Expr :=
568 Make_Op_Add (Sloc,
569 Left_Opnd => Num,
571 Right_Opnd =>
572 Actual_Family_Offset (
573 Make_Attribute_Reference (Sloc,
574 Attribute_Name => Name_Pos,
575 Prefix => New_Reference_To (Base_Type (S), Sloc),
576 Expressions => New_List (Relocate_Node (Index))),
577 Type_Low_Bound (S)));
578 else
579 Expr := Num;
580 end if;
582 -- Now add lengths of preceding entries and entry families
584 Prev := First_Entity (Ttyp);
586 while Chars (Prev) /= Chars (Ent)
587 or else (Ekind (Prev) /= Ekind (Ent))
588 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
589 loop
590 if Ekind (Prev) = E_Entry then
591 Set_Intval (Num, Intval (Num) + 1);
593 elsif Ekind (Prev) = E_Entry_Family then
594 S :=
595 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
597 -- The need for the following full view retrieval stems from
598 -- this complex case of nested generics and tasking:
600 -- generic
601 -- type Formal_Index is range <>;
602 -- ...
603 -- package Outer is
604 -- type Index is private;
605 -- generic
606 -- ...
607 -- package Inner is
608 -- procedure P;
609 -- end Inner;
610 -- private
611 -- type Index is new Formal_Index range 1 .. 10;
612 -- end Outer;
614 -- package body Outer is
615 -- task type T is
616 -- entry Fam (Index); -- (2)
617 -- entry E;
618 -- end T;
619 -- package body Inner is -- (3)
620 -- procedure P is
621 -- begin
622 -- T.E; -- (1)
623 -- end P;
624 -- end Inner;
625 -- ...
627 -- We are currently building the index expression for the entry
628 -- call "T.E" (1). Part of the expansion must mention the range
629 -- of the discrete type "Index" (2) of entry family "Fam".
630 -- However only the private view of type "Index" is available to
631 -- the inner generic (3) because there was no prior mention of
632 -- the type inside "Inner". This visibility requirement is
633 -- implicit and cannot be detected during the construction of
634 -- the generic trees and needs special handling.
636 if In_Instance_Body
637 and then Is_Private_Type (S)
638 and then Present (Full_View (S))
639 then
640 S := Full_View (S);
641 end if;
643 Lo := Type_Low_Bound (S);
644 Hi := Type_High_Bound (S);
646 Expr :=
647 Make_Op_Add (Sloc,
648 Left_Opnd => Expr,
649 Right_Opnd =>
650 Make_Op_Add (Sloc,
651 Left_Opnd =>
652 Actual_Family_Offset (Hi, Lo),
653 Right_Opnd =>
654 Make_Integer_Literal (Sloc, 1)));
656 -- Other components are anonymous types to be ignored
658 else
659 null;
660 end if;
662 Next_Entity (Prev);
663 end loop;
665 return Expr;
666 end Actual_Index_Expression;
668 --------------------------
669 -- Add_Formal_Renamings --
670 --------------------------
672 procedure Add_Formal_Renamings
673 (Spec : Node_Id;
674 Decls : List_Id;
675 Ent : Entity_Id;
676 Loc : Source_Ptr)
678 Ptr : constant Entity_Id :=
679 Defining_Identifier
680 (Next (First (Parameter_Specifications (Spec))));
681 -- The name of the formal that holds the address of the parameter block
682 -- for the call.
684 Comp : Entity_Id;
685 Decl : Node_Id;
686 Formal : Entity_Id;
687 New_F : Entity_Id;
688 Renamed_Formal : Node_Id;
690 begin
691 Formal := First_Formal (Ent);
692 while Present (Formal) loop
693 Comp := Entry_Component (Formal);
694 New_F :=
695 Make_Defining_Identifier (Sloc (Formal),
696 Chars => Chars (Formal));
697 Set_Etype (New_F, Etype (Formal));
698 Set_Scope (New_F, Ent);
700 -- Now we set debug info needed on New_F even though it does not
701 -- come from source, so that the debugger will get the right
702 -- information for these generated names.
704 Set_Debug_Info_Needed (New_F);
706 if Ekind (Formal) = E_In_Parameter then
707 Set_Ekind (New_F, E_Constant);
708 else
709 Set_Ekind (New_F, E_Variable);
710 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
711 end if;
713 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
715 Renamed_Formal :=
716 Make_Selected_Component (Loc,
717 Prefix =>
718 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
719 Make_Identifier (Loc, Chars (Ptr))),
720 Selector_Name => New_Reference_To (Comp, Loc));
722 Decl :=
723 Build_Renamed_Formal_Declaration
724 (New_F, Formal, Comp, Renamed_Formal);
726 Append (Decl, Decls);
727 Set_Renamed_Object (Formal, New_F);
728 Next_Formal (Formal);
729 end loop;
730 end Add_Formal_Renamings;
732 ------------------------
733 -- Add_Object_Pointer --
734 ------------------------
736 procedure Add_Object_Pointer
737 (Loc : Source_Ptr;
738 Conc_Typ : Entity_Id;
739 Decls : List_Id)
741 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
742 Decl : Node_Id;
743 Obj_Ptr : Node_Id;
745 begin
746 -- Create the renaming declaration for the Protection object of a
747 -- protected type. _Object is used by Complete_Entry_Body.
748 -- ??? An attempt to make this a renaming was unsuccessful.
750 -- Build the entity for the access type
752 Obj_Ptr :=
753 Make_Defining_Identifier (Loc,
754 New_External_Name (Chars (Rec_Typ), 'P'));
756 -- Generate:
757 -- _object : poVP := poVP!O;
759 Decl :=
760 Make_Object_Declaration (Loc,
761 Defining_Identifier =>
762 Make_Defining_Identifier (Loc, Name_uObject),
763 Object_Definition =>
764 New_Reference_To (Obj_Ptr, Loc),
765 Expression =>
766 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
767 Set_Debug_Info_Needed (Defining_Identifier (Decl));
768 Prepend_To (Decls, Decl);
770 -- Generate:
771 -- type poVP is access poV;
773 Decl :=
774 Make_Full_Type_Declaration (Loc,
775 Defining_Identifier =>
776 Obj_Ptr,
777 Type_Definition =>
778 Make_Access_To_Object_Definition (Loc,
779 Subtype_Indication =>
780 New_Reference_To (Rec_Typ, Loc)));
781 Set_Debug_Info_Needed (Defining_Identifier (Decl));
782 Prepend_To (Decls, Decl);
783 end Add_Object_Pointer;
785 -----------------------
786 -- Build_Accept_Body --
787 -----------------------
789 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
790 Loc : constant Source_Ptr := Sloc (Astat);
791 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
792 New_S : Node_Id;
793 Hand : Node_Id;
794 Call : Node_Id;
795 Ohandle : Node_Id;
797 begin
798 -- At the end of the statement sequence, Complete_Rendezvous is called.
799 -- A label skipping the Complete_Rendezvous, and all other accept
800 -- processing, has already been added for the expansion of requeue
801 -- statements. The Sloc is copied from the last statement since it
802 -- is really part of this last statement.
804 Call :=
805 Build_Runtime_Call
806 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
807 Insert_Before (Last (Statements (Stats)), Call);
808 Analyze (Call);
810 -- If exception handlers are present, then append Complete_Rendezvous
811 -- calls to the handlers, and construct the required outer block. As
812 -- above, the Sloc is copied from the last statement in the sequence.
814 if Present (Exception_Handlers (Stats)) then
815 Hand := First (Exception_Handlers (Stats));
816 while Present (Hand) loop
817 Call :=
818 Build_Runtime_Call
819 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
820 Append (Call, Statements (Hand));
821 Analyze (Call);
822 Next (Hand);
823 end loop;
825 New_S :=
826 Make_Handled_Sequence_Of_Statements (Loc,
827 Statements => New_List (
828 Make_Block_Statement (Loc,
829 Handled_Statement_Sequence => Stats)));
831 else
832 New_S := Stats;
833 end if;
835 -- At this stage we know that the new statement sequence does not
836 -- have an exception handler part, so we supply one to call
837 -- Exceptional_Complete_Rendezvous. This handler is
839 -- when all others =>
840 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
842 -- We handle Abort_Signal to make sure that we properly catch the abort
843 -- case and wake up the caller.
845 Ohandle := Make_Others_Choice (Loc);
846 Set_All_Others (Ohandle);
848 Set_Exception_Handlers (New_S,
849 New_List (
850 Make_Implicit_Exception_Handler (Loc,
851 Exception_Choices => New_List (Ohandle),
853 Statements => New_List (
854 Make_Procedure_Call_Statement (Sloc (Stats),
855 Name => New_Reference_To (
856 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
857 Parameter_Associations => New_List (
858 Make_Function_Call (Sloc (Stats),
859 Name => New_Reference_To (
860 RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
862 Set_Parent (New_S, Astat); -- temp parent for Analyze call
863 Analyze_Exception_Handlers (Exception_Handlers (New_S));
864 Expand_Exception_Handlers (New_S);
866 -- Exceptional_Complete_Rendezvous must be called with abort
867 -- still deferred, which is the case for a "when all others" handler.
869 return New_S;
870 end Build_Accept_Body;
872 -----------------------------------
873 -- Build_Activation_Chain_Entity --
874 -----------------------------------
876 procedure Build_Activation_Chain_Entity (N : Node_Id) is
877 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
878 -- Determine whether an extended return statement has an activation
879 -- chain.
881 --------------------------
882 -- Has_Activation_Chain --
883 --------------------------
885 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
886 Decl : Node_Id;
888 begin
889 Decl := First (Return_Object_Declarations (Stmt));
890 while Present (Decl) loop
891 if Nkind (Decl) = N_Object_Declaration
892 and then Chars (Defining_Identifier (Decl)) = Name_uChain
893 then
894 return True;
895 end if;
897 Next (Decl);
898 end loop;
900 return False;
901 end Has_Activation_Chain;
903 -- Local variables
905 Context : Node_Id;
906 Context_Id : Entity_Id;
907 Decls : List_Id;
909 -- Start of processing for Build_Activation_Chain_Entity
911 begin
912 Find_Enclosing_Context (N, Context, Context_Id, Decls);
914 -- If an activation chain entity has not been declared already, create
915 -- one.
917 if Nkind (Context) = N_Extended_Return_Statement
918 or else No (Activation_Chain_Entity (Context))
919 then
920 -- Since extended return statements do not store the entity of the
921 -- chain, examine the return object declarations to avoid creating
922 -- a duplicate.
924 if Nkind (Context) = N_Extended_Return_Statement
925 and then Has_Activation_Chain (Context)
926 then
927 return;
928 end if;
930 declare
931 Loc : constant Source_Ptr := Sloc (Context);
932 Chain : Entity_Id;
933 Decl : Node_Id;
935 begin
936 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
938 -- Note: An extended return statement is not really a task
939 -- activator, but it does have an activation chain on which to
940 -- store the tasks temporarily. On successful return, the tasks
941 -- on this chain are moved to the chain passed in by the caller.
942 -- We do not build an Activation_Chain_Entity for an extended
943 -- return statement, because we do not want to build a call to
944 -- Activate_Tasks. Task activation is the responsibility of the
945 -- caller.
947 if Nkind (Context) /= N_Extended_Return_Statement then
948 Set_Activation_Chain_Entity (Context, Chain);
949 end if;
951 Decl :=
952 Make_Object_Declaration (Loc,
953 Defining_Identifier => Chain,
954 Aliased_Present => True,
955 Object_Definition =>
956 New_Reference_To (RTE (RE_Activation_Chain), Loc));
958 Prepend_To (Decls, Decl);
960 -- Ensure that the _chain appears in the proper scope of the
961 -- context.
963 if Context_Id /= Current_Scope then
964 Push_Scope (Context_Id);
965 Analyze (Decl);
966 Pop_Scope;
967 else
968 Analyze (Decl);
969 end if;
970 end;
971 end if;
972 end Build_Activation_Chain_Entity;
974 ----------------------------
975 -- Build_Barrier_Function --
976 ----------------------------
978 function Build_Barrier_Function
979 (N : Node_Id;
980 Ent : Entity_Id;
981 Pid : Node_Id) return Node_Id
983 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
984 Cond : constant Node_Id := Condition (Ent_Formals);
985 Loc : constant Source_Ptr := Sloc (Cond);
986 Func_Id : constant Entity_Id := Barrier_Function (Ent);
987 Op_Decls : constant List_Id := New_List;
988 Stmt : Node_Id;
989 Func_Body : Node_Id;
991 begin
992 -- Add a declaration for the Protection object, renaming declarations
993 -- for the discriminals and privals and finally a declaration for the
994 -- entry family index (if applicable).
996 Install_Private_Data_Declarations (Sloc (N),
997 Spec_Id => Func_Id,
998 Conc_Typ => Pid,
999 Body_Nod => N,
1000 Decls => Op_Decls,
1001 Barrier => True,
1002 Family => Ekind (Ent) = E_Entry_Family);
1004 -- If compiling with -fpreserve-control-flow, make sure we insert an
1005 -- IF statement so that the back-end knows to generate a conditional
1006 -- branch instruction, even if the condition is just the name of a
1007 -- boolean object.
1009 if Opt.Suppress_Control_Flow_Optimizations then
1010 Stmt := Make_Implicit_If_Statement (Cond,
1011 Condition => Cond,
1012 Then_Statements => New_List (
1013 Make_Simple_Return_Statement (Loc,
1014 New_Occurrence_Of (Standard_True, Loc))),
1015 Else_Statements => New_List (
1016 Make_Simple_Return_Statement (Loc,
1017 New_Occurrence_Of (Standard_False, Loc))));
1019 else
1020 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1021 end if;
1023 -- Note: the condition in the barrier function needs to be properly
1024 -- processed for the C/Fortran boolean possibility, but this happens
1025 -- automatically since the return statement does this normalization.
1027 Func_Body :=
1028 Make_Subprogram_Body (Loc,
1029 Specification =>
1030 Build_Barrier_Function_Specification (Loc,
1031 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1032 Declarations => Op_Decls,
1033 Handled_Statement_Sequence =>
1034 Make_Handled_Sequence_Of_Statements (Loc,
1035 Statements => New_List (Stmt)));
1036 Set_Is_Entry_Barrier_Function (Func_Body);
1038 return Func_Body;
1039 end Build_Barrier_Function;
1041 ------------------------------------------
1042 -- Build_Barrier_Function_Specification --
1043 ------------------------------------------
1045 function Build_Barrier_Function_Specification
1046 (Loc : Source_Ptr;
1047 Def_Id : Entity_Id) return Node_Id
1049 begin
1050 Set_Debug_Info_Needed (Def_Id);
1052 return Make_Function_Specification (Loc,
1053 Defining_Unit_Name => Def_Id,
1054 Parameter_Specifications => New_List (
1055 Make_Parameter_Specification (Loc,
1056 Defining_Identifier =>
1057 Make_Defining_Identifier (Loc, Name_uO),
1058 Parameter_Type =>
1059 New_Reference_To (RTE (RE_Address), Loc)),
1061 Make_Parameter_Specification (Loc,
1062 Defining_Identifier =>
1063 Make_Defining_Identifier (Loc, Name_uE),
1064 Parameter_Type =>
1065 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
1067 Result_Definition =>
1068 New_Reference_To (Standard_Boolean, Loc));
1069 end Build_Barrier_Function_Specification;
1071 --------------------------
1072 -- Build_Call_With_Task --
1073 --------------------------
1075 function Build_Call_With_Task
1076 (N : Node_Id;
1077 E : Entity_Id) return Node_Id
1079 Loc : constant Source_Ptr := Sloc (N);
1080 begin
1081 return
1082 Make_Function_Call (Loc,
1083 Name => New_Reference_To (E, Loc),
1084 Parameter_Associations => New_List (Concurrent_Ref (N)));
1085 end Build_Call_With_Task;
1087 -----------------------------
1088 -- Build_Class_Wide_Master --
1089 -----------------------------
1091 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1092 Loc : constant Source_Ptr := Sloc (Typ);
1093 Master_Id : Entity_Id;
1094 Master_Scope : Entity_Id;
1095 Name_Id : Node_Id;
1096 Related_Node : Node_Id;
1097 Ren_Decl : Node_Id;
1099 begin
1100 -- Nothing to do if there is no task hierarchy
1102 if Restriction_Active (No_Task_Hierarchy) then
1103 return;
1104 end if;
1106 -- Find the declaration that created the access type. It is either a
1107 -- type declaration, or an object declaration with an access definition,
1108 -- in which case the type is anonymous.
1110 if Is_Itype (Typ) then
1111 Related_Node := Associated_Node_For_Itype (Typ);
1112 else
1113 Related_Node := Parent (Typ);
1114 end if;
1116 Master_Scope := Find_Master_Scope (Typ);
1118 -- Nothing to do if the master scope already contains a _master entity.
1119 -- The only exception to this is the following scenario:
1121 -- Source_Scope
1122 -- Transient_Scope_1
1123 -- _master
1125 -- Transient_Scope_2
1126 -- use of master
1128 -- In this case the source scope is marked as having the master entity
1129 -- even though the actual declaration appears inside an inner scope. If
1130 -- the second transient scope requires a _master, it cannot use the one
1131 -- already declared because the entity is not visible.
1133 Name_Id := Make_Identifier (Loc, Name_uMaster);
1135 if not Has_Master_Entity (Master_Scope)
1136 or else No (Current_Entity_In_Scope (Name_Id))
1137 then
1138 declare
1139 Master_Decl : Node_Id;
1141 begin
1142 Set_Has_Master_Entity (Master_Scope);
1144 -- Generate:
1145 -- _master : constant Integer := Current_Master.all;
1147 Master_Decl :=
1148 Make_Object_Declaration (Loc,
1149 Defining_Identifier =>
1150 Make_Defining_Identifier (Loc, Name_uMaster),
1151 Constant_Present => True,
1152 Object_Definition =>
1153 New_Reference_To (Standard_Integer, Loc),
1154 Expression =>
1155 Make_Explicit_Dereference (Loc,
1156 New_Reference_To (RTE (RE_Current_Master), Loc)));
1158 Insert_Action (Related_Node, Master_Decl);
1159 Analyze (Master_Decl);
1161 -- Mark the containing scope as a task master. Masters associated
1162 -- with return statements are already marked at this stage (see
1163 -- Analyze_Subprogram_Body).
1165 if Ekind (Current_Scope) /= E_Return_Statement then
1166 declare
1167 Par : Node_Id := Related_Node;
1169 begin
1170 while Nkind (Par) /= N_Compilation_Unit loop
1171 Par := Parent (Par);
1173 -- If we fall off the top, we are at the outer level, and
1174 -- the environment task is our effective master, so
1175 -- nothing to mark.
1177 if Nkind_In (Par, N_Block_Statement,
1178 N_Subprogram_Body,
1179 N_Task_Body)
1180 then
1181 Set_Is_Task_Master (Par);
1182 exit;
1183 end if;
1184 end loop;
1185 end;
1186 end if;
1187 end;
1188 end if;
1190 Master_Id :=
1191 Make_Defining_Identifier (Loc,
1192 New_External_Name (Chars (Typ), 'M'));
1194 -- Generate:
1195 -- Mnn renames _master;
1197 Ren_Decl :=
1198 Make_Object_Renaming_Declaration (Loc,
1199 Defining_Identifier => Master_Id,
1200 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
1201 Name => Name_Id);
1203 Insert_Action (Related_Node, Ren_Decl);
1205 Set_Master_Id (Typ, Master_Id);
1206 end Build_Class_Wide_Master;
1208 --------------------------------
1209 -- Build_Corresponding_Record --
1210 --------------------------------
1212 function Build_Corresponding_Record
1213 (N : Node_Id;
1214 Ctyp : Entity_Id;
1215 Loc : Source_Ptr) return Node_Id
1217 Rec_Ent : constant Entity_Id :=
1218 Make_Defining_Identifier
1219 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1220 Disc : Entity_Id;
1221 Dlist : List_Id;
1222 New_Disc : Entity_Id;
1223 Cdecls : List_Id;
1225 begin
1226 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1227 Set_Ekind (Rec_Ent, E_Record_Type);
1228 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1229 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1230 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1231 Set_Stored_Constraint (Rec_Ent, No_Elist);
1232 Cdecls := New_List;
1234 -- Use discriminals to create list of discriminants for record, and
1235 -- create new discriminals for use in default expressions, etc. It is
1236 -- worth noting that a task discriminant gives rise to 5 entities;
1238 -- a) The original discriminant.
1239 -- b) The discriminal for use in the task.
1240 -- c) The discriminant of the corresponding record.
1241 -- d) The discriminal for the init proc of the corresponding record.
1242 -- e) The local variable that renames the discriminant in the procedure
1243 -- for the task body.
1245 -- In fact the discriminals b) are used in the renaming declarations
1246 -- for e). See details in einfo (Handling of Discriminants).
1248 if Present (Discriminant_Specifications (N)) then
1249 Dlist := New_List;
1250 Disc := First_Discriminant (Ctyp);
1252 while Present (Disc) loop
1253 New_Disc := CR_Discriminant (Disc);
1255 Append_To (Dlist,
1256 Make_Discriminant_Specification (Loc,
1257 Defining_Identifier => New_Disc,
1258 Discriminant_Type =>
1259 New_Occurrence_Of (Etype (Disc), Loc),
1260 Expression =>
1261 New_Copy (Discriminant_Default_Value (Disc))));
1263 Next_Discriminant (Disc);
1264 end loop;
1266 else
1267 Dlist := No_List;
1268 end if;
1270 -- Now we can construct the record type declaration. Note that this
1271 -- record is "limited tagged". It is "limited" to reflect the underlying
1272 -- limitedness of the task or protected object that it represents, and
1273 -- ensuring for example that it is properly passed by reference. It is
1274 -- "tagged" to give support to dispatching calls through interfaces. We
1275 -- propagate here the list of interfaces covered by the concurrent type
1276 -- (Ada 2005: AI-345).
1278 return
1279 Make_Full_Type_Declaration (Loc,
1280 Defining_Identifier => Rec_Ent,
1281 Discriminant_Specifications => Dlist,
1282 Type_Definition =>
1283 Make_Record_Definition (Loc,
1284 Component_List =>
1285 Make_Component_List (Loc,
1286 Component_Items => Cdecls),
1287 Tagged_Present =>
1288 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1289 Interface_List => Interface_List (N),
1290 Limited_Present => True));
1291 end Build_Corresponding_Record;
1293 ----------------------------------
1294 -- Build_Entry_Count_Expression --
1295 ----------------------------------
1297 function Build_Entry_Count_Expression
1298 (Concurrent_Type : Node_Id;
1299 Component_List : List_Id;
1300 Loc : Source_Ptr) return Node_Id
1302 Eindx : Nat;
1303 Ent : Entity_Id;
1304 Ecount : Node_Id;
1305 Comp : Node_Id;
1306 Lo : Node_Id;
1307 Hi : Node_Id;
1308 Typ : Entity_Id;
1309 Large : Boolean;
1311 begin
1312 -- Count number of non-family entries
1314 Eindx := 0;
1315 Ent := First_Entity (Concurrent_Type);
1316 while Present (Ent) loop
1317 if Ekind (Ent) = E_Entry then
1318 Eindx := Eindx + 1;
1319 end if;
1321 Next_Entity (Ent);
1322 end loop;
1324 Ecount := Make_Integer_Literal (Loc, Eindx);
1326 -- Loop through entry families building the addition nodes
1328 Ent := First_Entity (Concurrent_Type);
1329 Comp := First (Component_List);
1330 while Present (Ent) loop
1331 if Ekind (Ent) = E_Entry_Family then
1332 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1333 Next (Comp);
1334 end loop;
1336 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1337 Hi := Type_High_Bound (Typ);
1338 Lo := Type_Low_Bound (Typ);
1339 Large := Is_Potentially_Large_Family
1340 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1341 Ecount :=
1342 Make_Op_Add (Loc,
1343 Left_Opnd => Ecount,
1344 Right_Opnd => Family_Size
1345 (Loc, Hi, Lo, Concurrent_Type, Large));
1346 end if;
1348 Next_Entity (Ent);
1349 end loop;
1351 return Ecount;
1352 end Build_Entry_Count_Expression;
1354 -----------------------
1355 -- Build_Entry_Names --
1356 -----------------------
1358 function Build_Entry_Names (Conc_Typ : Entity_Id) return Node_Id is
1359 Loc : constant Source_Ptr := Sloc (Conc_Typ);
1360 B_Decls : List_Id;
1361 B_Stmts : List_Id;
1362 Comp : Node_Id;
1363 Index : Entity_Id;
1364 Index_Typ : RE_Id;
1365 Typ : Entity_Id := Conc_Typ;
1367 procedure Build_Entry_Family_Name (Id : Entity_Id);
1368 -- Generate:
1369 -- for Lnn in Family_Low .. Family_High loop
1370 -- Inn := Inn + 1;
1371 -- Set_Entry_Name
1372 -- (_init._object <or> _init._task_id,
1373 -- Inn,
1374 -- new String ("<Entry name>(" & Lnn'Img & ")"));
1375 -- end loop;
1376 -- Note that the bounds of the range may reference discriminants. The
1377 -- above construct is added directly to the statements of the block.
1379 procedure Build_Entry_Name (Id : Entity_Id);
1380 -- Generate:
1381 -- Inn := Inn + 1;
1382 -- Set_Entry_Name
1383 -- (_init._object <or>_init._task_id,
1384 -- Inn,
1385 -- new String ("<Entry name>");
1386 -- The above construct is added directly to the statements of the block.
1388 function Build_Set_Entry_Name_Call (Arg3 : Node_Id) return Node_Id;
1389 -- Generate the call to the runtime routine Set_Entry_Name with actuals
1390 -- _init._task_id or _init._object, Inn and Arg3.
1392 procedure Increment_Index (Stmts : List_Id);
1393 -- Generate the following and add it to Stmts
1394 -- Inn := Inn + 1;
1396 -----------------------------
1397 -- Build_Entry_Family_Name --
1398 -----------------------------
1400 procedure Build_Entry_Family_Name (Id : Entity_Id) is
1401 Def : constant Node_Id :=
1402 Discrete_Subtype_Definition (Parent (Id));
1403 L_Id : constant Entity_Id := Make_Temporary (Loc, 'L');
1404 L_Stmts : constant List_Id := New_List;
1405 Val : Node_Id;
1407 function Build_Range (Def : Node_Id) return Node_Id;
1408 -- Given a discrete subtype definition of an entry family, generate a
1409 -- range node which covers the range of Def's type.
1411 -----------------
1412 -- Build_Range --
1413 -----------------
1415 function Build_Range (Def : Node_Id) return Node_Id is
1416 High : Node_Id := Type_High_Bound (Etype (Def));
1417 Low : Node_Id := Type_Low_Bound (Etype (Def));
1419 begin
1420 -- If a bound references a discriminant, generate an identifier
1421 -- with the same name. Resolution will map it to the formals of
1422 -- the init proc.
1424 if Is_Entity_Name (Low)
1425 and then Ekind (Entity (Low)) = E_Discriminant
1426 then
1427 Low := Make_Identifier (Loc, Chars (Low));
1428 else
1429 Low := New_Copy_Tree (Low);
1430 end if;
1432 if Is_Entity_Name (High)
1433 and then Ekind (Entity (High)) = E_Discriminant
1434 then
1435 High := Make_Identifier (Loc, Chars (High));
1436 else
1437 High := New_Copy_Tree (High);
1438 end if;
1440 return
1441 Make_Range (Loc,
1442 Low_Bound => Low,
1443 High_Bound => High);
1444 end Build_Range;
1446 -- Start of processing for Build_Entry_Family_Name
1448 begin
1449 Get_Name_String (Chars (Id));
1451 -- Add a leading '('
1453 Add_Char_To_Name_Buffer ('(');
1455 -- Generate:
1456 -- new String'("<Entry name>(" & Lnn'Img & ")");
1458 -- This is an implicit heap allocation, and Comes_From_Source is
1459 -- False, which ensures that it will get flagged as a violation of
1460 -- No_Implicit_Heap_Allocations when that restriction applies.
1462 Val :=
1463 Make_Allocator (Loc,
1464 Make_Qualified_Expression (Loc,
1465 Subtype_Mark =>
1466 New_Reference_To (Standard_String, Loc),
1467 Expression =>
1468 Make_Op_Concat (Loc,
1469 Left_Opnd =>
1470 Make_Op_Concat (Loc,
1471 Left_Opnd =>
1472 Make_String_Literal (Loc,
1473 Strval => String_From_Name_Buffer),
1474 Right_Opnd =>
1475 Make_Attribute_Reference (Loc,
1476 Prefix =>
1477 New_Reference_To (L_Id, Loc),
1478 Attribute_Name => Name_Img)),
1479 Right_Opnd =>
1480 Make_String_Literal (Loc,
1481 Strval => ")"))));
1483 Increment_Index (L_Stmts);
1484 Append_To (L_Stmts, Build_Set_Entry_Name_Call (Val));
1486 -- Generate:
1487 -- for Lnn in Family_Low .. Family_High loop
1488 -- Inn := Inn + 1;
1489 -- Set_Entry_Name
1490 -- (_init._object <or> _init._task_id, Inn, <Val>);
1491 -- end loop;
1493 Append_To (B_Stmts,
1494 Make_Loop_Statement (Loc,
1495 Iteration_Scheme =>
1496 Make_Iteration_Scheme (Loc,
1497 Loop_Parameter_Specification =>
1498 Make_Loop_Parameter_Specification (Loc,
1499 Defining_Identifier => L_Id,
1500 Discrete_Subtype_Definition => Build_Range (Def))),
1501 Statements => L_Stmts,
1502 End_Label => Empty));
1503 end Build_Entry_Family_Name;
1505 ----------------------
1506 -- Build_Entry_Name --
1507 ----------------------
1509 procedure Build_Entry_Name (Id : Entity_Id) is
1510 Val : Node_Id;
1512 begin
1513 Get_Name_String (Chars (Id));
1515 -- This is an implicit heap allocation, and Comes_From_Source is
1516 -- False, which ensures that it will get flagged as a violation of
1517 -- No_Implicit_Heap_Allocations when that restriction applies.
1519 Val :=
1520 Make_Allocator (Loc,
1521 Make_Qualified_Expression (Loc,
1522 Subtype_Mark =>
1523 New_Reference_To (Standard_String, Loc),
1524 Expression =>
1525 Make_String_Literal (Loc,
1526 String_From_Name_Buffer)));
1528 Increment_Index (B_Stmts);
1529 Append_To (B_Stmts, Build_Set_Entry_Name_Call (Val));
1530 end Build_Entry_Name;
1532 -------------------------------
1533 -- Build_Set_Entry_Name_Call --
1534 -------------------------------
1536 function Build_Set_Entry_Name_Call (Arg3 : Node_Id) return Node_Id is
1537 Arg1 : Name_Id;
1538 Proc : RE_Id;
1540 begin
1541 -- Determine the proper name for the first argument and the RTS
1542 -- routine to call.
1544 if Is_Protected_Type (Typ) then
1545 Arg1 := Name_uObject;
1546 Proc := RO_PE_Set_Entry_Name;
1548 else pragma Assert (Is_Task_Type (Typ));
1549 Arg1 := Name_uTask_Id;
1550 Proc := RO_TS_Set_Entry_Name;
1551 end if;
1553 -- Generate:
1554 -- Set_Entry_Name (_init.Arg1, Inn, Arg3);
1556 return
1557 Make_Procedure_Call_Statement (Loc,
1558 Name =>
1559 New_Reference_To (RTE (Proc), Loc),
1560 Parameter_Associations => New_List (
1561 Make_Selected_Component (Loc, -- _init._object
1562 Prefix => -- _init._task_id
1563 Make_Identifier (Loc, Name_uInit),
1564 Selector_Name =>
1565 Make_Identifier (Loc, Arg1)),
1566 New_Reference_To (Index, Loc), -- Inn
1567 Arg3)); -- Val
1568 end Build_Set_Entry_Name_Call;
1570 ---------------------
1571 -- Increment_Index --
1572 ---------------------
1574 procedure Increment_Index (Stmts : List_Id) is
1575 begin
1576 -- Generate:
1577 -- Inn := Inn + 1;
1579 Append_To (Stmts,
1580 Make_Assignment_Statement (Loc,
1581 Name =>
1582 New_Reference_To (Index, Loc),
1583 Expression =>
1584 Make_Op_Add (Loc,
1585 Left_Opnd =>
1586 New_Reference_To (Index, Loc),
1587 Right_Opnd =>
1588 Make_Integer_Literal (Loc, 1))));
1589 end Increment_Index;
1591 -- Start of processing for Build_Entry_Names
1593 begin
1594 -- Retrieve the original concurrent type
1596 if Is_Concurrent_Record_Type (Typ) then
1597 Typ := Corresponding_Concurrent_Type (Typ);
1598 end if;
1600 pragma Assert (Is_Protected_Type (Typ) or else Is_Task_Type (Typ));
1602 -- Nothing to do if the type has no entries
1604 if not Has_Entries (Typ) then
1605 return Empty;
1606 end if;
1608 -- Avoid generating entry names for a protected type with only one entry
1610 if Is_Protected_Type (Typ)
1611 and then Find_Protection_Type (Typ) /= RTE (RE_Protection_Entries)
1612 then
1613 return Empty;
1614 end if;
1616 Index := Make_Temporary (Loc, 'I');
1618 -- Step 1: Generate the declaration of the index variable:
1619 -- Inn : Protected_Entry_Index := 0;
1620 -- or
1621 -- Inn : Task_Entry_Index := 0;
1623 if Is_Protected_Type (Typ) then
1624 Index_Typ := RE_Protected_Entry_Index;
1625 else
1626 Index_Typ := RE_Task_Entry_Index;
1627 end if;
1629 B_Decls := New_List;
1630 Append_To (B_Decls,
1631 Make_Object_Declaration (Loc,
1632 Defining_Identifier => Index,
1633 Object_Definition => New_Reference_To (RTE (Index_Typ), Loc),
1634 Expression => Make_Integer_Literal (Loc, 0)));
1636 B_Stmts := New_List;
1638 -- Step 2: Generate a call to Set_Entry_Name for each entry and entry
1639 -- family member.
1641 Comp := First_Entity (Typ);
1642 while Present (Comp) loop
1643 if Ekind (Comp) = E_Entry then
1644 Build_Entry_Name (Comp);
1646 elsif Ekind (Comp) = E_Entry_Family then
1647 Build_Entry_Family_Name (Comp);
1648 end if;
1650 Next_Entity (Comp);
1651 end loop;
1653 -- Step 3: Wrap the statements in a block
1655 return
1656 Make_Block_Statement (Loc,
1657 Declarations => B_Decls,
1658 Handled_Statement_Sequence =>
1659 Make_Handled_Sequence_Of_Statements (Loc,
1660 Statements => B_Stmts));
1661 end Build_Entry_Names;
1663 ---------------------------
1664 -- Build_Parameter_Block --
1665 ---------------------------
1667 function Build_Parameter_Block
1668 (Loc : Source_Ptr;
1669 Actuals : List_Id;
1670 Formals : List_Id;
1671 Decls : List_Id) return Entity_Id
1673 Actual : Entity_Id;
1674 Comp_Nam : Node_Id;
1675 Comps : List_Id;
1676 Formal : Entity_Id;
1677 Has_Comp : Boolean := False;
1678 Rec_Nam : Node_Id;
1680 begin
1681 Actual := First (Actuals);
1682 Comps := New_List;
1683 Formal := Defining_Identifier (First (Formals));
1685 while Present (Actual) loop
1686 if not Is_Controlling_Actual (Actual) then
1688 -- Generate:
1689 -- type Ann is access all <actual-type>
1691 Comp_Nam := Make_Temporary (Loc, 'A');
1693 Append_To (Decls,
1694 Make_Full_Type_Declaration (Loc,
1695 Defining_Identifier => Comp_Nam,
1696 Type_Definition =>
1697 Make_Access_To_Object_Definition (Loc,
1698 All_Present => True,
1699 Constant_Present => Ekind (Formal) = E_In_Parameter,
1700 Subtype_Indication =>
1701 New_Reference_To (Etype (Actual), Loc))));
1703 -- Generate:
1704 -- Param : Ann;
1706 Append_To (Comps,
1707 Make_Component_Declaration (Loc,
1708 Defining_Identifier =>
1709 Make_Defining_Identifier (Loc, Chars (Formal)),
1710 Component_Definition =>
1711 Make_Component_Definition (Loc,
1712 Aliased_Present =>
1713 False,
1714 Subtype_Indication =>
1715 New_Reference_To (Comp_Nam, Loc))));
1717 Has_Comp := True;
1718 end if;
1720 Next_Actual (Actual);
1721 Next_Formal_With_Extras (Formal);
1722 end loop;
1724 Rec_Nam := Make_Temporary (Loc, 'P');
1726 if Has_Comp then
1728 -- Generate:
1729 -- type Pnn is record
1730 -- Param1 : Ann1;
1731 -- ...
1732 -- ParamN : AnnN;
1734 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1735 -- the original parameter names and Ann1 .. AnnN are the access to
1736 -- actual types.
1738 Append_To (Decls,
1739 Make_Full_Type_Declaration (Loc,
1740 Defining_Identifier =>
1741 Rec_Nam,
1742 Type_Definition =>
1743 Make_Record_Definition (Loc,
1744 Component_List =>
1745 Make_Component_List (Loc, Comps))));
1746 else
1747 -- Generate:
1748 -- type Pnn is null record;
1750 Append_To (Decls,
1751 Make_Full_Type_Declaration (Loc,
1752 Defining_Identifier =>
1753 Rec_Nam,
1754 Type_Definition =>
1755 Make_Record_Definition (Loc,
1756 Null_Present => True,
1757 Component_List => Empty)));
1758 end if;
1760 return Rec_Nam;
1761 end Build_Parameter_Block;
1763 --------------------------------------
1764 -- Build_Renamed_Formal_Declaration --
1765 --------------------------------------
1767 function Build_Renamed_Formal_Declaration
1768 (New_F : Entity_Id;
1769 Formal : Entity_Id;
1770 Comp : Entity_Id;
1771 Renamed_Formal : Node_Id) return Node_Id
1773 Loc : constant Source_Ptr := Sloc (New_F);
1774 Decl : Node_Id;
1776 begin
1777 -- If the formal is a tagged incomplete type, it is already passed
1778 -- by reference, so it is sufficient to rename the pointer component
1779 -- that corresponds to the actual. Otherwise we need to dereference
1780 -- the pointer component to obtain the actual.
1782 if Is_Incomplete_Type (Etype (Formal))
1783 and then Is_Tagged_Type (Etype (Formal))
1784 then
1785 Decl :=
1786 Make_Object_Renaming_Declaration (Loc,
1787 Defining_Identifier => New_F,
1788 Subtype_Mark => New_Reference_To (Etype (Comp), Loc),
1789 Name => Renamed_Formal);
1791 else
1792 Decl :=
1793 Make_Object_Renaming_Declaration (Loc,
1794 Defining_Identifier => New_F,
1795 Subtype_Mark => New_Reference_To (Etype (Formal), Loc),
1796 Name =>
1797 Make_Explicit_Dereference (Loc, Renamed_Formal));
1798 end if;
1800 return Decl;
1801 end Build_Renamed_Formal_Declaration;
1803 -----------------------
1804 -- Build_PPC_Wrapper --
1805 -----------------------
1807 procedure Build_PPC_Wrapper (E : Entity_Id; Decl : Node_Id) is
1808 Loc : constant Source_Ptr := Sloc (E);
1809 Synch_Type : constant Entity_Id := Scope (E);
1811 Wrapper_Id : constant Entity_Id :=
1812 Make_Defining_Identifier (Loc,
1813 Chars => New_External_Name (Chars (E), 'E'));
1814 -- the wrapper procedure name
1816 Wrapper_Body : Node_Id;
1818 Synch_Id : constant Entity_Id :=
1819 Make_Defining_Identifier (Loc,
1820 Chars => New_External_Name (Chars (Scope (E)), 'A'));
1821 -- The parameter that designates the synchronized object in the call
1823 Actuals : constant List_Id := New_List;
1824 -- The actuals in the entry call
1826 Decls : constant List_Id := New_List;
1828 Entry_Call : Node_Id;
1829 Entry_Name : Node_Id;
1831 Specs : List_Id;
1832 -- The specification of the wrapper procedure
1834 begin
1836 -- Only build the wrapper if entry has pre/postconditions.
1837 -- Should this be done unconditionally instead ???
1839 declare
1840 P : Node_Id;
1842 begin
1843 P := Spec_PPC_List (Contract (E));
1844 if No (P) then
1845 return;
1846 end if;
1848 -- Transfer ppc pragmas to the declarations of the wrapper
1850 while Present (P) loop
1851 if Pragma_Name (P) = Name_Precondition
1852 or else Pragma_Name (P) = Name_Postcondition
1853 then
1854 Append (Relocate_Node (P), Decls);
1855 Set_Analyzed (Last (Decls), False);
1856 end if;
1858 P := Next_Pragma (P);
1859 end loop;
1860 end;
1862 -- First formal is synchronized object
1864 Specs := New_List (
1865 Make_Parameter_Specification (Loc,
1866 Defining_Identifier => Synch_Id,
1867 Out_Present => True,
1868 In_Present => True,
1869 Parameter_Type => New_Occurrence_Of (Scope (E), Loc)));
1871 Entry_Name :=
1872 Make_Selected_Component (Loc,
1873 Prefix => New_Occurrence_Of (Synch_Id, Loc),
1874 Selector_Name => New_Occurrence_Of (E, Loc));
1876 -- If entity is entry family, second formal is the corresponding index,
1877 -- and entry name is an indexed component.
1879 if Ekind (E) = E_Entry_Family then
1880 declare
1881 Index : constant Entity_Id :=
1882 Make_Defining_Identifier (Loc, Name_I);
1883 begin
1884 Append_To (Specs,
1885 Make_Parameter_Specification (Loc,
1886 Defining_Identifier => Index,
1887 Parameter_Type =>
1888 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1890 Entry_Name :=
1891 Make_Indexed_Component (Loc,
1892 Prefix => Entry_Name,
1893 Expressions => New_List (New_Occurrence_Of (Index, Loc)));
1894 end;
1895 end if;
1897 Entry_Call :=
1898 Make_Procedure_Call_Statement (Loc,
1899 Name => Entry_Name,
1900 Parameter_Associations => Actuals);
1902 -- Now add formals that match those of the entry, and build actuals for
1903 -- the nested entry call.
1905 declare
1906 Form : Entity_Id;
1907 New_Form : Entity_Id;
1908 Parm_Spec : Node_Id;
1910 begin
1911 Form := First_Formal (E);
1912 while Present (Form) loop
1913 New_Form := Make_Defining_Identifier (Loc, Chars (Form));
1914 Parm_Spec :=
1915 Make_Parameter_Specification (Loc,
1916 Defining_Identifier => New_Form,
1917 Out_Present => Out_Present (Parent (Form)),
1918 In_Present => In_Present (Parent (Form)),
1919 Parameter_Type => New_Occurrence_Of (Etype (Form), Loc));
1921 Append (Parm_Spec, Specs);
1922 Append (New_Occurrence_Of (New_Form, Loc), Actuals);
1923 Next_Formal (Form);
1924 end loop;
1925 end;
1927 -- Add renaming declarations for the discriminants of the enclosing
1928 -- type, which may be visible in the preconditions.
1930 if Has_Discriminants (Synch_Type) then
1931 declare
1932 D : Entity_Id;
1933 Decl : Node_Id;
1935 begin
1936 D := First_Discriminant (Synch_Type);
1937 while Present (D) loop
1938 Decl :=
1939 Make_Object_Renaming_Declaration (Loc,
1940 Defining_Identifier =>
1941 Make_Defining_Identifier (Loc, Chars (D)),
1942 Subtype_Mark => New_Reference_To (Etype (D), Loc),
1943 Name =>
1944 Make_Selected_Component (Loc,
1945 Prefix => New_Reference_To (Synch_Id, Loc),
1946 Selector_Name => Make_Identifier (Loc, Chars (D))));
1947 Prepend (Decl, Decls);
1948 Next_Discriminant (D);
1949 end loop;
1950 end;
1951 end if;
1953 Set_PPC_Wrapper (E, Wrapper_Id);
1954 Wrapper_Body :=
1955 Make_Subprogram_Body (Loc,
1956 Specification =>
1957 Make_Procedure_Specification (Loc,
1958 Defining_Unit_Name => Wrapper_Id,
1959 Parameter_Specifications => Specs),
1960 Declarations => Decls,
1961 Handled_Statement_Sequence =>
1962 Make_Handled_Sequence_Of_Statements (Loc,
1963 Statements => New_List (Entry_Call)));
1965 -- The wrapper body is analyzed when the enclosing type is frozen
1967 Append_Freeze_Action (Defining_Entity (Decl), Wrapper_Body);
1968 end Build_PPC_Wrapper;
1970 --------------------------
1971 -- Build_Wrapper_Bodies --
1972 --------------------------
1974 procedure Build_Wrapper_Bodies
1975 (Loc : Source_Ptr;
1976 Typ : Entity_Id;
1977 N : Node_Id)
1979 Rec_Typ : Entity_Id;
1981 function Build_Wrapper_Body
1982 (Loc : Source_Ptr;
1983 Subp_Id : Entity_Id;
1984 Obj_Typ : Entity_Id;
1985 Formals : List_Id) return Node_Id;
1986 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
1987 -- associated with a protected or task type. Subp_Id is the subprogram
1988 -- name which will be wrapped. Obj_Typ is the type of the new formal
1989 -- parameter which handles dispatching and object notation. Formals are
1990 -- the original formals of Subp_Id which will be explicitly replicated.
1992 ------------------------
1993 -- Build_Wrapper_Body --
1994 ------------------------
1996 function Build_Wrapper_Body
1997 (Loc : Source_Ptr;
1998 Subp_Id : Entity_Id;
1999 Obj_Typ : Entity_Id;
2000 Formals : List_Id) return Node_Id
2002 Body_Spec : Node_Id;
2004 begin
2005 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
2007 -- The subprogram is not overriding or is not a primitive declared
2008 -- between two views.
2010 if No (Body_Spec) then
2011 return Empty;
2012 end if;
2014 declare
2015 Actuals : List_Id := No_List;
2016 Conv_Id : Node_Id;
2017 First_Form : Node_Id;
2018 Formal : Node_Id;
2019 Nam : Node_Id;
2021 begin
2022 -- Map formals to actuals. Use the list built for the wrapper
2023 -- spec, skipping the object notation parameter.
2025 First_Form := First (Parameter_Specifications (Body_Spec));
2027 Formal := First_Form;
2028 Next (Formal);
2030 if Present (Formal) then
2031 Actuals := New_List;
2032 while Present (Formal) loop
2033 Append_To (Actuals,
2034 Make_Identifier (Loc,
2035 Chars => Chars (Defining_Identifier (Formal))));
2036 Next (Formal);
2037 end loop;
2038 end if;
2040 -- Special processing for primitives declared between a private
2041 -- type and its completion: the wrapper needs a properly typed
2042 -- parameter if the wrapped operation has a controlling first
2043 -- parameter. Note that this might not be the case for a function
2044 -- with a controlling result.
2046 if Is_Private_Primitive_Subprogram (Subp_Id) then
2047 if No (Actuals) then
2048 Actuals := New_List;
2049 end if;
2051 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
2052 Prepend_To (Actuals,
2053 Unchecked_Convert_To
2054 (Corresponding_Concurrent_Type (Obj_Typ),
2055 Make_Identifier (Loc, Name_uO)));
2057 else
2058 Prepend_To (Actuals,
2059 Make_Identifier (Loc,
2060 Chars => Chars (Defining_Identifier (First_Form))));
2061 end if;
2063 Nam := New_Reference_To (Subp_Id, Loc);
2064 else
2065 -- An access-to-variable object parameter requires an explicit
2066 -- dereference in the unchecked conversion. This case occurs
2067 -- when a protected entry wrapper must override an interface
2068 -- level procedure with interface access as first parameter.
2070 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
2072 if Nkind (Parameter_Type (First_Form)) =
2073 N_Access_Definition
2074 then
2075 Conv_Id :=
2076 Make_Explicit_Dereference (Loc,
2077 Prefix => Make_Identifier (Loc, Name_uO));
2078 else
2079 Conv_Id := Make_Identifier (Loc, Name_uO);
2080 end if;
2082 Nam :=
2083 Make_Selected_Component (Loc,
2084 Prefix =>
2085 Unchecked_Convert_To
2086 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
2087 Selector_Name => New_Reference_To (Subp_Id, Loc));
2088 end if;
2090 -- Create the subprogram body. For a function, the call to the
2091 -- actual subprogram has to be converted to the corresponding
2092 -- record if it is a controlling result.
2094 if Ekind (Subp_Id) = E_Function then
2095 declare
2096 Res : Node_Id;
2098 begin
2099 Res :=
2100 Make_Function_Call (Loc,
2101 Name => Nam,
2102 Parameter_Associations => Actuals);
2104 if Has_Controlling_Result (Subp_Id) then
2105 Res :=
2106 Unchecked_Convert_To
2107 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
2108 end if;
2110 return
2111 Make_Subprogram_Body (Loc,
2112 Specification => Body_Spec,
2113 Declarations => Empty_List,
2114 Handled_Statement_Sequence =>
2115 Make_Handled_Sequence_Of_Statements (Loc,
2116 Statements => New_List (
2117 Make_Simple_Return_Statement (Loc, Res))));
2118 end;
2120 else
2121 return
2122 Make_Subprogram_Body (Loc,
2123 Specification => Body_Spec,
2124 Declarations => Empty_List,
2125 Handled_Statement_Sequence =>
2126 Make_Handled_Sequence_Of_Statements (Loc,
2127 Statements => New_List (
2128 Make_Procedure_Call_Statement (Loc,
2129 Name => Nam,
2130 Parameter_Associations => Actuals))));
2131 end if;
2132 end;
2133 end Build_Wrapper_Body;
2135 -- Start of processing for Build_Wrapper_Bodies
2137 begin
2138 if Is_Concurrent_Type (Typ) then
2139 Rec_Typ := Corresponding_Record_Type (Typ);
2140 else
2141 Rec_Typ := Typ;
2142 end if;
2144 -- Generate wrapper bodies for a concurrent type which implements an
2145 -- interface.
2147 if Present (Interfaces (Rec_Typ)) then
2148 declare
2149 Insert_Nod : Node_Id;
2150 Prim : Entity_Id;
2151 Prim_Elmt : Elmt_Id;
2152 Prim_Decl : Node_Id;
2153 Subp : Entity_Id;
2154 Wrap_Body : Node_Id;
2155 Wrap_Id : Entity_Id;
2157 begin
2158 Insert_Nod := N;
2160 -- Examine all primitive operations of the corresponding record
2161 -- type, looking for wrapper specs. Generate bodies in order to
2162 -- complete them.
2164 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2165 while Present (Prim_Elmt) loop
2166 Prim := Node (Prim_Elmt);
2168 if (Ekind (Prim) = E_Function
2169 or else Ekind (Prim) = E_Procedure)
2170 and then Is_Primitive_Wrapper (Prim)
2171 then
2172 Subp := Wrapped_Entity (Prim);
2173 Prim_Decl := Parent (Parent (Prim));
2175 Wrap_Body :=
2176 Build_Wrapper_Body (Loc,
2177 Subp_Id => Subp,
2178 Obj_Typ => Rec_Typ,
2179 Formals => Parameter_Specifications (Parent (Subp)));
2180 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2182 Set_Corresponding_Spec (Wrap_Body, Prim);
2183 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2185 Insert_After (Insert_Nod, Wrap_Body);
2186 Insert_Nod := Wrap_Body;
2188 Analyze (Wrap_Body);
2189 end if;
2191 Next_Elmt (Prim_Elmt);
2192 end loop;
2193 end;
2194 end if;
2195 end Build_Wrapper_Bodies;
2197 ------------------------
2198 -- Build_Wrapper_Spec --
2199 ------------------------
2201 function Build_Wrapper_Spec
2202 (Subp_Id : Entity_Id;
2203 Obj_Typ : Entity_Id;
2204 Formals : List_Id) return Node_Id
2206 Loc : constant Source_Ptr := Sloc (Subp_Id);
2207 First_Param : Node_Id;
2208 Iface : Entity_Id;
2209 Iface_Elmt : Elmt_Id;
2210 Iface_Op : Entity_Id;
2211 Iface_Op_Elmt : Elmt_Id;
2213 function Overriding_Possible
2214 (Iface_Op : Entity_Id;
2215 Wrapper : Entity_Id) return Boolean;
2216 -- Determine whether a primitive operation can be overridden by Wrapper.
2217 -- Iface_Op is the candidate primitive operation of an interface type,
2218 -- Wrapper is the generated entry wrapper.
2220 function Replicate_Formals
2221 (Loc : Source_Ptr;
2222 Formals : List_Id) return List_Id;
2223 -- An explicit parameter replication is required due to the Is_Entry_
2224 -- Formal flag being set for all the formals of an entry. The explicit
2225 -- replication removes the flag that would otherwise cause a different
2226 -- path of analysis.
2228 -------------------------
2229 -- Overriding_Possible --
2230 -------------------------
2232 function Overriding_Possible
2233 (Iface_Op : Entity_Id;
2234 Wrapper : Entity_Id) return Boolean
2236 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2237 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2239 function Type_Conformant_Parameters
2240 (Iface_Op_Params : List_Id;
2241 Wrapper_Params : List_Id) return Boolean;
2242 -- Determine whether the parameters of the generated entry wrapper
2243 -- and those of a primitive operation are type conformant. During
2244 -- this check, the first parameter of the primitive operation is
2245 -- skipped if it is a controlling argument: protected functions
2246 -- may have a controlling result.
2248 --------------------------------
2249 -- Type_Conformant_Parameters --
2250 --------------------------------
2252 function Type_Conformant_Parameters
2253 (Iface_Op_Params : List_Id;
2254 Wrapper_Params : List_Id) return Boolean
2256 Iface_Op_Param : Node_Id;
2257 Iface_Op_Typ : Entity_Id;
2258 Wrapper_Param : Node_Id;
2259 Wrapper_Typ : Entity_Id;
2261 begin
2262 -- Skip the first (controlling) parameter of primitive operation
2264 Iface_Op_Param := First (Iface_Op_Params);
2266 if Present (First_Formal (Iface_Op))
2267 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2268 then
2269 Iface_Op_Param := Next (Iface_Op_Param);
2270 end if;
2272 Wrapper_Param := First (Wrapper_Params);
2273 while Present (Iface_Op_Param)
2274 and then Present (Wrapper_Param)
2275 loop
2276 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2277 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2279 -- The two parameters must be mode conformant
2281 if not Conforming_Types
2282 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2283 then
2284 return False;
2285 end if;
2287 Next (Iface_Op_Param);
2288 Next (Wrapper_Param);
2289 end loop;
2291 -- One of the lists is longer than the other
2293 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2294 return False;
2295 end if;
2297 return True;
2298 end Type_Conformant_Parameters;
2300 -- Start of processing for Overriding_Possible
2302 begin
2303 if Chars (Iface_Op) /= Chars (Wrapper) then
2304 return False;
2305 end if;
2307 -- If an inherited subprogram is implemented by a protected procedure
2308 -- or an entry, then the first parameter of the inherited subprogram
2309 -- shall be of mode OUT or IN OUT, or access-to-variable parameter.
2311 if Ekind (Iface_Op) = E_Procedure
2312 and then Present (Parameter_Specifications (Iface_Op_Spec))
2313 then
2314 declare
2315 Obj_Param : constant Node_Id :=
2316 First (Parameter_Specifications (Iface_Op_Spec));
2317 begin
2318 if not Out_Present (Obj_Param)
2319 and then Nkind (Parameter_Type (Obj_Param)) /=
2320 N_Access_Definition
2321 then
2322 return False;
2323 end if;
2324 end;
2325 end if;
2327 return
2328 Type_Conformant_Parameters (
2329 Parameter_Specifications (Iface_Op_Spec),
2330 Parameter_Specifications (Wrapper_Spec));
2331 end Overriding_Possible;
2333 -----------------------
2334 -- Replicate_Formals --
2335 -----------------------
2337 function Replicate_Formals
2338 (Loc : Source_Ptr;
2339 Formals : List_Id) return List_Id
2341 New_Formals : constant List_Id := New_List;
2342 Formal : Node_Id;
2343 Param_Type : Node_Id;
2345 begin
2346 Formal := First (Formals);
2348 -- Skip the object parameter when dealing with primitives declared
2349 -- between two views.
2351 if Is_Private_Primitive_Subprogram (Subp_Id)
2352 and then not Has_Controlling_Result (Subp_Id)
2353 then
2354 Formal := Next (Formal);
2355 end if;
2357 while Present (Formal) loop
2359 -- Create an explicit copy of the entry parameter
2361 -- When creating the wrapper subprogram for a primitive operation
2362 -- of a protected interface we must construct an equivalent
2363 -- signature to that of the overriding operation. For regular
2364 -- parameters we can just use the type of the formal, but for
2365 -- access to subprogram parameters we need to reanalyze the
2366 -- parameter type to create local entities for the signature of
2367 -- the subprogram type. Using the entities of the overriding
2368 -- subprogram will result in out-of-scope errors in the back-end.
2370 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2371 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2372 else
2373 Param_Type :=
2374 New_Reference_To (Etype (Parameter_Type (Formal)), Loc);
2375 end if;
2377 Append_To (New_Formals,
2378 Make_Parameter_Specification (Loc,
2379 Defining_Identifier =>
2380 Make_Defining_Identifier (Loc,
2381 Chars => Chars (Defining_Identifier (Formal))),
2382 In_Present => In_Present (Formal),
2383 Out_Present => Out_Present (Formal),
2384 Parameter_Type => Param_Type));
2386 Next (Formal);
2387 end loop;
2389 return New_Formals;
2390 end Replicate_Formals;
2392 -- Start of processing for Build_Wrapper_Spec
2394 begin
2395 -- There is no point in building wrappers for non-tagged concurrent
2396 -- types.
2398 pragma Assert (Is_Tagged_Type (Obj_Typ));
2400 -- An entry or a protected procedure can override a routine where the
2401 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2402 -- type. Since the wrapper must have the exact same signature as that of
2403 -- the overridden subprogram, we try to find the overriding candidate
2404 -- and use its controlling formal.
2406 First_Param := Empty;
2408 -- Check every implemented interface
2410 if Present (Interfaces (Obj_Typ)) then
2411 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2412 Search : while Present (Iface_Elmt) loop
2413 Iface := Node (Iface_Elmt);
2415 -- Check every interface primitive
2417 if Present (Primitive_Operations (Iface)) then
2418 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2419 while Present (Iface_Op_Elmt) loop
2420 Iface_Op := Node (Iface_Op_Elmt);
2422 -- Ignore predefined primitives
2424 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2425 Iface_Op := Ultimate_Alias (Iface_Op);
2427 -- The current primitive operation can be overridden by
2428 -- the generated entry wrapper.
2430 if Overriding_Possible (Iface_Op, Subp_Id) then
2431 First_Param :=
2432 First (Parameter_Specifications (Parent (Iface_Op)));
2434 exit Search;
2435 end if;
2436 end if;
2438 Next_Elmt (Iface_Op_Elmt);
2439 end loop;
2440 end if;
2442 Next_Elmt (Iface_Elmt);
2443 end loop Search;
2444 end if;
2446 -- Ada 2012 (AI05-0090-1): If no interface primitive is covered by
2447 -- this subprogram and this is not a primitive declared between two
2448 -- views then force the generation of a wrapper. As an optimization,
2449 -- previous versions of the frontend avoid generating the wrapper;
2450 -- however, the wrapper facilitates locating and reporting an error
2451 -- when a duplicate declaration is found later. See example in
2452 -- AI05-0090-1.
2454 if No (First_Param)
2455 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2456 then
2457 if Is_Task_Type
2458 (Corresponding_Concurrent_Type (Obj_Typ))
2459 then
2460 First_Param :=
2461 Make_Parameter_Specification (Loc,
2462 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uO),
2463 In_Present => True,
2464 Out_Present => False,
2465 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2467 -- For entries and procedures of protected types the mode of
2468 -- the controlling argument must be in-out.
2470 else
2471 First_Param :=
2472 Make_Parameter_Specification (Loc,
2473 Defining_Identifier =>
2474 Make_Defining_Identifier (Loc,
2475 Chars => Name_uO),
2476 In_Present => True,
2477 Out_Present => (Ekind (Subp_Id) /= E_Function),
2478 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2479 end if;
2480 end if;
2482 declare
2483 Wrapper_Id : constant Entity_Id :=
2484 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2485 New_Formals : List_Id;
2486 Obj_Param : Node_Id;
2487 Obj_Param_Typ : Entity_Id;
2489 begin
2490 -- Minimum decoration is needed to catch the entity in
2491 -- Sem_Ch6.Override_Dispatching_Operation.
2493 if Ekind (Subp_Id) = E_Function then
2494 Set_Ekind (Wrapper_Id, E_Function);
2495 else
2496 Set_Ekind (Wrapper_Id, E_Procedure);
2497 end if;
2499 Set_Is_Primitive_Wrapper (Wrapper_Id);
2500 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2501 Set_Is_Private_Primitive (Wrapper_Id,
2502 Is_Private_Primitive_Subprogram (Subp_Id));
2504 -- Process the formals
2506 New_Formals := Replicate_Formals (Loc, Formals);
2508 -- A function with a controlling result and no first controlling
2509 -- formal needs no additional parameter.
2511 if Has_Controlling_Result (Subp_Id)
2512 and then
2513 (No (First_Formal (Subp_Id))
2514 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2515 then
2516 null;
2518 -- Routine Subp_Id has been found to override an interface primitive.
2519 -- If the interface operation has an access parameter, create a copy
2520 -- of it, with the same null exclusion indicator if present.
2522 elsif Present (First_Param) then
2523 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2524 Obj_Param_Typ :=
2525 Make_Access_Definition (Loc,
2526 Subtype_Mark =>
2527 New_Reference_To (Obj_Typ, Loc));
2528 Set_Null_Exclusion_Present (Obj_Param_Typ,
2529 Null_Exclusion_Present (Parameter_Type (First_Param)));
2531 else
2532 Obj_Param_Typ := New_Reference_To (Obj_Typ, Loc);
2533 end if;
2535 Obj_Param :=
2536 Make_Parameter_Specification (Loc,
2537 Defining_Identifier =>
2538 Make_Defining_Identifier (Loc,
2539 Chars => Name_uO),
2540 In_Present => In_Present (First_Param),
2541 Out_Present => Out_Present (First_Param),
2542 Parameter_Type => Obj_Param_Typ);
2544 Prepend_To (New_Formals, Obj_Param);
2546 -- If we are dealing with a primitive declared between two views,
2547 -- implemented by a synchronized operation, we need to create
2548 -- a default parameter. The mode of the parameter must match that
2549 -- of the primitive operation.
2551 else
2552 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2553 Obj_Param :=
2554 Make_Parameter_Specification (Loc,
2555 Defining_Identifier =>
2556 Make_Defining_Identifier (Loc, Name_uO),
2557 In_Present => In_Present (Parent (First_Entity (Subp_Id))),
2558 Out_Present => Ekind (Subp_Id) /= E_Function,
2559 Parameter_Type => New_Reference_To (Obj_Typ, Loc));
2560 Prepend_To (New_Formals, Obj_Param);
2561 end if;
2563 -- Build the final spec. If it is a function with a controlling
2564 -- result, it is a primitive operation of the corresponding
2565 -- record type, so mark the spec accordingly.
2567 if Ekind (Subp_Id) = E_Function then
2568 declare
2569 Res_Def : Node_Id;
2571 begin
2572 if Has_Controlling_Result (Subp_Id) then
2573 Res_Def :=
2574 New_Occurrence_Of
2575 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2576 else
2577 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2578 end if;
2580 return
2581 Make_Function_Specification (Loc,
2582 Defining_Unit_Name => Wrapper_Id,
2583 Parameter_Specifications => New_Formals,
2584 Result_Definition => Res_Def);
2585 end;
2586 else
2587 return
2588 Make_Procedure_Specification (Loc,
2589 Defining_Unit_Name => Wrapper_Id,
2590 Parameter_Specifications => New_Formals);
2591 end if;
2592 end;
2593 end Build_Wrapper_Spec;
2595 -------------------------
2596 -- Build_Wrapper_Specs --
2597 -------------------------
2599 procedure Build_Wrapper_Specs
2600 (Loc : Source_Ptr;
2601 Typ : Entity_Id;
2602 N : in out Node_Id)
2604 Def : Node_Id;
2605 Rec_Typ : Entity_Id;
2606 procedure Scan_Declarations (L : List_Id);
2607 -- Common processing for visible and private declarations
2608 -- of a protected type.
2610 procedure Scan_Declarations (L : List_Id) is
2611 Decl : Node_Id;
2612 Wrap_Decl : Node_Id;
2613 Wrap_Spec : Node_Id;
2615 begin
2616 if No (L) then
2617 return;
2618 end if;
2620 Decl := First (L);
2621 while Present (Decl) loop
2622 Wrap_Spec := Empty;
2624 if Nkind (Decl) = N_Entry_Declaration
2625 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2626 then
2627 Wrap_Spec :=
2628 Build_Wrapper_Spec
2629 (Subp_Id => Defining_Identifier (Decl),
2630 Obj_Typ => Rec_Typ,
2631 Formals => Parameter_Specifications (Decl));
2633 elsif Nkind (Decl) = N_Subprogram_Declaration then
2634 Wrap_Spec :=
2635 Build_Wrapper_Spec
2636 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2637 Obj_Typ => Rec_Typ,
2638 Formals =>
2639 Parameter_Specifications (Specification (Decl)));
2640 end if;
2642 if Present (Wrap_Spec) then
2643 Wrap_Decl :=
2644 Make_Subprogram_Declaration (Loc,
2645 Specification => Wrap_Spec);
2647 Insert_After (N, Wrap_Decl);
2648 N := Wrap_Decl;
2650 Analyze (Wrap_Decl);
2651 end if;
2653 Next (Decl);
2654 end loop;
2655 end Scan_Declarations;
2657 -- start of processing for Build_Wrapper_Specs
2659 begin
2660 if Is_Protected_Type (Typ) then
2661 Def := Protected_Definition (Parent (Typ));
2662 else pragma Assert (Is_Task_Type (Typ));
2663 Def := Task_Definition (Parent (Typ));
2664 end if;
2666 Rec_Typ := Corresponding_Record_Type (Typ);
2668 -- Generate wrapper specs for a concurrent type which implements an
2669 -- interface. Operations in both the visible and private parts may
2670 -- implement progenitor operations.
2672 if Present (Interfaces (Rec_Typ))
2673 and then Present (Def)
2674 then
2675 Scan_Declarations (Visible_Declarations (Def));
2676 Scan_Declarations (Private_Declarations (Def));
2677 end if;
2678 end Build_Wrapper_Specs;
2680 ---------------------------
2681 -- Build_Find_Body_Index --
2682 ---------------------------
2684 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2685 Loc : constant Source_Ptr := Sloc (Typ);
2686 Ent : Entity_Id;
2687 E_Typ : Entity_Id;
2688 Has_F : Boolean := False;
2689 Index : Nat;
2690 If_St : Node_Id := Empty;
2691 Lo : Node_Id;
2692 Hi : Node_Id;
2693 Decls : List_Id := New_List;
2694 Ret : Node_Id;
2695 Spec : Node_Id;
2696 Siz : Node_Id := Empty;
2698 procedure Add_If_Clause (Expr : Node_Id);
2699 -- Add test for range of current entry
2701 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2702 -- If a bound of an entry is given by a discriminant, retrieve the
2703 -- actual value of the discriminant from the enclosing object.
2705 -------------------
2706 -- Add_If_Clause --
2707 -------------------
2709 procedure Add_If_Clause (Expr : Node_Id) is
2710 Cond : Node_Id;
2711 Stats : constant List_Id :=
2712 New_List (
2713 Make_Simple_Return_Statement (Loc,
2714 Expression => Make_Integer_Literal (Loc, Index + 1)));
2716 begin
2717 -- Index for current entry body
2719 Index := Index + 1;
2721 -- Compute total length of entry queues so far
2723 if No (Siz) then
2724 Siz := Expr;
2725 else
2726 Siz :=
2727 Make_Op_Add (Loc,
2728 Left_Opnd => Siz,
2729 Right_Opnd => Expr);
2730 end if;
2732 Cond :=
2733 Make_Op_Le (Loc,
2734 Left_Opnd => Make_Identifier (Loc, Name_uE),
2735 Right_Opnd => Siz);
2737 -- Map entry queue indexes in the range of the current family
2738 -- into the current index, that designates the entry body.
2740 if No (If_St) then
2741 If_St :=
2742 Make_Implicit_If_Statement (Typ,
2743 Condition => Cond,
2744 Then_Statements => Stats,
2745 Elsif_Parts => New_List);
2746 Ret := If_St;
2748 else
2749 Append_To (Elsif_Parts (If_St),
2750 Make_Elsif_Part (Loc,
2751 Condition => Cond,
2752 Then_Statements => Stats));
2753 end if;
2754 end Add_If_Clause;
2756 ------------------------------
2757 -- Convert_Discriminant_Ref --
2758 ------------------------------
2760 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2761 B : Node_Id;
2763 begin
2764 if Is_Entity_Name (Bound)
2765 and then Ekind (Entity (Bound)) = E_Discriminant
2766 then
2767 B :=
2768 Make_Selected_Component (Loc,
2769 Prefix =>
2770 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2771 Make_Explicit_Dereference (Loc,
2772 Make_Identifier (Loc, Name_uObject))),
2773 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2774 Set_Etype (B, Etype (Entity (Bound)));
2775 else
2776 B := New_Copy_Tree (Bound);
2777 end if;
2779 return B;
2780 end Convert_Discriminant_Ref;
2782 -- Start of processing for Build_Find_Body_Index
2784 begin
2785 Spec := Build_Find_Body_Index_Spec (Typ);
2787 Ent := First_Entity (Typ);
2788 while Present (Ent) loop
2789 if Ekind (Ent) = E_Entry_Family then
2790 Has_F := True;
2791 exit;
2792 end if;
2794 Next_Entity (Ent);
2795 end loop;
2797 if not Has_F then
2799 -- If the protected type has no entry families, there is a one-one
2800 -- correspondence between entry queue and entry body.
2802 Ret :=
2803 Make_Simple_Return_Statement (Loc,
2804 Expression => Make_Identifier (Loc, Name_uE));
2806 else
2807 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2808 -- the following:
2810 -- if E <= l1 then return 1;
2811 -- elsif E <= l1 + l2 then return 2;
2812 -- ...
2814 Index := 0;
2815 Siz := Empty;
2816 Ent := First_Entity (Typ);
2818 Add_Object_Pointer (Loc, Typ, Decls);
2820 while Present (Ent) loop
2821 if Ekind (Ent) = E_Entry then
2822 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2824 elsif Ekind (Ent) = E_Entry_Family then
2825 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2826 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2827 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2828 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2829 end if;
2831 Next_Entity (Ent);
2832 end loop;
2834 if Index = 1 then
2835 Decls := New_List;
2836 Ret :=
2837 Make_Simple_Return_Statement (Loc,
2838 Expression => Make_Integer_Literal (Loc, 1));
2840 elsif Nkind (Ret) = N_If_Statement then
2842 -- Ranges are in increasing order, so last one doesn't need guard
2844 declare
2845 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2846 begin
2847 Remove (Nod);
2848 Set_Else_Statements (Ret, Then_Statements (Nod));
2849 end;
2850 end if;
2851 end if;
2853 return
2854 Make_Subprogram_Body (Loc,
2855 Specification => Spec,
2856 Declarations => Decls,
2857 Handled_Statement_Sequence =>
2858 Make_Handled_Sequence_Of_Statements (Loc,
2859 Statements => New_List (Ret)));
2860 end Build_Find_Body_Index;
2862 --------------------------------
2863 -- Build_Find_Body_Index_Spec --
2864 --------------------------------
2866 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2867 Loc : constant Source_Ptr := Sloc (Typ);
2868 Id : constant Entity_Id :=
2869 Make_Defining_Identifier (Loc,
2870 Chars => New_External_Name (Chars (Typ), 'F'));
2871 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2872 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2874 begin
2875 return
2876 Make_Function_Specification (Loc,
2877 Defining_Unit_Name => Id,
2878 Parameter_Specifications => New_List (
2879 Make_Parameter_Specification (Loc,
2880 Defining_Identifier => Parm1,
2881 Parameter_Type =>
2882 New_Reference_To (RTE (RE_Address), Loc)),
2884 Make_Parameter_Specification (Loc,
2885 Defining_Identifier => Parm2,
2886 Parameter_Type =>
2887 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))),
2889 Result_Definition => New_Occurrence_Of (
2890 RTE (RE_Protected_Entry_Index), Loc));
2891 end Build_Find_Body_Index_Spec;
2893 -----------------------------------------------
2894 -- Build_Lock_Free_Protected_Subprogram_Body --
2895 -----------------------------------------------
2897 function Build_Lock_Free_Protected_Subprogram_Body
2898 (N : Node_Id;
2899 Prot_Typ : Node_Id;
2900 Unprot_Spec : Node_Id) return Node_Id
2902 Actuals : constant List_Id := New_List;
2903 Loc : constant Source_Ptr := Sloc (N);
2904 Spec : constant Node_Id := Specification (N);
2905 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
2906 Formal : Node_Id;
2907 Prot_Spec : Node_Id;
2908 Stmt : Node_Id;
2910 begin
2911 -- Create the protected version of the body
2913 Prot_Spec :=
2914 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
2916 -- Build the actual parameters which appear in the call to the
2917 -- unprotected version of the body.
2919 Formal := First (Parameter_Specifications (Prot_Spec));
2920 while Present (Formal) loop
2921 Append_To (Actuals,
2922 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2924 Next (Formal);
2925 end loop;
2927 -- Function case, generate:
2928 -- return <Unprot_Func_Call>;
2930 if Nkind (Spec) = N_Function_Specification then
2931 Stmt :=
2932 Make_Simple_Return_Statement (Loc,
2933 Expression =>
2934 Make_Function_Call (Loc,
2935 Name =>
2936 Make_Identifier (Loc, Chars (Unprot_Id)),
2937 Parameter_Associations => Actuals));
2939 -- Procedure case, call the unprotected version
2941 else
2942 Stmt :=
2943 Make_Procedure_Call_Statement (Loc,
2944 Name =>
2945 Make_Identifier (Loc, Chars (Unprot_Id)),
2946 Parameter_Associations => Actuals);
2947 end if;
2949 return
2950 Make_Subprogram_Body (Loc,
2951 Declarations => Empty_List,
2952 Specification => Prot_Spec,
2953 Handled_Statement_Sequence =>
2954 Make_Handled_Sequence_Of_Statements (Loc,
2955 Statements => New_List (Stmt)));
2956 end Build_Lock_Free_Protected_Subprogram_Body;
2958 -------------------------------------------------
2959 -- Build_Lock_Free_Unprotected_Subprogram_Body --
2960 -------------------------------------------------
2962 -- Procedures which meet the lock-free implementation requirements and
2963 -- reference a unique scalar component Comp are expanded in the following
2964 -- manner:
2966 -- procedure P (...) is
2967 -- <original declarations>
2968 -- begin
2969 -- loop
2970 -- declare
2971 -- Saved_Comp : constant ... :=
2972 -- Atomic_Load (Comp'Address, Relaxed);
2973 -- Current_Comp : ... := Saved_Comp;
2974 -- begin
2975 -- <original statements>
2976 -- exit when Atomic_Compare (Comp, Saved_Comp, Current_Comp);
2977 -- end;
2978 -- <<L0>>
2979 -- end loop;
2980 -- end P;
2982 -- References to Comp which appear in the original statements are replaced
2983 -- with references to Current_Comp. Each return and raise statement of P is
2984 -- transformed into an atomic status check:
2986 -- if Atomic_Compare (Comp, Saved_Comp, Current_Comp) then
2987 -- <original statement>
2988 -- else
2989 -- goto L0;
2990 -- end if;
2992 -- Functions which meet the lock-free implementation requirements and
2993 -- reference a unique scalar component Comp are expanded in the following
2994 -- manner:
2996 -- function F (...) return ... is
2997 -- <original declarations>
2998 -- Saved_Comp : constant ... := Atomic_Load (Comp'Address);
2999 -- begin
3000 -- <original statements>
3001 -- end F;
3003 -- References to Comp which appear in the original statements are replaced
3004 -- with references to Saved_Comp.
3006 function Build_Lock_Free_Unprotected_Subprogram_Body
3007 (N : Node_Id;
3008 Prot_Typ : Node_Id) return Node_Id
3010 Is_Procedure : constant Boolean :=
3011 Ekind (Corresponding_Spec (N)) = E_Procedure;
3012 Loc : constant Source_Ptr := Sloc (N);
3013 Label_Id : Entity_Id := Empty;
3015 procedure Process_Stmts
3016 (Stmts : List_Id;
3017 Compare : Entity_Id;
3018 Unsigned : Entity_Id;
3019 Comp : Entity_Id;
3020 Saved_Comp : Entity_Id;
3021 Current_Comp : Entity_Id);
3022 -- Given a statement sequence Stmts, wrap any return or raise statements
3023 -- in the following manner:
3025 -- if System.Atomic_Primitives.Atomic_Compare_Exchange
3026 -- (Comp'Address,
3027 -- Interfaces.Unsigned (Saved_Comp),
3028 -- Interfaces.Unsigned (Current_Comp))
3029 -- then
3030 -- <Stmt>;
3031 -- else
3032 -- goto L0;
3033 -- end if;
3035 -- Replace all references to Comp with a reference to Current_Comp.
3037 function Referenced_Component (N : Node_Id) return Entity_Id;
3038 -- Subprograms which meet the lock-free implementation criteria are
3039 -- allowed to reference only one unique component. Return the prival
3040 -- of the said component.
3042 -------------------
3043 -- Process_Stmts --
3044 -------------------
3046 procedure Process_Stmts
3047 (Stmts : List_Id;
3048 Compare : Entity_Id;
3049 Unsigned : Entity_Id;
3050 Comp : Entity_Id;
3051 Saved_Comp : Entity_Id;
3052 Current_Comp : Entity_Id)
3054 function Process_Node (N : Node_Id) return Traverse_Result;
3055 -- Transform a single node if it is a return statement, a raise
3056 -- statement or a reference to Comp.
3058 ------------------
3059 -- Process_Node --
3060 ------------------
3062 function Process_Node (N : Node_Id) return Traverse_Result is
3064 procedure Wrap_Statement (Stmt : Node_Id);
3065 -- Wrap an arbitrary statement inside an if statement where the
3066 -- condition does an atomic check on the state of the object.
3068 --------------------
3069 -- Wrap_Statement --
3070 --------------------
3072 procedure Wrap_Statement (Stmt : Node_Id) is
3073 begin
3074 -- The first time through, create the declaration of a label
3075 -- which is used to skip the remainder of source statements if
3076 -- the state of the object has changed.
3078 if No (Label_Id) then
3079 Label_Id :=
3080 Make_Identifier (Loc, New_External_Name ('L', 0));
3081 Set_Entity (Label_Id,
3082 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3083 end if;
3085 -- Generate:
3087 -- if System.Atomic_Primitives.Atomic_Compare_Exchange
3088 -- (Comp'Address,
3089 -- Interfaces.Unsigned (Saved_Comp),
3090 -- Interfaces.Unsigned (Current_Comp))
3091 -- then
3092 -- <Stmt>;
3093 -- else
3094 -- goto L0;
3095 -- end if;
3097 Rewrite (Stmt,
3098 Make_If_Statement (Loc,
3099 Condition =>
3100 Make_Function_Call (Loc,
3101 Name =>
3102 New_Reference_To (Compare, Loc),
3103 Parameter_Associations => New_List (
3104 Make_Attribute_Reference (Loc,
3105 Prefix => New_Reference_To (Comp, Loc),
3106 Attribute_Name => Name_Address),
3108 Unchecked_Convert_To (Unsigned,
3109 New_Reference_To (Saved_Comp, Loc)),
3111 Unchecked_Convert_To (Unsigned,
3112 New_Reference_To (Current_Comp, Loc)))),
3114 Then_Statements => New_List (Relocate_Node (Stmt)),
3116 Else_Statements => New_List (
3117 Make_Goto_Statement (Loc,
3118 Name => New_Reference_To (Entity (Label_Id), Loc)))));
3119 end Wrap_Statement;
3121 -- Start of processing for Process_Node
3123 begin
3124 -- Wrap each return and raise statement that appear inside a
3125 -- procedure. Skip the last return statement which is added by
3126 -- default since it is transformed into an exit statement.
3128 if Is_Procedure
3129 and then Nkind_In (N, N_Simple_Return_Statement,
3130 N_Extended_Return_Statement,
3131 N_Raise_Statement)
3132 and then Nkind (Last (Stmts)) /= N_Simple_Return_Statement
3133 then
3134 Wrap_Statement (N);
3135 return Skip;
3137 -- Replace all references to the original component by a reference
3138 -- to the current state of the component.
3140 elsif Nkind (N) = N_Identifier
3141 and then Present (Entity (N))
3142 and then Entity (N) = Comp
3143 then
3144 Rewrite (N, Make_Identifier (Loc, Chars (Current_Comp)));
3145 return Skip;
3146 end if;
3148 -- Force reanalysis
3150 Set_Analyzed (N, False);
3152 return OK;
3153 end Process_Node;
3155 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3157 -- Local variables
3159 Stmt : Node_Id;
3161 -- Start of processing for Process_Stmts
3163 begin
3164 Stmt := First (Stmts);
3165 while Present (Stmt) loop
3166 Process_Nodes (Stmt);
3167 Next (Stmt);
3168 end loop;
3169 end Process_Stmts;
3171 --------------------------
3172 -- Referenced_Component --
3173 --------------------------
3175 function Referenced_Component (N : Node_Id) return Entity_Id is
3176 Comp : Entity_Id;
3177 Decl : Node_Id;
3178 Source_Comp : Entity_Id := Empty;
3180 begin
3181 -- Find the unique source component which N references in its
3182 -- statements.
3184 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
3185 declare
3186 Element : Lock_Free_Subprogram renames
3187 Lock_Free_Subprogram_Table.Table (Index);
3188 begin
3189 if Element.Sub_Body = N then
3190 Source_Comp := Element.Comp_Id;
3191 exit;
3192 end if;
3193 end;
3194 end loop;
3196 if No (Source_Comp) then
3197 return Empty;
3198 end if;
3200 -- Find the prival which corresponds to the source component within
3201 -- the declarations of N.
3203 Decl := First (Declarations (N));
3204 while Present (Decl) loop
3206 -- Privals appear as object renamings
3208 if Nkind (Decl) = N_Object_Renaming_Declaration then
3209 Comp := Defining_Identifier (Decl);
3211 if Present (Prival_Link (Comp))
3212 and then Prival_Link (Comp) = Source_Comp
3213 then
3214 return Comp;
3215 end if;
3216 end if;
3218 Next (Decl);
3219 end loop;
3221 return Empty;
3222 end Referenced_Component;
3224 -- Local variables
3226 Comp : constant Entity_Id := Referenced_Component (N);
3227 Decls : constant List_Id := Declarations (N);
3228 Stmts : List_Id;
3230 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
3232 begin
3233 Stmts := New_Copy_List (Statements (Handled_Statement_Sequence (N)));
3235 -- Perform the lock-free expansion when the subprogram references a
3236 -- protected component.
3238 if Present (Comp) then
3239 declare
3240 Comp_Type : constant Entity_Id := Etype (Comp);
3241 Block_Decls : List_Id;
3242 Compare : Entity_Id;
3243 Current_Comp : Entity_Id;
3244 Decl : Node_Id;
3245 Label : Node_Id;
3246 Load : Entity_Id;
3247 Load_Params : List_Id;
3248 Saved_Comp : Entity_Id;
3249 Stmt : Node_Id;
3250 Typ_Size : Int;
3251 Unsigned : Entity_Id;
3253 begin
3254 -- Get the type size
3256 -- Surely this should be Known_Static_Esize if you are about
3257 -- to assume you can do UI_To_Int on it! ???
3259 if Known_Esize (Comp_Type) then
3260 Typ_Size := UI_To_Int (Esize (Comp_Type));
3262 -- If the Esize (Object_Size) is unknown at compile-time, look at
3263 -- the RM_Size (Value_Size) since it may have been set by an
3264 -- explicit representation clause.
3266 -- And how do we know this is statically known???
3268 else
3269 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3270 end if;
3272 -- Retrieve all relevant atomic routines and types
3274 case Typ_Size is
3275 when 8 =>
3276 Compare := RTE (RE_Atomic_Compare_Exchange_8);
3277 Load := RTE (RE_Atomic_Load_8);
3278 Unsigned := RTE (RE_Uint8);
3280 when 16 =>
3281 Compare := RTE (RE_Atomic_Compare_Exchange_16);
3282 Load := RTE (RE_Atomic_Load_16);
3283 Unsigned := RTE (RE_Uint16);
3285 when 32 =>
3286 Compare := RTE (RE_Atomic_Compare_Exchange_32);
3287 Load := RTE (RE_Atomic_Load_32);
3288 Unsigned := RTE (RE_Uint32);
3290 when 64 =>
3291 Compare := RTE (RE_Atomic_Compare_Exchange_64);
3292 Load := RTE (RE_Atomic_Load_64);
3293 Unsigned := RTE (RE_Uint64);
3295 when others =>
3296 raise Program_Error;
3297 end case;
3299 -- Generate:
3300 -- For functions:
3302 -- Saved_Comp : constant Comp_Type :=
3303 -- Comp_Type (Atomic_Load (Comp'Address));
3305 -- For procedures:
3307 -- Saved_Comp : constant Comp_Type :=
3308 -- Comp_Type (Atomic_Load (Comp'Address),
3309 -- Relaxed);
3311 Saved_Comp :=
3312 Make_Defining_Identifier (Loc,
3313 New_External_Name (Chars (Comp), Suffix => "_saved"));
3315 Load_Params := New_List (
3316 Make_Attribute_Reference (Loc,
3317 Prefix => New_Reference_To (Comp, Loc),
3318 Attribute_Name => Name_Address));
3320 -- For protected procedures, set the memory model to be relaxed
3322 if Is_Procedure then
3323 Append_To (Load_Params,
3324 New_Reference_To (RTE (RE_Relaxed), Loc));
3325 end if;
3327 Decl :=
3328 Make_Object_Declaration (Loc,
3329 Defining_Identifier => Saved_Comp,
3330 Constant_Present => True,
3331 Object_Definition => New_Reference_To (Comp_Type, Loc),
3332 Expression =>
3333 Unchecked_Convert_To (Comp_Type,
3334 Make_Function_Call (Loc,
3335 Name => New_Reference_To (Load, Loc),
3336 Parameter_Associations => Load_Params)));
3338 -- Protected procedures
3340 if Is_Procedure then
3341 Block_Decls := New_List (Decl);
3343 -- Generate:
3344 -- Current_Comp : Comp_Type := Saved_Comp;
3346 Current_Comp :=
3347 Make_Defining_Identifier (Loc,
3348 New_External_Name (Chars (Comp), Suffix => "_current"));
3350 Append_To (Block_Decls,
3351 Make_Object_Declaration (Loc,
3352 Defining_Identifier => Current_Comp,
3353 Object_Definition => New_Reference_To (Comp_Type, Loc),
3354 Expression => New_Reference_To (Saved_Comp, Loc)));
3356 -- Protected function
3358 else
3359 Append_To (Decls, Decl);
3360 Current_Comp := Saved_Comp;
3361 end if;
3363 Process_Stmts
3364 (Stmts, Compare, Unsigned, Comp, Saved_Comp, Current_Comp);
3366 -- Generate:
3368 -- exit when System.Atomic_Primitives.Atomic_Compare_Exchange
3369 -- (Comp'Address,
3370 -- Interfaces.Unsigned (Saved_Comp),
3371 -- Interfaces.Unsigned (Current_Comp))
3373 if Is_Procedure then
3374 Stmt :=
3375 Make_Exit_Statement (Loc,
3376 Condition =>
3377 Make_Function_Call (Loc,
3378 Name =>
3379 New_Reference_To (Compare, Loc),
3380 Parameter_Associations => New_List (
3381 Make_Attribute_Reference (Loc,
3382 Prefix => New_Reference_To (Comp, Loc),
3383 Attribute_Name => Name_Address),
3385 Unchecked_Convert_To (Unsigned,
3386 New_Reference_To (Saved_Comp, Loc)),
3388 Unchecked_Convert_To (Unsigned,
3389 New_Reference_To (Current_Comp, Loc)))));
3391 -- Small optimization: transform the default return statement
3392 -- of a procedure into the atomic exit statement.
3394 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3395 Rewrite (Last (Stmts), Stmt);
3396 else
3397 Append_To (Stmts, Stmt);
3398 end if;
3399 end if;
3401 -- Create the declaration of the label used to skip the rest of
3402 -- the source statements when the object state changes.
3404 if Present (Label_Id) then
3405 Label := Make_Label (Loc, Label_Id);
3406 Append_To (Decls,
3407 Make_Implicit_Label_Declaration (Loc,
3408 Defining_Identifier => Entity (Label_Id),
3409 Label_Construct => Label));
3410 Append_To (Stmts, Label);
3411 end if;
3413 -- Generate:
3415 -- loop
3416 -- declare
3417 -- <Decls>
3418 -- begin
3419 -- <Stmts>
3420 -- end;
3421 -- end loop;
3423 if Is_Procedure then
3424 Stmts := New_List (
3425 Make_Procedure_Call_Statement (Loc,
3426 Name =>
3427 New_Reference_To (RTE (RE_Atomic_Synchronize), Loc)),
3428 Make_Loop_Statement (Loc,
3429 Statements => New_List (
3430 Make_Block_Statement (Loc,
3431 Declarations => Block_Decls,
3432 Handled_Statement_Sequence =>
3433 Make_Handled_Sequence_Of_Statements (Loc,
3434 Statements => Stmts))),
3435 End_Label => Empty));
3436 end if;
3437 end;
3438 end if;
3440 -- Add renamings for the protection object, discriminals, privals and
3441 -- the entry index constant for use by debugger.
3443 Debug_Private_Data_Declarations (Decls);
3445 -- Make an unprotected version of the subprogram for use within the same
3446 -- object, with new name and extra parameter representing the object.
3448 return
3449 Make_Subprogram_Body (Loc,
3450 Specification =>
3451 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3452 Declarations => Decls,
3453 Handled_Statement_Sequence =>
3454 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
3455 end Build_Lock_Free_Unprotected_Subprogram_Body;
3457 -------------------------
3458 -- Build_Master_Entity --
3459 -------------------------
3461 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3462 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3463 Context : Node_Id;
3464 Context_Id : Entity_Id;
3465 Decl : Node_Id;
3466 Decls : List_Id;
3467 Par : Node_Id;
3469 begin
3470 if Is_Itype (Obj_Or_Typ) then
3471 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3472 else
3473 Par := Parent (Obj_Or_Typ);
3474 end if;
3476 -- When creating a master for a record component which is either a task
3477 -- or access-to-task, the enclosing record is the master scope and the
3478 -- proper insertion point is the component list.
3480 if Is_Record_Type (Current_Scope) then
3481 Context := Par;
3482 Context_Id := Current_Scope;
3483 Decls := List_Containing (Context);
3485 -- Default case for object declarations and access types. Note that the
3486 -- context is updated to the nearest enclosing body, block, package or
3487 -- return statement.
3489 else
3490 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3491 end if;
3493 -- Do not create a master if one already exists or there is no task
3494 -- hierarchy.
3496 if Has_Master_Entity (Context_Id)
3497 or else Restriction_Active (No_Task_Hierarchy)
3498 then
3499 return;
3500 end if;
3502 -- Create a master, generate:
3503 -- _Master : constant Master_Id := Current_Master.all;
3505 Decl :=
3506 Make_Object_Declaration (Loc,
3507 Defining_Identifier =>
3508 Make_Defining_Identifier (Loc, Name_uMaster),
3509 Constant_Present => True,
3510 Object_Definition => New_Reference_To (RTE (RE_Master_Id), Loc),
3511 Expression =>
3512 Make_Explicit_Dereference (Loc,
3513 New_Reference_To (RTE (RE_Current_Master), Loc)));
3515 -- The master is inserted at the start of the declarative list of the
3516 -- context.
3518 Prepend_To (Decls, Decl);
3520 -- In certain cases where transient scopes are involved, the immediate
3521 -- scope is not always the proper master scope. Ensure that the master
3522 -- declaration and entity appear in the same context.
3524 if Context_Id /= Current_Scope then
3525 Push_Scope (Context_Id);
3526 Analyze (Decl);
3527 Pop_Scope;
3528 else
3529 Analyze (Decl);
3530 end if;
3532 -- Mark the enclosing scope and its associated construct as being task
3533 -- masters.
3535 Set_Has_Master_Entity (Context_Id);
3537 while Present (Context)
3538 and then Nkind (Context) /= N_Compilation_Unit
3539 loop
3540 if Nkind_In (Context, N_Block_Statement,
3541 N_Subprogram_Body,
3542 N_Task_Body)
3543 then
3544 Set_Is_Task_Master (Context);
3545 exit;
3547 elsif Nkind (Parent (Context)) = N_Subunit then
3548 Context := Corresponding_Stub (Parent (Context));
3549 end if;
3551 Context := Parent (Context);
3552 end loop;
3553 end Build_Master_Entity;
3555 ---------------------------
3556 -- Build_Master_Renaming --
3557 ---------------------------
3559 procedure Build_Master_Renaming
3560 (Ptr_Typ : Entity_Id;
3561 Ins_Nod : Node_Id := Empty)
3563 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3564 Context : Node_Id;
3565 Master_Decl : Node_Id;
3566 Master_Id : Entity_Id;
3568 begin
3569 -- Nothing to do if there is no task hierarchy
3571 if Restriction_Active (No_Task_Hierarchy) then
3572 return;
3573 end if;
3575 -- Determine the proper context to insert the master renaming
3577 if Present (Ins_Nod) then
3578 Context := Ins_Nod;
3579 elsif Is_Itype (Ptr_Typ) then
3580 Context := Associated_Node_For_Itype (Ptr_Typ);
3581 else
3582 Context := Parent (Ptr_Typ);
3583 end if;
3585 -- Generate:
3586 -- <Ptr_Typ>M : Master_Id renames _Master;
3588 Master_Id :=
3589 Make_Defining_Identifier (Loc,
3590 New_External_Name (Chars (Ptr_Typ), 'M'));
3592 Master_Decl :=
3593 Make_Object_Renaming_Declaration (Loc,
3594 Defining_Identifier => Master_Id,
3595 Subtype_Mark => New_Reference_To (RTE (RE_Master_Id), Loc),
3596 Name => Make_Identifier (Loc, Name_uMaster));
3598 Insert_Action (Context, Master_Decl);
3600 -- The renamed master now services the access type
3602 Set_Master_Id (Ptr_Typ, Master_Id);
3603 end Build_Master_Renaming;
3605 -----------------------------------------
3606 -- Build_Private_Protected_Declaration --
3607 -----------------------------------------
3609 function Build_Private_Protected_Declaration
3610 (N : Node_Id) return Entity_Id
3612 Loc : constant Source_Ptr := Sloc (N);
3613 Body_Id : constant Entity_Id := Defining_Entity (N);
3614 Decl : Node_Id;
3615 Plist : List_Id;
3616 Formal : Entity_Id;
3617 New_Spec : Node_Id;
3618 Spec_Id : Entity_Id;
3620 begin
3621 Formal := First_Formal (Body_Id);
3623 -- The protected operation always has at least one formal, namely the
3624 -- object itself, but it is only placed in the parameter list if
3625 -- expansion is enabled.
3627 if Present (Formal) or else Expander_Active then
3628 Plist := Copy_Parameter_List (Body_Id);
3629 else
3630 Plist := No_List;
3631 end if;
3633 if Nkind (Specification (N)) = N_Procedure_Specification then
3634 New_Spec :=
3635 Make_Procedure_Specification (Loc,
3636 Defining_Unit_Name =>
3637 Make_Defining_Identifier (Sloc (Body_Id),
3638 Chars => Chars (Body_Id)),
3639 Parameter_Specifications =>
3640 Plist);
3641 else
3642 New_Spec :=
3643 Make_Function_Specification (Loc,
3644 Defining_Unit_Name =>
3645 Make_Defining_Identifier (Sloc (Body_Id),
3646 Chars => Chars (Body_Id)),
3647 Parameter_Specifications => Plist,
3648 Result_Definition =>
3649 New_Occurrence_Of (Etype (Body_Id), Loc));
3650 end if;
3652 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3653 Insert_Before (N, Decl);
3654 Spec_Id := Defining_Unit_Name (New_Spec);
3656 -- Indicate that the entity comes from source, to ensure that cross-
3657 -- reference information is properly generated. The body itself is
3658 -- rewritten during expansion, and the body entity will not appear in
3659 -- calls to the operation.
3661 Set_Comes_From_Source (Spec_Id, True);
3662 Analyze (Decl);
3663 Set_Has_Completion (Spec_Id);
3664 Set_Convention (Spec_Id, Convention_Protected);
3665 return Spec_Id;
3666 end Build_Private_Protected_Declaration;
3668 ---------------------------
3669 -- Build_Protected_Entry --
3670 ---------------------------
3672 function Build_Protected_Entry
3673 (N : Node_Id;
3674 Ent : Entity_Id;
3675 Pid : Node_Id) return Node_Id
3677 Loc : constant Source_Ptr := Sloc (N);
3679 Decls : constant List_Id := Declarations (N);
3680 End_Lab : constant Node_Id :=
3681 End_Label (Handled_Statement_Sequence (N));
3682 End_Loc : constant Source_Ptr :=
3683 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3684 -- Used for the generated call to Complete_Entry_Body
3686 Han_Loc : Source_Ptr;
3687 -- Used for the exception handler, inserted at end of the body
3689 Op_Decls : constant List_Id := New_List;
3690 Complete : Node_Id;
3691 Edef : Entity_Id;
3692 Espec : Node_Id;
3693 Ohandle : Node_Id;
3694 Op_Stats : List_Id;
3696 begin
3697 -- Set the source location on the exception handler only when debugging
3698 -- the expanded code (see Make_Implicit_Exception_Handler).
3700 if Debug_Generated_Code then
3701 Han_Loc := End_Loc;
3703 -- Otherwise the inserted code should not be visible to the debugger
3705 else
3706 Han_Loc := No_Location;
3707 end if;
3709 Edef :=
3710 Make_Defining_Identifier (Loc,
3711 Chars => Chars (Protected_Body_Subprogram (Ent)));
3712 Espec :=
3713 Build_Protected_Entry_Specification (Loc, Edef, Empty);
3715 -- Add the following declarations:
3716 -- type poVP is access poV;
3717 -- _object : poVP := poVP (_O);
3719 -- where _O is the formal parameter associated with the concurrent
3720 -- object. These declarations are needed for Complete_Entry_Body.
3722 Add_Object_Pointer (Loc, Pid, Op_Decls);
3724 -- Add renamings for all formals, the Protection object, discriminals,
3725 -- privals and the entry index constant for use by debugger.
3727 Add_Formal_Renamings (Espec, Op_Decls, Ent, Loc);
3728 Debug_Private_Data_Declarations (Decls);
3730 case Corresponding_Runtime_Package (Pid) is
3731 when System_Tasking_Protected_Objects_Entries =>
3732 Complete :=
3733 New_Reference_To (RTE (RE_Complete_Entry_Body), Loc);
3735 when System_Tasking_Protected_Objects_Single_Entry =>
3736 Complete :=
3737 New_Reference_To (RTE (RE_Complete_Single_Entry_Body), Loc);
3739 when others =>
3740 raise Program_Error;
3741 end case;
3743 Op_Stats := New_List (
3744 Make_Block_Statement (Loc,
3745 Declarations => Decls,
3746 Handled_Statement_Sequence =>
3747 Handled_Statement_Sequence (N)),
3749 Make_Procedure_Call_Statement (End_Loc,
3750 Name => Complete,
3751 Parameter_Associations => New_List (
3752 Make_Attribute_Reference (End_Loc,
3753 Prefix =>
3754 Make_Selected_Component (End_Loc,
3755 Prefix => Make_Identifier (End_Loc, Name_uObject),
3756 Selector_Name => Make_Identifier (End_Loc, Name_uObject)),
3757 Attribute_Name => Name_Unchecked_Access))));
3759 -- When exceptions can not be propagated, we never need to call
3760 -- Exception_Complete_Entry_Body
3762 if No_Exception_Handlers_Set then
3763 return
3764 Make_Subprogram_Body (Loc,
3765 Specification => Espec,
3766 Declarations => Op_Decls,
3767 Handled_Statement_Sequence =>
3768 Make_Handled_Sequence_Of_Statements (Loc,
3769 Statements => Op_Stats,
3770 End_Label => End_Lab));
3772 else
3773 Ohandle := Make_Others_Choice (Loc);
3774 Set_All_Others (Ohandle);
3776 case Corresponding_Runtime_Package (Pid) is
3777 when System_Tasking_Protected_Objects_Entries =>
3778 Complete :=
3779 New_Reference_To
3780 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3782 when System_Tasking_Protected_Objects_Single_Entry =>
3783 Complete :=
3784 New_Reference_To
3785 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3787 when others =>
3788 raise Program_Error;
3789 end case;
3791 -- Establish link between subprogram body entity and source entry
3793 Set_Corresponding_Protected_Entry (Edef, Ent);
3795 -- Create body of entry procedure. The renaming declarations are
3796 -- placed ahead of the block that contains the actual entry body.
3798 return
3799 Make_Subprogram_Body (Loc,
3800 Specification => Espec,
3801 Declarations => Op_Decls,
3802 Handled_Statement_Sequence =>
3803 Make_Handled_Sequence_Of_Statements (Loc,
3804 Statements => Op_Stats,
3805 End_Label => End_Lab,
3806 Exception_Handlers => New_List (
3807 Make_Implicit_Exception_Handler (Han_Loc,
3808 Exception_Choices => New_List (Ohandle),
3810 Statements => New_List (
3811 Make_Procedure_Call_Statement (Han_Loc,
3812 Name => Complete,
3813 Parameter_Associations => New_List (
3814 Make_Attribute_Reference (Han_Loc,
3815 Prefix =>
3816 Make_Selected_Component (Han_Loc,
3817 Prefix =>
3818 Make_Identifier (Han_Loc, Name_uObject),
3819 Selector_Name =>
3820 Make_Identifier (Han_Loc, Name_uObject)),
3821 Attribute_Name => Name_Unchecked_Access),
3823 Make_Function_Call (Han_Loc,
3824 Name => New_Reference_To (
3825 RTE (RE_Get_GNAT_Exception), Loc)))))))));
3826 end if;
3827 end Build_Protected_Entry;
3829 -----------------------------------------
3830 -- Build_Protected_Entry_Specification --
3831 -----------------------------------------
3833 function Build_Protected_Entry_Specification
3834 (Loc : Source_Ptr;
3835 Def_Id : Entity_Id;
3836 Ent_Id : Entity_Id) return Node_Id
3838 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3840 begin
3841 Set_Debug_Info_Needed (Def_Id);
3843 if Present (Ent_Id) then
3844 Append_Elmt (P, Accept_Address (Ent_Id));
3845 end if;
3847 return
3848 Make_Procedure_Specification (Loc,
3849 Defining_Unit_Name => Def_Id,
3850 Parameter_Specifications => New_List (
3851 Make_Parameter_Specification (Loc,
3852 Defining_Identifier =>
3853 Make_Defining_Identifier (Loc, Name_uO),
3854 Parameter_Type =>
3855 New_Reference_To (RTE (RE_Address), Loc)),
3857 Make_Parameter_Specification (Loc,
3858 Defining_Identifier => P,
3859 Parameter_Type =>
3860 New_Reference_To (RTE (RE_Address), Loc)),
3862 Make_Parameter_Specification (Loc,
3863 Defining_Identifier =>
3864 Make_Defining_Identifier (Loc, Name_uE),
3865 Parameter_Type =>
3866 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc))));
3867 end Build_Protected_Entry_Specification;
3869 --------------------------
3870 -- Build_Protected_Spec --
3871 --------------------------
3873 function Build_Protected_Spec
3874 (N : Node_Id;
3875 Obj_Type : Entity_Id;
3876 Ident : Entity_Id;
3877 Unprotected : Boolean := False) return List_Id
3879 Loc : constant Source_Ptr := Sloc (N);
3880 Decl : Node_Id;
3881 Formal : Entity_Id;
3882 New_Plist : List_Id;
3883 New_Param : Node_Id;
3885 begin
3886 New_Plist := New_List;
3888 Formal := First_Formal (Ident);
3889 while Present (Formal) loop
3890 New_Param :=
3891 Make_Parameter_Specification (Loc,
3892 Defining_Identifier =>
3893 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
3894 In_Present => In_Present (Parent (Formal)),
3895 Out_Present => Out_Present (Parent (Formal)),
3896 Parameter_Type => New_Reference_To (Etype (Formal), Loc));
3898 if Unprotected then
3899 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
3900 end if;
3902 Append (New_Param, New_Plist);
3903 Next_Formal (Formal);
3904 end loop;
3906 -- If the subprogram is a procedure and the context is not an access
3907 -- to protected subprogram, the parameter is in-out. Otherwise it is
3908 -- an in parameter.
3910 Decl :=
3911 Make_Parameter_Specification (Loc,
3912 Defining_Identifier =>
3913 Make_Defining_Identifier (Loc, Name_uObject),
3914 In_Present => True,
3915 Out_Present =>
3916 (Etype (Ident) = Standard_Void_Type
3917 and then not Is_RTE (Obj_Type, RE_Address)),
3918 Parameter_Type =>
3919 New_Reference_To (Obj_Type, Loc));
3920 Set_Debug_Info_Needed (Defining_Identifier (Decl));
3921 Prepend_To (New_Plist, Decl);
3923 return New_Plist;
3924 end Build_Protected_Spec;
3926 ---------------------------------------
3927 -- Build_Protected_Sub_Specification --
3928 ---------------------------------------
3930 function Build_Protected_Sub_Specification
3931 (N : Node_Id;
3932 Prot_Typ : Entity_Id;
3933 Mode : Subprogram_Protection_Mode) return Node_Id
3935 Loc : constant Source_Ptr := Sloc (N);
3936 Decl : Node_Id;
3937 Def_Id : Entity_Id;
3938 New_Id : Entity_Id;
3939 New_Plist : List_Id;
3940 New_Spec : Node_Id;
3942 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
3943 (Dispatching_Mode => ' ',
3944 Protected_Mode => 'P',
3945 Unprotected_Mode => 'N');
3947 begin
3948 if Ekind (Defining_Unit_Name (Specification (N))) =
3949 E_Subprogram_Body
3950 then
3951 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
3952 else
3953 Decl := N;
3954 end if;
3956 Def_Id := Defining_Unit_Name (Specification (Decl));
3958 New_Plist :=
3959 Build_Protected_Spec
3960 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
3961 Mode = Unprotected_Mode);
3962 New_Id :=
3963 Make_Defining_Identifier (Loc,
3964 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
3966 -- The unprotected operation carries the user code, and debugging
3967 -- information must be generated for it, even though this spec does
3968 -- not come from source. It is also convenient to allow gdb to step
3969 -- into the protected operation, even though it only contains lock/
3970 -- unlock calls.
3972 Set_Debug_Info_Needed (New_Id);
3974 -- If a pragma Eliminate applies to the source entity, the internal
3975 -- subprograms will be eliminated as well.
3977 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
3979 if Nkind (Specification (Decl)) = N_Procedure_Specification then
3980 New_Spec :=
3981 Make_Procedure_Specification (Loc,
3982 Defining_Unit_Name => New_Id,
3983 Parameter_Specifications => New_Plist);
3985 -- Create a new specification for the anonymous subprogram type
3987 else
3988 New_Spec :=
3989 Make_Function_Specification (Loc,
3990 Defining_Unit_Name => New_Id,
3991 Parameter_Specifications => New_Plist,
3992 Result_Definition =>
3993 Copy_Result_Type (Result_Definition (Specification (Decl))));
3995 Set_Return_Present (Defining_Unit_Name (New_Spec));
3996 end if;
3998 return New_Spec;
3999 end Build_Protected_Sub_Specification;
4001 -------------------------------------
4002 -- Build_Protected_Subprogram_Body --
4003 -------------------------------------
4005 function Build_Protected_Subprogram_Body
4006 (N : Node_Id;
4007 Pid : Node_Id;
4008 N_Op_Spec : Node_Id) return Node_Id
4010 Loc : constant Source_Ptr := Sloc (N);
4011 Op_Spec : Node_Id;
4012 P_Op_Spec : Node_Id;
4013 Uactuals : List_Id;
4014 Pformal : Node_Id;
4015 Unprot_Call : Node_Id;
4016 Sub_Body : Node_Id;
4017 Lock_Name : Node_Id;
4018 Lock_Stmt : Node_Id;
4019 Service_Name : Node_Id;
4020 R : Node_Id;
4021 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
4022 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
4023 Stmts : List_Id;
4024 Object_Parm : Node_Id;
4025 Exc_Safe : Boolean;
4026 Lock_Kind : RE_Id;
4028 begin
4029 Op_Spec := Specification (N);
4030 Exc_Safe := Is_Exception_Safe (N);
4032 P_Op_Spec :=
4033 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
4035 -- Build a list of the formal parameters of the protected version of
4036 -- the subprogram to use as the actual parameters of the unprotected
4037 -- version.
4039 Uactuals := New_List;
4040 Pformal := First (Parameter_Specifications (P_Op_Spec));
4041 while Present (Pformal) loop
4042 Append_To (Uactuals,
4043 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
4044 Next (Pformal);
4045 end loop;
4047 -- Make a call to the unprotected version of the subprogram built above
4048 -- for use by the protected version built below.
4050 if Nkind (Op_Spec) = N_Function_Specification then
4051 if Exc_Safe then
4052 R := Make_Temporary (Loc, 'R');
4053 Unprot_Call :=
4054 Make_Object_Declaration (Loc,
4055 Defining_Identifier => R,
4056 Constant_Present => True,
4057 Object_Definition => New_Copy (Result_Definition (N_Op_Spec)),
4058 Expression =>
4059 Make_Function_Call (Loc,
4060 Name => Make_Identifier (Loc,
4061 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4062 Parameter_Associations => Uactuals));
4064 Return_Stmt :=
4065 Make_Simple_Return_Statement (Loc,
4066 Expression => New_Reference_To (R, Loc));
4068 else
4069 Unprot_Call := Make_Simple_Return_Statement (Loc,
4070 Expression => Make_Function_Call (Loc,
4071 Name =>
4072 Make_Identifier (Loc,
4073 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
4074 Parameter_Associations => Uactuals));
4075 end if;
4077 Lock_Kind := RE_Lock_Read_Only;
4079 else
4080 Unprot_Call :=
4081 Make_Procedure_Call_Statement (Loc,
4082 Name =>
4083 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
4084 Parameter_Associations => Uactuals);
4086 Lock_Kind := RE_Lock;
4087 end if;
4089 -- Wrap call in block that will be covered by an at_end handler
4091 if not Exc_Safe then
4092 Unprot_Call := Make_Block_Statement (Loc,
4093 Handled_Statement_Sequence =>
4094 Make_Handled_Sequence_Of_Statements (Loc,
4095 Statements => New_List (Unprot_Call)));
4096 end if;
4098 -- Make the protected subprogram body. This locks the protected
4099 -- object and calls the unprotected version of the subprogram.
4101 case Corresponding_Runtime_Package (Pid) is
4102 when System_Tasking_Protected_Objects_Entries =>
4103 Lock_Name := New_Reference_To (RTE (RE_Lock_Entries), Loc);
4104 Service_Name := New_Reference_To (RTE (RE_Service_Entries), Loc);
4106 when System_Tasking_Protected_Objects_Single_Entry =>
4107 Lock_Name := New_Reference_To (RTE (RE_Lock_Entry), Loc);
4108 Service_Name := New_Reference_To (RTE (RE_Service_Entry), Loc);
4110 when System_Tasking_Protected_Objects =>
4111 Lock_Name := New_Reference_To (RTE (Lock_Kind), Loc);
4112 Service_Name := New_Reference_To (RTE (RE_Unlock), Loc);
4114 when others =>
4115 raise Program_Error;
4116 end case;
4118 Object_Parm :=
4119 Make_Attribute_Reference (Loc,
4120 Prefix =>
4121 Make_Selected_Component (Loc,
4122 Prefix => Make_Identifier (Loc, Name_uObject),
4123 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4124 Attribute_Name => Name_Unchecked_Access);
4126 Lock_Stmt := Make_Procedure_Call_Statement (Loc,
4127 Name => Lock_Name,
4128 Parameter_Associations => New_List (Object_Parm));
4130 if Abort_Allowed then
4131 Stmts := New_List (
4132 Make_Procedure_Call_Statement (Loc,
4133 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
4134 Parameter_Associations => Empty_List),
4135 Lock_Stmt);
4137 else
4138 Stmts := New_List (Lock_Stmt);
4139 end if;
4141 if not Exc_Safe then
4142 Append (Unprot_Call, Stmts);
4143 else
4144 if Nkind (Op_Spec) = N_Function_Specification then
4145 Pre_Stmts := Stmts;
4146 Stmts := Empty_List;
4147 else
4148 Append (Unprot_Call, Stmts);
4149 end if;
4151 Append (
4152 Make_Procedure_Call_Statement (Loc,
4153 Name => Service_Name,
4154 Parameter_Associations =>
4155 New_List (New_Copy_Tree (Object_Parm))),
4156 Stmts);
4158 if Abort_Allowed then
4159 Append (
4160 Make_Procedure_Call_Statement (Loc,
4161 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
4162 Parameter_Associations => Empty_List),
4163 Stmts);
4164 end if;
4166 if Nkind (Op_Spec) = N_Function_Specification then
4167 Append (Return_Stmt, Stmts);
4168 Append (Make_Block_Statement (Loc,
4169 Declarations => New_List (Unprot_Call),
4170 Handled_Statement_Sequence =>
4171 Make_Handled_Sequence_Of_Statements (Loc,
4172 Statements => Stmts)), Pre_Stmts);
4173 Stmts := Pre_Stmts;
4174 end if;
4175 end if;
4177 Sub_Body :=
4178 Make_Subprogram_Body (Loc,
4179 Declarations => Empty_List,
4180 Specification => P_Op_Spec,
4181 Handled_Statement_Sequence =>
4182 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4184 if not Exc_Safe then
4185 Set_Is_Protected_Subprogram_Body (Sub_Body);
4186 end if;
4188 return Sub_Body;
4189 end Build_Protected_Subprogram_Body;
4191 -------------------------------------
4192 -- Build_Protected_Subprogram_Call --
4193 -------------------------------------
4195 procedure Build_Protected_Subprogram_Call
4196 (N : Node_Id;
4197 Name : Node_Id;
4198 Rec : Node_Id;
4199 External : Boolean := True)
4201 Loc : constant Source_Ptr := Sloc (N);
4202 Sub : constant Entity_Id := Entity (Name);
4203 New_Sub : Node_Id;
4204 Params : List_Id;
4206 begin
4207 if External then
4208 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4209 else
4210 New_Sub :=
4211 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4212 end if;
4214 if Present (Parameter_Associations (N)) then
4215 Params := New_Copy_List_Tree (Parameter_Associations (N));
4216 else
4217 Params := New_List;
4218 end if;
4220 -- If the type is an untagged derived type, convert to the root type,
4221 -- which is the one on which the operations are defined.
4223 if Nkind (Rec) = N_Unchecked_Type_Conversion
4224 and then not Is_Tagged_Type (Etype (Rec))
4225 and then Is_Derived_Type (Etype (Rec))
4226 then
4227 Set_Etype (Rec, Root_Type (Etype (Rec)));
4228 Set_Subtype_Mark (Rec,
4229 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4230 end if;
4232 Prepend (Rec, Params);
4234 if Ekind (Sub) = E_Procedure then
4235 Rewrite (N,
4236 Make_Procedure_Call_Statement (Loc,
4237 Name => New_Sub,
4238 Parameter_Associations => Params));
4240 else
4241 pragma Assert (Ekind (Sub) = E_Function);
4242 Rewrite (N,
4243 Make_Function_Call (Loc,
4244 Name => New_Sub,
4245 Parameter_Associations => Params));
4246 end if;
4248 if External
4249 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4250 and then Is_Entity_Name (Expression (Rec))
4251 and then Is_Shared_Passive (Entity (Expression (Rec)))
4252 then
4253 Add_Shared_Var_Lock_Procs (N);
4254 end if;
4255 end Build_Protected_Subprogram_Call;
4257 -------------------------
4258 -- Build_Selected_Name --
4259 -------------------------
4261 function Build_Selected_Name
4262 (Prefix : Entity_Id;
4263 Selector : Entity_Id;
4264 Append_Char : Character := ' ') return Name_Id
4266 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4267 Select_Len : Natural;
4269 begin
4270 Get_Name_String (Chars (Selector));
4271 Select_Len := Name_Len;
4272 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4273 Get_Name_String (Chars (Prefix));
4275 -- If scope is anonymous type, discard suffix to recover name of
4276 -- single protected object. Otherwise use protected type name.
4278 if Name_Buffer (Name_Len) = 'T' then
4279 Name_Len := Name_Len - 1;
4280 end if;
4282 Add_Str_To_Name_Buffer ("__");
4283 for J in 1 .. Select_Len loop
4284 Add_Char_To_Name_Buffer (Select_Buffer (J));
4285 end loop;
4287 -- Now add the Append_Char if specified. The encoding to follow
4288 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4289 -- then the entity is associated to a protected type subprogram.
4290 -- Otherwise, it is a protected type entry. For each case, the
4291 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4293 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4295 if Append_Char /= ' ' then
4296 if Append_Char = 'P' or Append_Char = 'N' then
4297 Add_Char_To_Name_Buffer (Append_Char);
4298 return Name_Find;
4299 else
4300 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4301 return New_External_Name (Name_Find, ' ', -1);
4302 end if;
4303 else
4304 return Name_Find;
4305 end if;
4306 end Build_Selected_Name;
4308 -----------------------------
4309 -- Build_Simple_Entry_Call --
4310 -----------------------------
4312 -- A task entry call is converted to a call to Call_Simple
4314 -- declare
4315 -- P : parms := (parm, parm, parm);
4316 -- begin
4317 -- Call_Simple (acceptor-task, entry-index, P'Address);
4318 -- parm := P.param;
4319 -- parm := P.param;
4320 -- ...
4321 -- end;
4323 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4324 -- the parameters, and the constructed aggregate value contains either the
4325 -- parameters or, in the case of non-elementary types, references to these
4326 -- parameters. Then the address of this aggregate is passed to the runtime
4327 -- routine, along with the task id value and the task entry index value.
4328 -- Pnn is only required if parameters are present.
4330 -- The assignments after the call are present only in the case of in-out
4331 -- or out parameters for elementary types, and are used to assign back the
4332 -- resulting values of such parameters.
4334 -- Note: the reason that we insert a block here is that in the context
4335 -- of selects, conditional entry calls etc. the entry call statement
4336 -- appears on its own, not as an element of a list.
4338 -- A protected entry call is converted to a Protected_Entry_Call:
4340 -- declare
4341 -- P : E1_Params := (param, param, param);
4342 -- Pnn : Boolean;
4343 -- Bnn : Communications_Block;
4345 -- declare
4346 -- P : E1_Params := (param, param, param);
4347 -- Bnn : Communications_Block;
4349 -- begin
4350 -- Protected_Entry_Call (
4351 -- Object => po._object'Access,
4352 -- E => <entry index>;
4353 -- Uninterpreted_Data => P'Address;
4354 -- Mode => Simple_Call;
4355 -- Block => Bnn);
4356 -- parm := P.param;
4357 -- parm := P.param;
4358 -- ...
4359 -- end;
4361 procedure Build_Simple_Entry_Call
4362 (N : Node_Id;
4363 Concval : Node_Id;
4364 Ename : Node_Id;
4365 Index : Node_Id)
4367 begin
4368 Expand_Call (N);
4370 -- If call has been inlined, nothing left to do
4372 if Nkind (N) = N_Block_Statement then
4373 return;
4374 end if;
4376 -- Convert entry call to Call_Simple call
4378 declare
4379 Loc : constant Source_Ptr := Sloc (N);
4380 Parms : constant List_Id := Parameter_Associations (N);
4381 Stats : constant List_Id := New_List;
4382 Actual : Node_Id;
4383 Call : Node_Id;
4384 Comm_Name : Entity_Id;
4385 Conctyp : Node_Id;
4386 Decls : List_Id;
4387 Ent : Entity_Id;
4388 Ent_Acc : Entity_Id;
4389 Formal : Node_Id;
4390 Iface_Tag : Entity_Id;
4391 Iface_Typ : Entity_Id;
4392 N_Node : Node_Id;
4393 N_Var : Node_Id;
4394 P : Entity_Id;
4395 Parm1 : Node_Id;
4396 Parm2 : Node_Id;
4397 Parm3 : Node_Id;
4398 Pdecl : Node_Id;
4399 Plist : List_Id;
4400 X : Entity_Id;
4401 Xdecl : Node_Id;
4403 begin
4404 -- Simple entry and entry family cases merge here
4406 Ent := Entity (Ename);
4407 Ent_Acc := Entry_Parameters_Type (Ent);
4408 Conctyp := Etype (Concval);
4410 -- If prefix is an access type, dereference to obtain the task type
4412 if Is_Access_Type (Conctyp) then
4413 Conctyp := Designated_Type (Conctyp);
4414 end if;
4416 -- Special case for protected subprogram calls
4418 if Is_Protected_Type (Conctyp)
4419 and then Is_Subprogram (Entity (Ename))
4420 then
4421 if not Is_Eliminated (Entity (Ename)) then
4422 Build_Protected_Subprogram_Call
4423 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4424 Analyze (N);
4425 end if;
4427 return;
4428 end if;
4430 -- First parameter is the Task_Id value from the task value or the
4431 -- Object from the protected object value, obtained by selecting
4432 -- the _Task_Id or _Object from the result of doing an unchecked
4433 -- conversion to convert the value to the corresponding record type.
4435 if Nkind (Concval) = N_Function_Call
4436 and then Is_Task_Type (Conctyp)
4437 and then Ada_Version >= Ada_2005
4438 then
4439 declare
4440 ExpR : constant Node_Id := Relocate_Node (Concval);
4441 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4442 Decl : Node_Id;
4444 begin
4445 Decl :=
4446 Make_Object_Declaration (Loc,
4447 Defining_Identifier => Obj,
4448 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4449 Expression => ExpR);
4450 Set_Etype (Obj, Conctyp);
4451 Decls := New_List (Decl);
4452 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4453 end;
4455 else
4456 Decls := New_List;
4457 end if;
4459 Parm1 := Concurrent_Ref (Concval);
4461 -- Second parameter is the entry index, computed by the routine
4462 -- provided for this purpose. The value of this expression is
4463 -- assigned to an intermediate variable to assure that any entry
4464 -- family index expressions are evaluated before the entry
4465 -- parameters.
4467 if Abort_Allowed
4468 or else Restriction_Active (No_Entry_Queue) = False
4469 or else not Is_Protected_Type (Conctyp)
4470 or else Number_Entries (Conctyp) > 1
4471 or else (Has_Attach_Handler (Conctyp)
4472 and then not Restricted_Profile)
4473 then
4474 X := Make_Defining_Identifier (Loc, Name_uX);
4476 Xdecl :=
4477 Make_Object_Declaration (Loc,
4478 Defining_Identifier => X,
4479 Object_Definition =>
4480 New_Reference_To (RTE (RE_Task_Entry_Index), Loc),
4481 Expression => Actual_Index_Expression (
4482 Loc, Entity (Ename), Index, Concval));
4484 Append_To (Decls, Xdecl);
4485 Parm2 := New_Reference_To (X, Loc);
4487 else
4488 Xdecl := Empty;
4489 Parm2 := Empty;
4490 end if;
4492 -- The third parameter is the packaged parameters. If there are
4493 -- none, then it is just the null address, since nothing is passed.
4495 if No (Parms) then
4496 Parm3 := New_Reference_To (RTE (RE_Null_Address), Loc);
4497 P := Empty;
4499 -- Case of parameters present, where third argument is the address
4500 -- of a packaged record containing the required parameter values.
4502 else
4503 -- First build a list of parameter values, which are references to
4504 -- objects of the parameter types.
4506 Plist := New_List;
4508 Actual := First_Actual (N);
4509 Formal := First_Formal (Ent);
4511 while Present (Actual) loop
4513 -- If it is a by_copy_type, copy it to a new variable. The
4514 -- packaged record has a field that points to this variable.
4516 if Is_By_Copy_Type (Etype (Actual)) then
4517 N_Node :=
4518 Make_Object_Declaration (Loc,
4519 Defining_Identifier => Make_Temporary (Loc, 'J'),
4520 Aliased_Present => True,
4521 Object_Definition =>
4522 New_Reference_To (Etype (Formal), Loc));
4524 -- Mark the object as not needing initialization since the
4525 -- initialization is performed separately, avoiding errors
4526 -- on cases such as formals of null-excluding access types.
4528 Set_No_Initialization (N_Node);
4530 -- We must make an assignment statement separate for the
4531 -- case of limited type. We cannot assign it unless the
4532 -- Assignment_OK flag is set first. An out formal of an
4533 -- access type must also be initialized from the actual,
4534 -- as stated in RM 6.4.1 (13).
4536 if Ekind (Formal) /= E_Out_Parameter
4537 or else Is_Access_Type (Etype (Formal))
4538 then
4539 N_Var :=
4540 New_Reference_To (Defining_Identifier (N_Node), Loc);
4541 Set_Assignment_OK (N_Var);
4542 Append_To (Stats,
4543 Make_Assignment_Statement (Loc,
4544 Name => N_Var,
4545 Expression => Relocate_Node (Actual)));
4546 end if;
4548 Append (N_Node, Decls);
4550 Append_To (Plist,
4551 Make_Attribute_Reference (Loc,
4552 Attribute_Name => Name_Unchecked_Access,
4553 Prefix =>
4554 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4556 -- If it is a VM_By_Copy_Actual, copy it to a new variable
4558 elsif Is_VM_By_Copy_Actual (Actual) then
4559 N_Node :=
4560 Make_Object_Declaration (Loc,
4561 Defining_Identifier => Make_Temporary (Loc, 'J'),
4562 Aliased_Present => True,
4563 Object_Definition =>
4564 New_Reference_To (Etype (Formal), Loc),
4565 Expression => New_Copy_Tree (Actual));
4566 Set_Assignment_OK (N_Node);
4568 Append (N_Node, Decls);
4570 Append_To (Plist,
4571 Make_Attribute_Reference (Loc,
4572 Attribute_Name => Name_Unchecked_Access,
4573 Prefix =>
4574 New_Reference_To (Defining_Identifier (N_Node), Loc)));
4576 else
4577 -- Interface class-wide formal
4579 if Ada_Version >= Ada_2005
4580 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4581 and then Is_Interface (Etype (Formal))
4582 then
4583 Iface_Typ := Etype (Etype (Formal));
4585 -- Generate:
4586 -- formal_iface_type! (actual.iface_tag)'reference
4588 Iface_Tag :=
4589 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4590 pragma Assert (Present (Iface_Tag));
4592 Append_To (Plist,
4593 Make_Reference (Loc,
4594 Unchecked_Convert_To (Iface_Typ,
4595 Make_Selected_Component (Loc,
4596 Prefix =>
4597 Relocate_Node (Actual),
4598 Selector_Name =>
4599 New_Reference_To (Iface_Tag, Loc)))));
4600 else
4601 -- Generate:
4602 -- actual'reference
4604 Append_To (Plist,
4605 Make_Reference (Loc, Relocate_Node (Actual)));
4606 end if;
4607 end if;
4609 Next_Actual (Actual);
4610 Next_Formal_With_Extras (Formal);
4611 end loop;
4613 -- Now build the declaration of parameters initialized with the
4614 -- aggregate containing this constructed parameter list.
4616 P := Make_Defining_Identifier (Loc, Name_uP);
4618 Pdecl :=
4619 Make_Object_Declaration (Loc,
4620 Defining_Identifier => P,
4621 Object_Definition =>
4622 New_Reference_To (Designated_Type (Ent_Acc), Loc),
4623 Expression =>
4624 Make_Aggregate (Loc, Expressions => Plist));
4626 Parm3 :=
4627 Make_Attribute_Reference (Loc,
4628 Prefix => New_Reference_To (P, Loc),
4629 Attribute_Name => Name_Address);
4631 Append (Pdecl, Decls);
4632 end if;
4634 -- Now we can create the call, case of protected type
4636 if Is_Protected_Type (Conctyp) then
4637 case Corresponding_Runtime_Package (Conctyp) is
4638 when System_Tasking_Protected_Objects_Entries =>
4640 -- Change the type of the index declaration
4642 Set_Object_Definition (Xdecl,
4643 New_Reference_To (RTE (RE_Protected_Entry_Index), Loc));
4645 -- Some additional declarations for protected entry calls
4647 if No (Decls) then
4648 Decls := New_List;
4649 end if;
4651 -- Bnn : Communications_Block;
4653 Comm_Name := Make_Temporary (Loc, 'B');
4655 Append_To (Decls,
4656 Make_Object_Declaration (Loc,
4657 Defining_Identifier => Comm_Name,
4658 Object_Definition =>
4659 New_Reference_To (RTE (RE_Communication_Block), Loc)));
4661 -- Some additional statements for protected entry calls
4663 -- Protected_Entry_Call (
4664 -- Object => po._object'Access,
4665 -- E => <entry index>;
4666 -- Uninterpreted_Data => P'Address;
4667 -- Mode => Simple_Call;
4668 -- Block => Bnn);
4670 Call :=
4671 Make_Procedure_Call_Statement (Loc,
4672 Name =>
4673 New_Reference_To (RTE (RE_Protected_Entry_Call), Loc),
4675 Parameter_Associations => New_List (
4676 Make_Attribute_Reference (Loc,
4677 Attribute_Name => Name_Unchecked_Access,
4678 Prefix => Parm1),
4679 Parm2,
4680 Parm3,
4681 New_Reference_To (RTE (RE_Simple_Call), Loc),
4682 New_Occurrence_Of (Comm_Name, Loc)));
4684 when System_Tasking_Protected_Objects_Single_Entry =>
4685 -- Protected_Single_Entry_Call (
4686 -- Object => po._object'Access,
4687 -- Uninterpreted_Data => P'Address;
4688 -- Mode => Simple_Call);
4690 Call :=
4691 Make_Procedure_Call_Statement (Loc,
4692 Name => New_Reference_To (
4693 RTE (RE_Protected_Single_Entry_Call), Loc),
4695 Parameter_Associations => New_List (
4696 Make_Attribute_Reference (Loc,
4697 Attribute_Name => Name_Unchecked_Access,
4698 Prefix => Parm1),
4699 Parm3,
4700 New_Reference_To (RTE (RE_Simple_Call), Loc)));
4702 when others =>
4703 raise Program_Error;
4704 end case;
4706 -- Case of task type
4708 else
4709 Call :=
4710 Make_Procedure_Call_Statement (Loc,
4711 Name => New_Reference_To (RTE (RE_Call_Simple), Loc),
4712 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4714 end if;
4716 Append_To (Stats, Call);
4718 -- If there are out or in/out parameters by copy add assignment
4719 -- statements for the result values.
4721 if Present (Parms) then
4722 Actual := First_Actual (N);
4723 Formal := First_Formal (Ent);
4725 Set_Assignment_OK (Actual);
4726 while Present (Actual) loop
4727 if (Is_By_Copy_Type (Etype (Actual))
4728 or else Is_VM_By_Copy_Actual (Actual))
4729 and then Ekind (Formal) /= E_In_Parameter
4730 then
4731 N_Node :=
4732 Make_Assignment_Statement (Loc,
4733 Name => New_Copy (Actual),
4734 Expression =>
4735 Make_Explicit_Dereference (Loc,
4736 Make_Selected_Component (Loc,
4737 Prefix => New_Reference_To (P, Loc),
4738 Selector_Name =>
4739 Make_Identifier (Loc, Chars (Formal)))));
4741 -- In all cases (including limited private types) we want
4742 -- the assignment to be valid.
4744 Set_Assignment_OK (Name (N_Node));
4746 -- If the call is the triggering alternative in an
4747 -- asynchronous select, or the entry_call alternative of a
4748 -- conditional entry call, the assignments for in-out
4749 -- parameters are incorporated into the statement list that
4750 -- follows, so that there are executed only if the entry
4751 -- call succeeds.
4753 if (Nkind (Parent (N)) = N_Triggering_Alternative
4754 and then N = Triggering_Statement (Parent (N)))
4755 or else
4756 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4757 and then N = Entry_Call_Statement (Parent (N)))
4758 then
4759 if No (Statements (Parent (N))) then
4760 Set_Statements (Parent (N), New_List);
4761 end if;
4763 Prepend (N_Node, Statements (Parent (N)));
4765 else
4766 Insert_After (Call, N_Node);
4767 end if;
4768 end if;
4770 Next_Actual (Actual);
4771 Next_Formal_With_Extras (Formal);
4772 end loop;
4773 end if;
4775 -- Finally, create block and analyze it
4777 Rewrite (N,
4778 Make_Block_Statement (Loc,
4779 Declarations => Decls,
4780 Handled_Statement_Sequence =>
4781 Make_Handled_Sequence_Of_Statements (Loc,
4782 Statements => Stats)));
4784 Analyze (N);
4785 end;
4786 end Build_Simple_Entry_Call;
4788 --------------------------------
4789 -- Build_Task_Activation_Call --
4790 --------------------------------
4792 procedure Build_Task_Activation_Call (N : Node_Id) is
4793 Loc : constant Source_Ptr := Sloc (N);
4794 Chain : Entity_Id;
4795 Call : Node_Id;
4796 Name : Node_Id;
4797 P : Node_Id;
4799 begin
4800 -- Get the activation chain entity. Except in the case of a package
4801 -- body, this is in the node that was passed. For a package body, we
4802 -- have to find the corresponding package declaration node.
4804 if Nkind (N) = N_Package_Body then
4805 P := Corresponding_Spec (N);
4806 loop
4807 P := Parent (P);
4808 exit when Nkind (P) = N_Package_Declaration;
4809 end loop;
4811 Chain := Activation_Chain_Entity (P);
4813 else
4814 Chain := Activation_Chain_Entity (N);
4815 end if;
4817 if Present (Chain) then
4818 if Restricted_Profile then
4819 Name := New_Reference_To (RTE (RE_Activate_Restricted_Tasks), Loc);
4820 else
4821 Name := New_Reference_To (RTE (RE_Activate_Tasks), Loc);
4822 end if;
4824 Call :=
4825 Make_Procedure_Call_Statement (Loc,
4826 Name => Name,
4827 Parameter_Associations =>
4828 New_List (Make_Attribute_Reference (Loc,
4829 Prefix => New_Occurrence_Of (Chain, Loc),
4830 Attribute_Name => Name_Unchecked_Access)));
4832 if Nkind (N) = N_Package_Declaration then
4833 if Present (Corresponding_Body (N)) then
4834 null;
4836 elsif Present (Private_Declarations (Specification (N))) then
4837 Append (Call, Private_Declarations (Specification (N)));
4839 else
4840 Append (Call, Visible_Declarations (Specification (N)));
4841 end if;
4843 else
4844 if Present (Handled_Statement_Sequence (N)) then
4846 -- The call goes at the start of the statement sequence
4847 -- after the start of exception range label if one is present.
4849 declare
4850 Stm : Node_Id;
4852 begin
4853 Stm := First (Statements (Handled_Statement_Sequence (N)));
4855 -- A special case, skip exception range label if one is
4856 -- present (from front end zcx processing).
4858 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
4859 Next (Stm);
4860 end if;
4862 -- Another special case, if the first statement is a block
4863 -- from optimization of a local raise to a goto, then the
4864 -- call goes inside this block.
4866 if Nkind (Stm) = N_Block_Statement
4867 and then Exception_Junk (Stm)
4868 then
4869 Stm :=
4870 First (Statements (Handled_Statement_Sequence (Stm)));
4871 end if;
4873 -- Insertion point is after any exception label pushes,
4874 -- since we want it covered by any local handlers.
4876 while Nkind (Stm) in N_Push_xxx_Label loop
4877 Next (Stm);
4878 end loop;
4880 -- Now we have the proper insertion point
4882 Insert_Before (Stm, Call);
4883 end;
4885 else
4886 Set_Handled_Statement_Sequence (N,
4887 Make_Handled_Sequence_Of_Statements (Loc,
4888 Statements => New_List (Call)));
4889 end if;
4890 end if;
4892 Analyze (Call);
4893 Check_Task_Activation (N);
4894 end if;
4895 end Build_Task_Activation_Call;
4897 -------------------------------
4898 -- Build_Task_Allocate_Block --
4899 -------------------------------
4901 procedure Build_Task_Allocate_Block
4902 (Actions : List_Id;
4903 N : Node_Id;
4904 Args : List_Id)
4906 T : constant Entity_Id := Entity (Expression (N));
4907 Init : constant Entity_Id := Base_Init_Proc (T);
4908 Loc : constant Source_Ptr := Sloc (N);
4909 Chain : constant Entity_Id :=
4910 Make_Defining_Identifier (Loc, Name_uChain);
4911 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4912 Block : Node_Id;
4914 begin
4915 Block :=
4916 Make_Block_Statement (Loc,
4917 Identifier => New_Reference_To (Blkent, Loc),
4918 Declarations => New_List (
4920 -- _Chain : Activation_Chain;
4922 Make_Object_Declaration (Loc,
4923 Defining_Identifier => Chain,
4924 Aliased_Present => True,
4925 Object_Definition =>
4926 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
4928 Handled_Statement_Sequence =>
4929 Make_Handled_Sequence_Of_Statements (Loc,
4931 Statements => New_List (
4933 -- Init (Args);
4935 Make_Procedure_Call_Statement (Loc,
4936 Name => New_Reference_To (Init, Loc),
4937 Parameter_Associations => Args),
4939 -- Activate_Tasks (_Chain);
4941 Make_Procedure_Call_Statement (Loc,
4942 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
4943 Parameter_Associations => New_List (
4944 Make_Attribute_Reference (Loc,
4945 Prefix => New_Reference_To (Chain, Loc),
4946 Attribute_Name => Name_Unchecked_Access))))),
4948 Has_Created_Identifier => True,
4949 Is_Task_Allocation_Block => True);
4951 Append_To (Actions,
4952 Make_Implicit_Label_Declaration (Loc,
4953 Defining_Identifier => Blkent,
4954 Label_Construct => Block));
4956 Append_To (Actions, Block);
4958 Set_Activation_Chain_Entity (Block, Chain);
4959 end Build_Task_Allocate_Block;
4961 -----------------------------------------------
4962 -- Build_Task_Allocate_Block_With_Init_Stmts --
4963 -----------------------------------------------
4965 procedure Build_Task_Allocate_Block_With_Init_Stmts
4966 (Actions : List_Id;
4967 N : Node_Id;
4968 Init_Stmts : List_Id)
4970 Loc : constant Source_Ptr := Sloc (N);
4971 Chain : constant Entity_Id :=
4972 Make_Defining_Identifier (Loc, Name_uChain);
4973 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4974 Block : Node_Id;
4976 begin
4977 Append_To (Init_Stmts,
4978 Make_Procedure_Call_Statement (Loc,
4979 Name => New_Reference_To (RTE (RE_Activate_Tasks), Loc),
4980 Parameter_Associations => New_List (
4981 Make_Attribute_Reference (Loc,
4982 Prefix => New_Reference_To (Chain, Loc),
4983 Attribute_Name => Name_Unchecked_Access))));
4985 Block :=
4986 Make_Block_Statement (Loc,
4987 Identifier => New_Reference_To (Blkent, Loc),
4988 Declarations => New_List (
4990 -- _Chain : Activation_Chain;
4992 Make_Object_Declaration (Loc,
4993 Defining_Identifier => Chain,
4994 Aliased_Present => True,
4995 Object_Definition =>
4996 New_Reference_To (RTE (RE_Activation_Chain), Loc))),
4998 Handled_Statement_Sequence =>
4999 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5001 Has_Created_Identifier => True,
5002 Is_Task_Allocation_Block => True);
5004 Append_To (Actions,
5005 Make_Implicit_Label_Declaration (Loc,
5006 Defining_Identifier => Blkent,
5007 Label_Construct => Block));
5009 Append_To (Actions, Block);
5011 Set_Activation_Chain_Entity (Block, Chain);
5012 end Build_Task_Allocate_Block_With_Init_Stmts;
5014 -----------------------------------
5015 -- Build_Task_Proc_Specification --
5016 -----------------------------------
5018 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5019 Loc : constant Source_Ptr := Sloc (T);
5020 Spec_Id : Entity_Id;
5022 begin
5023 -- Case of explicit task type, suffix TB
5025 if Comes_From_Source (T) then
5026 Spec_Id :=
5027 Make_Defining_Identifier (Loc,
5028 Chars => New_External_Name (Chars (T), "TB"));
5030 -- Case of anonymous task type, suffix B
5032 else
5033 Spec_Id :=
5034 Make_Defining_Identifier (Loc,
5035 Chars => New_External_Name (Chars (T), 'B'));
5036 end if;
5038 Set_Is_Internal (Spec_Id);
5040 -- Associate the procedure with the task, if this is the declaration
5041 -- (and not the body) of the procedure.
5043 if No (Task_Body_Procedure (T)) then
5044 Set_Task_Body_Procedure (T, Spec_Id);
5045 end if;
5047 return
5048 Make_Procedure_Specification (Loc,
5049 Defining_Unit_Name => Spec_Id,
5050 Parameter_Specifications => New_List (
5051 Make_Parameter_Specification (Loc,
5052 Defining_Identifier =>
5053 Make_Defining_Identifier (Loc, Name_uTask),
5054 Parameter_Type =>
5055 Make_Access_Definition (Loc,
5056 Subtype_Mark =>
5057 New_Reference_To (Corresponding_Record_Type (T), Loc)))));
5058 end Build_Task_Proc_Specification;
5060 ---------------------------------------
5061 -- Build_Unprotected_Subprogram_Body --
5062 ---------------------------------------
5064 function Build_Unprotected_Subprogram_Body
5065 (N : Node_Id;
5066 Pid : Node_Id) return Node_Id
5068 Decls : constant List_Id := Declarations (N);
5070 begin
5071 -- Add renamings for the Protection object, discriminals, privals and
5072 -- the entry index constant for use by debugger.
5074 Debug_Private_Data_Declarations (Decls);
5076 -- Make an unprotected version of the subprogram for use within the same
5077 -- object, with a new name and an additional parameter representing the
5078 -- object.
5080 return
5081 Make_Subprogram_Body (Sloc (N),
5082 Specification =>
5083 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5084 Declarations => Decls,
5085 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5086 end Build_Unprotected_Subprogram_Body;
5088 ----------------------------
5089 -- Collect_Entry_Families --
5090 ----------------------------
5092 procedure Collect_Entry_Families
5093 (Loc : Source_Ptr;
5094 Cdecls : List_Id;
5095 Current_Node : in out Node_Id;
5096 Conctyp : Entity_Id)
5098 Efam : Entity_Id;
5099 Efam_Decl : Node_Id;
5100 Efam_Type : Entity_Id;
5102 begin
5103 Efam := First_Entity (Conctyp);
5104 while Present (Efam) loop
5105 if Ekind (Efam) = E_Entry_Family then
5106 Efam_Type := Make_Temporary (Loc, 'F');
5108 declare
5109 Bas : Entity_Id :=
5110 Base_Type
5111 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5113 Bas_Decl : Node_Id := Empty;
5114 Lo, Hi : Node_Id;
5116 begin
5117 Get_Index_Bounds
5118 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5120 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5121 Bas := Make_Temporary (Loc, 'B');
5123 Bas_Decl :=
5124 Make_Subtype_Declaration (Loc,
5125 Defining_Identifier => Bas,
5126 Subtype_Indication =>
5127 Make_Subtype_Indication (Loc,
5128 Subtype_Mark =>
5129 New_Occurrence_Of (Standard_Integer, Loc),
5130 Constraint =>
5131 Make_Range_Constraint (Loc,
5132 Range_Expression => Make_Range (Loc,
5133 Make_Integer_Literal
5134 (Loc, -Entry_Family_Bound),
5135 Make_Integer_Literal
5136 (Loc, Entry_Family_Bound - 1)))));
5138 Insert_After (Current_Node, Bas_Decl);
5139 Current_Node := Bas_Decl;
5140 Analyze (Bas_Decl);
5141 end if;
5143 Efam_Decl :=
5144 Make_Full_Type_Declaration (Loc,
5145 Defining_Identifier => Efam_Type,
5146 Type_Definition =>
5147 Make_Unconstrained_Array_Definition (Loc,
5148 Subtype_Marks =>
5149 (New_List (New_Occurrence_Of (Bas, Loc))),
5151 Component_Definition =>
5152 Make_Component_Definition (Loc,
5153 Aliased_Present => False,
5154 Subtype_Indication =>
5155 New_Reference_To (Standard_Character, Loc))));
5156 end;
5158 Insert_After (Current_Node, Efam_Decl);
5159 Current_Node := Efam_Decl;
5160 Analyze (Efam_Decl);
5162 Append_To (Cdecls,
5163 Make_Component_Declaration (Loc,
5164 Defining_Identifier =>
5165 Make_Defining_Identifier (Loc, Chars (Efam)),
5167 Component_Definition =>
5168 Make_Component_Definition (Loc,
5169 Aliased_Present => False,
5170 Subtype_Indication =>
5171 Make_Subtype_Indication (Loc,
5172 Subtype_Mark =>
5173 New_Occurrence_Of (Efam_Type, Loc),
5175 Constraint =>
5176 Make_Index_Or_Discriminant_Constraint (Loc,
5177 Constraints => New_List (
5178 New_Occurrence_Of
5179 (Etype (Discrete_Subtype_Definition
5180 (Parent (Efam))), Loc)))))));
5182 end if;
5184 Next_Entity (Efam);
5185 end loop;
5186 end Collect_Entry_Families;
5188 -----------------------
5189 -- Concurrent_Object --
5190 -----------------------
5192 function Concurrent_Object
5193 (Spec_Id : Entity_Id;
5194 Conc_Typ : Entity_Id) return Entity_Id
5196 begin
5197 -- Parameter _O or _object
5199 if Is_Protected_Type (Conc_Typ) then
5200 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5202 -- Parameter _task
5204 else
5205 pragma Assert (Is_Task_Type (Conc_Typ));
5206 return First_Formal (Task_Body_Procedure (Conc_Typ));
5207 end if;
5208 end Concurrent_Object;
5210 ----------------------
5211 -- Copy_Result_Type --
5212 ----------------------
5214 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5215 New_Res : constant Node_Id := New_Copy_Tree (Res);
5216 Par_Spec : Node_Id;
5217 Formal : Entity_Id;
5219 begin
5220 -- If the result type is an access_to_subprogram, we must create
5221 -- new entities for its spec.
5223 if Nkind (New_Res) = N_Access_Definition
5224 and then Present (Access_To_Subprogram_Definition (New_Res))
5225 then
5226 -- Provide new entities for the formals
5228 Par_Spec := First (Parameter_Specifications
5229 (Access_To_Subprogram_Definition (New_Res)));
5230 while Present (Par_Spec) loop
5231 Formal := Defining_Identifier (Par_Spec);
5232 Set_Defining_Identifier (Par_Spec,
5233 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5234 Next (Par_Spec);
5235 end loop;
5236 end if;
5238 return New_Res;
5239 end Copy_Result_Type;
5241 --------------------
5242 -- Concurrent_Ref --
5243 --------------------
5245 -- The expression returned for a reference to a concurrent object has the
5246 -- form:
5248 -- taskV!(name)._Task_Id
5250 -- for a task, and
5252 -- objectV!(name)._Object
5254 -- for a protected object. For the case of an access to a concurrent
5255 -- object, there is an extra explicit dereference:
5257 -- taskV!(name.all)._Task_Id
5258 -- objectV!(name.all)._Object
5260 -- here taskV and objectV are the types for the associated records, which
5261 -- contain the required _Task_Id and _Object fields for tasks and protected
5262 -- objects, respectively.
5264 -- For the case of a task type name, the expression is
5266 -- Self;
5268 -- i.e. a call to the Self function which returns precisely this Task_Id
5270 -- For the case of a protected type name, the expression is
5272 -- objectR
5274 -- which is a renaming of the _object field of the current object
5275 -- record, passed into protected operations as a parameter.
5277 function Concurrent_Ref (N : Node_Id) return Node_Id is
5278 Loc : constant Source_Ptr := Sloc (N);
5279 Ntyp : constant Entity_Id := Etype (N);
5280 Dtyp : Entity_Id;
5281 Sel : Name_Id;
5283 function Is_Current_Task (T : Entity_Id) return Boolean;
5284 -- Check whether the reference is to the immediately enclosing task
5285 -- type, or to an outer one (rare but legal).
5287 ---------------------
5288 -- Is_Current_Task --
5289 ---------------------
5291 function Is_Current_Task (T : Entity_Id) return Boolean is
5292 Scop : Entity_Id;
5294 begin
5295 Scop := Current_Scope;
5296 while Present (Scop)
5297 and then Scop /= Standard_Standard
5298 loop
5300 if Scop = T then
5301 return True;
5303 elsif Is_Task_Type (Scop) then
5304 return False;
5306 -- If this is a procedure nested within the task type, we must
5307 -- assume that it can be called from an inner task, and therefore
5308 -- cannot treat it as a local reference.
5310 elsif Is_Overloadable (Scop)
5311 and then In_Open_Scopes (T)
5312 then
5313 return False;
5315 else
5316 Scop := Scope (Scop);
5317 end if;
5318 end loop;
5320 -- We know that we are within the task body, so should have found it
5321 -- in scope.
5323 raise Program_Error;
5324 end Is_Current_Task;
5326 -- Start of processing for Concurrent_Ref
5328 begin
5329 if Is_Access_Type (Ntyp) then
5330 Dtyp := Designated_Type (Ntyp);
5332 if Is_Protected_Type (Dtyp) then
5333 Sel := Name_uObject;
5334 else
5335 Sel := Name_uTask_Id;
5336 end if;
5338 return
5339 Make_Selected_Component (Loc,
5340 Prefix =>
5341 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5342 Make_Explicit_Dereference (Loc, N)),
5343 Selector_Name => Make_Identifier (Loc, Sel));
5345 elsif Is_Entity_Name (N)
5346 and then Is_Concurrent_Type (Entity (N))
5347 then
5348 if Is_Task_Type (Entity (N)) then
5350 if Is_Current_Task (Entity (N)) then
5351 return
5352 Make_Function_Call (Loc,
5353 Name => New_Reference_To (RTE (RE_Self), Loc));
5355 else
5356 declare
5357 Decl : Node_Id;
5358 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5359 T_Body : constant Node_Id :=
5360 Parent (Corresponding_Body (Parent (Entity (N))));
5362 begin
5363 Decl :=
5364 Make_Object_Declaration (Loc,
5365 Defining_Identifier => T_Self,
5366 Object_Definition =>
5367 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5368 Expression =>
5369 Make_Function_Call (Loc,
5370 Name => New_Reference_To (RTE (RE_Self), Loc)));
5371 Prepend (Decl, Declarations (T_Body));
5372 Analyze (Decl);
5373 Set_Scope (T_Self, Entity (N));
5374 return New_Occurrence_Of (T_Self, Loc);
5375 end;
5376 end if;
5378 else
5379 pragma Assert (Is_Protected_Type (Entity (N)));
5381 return
5382 New_Reference_To (Find_Protection_Object (Current_Scope), Loc);
5383 end if;
5385 else
5386 if Is_Protected_Type (Ntyp) then
5387 Sel := Name_uObject;
5389 elsif Is_Task_Type (Ntyp) then
5390 Sel := Name_uTask_Id;
5392 else
5393 raise Program_Error;
5394 end if;
5396 return
5397 Make_Selected_Component (Loc,
5398 Prefix =>
5399 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5400 New_Copy_Tree (N)),
5401 Selector_Name => Make_Identifier (Loc, Sel));
5402 end if;
5403 end Concurrent_Ref;
5405 ------------------------
5406 -- Convert_Concurrent --
5407 ------------------------
5409 function Convert_Concurrent
5410 (N : Node_Id;
5411 Typ : Entity_Id) return Node_Id
5413 begin
5414 if not Is_Concurrent_Type (Typ) then
5415 return N;
5416 else
5417 return
5418 Unchecked_Convert_To
5419 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5420 end if;
5421 end Convert_Concurrent;
5423 -------------------------------------
5424 -- Debug_Private_Data_Declarations --
5425 -------------------------------------
5427 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5428 Debug_Nod : Node_Id;
5429 Decl : Node_Id;
5431 begin
5432 Decl := First (Decls);
5433 while Present (Decl)
5434 and then not Comes_From_Source (Decl)
5435 loop
5436 -- Declaration for concurrent entity _object and its access type,
5437 -- along with the entry index subtype:
5438 -- type prot_typVP is access prot_typV;
5439 -- _object : prot_typVP := prot_typV (_O);
5440 -- subtype Jnn is <Type of Index> range Low .. High;
5442 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5443 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5445 -- Declaration for the Protection object, discriminals, privals and
5446 -- entry index constant:
5447 -- conc_typR : protection_typ renames _object._object;
5448 -- discr_nameD : discr_typ renames _object.discr_name;
5449 -- discr_nameD : discr_typ renames _task.discr_name;
5450 -- prival_name : comp_typ renames _object.comp_name;
5451 -- J : constant Jnn :=
5452 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5454 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5455 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5456 Debug_Nod := Debug_Renaming_Declaration (Decl);
5458 if Present (Debug_Nod) then
5459 Insert_After (Decl, Debug_Nod);
5460 end if;
5461 end if;
5463 Next (Decl);
5464 end loop;
5465 end Debug_Private_Data_Declarations;
5467 ----------------------------
5468 -- Entry_Index_Expression --
5469 ----------------------------
5471 function Entry_Index_Expression
5472 (Sloc : Source_Ptr;
5473 Ent : Entity_Id;
5474 Index : Node_Id;
5475 Ttyp : Entity_Id) return Node_Id
5477 Expr : Node_Id;
5478 Num : Node_Id;
5479 Lo : Node_Id;
5480 Hi : Node_Id;
5481 Prev : Entity_Id;
5482 S : Node_Id;
5484 begin
5485 -- The queues of entries and entry families appear in textual order in
5486 -- the associated record. The entry index is computed as the sum of the
5487 -- number of queues for all entries that precede the designated one, to
5488 -- which is added the index expression, if this expression denotes a
5489 -- member of a family.
5491 -- The following is a place holder for the count of simple entries
5493 Num := Make_Integer_Literal (Sloc, 1);
5495 -- We construct an expression which is a series of addition operations.
5496 -- The first operand is the number of single entries that precede this
5497 -- one, the second operand is the index value relative to the start of
5498 -- the referenced family, and the remaining operands are the lengths of
5499 -- the entry families that precede this entry, i.e. the constructed
5500 -- expression is:
5502 -- number_simple_entries +
5503 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5504 -- family'length + ...
5506 -- where index-value is the given index value, and s is the index
5507 -- subtype (we have to use pos because the subtype might be an
5508 -- enumeration type preventing direct subtraction). Note that the task
5509 -- entry array is one-indexed.
5511 -- The upper bound of the entry family may be a discriminant, so we
5512 -- retrieve the lower bound explicitly to compute offset, rather than
5513 -- using the index subtype which may mention a discriminant.
5515 if Present (Index) then
5516 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5518 Expr :=
5519 Make_Op_Add (Sloc,
5520 Left_Opnd => Num,
5522 Right_Opnd =>
5523 Family_Offset (
5524 Sloc,
5525 Make_Attribute_Reference (Sloc,
5526 Attribute_Name => Name_Pos,
5527 Prefix => New_Reference_To (Base_Type (S), Sloc),
5528 Expressions => New_List (Relocate_Node (Index))),
5529 Type_Low_Bound (S),
5530 Ttyp,
5531 False));
5532 else
5533 Expr := Num;
5534 end if;
5536 -- Now add lengths of preceding entries and entry families
5538 Prev := First_Entity (Ttyp);
5540 while Chars (Prev) /= Chars (Ent)
5541 or else (Ekind (Prev) /= Ekind (Ent))
5542 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5543 loop
5544 if Ekind (Prev) = E_Entry then
5545 Set_Intval (Num, Intval (Num) + 1);
5547 elsif Ekind (Prev) = E_Entry_Family then
5548 S :=
5549 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5550 Lo := Type_Low_Bound (S);
5551 Hi := Type_High_Bound (S);
5553 Expr :=
5554 Make_Op_Add (Sloc,
5555 Left_Opnd => Expr,
5556 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5558 -- Other components are anonymous types to be ignored
5560 else
5561 null;
5562 end if;
5564 Next_Entity (Prev);
5565 end loop;
5567 return Expr;
5568 end Entry_Index_Expression;
5570 ---------------------------
5571 -- Establish_Task_Master --
5572 ---------------------------
5574 procedure Establish_Task_Master (N : Node_Id) is
5575 Call : Node_Id;
5577 begin
5578 if Restriction_Active (No_Task_Hierarchy) = False then
5579 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5581 -- The block may have no declarations, and nevertheless be a task
5582 -- master, if it contains a call that may return an object that
5583 -- contains tasks.
5585 if No (Declarations (N)) then
5586 Set_Declarations (N, New_List (Call));
5587 else
5588 Prepend_To (Declarations (N), Call);
5589 end if;
5591 Analyze (Call);
5592 end if;
5593 end Establish_Task_Master;
5595 --------------------------------
5596 -- Expand_Accept_Declarations --
5597 --------------------------------
5599 -- Part of the expansion of an accept statement involves the creation of
5600 -- a declaration that can be referenced from the statement sequence of
5601 -- the accept:
5603 -- Ann : Address;
5605 -- This declaration is inserted immediately before the accept statement
5606 -- and it is important that it be inserted before the statements of the
5607 -- statement sequence are analyzed. Thus it would be too late to create
5608 -- this declaration in the Expand_N_Accept_Statement routine, which is
5609 -- why there is a separate procedure to be called directly from Sem_Ch9.
5611 -- Ann is used to hold the address of the record containing the parameters
5612 -- (see Expand_N_Entry_Call for more details on how this record is built).
5613 -- References to the parameters do an unchecked conversion of this address
5614 -- to a pointer to the required record type, and then access the field that
5615 -- holds the value of the required parameter. The entity for the address
5616 -- variable is held as the top stack element (i.e. the last element) of the
5617 -- Accept_Address stack in the corresponding entry entity, and this element
5618 -- must be set in place before the statements are processed.
5620 -- The above description applies to the case of a stand alone accept
5621 -- statement, i.e. one not appearing as part of a select alternative.
5623 -- For the case of an accept that appears as part of a select alternative
5624 -- of a selective accept, we must still create the declaration right away,
5625 -- since Ann is needed immediately, but there is an important difference:
5627 -- The declaration is inserted before the selective accept, not before
5628 -- the accept statement (which is not part of a list anyway, and so would
5629 -- not accommodate inserted declarations)
5631 -- We only need one address variable for the entire selective accept. So
5632 -- the Ann declaration is created only for the first accept alternative,
5633 -- and subsequent accept alternatives reference the same Ann variable.
5635 -- We can distinguish the two cases by seeing whether the accept statement
5636 -- is part of a list. If not, then it must be in an accept alternative.
5638 -- To expand the requeue statement, a label is provided at the end of the
5639 -- accept statement or alternative of which it is a part, so that the
5640 -- statement can be skipped after the requeue is complete. This label is
5641 -- created here rather than during the expansion of the accept statement,
5642 -- because it will be needed by any requeue statements within the accept,
5643 -- which are expanded before the accept.
5645 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5646 Loc : constant Source_Ptr := Sloc (N);
5647 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5648 Ann : Entity_Id := Empty;
5649 Adecl : Node_Id;
5650 Lab_Id : Node_Id;
5651 Lab : Node_Id;
5652 Ldecl : Node_Id;
5653 Ldecl2 : Node_Id;
5655 begin
5656 if Full_Expander_Active then
5658 -- If we have no handled statement sequence, we may need to build
5659 -- a dummy sequence consisting of a null statement. This can be
5660 -- skipped if the trivial accept optimization is permitted.
5662 if not Trivial_Accept_OK
5663 and then
5664 (No (Stats) or else Null_Statements (Statements (Stats)))
5665 then
5666 Set_Handled_Statement_Sequence (N,
5667 Make_Handled_Sequence_Of_Statements (Loc,
5668 Statements => New_List (Make_Null_Statement (Loc))));
5669 end if;
5671 -- Create and declare two labels to be placed at the end of the
5672 -- accept statement. The first label is used to allow requeues to
5673 -- skip the remainder of entry processing. The second label is used
5674 -- to skip the remainder of entry processing if the rendezvous
5675 -- completes in the middle of the accept body.
5677 if Present (Handled_Statement_Sequence (N)) then
5678 declare
5679 Ent : Entity_Id;
5681 begin
5682 Ent := Make_Temporary (Loc, 'L');
5683 Lab_Id := New_Reference_To (Ent, Loc);
5684 Lab := Make_Label (Loc, Lab_Id);
5685 Ldecl :=
5686 Make_Implicit_Label_Declaration (Loc,
5687 Defining_Identifier => Ent,
5688 Label_Construct => Lab);
5689 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5691 Ent := Make_Temporary (Loc, 'L');
5692 Lab_Id := New_Reference_To (Ent, Loc);
5693 Lab := Make_Label (Loc, Lab_Id);
5694 Ldecl2 :=
5695 Make_Implicit_Label_Declaration (Loc,
5696 Defining_Identifier => Ent,
5697 Label_Construct => Lab);
5698 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5699 end;
5701 else
5702 Ldecl := Empty;
5703 Ldecl2 := Empty;
5704 end if;
5706 -- Case of stand alone accept statement
5708 if Is_List_Member (N) then
5710 if Present (Handled_Statement_Sequence (N)) then
5711 Ann := Make_Temporary (Loc, 'A');
5713 Adecl :=
5714 Make_Object_Declaration (Loc,
5715 Defining_Identifier => Ann,
5716 Object_Definition =>
5717 New_Reference_To (RTE (RE_Address), Loc));
5719 Insert_Before (N, Adecl);
5720 Analyze (Adecl);
5722 Insert_Before (N, Ldecl);
5723 Analyze (Ldecl);
5725 Insert_Before (N, Ldecl2);
5726 Analyze (Ldecl2);
5727 end if;
5729 -- Case of accept statement which is in an accept alternative
5731 else
5732 declare
5733 Acc_Alt : constant Node_Id := Parent (N);
5734 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5735 Alt : Node_Id;
5737 begin
5738 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5739 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5741 -- ??? Consider a single label for select statements
5743 if Present (Handled_Statement_Sequence (N)) then
5744 Prepend (Ldecl2,
5745 Statements (Handled_Statement_Sequence (N)));
5746 Analyze (Ldecl2);
5748 Prepend (Ldecl,
5749 Statements (Handled_Statement_Sequence (N)));
5750 Analyze (Ldecl);
5751 end if;
5753 -- Find first accept alternative of the selective accept. A
5754 -- valid selective accept must have at least one accept in it.
5756 Alt := First (Select_Alternatives (Sel_Acc));
5758 while Nkind (Alt) /= N_Accept_Alternative loop
5759 Next (Alt);
5760 end loop;
5762 -- If we are the first accept statement, then we have to create
5763 -- the Ann variable, as for the stand alone case, except that
5764 -- it is inserted before the selective accept. Similarly, a
5765 -- label for requeue expansion must be declared.
5767 if N = Accept_Statement (Alt) then
5768 Ann := Make_Temporary (Loc, 'A');
5769 Adecl :=
5770 Make_Object_Declaration (Loc,
5771 Defining_Identifier => Ann,
5772 Object_Definition =>
5773 New_Reference_To (RTE (RE_Address), Loc));
5775 Insert_Before (Sel_Acc, Adecl);
5776 Analyze (Adecl);
5778 -- If we are not the first accept statement, then find the Ann
5779 -- variable allocated by the first accept and use it.
5781 else
5782 Ann :=
5783 Node (Last_Elmt (Accept_Address
5784 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5785 end if;
5786 end;
5787 end if;
5789 -- Merge here with Ann either created or referenced, and Adecl
5790 -- pointing to the corresponding declaration. Remaining processing
5791 -- is the same for the two cases.
5793 if Present (Ann) then
5794 Append_Elmt (Ann, Accept_Address (Ent));
5795 Set_Debug_Info_Needed (Ann);
5796 end if;
5798 -- Create renaming declarations for the entry formals. Each reference
5799 -- to a formal becomes a dereference of a component of the parameter
5800 -- block, whose address is held in Ann. These declarations are
5801 -- eventually inserted into the accept block, and analyzed there so
5802 -- that they have the proper scope for gdb and do not conflict with
5803 -- other declarations.
5805 if Present (Parameter_Specifications (N))
5806 and then Present (Handled_Statement_Sequence (N))
5807 then
5808 declare
5809 Comp : Entity_Id;
5810 Decl : Node_Id;
5811 Formal : Entity_Id;
5812 New_F : Entity_Id;
5813 Renamed_Formal : Node_Id;
5815 begin
5816 Push_Scope (Ent);
5817 Formal := First_Formal (Ent);
5819 while Present (Formal) loop
5820 Comp := Entry_Component (Formal);
5821 New_F :=
5822 Make_Defining_Identifier (Loc, Chars (Formal));
5824 Set_Etype (New_F, Etype (Formal));
5825 Set_Scope (New_F, Ent);
5827 -- Now we set debug info needed on New_F even though it does
5828 -- not come from source, so that the debugger will get the
5829 -- right information for these generated names.
5831 Set_Debug_Info_Needed (New_F);
5833 if Ekind (Formal) = E_In_Parameter then
5834 Set_Ekind (New_F, E_Constant);
5835 else
5836 Set_Ekind (New_F, E_Variable);
5837 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5838 end if;
5840 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5842 Renamed_Formal :=
5843 Make_Selected_Component (Loc,
5844 Prefix =>
5845 Unchecked_Convert_To (
5846 Entry_Parameters_Type (Ent),
5847 New_Reference_To (Ann, Loc)),
5848 Selector_Name =>
5849 New_Reference_To (Comp, Loc));
5851 Decl :=
5852 Build_Renamed_Formal_Declaration
5853 (New_F, Formal, Comp, Renamed_Formal);
5855 if No (Declarations (N)) then
5856 Set_Declarations (N, New_List);
5857 end if;
5859 Append (Decl, Declarations (N));
5860 Set_Renamed_Object (Formal, New_F);
5861 Next_Formal (Formal);
5862 end loop;
5864 End_Scope;
5865 end;
5866 end if;
5867 end if;
5868 end Expand_Accept_Declarations;
5870 ---------------------------------------------
5871 -- Expand_Access_Protected_Subprogram_Type --
5872 ---------------------------------------------
5874 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5875 Loc : constant Source_Ptr := Sloc (N);
5876 Comps : List_Id;
5877 T : constant Entity_Id := Defining_Identifier (N);
5878 D_T : constant Entity_Id := Designated_Type (T);
5879 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
5880 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
5881 P_List : constant List_Id := Build_Protected_Spec
5882 (N, RTE (RE_Address), D_T, False);
5883 Decl1 : Node_Id;
5884 Decl2 : Node_Id;
5885 Def1 : Node_Id;
5887 begin
5888 -- Create access to subprogram with full signature
5890 if Etype (D_T) /= Standard_Void_Type then
5891 Def1 :=
5892 Make_Access_Function_Definition (Loc,
5893 Parameter_Specifications => P_List,
5894 Result_Definition =>
5895 Copy_Result_Type (Result_Definition (Type_Definition (N))));
5897 else
5898 Def1 :=
5899 Make_Access_Procedure_Definition (Loc,
5900 Parameter_Specifications => P_List);
5901 end if;
5903 Decl1 :=
5904 Make_Full_Type_Declaration (Loc,
5905 Defining_Identifier => D_T2,
5906 Type_Definition => Def1);
5908 Insert_After (N, Decl1);
5909 Analyze (Decl1);
5911 -- Associate the access to subprogram with its original access to
5912 -- protected subprogram type. Needed by the backend to know that this
5913 -- type corresponds with an access to protected subprogram type.
5915 Set_Original_Access_Type (D_T2, T);
5917 -- Create Equivalent_Type, a record with two components for an access to
5918 -- object and an access to subprogram.
5920 Comps := New_List (
5921 Make_Component_Declaration (Loc,
5922 Defining_Identifier => Make_Temporary (Loc, 'P'),
5923 Component_Definition =>
5924 Make_Component_Definition (Loc,
5925 Aliased_Present => False,
5926 Subtype_Indication =>
5927 New_Occurrence_Of (RTE (RE_Address), Loc))),
5929 Make_Component_Declaration (Loc,
5930 Defining_Identifier => Make_Temporary (Loc, 'S'),
5931 Component_Definition =>
5932 Make_Component_Definition (Loc,
5933 Aliased_Present => False,
5934 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
5936 Decl2 :=
5937 Make_Full_Type_Declaration (Loc,
5938 Defining_Identifier => E_T,
5939 Type_Definition =>
5940 Make_Record_Definition (Loc,
5941 Component_List =>
5942 Make_Component_List (Loc, Component_Items => Comps)));
5944 Insert_After (Decl1, Decl2);
5945 Analyze (Decl2);
5946 Set_Equivalent_Type (T, E_T);
5947 end Expand_Access_Protected_Subprogram_Type;
5949 --------------------------
5950 -- Expand_Entry_Barrier --
5951 --------------------------
5953 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
5954 Cond : constant Node_Id :=
5955 Condition (Entry_Body_Formal_Part (N));
5956 Prot : constant Entity_Id := Scope (Ent);
5957 Spec_Decl : constant Node_Id := Parent (Prot);
5958 Func : Node_Id;
5959 B_F : Node_Id;
5960 Body_Decl : Node_Id;
5962 begin
5963 if No_Run_Time_Mode then
5964 Error_Msg_CRT ("entry barrier", N);
5965 return;
5966 end if;
5968 -- The body of the entry barrier must be analyzed in the context of the
5969 -- protected object, but its scope is external to it, just as any other
5970 -- unprotected version of a protected operation. The specification has
5971 -- been produced when the protected type declaration was elaborated. We
5972 -- build the body, insert it in the enclosing scope, but analyze it in
5973 -- the current context. A more uniform approach would be to treat the
5974 -- barrier just as a protected function, and discard the protected
5975 -- version of it because it is never called.
5977 if Full_Expander_Active then
5978 B_F := Build_Barrier_Function (N, Ent, Prot);
5979 Func := Barrier_Function (Ent);
5980 Set_Corresponding_Spec (B_F, Func);
5982 Body_Decl := Parent (Corresponding_Body (Spec_Decl));
5984 if Nkind (Parent (Body_Decl)) = N_Subunit then
5985 Body_Decl := Corresponding_Stub (Parent (Body_Decl));
5986 end if;
5988 Insert_Before_And_Analyze (Body_Decl, B_F);
5990 Set_Discriminals (Spec_Decl);
5991 Set_Scope (Func, Scope (Prot));
5993 else
5994 Analyze_And_Resolve (Cond, Any_Boolean);
5995 end if;
5997 -- The Ravenscar profile restricts barriers to simple variables declared
5998 -- within the protected object. We also allow Boolean constants, since
5999 -- these appear in several published examples and are also allowed by
6000 -- the Aonix compiler.
6002 -- Note that after analysis variables in this context will be replaced
6003 -- by the corresponding prival, that is to say a renaming of a selected
6004 -- component of the form _Object.Var. If expansion is disabled, as
6005 -- within a generic, we check that the entity appears in the current
6006 -- scope.
6008 if Is_Entity_Name (Cond) then
6010 -- A small optimization of useless renamings. If the scope of the
6011 -- entity of the condition is not the barrier function, then the
6012 -- condition does not reference any of the generated renamings
6013 -- within the function.
6015 if Full_Expander_Active
6016 and then Scope (Entity (Cond)) /= Func
6017 then
6018 Set_Declarations (B_F, Empty_List);
6019 end if;
6021 if Entity (Cond) = Standard_False
6022 or else
6023 Entity (Cond) = Standard_True
6024 then
6025 return;
6027 elsif not Expander_Active
6028 and then Scope (Entity (Cond)) = Current_Scope
6029 then
6030 return;
6032 -- Check for case of _object.all.field (note that the explicit
6033 -- dereference gets inserted by analyze/expand of _object.field)
6035 elsif Present (Renamed_Object (Entity (Cond)))
6036 and then
6037 Nkind (Renamed_Object (Entity (Cond))) = N_Selected_Component
6038 and then
6039 Chars
6040 (Prefix
6041 (Prefix (Renamed_Object (Entity (Cond))))) = Name_uObject
6042 then
6043 return;
6044 end if;
6045 end if;
6047 -- It is not a boolean variable or literal, so check the restriction
6049 Check_Restriction (Simple_Barriers, Cond);
6050 end Expand_Entry_Barrier;
6052 ------------------------------
6053 -- Expand_N_Abort_Statement --
6054 ------------------------------
6056 -- Expand abort T1, T2, .. Tn; into:
6057 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6059 procedure Expand_N_Abort_Statement (N : Node_Id) is
6060 Loc : constant Source_Ptr := Sloc (N);
6061 Tlist : constant List_Id := Names (N);
6062 Count : Nat;
6063 Aggr : Node_Id;
6064 Tasknm : Node_Id;
6066 begin
6067 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6068 Count := 0;
6070 Tasknm := First (Tlist);
6072 while Present (Tasknm) loop
6073 Count := Count + 1;
6075 -- A task interface class-wide type object is being aborted.
6076 -- Retrieve its _task_id by calling a dispatching routine.
6078 if Ada_Version >= Ada_2005
6079 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6080 and then Is_Interface (Etype (Tasknm))
6081 and then Is_Task_Interface (Etype (Tasknm))
6082 then
6083 Append_To (Component_Associations (Aggr),
6084 Make_Component_Association (Loc,
6085 Choices => New_List (
6086 Make_Integer_Literal (Loc, Count)),
6087 Expression =>
6089 -- Task_Id (Tasknm._disp_get_task_id)
6091 Make_Unchecked_Type_Conversion (Loc,
6092 Subtype_Mark =>
6093 New_Reference_To (RTE (RO_ST_Task_Id), Loc),
6094 Expression =>
6095 Make_Selected_Component (Loc,
6096 Prefix => New_Copy_Tree (Tasknm),
6097 Selector_Name =>
6098 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6100 else
6101 Append_To (Component_Associations (Aggr),
6102 Make_Component_Association (Loc,
6103 Choices => New_List (
6104 Make_Integer_Literal (Loc, Count)),
6105 Expression => Concurrent_Ref (Tasknm)));
6106 end if;
6108 Next (Tasknm);
6109 end loop;
6111 Rewrite (N,
6112 Make_Procedure_Call_Statement (Loc,
6113 Name => New_Reference_To (RTE (RE_Abort_Tasks), Loc),
6114 Parameter_Associations => New_List (
6115 Make_Qualified_Expression (Loc,
6116 Subtype_Mark => New_Reference_To (RTE (RE_Task_List), Loc),
6117 Expression => Aggr))));
6119 Analyze (N);
6120 end Expand_N_Abort_Statement;
6122 -------------------------------
6123 -- Expand_N_Accept_Statement --
6124 -------------------------------
6126 -- This procedure handles expansion of accept statements that stand
6127 -- alone, i.e. they are not part of an accept alternative. The expansion
6128 -- of accept statement in accept alternatives is handled by the routines
6129 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6130 -- following description applies only to stand alone accept statements.
6132 -- If there is no handled statement sequence, or only null statements,
6133 -- then this is called a trivial accept, and the expansion is:
6135 -- Accept_Trivial (entry-index)
6137 -- If there is a handled statement sequence, then the expansion is:
6139 -- Ann : Address;
6140 -- {Lnn : Label}
6142 -- begin
6143 -- begin
6144 -- Accept_Call (entry-index, Ann);
6145 -- Renaming_Declarations for formals
6146 -- <statement sequence from N_Accept_Statement node>
6147 -- Complete_Rendezvous;
6148 -- <<Lnn>>
6150 -- exception
6151 -- when ... =>
6152 -- <exception handler from N_Accept_Statement node>
6153 -- Complete_Rendezvous;
6154 -- when ... =>
6155 -- <exception handler from N_Accept_Statement node>
6156 -- Complete_Rendezvous;
6157 -- ...
6158 -- end;
6160 -- exception
6161 -- when all others =>
6162 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6163 -- end;
6165 -- The first three declarations were already inserted ahead of the accept
6166 -- statement by the Expand_Accept_Declarations procedure, which was called
6167 -- directly from the semantics during analysis of the accept statement,
6168 -- before analyzing its contained statements.
6170 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6171 -- from possible expansion activity (the original source of course does
6172 -- not have any declarations associated with the accept statement, since
6173 -- an accept statement has no declarative part). In particular, if the
6174 -- expander is active, the first such declaration is the declaration of
6175 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6177 -- The two blocks are merged into a single block if the inner block has
6178 -- no exception handlers, but otherwise two blocks are required, since
6179 -- exceptions might be raised in the exception handlers of the inner
6180 -- block, and Exceptional_Complete_Rendezvous must be called.
6182 procedure Expand_N_Accept_Statement (N : Node_Id) is
6183 Loc : constant Source_Ptr := Sloc (N);
6184 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6185 Ename : constant Node_Id := Entry_Direct_Name (N);
6186 Eindx : constant Node_Id := Entry_Index (N);
6187 Eent : constant Entity_Id := Entity (Ename);
6188 Acstack : constant Elist_Id := Accept_Address (Eent);
6189 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6190 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6191 Blkent : Entity_Id;
6192 Call : Node_Id;
6193 Block : Node_Id;
6195 -- Start of processing for Expand_N_Accept_Statement
6197 begin
6198 -- If accept statement is not part of a list, then its parent must be
6199 -- an accept alternative, and, as described above, we do not do any
6200 -- expansion for such accept statements at this level.
6202 if not Is_List_Member (N) then
6203 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6204 return;
6206 -- Trivial accept case (no statement sequence, or null statements).
6207 -- If the accept statement has declarations, then just insert them
6208 -- before the procedure call.
6210 elsif Trivial_Accept_OK
6211 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6212 then
6213 -- Remove declarations for renamings, because the parameter block
6214 -- will not be assigned.
6216 declare
6217 D : Node_Id;
6218 Next_D : Node_Id;
6220 begin
6221 D := First (Declarations (N));
6223 while Present (D) loop
6224 Next_D := Next (D);
6225 if Nkind (D) = N_Object_Renaming_Declaration then
6226 Remove (D);
6227 end if;
6229 D := Next_D;
6230 end loop;
6231 end;
6233 if Present (Declarations (N)) then
6234 Insert_Actions (N, Declarations (N));
6235 end if;
6237 Rewrite (N,
6238 Make_Procedure_Call_Statement (Loc,
6239 Name => New_Reference_To (RTE (RE_Accept_Trivial), Loc),
6240 Parameter_Associations => New_List (
6241 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6243 Analyze (N);
6245 -- Discard Entry_Address that was created for it, so it will not be
6246 -- emitted if this accept statement is in the statement part of a
6247 -- delay alternative.
6249 if Present (Stats) then
6250 Remove_Last_Elmt (Acstack);
6251 end if;
6253 -- Case of statement sequence present
6255 else
6256 -- Construct the block, using the declarations from the accept
6257 -- statement if any to initialize the declarations of the block.
6259 Blkent := Make_Temporary (Loc, 'A');
6260 Set_Ekind (Blkent, E_Block);
6261 Set_Etype (Blkent, Standard_Void_Type);
6262 Set_Scope (Blkent, Current_Scope);
6264 Block :=
6265 Make_Block_Statement (Loc,
6266 Identifier => New_Reference_To (Blkent, Loc),
6267 Declarations => Declarations (N),
6268 Handled_Statement_Sequence => Build_Accept_Body (N));
6270 -- For the analysis of the generated declarations, the parent node
6271 -- must be properly set.
6273 Set_Parent (Block, Parent (N));
6275 -- Prepend call to Accept_Call to main statement sequence If the
6276 -- accept has exception handlers, the statement sequence is wrapped
6277 -- in a block. Insert call and renaming declarations in the
6278 -- declarations of the block, so they are elaborated before the
6279 -- handlers.
6281 Call :=
6282 Make_Procedure_Call_Statement (Loc,
6283 Name => New_Reference_To (RTE (RE_Accept_Call), Loc),
6284 Parameter_Associations => New_List (
6285 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6286 New_Reference_To (Ann, Loc)));
6288 if Parent (Stats) = N then
6289 Prepend (Call, Statements (Stats));
6290 else
6291 Set_Declarations
6292 (Parent (Stats),
6293 New_List (Call));
6294 end if;
6296 Analyze (Call);
6298 Push_Scope (Blkent);
6300 declare
6301 D : Node_Id;
6302 Next_D : Node_Id;
6303 Typ : Entity_Id;
6305 begin
6306 D := First (Declarations (N));
6307 while Present (D) loop
6308 Next_D := Next (D);
6310 if Nkind (D) = N_Object_Renaming_Declaration then
6312 -- The renaming declarations for the formals were created
6313 -- during analysis of the accept statement, and attached to
6314 -- the list of declarations. Place them now in the context
6315 -- of the accept block or subprogram.
6317 Remove (D);
6318 Typ := Entity (Subtype_Mark (D));
6319 Insert_After (Call, D);
6320 Analyze (D);
6322 -- If the formal is class_wide, it does not have an actual
6323 -- subtype. The analysis of the renaming declaration creates
6324 -- one, but we need to retain the class-wide nature of the
6325 -- entity.
6327 if Is_Class_Wide_Type (Typ) then
6328 Set_Etype (Defining_Identifier (D), Typ);
6329 end if;
6331 end if;
6333 D := Next_D;
6334 end loop;
6335 end;
6337 End_Scope;
6339 -- Replace the accept statement by the new block
6341 Rewrite (N, Block);
6342 Analyze (N);
6344 -- Last step is to unstack the Accept_Address value
6346 Remove_Last_Elmt (Acstack);
6347 end if;
6348 end Expand_N_Accept_Statement;
6350 ----------------------------------
6351 -- Expand_N_Asynchronous_Select --
6352 ----------------------------------
6354 -- This procedure assumes that the trigger statement is an entry call or
6355 -- a dispatching procedure call. A delay alternative should already have
6356 -- been expanded into an entry call to the appropriate delay object Wait
6357 -- entry.
6359 -- If the trigger is a task entry call, the select is implemented with
6360 -- a Task_Entry_Call:
6362 -- declare
6363 -- B : Boolean;
6364 -- C : Boolean;
6365 -- P : parms := (parm, parm, parm);
6367 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6369 -- procedure _clean is
6370 -- begin
6371 -- ...
6372 -- Cancel_Task_Entry_Call (C);
6373 -- ...
6374 -- end _clean;
6376 -- begin
6377 -- Abort_Defer;
6378 -- Task_Entry_Call
6379 -- (<acceptor-task>, -- Acceptor
6380 -- <entry-index>, -- E
6381 -- P'Address, -- Uninterpreted_Data
6382 -- Asynchronous_Call, -- Mode
6383 -- B); -- Rendezvous_Successful
6385 -- begin
6386 -- begin
6387 -- Abort_Undefer;
6388 -- <abortable-part>
6389 -- at end
6390 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6391 -- end;
6392 -- exception
6393 -- when Abort_Signal => Abort_Undefer;
6394 -- end;
6396 -- parm := P.param;
6397 -- parm := P.param;
6398 -- ...
6399 -- if not C then
6400 -- <triggered-statements>
6401 -- end if;
6402 -- end;
6404 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6405 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6406 -- as follows:
6408 -- declare
6409 -- P : parms := (parm, parm, parm);
6410 -- begin
6411 -- Call_Simple (acceptor-task, entry-index, P'Address);
6412 -- parm := P.param;
6413 -- parm := P.param;
6414 -- ...
6415 -- end;
6417 -- so the task at hand is to convert the latter expansion into the former
6419 -- If the trigger is a protected entry call, the select is implemented
6420 -- with Protected_Entry_Call:
6422 -- declare
6423 -- P : E1_Params := (param, param, param);
6424 -- Bnn : Communications_Block;
6426 -- begin
6427 -- declare
6429 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6431 -- procedure _clean is
6432 -- begin
6433 -- ...
6434 -- if Enqueued (Bnn) then
6435 -- Cancel_Protected_Entry_Call (Bnn);
6436 -- end if;
6437 -- ...
6438 -- end _clean;
6440 -- begin
6441 -- begin
6442 -- Protected_Entry_Call
6443 -- (po._object'Access, -- Object
6444 -- <entry index>, -- E
6445 -- P'Address, -- Uninterpreted_Data
6446 -- Asynchronous_Call, -- Mode
6447 -- Bnn); -- Block
6449 -- if Enqueued (Bnn) then
6450 -- <abortable-part>
6451 -- end if;
6452 -- at end
6453 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6454 -- end;
6455 -- exception
6456 -- when Abort_Signal => Abort_Undefer;
6457 -- end;
6459 -- if not Cancelled (Bnn) then
6460 -- <triggered-statements>
6461 -- end if;
6462 -- end;
6464 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6465 -- entry call:
6467 -- declare
6468 -- P : E1_Params := (param, param, param);
6469 -- Bnn : Communications_Block;
6471 -- begin
6472 -- Protected_Entry_Call
6473 -- (po._object'Access, -- Object
6474 -- <entry index>, -- E
6475 -- P'Address, -- Uninterpreted_Data
6476 -- Simple_Call, -- Mode
6477 -- Bnn); -- Block
6478 -- parm := P.param;
6479 -- parm := P.param;
6480 -- ...
6481 -- end;
6483 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6484 -- expanded into:
6486 -- declare
6487 -- B : Boolean := False;
6488 -- Bnn : Communication_Block;
6489 -- C : Ada.Tags.Prim_Op_Kind;
6490 -- D : System.Storage_Elements.Dummy_Communication_Block;
6491 -- K : Ada.Tags.Tagged_Kind :=
6492 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6493 -- P : Parameters := (Param1 .. ParamN);
6494 -- S : Integer;
6495 -- U : Boolean;
6497 -- begin
6498 -- if K = Ada.Tags.TK_Limited_Tagged then
6499 -- <dispatching-call>;
6500 -- <triggering-statements>;
6502 -- else
6503 -- S :=
6504 -- Ada.Tags.Get_Offset_Index
6505 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6507 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6509 -- if C = POK_Protected_Entry then
6510 -- declare
6511 -- procedure _clean is
6512 -- begin
6513 -- if Enqueued (Bnn) then
6514 -- Cancel_Protected_Entry_Call (Bnn);
6515 -- end if;
6516 -- end _clean;
6518 -- begin
6519 -- begin
6520 -- _Disp_Asynchronous_Select
6521 -- (<object>, S, P'Address, D, B);
6522 -- Bnn := Communication_Block (D);
6524 -- Param1 := P.Param1;
6525 -- ...
6526 -- ParamN := P.ParamN;
6528 -- if Enqueued (Bnn) then
6529 -- <abortable-statements>
6530 -- end if;
6531 -- at end
6532 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6533 -- end;
6534 -- exception
6535 -- when Abort_Signal => Abort_Undefer;
6536 -- end;
6538 -- if not Cancelled (Bnn) then
6539 -- <triggering-statements>
6540 -- end if;
6542 -- elsif C = POK_Task_Entry then
6543 -- declare
6544 -- procedure _clean is
6545 -- begin
6546 -- Cancel_Task_Entry_Call (U);
6547 -- end _clean;
6549 -- begin
6550 -- Abort_Defer;
6552 -- _Disp_Asynchronous_Select
6553 -- (<object>, S, P'Address, D, B);
6554 -- Bnn := Communication_Bloc (D);
6556 -- Param1 := P.Param1;
6557 -- ...
6558 -- ParamN := P.ParamN;
6560 -- begin
6561 -- begin
6562 -- Abort_Undefer;
6563 -- <abortable-statements>
6564 -- at end
6565 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6566 -- end;
6567 -- exception
6568 -- when Abort_Signal => Abort_Undefer;
6569 -- end;
6571 -- if not U then
6572 -- <triggering-statements>
6573 -- end if;
6574 -- end;
6576 -- else
6577 -- <dispatching-call>;
6578 -- <triggering-statements>
6579 -- end if;
6580 -- end if;
6581 -- end;
6583 -- The job is to convert this to the asynchronous form
6585 -- If the trigger is a delay statement, it will have been expanded into a
6586 -- call to one of the GNARL delay procedures. This routine will convert
6587 -- this into a protected entry call on a delay object and then continue
6588 -- processing as for a protected entry call trigger. This requires
6589 -- declaring a Delay_Block object and adding a pointer to this object to
6590 -- the parameter list of the delay procedure to form the parameter list of
6591 -- the entry call. This object is used by the runtime to queue the delay
6592 -- request.
6594 -- For a description of the use of P and the assignments after the call,
6595 -- see Expand_N_Entry_Call_Statement.
6597 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6598 Loc : constant Source_Ptr := Sloc (N);
6599 Abrt : constant Node_Id := Abortable_Part (N);
6600 Trig : constant Node_Id := Triggering_Alternative (N);
6602 Abort_Block_Ent : Entity_Id;
6603 Abortable_Block : Node_Id;
6604 Actuals : List_Id;
6605 Astats : List_Id;
6606 Blk_Ent : Entity_Id;
6607 Blk_Typ : Entity_Id;
6608 Call : Node_Id;
6609 Call_Ent : Entity_Id;
6610 Cancel_Param : Entity_Id;
6611 Cleanup_Block : Node_Id;
6612 Cleanup_Block_Ent : Entity_Id;
6613 Cleanup_Stmts : List_Id;
6614 Conc_Typ_Stmts : List_Id;
6615 Concval : Node_Id;
6616 Dblock_Ent : Entity_Id;
6617 Decl : Node_Id;
6618 Decls : List_Id;
6619 Ecall : Node_Id;
6620 Ename : Node_Id;
6621 Enqueue_Call : Node_Id;
6622 Formals : List_Id;
6623 Hdle : List_Id;
6624 Handler_Stmt : Node_Id;
6625 Index : Node_Id;
6626 Lim_Typ_Stmts : List_Id;
6627 N_Orig : Node_Id;
6628 Obj : Entity_Id;
6629 Param : Node_Id;
6630 Params : List_Id;
6631 Pdef : Entity_Id;
6632 ProtE_Stmts : List_Id;
6633 ProtP_Stmts : List_Id;
6634 Stmt : Node_Id;
6635 Stmts : List_Id;
6636 TaskE_Stmts : List_Id;
6637 Tstats : List_Id;
6639 B : Entity_Id; -- Call status flag
6640 Bnn : Entity_Id; -- Communication block
6641 C : Entity_Id; -- Call kind
6642 K : Entity_Id; -- Tagged kind
6643 P : Entity_Id; -- Parameter block
6644 S : Entity_Id; -- Primitive operation slot
6645 T : Entity_Id; -- Additional status flag
6647 begin
6648 Process_Statements_For_Controlled_Objects (Trig);
6649 Process_Statements_For_Controlled_Objects (Abrt);
6651 -- Retrieve Astats and Tstats now because the finalization machinery may
6652 -- wrap them in blocks.
6654 Astats := Statements (Abrt);
6655 Tstats := Statements (Trig);
6657 Blk_Ent := Make_Temporary (Loc, 'A');
6658 Ecall := Triggering_Statement (Trig);
6660 -- The arguments in the call may require dynamic allocation, and the
6661 -- call statement may have been transformed into a block. The block
6662 -- may contain additional declarations for internal entities, and the
6663 -- original call is found by sequential search.
6665 if Nkind (Ecall) = N_Block_Statement then
6666 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6667 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6668 N_Entry_Call_Statement)
6669 loop
6670 Next (Ecall);
6671 end loop;
6672 end if;
6674 -- This is either a dispatching call or a delay statement used as a
6675 -- trigger which was expanded into a procedure call.
6677 if Nkind (Ecall) = N_Procedure_Call_Statement then
6678 if Ada_Version >= Ada_2005
6679 and then
6680 (No (Original_Node (Ecall))
6681 or else not Nkind_In (Original_Node (Ecall),
6682 N_Delay_Relative_Statement,
6683 N_Delay_Until_Statement))
6684 then
6685 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6687 Decls := New_List;
6688 Stmts := New_List;
6690 -- Call status flag processing, generate:
6691 -- B : Boolean := False;
6693 B := Build_B (Loc, Decls);
6695 -- Communication block processing, generate:
6696 -- Bnn : Communication_Block;
6698 Bnn := Make_Temporary (Loc, 'B');
6699 Append_To (Decls,
6700 Make_Object_Declaration (Loc,
6701 Defining_Identifier => Bnn,
6702 Object_Definition =>
6703 New_Reference_To (RTE (RE_Communication_Block), Loc)));
6705 -- Call kind processing, generate:
6706 -- C : Ada.Tags.Prim_Op_Kind;
6708 C := Build_C (Loc, Decls);
6710 -- Tagged kind processing, generate:
6711 -- K : Ada.Tags.Tagged_Kind :=
6712 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6714 -- Dummy communication block, generate:
6715 -- D : Dummy_Communication_Block;
6717 Append_To (Decls,
6718 Make_Object_Declaration (Loc,
6719 Defining_Identifier =>
6720 Make_Defining_Identifier (Loc, Name_uD),
6721 Object_Definition =>
6722 New_Reference_To (
6723 RTE (RE_Dummy_Communication_Block), Loc)));
6725 K := Build_K (Loc, Decls, Obj);
6727 -- Parameter block processing
6729 Blk_Typ := Build_Parameter_Block
6730 (Loc, Actuals, Formals, Decls);
6731 P := Parameter_Block_Pack
6732 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6734 -- Dispatch table slot processing, generate:
6735 -- S : Integer;
6737 S := Build_S (Loc, Decls);
6739 -- Additional status flag processing, generate:
6740 -- Tnn : Boolean;
6742 T := Make_Temporary (Loc, 'T');
6743 Append_To (Decls,
6744 Make_Object_Declaration (Loc,
6745 Defining_Identifier => T,
6746 Object_Definition =>
6747 New_Reference_To (Standard_Boolean, Loc)));
6749 ------------------------------
6750 -- Protected entry handling --
6751 ------------------------------
6753 -- Generate:
6754 -- Param1 := P.Param1;
6755 -- ...
6756 -- ParamN := P.ParamN;
6758 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6760 -- Generate:
6761 -- Bnn := Communication_Block (D);
6763 Prepend_To (Cleanup_Stmts,
6764 Make_Assignment_Statement (Loc,
6765 Name =>
6766 New_Reference_To (Bnn, Loc),
6767 Expression =>
6768 Make_Unchecked_Type_Conversion (Loc,
6769 Subtype_Mark =>
6770 New_Reference_To (RTE (RE_Communication_Block), Loc),
6771 Expression => Make_Identifier (Loc, Name_uD))));
6773 -- Generate:
6774 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6776 Prepend_To (Cleanup_Stmts,
6777 Make_Procedure_Call_Statement (Loc,
6778 Name =>
6779 New_Reference_To (
6780 Find_Prim_Op (Etype (Etype (Obj)),
6781 Name_uDisp_Asynchronous_Select),
6782 Loc),
6783 Parameter_Associations =>
6784 New_List (
6785 New_Copy_Tree (Obj), -- <object>
6786 New_Reference_To (S, Loc), -- S
6787 Make_Attribute_Reference (Loc, -- P'Address
6788 Prefix =>
6789 New_Reference_To (P, Loc),
6790 Attribute_Name =>
6791 Name_Address),
6792 Make_Identifier (Loc, Name_uD), -- D
6793 New_Reference_To (B, Loc)))); -- B
6795 -- Generate:
6796 -- if Enqueued (Bnn) then
6797 -- <abortable-statements>
6798 -- end if;
6800 Append_To (Cleanup_Stmts,
6801 Make_If_Statement (Loc,
6802 Condition =>
6803 Make_Function_Call (Loc,
6804 Name =>
6805 New_Reference_To (RTE (RE_Enqueued), Loc),
6806 Parameter_Associations =>
6807 New_List (
6808 New_Reference_To (Bnn, Loc))),
6810 Then_Statements =>
6811 New_Copy_List_Tree (Astats)));
6813 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6814 -- will then generate a _clean for the communication block Bnn.
6816 -- Generate:
6817 -- declare
6818 -- procedure _clean is
6819 -- begin
6820 -- if Enqueued (Bnn) then
6821 -- Cancel_Protected_Entry_Call (Bnn);
6822 -- end if;
6823 -- end _clean;
6824 -- begin
6825 -- Cleanup_Stmts
6826 -- at end
6827 -- _clean;
6828 -- end;
6830 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
6831 Cleanup_Block :=
6832 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
6834 -- Wrap the cleanup block in an exception handling block
6836 -- Generate:
6837 -- begin
6838 -- Cleanup_Block
6839 -- exception
6840 -- when Abort_Signal => Abort_Undefer;
6841 -- end;
6843 Abort_Block_Ent := Make_Temporary (Loc, 'A');
6844 ProtE_Stmts :=
6845 New_List (
6846 Make_Implicit_Label_Declaration (Loc,
6847 Defining_Identifier =>
6848 Abort_Block_Ent),
6850 Build_Abort_Block
6851 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
6853 -- Generate:
6854 -- if not Cancelled (Bnn) then
6855 -- <triggering-statements>
6856 -- end if;
6858 Append_To (ProtE_Stmts,
6859 Make_If_Statement (Loc,
6860 Condition =>
6861 Make_Op_Not (Loc,
6862 Right_Opnd =>
6863 Make_Function_Call (Loc,
6864 Name =>
6865 New_Reference_To (RTE (RE_Cancelled), Loc),
6866 Parameter_Associations =>
6867 New_List (
6868 New_Reference_To (Bnn, Loc)))),
6870 Then_Statements =>
6871 New_Copy_List_Tree (Tstats)));
6873 -------------------------
6874 -- Task entry handling --
6875 -------------------------
6877 -- Generate:
6878 -- Param1 := P.Param1;
6879 -- ...
6880 -- ParamN := P.ParamN;
6882 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6884 -- Generate:
6885 -- Bnn := Communication_Block (D);
6887 Append_To (TaskE_Stmts,
6888 Make_Assignment_Statement (Loc,
6889 Name =>
6890 New_Reference_To (Bnn, Loc),
6891 Expression =>
6892 Make_Unchecked_Type_Conversion (Loc,
6893 Subtype_Mark =>
6894 New_Reference_To (RTE (RE_Communication_Block), Loc),
6895 Expression => Make_Identifier (Loc, Name_uD))));
6897 -- Generate:
6898 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6900 Prepend_To (TaskE_Stmts,
6901 Make_Procedure_Call_Statement (Loc,
6902 Name =>
6903 New_Reference_To (
6904 Find_Prim_Op (Etype (Etype (Obj)),
6905 Name_uDisp_Asynchronous_Select),
6906 Loc),
6907 Parameter_Associations =>
6908 New_List (
6909 New_Copy_Tree (Obj), -- <object>
6910 New_Reference_To (S, Loc), -- S
6911 Make_Attribute_Reference (Loc, -- P'Address
6912 Prefix =>
6913 New_Reference_To (P, Loc),
6914 Attribute_Name =>
6915 Name_Address),
6916 Make_Identifier (Loc, Name_uD), -- D
6917 New_Reference_To (B, Loc)))); -- B
6919 -- Generate:
6920 -- Abort_Defer;
6922 Prepend_To (TaskE_Stmts,
6923 Make_Procedure_Call_Statement (Loc,
6924 Name =>
6925 New_Reference_To (RTE (RE_Abort_Defer), Loc),
6926 Parameter_Associations =>
6927 No_List));
6929 -- Generate:
6930 -- Abort_Undefer;
6931 -- <abortable-statements>
6933 Cleanup_Stmts := New_Copy_List_Tree (Astats);
6935 Prepend_To (Cleanup_Stmts,
6936 Make_Procedure_Call_Statement (Loc,
6937 Name =>
6938 New_Reference_To (RTE (RE_Abort_Undefer), Loc),
6939 Parameter_Associations =>
6940 No_List));
6942 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6943 -- will generate a _clean for the additional status flag.
6945 -- Generate:
6946 -- declare
6947 -- procedure _clean is
6948 -- begin
6949 -- Cancel_Task_Entry_Call (U);
6950 -- end _clean;
6951 -- begin
6952 -- Cleanup_Stmts
6953 -- at end
6954 -- _clean;
6955 -- end;
6957 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
6958 Cleanup_Block :=
6959 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
6961 -- Wrap the cleanup block in an exception handling block
6963 -- Generate:
6964 -- begin
6965 -- Cleanup_Block
6966 -- exception
6967 -- when Abort_Signal => Abort_Undefer;
6968 -- end;
6970 Abort_Block_Ent := Make_Temporary (Loc, 'A');
6972 Append_To (TaskE_Stmts,
6973 Make_Implicit_Label_Declaration (Loc,
6974 Defining_Identifier => Abort_Block_Ent));
6976 Append_To (TaskE_Stmts,
6977 Build_Abort_Block
6978 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
6980 -- Generate:
6981 -- if not T then
6982 -- <triggering-statements>
6983 -- end if;
6985 Append_To (TaskE_Stmts,
6986 Make_If_Statement (Loc,
6987 Condition =>
6988 Make_Op_Not (Loc,
6989 Right_Opnd =>
6990 New_Reference_To (T, Loc)),
6992 Then_Statements =>
6993 New_Copy_List_Tree (Tstats)));
6995 ----------------------------------
6996 -- Protected procedure handling --
6997 ----------------------------------
6999 -- Generate:
7000 -- <dispatching-call>;
7001 -- <triggering-statements>
7003 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7004 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7006 -- Generate:
7007 -- S := Ada.Tags.Get_Offset_Index
7008 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7010 Conc_Typ_Stmts :=
7011 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7013 -- Generate:
7014 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7016 Append_To (Conc_Typ_Stmts,
7017 Make_Procedure_Call_Statement (Loc,
7018 Name =>
7019 New_Reference_To (
7020 Find_Prim_Op (Etype (Etype (Obj)),
7021 Name_uDisp_Get_Prim_Op_Kind),
7022 Loc),
7023 Parameter_Associations =>
7024 New_List (
7025 New_Copy_Tree (Obj),
7026 New_Reference_To (S, Loc),
7027 New_Reference_To (C, Loc))));
7029 -- Generate:
7030 -- if C = POK_Procedure_Entry then
7031 -- ProtE_Stmts
7032 -- elsif C = POK_Task_Entry then
7033 -- TaskE_Stmts
7034 -- else
7035 -- ProtP_Stmts
7036 -- end if;
7038 Append_To (Conc_Typ_Stmts,
7039 Make_If_Statement (Loc,
7040 Condition =>
7041 Make_Op_Eq (Loc,
7042 Left_Opnd =>
7043 New_Reference_To (C, Loc),
7044 Right_Opnd =>
7045 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
7047 Then_Statements =>
7048 ProtE_Stmts,
7050 Elsif_Parts =>
7051 New_List (
7052 Make_Elsif_Part (Loc,
7053 Condition =>
7054 Make_Op_Eq (Loc,
7055 Left_Opnd =>
7056 New_Reference_To (C, Loc),
7057 Right_Opnd =>
7058 New_Reference_To (RTE (RE_POK_Task_Entry), Loc)),
7060 Then_Statements =>
7061 TaskE_Stmts)),
7063 Else_Statements =>
7064 ProtP_Stmts));
7066 -- Generate:
7067 -- <dispatching-call>;
7068 -- <triggering-statements>
7070 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7071 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7073 -- Generate:
7074 -- if K = Ada.Tags.TK_Limited_Tagged then
7075 -- Lim_Typ_Stmts
7076 -- else
7077 -- Conc_Typ_Stmts
7078 -- end if;
7080 Append_To (Stmts,
7081 Make_If_Statement (Loc,
7082 Condition =>
7083 Make_Op_Eq (Loc,
7084 Left_Opnd =>
7085 New_Reference_To (K, Loc),
7086 Right_Opnd =>
7087 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
7089 Then_Statements =>
7090 Lim_Typ_Stmts,
7092 Else_Statements =>
7093 Conc_Typ_Stmts));
7095 Rewrite (N,
7096 Make_Block_Statement (Loc,
7097 Declarations =>
7098 Decls,
7099 Handled_Statement_Sequence =>
7100 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7102 Analyze (N);
7103 return;
7105 -- Delay triggering statement processing
7107 else
7108 -- Add a Delay_Block object to the parameter list of the delay
7109 -- procedure to form the parameter list of the Wait entry call.
7111 Dblock_Ent := Make_Temporary (Loc, 'D');
7113 Pdef := Entity (Name (Ecall));
7115 if Is_RTE (Pdef, RO_CA_Delay_For) then
7116 Enqueue_Call :=
7117 New_Reference_To (RTE (RE_Enqueue_Duration), Loc);
7119 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7120 Enqueue_Call :=
7121 New_Reference_To (RTE (RE_Enqueue_Calendar), Loc);
7123 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7124 Enqueue_Call := New_Reference_To (RTE (RE_Enqueue_RT), Loc);
7125 end if;
7127 Append_To (Parameter_Associations (Ecall),
7128 Make_Attribute_Reference (Loc,
7129 Prefix => New_Reference_To (Dblock_Ent, Loc),
7130 Attribute_Name => Name_Unchecked_Access));
7132 -- Create the inner block to protect the abortable part
7134 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7136 Prepend_To (Astats,
7137 Make_Procedure_Call_Statement (Loc,
7138 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
7140 Abortable_Block :=
7141 Make_Block_Statement (Loc,
7142 Identifier => New_Reference_To (Blk_Ent, Loc),
7143 Handled_Statement_Sequence =>
7144 Make_Handled_Sequence_Of_Statements (Loc,
7145 Statements => Astats),
7146 Has_Created_Identifier => True,
7147 Is_Asynchronous_Call_Block => True);
7149 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7151 Rewrite (Ecall,
7152 Make_Implicit_If_Statement (N,
7153 Condition => Make_Function_Call (Loc,
7154 Name => Enqueue_Call,
7155 Parameter_Associations => Parameter_Associations (Ecall)),
7156 Then_Statements =>
7157 New_List (Make_Block_Statement (Loc,
7158 Handled_Statement_Sequence =>
7159 Make_Handled_Sequence_Of_Statements (Loc,
7160 Statements => New_List (
7161 Make_Implicit_Label_Declaration (Loc,
7162 Defining_Identifier => Blk_Ent,
7163 Label_Construct => Abortable_Block),
7164 Abortable_Block),
7165 Exception_Handlers => Hdle)))));
7167 Stmts := New_List (Ecall);
7169 -- Construct statement sequence for new block
7171 Append_To (Stmts,
7172 Make_Implicit_If_Statement (N,
7173 Condition => Make_Function_Call (Loc,
7174 Name => New_Reference_To (
7175 RTE (RE_Timed_Out), Loc),
7176 Parameter_Associations => New_List (
7177 Make_Attribute_Reference (Loc,
7178 Prefix => New_Reference_To (Dblock_Ent, Loc),
7179 Attribute_Name => Name_Unchecked_Access))),
7180 Then_Statements => Tstats));
7182 -- The result is the new block
7184 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7186 Rewrite (N,
7187 Make_Block_Statement (Loc,
7188 Declarations => New_List (
7189 Make_Object_Declaration (Loc,
7190 Defining_Identifier => Dblock_Ent,
7191 Aliased_Present => True,
7192 Object_Definition => New_Reference_To (
7193 RTE (RE_Delay_Block), Loc))),
7195 Handled_Statement_Sequence =>
7196 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7198 Analyze (N);
7199 return;
7200 end if;
7202 else
7203 N_Orig := N;
7204 end if;
7206 Extract_Entry (Ecall, Concval, Ename, Index);
7207 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7209 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7210 Decls := Declarations (Ecall);
7212 if Is_Protected_Type (Etype (Concval)) then
7214 -- Get the declarations of the block expanded from the entry call
7216 Decl := First (Decls);
7217 while Present (Decl)
7218 and then
7219 (Nkind (Decl) /= N_Object_Declaration
7220 or else not Is_RTE (Etype (Object_Definition (Decl)),
7221 RE_Communication_Block))
7222 loop
7223 Next (Decl);
7224 end loop;
7226 pragma Assert (Present (Decl));
7227 Cancel_Param := Defining_Identifier (Decl);
7229 -- Change the mode of the Protected_Entry_Call call
7231 -- Protected_Entry_Call (
7232 -- Object => po._object'Access,
7233 -- E => <entry index>;
7234 -- Uninterpreted_Data => P'Address;
7235 -- Mode => Asynchronous_Call;
7236 -- Block => Bnn);
7238 Stmt := First (Stmts);
7240 -- Skip assignments to temporaries created for in-out parameters
7242 -- This makes unwarranted assumptions about the shape of the expanded
7243 -- tree for the call, and should be cleaned up ???
7245 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7246 Next (Stmt);
7247 end loop;
7249 Call := Stmt;
7251 Param := First (Parameter_Associations (Call));
7252 while Present (Param)
7253 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7254 loop
7255 Next (Param);
7256 end loop;
7258 pragma Assert (Present (Param));
7259 Rewrite (Param, New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
7260 Analyze (Param);
7262 -- Append an if statement to execute the abortable part
7264 -- Generate:
7265 -- if Enqueued (Bnn) then
7267 Append_To (Stmts,
7268 Make_Implicit_If_Statement (N,
7269 Condition => Make_Function_Call (Loc,
7270 Name => New_Reference_To (RTE (RE_Enqueued), Loc),
7271 Parameter_Associations => New_List (
7272 New_Reference_To (Cancel_Param, Loc))),
7273 Then_Statements => Astats));
7275 Abortable_Block :=
7276 Make_Block_Statement (Loc,
7277 Identifier => New_Reference_To (Blk_Ent, Loc),
7278 Handled_Statement_Sequence =>
7279 Make_Handled_Sequence_Of_Statements (Loc,
7280 Statements => Stmts),
7281 Has_Created_Identifier => True,
7282 Is_Asynchronous_Call_Block => True);
7284 -- For the VM call Update_Exception instead of Abort_Undefer.
7285 -- See 4jexcept.ads for an explanation.
7287 if VM_Target = No_VM then
7288 if Exception_Mechanism = Back_End_Exceptions then
7290 -- Aborts are not deferred at beginning of exception handlers
7291 -- in ZCX.
7293 Handler_Stmt := Make_Null_Statement (Loc);
7295 else
7296 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7297 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc),
7298 Parameter_Associations => No_List);
7299 end if;
7300 else
7301 Handler_Stmt := Make_Procedure_Call_Statement (Loc,
7302 Name => New_Reference_To (RTE (RE_Update_Exception), Loc),
7303 Parameter_Associations => New_List (
7304 Make_Function_Call (Loc,
7305 Name => New_Occurrence_Of
7306 (RTE (RE_Current_Target_Exception), Loc))));
7307 end if;
7309 Stmts := New_List (
7310 Make_Block_Statement (Loc,
7311 Handled_Statement_Sequence =>
7312 Make_Handled_Sequence_Of_Statements (Loc,
7313 Statements => New_List (
7314 Make_Implicit_Label_Declaration (Loc,
7315 Defining_Identifier => Blk_Ent,
7316 Label_Construct => Abortable_Block),
7317 Abortable_Block),
7319 -- exception
7321 Exception_Handlers => New_List (
7322 Make_Implicit_Exception_Handler (Loc,
7324 -- when Abort_Signal =>
7325 -- Abort_Undefer.all;
7327 Exception_Choices =>
7328 New_List (New_Reference_To (Stand.Abort_Signal, Loc)),
7329 Statements => New_List (Handler_Stmt))))),
7331 -- if not Cancelled (Bnn) then
7332 -- triggered statements
7333 -- end if;
7335 Make_Implicit_If_Statement (N,
7336 Condition => Make_Op_Not (Loc,
7337 Right_Opnd =>
7338 Make_Function_Call (Loc,
7339 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7340 Parameter_Associations => New_List (
7341 New_Occurrence_Of (Cancel_Param, Loc)))),
7342 Then_Statements => Tstats));
7344 -- Asynchronous task entry call
7346 else
7347 if No (Decls) then
7348 Decls := New_List;
7349 end if;
7351 B := Make_Defining_Identifier (Loc, Name_uB);
7353 -- Insert declaration of B in declarations of existing block
7355 Prepend_To (Decls,
7356 Make_Object_Declaration (Loc,
7357 Defining_Identifier => B,
7358 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7360 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7362 -- Insert declaration of C in declarations of existing block
7364 Prepend_To (Decls,
7365 Make_Object_Declaration (Loc,
7366 Defining_Identifier => Cancel_Param,
7367 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
7369 -- Remove and save the call to Call_Simple
7371 Stmt := First (Stmts);
7373 -- Skip assignments to temporaries created for in-out parameters.
7374 -- This makes unwarranted assumptions about the shape of the expanded
7375 -- tree for the call, and should be cleaned up ???
7377 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7378 Next (Stmt);
7379 end loop;
7381 Call := Stmt;
7383 -- Create the inner block to protect the abortable part
7385 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7387 Prepend_To (Astats,
7388 Make_Procedure_Call_Statement (Loc,
7389 Name => New_Reference_To (RTE (RE_Abort_Undefer), Loc)));
7391 Abortable_Block :=
7392 Make_Block_Statement (Loc,
7393 Identifier => New_Reference_To (Blk_Ent, Loc),
7394 Handled_Statement_Sequence =>
7395 Make_Handled_Sequence_Of_Statements (Loc,
7396 Statements => Astats),
7397 Has_Created_Identifier => True,
7398 Is_Asynchronous_Call_Block => True);
7400 Insert_After (Call,
7401 Make_Block_Statement (Loc,
7402 Handled_Statement_Sequence =>
7403 Make_Handled_Sequence_Of_Statements (Loc,
7404 Statements => New_List (
7405 Make_Implicit_Label_Declaration (Loc,
7406 Defining_Identifier =>
7407 Blk_Ent,
7408 Label_Construct =>
7409 Abortable_Block),
7410 Abortable_Block),
7411 Exception_Handlers => Hdle)));
7413 -- Create new call statement
7415 Params := Parameter_Associations (Call);
7417 Append_To (Params,
7418 New_Reference_To (RTE (RE_Asynchronous_Call), Loc));
7419 Append_To (Params,
7420 New_Reference_To (B, Loc));
7422 Rewrite (Call,
7423 Make_Procedure_Call_Statement (Loc,
7424 Name =>
7425 New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
7426 Parameter_Associations => Params));
7428 -- Construct statement sequence for new block
7430 Append_To (Stmts,
7431 Make_Implicit_If_Statement (N,
7432 Condition =>
7433 Make_Op_Not (Loc,
7434 New_Reference_To (Cancel_Param, Loc)),
7435 Then_Statements => Tstats));
7437 -- Protected the call against abort
7439 Prepend_To (Stmts,
7440 Make_Procedure_Call_Statement (Loc,
7441 Name => New_Reference_To (RTE (RE_Abort_Defer), Loc),
7442 Parameter_Associations => Empty_List));
7443 end if;
7445 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7447 -- The result is the new block
7449 Rewrite (N_Orig,
7450 Make_Block_Statement (Loc,
7451 Declarations => Decls,
7452 Handled_Statement_Sequence =>
7453 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7455 Analyze (N_Orig);
7456 end Expand_N_Asynchronous_Select;
7458 -------------------------------------
7459 -- Expand_N_Conditional_Entry_Call --
7460 -------------------------------------
7462 -- The conditional task entry call is converted to a call to
7463 -- Task_Entry_Call:
7465 -- declare
7466 -- B : Boolean;
7467 -- P : parms := (parm, parm, parm);
7469 -- begin
7470 -- Task_Entry_Call
7471 -- (<acceptor-task>, -- Acceptor
7472 -- <entry-index>, -- E
7473 -- P'Address, -- Uninterpreted_Data
7474 -- Conditional_Call, -- Mode
7475 -- B); -- Rendezvous_Successful
7476 -- parm := P.param;
7477 -- parm := P.param;
7478 -- ...
7479 -- if B then
7480 -- normal-statements
7481 -- else
7482 -- else-statements
7483 -- end if;
7484 -- end;
7486 -- For a description of the use of P and the assignments after the call,
7487 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7488 -- conditional entry call has already been expanded (by the Expand_N_Entry
7489 -- _Call_Statement procedure) as follows:
7491 -- declare
7492 -- P : parms := (parm, parm, parm);
7493 -- begin
7494 -- ... info for in-out parameters
7495 -- Call_Simple (acceptor-task, entry-index, P'Address);
7496 -- parm := P.param;
7497 -- parm := P.param;
7498 -- ...
7499 -- end;
7501 -- so the task at hand is to convert the latter expansion into the former
7503 -- The conditional protected entry call is converted to a call to
7504 -- Protected_Entry_Call:
7506 -- declare
7507 -- P : parms := (parm, parm, parm);
7508 -- Bnn : Communications_Block;
7510 -- begin
7511 -- Protected_Entry_Call
7512 -- (po._object'Access, -- Object
7513 -- <entry index>, -- E
7514 -- P'Address, -- Uninterpreted_Data
7515 -- Conditional_Call, -- Mode
7516 -- Bnn); -- Block
7517 -- parm := P.param;
7518 -- parm := P.param;
7519 -- ...
7520 -- if Cancelled (Bnn) then
7521 -- else-statements
7522 -- else
7523 -- normal-statements
7524 -- end if;
7525 -- end;
7527 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7528 -- into:
7530 -- declare
7531 -- B : Boolean := False;
7532 -- C : Ada.Tags.Prim_Op_Kind;
7533 -- K : Ada.Tags.Tagged_Kind :=
7534 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7535 -- P : Parameters := (Param1 .. ParamN);
7536 -- S : Integer;
7538 -- begin
7539 -- if K = Ada.Tags.TK_Limited_Tagged then
7540 -- <dispatching-call>;
7541 -- <triggering-statements>
7543 -- else
7544 -- S :=
7545 -- Ada.Tags.Get_Offset_Index
7546 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7548 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7550 -- if C = POK_Protected_Entry
7551 -- or else C = POK_Task_Entry
7552 -- then
7553 -- Param1 := P.Param1;
7554 -- ...
7555 -- ParamN := P.ParamN;
7556 -- end if;
7558 -- if B then
7559 -- if C = POK_Procedure
7560 -- or else C = POK_Protected_Procedure
7561 -- or else C = POK_Task_Procedure
7562 -- then
7563 -- <dispatching-call>;
7564 -- end if;
7566 -- <triggering-statements>
7567 -- else
7568 -- <else-statements>
7569 -- end if;
7570 -- end if;
7571 -- end;
7573 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7574 Loc : constant Source_Ptr := Sloc (N);
7575 Alt : constant Node_Id := Entry_Call_Alternative (N);
7576 Blk : Node_Id := Entry_Call_Statement (Alt);
7578 Actuals : List_Id;
7579 Blk_Typ : Entity_Id;
7580 Call : Node_Id;
7581 Call_Ent : Entity_Id;
7582 Conc_Typ_Stmts : List_Id;
7583 Decl : Node_Id;
7584 Decls : List_Id;
7585 Formals : List_Id;
7586 Lim_Typ_Stmts : List_Id;
7587 N_Stats : List_Id;
7588 Obj : Entity_Id;
7589 Param : Node_Id;
7590 Params : List_Id;
7591 Stmt : Node_Id;
7592 Stmts : List_Id;
7593 Transient_Blk : Node_Id;
7594 Unpack : List_Id;
7596 B : Entity_Id; -- Call status flag
7597 C : Entity_Id; -- Call kind
7598 K : Entity_Id; -- Tagged kind
7599 P : Entity_Id; -- Parameter block
7600 S : Entity_Id; -- Primitive operation slot
7602 begin
7603 Process_Statements_For_Controlled_Objects (N);
7605 if Ada_Version >= Ada_2005
7606 and then Nkind (Blk) = N_Procedure_Call_Statement
7607 then
7608 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7610 Decls := New_List;
7611 Stmts := New_List;
7613 -- Call status flag processing, generate:
7614 -- B : Boolean := False;
7616 B := Build_B (Loc, Decls);
7618 -- Call kind processing, generate:
7619 -- C : Ada.Tags.Prim_Op_Kind;
7621 C := Build_C (Loc, Decls);
7623 -- Tagged kind processing, generate:
7624 -- K : Ada.Tags.Tagged_Kind :=
7625 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7627 K := Build_K (Loc, Decls, Obj);
7629 -- Parameter block processing
7631 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7632 P := Parameter_Block_Pack
7633 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7635 -- Dispatch table slot processing, generate:
7636 -- S : Integer;
7638 S := Build_S (Loc, Decls);
7640 -- Generate:
7641 -- S := Ada.Tags.Get_Offset_Index
7642 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7644 Conc_Typ_Stmts :=
7645 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7647 -- Generate:
7648 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7650 Append_To (Conc_Typ_Stmts,
7651 Make_Procedure_Call_Statement (Loc,
7652 Name =>
7653 New_Reference_To (
7654 Find_Prim_Op (Etype (Etype (Obj)),
7655 Name_uDisp_Conditional_Select),
7656 Loc),
7657 Parameter_Associations =>
7658 New_List (
7659 New_Copy_Tree (Obj), -- <object>
7660 New_Reference_To (S, Loc), -- S
7661 Make_Attribute_Reference (Loc, -- P'Address
7662 Prefix =>
7663 New_Reference_To (P, Loc),
7664 Attribute_Name =>
7665 Name_Address),
7666 New_Reference_To (C, Loc), -- C
7667 New_Reference_To (B, Loc)))); -- B
7669 -- Generate:
7670 -- if C = POK_Protected_Entry
7671 -- or else C = POK_Task_Entry
7672 -- then
7673 -- Param1 := P.Param1;
7674 -- ...
7675 -- ParamN := P.ParamN;
7676 -- end if;
7678 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7680 -- Generate the if statement only when the packed parameters need
7681 -- explicit assignments to their corresponding actuals.
7683 if Present (Unpack) then
7684 Append_To (Conc_Typ_Stmts,
7685 Make_If_Statement (Loc,
7687 Condition =>
7688 Make_Or_Else (Loc,
7689 Left_Opnd =>
7690 Make_Op_Eq (Loc,
7691 Left_Opnd =>
7692 New_Reference_To (C, Loc),
7693 Right_Opnd =>
7694 New_Reference_To (RTE (
7695 RE_POK_Protected_Entry), Loc)),
7696 Right_Opnd =>
7697 Make_Op_Eq (Loc,
7698 Left_Opnd =>
7699 New_Reference_To (C, Loc),
7700 Right_Opnd =>
7701 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
7703 Then_Statements =>
7704 Unpack));
7705 end if;
7707 -- Generate:
7708 -- if B then
7709 -- if C = POK_Procedure
7710 -- or else C = POK_Protected_Procedure
7711 -- or else C = POK_Task_Procedure
7712 -- then
7713 -- <dispatching-call>
7714 -- end if;
7715 -- <normal-statements>
7716 -- else
7717 -- <else-statements>
7718 -- end if;
7720 N_Stats := New_Copy_List_Tree (Statements (Alt));
7722 Prepend_To (N_Stats,
7723 Make_If_Statement (Loc,
7724 Condition =>
7725 Make_Or_Else (Loc,
7726 Left_Opnd =>
7727 Make_Op_Eq (Loc,
7728 Left_Opnd =>
7729 New_Reference_To (C, Loc),
7730 Right_Opnd =>
7731 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
7733 Right_Opnd =>
7734 Make_Or_Else (Loc,
7735 Left_Opnd =>
7736 Make_Op_Eq (Loc,
7737 Left_Opnd =>
7738 New_Reference_To (C, Loc),
7739 Right_Opnd =>
7740 New_Reference_To (RTE (
7741 RE_POK_Protected_Procedure), Loc)),
7743 Right_Opnd =>
7744 Make_Op_Eq (Loc,
7745 Left_Opnd =>
7746 New_Reference_To (C, Loc),
7747 Right_Opnd =>
7748 New_Reference_To (RTE (
7749 RE_POK_Task_Procedure), Loc)))),
7751 Then_Statements =>
7752 New_List (Blk)));
7754 Append_To (Conc_Typ_Stmts,
7755 Make_If_Statement (Loc,
7756 Condition => New_Reference_To (B, Loc),
7757 Then_Statements => N_Stats,
7758 Else_Statements => Else_Statements (N)));
7760 -- Generate:
7761 -- <dispatching-call>;
7762 -- <triggering-statements>
7764 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
7765 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7767 -- Generate:
7768 -- if K = Ada.Tags.TK_Limited_Tagged then
7769 -- Lim_Typ_Stmts
7770 -- else
7771 -- Conc_Typ_Stmts
7772 -- end if;
7774 Append_To (Stmts,
7775 Make_If_Statement (Loc,
7776 Condition =>
7777 Make_Op_Eq (Loc,
7778 Left_Opnd =>
7779 New_Reference_To (K, Loc),
7780 Right_Opnd =>
7781 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
7783 Then_Statements =>
7784 Lim_Typ_Stmts,
7786 Else_Statements =>
7787 Conc_Typ_Stmts));
7789 Rewrite (N,
7790 Make_Block_Statement (Loc,
7791 Declarations =>
7792 Decls,
7793 Handled_Statement_Sequence =>
7794 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7796 -- As described above, The entry alternative is transformed into a
7797 -- block that contains the gnulli call, and possibly assignment
7798 -- statements for in-out parameters. The gnulli call may itself be
7799 -- rewritten into a transient block if some unconstrained parameters
7800 -- require it. We need to retrieve the call to complete its parameter
7801 -- list.
7803 else
7804 Transient_Blk :=
7805 First_Real_Statement (Handled_Statement_Sequence (Blk));
7807 if Present (Transient_Blk)
7808 and then Nkind (Transient_Blk) = N_Block_Statement
7809 then
7810 Blk := Transient_Blk;
7811 end if;
7813 Stmts := Statements (Handled_Statement_Sequence (Blk));
7814 Stmt := First (Stmts);
7815 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7816 Next (Stmt);
7817 end loop;
7819 Call := Stmt;
7820 Params := Parameter_Associations (Call);
7822 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7824 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7826 Param := First (Params);
7827 while Present (Param)
7828 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7829 loop
7830 Next (Param);
7831 end loop;
7833 pragma Assert (Present (Param));
7834 Rewrite (Param, New_Reference_To (RTE (RE_Conditional_Call), Loc));
7836 Analyze (Param);
7838 -- Find the Communication_Block parameter for the call to the
7839 -- Cancelled function.
7841 Decl := First (Declarations (Blk));
7842 while Present (Decl)
7843 and then not Is_RTE (Etype (Object_Definition (Decl)),
7844 RE_Communication_Block)
7845 loop
7846 Next (Decl);
7847 end loop;
7849 -- Add an if statement to execute the else part if the call
7850 -- does not succeed (as indicated by the Cancelled predicate).
7852 Append_To (Stmts,
7853 Make_Implicit_If_Statement (N,
7854 Condition => Make_Function_Call (Loc,
7855 Name => New_Reference_To (RTE (RE_Cancelled), Loc),
7856 Parameter_Associations => New_List (
7857 New_Reference_To (Defining_Identifier (Decl), Loc))),
7858 Then_Statements => Else_Statements (N),
7859 Else_Statements => Statements (Alt)));
7861 else
7862 B := Make_Defining_Identifier (Loc, Name_uB);
7864 -- Insert declaration of B in declarations of existing block
7866 if No (Declarations (Blk)) then
7867 Set_Declarations (Blk, New_List);
7868 end if;
7870 Prepend_To (Declarations (Blk),
7871 Make_Object_Declaration (Loc,
7872 Defining_Identifier => B,
7873 Object_Definition =>
7874 New_Reference_To (Standard_Boolean, Loc)));
7876 -- Create new call statement
7878 Append_To (Params,
7879 New_Reference_To (RTE (RE_Conditional_Call), Loc));
7880 Append_To (Params, New_Reference_To (B, Loc));
7882 Rewrite (Call,
7883 Make_Procedure_Call_Statement (Loc,
7884 Name => New_Reference_To (RTE (RE_Task_Entry_Call), Loc),
7885 Parameter_Associations => Params));
7887 -- Construct statement sequence for new block
7889 Append_To (Stmts,
7890 Make_Implicit_If_Statement (N,
7891 Condition => New_Reference_To (B, Loc),
7892 Then_Statements => Statements (Alt),
7893 Else_Statements => Else_Statements (N)));
7894 end if;
7896 -- The result is the new block
7898 Rewrite (N,
7899 Make_Block_Statement (Loc,
7900 Declarations => Declarations (Blk),
7901 Handled_Statement_Sequence =>
7902 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7903 end if;
7905 Analyze (N);
7906 end Expand_N_Conditional_Entry_Call;
7908 ---------------------------------------
7909 -- Expand_N_Delay_Relative_Statement --
7910 ---------------------------------------
7912 -- Delay statement is implemented as a procedure call to Delay_For
7913 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
7914 -- simple delays imposed by the use of Protected Objects.
7916 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
7917 Loc : constant Source_Ptr := Sloc (N);
7918 begin
7919 Rewrite (N,
7920 Make_Procedure_Call_Statement (Loc,
7921 Name => New_Reference_To (RTE (RO_CA_Delay_For), Loc),
7922 Parameter_Associations => New_List (Expression (N))));
7923 Analyze (N);
7924 end Expand_N_Delay_Relative_Statement;
7926 ------------------------------------
7927 -- Expand_N_Delay_Until_Statement --
7928 ------------------------------------
7930 -- Delay Until statement is implemented as a procedure call to
7931 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
7933 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
7934 Loc : constant Source_Ptr := Sloc (N);
7935 Typ : Entity_Id;
7937 begin
7938 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
7939 Typ := RTE (RO_CA_Delay_Until);
7940 else
7941 Typ := RTE (RO_RT_Delay_Until);
7942 end if;
7944 Rewrite (N,
7945 Make_Procedure_Call_Statement (Loc,
7946 Name => New_Reference_To (Typ, Loc),
7947 Parameter_Associations => New_List (Expression (N))));
7949 Analyze (N);
7950 end Expand_N_Delay_Until_Statement;
7952 -------------------------
7953 -- Expand_N_Entry_Body --
7954 -------------------------
7956 procedure Expand_N_Entry_Body (N : Node_Id) is
7957 begin
7958 -- Associate discriminals with the next protected operation body to be
7959 -- expanded.
7961 if Present (Next_Protected_Operation (N)) then
7962 Set_Discriminals (Parent (Current_Scope));
7963 end if;
7964 end Expand_N_Entry_Body;
7966 -----------------------------------
7967 -- Expand_N_Entry_Call_Statement --
7968 -----------------------------------
7970 -- An entry call is expanded into GNARLI calls to implement a simple entry
7971 -- call (see Build_Simple_Entry_Call).
7973 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
7974 Concval : Node_Id;
7975 Ename : Node_Id;
7976 Index : Node_Id;
7978 begin
7979 if No_Run_Time_Mode then
7980 Error_Msg_CRT ("entry call", N);
7981 return;
7982 end if;
7984 -- If this entry call is part of an asynchronous select, don't expand it
7985 -- here; it will be expanded with the select statement. Don't expand
7986 -- timed entry calls either, as they are translated into asynchronous
7987 -- entry calls.
7989 -- ??? This whole approach is questionable; it may be better to go back
7990 -- to allowing the expansion to take place and then attempting to fix it
7991 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
7992 -- whether the expanded call is on a task or protected entry.
7994 if (Nkind (Parent (N)) /= N_Triggering_Alternative
7995 or else N /= Triggering_Statement (Parent (N)))
7996 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
7997 or else N /= Entry_Call_Statement (Parent (N))
7998 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
7999 then
8000 Extract_Entry (N, Concval, Ename, Index);
8001 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8002 end if;
8003 end Expand_N_Entry_Call_Statement;
8005 --------------------------------
8006 -- Expand_N_Entry_Declaration --
8007 --------------------------------
8009 -- If there are parameters, then first, each of the formals is marked by
8010 -- setting Is_Entry_Formal. Next a record type is built which is used to
8011 -- hold the parameter values. The name of this record type is entryP where
8012 -- entry is the name of the entry, with an additional corresponding access
8013 -- type called entryPA. The record type has matching components for each
8014 -- formal (the component names are the same as the formal names). For
8015 -- elementary types, the component type matches the formal type. For
8016 -- composite types, an access type is declared (with the name formalA)
8017 -- which designates the formal type, and the type of the component is this
8018 -- access type. Finally the Entry_Component of each formal is set to
8019 -- reference the corresponding record component.
8021 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8022 Loc : constant Source_Ptr := Sloc (N);
8023 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8024 Components : List_Id;
8025 Formal : Node_Id;
8026 Ftype : Entity_Id;
8027 Last_Decl : Node_Id;
8028 Component : Entity_Id;
8029 Ctype : Entity_Id;
8030 Decl : Node_Id;
8031 Rec_Ent : Entity_Id;
8032 Acc_Ent : Entity_Id;
8034 begin
8035 Formal := First_Formal (Entry_Ent);
8036 Last_Decl := N;
8038 -- Most processing is done only if parameters are present
8040 if Present (Formal) then
8041 Components := New_List;
8043 -- Loop through formals
8045 while Present (Formal) loop
8046 Set_Is_Entry_Formal (Formal);
8047 Component :=
8048 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8049 Set_Entry_Component (Formal, Component);
8050 Set_Entry_Formal (Component, Formal);
8051 Ftype := Etype (Formal);
8053 -- Declare new access type and then append
8055 Ctype := Make_Temporary (Loc, 'A');
8057 Decl :=
8058 Make_Full_Type_Declaration (Loc,
8059 Defining_Identifier => Ctype,
8060 Type_Definition =>
8061 Make_Access_To_Object_Definition (Loc,
8062 All_Present => True,
8063 Constant_Present => Ekind (Formal) = E_In_Parameter,
8064 Subtype_Indication => New_Reference_To (Ftype, Loc)));
8066 Insert_After (Last_Decl, Decl);
8067 Last_Decl := Decl;
8069 Append_To (Components,
8070 Make_Component_Declaration (Loc,
8071 Defining_Identifier => Component,
8072 Component_Definition =>
8073 Make_Component_Definition (Loc,
8074 Aliased_Present => False,
8075 Subtype_Indication => New_Reference_To (Ctype, Loc))));
8077 Next_Formal_With_Extras (Formal);
8078 end loop;
8080 -- Create the Entry_Parameter_Record declaration
8082 Rec_Ent := Make_Temporary (Loc, 'P');
8084 Decl :=
8085 Make_Full_Type_Declaration (Loc,
8086 Defining_Identifier => Rec_Ent,
8087 Type_Definition =>
8088 Make_Record_Definition (Loc,
8089 Component_List =>
8090 Make_Component_List (Loc,
8091 Component_Items => Components)));
8093 Insert_After (Last_Decl, Decl);
8094 Last_Decl := Decl;
8096 -- Construct and link in the corresponding access type
8098 Acc_Ent := Make_Temporary (Loc, 'A');
8100 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8102 Decl :=
8103 Make_Full_Type_Declaration (Loc,
8104 Defining_Identifier => Acc_Ent,
8105 Type_Definition =>
8106 Make_Access_To_Object_Definition (Loc,
8107 All_Present => True,
8108 Subtype_Indication => New_Reference_To (Rec_Ent, Loc)));
8110 Insert_After (Last_Decl, Decl);
8111 end if;
8112 end Expand_N_Entry_Declaration;
8114 -----------------------------
8115 -- Expand_N_Protected_Body --
8116 -----------------------------
8118 -- Protected bodies are expanded to the completion of the subprograms
8119 -- created for the corresponding protected type. These are a protected and
8120 -- unprotected version of each protected subprogram in the object, a
8121 -- function to calculate each entry barrier, and a procedure to execute the
8122 -- sequence of statements of each protected entry body. For example, for
8123 -- protected type ptype:
8125 -- function entB
8126 -- (O : System.Address;
8127 -- E : Protected_Entry_Index)
8128 -- return Boolean
8129 -- is
8130 -- <discriminant renamings>
8131 -- <private object renamings>
8132 -- begin
8133 -- return <barrier expression>;
8134 -- end entB;
8136 -- procedure pprocN (_object : in out poV;...) is
8137 -- <discriminant renamings>
8138 -- <private object renamings>
8139 -- begin
8140 -- <sequence of statements>
8141 -- end pprocN;
8143 -- procedure pprocP (_object : in out poV;...) is
8144 -- procedure _clean is
8145 -- Pn : Boolean;
8146 -- begin
8147 -- ptypeS (_object, Pn);
8148 -- Unlock (_object._object'Access);
8149 -- Abort_Undefer.all;
8150 -- end _clean;
8152 -- begin
8153 -- Abort_Defer.all;
8154 -- Lock (_object._object'Access);
8155 -- pprocN (_object;...);
8156 -- at end
8157 -- _clean;
8158 -- end pproc;
8160 -- function pfuncN (_object : poV;...) return Return_Type is
8161 -- <discriminant renamings>
8162 -- <private object renamings>
8163 -- begin
8164 -- <sequence of statements>
8165 -- end pfuncN;
8167 -- function pfuncP (_object : poV) return Return_Type is
8168 -- procedure _clean is
8169 -- begin
8170 -- Unlock (_object._object'Access);
8171 -- Abort_Undefer.all;
8172 -- end _clean;
8174 -- begin
8175 -- Abort_Defer.all;
8176 -- Lock (_object._object'Access);
8177 -- return pfuncN (_object);
8179 -- at end
8180 -- _clean;
8181 -- end pfunc;
8183 -- procedure entE
8184 -- (O : System.Address;
8185 -- P : System.Address;
8186 -- E : Protected_Entry_Index)
8187 -- is
8188 -- <discriminant renamings>
8189 -- <private object renamings>
8190 -- type poVP is access poV;
8191 -- _Object : ptVP := ptVP!(O);
8193 -- begin
8194 -- begin
8195 -- <statement sequence>
8196 -- Complete_Entry_Body (_Object._Object);
8197 -- exception
8198 -- when all others =>
8199 -- Exceptional_Complete_Entry_Body (
8200 -- _Object._Object, Get_GNAT_Exception);
8201 -- end;
8202 -- end entE;
8204 -- The type poV is the record created for the protected type to hold
8205 -- the state of the protected object.
8207 procedure Expand_N_Protected_Body (N : Node_Id) is
8208 Loc : constant Source_Ptr := Sloc (N);
8209 Pid : constant Entity_Id := Corresponding_Spec (N);
8211 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8212 -- This flag indicates whether the lock free implementation is active
8214 Current_Node : Node_Id;
8215 Disp_Op_Body : Node_Id;
8216 New_Op_Body : Node_Id;
8217 Num_Entries : Natural := 0;
8218 Op_Body : Node_Id;
8219 Op_Id : Entity_Id;
8221 function Build_Dispatching_Subprogram_Body
8222 (N : Node_Id;
8223 Pid : Node_Id;
8224 Prot_Bod : Node_Id) return Node_Id;
8225 -- Build a dispatching version of the protected subprogram body. The
8226 -- newly generated subprogram contains a call to the original protected
8227 -- body. The following code is generated:
8229 -- function <protected-function-name> (Param1 .. ParamN) return
8230 -- <return-type> is
8231 -- begin
8232 -- return <protected-function-name>P (Param1 .. ParamN);
8233 -- end <protected-function-name>;
8235 -- or
8237 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8238 -- begin
8239 -- <protected-procedure-name>P (Param1 .. ParamN);
8240 -- end <protected-procedure-name>
8242 ---------------------------------------
8243 -- Build_Dispatching_Subprogram_Body --
8244 ---------------------------------------
8246 function Build_Dispatching_Subprogram_Body
8247 (N : Node_Id;
8248 Pid : Node_Id;
8249 Prot_Bod : Node_Id) return Node_Id
8251 Loc : constant Source_Ptr := Sloc (N);
8252 Actuals : List_Id;
8253 Formal : Node_Id;
8254 Spec : Node_Id;
8255 Stmts : List_Id;
8257 begin
8258 -- Generate a specification without a letter suffix in order to
8259 -- override an interface function or procedure.
8261 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8263 -- The formal parameters become the actuals of the protected function
8264 -- or procedure call.
8266 Actuals := New_List;
8267 Formal := First (Parameter_Specifications (Spec));
8268 while Present (Formal) loop
8269 Append_To (Actuals,
8270 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8271 Next (Formal);
8272 end loop;
8274 if Nkind (Spec) = N_Procedure_Specification then
8275 Stmts :=
8276 New_List (
8277 Make_Procedure_Call_Statement (Loc,
8278 Name =>
8279 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
8280 Parameter_Associations => Actuals));
8282 else
8283 pragma Assert (Nkind (Spec) = N_Function_Specification);
8285 Stmts :=
8286 New_List (
8287 Make_Simple_Return_Statement (Loc,
8288 Expression =>
8289 Make_Function_Call (Loc,
8290 Name =>
8291 New_Reference_To (Corresponding_Spec (Prot_Bod), Loc),
8292 Parameter_Associations => Actuals)));
8293 end if;
8295 return
8296 Make_Subprogram_Body (Loc,
8297 Declarations => Empty_List,
8298 Specification => Spec,
8299 Handled_Statement_Sequence =>
8300 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8301 end Build_Dispatching_Subprogram_Body;
8303 -- Start of processing for Expand_N_Protected_Body
8305 begin
8306 if No_Run_Time_Mode then
8307 Error_Msg_CRT ("protected body", N);
8308 return;
8309 end if;
8311 -- This is the proper body corresponding to a stub. The declarations
8312 -- must be inserted at the point of the stub, which in turn is in the
8313 -- declarative part of the parent unit.
8315 if Nkind (Parent (N)) = N_Subunit then
8316 Current_Node := Corresponding_Stub (Parent (N));
8317 else
8318 Current_Node := N;
8319 end if;
8321 Op_Body := First (Declarations (N));
8323 -- The protected body is replaced with the bodies of its
8324 -- protected operations, and the declarations for internal objects
8325 -- that may have been created for entry family bounds.
8327 Rewrite (N, Make_Null_Statement (Sloc (N)));
8328 Analyze (N);
8330 while Present (Op_Body) loop
8331 case Nkind (Op_Body) is
8332 when N_Subprogram_Declaration =>
8333 null;
8335 when N_Subprogram_Body =>
8337 -- Do not create bodies for eliminated operations
8339 if not Is_Eliminated (Defining_Entity (Op_Body))
8340 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8341 then
8342 if Lock_Free_Active then
8343 New_Op_Body :=
8344 Build_Lock_Free_Unprotected_Subprogram_Body
8345 (Op_Body, Pid);
8346 else
8347 New_Op_Body :=
8348 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8349 end if;
8351 Insert_After (Current_Node, New_Op_Body);
8352 Current_Node := New_Op_Body;
8353 Analyze (New_Op_Body);
8355 -- Build the corresponding protected operation. It may
8356 -- appear that this is needed only if this is a visible
8357 -- operation of the type, or if it is an interrupt handler,
8358 -- and this was the strategy used previously in GNAT.
8360 -- However, the operation may be exported through a 'Access
8361 -- to an external caller. This is the common idiom in code
8362 -- that uses the Ada 2005 Timing_Events package. As a result
8363 -- we need to produce the protected body for both visible
8364 -- and private operations, as well as operations that only
8365 -- have a body in the source, and for which we create a
8366 -- declaration in the protected body itself.
8368 if Present (Corresponding_Spec (Op_Body)) then
8369 if Lock_Free_Active then
8370 New_Op_Body :=
8371 Build_Lock_Free_Protected_Subprogram_Body
8372 (Op_Body, Pid, Specification (New_Op_Body));
8373 else
8374 New_Op_Body :=
8375 Build_Protected_Subprogram_Body
8376 (Op_Body, Pid, Specification (New_Op_Body));
8377 end if;
8379 Insert_After (Current_Node, New_Op_Body);
8380 Analyze (New_Op_Body);
8382 Current_Node := New_Op_Body;
8384 -- Generate an overriding primitive operation body for
8385 -- this subprogram if the protected type implements an
8386 -- interface.
8388 if Ada_Version >= Ada_2005
8389 and then
8390 Present (Interfaces (Corresponding_Record_Type (Pid)))
8391 then
8392 Disp_Op_Body :=
8393 Build_Dispatching_Subprogram_Body
8394 (Op_Body, Pid, New_Op_Body);
8396 Insert_After (Current_Node, Disp_Op_Body);
8397 Analyze (Disp_Op_Body);
8399 Current_Node := Disp_Op_Body;
8400 end if;
8401 end if;
8402 end if;
8404 when N_Entry_Body =>
8405 Op_Id := Defining_Identifier (Op_Body);
8406 Num_Entries := Num_Entries + 1;
8408 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8410 Insert_After (Current_Node, New_Op_Body);
8411 Current_Node := New_Op_Body;
8412 Analyze (New_Op_Body);
8414 when N_Implicit_Label_Declaration =>
8415 null;
8417 when N_Itype_Reference =>
8418 Insert_After (Current_Node, New_Copy (Op_Body));
8420 when N_Freeze_Entity =>
8421 New_Op_Body := New_Copy (Op_Body);
8423 if Present (Entity (Op_Body))
8424 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8425 then
8426 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8427 end if;
8429 Insert_After (Current_Node, New_Op_Body);
8430 Current_Node := New_Op_Body;
8431 Analyze (New_Op_Body);
8433 when N_Pragma =>
8434 New_Op_Body := New_Copy (Op_Body);
8435 Insert_After (Current_Node, New_Op_Body);
8436 Current_Node := New_Op_Body;
8437 Analyze (New_Op_Body);
8439 when N_Object_Declaration =>
8440 pragma Assert (not Comes_From_Source (Op_Body));
8441 New_Op_Body := New_Copy (Op_Body);
8442 Insert_After (Current_Node, New_Op_Body);
8443 Current_Node := New_Op_Body;
8444 Analyze (New_Op_Body);
8446 when others =>
8447 raise Program_Error;
8449 end case;
8451 Next (Op_Body);
8452 end loop;
8454 -- Finally, create the body of the function that maps an entry index
8455 -- into the corresponding body index, except when there is no entry, or
8456 -- in a Ravenscar-like profile.
8458 if Corresponding_Runtime_Package (Pid) =
8459 System_Tasking_Protected_Objects_Entries
8460 then
8461 New_Op_Body := Build_Find_Body_Index (Pid);
8462 Insert_After (Current_Node, New_Op_Body);
8463 Current_Node := New_Op_Body;
8464 Analyze (New_Op_Body);
8465 end if;
8467 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8468 -- protected body. At this point all wrapper specs have been created,
8469 -- frozen and included in the dispatch table for the protected type.
8471 if Ada_Version >= Ada_2005 then
8472 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8473 end if;
8474 end Expand_N_Protected_Body;
8476 -----------------------------------------
8477 -- Expand_N_Protected_Type_Declaration --
8478 -----------------------------------------
8480 -- First we create a corresponding record type declaration used to
8481 -- represent values of this protected type.
8482 -- The general form of this type declaration is
8484 -- type poV (discriminants) is record
8485 -- _Object : aliased <kind>Protection
8486 -- [(<entry count> [, <handler count>])];
8487 -- [entry_family : array (bounds) of Void;]
8488 -- <private data fields>
8489 -- end record;
8491 -- The discriminants are present only if the corresponding protected type
8492 -- has discriminants, and they exactly mirror the protected type
8493 -- discriminants. The private data fields similarly mirror the private
8494 -- declarations of the protected type.
8496 -- The Object field is always present. It contains RTS specific data used
8497 -- to control the protected object. It is declared as Aliased so that it
8498 -- can be passed as a pointer to the RTS. This allows the protected record
8499 -- to be referenced within RTS data structures. An appropriate Protection
8500 -- type and discriminant are generated.
8502 -- The Service field is present for protected objects with entries. It
8503 -- contains sufficient information to allow the entry service procedure for
8504 -- this object to be called when the object is not known till runtime.
8506 -- One entry_family component is present for each entry family in the
8507 -- task definition (see Expand_N_Task_Type_Declaration).
8509 -- When a protected object is declared, an instance of the protected type
8510 -- value record is created. The elaboration of this declaration creates the
8511 -- correct bounds for the entry families, and also evaluates the priority
8512 -- expression if needed. The initialization routine for the protected type
8513 -- itself then calls Initialize_Protection with appropriate parameters to
8514 -- initialize the value of the Task_Id field. Install_Handlers may be also
8515 -- called if a pragma Attach_Handler applies.
8517 -- Note: this record is passed to the subprograms created by the expansion
8518 -- of protected subprograms and entries. It is an in parameter to protected
8519 -- functions and an in out parameter to procedures and entry bodies. The
8520 -- Entity_Id for this created record type is placed in the
8521 -- Corresponding_Record_Type field of the associated protected type entity.
8523 -- Next we create a procedure specifications for protected subprograms and
8524 -- entry bodies. For each protected subprograms two subprograms are
8525 -- created, an unprotected and a protected version. The unprotected version
8526 -- is called from within other operations of the same protected object.
8528 -- We also build the call to register the procedure if a pragma
8529 -- Interrupt_Handler applies.
8531 -- A single subprogram is created to service all entry bodies; it has an
8532 -- additional boolean out parameter indicating that the previous entry call
8533 -- made by the current task was serviced immediately, i.e. not by proxy.
8534 -- The O parameter contains a pointer to a record object of the type
8535 -- described above. An untyped interface is used here to allow this
8536 -- procedure to be called in places where the type of the object to be
8537 -- serviced is not known. This must be done, for example, when a call that
8538 -- may have been requeued is cancelled; the corresponding object must be
8539 -- serviced, but which object that is not known till runtime.
8541 -- procedure ptypeS
8542 -- (O : System.Address; P : out Boolean);
8543 -- procedure pprocN (_object : in out poV);
8544 -- procedure pproc (_object : in out poV);
8545 -- function pfuncN (_object : poV);
8546 -- function pfunc (_object : poV);
8547 -- ...
8549 -- Note that this must come after the record type declaration, since
8550 -- the specs refer to this type.
8552 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8553 Loc : constant Source_Ptr := Sloc (N);
8554 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8556 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8557 -- This flag indicates whether the lock free implementation is active
8559 Pdef : constant Node_Id := Protected_Definition (N);
8560 -- This contains two lists; one for visible and one for private decls
8562 Rec_Decl : Node_Id;
8563 Cdecls : List_Id;
8564 Discr_Map : constant Elist_Id := New_Elmt_List;
8565 Priv : Node_Id;
8566 New_Priv : Node_Id;
8567 Comp : Node_Id;
8568 Comp_Id : Entity_Id;
8569 Sub : Node_Id;
8570 Current_Node : Node_Id := N;
8571 Bdef : Entity_Id := Empty; -- avoid uninit warning
8572 Edef : Entity_Id := Empty; -- avoid uninit warning
8573 Entries_Aggr : Node_Id;
8574 Body_Id : Entity_Id;
8575 Body_Arr : Node_Id;
8576 E_Count : Int;
8577 Object_Comp : Node_Id;
8579 procedure Check_Inlining (Subp : Entity_Id);
8580 -- If the original operation has a pragma Inline, propagate the flag
8581 -- to the internal body, for possible inlining later on. The source
8582 -- operation is invisible to the back-end and is never actually called.
8584 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8585 -- When compiling under the Ravenscar profile, private components must
8586 -- have a static size, or else a protected object will require heap
8587 -- allocation, violating the corresponding restriction. It is preferable
8588 -- to make this check here, because it provides a better error message
8589 -- than the back-end, which refers to the object as a whole.
8591 procedure Register_Handler;
8592 -- For a protected operation that is an interrupt handler, add the
8593 -- freeze action that will register it as such.
8595 --------------------
8596 -- Check_Inlining --
8597 --------------------
8599 procedure Check_Inlining (Subp : Entity_Id) is
8600 begin
8601 if Is_Inlined (Subp) then
8602 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8603 Set_Is_Inlined (Subp, False);
8604 end if;
8605 end Check_Inlining;
8607 ---------------------------------
8608 -- Check_Static_Component_Size --
8609 ---------------------------------
8611 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8612 Typ : constant Entity_Id := Etype (Comp);
8613 C : Entity_Id;
8615 begin
8616 if Is_Scalar_Type (Typ) then
8617 return True;
8619 elsif Is_Array_Type (Typ) then
8620 return Compile_Time_Known_Bounds (Typ);
8622 elsif Is_Record_Type (Typ) then
8623 C := First_Component (Typ);
8624 while Present (C) loop
8625 if not Static_Component_Size (C) then
8626 return False;
8627 end if;
8629 Next_Component (C);
8630 end loop;
8632 return True;
8634 -- Any other type will be checked by the back-end
8636 else
8637 return True;
8638 end if;
8639 end Static_Component_Size;
8641 ----------------------
8642 -- Register_Handler --
8643 ----------------------
8645 procedure Register_Handler is
8647 -- All semantic checks already done in Sem_Prag
8649 Prot_Proc : constant Entity_Id :=
8650 Defining_Unit_Name (Specification (Current_Node));
8652 Proc_Address : constant Node_Id :=
8653 Make_Attribute_Reference (Loc,
8654 Prefix =>
8655 New_Reference_To (Prot_Proc, Loc),
8656 Attribute_Name => Name_Address);
8658 RTS_Call : constant Entity_Id :=
8659 Make_Procedure_Call_Statement (Loc,
8660 Name =>
8661 New_Reference_To
8662 (RTE (RE_Register_Interrupt_Handler), Loc),
8663 Parameter_Associations => New_List (Proc_Address));
8664 begin
8665 Append_Freeze_Action (Prot_Proc, RTS_Call);
8666 end Register_Handler;
8668 -- Start of processing for Expand_N_Protected_Type_Declaration
8670 begin
8671 if Present (Corresponding_Record_Type (Prot_Typ)) then
8672 return;
8673 else
8674 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8675 end if;
8677 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8679 Qualify_Entity_Names (N);
8681 -- If the type has discriminants, their occurrences in the declaration
8682 -- have been replaced by the corresponding discriminals. For components
8683 -- that are constrained by discriminants, their homologues in the
8684 -- corresponding record type must refer to the discriminants of that
8685 -- record, so we must apply a new renaming to subtypes_indications:
8687 -- protected discriminant => discriminal => record discriminant
8689 -- This replacement is not applied to default expressions, for which
8690 -- the discriminal is correct.
8692 if Has_Discriminants (Prot_Typ) then
8693 declare
8694 Disc : Entity_Id;
8695 Decl : Node_Id;
8697 begin
8698 Disc := First_Discriminant (Prot_Typ);
8699 Decl := First (Discriminant_Specifications (Rec_Decl));
8700 while Present (Disc) loop
8701 Append_Elmt (Discriminal (Disc), Discr_Map);
8702 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
8703 Next_Discriminant (Disc);
8704 Next (Decl);
8705 end loop;
8706 end;
8707 end if;
8709 -- Fill in the component declarations
8711 -- Add components for entry families. For each entry family, create an
8712 -- anonymous type declaration with the same size, and analyze the type.
8714 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
8716 pragma Assert (Present (Pdef));
8718 -- Add private field components
8720 if Present (Private_Declarations (Pdef)) then
8721 Priv := First (Private_Declarations (Pdef));
8723 while Present (Priv) loop
8725 if Nkind (Priv) = N_Component_Declaration then
8726 if not Static_Component_Size (Defining_Identifier (Priv)) then
8728 -- When compiling for a restricted profile, the private
8729 -- components must have a static size. If not, this is an
8730 -- error for a single protected declaration, and rates a
8731 -- warning on a protected type declaration.
8733 if not Comes_From_Source (Prot_Typ) then
8734 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
8736 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
8737 Error_Msg_N ("component has non-static size?", Priv);
8738 Error_Msg_NE
8739 ("\creation of protected object of type& will violate"
8740 & " restriction No_Implicit_Heap_Allocations?",
8741 Priv, Prot_Typ);
8742 end if;
8743 end if;
8745 -- The component definition consists of a subtype indication,
8746 -- or (in Ada 2005) an access definition. Make a copy of the
8747 -- proper definition.
8749 declare
8750 Old_Comp : constant Node_Id := Component_Definition (Priv);
8751 Oent : constant Entity_Id := Defining_Identifier (Priv);
8752 New_Comp : Node_Id;
8753 Nent : constant Entity_Id :=
8754 Make_Defining_Identifier (Sloc (Oent),
8755 Chars => Chars (Oent));
8757 begin
8758 if Present (Subtype_Indication (Old_Comp)) then
8759 New_Comp :=
8760 Make_Component_Definition (Sloc (Oent),
8761 Aliased_Present => False,
8762 Subtype_Indication =>
8763 New_Copy_Tree (Subtype_Indication (Old_Comp),
8764 Discr_Map));
8765 else
8766 New_Comp :=
8767 Make_Component_Definition (Sloc (Oent),
8768 Aliased_Present => False,
8769 Access_Definition =>
8770 New_Copy_Tree (Access_Definition (Old_Comp),
8771 Discr_Map));
8772 end if;
8774 New_Priv :=
8775 Make_Component_Declaration (Loc,
8776 Defining_Identifier => Nent,
8777 Component_Definition => New_Comp,
8778 Expression => Expression (Priv));
8780 Set_Has_Per_Object_Constraint (Nent,
8781 Has_Per_Object_Constraint (Oent));
8783 Append_To (Cdecls, New_Priv);
8784 end;
8786 elsif Nkind (Priv) = N_Subprogram_Declaration then
8788 -- Make the unprotected version of the subprogram available
8789 -- for expansion of intra object calls. There is need for
8790 -- a protected version only if the subprogram is an interrupt
8791 -- handler, otherwise this operation can only be called from
8792 -- within the body.
8794 Sub :=
8795 Make_Subprogram_Declaration (Loc,
8796 Specification =>
8797 Build_Protected_Sub_Specification
8798 (Priv, Prot_Typ, Unprotected_Mode));
8800 Insert_After (Current_Node, Sub);
8801 Analyze (Sub);
8803 Set_Protected_Body_Subprogram
8804 (Defining_Unit_Name (Specification (Priv)),
8805 Defining_Unit_Name (Specification (Sub)));
8806 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
8807 Current_Node := Sub;
8809 Sub :=
8810 Make_Subprogram_Declaration (Loc,
8811 Specification =>
8812 Build_Protected_Sub_Specification
8813 (Priv, Prot_Typ, Protected_Mode));
8815 Insert_After (Current_Node, Sub);
8816 Analyze (Sub);
8817 Current_Node := Sub;
8819 if Is_Interrupt_Handler
8820 (Defining_Unit_Name (Specification (Priv)))
8821 then
8822 if not Restricted_Profile then
8823 Register_Handler;
8824 end if;
8825 end if;
8826 end if;
8828 Next (Priv);
8829 end loop;
8830 end if;
8832 -- Except for the lock-free implementation, prepend the _Object field
8833 -- with the right type to the component list. We need to compute the
8834 -- number of entries, and in some cases the number of Attach_Handler
8835 -- pragmas.
8837 if not Lock_Free_Active then
8838 declare
8839 Ritem : Node_Id;
8840 Num_Attach_Handler : Int := 0;
8841 Protection_Subtype : Node_Id;
8842 Entry_Count_Expr : constant Node_Id :=
8843 Build_Entry_Count_Expression
8844 (Prot_Typ, Cdecls, Loc);
8846 begin
8847 -- Could this be simplified using Corresponding_Runtime_Package???
8849 if Has_Attach_Handler (Prot_Typ) then
8850 Ritem := First_Rep_Item (Prot_Typ);
8851 while Present (Ritem) loop
8852 if Nkind (Ritem) = N_Pragma
8853 and then Pragma_Name (Ritem) = Name_Attach_Handler
8854 then
8855 Num_Attach_Handler := Num_Attach_Handler + 1;
8856 end if;
8858 Next_Rep_Item (Ritem);
8859 end loop;
8861 if Restricted_Profile then
8862 if Has_Entries (Prot_Typ) then
8863 Protection_Subtype :=
8864 New_Reference_To (RTE (RE_Protection_Entry), Loc);
8865 else
8866 Protection_Subtype :=
8867 New_Reference_To (RTE (RE_Protection), Loc);
8868 end if;
8870 else
8871 Protection_Subtype :=
8872 Make_Subtype_Indication (Loc,
8873 Subtype_Mark =>
8874 New_Reference_To
8875 (RTE (RE_Static_Interrupt_Protection), Loc),
8876 Constraint =>
8877 Make_Index_Or_Discriminant_Constraint (Loc,
8878 Constraints => New_List (
8879 Entry_Count_Expr,
8880 Make_Integer_Literal (Loc, Num_Attach_Handler))));
8881 end if;
8883 elsif Has_Interrupt_Handler (Prot_Typ)
8884 and then not Restriction_Active (No_Dynamic_Attachment)
8885 then
8886 Protection_Subtype :=
8887 Make_Subtype_Indication (Loc,
8888 Subtype_Mark =>
8889 New_Reference_To
8890 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
8891 Constraint =>
8892 Make_Index_Or_Discriminant_Constraint (Loc,
8893 Constraints => New_List (Entry_Count_Expr)));
8895 -- Type has explicit entries or generated primitive entry wrappers
8897 elsif Has_Entries (Prot_Typ)
8898 or else (Ada_Version >= Ada_2005
8899 and then Present (Interface_List (N)))
8900 then
8901 case Corresponding_Runtime_Package (Prot_Typ) is
8902 when System_Tasking_Protected_Objects_Entries =>
8903 Protection_Subtype :=
8904 Make_Subtype_Indication (Loc,
8905 Subtype_Mark =>
8906 New_Reference_To
8907 (RTE (RE_Protection_Entries), Loc),
8908 Constraint =>
8909 Make_Index_Or_Discriminant_Constraint (Loc,
8910 Constraints => New_List (Entry_Count_Expr)));
8912 when System_Tasking_Protected_Objects_Single_Entry =>
8913 Protection_Subtype :=
8914 New_Reference_To (RTE (RE_Protection_Entry), Loc);
8916 when others =>
8917 raise Program_Error;
8918 end case;
8920 else
8921 Protection_Subtype :=
8922 New_Reference_To (RTE (RE_Protection), Loc);
8923 end if;
8925 Object_Comp :=
8926 Make_Component_Declaration (Loc,
8927 Defining_Identifier =>
8928 Make_Defining_Identifier (Loc, Name_uObject),
8929 Component_Definition =>
8930 Make_Component_Definition (Loc,
8931 Aliased_Present => True,
8932 Subtype_Indication => Protection_Subtype));
8933 end;
8935 -- Put the _Object component after the private component so that it
8936 -- be finalized early as required by 9.4 (20)
8938 Append_To (Cdecls, Object_Comp);
8939 end if;
8941 Insert_After (Current_Node, Rec_Decl);
8942 Current_Node := Rec_Decl;
8944 -- Analyze the record declaration immediately after construction,
8945 -- because the initialization procedure is needed for single object
8946 -- declarations before the next entity is analyzed (the freeze call
8947 -- that generates this initialization procedure is found below).
8949 Analyze (Rec_Decl, Suppress => All_Checks);
8951 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
8952 -- the corresponding record is frozen. If any wrappers are generated,
8953 -- Current_Node is updated accordingly.
8955 if Ada_Version >= Ada_2005 then
8956 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
8957 end if;
8959 -- Collect pointers to entry bodies and their barriers, to be placed
8960 -- in the Entry_Bodies_Array for the type. For each entry/family we
8961 -- add an expression to the aggregate which is the initial value of
8962 -- this array. The array is declared after all protected subprograms.
8964 if Has_Entries (Prot_Typ) then
8965 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
8966 else
8967 Entries_Aggr := Empty;
8968 end if;
8970 -- Build two new procedure specifications for each protected subprogram;
8971 -- one to call from outside the object and one to call from inside.
8972 -- Build a barrier function and an entry body action procedure
8973 -- specification for each protected entry. Initialize the entry body
8974 -- array. If subprogram is flagged as eliminated, do not generate any
8975 -- internal operations.
8977 E_Count := 0;
8978 Comp := First (Visible_Declarations (Pdef));
8979 while Present (Comp) loop
8980 if Nkind (Comp) = N_Subprogram_Declaration then
8981 Sub :=
8982 Make_Subprogram_Declaration (Loc,
8983 Specification =>
8984 Build_Protected_Sub_Specification
8985 (Comp, Prot_Typ, Unprotected_Mode));
8987 Insert_After (Current_Node, Sub);
8988 Analyze (Sub);
8990 Set_Protected_Body_Subprogram
8991 (Defining_Unit_Name (Specification (Comp)),
8992 Defining_Unit_Name (Specification (Sub)));
8993 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
8995 -- Make the protected version of the subprogram available for
8996 -- expansion of external calls.
8998 Current_Node := Sub;
9000 Sub :=
9001 Make_Subprogram_Declaration (Loc,
9002 Specification =>
9003 Build_Protected_Sub_Specification
9004 (Comp, Prot_Typ, Protected_Mode));
9006 Insert_After (Current_Node, Sub);
9007 Analyze (Sub);
9009 Current_Node := Sub;
9011 -- Generate an overriding primitive operation specification for
9012 -- this subprogram if the protected type implements an interface.
9014 if Ada_Version >= Ada_2005
9015 and then
9016 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9017 then
9018 Sub :=
9019 Make_Subprogram_Declaration (Loc,
9020 Specification =>
9021 Build_Protected_Sub_Specification
9022 (Comp, Prot_Typ, Dispatching_Mode));
9024 Insert_After (Current_Node, Sub);
9025 Analyze (Sub);
9027 Current_Node := Sub;
9028 end if;
9030 -- If a pragma Interrupt_Handler applies, build and add a call to
9031 -- Register_Interrupt_Handler to the freezing actions of the
9032 -- protected version (Current_Node) of the subprogram:
9034 -- system.interrupts.register_interrupt_handler
9035 -- (prot_procP'address);
9037 if not Restricted_Profile
9038 and then Is_Interrupt_Handler
9039 (Defining_Unit_Name (Specification (Comp)))
9040 then
9041 Register_Handler;
9042 end if;
9044 elsif Nkind (Comp) = N_Entry_Declaration then
9045 E_Count := E_Count + 1;
9046 Comp_Id := Defining_Identifier (Comp);
9048 Edef :=
9049 Make_Defining_Identifier (Loc,
9050 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9051 Sub :=
9052 Make_Subprogram_Declaration (Loc,
9053 Specification =>
9054 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
9056 Insert_After (Current_Node, Sub);
9057 Analyze (Sub);
9059 -- Build wrapper procedure for pre/postconditions
9061 Build_PPC_Wrapper (Comp_Id, N);
9063 Set_Protected_Body_Subprogram
9064 (Defining_Identifier (Comp),
9065 Defining_Unit_Name (Specification (Sub)));
9067 Current_Node := Sub;
9069 Bdef :=
9070 Make_Defining_Identifier (Loc,
9071 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'B'));
9072 Sub :=
9073 Make_Subprogram_Declaration (Loc,
9074 Specification =>
9075 Build_Barrier_Function_Specification (Loc, Bdef));
9077 Insert_After (Current_Node, Sub);
9078 Analyze (Sub);
9079 Set_Protected_Body_Subprogram (Bdef, Bdef);
9080 Set_Barrier_Function (Comp_Id, Bdef);
9081 Set_Scope (Bdef, Scope (Comp_Id));
9082 Current_Node := Sub;
9084 -- Collect pointers to the protected subprogram and the barrier
9085 -- of the current entry, for insertion into Entry_Bodies_Array.
9087 Append_To (Expressions (Entries_Aggr),
9088 Make_Aggregate (Loc,
9089 Expressions => New_List (
9090 Make_Attribute_Reference (Loc,
9091 Prefix => New_Reference_To (Bdef, Loc),
9092 Attribute_Name => Name_Unrestricted_Access),
9093 Make_Attribute_Reference (Loc,
9094 Prefix => New_Reference_To (Edef, Loc),
9095 Attribute_Name => Name_Unrestricted_Access))));
9096 end if;
9098 Next (Comp);
9099 end loop;
9101 -- If there are some private entry declarations, expand it as if they
9102 -- were visible entries.
9104 if Present (Private_Declarations (Pdef)) then
9105 Comp := First (Private_Declarations (Pdef));
9106 while Present (Comp) loop
9107 if Nkind (Comp) = N_Entry_Declaration then
9108 E_Count := E_Count + 1;
9109 Comp_Id := Defining_Identifier (Comp);
9111 Edef :=
9112 Make_Defining_Identifier (Loc,
9113 Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9114 Sub :=
9115 Make_Subprogram_Declaration (Loc,
9116 Specification =>
9117 Build_Protected_Entry_Specification (Loc, Edef, Comp_Id));
9119 Insert_After (Current_Node, Sub);
9120 Analyze (Sub);
9122 Set_Protected_Body_Subprogram
9123 (Defining_Identifier (Comp),
9124 Defining_Unit_Name (Specification (Sub)));
9126 Current_Node := Sub;
9128 Bdef :=
9129 Make_Defining_Identifier (Loc,
9130 Chars => Build_Selected_Name (Prot_Typ, Comp_Id, 'E'));
9132 Sub :=
9133 Make_Subprogram_Declaration (Loc,
9134 Specification =>
9135 Build_Barrier_Function_Specification (Loc, Bdef));
9137 Insert_After (Current_Node, Sub);
9138 Analyze (Sub);
9139 Set_Protected_Body_Subprogram (Bdef, Bdef);
9140 Set_Barrier_Function (Comp_Id, Bdef);
9141 Set_Scope (Bdef, Scope (Comp_Id));
9142 Current_Node := Sub;
9144 -- Collect pointers to the protected subprogram and the barrier
9145 -- of the current entry, for insertion into Entry_Bodies_Array.
9147 Append_To (Expressions (Entries_Aggr),
9148 Make_Aggregate (Loc,
9149 Expressions => New_List (
9150 Make_Attribute_Reference (Loc,
9151 Prefix => New_Reference_To (Bdef, Loc),
9152 Attribute_Name => Name_Unrestricted_Access),
9153 Make_Attribute_Reference (Loc,
9154 Prefix => New_Reference_To (Edef, Loc),
9155 Attribute_Name => Name_Unrestricted_Access))));
9156 end if;
9158 Next (Comp);
9159 end loop;
9160 end if;
9162 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9163 -- all protected subprograms have been collected.
9165 if Has_Entries (Prot_Typ) then
9166 Body_Id :=
9167 Make_Defining_Identifier (Sloc (Prot_Typ),
9168 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9170 case Corresponding_Runtime_Package (Prot_Typ) is
9171 when System_Tasking_Protected_Objects_Entries =>
9172 Body_Arr := Make_Object_Declaration (Loc,
9173 Defining_Identifier => Body_Id,
9174 Aliased_Present => True,
9175 Object_Definition =>
9176 Make_Subtype_Indication (Loc,
9177 Subtype_Mark => New_Reference_To (
9178 RTE (RE_Protected_Entry_Body_Array), Loc),
9179 Constraint =>
9180 Make_Index_Or_Discriminant_Constraint (Loc,
9181 Constraints => New_List (
9182 Make_Range (Loc,
9183 Make_Integer_Literal (Loc, 1),
9184 Make_Integer_Literal (Loc, E_Count))))),
9185 Expression => Entries_Aggr);
9187 when System_Tasking_Protected_Objects_Single_Entry =>
9188 Body_Arr := Make_Object_Declaration (Loc,
9189 Defining_Identifier => Body_Id,
9190 Aliased_Present => True,
9191 Object_Definition => New_Reference_To
9192 (RTE (RE_Entry_Body), Loc),
9193 Expression =>
9194 Make_Aggregate (Loc,
9195 Expressions => New_List (
9196 Make_Attribute_Reference (Loc,
9197 Prefix => New_Reference_To (Bdef, Loc),
9198 Attribute_Name => Name_Unrestricted_Access),
9199 Make_Attribute_Reference (Loc,
9200 Prefix => New_Reference_To (Edef, Loc),
9201 Attribute_Name => Name_Unrestricted_Access))));
9203 when others =>
9204 raise Program_Error;
9205 end case;
9207 -- A pointer to this array will be placed in the corresponding record
9208 -- by its initialization procedure so this needs to be analyzed here.
9210 Insert_After (Current_Node, Body_Arr);
9211 Current_Node := Body_Arr;
9212 Analyze (Body_Arr);
9214 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9216 -- Finally, build the function that maps an entry index into the
9217 -- corresponding body. A pointer to this function is placed in each
9218 -- object of the type. Except for a ravenscar-like profile (no abort,
9219 -- no entry queue, 1 entry)
9221 if Corresponding_Runtime_Package (Prot_Typ) =
9222 System_Tasking_Protected_Objects_Entries
9223 then
9224 Sub :=
9225 Make_Subprogram_Declaration (Loc,
9226 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9227 Insert_After (Current_Node, Sub);
9228 Analyze (Sub);
9229 end if;
9230 end if;
9231 end Expand_N_Protected_Type_Declaration;
9233 --------------------------------
9234 -- Expand_N_Requeue_Statement --
9235 --------------------------------
9237 -- A non-dispatching requeue statement is expanded into one of four GNARLI
9238 -- operations, depending on the source and destination (task or protected
9239 -- object). A dispatching requeue statement is expanded into a call to the
9240 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9241 -- jump around the remainder of processing for the original entry and, if
9242 -- the destination is (different) protected object, to attempt to service
9243 -- it. The following illustrates the various cases:
9245 -- procedure entE
9246 -- (O : System.Address;
9247 -- P : System.Address;
9248 -- E : Protected_Entry_Index)
9249 -- is
9250 -- <discriminant renamings>
9251 -- <private object renamings>
9252 -- type poVP is access poV;
9253 -- _object : ptVP := ptVP!(O);
9255 -- begin
9256 -- begin
9257 -- <start of statement sequence for entry>
9259 -- -- Requeue from one protected entry body to another protected
9260 -- -- entry.
9262 -- Requeue_Protected_Entry (
9263 -- _object._object'Access,
9264 -- new._object'Access,
9265 -- E,
9266 -- Abort_Present);
9267 -- return;
9269 -- <some more of the statement sequence for entry>
9271 -- -- Requeue from an entry body to a task entry
9273 -- Requeue_Protected_To_Task_Entry (
9274 -- New._task_id,
9275 -- E,
9276 -- Abort_Present);
9277 -- return;
9279 -- <rest of statement sequence for entry>
9280 -- Complete_Entry_Body (_object._object);
9282 -- exception
9283 -- when all others =>
9284 -- Exceptional_Complete_Entry_Body (
9285 -- _object._object, Get_GNAT_Exception);
9286 -- end;
9287 -- end entE;
9289 -- Requeue of a task entry call to a task entry
9291 -- Accept_Call (E, Ann);
9292 -- <start of statement sequence for accept statement>
9293 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9294 -- goto Lnn;
9295 -- <rest of statement sequence for accept statement>
9296 -- <<Lnn>>
9297 -- Complete_Rendezvous;
9299 -- exception
9300 -- when all others =>
9301 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9303 -- Requeue of a task entry call to a protected entry
9305 -- Accept_Call (E, Ann);
9306 -- <start of statement sequence for accept statement>
9307 -- Requeue_Task_To_Protected_Entry (
9308 -- new._object'Access,
9309 -- E,
9310 -- Abort_Present);
9311 -- newS (new, Pnn);
9312 -- goto Lnn;
9313 -- <rest of statement sequence for accept statement>
9314 -- <<Lnn>>
9315 -- Complete_Rendezvous;
9317 -- exception
9318 -- when all others =>
9319 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9321 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9322 -- marked by pragma Implemented (XXX, By_Entry).
9324 -- The requeue is inside a protected entry:
9326 -- procedure entE
9327 -- (O : System.Address;
9328 -- P : System.Address;
9329 -- E : Protected_Entry_Index)
9330 -- is
9331 -- <discriminant renamings>
9332 -- <private object renamings>
9333 -- type poVP is access poV;
9334 -- _object : ptVP := ptVP!(O);
9336 -- begin
9337 -- begin
9338 -- <start of statement sequence for entry>
9340 -- _Disp_Requeue
9341 -- (<interface class-wide object>,
9342 -- True,
9343 -- _object'Address,
9344 -- Ada.Tags.Get_Offset_Index
9345 -- (Tag (_object),
9346 -- <interface dispatch table index of target entry>),
9347 -- Abort_Present);
9348 -- return;
9350 -- <rest of statement sequence for entry>
9351 -- Complete_Entry_Body (_object._object);
9353 -- exception
9354 -- when all others =>
9355 -- Exceptional_Complete_Entry_Body (
9356 -- _object._object, Get_GNAT_Exception);
9357 -- end;
9358 -- end entE;
9360 -- The requeue is inside a task entry:
9362 -- Accept_Call (E, Ann);
9363 -- <start of statement sequence for accept statement>
9364 -- _Disp_Requeue
9365 -- (<interface class-wide object>,
9366 -- False,
9367 -- null,
9368 -- Ada.Tags.Get_Offset_Index
9369 -- (Tag (_object),
9370 -- <interface dispatch table index of target entrt>),
9371 -- Abort_Present);
9372 -- newS (new, Pnn);
9373 -- goto Lnn;
9374 -- <rest of statement sequence for accept statement>
9375 -- <<Lnn>>
9376 -- Complete_Rendezvous;
9378 -- exception
9379 -- when all others =>
9380 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9382 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9383 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9384 -- statement is replaced by a dispatching call with actual parameters taken
9385 -- from the inner-most accept statement or entry body.
9387 -- Target.Primitive (Param1, ..., ParamN);
9389 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9390 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9391 -- at all.
9393 -- declare
9394 -- S : constant Offset_Index :=
9395 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9396 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9398 -- begin
9399 -- if C = POK_Protected_Entry
9400 -- or else C = POK_Task_Entry
9401 -- then
9402 -- <statements for dispatching requeue>
9404 -- elsif C = POK_Protected_Procedure then
9405 -- <dispatching call equivalent>
9407 -- else
9408 -- raise Program_Error;
9409 -- end if;
9410 -- end;
9412 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9413 Loc : constant Source_Ptr := Sloc (N);
9414 Conc_Typ : Entity_Id;
9415 Concval : Node_Id;
9416 Ename : Node_Id;
9417 Index : Node_Id;
9418 Old_Typ : Entity_Id;
9420 function Build_Dispatching_Call_Equivalent return Node_Id;
9421 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9422 -- the form Concval.Ename. It is statically known that Ename is allowed
9423 -- to be implemented by a protected procedure. Create a dispatching call
9424 -- equivalent of Concval.Ename taking the actual parameters from the
9425 -- inner-most accept statement or entry body.
9427 function Build_Dispatching_Requeue return Node_Id;
9428 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9429 -- the form Concval.Ename. It is statically known that Ename is allowed
9430 -- to be implemented by a protected or a task entry. Create a call to
9431 -- primitive _Disp_Requeue which handles the low-level actions.
9433 function Build_Dispatching_Requeue_To_Any return Node_Id;
9434 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9435 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9436 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9437 -- determines at runtime whether Ename denotes an entry or a procedure
9438 -- and perform the appropriate kind of dispatching select.
9440 function Build_Normal_Requeue return Node_Id;
9441 -- N denotes a non-dispatching requeue statement to either a task or a
9442 -- protected entry. Build the appropriate runtime call to perform the
9443 -- action.
9445 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9446 -- For a protected entry, create a return statement to skip the rest of
9447 -- the entry body. Otherwise, create a goto statement to skip the rest
9448 -- of a task accept statement. The lookup for the enclosing entry body
9449 -- or accept statement starts from Search.
9451 ---------------------------------------
9452 -- Build_Dispatching_Call_Equivalent --
9453 ---------------------------------------
9455 function Build_Dispatching_Call_Equivalent return Node_Id is
9456 Call_Ent : constant Entity_Id := Entity (Ename);
9457 Obj : constant Node_Id := Original_Node (Concval);
9458 Acc_Ent : Node_Id;
9459 Actuals : List_Id;
9460 Formal : Node_Id;
9461 Formals : List_Id;
9463 begin
9464 -- Climb the parent chain looking for the inner-most entry body or
9465 -- accept statement.
9467 Acc_Ent := N;
9468 while Present (Acc_Ent)
9469 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
9470 N_Entry_Body)
9471 loop
9472 Acc_Ent := Parent (Acc_Ent);
9473 end loop;
9475 -- A requeue statement should be housed inside an entry body or an
9476 -- accept statement at some level. If this is not the case, then the
9477 -- tree is malformed.
9479 pragma Assert (Present (Acc_Ent));
9481 -- Recover the list of formal parameters
9483 if Nkind (Acc_Ent) = N_Entry_Body then
9484 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9485 end if;
9487 Formals := Parameter_Specifications (Acc_Ent);
9489 -- Create the actual parameters for the dispatching call. These are
9490 -- simply copies of the entry body or accept statement formals in the
9491 -- same order as they appear.
9493 Actuals := No_List;
9495 if Present (Formals) then
9496 Actuals := New_List;
9497 Formal := First (Formals);
9498 while Present (Formal) loop
9499 Append_To (Actuals,
9500 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9501 Next (Formal);
9502 end loop;
9503 end if;
9505 -- Generate:
9506 -- Obj.Call_Ent (Actuals);
9508 return
9509 Make_Procedure_Call_Statement (Loc,
9510 Name =>
9511 Make_Selected_Component (Loc,
9512 Prefix => Make_Identifier (Loc, Chars (Obj)),
9513 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9515 Parameter_Associations => Actuals);
9516 end Build_Dispatching_Call_Equivalent;
9518 -------------------------------
9519 -- Build_Dispatching_Requeue --
9520 -------------------------------
9522 function Build_Dispatching_Requeue return Node_Id is
9523 Params : constant List_Id := New_List;
9525 begin
9526 -- Process the "with abort" parameter
9528 Prepend_To (Params,
9529 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9531 -- Process the entry wrapper's position in the primary dispatch
9532 -- table parameter. Generate:
9534 -- Ada.Tags.Get_Entry_Index
9535 -- (T => To_Tag_Ptr (Obj'Address).all,
9536 -- Position =>
9537 -- Ada.Tags.Get_Offset_Index
9538 -- (Ada.Tags.Tag (Concval),
9539 -- <interface dispatch table position of Ename>));
9541 -- Note that Obj'Address is recursively expanded into a call to
9542 -- Base_Address (Obj).
9544 if Tagged_Type_Expansion then
9545 Prepend_To (Params,
9546 Make_Function_Call (Loc,
9547 Name => New_Reference_To (RTE (RE_Get_Entry_Index), Loc),
9548 Parameter_Associations => New_List (
9550 Make_Explicit_Dereference (Loc,
9551 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9552 Make_Attribute_Reference (Loc,
9553 Prefix => New_Copy_Tree (Concval),
9554 Attribute_Name => Name_Address))),
9556 Make_Function_Call (Loc,
9557 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9558 Parameter_Associations => New_List (
9559 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9560 Make_Integer_Literal (Loc,
9561 DT_Position (Entity (Ename))))))));
9563 -- VM targets
9565 else
9566 Prepend_To (Params,
9567 Make_Function_Call (Loc,
9568 Name => New_Reference_To (RTE (RE_Get_Entry_Index), Loc),
9569 Parameter_Associations => New_List (
9571 Make_Attribute_Reference (Loc,
9572 Prefix => Concval,
9573 Attribute_Name => Name_Tag),
9575 Make_Function_Call (Loc,
9576 Name => New_Reference_To (RTE (RE_Get_Offset_Index), Loc),
9578 Parameter_Associations => New_List (
9580 -- Obj_Tag
9582 Make_Attribute_Reference (Loc,
9583 Prefix => Concval,
9584 Attribute_Name => Name_Tag),
9586 -- Tag_Typ
9588 Make_Attribute_Reference (Loc,
9589 Prefix => New_Reference_To (Etype (Concval), Loc),
9590 Attribute_Name => Name_Tag),
9592 -- Position
9594 Make_Integer_Literal (Loc,
9595 DT_Position (Entity (Ename))))))));
9596 end if;
9598 -- Specific actuals for protected to XXX requeue
9600 if Is_Protected_Type (Old_Typ) then
9601 Prepend_To (Params,
9602 Make_Attribute_Reference (Loc, -- _object'Address
9603 Prefix =>
9604 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9605 Attribute_Name => Name_Address));
9607 Prepend_To (Params, -- True
9608 New_Reference_To (Standard_True, Loc));
9610 -- Specific actuals for task to XXX requeue
9612 else
9613 pragma Assert (Is_Task_Type (Old_Typ));
9615 Prepend_To (Params, -- null
9616 New_Reference_To (RTE (RE_Null_Address), Loc));
9618 Prepend_To (Params, -- False
9619 New_Reference_To (Standard_False, Loc));
9620 end if;
9622 -- Add the object parameter
9624 Prepend_To (Params, New_Copy_Tree (Concval));
9626 -- Generate:
9627 -- _Disp_Requeue (<Params>);
9629 -- Find entity for Disp_Requeue operation, which belongs to
9630 -- the type and may not be directly visible.
9632 declare
9633 Elmt : Elmt_Id;
9634 Op : Entity_Id;
9636 begin
9637 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
9638 while Present (Elmt) loop
9639 Op := Node (Elmt);
9640 exit when Chars (Op) = Name_uDisp_Requeue;
9641 Next_Elmt (Elmt);
9642 end loop;
9644 return
9645 Make_Procedure_Call_Statement (Loc,
9646 Name => New_Occurrence_Of (Op, Loc),
9647 Parameter_Associations => Params);
9648 end;
9649 end Build_Dispatching_Requeue;
9651 --------------------------------------
9652 -- Build_Dispatching_Requeue_To_Any --
9653 --------------------------------------
9655 function Build_Dispatching_Requeue_To_Any return Node_Id is
9656 Call_Ent : constant Entity_Id := Entity (Ename);
9657 Obj : constant Node_Id := Original_Node (Concval);
9658 Skip : constant Node_Id := Build_Skip_Statement (N);
9659 C : Entity_Id;
9660 Decls : List_Id;
9661 S : Entity_Id;
9662 Stmts : List_Id;
9664 begin
9665 Decls := New_List;
9666 Stmts := New_List;
9668 -- Dispatch table slot processing, generate:
9669 -- S : Integer;
9671 S := Build_S (Loc, Decls);
9673 -- Call kind processing, generate:
9674 -- C : Ada.Tags.Prim_Op_Kind;
9676 C := Build_C (Loc, Decls);
9678 -- Generate:
9679 -- S := Ada.Tags.Get_Offset_Index
9680 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
9682 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
9684 -- Generate:
9685 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
9687 Append_To (Stmts,
9688 Make_Procedure_Call_Statement (Loc,
9689 Name =>
9690 New_Reference_To (
9691 Find_Prim_Op (Etype (Etype (Obj)),
9692 Name_uDisp_Get_Prim_Op_Kind),
9693 Loc),
9694 Parameter_Associations => New_List (
9695 New_Copy_Tree (Obj),
9696 New_Reference_To (S, Loc),
9697 New_Reference_To (C, Loc))));
9699 Append_To (Stmts,
9701 -- if C = POK_Protected_Entry
9702 -- or else C = POK_Task_Entry
9703 -- then
9705 Make_If_Statement (Loc,
9706 Condition =>
9707 Make_Op_Or (Loc,
9708 Left_Opnd =>
9709 Make_Op_Eq (Loc,
9710 Left_Opnd =>
9711 New_Reference_To (C, Loc),
9712 Right_Opnd =>
9713 New_Reference_To (RTE (RE_POK_Protected_Entry), Loc)),
9715 Right_Opnd =>
9716 Make_Op_Eq (Loc,
9717 Left_Opnd =>
9718 New_Reference_To (C, Loc),
9719 Right_Opnd =>
9720 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
9722 -- Dispatching requeue equivalent
9724 Then_Statements => New_List (
9725 Build_Dispatching_Requeue,
9726 Skip),
9728 -- elsif C = POK_Protected_Procedure then
9730 Elsif_Parts => New_List (
9731 Make_Elsif_Part (Loc,
9732 Condition =>
9733 Make_Op_Eq (Loc,
9734 Left_Opnd =>
9735 New_Reference_To (C, Loc),
9736 Right_Opnd =>
9737 New_Reference_To (
9738 RTE (RE_POK_Protected_Procedure), Loc)),
9740 -- Dispatching call equivalent
9742 Then_Statements => New_List (
9743 Build_Dispatching_Call_Equivalent))),
9745 -- else
9746 -- raise Program_Error;
9747 -- end if;
9749 Else_Statements => New_List (
9750 Make_Raise_Program_Error (Loc,
9751 Reason => PE_Explicit_Raise))));
9753 -- Wrap everything into a block
9755 return
9756 Make_Block_Statement (Loc,
9757 Declarations => Decls,
9758 Handled_Statement_Sequence =>
9759 Make_Handled_Sequence_Of_Statements (Loc,
9760 Statements => Stmts));
9761 end Build_Dispatching_Requeue_To_Any;
9763 --------------------------
9764 -- Build_Normal_Requeue --
9765 --------------------------
9767 function Build_Normal_Requeue return Node_Id is
9768 Params : constant List_Id := New_List;
9769 Param : Node_Id;
9770 RT_Call : Node_Id;
9772 begin
9773 -- Process the "with abort" parameter
9775 Prepend_To (Params,
9776 New_Reference_To (Boolean_Literals (Abort_Present (N)), Loc));
9778 -- Add the index expression to the parameters. It is common among all
9779 -- four cases.
9781 Prepend_To (Params,
9782 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
9784 if Is_Protected_Type (Old_Typ) then
9785 declare
9786 Self_Param : Node_Id;
9788 begin
9789 Self_Param :=
9790 Make_Attribute_Reference (Loc,
9791 Prefix =>
9792 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9793 Attribute_Name =>
9794 Name_Unchecked_Access);
9796 -- Protected to protected requeue
9798 if Is_Protected_Type (Conc_Typ) then
9799 RT_Call :=
9800 New_Reference_To (
9801 RTE (RE_Requeue_Protected_Entry), Loc);
9803 Param :=
9804 Make_Attribute_Reference (Loc,
9805 Prefix =>
9806 Concurrent_Ref (Concval),
9807 Attribute_Name =>
9808 Name_Unchecked_Access);
9810 -- Protected to task requeue
9812 else pragma Assert (Is_Task_Type (Conc_Typ));
9813 RT_Call :=
9814 New_Reference_To (
9815 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
9817 Param := Concurrent_Ref (Concval);
9818 end if;
9820 Prepend_To (Params, Param);
9821 Prepend_To (Params, Self_Param);
9822 end;
9824 else pragma Assert (Is_Task_Type (Old_Typ));
9826 -- Task to protected requeue
9828 if Is_Protected_Type (Conc_Typ) then
9829 RT_Call :=
9830 New_Reference_To (
9831 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
9833 Param :=
9834 Make_Attribute_Reference (Loc,
9835 Prefix =>
9836 Concurrent_Ref (Concval),
9837 Attribute_Name =>
9838 Name_Unchecked_Access);
9840 -- Task to task requeue
9842 else pragma Assert (Is_Task_Type (Conc_Typ));
9843 RT_Call :=
9844 New_Reference_To (RTE (RE_Requeue_Task_Entry), Loc);
9846 Param := Concurrent_Ref (Concval);
9847 end if;
9849 Prepend_To (Params, Param);
9850 end if;
9852 return
9853 Make_Procedure_Call_Statement (Loc,
9854 Name => RT_Call,
9855 Parameter_Associations => Params);
9856 end Build_Normal_Requeue;
9858 --------------------------
9859 -- Build_Skip_Statement --
9860 --------------------------
9862 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
9863 Skip_Stmt : Node_Id;
9865 begin
9866 -- Build a return statement to skip the rest of the entire body
9868 if Is_Protected_Type (Old_Typ) then
9869 Skip_Stmt := Make_Simple_Return_Statement (Loc);
9871 -- If the requeue is within a task, find the end label of the
9872 -- enclosing accept statement and create a goto statement to it.
9874 else
9875 declare
9876 Acc : Node_Id;
9877 Label : Node_Id;
9879 begin
9880 -- Climb the parent chain looking for the enclosing accept
9881 -- statement.
9883 Acc := Parent (Search);
9884 while Present (Acc)
9885 and then Nkind (Acc) /= N_Accept_Statement
9886 loop
9887 Acc := Parent (Acc);
9888 end loop;
9890 -- The last statement is the second label used for completing
9891 -- the rendezvous the usual way. The label we are looking for
9892 -- is right before it.
9894 Label :=
9895 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
9897 pragma Assert (Nkind (Label) = N_Label);
9899 -- Generate a goto statement to skip the rest of the accept
9901 Skip_Stmt :=
9902 Make_Goto_Statement (Loc,
9903 Name =>
9904 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
9905 end;
9906 end if;
9908 Set_Analyzed (Skip_Stmt);
9910 return Skip_Stmt;
9911 end Build_Skip_Statement;
9913 -- Start of processing for Expand_N_Requeue_Statement
9915 begin
9916 -- Extract the components of the entry call
9918 Extract_Entry (N, Concval, Ename, Index);
9919 Conc_Typ := Etype (Concval);
9921 -- If the prefix is an access to class-wide type, dereference to get
9922 -- object and entry type.
9924 if Is_Access_Type (Conc_Typ) then
9925 Conc_Typ := Designated_Type (Conc_Typ);
9926 Rewrite (Concval,
9927 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
9928 Analyze_And_Resolve (Concval, Conc_Typ);
9929 end if;
9931 -- Examine the scope stack in order to find nearest enclosing protected
9932 -- or task type. This will constitute our invocation source.
9934 Old_Typ := Current_Scope;
9935 while Present (Old_Typ)
9936 and then not Is_Protected_Type (Old_Typ)
9937 and then not Is_Task_Type (Old_Typ)
9938 loop
9939 Old_Typ := Scope (Old_Typ);
9940 end loop;
9942 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
9943 -- Concval.Ename where the type of Concval is class-wide concurrent
9944 -- interface.
9946 if Ada_Version >= Ada_2012
9947 and then Present (Concval)
9948 and then Is_Class_Wide_Type (Conc_Typ)
9949 and then Is_Concurrent_Interface (Conc_Typ)
9950 then
9951 declare
9952 Has_Impl : Boolean := False;
9953 Impl_Kind : Name_Id := No_Name;
9955 begin
9956 -- Check whether the Ename is flagged by pragma Implemented
9958 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
9959 Has_Impl := True;
9960 Impl_Kind := Implementation_Kind (Entity (Ename));
9961 end if;
9963 -- The procedure_or_entry_NAME is guaranteed to be overridden by
9964 -- an entry. Create a call to predefined primitive _Disp_Requeue.
9966 if Has_Impl
9967 and then Impl_Kind = Name_By_Entry
9968 then
9969 Rewrite (N, Build_Dispatching_Requeue);
9970 Analyze (N);
9971 Insert_After (N, Build_Skip_Statement (N));
9973 -- The procedure_or_entry_NAME is guaranteed to be overridden by
9974 -- a protected procedure. In this case the requeue is transformed
9975 -- into a dispatching call.
9977 elsif Has_Impl
9978 and then Impl_Kind = Name_By_Protected_Procedure
9979 then
9980 Rewrite (N, Build_Dispatching_Call_Equivalent);
9981 Analyze (N);
9983 -- The procedure_or_entry_NAME's implementation kind is either
9984 -- By_Any, Optional, or pragma Implemented was not applied at all.
9985 -- In this case a runtime test determines whether Ename denotes an
9986 -- entry or a protected procedure and performs the appropriate
9987 -- call.
9989 else
9990 Rewrite (N, Build_Dispatching_Requeue_To_Any);
9991 Analyze (N);
9992 end if;
9993 end;
9995 -- Processing for regular (non-dispatching) requeues
9997 else
9998 Rewrite (N, Build_Normal_Requeue);
9999 Analyze (N);
10000 Insert_After (N, Build_Skip_Statement (N));
10001 end if;
10002 end Expand_N_Requeue_Statement;
10004 -------------------------------
10005 -- Expand_N_Selective_Accept --
10006 -------------------------------
10008 procedure Expand_N_Selective_Accept (N : Node_Id) is
10009 Loc : constant Source_Ptr := Sloc (N);
10010 Alts : constant List_Id := Select_Alternatives (N);
10012 -- Note: in the below declarations a lot of new lists are allocated
10013 -- unconditionally which may well not end up being used. That's
10014 -- not a good idea since it wastes space gratuitously ???
10016 Accept_Case : List_Id;
10017 Accept_List : constant List_Id := New_List;
10019 Alt : Node_Id;
10020 Alt_List : constant List_Id := New_List;
10021 Alt_Stats : List_Id;
10022 Ann : Entity_Id := Empty;
10024 Block : Node_Id;
10025 Check_Guard : Boolean := True;
10027 Decls : constant List_Id := New_List;
10028 Stats : constant List_Id := New_List;
10029 Body_List : constant List_Id := New_List;
10030 Trailing_List : constant List_Id := New_List;
10032 Choices : List_Id;
10033 Else_Present : Boolean := False;
10034 Terminate_Alt : Node_Id := Empty;
10035 Select_Mode : Node_Id;
10037 Delay_Case : List_Id;
10038 Delay_Count : Integer := 0;
10039 Delay_Val : Entity_Id;
10040 Delay_Index : Entity_Id;
10041 Delay_Min : Entity_Id;
10042 Delay_Num : Int := 1;
10043 Delay_Alt_List : List_Id := New_List;
10044 Delay_List : constant List_Id := New_List;
10045 D : Entity_Id;
10046 M : Entity_Id;
10048 First_Delay : Boolean := True;
10049 Guard_Open : Entity_Id;
10051 End_Lab : Node_Id;
10052 Index : Int := 1;
10053 Lab : Node_Id;
10054 Num_Alts : Int;
10055 Num_Accept : Nat := 0;
10056 Proc : Node_Id;
10057 Q : Node_Id;
10058 Time_Type : Entity_Id;
10059 X : Node_Id;
10060 Select_Call : Node_Id;
10062 Qnam : constant Entity_Id :=
10063 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10065 Xnam : constant Entity_Id :=
10066 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10068 -----------------------
10069 -- Local subprograms --
10070 -----------------------
10072 function Accept_Or_Raise return List_Id;
10073 -- For the rare case where delay alternatives all have guards, and
10074 -- all of them are closed, it is still possible that there were open
10075 -- accept alternatives with no callers. We must reexamine the
10076 -- Accept_List, and execute a selective wait with no else if some
10077 -- accept is open. If none, we raise program_error.
10079 procedure Add_Accept (Alt : Node_Id);
10080 -- Process a single accept statement in a select alternative. Build
10081 -- procedure for body of accept, and add entry to dispatch table with
10082 -- expression for guard, in preparation for call to run time select.
10084 function Make_And_Declare_Label (Num : Int) return Node_Id;
10085 -- Manufacture a label using Num as a serial number and declare it.
10086 -- The declaration is appended to Decls. The label marks the trailing
10087 -- statements of an accept or delay alternative.
10089 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10090 -- Build call to Selective_Wait runtime routine
10092 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10093 -- Add code to compare value of delay with previous values, and
10094 -- generate case entry for trailing statements.
10096 procedure Process_Accept_Alternative
10097 (Alt : Node_Id;
10098 Index : Int;
10099 Proc : Node_Id);
10100 -- Add code to call corresponding procedure, and branch to
10101 -- trailing statements, if any.
10103 ---------------------
10104 -- Accept_Or_Raise --
10105 ---------------------
10107 function Accept_Or_Raise return List_Id is
10108 Cond : Node_Id;
10109 Stats : List_Id;
10110 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10112 begin
10113 -- We generate the following:
10115 -- for J in q'range loop
10116 -- if q(J).S /=null_task_entry then
10117 -- selective_wait (simple_mode,...);
10118 -- done := True;
10119 -- exit;
10120 -- end if;
10121 -- end loop;
10123 -- if no rendez_vous then
10124 -- raise program_error;
10125 -- end if;
10127 -- Note that the code needs to know that the selector name
10128 -- in an Accept_Alternative is named S.
10130 Cond := Make_Op_Ne (Loc,
10131 Left_Opnd =>
10132 Make_Selected_Component (Loc,
10133 Prefix =>
10134 Make_Indexed_Component (Loc,
10135 Prefix => New_Reference_To (Qnam, Loc),
10136 Expressions => New_List (New_Reference_To (J, Loc))),
10137 Selector_Name => Make_Identifier (Loc, Name_S)),
10138 Right_Opnd =>
10139 New_Reference_To (RTE (RE_Null_Task_Entry), Loc));
10141 Stats := New_List (
10142 Make_Implicit_Loop_Statement (N,
10143 Identifier => Empty,
10144 Iteration_Scheme =>
10145 Make_Iteration_Scheme (Loc,
10146 Loop_Parameter_Specification =>
10147 Make_Loop_Parameter_Specification (Loc,
10148 Defining_Identifier => J,
10149 Discrete_Subtype_Definition =>
10150 Make_Attribute_Reference (Loc,
10151 Prefix => New_Reference_To (Qnam, Loc),
10152 Attribute_Name => Name_Range,
10153 Expressions => New_List (
10154 Make_Integer_Literal (Loc, 1))))),
10156 Statements => New_List (
10157 Make_Implicit_If_Statement (N,
10158 Condition => Cond,
10159 Then_Statements => New_List (
10160 Make_Select_Call (
10161 New_Reference_To (RTE (RE_Simple_Mode), Loc)),
10162 Make_Exit_Statement (Loc))))));
10164 Append_To (Stats,
10165 Make_Raise_Program_Error (Loc,
10166 Condition => Make_Op_Eq (Loc,
10167 Left_Opnd => New_Reference_To (Xnam, Loc),
10168 Right_Opnd =>
10169 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
10170 Reason => PE_All_Guards_Closed));
10172 return Stats;
10173 end Accept_Or_Raise;
10175 ----------------
10176 -- Add_Accept --
10177 ----------------
10179 procedure Add_Accept (Alt : Node_Id) is
10180 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10181 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10182 Eloc : constant Source_Ptr := Sloc (Ename);
10183 Eent : constant Entity_Id := Entity (Ename);
10184 Index : constant Node_Id := Entry_Index (Acc_Stm);
10185 Null_Body : Node_Id;
10186 Proc_Body : Node_Id;
10187 PB_Ent : Entity_Id;
10188 Expr : Node_Id;
10189 Call : Node_Id;
10191 begin
10192 if No (Ann) then
10193 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10194 end if;
10196 if Present (Condition (Alt)) then
10197 Expr :=
10198 Make_Conditional_Expression (Eloc, New_List (
10199 Condition (Alt),
10200 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10201 New_Reference_To (RTE (RE_Null_Task_Entry), Eloc)));
10202 else
10203 Expr :=
10204 Entry_Index_Expression
10205 (Eloc, Eent, Index, Scope (Eent));
10206 end if;
10208 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10209 Null_Body := New_Reference_To (Standard_False, Eloc);
10211 if Abort_Allowed then
10212 Call := Make_Procedure_Call_Statement (Eloc,
10213 Name => New_Reference_To (RTE (RE_Abort_Undefer), Eloc));
10214 Insert_Before (First (Statements (Handled_Statement_Sequence (
10215 Accept_Statement (Alt)))), Call);
10216 Analyze (Call);
10217 end if;
10219 PB_Ent :=
10220 Make_Defining_Identifier (Eloc,
10221 New_External_Name (Chars (Ename), 'A', Num_Accept));
10223 if Comes_From_Source (Alt) then
10224 Set_Debug_Info_Needed (PB_Ent);
10225 end if;
10227 Proc_Body :=
10228 Make_Subprogram_Body (Eloc,
10229 Specification =>
10230 Make_Procedure_Specification (Eloc,
10231 Defining_Unit_Name => PB_Ent),
10232 Declarations => Declarations (Acc_Stm),
10233 Handled_Statement_Sequence =>
10234 Build_Accept_Body (Accept_Statement (Alt)));
10236 -- During the analysis of the body of the accept statement, any
10237 -- zero cost exception handler records were collected in the
10238 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10239 -- This is where we move them to where they belong, namely the
10240 -- newly created procedure.
10242 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10243 Append (Proc_Body, Body_List);
10245 else
10246 Null_Body := New_Reference_To (Standard_True, Eloc);
10248 -- if accept statement has declarations, insert above, given that
10249 -- we are not creating a body for the accept.
10251 if Present (Declarations (Acc_Stm)) then
10252 Insert_Actions (N, Declarations (Acc_Stm));
10253 end if;
10254 end if;
10256 Append_To (Accept_List,
10257 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10259 Num_Accept := Num_Accept + 1;
10260 end Add_Accept;
10262 ----------------------------
10263 -- Make_And_Declare_Label --
10264 ----------------------------
10266 function Make_And_Declare_Label (Num : Int) return Node_Id is
10267 Lab_Id : Node_Id;
10269 begin
10270 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10271 Lab :=
10272 Make_Label (Loc, Lab_Id);
10274 Append_To (Decls,
10275 Make_Implicit_Label_Declaration (Loc,
10276 Defining_Identifier =>
10277 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10278 Label_Construct => Lab));
10280 return Lab;
10281 end Make_And_Declare_Label;
10283 ----------------------
10284 -- Make_Select_Call --
10285 ----------------------
10287 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10288 Params : constant List_Id := New_List;
10290 begin
10291 Append (
10292 Make_Attribute_Reference (Loc,
10293 Prefix => New_Reference_To (Qnam, Loc),
10294 Attribute_Name => Name_Unchecked_Access),
10295 Params);
10296 Append (Select_Mode, Params);
10297 Append (New_Reference_To (Ann, Loc), Params);
10298 Append (New_Reference_To (Xnam, Loc), Params);
10300 return
10301 Make_Procedure_Call_Statement (Loc,
10302 Name => New_Reference_To (RTE (RE_Selective_Wait), Loc),
10303 Parameter_Associations => Params);
10304 end Make_Select_Call;
10306 --------------------------------
10307 -- Process_Accept_Alternative --
10308 --------------------------------
10310 procedure Process_Accept_Alternative
10311 (Alt : Node_Id;
10312 Index : Int;
10313 Proc : Node_Id)
10315 Choices : List_Id := No_List;
10316 Alt_Stats : List_Id;
10318 begin
10319 Adjust_Condition (Condition (Alt));
10320 Alt_Stats := No_List;
10322 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10323 Choices := New_List (
10324 Make_Integer_Literal (Loc, Index));
10326 Alt_Stats := New_List (
10327 Make_Procedure_Call_Statement (Sloc (Proc),
10328 Name => New_Reference_To (
10329 Defining_Unit_Name (Specification (Proc)), Sloc (Proc))));
10330 end if;
10332 if Statements (Alt) /= Empty_List then
10334 if No (Alt_Stats) then
10336 -- Accept with no body, followed by trailing statements
10338 Choices := New_List (
10339 Make_Integer_Literal (Loc, Index));
10341 Alt_Stats := New_List;
10342 end if;
10344 -- After the call, if any, branch to trailing statements. We
10345 -- create a label for each, as well as the corresponding label
10346 -- declaration.
10348 Lab := Make_And_Declare_Label (Index);
10349 Append_To (Alt_Stats,
10350 Make_Goto_Statement (Loc,
10351 Name => New_Copy (Identifier (Lab))));
10353 Append (Lab, Trailing_List);
10354 Append_List (Statements (Alt), Trailing_List);
10355 Append_To (Trailing_List,
10356 Make_Goto_Statement (Loc,
10357 Name => New_Copy (Identifier (End_Lab))));
10358 end if;
10360 if Present (Alt_Stats) then
10362 -- Procedure call. and/or trailing statements
10364 Append_To (Alt_List,
10365 Make_Case_Statement_Alternative (Loc,
10366 Discrete_Choices => Choices,
10367 Statements => Alt_Stats));
10368 end if;
10369 end Process_Accept_Alternative;
10371 -------------------------------
10372 -- Process_Delay_Alternative --
10373 -------------------------------
10375 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10376 Choices : List_Id;
10377 Cond : Node_Id;
10378 Delay_Alt : List_Id;
10380 begin
10381 -- Deal with C/Fortran boolean as delay condition
10383 Adjust_Condition (Condition (Alt));
10385 -- Determine the smallest specified delay
10387 -- for each delay alternative generate:
10389 -- if guard-expression then
10390 -- Delay_Val := delay-expression;
10391 -- Guard_Open := True;
10392 -- if Delay_Val < Delay_Min then
10393 -- Delay_Min := Delay_Val;
10394 -- Delay_Index := Index;
10395 -- end if;
10396 -- end if;
10398 -- The enclosing if-statement is omitted if there is no guard
10400 if Delay_Count = 1
10401 or else First_Delay
10402 then
10403 First_Delay := False;
10405 Delay_Alt := New_List (
10406 Make_Assignment_Statement (Loc,
10407 Name => New_Reference_To (Delay_Min, Loc),
10408 Expression => Expression (Delay_Statement (Alt))));
10410 if Delay_Count > 1 then
10411 Append_To (Delay_Alt,
10412 Make_Assignment_Statement (Loc,
10413 Name => New_Reference_To (Delay_Index, Loc),
10414 Expression => Make_Integer_Literal (Loc, Index)));
10415 end if;
10417 else
10418 Delay_Alt := New_List (
10419 Make_Assignment_Statement (Loc,
10420 Name => New_Reference_To (Delay_Val, Loc),
10421 Expression => Expression (Delay_Statement (Alt))));
10423 if Time_Type = Standard_Duration then
10424 Cond :=
10425 Make_Op_Lt (Loc,
10426 Left_Opnd => New_Reference_To (Delay_Val, Loc),
10427 Right_Opnd => New_Reference_To (Delay_Min, Loc));
10429 else
10430 -- The scope of the time type must define a comparison
10431 -- operator. The scope itself may not be visible, so we
10432 -- construct a node with entity information to insure that
10433 -- semantic analysis can find the proper operator.
10435 Cond :=
10436 Make_Function_Call (Loc,
10437 Name => Make_Selected_Component (Loc,
10438 Prefix => New_Reference_To (Scope (Time_Type), Loc),
10439 Selector_Name =>
10440 Make_Operator_Symbol (Loc,
10441 Chars => Name_Op_Lt,
10442 Strval => No_String)),
10443 Parameter_Associations =>
10444 New_List (
10445 New_Reference_To (Delay_Val, Loc),
10446 New_Reference_To (Delay_Min, Loc)));
10448 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10449 end if;
10451 Append_To (Delay_Alt,
10452 Make_Implicit_If_Statement (N,
10453 Condition => Cond,
10454 Then_Statements => New_List (
10455 Make_Assignment_Statement (Loc,
10456 Name => New_Reference_To (Delay_Min, Loc),
10457 Expression => New_Reference_To (Delay_Val, Loc)),
10459 Make_Assignment_Statement (Loc,
10460 Name => New_Reference_To (Delay_Index, Loc),
10461 Expression => Make_Integer_Literal (Loc, Index)))));
10462 end if;
10464 if Check_Guard then
10465 Append_To (Delay_Alt,
10466 Make_Assignment_Statement (Loc,
10467 Name => New_Reference_To (Guard_Open, Loc),
10468 Expression => New_Reference_To (Standard_True, Loc)));
10469 end if;
10471 if Present (Condition (Alt)) then
10472 Delay_Alt := New_List (
10473 Make_Implicit_If_Statement (N,
10474 Condition => Condition (Alt),
10475 Then_Statements => Delay_Alt));
10476 end if;
10478 Append_List (Delay_Alt, Delay_List);
10480 -- If the delay alternative has a statement part, add choice to the
10481 -- case statements for delays.
10483 if Present (Statements (Alt)) then
10485 if Delay_Count = 1 then
10486 Append_List (Statements (Alt), Delay_Alt_List);
10488 else
10489 Choices := New_List (
10490 Make_Integer_Literal (Loc, Index));
10492 Append_To (Delay_Alt_List,
10493 Make_Case_Statement_Alternative (Loc,
10494 Discrete_Choices => Choices,
10495 Statements => Statements (Alt)));
10496 end if;
10498 elsif Delay_Count = 1 then
10500 -- If the single delay has no trailing statements, add a branch
10501 -- to the exit label to the selective wait.
10503 Delay_Alt_List := New_List (
10504 Make_Goto_Statement (Loc,
10505 Name => New_Copy (Identifier (End_Lab))));
10507 end if;
10508 end Process_Delay_Alternative;
10510 -- Start of processing for Expand_N_Selective_Accept
10512 begin
10513 Process_Statements_For_Controlled_Objects (N);
10515 -- First insert some declarations before the select. The first is:
10517 -- Ann : Address
10519 -- This variable holds the parameters passed to the accept body. This
10520 -- declaration has already been inserted by the time we get here by
10521 -- a call to Expand_Accept_Declarations made from the semantics when
10522 -- processing the first accept statement contained in the select. We
10523 -- can find this entity as Accept_Address (E), where E is any of the
10524 -- entries references by contained accept statements.
10526 -- The first step is to scan the list of Selective_Accept_Statements
10527 -- to find this entity, and also count the number of accepts, and
10528 -- determine if terminated, delay or else is present:
10530 Num_Alts := 0;
10532 Alt := First (Alts);
10533 while Present (Alt) loop
10534 Process_Statements_For_Controlled_Objects (Alt);
10536 if Nkind (Alt) = N_Accept_Alternative then
10537 Add_Accept (Alt);
10539 elsif Nkind (Alt) = N_Delay_Alternative then
10540 Delay_Count := Delay_Count + 1;
10542 -- If the delays are relative delays, the delay expressions have
10543 -- type Standard_Duration. Otherwise they must have some time type
10544 -- recognized by GNAT.
10546 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10547 Time_Type := Standard_Duration;
10548 else
10549 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10551 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10552 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10553 then
10554 null;
10555 else
10556 Error_Msg_NE (
10557 "& is not a time type (RM 9.6(6))",
10558 Expression (Delay_Statement (Alt)), Time_Type);
10559 Time_Type := Standard_Duration;
10560 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10561 end if;
10562 end if;
10564 if No (Condition (Alt)) then
10566 -- This guard will always be open
10568 Check_Guard := False;
10569 end if;
10571 elsif Nkind (Alt) = N_Terminate_Alternative then
10572 Adjust_Condition (Condition (Alt));
10573 Terminate_Alt := Alt;
10574 end if;
10576 Num_Alts := Num_Alts + 1;
10577 Next (Alt);
10578 end loop;
10580 Else_Present := Present (Else_Statements (N));
10582 -- At the same time (see procedure Add_Accept) we build the accept list:
10584 -- Qnn : Accept_List (1 .. num-select) := (
10585 -- (null-body, entry-index),
10586 -- (null-body, entry-index),
10587 -- ..
10588 -- (null_body, entry-index));
10590 -- In the above declaration, null-body is True if the corresponding
10591 -- accept has no body, and false otherwise. The entry is either the
10592 -- entry index expression if there is no guard, or if a guard is
10593 -- present, then a conditional expression of the form:
10595 -- (if guard then entry-index else Null_Task_Entry)
10597 -- If a guard is statically known to be false, the entry can simply
10598 -- be omitted from the accept list.
10600 Q :=
10601 Make_Object_Declaration (Loc,
10602 Defining_Identifier => Qnam,
10603 Object_Definition =>
10604 New_Reference_To (RTE (RE_Accept_List), Loc),
10605 Aliased_Present => True,
10607 Expression =>
10608 Make_Qualified_Expression (Loc,
10609 Subtype_Mark =>
10610 New_Reference_To (RTE (RE_Accept_List), Loc),
10611 Expression =>
10612 Make_Aggregate (Loc, Expressions => Accept_List)));
10614 Append (Q, Decls);
10616 -- Then we declare the variable that holds the index for the accept
10617 -- that will be selected for service:
10619 -- Xnn : Select_Index;
10621 X :=
10622 Make_Object_Declaration (Loc,
10623 Defining_Identifier => Xnam,
10624 Object_Definition =>
10625 New_Reference_To (RTE (RE_Select_Index), Loc),
10626 Expression =>
10627 New_Reference_To (RTE (RE_No_Rendezvous), Loc));
10629 Append (X, Decls);
10631 -- After this follow procedure declarations for each accept body
10633 -- procedure Pnn is
10634 -- begin
10635 -- ...
10636 -- end;
10638 -- where the ... are statements from the corresponding procedure body.
10639 -- No parameters are involved, since the parameters are passed via Ann
10640 -- and the parameter references have already been expanded to be direct
10641 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10642 -- any embedded tasking statements (which would normally be illegal in
10643 -- procedures), have been converted to calls to the tasking runtime so
10644 -- there is no problem in putting them into procedures.
10646 -- The original accept statement has been expanded into a block in
10647 -- the same fashion as for simple accepts (see Build_Accept_Body).
10649 -- Note: we don't really need to build these procedures for the case
10650 -- where no delay statement is present, but it is just as easy to
10651 -- build them unconditionally, and not significantly inefficient,
10652 -- since if they are short they will be inlined anyway.
10654 -- The procedure declarations have been assembled in Body_List
10656 -- If delays are present, we must compute the required delay.
10657 -- We first generate the declarations:
10659 -- Delay_Index : Boolean := 0;
10660 -- Delay_Min : Some_Time_Type.Time;
10661 -- Delay_Val : Some_Time_Type.Time;
10663 -- Delay_Index will be set to the index of the minimum delay, i.e. the
10664 -- active delay that is actually chosen as the basis for the possible
10665 -- delay if an immediate rendez-vous is not possible.
10667 -- In the most common case there is a single delay statement, and this
10668 -- is handled specially.
10670 if Delay_Count > 0 then
10672 -- Generate the required declarations
10674 Delay_Val :=
10675 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
10676 Delay_Index :=
10677 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
10678 Delay_Min :=
10679 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
10681 Append_To (Decls,
10682 Make_Object_Declaration (Loc,
10683 Defining_Identifier => Delay_Val,
10684 Object_Definition => New_Reference_To (Time_Type, Loc)));
10686 Append_To (Decls,
10687 Make_Object_Declaration (Loc,
10688 Defining_Identifier => Delay_Index,
10689 Object_Definition => New_Reference_To (Standard_Integer, Loc),
10690 Expression => Make_Integer_Literal (Loc, 0)));
10692 Append_To (Decls,
10693 Make_Object_Declaration (Loc,
10694 Defining_Identifier => Delay_Min,
10695 Object_Definition => New_Reference_To (Time_Type, Loc),
10696 Expression =>
10697 Unchecked_Convert_To (Time_Type,
10698 Make_Attribute_Reference (Loc,
10699 Prefix =>
10700 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
10701 Attribute_Name => Name_Last))));
10703 -- Create Duration and Delay_Mode objects used for passing a delay
10704 -- value to RTS
10706 D := Make_Temporary (Loc, 'D');
10707 M := Make_Temporary (Loc, 'M');
10709 declare
10710 Discr : Entity_Id;
10712 begin
10713 -- Note that these values are defined in s-osprim.ads and must
10714 -- be kept in sync:
10716 -- Relative : constant := 0;
10717 -- Absolute_Calendar : constant := 1;
10718 -- Absolute_RT : constant := 2;
10720 if Time_Type = Standard_Duration then
10721 Discr := Make_Integer_Literal (Loc, 0);
10723 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10724 Discr := Make_Integer_Literal (Loc, 1);
10726 else
10727 pragma Assert
10728 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
10729 Discr := Make_Integer_Literal (Loc, 2);
10730 end if;
10732 Append_To (Decls,
10733 Make_Object_Declaration (Loc,
10734 Defining_Identifier => D,
10735 Object_Definition =>
10736 New_Reference_To (Standard_Duration, Loc)));
10738 Append_To (Decls,
10739 Make_Object_Declaration (Loc,
10740 Defining_Identifier => M,
10741 Object_Definition =>
10742 New_Reference_To (Standard_Integer, Loc),
10743 Expression => Discr));
10744 end;
10746 if Check_Guard then
10747 Guard_Open :=
10748 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
10750 Append_To (Decls,
10751 Make_Object_Declaration (Loc,
10752 Defining_Identifier => Guard_Open,
10753 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
10754 Expression => New_Reference_To (Standard_False, Loc)));
10755 end if;
10757 -- Delay_Count is zero, don't need M and D set (suppress warning)
10759 else
10760 M := Empty;
10761 D := Empty;
10762 end if;
10764 if Present (Terminate_Alt) then
10766 -- If the terminate alternative guard is False, use
10767 -- Simple_Mode; otherwise use Terminate_Mode.
10769 if Present (Condition (Terminate_Alt)) then
10770 Select_Mode := Make_Conditional_Expression (Loc,
10771 New_List (Condition (Terminate_Alt),
10772 New_Reference_To (RTE (RE_Terminate_Mode), Loc),
10773 New_Reference_To (RTE (RE_Simple_Mode), Loc)));
10774 else
10775 Select_Mode := New_Reference_To (RTE (RE_Terminate_Mode), Loc);
10776 end if;
10778 elsif Else_Present or Delay_Count > 0 then
10779 Select_Mode := New_Reference_To (RTE (RE_Else_Mode), Loc);
10781 else
10782 Select_Mode := New_Reference_To (RTE (RE_Simple_Mode), Loc);
10783 end if;
10785 Select_Call := Make_Select_Call (Select_Mode);
10786 Append (Select_Call, Stats);
10788 -- Now generate code to act on the result. There is an entry
10789 -- in this case for each accept statement with a non-null body,
10790 -- followed by a branch to the statements that follow the Accept.
10791 -- In the absence of delay alternatives, we generate:
10793 -- case X is
10794 -- when No_Rendezvous => -- omitted if simple mode
10795 -- goto Lab0;
10797 -- when 1 =>
10798 -- P1n;
10799 -- goto Lab1;
10801 -- when 2 =>
10802 -- P2n;
10803 -- goto Lab2;
10805 -- when others =>
10806 -- goto Exit;
10807 -- end case;
10809 -- Lab0: Else_Statements;
10810 -- goto exit;
10812 -- Lab1: Trailing_Statements1;
10813 -- goto Exit;
10815 -- Lab2: Trailing_Statements2;
10816 -- goto Exit;
10817 -- ...
10818 -- Exit:
10820 -- Generate label for common exit
10822 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
10824 -- First entry is the default case, when no rendezvous is possible
10826 Choices := New_List (New_Reference_To (RTE (RE_No_Rendezvous), Loc));
10828 if Else_Present then
10830 -- If no rendezvous is possible, the else part is executed
10832 Lab := Make_And_Declare_Label (0);
10833 Alt_Stats := New_List (
10834 Make_Goto_Statement (Loc,
10835 Name => New_Copy (Identifier (Lab))));
10837 Append (Lab, Trailing_List);
10838 Append_List (Else_Statements (N), Trailing_List);
10839 Append_To (Trailing_List,
10840 Make_Goto_Statement (Loc,
10841 Name => New_Copy (Identifier (End_Lab))));
10842 else
10843 Alt_Stats := New_List (
10844 Make_Goto_Statement (Loc,
10845 Name => New_Copy (Identifier (End_Lab))));
10846 end if;
10848 Append_To (Alt_List,
10849 Make_Case_Statement_Alternative (Loc,
10850 Discrete_Choices => Choices,
10851 Statements => Alt_Stats));
10853 -- We make use of the fact that Accept_Index is an integer type, and
10854 -- generate successive literals for entries for each accept. Only those
10855 -- for which there is a body or trailing statements get a case entry.
10857 Alt := First (Select_Alternatives (N));
10858 Proc := First (Body_List);
10859 while Present (Alt) loop
10861 if Nkind (Alt) = N_Accept_Alternative then
10862 Process_Accept_Alternative (Alt, Index, Proc);
10863 Index := Index + 1;
10865 if Present
10866 (Handled_Statement_Sequence (Accept_Statement (Alt)))
10867 then
10868 Next (Proc);
10869 end if;
10871 elsif Nkind (Alt) = N_Delay_Alternative then
10872 Process_Delay_Alternative (Alt, Delay_Num);
10873 Delay_Num := Delay_Num + 1;
10874 end if;
10876 Next (Alt);
10877 end loop;
10879 -- An others choice is always added to the main case, as well
10880 -- as the delay case (to satisfy the compiler).
10882 Append_To (Alt_List,
10883 Make_Case_Statement_Alternative (Loc,
10884 Discrete_Choices =>
10885 New_List (Make_Others_Choice (Loc)),
10886 Statements =>
10887 New_List (Make_Goto_Statement (Loc,
10888 Name => New_Copy (Identifier (End_Lab))))));
10890 Accept_Case := New_List (
10891 Make_Case_Statement (Loc,
10892 Expression => New_Reference_To (Xnam, Loc),
10893 Alternatives => Alt_List));
10895 Append_List (Trailing_List, Accept_Case);
10896 Append (End_Lab, Accept_Case);
10897 Append_List (Body_List, Decls);
10899 -- Construct case statement for trailing statements of delay
10900 -- alternatives, if there are several of them.
10902 if Delay_Count > 1 then
10903 Append_To (Delay_Alt_List,
10904 Make_Case_Statement_Alternative (Loc,
10905 Discrete_Choices =>
10906 New_List (Make_Others_Choice (Loc)),
10907 Statements =>
10908 New_List (Make_Null_Statement (Loc))));
10910 Delay_Case := New_List (
10911 Make_Case_Statement (Loc,
10912 Expression => New_Reference_To (Delay_Index, Loc),
10913 Alternatives => Delay_Alt_List));
10914 else
10915 Delay_Case := Delay_Alt_List;
10916 end if;
10918 -- If there are no delay alternatives, we append the case statement
10919 -- to the statement list.
10921 if Delay_Count = 0 then
10922 Append_List (Accept_Case, Stats);
10924 -- Delay alternatives present
10926 else
10927 -- If delay alternatives are present we generate:
10929 -- find minimum delay.
10930 -- DX := minimum delay;
10931 -- M := <delay mode>;
10932 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
10933 -- DX, MX, X);
10935 -- if X = No_Rendezvous then
10936 -- case statement for delay statements.
10937 -- else
10938 -- case statement for accept alternatives.
10939 -- end if;
10941 declare
10942 Cases : Node_Id;
10943 Stmt : Node_Id;
10944 Parms : List_Id;
10945 Parm : Node_Id;
10946 Conv : Node_Id;
10948 begin
10949 -- The type of the delay expression is known to be legal
10951 if Time_Type = Standard_Duration then
10952 Conv := New_Reference_To (Delay_Min, Loc);
10954 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10955 Conv := Make_Function_Call (Loc,
10956 New_Reference_To (RTE (RO_CA_To_Duration), Loc),
10957 New_List (New_Reference_To (Delay_Min, Loc)));
10959 else
10960 pragma Assert
10961 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
10963 Conv := Make_Function_Call (Loc,
10964 New_Reference_To (RTE (RO_RT_To_Duration), Loc),
10965 New_List (New_Reference_To (Delay_Min, Loc)));
10966 end if;
10968 Stmt := Make_Assignment_Statement (Loc,
10969 Name => New_Reference_To (D, Loc),
10970 Expression => Conv);
10972 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
10974 Parms := Parameter_Associations (Select_Call);
10975 Parm := First (Parms);
10977 while Present (Parm)
10978 and then Parm /= Select_Mode
10979 loop
10980 Next (Parm);
10981 end loop;
10983 pragma Assert (Present (Parm));
10984 Rewrite (Parm, New_Reference_To (RTE (RE_Delay_Mode), Loc));
10985 Analyze (Parm);
10987 -- Prepare two new parameters of Duration and Delay_Mode type
10988 -- which represent the value and the mode of the minimum delay.
10990 Next (Parm);
10991 Insert_After (Parm, New_Reference_To (M, Loc));
10992 Insert_After (Parm, New_Reference_To (D, Loc));
10994 -- Create a call to RTS
10996 Rewrite (Select_Call,
10997 Make_Procedure_Call_Statement (Loc,
10998 Name => New_Reference_To (RTE (RE_Timed_Selective_Wait), Loc),
10999 Parameter_Associations => Parms));
11001 -- This new call should follow the calculation of the minimum
11002 -- delay.
11004 Insert_List_Before (Select_Call, Delay_List);
11006 if Check_Guard then
11007 Stmt :=
11008 Make_Implicit_If_Statement (N,
11009 Condition => New_Reference_To (Guard_Open, Loc),
11010 Then_Statements =>
11011 New_List (New_Copy_Tree (Stmt),
11012 New_Copy_Tree (Select_Call)),
11013 Else_Statements => Accept_Or_Raise);
11014 Rewrite (Select_Call, Stmt);
11015 else
11016 Insert_Before (Select_Call, Stmt);
11017 end if;
11019 Cases :=
11020 Make_Implicit_If_Statement (N,
11021 Condition => Make_Op_Eq (Loc,
11022 Left_Opnd => New_Reference_To (Xnam, Loc),
11023 Right_Opnd =>
11024 New_Reference_To (RTE (RE_No_Rendezvous), Loc)),
11026 Then_Statements => Delay_Case,
11027 Else_Statements => Accept_Case);
11029 Append (Cases, Stats);
11030 end;
11031 end if;
11033 -- Replace accept statement with appropriate block
11035 Block :=
11036 Make_Block_Statement (Loc,
11037 Declarations => Decls,
11038 Handled_Statement_Sequence =>
11039 Make_Handled_Sequence_Of_Statements (Loc,
11040 Statements => Stats));
11042 Rewrite (N, Block);
11043 Analyze (N);
11045 -- Note: have to worry more about abort deferral in above code ???
11047 -- Final step is to unstack the Accept_Address entries for all accept
11048 -- statements appearing in accept alternatives in the select statement
11050 Alt := First (Alts);
11051 while Present (Alt) loop
11052 if Nkind (Alt) = N_Accept_Alternative then
11053 Remove_Last_Elmt (Accept_Address
11054 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11055 end if;
11057 Next (Alt);
11058 end loop;
11059 end Expand_N_Selective_Accept;
11061 --------------------------------------
11062 -- Expand_N_Single_Task_Declaration --
11063 --------------------------------------
11065 -- Single task declarations should never be present after semantic
11066 -- analysis, since we expect them to be replaced by a declaration of an
11067 -- anonymous task type, followed by a declaration of the task object. We
11068 -- include this routine to make sure that is happening!
11070 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11071 begin
11072 raise Program_Error;
11073 end Expand_N_Single_Task_Declaration;
11075 ------------------------
11076 -- Expand_N_Task_Body --
11077 ------------------------
11079 -- Given a task body
11081 -- task body tname is
11082 -- <declarations>
11083 -- begin
11084 -- <statements>
11085 -- end x;
11087 -- This expansion routine converts it into a procedure and sets the
11088 -- elaboration flag for the procedure to true, to represent the fact
11089 -- that the task body is now elaborated:
11091 -- procedure tnameB (_Task : access tnameV) is
11092 -- discriminal : dtype renames _Task.discriminant;
11094 -- procedure _clean is
11095 -- begin
11096 -- Abort_Defer.all;
11097 -- Complete_Task;
11098 -- Abort_Undefer.all;
11099 -- return;
11100 -- end _clean;
11102 -- begin
11103 -- Abort_Undefer.all;
11104 -- <declarations>
11105 -- System.Task_Stages.Complete_Activation;
11106 -- <statements>
11107 -- at end
11108 -- _clean;
11109 -- end tnameB;
11111 -- tnameE := True;
11113 -- In addition, if the task body is an activator, then a call to activate
11114 -- tasks is added at the start of the statements, before the call to
11115 -- Complete_Activation, and if in addition the task is a master then it
11116 -- must be established as a master. These calls are inserted and analyzed
11117 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11118 -- expanded.
11120 -- There is one discriminal declaration line generated for each
11121 -- discriminant that is present to provide an easy reference point for
11122 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11124 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11125 -- task body procedures have a profile (Arg : System.Address). That is
11126 -- needed because GNARLI has to use the same access-to-subprogram type
11127 -- for all task types. We depend here on knowing that in GNAT, passing
11128 -- an address argument by value is identical to passing a record value
11129 -- by access (in either case a single pointer is passed), so even though
11130 -- this procedure has the wrong profile. In fact it's all OK, since the
11131 -- callings sequence is identical.
11133 procedure Expand_N_Task_Body (N : Node_Id) is
11134 Loc : constant Source_Ptr := Sloc (N);
11135 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11136 Call : Node_Id;
11137 New_N : Node_Id;
11139 Insert_Nod : Node_Id;
11140 -- Used to determine the proper location of wrapper body insertions
11142 begin
11143 -- Add renaming declarations for discriminals and a declaration for the
11144 -- entry family index (if applicable).
11146 Install_Private_Data_Declarations
11147 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11149 -- Add a call to Abort_Undefer at the very beginning of the task
11150 -- body since this body is called with abort still deferred.
11152 if Abort_Allowed then
11153 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11154 Insert_Before
11155 (First (Statements (Handled_Statement_Sequence (N))), Call);
11156 Analyze (Call);
11157 end if;
11159 -- The statement part has already been protected with an at_end and
11160 -- cleanup actions. The call to Complete_Activation must be placed
11161 -- at the head of the sequence of statements of that block. The
11162 -- declarations have been merged in this sequence of statements but
11163 -- the first real statement is accessible from the First_Real_Statement
11164 -- field (which was set for exactly this purpose).
11166 if Restricted_Profile then
11167 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11168 else
11169 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11170 end if;
11172 Insert_Before
11173 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11174 Analyze (Call);
11176 New_N :=
11177 Make_Subprogram_Body (Loc,
11178 Specification => Build_Task_Proc_Specification (Ttyp),
11179 Declarations => Declarations (N),
11180 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11182 -- If the task contains generic instantiations, cleanup actions are
11183 -- delayed until after instantiation. Transfer the activation chain to
11184 -- the subprogram, to insure that the activation call is properly
11185 -- generated. It the task body contains inner tasks, indicate that the
11186 -- subprogram is a task master.
11188 if Delay_Cleanups (Ttyp) then
11189 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11190 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11191 end if;
11193 Rewrite (N, New_N);
11194 Analyze (N);
11196 -- Set elaboration flag immediately after task body. If the body is a
11197 -- subunit, the flag is set in the declarative part containing the stub.
11199 if Nkind (Parent (N)) /= N_Subunit then
11200 Insert_After (N,
11201 Make_Assignment_Statement (Loc,
11202 Name =>
11203 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11204 Expression => New_Reference_To (Standard_True, Loc)));
11205 end if;
11207 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11208 -- the task body. At this point all wrapper specs have been created,
11209 -- frozen and included in the dispatch table for the task type.
11211 if Ada_Version >= Ada_2005 then
11212 if Nkind (Parent (N)) = N_Subunit then
11213 Insert_Nod := Corresponding_Stub (Parent (N));
11214 else
11215 Insert_Nod := N;
11216 end if;
11218 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11219 end if;
11220 end Expand_N_Task_Body;
11222 ------------------------------------
11223 -- Expand_N_Task_Type_Declaration --
11224 ------------------------------------
11226 -- We have several things to do. First we must create a Boolean flag used
11227 -- to mark if the body is elaborated yet. This variable gets set to True
11228 -- when the body of the task is elaborated (we can't rely on the normal
11229 -- ABE mechanism for the task body, since we need to pass an access to
11230 -- this elaboration boolean to the runtime routines).
11232 -- taskE : aliased Boolean := False;
11234 -- Next a variable is declared to hold the task stack size (either the
11235 -- default : Unspecified_Size, or a value that is set by a pragma
11236 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11237 -- the variable is initialized with this value:
11239 -- taskZ : Size_Type := Unspecified_Size;
11240 -- or
11241 -- taskZ : Size_Type := Size_Type (size_expression);
11243 -- Note: No variable is needed to hold the task relative deadline since
11244 -- its value would never be static because the parameter is of a private
11245 -- type (Ada.Real_Time.Time_Span).
11247 -- Next we create a corresponding record type declaration used to represent
11248 -- values of this task. The general form of this type declaration is
11250 -- type taskV (discriminants) is record
11251 -- _Task_Id : Task_Id;
11252 -- entry_family : array (bounds) of Void;
11253 -- _Priority : Integer := priority_expression;
11254 -- _Size : Size_Type := size_expression;
11255 -- _Task_Info : Task_Info_Type := task_info_expression;
11256 -- _CPU : Integer := cpu_range_expression;
11257 -- _Relative_Deadline : Time_Span := time_span_expression;
11258 -- _Domain : Dispatching_Domain := dd_expression;
11259 -- end record;
11261 -- The discriminants are present only if the corresponding task type has
11262 -- discriminants, and they exactly mirror the task type discriminants.
11264 -- The Id field is always present. It contains the Task_Id value, as set by
11265 -- the call to Create_Task. Note that although the task is limited, the
11266 -- task value record type is not limited, so there is no problem in passing
11267 -- this field as an out parameter to Create_Task.
11269 -- One entry_family component is present for each entry family in the task
11270 -- definition. The bounds correspond to the bounds of the entry family
11271 -- (which may depend on discriminants). The element type is void, since we
11272 -- only need the bounds information for determining the entry index. Note
11273 -- that the use of an anonymous array would normally be illegal in this
11274 -- context, but this is a parser check, and the semantics is quite prepared
11275 -- to handle such a case.
11277 -- The _Size field is present only if a Storage_Size pragma appears in the
11278 -- task definition. The expression captures the argument that was present
11279 -- in the pragma, and is used to override the task stack size otherwise
11280 -- associated with the task type.
11282 -- The _Priority field is present only if a Priority or Interrupt_Priority
11283 -- pragma appears in the task definition. The expression captures the
11284 -- argument that was present in the pragma, and is used to provide the Size
11285 -- parameter to the call to Create_Task.
11287 -- The _Task_Info field is present only if a Task_Info pragma appears in
11288 -- the task definition. The expression captures the argument that was
11289 -- present in the pragma, and is used to provide the Task_Image parameter
11290 -- to the call to Create_Task.
11292 -- The _CPU field is present only if a CPU pragma appears in the task
11293 -- definition. The expression captures the argument that was present in
11294 -- the pragma, and is used to provide the CPU parameter to the call to
11295 -- Create_Task.
11297 -- The _Relative_Deadline field is present only if a Relative_Deadline
11298 -- pragma appears in the task definition. The expression captures the
11299 -- argument that was present in the pragma, and is used to provide the
11300 -- Relative_Deadline parameter to the call to Create_Task.
11302 -- The _Domain field is present only if a Dispatching_Domain pragma or
11303 -- aspect appears in the task definition. The expression captures the
11304 -- argument that was present in the pragma or aspect, and is used to
11305 -- provide the Dispatching_Domain parameter to the call to Create_Task.
11307 -- When a task is declared, an instance of the task value record is
11308 -- created. The elaboration of this declaration creates the correct bounds
11309 -- for the entry families, and also evaluates the size, priority, and
11310 -- task_Info expressions if needed. The initialization routine for the task
11311 -- type itself then calls Create_Task with appropriate parameters to
11312 -- initialize the value of the Task_Id field.
11314 -- Note: the address of this record is passed as the "Discriminants"
11315 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11316 -- body procedure, it does not matter that it does not quite match the
11317 -- GNARLI model of what is being passed (the record contains more than just
11318 -- the discriminants, but the discriminants can be found from the record
11319 -- value).
11321 -- The Entity_Id for this created record type is placed in the
11322 -- Corresponding_Record_Type field of the associated task type entity.
11324 -- Next we create a procedure specification for the task body procedure:
11326 -- procedure taskB (_Task : access taskV);
11328 -- Note that this must come after the record type declaration, since
11329 -- the spec refers to this type. It turns out that the initialization
11330 -- procedure for the value type references the task body spec, but that's
11331 -- fine, since it won't be generated till the freeze point for the type,
11332 -- which is certainly after the task body spec declaration.
11334 -- Finally, we set the task index value field of the entry attribute in
11335 -- the case of a simple entry.
11337 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11338 Loc : constant Source_Ptr := Sloc (N);
11339 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11340 Tasknm : constant Name_Id := Chars (Tasktyp);
11341 Taskdef : constant Node_Id := Task_Definition (N);
11343 Proc_Spec : Node_Id;
11344 Rec_Decl : Node_Id;
11345 Rec_Ent : Entity_Id;
11346 Cdecls : List_Id;
11347 Elab_Decl : Node_Id;
11348 Size_Decl : Node_Id;
11349 Body_Decl : Node_Id;
11350 Task_Size : Node_Id;
11351 Ent_Stack : Entity_Id;
11352 Decl_Stack : Node_Id;
11354 begin
11355 -- If already expanded, nothing to do
11357 if Present (Corresponding_Record_Type (Tasktyp)) then
11358 return;
11359 end if;
11361 -- Here we will do the expansion
11363 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11365 Rec_Ent := Defining_Identifier (Rec_Decl);
11366 Cdecls := Component_Items (Component_List
11367 (Type_Definition (Rec_Decl)));
11369 Qualify_Entity_Names (N);
11371 -- First create the elaboration variable
11373 Elab_Decl :=
11374 Make_Object_Declaration (Loc,
11375 Defining_Identifier =>
11376 Make_Defining_Identifier (Sloc (Tasktyp),
11377 Chars => New_External_Name (Tasknm, 'E')),
11378 Aliased_Present => True,
11379 Object_Definition => New_Reference_To (Standard_Boolean, Loc),
11380 Expression => New_Reference_To (Standard_False, Loc));
11381 Insert_After (N, Elab_Decl);
11383 -- Next create the declaration of the size variable (tasknmZ)
11385 Set_Storage_Size_Variable (Tasktyp,
11386 Make_Defining_Identifier (Sloc (Tasktyp),
11387 Chars => New_External_Name (Tasknm, 'Z')));
11389 if Present (Taskdef)
11390 and then Has_Storage_Size_Pragma (Taskdef)
11391 and then
11392 Is_Static_Expression
11393 (Expression
11394 (First (Pragma_Argument_Associations
11395 (Find_Task_Or_Protected_Pragma
11396 (Taskdef, Name_Storage_Size)))))
11397 then
11398 Size_Decl :=
11399 Make_Object_Declaration (Loc,
11400 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11401 Object_Definition => New_Reference_To (RTE (RE_Size_Type), Loc),
11402 Expression =>
11403 Convert_To (RTE (RE_Size_Type),
11404 Relocate_Node
11405 (Expression (First (Pragma_Argument_Associations
11406 (Find_Task_Or_Protected_Pragma
11407 (Taskdef, Name_Storage_Size)))))));
11409 else
11410 Size_Decl :=
11411 Make_Object_Declaration (Loc,
11412 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11413 Object_Definition =>
11414 New_Reference_To (RTE (RE_Size_Type), Loc),
11415 Expression =>
11416 New_Reference_To (RTE (RE_Unspecified_Size), Loc));
11417 end if;
11419 Insert_After (Elab_Decl, Size_Decl);
11421 -- Next build the rest of the corresponding record declaration. This is
11422 -- done last, since the corresponding record initialization procedure
11423 -- will reference the previously created entities.
11425 -- Fill in the component declarations -- first the _Task_Id field
11427 Append_To (Cdecls,
11428 Make_Component_Declaration (Loc,
11429 Defining_Identifier =>
11430 Make_Defining_Identifier (Loc, Name_uTask_Id),
11431 Component_Definition =>
11432 Make_Component_Definition (Loc,
11433 Aliased_Present => False,
11434 Subtype_Indication => New_Reference_To (RTE (RO_ST_Task_Id),
11435 Loc))));
11437 -- Declare static ATCB (that is, created by the expander) if we are
11438 -- using the Restricted run time.
11440 if Restricted_Profile then
11441 Append_To (Cdecls,
11442 Make_Component_Declaration (Loc,
11443 Defining_Identifier =>
11444 Make_Defining_Identifier (Loc, Name_uATCB),
11446 Component_Definition =>
11447 Make_Component_Definition (Loc,
11448 Aliased_Present => True,
11449 Subtype_Indication => Make_Subtype_Indication (Loc,
11450 Subtype_Mark =>
11451 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11453 Constraint =>
11454 Make_Index_Or_Discriminant_Constraint (Loc,
11455 Constraints =>
11456 New_List (Make_Integer_Literal (Loc, 0)))))));
11458 end if;
11460 -- Declare static stack (that is, created by the expander) if we are
11461 -- using the Restricted run time on a bare board configuration.
11463 if Restricted_Profile
11464 and then Preallocated_Stacks_On_Target
11465 then
11466 -- First we need to extract the appropriate stack size
11468 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11470 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11471 declare
11472 Expr_N : constant Node_Id :=
11473 Expression (First (
11474 Pragma_Argument_Associations (
11475 Find_Task_Or_Protected_Pragma
11476 (Taskdef, Name_Storage_Size))));
11477 Etyp : constant Entity_Id := Etype (Expr_N);
11478 P : constant Node_Id := Parent (Expr_N);
11480 begin
11481 -- The stack is defined inside the corresponding record.
11482 -- Therefore if the size of the stack is set by means of
11483 -- a discriminant, we must reference the discriminant of the
11484 -- corresponding record type.
11486 if Nkind (Expr_N) in N_Has_Entity
11487 and then Present (Discriminal_Link (Entity (Expr_N)))
11488 then
11489 Task_Size :=
11490 New_Reference_To
11491 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11492 Loc);
11493 Set_Parent (Task_Size, P);
11494 Set_Etype (Task_Size, Etyp);
11495 Set_Analyzed (Task_Size);
11497 else
11498 Task_Size := Relocate_Node (Expr_N);
11499 end if;
11500 end;
11502 else
11503 Task_Size :=
11504 New_Reference_To (RTE (RE_Default_Stack_Size), Loc);
11505 end if;
11507 Decl_Stack := Make_Component_Declaration (Loc,
11508 Defining_Identifier => Ent_Stack,
11510 Component_Definition =>
11511 Make_Component_Definition (Loc,
11512 Aliased_Present => True,
11513 Subtype_Indication => Make_Subtype_Indication (Loc,
11514 Subtype_Mark =>
11515 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11517 Constraint =>
11518 Make_Index_Or_Discriminant_Constraint (Loc,
11519 Constraints => New_List (Make_Range (Loc,
11520 Low_Bound => Make_Integer_Literal (Loc, 1),
11521 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11522 Task_Size)))))));
11524 Append_To (Cdecls, Decl_Stack);
11526 -- The appropriate alignment for the stack is ensured by the run-time
11527 -- code in charge of task creation.
11529 end if;
11531 -- Add components for entry families
11533 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
11535 -- Add the _Priority component if a Priority pragma is present
11537 if Present (Taskdef) and then Has_Pragma_Priority (Taskdef) then
11538 declare
11539 Prag : constant Node_Id :=
11540 Find_Task_Or_Protected_Pragma (Taskdef, Name_Priority);
11541 Expr : Node_Id;
11543 begin
11544 Expr := First (Pragma_Argument_Associations (Prag));
11546 if Nkind (Expr) = N_Pragma_Argument_Association then
11547 Expr := Expression (Expr);
11548 end if;
11550 Expr := New_Copy_Tree (Expr);
11552 -- Add conversion to proper type to do range check if required
11553 -- Note that for runtime units, we allow out of range interrupt
11554 -- priority values to be used in a priority pragma. This is for
11555 -- the benefit of some versions of System.Interrupts which use
11556 -- a special server task with maximum interrupt priority.
11558 if Pragma_Name (Prag) = Name_Priority
11559 and then not GNAT_Mode
11560 then
11561 Rewrite (Expr, Convert_To (RTE (RE_Priority), Expr));
11562 else
11563 Rewrite (Expr, Convert_To (RTE (RE_Any_Priority), Expr));
11564 end if;
11566 Append_To (Cdecls,
11567 Make_Component_Declaration (Loc,
11568 Defining_Identifier =>
11569 Make_Defining_Identifier (Loc, Name_uPriority),
11570 Component_Definition =>
11571 Make_Component_Definition (Loc,
11572 Aliased_Present => False,
11573 Subtype_Indication => New_Reference_To (Standard_Integer,
11574 Loc)),
11575 Expression => Expr));
11576 end;
11577 end if;
11579 -- Add the _Task_Size component if a Storage_Size pragma is present
11581 if Present (Taskdef)
11582 and then Has_Storage_Size_Pragma (Taskdef)
11583 then
11584 Append_To (Cdecls,
11585 Make_Component_Declaration (Loc,
11586 Defining_Identifier =>
11587 Make_Defining_Identifier (Loc, Name_uSize),
11589 Component_Definition =>
11590 Make_Component_Definition (Loc,
11591 Aliased_Present => False,
11592 Subtype_Indication => New_Reference_To (RTE (RE_Size_Type),
11593 Loc)),
11595 Expression =>
11596 Convert_To (RTE (RE_Size_Type),
11597 Relocate_Node (
11598 Expression (First (
11599 Pragma_Argument_Associations (
11600 Find_Task_Or_Protected_Pragma
11601 (Taskdef, Name_Storage_Size))))))));
11602 end if;
11604 -- Add the _Task_Info component if a Task_Info pragma is present
11606 if Present (Taskdef) and then Has_Task_Info_Pragma (Taskdef) then
11607 Append_To (Cdecls,
11608 Make_Component_Declaration (Loc,
11609 Defining_Identifier =>
11610 Make_Defining_Identifier (Loc, Name_uTask_Info),
11612 Component_Definition =>
11613 Make_Component_Definition (Loc,
11614 Aliased_Present => False,
11615 Subtype_Indication =>
11616 New_Reference_To (RTE (RE_Task_Info_Type), Loc)),
11618 Expression => New_Copy (
11619 Expression (First (
11620 Pragma_Argument_Associations (
11621 Find_Task_Or_Protected_Pragma
11622 (Taskdef, Name_Task_Info)))))));
11623 end if;
11625 -- Add the _CPU component if a CPU pragma is present
11627 if Present (Taskdef) and then Has_Pragma_CPU (Taskdef) then
11628 Append_To (Cdecls,
11629 Make_Component_Declaration (Loc,
11630 Defining_Identifier =>
11631 Make_Defining_Identifier (Loc, Name_uCPU),
11633 Component_Definition =>
11634 Make_Component_Definition (Loc,
11635 Aliased_Present => False,
11636 Subtype_Indication =>
11637 New_Reference_To (RTE (RE_CPU_Range), Loc)),
11639 Expression => New_Copy (
11640 Expression (First (
11641 Pragma_Argument_Associations (
11642 Find_Task_Or_Protected_Pragma
11643 (Taskdef, Name_CPU)))))));
11644 end if;
11646 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
11647 -- present. If we are using a restricted run time this component will
11648 -- not be added (deadlines are not allowed by the Ravenscar profile).
11650 if not Restricted_Profile
11651 and then Present (Taskdef)
11652 and then Has_Relative_Deadline_Pragma (Taskdef)
11653 then
11654 Append_To (Cdecls,
11655 Make_Component_Declaration (Loc,
11656 Defining_Identifier =>
11657 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
11659 Component_Definition =>
11660 Make_Component_Definition (Loc,
11661 Aliased_Present => False,
11662 Subtype_Indication =>
11663 New_Reference_To (RTE (RE_Time_Span), Loc)),
11665 Expression =>
11666 Convert_To (RTE (RE_Time_Span),
11667 Relocate_Node (
11668 Expression (First (
11669 Pragma_Argument_Associations (
11670 Find_Task_Or_Protected_Pragma
11671 (Taskdef, Name_Relative_Deadline))))))));
11672 end if;
11674 -- Add the _Dispatching_Domain component if a Dispatching_Domain pragma
11675 -- or aspect is present. If we are using a restricted run time this
11676 -- component will not be added (dispatching domains are not allowed by
11677 -- the Ravenscar profile).
11679 if not Restricted_Profile
11680 and then Present (Taskdef)
11681 and then Has_Pragma_Dispatching_Domain (Taskdef)
11682 then
11683 Append_To (Cdecls,
11684 Make_Component_Declaration (Loc,
11685 Defining_Identifier =>
11686 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
11688 Component_Definition =>
11689 Make_Component_Definition (Loc,
11690 Aliased_Present => False,
11691 Subtype_Indication =>
11692 New_Reference_To
11693 (RTE (RE_Dispatching_Domain_Access), Loc)),
11695 Expression =>
11696 Unchecked_Convert_To (RTE (RE_Dispatching_Domain_Access),
11697 Relocate_Node
11698 (Expression
11699 (First
11700 (Pragma_Argument_Associations
11701 (Find_Task_Or_Protected_Pragma
11702 (Taskdef, Name_Dispatching_Domain))))))));
11703 end if;
11705 Insert_After (Size_Decl, Rec_Decl);
11707 -- Analyze the record declaration immediately after construction,
11708 -- because the initialization procedure is needed for single task
11709 -- declarations before the next entity is analyzed.
11711 Analyze (Rec_Decl);
11713 -- Create the declaration of the task body procedure
11715 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
11716 Body_Decl :=
11717 Make_Subprogram_Declaration (Loc,
11718 Specification => Proc_Spec);
11720 Insert_After (Rec_Decl, Body_Decl);
11722 -- The subprogram does not comes from source, so we have to indicate the
11723 -- need for debugging information explicitly.
11725 if Comes_From_Source (Original_Node (N)) then
11726 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
11727 end if;
11729 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
11730 -- the corresponding record has been frozen.
11732 if Ada_Version >= Ada_2005 then
11733 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
11734 end if;
11736 -- Ada 2005 (AI-345): We must defer freezing to allow further
11737 -- declaration of primitive subprograms covering task interfaces
11739 if Ada_Version <= Ada_95 then
11741 -- Now we can freeze the corresponding record. This needs manually
11742 -- freezing, since it is really part of the task type, and the task
11743 -- type is frozen at this stage. We of course need the initialization
11744 -- procedure for this corresponding record type and we won't get it
11745 -- in time if we don't freeze now.
11747 declare
11748 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
11749 begin
11750 if Is_Non_Empty_List (L) then
11751 Insert_List_After (Body_Decl, L);
11752 end if;
11753 end;
11754 end if;
11756 -- Complete the expansion of access types to the current task type, if
11757 -- any were declared.
11759 Expand_Previous_Access_Type (Tasktyp);
11761 -- Create wrappers for entries that have pre/postconditions
11763 declare
11764 Ent : Entity_Id;
11766 begin
11767 Ent := First_Entity (Tasktyp);
11768 while Present (Ent) loop
11769 if Ekind_In (Ent, E_Entry, E_Entry_Family)
11770 and then Present (Spec_PPC_List (Contract (Ent)))
11771 then
11772 Build_PPC_Wrapper (Ent, N);
11773 end if;
11775 Next_Entity (Ent);
11776 end loop;
11777 end;
11778 end Expand_N_Task_Type_Declaration;
11780 -------------------------------
11781 -- Expand_N_Timed_Entry_Call --
11782 -------------------------------
11784 -- A timed entry call in normal case is not implemented using ATC mechanism
11785 -- anymore for efficiency reason.
11787 -- select
11788 -- T.E;
11789 -- S1;
11790 -- or
11791 -- Delay D;
11792 -- S2;
11793 -- end select;
11795 -- is expanded as follow:
11797 -- 1) When T.E is a task entry_call;
11799 -- declare
11800 -- B : Boolean;
11801 -- X : Task_Entry_Index := <entry index>;
11802 -- DX : Duration := To_Duration (D);
11803 -- M : Delay_Mode := <discriminant>;
11804 -- P : parms := (parm, parm, parm);
11806 -- begin
11807 -- Timed_Protected_Entry_Call
11808 -- (<acceptor-task>, X, P'Address, DX, M, B);
11809 -- if B then
11810 -- S1;
11811 -- else
11812 -- S2;
11813 -- end if;
11814 -- end;
11816 -- 2) When T.E is a protected entry_call;
11818 -- declare
11819 -- B : Boolean;
11820 -- X : Protected_Entry_Index := <entry index>;
11821 -- DX : Duration := To_Duration (D);
11822 -- M : Delay_Mode := <discriminant>;
11823 -- P : parms := (parm, parm, parm);
11825 -- begin
11826 -- Timed_Protected_Entry_Call
11827 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
11828 -- if B then
11829 -- S1;
11830 -- else
11831 -- S2;
11832 -- end if;
11833 -- end;
11835 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call;
11837 -- declare
11838 -- B : Boolean := False;
11839 -- C : Ada.Tags.Prim_Op_Kind;
11840 -- DX : Duration := To_Duration (D)
11841 -- K : Ada.Tags.Tagged_Kind :=
11842 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
11843 -- M : Integer :=...;
11844 -- P : Parameters := (Param1 .. ParamN);
11845 -- S : Integer;
11847 -- begin
11848 -- if K = Ada.Tags.TK_Limited_Tagged then
11849 -- <dispatching-call>;
11850 -- <triggering-statements>
11852 -- else
11853 -- S :=
11854 -- Ada.Tags.Get_Offset_Index
11855 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
11857 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
11859 -- if C = POK_Protected_Entry
11860 -- or else C = POK_Task_Entry
11861 -- then
11862 -- Param1 := P.Param1;
11863 -- ...
11864 -- ParamN := P.ParamN;
11865 -- end if;
11867 -- if B then
11868 -- if C = POK_Procedure
11869 -- or else C = POK_Protected_Procedure
11870 -- or else C = POK_Task_Procedure
11871 -- then
11872 -- <dispatching-call>;
11873 -- end if;
11875 -- <triggering-statements>
11876 -- else
11877 -- <timed-statements>
11878 -- end if;
11879 -- end if;
11880 -- end;
11882 -- The triggering statement and the sequence of timed statements have not
11883 -- been analyzed yet (see Analyzed_Timed_Entry_Call). They may contain
11884 -- local declarations, and therefore the copies that are made during
11885 -- expansion must be disjoint, as for any other inlining.
11887 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
11888 Loc : constant Source_Ptr := Sloc (N);
11890 Actuals : List_Id;
11891 Blk_Typ : Entity_Id;
11892 Call : Node_Id;
11893 Call_Ent : Entity_Id;
11894 Conc_Typ_Stmts : List_Id;
11895 Concval : Node_Id;
11896 D_Conv : Node_Id;
11897 D_Disc : Node_Id;
11898 D_Stat : Node_Id;
11899 D_Stats : List_Id;
11900 D_Type : Entity_Id;
11901 Decls : List_Id;
11902 Dummy : Node_Id;
11903 E_Call : Node_Id;
11904 E_Stats : List_Id;
11905 Ename : Node_Id;
11906 Formals : List_Id;
11907 Index : Node_Id;
11908 Is_Disp_Select : Boolean;
11909 Lim_Typ_Stmts : List_Id;
11910 N_Stats : List_Id;
11911 Obj : Entity_Id;
11912 Param : Node_Id;
11913 Params : List_Id;
11914 Stmt : Node_Id;
11915 Stmts : List_Id;
11916 Unpack : List_Id;
11918 B : Entity_Id; -- Call status flag
11919 C : Entity_Id; -- Call kind
11920 D : Entity_Id; -- Delay
11921 K : Entity_Id; -- Tagged kind
11922 M : Entity_Id; -- Delay mode
11923 P : Entity_Id; -- Parameter block
11924 S : Entity_Id; -- Primitive operation slot
11926 begin
11927 -- Under the Ravenscar profile, timed entry calls are excluded. An error
11928 -- was already reported on spec, so do not attempt to expand the call.
11930 if Restriction_Active (No_Select_Statements) then
11931 return;
11932 end if;
11934 E_Call := Entry_Call_Statement (Entry_Call_Alternative (N));
11935 D_Stat := Delay_Statement (Delay_Alternative (N));
11937 Process_Statements_For_Controlled_Objects (Entry_Call_Alternative (N));
11938 Process_Statements_For_Controlled_Objects (Delay_Alternative (N));
11940 -- Retrieve E_Stats and D_Stats now because the finalization machinery
11941 -- may wrap them in blocks.
11943 E_Stats := Statements (Entry_Call_Alternative (N));
11944 D_Stats := Statements (Delay_Alternative (N));
11946 -- The arguments in the call may require dynamic allocation, and the
11947 -- call statement may have been transformed into a block. The block
11948 -- may contain additional declarations for internal entities, and the
11949 -- original call is found by sequential search.
11951 if Nkind (E_Call) = N_Block_Statement then
11952 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
11953 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
11954 N_Entry_Call_Statement)
11955 loop
11956 Next (E_Call);
11957 end loop;
11958 end if;
11960 Is_Disp_Select :=
11961 Ada_Version >= Ada_2005
11962 and then Nkind (E_Call) = N_Procedure_Call_Statement;
11964 if Is_Disp_Select then
11965 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
11967 Decls := New_List;
11968 Stmts := New_List;
11970 -- Generate:
11971 -- B : Boolean := False;
11973 B := Build_B (Loc, Decls);
11975 -- Generate:
11976 -- C : Ada.Tags.Prim_Op_Kind;
11978 C := Build_C (Loc, Decls);
11980 -- Because the analysis of all statements was disabled, manually
11981 -- analyze the delay statement.
11983 Analyze (D_Stat);
11984 D_Stat := Original_Node (D_Stat);
11986 else
11987 -- Build an entry call using Simple_Entry_Call
11989 Extract_Entry (E_Call, Concval, Ename, Index);
11990 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
11992 Decls := Declarations (E_Call);
11993 Stmts := Statements (Handled_Statement_Sequence (E_Call));
11995 if No (Decls) then
11996 Decls := New_List;
11997 end if;
11999 -- Generate:
12000 -- B : Boolean;
12002 B := Make_Defining_Identifier (Loc, Name_uB);
12004 Prepend_To (Decls,
12005 Make_Object_Declaration (Loc,
12006 Defining_Identifier => B,
12007 Object_Definition => New_Reference_To (Standard_Boolean, Loc)));
12008 end if;
12010 -- Duration and mode processing
12012 D_Type := Base_Type (Etype (Expression (D_Stat)));
12014 -- Use the type of the delay expression (Calendar or Real_Time) to
12015 -- generate the appropriate conversion.
12017 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12018 D_Disc := Make_Integer_Literal (Loc, 0);
12019 D_Conv := Relocate_Node (Expression (D_Stat));
12021 elsif Is_RTE (D_Type, RO_CA_Time) then
12022 D_Disc := Make_Integer_Literal (Loc, 1);
12023 D_Conv :=
12024 Make_Function_Call (Loc,
12025 Name => New_Reference_To (RTE (RO_CA_To_Duration), Loc),
12026 Parameter_Associations =>
12027 New_List (New_Copy (Expression (D_Stat))));
12029 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12030 D_Disc := Make_Integer_Literal (Loc, 2);
12031 D_Conv :=
12032 Make_Function_Call (Loc,
12033 Name => New_Reference_To (RTE (RO_RT_To_Duration), Loc),
12034 Parameter_Associations =>
12035 New_List (New_Copy (Expression (D_Stat))));
12036 end if;
12038 D := Make_Temporary (Loc, 'D');
12040 -- Generate:
12041 -- D : Duration;
12043 Append_To (Decls,
12044 Make_Object_Declaration (Loc,
12045 Defining_Identifier => D,
12046 Object_Definition => New_Reference_To (Standard_Duration, Loc)));
12048 M := Make_Temporary (Loc, 'M');
12050 -- Generate:
12051 -- M : Integer := (0 | 1 | 2);
12053 Append_To (Decls,
12054 Make_Object_Declaration (Loc,
12055 Defining_Identifier => M,
12056 Object_Definition => New_Reference_To (Standard_Integer, Loc),
12057 Expression => D_Disc));
12059 -- Do the assignment at this stage only because the evaluation of the
12060 -- expression must not occur before (see ACVC C97302A).
12062 Append_To (Stmts,
12063 Make_Assignment_Statement (Loc,
12064 Name => New_Reference_To (D, Loc),
12065 Expression => D_Conv));
12067 -- Parameter block processing
12069 -- Manually create the parameter block for dispatching calls. In the
12070 -- case of entries, the block has already been created during the call
12071 -- to Build_Simple_Entry_Call.
12073 if Is_Disp_Select then
12075 -- Tagged kind processing, generate:
12076 -- K : Ada.Tags.Tagged_Kind :=
12077 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12079 K := Build_K (Loc, Decls, Obj);
12081 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12082 P :=
12083 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12085 -- Dispatch table slot processing, generate:
12086 -- S : Integer;
12088 S := Build_S (Loc, Decls);
12090 -- Generate:
12091 -- S := Ada.Tags.Get_Offset_Index
12092 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12094 Conc_Typ_Stmts :=
12095 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12097 -- Generate:
12098 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12100 -- where Obj is the controlling formal parameter, S is the dispatch
12101 -- table slot number of the dispatching operation, P is the wrapped
12102 -- parameter block, D is the duration, M is the duration mode, C is
12103 -- the call kind and B is the call status.
12105 Params := New_List;
12107 Append_To (Params, New_Copy_Tree (Obj));
12108 Append_To (Params, New_Reference_To (S, Loc));
12109 Append_To (Params,
12110 Make_Attribute_Reference (Loc,
12111 Prefix => New_Reference_To (P, Loc),
12112 Attribute_Name => Name_Address));
12113 Append_To (Params, New_Reference_To (D, Loc));
12114 Append_To (Params, New_Reference_To (M, Loc));
12115 Append_To (Params, New_Reference_To (C, Loc));
12116 Append_To (Params, New_Reference_To (B, Loc));
12118 Append_To (Conc_Typ_Stmts,
12119 Make_Procedure_Call_Statement (Loc,
12120 Name =>
12121 New_Reference_To
12122 (Find_Prim_Op
12123 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12124 Parameter_Associations => Params));
12126 -- Generate:
12127 -- if C = POK_Protected_Entry
12128 -- or else C = POK_Task_Entry
12129 -- then
12130 -- Param1 := P.Param1;
12131 -- ...
12132 -- ParamN := P.ParamN;
12133 -- end if;
12135 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12137 -- Generate the if statement only when the packed parameters need
12138 -- explicit assignments to their corresponding actuals.
12140 if Present (Unpack) then
12141 Append_To (Conc_Typ_Stmts,
12142 Make_If_Statement (Loc,
12144 Condition =>
12145 Make_Or_Else (Loc,
12146 Left_Opnd =>
12147 Make_Op_Eq (Loc,
12148 Left_Opnd => New_Reference_To (C, Loc),
12149 Right_Opnd =>
12150 New_Reference_To
12151 (RTE (RE_POK_Protected_Entry), Loc)),
12153 Right_Opnd =>
12154 Make_Op_Eq (Loc,
12155 Left_Opnd => New_Reference_To (C, Loc),
12156 Right_Opnd =>
12157 New_Reference_To (RTE (RE_POK_Task_Entry), Loc))),
12159 Then_Statements => Unpack));
12160 end if;
12162 -- Generate:
12164 -- if B then
12165 -- if C = POK_Procedure
12166 -- or else C = POK_Protected_Procedure
12167 -- or else C = POK_Task_Procedure
12168 -- then
12169 -- <dispatching-call>
12170 -- end if;
12171 -- <triggering-statements>
12172 -- else
12173 -- <timed-statements>
12174 -- end if;
12176 N_Stats := Copy_Separate_List (E_Stats);
12178 Prepend_To (N_Stats,
12179 Make_If_Statement (Loc,
12181 Condition =>
12182 Make_Or_Else (Loc,
12183 Left_Opnd =>
12184 Make_Op_Eq (Loc,
12185 Left_Opnd => New_Reference_To (C, Loc),
12186 Right_Opnd =>
12187 New_Reference_To (RTE (RE_POK_Procedure), Loc)),
12189 Right_Opnd =>
12190 Make_Or_Else (Loc,
12191 Left_Opnd =>
12192 Make_Op_Eq (Loc,
12193 Left_Opnd => New_Reference_To (C, Loc),
12194 Right_Opnd =>
12195 New_Reference_To (RTE (
12196 RE_POK_Protected_Procedure), Loc)),
12197 Right_Opnd =>
12198 Make_Op_Eq (Loc,
12199 Left_Opnd => New_Reference_To (C, Loc),
12200 Right_Opnd =>
12201 New_Reference_To
12202 (RTE (RE_POK_Task_Procedure), Loc)))),
12204 Then_Statements => New_List (E_Call)));
12206 Append_To (Conc_Typ_Stmts,
12207 Make_If_Statement (Loc,
12208 Condition => New_Reference_To (B, Loc),
12209 Then_Statements => N_Stats,
12210 Else_Statements => D_Stats));
12212 -- Generate:
12213 -- <dispatching-call>;
12214 -- <triggering-statements>
12216 Lim_Typ_Stmts := Copy_Separate_List (E_Stats);
12217 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (E_Call));
12219 -- Generate:
12220 -- if K = Ada.Tags.TK_Limited_Tagged then
12221 -- Lim_Typ_Stmts
12222 -- else
12223 -- Conc_Typ_Stmts
12224 -- end if;
12226 Append_To (Stmts,
12227 Make_If_Statement (Loc,
12228 Condition =>
12229 Make_Op_Eq (Loc,
12230 Left_Opnd => New_Reference_To (K, Loc),
12231 Right_Opnd =>
12232 New_Reference_To (RTE (RE_TK_Limited_Tagged), Loc)),
12233 Then_Statements => Lim_Typ_Stmts,
12234 Else_Statements => Conc_Typ_Stmts));
12236 else
12237 -- Skip assignments to temporaries created for in-out parameters.
12238 -- This makes unwarranted assumptions about the shape of the expanded
12239 -- tree for the call, and should be cleaned up ???
12241 Stmt := First (Stmts);
12242 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12243 Next (Stmt);
12244 end loop;
12246 -- Do the assignment at this stage only because the evaluation
12247 -- of the expression must not occur before (see ACVC C97302A).
12249 Insert_Before (Stmt,
12250 Make_Assignment_Statement (Loc,
12251 Name => New_Reference_To (D, Loc),
12252 Expression => D_Conv));
12254 Call := Stmt;
12255 Params := Parameter_Associations (Call);
12257 -- For a protected type, we build a Timed_Protected_Entry_Call
12259 if Is_Protected_Type (Etype (Concval)) then
12261 -- Create a new call statement
12263 Param := First (Params);
12264 while Present (Param)
12265 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12266 loop
12267 Next (Param);
12268 end loop;
12270 Dummy := Remove_Next (Next (Param));
12272 -- Remove garbage is following the Cancel_Param if present
12274 Dummy := Next (Param);
12276 -- Remove the mode of the Protected_Entry_Call call, then remove
12277 -- the Communication_Block of the Protected_Entry_Call call, and
12278 -- finally add Duration and a Delay_Mode parameter
12280 pragma Assert (Present (Param));
12281 Rewrite (Param, New_Reference_To (D, Loc));
12283 Rewrite (Dummy, New_Reference_To (M, Loc));
12285 -- Add a Boolean flag for successful entry call
12287 Append_To (Params, New_Reference_To (B, Loc));
12289 case Corresponding_Runtime_Package (Etype (Concval)) is
12290 when System_Tasking_Protected_Objects_Entries =>
12291 Rewrite (Call,
12292 Make_Procedure_Call_Statement (Loc,
12293 Name =>
12294 New_Reference_To
12295 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12296 Parameter_Associations => Params));
12298 when System_Tasking_Protected_Objects_Single_Entry =>
12299 Param := First (Params);
12300 while Present (Param)
12301 and then not
12302 Is_RTE (Etype (Param), RE_Protected_Entry_Index)
12303 loop
12304 Next (Param);
12305 end loop;
12307 Remove (Param);
12309 Rewrite (Call,
12310 Make_Procedure_Call_Statement (Loc,
12311 Name =>
12312 New_Reference_To
12313 (RTE (RE_Timed_Protected_Single_Entry_Call), Loc),
12314 Parameter_Associations => Params));
12316 when others =>
12317 raise Program_Error;
12318 end case;
12320 -- For the task case, build a Timed_Task_Entry_Call
12322 else
12323 -- Create a new call statement
12325 Append_To (Params, New_Reference_To (D, Loc));
12326 Append_To (Params, New_Reference_To (M, Loc));
12327 Append_To (Params, New_Reference_To (B, Loc));
12329 Rewrite (Call,
12330 Make_Procedure_Call_Statement (Loc,
12331 Name =>
12332 New_Reference_To (RTE (RE_Timed_Task_Entry_Call), Loc),
12333 Parameter_Associations => Params));
12334 end if;
12336 Append_To (Stmts,
12337 Make_Implicit_If_Statement (N,
12338 Condition => New_Reference_To (B, Loc),
12339 Then_Statements => E_Stats,
12340 Else_Statements => D_Stats));
12341 end if;
12343 Rewrite (N,
12344 Make_Block_Statement (Loc,
12345 Declarations => Decls,
12346 Handled_Statement_Sequence =>
12347 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12349 Analyze (N);
12350 end Expand_N_Timed_Entry_Call;
12352 ----------------------------------------
12353 -- Expand_Protected_Body_Declarations --
12354 ----------------------------------------
12356 procedure Expand_Protected_Body_Declarations
12357 (N : Node_Id;
12358 Spec_Id : Entity_Id)
12360 begin
12361 if No_Run_Time_Mode then
12362 Error_Msg_CRT ("protected body", N);
12363 return;
12365 elsif Full_Expander_Active then
12367 -- Associate discriminals with the first subprogram or entry body to
12368 -- be expanded.
12370 if Present (First_Protected_Operation (Declarations (N))) then
12371 Set_Discriminals (Parent (Spec_Id));
12372 end if;
12373 end if;
12374 end Expand_Protected_Body_Declarations;
12376 -------------------------
12377 -- External_Subprogram --
12378 -------------------------
12380 function External_Subprogram (E : Entity_Id) return Entity_Id is
12381 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12383 begin
12384 -- The internal and external subprograms follow each other on the entity
12385 -- chain. Note that previously private operations had no separate
12386 -- external subprogram. We now create one in all cases, because a
12387 -- private operation may actually appear in an external call, through
12388 -- a 'Access reference used for a callback.
12390 -- If the operation is a function that returns an anonymous access type,
12391 -- the corresponding itype appears before the operation, and must be
12392 -- skipped.
12394 -- This mechanism is fragile, there should be a real link between the
12395 -- two versions of the operation, but there is no place to put it ???
12397 if Is_Access_Type (Next_Entity (Subp)) then
12398 return Next_Entity (Next_Entity (Subp));
12399 else
12400 return Next_Entity (Subp);
12401 end if;
12402 end External_Subprogram;
12404 ------------------------------
12405 -- Extract_Dispatching_Call --
12406 ------------------------------
12408 procedure Extract_Dispatching_Call
12409 (N : Node_Id;
12410 Call_Ent : out Entity_Id;
12411 Object : out Entity_Id;
12412 Actuals : out List_Id;
12413 Formals : out List_Id)
12415 Call_Nam : Node_Id;
12417 begin
12418 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12420 if Present (Original_Node (N)) then
12421 Call_Nam := Name (Original_Node (N));
12422 else
12423 Call_Nam := Name (N);
12424 end if;
12426 -- Retrieve the name of the dispatching procedure. It contains the
12427 -- dispatch table slot number.
12429 loop
12430 case Nkind (Call_Nam) is
12431 when N_Identifier =>
12432 exit;
12434 when N_Selected_Component =>
12435 Call_Nam := Selector_Name (Call_Nam);
12437 when others =>
12438 raise Program_Error;
12440 end case;
12441 end loop;
12443 Actuals := Parameter_Associations (N);
12444 Call_Ent := Entity (Call_Nam);
12445 Formals := Parameter_Specifications (Parent (Call_Ent));
12446 Object := First (Actuals);
12448 if Present (Original_Node (Object)) then
12449 Object := Original_Node (Object);
12450 end if;
12452 -- If the type of the dispatching object is an access type then return
12453 -- an explicit dereference.
12455 if Is_Access_Type (Etype (Object)) then
12456 Object := Make_Explicit_Dereference (Sloc (N), Object);
12457 Analyze (Object);
12458 end if;
12459 end Extract_Dispatching_Call;
12461 -------------------
12462 -- Extract_Entry --
12463 -------------------
12465 procedure Extract_Entry
12466 (N : Node_Id;
12467 Concval : out Node_Id;
12468 Ename : out Node_Id;
12469 Index : out Node_Id)
12471 Nam : constant Node_Id := Name (N);
12473 begin
12474 -- For a simple entry, the name is a selected component, with the
12475 -- prefix being the task value, and the selector being the entry.
12477 if Nkind (Nam) = N_Selected_Component then
12478 Concval := Prefix (Nam);
12479 Ename := Selector_Name (Nam);
12480 Index := Empty;
12482 -- For a member of an entry family, the name is an indexed component
12483 -- where the prefix is a selected component, whose prefix in turn is
12484 -- the task value, and whose selector is the entry family. The single
12485 -- expression in the expressions list of the indexed component is the
12486 -- subscript for the family.
12488 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12489 Concval := Prefix (Prefix (Nam));
12490 Ename := Selector_Name (Prefix (Nam));
12491 Index := First (Expressions (Nam));
12492 end if;
12493 end Extract_Entry;
12495 -------------------
12496 -- Family_Offset --
12497 -------------------
12499 function Family_Offset
12500 (Loc : Source_Ptr;
12501 Hi : Node_Id;
12502 Lo : Node_Id;
12503 Ttyp : Entity_Id;
12504 Cap : Boolean) return Node_Id
12506 Ityp : Entity_Id;
12507 Real_Hi : Node_Id;
12508 Real_Lo : Node_Id;
12510 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12511 -- If one of the bounds is a reference to a discriminant, replace with
12512 -- corresponding discriminal of type. Within the body of a task retrieve
12513 -- the renamed discriminant by simple visibility, using its generated
12514 -- name. Within a protected object, find the original discriminant and
12515 -- replace it with the discriminal of the current protected operation.
12517 ------------------------------
12518 -- Convert_Discriminant_Ref --
12519 ------------------------------
12521 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
12522 Loc : constant Source_Ptr := Sloc (Bound);
12523 B : Node_Id;
12524 D : Entity_Id;
12526 begin
12527 if Is_Entity_Name (Bound)
12528 and then Ekind (Entity (Bound)) = E_Discriminant
12529 then
12530 if Is_Task_Type (Ttyp)
12531 and then Has_Completion (Ttyp)
12532 then
12533 B := Make_Identifier (Loc, Chars (Entity (Bound)));
12534 Find_Direct_Name (B);
12536 elsif Is_Protected_Type (Ttyp) then
12537 D := First_Discriminant (Ttyp);
12538 while Chars (D) /= Chars (Entity (Bound)) loop
12539 Next_Discriminant (D);
12540 end loop;
12542 B := New_Reference_To (Discriminal (D), Loc);
12544 else
12545 B := New_Reference_To (Discriminal (Entity (Bound)), Loc);
12546 end if;
12548 elsif Nkind (Bound) = N_Attribute_Reference then
12549 return Bound;
12551 else
12552 B := New_Copy_Tree (Bound);
12553 end if;
12555 return
12556 Make_Attribute_Reference (Loc,
12557 Attribute_Name => Name_Pos,
12558 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
12559 Expressions => New_List (B));
12560 end Convert_Discriminant_Ref;
12562 -- Start of processing for Family_Offset
12564 begin
12565 Real_Hi := Convert_Discriminant_Ref (Hi);
12566 Real_Lo := Convert_Discriminant_Ref (Lo);
12568 if Cap then
12569 if Is_Task_Type (Ttyp) then
12570 Ityp := RTE (RE_Task_Entry_Index);
12571 else
12572 Ityp := RTE (RE_Protected_Entry_Index);
12573 end if;
12575 Real_Hi :=
12576 Make_Attribute_Reference (Loc,
12577 Prefix => New_Reference_To (Ityp, Loc),
12578 Attribute_Name => Name_Min,
12579 Expressions => New_List (
12580 Real_Hi,
12581 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
12583 Real_Lo :=
12584 Make_Attribute_Reference (Loc,
12585 Prefix => New_Reference_To (Ityp, Loc),
12586 Attribute_Name => Name_Max,
12587 Expressions => New_List (
12588 Real_Lo,
12589 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
12590 end if;
12592 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
12593 end Family_Offset;
12595 -----------------
12596 -- Family_Size --
12597 -----------------
12599 function Family_Size
12600 (Loc : Source_Ptr;
12601 Hi : Node_Id;
12602 Lo : Node_Id;
12603 Ttyp : Entity_Id;
12604 Cap : Boolean) return Node_Id
12606 Ityp : Entity_Id;
12608 begin
12609 if Is_Task_Type (Ttyp) then
12610 Ityp := RTE (RE_Task_Entry_Index);
12611 else
12612 Ityp := RTE (RE_Protected_Entry_Index);
12613 end if;
12615 return
12616 Make_Attribute_Reference (Loc,
12617 Prefix => New_Reference_To (Ityp, Loc),
12618 Attribute_Name => Name_Max,
12619 Expressions => New_List (
12620 Make_Op_Add (Loc,
12621 Left_Opnd =>
12622 Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
12623 Right_Opnd =>
12624 Make_Integer_Literal (Loc, 1)),
12625 Make_Integer_Literal (Loc, 0)));
12626 end Family_Size;
12628 ----------------------------
12629 -- Find_Enclosing_Context --
12630 ----------------------------
12632 procedure Find_Enclosing_Context
12633 (N : Node_Id;
12634 Context : out Node_Id;
12635 Context_Id : out Entity_Id;
12636 Context_Decls : out List_Id)
12638 begin
12639 -- Traverse the parent chain looking for an enclosing body, block,
12640 -- package or return statement.
12642 Context := Parent (N);
12643 while not Nkind_In (Context, N_Block_Statement,
12644 N_Entry_Body,
12645 N_Extended_Return_Statement,
12646 N_Package_Body,
12647 N_Package_Declaration,
12648 N_Subprogram_Body,
12649 N_Task_Body)
12650 loop
12651 Context := Parent (Context);
12652 end loop;
12654 -- Extract the constituents of the context
12656 if Nkind (Context) = N_Extended_Return_Statement then
12657 Context_Decls := Return_Object_Declarations (Context);
12658 Context_Id := Return_Statement_Entity (Context);
12660 -- Package declarations and bodies use a common library-level activation
12661 -- chain or task master, therefore return the package declaration as the
12662 -- proper carrier for the appropriate flag.
12664 elsif Nkind (Context) = N_Package_Body then
12665 Context_Decls := Declarations (Context);
12666 Context_Id := Corresponding_Spec (Context);
12667 Context := Parent (Context_Id);
12669 if Nkind (Context) = N_Defining_Program_Unit_Name then
12670 Context := Parent (Parent (Context));
12671 else
12672 Context := Parent (Context);
12673 end if;
12675 elsif Nkind (Context) = N_Package_Declaration then
12676 Context_Decls := Visible_Declarations (Specification (Context));
12677 Context_Id := Defining_Unit_Name (Specification (Context));
12679 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
12680 Context_Id := Defining_Identifier (Context_Id);
12681 end if;
12683 else
12684 Context_Decls := Declarations (Context);
12686 if Nkind (Context) = N_Block_Statement then
12687 Context_Id := Entity (Identifier (Context));
12689 elsif Nkind (Context) = N_Entry_Body then
12690 Context_Id := Defining_Identifier (Context);
12692 elsif Nkind (Context) = N_Subprogram_Body then
12693 if Present (Corresponding_Spec (Context)) then
12694 Context_Id := Corresponding_Spec (Context);
12695 else
12696 Context_Id := Defining_Unit_Name (Specification (Context));
12698 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
12699 Context_Id := Defining_Identifier (Context_Id);
12700 end if;
12701 end if;
12703 elsif Nkind (Context) = N_Task_Body then
12704 Context_Id := Corresponding_Spec (Context);
12706 else
12707 raise Program_Error;
12708 end if;
12709 end if;
12711 pragma Assert (Present (Context));
12712 pragma Assert (Present (Context_Id));
12713 pragma Assert (Present (Context_Decls));
12714 end Find_Enclosing_Context;
12716 -----------------------
12717 -- Find_Master_Scope --
12718 -----------------------
12720 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
12721 S : Entity_Id;
12723 begin
12724 -- In Ada 2005, the master is the innermost enclosing scope that is not
12725 -- transient. If the enclosing block is the rewriting of a call or the
12726 -- scope is an extended return statement this is valid master. The
12727 -- master in an extended return is only used within the return, and is
12728 -- subsequently overwritten in Move_Activation_Chain, but it must exist
12729 -- now before that overwriting occurs.
12731 S := Scope (E);
12733 if Ada_Version >= Ada_2005 then
12734 while Is_Internal (S) loop
12735 if Nkind (Parent (S)) = N_Block_Statement
12736 and then
12737 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
12738 then
12739 exit;
12741 elsif Ekind (S) = E_Return_Statement then
12742 exit;
12744 else
12745 S := Scope (S);
12746 end if;
12747 end loop;
12748 end if;
12750 return S;
12751 end Find_Master_Scope;
12753 -----------------------------------
12754 -- Find_Task_Or_Protected_Pragma --
12755 -----------------------------------
12757 function Find_Task_Or_Protected_Pragma
12758 (T : Node_Id;
12759 P : Name_Id) return Node_Id
12761 N : Node_Id;
12763 begin
12764 N := First (Visible_Declarations (T));
12765 while Present (N) loop
12766 if Nkind (N) = N_Pragma then
12767 if Pragma_Name (N) = P then
12768 return N;
12770 elsif P = Name_Priority
12771 and then Pragma_Name (N) = Name_Interrupt_Priority
12772 then
12773 return N;
12775 else
12776 Next (N);
12777 end if;
12779 else
12780 Next (N);
12781 end if;
12782 end loop;
12784 N := First (Private_Declarations (T));
12785 while Present (N) loop
12786 if Nkind (N) = N_Pragma then
12787 if Pragma_Name (N) = P then
12788 return N;
12790 elsif P = Name_Priority
12791 and then Pragma_Name (N) = Name_Interrupt_Priority
12792 then
12793 return N;
12795 else
12796 Next (N);
12797 end if;
12799 else
12800 Next (N);
12801 end if;
12802 end loop;
12804 raise Program_Error;
12805 end Find_Task_Or_Protected_Pragma;
12807 -------------------------------
12808 -- First_Protected_Operation --
12809 -------------------------------
12811 function First_Protected_Operation (D : List_Id) return Node_Id is
12812 First_Op : Node_Id;
12814 begin
12815 First_Op := First (D);
12816 while Present (First_Op)
12817 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
12818 loop
12819 Next (First_Op);
12820 end loop;
12822 return First_Op;
12823 end First_Protected_Operation;
12825 ---------------------------------------
12826 -- Install_Private_Data_Declarations --
12827 ---------------------------------------
12829 procedure Install_Private_Data_Declarations
12830 (Loc : Source_Ptr;
12831 Spec_Id : Entity_Id;
12832 Conc_Typ : Entity_Id;
12833 Body_Nod : Node_Id;
12834 Decls : List_Id;
12835 Barrier : Boolean := False;
12836 Family : Boolean := False)
12838 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
12839 Decl : Node_Id;
12840 Def : Node_Id;
12841 Insert_Node : Node_Id := Empty;
12842 Obj_Ent : Entity_Id;
12844 procedure Add (Decl : Node_Id);
12845 -- Add a single declaration after Insert_Node. If this is the first
12846 -- addition, Decl is added to the front of Decls and it becomes the
12847 -- insertion node.
12849 function Replace_Bound (Bound : Node_Id) return Node_Id;
12850 -- The bounds of an entry index may depend on discriminants, create a
12851 -- reference to the corresponding prival. Otherwise return a duplicate
12852 -- of the original bound.
12854 ---------
12855 -- Add --
12856 ---------
12858 procedure Add (Decl : Node_Id) is
12859 begin
12860 if No (Insert_Node) then
12861 Prepend_To (Decls, Decl);
12862 else
12863 Insert_After (Insert_Node, Decl);
12864 end if;
12866 Insert_Node := Decl;
12867 end Add;
12869 --------------------------
12870 -- Replace_Discriminant --
12871 --------------------------
12873 function Replace_Bound (Bound : Node_Id) return Node_Id is
12874 begin
12875 if Nkind (Bound) = N_Identifier
12876 and then Is_Discriminal (Entity (Bound))
12877 then
12878 return Make_Identifier (Loc, Chars (Entity (Bound)));
12879 else
12880 return Duplicate_Subexpr (Bound);
12881 end if;
12882 end Replace_Bound;
12884 -- Start of processing for Install_Private_Data_Declarations
12886 begin
12887 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
12888 -- formal parameter _O, _object or _task depending on the context.
12890 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
12892 -- Special processing of _O for barrier functions, protected entries
12893 -- and families.
12895 if Barrier
12896 or else
12897 (Is_Protected
12898 and then
12899 (Ekind (Spec_Id) = E_Entry
12900 or else Ekind (Spec_Id) = E_Entry_Family))
12901 then
12902 declare
12903 Conc_Rec : constant Entity_Id :=
12904 Corresponding_Record_Type (Conc_Typ);
12905 Typ_Id : constant Entity_Id :=
12906 Make_Defining_Identifier (Loc,
12907 New_External_Name (Chars (Conc_Rec), 'P'));
12908 begin
12909 -- Generate:
12910 -- type prot_typVP is access prot_typV;
12912 Decl :=
12913 Make_Full_Type_Declaration (Loc,
12914 Defining_Identifier => Typ_Id,
12915 Type_Definition =>
12916 Make_Access_To_Object_Definition (Loc,
12917 Subtype_Indication =>
12918 New_Reference_To (Conc_Rec, Loc)));
12919 Add (Decl);
12921 -- Generate:
12922 -- _object : prot_typVP := prot_typV (_O);
12924 Decl :=
12925 Make_Object_Declaration (Loc,
12926 Defining_Identifier =>
12927 Make_Defining_Identifier (Loc, Name_uObject),
12928 Object_Definition => New_Reference_To (Typ_Id, Loc),
12929 Expression =>
12930 Unchecked_Convert_To (Typ_Id,
12931 New_Reference_To (Obj_Ent, Loc)));
12932 Add (Decl);
12934 -- Set the reference to the concurrent object
12936 Obj_Ent := Defining_Identifier (Decl);
12937 end;
12938 end if;
12940 -- Step 2: Create the Protection object and build its declaration for
12941 -- any protected entry (family) of subprogram. Note for the lock-free
12942 -- implementation, the Protection object is not needed anymore.
12944 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
12945 declare
12946 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
12947 Prot_Typ : RE_Id;
12949 begin
12950 Set_Protection_Object (Spec_Id, Prot_Ent);
12952 -- Determine the proper protection type
12954 if Has_Attach_Handler (Conc_Typ)
12955 and then not Restricted_Profile
12956 and then not Restriction_Active (No_Dynamic_Attachment)
12957 then
12958 Prot_Typ := RE_Static_Interrupt_Protection;
12960 elsif Has_Interrupt_Handler (Conc_Typ)
12961 and then not Restriction_Active (No_Dynamic_Attachment)
12962 then
12963 Prot_Typ := RE_Dynamic_Interrupt_Protection;
12965 -- The type has explicit entries or generated primitive entry
12966 -- wrappers.
12968 elsif Has_Entries (Conc_Typ)
12969 or else
12970 (Ada_Version >= Ada_2005
12971 and then Present (Interface_List (Parent (Conc_Typ))))
12972 then
12973 case Corresponding_Runtime_Package (Conc_Typ) is
12974 when System_Tasking_Protected_Objects_Entries =>
12975 Prot_Typ := RE_Protection_Entries;
12977 when System_Tasking_Protected_Objects_Single_Entry =>
12978 Prot_Typ := RE_Protection_Entry;
12980 when others =>
12981 raise Program_Error;
12982 end case;
12984 else
12985 Prot_Typ := RE_Protection;
12986 end if;
12988 -- Generate:
12989 -- conc_typR : protection_typ renames _object._object;
12991 Decl :=
12992 Make_Object_Renaming_Declaration (Loc,
12993 Defining_Identifier => Prot_Ent,
12994 Subtype_Mark =>
12995 New_Reference_To (RTE (Prot_Typ), Loc),
12996 Name =>
12997 Make_Selected_Component (Loc,
12998 Prefix => New_Reference_To (Obj_Ent, Loc),
12999 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13000 Add (Decl);
13001 end;
13002 end if;
13004 -- Step 3: Add discriminant renamings (if any)
13006 if Has_Discriminants (Conc_Typ) then
13007 declare
13008 D : Entity_Id;
13010 begin
13011 D := First_Discriminant (Conc_Typ);
13012 while Present (D) loop
13014 -- Adjust the source location
13016 Set_Sloc (Discriminal (D), Loc);
13018 -- Generate:
13019 -- discr_name : discr_typ renames _object.discr_name;
13020 -- or
13021 -- discr_name : discr_typ renames _task.discr_name;
13023 Decl :=
13024 Make_Object_Renaming_Declaration (Loc,
13025 Defining_Identifier => Discriminal (D),
13026 Subtype_Mark => New_Reference_To (Etype (D), Loc),
13027 Name =>
13028 Make_Selected_Component (Loc,
13029 Prefix => New_Reference_To (Obj_Ent, Loc),
13030 Selector_Name => Make_Identifier (Loc, Chars (D))));
13031 Add (Decl);
13033 Next_Discriminant (D);
13034 end loop;
13035 end;
13036 end if;
13038 -- Step 4: Add private component renamings (if any)
13040 if Is_Protected then
13041 Def := Protected_Definition (Parent (Conc_Typ));
13043 if Present (Private_Declarations (Def)) then
13044 declare
13045 Comp : Node_Id;
13046 Comp_Id : Entity_Id;
13047 Decl_Id : Entity_Id;
13049 begin
13050 Comp := First (Private_Declarations (Def));
13051 while Present (Comp) loop
13052 if Nkind (Comp) = N_Component_Declaration then
13053 Comp_Id := Defining_Identifier (Comp);
13054 Decl_Id :=
13055 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13057 -- Minimal decoration
13059 if Ekind (Spec_Id) = E_Function then
13060 Set_Ekind (Decl_Id, E_Constant);
13061 else
13062 Set_Ekind (Decl_Id, E_Variable);
13063 end if;
13065 Set_Prival (Comp_Id, Decl_Id);
13066 Set_Prival_Link (Decl_Id, Comp_Id);
13067 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13069 -- Generate:
13070 -- comp_name : comp_typ renames _object.comp_name;
13072 Decl :=
13073 Make_Object_Renaming_Declaration (Loc,
13074 Defining_Identifier => Decl_Id,
13075 Subtype_Mark =>
13076 New_Reference_To (Etype (Comp_Id), Loc),
13077 Name =>
13078 Make_Selected_Component (Loc,
13079 Prefix =>
13080 New_Reference_To (Obj_Ent, Loc),
13081 Selector_Name =>
13082 Make_Identifier (Loc, Chars (Comp_Id))));
13083 Add (Decl);
13084 end if;
13086 Next (Comp);
13087 end loop;
13088 end;
13089 end if;
13090 end if;
13092 -- Step 5: Add the declaration of the entry index and the associated
13093 -- type for barrier functions and entry families.
13095 if (Barrier and then Family)
13096 or else Ekind (Spec_Id) = E_Entry_Family
13097 then
13098 declare
13099 E : constant Entity_Id := Index_Object (Spec_Id);
13100 Index : constant Entity_Id :=
13101 Defining_Identifier (
13102 Entry_Index_Specification (
13103 Entry_Body_Formal_Part (Body_Nod)));
13104 Index_Con : constant Entity_Id :=
13105 Make_Defining_Identifier (Loc, Chars (Index));
13106 High : Node_Id;
13107 Index_Typ : Entity_Id;
13108 Low : Node_Id;
13110 begin
13111 -- Minimal decoration
13113 Set_Ekind (Index_Con, E_Constant);
13114 Set_Entry_Index_Constant (Index, Index_Con);
13115 Set_Discriminal_Link (Index_Con, Index);
13117 -- Retrieve the bounds of the entry family
13119 High := Type_High_Bound (Etype (Index));
13120 Low := Type_Low_Bound (Etype (Index));
13122 -- In the simple case the entry family is given by a subtype
13123 -- mark and the index constant has the same type.
13125 if Is_Entity_Name (Original_Node (
13126 Discrete_Subtype_Definition (Parent (Index))))
13127 then
13128 Index_Typ := Etype (Index);
13130 -- Otherwise a new subtype declaration is required
13132 else
13133 High := Replace_Bound (High);
13134 Low := Replace_Bound (Low);
13136 Index_Typ := Make_Temporary (Loc, 'J');
13138 -- Generate:
13139 -- subtype Jnn is <Etype of Index> range Low .. High;
13141 Decl :=
13142 Make_Subtype_Declaration (Loc,
13143 Defining_Identifier => Index_Typ,
13144 Subtype_Indication =>
13145 Make_Subtype_Indication (Loc,
13146 Subtype_Mark =>
13147 New_Reference_To (Base_Type (Etype (Index)), Loc),
13148 Constraint =>
13149 Make_Range_Constraint (Loc,
13150 Range_Expression =>
13151 Make_Range (Loc, Low, High))));
13152 Add (Decl);
13153 end if;
13155 Set_Etype (Index_Con, Index_Typ);
13157 -- Create the object which designates the index:
13158 -- J : constant Jnn :=
13159 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13161 -- where Jnn is the subtype created above or the original type of
13162 -- the index, _E is a formal of the protected body subprogram and
13163 -- <index expr> is the index of the first family member.
13165 Decl :=
13166 Make_Object_Declaration (Loc,
13167 Defining_Identifier => Index_Con,
13168 Constant_Present => True,
13169 Object_Definition =>
13170 New_Reference_To (Index_Typ, Loc),
13172 Expression =>
13173 Make_Attribute_Reference (Loc,
13174 Prefix =>
13175 New_Reference_To (Index_Typ, Loc),
13176 Attribute_Name => Name_Val,
13178 Expressions => New_List (
13180 Make_Op_Add (Loc,
13181 Left_Opnd =>
13182 Make_Op_Subtract (Loc,
13183 Left_Opnd =>
13184 New_Reference_To (E, Loc),
13185 Right_Opnd =>
13186 Entry_Index_Expression (Loc,
13187 Defining_Identifier (Body_Nod),
13188 Empty, Conc_Typ)),
13190 Right_Opnd =>
13191 Make_Attribute_Reference (Loc,
13192 Prefix =>
13193 New_Reference_To (Index_Typ, Loc),
13194 Attribute_Name => Name_Pos,
13195 Expressions => New_List (
13196 Make_Attribute_Reference (Loc,
13197 Prefix =>
13198 New_Reference_To (Index_Typ, Loc),
13199 Attribute_Name => Name_First)))))));
13200 Add (Decl);
13201 end;
13202 end if;
13203 end Install_Private_Data_Declarations;
13205 -----------------------
13206 -- Is_Exception_Safe --
13207 -----------------------
13209 function Is_Exception_Safe (Subprogram : Node_Id) return Boolean is
13211 function Has_Side_Effect (N : Node_Id) return Boolean;
13212 -- Return True whenever encountering a subprogram call or raise
13213 -- statement of any kind in the sequence of statements
13215 ---------------------
13216 -- Has_Side_Effect --
13217 ---------------------
13219 -- What is this doing buried two levels down in exp_ch9. It seems like a
13220 -- generally useful function, and indeed there may be code duplication
13221 -- going on here ???
13223 function Has_Side_Effect (N : Node_Id) return Boolean is
13224 Stmt : Node_Id;
13225 Expr : Node_Id;
13227 function Is_Call_Or_Raise (N : Node_Id) return Boolean;
13228 -- Indicate whether N is a subprogram call or a raise statement
13230 ----------------------
13231 -- Is_Call_Or_Raise --
13232 ----------------------
13234 function Is_Call_Or_Raise (N : Node_Id) return Boolean is
13235 begin
13236 return Nkind_In (N, N_Procedure_Call_Statement,
13237 N_Function_Call,
13238 N_Raise_Statement,
13239 N_Raise_Constraint_Error,
13240 N_Raise_Program_Error,
13241 N_Raise_Storage_Error);
13242 end Is_Call_Or_Raise;
13244 -- Start of processing for Has_Side_Effect
13246 begin
13247 Stmt := N;
13248 while Present (Stmt) loop
13249 if Is_Call_Or_Raise (Stmt) then
13250 return True;
13251 end if;
13253 -- An object declaration can also contain a function call or a
13254 -- raise statement.
13256 if Nkind (Stmt) = N_Object_Declaration then
13257 Expr := Expression (Stmt);
13259 if Present (Expr) and then Is_Call_Or_Raise (Expr) then
13260 return True;
13261 end if;
13262 end if;
13264 Next (Stmt);
13265 end loop;
13267 return False;
13268 end Has_Side_Effect;
13270 -- Start of processing for Is_Exception_Safe
13272 begin
13273 -- If the checks handled by the back end are not disabled, we cannot
13274 -- ensure that no exception will be raised.
13276 if not Access_Checks_Suppressed (Empty)
13277 or else not Discriminant_Checks_Suppressed (Empty)
13278 or else not Range_Checks_Suppressed (Empty)
13279 or else not Index_Checks_Suppressed (Empty)
13280 or else Opt.Stack_Checking_Enabled
13281 then
13282 return False;
13283 end if;
13285 if Has_Side_Effect (First (Declarations (Subprogram)))
13286 or else
13287 Has_Side_Effect
13288 (First (Statements (Handled_Statement_Sequence (Subprogram))))
13289 then
13290 return False;
13291 else
13292 return True;
13293 end if;
13294 end Is_Exception_Safe;
13296 ---------------------------------
13297 -- Is_Potentially_Large_Family --
13298 ---------------------------------
13300 function Is_Potentially_Large_Family
13301 (Base_Index : Entity_Id;
13302 Conctyp : Entity_Id;
13303 Lo : Node_Id;
13304 Hi : Node_Id) return Boolean
13306 begin
13307 return Scope (Base_Index) = Standard_Standard
13308 and then Base_Index = Base_Type (Standard_Integer)
13309 and then Has_Discriminants (Conctyp)
13310 and then
13311 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13312 and then
13313 (Denotes_Discriminant (Lo, True)
13314 or else
13315 Denotes_Discriminant (Hi, True));
13316 end Is_Potentially_Large_Family;
13318 -------------------------------------
13319 -- Is_Private_Primitive_Subprogram --
13320 -------------------------------------
13322 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13323 begin
13324 return
13325 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13326 and then Is_Private_Primitive (Id);
13327 end Is_Private_Primitive_Subprogram;
13329 ------------------
13330 -- Index_Object --
13331 ------------------
13333 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13334 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13335 Formal : Entity_Id;
13337 begin
13338 Formal := First_Formal (Bod_Subp);
13339 while Present (Formal) loop
13341 -- Look for formal parameter _E
13343 if Chars (Formal) = Name_uE then
13344 return Formal;
13345 end if;
13347 Next_Formal (Formal);
13348 end loop;
13350 -- A protected body subprogram should always have the parameter in
13351 -- question.
13353 raise Program_Error;
13354 end Index_Object;
13356 --------------------------------
13357 -- Make_Initialize_Protection --
13358 --------------------------------
13360 function Make_Initialize_Protection
13361 (Protect_Rec : Entity_Id) return List_Id
13363 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13364 P_Arr : Entity_Id;
13365 Pdef : Node_Id;
13366 Pdec : Node_Id;
13367 Ptyp : constant Node_Id :=
13368 Corresponding_Concurrent_Type (Protect_Rec);
13369 Args : List_Id;
13370 L : constant List_Id := New_List;
13371 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13372 Restricted : constant Boolean := Restricted_Profile;
13374 begin
13375 -- We may need two calls to properly initialize the object, one to
13376 -- Initialize_Protection, and possibly one to Install_Handlers if we
13377 -- have a pragma Attach_Handler.
13379 -- Get protected declaration. In the case of a task type declaration,
13380 -- this is simply the parent of the protected type entity. In the single
13381 -- protected object declaration, this parent will be the implicit type,
13382 -- and we can find the corresponding single protected object declaration
13383 -- by searching forward in the declaration list in the tree.
13385 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13386 -- of this type should have been removed during semantic analysis.
13388 Pdec := Parent (Ptyp);
13389 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13390 N_Single_Protected_Declaration)
13391 loop
13392 Next (Pdec);
13393 end loop;
13395 -- Now we can find the object definition from this declaration
13397 Pdef := Protected_Definition (Pdec);
13399 -- Build the parameter list for the call. Note that _Init is the name
13400 -- of the formal for the object to be initialized, which is the task
13401 -- value record itself.
13403 Args := New_List;
13405 -- For lock-free implementation, skip initializations of the Protection
13406 -- object.
13408 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13409 -- Object parameter. This is a pointer to the object of type
13410 -- Protection used by the GNARL to control the protected object.
13412 Append_To (Args,
13413 Make_Attribute_Reference (Loc,
13414 Prefix =>
13415 Make_Selected_Component (Loc,
13416 Prefix => Make_Identifier (Loc, Name_uInit),
13417 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13418 Attribute_Name => Name_Unchecked_Access));
13420 -- Priority parameter. Set to Unspecified_Priority unless there is a
13421 -- priority pragma, in which case we take the value from the pragma,
13422 -- or there is an interrupt pragma and no priority pragma, and we
13423 -- set the ceiling to Interrupt_Priority'Last, an implementation-
13424 -- defined value, see D.3(10).
13426 if Present (Pdef)
13427 and then Has_Pragma_Priority (Pdef)
13428 then
13429 declare
13430 Prio : constant Node_Id :=
13431 Expression
13432 (First
13433 (Pragma_Argument_Associations
13434 (Find_Task_Or_Protected_Pragma
13435 (Pdef, Name_Priority))));
13436 Temp : Entity_Id;
13438 begin
13439 -- If priority is a static expression, then we can duplicate it
13440 -- with no problem and simply append it to the argument list.
13442 if Is_Static_Expression (Prio) then
13443 Append_To (Args,
13444 Duplicate_Subexpr_No_Checks (Prio));
13446 -- Otherwise, the priority may be a per-object expression, if
13447 -- it depends on a discriminant of the type. In this case,
13448 -- create local variable to capture the expression. Note that
13449 -- it is really necessary to create this variable explicitly.
13450 -- It might be thought that removing side effects would the
13451 -- appropriate approach, but that could generate declarations
13452 -- improperly placed in the enclosing scope.
13454 -- Note: Use System.Any_Priority as the expected type for the
13455 -- non-static priority expression, in case the expression has
13456 -- not been analyzed yet (as occurs for example with pragma
13457 -- Interrupt_Priority).
13459 else
13460 Temp := Make_Temporary (Loc, 'R', Prio);
13461 Append_To (L,
13462 Make_Object_Declaration (Loc,
13463 Defining_Identifier => Temp,
13464 Object_Definition =>
13465 New_Occurrence_Of (RTE (RE_Any_Priority), Loc),
13466 Expression => Relocate_Node (Prio)));
13468 Append_To (Args, New_Occurrence_Of (Temp, Loc));
13469 end if;
13470 end;
13472 -- When no priority is specified but an xx_Handler pragma is, we
13473 -- default to System.Interrupts.Default_Interrupt_Priority, see
13474 -- D.3(10).
13476 elsif Has_Attach_Handler (Ptyp)
13477 or else Has_Interrupt_Handler (Ptyp)
13478 then
13479 Append_To (Args,
13480 New_Reference_To (RTE (RE_Default_Interrupt_Priority), Loc));
13482 -- Normal case, no priority or xx_Handler specified, default priority
13484 else
13485 Append_To (Args,
13486 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
13487 end if;
13489 -- Test for Compiler_Info parameter. This parameter allows entry body
13490 -- procedures and barrier functions to be called from the runtime. It
13491 -- is a pointer to the record generated by the compiler to represent
13492 -- the protected object.
13494 -- A protected type without entries that covers an interface and
13495 -- overrides the abstract routines with protected procedures is
13496 -- considered equivalent to a protected type with entries in the
13497 -- context of dispatching select statements.
13499 if Has_Entry
13500 or else Has_Interfaces (Protect_Rec)
13501 or else
13502 ((Has_Attach_Handler (Ptyp) or else Has_Interrupt_Handler (Ptyp))
13503 and then not Restriction_Active (No_Dynamic_Attachment))
13504 then
13505 declare
13506 Pkg_Id : constant RTU_Id :=
13507 Corresponding_Runtime_Package (Ptyp);
13509 Called_Subp : RE_Id;
13511 begin
13512 case Pkg_Id is
13513 when System_Tasking_Protected_Objects_Entries =>
13514 Called_Subp := RE_Initialize_Protection_Entries;
13516 when System_Tasking_Protected_Objects =>
13517 Called_Subp := RE_Initialize_Protection;
13519 when System_Tasking_Protected_Objects_Single_Entry =>
13520 Called_Subp := RE_Initialize_Protection_Entry;
13522 when others =>
13523 raise Program_Error;
13524 end case;
13526 if Has_Entry
13527 or else not Restricted
13528 or else Has_Interfaces (Protect_Rec)
13529 then
13530 Append_To (Args,
13531 Make_Attribute_Reference (Loc,
13532 Prefix => Make_Identifier (Loc, Name_uInit),
13533 Attribute_Name => Name_Address));
13534 end if;
13536 -- Entry_Bodies parameter. This is a pointer to an array of
13537 -- pointers to the entry body procedures and barrier functions
13538 -- of the object. If the protected type has no entries this
13539 -- object will not exist, in this case, pass a null.
13541 if Has_Entry then
13542 P_Arr := Entry_Bodies_Array (Ptyp);
13544 Append_To (Args,
13545 Make_Attribute_Reference (Loc,
13546 Prefix => New_Reference_To (P_Arr, Loc),
13547 Attribute_Name => Name_Unrestricted_Access));
13549 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13551 -- Find index mapping function (clumsy but ok for now)
13553 while Ekind (P_Arr) /= E_Function loop
13554 Next_Entity (P_Arr);
13555 end loop;
13557 Append_To (Args,
13558 Make_Attribute_Reference (Loc,
13559 Prefix => New_Reference_To (P_Arr, Loc),
13560 Attribute_Name => Name_Unrestricted_Access));
13562 -- Build_Entry_Names generation flag. When set to true,
13563 -- the runtime will allocate an array to hold the string
13564 -- names of protected entries.
13566 if not Restricted_Profile then
13567 if Entry_Names_OK then
13568 Append_To (Args,
13569 New_Reference_To (Standard_True, Loc));
13570 else
13571 Append_To (Args,
13572 New_Reference_To (Standard_False, Loc));
13573 end if;
13574 end if;
13575 end if;
13577 elsif Pkg_Id =
13578 System_Tasking_Protected_Objects_Single_Entry
13579 then
13580 Append_To (Args, Make_Null (Loc));
13582 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13583 Append_To (Args, Make_Null (Loc));
13584 Append_To (Args, Make_Null (Loc));
13585 Append_To (Args, New_Reference_To (Standard_False, Loc));
13586 end if;
13588 Append_To (L,
13589 Make_Procedure_Call_Statement (Loc,
13590 Name => New_Reference_To (RTE (Called_Subp), Loc),
13591 Parameter_Associations => Args));
13592 end;
13593 else
13594 Append_To (L,
13595 Make_Procedure_Call_Statement (Loc,
13596 Name => New_Reference_To (RTE (RE_Initialize_Protection), Loc),
13597 Parameter_Associations => Args));
13598 end if;
13599 end if;
13601 if Has_Attach_Handler (Ptyp) then
13603 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
13604 -- make the following call:
13606 -- Install_Handlers (_object,
13607 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13609 -- or, in the case of Ravenscar:
13611 -- Install_Restricted_Handlers
13612 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13614 declare
13615 Args : constant List_Id := New_List;
13616 Table : constant List_Id := New_List;
13617 Ritem : Node_Id := First_Rep_Item (Ptyp);
13619 begin
13620 -- Build the Attach_Handler table argument
13622 while Present (Ritem) loop
13623 if Nkind (Ritem) = N_Pragma
13624 and then Pragma_Name (Ritem) = Name_Attach_Handler
13625 then
13626 declare
13627 Handler : constant Node_Id :=
13628 First (Pragma_Argument_Associations (Ritem));
13630 Interrupt : constant Node_Id := Next (Handler);
13631 Expr : constant Node_Id := Expression (Interrupt);
13633 begin
13634 Append_To (Table,
13635 Make_Aggregate (Loc, Expressions => New_List (
13636 Unchecked_Convert_To
13637 (RTE (RE_System_Interrupt_Id), Expr),
13638 Make_Attribute_Reference (Loc,
13639 Prefix => Make_Selected_Component (Loc,
13640 Make_Identifier (Loc, Name_uInit),
13641 Duplicate_Subexpr_No_Checks
13642 (Expression (Handler))),
13643 Attribute_Name => Name_Access))));
13644 end;
13645 end if;
13647 Next_Rep_Item (Ritem);
13648 end loop;
13650 -- Append the table argument we just built
13652 Append_To (Args, Make_Aggregate (Loc, Table));
13654 -- Append the Install_Handlers (or Install_Restricted_Handlers)
13655 -- call to the statements.
13657 if Restricted then
13658 -- Call a simplified version of Install_Handlers to be used
13659 -- when the Ravenscar restrictions are in effect
13660 -- (Install_Restricted_Handlers).
13662 Append_To (L,
13663 Make_Procedure_Call_Statement (Loc,
13664 Name =>
13665 New_Reference_To
13666 (RTE (RE_Install_Restricted_Handlers), Loc),
13667 Parameter_Associations => Args));
13669 else
13670 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13671 -- First, prepends the _object argument
13673 Prepend_To (Args,
13674 Make_Attribute_Reference (Loc,
13675 Prefix =>
13676 Make_Selected_Component (Loc,
13677 Prefix => Make_Identifier (Loc, Name_uInit),
13678 Selector_Name =>
13679 Make_Identifier (Loc, Name_uObject)),
13680 Attribute_Name => Name_Unchecked_Access));
13681 end if;
13683 -- Then, insert call to Install_Handlers
13685 Append_To (L,
13686 Make_Procedure_Call_Statement (Loc,
13687 Name => New_Reference_To (RTE (RE_Install_Handlers), Loc),
13688 Parameter_Associations => Args));
13689 end if;
13690 end;
13691 end if;
13693 return L;
13694 end Make_Initialize_Protection;
13696 ---------------------------
13697 -- Make_Task_Create_Call --
13698 ---------------------------
13700 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
13701 Loc : constant Source_Ptr := Sloc (Task_Rec);
13702 Args : List_Id;
13703 Ecount : Node_Id;
13704 Name : Node_Id;
13705 Tdec : Node_Id;
13706 Tdef : Node_Id;
13707 Tnam : Name_Id;
13708 Ttyp : Node_Id;
13710 begin
13711 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
13712 Tnam := Chars (Ttyp);
13714 -- Get task declaration. In the case of a task type declaration, this is
13715 -- simply the parent of the task type entity. In the single task
13716 -- declaration, this parent will be the implicit type, and we can find
13717 -- the corresponding single task declaration by searching forward in the
13718 -- declaration list in the tree.
13720 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
13721 -- this type should have been removed during semantic analysis.
13723 Tdec := Parent (Ttyp);
13724 while not Nkind_In (Tdec, N_Task_Type_Declaration,
13725 N_Single_Task_Declaration)
13726 loop
13727 Next (Tdec);
13728 end loop;
13730 -- Now we can find the task definition from this declaration
13732 Tdef := Task_Definition (Tdec);
13734 -- Build the parameter list for the call. Note that _Init is the name
13735 -- of the formal for the object to be initialized, which is the task
13736 -- value record itself.
13738 Args := New_List;
13740 -- Priority parameter. Set to Unspecified_Priority unless there is a
13741 -- priority pragma, in which case we take the value from the pragma.
13743 if Present (Tdef) and then Has_Pragma_Priority (Tdef) then
13744 Append_To (Args,
13745 Make_Selected_Component (Loc,
13746 Prefix => Make_Identifier (Loc, Name_uInit),
13747 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
13748 else
13749 Append_To (Args,
13750 New_Reference_To (RTE (RE_Unspecified_Priority), Loc));
13751 end if;
13753 -- Optional Stack parameter
13755 if Restricted_Profile then
13757 -- If the stack has been preallocated by the expander then
13758 -- pass its address. Otherwise, pass a null address.
13760 if Preallocated_Stacks_On_Target then
13761 Append_To (Args,
13762 Make_Attribute_Reference (Loc,
13763 Prefix =>
13764 Make_Selected_Component (Loc,
13765 Prefix => Make_Identifier (Loc, Name_uInit),
13766 Selector_Name => Make_Identifier (Loc, Name_uStack)),
13767 Attribute_Name => Name_Address));
13769 else
13770 Append_To (Args,
13771 New_Reference_To (RTE (RE_Null_Address), Loc));
13772 end if;
13773 end if;
13775 -- Size parameter. If no Storage_Size pragma is present, then
13776 -- the size is taken from the taskZ variable for the type, which
13777 -- is either Unspecified_Size, or has been reset by the use of
13778 -- a Storage_Size attribute definition clause. If a pragma is
13779 -- present, then the size is taken from the _Size field of the
13780 -- task value record, which was set from the pragma value.
13782 if Present (Tdef)
13783 and then Has_Storage_Size_Pragma (Tdef)
13784 then
13785 Append_To (Args,
13786 Make_Selected_Component (Loc,
13787 Prefix => Make_Identifier (Loc, Name_uInit),
13788 Selector_Name => Make_Identifier (Loc, Name_uSize)));
13790 else
13791 Append_To (Args,
13792 New_Reference_To (Storage_Size_Variable (Ttyp), Loc));
13793 end if;
13795 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
13796 -- Task_Info pragma, in which case we take the value from the pragma.
13798 if Present (Tdef)
13799 and then Has_Task_Info_Pragma (Tdef)
13800 then
13801 Append_To (Args,
13802 Make_Selected_Component (Loc,
13803 Prefix => Make_Identifier (Loc, Name_uInit),
13804 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
13806 else
13807 Append_To (Args,
13808 New_Reference_To (RTE (RE_Unspecified_Task_Info), Loc));
13809 end if;
13811 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU pragma,
13812 -- in which case we take the value from the pragma. The parameter is
13813 -- passed as an Integer because in the case of unspecified CPU the
13814 -- value is not in the range of CPU_Range.
13816 if Present (Tdef) and then Has_Pragma_CPU (Tdef) then
13817 Append_To (Args,
13818 Convert_To (Standard_Integer,
13819 Make_Selected_Component (Loc,
13820 Prefix => Make_Identifier (Loc, Name_uInit),
13821 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
13823 else
13824 Append_To (Args,
13825 New_Reference_To (RTE (RE_Unspecified_CPU), Loc));
13826 end if;
13828 if not Restricted_Profile then
13830 -- Deadline parameter. If no Relative_Deadline pragma is present,
13831 -- then the deadline is Time_Span_Zero. If a pragma is present, then
13832 -- the deadline is taken from the _Relative_Deadline field of the
13833 -- task value record, which was set from the pragma value. Note that
13834 -- this parameter must not be generated for the restricted profiles
13835 -- since Ravenscar does not allow deadlines.
13837 -- Case where pragma Relative_Deadline applies: use given value
13839 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
13840 Append_To (Args,
13841 Make_Selected_Component (Loc,
13842 Prefix =>
13843 Make_Identifier (Loc, Name_uInit),
13844 Selector_Name =>
13845 Make_Identifier (Loc, Name_uRelative_Deadline)));
13847 -- No pragma Relative_Deadline apply to the task
13849 else
13850 Append_To (Args,
13851 New_Reference_To (RTE (RE_Time_Span_Zero), Loc));
13852 end if;
13854 -- Dispatching_Domain parameter. If no Dispatching_Domain pragma or
13855 -- aspect is present, then the dispatching domain is null. If a
13856 -- pragma or aspect is present, then the dispatching domain is taken
13857 -- from the _Dispatching_Domain field of the task value record,
13858 -- which was set from the pragma value. Note that this parameter
13859 -- must not be generated for the restricted profiles since Ravenscar
13860 -- does not allow dispatching domains.
13862 -- Case where pragma or aspect Dispatching_Domain applies: use given
13863 -- value.
13865 if Present (Tdef) and then Has_Pragma_Dispatching_Domain (Tdef) then
13866 Append_To (Args,
13867 Make_Selected_Component (Loc,
13868 Prefix =>
13869 Make_Identifier (Loc, Name_uInit),
13870 Selector_Name =>
13871 Make_Identifier (Loc, Name_uDispatching_Domain)));
13873 -- No pragma or aspect Dispatching_Domain apply to the task
13875 else
13876 Append_To (Args, Make_Null (Loc));
13877 end if;
13879 -- Number of entries. This is an expression of the form:
13881 -- n + _Init.a'Length + _Init.a'B'Length + ...
13883 -- where a,b... are the entry family names for the task definition
13885 Ecount :=
13886 Build_Entry_Count_Expression
13887 (Ttyp,
13888 Component_Items
13889 (Component_List
13890 (Type_Definition
13891 (Parent (Corresponding_Record_Type (Ttyp))))),
13892 Loc);
13893 Append_To (Args, Ecount);
13895 -- Master parameter. This is a reference to the _Master parameter of
13896 -- the initialization procedure, except in the case of the pragma
13897 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
13898 -- System.Tasking.Library_Task_Level.
13900 if Restriction_Active (No_Task_Hierarchy) = False then
13901 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
13902 else
13903 Append_To (Args,
13904 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
13905 end if;
13906 end if;
13908 -- State parameter. This is a pointer to the task body procedure. The
13909 -- required value is obtained by taking 'Unrestricted_Access of the task
13910 -- body procedure and converting it (with an unchecked conversion) to
13911 -- the type required by the task kernel. For further details, see the
13912 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
13913 -- than 'Address in order to avoid creating trampolines.
13915 declare
13916 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
13917 Subp_Ptr_Typ : constant Node_Id :=
13918 Create_Itype (E_Access_Subprogram_Type, Tdec);
13919 Ref : constant Node_Id := Make_Itype_Reference (Loc);
13921 begin
13922 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
13923 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
13925 -- Be sure to freeze a reference to the access-to-subprogram type,
13926 -- otherwise gigi will complain that it's in the wrong scope, because
13927 -- it's actually inside the init procedure for the record type that
13928 -- corresponds to the task type.
13930 -- This processing is causing a crash in the .NET/JVM back ends that
13931 -- is not yet understood, so skip it in these cases ???
13933 if VM_Target = No_VM then
13934 Set_Itype (Ref, Subp_Ptr_Typ);
13935 Append_Freeze_Action (Task_Rec, Ref);
13937 Append_To (Args,
13938 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
13939 Make_Qualified_Expression (Loc,
13940 Subtype_Mark => New_Reference_To (Subp_Ptr_Typ, Loc),
13941 Expression =>
13942 Make_Attribute_Reference (Loc,
13943 Prefix =>
13944 New_Occurrence_Of (Body_Proc, Loc),
13945 Attribute_Name => Name_Unrestricted_Access))));
13947 -- For the .NET/JVM cases revert to the original code below ???
13949 else
13950 Append_To (Args,
13951 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
13952 Make_Attribute_Reference (Loc,
13953 Prefix =>
13954 New_Occurrence_Of (Body_Proc, Loc),
13955 Attribute_Name => Name_Address)));
13956 end if;
13957 end;
13959 -- Discriminants parameter. This is just the address of the task
13960 -- value record itself (which contains the discriminant values
13962 Append_To (Args,
13963 Make_Attribute_Reference (Loc,
13964 Prefix => Make_Identifier (Loc, Name_uInit),
13965 Attribute_Name => Name_Address));
13967 -- Elaborated parameter. This is an access to the elaboration Boolean
13969 Append_To (Args,
13970 Make_Attribute_Reference (Loc,
13971 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
13972 Attribute_Name => Name_Unchecked_Access));
13974 -- Chain parameter. This is a reference to the _Chain parameter of
13975 -- the initialization procedure.
13977 Append_To (Args, Make_Identifier (Loc, Name_uChain));
13979 -- Task name parameter. Take this from the _Task_Id parameter to the
13980 -- init call unless there is a Task_Name pragma, in which case we take
13981 -- the value from the pragma.
13983 if Present (Tdef)
13984 and then Has_Task_Name_Pragma (Tdef)
13985 then
13986 -- Copy expression in full, because it may be dynamic and have
13987 -- side effects.
13989 Append_To (Args,
13990 New_Copy_Tree
13991 (Expression (First
13992 (Pragma_Argument_Associations
13993 (Find_Task_Or_Protected_Pragma
13994 (Tdef, Name_Task_Name))))));
13996 else
13997 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
13998 end if;
14000 -- Created_Task parameter. This is the _Task_Id field of the task
14001 -- record value
14003 Append_To (Args,
14004 Make_Selected_Component (Loc,
14005 Prefix => Make_Identifier (Loc, Name_uInit),
14006 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14008 -- Build_Entry_Names generation flag. When set to true, the runtime
14009 -- will allocate an array to hold the string names of task entries.
14011 if not Restricted_Profile then
14012 if Has_Entries (Ttyp)
14013 and then Entry_Names_OK
14014 then
14015 Append_To (Args, New_Reference_To (Standard_True, Loc));
14016 else
14017 Append_To (Args, New_Reference_To (Standard_False, Loc));
14018 end if;
14019 end if;
14021 if Restricted_Profile then
14022 Name := New_Reference_To (RTE (RE_Create_Restricted_Task), Loc);
14023 else
14024 Name := New_Reference_To (RTE (RE_Create_Task), Loc);
14025 end if;
14027 return
14028 Make_Procedure_Call_Statement (Loc,
14029 Name => Name,
14030 Parameter_Associations => Args);
14031 end Make_Task_Create_Call;
14033 ------------------------------
14034 -- Next_Protected_Operation --
14035 ------------------------------
14037 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14038 Next_Op : Node_Id;
14040 begin
14041 Next_Op := Next (N);
14042 while Present (Next_Op)
14043 and then not Nkind_In (Next_Op, N_Subprogram_Body, N_Entry_Body)
14044 loop
14045 Next (Next_Op);
14046 end loop;
14048 return Next_Op;
14049 end Next_Protected_Operation;
14051 ---------------------
14052 -- Null_Statements --
14053 ---------------------
14055 function Null_Statements (Stats : List_Id) return Boolean is
14056 Stmt : Node_Id;
14058 begin
14059 Stmt := First (Stats);
14060 while Nkind (Stmt) /= N_Empty
14061 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14062 or else
14063 (Nkind (Stmt) = N_Pragma
14064 and then (Pragma_Name (Stmt) = Name_Unreferenced
14065 or else
14066 Pragma_Name (Stmt) = Name_Unmodified
14067 or else
14068 Pragma_Name (Stmt) = Name_Warnings)))
14069 loop
14070 Next (Stmt);
14071 end loop;
14073 return Nkind (Stmt) = N_Empty;
14074 end Null_Statements;
14076 --------------------------
14077 -- Parameter_Block_Pack --
14078 --------------------------
14080 function Parameter_Block_Pack
14081 (Loc : Source_Ptr;
14082 Blk_Typ : Entity_Id;
14083 Actuals : List_Id;
14084 Formals : List_Id;
14085 Decls : List_Id;
14086 Stmts : List_Id) return Node_Id
14088 Actual : Entity_Id;
14089 Expr : Node_Id := Empty;
14090 Formal : Entity_Id;
14091 Has_Param : Boolean := False;
14092 P : Entity_Id;
14093 Params : List_Id;
14094 Temp_Asn : Node_Id;
14095 Temp_Nam : Node_Id;
14097 begin
14098 Actual := First (Actuals);
14099 Formal := Defining_Identifier (First (Formals));
14100 Params := New_List;
14102 while Present (Actual) loop
14103 if Is_By_Copy_Type (Etype (Actual)) then
14104 -- Generate:
14105 -- Jnn : aliased <formal-type>
14107 Temp_Nam := Make_Temporary (Loc, 'J');
14109 Append_To (Decls,
14110 Make_Object_Declaration (Loc,
14111 Aliased_Present =>
14112 True,
14113 Defining_Identifier =>
14114 Temp_Nam,
14115 Object_Definition =>
14116 New_Reference_To (Etype (Formal), Loc)));
14118 if Ekind (Formal) /= E_Out_Parameter then
14120 -- Generate:
14121 -- Jnn := <actual>
14123 Temp_Asn :=
14124 New_Reference_To (Temp_Nam, Loc);
14126 Set_Assignment_OK (Temp_Asn);
14128 Append_To (Stmts,
14129 Make_Assignment_Statement (Loc,
14130 Name =>
14131 Temp_Asn,
14132 Expression =>
14133 New_Copy_Tree (Actual)));
14134 end if;
14136 -- Generate:
14137 -- Jnn'unchecked_access
14139 Append_To (Params,
14140 Make_Attribute_Reference (Loc,
14141 Attribute_Name =>
14142 Name_Unchecked_Access,
14143 Prefix =>
14144 New_Reference_To (Temp_Nam, Loc)));
14146 Has_Param := True;
14148 -- The controlling parameter is omitted
14150 else
14151 if not Is_Controlling_Actual (Actual) then
14152 Append_To (Params,
14153 Make_Reference (Loc, New_Copy_Tree (Actual)));
14155 Has_Param := True;
14156 end if;
14157 end if;
14159 Next_Actual (Actual);
14160 Next_Formal_With_Extras (Formal);
14161 end loop;
14163 if Has_Param then
14164 Expr := Make_Aggregate (Loc, Params);
14165 end if;
14167 -- Generate:
14168 -- P : Ann := (
14169 -- J1'unchecked_access;
14170 -- <actual2>'reference;
14171 -- ...);
14173 P := Make_Temporary (Loc, 'P');
14175 Append_To (Decls,
14176 Make_Object_Declaration (Loc,
14177 Defining_Identifier =>
14179 Object_Definition =>
14180 New_Reference_To (Blk_Typ, Loc),
14181 Expression =>
14182 Expr));
14184 return P;
14185 end Parameter_Block_Pack;
14187 ----------------------------
14188 -- Parameter_Block_Unpack --
14189 ----------------------------
14191 function Parameter_Block_Unpack
14192 (Loc : Source_Ptr;
14193 P : Entity_Id;
14194 Actuals : List_Id;
14195 Formals : List_Id) return List_Id
14197 Actual : Entity_Id;
14198 Asnmt : Node_Id;
14199 Formal : Entity_Id;
14200 Has_Asnmt : Boolean := False;
14201 Result : constant List_Id := New_List;
14203 begin
14204 Actual := First (Actuals);
14205 Formal := Defining_Identifier (First (Formals));
14206 while Present (Actual) loop
14207 if Is_By_Copy_Type (Etype (Actual))
14208 and then Ekind (Formal) /= E_In_Parameter
14209 then
14210 -- Generate:
14211 -- <actual> := P.<formal>;
14213 Asnmt :=
14214 Make_Assignment_Statement (Loc,
14215 Name =>
14216 New_Copy (Actual),
14217 Expression =>
14218 Make_Explicit_Dereference (Loc,
14219 Make_Selected_Component (Loc,
14220 Prefix =>
14221 New_Reference_To (P, Loc),
14222 Selector_Name =>
14223 Make_Identifier (Loc, Chars (Formal)))));
14225 Set_Assignment_OK (Name (Asnmt));
14226 Append_To (Result, Asnmt);
14228 Has_Asnmt := True;
14229 end if;
14231 Next_Actual (Actual);
14232 Next_Formal_With_Extras (Formal);
14233 end loop;
14235 if Has_Asnmt then
14236 return Result;
14237 else
14238 return New_List (Make_Null_Statement (Loc));
14239 end if;
14240 end Parameter_Block_Unpack;
14242 ----------------------
14243 -- Set_Discriminals --
14244 ----------------------
14246 procedure Set_Discriminals (Dec : Node_Id) is
14247 D : Entity_Id;
14248 Pdef : Entity_Id;
14249 D_Minal : Entity_Id;
14251 begin
14252 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14253 Pdef := Defining_Identifier (Dec);
14255 if Has_Discriminants (Pdef) then
14256 D := First_Discriminant (Pdef);
14257 while Present (D) loop
14258 D_Minal :=
14259 Make_Defining_Identifier (Sloc (D),
14260 Chars => New_External_Name (Chars (D), 'D'));
14262 Set_Ekind (D_Minal, E_Constant);
14263 Set_Etype (D_Minal, Etype (D));
14264 Set_Scope (D_Minal, Pdef);
14265 Set_Discriminal (D, D_Minal);
14266 Set_Discriminal_Link (D_Minal, D);
14268 Next_Discriminant (D);
14269 end loop;
14270 end if;
14271 end Set_Discriminals;
14273 -----------------------
14274 -- Trivial_Accept_OK --
14275 -----------------------
14277 function Trivial_Accept_OK return Boolean is
14278 begin
14279 case Opt.Task_Dispatching_Policy is
14281 -- If we have the default task dispatching policy in effect, we can
14282 -- definitely do the optimization (one way of looking at this is to
14283 -- think of the formal definition of the default policy being allowed
14284 -- to run any task it likes after a rendezvous, so even if notionally
14285 -- a full rescheduling occurs, we can say that our dispatching policy
14286 -- (i.e. the default dispatching policy) reorders the queue to be the
14287 -- same as just before the call.
14289 when ' ' =>
14290 return True;
14292 -- FIFO_Within_Priorities certainly does not permit this
14293 -- optimization since the Rendezvous is a scheduling action that may
14294 -- require some other task to be run.
14296 when 'F' =>
14297 return False;
14299 -- For now, disallow the optimization for all other policies. This
14300 -- may be over-conservative, but it is certainly not incorrect.
14302 when others =>
14303 return False;
14305 end case;
14306 end Trivial_Accept_OK;
14308 end Exp_Ch9;