* lto.c (do_stream_out): Add PART parameter; open dump file.
[official-gcc.git] / gcc / ada / exp_ch9.adb
blobea03fe219a9dcea8725f8341c4536119516f4e6a
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-2018, 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 Einfo; use Einfo;
28 with Elists; use Elists;
29 with Errout; use Errout;
30 with Exp_Ch3; use Exp_Ch3;
31 with Exp_Ch6; use Exp_Ch6;
32 with Exp_Ch11; use Exp_Ch11;
33 with Exp_Dbug; use Exp_Dbug;
34 with Exp_Sel; use Exp_Sel;
35 with Exp_Smem; use Exp_Smem;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Hostparm;
40 with Itypes; use Itypes;
41 with Namet; use Namet;
42 with Nlists; use Nlists;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Restrict; use Restrict;
46 with Rident; use Rident;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Ch9; use Sem_Ch9;
53 with Sem_Ch11; use Sem_Ch11;
54 with Sem_Elab; use Sem_Elab;
55 with Sem_Eval; use Sem_Eval;
56 with Sem_Res; use Sem_Res;
57 with Sem_Util; use Sem_Util;
58 with Sinfo; use Sinfo;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Targparm; use Targparm;
62 with Tbuild; use Tbuild;
63 with Uintp; use Uintp;
64 with Validsw; use Validsw;
66 package body Exp_Ch9 is
68 -- The following constant establishes the upper bound for the index of
69 -- an entry family. It is used to limit the allocated size of protected
70 -- types with defaulted discriminant of an integer type, when the bound
71 -- of some entry family depends on a discriminant. The limitation to entry
72 -- families of 128K should be reasonable in all cases, and is a documented
73 -- implementation restriction.
75 Entry_Family_Bound : constant Pos := 2**16;
77 -----------------------
78 -- Local Subprograms --
79 -----------------------
81 function Actual_Index_Expression
82 (Sloc : Source_Ptr;
83 Ent : Entity_Id;
84 Index : Node_Id;
85 Tsk : Entity_Id) return Node_Id;
86 -- Compute the index position for an entry call. Tsk is the target task. If
87 -- the bounds of some entry family depend on discriminants, the expression
88 -- computed by this function uses the discriminants of the target task.
90 procedure Add_Object_Pointer
91 (Loc : Source_Ptr;
92 Conc_Typ : Entity_Id;
93 Decls : List_Id);
94 -- Prepend an object pointer declaration to the declaration list Decls.
95 -- This object pointer is initialized to a type conversion of the System.
96 -- Address pointer passed to entry barrier functions and entry body
97 -- procedures.
99 procedure Add_Formal_Renamings
100 (Spec : Node_Id;
101 Decls : List_Id;
102 Ent : Entity_Id;
103 Loc : Source_Ptr);
104 -- Create renaming declarations for the formals, inside the procedure that
105 -- implements an entry body. The renamings make the original names of the
106 -- formals accessible to gdb, and serve no other purpose.
107 -- Spec is the specification of the procedure being built.
108 -- Decls is the list of declarations to be enhanced.
109 -- Ent is the entity for the original entry body.
111 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
112 -- Transform accept statement into a block with added exception handler.
113 -- Used both for simple accept statements and for accept alternatives in
114 -- select statements. Astat is the accept statement.
116 function Build_Barrier_Function
117 (N : Node_Id;
118 Ent : Entity_Id;
119 Pid : Node_Id) return Node_Id;
120 -- Build the function body returning the value of the barrier expression
121 -- for the specified entry body.
123 function Build_Barrier_Function_Specification
124 (Loc : Source_Ptr;
125 Def_Id : Entity_Id) return Node_Id;
126 -- Build a specification for a function implementing the protected entry
127 -- barrier of the specified entry body.
129 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id);
130 -- Build the body of a wrapper procedure for an entry or entry family that
131 -- has contract cases, preconditions, or postconditions. The body gathers
132 -- the executable contract items and expands them in the usual way, and
133 -- performs the entry call itself. This way preconditions are evaluated
134 -- before the call is queued. E is the entry in question, and Decl is the
135 -- enclosing synchronized type declaration at whose freeze point the
136 -- generated body is analyzed.
138 function Build_Corresponding_Record
139 (N : Node_Id;
140 Ctyp : Node_Id;
141 Loc : Source_Ptr) return Node_Id;
142 -- Common to tasks and protected types. Copy discriminant specifications,
143 -- build record declaration. N is the type declaration, Ctyp is the
144 -- concurrent entity (task type or protected type).
146 function Build_Dispatching_Tag_Check
147 (K : Entity_Id;
148 N : Node_Id) return Node_Id;
149 -- Utility to create the tree to check whether the dispatching call in
150 -- a timed entry call, a conditional entry call, or an asynchronous
151 -- transfer of control is a call to a primitive of a non-synchronized type.
152 -- K is the temporary that holds the tagged kind of the target object, and
153 -- N is the enclosing construct.
155 function Build_Entry_Count_Expression
156 (Concurrent_Type : Node_Id;
157 Component_List : List_Id;
158 Loc : Source_Ptr) return Node_Id;
159 -- Compute number of entries for concurrent object. This is a count of
160 -- simple entries, followed by an expression that computes the length
161 -- of the range of each entry family. A single array with that size is
162 -- allocated for each concurrent object of the type.
164 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
165 -- Build the function that translates the entry index in the call
166 -- (which depends on the size of entry families) into an index into the
167 -- Entry_Bodies_Array, to determine the body and barrier function used
168 -- in a protected entry call. A pointer to this function appears in every
169 -- protected object.
171 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
172 -- Build subprogram declaration for previous one
174 function Build_Lock_Free_Protected_Subprogram_Body
175 (N : Node_Id;
176 Prot_Typ : Node_Id;
177 Unprot_Spec : Node_Id) return Node_Id;
178 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
179 -- the subprogram specification of the unprotected version of N. Transform
180 -- N such that it invokes the unprotected version of the body.
182 function Build_Lock_Free_Unprotected_Subprogram_Body
183 (N : Node_Id;
184 Prot_Typ : Node_Id) return Node_Id;
185 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
186 -- of N where the original statements of N are synchronized through atomic
187 -- actions such as compare and exchange. Prior to invoking this routine, it
188 -- has been established that N can be implemented in a lock-free fashion.
190 function Build_Parameter_Block
191 (Loc : Source_Ptr;
192 Actuals : List_Id;
193 Formals : List_Id;
194 Decls : List_Id) return Entity_Id;
195 -- Generate an access type for each actual parameter in the list Actuals.
196 -- Create an encapsulating record that contains all the actuals and return
197 -- its type. Generate:
198 -- type Ann1 is access all <actual1-type>
199 -- ...
200 -- type AnnN is access all <actualN-type>
201 -- type Pnn is record
202 -- <formal1> : Ann1;
203 -- ...
204 -- <formalN> : AnnN;
205 -- end record;
207 function Build_Protected_Entry
208 (N : Node_Id;
209 Ent : Entity_Id;
210 Pid : Node_Id) return Node_Id;
211 -- Build the procedure implementing the statement sequence of the specified
212 -- entry body.
214 function Build_Protected_Entry_Specification
215 (Loc : Source_Ptr;
216 Def_Id : Entity_Id;
217 Ent_Id : Entity_Id) return Node_Id;
218 -- Build a specification for the procedure implementing the statements of
219 -- the specified entry body. Add attributes associating it with the entry
220 -- defining identifier Ent_Id.
222 function Build_Protected_Spec
223 (N : Node_Id;
224 Obj_Type : Entity_Id;
225 Ident : Entity_Id;
226 Unprotected : Boolean := False) return List_Id;
227 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
228 -- Subprogram_Type. Builds signature of protected subprogram, adding the
229 -- formal that corresponds to the object itself. For an access to protected
230 -- subprogram, there is no object type to specify, so the parameter has
231 -- type Address and mode In. An indirect call through such a pointer will
232 -- convert the address to a reference to the actual object. The object is
233 -- a limited record and therefore a by_reference type.
235 function Build_Protected_Subprogram_Body
236 (N : Node_Id;
237 Pid : Node_Id;
238 N_Op_Spec : Node_Id) return Node_Id;
239 -- This function is used to construct the protected version of a protected
240 -- subprogram. Its statement sequence first defers abort, then locks the
241 -- associated protected object, and then enters a block that contains a
242 -- call to the unprotected version of the subprogram (for details, see
243 -- Build_Unprotected_Subprogram_Body). This block statement requires a
244 -- cleanup handler that unlocks the object in all cases. For details,
245 -- see Exp_Ch7.Expand_Cleanup_Actions.
247 function Build_Renamed_Formal_Declaration
248 (New_F : Entity_Id;
249 Formal : Entity_Id;
250 Comp : Entity_Id;
251 Renamed_Formal : Node_Id) return Node_Id;
252 -- Create a renaming declaration for a formal, within a protected entry
253 -- body or an accept body. The renamed object is a component of the
254 -- parameter block that is a parameter in the entry call.
256 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
257 -- does not dereference the corresponding component to prevent an illegal
258 -- use of the incomplete type (AI05-0151).
260 function Build_Selected_Name
261 (Prefix : Entity_Id;
262 Selector : Entity_Id;
263 Append_Char : Character := ' ') return Name_Id;
264 -- Build a name in the form of Prefix__Selector, with an optional character
265 -- appended. This is used for internal subprograms generated for operations
266 -- of protected types, including barrier functions. For the subprograms
267 -- generated for entry bodies and entry barriers, the generated name
268 -- includes a sequence number that makes names unique in the presence of
269 -- entry overloading. This is necessary because entry body procedures and
270 -- barrier functions all have the same signature.
272 procedure Build_Simple_Entry_Call
273 (N : Node_Id;
274 Concval : Node_Id;
275 Ename : Node_Id;
276 Index : Node_Id);
277 -- Some comments here would be useful ???
279 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
280 -- This routine constructs a specification for the procedure that we will
281 -- build for the task body for task type T. The spec has the form:
283 -- procedure tnameB (_Task : access tnameV);
285 -- where name is the character name taken from the task type entity that
286 -- is passed as the argument to the procedure, and tnameV is the task
287 -- value type that is associated with the task type.
289 function Build_Unprotected_Subprogram_Body
290 (N : Node_Id;
291 Pid : Node_Id) return Node_Id;
292 -- This routine constructs the unprotected version of a protected
293 -- subprogram body, which is contains all of the code in the original,
294 -- unexpanded body. This is the version of the protected subprogram that is
295 -- called from all protected operations on the same object, including the
296 -- protected version of the same subprogram.
298 procedure Build_Wrapper_Bodies
299 (Loc : Source_Ptr;
300 Typ : Entity_Id;
301 N : Node_Id);
302 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
303 -- record of a concurrent type. N is the insertion node where all bodies
304 -- will be placed. This routine builds the bodies of the subprograms which
305 -- serve as an indirection mechanism to overriding primitives of concurrent
306 -- types, entries and protected procedures. Any new body is analyzed.
308 procedure Build_Wrapper_Specs
309 (Loc : Source_Ptr;
310 Typ : Entity_Id;
311 N : in out Node_Id);
312 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
313 -- record of a concurrent type. N is the insertion node where all specs
314 -- will be placed. This routine builds the specs of the subprograms which
315 -- serve as an indirection mechanism to overriding primitives of concurrent
316 -- types, entries and protected procedures. Any new spec is analyzed.
318 procedure Collect_Entry_Families
319 (Loc : Source_Ptr;
320 Cdecls : List_Id;
321 Current_Node : in out Node_Id;
322 Conctyp : Entity_Id);
323 -- For each entry family in a concurrent type, create an anonymous array
324 -- type of the right size, and add a component to the corresponding_record.
326 function Concurrent_Object
327 (Spec_Id : Entity_Id;
328 Conc_Typ : Entity_Id) return Entity_Id;
329 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
330 -- the entity associated with the concurrent object in the Protected_Body_
331 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
332 -- denotes formal parameter _O, _object or _task.
334 function Copy_Result_Type (Res : Node_Id) return Node_Id;
335 -- Copy the result type of a function specification, when building the
336 -- internal operation corresponding to a protected function, or when
337 -- expanding an access to protected function. If the result is an anonymous
338 -- access to subprogram itself, we need to create a new signature with the
339 -- same parameter names and the same resolved types, but with new entities
340 -- for the formals.
342 function Create_Secondary_Stack_For_Task (T : Node_Id) return Boolean;
343 -- Return whether a secondary stack for the task T should be created by the
344 -- expander. The secondary stack for a task will be created by the expander
345 -- if the size of the stack has been specified by the Secondary_Stack_Size
346 -- representation aspect and either the No_Implicit_Heap_Allocations or
347 -- No_Implicit_Task_Allocations restrictions are in effect and the
348 -- No_Secondary_Stack restriction is not.
350 procedure Debug_Private_Data_Declarations (Decls : List_Id);
351 -- Decls is a list which may contain the declarations created by Install_
352 -- Private_Data_Declarations. All generated entities are marked as needing
353 -- debug info and debug nodes are manually generation where necessary. This
354 -- step of the expansion must to be done after private data has been moved
355 -- to its final resting scope to ensure proper visibility of debug objects.
357 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
358 -- If control flow optimizations are suppressed, and Alt is an accept,
359 -- delay, or entry call alternative with no trailing statements, insert
360 -- a null trailing statement with the given Loc (which is the sloc of
361 -- the accept, delay, or entry call statement). There might not be any
362 -- generated code for the accept, delay, or entry call itself (the effect
363 -- of these statements is part of the general processsing done for the
364 -- enclosing selective accept, timed entry call, or asynchronous select),
365 -- and the null statement is there to carry the sloc of that statement to
366 -- the back-end for trace-based coverage analysis purposes.
368 procedure Extract_Dispatching_Call
369 (N : Node_Id;
370 Call_Ent : out Entity_Id;
371 Object : out Entity_Id;
372 Actuals : out List_Id;
373 Formals : out List_Id);
374 -- Given a dispatching call, extract the entity of the name of the call,
375 -- its actual dispatching object, its actual parameters and the formal
376 -- parameters of the overridden interface-level version. If the type of
377 -- the dispatching object is an access type then an explicit dereference
378 -- is returned in Object.
380 procedure Extract_Entry
381 (N : Node_Id;
382 Concval : out Node_Id;
383 Ename : out Node_Id;
384 Index : out Node_Id);
385 -- Given an entry call, returns the associated concurrent object, the entry
386 -- name, and the entry family index.
388 function Family_Offset
389 (Loc : Source_Ptr;
390 Hi : Node_Id;
391 Lo : Node_Id;
392 Ttyp : Entity_Id;
393 Cap : Boolean) return Node_Id;
394 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
395 -- accept statement, or the upper bound in the discrete subtype of an entry
396 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
397 -- type of the entry. If Cap is true, the result is capped according to
398 -- Entry_Family_Bound.
400 function Family_Size
401 (Loc : Source_Ptr;
402 Hi : Node_Id;
403 Lo : Node_Id;
404 Ttyp : Entity_Id;
405 Cap : Boolean) return Node_Id;
406 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
407 -- family, and handle properly the superflat case. This is equivalent to
408 -- the use of 'Length on the index type, but must use Family_Offset to
409 -- handle properly the case of bounds that depend on discriminants. If
410 -- Cap is true, the result is capped according to Entry_Family_Bound.
412 procedure Find_Enclosing_Context
413 (N : Node_Id;
414 Context : out Node_Id;
415 Context_Id : out Entity_Id;
416 Context_Decls : out List_Id);
417 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
418 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
419 -- nearest enclosing body, block, package, or return statement and return
420 -- its constituents. Context is the enclosing construct, Context_Id is
421 -- the scope of Context_Id and Context_Decls is the declarative list of
422 -- Context.
424 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
425 -- Given a subprogram identifier, return the entity which is associated
426 -- with the protection entry index in the Protected_Body_Subprogram or
427 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
428 -- parameter _E.
430 function Is_Potentially_Large_Family
431 (Base_Index : Entity_Id;
432 Conctyp : Entity_Id;
433 Lo : Node_Id;
434 Hi : Node_Id) return Boolean;
436 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
437 -- Determine whether Id is a function or a procedure and is marked as a
438 -- private primitive.
440 function Null_Statements (Stats : List_Id) return Boolean;
441 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
442 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
443 -- to still count as null. Returns True for a null sequence. The argument
444 -- is the list of statements from the DO-END sequence.
446 function Parameter_Block_Pack
447 (Loc : Source_Ptr;
448 Blk_Typ : Entity_Id;
449 Actuals : List_Id;
450 Formals : List_Id;
451 Decls : List_Id;
452 Stmts : List_Id) return Entity_Id;
453 -- Set the components of the generated parameter block with the values
454 -- of the actual parameters. Generate aliased temporaries to capture the
455 -- values for types that are passed by copy. Otherwise generate a reference
456 -- to the actual's value. Return the address of the aggregate block.
457 -- Generate:
458 -- Jnn1 : alias <formal-type1>;
459 -- Jnn1 := <actual1>;
460 -- ...
461 -- P : Blk_Typ := (
462 -- Jnn1'unchecked_access;
463 -- <actual2>'reference;
464 -- ...);
466 function Parameter_Block_Unpack
467 (Loc : Source_Ptr;
468 P : Entity_Id;
469 Actuals : List_Id;
470 Formals : List_Id) return List_Id;
471 -- Retrieve the values of the components from the parameter block and
472 -- assign then to the original actual parameters. Generate:
473 -- <actual1> := P.<formal1>;
474 -- ...
475 -- <actualN> := P.<formalN>;
477 function Trivial_Accept_OK return Boolean;
478 -- If there is no DO-END block for an accept, or if the DO-END block has
479 -- only null statements, then it is possible to do the Rendezvous with much
480 -- less overhead using the Accept_Trivial routine in the run-time library.
481 -- However, this is not always a valid optimization. Whether it is valid or
482 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
483 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
484 -- a rescheduling is required, so this optimization is not allowed. This
485 -- function returns True if the optimization is permitted.
487 -----------------------------
488 -- Actual_Index_Expression --
489 -----------------------------
491 function Actual_Index_Expression
492 (Sloc : Source_Ptr;
493 Ent : Entity_Id;
494 Index : Node_Id;
495 Tsk : Entity_Id) return Node_Id
497 Ttyp : constant Entity_Id := Etype (Tsk);
498 Expr : Node_Id;
499 Num : Node_Id;
500 Lo : Node_Id;
501 Hi : Node_Id;
502 Prev : Entity_Id;
503 S : Node_Id;
505 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
506 -- Compute difference between bounds of entry family
508 --------------------------
509 -- Actual_Family_Offset --
510 --------------------------
512 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
514 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
515 -- Replace a reference to a discriminant with a selected component
516 -- denoting the discriminant of the target task.
518 -----------------------------
519 -- Actual_Discriminant_Ref --
520 -----------------------------
522 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
523 Typ : constant Entity_Id := Etype (Bound);
524 B : Node_Id;
526 begin
527 if not Is_Entity_Name (Bound)
528 or else Ekind (Entity (Bound)) /= E_Discriminant
529 then
530 if Nkind (Bound) = N_Attribute_Reference then
531 return Bound;
532 else
533 B := New_Copy_Tree (Bound);
534 end if;
536 else
537 B :=
538 Make_Selected_Component (Sloc,
539 Prefix => New_Copy_Tree (Tsk),
540 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
542 Analyze_And_Resolve (B, Typ);
543 end if;
545 return
546 Make_Attribute_Reference (Sloc,
547 Attribute_Name => Name_Pos,
548 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
549 Expressions => New_List (B));
550 end Actual_Discriminant_Ref;
552 -- Start of processing for Actual_Family_Offset
554 begin
555 return
556 Make_Op_Subtract (Sloc,
557 Left_Opnd => Actual_Discriminant_Ref (Hi),
558 Right_Opnd => Actual_Discriminant_Ref (Lo));
559 end Actual_Family_Offset;
561 -- Start of processing for Actual_Index_Expression
563 begin
564 -- The queues of entries and entry families appear in textual order in
565 -- the associated record. The entry index is computed as the sum of the
566 -- number of queues for all entries that precede the designated one, to
567 -- which is added the index expression, if this expression denotes a
568 -- member of a family.
570 -- The following is a place holder for the count of simple entries
572 Num := Make_Integer_Literal (Sloc, 1);
574 -- We construct an expression which is a series of addition operations.
575 -- See comments in Entry_Index_Expression, which is identical in
576 -- structure.
578 if Present (Index) then
579 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
581 Expr :=
582 Make_Op_Add (Sloc,
583 Left_Opnd => Num,
584 Right_Opnd =>
585 Actual_Family_Offset (
586 Make_Attribute_Reference (Sloc,
587 Attribute_Name => Name_Pos,
588 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
589 Expressions => New_List (Relocate_Node (Index))),
590 Type_Low_Bound (S)));
591 else
592 Expr := Num;
593 end if;
595 -- Now add lengths of preceding entries and entry families
597 Prev := First_Entity (Ttyp);
598 while Chars (Prev) /= Chars (Ent)
599 or else (Ekind (Prev) /= Ekind (Ent))
600 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
601 loop
602 if Ekind (Prev) = E_Entry then
603 Set_Intval (Num, Intval (Num) + 1);
605 elsif Ekind (Prev) = E_Entry_Family then
606 S :=
607 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
609 -- The need for the following full view retrieval stems from this
610 -- complex case of nested generics and tasking:
612 -- generic
613 -- type Formal_Index is range <>;
614 -- ...
615 -- package Outer is
616 -- type Index is private;
617 -- generic
618 -- ...
619 -- package Inner is
620 -- procedure P;
621 -- end Inner;
622 -- private
623 -- type Index is new Formal_Index range 1 .. 10;
624 -- end Outer;
626 -- package body Outer is
627 -- task type T is
628 -- entry Fam (Index); -- (2)
629 -- entry E;
630 -- end T;
631 -- package body Inner is -- (3)
632 -- procedure P is
633 -- begin
634 -- T.E; -- (1)
635 -- end P;
636 -- end Inner;
637 -- ...
639 -- We are currently building the index expression for the entry
640 -- call "T.E" (1). Part of the expansion must mention the range
641 -- of the discrete type "Index" (2) of entry family "Fam".
643 -- However only the private view of type "Index" is available to
644 -- the inner generic (3) because there was no prior mention of
645 -- the type inside "Inner". This visibility requirement is
646 -- implicit and cannot be detected during the construction of
647 -- the generic trees and needs special handling.
649 if In_Instance_Body
650 and then Is_Private_Type (S)
651 and then Present (Full_View (S))
652 then
653 S := Full_View (S);
654 end if;
656 Lo := Type_Low_Bound (S);
657 Hi := Type_High_Bound (S);
659 Expr :=
660 Make_Op_Add (Sloc,
661 Left_Opnd => Expr,
662 Right_Opnd =>
663 Make_Op_Add (Sloc,
664 Left_Opnd => Actual_Family_Offset (Hi, Lo),
665 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
667 -- Other components are anonymous types to be ignored
669 else
670 null;
671 end if;
673 Next_Entity (Prev);
674 end loop;
676 return Expr;
677 end Actual_Index_Expression;
679 --------------------------
680 -- Add_Formal_Renamings --
681 --------------------------
683 procedure Add_Formal_Renamings
684 (Spec : Node_Id;
685 Decls : List_Id;
686 Ent : Entity_Id;
687 Loc : Source_Ptr)
689 Ptr : constant Entity_Id :=
690 Defining_Identifier
691 (Next (First (Parameter_Specifications (Spec))));
692 -- The name of the formal that holds the address of the parameter block
693 -- for the call.
695 Comp : Entity_Id;
696 Decl : Node_Id;
697 Formal : Entity_Id;
698 New_F : Entity_Id;
699 Renamed_Formal : Node_Id;
701 begin
702 Formal := First_Formal (Ent);
703 while Present (Formal) loop
704 Comp := Entry_Component (Formal);
705 New_F :=
706 Make_Defining_Identifier (Sloc (Formal),
707 Chars => Chars (Formal));
708 Set_Etype (New_F, Etype (Formal));
709 Set_Scope (New_F, Ent);
711 -- Now we set debug info needed on New_F even though it does not come
712 -- from source, so that the debugger will get the right information
713 -- for these generated names.
715 Set_Debug_Info_Needed (New_F);
717 if Ekind (Formal) = E_In_Parameter then
718 Set_Ekind (New_F, E_Constant);
719 else
720 Set_Ekind (New_F, E_Variable);
721 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
722 end if;
724 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
726 Renamed_Formal :=
727 Make_Selected_Component (Loc,
728 Prefix =>
729 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
730 Make_Identifier (Loc, Chars (Ptr))),
731 Selector_Name => New_Occurrence_Of (Comp, Loc));
733 Decl :=
734 Build_Renamed_Formal_Declaration
735 (New_F, Formal, Comp, Renamed_Formal);
737 Append (Decl, Decls);
738 Set_Renamed_Object (Formal, New_F);
739 Next_Formal (Formal);
740 end loop;
741 end Add_Formal_Renamings;
743 ------------------------
744 -- Add_Object_Pointer --
745 ------------------------
747 procedure Add_Object_Pointer
748 (Loc : Source_Ptr;
749 Conc_Typ : Entity_Id;
750 Decls : List_Id)
752 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
753 Decl : Node_Id;
754 Obj_Ptr : Node_Id;
756 begin
757 -- Create the renaming declaration for the Protection object of a
758 -- protected type. _Object is used by Complete_Entry_Body.
759 -- ??? An attempt to make this a renaming was unsuccessful.
761 -- Build the entity for the access type
763 Obj_Ptr :=
764 Make_Defining_Identifier (Loc,
765 New_External_Name (Chars (Rec_Typ), 'P'));
767 -- Generate:
768 -- _object : poVP := poVP!O;
770 Decl :=
771 Make_Object_Declaration (Loc,
772 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
773 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
774 Expression =>
775 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
776 Set_Debug_Info_Needed (Defining_Identifier (Decl));
777 Prepend_To (Decls, Decl);
779 -- Generate:
780 -- type poVP is access poV;
782 Decl :=
783 Make_Full_Type_Declaration (Loc,
784 Defining_Identifier =>
785 Obj_Ptr,
786 Type_Definition =>
787 Make_Access_To_Object_Definition (Loc,
788 Subtype_Indication =>
789 New_Occurrence_Of (Rec_Typ, Loc)));
790 Set_Debug_Info_Needed (Defining_Identifier (Decl));
791 Prepend_To (Decls, Decl);
792 end Add_Object_Pointer;
794 -----------------------
795 -- Build_Accept_Body --
796 -----------------------
798 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
799 Loc : constant Source_Ptr := Sloc (Astat);
800 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
801 New_S : Node_Id;
802 Hand : Node_Id;
803 Call : Node_Id;
804 Ohandle : Node_Id;
806 begin
807 -- At the end of the statement sequence, Complete_Rendezvous is called.
808 -- A label skipping the Complete_Rendezvous, and all other accept
809 -- processing, has already been added for the expansion of requeue
810 -- statements. The Sloc is copied from the last statement since it
811 -- is really part of this last statement.
813 Call :=
814 Build_Runtime_Call
815 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
816 Insert_Before (Last (Statements (Stats)), Call);
817 Analyze (Call);
819 -- If exception handlers are present, then append Complete_Rendezvous
820 -- calls to the handlers, and construct the required outer block. As
821 -- above, the Sloc is copied from the last statement in the sequence.
823 if Present (Exception_Handlers (Stats)) then
824 Hand := First (Exception_Handlers (Stats));
825 while Present (Hand) loop
826 Call :=
827 Build_Runtime_Call
828 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
829 Append (Call, Statements (Hand));
830 Analyze (Call);
831 Next (Hand);
832 end loop;
834 New_S :=
835 Make_Handled_Sequence_Of_Statements (Loc,
836 Statements => New_List (
837 Make_Block_Statement (Loc,
838 Handled_Statement_Sequence => Stats)));
840 else
841 New_S := Stats;
842 end if;
844 -- At this stage we know that the new statement sequence does
845 -- not have an exception handler part, so we supply one to call
846 -- Exceptional_Complete_Rendezvous. This handler is
848 -- when all others =>
849 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
851 -- We handle Abort_Signal to make sure that we properly catch the abort
852 -- case and wake up the caller.
854 Ohandle := Make_Others_Choice (Loc);
855 Set_All_Others (Ohandle);
857 Set_Exception_Handlers (New_S,
858 New_List (
859 Make_Implicit_Exception_Handler (Loc,
860 Exception_Choices => New_List (Ohandle),
862 Statements => New_List (
863 Make_Procedure_Call_Statement (Sloc (Stats),
864 Name => New_Occurrence_Of (
865 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
866 Parameter_Associations => New_List (
867 Make_Function_Call (Sloc (Stats),
868 Name =>
869 New_Occurrence_Of
870 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
872 Set_Parent (New_S, Astat); -- temp parent for Analyze call
873 Analyze_Exception_Handlers (Exception_Handlers (New_S));
874 Expand_Exception_Handlers (New_S);
876 -- Exceptional_Complete_Rendezvous must be called with abort still
877 -- deferred, which is the case for a "when all others" handler.
879 return New_S;
880 end Build_Accept_Body;
882 -----------------------------------
883 -- Build_Activation_Chain_Entity --
884 -----------------------------------
886 procedure Build_Activation_Chain_Entity (N : Node_Id) is
887 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
888 -- Determine whether an extended return statement has activation chain
890 --------------------------
891 -- Has_Activation_Chain --
892 --------------------------
894 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
895 Decl : Node_Id;
897 begin
898 Decl := First (Return_Object_Declarations (Stmt));
899 while Present (Decl) loop
900 if Nkind (Decl) = N_Object_Declaration
901 and then Chars (Defining_Identifier (Decl)) = Name_uChain
902 then
903 return True;
904 end if;
906 Next (Decl);
907 end loop;
909 return False;
910 end Has_Activation_Chain;
912 -- Local variables
914 Context : Node_Id;
915 Context_Id : Entity_Id;
916 Decls : List_Id;
918 -- Start of processing for Build_Activation_Chain_Entity
920 begin
921 -- Activation chain is never used for sequential elaboration policy, see
922 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
924 if Partition_Elaboration_Policy = 'S' then
925 return;
926 end if;
928 Find_Enclosing_Context (N, Context, Context_Id, Decls);
930 -- If activation chain entity has not been declared already, create one
932 if Nkind (Context) = N_Extended_Return_Statement
933 or else No (Activation_Chain_Entity (Context))
934 then
935 -- Since extended return statements do not store the entity of the
936 -- chain, examine the return object declarations to avoid creating
937 -- a duplicate.
939 if Nkind (Context) = N_Extended_Return_Statement
940 and then Has_Activation_Chain (Context)
941 then
942 return;
943 end if;
945 declare
946 Loc : constant Source_Ptr := Sloc (Context);
947 Chain : Entity_Id;
948 Decl : Node_Id;
950 begin
951 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
953 -- Note: An extended return statement is not really a task
954 -- activator, but it does have an activation chain on which to
955 -- store the tasks temporarily. On successful return, the tasks
956 -- on this chain are moved to the chain passed in by the caller.
957 -- We do not build an Activation_Chain_Entity for an extended
958 -- return statement, because we do not want to build a call to
959 -- Activate_Tasks. Task activation is the responsibility of the
960 -- caller.
962 if Nkind (Context) /= N_Extended_Return_Statement then
963 Set_Activation_Chain_Entity (Context, Chain);
964 end if;
966 Decl :=
967 Make_Object_Declaration (Loc,
968 Defining_Identifier => Chain,
969 Aliased_Present => True,
970 Object_Definition =>
971 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
973 Prepend_To (Decls, Decl);
975 -- Ensure that _chain appears in the proper scope of the context
977 if Context_Id /= Current_Scope then
978 Push_Scope (Context_Id);
979 Analyze (Decl);
980 Pop_Scope;
981 else
982 Analyze (Decl);
983 end if;
984 end;
985 end if;
986 end Build_Activation_Chain_Entity;
988 ----------------------------
989 -- Build_Barrier_Function --
990 ----------------------------
992 function Build_Barrier_Function
993 (N : Node_Id;
994 Ent : Entity_Id;
995 Pid : Node_Id) return Node_Id
997 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
998 Cond : constant Node_Id := Condition (Ent_Formals);
999 Loc : constant Source_Ptr := Sloc (Cond);
1000 Func_Id : constant Entity_Id := Barrier_Function (Ent);
1001 Op_Decls : constant List_Id := New_List;
1002 Stmt : Node_Id;
1003 Func_Body : Node_Id;
1005 begin
1006 -- Add a declaration for the Protection object, renaming declarations
1007 -- for the discriminals and privals and finally a declaration for the
1008 -- entry family index (if applicable).
1010 Install_Private_Data_Declarations (Sloc (N),
1011 Spec_Id => Func_Id,
1012 Conc_Typ => Pid,
1013 Body_Nod => N,
1014 Decls => Op_Decls,
1015 Barrier => True,
1016 Family => Ekind (Ent) = E_Entry_Family);
1018 -- If compiling with -fpreserve-control-flow, make sure we insert an
1019 -- IF statement so that the back-end knows to generate a conditional
1020 -- branch instruction, even if the condition is just the name of a
1021 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1022 -- such redundant IF statements under -fpreserve-control-flow
1023 -- (whether coming from this routine, or directly from source).
1025 if Opt.Suppress_Control_Flow_Optimizations then
1026 Stmt :=
1027 Make_Implicit_If_Statement (Cond,
1028 Condition => Cond,
1029 Then_Statements => New_List (
1030 Make_Simple_Return_Statement (Loc,
1031 New_Occurrence_Of (Standard_True, Loc))),
1033 Else_Statements => New_List (
1034 Make_Simple_Return_Statement (Loc,
1035 New_Occurrence_Of (Standard_False, Loc))));
1037 else
1038 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1039 end if;
1041 -- Note: the condition in the barrier function needs to be properly
1042 -- processed for the C/Fortran boolean possibility, but this happens
1043 -- automatically since the return statement does this normalization.
1045 Func_Body :=
1046 Make_Subprogram_Body (Loc,
1047 Specification =>
1048 Build_Barrier_Function_Specification (Loc,
1049 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1050 Declarations => Op_Decls,
1051 Handled_Statement_Sequence =>
1052 Make_Handled_Sequence_Of_Statements (Loc,
1053 Statements => New_List (Stmt)));
1054 Set_Is_Entry_Barrier_Function (Func_Body);
1056 return Func_Body;
1057 end Build_Barrier_Function;
1059 ------------------------------------------
1060 -- Build_Barrier_Function_Specification --
1061 ------------------------------------------
1063 function Build_Barrier_Function_Specification
1064 (Loc : Source_Ptr;
1065 Def_Id : Entity_Id) return Node_Id
1067 begin
1068 Set_Debug_Info_Needed (Def_Id);
1070 return
1071 Make_Function_Specification (Loc,
1072 Defining_Unit_Name => Def_Id,
1073 Parameter_Specifications => New_List (
1074 Make_Parameter_Specification (Loc,
1075 Defining_Identifier =>
1076 Make_Defining_Identifier (Loc, Name_uO),
1077 Parameter_Type =>
1078 New_Occurrence_Of (RTE (RE_Address), Loc)),
1080 Make_Parameter_Specification (Loc,
1081 Defining_Identifier =>
1082 Make_Defining_Identifier (Loc, Name_uE),
1083 Parameter_Type =>
1084 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1086 Result_Definition =>
1087 New_Occurrence_Of (Standard_Boolean, Loc));
1088 end Build_Barrier_Function_Specification;
1090 --------------------------
1091 -- Build_Call_With_Task --
1092 --------------------------
1094 function Build_Call_With_Task
1095 (N : Node_Id;
1096 E : Entity_Id) return Node_Id
1098 Loc : constant Source_Ptr := Sloc (N);
1099 begin
1100 return
1101 Make_Function_Call (Loc,
1102 Name => New_Occurrence_Of (E, Loc),
1103 Parameter_Associations => New_List (Concurrent_Ref (N)));
1104 end Build_Call_With_Task;
1106 -----------------------------
1107 -- Build_Class_Wide_Master --
1108 -----------------------------
1110 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1111 Loc : constant Source_Ptr := Sloc (Typ);
1112 Master_Decl : Node_Id;
1113 Master_Id : Entity_Id;
1114 Master_Scope : Entity_Id;
1115 Name_Id : Node_Id;
1116 Related_Node : Node_Id;
1117 Ren_Decl : Node_Id;
1119 begin
1120 -- Nothing to do if there is no task hierarchy
1122 if Restriction_Active (No_Task_Hierarchy) then
1123 return;
1124 end if;
1126 -- Find the declaration that created the access type, which is either a
1127 -- type declaration, or an object declaration with an access definition,
1128 -- in which case the type is anonymous.
1130 if Is_Itype (Typ) then
1131 Related_Node := Associated_Node_For_Itype (Typ);
1132 else
1133 Related_Node := Parent (Typ);
1134 end if;
1136 Master_Scope := Find_Master_Scope (Typ);
1138 -- Nothing to do if the master scope already contains a _master entity.
1139 -- The only exception to this is the following scenario:
1141 -- Source_Scope
1142 -- Transient_Scope_1
1143 -- _master
1145 -- Transient_Scope_2
1146 -- use of master
1148 -- In this case the source scope is marked as having the master entity
1149 -- even though the actual declaration appears inside an inner scope. If
1150 -- the second transient scope requires a _master, it cannot use the one
1151 -- already declared because the entity is not visible.
1153 Name_Id := Make_Identifier (Loc, Name_uMaster);
1154 Master_Decl := Empty;
1156 if not Has_Master_Entity (Master_Scope)
1157 or else No (Current_Entity_In_Scope (Name_Id))
1158 then
1159 begin
1160 Set_Has_Master_Entity (Master_Scope);
1162 -- Generate:
1163 -- _master : constant Integer := Current_Master.all;
1165 Master_Decl :=
1166 Make_Object_Declaration (Loc,
1167 Defining_Identifier =>
1168 Make_Defining_Identifier (Loc, Name_uMaster),
1169 Constant_Present => True,
1170 Object_Definition =>
1171 New_Occurrence_Of (Standard_Integer, Loc),
1172 Expression =>
1173 Make_Explicit_Dereference (Loc,
1174 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1176 Insert_Action (Find_Hook_Context (Related_Node), Master_Decl);
1177 Analyze (Master_Decl);
1179 -- Mark the containing scope as a task master. Masters associated
1180 -- with return statements are already marked at this stage (see
1181 -- Analyze_Subprogram_Body).
1183 if Ekind (Current_Scope) /= E_Return_Statement then
1184 declare
1185 Par : Node_Id := Related_Node;
1187 begin
1188 while Nkind (Par) /= N_Compilation_Unit loop
1189 Par := Parent (Par);
1191 -- If we fall off the top, we are at the outer level,
1192 -- and the environment task is our effective master,
1193 -- so nothing to mark.
1195 if Nkind_In (Par, N_Block_Statement,
1196 N_Subprogram_Body,
1197 N_Task_Body)
1198 then
1199 Set_Is_Task_Master (Par);
1200 exit;
1201 end if;
1202 end loop;
1203 end;
1204 end if;
1205 end;
1206 end if;
1208 Master_Id :=
1209 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1211 -- Generate:
1212 -- typeMnn renames _master;
1214 Ren_Decl :=
1215 Make_Object_Renaming_Declaration (Loc,
1216 Defining_Identifier => Master_Id,
1217 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1218 Name => Name_Id);
1220 -- If the master is declared locally, add the renaming declaration
1221 -- immediately after it, to prevent access-before-elaboration in the
1222 -- back-end.
1224 if Present (Master_Decl) then
1225 Insert_After (Master_Decl, Ren_Decl);
1226 Analyze (Ren_Decl);
1228 else
1229 Insert_Action (Related_Node, Ren_Decl);
1230 end if;
1232 Set_Master_Id (Typ, Master_Id);
1233 end Build_Class_Wide_Master;
1235 ----------------------------
1236 -- Build_Contract_Wrapper --
1237 ----------------------------
1239 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id) is
1240 Conc_Typ : constant Entity_Id := Scope (E);
1241 Loc : constant Source_Ptr := Sloc (E);
1243 procedure Add_Discriminant_Renamings
1244 (Obj_Id : Entity_Id;
1245 Decls : List_Id);
1246 -- Add renaming declarations for all discriminants of concurrent type
1247 -- Conc_Typ. Obj_Id is the entity of the wrapper formal parameter which
1248 -- represents the concurrent object.
1250 procedure Add_Matching_Formals
1251 (Formals : List_Id;
1252 Actuals : in out List_Id);
1253 -- Add formal parameters that match those of entry E to list Formals.
1254 -- The routine also adds matching actuals for the new formals to list
1255 -- Actuals.
1257 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id);
1258 -- Relocate pragma Prag to list To. The routine creates a new list if
1259 -- To does not exist.
1261 --------------------------------
1262 -- Add_Discriminant_Renamings --
1263 --------------------------------
1265 procedure Add_Discriminant_Renamings
1266 (Obj_Id : Entity_Id;
1267 Decls : List_Id)
1269 Discr : Entity_Id;
1271 begin
1272 -- Inspect the discriminants of the concurrent type and generate a
1273 -- renaming for each one.
1275 if Has_Discriminants (Conc_Typ) then
1276 Discr := First_Discriminant (Conc_Typ);
1277 while Present (Discr) loop
1278 Prepend_To (Decls,
1279 Make_Object_Renaming_Declaration (Loc,
1280 Defining_Identifier =>
1281 Make_Defining_Identifier (Loc, Chars (Discr)),
1282 Subtype_Mark =>
1283 New_Occurrence_Of (Etype (Discr), Loc),
1284 Name =>
1285 Make_Selected_Component (Loc,
1286 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1287 Selector_Name =>
1288 Make_Identifier (Loc, Chars (Discr)))));
1290 Next_Discriminant (Discr);
1291 end loop;
1292 end if;
1293 end Add_Discriminant_Renamings;
1295 --------------------------
1296 -- Add_Matching_Formals --
1297 --------------------------
1299 procedure Add_Matching_Formals
1300 (Formals : List_Id;
1301 Actuals : in out List_Id)
1303 Formal : Entity_Id;
1304 New_Formal : Entity_Id;
1306 begin
1307 -- Inspect the formal parameters of the entry and generate a new
1308 -- matching formal with the same name for the wrapper. A reference
1309 -- to the new formal becomes an actual in the entry call.
1311 Formal := First_Formal (E);
1312 while Present (Formal) loop
1313 New_Formal := Make_Defining_Identifier (Loc, Chars (Formal));
1314 Append_To (Formals,
1315 Make_Parameter_Specification (Loc,
1316 Defining_Identifier => New_Formal,
1317 In_Present => In_Present (Parent (Formal)),
1318 Out_Present => Out_Present (Parent (Formal)),
1319 Parameter_Type =>
1320 New_Occurrence_Of (Etype (Formal), Loc)));
1322 if No (Actuals) then
1323 Actuals := New_List;
1324 end if;
1326 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1327 Next_Formal (Formal);
1328 end loop;
1329 end Add_Matching_Formals;
1331 ---------------------
1332 -- Transfer_Pragma --
1333 ---------------------
1335 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id) is
1336 New_Prag : Node_Id;
1338 begin
1339 if No (To) then
1340 To := New_List;
1341 end if;
1343 New_Prag := Relocate_Node (Prag);
1345 Set_Analyzed (New_Prag, False);
1346 Append (New_Prag, To);
1347 end Transfer_Pragma;
1349 -- Local variables
1351 Items : constant Node_Id := Contract (E);
1352 Actuals : List_Id := No_List;
1353 Call : Node_Id;
1354 Call_Nam : Node_Id;
1355 Decls : List_Id := No_List;
1356 Formals : List_Id;
1357 Has_Pragma : Boolean := False;
1358 Index_Id : Entity_Id;
1359 Obj_Id : Entity_Id;
1360 Prag : Node_Id;
1361 Wrapper_Id : Entity_Id;
1363 -- Start of processing for Build_Contract_Wrapper
1365 begin
1366 -- This routine generates a specialized wrapper for a protected or task
1367 -- entry [family] which implements precondition/postcondition semantics.
1368 -- Preconditions and case guards of contract cases are checked before
1369 -- the protected action or rendezvous takes place. Postconditions and
1370 -- consequences of contract cases are checked after the protected action
1371 -- or rendezvous takes place. The structure of the generated wrapper is
1372 -- as follows:
1374 -- procedure Wrapper
1375 -- (Obj_Id : Conc_Typ; -- concurrent object
1376 -- [Index : Index_Typ;] -- index of entry family
1377 -- [Formal_1 : ...; -- parameters of original entry
1378 -- Formal_N : ...])
1379 -- is
1380 -- [Discr_1 : ... renames Obj_Id.Discr_1; -- discriminant
1381 -- Discr_N : ... renames Obj_Id.Discr_N;] -- renamings
1383 -- <precondition checks>
1384 -- <case guard checks>
1386 -- procedure _Postconditions is
1387 -- begin
1388 -- <postcondition checks>
1389 -- <consequence checks>
1390 -- end _Postconditions;
1392 -- begin
1393 -- Entry_Call (Obj_Id, [Index,] [Formal_1, Formal_N]);
1394 -- _Postconditions;
1395 -- end Wrapper;
1397 -- Create the wrapper only when the entry has at least one executable
1398 -- contract item such as contract cases, precondition or postcondition.
1400 if Present (Items) then
1402 -- Inspect the list of pre/postconditions and transfer all available
1403 -- pragmas to the declarative list of the wrapper.
1405 Prag := Pre_Post_Conditions (Items);
1406 while Present (Prag) loop
1407 if Nam_In (Pragma_Name_Unmapped (Prag),
1408 Name_Postcondition, Name_Precondition)
1409 and then Is_Checked (Prag)
1410 then
1411 Has_Pragma := True;
1412 Transfer_Pragma (Prag, To => Decls);
1413 end if;
1415 Prag := Next_Pragma (Prag);
1416 end loop;
1418 -- Inspect the list of test/contract cases and transfer only contract
1419 -- cases pragmas to the declarative part of the wrapper.
1421 Prag := Contract_Test_Cases (Items);
1422 while Present (Prag) loop
1423 if Pragma_Name (Prag) = Name_Contract_Cases
1424 and then Is_Checked (Prag)
1425 then
1426 Has_Pragma := True;
1427 Transfer_Pragma (Prag, To => Decls);
1428 end if;
1430 Prag := Next_Pragma (Prag);
1431 end loop;
1432 end if;
1434 -- The entry lacks executable contract items and a wrapper is not needed
1436 if not Has_Pragma then
1437 return;
1438 end if;
1440 -- Create the profile of the wrapper. The first formal parameter is the
1441 -- concurrent object.
1443 Obj_Id :=
1444 Make_Defining_Identifier (Loc,
1445 Chars => New_External_Name (Chars (Conc_Typ), 'A'));
1447 Formals := New_List (
1448 Make_Parameter_Specification (Loc,
1449 Defining_Identifier => Obj_Id,
1450 Out_Present => True,
1451 In_Present => True,
1452 Parameter_Type => New_Occurrence_Of (Conc_Typ, Loc)));
1454 -- Construct the call to the original entry. The call will be gradually
1455 -- augmented with an optional entry index and extra parameters.
1457 Call_Nam :=
1458 Make_Selected_Component (Loc,
1459 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1460 Selector_Name => New_Occurrence_Of (E, Loc));
1462 -- When creating a wrapper for an entry family, the second formal is the
1463 -- entry index.
1465 if Ekind (E) = E_Entry_Family then
1466 Index_Id := Make_Defining_Identifier (Loc, Name_I);
1468 Append_To (Formals,
1469 Make_Parameter_Specification (Loc,
1470 Defining_Identifier => Index_Id,
1471 Parameter_Type =>
1472 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1474 -- The call to the original entry becomes an indexed component to
1475 -- accommodate the entry index.
1477 Call_Nam :=
1478 Make_Indexed_Component (Loc,
1479 Prefix => Call_Nam,
1480 Expressions => New_List (New_Occurrence_Of (Index_Id, Loc)));
1481 end if;
1483 -- Add formal parameters to match those of the entry and build actuals
1484 -- for the entry call.
1486 Add_Matching_Formals (Formals, Actuals);
1488 Call :=
1489 Make_Procedure_Call_Statement (Loc,
1490 Name => Call_Nam,
1491 Parameter_Associations => Actuals);
1493 -- Add renaming declarations for the discriminants of the enclosing type
1494 -- as the various contract items may reference them.
1496 Add_Discriminant_Renamings (Obj_Id, Decls);
1498 Wrapper_Id :=
1499 Make_Defining_Identifier (Loc, New_External_Name (Chars (E), 'E'));
1500 Set_Contract_Wrapper (E, Wrapper_Id);
1501 Set_Is_Entry_Wrapper (Wrapper_Id);
1503 -- The wrapper body is analyzed when the enclosing type is frozen
1505 Append_Freeze_Action (Defining_Entity (Decl),
1506 Make_Subprogram_Body (Loc,
1507 Specification =>
1508 Make_Procedure_Specification (Loc,
1509 Defining_Unit_Name => Wrapper_Id,
1510 Parameter_Specifications => Formals),
1511 Declarations => Decls,
1512 Handled_Statement_Sequence =>
1513 Make_Handled_Sequence_Of_Statements (Loc,
1514 Statements => New_List (Call))));
1515 end Build_Contract_Wrapper;
1517 --------------------------------
1518 -- Build_Corresponding_Record --
1519 --------------------------------
1521 function Build_Corresponding_Record
1522 (N : Node_Id;
1523 Ctyp : Entity_Id;
1524 Loc : Source_Ptr) return Node_Id
1526 Rec_Ent : constant Entity_Id :=
1527 Make_Defining_Identifier
1528 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1529 Disc : Entity_Id;
1530 Dlist : List_Id;
1531 New_Disc : Entity_Id;
1532 Cdecls : List_Id;
1534 begin
1535 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1536 Set_Ekind (Rec_Ent, E_Record_Type);
1537 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1538 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1539 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1540 Set_Stored_Constraint (Rec_Ent, No_Elist);
1541 Cdecls := New_List;
1543 -- Use discriminals to create list of discriminants for record, and
1544 -- create new discriminals for use in default expressions, etc. It is
1545 -- worth noting that a task discriminant gives rise to 5 entities;
1547 -- a) The original discriminant.
1548 -- b) The discriminal for use in the task.
1549 -- c) The discriminant of the corresponding record.
1550 -- d) The discriminal for the init proc of the corresponding record.
1551 -- e) The local variable that renames the discriminant in the procedure
1552 -- for the task body.
1554 -- In fact the discriminals b) are used in the renaming declarations
1555 -- for e). See details in einfo (Handling of Discriminants).
1557 if Present (Discriminant_Specifications (N)) then
1558 Dlist := New_List;
1559 Disc := First_Discriminant (Ctyp);
1561 while Present (Disc) loop
1562 New_Disc := CR_Discriminant (Disc);
1564 Append_To (Dlist,
1565 Make_Discriminant_Specification (Loc,
1566 Defining_Identifier => New_Disc,
1567 Discriminant_Type =>
1568 New_Occurrence_Of (Etype (Disc), Loc),
1569 Expression =>
1570 New_Copy (Discriminant_Default_Value (Disc))));
1572 Next_Discriminant (Disc);
1573 end loop;
1575 else
1576 Dlist := No_List;
1577 end if;
1579 -- Now we can construct the record type declaration. Note that this
1580 -- record is "limited tagged". It is "limited" to reflect the underlying
1581 -- limitedness of the task or protected object that it represents, and
1582 -- ensuring for example that it is properly passed by reference. It is
1583 -- "tagged" to give support to dispatching calls through interfaces. We
1584 -- propagate here the list of interfaces covered by the concurrent type
1585 -- (Ada 2005: AI-345).
1587 return
1588 Make_Full_Type_Declaration (Loc,
1589 Defining_Identifier => Rec_Ent,
1590 Discriminant_Specifications => Dlist,
1591 Type_Definition =>
1592 Make_Record_Definition (Loc,
1593 Component_List =>
1594 Make_Component_List (Loc, Component_Items => Cdecls),
1595 Tagged_Present =>
1596 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1597 Interface_List => Interface_List (N),
1598 Limited_Present => True));
1599 end Build_Corresponding_Record;
1601 ---------------------------------
1602 -- Build_Dispatching_Tag_Check --
1603 ---------------------------------
1605 function Build_Dispatching_Tag_Check
1606 (K : Entity_Id;
1607 N : Node_Id) return Node_Id
1609 Loc : constant Source_Ptr := Sloc (N);
1611 begin
1612 return
1613 Make_Op_Or (Loc,
1614 Make_Op_Eq (Loc,
1615 Left_Opnd =>
1616 New_Occurrence_Of (K, Loc),
1617 Right_Opnd =>
1618 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1620 Make_Op_Eq (Loc,
1621 Left_Opnd =>
1622 New_Occurrence_Of (K, Loc),
1623 Right_Opnd =>
1624 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1625 end Build_Dispatching_Tag_Check;
1627 ----------------------------------
1628 -- Build_Entry_Count_Expression --
1629 ----------------------------------
1631 function Build_Entry_Count_Expression
1632 (Concurrent_Type : Node_Id;
1633 Component_List : List_Id;
1634 Loc : Source_Ptr) return Node_Id
1636 Eindx : Nat;
1637 Ent : Entity_Id;
1638 Ecount : Node_Id;
1639 Comp : Node_Id;
1640 Lo : Node_Id;
1641 Hi : Node_Id;
1642 Typ : Entity_Id;
1643 Large : Boolean;
1645 begin
1646 -- Count number of non-family entries
1648 Eindx := 0;
1649 Ent := First_Entity (Concurrent_Type);
1650 while Present (Ent) loop
1651 if Ekind (Ent) = E_Entry then
1652 Eindx := Eindx + 1;
1653 end if;
1655 Next_Entity (Ent);
1656 end loop;
1658 Ecount := Make_Integer_Literal (Loc, Eindx);
1660 -- Loop through entry families building the addition nodes
1662 Ent := First_Entity (Concurrent_Type);
1663 Comp := First (Component_List);
1664 while Present (Ent) loop
1665 if Ekind (Ent) = E_Entry_Family then
1666 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1667 Next (Comp);
1668 end loop;
1670 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1671 Hi := Type_High_Bound (Typ);
1672 Lo := Type_Low_Bound (Typ);
1673 Large := Is_Potentially_Large_Family
1674 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1675 Ecount :=
1676 Make_Op_Add (Loc,
1677 Left_Opnd => Ecount,
1678 Right_Opnd =>
1679 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1680 end if;
1682 Next_Entity (Ent);
1683 end loop;
1685 return Ecount;
1686 end Build_Entry_Count_Expression;
1688 ---------------------------
1689 -- Build_Parameter_Block --
1690 ---------------------------
1692 function Build_Parameter_Block
1693 (Loc : Source_Ptr;
1694 Actuals : List_Id;
1695 Formals : List_Id;
1696 Decls : List_Id) return Entity_Id
1698 Actual : Entity_Id;
1699 Comp_Nam : Node_Id;
1700 Comps : List_Id;
1701 Formal : Entity_Id;
1702 Has_Comp : Boolean := False;
1703 Rec_Nam : Node_Id;
1705 begin
1706 Actual := First (Actuals);
1707 Comps := New_List;
1708 Formal := Defining_Identifier (First (Formals));
1710 while Present (Actual) loop
1711 if not Is_Controlling_Actual (Actual) then
1713 -- Generate:
1714 -- type Ann is access all <actual-type>
1716 Comp_Nam := Make_Temporary (Loc, 'A');
1717 Set_Is_Param_Block_Component_Type (Comp_Nam);
1719 Append_To (Decls,
1720 Make_Full_Type_Declaration (Loc,
1721 Defining_Identifier => Comp_Nam,
1722 Type_Definition =>
1723 Make_Access_To_Object_Definition (Loc,
1724 All_Present => True,
1725 Constant_Present => Ekind (Formal) = E_In_Parameter,
1726 Subtype_Indication =>
1727 New_Occurrence_Of (Etype (Actual), Loc))));
1729 -- Generate:
1730 -- Param : Ann;
1732 Append_To (Comps,
1733 Make_Component_Declaration (Loc,
1734 Defining_Identifier =>
1735 Make_Defining_Identifier (Loc, Chars (Formal)),
1736 Component_Definition =>
1737 Make_Component_Definition (Loc,
1738 Aliased_Present =>
1739 False,
1740 Subtype_Indication =>
1741 New_Occurrence_Of (Comp_Nam, Loc))));
1743 Has_Comp := True;
1744 end if;
1746 Next_Actual (Actual);
1747 Next_Formal_With_Extras (Formal);
1748 end loop;
1750 Rec_Nam := Make_Temporary (Loc, 'P');
1752 if Has_Comp then
1754 -- Generate:
1755 -- type Pnn is record
1756 -- Param1 : Ann1;
1757 -- ...
1758 -- ParamN : AnnN;
1760 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1761 -- the original parameter names and Ann1 .. AnnN are the access to
1762 -- actual types.
1764 Append_To (Decls,
1765 Make_Full_Type_Declaration (Loc,
1766 Defining_Identifier =>
1767 Rec_Nam,
1768 Type_Definition =>
1769 Make_Record_Definition (Loc,
1770 Component_List =>
1771 Make_Component_List (Loc, Comps))));
1772 else
1773 -- Generate:
1774 -- type Pnn is null record;
1776 Append_To (Decls,
1777 Make_Full_Type_Declaration (Loc,
1778 Defining_Identifier =>
1779 Rec_Nam,
1780 Type_Definition =>
1781 Make_Record_Definition (Loc,
1782 Null_Present => True,
1783 Component_List => Empty)));
1784 end if;
1786 return Rec_Nam;
1787 end Build_Parameter_Block;
1789 --------------------------------------
1790 -- Build_Renamed_Formal_Declaration --
1791 --------------------------------------
1793 function Build_Renamed_Formal_Declaration
1794 (New_F : Entity_Id;
1795 Formal : Entity_Id;
1796 Comp : Entity_Id;
1797 Renamed_Formal : Node_Id) return Node_Id
1799 Loc : constant Source_Ptr := Sloc (New_F);
1800 Decl : Node_Id;
1802 begin
1803 -- If the formal is a tagged incomplete type, it is already passed
1804 -- by reference, so it is sufficient to rename the pointer component
1805 -- that corresponds to the actual. Otherwise we need to dereference
1806 -- the pointer component to obtain the actual.
1808 if Is_Incomplete_Type (Etype (Formal))
1809 and then Is_Tagged_Type (Etype (Formal))
1810 then
1811 Decl :=
1812 Make_Object_Renaming_Declaration (Loc,
1813 Defining_Identifier => New_F,
1814 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
1815 Name => Renamed_Formal);
1817 else
1818 Decl :=
1819 Make_Object_Renaming_Declaration (Loc,
1820 Defining_Identifier => New_F,
1821 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
1822 Name =>
1823 Make_Explicit_Dereference (Loc, Renamed_Formal));
1824 end if;
1826 return Decl;
1827 end Build_Renamed_Formal_Declaration;
1829 --------------------------
1830 -- Build_Wrapper_Bodies --
1831 --------------------------
1833 procedure Build_Wrapper_Bodies
1834 (Loc : Source_Ptr;
1835 Typ : Entity_Id;
1836 N : Node_Id)
1838 Rec_Typ : Entity_Id;
1840 function Build_Wrapper_Body
1841 (Loc : Source_Ptr;
1842 Subp_Id : Entity_Id;
1843 Obj_Typ : Entity_Id;
1844 Formals : List_Id) return Node_Id;
1845 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
1846 -- associated with a protected or task type. Subp_Id is the subprogram
1847 -- name which will be wrapped. Obj_Typ is the type of the new formal
1848 -- parameter which handles dispatching and object notation. Formals are
1849 -- the original formals of Subp_Id which will be explicitly replicated.
1851 ------------------------
1852 -- Build_Wrapper_Body --
1853 ------------------------
1855 function Build_Wrapper_Body
1856 (Loc : Source_Ptr;
1857 Subp_Id : Entity_Id;
1858 Obj_Typ : Entity_Id;
1859 Formals : List_Id) return Node_Id
1861 Body_Spec : Node_Id;
1863 begin
1864 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
1866 -- The subprogram is not overriding or is not a primitive declared
1867 -- between two views.
1869 if No (Body_Spec) then
1870 return Empty;
1871 end if;
1873 declare
1874 Actuals : List_Id := No_List;
1875 Conv_Id : Node_Id;
1876 First_Form : Node_Id;
1877 Formal : Node_Id;
1878 Nam : Node_Id;
1880 begin
1881 -- Map formals to actuals. Use the list built for the wrapper
1882 -- spec, skipping the object notation parameter.
1884 First_Form := First (Parameter_Specifications (Body_Spec));
1886 Formal := First_Form;
1887 Next (Formal);
1889 if Present (Formal) then
1890 Actuals := New_List;
1891 while Present (Formal) loop
1892 Append_To (Actuals,
1893 Make_Identifier (Loc,
1894 Chars => Chars (Defining_Identifier (Formal))));
1895 Next (Formal);
1896 end loop;
1897 end if;
1899 -- Special processing for primitives declared between a private
1900 -- type and its completion: the wrapper needs a properly typed
1901 -- parameter if the wrapped operation has a controlling first
1902 -- parameter. Note that this might not be the case for a function
1903 -- with a controlling result.
1905 if Is_Private_Primitive_Subprogram (Subp_Id) then
1906 if No (Actuals) then
1907 Actuals := New_List;
1908 end if;
1910 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
1911 Prepend_To (Actuals,
1912 Unchecked_Convert_To
1913 (Corresponding_Concurrent_Type (Obj_Typ),
1914 Make_Identifier (Loc, Name_uO)));
1916 else
1917 Prepend_To (Actuals,
1918 Make_Identifier (Loc,
1919 Chars => Chars (Defining_Identifier (First_Form))));
1920 end if;
1922 Nam := New_Occurrence_Of (Subp_Id, Loc);
1923 else
1924 -- An access-to-variable object parameter requires an explicit
1925 -- dereference in the unchecked conversion. This case occurs
1926 -- when a protected entry wrapper must override an interface
1927 -- level procedure with interface access as first parameter.
1929 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
1931 if Nkind (Parameter_Type (First_Form)) =
1932 N_Access_Definition
1933 then
1934 Conv_Id :=
1935 Make_Explicit_Dereference (Loc,
1936 Prefix => Make_Identifier (Loc, Name_uO));
1937 else
1938 Conv_Id := Make_Identifier (Loc, Name_uO);
1939 end if;
1941 Nam :=
1942 Make_Selected_Component (Loc,
1943 Prefix =>
1944 Unchecked_Convert_To
1945 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
1946 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
1947 end if;
1949 -- Create the subprogram body. For a function, the call to the
1950 -- actual subprogram has to be converted to the corresponding
1951 -- record if it is a controlling result.
1953 if Ekind (Subp_Id) = E_Function then
1954 declare
1955 Res : Node_Id;
1957 begin
1958 Res :=
1959 Make_Function_Call (Loc,
1960 Name => Nam,
1961 Parameter_Associations => Actuals);
1963 if Has_Controlling_Result (Subp_Id) then
1964 Res :=
1965 Unchecked_Convert_To
1966 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
1967 end if;
1969 return
1970 Make_Subprogram_Body (Loc,
1971 Specification => Body_Spec,
1972 Declarations => Empty_List,
1973 Handled_Statement_Sequence =>
1974 Make_Handled_Sequence_Of_Statements (Loc,
1975 Statements => New_List (
1976 Make_Simple_Return_Statement (Loc, Res))));
1977 end;
1979 else
1980 return
1981 Make_Subprogram_Body (Loc,
1982 Specification => Body_Spec,
1983 Declarations => Empty_List,
1984 Handled_Statement_Sequence =>
1985 Make_Handled_Sequence_Of_Statements (Loc,
1986 Statements => New_List (
1987 Make_Procedure_Call_Statement (Loc,
1988 Name => Nam,
1989 Parameter_Associations => Actuals))));
1990 end if;
1991 end;
1992 end Build_Wrapper_Body;
1994 -- Start of processing for Build_Wrapper_Bodies
1996 begin
1997 if Is_Concurrent_Type (Typ) then
1998 Rec_Typ := Corresponding_Record_Type (Typ);
1999 else
2000 Rec_Typ := Typ;
2001 end if;
2003 -- Generate wrapper bodies for a concurrent type which implements an
2004 -- interface.
2006 if Present (Interfaces (Rec_Typ)) then
2007 declare
2008 Insert_Nod : Node_Id;
2009 Prim : Entity_Id;
2010 Prim_Elmt : Elmt_Id;
2011 Prim_Decl : Node_Id;
2012 Subp : Entity_Id;
2013 Wrap_Body : Node_Id;
2014 Wrap_Id : Entity_Id;
2016 begin
2017 Insert_Nod := N;
2019 -- Examine all primitive operations of the corresponding record
2020 -- type, looking for wrapper specs. Generate bodies in order to
2021 -- complete them.
2023 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2024 while Present (Prim_Elmt) loop
2025 Prim := Node (Prim_Elmt);
2027 if (Ekind (Prim) = E_Function
2028 or else Ekind (Prim) = E_Procedure)
2029 and then Is_Primitive_Wrapper (Prim)
2030 then
2031 Subp := Wrapped_Entity (Prim);
2032 Prim_Decl := Parent (Parent (Prim));
2034 Wrap_Body :=
2035 Build_Wrapper_Body (Loc,
2036 Subp_Id => Subp,
2037 Obj_Typ => Rec_Typ,
2038 Formals => Parameter_Specifications (Parent (Subp)));
2039 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2041 Set_Corresponding_Spec (Wrap_Body, Prim);
2042 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2044 Insert_After (Insert_Nod, Wrap_Body);
2045 Insert_Nod := Wrap_Body;
2047 Analyze (Wrap_Body);
2048 end if;
2050 Next_Elmt (Prim_Elmt);
2051 end loop;
2052 end;
2053 end if;
2054 end Build_Wrapper_Bodies;
2056 ------------------------
2057 -- Build_Wrapper_Spec --
2058 ------------------------
2060 function Build_Wrapper_Spec
2061 (Subp_Id : Entity_Id;
2062 Obj_Typ : Entity_Id;
2063 Formals : List_Id) return Node_Id
2065 function Overriding_Possible
2066 (Iface_Op : Entity_Id;
2067 Wrapper : Entity_Id) return Boolean;
2068 -- Determine whether a primitive operation can be overridden by Wrapper.
2069 -- Iface_Op is the candidate primitive operation of an interface type,
2070 -- Wrapper is the generated entry wrapper.
2072 function Replicate_Formals
2073 (Loc : Source_Ptr;
2074 Formals : List_Id) return List_Id;
2075 -- An explicit parameter replication is required due to the Is_Entry_
2076 -- Formal flag being set for all the formals of an entry. The explicit
2077 -- replication removes the flag that would otherwise cause a different
2078 -- path of analysis.
2080 -------------------------
2081 -- Overriding_Possible --
2082 -------------------------
2084 function Overriding_Possible
2085 (Iface_Op : Entity_Id;
2086 Wrapper : Entity_Id) return Boolean
2088 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2089 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2091 function Type_Conformant_Parameters
2092 (Iface_Op_Params : List_Id;
2093 Wrapper_Params : List_Id) return Boolean;
2094 -- Determine whether the parameters of the generated entry wrapper
2095 -- and those of a primitive operation are type conformant. During
2096 -- this check, the first parameter of the primitive operation is
2097 -- skipped if it is a controlling argument: protected functions
2098 -- may have a controlling result.
2100 --------------------------------
2101 -- Type_Conformant_Parameters --
2102 --------------------------------
2104 function Type_Conformant_Parameters
2105 (Iface_Op_Params : List_Id;
2106 Wrapper_Params : List_Id) return Boolean
2108 Iface_Op_Param : Node_Id;
2109 Iface_Op_Typ : Entity_Id;
2110 Wrapper_Param : Node_Id;
2111 Wrapper_Typ : Entity_Id;
2113 begin
2114 -- Skip the first (controlling) parameter of primitive operation
2116 Iface_Op_Param := First (Iface_Op_Params);
2118 if Present (First_Formal (Iface_Op))
2119 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2120 then
2121 Iface_Op_Param := Next (Iface_Op_Param);
2122 end if;
2124 Wrapper_Param := First (Wrapper_Params);
2125 while Present (Iface_Op_Param)
2126 and then Present (Wrapper_Param)
2127 loop
2128 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2129 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2131 -- The two parameters must be mode conformant
2133 if not Conforming_Types
2134 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2135 then
2136 return False;
2137 end if;
2139 Next (Iface_Op_Param);
2140 Next (Wrapper_Param);
2141 end loop;
2143 -- One of the lists is longer than the other
2145 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2146 return False;
2147 end if;
2149 return True;
2150 end Type_Conformant_Parameters;
2152 -- Start of processing for Overriding_Possible
2154 begin
2155 if Chars (Iface_Op) /= Chars (Wrapper) then
2156 return False;
2157 end if;
2159 -- If an inherited subprogram is implemented by a protected procedure
2160 -- or an entry, then the first parameter of the inherited subprogram
2161 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
2163 if Ekind (Iface_Op) = E_Procedure
2164 and then Present (Parameter_Specifications (Iface_Op_Spec))
2165 then
2166 declare
2167 Obj_Param : constant Node_Id :=
2168 First (Parameter_Specifications (Iface_Op_Spec));
2169 begin
2170 if not Out_Present (Obj_Param)
2171 and then Nkind (Parameter_Type (Obj_Param)) /=
2172 N_Access_Definition
2173 then
2174 return False;
2175 end if;
2176 end;
2177 end if;
2179 return
2180 Type_Conformant_Parameters
2181 (Parameter_Specifications (Iface_Op_Spec),
2182 Parameter_Specifications (Wrapper_Spec));
2183 end Overriding_Possible;
2185 -----------------------
2186 -- Replicate_Formals --
2187 -----------------------
2189 function Replicate_Formals
2190 (Loc : Source_Ptr;
2191 Formals : List_Id) return List_Id
2193 New_Formals : constant List_Id := New_List;
2194 Formal : Node_Id;
2195 Param_Type : Node_Id;
2197 begin
2198 Formal := First (Formals);
2200 -- Skip the object parameter when dealing with primitives declared
2201 -- between two views.
2203 if Is_Private_Primitive_Subprogram (Subp_Id)
2204 and then not Has_Controlling_Result (Subp_Id)
2205 then
2206 Formal := Next (Formal);
2207 end if;
2209 while Present (Formal) loop
2211 -- Create an explicit copy of the entry parameter
2213 -- When creating the wrapper subprogram for a primitive operation
2214 -- of a protected interface we must construct an equivalent
2215 -- signature to that of the overriding operation. For regular
2216 -- parameters we can just use the type of the formal, but for
2217 -- access to subprogram parameters we need to reanalyze the
2218 -- parameter type to create local entities for the signature of
2219 -- the subprogram type. Using the entities of the overriding
2220 -- subprogram will result in out-of-scope errors in the back-end.
2222 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2223 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2224 else
2225 Param_Type :=
2226 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2227 end if;
2229 Append_To (New_Formals,
2230 Make_Parameter_Specification (Loc,
2231 Defining_Identifier =>
2232 Make_Defining_Identifier (Loc,
2233 Chars => Chars (Defining_Identifier (Formal))),
2234 In_Present => In_Present (Formal),
2235 Out_Present => Out_Present (Formal),
2236 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2237 Parameter_Type => Param_Type));
2239 Next (Formal);
2240 end loop;
2242 return New_Formals;
2243 end Replicate_Formals;
2245 -- Local variables
2247 Loc : constant Source_Ptr := Sloc (Subp_Id);
2248 First_Param : Node_Id := Empty;
2249 Iface : Entity_Id;
2250 Iface_Elmt : Elmt_Id;
2251 Iface_Op : Entity_Id;
2252 Iface_Op_Elmt : Elmt_Id;
2253 Overridden_Subp : Entity_Id;
2255 -- Start of processing for Build_Wrapper_Spec
2257 begin
2258 -- No point in building wrappers for untagged concurrent types
2260 pragma Assert (Is_Tagged_Type (Obj_Typ));
2262 -- Check if this subprogram has a profile that matches some interface
2263 -- primitive.
2265 Check_Synchronized_Overriding (Subp_Id, Overridden_Subp);
2267 if Present (Overridden_Subp) then
2268 First_Param :=
2269 First (Parameter_Specifications (Parent (Overridden_Subp)));
2271 -- An entry or a protected procedure can override a routine where the
2272 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2273 -- type. Since the wrapper must have the exact same signature as that of
2274 -- the overridden subprogram, we try to find the overriding candidate
2275 -- and use its controlling formal.
2277 -- Check every implemented interface
2279 elsif Present (Interfaces (Obj_Typ)) then
2280 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2281 Search : while Present (Iface_Elmt) loop
2282 Iface := Node (Iface_Elmt);
2284 -- Check every interface primitive
2286 if Present (Primitive_Operations (Iface)) then
2287 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2288 while Present (Iface_Op_Elmt) loop
2289 Iface_Op := Node (Iface_Op_Elmt);
2291 -- Ignore predefined primitives
2293 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2294 Iface_Op := Ultimate_Alias (Iface_Op);
2296 -- The current primitive operation can be overridden by
2297 -- the generated entry wrapper.
2299 if Overriding_Possible (Iface_Op, Subp_Id) then
2300 First_Param :=
2301 First (Parameter_Specifications (Parent (Iface_Op)));
2303 exit Search;
2304 end if;
2305 end if;
2307 Next_Elmt (Iface_Op_Elmt);
2308 end loop;
2309 end if;
2311 Next_Elmt (Iface_Elmt);
2312 end loop Search;
2313 end if;
2315 -- Do not generate the wrapper if no interface primitive is covered by
2316 -- the subprogram and it is not a primitive declared between two views
2317 -- (see Process_Full_View).
2319 if No (First_Param)
2320 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2321 then
2322 return Empty;
2323 end if;
2325 declare
2326 Wrapper_Id : constant Entity_Id :=
2327 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2328 New_Formals : List_Id;
2329 Obj_Param : Node_Id;
2330 Obj_Param_Typ : Entity_Id;
2332 begin
2333 -- Minimum decoration is needed to catch the entity in
2334 -- Sem_Ch6.Override_Dispatching_Operation.
2336 if Ekind (Subp_Id) = E_Function then
2337 Set_Ekind (Wrapper_Id, E_Function);
2338 else
2339 Set_Ekind (Wrapper_Id, E_Procedure);
2340 end if;
2342 Set_Is_Primitive_Wrapper (Wrapper_Id);
2343 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2344 Set_Is_Private_Primitive (Wrapper_Id,
2345 Is_Private_Primitive_Subprogram (Subp_Id));
2347 -- Process the formals
2349 New_Formals := Replicate_Formals (Loc, Formals);
2351 -- A function with a controlling result and no first controlling
2352 -- formal needs no additional parameter.
2354 if Has_Controlling_Result (Subp_Id)
2355 and then
2356 (No (First_Formal (Subp_Id))
2357 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2358 then
2359 null;
2361 -- Routine Subp_Id has been found to override an interface primitive.
2362 -- If the interface operation has an access parameter, create a copy
2363 -- of it, with the same null exclusion indicator if present.
2365 elsif Present (First_Param) then
2366 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2367 Obj_Param_Typ :=
2368 Make_Access_Definition (Loc,
2369 Subtype_Mark =>
2370 New_Occurrence_Of (Obj_Typ, Loc),
2371 Null_Exclusion_Present =>
2372 Null_Exclusion_Present (Parameter_Type (First_Param)),
2373 Constant_Present =>
2374 Constant_Present (Parameter_Type (First_Param)));
2375 else
2376 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2377 end if;
2379 Obj_Param :=
2380 Make_Parameter_Specification (Loc,
2381 Defining_Identifier =>
2382 Make_Defining_Identifier (Loc,
2383 Chars => Name_uO),
2384 In_Present => In_Present (First_Param),
2385 Out_Present => Out_Present (First_Param),
2386 Parameter_Type => Obj_Param_Typ);
2388 Prepend_To (New_Formals, Obj_Param);
2390 -- If we are dealing with a primitive declared between two views,
2391 -- implemented by a synchronized operation, we need to create
2392 -- a default parameter. The mode of the parameter must match that
2393 -- of the primitive operation.
2395 else
2396 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2398 Obj_Param :=
2399 Make_Parameter_Specification (Loc,
2400 Defining_Identifier =>
2401 Make_Defining_Identifier (Loc, Name_uO),
2402 In_Present =>
2403 In_Present (Parent (First_Entity (Subp_Id))),
2404 Out_Present => Ekind (Subp_Id) /= E_Function,
2405 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2407 Prepend_To (New_Formals, Obj_Param);
2408 end if;
2410 -- Build the final spec. If it is a function with a controlling
2411 -- result, it is a primitive operation of the corresponding
2412 -- record type, so mark the spec accordingly.
2414 if Ekind (Subp_Id) = E_Function then
2415 declare
2416 Res_Def : Node_Id;
2418 begin
2419 if Has_Controlling_Result (Subp_Id) then
2420 Res_Def :=
2421 New_Occurrence_Of
2422 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2423 else
2424 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2425 end if;
2427 return
2428 Make_Function_Specification (Loc,
2429 Defining_Unit_Name => Wrapper_Id,
2430 Parameter_Specifications => New_Formals,
2431 Result_Definition => Res_Def);
2432 end;
2433 else
2434 return
2435 Make_Procedure_Specification (Loc,
2436 Defining_Unit_Name => Wrapper_Id,
2437 Parameter_Specifications => New_Formals);
2438 end if;
2439 end;
2440 end Build_Wrapper_Spec;
2442 -------------------------
2443 -- Build_Wrapper_Specs --
2444 -------------------------
2446 procedure Build_Wrapper_Specs
2447 (Loc : Source_Ptr;
2448 Typ : Entity_Id;
2449 N : in out Node_Id)
2451 Def : Node_Id;
2452 Rec_Typ : Entity_Id;
2453 procedure Scan_Declarations (L : List_Id);
2454 -- Common processing for visible and private declarations
2455 -- of a protected type.
2457 procedure Scan_Declarations (L : List_Id) is
2458 Decl : Node_Id;
2459 Wrap_Decl : Node_Id;
2460 Wrap_Spec : Node_Id;
2462 begin
2463 if No (L) then
2464 return;
2465 end if;
2467 Decl := First (L);
2468 while Present (Decl) loop
2469 Wrap_Spec := Empty;
2471 if Nkind (Decl) = N_Entry_Declaration
2472 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2473 then
2474 Wrap_Spec :=
2475 Build_Wrapper_Spec
2476 (Subp_Id => Defining_Identifier (Decl),
2477 Obj_Typ => Rec_Typ,
2478 Formals => Parameter_Specifications (Decl));
2480 elsif Nkind (Decl) = N_Subprogram_Declaration then
2481 Wrap_Spec :=
2482 Build_Wrapper_Spec
2483 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2484 Obj_Typ => Rec_Typ,
2485 Formals =>
2486 Parameter_Specifications (Specification (Decl)));
2487 end if;
2489 if Present (Wrap_Spec) then
2490 Wrap_Decl :=
2491 Make_Subprogram_Declaration (Loc,
2492 Specification => Wrap_Spec);
2494 Insert_After (N, Wrap_Decl);
2495 N := Wrap_Decl;
2497 Analyze (Wrap_Decl);
2498 end if;
2500 Next (Decl);
2501 end loop;
2502 end Scan_Declarations;
2504 -- start of processing for Build_Wrapper_Specs
2506 begin
2507 if Is_Protected_Type (Typ) then
2508 Def := Protected_Definition (Parent (Typ));
2509 else pragma Assert (Is_Task_Type (Typ));
2510 Def := Task_Definition (Parent (Typ));
2511 end if;
2513 Rec_Typ := Corresponding_Record_Type (Typ);
2515 -- Generate wrapper specs for a concurrent type which implements an
2516 -- interface. Operations in both the visible and private parts may
2517 -- implement progenitor operations.
2519 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2520 Scan_Declarations (Visible_Declarations (Def));
2521 Scan_Declarations (Private_Declarations (Def));
2522 end if;
2523 end Build_Wrapper_Specs;
2525 ---------------------------
2526 -- Build_Find_Body_Index --
2527 ---------------------------
2529 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2530 Loc : constant Source_Ptr := Sloc (Typ);
2531 Ent : Entity_Id;
2532 E_Typ : Entity_Id;
2533 Has_F : Boolean := False;
2534 Index : Nat;
2535 If_St : Node_Id := Empty;
2536 Lo : Node_Id;
2537 Hi : Node_Id;
2538 Decls : List_Id := New_List;
2539 Ret : Node_Id;
2540 Spec : Node_Id;
2541 Siz : Node_Id := Empty;
2543 procedure Add_If_Clause (Expr : Node_Id);
2544 -- Add test for range of current entry
2546 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2547 -- If a bound of an entry is given by a discriminant, retrieve the
2548 -- actual value of the discriminant from the enclosing object.
2550 -------------------
2551 -- Add_If_Clause --
2552 -------------------
2554 procedure Add_If_Clause (Expr : Node_Id) is
2555 Cond : Node_Id;
2556 Stats : constant List_Id :=
2557 New_List (
2558 Make_Simple_Return_Statement (Loc,
2559 Expression => Make_Integer_Literal (Loc, Index + 1)));
2561 begin
2562 -- Index for current entry body
2564 Index := Index + 1;
2566 -- Compute total length of entry queues so far
2568 if No (Siz) then
2569 Siz := Expr;
2570 else
2571 Siz :=
2572 Make_Op_Add (Loc,
2573 Left_Opnd => Siz,
2574 Right_Opnd => Expr);
2575 end if;
2577 Cond :=
2578 Make_Op_Le (Loc,
2579 Left_Opnd => Make_Identifier (Loc, Name_uE),
2580 Right_Opnd => Siz);
2582 -- Map entry queue indexes in the range of the current family
2583 -- into the current index, that designates the entry body.
2585 if No (If_St) then
2586 If_St :=
2587 Make_Implicit_If_Statement (Typ,
2588 Condition => Cond,
2589 Then_Statements => Stats,
2590 Elsif_Parts => New_List);
2591 Ret := If_St;
2593 else
2594 Append_To (Elsif_Parts (If_St),
2595 Make_Elsif_Part (Loc,
2596 Condition => Cond,
2597 Then_Statements => Stats));
2598 end if;
2599 end Add_If_Clause;
2601 ------------------------------
2602 -- Convert_Discriminant_Ref --
2603 ------------------------------
2605 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2606 B : Node_Id;
2608 begin
2609 if Is_Entity_Name (Bound)
2610 and then Ekind (Entity (Bound)) = E_Discriminant
2611 then
2612 B :=
2613 Make_Selected_Component (Loc,
2614 Prefix =>
2615 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2616 Make_Explicit_Dereference (Loc,
2617 Make_Identifier (Loc, Name_uObject))),
2618 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2619 Set_Etype (B, Etype (Entity (Bound)));
2620 else
2621 B := New_Copy_Tree (Bound);
2622 end if;
2624 return B;
2625 end Convert_Discriminant_Ref;
2627 -- Start of processing for Build_Find_Body_Index
2629 begin
2630 Spec := Build_Find_Body_Index_Spec (Typ);
2632 Ent := First_Entity (Typ);
2633 while Present (Ent) loop
2634 if Ekind (Ent) = E_Entry_Family then
2635 Has_F := True;
2636 exit;
2637 end if;
2639 Next_Entity (Ent);
2640 end loop;
2642 if not Has_F then
2644 -- If the protected type has no entry families, there is a one-one
2645 -- correspondence between entry queue and entry body.
2647 Ret :=
2648 Make_Simple_Return_Statement (Loc,
2649 Expression => Make_Identifier (Loc, Name_uE));
2651 else
2652 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2653 -- the following:
2655 -- if E <= l1 then return 1;
2656 -- elsif E <= l1 + l2 then return 2;
2657 -- ...
2659 Index := 0;
2660 Siz := Empty;
2661 Ent := First_Entity (Typ);
2663 Add_Object_Pointer (Loc, Typ, Decls);
2665 while Present (Ent) loop
2666 if Ekind (Ent) = E_Entry then
2667 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2669 elsif Ekind (Ent) = E_Entry_Family then
2670 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2671 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2672 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2673 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2674 end if;
2676 Next_Entity (Ent);
2677 end loop;
2679 if Index = 1 then
2680 Decls := New_List;
2681 Ret :=
2682 Make_Simple_Return_Statement (Loc,
2683 Expression => Make_Integer_Literal (Loc, 1));
2685 elsif Nkind (Ret) = N_If_Statement then
2687 -- Ranges are in increasing order, so last one doesn't need guard
2689 declare
2690 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2691 begin
2692 Remove (Nod);
2693 Set_Else_Statements (Ret, Then_Statements (Nod));
2694 end;
2695 end if;
2696 end if;
2698 return
2699 Make_Subprogram_Body (Loc,
2700 Specification => Spec,
2701 Declarations => Decls,
2702 Handled_Statement_Sequence =>
2703 Make_Handled_Sequence_Of_Statements (Loc,
2704 Statements => New_List (Ret)));
2705 end Build_Find_Body_Index;
2707 --------------------------------
2708 -- Build_Find_Body_Index_Spec --
2709 --------------------------------
2711 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2712 Loc : constant Source_Ptr := Sloc (Typ);
2713 Id : constant Entity_Id :=
2714 Make_Defining_Identifier (Loc,
2715 Chars => New_External_Name (Chars (Typ), 'F'));
2716 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2717 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2719 begin
2720 return
2721 Make_Function_Specification (Loc,
2722 Defining_Unit_Name => Id,
2723 Parameter_Specifications => New_List (
2724 Make_Parameter_Specification (Loc,
2725 Defining_Identifier => Parm1,
2726 Parameter_Type =>
2727 New_Occurrence_Of (RTE (RE_Address), Loc)),
2729 Make_Parameter_Specification (Loc,
2730 Defining_Identifier => Parm2,
2731 Parameter_Type =>
2732 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
2734 Result_Definition => New_Occurrence_Of (
2735 RTE (RE_Protected_Entry_Index), Loc));
2736 end Build_Find_Body_Index_Spec;
2738 -----------------------------------------------
2739 -- Build_Lock_Free_Protected_Subprogram_Body --
2740 -----------------------------------------------
2742 function Build_Lock_Free_Protected_Subprogram_Body
2743 (N : Node_Id;
2744 Prot_Typ : Node_Id;
2745 Unprot_Spec : Node_Id) return Node_Id
2747 Actuals : constant List_Id := New_List;
2748 Loc : constant Source_Ptr := Sloc (N);
2749 Spec : constant Node_Id := Specification (N);
2750 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
2751 Formal : Node_Id;
2752 Prot_Spec : Node_Id;
2753 Stmt : Node_Id;
2755 begin
2756 -- Create the protected version of the body
2758 Prot_Spec :=
2759 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
2761 -- Build the actual parameters which appear in the call to the
2762 -- unprotected version of the body.
2764 Formal := First (Parameter_Specifications (Prot_Spec));
2765 while Present (Formal) loop
2766 Append_To (Actuals,
2767 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2769 Next (Formal);
2770 end loop;
2772 -- Function case, generate:
2773 -- return <Unprot_Func_Call>;
2775 if Nkind (Spec) = N_Function_Specification then
2776 Stmt :=
2777 Make_Simple_Return_Statement (Loc,
2778 Expression =>
2779 Make_Function_Call (Loc,
2780 Name =>
2781 Make_Identifier (Loc, Chars (Unprot_Id)),
2782 Parameter_Associations => Actuals));
2784 -- Procedure case, call the unprotected version
2786 else
2787 Stmt :=
2788 Make_Procedure_Call_Statement (Loc,
2789 Name =>
2790 Make_Identifier (Loc, Chars (Unprot_Id)),
2791 Parameter_Associations => Actuals);
2792 end if;
2794 return
2795 Make_Subprogram_Body (Loc,
2796 Declarations => Empty_List,
2797 Specification => Prot_Spec,
2798 Handled_Statement_Sequence =>
2799 Make_Handled_Sequence_Of_Statements (Loc,
2800 Statements => New_List (Stmt)));
2801 end Build_Lock_Free_Protected_Subprogram_Body;
2803 -------------------------------------------------
2804 -- Build_Lock_Free_Unprotected_Subprogram_Body --
2805 -------------------------------------------------
2807 -- Procedures which meet the lock-free implementation requirements and
2808 -- reference a unique scalar component Comp are expanded in the following
2809 -- manner:
2811 -- procedure P (...) is
2812 -- Expected_Comp : constant Comp_Type :=
2813 -- Comp_Type
2814 -- (System.Atomic_Primitives.Lock_Free_Read_N
2815 -- (_Object.Comp'Address));
2816 -- begin
2817 -- loop
2818 -- declare
2819 -- <original declarations before the object renaming declaration
2820 -- of Comp>
2822 -- Desired_Comp : Comp_Type := Expected_Comp;
2823 -- Comp : Comp_Type renames Desired_Comp;
2825 -- <original delarations after the object renaming declaration
2826 -- of Comp>
2828 -- begin
2829 -- <original statements>
2830 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
2831 -- (_Object.Comp'Address,
2832 -- Interfaces.Unsigned_N (Expected_Comp),
2833 -- Interfaces.Unsigned_N (Desired_Comp));
2834 -- end;
2835 -- end loop;
2836 -- end P;
2838 -- Each return and raise statement of P is transformed into an atomic
2839 -- status check:
2841 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2842 -- (_Object.Comp'Address,
2843 -- Interfaces.Unsigned_N (Expected_Comp),
2844 -- Interfaces.Unsigned_N (Desired_Comp));
2845 -- then
2846 -- <original statement>
2847 -- else
2848 -- goto L0;
2849 -- end if;
2851 -- Functions which meet the lock-free implementation requirements and
2852 -- reference a unique scalar component Comp are expanded in the following
2853 -- manner:
2855 -- function F (...) return ... is
2856 -- <original declarations before the object renaming declaration
2857 -- of Comp>
2859 -- Expected_Comp : constant Comp_Type :=
2860 -- Comp_Type
2861 -- (System.Atomic_Primitives.Lock_Free_Read_N
2862 -- (_Object.Comp'Address));
2863 -- Comp : Comp_Type renames Expected_Comp;
2865 -- <original delarations after the object renaming declaration of
2866 -- Comp>
2868 -- begin
2869 -- <original statements>
2870 -- end F;
2872 function Build_Lock_Free_Unprotected_Subprogram_Body
2873 (N : Node_Id;
2874 Prot_Typ : Node_Id) return Node_Id
2876 function Referenced_Component (N : Node_Id) return Entity_Id;
2877 -- Subprograms which meet the lock-free implementation criteria are
2878 -- allowed to reference only one unique component. Return the prival
2879 -- of the said component.
2881 --------------------------
2882 -- Referenced_Component --
2883 --------------------------
2885 function Referenced_Component (N : Node_Id) return Entity_Id is
2886 Comp : Entity_Id;
2887 Decl : Node_Id;
2888 Source_Comp : Entity_Id := Empty;
2890 begin
2891 -- Find the unique source component which N references in its
2892 -- statements.
2894 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
2895 declare
2896 Element : Lock_Free_Subprogram renames
2897 Lock_Free_Subprogram_Table.Table (Index);
2898 begin
2899 if Element.Sub_Body = N then
2900 Source_Comp := Element.Comp_Id;
2901 exit;
2902 end if;
2903 end;
2904 end loop;
2906 if No (Source_Comp) then
2907 return Empty;
2908 end if;
2910 -- Find the prival which corresponds to the source component within
2911 -- the declarations of N.
2913 Decl := First (Declarations (N));
2914 while Present (Decl) loop
2916 -- Privals appear as object renamings
2918 if Nkind (Decl) = N_Object_Renaming_Declaration then
2919 Comp := Defining_Identifier (Decl);
2921 if Present (Prival_Link (Comp))
2922 and then Prival_Link (Comp) = Source_Comp
2923 then
2924 return Comp;
2925 end if;
2926 end if;
2928 Next (Decl);
2929 end loop;
2931 return Empty;
2932 end Referenced_Component;
2934 -- Local variables
2936 Comp : constant Entity_Id := Referenced_Component (N);
2937 Loc : constant Source_Ptr := Sloc (N);
2938 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
2939 Decls : List_Id := Declarations (N);
2941 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
2943 begin
2944 -- Add renamings for the protection object, discriminals, privals, and
2945 -- the entry index constant for use by debugger.
2947 Debug_Private_Data_Declarations (Decls);
2949 -- Perform the lock-free expansion when the subprogram references a
2950 -- protected component.
2952 if Present (Comp) then
2953 Protected_Component_Ref : declare
2954 Comp_Decl : constant Node_Id := Parent (Comp);
2955 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
2956 Comp_Type : constant Entity_Id := Etype (Comp);
2958 Is_Procedure : constant Boolean :=
2959 Ekind (Corresponding_Spec (N)) = E_Procedure;
2960 -- Indicates if N is a protected procedure body
2962 Block_Decls : List_Id := No_List;
2963 Try_Write : Entity_Id;
2964 Desired_Comp : Entity_Id;
2965 Decl : Node_Id;
2966 Label : Node_Id;
2967 Label_Id : Entity_Id := Empty;
2968 Read : Entity_Id;
2969 Expected_Comp : Entity_Id;
2970 Stmt : Node_Id;
2971 Stmts : List_Id :=
2972 New_Copy_List (Statements (Hand_Stmt_Seq));
2973 Typ_Size : Int;
2974 Unsigned : Entity_Id;
2976 function Process_Node (N : Node_Id) return Traverse_Result;
2977 -- Transform a single node if it is a return statement, a raise
2978 -- statement or a reference to Comp.
2980 procedure Process_Stmts (Stmts : List_Id);
2981 -- Given a statement sequence Stmts, wrap any return or raise
2982 -- statements in the following manner:
2984 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2985 -- (_Object.Comp'Address,
2986 -- Interfaces.Unsigned_N (Expected_Comp),
2987 -- Interfaces.Unsigned_N (Desired_Comp))
2988 -- then
2989 -- <Stmt>;
2990 -- else
2991 -- goto L0;
2992 -- end if;
2994 ------------------
2995 -- Process_Node --
2996 ------------------
2998 function Process_Node (N : Node_Id) return Traverse_Result is
3000 procedure Wrap_Statement (Stmt : Node_Id);
3001 -- Wrap an arbitrary statement inside an if statement where the
3002 -- condition does an atomic check on the state of the object.
3004 --------------------
3005 -- Wrap_Statement --
3006 --------------------
3008 procedure Wrap_Statement (Stmt : Node_Id) is
3009 begin
3010 -- The first time through, create the declaration of a label
3011 -- which is used to skip the remainder of source statements
3012 -- if the state of the object has changed.
3014 if No (Label_Id) then
3015 Label_Id :=
3016 Make_Identifier (Loc, New_External_Name ('L', 0));
3017 Set_Entity (Label_Id,
3018 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3019 end if;
3021 -- Generate:
3022 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3023 -- (_Object.Comp'Address,
3024 -- Interfaces.Unsigned_N (Expected_Comp),
3025 -- Interfaces.Unsigned_N (Desired_Comp))
3026 -- then
3027 -- <Stmt>;
3028 -- else
3029 -- goto L0;
3030 -- end if;
3032 Rewrite (Stmt,
3033 Make_Implicit_If_Statement (N,
3034 Condition =>
3035 Make_Function_Call (Loc,
3036 Name =>
3037 New_Occurrence_Of (Try_Write, Loc),
3038 Parameter_Associations => New_List (
3039 Make_Attribute_Reference (Loc,
3040 Prefix => Relocate_Node (Comp_Sel_Nam),
3041 Attribute_Name => Name_Address),
3043 Unchecked_Convert_To (Unsigned,
3044 New_Occurrence_Of (Expected_Comp, Loc)),
3046 Unchecked_Convert_To (Unsigned,
3047 New_Occurrence_Of (Desired_Comp, Loc)))),
3049 Then_Statements => New_List (Relocate_Node (Stmt)),
3051 Else_Statements => New_List (
3052 Make_Goto_Statement (Loc,
3053 Name =>
3054 New_Occurrence_Of (Entity (Label_Id), Loc)))));
3055 end Wrap_Statement;
3057 -- Start of processing for Process_Node
3059 begin
3060 -- Wrap each return and raise statement that appear inside a
3061 -- procedure. Skip the last return statement which is added by
3062 -- default since it is transformed into an exit statement.
3064 if Is_Procedure
3065 and then ((Nkind (N) = N_Simple_Return_Statement
3066 and then N /= Last (Stmts))
3067 or else Nkind (N) = N_Extended_Return_Statement
3068 or else (Nkind_In (N, N_Raise_Constraint_Error,
3069 N_Raise_Program_Error,
3070 N_Raise_Statement,
3071 N_Raise_Storage_Error)
3072 and then Comes_From_Source (N)))
3073 then
3074 Wrap_Statement (N);
3075 return Skip;
3076 end if;
3078 -- Force reanalysis
3080 Set_Analyzed (N, False);
3082 return OK;
3083 end Process_Node;
3085 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3087 -------------------
3088 -- Process_Stmts --
3089 -------------------
3091 procedure Process_Stmts (Stmts : List_Id) is
3092 Stmt : Node_Id;
3093 begin
3094 Stmt := First (Stmts);
3095 while Present (Stmt) loop
3096 Process_Nodes (Stmt);
3097 Next (Stmt);
3098 end loop;
3099 end Process_Stmts;
3101 -- Start of processing for Protected_Component_Ref
3103 begin
3104 -- Get the type size
3106 if Known_Static_Esize (Comp_Type) then
3107 Typ_Size := UI_To_Int (Esize (Comp_Type));
3109 -- If the Esize (Object_Size) is unknown at compile time, look at
3110 -- the RM_Size (Value_Size) since it may have been set by an
3111 -- explicit representation clause.
3113 elsif Known_Static_RM_Size (Comp_Type) then
3114 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3116 -- Should not happen since this has already been checked in
3117 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3119 else
3120 raise Program_Error;
3121 end if;
3123 -- Retrieve all relevant atomic routines and types
3125 case Typ_Size is
3126 when 8 =>
3127 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3128 Read := RTE (RE_Lock_Free_Read_8);
3129 Unsigned := RTE (RE_Uint8);
3131 when 16 =>
3132 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3133 Read := RTE (RE_Lock_Free_Read_16);
3134 Unsigned := RTE (RE_Uint16);
3136 when 32 =>
3137 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3138 Read := RTE (RE_Lock_Free_Read_32);
3139 Unsigned := RTE (RE_Uint32);
3141 when 64 =>
3142 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3143 Read := RTE (RE_Lock_Free_Read_64);
3144 Unsigned := RTE (RE_Uint64);
3146 when others =>
3147 raise Program_Error;
3148 end case;
3150 -- Generate:
3151 -- Expected_Comp : constant Comp_Type :=
3152 -- Comp_Type
3153 -- (System.Atomic_Primitives.Lock_Free_Read_N
3154 -- (_Object.Comp'Address));
3156 Expected_Comp :=
3157 Make_Defining_Identifier (Loc,
3158 New_External_Name (Chars (Comp), Suffix => "_saved"));
3160 Decl :=
3161 Make_Object_Declaration (Loc,
3162 Defining_Identifier => Expected_Comp,
3163 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3164 Constant_Present => True,
3165 Expression =>
3166 Unchecked_Convert_To (Comp_Type,
3167 Make_Function_Call (Loc,
3168 Name => New_Occurrence_Of (Read, Loc),
3169 Parameter_Associations => New_List (
3170 Make_Attribute_Reference (Loc,
3171 Prefix => Relocate_Node (Comp_Sel_Nam),
3172 Attribute_Name => Name_Address)))));
3174 -- Protected procedures
3176 if Is_Procedure then
3177 -- Move the original declarations inside the generated block
3179 Block_Decls := Decls;
3181 -- Reset the declarations list of the protected procedure to
3182 -- contain only Decl.
3184 Decls := New_List (Decl);
3186 -- Generate:
3187 -- Desired_Comp : Comp_Type := Expected_Comp;
3189 Desired_Comp :=
3190 Make_Defining_Identifier (Loc,
3191 New_External_Name (Chars (Comp), Suffix => "_current"));
3193 -- Insert the declarations of Expected_Comp and Desired_Comp in
3194 -- the block declarations right before the renaming of the
3195 -- protected component.
3197 Insert_Before (Comp_Decl,
3198 Make_Object_Declaration (Loc,
3199 Defining_Identifier => Desired_Comp,
3200 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3201 Expression =>
3202 New_Occurrence_Of (Expected_Comp, Loc)));
3204 -- Protected function
3206 else
3207 Desired_Comp := Expected_Comp;
3209 -- Insert the declaration of Expected_Comp in the function
3210 -- declarations right before the renaming of the protected
3211 -- component.
3213 Insert_Before (Comp_Decl, Decl);
3214 end if;
3216 -- Rewrite the protected component renaming declaration to be a
3217 -- renaming of Desired_Comp.
3219 -- Generate:
3220 -- Comp : Comp_Type renames Desired_Comp;
3222 Rewrite (Comp_Decl,
3223 Make_Object_Renaming_Declaration (Loc,
3224 Defining_Identifier =>
3225 Defining_Identifier (Comp_Decl),
3226 Subtype_Mark =>
3227 New_Occurrence_Of (Comp_Type, Loc),
3228 Name =>
3229 New_Occurrence_Of (Desired_Comp, Loc)));
3231 -- Wrap any return or raise statements in Stmts in same the manner
3232 -- described in Process_Stmts.
3234 Process_Stmts (Stmts);
3236 -- Generate:
3237 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3238 -- (_Object.Comp'Address,
3239 -- Interfaces.Unsigned_N (Expected_Comp),
3240 -- Interfaces.Unsigned_N (Desired_Comp))
3242 if Is_Procedure then
3243 Stmt :=
3244 Make_Exit_Statement (Loc,
3245 Condition =>
3246 Make_Function_Call (Loc,
3247 Name =>
3248 New_Occurrence_Of (Try_Write, Loc),
3249 Parameter_Associations => New_List (
3250 Make_Attribute_Reference (Loc,
3251 Prefix => Relocate_Node (Comp_Sel_Nam),
3252 Attribute_Name => Name_Address),
3254 Unchecked_Convert_To (Unsigned,
3255 New_Occurrence_Of (Expected_Comp, Loc)),
3257 Unchecked_Convert_To (Unsigned,
3258 New_Occurrence_Of (Desired_Comp, Loc)))));
3260 -- Small optimization: transform the default return statement
3261 -- of a procedure into the atomic exit statement.
3263 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3264 Rewrite (Last (Stmts), Stmt);
3265 else
3266 Append_To (Stmts, Stmt);
3267 end if;
3268 end if;
3270 -- Create the declaration of the label used to skip the rest of
3271 -- the source statements when the object state changes.
3273 if Present (Label_Id) then
3274 Label := Make_Label (Loc, Label_Id);
3275 Append_To (Decls,
3276 Make_Implicit_Label_Declaration (Loc,
3277 Defining_Identifier => Entity (Label_Id),
3278 Label_Construct => Label));
3279 Append_To (Stmts, Label);
3280 end if;
3282 -- Generate:
3283 -- loop
3284 -- declare
3285 -- <Decls>
3286 -- begin
3287 -- <Stmts>
3288 -- end;
3289 -- end loop;
3291 if Is_Procedure then
3292 Stmts :=
3293 New_List (
3294 Make_Loop_Statement (Loc,
3295 Statements => New_List (
3296 Make_Block_Statement (Loc,
3297 Declarations => Block_Decls,
3298 Handled_Statement_Sequence =>
3299 Make_Handled_Sequence_Of_Statements (Loc,
3300 Statements => Stmts))),
3301 End_Label => Empty));
3302 end if;
3304 Hand_Stmt_Seq :=
3305 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3306 end Protected_Component_Ref;
3307 end if;
3309 -- Make an unprotected version of the subprogram for use within the same
3310 -- object, with new name and extra parameter representing the object.
3312 return
3313 Make_Subprogram_Body (Loc,
3314 Specification =>
3315 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3316 Declarations => Decls,
3317 Handled_Statement_Sequence => Hand_Stmt_Seq);
3318 end Build_Lock_Free_Unprotected_Subprogram_Body;
3320 -------------------------
3321 -- Build_Master_Entity --
3322 -------------------------
3324 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3325 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3326 Context : Node_Id;
3327 Context_Id : Entity_Id;
3328 Decl : Node_Id;
3329 Decls : List_Id;
3330 Par : Node_Id;
3332 begin
3333 if Is_Itype (Obj_Or_Typ) then
3334 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3335 else
3336 Par := Parent (Obj_Or_Typ);
3337 end if;
3339 -- When creating a master for a record component which is either a task
3340 -- or access-to-task, the enclosing record is the master scope and the
3341 -- proper insertion point is the component list.
3343 if Is_Record_Type (Current_Scope) then
3344 Context := Par;
3345 Context_Id := Current_Scope;
3346 Decls := List_Containing (Context);
3348 -- Default case for object declarations and access types. Note that the
3349 -- context is updated to the nearest enclosing body, block, package, or
3350 -- return statement.
3352 else
3353 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3354 end if;
3356 -- Nothing to do if the context already has a master
3358 if Has_Master_Entity (Context_Id) then
3359 return;
3361 -- Nothing to do if tasks or tasking hierarchies are prohibited
3363 elsif Restriction_Active (No_Tasking)
3364 or else Restriction_Active (No_Task_Hierarchy)
3365 then
3366 return;
3367 end if;
3369 -- Create a master, generate:
3370 -- _Master : constant Master_Id := Current_Master.all;
3372 Decl :=
3373 Make_Object_Declaration (Loc,
3374 Defining_Identifier =>
3375 Make_Defining_Identifier (Loc, Name_uMaster),
3376 Constant_Present => True,
3377 Object_Definition => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3378 Expression =>
3379 Make_Explicit_Dereference (Loc,
3380 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
3382 -- The master is inserted at the start of the declarative list of the
3383 -- context.
3385 Prepend_To (Decls, Decl);
3387 -- In certain cases where transient scopes are involved, the immediate
3388 -- scope is not always the proper master scope. Ensure that the master
3389 -- declaration and entity appear in the same context.
3391 if Context_Id /= Current_Scope then
3392 Push_Scope (Context_Id);
3393 Analyze (Decl);
3394 Pop_Scope;
3395 else
3396 Analyze (Decl);
3397 end if;
3399 -- Mark the enclosing scope and its associated construct as being task
3400 -- masters.
3402 Set_Has_Master_Entity (Context_Id);
3404 while Present (Context)
3405 and then Nkind (Context) /= N_Compilation_Unit
3406 loop
3407 if Nkind_In (Context, N_Block_Statement,
3408 N_Subprogram_Body,
3409 N_Task_Body)
3410 then
3411 Set_Is_Task_Master (Context);
3412 exit;
3414 elsif Nkind (Parent (Context)) = N_Subunit then
3415 Context := Corresponding_Stub (Parent (Context));
3416 end if;
3418 Context := Parent (Context);
3419 end loop;
3420 end Build_Master_Entity;
3422 ---------------------------
3423 -- Build_Master_Renaming --
3424 ---------------------------
3426 procedure Build_Master_Renaming
3427 (Ptr_Typ : Entity_Id;
3428 Ins_Nod : Node_Id := Empty)
3430 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3431 Context : Node_Id;
3432 Master_Decl : Node_Id;
3433 Master_Id : Entity_Id;
3435 begin
3436 -- Nothing to do if tasks or tasking hierarchies are prohibited
3438 if Restriction_Active (No_Tasking)
3439 or else Restriction_Active (No_Task_Hierarchy)
3440 then
3441 return;
3442 end if;
3444 -- Determine the proper context to insert the master renaming
3446 if Present (Ins_Nod) then
3447 Context := Ins_Nod;
3448 elsif Is_Itype (Ptr_Typ) then
3449 Context := Associated_Node_For_Itype (Ptr_Typ);
3450 else
3451 Context := Parent (Ptr_Typ);
3452 end if;
3454 -- Generate:
3455 -- <Ptr_Typ>M : Master_Id renames _Master;
3457 Master_Id :=
3458 Make_Defining_Identifier (Loc,
3459 New_External_Name (Chars (Ptr_Typ), 'M'));
3461 Master_Decl :=
3462 Make_Object_Renaming_Declaration (Loc,
3463 Defining_Identifier => Master_Id,
3464 Subtype_Mark => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3465 Name => Make_Identifier (Loc, Name_uMaster));
3467 Insert_Action (Context, Master_Decl);
3469 -- The renamed master now services the access type
3471 Set_Master_Id (Ptr_Typ, Master_Id);
3472 end Build_Master_Renaming;
3474 -----------------------------------------
3475 -- Build_Private_Protected_Declaration --
3476 -----------------------------------------
3478 function Build_Private_Protected_Declaration
3479 (N : Node_Id) return Entity_Id
3481 Loc : constant Source_Ptr := Sloc (N);
3482 Body_Id : constant Entity_Id := Defining_Entity (N);
3483 Decl : Node_Id;
3484 Plist : List_Id;
3485 Formal : Entity_Id;
3486 New_Spec : Node_Id;
3487 Spec_Id : Entity_Id;
3489 begin
3490 Formal := First_Formal (Body_Id);
3492 -- The protected operation always has at least one formal, namely the
3493 -- object itself, but it is only placed in the parameter list if
3494 -- expansion is enabled.
3496 if Present (Formal) or else Expander_Active then
3497 Plist := Copy_Parameter_List (Body_Id);
3498 else
3499 Plist := No_List;
3500 end if;
3502 if Nkind (Specification (N)) = N_Procedure_Specification then
3503 New_Spec :=
3504 Make_Procedure_Specification (Loc,
3505 Defining_Unit_Name =>
3506 Make_Defining_Identifier (Sloc (Body_Id),
3507 Chars => Chars (Body_Id)),
3508 Parameter_Specifications =>
3509 Plist);
3510 else
3511 New_Spec :=
3512 Make_Function_Specification (Loc,
3513 Defining_Unit_Name =>
3514 Make_Defining_Identifier (Sloc (Body_Id),
3515 Chars => Chars (Body_Id)),
3516 Parameter_Specifications => Plist,
3517 Result_Definition =>
3518 New_Occurrence_Of (Etype (Body_Id), Loc));
3519 end if;
3521 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3522 Insert_Before (N, Decl);
3523 Spec_Id := Defining_Unit_Name (New_Spec);
3525 -- Indicate that the entity comes from source, to ensure that cross-
3526 -- reference information is properly generated. The body itself is
3527 -- rewritten during expansion, and the body entity will not appear in
3528 -- calls to the operation.
3530 Set_Comes_From_Source (Spec_Id, True);
3531 Analyze (Decl);
3532 Set_Has_Completion (Spec_Id);
3533 Set_Convention (Spec_Id, Convention_Protected);
3534 return Spec_Id;
3535 end Build_Private_Protected_Declaration;
3537 ---------------------------
3538 -- Build_Protected_Entry --
3539 ---------------------------
3541 function Build_Protected_Entry
3542 (N : Node_Id;
3543 Ent : Entity_Id;
3544 Pid : Node_Id) return Node_Id
3546 Bod_Decls : constant List_Id := New_List;
3547 Decls : constant List_Id := Declarations (N);
3548 End_Lab : constant Node_Id :=
3549 End_Label (Handled_Statement_Sequence (N));
3550 End_Loc : constant Source_Ptr :=
3551 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3552 -- Used for the generated call to Complete_Entry_Body
3554 Loc : constant Source_Ptr := Sloc (N);
3556 Bod_Id : Entity_Id;
3557 Bod_Spec : Node_Id;
3558 Bod_Stmts : List_Id;
3559 Complete : Node_Id;
3560 Ohandle : Node_Id;
3562 EH_Loc : Source_Ptr;
3563 -- Used for the exception handler, inserted at end of the body
3565 begin
3566 -- Set the source location on the exception handler only when debugging
3567 -- the expanded code (see Make_Implicit_Exception_Handler).
3569 if Debug_Generated_Code then
3570 EH_Loc := End_Loc;
3572 -- Otherwise the inserted code should not be visible to the debugger
3574 else
3575 EH_Loc := No_Location;
3576 end if;
3578 Bod_Id :=
3579 Make_Defining_Identifier (Loc,
3580 Chars => Chars (Protected_Body_Subprogram (Ent)));
3581 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3583 -- Add the following declarations:
3585 -- type poVP is access poV;
3586 -- _object : poVP := poVP (_O);
3588 -- where _O is the formal parameter associated with the concurrent
3589 -- object. These declarations are needed for Complete_Entry_Body.
3591 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3593 -- Add renamings for all formals, the Protection object, discriminals,
3594 -- privals and the entry index constant for use by debugger.
3596 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3597 Debug_Private_Data_Declarations (Decls);
3599 -- Put the declarations and the statements from the entry
3601 Bod_Stmts :=
3602 New_List (
3603 Make_Block_Statement (Loc,
3604 Declarations => Decls,
3605 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3607 case Corresponding_Runtime_Package (Pid) is
3608 when System_Tasking_Protected_Objects_Entries =>
3609 Append_To (Bod_Stmts,
3610 Make_Procedure_Call_Statement (End_Loc,
3611 Name =>
3612 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
3613 Parameter_Associations => New_List (
3614 Make_Attribute_Reference (End_Loc,
3615 Prefix =>
3616 Make_Selected_Component (End_Loc,
3617 Prefix =>
3618 Make_Identifier (End_Loc, Name_uObject),
3619 Selector_Name =>
3620 Make_Identifier (End_Loc, Name_uObject)),
3621 Attribute_Name => Name_Unchecked_Access))));
3623 when System_Tasking_Protected_Objects_Single_Entry =>
3625 -- Historically, a call to Complete_Single_Entry_Body was
3626 -- inserted, but it was a null procedure.
3628 null;
3630 when others =>
3631 raise Program_Error;
3632 end case;
3634 -- When exceptions can not be propagated, we never need to call
3635 -- Exception_Complete_Entry_Body.
3637 if No_Exception_Handlers_Set then
3638 return
3639 Make_Subprogram_Body (Loc,
3640 Specification => Bod_Spec,
3641 Declarations => Bod_Decls,
3642 Handled_Statement_Sequence =>
3643 Make_Handled_Sequence_Of_Statements (Loc,
3644 Statements => Bod_Stmts,
3645 End_Label => End_Lab));
3647 else
3648 Ohandle := Make_Others_Choice (Loc);
3649 Set_All_Others (Ohandle);
3651 case Corresponding_Runtime_Package (Pid) is
3652 when System_Tasking_Protected_Objects_Entries =>
3653 Complete :=
3654 New_Occurrence_Of
3655 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3657 when System_Tasking_Protected_Objects_Single_Entry =>
3658 Complete :=
3659 New_Occurrence_Of
3660 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3662 when others =>
3663 raise Program_Error;
3664 end case;
3666 -- Establish link between subprogram body entity and source entry
3668 Set_Corresponding_Protected_Entry (Bod_Id, Ent);
3670 -- Create body of entry procedure. The renaming declarations are
3671 -- placed ahead of the block that contains the actual entry body.
3673 return
3674 Make_Subprogram_Body (Loc,
3675 Specification => Bod_Spec,
3676 Declarations => Bod_Decls,
3677 Handled_Statement_Sequence =>
3678 Make_Handled_Sequence_Of_Statements (Loc,
3679 Statements => Bod_Stmts,
3680 End_Label => End_Lab,
3681 Exception_Handlers => New_List (
3682 Make_Implicit_Exception_Handler (EH_Loc,
3683 Exception_Choices => New_List (Ohandle),
3685 Statements => New_List (
3686 Make_Procedure_Call_Statement (EH_Loc,
3687 Name => Complete,
3688 Parameter_Associations => New_List (
3689 Make_Attribute_Reference (EH_Loc,
3690 Prefix =>
3691 Make_Selected_Component (EH_Loc,
3692 Prefix =>
3693 Make_Identifier (EH_Loc, Name_uObject),
3694 Selector_Name =>
3695 Make_Identifier (EH_Loc, Name_uObject)),
3696 Attribute_Name => Name_Unchecked_Access),
3698 Make_Function_Call (EH_Loc,
3699 Name =>
3700 New_Occurrence_Of
3701 (RTE (RE_Get_GNAT_Exception), Loc)))))))));
3702 end if;
3703 end Build_Protected_Entry;
3705 -----------------------------------------
3706 -- Build_Protected_Entry_Specification --
3707 -----------------------------------------
3709 function Build_Protected_Entry_Specification
3710 (Loc : Source_Ptr;
3711 Def_Id : Entity_Id;
3712 Ent_Id : Entity_Id) return Node_Id
3714 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3716 begin
3717 Set_Debug_Info_Needed (Def_Id);
3719 if Present (Ent_Id) then
3720 Append_Elmt (P, Accept_Address (Ent_Id));
3721 end if;
3723 return
3724 Make_Procedure_Specification (Loc,
3725 Defining_Unit_Name => Def_Id,
3726 Parameter_Specifications => New_List (
3727 Make_Parameter_Specification (Loc,
3728 Defining_Identifier =>
3729 Make_Defining_Identifier (Loc, Name_uO),
3730 Parameter_Type =>
3731 New_Occurrence_Of (RTE (RE_Address), Loc)),
3733 Make_Parameter_Specification (Loc,
3734 Defining_Identifier => P,
3735 Parameter_Type =>
3736 New_Occurrence_Of (RTE (RE_Address), Loc)),
3738 Make_Parameter_Specification (Loc,
3739 Defining_Identifier =>
3740 Make_Defining_Identifier (Loc, Name_uE),
3741 Parameter_Type =>
3742 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
3743 end Build_Protected_Entry_Specification;
3745 --------------------------
3746 -- Build_Protected_Spec --
3747 --------------------------
3749 function Build_Protected_Spec
3750 (N : Node_Id;
3751 Obj_Type : Entity_Id;
3752 Ident : Entity_Id;
3753 Unprotected : Boolean := False) return List_Id
3755 Loc : constant Source_Ptr := Sloc (N);
3756 Decl : Node_Id;
3757 Formal : Entity_Id;
3758 New_Plist : List_Id;
3759 New_Param : Node_Id;
3761 begin
3762 New_Plist := New_List;
3764 Formal := First_Formal (Ident);
3765 while Present (Formal) loop
3766 New_Param :=
3767 Make_Parameter_Specification (Loc,
3768 Defining_Identifier =>
3769 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
3770 Aliased_Present => Aliased_Present (Parent (Formal)),
3771 In_Present => In_Present (Parent (Formal)),
3772 Out_Present => Out_Present (Parent (Formal)),
3773 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc));
3775 if Unprotected then
3776 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
3777 end if;
3779 Append (New_Param, New_Plist);
3780 Next_Formal (Formal);
3781 end loop;
3783 -- If the subprogram is a procedure and the context is not an access
3784 -- to protected subprogram, the parameter is in-out. Otherwise it is
3785 -- an in parameter.
3787 Decl :=
3788 Make_Parameter_Specification (Loc,
3789 Defining_Identifier =>
3790 Make_Defining_Identifier (Loc, Name_uObject),
3791 In_Present => True,
3792 Out_Present =>
3793 (Etype (Ident) = Standard_Void_Type
3794 and then not Is_RTE (Obj_Type, RE_Address)),
3795 Parameter_Type =>
3796 New_Occurrence_Of (Obj_Type, Loc));
3797 Set_Debug_Info_Needed (Defining_Identifier (Decl));
3798 Prepend_To (New_Plist, Decl);
3800 return New_Plist;
3801 end Build_Protected_Spec;
3803 ---------------------------------------
3804 -- Build_Protected_Sub_Specification --
3805 ---------------------------------------
3807 function Build_Protected_Sub_Specification
3808 (N : Node_Id;
3809 Prot_Typ : Entity_Id;
3810 Mode : Subprogram_Protection_Mode) return Node_Id
3812 Loc : constant Source_Ptr := Sloc (N);
3813 Decl : Node_Id;
3814 Def_Id : Entity_Id;
3815 New_Id : Entity_Id;
3816 New_Plist : List_Id;
3817 New_Spec : Node_Id;
3819 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
3820 (Dispatching_Mode => ' ',
3821 Protected_Mode => 'P',
3822 Unprotected_Mode => 'N');
3824 begin
3825 if Ekind (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
3826 then
3827 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
3828 else
3829 Decl := N;
3830 end if;
3832 Def_Id := Defining_Unit_Name (Specification (Decl));
3834 New_Plist :=
3835 Build_Protected_Spec
3836 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
3837 Mode = Unprotected_Mode);
3838 New_Id :=
3839 Make_Defining_Identifier (Loc,
3840 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
3842 -- Reference the original nondispatching subprogram since the analysis
3843 -- of the object.operation notation may need its original name (see
3844 -- Sem_Ch4.Names_Match).
3846 if Mode = Dispatching_Mode then
3847 Set_Ekind (New_Id, Ekind (Def_Id));
3848 Set_Original_Protected_Subprogram (New_Id, Def_Id);
3849 end if;
3851 -- Link the protected or unprotected version to the original subprogram
3852 -- it emulates.
3854 Set_Ekind (New_Id, Ekind (Def_Id));
3855 Set_Protected_Subprogram (New_Id, Def_Id);
3857 -- The unprotected operation carries the user code, and debugging
3858 -- information must be generated for it, even though this spec does
3859 -- not come from source. It is also convenient to allow gdb to step
3860 -- into the protected operation, even though it only contains lock/
3861 -- unlock calls.
3863 Set_Debug_Info_Needed (New_Id);
3865 -- If a pragma Eliminate applies to the source entity, the internal
3866 -- subprograms will be eliminated as well.
3868 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
3870 if Nkind (Specification (Decl)) = N_Procedure_Specification then
3871 New_Spec :=
3872 Make_Procedure_Specification (Loc,
3873 Defining_Unit_Name => New_Id,
3874 Parameter_Specifications => New_Plist);
3876 -- Create a new specification for the anonymous subprogram type
3878 else
3879 New_Spec :=
3880 Make_Function_Specification (Loc,
3881 Defining_Unit_Name => New_Id,
3882 Parameter_Specifications => New_Plist,
3883 Result_Definition =>
3884 Copy_Result_Type (Result_Definition (Specification (Decl))));
3886 Set_Return_Present (Defining_Unit_Name (New_Spec));
3887 end if;
3889 return New_Spec;
3890 end Build_Protected_Sub_Specification;
3892 -------------------------------------
3893 -- Build_Protected_Subprogram_Body --
3894 -------------------------------------
3896 function Build_Protected_Subprogram_Body
3897 (N : Node_Id;
3898 Pid : Node_Id;
3899 N_Op_Spec : Node_Id) return Node_Id
3901 Exc_Safe : constant Boolean := not Might_Raise (N);
3902 -- True if N cannot raise an exception
3904 Loc : constant Source_Ptr := Sloc (N);
3905 Op_Spec : constant Node_Id := Specification (N);
3906 P_Op_Spec : constant Node_Id :=
3907 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
3909 Lock_Kind : RE_Id;
3910 Lock_Name : Node_Id;
3911 Lock_Stmt : Node_Id;
3912 Object_Parm : Node_Id;
3913 Pformal : Node_Id;
3914 R : Node_Id;
3915 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
3916 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
3917 Stmts : List_Id;
3918 Sub_Body : Node_Id;
3919 Uactuals : List_Id;
3920 Unprot_Call : Node_Id;
3922 begin
3923 -- Build a list of the formal parameters of the protected version of
3924 -- the subprogram to use as the actual parameters of the unprotected
3925 -- version.
3927 Uactuals := New_List;
3928 Pformal := First (Parameter_Specifications (P_Op_Spec));
3929 while Present (Pformal) loop
3930 Append_To (Uactuals,
3931 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
3932 Next (Pformal);
3933 end loop;
3935 -- Make a call to the unprotected version of the subprogram built above
3936 -- for use by the protected version built below.
3938 if Nkind (Op_Spec) = N_Function_Specification then
3939 if Exc_Safe then
3940 R := Make_Temporary (Loc, 'R');
3942 Unprot_Call :=
3943 Make_Object_Declaration (Loc,
3944 Defining_Identifier => R,
3945 Constant_Present => True,
3946 Object_Definition =>
3947 New_Copy (Result_Definition (N_Op_Spec)),
3948 Expression =>
3949 Make_Function_Call (Loc,
3950 Name =>
3951 Make_Identifier (Loc,
3952 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3953 Parameter_Associations => Uactuals));
3955 Return_Stmt :=
3956 Make_Simple_Return_Statement (Loc,
3957 Expression => New_Occurrence_Of (R, Loc));
3959 else
3960 Unprot_Call :=
3961 Make_Simple_Return_Statement (Loc,
3962 Expression =>
3963 Make_Function_Call (Loc,
3964 Name =>
3965 Make_Identifier (Loc,
3966 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3967 Parameter_Associations => Uactuals));
3968 end if;
3970 Lock_Kind := RE_Lock_Read_Only;
3972 else
3973 Unprot_Call :=
3974 Make_Procedure_Call_Statement (Loc,
3975 Name =>
3976 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
3977 Parameter_Associations => Uactuals);
3979 Lock_Kind := RE_Lock;
3980 end if;
3982 -- Wrap call in block that will be covered by an at_end handler
3984 if not Exc_Safe then
3985 Unprot_Call :=
3986 Make_Block_Statement (Loc,
3987 Handled_Statement_Sequence =>
3988 Make_Handled_Sequence_Of_Statements (Loc,
3989 Statements => New_List (Unprot_Call)));
3990 end if;
3992 -- Make the protected subprogram body. This locks the protected
3993 -- object and calls the unprotected version of the subprogram.
3995 case Corresponding_Runtime_Package (Pid) is
3996 when System_Tasking_Protected_Objects_Entries =>
3997 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
3999 when System_Tasking_Protected_Objects_Single_Entry =>
4000 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
4002 when System_Tasking_Protected_Objects =>
4003 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
4005 when others =>
4006 raise Program_Error;
4007 end case;
4009 Object_Parm :=
4010 Make_Attribute_Reference (Loc,
4011 Prefix =>
4012 Make_Selected_Component (Loc,
4013 Prefix => Make_Identifier (Loc, Name_uObject),
4014 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4015 Attribute_Name => Name_Unchecked_Access);
4017 Lock_Stmt :=
4018 Make_Procedure_Call_Statement (Loc,
4019 Name => Lock_Name,
4020 Parameter_Associations => New_List (Object_Parm));
4022 if Abort_Allowed then
4023 Stmts := New_List (
4024 Build_Runtime_Call (Loc, RE_Abort_Defer),
4025 Lock_Stmt);
4027 else
4028 Stmts := New_List (Lock_Stmt);
4029 end if;
4031 if not Exc_Safe then
4032 Append (Unprot_Call, Stmts);
4033 else
4034 if Nkind (Op_Spec) = N_Function_Specification then
4035 Pre_Stmts := Stmts;
4036 Stmts := Empty_List;
4037 else
4038 Append (Unprot_Call, Stmts);
4039 end if;
4041 -- Historical note: Previously, call to the cleanup was inserted
4042 -- here. This is now done by Build_Protected_Subprogram_Call_Cleanup,
4043 -- which is also shared by the 'not Exc_Safe' path.
4045 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
4047 if Nkind (Op_Spec) = N_Function_Specification then
4048 Append_To (Stmts, Return_Stmt);
4049 Append_To (Pre_Stmts,
4050 Make_Block_Statement (Loc,
4051 Declarations => New_List (Unprot_Call),
4052 Handled_Statement_Sequence =>
4053 Make_Handled_Sequence_Of_Statements (Loc,
4054 Statements => Stmts)));
4055 Stmts := Pre_Stmts;
4056 end if;
4057 end if;
4059 Sub_Body :=
4060 Make_Subprogram_Body (Loc,
4061 Declarations => Empty_List,
4062 Specification => P_Op_Spec,
4063 Handled_Statement_Sequence =>
4064 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4066 -- Mark this subprogram as a protected subprogram body so that the
4067 -- cleanup will be inserted. This is done only in the 'not Exc_Safe'
4068 -- path as otherwise the cleanup has already been inserted.
4070 if not Exc_Safe then
4071 Set_Is_Protected_Subprogram_Body (Sub_Body);
4072 end if;
4074 return Sub_Body;
4075 end Build_Protected_Subprogram_Body;
4077 -------------------------------------
4078 -- Build_Protected_Subprogram_Call --
4079 -------------------------------------
4081 procedure Build_Protected_Subprogram_Call
4082 (N : Node_Id;
4083 Name : Node_Id;
4084 Rec : Node_Id;
4085 External : Boolean := True)
4087 Loc : constant Source_Ptr := Sloc (N);
4088 Sub : constant Entity_Id := Entity (Name);
4089 New_Sub : Node_Id;
4090 Params : List_Id;
4092 begin
4093 if External then
4094 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4095 else
4096 New_Sub :=
4097 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4098 end if;
4100 if Present (Parameter_Associations (N)) then
4101 Params := New_Copy_List_Tree (Parameter_Associations (N));
4102 else
4103 Params := New_List;
4104 end if;
4106 -- If the type is an untagged derived type, convert to the root type,
4107 -- which is the one on which the operations are defined.
4109 if Nkind (Rec) = N_Unchecked_Type_Conversion
4110 and then not Is_Tagged_Type (Etype (Rec))
4111 and then Is_Derived_Type (Etype (Rec))
4112 then
4113 Set_Etype (Rec, Root_Type (Etype (Rec)));
4114 Set_Subtype_Mark (Rec,
4115 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4116 end if;
4118 Prepend (Rec, Params);
4120 if Ekind (Sub) = E_Procedure then
4121 Rewrite (N,
4122 Make_Procedure_Call_Statement (Loc,
4123 Name => New_Sub,
4124 Parameter_Associations => Params));
4126 else
4127 pragma Assert (Ekind (Sub) = E_Function);
4128 Rewrite (N,
4129 Make_Function_Call (Loc,
4130 Name => New_Sub,
4131 Parameter_Associations => Params));
4133 -- Preserve type of call for subsequent processing (required for
4134 -- call to Wrap_Transient_Expression in the case of a shared passive
4135 -- protected).
4137 Set_Etype (N, Etype (New_Sub));
4138 end if;
4140 if External
4141 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4142 and then Is_Entity_Name (Expression (Rec))
4143 and then Is_Shared_Passive (Entity (Expression (Rec)))
4144 then
4145 Add_Shared_Var_Lock_Procs (N);
4146 end if;
4147 end Build_Protected_Subprogram_Call;
4149 ---------------------------------------------
4150 -- Build_Protected_Subprogram_Call_Cleanup --
4151 ---------------------------------------------
4153 procedure Build_Protected_Subprogram_Call_Cleanup
4154 (Op_Spec : Node_Id;
4155 Conc_Typ : Node_Id;
4156 Loc : Source_Ptr;
4157 Stmts : List_Id)
4159 Nam : Node_Id;
4161 begin
4162 -- If the associated protected object has entries, a protected
4163 -- procedure has to service entry queues. In this case generate:
4165 -- Service_Entries (_object._object'Access);
4167 if Nkind (Op_Spec) = N_Procedure_Specification
4168 and then Has_Entries (Conc_Typ)
4169 then
4170 case Corresponding_Runtime_Package (Conc_Typ) is
4171 when System_Tasking_Protected_Objects_Entries =>
4172 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4174 when System_Tasking_Protected_Objects_Single_Entry =>
4175 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4177 when others =>
4178 raise Program_Error;
4179 end case;
4181 Append_To (Stmts,
4182 Make_Procedure_Call_Statement (Loc,
4183 Name => Nam,
4184 Parameter_Associations => New_List (
4185 Make_Attribute_Reference (Loc,
4186 Prefix =>
4187 Make_Selected_Component (Loc,
4188 Prefix => Make_Identifier (Loc, Name_uObject),
4189 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4190 Attribute_Name => Name_Unchecked_Access))));
4192 else
4193 -- Generate:
4194 -- Unlock (_object._object'Access);
4196 case Corresponding_Runtime_Package (Conc_Typ) is
4197 when System_Tasking_Protected_Objects_Entries =>
4198 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4200 when System_Tasking_Protected_Objects_Single_Entry =>
4201 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4203 when System_Tasking_Protected_Objects =>
4204 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4206 when others =>
4207 raise Program_Error;
4208 end case;
4210 Append_To (Stmts,
4211 Make_Procedure_Call_Statement (Loc,
4212 Name => Nam,
4213 Parameter_Associations => New_List (
4214 Make_Attribute_Reference (Loc,
4215 Prefix =>
4216 Make_Selected_Component (Loc,
4217 Prefix => Make_Identifier (Loc, Name_uObject),
4218 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4219 Attribute_Name => Name_Unchecked_Access))));
4220 end if;
4222 -- Generate:
4223 -- Abort_Undefer;
4225 if Abort_Allowed then
4226 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
4227 end if;
4228 end Build_Protected_Subprogram_Call_Cleanup;
4230 -------------------------
4231 -- Build_Selected_Name --
4232 -------------------------
4234 function Build_Selected_Name
4235 (Prefix : Entity_Id;
4236 Selector : Entity_Id;
4237 Append_Char : Character := ' ') return Name_Id
4239 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4240 Select_Len : Natural;
4242 begin
4243 Get_Name_String (Chars (Selector));
4244 Select_Len := Name_Len;
4245 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4246 Get_Name_String (Chars (Prefix));
4248 -- If scope is anonymous type, discard suffix to recover name of
4249 -- single protected object. Otherwise use protected type name.
4251 if Name_Buffer (Name_Len) = 'T' then
4252 Name_Len := Name_Len - 1;
4253 end if;
4255 Add_Str_To_Name_Buffer ("__");
4256 for J in 1 .. Select_Len loop
4257 Add_Char_To_Name_Buffer (Select_Buffer (J));
4258 end loop;
4260 -- Now add the Append_Char if specified. The encoding to follow
4261 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4262 -- then the entity is associated to a protected type subprogram.
4263 -- Otherwise, it is a protected type entry. For each case, the
4264 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4266 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4268 if Append_Char /= ' ' then
4269 if Append_Char = 'P' or Append_Char = 'N' then
4270 Add_Char_To_Name_Buffer (Append_Char);
4271 return Name_Find;
4272 else
4273 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4274 return New_External_Name (Name_Find, ' ', -1);
4275 end if;
4276 else
4277 return Name_Find;
4278 end if;
4279 end Build_Selected_Name;
4281 -----------------------------
4282 -- Build_Simple_Entry_Call --
4283 -----------------------------
4285 -- A task entry call is converted to a call to Call_Simple
4287 -- declare
4288 -- P : parms := (parm, parm, parm);
4289 -- begin
4290 -- Call_Simple (acceptor-task, entry-index, P'Address);
4291 -- parm := P.param;
4292 -- parm := P.param;
4293 -- ...
4294 -- end;
4296 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4297 -- the parameters, and the constructed aggregate value contains either the
4298 -- parameters or, in the case of non-elementary types, references to these
4299 -- parameters. Then the address of this aggregate is passed to the runtime
4300 -- routine, along with the task id value and the task entry index value.
4301 -- Pnn is only required if parameters are present.
4303 -- The assignments after the call are present only in the case of in-out
4304 -- or out parameters for elementary types, and are used to assign back the
4305 -- resulting values of such parameters.
4307 -- Note: the reason that we insert a block here is that in the context
4308 -- of selects, conditional entry calls etc. the entry call statement
4309 -- appears on its own, not as an element of a list.
4311 -- A protected entry call is converted to a Protected_Entry_Call:
4313 -- declare
4314 -- P : E1_Params := (param, param, param);
4315 -- Pnn : Boolean;
4316 -- Bnn : Communications_Block;
4318 -- declare
4319 -- P : E1_Params := (param, param, param);
4320 -- Bnn : Communications_Block;
4322 -- begin
4323 -- Protected_Entry_Call (
4324 -- Object => po._object'Access,
4325 -- E => <entry index>;
4326 -- Uninterpreted_Data => P'Address;
4327 -- Mode => Simple_Call;
4328 -- Block => Bnn);
4329 -- parm := P.param;
4330 -- parm := P.param;
4331 -- ...
4332 -- end;
4334 procedure Build_Simple_Entry_Call
4335 (N : Node_Id;
4336 Concval : Node_Id;
4337 Ename : Node_Id;
4338 Index : Node_Id)
4340 begin
4341 Expand_Call (N);
4343 -- If call has been inlined, nothing left to do
4345 if Nkind (N) = N_Block_Statement then
4346 return;
4347 end if;
4349 -- Convert entry call to Call_Simple call
4351 declare
4352 Loc : constant Source_Ptr := Sloc (N);
4353 Parms : constant List_Id := Parameter_Associations (N);
4354 Stats : constant List_Id := New_List;
4355 Actual : Node_Id;
4356 Call : Node_Id;
4357 Comm_Name : Entity_Id;
4358 Conctyp : Node_Id;
4359 Decls : List_Id;
4360 Ent : Entity_Id;
4361 Ent_Acc : Entity_Id;
4362 Formal : Node_Id;
4363 Iface_Tag : Entity_Id;
4364 Iface_Typ : Entity_Id;
4365 N_Node : Node_Id;
4366 N_Var : Node_Id;
4367 P : Entity_Id;
4368 Parm1 : Node_Id;
4369 Parm2 : Node_Id;
4370 Parm3 : Node_Id;
4371 Pdecl : Node_Id;
4372 Plist : List_Id;
4373 X : Entity_Id;
4374 Xdecl : Node_Id;
4376 begin
4377 -- Simple entry and entry family cases merge here
4379 Ent := Entity (Ename);
4380 Ent_Acc := Entry_Parameters_Type (Ent);
4381 Conctyp := Etype (Concval);
4383 -- If prefix is an access type, dereference to obtain the task type
4385 if Is_Access_Type (Conctyp) then
4386 Conctyp := Designated_Type (Conctyp);
4387 end if;
4389 -- Special case for protected subprogram calls
4391 if Is_Protected_Type (Conctyp)
4392 and then Is_Subprogram (Entity (Ename))
4393 then
4394 if not Is_Eliminated (Entity (Ename)) then
4395 Build_Protected_Subprogram_Call
4396 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4397 Analyze (N);
4398 end if;
4400 return;
4401 end if;
4403 -- First parameter is the Task_Id value from the task value or the
4404 -- Object from the protected object value, obtained by selecting
4405 -- the _Task_Id or _Object from the result of doing an unchecked
4406 -- conversion to convert the value to the corresponding record type.
4408 if Nkind (Concval) = N_Function_Call
4409 and then Is_Task_Type (Conctyp)
4410 and then Ada_Version >= Ada_2005
4411 then
4412 declare
4413 ExpR : constant Node_Id := Relocate_Node (Concval);
4414 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4415 Decl : Node_Id;
4417 begin
4418 Decl :=
4419 Make_Object_Declaration (Loc,
4420 Defining_Identifier => Obj,
4421 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4422 Expression => ExpR);
4423 Set_Etype (Obj, Conctyp);
4424 Decls := New_List (Decl);
4425 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4426 end;
4428 else
4429 Decls := New_List;
4430 end if;
4432 Parm1 := Concurrent_Ref (Concval);
4434 -- Second parameter is the entry index, computed by the routine
4435 -- provided for this purpose. The value of this expression is
4436 -- assigned to an intermediate variable to assure that any entry
4437 -- family index expressions are evaluated before the entry
4438 -- parameters.
4440 if not Is_Protected_Type (Conctyp)
4441 or else
4442 Corresponding_Runtime_Package (Conctyp) =
4443 System_Tasking_Protected_Objects_Entries
4444 then
4445 X := Make_Defining_Identifier (Loc, Name_uX);
4447 Xdecl :=
4448 Make_Object_Declaration (Loc,
4449 Defining_Identifier => X,
4450 Object_Definition =>
4451 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4452 Expression => Actual_Index_Expression (
4453 Loc, Entity (Ename), Index, Concval));
4455 Append_To (Decls, Xdecl);
4456 Parm2 := New_Occurrence_Of (X, Loc);
4458 else
4459 Xdecl := Empty;
4460 Parm2 := Empty;
4461 end if;
4463 -- The third parameter is the packaged parameters. If there are
4464 -- none, then it is just the null address, since nothing is passed.
4466 if No (Parms) then
4467 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4468 P := Empty;
4470 -- Case of parameters present, where third argument is the address
4471 -- of a packaged record containing the required parameter values.
4473 else
4474 -- First build a list of parameter values, which are references to
4475 -- objects of the parameter types.
4477 Plist := New_List;
4479 Actual := First_Actual (N);
4480 Formal := First_Formal (Ent);
4481 while Present (Actual) loop
4483 -- If it is a by-copy type, copy it to a new variable. The
4484 -- packaged record has a field that points to this variable.
4486 if Is_By_Copy_Type (Etype (Actual)) then
4487 N_Node :=
4488 Make_Object_Declaration (Loc,
4489 Defining_Identifier => Make_Temporary (Loc, 'J'),
4490 Aliased_Present => True,
4491 Object_Definition =>
4492 New_Occurrence_Of (Etype (Formal), Loc));
4494 -- Mark the object as not needing initialization since the
4495 -- initialization is performed separately, avoiding errors
4496 -- on cases such as formals of null-excluding access types.
4498 Set_No_Initialization (N_Node);
4500 -- We must make a separate assignment statement for the
4501 -- case of limited types. We cannot assign it unless the
4502 -- Assignment_OK flag is set first. An out formal of an
4503 -- access type or whose type has a Default_Value must also
4504 -- be initialized from the actual (see RM 6.4.1 (13-13.1)),
4505 -- but no constraint, predicate, or null-exclusion check is
4506 -- applied before the call.
4508 if Ekind (Formal) /= E_Out_Parameter
4509 or else Is_Access_Type (Etype (Formal))
4510 or else
4511 (Is_Scalar_Type (Etype (Formal))
4512 and then
4513 Present (Default_Aspect_Value (Etype (Formal))))
4514 then
4515 N_Var :=
4516 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4517 Set_Assignment_OK (N_Var);
4518 Append_To (Stats,
4519 Make_Assignment_Statement (Loc,
4520 Name => N_Var,
4521 Expression => Relocate_Node (Actual)));
4523 -- Mark the object as internal, so we don't later reset
4524 -- No_Initialization flag in Default_Initialize_Object,
4525 -- which would lead to needless default initialization.
4526 -- We don't set this outside the if statement, because
4527 -- out scalar parameters without Default_Value do require
4528 -- default initialization if Initialize_Scalars applies.
4530 Set_Is_Internal (Defining_Identifier (N_Node));
4532 -- If actual is an out parameter of a null-excluding
4533 -- access type, there is access check on entry, so set
4534 -- Suppress_Assignment_Checks on the generated statement
4535 -- that assigns the actual to the parameter block.
4537 Set_Suppress_Assignment_Checks (Last (Stats));
4538 end if;
4540 Append (N_Node, Decls);
4542 Append_To (Plist,
4543 Make_Attribute_Reference (Loc,
4544 Attribute_Name => Name_Unchecked_Access,
4545 Prefix =>
4546 New_Occurrence_Of
4547 (Defining_Identifier (N_Node), Loc)));
4549 else
4550 -- Interface class-wide formal
4552 if Ada_Version >= Ada_2005
4553 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4554 and then Is_Interface (Etype (Formal))
4555 then
4556 Iface_Typ := Etype (Etype (Formal));
4558 -- Generate:
4559 -- formal_iface_type! (actual.iface_tag)'reference
4561 Iface_Tag :=
4562 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4563 pragma Assert (Present (Iface_Tag));
4565 Append_To (Plist,
4566 Make_Reference (Loc,
4567 Unchecked_Convert_To (Iface_Typ,
4568 Make_Selected_Component (Loc,
4569 Prefix =>
4570 Relocate_Node (Actual),
4571 Selector_Name =>
4572 New_Occurrence_Of (Iface_Tag, Loc)))));
4573 else
4574 -- Generate:
4575 -- actual'reference
4577 Append_To (Plist,
4578 Make_Reference (Loc, Relocate_Node (Actual)));
4579 end if;
4580 end if;
4582 Next_Actual (Actual);
4583 Next_Formal_With_Extras (Formal);
4584 end loop;
4586 -- Now build the declaration of parameters initialized with the
4587 -- aggregate containing this constructed parameter list.
4589 P := Make_Defining_Identifier (Loc, Name_uP);
4591 Pdecl :=
4592 Make_Object_Declaration (Loc,
4593 Defining_Identifier => P,
4594 Object_Definition =>
4595 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4596 Expression =>
4597 Make_Aggregate (Loc, Expressions => Plist));
4599 Parm3 :=
4600 Make_Attribute_Reference (Loc,
4601 Prefix => New_Occurrence_Of (P, Loc),
4602 Attribute_Name => Name_Address);
4604 Append (Pdecl, Decls);
4605 end if;
4607 -- Now we can create the call, case of protected type
4609 if Is_Protected_Type (Conctyp) then
4610 case Corresponding_Runtime_Package (Conctyp) is
4611 when System_Tasking_Protected_Objects_Entries =>
4613 -- Change the type of the index declaration
4615 Set_Object_Definition (Xdecl,
4616 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4618 -- Some additional declarations for protected entry calls
4620 if No (Decls) then
4621 Decls := New_List;
4622 end if;
4624 -- Bnn : Communications_Block;
4626 Comm_Name := Make_Temporary (Loc, 'B');
4628 Append_To (Decls,
4629 Make_Object_Declaration (Loc,
4630 Defining_Identifier => Comm_Name,
4631 Object_Definition =>
4632 New_Occurrence_Of
4633 (RTE (RE_Communication_Block), Loc)));
4635 -- Some additional statements for protected entry calls
4637 -- Protected_Entry_Call
4638 -- (Object => po._object'Access,
4639 -- E => <entry index>;
4640 -- Uninterpreted_Data => P'Address;
4641 -- Mode => Simple_Call;
4642 -- Block => Bnn);
4644 Call :=
4645 Make_Procedure_Call_Statement (Loc,
4646 Name =>
4647 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
4649 Parameter_Associations => New_List (
4650 Make_Attribute_Reference (Loc,
4651 Attribute_Name => Name_Unchecked_Access,
4652 Prefix => Parm1),
4653 Parm2,
4654 Parm3,
4655 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
4656 New_Occurrence_Of (Comm_Name, Loc)));
4658 when System_Tasking_Protected_Objects_Single_Entry =>
4660 -- Protected_Single_Entry_Call
4661 -- (Object => po._object'Access,
4662 -- Uninterpreted_Data => P'Address);
4664 Call :=
4665 Make_Procedure_Call_Statement (Loc,
4666 Name =>
4667 New_Occurrence_Of
4668 (RTE (RE_Protected_Single_Entry_Call), Loc),
4670 Parameter_Associations => New_List (
4671 Make_Attribute_Reference (Loc,
4672 Attribute_Name => Name_Unchecked_Access,
4673 Prefix => Parm1),
4674 Parm3));
4676 when others =>
4677 raise Program_Error;
4678 end case;
4680 -- Case of task type
4682 else
4683 Call :=
4684 Make_Procedure_Call_Statement (Loc,
4685 Name =>
4686 New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
4687 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4689 end if;
4691 Append_To (Stats, Call);
4693 -- If there are out or in/out parameters by copy add assignment
4694 -- statements for the result values.
4696 if Present (Parms) then
4697 Actual := First_Actual (N);
4698 Formal := First_Formal (Ent);
4700 Set_Assignment_OK (Actual);
4701 while Present (Actual) loop
4702 if Is_By_Copy_Type (Etype (Actual))
4703 and then Ekind (Formal) /= E_In_Parameter
4704 then
4705 N_Node :=
4706 Make_Assignment_Statement (Loc,
4707 Name => New_Copy (Actual),
4708 Expression =>
4709 Make_Explicit_Dereference (Loc,
4710 Make_Selected_Component (Loc,
4711 Prefix => New_Occurrence_Of (P, Loc),
4712 Selector_Name =>
4713 Make_Identifier (Loc, Chars (Formal)))));
4715 -- In all cases (including limited private types) we want
4716 -- the assignment to be valid.
4718 Set_Assignment_OK (Name (N_Node));
4720 -- If the call is the triggering alternative in an
4721 -- asynchronous select, or the entry_call alternative of a
4722 -- conditional entry call, the assignments for in-out
4723 -- parameters are incorporated into the statement list that
4724 -- follows, so that there are executed only if the entry
4725 -- call succeeds.
4727 if (Nkind (Parent (N)) = N_Triggering_Alternative
4728 and then N = Triggering_Statement (Parent (N)))
4729 or else
4730 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4731 and then N = Entry_Call_Statement (Parent (N)))
4732 then
4733 if No (Statements (Parent (N))) then
4734 Set_Statements (Parent (N), New_List);
4735 end if;
4737 Prepend (N_Node, Statements (Parent (N)));
4739 else
4740 Insert_After (Call, N_Node);
4741 end if;
4742 end if;
4744 Next_Actual (Actual);
4745 Next_Formal_With_Extras (Formal);
4746 end loop;
4747 end if;
4749 -- Finally, create block and analyze it
4751 Rewrite (N,
4752 Make_Block_Statement (Loc,
4753 Declarations => Decls,
4754 Handled_Statement_Sequence =>
4755 Make_Handled_Sequence_Of_Statements (Loc,
4756 Statements => Stats)));
4758 Analyze (N);
4759 end;
4760 end Build_Simple_Entry_Call;
4762 --------------------------------
4763 -- Build_Task_Activation_Call --
4764 --------------------------------
4766 procedure Build_Task_Activation_Call (N : Node_Id) is
4767 function Activation_Call_Loc return Source_Ptr;
4768 -- Find a suitable source location for the activation call
4770 -------------------------
4771 -- Activation_Call_Loc --
4772 -------------------------
4774 function Activation_Call_Loc return Source_Ptr is
4775 begin
4776 -- The activation call must carry the location of the "end" keyword
4777 -- when the context is a package declaration.
4779 if Nkind (N) = N_Package_Declaration then
4780 return End_Keyword_Location (N);
4782 -- Otherwise the activation call must carry the location of the
4783 -- "begin" keyword.
4785 else
4786 return Begin_Keyword_Location (N);
4787 end if;
4788 end Activation_Call_Loc;
4790 -- Local variables
4792 Chain : Entity_Id;
4793 Call : Node_Id;
4794 Loc : Source_Ptr;
4795 Name : Node_Id;
4796 Owner : Node_Id;
4797 Stmt : Node_Id;
4799 -- Start of processing for Build_Task_Activation_Call
4801 begin
4802 -- For sequential elaboration policy, all the tasks will be activated at
4803 -- the end of the elaboration.
4805 if Partition_Elaboration_Policy = 'S' then
4806 return;
4808 -- Do not create an activation call for a package spec if the package
4809 -- has a completing body. The activation call will be inserted after
4810 -- the "begin" of the body.
4812 elsif Nkind (N) = N_Package_Declaration
4813 and then Present (Corresponding_Body (N))
4814 then
4815 return;
4816 end if;
4818 -- Obtain the activation chain entity. Block statements, entry bodies,
4819 -- subprogram bodies, and task bodies keep the entity in their nodes.
4820 -- Package bodies on the other hand store it in the declaration of the
4821 -- corresponding package spec.
4823 Owner := N;
4825 if Nkind (Owner) = N_Package_Body then
4826 Owner := Unit_Declaration_Node (Corresponding_Spec (Owner));
4827 end if;
4829 Chain := Activation_Chain_Entity (Owner);
4831 -- Nothing to do when there are no tasks to activate. This is indicated
4832 -- by a missing activation chain entity.
4834 if No (Chain) then
4835 return;
4836 end if;
4838 -- The location of the activation call must be as close as possible to
4839 -- the intended semantic location of the activation because the ABE
4840 -- mechanism relies heavily on accurate locations.
4842 Loc := Activation_Call_Loc;
4844 if Restricted_Profile then
4845 Name := New_Occurrence_Of (RTE (RE_Activate_Restricted_Tasks), Loc);
4846 else
4847 Name := New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc);
4848 end if;
4850 Call :=
4851 Make_Procedure_Call_Statement (Loc,
4852 Name => Name,
4853 Parameter_Associations =>
4854 New_List (Make_Attribute_Reference (Loc,
4855 Prefix => New_Occurrence_Of (Chain, Loc),
4856 Attribute_Name => Name_Unchecked_Access)));
4858 if Nkind (N) = N_Package_Declaration then
4859 if Present (Private_Declarations (Specification (N))) then
4860 Append (Call, Private_Declarations (Specification (N)));
4861 else
4862 Append (Call, Visible_Declarations (Specification (N)));
4863 end if;
4865 else
4866 -- The call goes at the start of the statement sequence after the
4867 -- start of exception range label if one is present.
4869 if Present (Handled_Statement_Sequence (N)) then
4870 Stmt := First (Statements (Handled_Statement_Sequence (N)));
4872 -- A special case, skip exception range label if one is present
4873 -- (from front end zcx processing).
4875 if Nkind (Stmt) = N_Label and then Exception_Junk (Stmt) then
4876 Next (Stmt);
4877 end if;
4879 -- Another special case, if the first statement is a block from
4880 -- optimization of a local raise to a goto, then the call goes
4881 -- inside this block.
4883 if Nkind (Stmt) = N_Block_Statement
4884 and then Exception_Junk (Stmt)
4885 then
4886 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
4887 end if;
4889 -- Insertion point is after any exception label pushes, since we
4890 -- want it covered by any local handlers.
4892 while Nkind (Stmt) in N_Push_xxx_Label loop
4893 Next (Stmt);
4894 end loop;
4896 -- Now we have the proper insertion point
4898 Insert_Before (Stmt, Call);
4900 else
4901 Set_Handled_Statement_Sequence (N,
4902 Make_Handled_Sequence_Of_Statements (Loc,
4903 Statements => New_List (Call)));
4904 end if;
4905 end if;
4907 Analyze (Call);
4909 if Legacy_Elaboration_Checks then
4910 Check_Task_Activation (N);
4911 end if;
4912 end Build_Task_Activation_Call;
4914 -------------------------------
4915 -- Build_Task_Allocate_Block --
4916 -------------------------------
4918 procedure Build_Task_Allocate_Block
4919 (Actions : List_Id;
4920 N : Node_Id;
4921 Args : List_Id)
4923 T : constant Entity_Id := Entity (Expression (N));
4924 Init : constant Entity_Id := Base_Init_Proc (T);
4925 Loc : constant Source_Ptr := Sloc (N);
4926 Chain : constant Entity_Id :=
4927 Make_Defining_Identifier (Loc, Name_uChain);
4928 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4929 Block : Node_Id;
4931 begin
4932 Block :=
4933 Make_Block_Statement (Loc,
4934 Identifier => New_Occurrence_Of (Blkent, Loc),
4935 Declarations => New_List (
4937 -- _Chain : Activation_Chain;
4939 Make_Object_Declaration (Loc,
4940 Defining_Identifier => Chain,
4941 Aliased_Present => True,
4942 Object_Definition =>
4943 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
4945 Handled_Statement_Sequence =>
4946 Make_Handled_Sequence_Of_Statements (Loc,
4948 Statements => New_List (
4950 -- Init (Args);
4952 Make_Procedure_Call_Statement (Loc,
4953 Name => New_Occurrence_Of (Init, Loc),
4954 Parameter_Associations => Args),
4956 -- Activate_Tasks (_Chain);
4958 Make_Procedure_Call_Statement (Loc,
4959 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4960 Parameter_Associations => New_List (
4961 Make_Attribute_Reference (Loc,
4962 Prefix => New_Occurrence_Of (Chain, Loc),
4963 Attribute_Name => Name_Unchecked_Access))))),
4965 Has_Created_Identifier => True,
4966 Is_Task_Allocation_Block => True);
4968 Append_To (Actions,
4969 Make_Implicit_Label_Declaration (Loc,
4970 Defining_Identifier => Blkent,
4971 Label_Construct => Block));
4973 Append_To (Actions, Block);
4975 Set_Activation_Chain_Entity (Block, Chain);
4976 end Build_Task_Allocate_Block;
4978 -----------------------------------------------
4979 -- Build_Task_Allocate_Block_With_Init_Stmts --
4980 -----------------------------------------------
4982 procedure Build_Task_Allocate_Block_With_Init_Stmts
4983 (Actions : List_Id;
4984 N : Node_Id;
4985 Init_Stmts : List_Id)
4987 Loc : constant Source_Ptr := Sloc (N);
4988 Chain : constant Entity_Id :=
4989 Make_Defining_Identifier (Loc, Name_uChain);
4990 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4991 Block : Node_Id;
4993 begin
4994 Append_To (Init_Stmts,
4995 Make_Procedure_Call_Statement (Loc,
4996 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4997 Parameter_Associations => New_List (
4998 Make_Attribute_Reference (Loc,
4999 Prefix => New_Occurrence_Of (Chain, Loc),
5000 Attribute_Name => Name_Unchecked_Access))));
5002 Block :=
5003 Make_Block_Statement (Loc,
5004 Identifier => New_Occurrence_Of (Blkent, Loc),
5005 Declarations => New_List (
5007 -- _Chain : Activation_Chain;
5009 Make_Object_Declaration (Loc,
5010 Defining_Identifier => Chain,
5011 Aliased_Present => True,
5012 Object_Definition =>
5013 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
5015 Handled_Statement_Sequence =>
5016 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
5018 Has_Created_Identifier => True,
5019 Is_Task_Allocation_Block => True);
5021 Append_To (Actions,
5022 Make_Implicit_Label_Declaration (Loc,
5023 Defining_Identifier => Blkent,
5024 Label_Construct => Block));
5026 Append_To (Actions, Block);
5028 Set_Activation_Chain_Entity (Block, Chain);
5029 end Build_Task_Allocate_Block_With_Init_Stmts;
5031 -----------------------------------
5032 -- Build_Task_Proc_Specification --
5033 -----------------------------------
5035 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
5036 Loc : constant Source_Ptr := Sloc (T);
5037 Spec_Id : Entity_Id;
5039 begin
5040 -- Case of explicit task type, suffix TB
5042 if Comes_From_Source (T) then
5043 Spec_Id :=
5044 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
5046 -- Case of anonymous task type, suffix B
5048 else
5049 Spec_Id :=
5050 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
5051 end if;
5053 Set_Is_Internal (Spec_Id);
5055 -- Associate the procedure with the task, if this is the declaration
5056 -- (and not the body) of the procedure.
5058 if No (Task_Body_Procedure (T)) then
5059 Set_Task_Body_Procedure (T, Spec_Id);
5060 end if;
5062 return
5063 Make_Procedure_Specification (Loc,
5064 Defining_Unit_Name => Spec_Id,
5065 Parameter_Specifications => New_List (
5066 Make_Parameter_Specification (Loc,
5067 Defining_Identifier =>
5068 Make_Defining_Identifier (Loc, Name_uTask),
5069 Parameter_Type =>
5070 Make_Access_Definition (Loc,
5071 Subtype_Mark =>
5072 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
5073 end Build_Task_Proc_Specification;
5075 ---------------------------------------
5076 -- Build_Unprotected_Subprogram_Body --
5077 ---------------------------------------
5079 function Build_Unprotected_Subprogram_Body
5080 (N : Node_Id;
5081 Pid : Node_Id) return Node_Id
5083 Decls : constant List_Id := Declarations (N);
5085 begin
5086 -- Add renamings for the Protection object, discriminals, privals, and
5087 -- the entry index constant for use by debugger.
5089 Debug_Private_Data_Declarations (Decls);
5091 -- Make an unprotected version of the subprogram for use within the same
5092 -- object, with a new name and an additional parameter representing the
5093 -- object.
5095 return
5096 Make_Subprogram_Body (Sloc (N),
5097 Specification =>
5098 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5099 Declarations => Decls,
5100 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5101 end Build_Unprotected_Subprogram_Body;
5103 ----------------------------
5104 -- Collect_Entry_Families --
5105 ----------------------------
5107 procedure Collect_Entry_Families
5108 (Loc : Source_Ptr;
5109 Cdecls : List_Id;
5110 Current_Node : in out Node_Id;
5111 Conctyp : Entity_Id)
5113 Efam : Entity_Id;
5114 Efam_Decl : Node_Id;
5115 Efam_Type : Entity_Id;
5117 begin
5118 Efam := First_Entity (Conctyp);
5119 while Present (Efam) loop
5120 if Ekind (Efam) = E_Entry_Family then
5121 Efam_Type := Make_Temporary (Loc, 'F');
5123 declare
5124 Bas : Entity_Id :=
5125 Base_Type
5126 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5128 Bas_Decl : Node_Id := Empty;
5129 Lo, Hi : Node_Id;
5131 begin
5132 Get_Index_Bounds
5133 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5135 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5136 Bas := Make_Temporary (Loc, 'B');
5138 Bas_Decl :=
5139 Make_Subtype_Declaration (Loc,
5140 Defining_Identifier => Bas,
5141 Subtype_Indication =>
5142 Make_Subtype_Indication (Loc,
5143 Subtype_Mark =>
5144 New_Occurrence_Of (Standard_Integer, Loc),
5145 Constraint =>
5146 Make_Range_Constraint (Loc,
5147 Range_Expression => Make_Range (Loc,
5148 Make_Integer_Literal
5149 (Loc, -Entry_Family_Bound),
5150 Make_Integer_Literal
5151 (Loc, Entry_Family_Bound - 1)))));
5153 Insert_After (Current_Node, Bas_Decl);
5154 Current_Node := Bas_Decl;
5155 Analyze (Bas_Decl);
5156 end if;
5158 Efam_Decl :=
5159 Make_Full_Type_Declaration (Loc,
5160 Defining_Identifier => Efam_Type,
5161 Type_Definition =>
5162 Make_Unconstrained_Array_Definition (Loc,
5163 Subtype_Marks =>
5164 (New_List (New_Occurrence_Of (Bas, Loc))),
5166 Component_Definition =>
5167 Make_Component_Definition (Loc,
5168 Aliased_Present => False,
5169 Subtype_Indication =>
5170 New_Occurrence_Of (Standard_Character, Loc))));
5171 end;
5173 Insert_After (Current_Node, Efam_Decl);
5174 Current_Node := Efam_Decl;
5175 Analyze (Efam_Decl);
5177 Append_To (Cdecls,
5178 Make_Component_Declaration (Loc,
5179 Defining_Identifier =>
5180 Make_Defining_Identifier (Loc, Chars (Efam)),
5182 Component_Definition =>
5183 Make_Component_Definition (Loc,
5184 Aliased_Present => False,
5185 Subtype_Indication =>
5186 Make_Subtype_Indication (Loc,
5187 Subtype_Mark =>
5188 New_Occurrence_Of (Efam_Type, Loc),
5190 Constraint =>
5191 Make_Index_Or_Discriminant_Constraint (Loc,
5192 Constraints => New_List (
5193 New_Occurrence_Of
5194 (Etype (Discrete_Subtype_Definition
5195 (Parent (Efam))), Loc)))))));
5197 end if;
5199 Next_Entity (Efam);
5200 end loop;
5201 end Collect_Entry_Families;
5203 -----------------------
5204 -- Concurrent_Object --
5205 -----------------------
5207 function Concurrent_Object
5208 (Spec_Id : Entity_Id;
5209 Conc_Typ : Entity_Id) return Entity_Id
5211 begin
5212 -- Parameter _O or _object
5214 if Is_Protected_Type (Conc_Typ) then
5215 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5217 -- Parameter _task
5219 else
5220 pragma Assert (Is_Task_Type (Conc_Typ));
5221 return First_Formal (Task_Body_Procedure (Conc_Typ));
5222 end if;
5223 end Concurrent_Object;
5225 ----------------------
5226 -- Copy_Result_Type --
5227 ----------------------
5229 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5230 New_Res : constant Node_Id := New_Copy_Tree (Res);
5231 Par_Spec : Node_Id;
5232 Formal : Entity_Id;
5234 begin
5235 -- If the result type is an access_to_subprogram, we must create new
5236 -- entities for its spec.
5238 if Nkind (New_Res) = N_Access_Definition
5239 and then Present (Access_To_Subprogram_Definition (New_Res))
5240 then
5241 -- Provide new entities for the formals
5243 Par_Spec := First (Parameter_Specifications
5244 (Access_To_Subprogram_Definition (New_Res)));
5245 while Present (Par_Spec) loop
5246 Formal := Defining_Identifier (Par_Spec);
5247 Set_Defining_Identifier (Par_Spec,
5248 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5249 Next (Par_Spec);
5250 end loop;
5251 end if;
5253 return New_Res;
5254 end Copy_Result_Type;
5256 --------------------
5257 -- Concurrent_Ref --
5258 --------------------
5260 -- The expression returned for a reference to a concurrent object has the
5261 -- form:
5263 -- taskV!(name)._Task_Id
5265 -- for a task, and
5267 -- objectV!(name)._Object
5269 -- for a protected object. For the case of an access to a concurrent
5270 -- object, there is an extra explicit dereference:
5272 -- taskV!(name.all)._Task_Id
5273 -- objectV!(name.all)._Object
5275 -- here taskV and objectV are the types for the associated records, which
5276 -- contain the required _Task_Id and _Object fields for tasks and protected
5277 -- objects, respectively.
5279 -- For the case of a task type name, the expression is
5281 -- Self;
5283 -- i.e. a call to the Self function which returns precisely this Task_Id
5285 -- For the case of a protected type name, the expression is
5287 -- objectR
5289 -- which is a renaming of the _object field of the current object
5290 -- record, passed into protected operations as a parameter.
5292 function Concurrent_Ref (N : Node_Id) return Node_Id is
5293 Loc : constant Source_Ptr := Sloc (N);
5294 Ntyp : constant Entity_Id := Etype (N);
5295 Dtyp : Entity_Id;
5296 Sel : Name_Id;
5298 function Is_Current_Task (T : Entity_Id) return Boolean;
5299 -- Check whether the reference is to the immediately enclosing task
5300 -- type, or to an outer one (rare but legal).
5302 ---------------------
5303 -- Is_Current_Task --
5304 ---------------------
5306 function Is_Current_Task (T : Entity_Id) return Boolean is
5307 Scop : Entity_Id;
5309 begin
5310 Scop := Current_Scope;
5311 while Present (Scop) and then Scop /= Standard_Standard loop
5312 if Scop = T then
5313 return True;
5315 elsif Is_Task_Type (Scop) then
5316 return False;
5318 -- If this is a procedure nested within the task type, we must
5319 -- assume that it can be called from an inner task, and therefore
5320 -- cannot treat it as a local reference.
5322 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5323 return False;
5325 else
5326 Scop := Scope (Scop);
5327 end if;
5328 end loop;
5330 -- We know that we are within the task body, so should have found it
5331 -- in scope.
5333 raise Program_Error;
5334 end Is_Current_Task;
5336 -- Start of processing for Concurrent_Ref
5338 begin
5339 if Is_Access_Type (Ntyp) then
5340 Dtyp := Designated_Type (Ntyp);
5342 if Is_Protected_Type (Dtyp) then
5343 Sel := Name_uObject;
5344 else
5345 Sel := Name_uTask_Id;
5346 end if;
5348 return
5349 Make_Selected_Component (Loc,
5350 Prefix =>
5351 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5352 Make_Explicit_Dereference (Loc, N)),
5353 Selector_Name => Make_Identifier (Loc, Sel));
5355 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5356 if Is_Task_Type (Entity (N)) then
5358 if Is_Current_Task (Entity (N)) then
5359 return
5360 Make_Function_Call (Loc,
5361 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5363 else
5364 declare
5365 Decl : Node_Id;
5366 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5367 T_Body : constant Node_Id :=
5368 Parent (Corresponding_Body (Parent (Entity (N))));
5370 begin
5371 Decl :=
5372 Make_Object_Declaration (Loc,
5373 Defining_Identifier => T_Self,
5374 Object_Definition =>
5375 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5376 Expression =>
5377 Make_Function_Call (Loc,
5378 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5379 Prepend (Decl, Declarations (T_Body));
5380 Analyze (Decl);
5381 Set_Scope (T_Self, Entity (N));
5382 return New_Occurrence_Of (T_Self, Loc);
5383 end;
5384 end if;
5386 else
5387 pragma Assert (Is_Protected_Type (Entity (N)));
5389 return
5390 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5391 end if;
5393 else
5394 if Is_Protected_Type (Ntyp) then
5395 Sel := Name_uObject;
5396 elsif Is_Task_Type (Ntyp) then
5397 Sel := Name_uTask_Id;
5398 else
5399 raise Program_Error;
5400 end if;
5402 return
5403 Make_Selected_Component (Loc,
5404 Prefix =>
5405 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5406 New_Copy_Tree (N)),
5407 Selector_Name => Make_Identifier (Loc, Sel));
5408 end if;
5409 end Concurrent_Ref;
5411 ------------------------
5412 -- Convert_Concurrent --
5413 ------------------------
5415 function Convert_Concurrent
5416 (N : Node_Id;
5417 Typ : Entity_Id) return Node_Id
5419 begin
5420 if not Is_Concurrent_Type (Typ) then
5421 return N;
5422 else
5423 return
5424 Unchecked_Convert_To
5425 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5426 end if;
5427 end Convert_Concurrent;
5429 -------------------------------------
5430 -- Create_Secondary_Stack_For_Task --
5431 -------------------------------------
5433 function Create_Secondary_Stack_For_Task (T : Node_Id) return Boolean is
5434 begin
5435 return
5436 (Restriction_Active (No_Implicit_Heap_Allocations)
5437 or else Restriction_Active (No_Implicit_Task_Allocations))
5438 and then not Restriction_Active (No_Secondary_Stack)
5439 and then Has_Rep_Pragma
5440 (T, Name_Secondary_Stack_Size, Check_Parents => False);
5441 end Create_Secondary_Stack_For_Task;
5443 -------------------------------------
5444 -- Debug_Private_Data_Declarations --
5445 -------------------------------------
5447 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5448 Debug_Nod : Node_Id;
5449 Decl : Node_Id;
5451 begin
5452 Decl := First (Decls);
5453 while Present (Decl) and then not Comes_From_Source (Decl) loop
5455 -- Declaration for concurrent entity _object and its access type,
5456 -- along with the entry index subtype:
5457 -- type prot_typVP is access prot_typV;
5458 -- _object : prot_typVP := prot_typV (_O);
5459 -- subtype Jnn is <Type of Index> range Low .. High;
5461 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5462 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5464 -- Declaration for the Protection object, discriminals, privals, and
5465 -- entry index constant:
5466 -- conc_typR : protection_typ renames _object._object;
5467 -- discr_nameD : discr_typ renames _object.discr_name;
5468 -- discr_nameD : discr_typ renames _task.discr_name;
5469 -- prival_name : comp_typ renames _object.comp_name;
5470 -- J : constant Jnn :=
5471 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5473 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5474 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5475 Debug_Nod := Debug_Renaming_Declaration (Decl);
5477 if Present (Debug_Nod) then
5478 Insert_After (Decl, Debug_Nod);
5479 end if;
5480 end if;
5482 Next (Decl);
5483 end loop;
5484 end Debug_Private_Data_Declarations;
5486 ------------------------------
5487 -- Ensure_Statement_Present --
5488 ------------------------------
5490 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5491 Stmt : Node_Id;
5493 begin
5494 if Opt.Suppress_Control_Flow_Optimizations
5495 and then Is_Empty_List (Statements (Alt))
5496 then
5497 Stmt := Make_Null_Statement (Loc);
5499 -- Mark NULL statement as coming from source so that it is not
5500 -- eliminated by GIGI.
5502 -- Another covert channel. If this is a requirement, it must be
5503 -- documented in sinfo/einfo ???
5505 Set_Comes_From_Source (Stmt, True);
5507 Set_Statements (Alt, New_List (Stmt));
5508 end if;
5509 end Ensure_Statement_Present;
5511 ----------------------------
5512 -- Entry_Index_Expression --
5513 ----------------------------
5515 function Entry_Index_Expression
5516 (Sloc : Source_Ptr;
5517 Ent : Entity_Id;
5518 Index : Node_Id;
5519 Ttyp : Entity_Id) return Node_Id
5521 Expr : Node_Id;
5522 Num : Node_Id;
5523 Lo : Node_Id;
5524 Hi : Node_Id;
5525 Prev : Entity_Id;
5526 S : Node_Id;
5528 begin
5529 -- The queues of entries and entry families appear in textual order in
5530 -- the associated record. The entry index is computed as the sum of the
5531 -- number of queues for all entries that precede the designated one, to
5532 -- which is added the index expression, if this expression denotes a
5533 -- member of a family.
5535 -- The following is a place holder for the count of simple entries
5537 Num := Make_Integer_Literal (Sloc, 1);
5539 -- We construct an expression which is a series of addition operations.
5540 -- The first operand is the number of single entries that precede this
5541 -- one, the second operand is the index value relative to the start of
5542 -- the referenced family, and the remaining operands are the lengths of
5543 -- the entry families that precede this entry, i.e. the constructed
5544 -- expression is:
5546 -- number_simple_entries +
5547 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5548 -- family'length + ...
5550 -- where index-value is the given index value, and s is the index
5551 -- subtype (we have to use pos because the subtype might be an
5552 -- enumeration type preventing direct subtraction). Note that the task
5553 -- entry array is one-indexed.
5555 -- The upper bound of the entry family may be a discriminant, so we
5556 -- retrieve the lower bound explicitly to compute offset, rather than
5557 -- using the index subtype which may mention a discriminant.
5559 if Present (Index) then
5560 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5562 Expr :=
5563 Make_Op_Add (Sloc,
5564 Left_Opnd => Num,
5565 Right_Opnd =>
5566 Family_Offset
5567 (Sloc,
5568 Make_Attribute_Reference (Sloc,
5569 Attribute_Name => Name_Pos,
5570 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5571 Expressions => New_List (Relocate_Node (Index))),
5572 Type_Low_Bound (S),
5573 Ttyp,
5574 False));
5575 else
5576 Expr := Num;
5577 end if;
5579 -- Now add lengths of preceding entries and entry families
5581 Prev := First_Entity (Ttyp);
5582 while Chars (Prev) /= Chars (Ent)
5583 or else (Ekind (Prev) /= Ekind (Ent))
5584 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5585 loop
5586 if Ekind (Prev) = E_Entry then
5587 Set_Intval (Num, Intval (Num) + 1);
5589 elsif Ekind (Prev) = E_Entry_Family then
5590 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5591 Lo := Type_Low_Bound (S);
5592 Hi := Type_High_Bound (S);
5594 Expr :=
5595 Make_Op_Add (Sloc,
5596 Left_Opnd => Expr,
5597 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5599 -- Other components are anonymous types to be ignored
5601 else
5602 null;
5603 end if;
5605 Next_Entity (Prev);
5606 end loop;
5608 return Expr;
5609 end Entry_Index_Expression;
5611 ---------------------------
5612 -- Establish_Task_Master --
5613 ---------------------------
5615 procedure Establish_Task_Master (N : Node_Id) is
5616 Call : Node_Id;
5618 begin
5619 if Restriction_Active (No_Task_Hierarchy) = False then
5620 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5622 -- The block may have no declarations (and nevertheless be a task
5623 -- master) if it contains a call that may return an object that
5624 -- contains tasks.
5626 if No (Declarations (N)) then
5627 Set_Declarations (N, New_List (Call));
5628 else
5629 Prepend_To (Declarations (N), Call);
5630 end if;
5632 Analyze (Call);
5633 end if;
5634 end Establish_Task_Master;
5636 --------------------------------
5637 -- Expand_Accept_Declarations --
5638 --------------------------------
5640 -- Part of the expansion of an accept statement involves the creation of
5641 -- a declaration that can be referenced from the statement sequence of
5642 -- the accept:
5644 -- Ann : Address;
5646 -- This declaration is inserted immediately before the accept statement
5647 -- and it is important that it be inserted before the statements of the
5648 -- statement sequence are analyzed. Thus it would be too late to create
5649 -- this declaration in the Expand_N_Accept_Statement routine, which is
5650 -- why there is a separate procedure to be called directly from Sem_Ch9.
5652 -- Ann is used to hold the address of the record containing the parameters
5653 -- (see Expand_N_Entry_Call for more details on how this record is built).
5654 -- References to the parameters do an unchecked conversion of this address
5655 -- to a pointer to the required record type, and then access the field that
5656 -- holds the value of the required parameter. The entity for the address
5657 -- variable is held as the top stack element (i.e. the last element) of the
5658 -- Accept_Address stack in the corresponding entry entity, and this element
5659 -- must be set in place before the statements are processed.
5661 -- The above description applies to the case of a stand alone accept
5662 -- statement, i.e. one not appearing as part of a select alternative.
5664 -- For the case of an accept that appears as part of a select alternative
5665 -- of a selective accept, we must still create the declaration right away,
5666 -- since Ann is needed immediately, but there is an important difference:
5668 -- The declaration is inserted before the selective accept, not before
5669 -- the accept statement (which is not part of a list anyway, and so would
5670 -- not accommodate inserted declarations)
5672 -- We only need one address variable for the entire selective accept. So
5673 -- the Ann declaration is created only for the first accept alternative,
5674 -- and subsequent accept alternatives reference the same Ann variable.
5676 -- We can distinguish the two cases by seeing whether the accept statement
5677 -- is part of a list. If not, then it must be in an accept alternative.
5679 -- To expand the requeue statement, a label is provided at the end of the
5680 -- accept statement or alternative of which it is a part, so that the
5681 -- statement can be skipped after the requeue is complete. This label is
5682 -- created here rather than during the expansion of the accept statement,
5683 -- because it will be needed by any requeue statements within the accept,
5684 -- which are expanded before the accept.
5686 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5687 Loc : constant Source_Ptr := Sloc (N);
5688 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5689 Ann : Entity_Id := Empty;
5690 Adecl : Node_Id;
5691 Lab : Node_Id;
5692 Ldecl : Node_Id;
5693 Ldecl2 : Node_Id;
5695 begin
5696 if Expander_Active then
5698 -- If we have no handled statement sequence, we may need to build
5699 -- a dummy sequence consisting of a null statement. This can be
5700 -- skipped if the trivial accept optimization is permitted.
5702 if not Trivial_Accept_OK
5703 and then (No (Stats) or else Null_Statements (Statements (Stats)))
5704 then
5705 Set_Handled_Statement_Sequence (N,
5706 Make_Handled_Sequence_Of_Statements (Loc,
5707 Statements => New_List (Make_Null_Statement (Loc))));
5708 end if;
5710 -- Create and declare two labels to be placed at the end of the
5711 -- accept statement. The first label is used to allow requeues to
5712 -- skip the remainder of entry processing. The second label is used
5713 -- to skip the remainder of entry processing if the rendezvous
5714 -- completes in the middle of the accept body.
5716 if Present (Handled_Statement_Sequence (N)) then
5717 declare
5718 Ent : Entity_Id;
5720 begin
5721 Ent := Make_Temporary (Loc, 'L');
5722 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5723 Ldecl :=
5724 Make_Implicit_Label_Declaration (Loc,
5725 Defining_Identifier => Ent,
5726 Label_Construct => Lab);
5727 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5729 Ent := Make_Temporary (Loc, 'L');
5730 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5731 Ldecl2 :=
5732 Make_Implicit_Label_Declaration (Loc,
5733 Defining_Identifier => Ent,
5734 Label_Construct => Lab);
5735 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5736 end;
5738 else
5739 Ldecl := Empty;
5740 Ldecl2 := Empty;
5741 end if;
5743 -- Case of stand alone accept statement
5745 if Is_List_Member (N) then
5747 if Present (Handled_Statement_Sequence (N)) then
5748 Ann := Make_Temporary (Loc, 'A');
5750 Adecl :=
5751 Make_Object_Declaration (Loc,
5752 Defining_Identifier => Ann,
5753 Object_Definition =>
5754 New_Occurrence_Of (RTE (RE_Address), Loc));
5756 Insert_Before_And_Analyze (N, Adecl);
5757 Insert_Before_And_Analyze (N, Ldecl);
5758 Insert_Before_And_Analyze (N, Ldecl2);
5759 end if;
5761 -- Case of accept statement which is in an accept alternative
5763 else
5764 declare
5765 Acc_Alt : constant Node_Id := Parent (N);
5766 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5767 Alt : Node_Id;
5769 begin
5770 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5771 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5773 -- ??? Consider a single label for select statements
5775 if Present (Handled_Statement_Sequence (N)) then
5776 Prepend (Ldecl2,
5777 Statements (Handled_Statement_Sequence (N)));
5778 Analyze (Ldecl2);
5780 Prepend (Ldecl,
5781 Statements (Handled_Statement_Sequence (N)));
5782 Analyze (Ldecl);
5783 end if;
5785 -- Find first accept alternative of the selective accept. A
5786 -- valid selective accept must have at least one accept in it.
5788 Alt := First (Select_Alternatives (Sel_Acc));
5790 while Nkind (Alt) /= N_Accept_Alternative loop
5791 Next (Alt);
5792 end loop;
5794 -- If this is the first accept statement, then we have to
5795 -- create the Ann variable, as for the stand alone case, except
5796 -- that it is inserted before the selective accept. Similarly,
5797 -- a label for requeue expansion must be declared.
5799 if N = Accept_Statement (Alt) then
5800 Ann := Make_Temporary (Loc, 'A');
5801 Adecl :=
5802 Make_Object_Declaration (Loc,
5803 Defining_Identifier => Ann,
5804 Object_Definition =>
5805 New_Occurrence_Of (RTE (RE_Address), Loc));
5807 Insert_Before_And_Analyze (Sel_Acc, Adecl);
5809 -- If this is not the first accept statement, then find the Ann
5810 -- variable allocated by the first accept and use it.
5812 else
5813 Ann :=
5814 Node (Last_Elmt (Accept_Address
5815 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5816 end if;
5817 end;
5818 end if;
5820 -- Merge here with Ann either created or referenced, and Adecl
5821 -- pointing to the corresponding declaration. Remaining processing
5822 -- is the same for the two cases.
5824 if Present (Ann) then
5825 Append_Elmt (Ann, Accept_Address (Ent));
5826 Set_Debug_Info_Needed (Ann);
5827 end if;
5829 -- Create renaming declarations for the entry formals. Each reference
5830 -- to a formal becomes a dereference of a component of the parameter
5831 -- block, whose address is held in Ann. These declarations are
5832 -- eventually inserted into the accept block, and analyzed there so
5833 -- that they have the proper scope for gdb and do not conflict with
5834 -- other declarations.
5836 if Present (Parameter_Specifications (N))
5837 and then Present (Handled_Statement_Sequence (N))
5838 then
5839 declare
5840 Comp : Entity_Id;
5841 Decl : Node_Id;
5842 Formal : Entity_Id;
5843 New_F : Entity_Id;
5844 Renamed_Formal : Node_Id;
5846 begin
5847 Push_Scope (Ent);
5848 Formal := First_Formal (Ent);
5850 while Present (Formal) loop
5851 Comp := Entry_Component (Formal);
5852 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
5854 Set_Etype (New_F, Etype (Formal));
5855 Set_Scope (New_F, Ent);
5857 -- Now we set debug info needed on New_F even though it does
5858 -- not come from source, so that the debugger will get the
5859 -- right information for these generated names.
5861 Set_Debug_Info_Needed (New_F);
5863 if Ekind (Formal) = E_In_Parameter then
5864 Set_Ekind (New_F, E_Constant);
5865 else
5866 Set_Ekind (New_F, E_Variable);
5867 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5868 end if;
5870 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5872 Renamed_Formal :=
5873 Make_Selected_Component (Loc,
5874 Prefix =>
5875 Unchecked_Convert_To (
5876 Entry_Parameters_Type (Ent),
5877 New_Occurrence_Of (Ann, Loc)),
5878 Selector_Name =>
5879 New_Occurrence_Of (Comp, Loc));
5881 Decl :=
5882 Build_Renamed_Formal_Declaration
5883 (New_F, Formal, Comp, Renamed_Formal);
5885 if No (Declarations (N)) then
5886 Set_Declarations (N, New_List);
5887 end if;
5889 Append (Decl, Declarations (N));
5890 Set_Renamed_Object (Formal, New_F);
5891 Next_Formal (Formal);
5892 end loop;
5894 End_Scope;
5895 end;
5896 end if;
5897 end if;
5898 end Expand_Accept_Declarations;
5900 ---------------------------------------------
5901 -- Expand_Access_Protected_Subprogram_Type --
5902 ---------------------------------------------
5904 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5905 Loc : constant Source_Ptr := Sloc (N);
5906 T : constant Entity_Id := Defining_Identifier (N);
5907 D_T : constant Entity_Id := Designated_Type (T);
5908 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
5909 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
5910 P_List : constant List_Id :=
5911 Build_Protected_Spec (N, RTE (RE_Address), D_T, False);
5913 Comps : List_Id;
5914 Decl1 : Node_Id;
5915 Decl2 : Node_Id;
5916 Def1 : Node_Id;
5918 begin
5919 -- Create access to subprogram with full signature
5921 if Etype (D_T) /= Standard_Void_Type then
5922 Def1 :=
5923 Make_Access_Function_Definition (Loc,
5924 Parameter_Specifications => P_List,
5925 Result_Definition =>
5926 Copy_Result_Type (Result_Definition (Type_Definition (N))));
5928 else
5929 Def1 :=
5930 Make_Access_Procedure_Definition (Loc,
5931 Parameter_Specifications => P_List);
5932 end if;
5934 Decl1 :=
5935 Make_Full_Type_Declaration (Loc,
5936 Defining_Identifier => D_T2,
5937 Type_Definition => Def1);
5939 -- Declare the new types before the original one since the latter will
5940 -- refer to them through the Equivalent_Type slot.
5942 Insert_Before_And_Analyze (N, Decl1);
5944 -- Associate the access to subprogram with its original access to
5945 -- protected subprogram type. Needed by the backend to know that this
5946 -- type corresponds with an access to protected subprogram type.
5948 Set_Original_Access_Type (D_T2, T);
5950 -- Create Equivalent_Type, a record with two components for an access to
5951 -- object and an access to subprogram.
5953 Comps := New_List (
5954 Make_Component_Declaration (Loc,
5955 Defining_Identifier => Make_Temporary (Loc, 'P'),
5956 Component_Definition =>
5957 Make_Component_Definition (Loc,
5958 Aliased_Present => False,
5959 Subtype_Indication =>
5960 New_Occurrence_Of (RTE (RE_Address), Loc))),
5962 Make_Component_Declaration (Loc,
5963 Defining_Identifier => Make_Temporary (Loc, 'S'),
5964 Component_Definition =>
5965 Make_Component_Definition (Loc,
5966 Aliased_Present => False,
5967 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
5969 Decl2 :=
5970 Make_Full_Type_Declaration (Loc,
5971 Defining_Identifier => E_T,
5972 Type_Definition =>
5973 Make_Record_Definition (Loc,
5974 Component_List =>
5975 Make_Component_List (Loc, Component_Items => Comps)));
5977 Insert_Before_And_Analyze (N, Decl2);
5978 Set_Equivalent_Type (T, E_T);
5979 end Expand_Access_Protected_Subprogram_Type;
5981 --------------------------
5982 -- Expand_Entry_Barrier --
5983 --------------------------
5985 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
5986 Cond : constant Node_Id := Condition (Entry_Body_Formal_Part (N));
5987 Prot : constant Entity_Id := Scope (Ent);
5988 Spec_Decl : constant Node_Id := Parent (Prot);
5990 Func_Id : Entity_Id := Empty;
5991 -- The entity of the barrier function
5993 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
5994 -- Check whether entity in Barrier is external to protected type.
5995 -- If so, barrier may not be properly synchronized.
5997 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
5998 -- Check whether N follows the Pure_Barriers restriction. Return OK if
5999 -- so.
6001 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean;
6002 -- Check whether entity name N denotes a component of the protected
6003 -- object. This is used to check the Simple_Barrier restriction.
6005 ----------------------
6006 -- Is_Global_Entity --
6007 ----------------------
6009 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
6010 E : Entity_Id;
6011 S : Entity_Id;
6013 begin
6014 if Is_Entity_Name (N) and then Present (Entity (N)) then
6015 E := Entity (N);
6016 S := Scope (E);
6018 if Ekind (E) = E_Variable then
6020 -- If the variable is local to the barrier function generated
6021 -- during expansion, it is ok. If expansion is not performed,
6022 -- then Func is Empty so this test cannot succeed.
6024 if Scope (E) = Func_Id then
6025 null;
6027 -- A protected call from a barrier to another object is ok
6029 elsif Ekind (Etype (E)) = E_Protected_Type then
6030 null;
6032 -- If the variable is within the package body we consider
6033 -- this safe. This is a common (if dubious) idiom.
6035 elsif S = Scope (Prot)
6036 and then Ekind_In (S, E_Package, E_Generic_Package)
6037 and then Nkind (Parent (E)) = N_Object_Declaration
6038 and then Nkind (Parent (Parent (E))) = N_Package_Body
6039 then
6040 null;
6042 else
6043 Error_Msg_N ("potentially unsynchronized barrier??", N);
6044 Error_Msg_N ("\& should be private component of type??", N);
6045 end if;
6046 end if;
6047 end if;
6049 return OK;
6050 end Is_Global_Entity;
6052 procedure Check_Unprotected_Barrier is
6053 new Traverse_Proc (Is_Global_Entity);
6055 ----------------------------
6056 -- Is_Simple_Barrier_Name --
6057 ----------------------------
6059 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean is
6060 Renamed : Node_Id;
6062 begin
6063 -- Check if the name is a component of the protected object. If
6064 -- the expander is active, the component has been transformed into a
6065 -- renaming of _object.all.component. Original_Node is needed in case
6066 -- validity checking is enabled, in which case the simple object
6067 -- reference will have been rewritten.
6069 if Expander_Active then
6071 -- The expanded name may have been constant folded in which case
6072 -- the original node is not necessarily an entity name (e.g. an
6073 -- indexed component).
6075 if not Is_Entity_Name (Original_Node (N)) then
6076 return False;
6077 end if;
6079 Renamed := Renamed_Object (Entity (Original_Node (N)));
6081 return
6082 Present (Renamed)
6083 and then Nkind (Renamed) = N_Selected_Component
6084 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
6085 else
6086 return Is_Protected_Component (Entity (N));
6087 end if;
6088 end Is_Simple_Barrier_Name;
6090 ---------------------
6091 -- Is_Pure_Barrier --
6092 ---------------------
6094 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
6095 begin
6096 case Nkind (N) is
6097 when N_Expanded_Name
6098 | N_Identifier
6100 if No (Entity (N)) then
6101 return Abandon;
6103 elsif Is_Universal_Numeric_Type (Entity (N)) then
6104 return OK;
6105 end if;
6107 case Ekind (Entity (N)) is
6108 when E_Constant
6109 | E_Discriminant
6110 | E_Enumeration_Literal
6111 | E_Named_Integer
6112 | E_Named_Real
6114 return OK;
6116 when E_Component =>
6117 return OK;
6119 when E_Variable =>
6120 if Is_Simple_Barrier_Name (N) then
6121 return OK;
6122 end if;
6124 when E_Function =>
6126 -- The count attribute has been transformed into run-time
6127 -- calls.
6129 if Is_RTE (Entity (N), RE_Protected_Count)
6130 or else Is_RTE (Entity (N), RE_Protected_Count_Entry)
6131 then
6132 return OK;
6133 end if;
6135 when others =>
6136 null;
6137 end case;
6139 when N_Function_Call =>
6141 -- Function call checks are carried out as part of the analysis
6142 -- of the function call name.
6144 return OK;
6146 when N_Character_Literal
6147 | N_Integer_Literal
6148 | N_Real_Literal
6150 return OK;
6152 when N_Op_Boolean
6153 | N_Op_Not
6155 if Ekind (Entity (N)) = E_Operator then
6156 return OK;
6157 end if;
6159 when N_Short_Circuit =>
6160 return OK;
6162 when N_Indexed_Component
6163 | N_Selected_Component
6165 if not Is_Access_Type (Etype (Prefix (N))) then
6166 return OK;
6167 end if;
6169 when N_Type_Conversion =>
6171 -- Conversions to Universal_Integer will not raise constraint
6172 -- errors.
6174 if Cannot_Raise_Constraint_Error (N)
6175 or else Etype (N) = Universal_Integer
6176 then
6177 return OK;
6178 end if;
6180 when N_Unchecked_Type_Conversion =>
6181 return OK;
6183 when others =>
6184 null;
6185 end case;
6187 return Abandon;
6188 end Is_Pure_Barrier;
6190 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6192 -- Local variables
6194 Cond_Id : Entity_Id;
6195 Entry_Body : Node_Id;
6196 Func_Body : Node_Id := Empty;
6198 -- Start of processing for Expand_Entry_Barrier
6200 begin
6201 if No_Run_Time_Mode then
6202 Error_Msg_CRT ("entry barrier", N);
6203 return;
6204 end if;
6206 -- The body of the entry barrier must be analyzed in the context of the
6207 -- protected object, but its scope is external to it, just as any other
6208 -- unprotected version of a protected operation. The specification has
6209 -- been produced when the protected type declaration was elaborated. We
6210 -- build the body, insert it in the enclosing scope, but analyze it in
6211 -- the current context. A more uniform approach would be to treat the
6212 -- barrier just as a protected function, and discard the protected
6213 -- version of it because it is never called.
6215 if Expander_Active then
6216 Func_Body := Build_Barrier_Function (N, Ent, Prot);
6217 Func_Id := Barrier_Function (Ent);
6218 Set_Corresponding_Spec (Func_Body, Func_Id);
6220 Entry_Body := Parent (Corresponding_Body (Spec_Decl));
6222 if Nkind (Parent (Entry_Body)) = N_Subunit then
6223 Entry_Body := Corresponding_Stub (Parent (Entry_Body));
6224 end if;
6226 Insert_Before_And_Analyze (Entry_Body, Func_Body);
6228 Set_Discriminals (Spec_Decl);
6229 Set_Scope (Func_Id, Scope (Prot));
6231 else
6232 Analyze_And_Resolve (Cond, Any_Boolean);
6233 end if;
6235 -- Check Pure_Barriers restriction
6237 if Check_Pure_Barriers (Cond) = Abandon then
6238 Check_Restriction (Pure_Barriers, Cond);
6239 end if;
6241 -- The Ravenscar profile restricts barriers to simple variables declared
6242 -- within the protected object. We also allow Boolean constants, since
6243 -- these appear in several published examples and are also allowed by
6244 -- other compilers.
6246 -- Note that after analysis variables in this context will be replaced
6247 -- by the corresponding prival, that is to say a renaming of a selected
6248 -- component of the form _Object.Var. If expansion is disabled, as
6249 -- within a generic, we check that the entity appears in the current
6250 -- scope.
6252 if Is_Entity_Name (Cond) then
6253 Cond_Id := Entity (Cond);
6255 -- Perform a small optimization of simple barrier functions. If the
6256 -- scope of the condition's entity is not the barrier function, then
6257 -- the condition does not depend on any of the generated renamings.
6258 -- If this is the case, eliminate the renamings as they are useless.
6259 -- This optimization is not performed when the condition was folded
6260 -- and validity checks are in effect because the original condition
6261 -- may have produced at least one check that depends on the generated
6262 -- renamings.
6264 if Expander_Active
6265 and then Scope (Cond_Id) /= Func_Id
6266 and then not Validity_Check_Operands
6267 then
6268 Set_Declarations (Func_Body, Empty_List);
6269 end if;
6271 if Cond_Id = Standard_False or else Cond_Id = Standard_True then
6272 return;
6274 elsif Is_Simple_Barrier_Name (Cond) then
6275 return;
6276 end if;
6277 end if;
6279 -- It is not a boolean variable or literal, so check the restriction.
6280 -- Note that it is safe to be calling Check_Restriction from here, even
6281 -- though this is part of the expander, since Expand_Entry_Barrier is
6282 -- called from Sem_Ch9 even in -gnatc mode.
6284 Check_Restriction (Simple_Barriers, Cond);
6286 -- Emit warning if barrier contains global entities and is thus
6287 -- potentially unsynchronized.
6289 Check_Unprotected_Barrier (Cond);
6290 end Expand_Entry_Barrier;
6292 ------------------------------
6293 -- Expand_N_Abort_Statement --
6294 ------------------------------
6296 -- Expand abort T1, T2, .. Tn; into:
6297 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6299 procedure Expand_N_Abort_Statement (N : Node_Id) is
6300 Loc : constant Source_Ptr := Sloc (N);
6301 Tlist : constant List_Id := Names (N);
6302 Count : Nat;
6303 Aggr : Node_Id;
6304 Tasknm : Node_Id;
6306 begin
6307 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6308 Count := 0;
6310 Tasknm := First (Tlist);
6312 while Present (Tasknm) loop
6313 Count := Count + 1;
6315 -- A task interface class-wide type object is being aborted. Retrieve
6316 -- its _task_id by calling a dispatching routine.
6318 if Ada_Version >= Ada_2005
6319 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6320 and then Is_Interface (Etype (Tasknm))
6321 and then Is_Task_Interface (Etype (Tasknm))
6322 then
6323 Append_To (Component_Associations (Aggr),
6324 Make_Component_Association (Loc,
6325 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6326 Expression =>
6328 -- Task_Id (Tasknm._disp_get_task_id)
6330 Make_Unchecked_Type_Conversion (Loc,
6331 Subtype_Mark =>
6332 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6333 Expression =>
6334 Make_Selected_Component (Loc,
6335 Prefix => New_Copy_Tree (Tasknm),
6336 Selector_Name =>
6337 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6339 else
6340 Append_To (Component_Associations (Aggr),
6341 Make_Component_Association (Loc,
6342 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6343 Expression => Concurrent_Ref (Tasknm)));
6344 end if;
6346 Next (Tasknm);
6347 end loop;
6349 Rewrite (N,
6350 Make_Procedure_Call_Statement (Loc,
6351 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6352 Parameter_Associations => New_List (
6353 Make_Qualified_Expression (Loc,
6354 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6355 Expression => Aggr))));
6357 Analyze (N);
6358 end Expand_N_Abort_Statement;
6360 -------------------------------
6361 -- Expand_N_Accept_Statement --
6362 -------------------------------
6364 -- This procedure handles expansion of accept statements that stand alone,
6365 -- i.e. they are not part of an accept alternative. The expansion of
6366 -- accept statement in accept alternatives is handled by the routines
6367 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6368 -- following description applies only to stand alone accept statements.
6370 -- If there is no handled statement sequence, or only null statements, then
6371 -- this is called a trivial accept, and the expansion is:
6373 -- Accept_Trivial (entry-index)
6375 -- If there is a handled statement sequence, then the expansion is:
6377 -- Ann : Address;
6378 -- {Lnn : Label}
6380 -- begin
6381 -- begin
6382 -- Accept_Call (entry-index, Ann);
6383 -- Renaming_Declarations for formals
6384 -- <statement sequence from N_Accept_Statement node>
6385 -- Complete_Rendezvous;
6386 -- <<Lnn>>
6388 -- exception
6389 -- when ... =>
6390 -- <exception handler from N_Accept_Statement node>
6391 -- Complete_Rendezvous;
6392 -- when ... =>
6393 -- <exception handler from N_Accept_Statement node>
6394 -- Complete_Rendezvous;
6395 -- ...
6396 -- end;
6398 -- exception
6399 -- when all others =>
6400 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6401 -- end;
6403 -- The first three declarations were already inserted ahead of the accept
6404 -- statement by the Expand_Accept_Declarations procedure, which was called
6405 -- directly from the semantics during analysis of the accept statement,
6406 -- before analyzing its contained statements.
6408 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6409 -- from possible expansion activity (the original source of course does
6410 -- not have any declarations associated with the accept statement, since
6411 -- an accept statement has no declarative part). In particular, if the
6412 -- expander is active, the first such declaration is the declaration of
6413 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6415 -- The two blocks are merged into a single block if the inner block has
6416 -- no exception handlers, but otherwise two blocks are required, since
6417 -- exceptions might be raised in the exception handlers of the inner
6418 -- block, and Exceptional_Complete_Rendezvous must be called.
6420 procedure Expand_N_Accept_Statement (N : Node_Id) is
6421 Loc : constant Source_Ptr := Sloc (N);
6422 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6423 Ename : constant Node_Id := Entry_Direct_Name (N);
6424 Eindx : constant Node_Id := Entry_Index (N);
6425 Eent : constant Entity_Id := Entity (Ename);
6426 Acstack : constant Elist_Id := Accept_Address (Eent);
6427 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6428 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6429 Blkent : Entity_Id;
6430 Call : Node_Id;
6431 Block : Node_Id;
6433 begin
6434 -- If the accept statement is not part of a list, then its parent must
6435 -- be an accept alternative, and, as described above, we do not do any
6436 -- expansion for such accept statements at this level.
6438 if not Is_List_Member (N) then
6439 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6440 return;
6442 -- Trivial accept case (no statement sequence, or null statements).
6443 -- If the accept statement has declarations, then just insert them
6444 -- before the procedure call.
6446 elsif Trivial_Accept_OK
6447 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6448 then
6449 -- Remove declarations for renamings, because the parameter block
6450 -- will not be assigned.
6452 declare
6453 D : Node_Id;
6454 Next_D : Node_Id;
6456 begin
6457 D := First (Declarations (N));
6458 while Present (D) loop
6459 Next_D := Next (D);
6460 if Nkind (D) = N_Object_Renaming_Declaration then
6461 Remove (D);
6462 end if;
6464 D := Next_D;
6465 end loop;
6466 end;
6468 if Present (Declarations (N)) then
6469 Insert_Actions (N, Declarations (N));
6470 end if;
6472 Rewrite (N,
6473 Make_Procedure_Call_Statement (Loc,
6474 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6475 Parameter_Associations => New_List (
6476 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6478 Analyze (N);
6480 -- Discard Entry_Address that was created for it, so it will not be
6481 -- emitted if this accept statement is in the statement part of a
6482 -- delay alternative.
6484 if Present (Stats) then
6485 Remove_Last_Elmt (Acstack);
6486 end if;
6488 -- Case of statement sequence present
6490 else
6491 -- Construct the block, using the declarations from the accept
6492 -- statement if any to initialize the declarations of the block.
6494 Blkent := Make_Temporary (Loc, 'A');
6495 Set_Ekind (Blkent, E_Block);
6496 Set_Etype (Blkent, Standard_Void_Type);
6497 Set_Scope (Blkent, Current_Scope);
6499 Block :=
6500 Make_Block_Statement (Loc,
6501 Identifier => New_Occurrence_Of (Blkent, Loc),
6502 Declarations => Declarations (N),
6503 Handled_Statement_Sequence => Build_Accept_Body (N));
6505 -- For the analysis of the generated declarations, the parent node
6506 -- must be properly set.
6508 Set_Parent (Block, Parent (N));
6510 -- Prepend call to Accept_Call to main statement sequence If the
6511 -- accept has exception handlers, the statement sequence is wrapped
6512 -- in a block. Insert call and renaming declarations in the
6513 -- declarations of the block, so they are elaborated before the
6514 -- handlers.
6516 Call :=
6517 Make_Procedure_Call_Statement (Loc,
6518 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6519 Parameter_Associations => New_List (
6520 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6521 New_Occurrence_Of (Ann, Loc)));
6523 if Parent (Stats) = N then
6524 Prepend (Call, Statements (Stats));
6525 else
6526 Set_Declarations (Parent (Stats), New_List (Call));
6527 end if;
6529 Analyze (Call);
6531 Push_Scope (Blkent);
6533 declare
6534 D : Node_Id;
6535 Next_D : Node_Id;
6536 Typ : Entity_Id;
6538 begin
6539 D := First (Declarations (N));
6540 while Present (D) loop
6541 Next_D := Next (D);
6543 if Nkind (D) = N_Object_Renaming_Declaration then
6545 -- The renaming declarations for the formals were created
6546 -- during analysis of the accept statement, and attached to
6547 -- the list of declarations. Place them now in the context
6548 -- of the accept block or subprogram.
6550 Remove (D);
6551 Typ := Entity (Subtype_Mark (D));
6552 Insert_After (Call, D);
6553 Analyze (D);
6555 -- If the formal is class_wide, it does not have an actual
6556 -- subtype. The analysis of the renaming declaration creates
6557 -- one, but we need to retain the class-wide nature of the
6558 -- entity.
6560 if Is_Class_Wide_Type (Typ) then
6561 Set_Etype (Defining_Identifier (D), Typ);
6562 end if;
6564 end if;
6566 D := Next_D;
6567 end loop;
6568 end;
6570 End_Scope;
6572 -- Replace the accept statement by the new block
6574 Rewrite (N, Block);
6575 Analyze (N);
6577 -- Last step is to unstack the Accept_Address value
6579 Remove_Last_Elmt (Acstack);
6580 end if;
6581 end Expand_N_Accept_Statement;
6583 ----------------------------------
6584 -- Expand_N_Asynchronous_Select --
6585 ----------------------------------
6587 -- This procedure assumes that the trigger statement is an entry call or
6588 -- a dispatching procedure call. A delay alternative should already have
6589 -- been expanded into an entry call to the appropriate delay object Wait
6590 -- entry.
6592 -- If the trigger is a task entry call, the select is implemented with
6593 -- a Task_Entry_Call:
6595 -- declare
6596 -- B : Boolean;
6597 -- C : Boolean;
6598 -- P : parms := (parm, parm, parm);
6600 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6602 -- procedure _clean is
6603 -- begin
6604 -- ...
6605 -- Cancel_Task_Entry_Call (C);
6606 -- ...
6607 -- end _clean;
6609 -- begin
6610 -- Abort_Defer;
6611 -- Task_Entry_Call
6612 -- (<acceptor-task>, -- Acceptor
6613 -- <entry-index>, -- E
6614 -- P'Address, -- Uninterpreted_Data
6615 -- Asynchronous_Call, -- Mode
6616 -- B); -- Rendezvous_Successful
6618 -- begin
6619 -- begin
6620 -- Abort_Undefer;
6621 -- <abortable-part>
6622 -- at end
6623 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6624 -- end;
6625 -- exception
6626 -- when Abort_Signal => Abort_Undefer;
6627 -- end;
6629 -- parm := P.param;
6630 -- parm := P.param;
6631 -- ...
6632 -- if not C then
6633 -- <triggered-statements>
6634 -- end if;
6635 -- end;
6637 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6638 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6639 -- as follows:
6641 -- declare
6642 -- P : parms := (parm, parm, parm);
6643 -- begin
6644 -- Call_Simple (acceptor-task, entry-index, P'Address);
6645 -- parm := P.param;
6646 -- parm := P.param;
6647 -- ...
6648 -- end;
6650 -- so the task at hand is to convert the latter expansion into the former
6652 -- If the trigger is a protected entry call, the select is implemented
6653 -- with Protected_Entry_Call:
6655 -- declare
6656 -- P : E1_Params := (param, param, param);
6657 -- Bnn : Communications_Block;
6659 -- begin
6660 -- declare
6662 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6664 -- procedure _clean is
6665 -- begin
6666 -- ...
6667 -- if Enqueued (Bnn) then
6668 -- Cancel_Protected_Entry_Call (Bnn);
6669 -- end if;
6670 -- ...
6671 -- end _clean;
6673 -- begin
6674 -- begin
6675 -- Protected_Entry_Call
6676 -- (po._object'Access, -- Object
6677 -- <entry index>, -- E
6678 -- P'Address, -- Uninterpreted_Data
6679 -- Asynchronous_Call, -- Mode
6680 -- Bnn); -- Block
6682 -- if Enqueued (Bnn) then
6683 -- <abortable-part>
6684 -- end if;
6685 -- at end
6686 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6687 -- end;
6688 -- exception
6689 -- when Abort_Signal => Abort_Undefer;
6690 -- end;
6692 -- if not Cancelled (Bnn) then
6693 -- <triggered-statements>
6694 -- end if;
6695 -- end;
6697 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6698 -- entry call:
6700 -- declare
6701 -- P : E1_Params := (param, param, param);
6702 -- Bnn : Communications_Block;
6704 -- begin
6705 -- Protected_Entry_Call
6706 -- (po._object'Access, -- Object
6707 -- <entry index>, -- E
6708 -- P'Address, -- Uninterpreted_Data
6709 -- Simple_Call, -- Mode
6710 -- Bnn); -- Block
6711 -- parm := P.param;
6712 -- parm := P.param;
6713 -- ...
6714 -- end;
6716 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6717 -- expanded into:
6719 -- declare
6720 -- B : Boolean := False;
6721 -- Bnn : Communication_Block;
6722 -- C : Ada.Tags.Prim_Op_Kind;
6723 -- D : System.Storage_Elements.Dummy_Communication_Block;
6724 -- K : Ada.Tags.Tagged_Kind :=
6725 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6726 -- P : Parameters := (Param1 .. ParamN);
6727 -- S : Integer;
6728 -- U : Boolean;
6730 -- begin
6731 -- if K = Ada.Tags.TK_Limited_Tagged
6732 -- or else K = Ada.Tags.TK_Tagged
6733 -- then
6734 -- <dispatching-call>;
6735 -- <triggering-statements>;
6737 -- else
6738 -- S :=
6739 -- Ada.Tags.Get_Offset_Index
6740 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6742 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6744 -- if C = POK_Protected_Entry then
6745 -- declare
6746 -- procedure _clean is
6747 -- begin
6748 -- if Enqueued (Bnn) then
6749 -- Cancel_Protected_Entry_Call (Bnn);
6750 -- end if;
6751 -- end _clean;
6753 -- begin
6754 -- begin
6755 -- _Disp_Asynchronous_Select
6756 -- (<object>, S, P'Address, D, B);
6757 -- Bnn := Communication_Block (D);
6759 -- Param1 := P.Param1;
6760 -- ...
6761 -- ParamN := P.ParamN;
6763 -- if Enqueued (Bnn) then
6764 -- <abortable-statements>
6765 -- end if;
6766 -- at end
6767 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6768 -- end;
6769 -- exception
6770 -- when Abort_Signal => Abort_Undefer;
6771 -- end;
6773 -- if not Cancelled (Bnn) then
6774 -- <triggering-statements>
6775 -- end if;
6777 -- elsif C = POK_Task_Entry then
6778 -- declare
6779 -- procedure _clean is
6780 -- begin
6781 -- Cancel_Task_Entry_Call (U);
6782 -- end _clean;
6784 -- begin
6785 -- Abort_Defer;
6787 -- _Disp_Asynchronous_Select
6788 -- (<object>, S, P'Address, D, B);
6789 -- Bnn := Communication_Bloc (D);
6791 -- Param1 := P.Param1;
6792 -- ...
6793 -- ParamN := P.ParamN;
6795 -- begin
6796 -- begin
6797 -- Abort_Undefer;
6798 -- <abortable-statements>
6799 -- at end
6800 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6801 -- end;
6802 -- exception
6803 -- when Abort_Signal => Abort_Undefer;
6804 -- end;
6806 -- if not U then
6807 -- <triggering-statements>
6808 -- end if;
6809 -- end;
6811 -- else
6812 -- <dispatching-call>;
6813 -- <triggering-statements>
6814 -- end if;
6815 -- end if;
6816 -- end;
6818 -- The job is to convert this to the asynchronous form
6820 -- If the trigger is a delay statement, it will have been expanded into
6821 -- a call to one of the GNARL delay procedures. This routine will convert
6822 -- this into a protected entry call on a delay object and then continue
6823 -- processing as for a protected entry call trigger. This requires
6824 -- declaring a Delay_Block object and adding a pointer to this object to
6825 -- the parameter list of the delay procedure to form the parameter list of
6826 -- the entry call. This object is used by the runtime to queue the delay
6827 -- request.
6829 -- For a description of the use of P and the assignments after the call,
6830 -- see Expand_N_Entry_Call_Statement.
6832 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6833 Loc : constant Source_Ptr := Sloc (N);
6834 Abrt : constant Node_Id := Abortable_Part (N);
6835 Trig : constant Node_Id := Triggering_Alternative (N);
6837 Abort_Block_Ent : Entity_Id;
6838 Abortable_Block : Node_Id;
6839 Actuals : List_Id;
6840 Astats : List_Id;
6841 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6842 Blk_Typ : Entity_Id;
6843 Call : Node_Id;
6844 Call_Ent : Entity_Id;
6845 Cancel_Param : Entity_Id;
6846 Cleanup_Block : Node_Id;
6847 Cleanup_Block_Ent : Entity_Id;
6848 Cleanup_Stmts : List_Id;
6849 Conc_Typ_Stmts : List_Id;
6850 Concval : Node_Id;
6851 Dblock_Ent : Entity_Id;
6852 Decl : Node_Id;
6853 Decls : List_Id;
6854 Ecall : Node_Id;
6855 Ename : Node_Id;
6856 Enqueue_Call : Node_Id;
6857 Formals : List_Id;
6858 Hdle : List_Id;
6859 Handler_Stmt : Node_Id;
6860 Index : Node_Id;
6861 Lim_Typ_Stmts : List_Id;
6862 N_Orig : Node_Id;
6863 Obj : Entity_Id;
6864 Param : Node_Id;
6865 Params : List_Id;
6866 Pdef : Entity_Id;
6867 ProtE_Stmts : List_Id;
6868 ProtP_Stmts : List_Id;
6869 Stmt : Node_Id;
6870 Stmts : List_Id;
6871 TaskE_Stmts : List_Id;
6872 Tstats : List_Id;
6874 B : Entity_Id; -- Call status flag
6875 Bnn : Entity_Id; -- Communication block
6876 C : Entity_Id; -- Call kind
6877 K : Entity_Id; -- Tagged kind
6878 P : Entity_Id; -- Parameter block
6879 S : Entity_Id; -- Primitive operation slot
6880 T : Entity_Id; -- Additional status flag
6882 procedure Rewrite_Abortable_Part;
6883 -- If the trigger is a dispatching call, the expansion inserts multiple
6884 -- copies of the abortable part. This is both inefficient, and may lead
6885 -- to duplicate definitions that the back-end will reject, when the
6886 -- abortable part includes loops. This procedure rewrites the abortable
6887 -- part into a call to a generated procedure.
6889 ----------------------------
6890 -- Rewrite_Abortable_Part --
6891 ----------------------------
6893 procedure Rewrite_Abortable_Part is
6894 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6895 Decl : Node_Id;
6897 begin
6898 Decl :=
6899 Make_Subprogram_Body (Loc,
6900 Specification =>
6901 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6902 Declarations => New_List,
6903 Handled_Statement_Sequence =>
6904 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6905 Insert_Before (N, Decl);
6906 Analyze (Decl);
6908 -- Rewrite abortable part into a call to this procedure
6910 Astats :=
6911 New_List (
6912 Make_Procedure_Call_Statement (Loc,
6913 Name => New_Occurrence_Of (Proc, Loc)));
6914 end Rewrite_Abortable_Part;
6916 -- Start of processing for Expand_N_Asynchronous_Select
6918 begin
6919 -- Asynchronous select is not supported on restricted runtimes. Don't
6920 -- try to expand.
6922 if Restricted_Profile then
6923 return;
6924 end if;
6926 Process_Statements_For_Controlled_Objects (Trig);
6927 Process_Statements_For_Controlled_Objects (Abrt);
6929 Ecall := Triggering_Statement (Trig);
6931 Ensure_Statement_Present (Sloc (Ecall), Trig);
6933 -- Retrieve Astats and Tstats now because the finalization machinery may
6934 -- wrap them in blocks.
6936 Astats := Statements (Abrt);
6937 Tstats := Statements (Trig);
6939 -- The arguments in the call may require dynamic allocation, and the
6940 -- call statement may have been transformed into a block. The block
6941 -- may contain additional declarations for internal entities, and the
6942 -- original call is found by sequential search.
6944 if Nkind (Ecall) = N_Block_Statement then
6945 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6946 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6947 N_Entry_Call_Statement)
6948 loop
6949 Next (Ecall);
6950 end loop;
6951 end if;
6953 -- This is either a dispatching call or a delay statement used as a
6954 -- trigger which was expanded into a procedure call.
6956 if Nkind (Ecall) = N_Procedure_Call_Statement then
6957 if Ada_Version >= Ada_2005
6958 and then
6959 (No (Original_Node (Ecall))
6960 or else not Nkind_In (Original_Node (Ecall),
6961 N_Delay_Relative_Statement,
6962 N_Delay_Until_Statement))
6963 then
6964 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6966 Rewrite_Abortable_Part;
6967 Decls := New_List;
6968 Stmts := New_List;
6970 -- Call status flag processing, generate:
6971 -- B : Boolean := False;
6973 B := Build_B (Loc, Decls);
6975 -- Communication block processing, generate:
6976 -- Bnn : Communication_Block;
6978 Bnn := Make_Temporary (Loc, 'B');
6979 Append_To (Decls,
6980 Make_Object_Declaration (Loc,
6981 Defining_Identifier => Bnn,
6982 Object_Definition =>
6983 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
6985 -- Call kind processing, generate:
6986 -- C : Ada.Tags.Prim_Op_Kind;
6988 C := Build_C (Loc, Decls);
6990 -- Tagged kind processing, generate:
6991 -- K : Ada.Tags.Tagged_Kind :=
6992 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6994 -- Dummy communication block, generate:
6995 -- D : Dummy_Communication_Block;
6997 Append_To (Decls,
6998 Make_Object_Declaration (Loc,
6999 Defining_Identifier =>
7000 Make_Defining_Identifier (Loc, Name_uD),
7001 Object_Definition =>
7002 New_Occurrence_Of
7003 (RTE (RE_Dummy_Communication_Block), Loc)));
7005 K := Build_K (Loc, Decls, Obj);
7007 -- Parameter block processing
7009 Blk_Typ := Build_Parameter_Block
7010 (Loc, Actuals, Formals, Decls);
7011 P := Parameter_Block_Pack
7012 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7014 -- Dispatch table slot processing, generate:
7015 -- S : Integer;
7017 S := Build_S (Loc, Decls);
7019 -- Additional status flag processing, generate:
7020 -- Tnn : Boolean;
7022 T := Make_Temporary (Loc, 'T');
7023 Append_To (Decls,
7024 Make_Object_Declaration (Loc,
7025 Defining_Identifier => T,
7026 Object_Definition =>
7027 New_Occurrence_Of (Standard_Boolean, Loc)));
7029 ------------------------------
7030 -- Protected entry handling --
7031 ------------------------------
7033 -- Generate:
7034 -- Param1 := P.Param1;
7035 -- ...
7036 -- ParamN := P.ParamN;
7038 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7040 -- Generate:
7041 -- Bnn := Communication_Block (D);
7043 Prepend_To (Cleanup_Stmts,
7044 Make_Assignment_Statement (Loc,
7045 Name => New_Occurrence_Of (Bnn, Loc),
7046 Expression =>
7047 Make_Unchecked_Type_Conversion (Loc,
7048 Subtype_Mark =>
7049 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7050 Expression => Make_Identifier (Loc, Name_uD))));
7052 -- Generate:
7053 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7055 Prepend_To (Cleanup_Stmts,
7056 Make_Procedure_Call_Statement (Loc,
7057 Name =>
7058 New_Occurrence_Of
7059 (Find_Prim_Op
7060 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7061 Loc),
7062 Parameter_Associations =>
7063 New_List (
7064 New_Copy_Tree (Obj), -- <object>
7065 New_Occurrence_Of (S, Loc), -- S
7066 Make_Attribute_Reference (Loc, -- P'Address
7067 Prefix => New_Occurrence_Of (P, Loc),
7068 Attribute_Name => Name_Address),
7069 Make_Identifier (Loc, Name_uD), -- D
7070 New_Occurrence_Of (B, Loc)))); -- B
7072 -- Generate:
7073 -- if Enqueued (Bnn) then
7074 -- <abortable-statements>
7075 -- end if;
7077 Append_To (Cleanup_Stmts,
7078 Make_Implicit_If_Statement (N,
7079 Condition =>
7080 Make_Function_Call (Loc,
7081 Name =>
7082 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7083 Parameter_Associations =>
7084 New_List (New_Occurrence_Of (Bnn, Loc))),
7086 Then_Statements =>
7087 New_Copy_List_Tree (Astats)));
7089 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7090 -- will then generate a _clean for the communication block Bnn.
7092 -- Generate:
7093 -- declare
7094 -- procedure _clean is
7095 -- begin
7096 -- if Enqueued (Bnn) then
7097 -- Cancel_Protected_Entry_Call (Bnn);
7098 -- end if;
7099 -- end _clean;
7100 -- begin
7101 -- Cleanup_Stmts
7102 -- at end
7103 -- _clean;
7104 -- end;
7106 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7107 Cleanup_Block :=
7108 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7110 -- Wrap the cleanup block in an exception handling block
7112 -- Generate:
7113 -- begin
7114 -- Cleanup_Block
7115 -- exception
7116 -- when Abort_Signal => Abort_Undefer;
7117 -- end;
7119 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7120 ProtE_Stmts :=
7121 New_List (
7122 Make_Implicit_Label_Declaration (Loc,
7123 Defining_Identifier => Abort_Block_Ent),
7125 Build_Abort_Block
7126 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7128 -- Generate:
7129 -- if not Cancelled (Bnn) then
7130 -- <triggering-statements>
7131 -- end if;
7133 Append_To (ProtE_Stmts,
7134 Make_Implicit_If_Statement (N,
7135 Condition =>
7136 Make_Op_Not (Loc,
7137 Right_Opnd =>
7138 Make_Function_Call (Loc,
7139 Name =>
7140 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7141 Parameter_Associations =>
7142 New_List (New_Occurrence_Of (Bnn, Loc)))),
7144 Then_Statements =>
7145 New_Copy_List_Tree (Tstats)));
7147 -------------------------
7148 -- Task entry handling --
7149 -------------------------
7151 -- Generate:
7152 -- Param1 := P.Param1;
7153 -- ...
7154 -- ParamN := P.ParamN;
7156 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7158 -- Generate:
7159 -- Bnn := Communication_Block (D);
7161 Append_To (TaskE_Stmts,
7162 Make_Assignment_Statement (Loc,
7163 Name =>
7164 New_Occurrence_Of (Bnn, Loc),
7165 Expression =>
7166 Make_Unchecked_Type_Conversion (Loc,
7167 Subtype_Mark =>
7168 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7169 Expression => Make_Identifier (Loc, Name_uD))));
7171 -- Generate:
7172 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7174 Prepend_To (TaskE_Stmts,
7175 Make_Procedure_Call_Statement (Loc,
7176 Name =>
7177 New_Occurrence_Of (
7178 Find_Prim_Op (Etype (Etype (Obj)),
7179 Name_uDisp_Asynchronous_Select),
7180 Loc),
7182 Parameter_Associations => New_List (
7183 New_Copy_Tree (Obj), -- <object>
7184 New_Occurrence_Of (S, Loc), -- S
7185 Make_Attribute_Reference (Loc, -- P'Address
7186 Prefix => New_Occurrence_Of (P, Loc),
7187 Attribute_Name => Name_Address),
7188 Make_Identifier (Loc, Name_uD), -- D
7189 New_Occurrence_Of (B, Loc)))); -- B
7191 -- Generate:
7192 -- Abort_Defer;
7194 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7196 -- Generate:
7197 -- Abort_Undefer;
7198 -- <abortable-statements>
7200 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7202 Prepend_To
7203 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7205 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7206 -- will generate a _clean for the additional status flag.
7208 -- Generate:
7209 -- declare
7210 -- procedure _clean is
7211 -- begin
7212 -- Cancel_Task_Entry_Call (U);
7213 -- end _clean;
7214 -- begin
7215 -- Cleanup_Stmts
7216 -- at end
7217 -- _clean;
7218 -- end;
7220 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7221 Cleanup_Block :=
7222 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7224 -- Wrap the cleanup block in an exception handling block
7226 -- Generate:
7227 -- begin
7228 -- Cleanup_Block
7229 -- exception
7230 -- when Abort_Signal => Abort_Undefer;
7231 -- end;
7233 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7235 Append_To (TaskE_Stmts,
7236 Make_Implicit_Label_Declaration (Loc,
7237 Defining_Identifier => Abort_Block_Ent));
7239 Append_To (TaskE_Stmts,
7240 Build_Abort_Block
7241 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7243 -- Generate:
7244 -- if not T then
7245 -- <triggering-statements>
7246 -- end if;
7248 Append_To (TaskE_Stmts,
7249 Make_Implicit_If_Statement (N,
7250 Condition =>
7251 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7253 Then_Statements =>
7254 New_Copy_List_Tree (Tstats)));
7256 ----------------------------------
7257 -- Protected procedure handling --
7258 ----------------------------------
7260 -- Generate:
7261 -- <dispatching-call>;
7262 -- <triggering-statements>
7264 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7265 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7267 -- Generate:
7268 -- S := Ada.Tags.Get_Offset_Index
7269 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7271 Conc_Typ_Stmts :=
7272 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7274 -- Generate:
7275 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7277 Append_To (Conc_Typ_Stmts,
7278 Make_Procedure_Call_Statement (Loc,
7279 Name =>
7280 New_Occurrence_Of
7281 (Find_Prim_Op (Etype (Etype (Obj)),
7282 Name_uDisp_Get_Prim_Op_Kind),
7283 Loc),
7284 Parameter_Associations =>
7285 New_List (
7286 New_Copy_Tree (Obj),
7287 New_Occurrence_Of (S, Loc),
7288 New_Occurrence_Of (C, Loc))));
7290 -- Generate:
7291 -- if C = POK_Procedure_Entry then
7292 -- ProtE_Stmts
7293 -- elsif C = POK_Task_Entry then
7294 -- TaskE_Stmts
7295 -- else
7296 -- ProtP_Stmts
7297 -- end if;
7299 Append_To (Conc_Typ_Stmts,
7300 Make_Implicit_If_Statement (N,
7301 Condition =>
7302 Make_Op_Eq (Loc,
7303 Left_Opnd =>
7304 New_Occurrence_Of (C, Loc),
7305 Right_Opnd =>
7306 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7308 Then_Statements =>
7309 ProtE_Stmts,
7311 Elsif_Parts =>
7312 New_List (
7313 Make_Elsif_Part (Loc,
7314 Condition =>
7315 Make_Op_Eq (Loc,
7316 Left_Opnd =>
7317 New_Occurrence_Of (C, Loc),
7318 Right_Opnd =>
7319 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7321 Then_Statements =>
7322 TaskE_Stmts)),
7324 Else_Statements =>
7325 ProtP_Stmts));
7327 -- Generate:
7328 -- <dispatching-call>;
7329 -- <triggering-statements>
7331 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7332 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7334 -- Generate:
7335 -- if K = Ada.Tags.TK_Limited_Tagged
7336 -- or else K = Ada.Tags.TK_Tagged
7337 -- then
7338 -- Lim_Typ_Stmts
7339 -- else
7340 -- Conc_Typ_Stmts
7341 -- end if;
7343 Append_To (Stmts,
7344 Make_Implicit_If_Statement (N,
7345 Condition => Build_Dispatching_Tag_Check (K, N),
7346 Then_Statements => Lim_Typ_Stmts,
7347 Else_Statements => Conc_Typ_Stmts));
7349 Rewrite (N,
7350 Make_Block_Statement (Loc,
7351 Declarations =>
7352 Decls,
7353 Handled_Statement_Sequence =>
7354 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7356 Analyze (N);
7357 return;
7359 -- Delay triggering statement processing
7361 else
7362 -- Add a Delay_Block object to the parameter list of the delay
7363 -- procedure to form the parameter list of the Wait entry call.
7365 Dblock_Ent := Make_Temporary (Loc, 'D');
7367 Pdef := Entity (Name (Ecall));
7369 if Is_RTE (Pdef, RO_CA_Delay_For) then
7370 Enqueue_Call :=
7371 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7373 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7374 Enqueue_Call :=
7375 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7377 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7378 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7379 end if;
7381 Append_To (Parameter_Associations (Ecall),
7382 Make_Attribute_Reference (Loc,
7383 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7384 Attribute_Name => Name_Unchecked_Access));
7386 -- Create the inner block to protect the abortable part
7388 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7390 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7392 Abortable_Block :=
7393 Make_Block_Statement (Loc,
7394 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7395 Handled_Statement_Sequence =>
7396 Make_Handled_Sequence_Of_Statements (Loc,
7397 Statements => Astats),
7398 Has_Created_Identifier => True,
7399 Is_Asynchronous_Call_Block => True);
7401 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7403 Rewrite (Ecall,
7404 Make_Implicit_If_Statement (N,
7405 Condition =>
7406 Make_Function_Call (Loc,
7407 Name => Enqueue_Call,
7408 Parameter_Associations => Parameter_Associations (Ecall)),
7409 Then_Statements =>
7410 New_List (Make_Block_Statement (Loc,
7411 Handled_Statement_Sequence =>
7412 Make_Handled_Sequence_Of_Statements (Loc,
7413 Statements => New_List (
7414 Make_Implicit_Label_Declaration (Loc,
7415 Defining_Identifier => Blk_Ent,
7416 Label_Construct => Abortable_Block),
7417 Abortable_Block),
7418 Exception_Handlers => Hdle)))));
7420 Stmts := New_List (Ecall);
7422 -- Construct statement sequence for new block
7424 Append_To (Stmts,
7425 Make_Implicit_If_Statement (N,
7426 Condition =>
7427 Make_Function_Call (Loc,
7428 Name => New_Occurrence_Of (
7429 RTE (RE_Timed_Out), Loc),
7430 Parameter_Associations => New_List (
7431 Make_Attribute_Reference (Loc,
7432 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7433 Attribute_Name => Name_Unchecked_Access))),
7434 Then_Statements => Tstats));
7436 -- The result is the new block
7438 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7440 Rewrite (N,
7441 Make_Block_Statement (Loc,
7442 Declarations => New_List (
7443 Make_Object_Declaration (Loc,
7444 Defining_Identifier => Dblock_Ent,
7445 Aliased_Present => True,
7446 Object_Definition =>
7447 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7449 Handled_Statement_Sequence =>
7450 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7452 Analyze (N);
7453 return;
7454 end if;
7456 else
7457 N_Orig := N;
7458 end if;
7460 Extract_Entry (Ecall, Concval, Ename, Index);
7461 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7463 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7464 Decls := Declarations (Ecall);
7466 if Is_Protected_Type (Etype (Concval)) then
7468 -- Get the declarations of the block expanded from the entry call
7470 Decl := First (Decls);
7471 while Present (Decl)
7472 and then (Nkind (Decl) /= N_Object_Declaration
7473 or else not Is_RTE (Etype (Object_Definition (Decl)),
7474 RE_Communication_Block))
7475 loop
7476 Next (Decl);
7477 end loop;
7479 pragma Assert (Present (Decl));
7480 Cancel_Param := Defining_Identifier (Decl);
7482 -- Change the mode of the Protected_Entry_Call call
7484 -- Protected_Entry_Call (
7485 -- Object => po._object'Access,
7486 -- E => <entry index>;
7487 -- Uninterpreted_Data => P'Address;
7488 -- Mode => Asynchronous_Call;
7489 -- Block => Bnn);
7491 -- Skip assignments to temporaries created for in-out parameters
7493 -- This makes unwarranted assumptions about the shape of the expanded
7494 -- tree for the call, and should be cleaned up ???
7496 Stmt := First (Stmts);
7497 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7498 Next (Stmt);
7499 end loop;
7501 Call := Stmt;
7503 Param := First (Parameter_Associations (Call));
7504 while Present (Param)
7505 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7506 loop
7507 Next (Param);
7508 end loop;
7510 pragma Assert (Present (Param));
7511 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7512 Analyze (Param);
7514 -- Append an if statement to execute the abortable part
7516 -- Generate:
7517 -- if Enqueued (Bnn) then
7519 Append_To (Stmts,
7520 Make_Implicit_If_Statement (N,
7521 Condition =>
7522 Make_Function_Call (Loc,
7523 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7524 Parameter_Associations => New_List (
7525 New_Occurrence_Of (Cancel_Param, Loc))),
7526 Then_Statements => Astats));
7528 Abortable_Block :=
7529 Make_Block_Statement (Loc,
7530 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7531 Handled_Statement_Sequence =>
7532 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7533 Has_Created_Identifier => True,
7534 Is_Asynchronous_Call_Block => True);
7536 -- Aborts are not deferred at beginning of exception handlers in
7537 -- ZCX mode.
7539 if ZCX_Exceptions then
7540 Handler_Stmt := Make_Null_Statement (Loc);
7542 else
7543 Handler_Stmt := Build_Runtime_Call (Loc, RE_Abort_Undefer);
7544 end if;
7546 Stmts := New_List (
7547 Make_Block_Statement (Loc,
7548 Handled_Statement_Sequence =>
7549 Make_Handled_Sequence_Of_Statements (Loc,
7550 Statements => New_List (
7551 Make_Implicit_Label_Declaration (Loc,
7552 Defining_Identifier => Blk_Ent,
7553 Label_Construct => Abortable_Block),
7554 Abortable_Block),
7556 -- exception
7558 Exception_Handlers => New_List (
7559 Make_Implicit_Exception_Handler (Loc,
7561 -- when Abort_Signal =>
7562 -- Abort_Undefer.all;
7564 Exception_Choices =>
7565 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7566 Statements => New_List (Handler_Stmt))))),
7568 -- if not Cancelled (Bnn) then
7569 -- triggered statements
7570 -- end if;
7572 Make_Implicit_If_Statement (N,
7573 Condition => Make_Op_Not (Loc,
7574 Right_Opnd =>
7575 Make_Function_Call (Loc,
7576 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7577 Parameter_Associations => New_List (
7578 New_Occurrence_Of (Cancel_Param, Loc)))),
7579 Then_Statements => Tstats));
7581 -- Asynchronous task entry call
7583 else
7584 if No (Decls) then
7585 Decls := New_List;
7586 end if;
7588 B := Make_Defining_Identifier (Loc, Name_uB);
7590 -- Insert declaration of B in declarations of existing block
7592 Prepend_To (Decls,
7593 Make_Object_Declaration (Loc,
7594 Defining_Identifier => B,
7595 Object_Definition =>
7596 New_Occurrence_Of (Standard_Boolean, Loc)));
7598 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7600 -- Insert the declaration of C in the declarations of the existing
7601 -- block. The variable is initialized to something (True or False,
7602 -- does not matter) to prevent CodePeer from complaining about a
7603 -- possible read of an uninitialized variable.
7605 Prepend_To (Decls,
7606 Make_Object_Declaration (Loc,
7607 Defining_Identifier => Cancel_Param,
7608 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7609 Expression => New_Occurrence_Of (Standard_False, Loc),
7610 Has_Init_Expression => True));
7612 -- Remove and save the call to Call_Simple
7614 Stmt := First (Stmts);
7616 -- Skip assignments to temporaries created for in-out parameters.
7617 -- This makes unwarranted assumptions about the shape of the expanded
7618 -- tree for the call, and should be cleaned up ???
7620 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7621 Next (Stmt);
7622 end loop;
7624 Call := Stmt;
7626 -- Create the inner block to protect the abortable part
7628 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7630 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7632 Abortable_Block :=
7633 Make_Block_Statement (Loc,
7634 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7635 Handled_Statement_Sequence =>
7636 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7637 Has_Created_Identifier => True,
7638 Is_Asynchronous_Call_Block => True);
7640 Insert_After (Call,
7641 Make_Block_Statement (Loc,
7642 Handled_Statement_Sequence =>
7643 Make_Handled_Sequence_Of_Statements (Loc,
7644 Statements => New_List (
7645 Make_Implicit_Label_Declaration (Loc,
7646 Defining_Identifier => Blk_Ent,
7647 Label_Construct => Abortable_Block),
7648 Abortable_Block),
7649 Exception_Handlers => Hdle)));
7651 -- Create new call statement
7653 Params := Parameter_Associations (Call);
7655 Append_To (Params,
7656 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7657 Append_To (Params, New_Occurrence_Of (B, Loc));
7659 Rewrite (Call,
7660 Make_Procedure_Call_Statement (Loc,
7661 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7662 Parameter_Associations => Params));
7664 -- Construct statement sequence for new block
7666 Append_To (Stmts,
7667 Make_Implicit_If_Statement (N,
7668 Condition =>
7669 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7670 Then_Statements => Tstats));
7672 -- Protected the call against abort
7674 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7675 end if;
7677 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7679 -- The result is the new block
7681 Rewrite (N_Orig,
7682 Make_Block_Statement (Loc,
7683 Declarations => Decls,
7684 Handled_Statement_Sequence =>
7685 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7687 Analyze (N_Orig);
7688 end Expand_N_Asynchronous_Select;
7690 -------------------------------------
7691 -- Expand_N_Conditional_Entry_Call --
7692 -------------------------------------
7694 -- The conditional task entry call is converted to a call to
7695 -- Task_Entry_Call:
7697 -- declare
7698 -- B : Boolean;
7699 -- P : parms := (parm, parm, parm);
7701 -- begin
7702 -- Task_Entry_Call
7703 -- (<acceptor-task>, -- Acceptor
7704 -- <entry-index>, -- E
7705 -- P'Address, -- Uninterpreted_Data
7706 -- Conditional_Call, -- Mode
7707 -- B); -- Rendezvous_Successful
7708 -- parm := P.param;
7709 -- parm := P.param;
7710 -- ...
7711 -- if B then
7712 -- normal-statements
7713 -- else
7714 -- else-statements
7715 -- end if;
7716 -- end;
7718 -- For a description of the use of P and the assignments after the call,
7719 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7720 -- conditional entry call has already been expanded (by the Expand_N_Entry
7721 -- _Call_Statement procedure) as follows:
7723 -- declare
7724 -- P : parms := (parm, parm, parm);
7725 -- begin
7726 -- ... info for in-out parameters
7727 -- Call_Simple (acceptor-task, entry-index, P'Address);
7728 -- parm := P.param;
7729 -- parm := P.param;
7730 -- ...
7731 -- end;
7733 -- so the task at hand is to convert the latter expansion into the former
7735 -- The conditional protected entry call is converted to a call to
7736 -- Protected_Entry_Call:
7738 -- declare
7739 -- P : parms := (parm, parm, parm);
7740 -- Bnn : Communications_Block;
7742 -- begin
7743 -- Protected_Entry_Call
7744 -- (po._object'Access, -- Object
7745 -- <entry index>, -- E
7746 -- P'Address, -- Uninterpreted_Data
7747 -- Conditional_Call, -- Mode
7748 -- Bnn); -- Block
7749 -- parm := P.param;
7750 -- parm := P.param;
7751 -- ...
7752 -- if Cancelled (Bnn) then
7753 -- else-statements
7754 -- else
7755 -- normal-statements
7756 -- end if;
7757 -- end;
7759 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7760 -- into:
7762 -- declare
7763 -- B : Boolean := False;
7764 -- C : Ada.Tags.Prim_Op_Kind;
7765 -- K : Ada.Tags.Tagged_Kind :=
7766 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7767 -- P : Parameters := (Param1 .. ParamN);
7768 -- S : Integer;
7770 -- begin
7771 -- if K = Ada.Tags.TK_Limited_Tagged
7772 -- or else K = Ada.Tags.TK_Tagged
7773 -- then
7774 -- <dispatching-call>;
7775 -- <triggering-statements>
7777 -- else
7778 -- S :=
7779 -- Ada.Tags.Get_Offset_Index
7780 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7782 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7784 -- if C = POK_Protected_Entry
7785 -- or else C = POK_Task_Entry
7786 -- then
7787 -- Param1 := P.Param1;
7788 -- ...
7789 -- ParamN := P.ParamN;
7790 -- end if;
7792 -- if B then
7793 -- if C = POK_Procedure
7794 -- or else C = POK_Protected_Procedure
7795 -- or else C = POK_Task_Procedure
7796 -- then
7797 -- <dispatching-call>;
7798 -- end if;
7800 -- <triggering-statements>
7801 -- else
7802 -- <else-statements>
7803 -- end if;
7804 -- end if;
7805 -- end;
7807 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7808 Loc : constant Source_Ptr := Sloc (N);
7809 Alt : constant Node_Id := Entry_Call_Alternative (N);
7810 Blk : Node_Id := Entry_Call_Statement (Alt);
7812 Actuals : List_Id;
7813 Blk_Typ : Entity_Id;
7814 Call : Node_Id;
7815 Call_Ent : Entity_Id;
7816 Conc_Typ_Stmts : List_Id;
7817 Decl : Node_Id;
7818 Decls : List_Id;
7819 Formals : List_Id;
7820 Lim_Typ_Stmts : List_Id;
7821 N_Stats : List_Id;
7822 Obj : Entity_Id;
7823 Param : Node_Id;
7824 Params : List_Id;
7825 Stmt : Node_Id;
7826 Stmts : List_Id;
7827 Transient_Blk : Node_Id;
7828 Unpack : List_Id;
7830 B : Entity_Id; -- Call status flag
7831 C : Entity_Id; -- Call kind
7832 K : Entity_Id; -- Tagged kind
7833 P : Entity_Id; -- Parameter block
7834 S : Entity_Id; -- Primitive operation slot
7836 begin
7837 Process_Statements_For_Controlled_Objects (N);
7839 if Ada_Version >= Ada_2005
7840 and then Nkind (Blk) = N_Procedure_Call_Statement
7841 then
7842 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7844 Decls := New_List;
7845 Stmts := New_List;
7847 -- Call status flag processing, generate:
7848 -- B : Boolean := False;
7850 B := Build_B (Loc, Decls);
7852 -- Call kind processing, generate:
7853 -- C : Ada.Tags.Prim_Op_Kind;
7855 C := Build_C (Loc, Decls);
7857 -- Tagged kind processing, generate:
7858 -- K : Ada.Tags.Tagged_Kind :=
7859 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7861 K := Build_K (Loc, Decls, Obj);
7863 -- Parameter block processing
7865 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7866 P := Parameter_Block_Pack
7867 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7869 -- Dispatch table slot processing, generate:
7870 -- S : Integer;
7872 S := Build_S (Loc, Decls);
7874 -- Generate:
7875 -- S := Ada.Tags.Get_Offset_Index
7876 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7878 Conc_Typ_Stmts :=
7879 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7881 -- Generate:
7882 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7884 Append_To (Conc_Typ_Stmts,
7885 Make_Procedure_Call_Statement (Loc,
7886 Name =>
7887 New_Occurrence_Of (
7888 Find_Prim_Op (Etype (Etype (Obj)),
7889 Name_uDisp_Conditional_Select),
7890 Loc),
7891 Parameter_Associations =>
7892 New_List (
7893 New_Copy_Tree (Obj), -- <object>
7894 New_Occurrence_Of (S, Loc), -- S
7895 Make_Attribute_Reference (Loc, -- P'Address
7896 Prefix => New_Occurrence_Of (P, Loc),
7897 Attribute_Name => Name_Address),
7898 New_Occurrence_Of (C, Loc), -- C
7899 New_Occurrence_Of (B, Loc)))); -- B
7901 -- Generate:
7902 -- if C = POK_Protected_Entry
7903 -- or else C = POK_Task_Entry
7904 -- then
7905 -- Param1 := P.Param1;
7906 -- ...
7907 -- ParamN := P.ParamN;
7908 -- end if;
7910 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7912 -- Generate the if statement only when the packed parameters need
7913 -- explicit assignments to their corresponding actuals.
7915 if Present (Unpack) then
7916 Append_To (Conc_Typ_Stmts,
7917 Make_Implicit_If_Statement (N,
7918 Condition =>
7919 Make_Or_Else (Loc,
7920 Left_Opnd =>
7921 Make_Op_Eq (Loc,
7922 Left_Opnd =>
7923 New_Occurrence_Of (C, Loc),
7924 Right_Opnd =>
7925 New_Occurrence_Of (RTE (
7926 RE_POK_Protected_Entry), Loc)),
7928 Right_Opnd =>
7929 Make_Op_Eq (Loc,
7930 Left_Opnd =>
7931 New_Occurrence_Of (C, Loc),
7932 Right_Opnd =>
7933 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
7935 Then_Statements => Unpack));
7936 end if;
7938 -- Generate:
7939 -- if B then
7940 -- if C = POK_Procedure
7941 -- or else C = POK_Protected_Procedure
7942 -- or else C = POK_Task_Procedure
7943 -- then
7944 -- <dispatching-call>
7945 -- end if;
7946 -- <normal-statements>
7947 -- else
7948 -- <else-statements>
7949 -- end if;
7951 N_Stats := New_Copy_List_Tree (Statements (Alt));
7953 Prepend_To (N_Stats,
7954 Make_Implicit_If_Statement (N,
7955 Condition =>
7956 Make_Or_Else (Loc,
7957 Left_Opnd =>
7958 Make_Op_Eq (Loc,
7959 Left_Opnd =>
7960 New_Occurrence_Of (C, Loc),
7961 Right_Opnd =>
7962 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
7964 Right_Opnd =>
7965 Make_Or_Else (Loc,
7966 Left_Opnd =>
7967 Make_Op_Eq (Loc,
7968 Left_Opnd =>
7969 New_Occurrence_Of (C, Loc),
7970 Right_Opnd =>
7971 New_Occurrence_Of (RTE (
7972 RE_POK_Protected_Procedure), Loc)),
7974 Right_Opnd =>
7975 Make_Op_Eq (Loc,
7976 Left_Opnd =>
7977 New_Occurrence_Of (C, Loc),
7978 Right_Opnd =>
7979 New_Occurrence_Of (RTE (
7980 RE_POK_Task_Procedure), Loc)))),
7982 Then_Statements =>
7983 New_List (Blk)));
7985 Append_To (Conc_Typ_Stmts,
7986 Make_Implicit_If_Statement (N,
7987 Condition => New_Occurrence_Of (B, Loc),
7988 Then_Statements => N_Stats,
7989 Else_Statements => Else_Statements (N)));
7991 -- Generate:
7992 -- <dispatching-call>;
7993 -- <triggering-statements>
7995 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
7996 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7998 -- Generate:
7999 -- if K = Ada.Tags.TK_Limited_Tagged
8000 -- or else K = Ada.Tags.TK_Tagged
8001 -- then
8002 -- Lim_Typ_Stmts
8003 -- else
8004 -- Conc_Typ_Stmts
8005 -- end if;
8007 Append_To (Stmts,
8008 Make_Implicit_If_Statement (N,
8009 Condition => Build_Dispatching_Tag_Check (K, N),
8010 Then_Statements => Lim_Typ_Stmts,
8011 Else_Statements => Conc_Typ_Stmts));
8013 Rewrite (N,
8014 Make_Block_Statement (Loc,
8015 Declarations =>
8016 Decls,
8017 Handled_Statement_Sequence =>
8018 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8020 -- As described above, the entry alternative is transformed into a
8021 -- block that contains the gnulli call, and possibly assignment
8022 -- statements for in-out parameters. The gnulli call may itself be
8023 -- rewritten into a transient block if some unconstrained parameters
8024 -- require it. We need to retrieve the call to complete its parameter
8025 -- list.
8027 else
8028 Transient_Blk :=
8029 First_Real_Statement (Handled_Statement_Sequence (Blk));
8031 if Present (Transient_Blk)
8032 and then Nkind (Transient_Blk) = N_Block_Statement
8033 then
8034 Blk := Transient_Blk;
8035 end if;
8037 Stmts := Statements (Handled_Statement_Sequence (Blk));
8038 Stmt := First (Stmts);
8039 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
8040 Next (Stmt);
8041 end loop;
8043 Call := Stmt;
8044 Params := Parameter_Associations (Call);
8046 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
8048 -- Substitute Conditional_Entry_Call for Simple_Call parameter
8050 Param := First (Params);
8051 while Present (Param)
8052 and then not Is_RTE (Etype (Param), RE_Call_Modes)
8053 loop
8054 Next (Param);
8055 end loop;
8057 pragma Assert (Present (Param));
8058 Rewrite (Param,
8059 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8061 Analyze (Param);
8063 -- Find the Communication_Block parameter for the call to the
8064 -- Cancelled function.
8066 Decl := First (Declarations (Blk));
8067 while Present (Decl)
8068 and then not Is_RTE (Etype (Object_Definition (Decl)),
8069 RE_Communication_Block)
8070 loop
8071 Next (Decl);
8072 end loop;
8074 -- Add an if statement to execute the else part if the call
8075 -- does not succeed (as indicated by the Cancelled predicate).
8077 Append_To (Stmts,
8078 Make_Implicit_If_Statement (N,
8079 Condition => Make_Function_Call (Loc,
8080 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
8081 Parameter_Associations => New_List (
8082 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8083 Then_Statements => Else_Statements (N),
8084 Else_Statements => Statements (Alt)));
8086 else
8087 B := Make_Defining_Identifier (Loc, Name_uB);
8089 -- Insert declaration of B in declarations of existing block
8091 if No (Declarations (Blk)) then
8092 Set_Declarations (Blk, New_List);
8093 end if;
8095 Prepend_To (Declarations (Blk),
8096 Make_Object_Declaration (Loc,
8097 Defining_Identifier => B,
8098 Object_Definition =>
8099 New_Occurrence_Of (Standard_Boolean, Loc)));
8101 -- Create new call statement
8103 Append_To (Params,
8104 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8105 Append_To (Params, New_Occurrence_Of (B, Loc));
8107 Rewrite (Call,
8108 Make_Procedure_Call_Statement (Loc,
8109 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8110 Parameter_Associations => Params));
8112 -- Construct statement sequence for new block
8114 Append_To (Stmts,
8115 Make_Implicit_If_Statement (N,
8116 Condition => New_Occurrence_Of (B, Loc),
8117 Then_Statements => Statements (Alt),
8118 Else_Statements => Else_Statements (N)));
8119 end if;
8121 -- The result is the new block
8123 Rewrite (N,
8124 Make_Block_Statement (Loc,
8125 Declarations => Declarations (Blk),
8126 Handled_Statement_Sequence =>
8127 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8128 end if;
8130 Analyze (N);
8131 end Expand_N_Conditional_Entry_Call;
8133 ---------------------------------------
8134 -- Expand_N_Delay_Relative_Statement --
8135 ---------------------------------------
8137 -- Delay statement is implemented as a procedure call to Delay_For
8138 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8139 -- simple delays imposed by the use of Protected Objects.
8141 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8142 Loc : constant Source_Ptr := Sloc (N);
8143 Proc : Entity_Id;
8145 begin
8146 -- Try to use System.Relative_Delays.Delay_For only if available. This
8147 -- is the implementation used on restricted platforms when Ada.Calendar
8148 -- is not available.
8150 if RTE_Available (RO_RD_Delay_For) then
8151 Proc := RTE (RO_RD_Delay_For);
8153 -- Otherwise, use Ada.Calendar.Delays.Delay_For and emit an error
8154 -- message if not available.
8156 else
8157 Proc := RTE (RO_CA_Delay_For);
8158 end if;
8160 Rewrite (N,
8161 Make_Procedure_Call_Statement (Loc,
8162 Name => New_Occurrence_Of (Proc, Loc),
8163 Parameter_Associations => New_List (Expression (N))));
8164 Analyze (N);
8165 end Expand_N_Delay_Relative_Statement;
8167 ------------------------------------
8168 -- Expand_N_Delay_Until_Statement --
8169 ------------------------------------
8171 -- Delay Until statement is implemented as a procedure call to
8172 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8174 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8175 Loc : constant Source_Ptr := Sloc (N);
8176 Typ : Entity_Id;
8178 begin
8179 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8180 Typ := RTE (RO_CA_Delay_Until);
8181 else
8182 Typ := RTE (RO_RT_Delay_Until);
8183 end if;
8185 Rewrite (N,
8186 Make_Procedure_Call_Statement (Loc,
8187 Name => New_Occurrence_Of (Typ, Loc),
8188 Parameter_Associations => New_List (Expression (N))));
8190 Analyze (N);
8191 end Expand_N_Delay_Until_Statement;
8193 -------------------------
8194 -- Expand_N_Entry_Body --
8195 -------------------------
8197 procedure Expand_N_Entry_Body (N : Node_Id) is
8198 begin
8199 -- Associate discriminals with the next protected operation body to be
8200 -- expanded.
8202 if Present (Next_Protected_Operation (N)) then
8203 Set_Discriminals (Parent (Current_Scope));
8204 end if;
8205 end Expand_N_Entry_Body;
8207 -----------------------------------
8208 -- Expand_N_Entry_Call_Statement --
8209 -----------------------------------
8211 -- An entry call is expanded into GNARLI calls to implement a simple entry
8212 -- call (see Build_Simple_Entry_Call).
8214 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8215 Concval : Node_Id;
8216 Ename : Node_Id;
8217 Index : Node_Id;
8219 begin
8220 if No_Run_Time_Mode then
8221 Error_Msg_CRT ("entry call", N);
8222 return;
8223 end if;
8225 -- If this entry call is part of an asynchronous select, don't expand it
8226 -- here; it will be expanded with the select statement. Don't expand
8227 -- timed entry calls either, as they are translated into asynchronous
8228 -- entry calls.
8230 -- ??? This whole approach is questionable; it may be better to go back
8231 -- to allowing the expansion to take place and then attempting to fix it
8232 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8233 -- whether the expanded call is on a task or protected entry.
8235 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8236 or else N /= Triggering_Statement (Parent (N)))
8237 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8238 or else N /= Entry_Call_Statement (Parent (N))
8239 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8240 then
8241 Extract_Entry (N, Concval, Ename, Index);
8242 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8243 end if;
8244 end Expand_N_Entry_Call_Statement;
8246 --------------------------------
8247 -- Expand_N_Entry_Declaration --
8248 --------------------------------
8250 -- If there are parameters, then first, each of the formals is marked by
8251 -- setting Is_Entry_Formal. Next a record type is built which is used to
8252 -- hold the parameter values. The name of this record type is entryP where
8253 -- entry is the name of the entry, with an additional corresponding access
8254 -- type called entryPA. The record type has matching components for each
8255 -- formal (the component names are the same as the formal names). For
8256 -- elementary types, the component type matches the formal type. For
8257 -- composite types, an access type is declared (with the name formalA)
8258 -- which designates the formal type, and the type of the component is this
8259 -- access type. Finally the Entry_Component of each formal is set to
8260 -- reference the corresponding record component.
8262 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8263 Loc : constant Source_Ptr := Sloc (N);
8264 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8265 Components : List_Id;
8266 Formal : Node_Id;
8267 Ftype : Entity_Id;
8268 Last_Decl : Node_Id;
8269 Component : Entity_Id;
8270 Ctype : Entity_Id;
8271 Decl : Node_Id;
8272 Rec_Ent : Entity_Id;
8273 Acc_Ent : Entity_Id;
8275 begin
8276 Formal := First_Formal (Entry_Ent);
8277 Last_Decl := N;
8279 -- Most processing is done only if parameters are present
8281 if Present (Formal) then
8282 Components := New_List;
8284 -- Loop through formals
8286 while Present (Formal) loop
8287 Set_Is_Entry_Formal (Formal);
8288 Component :=
8289 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8290 Set_Entry_Component (Formal, Component);
8291 Set_Entry_Formal (Component, Formal);
8292 Ftype := Etype (Formal);
8294 -- Declare new access type and then append
8296 Ctype := Make_Temporary (Loc, 'A');
8297 Set_Is_Param_Block_Component_Type (Ctype);
8299 Decl :=
8300 Make_Full_Type_Declaration (Loc,
8301 Defining_Identifier => Ctype,
8302 Type_Definition =>
8303 Make_Access_To_Object_Definition (Loc,
8304 All_Present => True,
8305 Constant_Present => Ekind (Formal) = E_In_Parameter,
8306 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8308 Insert_After (Last_Decl, Decl);
8309 Last_Decl := Decl;
8311 Append_To (Components,
8312 Make_Component_Declaration (Loc,
8313 Defining_Identifier => Component,
8314 Component_Definition =>
8315 Make_Component_Definition (Loc,
8316 Aliased_Present => False,
8317 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8319 Next_Formal_With_Extras (Formal);
8320 end loop;
8322 -- Create the Entry_Parameter_Record declaration
8324 Rec_Ent := Make_Temporary (Loc, 'P');
8326 Decl :=
8327 Make_Full_Type_Declaration (Loc,
8328 Defining_Identifier => Rec_Ent,
8329 Type_Definition =>
8330 Make_Record_Definition (Loc,
8331 Component_List =>
8332 Make_Component_List (Loc,
8333 Component_Items => Components)));
8335 Insert_After (Last_Decl, Decl);
8336 Last_Decl := Decl;
8338 -- Construct and link in the corresponding access type
8340 Acc_Ent := Make_Temporary (Loc, 'A');
8342 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8344 Decl :=
8345 Make_Full_Type_Declaration (Loc,
8346 Defining_Identifier => Acc_Ent,
8347 Type_Definition =>
8348 Make_Access_To_Object_Definition (Loc,
8349 All_Present => True,
8350 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8352 Insert_After (Last_Decl, Decl);
8353 end if;
8354 end Expand_N_Entry_Declaration;
8356 -----------------------------
8357 -- Expand_N_Protected_Body --
8358 -----------------------------
8360 -- Protected bodies are expanded to the completion of the subprograms
8361 -- created for the corresponding protected type. These are a protected and
8362 -- unprotected version of each protected subprogram in the object, a
8363 -- function to calculate each entry barrier, and a procedure to execute the
8364 -- sequence of statements of each protected entry body. For example, for
8365 -- protected type ptype:
8367 -- function entB
8368 -- (O : System.Address;
8369 -- E : Protected_Entry_Index)
8370 -- return Boolean
8371 -- is
8372 -- <discriminant renamings>
8373 -- <private object renamings>
8374 -- begin
8375 -- return <barrier expression>;
8376 -- end entB;
8378 -- procedure pprocN (_object : in out poV;...) is
8379 -- <discriminant renamings>
8380 -- <private object renamings>
8381 -- begin
8382 -- <sequence of statements>
8383 -- end pprocN;
8385 -- procedure pprocP (_object : in out poV;...) is
8386 -- procedure _clean is
8387 -- Pn : Boolean;
8388 -- begin
8389 -- ptypeS (_object, Pn);
8390 -- Unlock (_object._object'Access);
8391 -- Abort_Undefer.all;
8392 -- end _clean;
8394 -- begin
8395 -- Abort_Defer.all;
8396 -- Lock (_object._object'Access);
8397 -- pprocN (_object;...);
8398 -- at end
8399 -- _clean;
8400 -- end pproc;
8402 -- function pfuncN (_object : poV;...) return Return_Type is
8403 -- <discriminant renamings>
8404 -- <private object renamings>
8405 -- begin
8406 -- <sequence of statements>
8407 -- end pfuncN;
8409 -- function pfuncP (_object : poV) return Return_Type is
8410 -- procedure _clean is
8411 -- begin
8412 -- Unlock (_object._object'Access);
8413 -- Abort_Undefer.all;
8414 -- end _clean;
8416 -- begin
8417 -- Abort_Defer.all;
8418 -- Lock (_object._object'Access);
8419 -- return pfuncN (_object);
8421 -- at end
8422 -- _clean;
8423 -- end pfunc;
8425 -- procedure entE
8426 -- (O : System.Address;
8427 -- P : System.Address;
8428 -- E : Protected_Entry_Index)
8429 -- is
8430 -- <discriminant renamings>
8431 -- <private object renamings>
8432 -- type poVP is access poV;
8433 -- _Object : ptVP := ptVP!(O);
8435 -- begin
8436 -- begin
8437 -- <statement sequence>
8438 -- Complete_Entry_Body (_Object._Object);
8439 -- exception
8440 -- when all others =>
8441 -- Exceptional_Complete_Entry_Body (
8442 -- _Object._Object, Get_GNAT_Exception);
8443 -- end;
8444 -- end entE;
8446 -- The type poV is the record created for the protected type to hold
8447 -- the state of the protected object.
8449 procedure Expand_N_Protected_Body (N : Node_Id) is
8450 Loc : constant Source_Ptr := Sloc (N);
8451 Pid : constant Entity_Id := Corresponding_Spec (N);
8453 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8454 -- This flag indicates whether the lock free implementation is active
8456 Current_Node : Node_Id;
8457 Disp_Op_Body : Node_Id;
8458 New_Op_Body : Node_Id;
8459 Op_Body : Node_Id;
8460 Op_Id : Entity_Id;
8462 function Build_Dispatching_Subprogram_Body
8463 (N : Node_Id;
8464 Pid : Node_Id;
8465 Prot_Bod : Node_Id) return Node_Id;
8466 -- Build a dispatching version of the protected subprogram body. The
8467 -- newly generated subprogram contains a call to the original protected
8468 -- body. The following code is generated:
8470 -- function <protected-function-name> (Param1 .. ParamN) return
8471 -- <return-type> is
8472 -- begin
8473 -- return <protected-function-name>P (Param1 .. ParamN);
8474 -- end <protected-function-name>;
8476 -- or
8478 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8479 -- begin
8480 -- <protected-procedure-name>P (Param1 .. ParamN);
8481 -- end <protected-procedure-name>
8483 ---------------------------------------
8484 -- Build_Dispatching_Subprogram_Body --
8485 ---------------------------------------
8487 function Build_Dispatching_Subprogram_Body
8488 (N : Node_Id;
8489 Pid : Node_Id;
8490 Prot_Bod : Node_Id) return Node_Id
8492 Loc : constant Source_Ptr := Sloc (N);
8493 Actuals : List_Id;
8494 Formal : Node_Id;
8495 Spec : Node_Id;
8496 Stmts : List_Id;
8498 begin
8499 -- Generate a specification without a letter suffix in order to
8500 -- override an interface function or procedure.
8502 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8504 -- The formal parameters become the actuals of the protected function
8505 -- or procedure call.
8507 Actuals := New_List;
8508 Formal := First (Parameter_Specifications (Spec));
8509 while Present (Formal) loop
8510 Append_To (Actuals,
8511 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8512 Next (Formal);
8513 end loop;
8515 if Nkind (Spec) = N_Procedure_Specification then
8516 Stmts :=
8517 New_List (
8518 Make_Procedure_Call_Statement (Loc,
8519 Name =>
8520 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8521 Parameter_Associations => Actuals));
8523 else
8524 pragma Assert (Nkind (Spec) = N_Function_Specification);
8526 Stmts :=
8527 New_List (
8528 Make_Simple_Return_Statement (Loc,
8529 Expression =>
8530 Make_Function_Call (Loc,
8531 Name =>
8532 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8533 Parameter_Associations => Actuals)));
8534 end if;
8536 return
8537 Make_Subprogram_Body (Loc,
8538 Declarations => Empty_List,
8539 Specification => Spec,
8540 Handled_Statement_Sequence =>
8541 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8542 end Build_Dispatching_Subprogram_Body;
8544 -- Start of processing for Expand_N_Protected_Body
8546 begin
8547 if No_Run_Time_Mode then
8548 Error_Msg_CRT ("protected body", N);
8549 return;
8550 end if;
8552 -- This is the proper body corresponding to a stub. The declarations
8553 -- must be inserted at the point of the stub, which in turn is in the
8554 -- declarative part of the parent unit.
8556 if Nkind (Parent (N)) = N_Subunit then
8557 Current_Node := Corresponding_Stub (Parent (N));
8558 else
8559 Current_Node := N;
8560 end if;
8562 Op_Body := First (Declarations (N));
8564 -- The protected body is replaced with the bodies of its protected
8565 -- operations, and the declarations for internal objects that may
8566 -- have been created for entry family bounds.
8568 Rewrite (N, Make_Null_Statement (Sloc (N)));
8569 Analyze (N);
8571 while Present (Op_Body) loop
8572 case Nkind (Op_Body) is
8573 when N_Subprogram_Declaration =>
8574 null;
8576 when N_Subprogram_Body =>
8578 -- Do not create bodies for eliminated operations
8580 if not Is_Eliminated (Defining_Entity (Op_Body))
8581 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8582 then
8583 if Lock_Free_Active then
8584 New_Op_Body :=
8585 Build_Lock_Free_Unprotected_Subprogram_Body
8586 (Op_Body, Pid);
8587 else
8588 New_Op_Body :=
8589 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8590 end if;
8592 Insert_After (Current_Node, New_Op_Body);
8593 Current_Node := New_Op_Body;
8594 Analyze (New_Op_Body);
8596 -- Build the corresponding protected operation. It may
8597 -- appear that this is needed only if this is a visible
8598 -- operation of the type, or if it is an interrupt handler,
8599 -- and this was the strategy used previously in GNAT.
8601 -- However, the operation may be exported through a 'Access
8602 -- to an external caller. This is the common idiom in code
8603 -- that uses the Ada 2005 Timing_Events package. As a result
8604 -- we need to produce the protected body for both visible
8605 -- and private operations, as well as operations that only
8606 -- have a body in the source, and for which we create a
8607 -- declaration in the protected body itself.
8609 if Present (Corresponding_Spec (Op_Body)) then
8610 if Lock_Free_Active then
8611 New_Op_Body :=
8612 Build_Lock_Free_Protected_Subprogram_Body
8613 (Op_Body, Pid, Specification (New_Op_Body));
8614 else
8615 New_Op_Body :=
8616 Build_Protected_Subprogram_Body
8617 (Op_Body, Pid, Specification (New_Op_Body));
8618 end if;
8620 Insert_After (Current_Node, New_Op_Body);
8621 Analyze (New_Op_Body);
8623 Current_Node := New_Op_Body;
8625 -- Generate an overriding primitive operation body for
8626 -- this subprogram if the protected type implements an
8627 -- interface.
8629 if Ada_Version >= Ada_2005
8630 and then
8631 Present (Interfaces (Corresponding_Record_Type (Pid)))
8632 then
8633 Disp_Op_Body :=
8634 Build_Dispatching_Subprogram_Body
8635 (Op_Body, Pid, New_Op_Body);
8637 Insert_After (Current_Node, Disp_Op_Body);
8638 Analyze (Disp_Op_Body);
8640 Current_Node := Disp_Op_Body;
8641 end if;
8642 end if;
8643 end if;
8645 when N_Entry_Body =>
8646 Op_Id := Defining_Identifier (Op_Body);
8647 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8649 Insert_After (Current_Node, New_Op_Body);
8650 Current_Node := New_Op_Body;
8651 Analyze (New_Op_Body);
8653 when N_Implicit_Label_Declaration =>
8654 null;
8656 when N_Call_Marker
8657 | N_Itype_Reference
8659 New_Op_Body := New_Copy (Op_Body);
8660 Insert_After (Current_Node, New_Op_Body);
8661 Current_Node := New_Op_Body;
8663 when N_Freeze_Entity =>
8664 New_Op_Body := New_Copy (Op_Body);
8666 if Present (Entity (Op_Body))
8667 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8668 then
8669 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8670 end if;
8672 Insert_After (Current_Node, New_Op_Body);
8673 Current_Node := New_Op_Body;
8674 Analyze (New_Op_Body);
8676 when N_Pragma =>
8677 New_Op_Body := New_Copy (Op_Body);
8678 Insert_After (Current_Node, New_Op_Body);
8679 Current_Node := New_Op_Body;
8680 Analyze (New_Op_Body);
8682 when N_Object_Declaration =>
8683 pragma Assert (not Comes_From_Source (Op_Body));
8684 New_Op_Body := New_Copy (Op_Body);
8685 Insert_After (Current_Node, New_Op_Body);
8686 Current_Node := New_Op_Body;
8687 Analyze (New_Op_Body);
8689 when others =>
8690 raise Program_Error;
8691 end case;
8693 Next (Op_Body);
8694 end loop;
8696 -- Finally, create the body of the function that maps an entry index
8697 -- into the corresponding body index, except when there is no entry, or
8698 -- in a Ravenscar-like profile.
8700 if Corresponding_Runtime_Package (Pid) =
8701 System_Tasking_Protected_Objects_Entries
8702 then
8703 New_Op_Body := Build_Find_Body_Index (Pid);
8704 Insert_After (Current_Node, New_Op_Body);
8705 Current_Node := New_Op_Body;
8706 Analyze (New_Op_Body);
8707 end if;
8709 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8710 -- protected body. At this point all wrapper specs have been created,
8711 -- frozen and included in the dispatch table for the protected type.
8713 if Ada_Version >= Ada_2005 then
8714 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8715 end if;
8716 end Expand_N_Protected_Body;
8718 -----------------------------------------
8719 -- Expand_N_Protected_Type_Declaration --
8720 -----------------------------------------
8722 -- First we create a corresponding record type declaration used to
8723 -- represent values of this protected type.
8724 -- The general form of this type declaration is
8726 -- type poV (discriminants) is record
8727 -- _Object : aliased <kind>Protection
8728 -- [(<entry count> [, <handler count>])];
8729 -- [entry_family : array (bounds) of Void;]
8730 -- <private data fields>
8731 -- end record;
8733 -- The discriminants are present only if the corresponding protected type
8734 -- has discriminants, and they exactly mirror the protected type
8735 -- discriminants. The private data fields similarly mirror the private
8736 -- declarations of the protected type.
8738 -- The Object field is always present. It contains RTS specific data used
8739 -- to control the protected object. It is declared as Aliased so that it
8740 -- can be passed as a pointer to the RTS. This allows the protected record
8741 -- to be referenced within RTS data structures. An appropriate Protection
8742 -- type and discriminant are generated.
8744 -- The Service field is present for protected objects with entries. It
8745 -- contains sufficient information to allow the entry service procedure for
8746 -- this object to be called when the object is not known till runtime.
8748 -- One entry_family component is present for each entry family in the
8749 -- task definition (see Expand_N_Task_Type_Declaration).
8751 -- When a protected object is declared, an instance of the protected type
8752 -- value record is created. The elaboration of this declaration creates the
8753 -- correct bounds for the entry families, and also evaluates the priority
8754 -- expression if needed. The initialization routine for the protected type
8755 -- itself then calls Initialize_Protection with appropriate parameters to
8756 -- initialize the value of the Task_Id field. Install_Handlers may be also
8757 -- called if a pragma Attach_Handler applies.
8759 -- Note: this record is passed to the subprograms created by the expansion
8760 -- of protected subprograms and entries. It is an in parameter to protected
8761 -- functions and an in out parameter to procedures and entry bodies. The
8762 -- Entity_Id for this created record type is placed in the
8763 -- Corresponding_Record_Type field of the associated protected type entity.
8765 -- Next we create a procedure specifications for protected subprograms and
8766 -- entry bodies. For each protected subprograms two subprograms are
8767 -- created, an unprotected and a protected version. The unprotected version
8768 -- is called from within other operations of the same protected object.
8770 -- We also build the call to register the procedure if a pragma
8771 -- Interrupt_Handler applies.
8773 -- A single subprogram is created to service all entry bodies; it has an
8774 -- additional boolean out parameter indicating that the previous entry call
8775 -- made by the current task was serviced immediately, i.e. not by proxy.
8776 -- The O parameter contains a pointer to a record object of the type
8777 -- described above. An untyped interface is used here to allow this
8778 -- procedure to be called in places where the type of the object to be
8779 -- serviced is not known. This must be done, for example, when a call that
8780 -- may have been requeued is cancelled; the corresponding object must be
8781 -- serviced, but which object that is not known till runtime.
8783 -- procedure ptypeS
8784 -- (O : System.Address; P : out Boolean);
8785 -- procedure pprocN (_object : in out poV);
8786 -- procedure pproc (_object : in out poV);
8787 -- function pfuncN (_object : poV);
8788 -- function pfunc (_object : poV);
8789 -- ...
8791 -- Note that this must come after the record type declaration, since
8792 -- the specs refer to this type.
8794 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8795 Discr_Map : constant Elist_Id := New_Elmt_List;
8796 Loc : constant Source_Ptr := Sloc (N);
8797 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8799 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8800 -- This flag indicates whether the lock free implementation is active
8802 Pdef : constant Node_Id := Protected_Definition (N);
8803 -- This contains two lists; one for visible and one for private decls
8805 Current_Node : Node_Id := N;
8806 E_Count : Int;
8807 Entries_Aggr : Node_Id;
8809 procedure Check_Inlining (Subp : Entity_Id);
8810 -- If the original operation has a pragma Inline, propagate the flag
8811 -- to the internal body, for possible inlining later on. The source
8812 -- operation is invisible to the back-end and is never actually called.
8814 procedure Expand_Entry_Declaration (Decl : Node_Id);
8815 -- Create the entry barrier and the procedure body for entry declaration
8816 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
8818 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8819 -- When compiling under the Ravenscar profile, private components must
8820 -- have a static size, or else a protected object will require heap
8821 -- allocation, violating the corresponding restriction. It is preferable
8822 -- to make this check here, because it provides a better error message
8823 -- than the back-end, which refers to the object as a whole.
8825 procedure Register_Handler;
8826 -- For a protected operation that is an interrupt handler, add the
8827 -- freeze action that will register it as such.
8829 --------------------
8830 -- Check_Inlining --
8831 --------------------
8833 procedure Check_Inlining (Subp : Entity_Id) is
8834 begin
8835 if Is_Inlined (Subp) then
8836 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8837 Set_Is_Inlined (Subp, False);
8838 end if;
8839 end Check_Inlining;
8841 ---------------------------
8842 -- Static_Component_Size --
8843 ---------------------------
8845 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8846 Typ : constant Entity_Id := Etype (Comp);
8847 C : Entity_Id;
8849 begin
8850 if Is_Scalar_Type (Typ) then
8851 return True;
8853 elsif Is_Array_Type (Typ) then
8854 return Compile_Time_Known_Bounds (Typ);
8856 elsif Is_Record_Type (Typ) then
8857 C := First_Component (Typ);
8858 while Present (C) loop
8859 if not Static_Component_Size (C) then
8860 return False;
8861 end if;
8863 Next_Component (C);
8864 end loop;
8866 return True;
8868 -- Any other type will be checked by the back-end
8870 else
8871 return True;
8872 end if;
8873 end Static_Component_Size;
8875 ------------------------------
8876 -- Expand_Entry_Declaration --
8877 ------------------------------
8879 procedure Expand_Entry_Declaration (Decl : Node_Id) is
8880 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
8881 Bar_Id : Entity_Id;
8882 Bod_Id : Entity_Id;
8883 Subp : Node_Id;
8885 begin
8886 E_Count := E_Count + 1;
8888 -- Create the protected body subprogram
8890 Bod_Id :=
8891 Make_Defining_Identifier (Loc,
8892 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
8893 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
8895 Subp :=
8896 Make_Subprogram_Declaration (Loc,
8897 Specification =>
8898 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
8900 Insert_After (Current_Node, Subp);
8901 Current_Node := Subp;
8903 Analyze (Subp);
8905 -- Build a wrapper procedure to handle contract cases, preconditions,
8906 -- and postconditions.
8908 Build_Contract_Wrapper (Ent_Id, N);
8910 -- Create the barrier function
8912 Bar_Id :=
8913 Make_Defining_Identifier (Loc,
8914 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
8915 Set_Barrier_Function (Ent_Id, Bar_Id);
8917 Subp :=
8918 Make_Subprogram_Declaration (Loc,
8919 Specification =>
8920 Build_Barrier_Function_Specification (Loc, Bar_Id));
8921 Set_Is_Entry_Barrier_Function (Subp);
8923 Insert_After (Current_Node, Subp);
8924 Current_Node := Subp;
8926 Analyze (Subp);
8928 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
8929 Set_Scope (Bar_Id, Scope (Ent_Id));
8931 -- Collect pointers to the protected subprogram and the barrier
8932 -- of the current entry, for insertion into Entry_Bodies_Array.
8934 Append_To (Expressions (Entries_Aggr),
8935 Make_Aggregate (Loc,
8936 Expressions => New_List (
8937 Make_Attribute_Reference (Loc,
8938 Prefix => New_Occurrence_Of (Bar_Id, Loc),
8939 Attribute_Name => Name_Unrestricted_Access),
8940 Make_Attribute_Reference (Loc,
8941 Prefix => New_Occurrence_Of (Bod_Id, Loc),
8942 Attribute_Name => Name_Unrestricted_Access))));
8943 end Expand_Entry_Declaration;
8945 ----------------------
8946 -- Register_Handler --
8947 ----------------------
8949 procedure Register_Handler is
8951 -- All semantic checks already done in Sem_Prag
8953 Prot_Proc : constant Entity_Id :=
8954 Defining_Unit_Name (Specification (Current_Node));
8956 Proc_Address : constant Node_Id :=
8957 Make_Attribute_Reference (Loc,
8958 Prefix =>
8959 New_Occurrence_Of (Prot_Proc, Loc),
8960 Attribute_Name => Name_Address);
8962 RTS_Call : constant Entity_Id :=
8963 Make_Procedure_Call_Statement (Loc,
8964 Name =>
8965 New_Occurrence_Of
8966 (RTE (RE_Register_Interrupt_Handler), Loc),
8967 Parameter_Associations => New_List (Proc_Address));
8968 begin
8969 Append_Freeze_Action (Prot_Proc, RTS_Call);
8970 end Register_Handler;
8972 -- Local variables
8974 Body_Arr : Node_Id;
8975 Body_Id : Entity_Id;
8976 Cdecls : List_Id;
8977 Comp : Node_Id;
8978 Expr : Node_Id;
8979 New_Priv : Node_Id;
8980 Obj_Def : Node_Id;
8981 Object_Comp : Node_Id;
8982 Priv : Node_Id;
8983 Rec_Decl : Node_Id;
8984 Sub : Node_Id;
8986 -- Start of processing for Expand_N_Protected_Type_Declaration
8988 begin
8989 if Present (Corresponding_Record_Type (Prot_Typ)) then
8990 return;
8991 else
8992 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8993 end if;
8995 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8997 Qualify_Entity_Names (N);
8999 -- If the type has discriminants, their occurrences in the declaration
9000 -- have been replaced by the corresponding discriminals. For components
9001 -- that are constrained by discriminants, their homologues in the
9002 -- corresponding record type must refer to the discriminants of that
9003 -- record, so we must apply a new renaming to subtypes_indications:
9005 -- protected discriminant => discriminal => record discriminant
9007 -- This replacement is not applied to default expressions, for which
9008 -- the discriminal is correct.
9010 if Has_Discriminants (Prot_Typ) then
9011 declare
9012 Disc : Entity_Id;
9013 Decl : Node_Id;
9015 begin
9016 Disc := First_Discriminant (Prot_Typ);
9017 Decl := First (Discriminant_Specifications (Rec_Decl));
9018 while Present (Disc) loop
9019 Append_Elmt (Discriminal (Disc), Discr_Map);
9020 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
9021 Next_Discriminant (Disc);
9022 Next (Decl);
9023 end loop;
9024 end;
9025 end if;
9027 -- Fill in the component declarations
9029 -- Add components for entry families. For each entry family, create an
9030 -- anonymous type declaration with the same size, and analyze the type.
9032 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9034 pragma Assert (Present (Pdef));
9036 Insert_After (Current_Node, Rec_Decl);
9037 Current_Node := Rec_Decl;
9039 -- Add private field components
9041 if Present (Private_Declarations (Pdef)) then
9042 Priv := First (Private_Declarations (Pdef));
9043 while Present (Priv) loop
9044 if Nkind (Priv) = N_Component_Declaration then
9045 if not Static_Component_Size (Defining_Identifier (Priv)) then
9047 -- When compiling for a restricted profile, the private
9048 -- components must have a static size. If not, this is an
9049 -- error for a single protected declaration, and rates a
9050 -- warning on a protected type declaration.
9052 if not Comes_From_Source (Prot_Typ) then
9054 -- It's ok to be checking this restriction at expansion
9055 -- time, because this is only for the restricted profile,
9056 -- which is not subject to strict RM conformance, so it
9057 -- is OK to miss this check in -gnatc mode.
9059 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9060 Check_Restriction
9061 (No_Implicit_Protected_Object_Allocations, Priv);
9063 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9064 if not Discriminated_Size (Defining_Identifier (Priv))
9065 then
9066 -- Any object of the type will be non-static
9068 Error_Msg_N ("component has non-static size??", Priv);
9069 Error_Msg_NE
9070 ("\creation of protected object of type& will "
9071 & "violate restriction "
9072 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9073 else
9074 -- Object will be non-static if discriminants are
9076 Error_Msg_NE
9077 ("creation of protected object of type& with "
9078 & "non-static discriminants will violate "
9079 & "restriction No_Implicit_Heap_Allocations??",
9080 Priv, Prot_Typ);
9081 end if;
9083 -- Likewise for No_Implicit_Protected_Object_Allocations
9085 elsif Restriction_Active
9086 (No_Implicit_Protected_Object_Allocations)
9087 then
9088 if not Discriminated_Size (Defining_Identifier (Priv))
9089 then
9090 -- Any object of the type will be non-static
9092 Error_Msg_N ("component has non-static size??", Priv);
9093 Error_Msg_NE
9094 ("\creation of protected object of type& will "
9095 & "violate restriction "
9096 & "No_Implicit_Protected_Object_Allocations??",
9097 Priv, Prot_Typ);
9098 else
9099 -- Object will be non-static if discriminants are
9101 Error_Msg_NE
9102 ("creation of protected object of type& with "
9103 & "non-static discriminants will violate "
9104 & "restriction "
9105 & "No_Implicit_Protected_Object_Allocations??",
9106 Priv, Prot_Typ);
9107 end if;
9108 end if;
9109 end if;
9111 -- The component definition consists of a subtype indication,
9112 -- or (in Ada 2005) an access definition. Make a copy of the
9113 -- proper definition.
9115 declare
9116 Old_Comp : constant Node_Id := Component_Definition (Priv);
9117 Oent : constant Entity_Id := Defining_Identifier (Priv);
9118 Nent : constant Entity_Id :=
9119 Make_Defining_Identifier (Sloc (Oent),
9120 Chars => Chars (Oent));
9121 New_Comp : Node_Id;
9123 begin
9124 if Present (Subtype_Indication (Old_Comp)) then
9125 New_Comp :=
9126 Make_Component_Definition (Sloc (Oent),
9127 Aliased_Present => False,
9128 Subtype_Indication =>
9129 New_Copy_Tree
9130 (Subtype_Indication (Old_Comp), Discr_Map));
9131 else
9132 New_Comp :=
9133 Make_Component_Definition (Sloc (Oent),
9134 Aliased_Present => False,
9135 Access_Definition =>
9136 New_Copy_Tree
9137 (Access_Definition (Old_Comp), Discr_Map));
9138 end if;
9140 New_Priv :=
9141 Make_Component_Declaration (Loc,
9142 Defining_Identifier => Nent,
9143 Component_Definition => New_Comp,
9144 Expression => Expression (Priv));
9146 Set_Has_Per_Object_Constraint (Nent,
9147 Has_Per_Object_Constraint (Oent));
9149 Append_To (Cdecls, New_Priv);
9150 end;
9152 elsif Nkind (Priv) = N_Subprogram_Declaration then
9154 -- Make the unprotected version of the subprogram available
9155 -- for expansion of intra object calls. There is need for
9156 -- a protected version only if the subprogram is an interrupt
9157 -- handler, otherwise this operation can only be called from
9158 -- within the body.
9160 Sub :=
9161 Make_Subprogram_Declaration (Loc,
9162 Specification =>
9163 Build_Protected_Sub_Specification
9164 (Priv, Prot_Typ, Unprotected_Mode));
9166 Insert_After (Current_Node, Sub);
9167 Analyze (Sub);
9169 Set_Protected_Body_Subprogram
9170 (Defining_Unit_Name (Specification (Priv)),
9171 Defining_Unit_Name (Specification (Sub)));
9172 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9173 Current_Node := Sub;
9175 Sub :=
9176 Make_Subprogram_Declaration (Loc,
9177 Specification =>
9178 Build_Protected_Sub_Specification
9179 (Priv, Prot_Typ, Protected_Mode));
9181 Insert_After (Current_Node, Sub);
9182 Analyze (Sub);
9183 Current_Node := Sub;
9185 if Is_Interrupt_Handler
9186 (Defining_Unit_Name (Specification (Priv)))
9187 then
9188 if not Restricted_Profile then
9189 Register_Handler;
9190 end if;
9191 end if;
9192 end if;
9194 Next (Priv);
9195 end loop;
9196 end if;
9198 -- Except for the lock-free implementation, append the _Object field
9199 -- with the right type to the component list. We need to compute the
9200 -- number of entries, and in some cases the number of Attach_Handler
9201 -- pragmas.
9203 if not Lock_Free_Active then
9204 declare
9205 Entry_Count_Expr : constant Node_Id :=
9206 Build_Entry_Count_Expression
9207 (Prot_Typ, Cdecls, Loc);
9208 Num_Attach_Handler : Nat := 0;
9209 Protection_Subtype : Node_Id;
9210 Ritem : Node_Id;
9212 begin
9213 if Has_Attach_Handler (Prot_Typ) then
9214 Ritem := First_Rep_Item (Prot_Typ);
9215 while Present (Ritem) loop
9216 if Nkind (Ritem) = N_Pragma
9217 and then Pragma_Name (Ritem) = Name_Attach_Handler
9218 then
9219 Num_Attach_Handler := Num_Attach_Handler + 1;
9220 end if;
9222 Next_Rep_Item (Ritem);
9223 end loop;
9224 end if;
9226 -- Determine the proper protection type. There are two special
9227 -- cases: 1) when the protected type has dynamic interrupt
9228 -- handlers, and 2) when it has static handlers and we use a
9229 -- restricted profile.
9231 if Has_Attach_Handler (Prot_Typ)
9232 and then not Restricted_Profile
9233 then
9234 Protection_Subtype :=
9235 Make_Subtype_Indication (Loc,
9236 Subtype_Mark =>
9237 New_Occurrence_Of
9238 (RTE (RE_Static_Interrupt_Protection), Loc),
9239 Constraint =>
9240 Make_Index_Or_Discriminant_Constraint (Loc,
9241 Constraints => New_List (
9242 Entry_Count_Expr,
9243 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9245 elsif Has_Interrupt_Handler (Prot_Typ)
9246 and then not Restriction_Active (No_Dynamic_Attachment)
9247 then
9248 Protection_Subtype :=
9249 Make_Subtype_Indication (Loc,
9250 Subtype_Mark =>
9251 New_Occurrence_Of
9252 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9253 Constraint =>
9254 Make_Index_Or_Discriminant_Constraint (Loc,
9255 Constraints => New_List (Entry_Count_Expr)));
9257 else
9258 case Corresponding_Runtime_Package (Prot_Typ) is
9259 when System_Tasking_Protected_Objects_Entries =>
9260 Protection_Subtype :=
9261 Make_Subtype_Indication (Loc,
9262 Subtype_Mark =>
9263 New_Occurrence_Of
9264 (RTE (RE_Protection_Entries), Loc),
9265 Constraint =>
9266 Make_Index_Or_Discriminant_Constraint (Loc,
9267 Constraints => New_List (Entry_Count_Expr)));
9269 when System_Tasking_Protected_Objects_Single_Entry =>
9270 Protection_Subtype :=
9271 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9273 when System_Tasking_Protected_Objects =>
9274 Protection_Subtype :=
9275 New_Occurrence_Of (RTE (RE_Protection), Loc);
9277 when others =>
9278 raise Program_Error;
9279 end case;
9280 end if;
9282 Object_Comp :=
9283 Make_Component_Declaration (Loc,
9284 Defining_Identifier =>
9285 Make_Defining_Identifier (Loc, Name_uObject),
9286 Component_Definition =>
9287 Make_Component_Definition (Loc,
9288 Aliased_Present => True,
9289 Subtype_Indication => Protection_Subtype));
9290 end;
9292 -- Put the _Object component after the private component so that it
9293 -- be finalized early as required by 9.4 (20)
9295 Append_To (Cdecls, Object_Comp);
9296 end if;
9298 -- Analyze the record declaration immediately after construction,
9299 -- because the initialization procedure is needed for single object
9300 -- declarations before the next entity is analyzed (the freeze call
9301 -- that generates this initialization procedure is found below).
9303 Analyze (Rec_Decl, Suppress => All_Checks);
9305 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9306 -- the corresponding record is frozen. If any wrappers are generated,
9307 -- Current_Node is updated accordingly.
9309 if Ada_Version >= Ada_2005 then
9310 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9311 end if;
9313 -- Collect pointers to entry bodies and their barriers, to be placed
9314 -- in the Entry_Bodies_Array for the type. For each entry/family we
9315 -- add an expression to the aggregate which is the initial value of
9316 -- this array. The array is declared after all protected subprograms.
9318 if Has_Entries (Prot_Typ) then
9319 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9320 else
9321 Entries_Aggr := Empty;
9322 end if;
9324 -- Build two new procedure specifications for each protected subprogram;
9325 -- one to call from outside the object and one to call from inside.
9326 -- Build a barrier function and an entry body action procedure
9327 -- specification for each protected entry. Initialize the entry body
9328 -- array. If subprogram is flagged as eliminated, do not generate any
9329 -- internal operations.
9331 E_Count := 0;
9332 Comp := First (Visible_Declarations (Pdef));
9333 while Present (Comp) loop
9334 if Nkind (Comp) = N_Subprogram_Declaration then
9335 Sub :=
9336 Make_Subprogram_Declaration (Loc,
9337 Specification =>
9338 Build_Protected_Sub_Specification
9339 (Comp, Prot_Typ, Unprotected_Mode));
9341 Insert_After (Current_Node, Sub);
9342 Analyze (Sub);
9344 Set_Protected_Body_Subprogram
9345 (Defining_Unit_Name (Specification (Comp)),
9346 Defining_Unit_Name (Specification (Sub)));
9347 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9349 -- Make the protected version of the subprogram available for
9350 -- expansion of external calls.
9352 Current_Node := Sub;
9354 Sub :=
9355 Make_Subprogram_Declaration (Loc,
9356 Specification =>
9357 Build_Protected_Sub_Specification
9358 (Comp, Prot_Typ, Protected_Mode));
9360 Insert_After (Current_Node, Sub);
9361 Analyze (Sub);
9363 Current_Node := Sub;
9365 -- Generate an overriding primitive operation specification for
9366 -- this subprogram if the protected type implements an interface
9367 -- and Build_Wrapper_Spec did not generate its wrapper.
9369 if Ada_Version >= Ada_2005
9370 and then
9371 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9372 then
9373 declare
9374 Found : Boolean := False;
9375 Prim_Elmt : Elmt_Id;
9376 Prim_Op : Node_Id;
9378 begin
9379 Prim_Elmt :=
9380 First_Elmt
9381 (Primitive_Operations
9382 (Corresponding_Record_Type (Prot_Typ)));
9384 while Present (Prim_Elmt) loop
9385 Prim_Op := Node (Prim_Elmt);
9387 if Is_Primitive_Wrapper (Prim_Op)
9388 and then Wrapped_Entity (Prim_Op) =
9389 Defining_Entity (Specification (Comp))
9390 then
9391 Found := True;
9392 exit;
9393 end if;
9395 Next_Elmt (Prim_Elmt);
9396 end loop;
9398 if not Found then
9399 Sub :=
9400 Make_Subprogram_Declaration (Loc,
9401 Specification =>
9402 Build_Protected_Sub_Specification
9403 (Comp, Prot_Typ, Dispatching_Mode));
9405 Insert_After (Current_Node, Sub);
9406 Analyze (Sub);
9408 Current_Node := Sub;
9409 end if;
9410 end;
9411 end if;
9413 -- If a pragma Interrupt_Handler applies, build and add a call to
9414 -- Register_Interrupt_Handler to the freezing actions of the
9415 -- protected version (Current_Node) of the subprogram:
9417 -- system.interrupts.register_interrupt_handler
9418 -- (prot_procP'address);
9420 if not Restricted_Profile
9421 and then Is_Interrupt_Handler
9422 (Defining_Unit_Name (Specification (Comp)))
9423 then
9424 Register_Handler;
9425 end if;
9427 elsif Nkind (Comp) = N_Entry_Declaration then
9428 Expand_Entry_Declaration (Comp);
9429 end if;
9431 Next (Comp);
9432 end loop;
9434 -- If there are some private entry declarations, expand it as if they
9435 -- were visible entries.
9437 if Present (Private_Declarations (Pdef)) then
9438 Comp := First (Private_Declarations (Pdef));
9439 while Present (Comp) loop
9440 if Nkind (Comp) = N_Entry_Declaration then
9441 Expand_Entry_Declaration (Comp);
9442 end if;
9444 Next (Comp);
9445 end loop;
9446 end if;
9448 -- Create the declaration of an array object which contains the values
9449 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9450 -- type. This object is later passed to the appropriate protected object
9451 -- initialization routine.
9453 if Has_Entries (Prot_Typ)
9454 and then Corresponding_Runtime_Package (Prot_Typ) =
9455 System_Tasking_Protected_Objects_Entries
9456 then
9457 declare
9458 Count : Int;
9459 Item : Entity_Id;
9460 Max_Vals : Node_Id;
9461 Maxes : List_Id;
9462 Maxes_Id : Entity_Id;
9463 Need_Array : Boolean := False;
9465 begin
9466 -- First check if there is any Max_Queue_Length pragma
9468 Item := First_Entity (Prot_Typ);
9469 while Present (Item) loop
9470 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9471 Need_Array := True;
9472 exit;
9473 end if;
9475 Next_Entity (Item);
9476 end loop;
9478 -- Gather the Max_Queue_Length values of all entries in a list. A
9479 -- value of zero indicates that the entry has no limitation on its
9480 -- queue length.
9482 if Need_Array then
9483 Count := 0;
9484 Item := First_Entity (Prot_Typ);
9485 Maxes := New_List;
9486 while Present (Item) loop
9487 if Is_Entry (Item) then
9488 Count := Count + 1;
9489 Append_To (Maxes,
9490 Make_Integer_Literal
9491 (Loc, Get_Max_Queue_Length (Item)));
9492 end if;
9494 Next_Entity (Item);
9495 end loop;
9497 -- Create the declaration of the array object. Generate:
9499 -- Maxes_Id : aliased constant
9500 -- Protected_Entry_Queue_Max_Array
9501 -- (1 .. Count) := (..., ...);
9503 Maxes_Id :=
9504 Make_Defining_Identifier (Loc,
9505 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9507 Max_Vals :=
9508 Make_Object_Declaration (Loc,
9509 Defining_Identifier => Maxes_Id,
9510 Aliased_Present => True,
9511 Constant_Present => True,
9512 Object_Definition =>
9513 Make_Subtype_Indication (Loc,
9514 Subtype_Mark =>
9515 New_Occurrence_Of
9516 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9517 Constraint =>
9518 Make_Index_Or_Discriminant_Constraint (Loc,
9519 Constraints => New_List (
9520 Make_Range (Loc,
9521 Make_Integer_Literal (Loc, 1),
9522 Make_Integer_Literal (Loc, Count))))),
9523 Expression => Make_Aggregate (Loc, Maxes));
9525 -- A pointer to this array will be placed in the corresponding
9526 -- record by its initialization procedure so this needs to be
9527 -- analyzed here.
9529 Insert_After (Current_Node, Max_Vals);
9530 Current_Node := Max_Vals;
9531 Analyze (Max_Vals);
9533 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9534 end if;
9535 end;
9536 end if;
9538 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9539 -- all protected subprograms have been collected.
9541 if Has_Entries (Prot_Typ) then
9542 Body_Id :=
9543 Make_Defining_Identifier (Sloc (Prot_Typ),
9544 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9546 case Corresponding_Runtime_Package (Prot_Typ) is
9547 when System_Tasking_Protected_Objects_Entries =>
9548 Expr := Entries_Aggr;
9549 Obj_Def :=
9550 Make_Subtype_Indication (Loc,
9551 Subtype_Mark =>
9552 New_Occurrence_Of
9553 (RTE (RE_Protected_Entry_Body_Array), Loc),
9554 Constraint =>
9555 Make_Index_Or_Discriminant_Constraint (Loc,
9556 Constraints => New_List (
9557 Make_Range (Loc,
9558 Make_Integer_Literal (Loc, 1),
9559 Make_Integer_Literal (Loc, E_Count)))));
9561 when System_Tasking_Protected_Objects_Single_Entry =>
9562 Expr := Remove_Head (Expressions (Entries_Aggr));
9563 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9565 when others =>
9566 raise Program_Error;
9567 end case;
9569 Body_Arr :=
9570 Make_Object_Declaration (Loc,
9571 Defining_Identifier => Body_Id,
9572 Aliased_Present => True,
9573 Constant_Present => True,
9574 Object_Definition => Obj_Def,
9575 Expression => Expr);
9577 -- A pointer to this array will be placed in the corresponding record
9578 -- by its initialization procedure so this needs to be analyzed here.
9580 Insert_After (Current_Node, Body_Arr);
9581 Current_Node := Body_Arr;
9582 Analyze (Body_Arr);
9584 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9586 -- Finally, build the function that maps an entry index into the
9587 -- corresponding body. A pointer to this function is placed in each
9588 -- object of the type. Except for a ravenscar-like profile (no abort,
9589 -- no entry queue, 1 entry)
9591 if Corresponding_Runtime_Package (Prot_Typ) =
9592 System_Tasking_Protected_Objects_Entries
9593 then
9594 Sub :=
9595 Make_Subprogram_Declaration (Loc,
9596 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9598 Insert_After (Current_Node, Sub);
9599 Analyze (Sub);
9600 end if;
9601 end if;
9602 end Expand_N_Protected_Type_Declaration;
9604 --------------------------------
9605 -- Expand_N_Requeue_Statement --
9606 --------------------------------
9608 -- A nondispatching requeue statement is expanded into one of four GNARLI
9609 -- operations, depending on the source and destination (task or protected
9610 -- object). A dispatching requeue statement is expanded into a call to the
9611 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9612 -- jump around the remainder of processing for the original entry and, if
9613 -- the destination is (different) protected object, to attempt to service
9614 -- it. The following illustrates the various cases:
9616 -- procedure entE
9617 -- (O : System.Address;
9618 -- P : System.Address;
9619 -- E : Protected_Entry_Index)
9620 -- is
9621 -- <discriminant renamings>
9622 -- <private object renamings>
9623 -- type poVP is access poV;
9624 -- _object : ptVP := ptVP!(O);
9626 -- begin
9627 -- begin
9628 -- <start of statement sequence for entry>
9630 -- -- Requeue from one protected entry body to another protected
9631 -- -- entry.
9633 -- Requeue_Protected_Entry (
9634 -- _object._object'Access,
9635 -- new._object'Access,
9636 -- E,
9637 -- Abort_Present);
9638 -- return;
9640 -- <some more of the statement sequence for entry>
9642 -- -- Requeue from an entry body to a task entry
9644 -- Requeue_Protected_To_Task_Entry (
9645 -- New._task_id,
9646 -- E,
9647 -- Abort_Present);
9648 -- return;
9650 -- <rest of statement sequence for entry>
9651 -- Complete_Entry_Body (_object._object);
9653 -- exception
9654 -- when all others =>
9655 -- Exceptional_Complete_Entry_Body (
9656 -- _object._object, Get_GNAT_Exception);
9657 -- end;
9658 -- end entE;
9660 -- Requeue of a task entry call to a task entry
9662 -- Accept_Call (E, Ann);
9663 -- <start of statement sequence for accept statement>
9664 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9665 -- goto Lnn;
9666 -- <rest of statement sequence for accept statement>
9667 -- <<Lnn>>
9668 -- Complete_Rendezvous;
9670 -- exception
9671 -- when all others =>
9672 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9674 -- Requeue of a task entry call to a protected entry
9676 -- Accept_Call (E, Ann);
9677 -- <start of statement sequence for accept statement>
9678 -- Requeue_Task_To_Protected_Entry (
9679 -- new._object'Access,
9680 -- E,
9681 -- Abort_Present);
9682 -- newS (new, Pnn);
9683 -- goto Lnn;
9684 -- <rest of statement sequence for accept statement>
9685 -- <<Lnn>>
9686 -- Complete_Rendezvous;
9688 -- exception
9689 -- when all others =>
9690 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9692 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9693 -- marked by pragma Implemented (XXX, By_Entry).
9695 -- The requeue is inside a protected entry:
9697 -- procedure entE
9698 -- (O : System.Address;
9699 -- P : System.Address;
9700 -- E : Protected_Entry_Index)
9701 -- is
9702 -- <discriminant renamings>
9703 -- <private object renamings>
9704 -- type poVP is access poV;
9705 -- _object : ptVP := ptVP!(O);
9707 -- begin
9708 -- begin
9709 -- <start of statement sequence for entry>
9711 -- _Disp_Requeue
9712 -- (<interface class-wide object>,
9713 -- True,
9714 -- _object'Address,
9715 -- Ada.Tags.Get_Offset_Index
9716 -- (Tag (_object),
9717 -- <interface dispatch table index of target entry>),
9718 -- Abort_Present);
9719 -- return;
9721 -- <rest of statement sequence for entry>
9722 -- Complete_Entry_Body (_object._object);
9724 -- exception
9725 -- when all others =>
9726 -- Exceptional_Complete_Entry_Body (
9727 -- _object._object, Get_GNAT_Exception);
9728 -- end;
9729 -- end entE;
9731 -- The requeue is inside a task entry:
9733 -- Accept_Call (E, Ann);
9734 -- <start of statement sequence for accept statement>
9735 -- _Disp_Requeue
9736 -- (<interface class-wide object>,
9737 -- False,
9738 -- null,
9739 -- Ada.Tags.Get_Offset_Index
9740 -- (Tag (_object),
9741 -- <interface dispatch table index of target entrt>),
9742 -- Abort_Present);
9743 -- newS (new, Pnn);
9744 -- goto Lnn;
9745 -- <rest of statement sequence for accept statement>
9746 -- <<Lnn>>
9747 -- Complete_Rendezvous;
9749 -- exception
9750 -- when all others =>
9751 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9753 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9754 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9755 -- statement is replaced by a dispatching call with actual parameters taken
9756 -- from the inner-most accept statement or entry body.
9758 -- Target.Primitive (Param1, ..., ParamN);
9760 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9761 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9762 -- at all.
9764 -- declare
9765 -- S : constant Offset_Index :=
9766 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9767 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9769 -- begin
9770 -- if C = POK_Protected_Entry
9771 -- or else C = POK_Task_Entry
9772 -- then
9773 -- <statements for dispatching requeue>
9775 -- elsif C = POK_Protected_Procedure then
9776 -- <dispatching call equivalent>
9778 -- else
9779 -- raise Program_Error;
9780 -- end if;
9781 -- end;
9783 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9784 Loc : constant Source_Ptr := Sloc (N);
9785 Conc_Typ : Entity_Id;
9786 Concval : Node_Id;
9787 Ename : Node_Id;
9788 Index : Node_Id;
9789 Old_Typ : Entity_Id;
9791 function Build_Dispatching_Call_Equivalent return Node_Id;
9792 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9793 -- the form Concval.Ename. It is statically known that Ename is allowed
9794 -- to be implemented by a protected procedure. Create a dispatching call
9795 -- equivalent of Concval.Ename taking the actual parameters from the
9796 -- inner-most accept statement or entry body.
9798 function Build_Dispatching_Requeue return Node_Id;
9799 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9800 -- the form Concval.Ename. It is statically known that Ename is allowed
9801 -- to be implemented by a protected or a task entry. Create a call to
9802 -- primitive _Disp_Requeue which handles the low-level actions.
9804 function Build_Dispatching_Requeue_To_Any return Node_Id;
9805 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9806 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9807 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9808 -- determines at runtime whether Ename denotes an entry or a procedure
9809 -- and perform the appropriate kind of dispatching select.
9811 function Build_Normal_Requeue return Node_Id;
9812 -- N denotes a nondispatching requeue statement to either a task or a
9813 -- protected entry. Build the appropriate runtime call to perform the
9814 -- action.
9816 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9817 -- For a protected entry, create a return statement to skip the rest of
9818 -- the entry body. Otherwise, create a goto statement to skip the rest
9819 -- of a task accept statement. The lookup for the enclosing entry body
9820 -- or accept statement starts from Search.
9822 ---------------------------------------
9823 -- Build_Dispatching_Call_Equivalent --
9824 ---------------------------------------
9826 function Build_Dispatching_Call_Equivalent return Node_Id is
9827 Call_Ent : constant Entity_Id := Entity (Ename);
9828 Obj : constant Node_Id := Original_Node (Concval);
9829 Acc_Ent : Node_Id;
9830 Actuals : List_Id;
9831 Formal : Node_Id;
9832 Formals : List_Id;
9834 begin
9835 -- Climb the parent chain looking for the inner-most entry body or
9836 -- accept statement.
9838 Acc_Ent := N;
9839 while Present (Acc_Ent)
9840 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
9841 N_Entry_Body)
9842 loop
9843 Acc_Ent := Parent (Acc_Ent);
9844 end loop;
9846 -- A requeue statement should be housed inside an entry body or an
9847 -- accept statement at some level. If this is not the case, then the
9848 -- tree is malformed.
9850 pragma Assert (Present (Acc_Ent));
9852 -- Recover the list of formal parameters
9854 if Nkind (Acc_Ent) = N_Entry_Body then
9855 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9856 end if;
9858 Formals := Parameter_Specifications (Acc_Ent);
9860 -- Create the actual parameters for the dispatching call. These are
9861 -- simply copies of the entry body or accept statement formals in the
9862 -- same order as they appear.
9864 Actuals := No_List;
9866 if Present (Formals) then
9867 Actuals := New_List;
9868 Formal := First (Formals);
9869 while Present (Formal) loop
9870 Append_To (Actuals,
9871 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9872 Next (Formal);
9873 end loop;
9874 end if;
9876 -- Generate:
9877 -- Obj.Call_Ent (Actuals);
9879 return
9880 Make_Procedure_Call_Statement (Loc,
9881 Name =>
9882 Make_Selected_Component (Loc,
9883 Prefix => Make_Identifier (Loc, Chars (Obj)),
9884 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9886 Parameter_Associations => Actuals);
9887 end Build_Dispatching_Call_Equivalent;
9889 -------------------------------
9890 -- Build_Dispatching_Requeue --
9891 -------------------------------
9893 function Build_Dispatching_Requeue return Node_Id is
9894 Params : constant List_Id := New_List;
9896 begin
9897 -- Process the "with abort" parameter
9899 Prepend_To (Params,
9900 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
9902 -- Process the entry wrapper's position in the primary dispatch
9903 -- table parameter. Generate:
9905 -- Ada.Tags.Get_Entry_Index
9906 -- (T => To_Tag_Ptr (Obj'Address).all,
9907 -- Position =>
9908 -- Ada.Tags.Get_Offset_Index
9909 -- (Ada.Tags.Tag (Concval),
9910 -- <interface dispatch table position of Ename>));
9912 -- Note that Obj'Address is recursively expanded into a call to
9913 -- Base_Address (Obj).
9915 if Tagged_Type_Expansion then
9916 Prepend_To (Params,
9917 Make_Function_Call (Loc,
9918 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9919 Parameter_Associations => New_List (
9921 Make_Explicit_Dereference (Loc,
9922 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9923 Make_Attribute_Reference (Loc,
9924 Prefix => New_Copy_Tree (Concval),
9925 Attribute_Name => Name_Address))),
9927 Make_Function_Call (Loc,
9928 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9929 Parameter_Associations => New_List (
9930 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9931 Make_Integer_Literal (Loc,
9932 DT_Position (Entity (Ename))))))));
9934 -- VM targets
9936 else
9937 Prepend_To (Params,
9938 Make_Function_Call (Loc,
9939 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9940 Parameter_Associations => New_List (
9942 Make_Attribute_Reference (Loc,
9943 Prefix => Concval,
9944 Attribute_Name => Name_Tag),
9946 Make_Function_Call (Loc,
9947 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9949 Parameter_Associations => New_List (
9951 -- Obj_Tag
9953 Make_Attribute_Reference (Loc,
9954 Prefix => Concval,
9955 Attribute_Name => Name_Tag),
9957 -- Tag_Typ
9959 Make_Attribute_Reference (Loc,
9960 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
9961 Attribute_Name => Name_Tag),
9963 -- Position
9965 Make_Integer_Literal (Loc,
9966 DT_Position (Entity (Ename))))))));
9967 end if;
9969 -- Specific actuals for protected to XXX requeue
9971 if Is_Protected_Type (Old_Typ) then
9972 Prepend_To (Params,
9973 Make_Attribute_Reference (Loc, -- _object'Address
9974 Prefix =>
9975 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9976 Attribute_Name => Name_Address));
9978 Prepend_To (Params, -- True
9979 New_Occurrence_Of (Standard_True, Loc));
9981 -- Specific actuals for task to XXX requeue
9983 else
9984 pragma Assert (Is_Task_Type (Old_Typ));
9986 Prepend_To (Params, -- null
9987 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
9989 Prepend_To (Params, -- False
9990 New_Occurrence_Of (Standard_False, Loc));
9991 end if;
9993 -- Add the object parameter
9995 Prepend_To (Params, New_Copy_Tree (Concval));
9997 -- Generate:
9998 -- _Disp_Requeue (<Params>);
10000 -- Find entity for Disp_Requeue operation, which belongs to
10001 -- the type and may not be directly visible.
10003 declare
10004 Elmt : Elmt_Id;
10005 Op : Entity_Id;
10006 pragma Warnings (Off, Op);
10008 begin
10009 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
10010 while Present (Elmt) loop
10011 Op := Node (Elmt);
10012 exit when Chars (Op) = Name_uDisp_Requeue;
10013 Next_Elmt (Elmt);
10014 end loop;
10016 return
10017 Make_Procedure_Call_Statement (Loc,
10018 Name => New_Occurrence_Of (Op, Loc),
10019 Parameter_Associations => Params);
10020 end;
10021 end Build_Dispatching_Requeue;
10023 --------------------------------------
10024 -- Build_Dispatching_Requeue_To_Any --
10025 --------------------------------------
10027 function Build_Dispatching_Requeue_To_Any return Node_Id is
10028 Call_Ent : constant Entity_Id := Entity (Ename);
10029 Obj : constant Node_Id := Original_Node (Concval);
10030 Skip : constant Node_Id := Build_Skip_Statement (N);
10031 C : Entity_Id;
10032 Decls : List_Id;
10033 S : Entity_Id;
10034 Stmts : List_Id;
10036 begin
10037 Decls := New_List;
10038 Stmts := New_List;
10040 -- Dispatch table slot processing, generate:
10041 -- S : Integer;
10043 S := Build_S (Loc, Decls);
10045 -- Call kind processing, generate:
10046 -- C : Ada.Tags.Prim_Op_Kind;
10048 C := Build_C (Loc, Decls);
10050 -- Generate:
10051 -- S := Ada.Tags.Get_Offset_Index
10052 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10054 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10056 -- Generate:
10057 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10059 Append_To (Stmts,
10060 Make_Procedure_Call_Statement (Loc,
10061 Name =>
10062 New_Occurrence_Of (
10063 Find_Prim_Op (Etype (Etype (Obj)),
10064 Name_uDisp_Get_Prim_Op_Kind),
10065 Loc),
10066 Parameter_Associations => New_List (
10067 New_Copy_Tree (Obj),
10068 New_Occurrence_Of (S, Loc),
10069 New_Occurrence_Of (C, Loc))));
10071 Append_To (Stmts,
10073 -- if C = POK_Protected_Entry
10074 -- or else C = POK_Task_Entry
10075 -- then
10077 Make_Implicit_If_Statement (N,
10078 Condition =>
10079 Make_Op_Or (Loc,
10080 Left_Opnd =>
10081 Make_Op_Eq (Loc,
10082 Left_Opnd =>
10083 New_Occurrence_Of (C, Loc),
10084 Right_Opnd =>
10085 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10087 Right_Opnd =>
10088 Make_Op_Eq (Loc,
10089 Left_Opnd =>
10090 New_Occurrence_Of (C, Loc),
10091 Right_Opnd =>
10092 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10094 -- Dispatching requeue equivalent
10096 Then_Statements => New_List (
10097 Build_Dispatching_Requeue,
10098 Skip),
10100 -- elsif C = POK_Protected_Procedure then
10102 Elsif_Parts => New_List (
10103 Make_Elsif_Part (Loc,
10104 Condition =>
10105 Make_Op_Eq (Loc,
10106 Left_Opnd =>
10107 New_Occurrence_Of (C, Loc),
10108 Right_Opnd =>
10109 New_Occurrence_Of (
10110 RTE (RE_POK_Protected_Procedure), Loc)),
10112 -- Dispatching call equivalent
10114 Then_Statements => New_List (
10115 Build_Dispatching_Call_Equivalent))),
10117 -- else
10118 -- raise Program_Error;
10119 -- end if;
10121 Else_Statements => New_List (
10122 Make_Raise_Program_Error (Loc,
10123 Reason => PE_Explicit_Raise))));
10125 -- Wrap everything into a block
10127 return
10128 Make_Block_Statement (Loc,
10129 Declarations => Decls,
10130 Handled_Statement_Sequence =>
10131 Make_Handled_Sequence_Of_Statements (Loc,
10132 Statements => Stmts));
10133 end Build_Dispatching_Requeue_To_Any;
10135 --------------------------
10136 -- Build_Normal_Requeue --
10137 --------------------------
10139 function Build_Normal_Requeue return Node_Id is
10140 Params : constant List_Id := New_List;
10141 Param : Node_Id;
10142 RT_Call : Node_Id;
10144 begin
10145 -- Process the "with abort" parameter
10147 Prepend_To (Params,
10148 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10150 -- Add the index expression to the parameters. It is common among all
10151 -- four cases.
10153 Prepend_To (Params,
10154 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10156 if Is_Protected_Type (Old_Typ) then
10157 declare
10158 Self_Param : Node_Id;
10160 begin
10161 Self_Param :=
10162 Make_Attribute_Reference (Loc,
10163 Prefix =>
10164 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10165 Attribute_Name =>
10166 Name_Unchecked_Access);
10168 -- Protected to protected requeue
10170 if Is_Protected_Type (Conc_Typ) then
10171 RT_Call :=
10172 New_Occurrence_Of (
10173 RTE (RE_Requeue_Protected_Entry), Loc);
10175 Param :=
10176 Make_Attribute_Reference (Loc,
10177 Prefix =>
10178 Concurrent_Ref (Concval),
10179 Attribute_Name =>
10180 Name_Unchecked_Access);
10182 -- Protected to task requeue
10184 else pragma Assert (Is_Task_Type (Conc_Typ));
10185 RT_Call :=
10186 New_Occurrence_Of (
10187 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10189 Param := Concurrent_Ref (Concval);
10190 end if;
10192 Prepend_To (Params, Param);
10193 Prepend_To (Params, Self_Param);
10194 end;
10196 else pragma Assert (Is_Task_Type (Old_Typ));
10198 -- Task to protected requeue
10200 if Is_Protected_Type (Conc_Typ) then
10201 RT_Call :=
10202 New_Occurrence_Of (
10203 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10205 Param :=
10206 Make_Attribute_Reference (Loc,
10207 Prefix =>
10208 Concurrent_Ref (Concval),
10209 Attribute_Name =>
10210 Name_Unchecked_Access);
10212 -- Task to task requeue
10214 else pragma Assert (Is_Task_Type (Conc_Typ));
10215 RT_Call :=
10216 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10218 Param := Concurrent_Ref (Concval);
10219 end if;
10221 Prepend_To (Params, Param);
10222 end if;
10224 return
10225 Make_Procedure_Call_Statement (Loc,
10226 Name => RT_Call,
10227 Parameter_Associations => Params);
10228 end Build_Normal_Requeue;
10230 --------------------------
10231 -- Build_Skip_Statement --
10232 --------------------------
10234 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10235 Skip_Stmt : Node_Id;
10237 begin
10238 -- Build a return statement to skip the rest of the entire body
10240 if Is_Protected_Type (Old_Typ) then
10241 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10243 -- If the requeue is within a task, find the end label of the
10244 -- enclosing accept statement and create a goto statement to it.
10246 else
10247 declare
10248 Acc : Node_Id;
10249 Label : Node_Id;
10251 begin
10252 -- Climb the parent chain looking for the enclosing accept
10253 -- statement.
10255 Acc := Parent (Search);
10256 while Present (Acc)
10257 and then Nkind (Acc) /= N_Accept_Statement
10258 loop
10259 Acc := Parent (Acc);
10260 end loop;
10262 -- The last statement is the second label used for completing
10263 -- the rendezvous the usual way. The label we are looking for
10264 -- is right before it.
10266 Label :=
10267 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10269 pragma Assert (Nkind (Label) = N_Label);
10271 -- Generate a goto statement to skip the rest of the accept
10273 Skip_Stmt :=
10274 Make_Goto_Statement (Loc,
10275 Name =>
10276 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10277 end;
10278 end if;
10280 Set_Analyzed (Skip_Stmt);
10282 return Skip_Stmt;
10283 end Build_Skip_Statement;
10285 -- Start of processing for Expand_N_Requeue_Statement
10287 begin
10288 -- Extract the components of the entry call
10290 Extract_Entry (N, Concval, Ename, Index);
10291 Conc_Typ := Etype (Concval);
10293 -- If the prefix is an access to class-wide type, dereference to get
10294 -- object and entry type.
10296 if Is_Access_Type (Conc_Typ) then
10297 Conc_Typ := Designated_Type (Conc_Typ);
10298 Rewrite (Concval,
10299 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10300 Analyze_And_Resolve (Concval, Conc_Typ);
10301 end if;
10303 -- Examine the scope stack in order to find nearest enclosing protected
10304 -- or task type. This will constitute our invocation source.
10306 Old_Typ := Current_Scope;
10307 while Present (Old_Typ)
10308 and then not Is_Protected_Type (Old_Typ)
10309 and then not Is_Task_Type (Old_Typ)
10310 loop
10311 Old_Typ := Scope (Old_Typ);
10312 end loop;
10314 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10315 -- Concval.Ename where the type of Concval is class-wide concurrent
10316 -- interface.
10318 if Ada_Version >= Ada_2012
10319 and then Present (Concval)
10320 and then Is_Class_Wide_Type (Conc_Typ)
10321 and then Is_Concurrent_Interface (Conc_Typ)
10322 then
10323 declare
10324 Has_Impl : Boolean := False;
10325 Impl_Kind : Name_Id := No_Name;
10327 begin
10328 -- Check whether the Ename is flagged by pragma Implemented
10330 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10331 Has_Impl := True;
10332 Impl_Kind := Implementation_Kind (Entity (Ename));
10333 end if;
10335 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10336 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10338 if Has_Impl and then Impl_Kind = Name_By_Entry then
10339 Rewrite (N, Build_Dispatching_Requeue);
10340 Analyze (N);
10341 Insert_After (N, Build_Skip_Statement (N));
10343 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10344 -- a protected procedure. In this case the requeue is transformed
10345 -- into a dispatching call.
10347 elsif Has_Impl
10348 and then Impl_Kind = Name_By_Protected_Procedure
10349 then
10350 Rewrite (N, Build_Dispatching_Call_Equivalent);
10351 Analyze (N);
10353 -- The procedure_or_entry_NAME's implementation kind is either
10354 -- By_Any, Optional, or pragma Implemented was not applied at all.
10355 -- In this case a runtime test determines whether Ename denotes an
10356 -- entry or a protected procedure and performs the appropriate
10357 -- call.
10359 else
10360 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10361 Analyze (N);
10362 end if;
10363 end;
10365 -- Processing for regular (nondispatching) requeues
10367 else
10368 Rewrite (N, Build_Normal_Requeue);
10369 Analyze (N);
10370 Insert_After (N, Build_Skip_Statement (N));
10371 end if;
10372 end Expand_N_Requeue_Statement;
10374 -------------------------------
10375 -- Expand_N_Selective_Accept --
10376 -------------------------------
10378 procedure Expand_N_Selective_Accept (N : Node_Id) is
10379 Loc : constant Source_Ptr := Sloc (N);
10380 Alts : constant List_Id := Select_Alternatives (N);
10382 -- Note: in the below declarations a lot of new lists are allocated
10383 -- unconditionally which may well not end up being used. That's not
10384 -- a good idea since it wastes space gratuitously ???
10386 Accept_Case : List_Id;
10387 Accept_List : constant List_Id := New_List;
10389 Alt : Node_Id;
10390 Alt_List : constant List_Id := New_List;
10391 Alt_Stats : List_Id;
10392 Ann : Entity_Id := Empty;
10394 Check_Guard : Boolean := True;
10396 Decls : constant List_Id := New_List;
10397 Stats : constant List_Id := New_List;
10398 Body_List : constant List_Id := New_List;
10399 Trailing_List : constant List_Id := New_List;
10401 Choices : List_Id;
10402 Else_Present : Boolean := False;
10403 Terminate_Alt : Node_Id := Empty;
10404 Select_Mode : Node_Id;
10406 Delay_Case : List_Id;
10407 Delay_Count : Integer := 0;
10408 Delay_Val : Entity_Id;
10409 Delay_Index : Entity_Id;
10410 Delay_Min : Entity_Id;
10411 Delay_Num : Pos := 1;
10412 Delay_Alt_List : List_Id := New_List;
10413 Delay_List : constant List_Id := New_List;
10414 D : Entity_Id;
10415 M : Entity_Id;
10417 First_Delay : Boolean := True;
10418 Guard_Open : Entity_Id;
10420 End_Lab : Node_Id;
10421 Index : Pos := 1;
10422 Lab : Node_Id;
10423 Num_Alts : Nat;
10424 Num_Accept : Nat := 0;
10425 Proc : Node_Id;
10426 Time_Type : Entity_Id;
10427 Select_Call : Node_Id;
10429 Qnam : constant Entity_Id :=
10430 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10432 Xnam : constant Entity_Id :=
10433 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10435 -----------------------
10436 -- Local subprograms --
10437 -----------------------
10439 function Accept_Or_Raise return List_Id;
10440 -- For the rare case where delay alternatives all have guards, and
10441 -- all of them are closed, it is still possible that there were open
10442 -- accept alternatives with no callers. We must reexamine the
10443 -- Accept_List, and execute a selective wait with no else if some
10444 -- accept is open. If none, we raise program_error.
10446 procedure Add_Accept (Alt : Node_Id);
10447 -- Process a single accept statement in a select alternative. Build
10448 -- procedure for body of accept, and add entry to dispatch table with
10449 -- expression for guard, in preparation for call to run time select.
10451 function Make_And_Declare_Label (Num : Int) return Node_Id;
10452 -- Manufacture a label using Num as a serial number and declare it.
10453 -- The declaration is appended to Decls. The label marks the trailing
10454 -- statements of an accept or delay alternative.
10456 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10457 -- Build call to Selective_Wait runtime routine
10459 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10460 -- Add code to compare value of delay with previous values, and
10461 -- generate case entry for trailing statements.
10463 procedure Process_Accept_Alternative
10464 (Alt : Node_Id;
10465 Index : Int;
10466 Proc : Node_Id);
10467 -- Add code to call corresponding procedure, and branch to
10468 -- trailing statements, if any.
10470 ---------------------
10471 -- Accept_Or_Raise --
10472 ---------------------
10474 function Accept_Or_Raise return List_Id is
10475 Cond : Node_Id;
10476 Stats : List_Id;
10477 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10479 begin
10480 -- We generate the following:
10482 -- for J in q'range loop
10483 -- if q(J).S /=null_task_entry then
10484 -- selective_wait (simple_mode,...);
10485 -- done := True;
10486 -- exit;
10487 -- end if;
10488 -- end loop;
10490 -- if no rendez_vous then
10491 -- raise program_error;
10492 -- end if;
10494 -- Note that the code needs to know that the selector name
10495 -- in an Accept_Alternative is named S.
10497 Cond := Make_Op_Ne (Loc,
10498 Left_Opnd =>
10499 Make_Selected_Component (Loc,
10500 Prefix =>
10501 Make_Indexed_Component (Loc,
10502 Prefix => New_Occurrence_Of (Qnam, Loc),
10503 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10504 Selector_Name => Make_Identifier (Loc, Name_S)),
10505 Right_Opnd =>
10506 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10508 Stats := New_List (
10509 Make_Implicit_Loop_Statement (N,
10510 Iteration_Scheme =>
10511 Make_Iteration_Scheme (Loc,
10512 Loop_Parameter_Specification =>
10513 Make_Loop_Parameter_Specification (Loc,
10514 Defining_Identifier => J,
10515 Discrete_Subtype_Definition =>
10516 Make_Attribute_Reference (Loc,
10517 Prefix => New_Occurrence_Of (Qnam, Loc),
10518 Attribute_Name => Name_Range,
10519 Expressions => New_List (
10520 Make_Integer_Literal (Loc, 1))))),
10522 Statements => New_List (
10523 Make_Implicit_If_Statement (N,
10524 Condition => Cond,
10525 Then_Statements => New_List (
10526 Make_Select_Call (
10527 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10528 Make_Exit_Statement (Loc))))));
10530 Append_To (Stats,
10531 Make_Raise_Program_Error (Loc,
10532 Condition => Make_Op_Eq (Loc,
10533 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10534 Right_Opnd =>
10535 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10536 Reason => PE_All_Guards_Closed));
10538 return Stats;
10539 end Accept_Or_Raise;
10541 ----------------
10542 -- Add_Accept --
10543 ----------------
10545 procedure Add_Accept (Alt : Node_Id) is
10546 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10547 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10548 Eloc : constant Source_Ptr := Sloc (Ename);
10549 Eent : constant Entity_Id := Entity (Ename);
10550 Index : constant Node_Id := Entry_Index (Acc_Stm);
10551 Null_Body : Node_Id;
10552 Proc_Body : Node_Id;
10553 PB_Ent : Entity_Id;
10554 Expr : Node_Id;
10555 Call : Node_Id;
10557 begin
10558 if No (Ann) then
10559 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10560 end if;
10562 if Present (Condition (Alt)) then
10563 Expr :=
10564 Make_If_Expression (Eloc, New_List (
10565 Condition (Alt),
10566 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10567 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10568 else
10569 Expr :=
10570 Entry_Index_Expression
10571 (Eloc, Eent, Index, Scope (Eent));
10572 end if;
10574 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10575 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10577 -- Always add call to Abort_Undefer when generating code, since
10578 -- this is what the runtime expects (abort deferred in
10579 -- Selective_Wait). In CodePeer mode this only confuses the
10580 -- analysis with unknown calls, so don't do it.
10582 if not CodePeer_Mode then
10583 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10584 Insert_Before
10585 (First (Statements (Handled_Statement_Sequence
10586 (Accept_Statement (Alt)))),
10587 Call);
10588 Analyze (Call);
10589 end if;
10591 PB_Ent :=
10592 Make_Defining_Identifier (Eloc,
10593 New_External_Name (Chars (Ename), 'A', Num_Accept));
10595 -- Link the acceptor to the original receiving entry
10597 Set_Ekind (PB_Ent, E_Procedure);
10598 Set_Receiving_Entry (PB_Ent, Eent);
10600 if Comes_From_Source (Alt) then
10601 Set_Debug_Info_Needed (PB_Ent);
10602 end if;
10604 Proc_Body :=
10605 Make_Subprogram_Body (Eloc,
10606 Specification =>
10607 Make_Procedure_Specification (Eloc,
10608 Defining_Unit_Name => PB_Ent),
10609 Declarations => Declarations (Acc_Stm),
10610 Handled_Statement_Sequence =>
10611 Build_Accept_Body (Accept_Statement (Alt)));
10613 -- During the analysis of the body of the accept statement, any
10614 -- zero cost exception handler records were collected in the
10615 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10616 -- This is where we move them to where they belong, namely the
10617 -- newly created procedure.
10619 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10620 Append (Proc_Body, Body_List);
10622 else
10623 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10625 -- if accept statement has declarations, insert above, given that
10626 -- we are not creating a body for the accept.
10628 if Present (Declarations (Acc_Stm)) then
10629 Insert_Actions (N, Declarations (Acc_Stm));
10630 end if;
10631 end if;
10633 Append_To (Accept_List,
10634 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10636 Num_Accept := Num_Accept + 1;
10637 end Add_Accept;
10639 ----------------------------
10640 -- Make_And_Declare_Label --
10641 ----------------------------
10643 function Make_And_Declare_Label (Num : Int) return Node_Id is
10644 Lab_Id : Node_Id;
10646 begin
10647 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10648 Lab :=
10649 Make_Label (Loc, Lab_Id);
10651 Append_To (Decls,
10652 Make_Implicit_Label_Declaration (Loc,
10653 Defining_Identifier =>
10654 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10655 Label_Construct => Lab));
10657 return Lab;
10658 end Make_And_Declare_Label;
10660 ----------------------
10661 -- Make_Select_Call --
10662 ----------------------
10664 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10665 Params : constant List_Id := New_List;
10667 begin
10668 Append_To (Params,
10669 Make_Attribute_Reference (Loc,
10670 Prefix => New_Occurrence_Of (Qnam, Loc),
10671 Attribute_Name => Name_Unchecked_Access));
10672 Append_To (Params, Select_Mode);
10673 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10674 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10676 return
10677 Make_Procedure_Call_Statement (Loc,
10678 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10679 Parameter_Associations => Params);
10680 end Make_Select_Call;
10682 --------------------------------
10683 -- Process_Accept_Alternative --
10684 --------------------------------
10686 procedure Process_Accept_Alternative
10687 (Alt : Node_Id;
10688 Index : Int;
10689 Proc : Node_Id)
10691 Astmt : constant Node_Id := Accept_Statement (Alt);
10692 Alt_Stats : List_Id;
10694 begin
10695 Adjust_Condition (Condition (Alt));
10697 -- Accept with body
10699 if Present (Handled_Statement_Sequence (Astmt)) then
10700 Alt_Stats :=
10701 New_List (
10702 Make_Procedure_Call_Statement (Sloc (Proc),
10703 Name =>
10704 New_Occurrence_Of
10705 (Defining_Unit_Name (Specification (Proc)),
10706 Sloc (Proc))));
10708 -- Accept with no body (followed by trailing statements)
10710 else
10711 Alt_Stats := Empty_List;
10712 end if;
10714 Ensure_Statement_Present (Sloc (Astmt), Alt);
10716 -- After the call, if any, branch to trailing statements, if any.
10717 -- We create a label for each, as well as the corresponding label
10718 -- declaration.
10720 if not Is_Empty_List (Statements (Alt)) then
10721 Lab := Make_And_Declare_Label (Index);
10722 Append (Lab, Trailing_List);
10723 Append_List (Statements (Alt), Trailing_List);
10724 Append_To (Trailing_List,
10725 Make_Goto_Statement (Loc,
10726 Name => New_Copy (Identifier (End_Lab))));
10728 else
10729 Lab := End_Lab;
10730 end if;
10732 Append_To (Alt_Stats,
10733 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10735 Append_To (Alt_List,
10736 Make_Case_Statement_Alternative (Loc,
10737 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10738 Statements => Alt_Stats));
10739 end Process_Accept_Alternative;
10741 -------------------------------
10742 -- Process_Delay_Alternative --
10743 -------------------------------
10745 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10746 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10747 Cond : Node_Id;
10748 Delay_Alt : List_Id;
10750 begin
10751 -- Deal with C/Fortran boolean as delay condition
10753 Adjust_Condition (Condition (Alt));
10755 -- Determine the smallest specified delay
10757 -- for each delay alternative generate:
10759 -- if guard-expression then
10760 -- Delay_Val := delay-expression;
10761 -- Guard_Open := True;
10762 -- if Delay_Val < Delay_Min then
10763 -- Delay_Min := Delay_Val;
10764 -- Delay_Index := Index;
10765 -- end if;
10766 -- end if;
10768 -- The enclosing if-statement is omitted if there is no guard
10770 if Delay_Count = 1 or else First_Delay then
10771 First_Delay := False;
10773 Delay_Alt := New_List (
10774 Make_Assignment_Statement (Loc,
10775 Name => New_Occurrence_Of (Delay_Min, Loc),
10776 Expression => Expression (Delay_Statement (Alt))));
10778 if Delay_Count > 1 then
10779 Append_To (Delay_Alt,
10780 Make_Assignment_Statement (Loc,
10781 Name => New_Occurrence_Of (Delay_Index, Loc),
10782 Expression => Make_Integer_Literal (Loc, Index)));
10783 end if;
10785 else
10786 Delay_Alt := New_List (
10787 Make_Assignment_Statement (Loc,
10788 Name => New_Occurrence_Of (Delay_Val, Loc),
10789 Expression => Expression (Delay_Statement (Alt))));
10791 if Time_Type = Standard_Duration then
10792 Cond :=
10793 Make_Op_Lt (Loc,
10794 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
10795 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
10797 else
10798 -- The scope of the time type must define a comparison
10799 -- operator. The scope itself may not be visible, so we
10800 -- construct a node with entity information to insure that
10801 -- semantic analysis can find the proper operator.
10803 Cond :=
10804 Make_Function_Call (Loc,
10805 Name => Make_Selected_Component (Loc,
10806 Prefix =>
10807 New_Occurrence_Of (Scope (Time_Type), Loc),
10808 Selector_Name =>
10809 Make_Operator_Symbol (Loc,
10810 Chars => Name_Op_Lt,
10811 Strval => No_String)),
10812 Parameter_Associations =>
10813 New_List (
10814 New_Occurrence_Of (Delay_Val, Loc),
10815 New_Occurrence_Of (Delay_Min, Loc)));
10817 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10818 end if;
10820 Append_To (Delay_Alt,
10821 Make_Implicit_If_Statement (N,
10822 Condition => Cond,
10823 Then_Statements => New_List (
10824 Make_Assignment_Statement (Loc,
10825 Name => New_Occurrence_Of (Delay_Min, Loc),
10826 Expression => New_Occurrence_Of (Delay_Val, Loc)),
10828 Make_Assignment_Statement (Loc,
10829 Name => New_Occurrence_Of (Delay_Index, Loc),
10830 Expression => Make_Integer_Literal (Loc, Index)))));
10831 end if;
10833 if Check_Guard then
10834 Append_To (Delay_Alt,
10835 Make_Assignment_Statement (Loc,
10836 Name => New_Occurrence_Of (Guard_Open, Loc),
10837 Expression => New_Occurrence_Of (Standard_True, Loc)));
10838 end if;
10840 if Present (Condition (Alt)) then
10841 Delay_Alt := New_List (
10842 Make_Implicit_If_Statement (N,
10843 Condition => Condition (Alt),
10844 Then_Statements => Delay_Alt));
10845 end if;
10847 Append_List (Delay_Alt, Delay_List);
10849 Ensure_Statement_Present (Dloc, Alt);
10851 -- If the delay alternative has a statement part, add choice to the
10852 -- case statements for delays.
10854 if not Is_Empty_List (Statements (Alt)) then
10856 if Delay_Count = 1 then
10857 Append_List (Statements (Alt), Delay_Alt_List);
10859 else
10860 Append_To (Delay_Alt_List,
10861 Make_Case_Statement_Alternative (Loc,
10862 Discrete_Choices => New_List (
10863 Make_Integer_Literal (Loc, Index)),
10864 Statements => Statements (Alt)));
10865 end if;
10867 elsif Delay_Count = 1 then
10869 -- If the single delay has no trailing statements, add a branch
10870 -- to the exit label to the selective wait.
10872 Delay_Alt_List := New_List (
10873 Make_Goto_Statement (Loc,
10874 Name => New_Copy (Identifier (End_Lab))));
10876 end if;
10877 end Process_Delay_Alternative;
10879 -- Start of processing for Expand_N_Selective_Accept
10881 begin
10882 Process_Statements_For_Controlled_Objects (N);
10884 -- First insert some declarations before the select. The first is:
10886 -- Ann : Address
10888 -- This variable holds the parameters passed to the accept body. This
10889 -- declaration has already been inserted by the time we get here by
10890 -- a call to Expand_Accept_Declarations made from the semantics when
10891 -- processing the first accept statement contained in the select. We
10892 -- can find this entity as Accept_Address (E), where E is any of the
10893 -- entries references by contained accept statements.
10895 -- The first step is to scan the list of Selective_Accept_Statements
10896 -- to find this entity, and also count the number of accepts, and
10897 -- determine if terminated, delay or else is present:
10899 Num_Alts := 0;
10901 Alt := First (Alts);
10902 while Present (Alt) loop
10903 Process_Statements_For_Controlled_Objects (Alt);
10905 if Nkind (Alt) = N_Accept_Alternative then
10906 Add_Accept (Alt);
10908 elsif Nkind (Alt) = N_Delay_Alternative then
10909 Delay_Count := Delay_Count + 1;
10911 -- If the delays are relative delays, the delay expressions have
10912 -- type Standard_Duration. Otherwise they must have some time type
10913 -- recognized by GNAT.
10915 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10916 Time_Type := Standard_Duration;
10917 else
10918 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10920 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10921 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10922 then
10923 null;
10924 else
10925 Error_Msg_NE (
10926 "& is not a time type (RM 9.6(6))",
10927 Expression (Delay_Statement (Alt)), Time_Type);
10928 Time_Type := Standard_Duration;
10929 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10930 end if;
10931 end if;
10933 if No (Condition (Alt)) then
10935 -- This guard will always be open
10937 Check_Guard := False;
10938 end if;
10940 elsif Nkind (Alt) = N_Terminate_Alternative then
10941 Adjust_Condition (Condition (Alt));
10942 Terminate_Alt := Alt;
10943 end if;
10945 Num_Alts := Num_Alts + 1;
10946 Next (Alt);
10947 end loop;
10949 Else_Present := Present (Else_Statements (N));
10951 -- At the same time (see procedure Add_Accept) we build the accept list:
10953 -- Qnn : Accept_List (1 .. num-select) := (
10954 -- (null-body, entry-index),
10955 -- (null-body, entry-index),
10956 -- ..
10957 -- (null_body, entry-index));
10959 -- In the above declaration, null-body is True if the corresponding
10960 -- accept has no body, and false otherwise. The entry is either the
10961 -- entry index expression if there is no guard, or if a guard is
10962 -- present, then an if expression of the form:
10964 -- (if guard then entry-index else Null_Task_Entry)
10966 -- If a guard is statically known to be false, the entry can simply
10967 -- be omitted from the accept list.
10969 Append_To (Decls,
10970 Make_Object_Declaration (Loc,
10971 Defining_Identifier => Qnam,
10972 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10973 Aliased_Present => True,
10974 Expression =>
10975 Make_Qualified_Expression (Loc,
10976 Subtype_Mark =>
10977 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10978 Expression =>
10979 Make_Aggregate (Loc, Expressions => Accept_List))));
10981 -- Then we declare the variable that holds the index for the accept
10982 -- that will be selected for service:
10984 -- Xnn : Select_Index;
10986 Append_To (Decls,
10987 Make_Object_Declaration (Loc,
10988 Defining_Identifier => Xnam,
10989 Object_Definition =>
10990 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
10991 Expression =>
10992 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
10994 -- After this follow procedure declarations for each accept body
10996 -- procedure Pnn is
10997 -- begin
10998 -- ...
10999 -- end;
11001 -- where the ... are statements from the corresponding procedure body.
11002 -- No parameters are involved, since the parameters are passed via Ann
11003 -- and the parameter references have already been expanded to be direct
11004 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
11005 -- any embedded tasking statements (which would normally be illegal in
11006 -- procedures), have been converted to calls to the tasking runtime so
11007 -- there is no problem in putting them into procedures.
11009 -- The original accept statement has been expanded into a block in
11010 -- the same fashion as for simple accepts (see Build_Accept_Body).
11012 -- Note: we don't really need to build these procedures for the case
11013 -- where no delay statement is present, but it is just as easy to
11014 -- build them unconditionally, and not significantly inefficient,
11015 -- since if they are short they will be inlined anyway.
11017 -- The procedure declarations have been assembled in Body_List
11019 -- If delays are present, we must compute the required delay.
11020 -- We first generate the declarations:
11022 -- Delay_Index : Boolean := 0;
11023 -- Delay_Min : Some_Time_Type.Time;
11024 -- Delay_Val : Some_Time_Type.Time;
11026 -- Delay_Index will be set to the index of the minimum delay, i.e. the
11027 -- active delay that is actually chosen as the basis for the possible
11028 -- delay if an immediate rendez-vous is not possible.
11030 -- In the most common case there is a single delay statement, and this
11031 -- is handled specially.
11033 if Delay_Count > 0 then
11035 -- Generate the required declarations
11037 Delay_Val :=
11038 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11039 Delay_Index :=
11040 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11041 Delay_Min :=
11042 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11044 Append_To (Decls,
11045 Make_Object_Declaration (Loc,
11046 Defining_Identifier => Delay_Val,
11047 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11049 Append_To (Decls,
11050 Make_Object_Declaration (Loc,
11051 Defining_Identifier => Delay_Index,
11052 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11053 Expression => Make_Integer_Literal (Loc, 0)));
11055 Append_To (Decls,
11056 Make_Object_Declaration (Loc,
11057 Defining_Identifier => Delay_Min,
11058 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11059 Expression =>
11060 Unchecked_Convert_To (Time_Type,
11061 Make_Attribute_Reference (Loc,
11062 Prefix =>
11063 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11064 Attribute_Name => Name_Last))));
11066 -- Create Duration and Delay_Mode objects used for passing a delay
11067 -- value to RTS
11069 D := Make_Temporary (Loc, 'D');
11070 M := Make_Temporary (Loc, 'M');
11072 declare
11073 Discr : Entity_Id;
11075 begin
11076 -- Note that these values are defined in s-osprim.ads and must
11077 -- be kept in sync:
11079 -- Relative : constant := 0;
11080 -- Absolute_Calendar : constant := 1;
11081 -- Absolute_RT : constant := 2;
11083 if Time_Type = Standard_Duration then
11084 Discr := Make_Integer_Literal (Loc, 0);
11086 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11087 Discr := Make_Integer_Literal (Loc, 1);
11089 else
11090 pragma Assert
11091 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11092 Discr := Make_Integer_Literal (Loc, 2);
11093 end if;
11095 Append_To (Decls,
11096 Make_Object_Declaration (Loc,
11097 Defining_Identifier => D,
11098 Object_Definition =>
11099 New_Occurrence_Of (Standard_Duration, Loc)));
11101 Append_To (Decls,
11102 Make_Object_Declaration (Loc,
11103 Defining_Identifier => M,
11104 Object_Definition =>
11105 New_Occurrence_Of (Standard_Integer, Loc),
11106 Expression => Discr));
11107 end;
11109 if Check_Guard then
11110 Guard_Open :=
11111 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11113 Append_To (Decls,
11114 Make_Object_Declaration (Loc,
11115 Defining_Identifier => Guard_Open,
11116 Object_Definition =>
11117 New_Occurrence_Of (Standard_Boolean, Loc),
11118 Expression =>
11119 New_Occurrence_Of (Standard_False, Loc)));
11120 end if;
11122 -- Delay_Count is zero, don't need M and D set (suppress warning)
11124 else
11125 M := Empty;
11126 D := Empty;
11127 end if;
11129 if Present (Terminate_Alt) then
11131 -- If the terminate alternative guard is False, use
11132 -- Simple_Mode; otherwise use Terminate_Mode.
11134 if Present (Condition (Terminate_Alt)) then
11135 Select_Mode := Make_If_Expression (Loc,
11136 New_List (Condition (Terminate_Alt),
11137 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11138 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11139 else
11140 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11141 end if;
11143 elsif Else_Present or Delay_Count > 0 then
11144 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11146 else
11147 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11148 end if;
11150 Select_Call := Make_Select_Call (Select_Mode);
11151 Append (Select_Call, Stats);
11153 -- Now generate code to act on the result. There is an entry
11154 -- in this case for each accept statement with a non-null body,
11155 -- followed by a branch to the statements that follow the Accept.
11156 -- In the absence of delay alternatives, we generate:
11158 -- case X is
11159 -- when No_Rendezvous => -- omitted if simple mode
11160 -- goto Lab0;
11162 -- when 1 =>
11163 -- P1n;
11164 -- goto Lab1;
11166 -- when 2 =>
11167 -- P2n;
11168 -- goto Lab2;
11170 -- when others =>
11171 -- goto Exit;
11172 -- end case;
11174 -- Lab0: Else_Statements;
11175 -- goto exit;
11177 -- Lab1: Trailing_Statements1;
11178 -- goto Exit;
11180 -- Lab2: Trailing_Statements2;
11181 -- goto Exit;
11182 -- ...
11183 -- Exit:
11185 -- Generate label for common exit
11187 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11189 -- First entry is the default case, when no rendezvous is possible
11191 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11193 if Else_Present then
11195 -- If no rendezvous is possible, the else part is executed
11197 Lab := Make_And_Declare_Label (0);
11198 Alt_Stats := New_List (
11199 Make_Goto_Statement (Loc,
11200 Name => New_Copy (Identifier (Lab))));
11202 Append (Lab, Trailing_List);
11203 Append_List (Else_Statements (N), Trailing_List);
11204 Append_To (Trailing_List,
11205 Make_Goto_Statement (Loc,
11206 Name => New_Copy (Identifier (End_Lab))));
11207 else
11208 Alt_Stats := New_List (
11209 Make_Goto_Statement (Loc,
11210 Name => New_Copy (Identifier (End_Lab))));
11211 end if;
11213 Append_To (Alt_List,
11214 Make_Case_Statement_Alternative (Loc,
11215 Discrete_Choices => Choices,
11216 Statements => Alt_Stats));
11218 -- We make use of the fact that Accept_Index is an integer type, and
11219 -- generate successive literals for entries for each accept. Only those
11220 -- for which there is a body or trailing statements get a case entry.
11222 Alt := First (Select_Alternatives (N));
11223 Proc := First (Body_List);
11224 while Present (Alt) loop
11226 if Nkind (Alt) = N_Accept_Alternative then
11227 Process_Accept_Alternative (Alt, Index, Proc);
11228 Index := Index + 1;
11230 if Present
11231 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11232 then
11233 Next (Proc);
11234 end if;
11236 elsif Nkind (Alt) = N_Delay_Alternative then
11237 Process_Delay_Alternative (Alt, Delay_Num);
11238 Delay_Num := Delay_Num + 1;
11239 end if;
11241 Next (Alt);
11242 end loop;
11244 -- An others choice is always added to the main case, as well
11245 -- as the delay case (to satisfy the compiler).
11247 Append_To (Alt_List,
11248 Make_Case_Statement_Alternative (Loc,
11249 Discrete_Choices =>
11250 New_List (Make_Others_Choice (Loc)),
11251 Statements =>
11252 New_List (Make_Goto_Statement (Loc,
11253 Name => New_Copy (Identifier (End_Lab))))));
11255 Accept_Case := New_List (
11256 Make_Case_Statement (Loc,
11257 Expression => New_Occurrence_Of (Xnam, Loc),
11258 Alternatives => Alt_List));
11260 Append_List (Trailing_List, Accept_Case);
11261 Append_List (Body_List, Decls);
11263 -- Construct case statement for trailing statements of delay
11264 -- alternatives, if there are several of them.
11266 if Delay_Count > 1 then
11267 Append_To (Delay_Alt_List,
11268 Make_Case_Statement_Alternative (Loc,
11269 Discrete_Choices =>
11270 New_List (Make_Others_Choice (Loc)),
11271 Statements =>
11272 New_List (Make_Null_Statement (Loc))));
11274 Delay_Case := New_List (
11275 Make_Case_Statement (Loc,
11276 Expression => New_Occurrence_Of (Delay_Index, Loc),
11277 Alternatives => Delay_Alt_List));
11278 else
11279 Delay_Case := Delay_Alt_List;
11280 end if;
11282 -- If there are no delay alternatives, we append the case statement
11283 -- to the statement list.
11285 if Delay_Count = 0 then
11286 Append_List (Accept_Case, Stats);
11288 -- Delay alternatives present
11290 else
11291 -- If delay alternatives are present we generate:
11293 -- find minimum delay.
11294 -- DX := minimum delay;
11295 -- M := <delay mode>;
11296 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11297 -- DX, MX, X);
11299 -- if X = No_Rendezvous then
11300 -- case statement for delay statements.
11301 -- else
11302 -- case statement for accept alternatives.
11303 -- end if;
11305 declare
11306 Cases : Node_Id;
11307 Stmt : Node_Id;
11308 Parms : List_Id;
11309 Parm : Node_Id;
11310 Conv : Node_Id;
11312 begin
11313 -- The type of the delay expression is known to be legal
11315 if Time_Type = Standard_Duration then
11316 Conv := New_Occurrence_Of (Delay_Min, Loc);
11318 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11319 Conv := Make_Function_Call (Loc,
11320 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11321 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11323 else
11324 pragma Assert
11325 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11327 Conv := Make_Function_Call (Loc,
11328 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11329 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11330 end if;
11332 Stmt := Make_Assignment_Statement (Loc,
11333 Name => New_Occurrence_Of (D, Loc),
11334 Expression => Conv);
11336 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11338 Parms := Parameter_Associations (Select_Call);
11340 Parm := First (Parms);
11341 while Present (Parm) and then Parm /= Select_Mode loop
11342 Next (Parm);
11343 end loop;
11345 pragma Assert (Present (Parm));
11346 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11347 Analyze (Parm);
11349 -- Prepare two new parameters of Duration and Delay_Mode type
11350 -- which represent the value and the mode of the minimum delay.
11352 Next (Parm);
11353 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11354 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11356 -- Create a call to RTS
11358 Rewrite (Select_Call,
11359 Make_Procedure_Call_Statement (Loc,
11360 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11361 Parameter_Associations => Parms));
11363 -- This new call should follow the calculation of the minimum
11364 -- delay.
11366 Insert_List_Before (Select_Call, Delay_List);
11368 if Check_Guard then
11369 Stmt :=
11370 Make_Implicit_If_Statement (N,
11371 Condition => New_Occurrence_Of (Guard_Open, Loc),
11372 Then_Statements => New_List (
11373 New_Copy_Tree (Stmt),
11374 New_Copy_Tree (Select_Call)),
11375 Else_Statements => Accept_Or_Raise);
11376 Rewrite (Select_Call, Stmt);
11377 else
11378 Insert_Before (Select_Call, Stmt);
11379 end if;
11381 Cases :=
11382 Make_Implicit_If_Statement (N,
11383 Condition => Make_Op_Eq (Loc,
11384 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11385 Right_Opnd =>
11386 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11388 Then_Statements => Delay_Case,
11389 Else_Statements => Accept_Case);
11391 Append (Cases, Stats);
11392 end;
11393 end if;
11395 Append (End_Lab, Stats);
11397 -- Replace accept statement with appropriate block
11399 Rewrite (N,
11400 Make_Block_Statement (Loc,
11401 Declarations => Decls,
11402 Handled_Statement_Sequence =>
11403 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11404 Analyze (N);
11406 -- Note: have to worry more about abort deferral in above code ???
11408 -- Final step is to unstack the Accept_Address entries for all accept
11409 -- statements appearing in accept alternatives in the select statement
11411 Alt := First (Alts);
11412 while Present (Alt) loop
11413 if Nkind (Alt) = N_Accept_Alternative then
11414 Remove_Last_Elmt (Accept_Address
11415 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11416 end if;
11418 Next (Alt);
11419 end loop;
11420 end Expand_N_Selective_Accept;
11422 -------------------------------------------
11423 -- Expand_N_Single_Protected_Declaration --
11424 -------------------------------------------
11426 -- A single protected declaration should never be present after semantic
11427 -- analysis because it is transformed into a protected type declaration
11428 -- and an accompanying anonymous object. This routine ensures that the
11429 -- transformation takes place.
11431 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11432 begin
11433 raise Program_Error;
11434 end Expand_N_Single_Protected_Declaration;
11436 --------------------------------------
11437 -- Expand_N_Single_Task_Declaration --
11438 --------------------------------------
11440 -- A single task declaration should never be present after semantic
11441 -- analysis because it is transformed into a task type declaration and
11442 -- an accompanying anonymous object. This routine ensures that the
11443 -- transformation takes place.
11445 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11446 begin
11447 raise Program_Error;
11448 end Expand_N_Single_Task_Declaration;
11450 ------------------------
11451 -- Expand_N_Task_Body --
11452 ------------------------
11454 -- Given a task body
11456 -- task body tname is
11457 -- <declarations>
11458 -- begin
11459 -- <statements>
11460 -- end x;
11462 -- This expansion routine converts it into a procedure and sets the
11463 -- elaboration flag for the procedure to true, to represent the fact
11464 -- that the task body is now elaborated:
11466 -- procedure tnameB (_Task : access tnameV) is
11467 -- discriminal : dtype renames _Task.discriminant;
11469 -- procedure _clean is
11470 -- begin
11471 -- Abort_Defer.all;
11472 -- Complete_Task;
11473 -- Abort_Undefer.all;
11474 -- return;
11475 -- end _clean;
11477 -- begin
11478 -- Abort_Undefer.all;
11479 -- <declarations>
11480 -- System.Task_Stages.Complete_Activation;
11481 -- <statements>
11482 -- at end
11483 -- _clean;
11484 -- end tnameB;
11486 -- tnameE := True;
11488 -- In addition, if the task body is an activator, then a call to activate
11489 -- tasks is added at the start of the statements, before the call to
11490 -- Complete_Activation, and if in addition the task is a master then it
11491 -- must be established as a master. These calls are inserted and analyzed
11492 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11493 -- expanded.
11495 -- There is one discriminal declaration line generated for each
11496 -- discriminant that is present to provide an easy reference point for
11497 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11499 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11500 -- task body procedures have a profile (Arg : System.Address). That is
11501 -- needed because GNARLI has to use the same access-to-subprogram type
11502 -- for all task types. We depend here on knowing that in GNAT, passing
11503 -- an address argument by value is identical to passing a record value
11504 -- by access (in either case a single pointer is passed), so even though
11505 -- this procedure has the wrong profile. In fact it's all OK, since the
11506 -- callings sequence is identical.
11508 procedure Expand_N_Task_Body (N : Node_Id) is
11509 Loc : constant Source_Ptr := Sloc (N);
11510 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11511 Call : Node_Id;
11512 New_N : Node_Id;
11514 Insert_Nod : Node_Id;
11515 -- Used to determine the proper location of wrapper body insertions
11517 begin
11518 -- if no task body procedure, means we had an error in configurable
11519 -- run-time mode, and there is no point in proceeding further.
11521 if No (Task_Body_Procedure (Ttyp)) then
11522 return;
11523 end if;
11525 -- Add renaming declarations for discriminals and a declaration for the
11526 -- entry family index (if applicable).
11528 Install_Private_Data_Declarations
11529 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11531 -- Add a call to Abort_Undefer at the very beginning of the task
11532 -- body since this body is called with abort still deferred.
11534 if Abort_Allowed then
11535 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11536 Insert_Before
11537 (First (Statements (Handled_Statement_Sequence (N))), Call);
11538 Analyze (Call);
11539 end if;
11541 -- The statement part has already been protected with an at_end and
11542 -- cleanup actions. The call to Complete_Activation must be placed
11543 -- at the head of the sequence of statements of that block. The
11544 -- declarations have been merged in this sequence of statements but
11545 -- the first real statement is accessible from the First_Real_Statement
11546 -- field (which was set for exactly this purpose).
11548 if Restricted_Profile then
11549 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11550 else
11551 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11552 end if;
11554 Insert_Before
11555 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11556 Analyze (Call);
11558 New_N :=
11559 Make_Subprogram_Body (Loc,
11560 Specification => Build_Task_Proc_Specification (Ttyp),
11561 Declarations => Declarations (N),
11562 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11563 Set_Is_Task_Body_Procedure (New_N);
11565 -- If the task contains generic instantiations, cleanup actions are
11566 -- delayed until after instantiation. Transfer the activation chain to
11567 -- the subprogram, to insure that the activation call is properly
11568 -- generated. It the task body contains inner tasks, indicate that the
11569 -- subprogram is a task master.
11571 if Delay_Cleanups (Ttyp) then
11572 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11573 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11574 end if;
11576 Rewrite (N, New_N);
11577 Analyze (N);
11579 -- Set elaboration flag immediately after task body. If the body is a
11580 -- subunit, the flag is set in the declarative part containing the stub.
11582 if Nkind (Parent (N)) /= N_Subunit then
11583 Insert_After (N,
11584 Make_Assignment_Statement (Loc,
11585 Name =>
11586 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11587 Expression => New_Occurrence_Of (Standard_True, Loc)));
11588 end if;
11590 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11591 -- the task body. At this point all wrapper specs have been created,
11592 -- frozen and included in the dispatch table for the task type.
11594 if Ada_Version >= Ada_2005 then
11595 if Nkind (Parent (N)) = N_Subunit then
11596 Insert_Nod := Corresponding_Stub (Parent (N));
11597 else
11598 Insert_Nod := N;
11599 end if;
11601 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11602 end if;
11603 end Expand_N_Task_Body;
11605 ------------------------------------
11606 -- Expand_N_Task_Type_Declaration --
11607 ------------------------------------
11609 -- We have several things to do. First we must create a Boolean flag used
11610 -- to mark if the body is elaborated yet. This variable gets set to True
11611 -- when the body of the task is elaborated (we can't rely on the normal
11612 -- ABE mechanism for the task body, since we need to pass an access to
11613 -- this elaboration boolean to the runtime routines).
11615 -- taskE : aliased Boolean := False;
11617 -- Next a variable is declared to hold the task stack size (either the
11618 -- default : Unspecified_Size, or a value that is set by a pragma
11619 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11620 -- the variable is initialized with this value:
11622 -- taskZ : Size_Type := Unspecified_Size;
11623 -- or
11624 -- taskZ : Size_Type := Size_Type (size_expression);
11626 -- Note: No variable is needed to hold the task relative deadline since
11627 -- its value would never be static because the parameter is of a private
11628 -- type (Ada.Real_Time.Time_Span).
11630 -- Next we create a corresponding record type declaration used to represent
11631 -- values of this task. The general form of this type declaration is
11633 -- type taskV (discriminants) is record
11634 -- _Task_Id : Task_Id;
11635 -- entry_family : array (bounds) of Void;
11636 -- _Priority : Integer := priority_expression;
11637 -- _Size : Size_Type := size_expression;
11638 -- _Secondary_Stack_Size : Size_Type := size_expression;
11639 -- _Task_Info : Task_Info_Type := task_info_expression;
11640 -- _CPU : Integer := cpu_range_expression;
11641 -- _Relative_Deadline : Time_Span := time_span_expression;
11642 -- _Domain : Dispatching_Domain := dd_expression;
11643 -- end record;
11645 -- The discriminants are present only if the corresponding task type has
11646 -- discriminants, and they exactly mirror the task type discriminants.
11648 -- The Id field is always present. It contains the Task_Id value, as set by
11649 -- the call to Create_Task. Note that although the task is limited, the
11650 -- task value record type is not limited, so there is no problem in passing
11651 -- this field as an out parameter to Create_Task.
11653 -- One entry_family component is present for each entry family in the task
11654 -- definition. The bounds correspond to the bounds of the entry family
11655 -- (which may depend on discriminants). The element type is void, since we
11656 -- only need the bounds information for determining the entry index. Note
11657 -- that the use of an anonymous array would normally be illegal in this
11658 -- context, but this is a parser check, and the semantics is quite prepared
11659 -- to handle such a case.
11661 -- The _Size field is present only if a Storage_Size pragma appears in the
11662 -- task definition. The expression captures the argument that was present
11663 -- in the pragma, and is used to override the task stack size otherwise
11664 -- associated with the task type.
11666 -- The _Secondary_Stack_Size field is present only the task entity has a
11667 -- Secondary_Stack_Size rep item. It will be filled at the freeze point,
11668 -- when the record init proc is built, to capture the expression of the
11669 -- rep item (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot
11670 -- be filled here since aspect evaluations are delayed till the freeze
11671 -- point.
11673 -- The _Priority field is present only if the task entity has a Priority or
11674 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11675 -- definition clause). It will be filled at the freeze point, when the
11676 -- record init proc is built, to capture the expression of the rep item
11677 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11678 -- here since aspect evaluations are delayed till the freeze point.
11680 -- The _Task_Info field is present only if a Task_Info pragma appears in
11681 -- the task definition. The expression captures the argument that was
11682 -- present in the pragma, and is used to provide the Task_Image parameter
11683 -- to the call to Create_Task.
11685 -- The _CPU field is present only if the task entity has a CPU rep item
11686 -- (pragma, aspect specification or attribute definition clause). It will
11687 -- be filled at the freeze point, when the record init proc is built, to
11688 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11689 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11690 -- are delayed till the freeze point.
11692 -- The _Relative_Deadline field is present only if a Relative_Deadline
11693 -- pragma appears in the task definition. The expression captures the
11694 -- argument that was present in the pragma, and is used to provide the
11695 -- Relative_Deadline parameter to the call to Create_Task.
11697 -- The _Domain field is present only if the task entity has a
11698 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11699 -- definition clause). It will be filled at the freeze point, when the
11700 -- record init proc is built, to capture the expression of the rep item
11701 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11702 -- here since aspect evaluations are delayed till the freeze point.
11704 -- When a task is declared, an instance of the task value record is
11705 -- created. The elaboration of this declaration creates the correct bounds
11706 -- for the entry families, and also evaluates the size, priority, and
11707 -- task_Info expressions if needed. The initialization routine for the task
11708 -- type itself then calls Create_Task with appropriate parameters to
11709 -- initialize the value of the Task_Id field.
11711 -- Note: the address of this record is passed as the "Discriminants"
11712 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11713 -- body procedure, it does not matter that it does not quite match the
11714 -- GNARLI model of what is being passed (the record contains more than just
11715 -- the discriminants, but the discriminants can be found from the record
11716 -- value).
11718 -- The Entity_Id for this created record type is placed in the
11719 -- Corresponding_Record_Type field of the associated task type entity.
11721 -- Next we create a procedure specification for the task body procedure:
11723 -- procedure taskB (_Task : access taskV);
11725 -- Note that this must come after the record type declaration, since
11726 -- the spec refers to this type. It turns out that the initialization
11727 -- procedure for the value type references the task body spec, but that's
11728 -- fine, since it won't be generated till the freeze point for the type,
11729 -- which is certainly after the task body spec declaration.
11731 -- Finally, we set the task index value field of the entry attribute in
11732 -- the case of a simple entry.
11734 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11735 Loc : constant Source_Ptr := Sloc (N);
11736 TaskId : constant Entity_Id := Defining_Identifier (N);
11737 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11738 Tasknm : constant Name_Id := Chars (Tasktyp);
11739 Taskdef : constant Node_Id := Task_Definition (N);
11741 Body_Decl : Node_Id;
11742 Cdecls : List_Id;
11743 Decl_Stack : Node_Id;
11744 Decl_SS : Node_Id;
11745 Elab_Decl : Node_Id;
11746 Ent_Stack : Entity_Id;
11747 Proc_Spec : Node_Id;
11748 Rec_Decl : Node_Id;
11749 Rec_Ent : Entity_Id;
11750 Size_Decl : Entity_Id;
11751 Task_Size : Node_Id;
11753 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11754 -- Searches the task definition T for the first occurrence of the pragma
11755 -- Relative Deadline. The caller has ensured that the pragma is present
11756 -- in the task definition. Note that this routine cannot be implemented
11757 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11758 -- not chained because their expansion into a procedure call statement
11759 -- would cause a break in the chain.
11761 ----------------------------------
11762 -- Get_Relative_Deadline_Pragma --
11763 ----------------------------------
11765 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11766 N : Node_Id;
11768 begin
11769 N := First (Visible_Declarations (T));
11770 while Present (N) loop
11771 if Nkind (N) = N_Pragma
11772 and then Pragma_Name (N) = Name_Relative_Deadline
11773 then
11774 return N;
11775 end if;
11777 Next (N);
11778 end loop;
11780 N := First (Private_Declarations (T));
11781 while Present (N) loop
11782 if Nkind (N) = N_Pragma
11783 and then Pragma_Name (N) = Name_Relative_Deadline
11784 then
11785 return N;
11786 end if;
11788 Next (N);
11789 end loop;
11791 raise Program_Error;
11792 end Get_Relative_Deadline_Pragma;
11794 -- Start of processing for Expand_N_Task_Type_Declaration
11796 begin
11797 -- If already expanded, nothing to do
11799 if Present (Corresponding_Record_Type (Tasktyp)) then
11800 return;
11801 end if;
11803 -- Here we will do the expansion
11805 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11807 Rec_Ent := Defining_Identifier (Rec_Decl);
11808 Cdecls := Component_Items (Component_List
11809 (Type_Definition (Rec_Decl)));
11811 Qualify_Entity_Names (N);
11813 -- First create the elaboration variable
11815 Elab_Decl :=
11816 Make_Object_Declaration (Loc,
11817 Defining_Identifier =>
11818 Make_Defining_Identifier (Sloc (Tasktyp),
11819 Chars => New_External_Name (Tasknm, 'E')),
11820 Aliased_Present => True,
11821 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
11822 Expression => New_Occurrence_Of (Standard_False, Loc));
11824 Insert_After (N, Elab_Decl);
11826 -- Next create the declaration of the size variable (tasknmZ)
11828 Set_Storage_Size_Variable (Tasktyp,
11829 Make_Defining_Identifier (Sloc (Tasktyp),
11830 Chars => New_External_Name (Tasknm, 'Z')));
11832 if Present (Taskdef)
11833 and then Has_Storage_Size_Pragma (Taskdef)
11834 and then
11835 Is_OK_Static_Expression
11836 (Expression
11837 (First (Pragma_Argument_Associations
11838 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11839 then
11840 Size_Decl :=
11841 Make_Object_Declaration (Loc,
11842 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11843 Object_Definition =>
11844 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11845 Expression =>
11846 Convert_To (RTE (RE_Size_Type),
11847 Relocate_Node
11848 (Expression (First (Pragma_Argument_Associations
11849 (Get_Rep_Pragma
11850 (TaskId, Name_Storage_Size)))))));
11852 else
11853 Size_Decl :=
11854 Make_Object_Declaration (Loc,
11855 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11856 Object_Definition =>
11857 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11858 Expression =>
11859 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
11860 end if;
11862 Insert_After (Elab_Decl, Size_Decl);
11864 -- Next build the rest of the corresponding record declaration. This is
11865 -- done last, since the corresponding record initialization procedure
11866 -- will reference the previously created entities.
11868 -- Fill in the component declarations -- first the _Task_Id field
11870 Append_To (Cdecls,
11871 Make_Component_Declaration (Loc,
11872 Defining_Identifier =>
11873 Make_Defining_Identifier (Loc, Name_uTask_Id),
11874 Component_Definition =>
11875 Make_Component_Definition (Loc,
11876 Aliased_Present => False,
11877 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
11878 Loc))));
11880 -- Declare static ATCB (that is, created by the expander) if we are
11881 -- using the Restricted run time.
11883 if Restricted_Profile then
11884 Append_To (Cdecls,
11885 Make_Component_Declaration (Loc,
11886 Defining_Identifier =>
11887 Make_Defining_Identifier (Loc, Name_uATCB),
11889 Component_Definition =>
11890 Make_Component_Definition (Loc,
11891 Aliased_Present => True,
11892 Subtype_Indication => Make_Subtype_Indication (Loc,
11893 Subtype_Mark =>
11894 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11896 Constraint =>
11897 Make_Index_Or_Discriminant_Constraint (Loc,
11898 Constraints =>
11899 New_List (Make_Integer_Literal (Loc, 0)))))));
11901 end if;
11903 -- Declare static stack (that is, created by the expander) if we are
11904 -- using the Restricted run time on a bare board configuration.
11906 if Restricted_Profile and then Preallocated_Stacks_On_Target then
11908 -- First we need to extract the appropriate stack size
11910 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11912 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11913 declare
11914 Expr_N : constant Node_Id :=
11915 Expression (First (
11916 Pragma_Argument_Associations (
11917 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11918 Etyp : constant Entity_Id := Etype (Expr_N);
11919 P : constant Node_Id := Parent (Expr_N);
11921 begin
11922 -- The stack is defined inside the corresponding record.
11923 -- Therefore if the size of the stack is set by means of
11924 -- a discriminant, we must reference the discriminant of the
11925 -- corresponding record type.
11927 if Nkind (Expr_N) in N_Has_Entity
11928 and then Present (Discriminal_Link (Entity (Expr_N)))
11929 then
11930 Task_Size :=
11931 New_Occurrence_Of
11932 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11933 Loc);
11934 Set_Parent (Task_Size, P);
11935 Set_Etype (Task_Size, Etyp);
11936 Set_Analyzed (Task_Size);
11938 else
11939 Task_Size := New_Copy_Tree (Expr_N);
11940 end if;
11941 end;
11943 else
11944 Task_Size :=
11945 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
11946 end if;
11948 Decl_Stack := Make_Component_Declaration (Loc,
11949 Defining_Identifier => Ent_Stack,
11951 Component_Definition =>
11952 Make_Component_Definition (Loc,
11953 Aliased_Present => True,
11954 Subtype_Indication => Make_Subtype_Indication (Loc,
11955 Subtype_Mark =>
11956 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11958 Constraint =>
11959 Make_Index_Or_Discriminant_Constraint (Loc,
11960 Constraints => New_List (Make_Range (Loc,
11961 Low_Bound => Make_Integer_Literal (Loc, 1),
11962 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11963 Task_Size)))))));
11965 Append_To (Cdecls, Decl_Stack);
11967 -- The appropriate alignment for the stack is ensured by the run-time
11968 -- code in charge of task creation.
11970 end if;
11972 -- Declare a static secondary stack if the conditions for a statically
11973 -- generated stack are met.
11975 if Create_Secondary_Stack_For_Task (TaskId) then
11976 declare
11977 Size_Expr : constant Node_Id :=
11978 Expression (First (
11979 Pragma_Argument_Associations (
11980 Get_Rep_Pragma (TaskId,
11981 Name_Secondary_Stack_Size))));
11983 Stack_Size : Node_Id;
11985 begin
11986 -- The secondary stack is defined inside the corresponding
11987 -- record. Therefore if the size of the stack is set by means
11988 -- of a discriminant, we must reference the discriminant of the
11989 -- corresponding record type.
11991 if Nkind (Size_Expr) in N_Has_Entity
11992 and then Present (Discriminal_Link (Entity (Size_Expr)))
11993 then
11994 Stack_Size :=
11995 New_Occurrence_Of
11996 (CR_Discriminant (Discriminal_Link (Entity (Size_Expr))),
11997 Loc);
11998 Set_Parent (Stack_Size, Parent (Size_Expr));
11999 Set_Etype (Stack_Size, Etype (Size_Expr));
12000 Set_Analyzed (Stack_Size);
12002 else
12003 Stack_Size := New_Copy_Tree (Size_Expr);
12004 end if;
12006 -- Create the secondary stack for the task
12008 Decl_SS :=
12009 Make_Component_Declaration (Loc,
12010 Defining_Identifier =>
12011 Make_Defining_Identifier (Loc, Name_uSecondary_Stack),
12012 Component_Definition =>
12013 Make_Component_Definition (Loc,
12014 Aliased_Present => True,
12015 Subtype_Indication =>
12016 Make_Subtype_Indication (Loc,
12017 Subtype_Mark =>
12018 New_Occurrence_Of (RTE (RE_SS_Stack), Loc),
12019 Constraint =>
12020 Make_Index_Or_Discriminant_Constraint (Loc,
12021 Constraints => New_List (
12022 Convert_To (RTE (RE_Size_Type),
12023 Stack_Size))))));
12025 Append_To (Cdecls, Decl_SS);
12026 end;
12027 end if;
12029 -- Add components for entry families
12031 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
12033 -- Add the _Priority component if a Interrupt_Priority or Priority rep
12034 -- item is present.
12036 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
12037 Append_To (Cdecls,
12038 Make_Component_Declaration (Loc,
12039 Defining_Identifier =>
12040 Make_Defining_Identifier (Loc, Name_uPriority),
12041 Component_Definition =>
12042 Make_Component_Definition (Loc,
12043 Aliased_Present => False,
12044 Subtype_Indication =>
12045 New_Occurrence_Of (Standard_Integer, Loc))));
12046 end if;
12048 -- Add the _Size component if a Storage_Size pragma is present
12050 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12051 Append_To (Cdecls,
12052 Make_Component_Declaration (Loc,
12053 Defining_Identifier =>
12054 Make_Defining_Identifier (Loc, Name_uSize),
12056 Component_Definition =>
12057 Make_Component_Definition (Loc,
12058 Aliased_Present => False,
12059 Subtype_Indication =>
12060 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
12062 Expression =>
12063 Convert_To (RTE (RE_Size_Type),
12064 New_Copy_Tree (
12065 Expression (First (
12066 Pragma_Argument_Associations (
12067 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
12068 end if;
12070 -- Add the _Secondary_Stack_Size component if a Secondary_Stack_Size
12071 -- pragma is present.
12073 if Has_Rep_Pragma
12074 (TaskId, Name_Secondary_Stack_Size, Check_Parents => False)
12075 then
12076 Append_To (Cdecls,
12077 Make_Component_Declaration (Loc,
12078 Defining_Identifier =>
12079 Make_Defining_Identifier (Loc, Name_uSecondary_Stack_Size),
12081 Component_Definition =>
12082 Make_Component_Definition (Loc,
12083 Aliased_Present => False,
12084 Subtype_Indication =>
12085 New_Occurrence_Of (RTE (RE_Size_Type), Loc))));
12086 end if;
12088 -- Add the _Task_Info component if a Task_Info pragma is present
12090 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
12091 Append_To (Cdecls,
12092 Make_Component_Declaration (Loc,
12093 Defining_Identifier =>
12094 Make_Defining_Identifier (Loc, Name_uTask_Info),
12096 Component_Definition =>
12097 Make_Component_Definition (Loc,
12098 Aliased_Present => False,
12099 Subtype_Indication =>
12100 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12102 Expression => New_Copy (
12103 Expression (First (
12104 Pragma_Argument_Associations (
12105 Get_Rep_Pragma
12106 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12107 end if;
12109 -- Add the _CPU component if a CPU rep item is present
12111 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12112 Append_To (Cdecls,
12113 Make_Component_Declaration (Loc,
12114 Defining_Identifier =>
12115 Make_Defining_Identifier (Loc, Name_uCPU),
12117 Component_Definition =>
12118 Make_Component_Definition (Loc,
12119 Aliased_Present => False,
12120 Subtype_Indication =>
12121 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12122 end if;
12124 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12125 -- present. If we are using a restricted run time this component will
12126 -- not be added (deadlines are not allowed by the Ravenscar profile),
12127 -- unless the task dispatching policy is EDF (for GNAT_Ravenscar_EDF
12128 -- profile).
12130 if (not Restricted_Profile or else Task_Dispatching_Policy = 'E')
12131 and then Present (Taskdef)
12132 and then Has_Relative_Deadline_Pragma (Taskdef)
12133 then
12134 Append_To (Cdecls,
12135 Make_Component_Declaration (Loc,
12136 Defining_Identifier =>
12137 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12139 Component_Definition =>
12140 Make_Component_Definition (Loc,
12141 Aliased_Present => False,
12142 Subtype_Indication =>
12143 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12145 Expression =>
12146 Convert_To (RTE (RE_Time_Span),
12147 New_Copy_Tree (
12148 Expression (First (
12149 Pragma_Argument_Associations (
12150 Get_Relative_Deadline_Pragma (Taskdef))))))));
12151 end if;
12153 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12154 -- item is present. If we are using a restricted run time this component
12155 -- will not be added (dispatching domains are not allowed by the
12156 -- Ravenscar profile).
12158 if not Restricted_Profile
12159 and then
12160 Has_Rep_Item
12161 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12162 then
12163 Append_To (Cdecls,
12164 Make_Component_Declaration (Loc,
12165 Defining_Identifier =>
12166 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12168 Component_Definition =>
12169 Make_Component_Definition (Loc,
12170 Aliased_Present => False,
12171 Subtype_Indication =>
12172 New_Occurrence_Of
12173 (RTE (RE_Dispatching_Domain_Access), Loc))));
12174 end if;
12176 Insert_After (Size_Decl, Rec_Decl);
12178 -- Analyze the record declaration immediately after construction,
12179 -- because the initialization procedure is needed for single task
12180 -- declarations before the next entity is analyzed.
12182 Analyze (Rec_Decl);
12184 -- Create the declaration of the task body procedure
12186 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12187 Body_Decl :=
12188 Make_Subprogram_Declaration (Loc,
12189 Specification => Proc_Spec);
12190 Set_Is_Task_Body_Procedure (Body_Decl);
12192 Insert_After (Rec_Decl, Body_Decl);
12194 -- The subprogram does not comes from source, so we have to indicate the
12195 -- need for debugging information explicitly.
12197 if Comes_From_Source (Original_Node (N)) then
12198 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12199 end if;
12201 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12202 -- the corresponding record has been frozen.
12204 if Ada_Version >= Ada_2005 then
12205 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12206 end if;
12208 -- Ada 2005 (AI-345): We must defer freezing to allow further
12209 -- declaration of primitive subprograms covering task interfaces
12211 if Ada_Version <= Ada_95 then
12213 -- Now we can freeze the corresponding record. This needs manually
12214 -- freezing, since it is really part of the task type, and the task
12215 -- type is frozen at this stage. We of course need the initialization
12216 -- procedure for this corresponding record type and we won't get it
12217 -- in time if we don't freeze now.
12219 declare
12220 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
12221 begin
12222 if Is_Non_Empty_List (L) then
12223 Insert_List_After (Body_Decl, L);
12224 end if;
12225 end;
12226 end if;
12228 -- Complete the expansion of access types to the current task type, if
12229 -- any were declared.
12231 Expand_Previous_Access_Type (Tasktyp);
12233 -- Create wrappers for entries that have contract cases, preconditions
12234 -- and postconditions.
12236 declare
12237 Ent : Entity_Id;
12239 begin
12240 Ent := First_Entity (Tasktyp);
12241 while Present (Ent) loop
12242 if Ekind_In (Ent, E_Entry, E_Entry_Family) then
12243 Build_Contract_Wrapper (Ent, N);
12244 end if;
12246 Next_Entity (Ent);
12247 end loop;
12248 end;
12249 end Expand_N_Task_Type_Declaration;
12251 -------------------------------
12252 -- Expand_N_Timed_Entry_Call --
12253 -------------------------------
12255 -- A timed entry call in normal case is not implemented using ATC mechanism
12256 -- anymore for efficiency reason.
12258 -- select
12259 -- T.E;
12260 -- S1;
12261 -- or
12262 -- delay D;
12263 -- S2;
12264 -- end select;
12266 -- is expanded as follows:
12268 -- 1) When T.E is a task entry_call;
12270 -- declare
12271 -- B : Boolean;
12272 -- X : Task_Entry_Index := <entry index>;
12273 -- DX : Duration := To_Duration (D);
12274 -- M : Delay_Mode := <discriminant>;
12275 -- P : parms := (parm, parm, parm);
12277 -- begin
12278 -- Timed_Protected_Entry_Call
12279 -- (<acceptor-task>, X, P'Address, DX, M, B);
12280 -- if B then
12281 -- S1;
12282 -- else
12283 -- S2;
12284 -- end if;
12285 -- end;
12287 -- 2) When T.E is a protected entry_call;
12289 -- declare
12290 -- B : Boolean;
12291 -- X : Protected_Entry_Index := <entry index>;
12292 -- DX : Duration := To_Duration (D);
12293 -- M : Delay_Mode := <discriminant>;
12294 -- P : parms := (parm, parm, parm);
12296 -- begin
12297 -- Timed_Protected_Entry_Call
12298 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12299 -- if B then
12300 -- S1;
12301 -- else
12302 -- S2;
12303 -- end if;
12304 -- end;
12306 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12307 -- is no delay and the triggering statements are executed. We first
12308 -- determine the kind of the triggering call and then execute a
12309 -- synchronized operation or a direct call.
12311 -- declare
12312 -- B : Boolean := False;
12313 -- C : Ada.Tags.Prim_Op_Kind;
12314 -- DX : Duration := To_Duration (D)
12315 -- K : Ada.Tags.Tagged_Kind :=
12316 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12317 -- M : Integer :=...;
12318 -- P : Parameters := (Param1 .. ParamN);
12319 -- S : Integer;
12321 -- begin
12322 -- if K = Ada.Tags.TK_Limited_Tagged
12323 -- or else K = Ada.Tags.TK_Tagged
12324 -- then
12325 -- <dispatching-call>;
12326 -- B := True;
12328 -- else
12329 -- S :=
12330 -- Ada.Tags.Get_Offset_Index
12331 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12333 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12335 -- if C = POK_Protected_Entry
12336 -- or else C = POK_Task_Entry
12337 -- then
12338 -- Param1 := P.Param1;
12339 -- ...
12340 -- ParamN := P.ParamN;
12341 -- end if;
12343 -- if B then
12344 -- if C = POK_Procedure
12345 -- or else C = POK_Protected_Procedure
12346 -- or else C = POK_Task_Procedure
12347 -- then
12348 -- <dispatching-call>;
12349 -- end if;
12350 -- end if;
12351 -- end if;
12353 -- if B then
12354 -- <triggering-statements>
12355 -- else
12356 -- <timed-statements>
12357 -- end if;
12358 -- end;
12360 -- The triggering statement and the sequence of timed statements have not
12361 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12362 -- global references if within an instantiation.
12364 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12365 Loc : constant Source_Ptr := Sloc (N);
12367 Actuals : List_Id;
12368 Blk_Typ : Entity_Id;
12369 Call : Node_Id;
12370 Call_Ent : Entity_Id;
12371 Conc_Typ_Stmts : List_Id;
12372 Concval : Node_Id := Empty; -- init to avoid warning
12373 D_Alt : constant Node_Id := Delay_Alternative (N);
12374 D_Conv : Node_Id;
12375 D_Disc : Node_Id;
12376 D_Stat : Node_Id := Delay_Statement (D_Alt);
12377 D_Stats : List_Id;
12378 D_Type : Entity_Id;
12379 Decls : List_Id;
12380 Dummy : Node_Id;
12381 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12382 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12383 E_Stats : List_Id;
12384 Ename : Node_Id;
12385 Formals : List_Id;
12386 Index : Node_Id;
12387 Is_Disp_Select : Boolean;
12388 Lim_Typ_Stmts : List_Id;
12389 N_Stats : List_Id;
12390 Obj : Entity_Id;
12391 Param : Node_Id;
12392 Params : List_Id;
12393 Stmt : Node_Id;
12394 Stmts : List_Id;
12395 Unpack : List_Id;
12397 B : Entity_Id; -- Call status flag
12398 C : Entity_Id; -- Call kind
12399 D : Entity_Id; -- Delay
12400 K : Entity_Id; -- Tagged kind
12401 M : Entity_Id; -- Delay mode
12402 P : Entity_Id; -- Parameter block
12403 S : Entity_Id; -- Primitive operation slot
12405 -- Start of processing for Expand_N_Timed_Entry_Call
12407 begin
12408 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12409 -- was already reported on spec, so do not attempt to expand the call.
12411 if Restriction_Active (No_Select_Statements) then
12412 return;
12413 end if;
12415 Process_Statements_For_Controlled_Objects (E_Alt);
12416 Process_Statements_For_Controlled_Objects (D_Alt);
12418 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12420 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12421 -- may wrap them in blocks.
12423 E_Stats := Statements (E_Alt);
12424 D_Stats := Statements (D_Alt);
12426 -- The arguments in the call may require dynamic allocation, and the
12427 -- call statement may have been transformed into a block. The block
12428 -- may contain additional declarations for internal entities, and the
12429 -- original call is found by sequential search.
12431 if Nkind (E_Call) = N_Block_Statement then
12432 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12433 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12434 N_Entry_Call_Statement)
12435 loop
12436 Next (E_Call);
12437 end loop;
12438 end if;
12440 Is_Disp_Select :=
12441 Ada_Version >= Ada_2005
12442 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12444 if Is_Disp_Select then
12445 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12446 Decls := New_List;
12448 Stmts := New_List;
12450 -- Generate:
12451 -- B : Boolean := False;
12453 B := Build_B (Loc, Decls);
12455 -- Generate:
12456 -- C : Ada.Tags.Prim_Op_Kind;
12458 C := Build_C (Loc, Decls);
12460 -- Because the analysis of all statements was disabled, manually
12461 -- analyze the delay statement.
12463 Analyze (D_Stat);
12464 D_Stat := Original_Node (D_Stat);
12466 else
12467 -- Build an entry call using Simple_Entry_Call
12469 Extract_Entry (E_Call, Concval, Ename, Index);
12470 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12472 Decls := Declarations (E_Call);
12473 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12475 if No (Decls) then
12476 Decls := New_List;
12477 end if;
12479 -- Generate:
12480 -- B : Boolean;
12482 B := Make_Defining_Identifier (Loc, Name_uB);
12484 Prepend_To (Decls,
12485 Make_Object_Declaration (Loc,
12486 Defining_Identifier => B,
12487 Object_Definition =>
12488 New_Occurrence_Of (Standard_Boolean, Loc)));
12489 end if;
12491 -- Duration and mode processing
12493 D_Type := Base_Type (Etype (Expression (D_Stat)));
12495 -- Use the type of the delay expression (Calendar or Real_Time) to
12496 -- generate the appropriate conversion.
12498 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12499 D_Disc := Make_Integer_Literal (Loc, 0);
12500 D_Conv := Relocate_Node (Expression (D_Stat));
12502 elsif Is_RTE (D_Type, RO_CA_Time) then
12503 D_Disc := Make_Integer_Literal (Loc, 1);
12504 D_Conv :=
12505 Make_Function_Call (Loc,
12506 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12507 Parameter_Associations =>
12508 New_List (New_Copy (Expression (D_Stat))));
12510 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12511 D_Disc := Make_Integer_Literal (Loc, 2);
12512 D_Conv :=
12513 Make_Function_Call (Loc,
12514 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12515 Parameter_Associations =>
12516 New_List (New_Copy (Expression (D_Stat))));
12517 end if;
12519 D := Make_Temporary (Loc, 'D');
12521 -- Generate:
12522 -- D : Duration;
12524 Append_To (Decls,
12525 Make_Object_Declaration (Loc,
12526 Defining_Identifier => D,
12527 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12529 M := Make_Temporary (Loc, 'M');
12531 -- Generate:
12532 -- M : Integer := (0 | 1 | 2);
12534 Append_To (Decls,
12535 Make_Object_Declaration (Loc,
12536 Defining_Identifier => M,
12537 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12538 Expression => D_Disc));
12540 -- Do the assignment at this stage only because the evaluation of the
12541 -- expression must not occur before (see ACVC C97302A).
12543 Append_To (Stmts,
12544 Make_Assignment_Statement (Loc,
12545 Name => New_Occurrence_Of (D, Loc),
12546 Expression => D_Conv));
12548 -- Parameter block processing
12550 -- Manually create the parameter block for dispatching calls. In the
12551 -- case of entries, the block has already been created during the call
12552 -- to Build_Simple_Entry_Call.
12554 if Is_Disp_Select then
12556 -- Tagged kind processing, generate:
12557 -- K : Ada.Tags.Tagged_Kind :=
12558 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12560 K := Build_K (Loc, Decls, Obj);
12562 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12563 P :=
12564 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12566 -- Dispatch table slot processing, generate:
12567 -- S : Integer;
12569 S := Build_S (Loc, Decls);
12571 -- Generate:
12572 -- S := Ada.Tags.Get_Offset_Index
12573 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12575 Conc_Typ_Stmts :=
12576 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12578 -- Generate:
12579 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12581 -- where Obj is the controlling formal parameter, S is the dispatch
12582 -- table slot number of the dispatching operation, P is the wrapped
12583 -- parameter block, D is the duration, M is the duration mode, C is
12584 -- the call kind and B is the call status.
12586 Params := New_List;
12588 Append_To (Params, New_Copy_Tree (Obj));
12589 Append_To (Params, New_Occurrence_Of (S, Loc));
12590 Append_To (Params,
12591 Make_Attribute_Reference (Loc,
12592 Prefix => New_Occurrence_Of (P, Loc),
12593 Attribute_Name => Name_Address));
12594 Append_To (Params, New_Occurrence_Of (D, Loc));
12595 Append_To (Params, New_Occurrence_Of (M, Loc));
12596 Append_To (Params, New_Occurrence_Of (C, Loc));
12597 Append_To (Params, New_Occurrence_Of (B, Loc));
12599 Append_To (Conc_Typ_Stmts,
12600 Make_Procedure_Call_Statement (Loc,
12601 Name =>
12602 New_Occurrence_Of
12603 (Find_Prim_Op
12604 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12605 Parameter_Associations => Params));
12607 -- Generate:
12608 -- if C = POK_Protected_Entry
12609 -- or else C = POK_Task_Entry
12610 -- then
12611 -- Param1 := P.Param1;
12612 -- ...
12613 -- ParamN := P.ParamN;
12614 -- end if;
12616 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12618 -- Generate the if statement only when the packed parameters need
12619 -- explicit assignments to their corresponding actuals.
12621 if Present (Unpack) then
12622 Append_To (Conc_Typ_Stmts,
12623 Make_Implicit_If_Statement (N,
12625 Condition =>
12626 Make_Or_Else (Loc,
12627 Left_Opnd =>
12628 Make_Op_Eq (Loc,
12629 Left_Opnd => New_Occurrence_Of (C, Loc),
12630 Right_Opnd =>
12631 New_Occurrence_Of
12632 (RTE (RE_POK_Protected_Entry), Loc)),
12634 Right_Opnd =>
12635 Make_Op_Eq (Loc,
12636 Left_Opnd => New_Occurrence_Of (C, Loc),
12637 Right_Opnd =>
12638 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12640 Then_Statements => Unpack));
12641 end if;
12643 -- Generate:
12645 -- if B then
12646 -- if C = POK_Procedure
12647 -- or else C = POK_Protected_Procedure
12648 -- or else C = POK_Task_Procedure
12649 -- then
12650 -- <dispatching-call>
12651 -- end if;
12652 -- end if;
12654 N_Stats := New_List (
12655 Make_Implicit_If_Statement (N,
12656 Condition =>
12657 Make_Or_Else (Loc,
12658 Left_Opnd =>
12659 Make_Op_Eq (Loc,
12660 Left_Opnd => New_Occurrence_Of (C, Loc),
12661 Right_Opnd =>
12662 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12664 Right_Opnd =>
12665 Make_Or_Else (Loc,
12666 Left_Opnd =>
12667 Make_Op_Eq (Loc,
12668 Left_Opnd => New_Occurrence_Of (C, Loc),
12669 Right_Opnd =>
12670 New_Occurrence_Of (RTE (
12671 RE_POK_Protected_Procedure), Loc)),
12672 Right_Opnd =>
12673 Make_Op_Eq (Loc,
12674 Left_Opnd => New_Occurrence_Of (C, Loc),
12675 Right_Opnd =>
12676 New_Occurrence_Of
12677 (RTE (RE_POK_Task_Procedure), Loc)))),
12679 Then_Statements => New_List (E_Call)));
12681 Append_To (Conc_Typ_Stmts,
12682 Make_Implicit_If_Statement (N,
12683 Condition => New_Occurrence_Of (B, Loc),
12684 Then_Statements => N_Stats));
12686 -- Generate:
12687 -- <dispatching-call>;
12688 -- B := True;
12690 Lim_Typ_Stmts :=
12691 New_List (New_Copy_Tree (E_Call),
12692 Make_Assignment_Statement (Loc,
12693 Name => New_Occurrence_Of (B, Loc),
12694 Expression => New_Occurrence_Of (Standard_True, Loc)));
12696 -- Generate:
12697 -- if K = Ada.Tags.TK_Limited_Tagged
12698 -- or else K = Ada.Tags.TK_Tagged
12699 -- then
12700 -- Lim_Typ_Stmts
12701 -- else
12702 -- Conc_Typ_Stmts
12703 -- end if;
12705 Append_To (Stmts,
12706 Make_Implicit_If_Statement (N,
12707 Condition => Build_Dispatching_Tag_Check (K, N),
12708 Then_Statements => Lim_Typ_Stmts,
12709 Else_Statements => Conc_Typ_Stmts));
12711 -- Generate:
12713 -- if B then
12714 -- <triggering-statements>
12715 -- else
12716 -- <timed-statements>
12717 -- end if;
12719 Append_To (Stmts,
12720 Make_Implicit_If_Statement (N,
12721 Condition => New_Occurrence_Of (B, Loc),
12722 Then_Statements => E_Stats,
12723 Else_Statements => D_Stats));
12725 else
12726 -- Simple case of a nondispatching trigger. Skip assignments to
12727 -- temporaries created for in-out parameters.
12729 -- This makes unwarranted assumptions about the shape of the expanded
12730 -- tree for the call, and should be cleaned up ???
12732 Stmt := First (Stmts);
12733 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12734 Next (Stmt);
12735 end loop;
12737 -- Do the assignment at this stage only because the evaluation
12738 -- of the expression must not occur before (see ACVC C97302A).
12740 Insert_Before (Stmt,
12741 Make_Assignment_Statement (Loc,
12742 Name => New_Occurrence_Of (D, Loc),
12743 Expression => D_Conv));
12745 Call := Stmt;
12746 Params := Parameter_Associations (Call);
12748 -- For a protected type, we build a Timed_Protected_Entry_Call
12750 if Is_Protected_Type (Etype (Concval)) then
12752 -- Create a new call statement
12754 Param := First (Params);
12755 while Present (Param)
12756 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12757 loop
12758 Next (Param);
12759 end loop;
12761 Dummy := Remove_Next (Next (Param));
12763 -- Remove garbage is following the Cancel_Param if present
12765 Dummy := Next (Param);
12767 -- Remove the mode of the Protected_Entry_Call call, then remove
12768 -- the Communication_Block of the Protected_Entry_Call call, and
12769 -- finally add Duration and a Delay_Mode parameter
12771 pragma Assert (Present (Param));
12772 Rewrite (Param, New_Occurrence_Of (D, Loc));
12774 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12776 -- Add a Boolean flag for successful entry call
12778 Append_To (Params, New_Occurrence_Of (B, Loc));
12780 case Corresponding_Runtime_Package (Etype (Concval)) is
12781 when System_Tasking_Protected_Objects_Entries =>
12782 Rewrite (Call,
12783 Make_Procedure_Call_Statement (Loc,
12784 Name =>
12785 New_Occurrence_Of
12786 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12787 Parameter_Associations => Params));
12789 when others =>
12790 raise Program_Error;
12791 end case;
12793 -- For the task case, build a Timed_Task_Entry_Call
12795 else
12796 -- Create a new call statement
12798 Append_To (Params, New_Occurrence_Of (D, Loc));
12799 Append_To (Params, New_Occurrence_Of (M, Loc));
12800 Append_To (Params, New_Occurrence_Of (B, Loc));
12802 Rewrite (Call,
12803 Make_Procedure_Call_Statement (Loc,
12804 Name =>
12805 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
12806 Parameter_Associations => Params));
12807 end if;
12809 Append_To (Stmts,
12810 Make_Implicit_If_Statement (N,
12811 Condition => New_Occurrence_Of (B, Loc),
12812 Then_Statements => E_Stats,
12813 Else_Statements => D_Stats));
12814 end if;
12816 Rewrite (N,
12817 Make_Block_Statement (Loc,
12818 Declarations => Decls,
12819 Handled_Statement_Sequence =>
12820 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12822 Analyze (N);
12823 end Expand_N_Timed_Entry_Call;
12825 ----------------------------------------
12826 -- Expand_Protected_Body_Declarations --
12827 ----------------------------------------
12829 procedure Expand_Protected_Body_Declarations
12830 (N : Node_Id;
12831 Spec_Id : Entity_Id)
12833 begin
12834 if No_Run_Time_Mode then
12835 Error_Msg_CRT ("protected body", N);
12836 return;
12838 elsif Expander_Active then
12840 -- Associate discriminals with the first subprogram or entry body to
12841 -- be expanded.
12843 if Present (First_Protected_Operation (Declarations (N))) then
12844 Set_Discriminals (Parent (Spec_Id));
12845 end if;
12846 end if;
12847 end Expand_Protected_Body_Declarations;
12849 -------------------------
12850 -- External_Subprogram --
12851 -------------------------
12853 function External_Subprogram (E : Entity_Id) return Entity_Id is
12854 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12856 begin
12857 -- The internal and external subprograms follow each other on the entity
12858 -- chain. Note that previously private operations had no separate
12859 -- external subprogram. We now create one in all cases, because a
12860 -- private operation may actually appear in an external call, through
12861 -- a 'Access reference used for a callback.
12863 -- If the operation is a function that returns an anonymous access type,
12864 -- the corresponding itype appears before the operation, and must be
12865 -- skipped.
12867 -- This mechanism is fragile, there should be a real link between the
12868 -- two versions of the operation, but there is no place to put it ???
12870 if Is_Access_Type (Next_Entity (Subp)) then
12871 return Next_Entity (Next_Entity (Subp));
12872 else
12873 return Next_Entity (Subp);
12874 end if;
12875 end External_Subprogram;
12877 ------------------------------
12878 -- Extract_Dispatching_Call --
12879 ------------------------------
12881 procedure Extract_Dispatching_Call
12882 (N : Node_Id;
12883 Call_Ent : out Entity_Id;
12884 Object : out Entity_Id;
12885 Actuals : out List_Id;
12886 Formals : out List_Id)
12888 Call_Nam : Node_Id;
12890 begin
12891 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12893 if Present (Original_Node (N)) then
12894 Call_Nam := Name (Original_Node (N));
12895 else
12896 Call_Nam := Name (N);
12897 end if;
12899 -- Retrieve the name of the dispatching procedure. It contains the
12900 -- dispatch table slot number.
12902 loop
12903 case Nkind (Call_Nam) is
12904 when N_Identifier =>
12905 exit;
12907 when N_Selected_Component =>
12908 Call_Nam := Selector_Name (Call_Nam);
12910 when others =>
12911 raise Program_Error;
12912 end case;
12913 end loop;
12915 Actuals := Parameter_Associations (N);
12916 Call_Ent := Entity (Call_Nam);
12917 Formals := Parameter_Specifications (Parent (Call_Ent));
12918 Object := First (Actuals);
12920 if Present (Original_Node (Object)) then
12921 Object := Original_Node (Object);
12922 end if;
12924 -- If the type of the dispatching object is an access type then return
12925 -- an explicit dereference of a copy of the object, and note that this
12926 -- is the controlling actual of the call.
12928 if Is_Access_Type (Etype (Object)) then
12929 Object :=
12930 Make_Explicit_Dereference (Sloc (N), New_Copy_Tree (Object));
12931 Analyze (Object);
12932 Set_Is_Controlling_Actual (Object);
12933 end if;
12934 end Extract_Dispatching_Call;
12936 -------------------
12937 -- Extract_Entry --
12938 -------------------
12940 procedure Extract_Entry
12941 (N : Node_Id;
12942 Concval : out Node_Id;
12943 Ename : out Node_Id;
12944 Index : out Node_Id)
12946 Nam : constant Node_Id := Name (N);
12948 begin
12949 -- For a simple entry, the name is a selected component, with the
12950 -- prefix being the task value, and the selector being the entry.
12952 if Nkind (Nam) = N_Selected_Component then
12953 Concval := Prefix (Nam);
12954 Ename := Selector_Name (Nam);
12955 Index := Empty;
12957 -- For a member of an entry family, the name is an indexed component
12958 -- where the prefix is a selected component, whose prefix in turn is
12959 -- the task value, and whose selector is the entry family. The single
12960 -- expression in the expressions list of the indexed component is the
12961 -- subscript for the family.
12963 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12964 Concval := Prefix (Prefix (Nam));
12965 Ename := Selector_Name (Prefix (Nam));
12966 Index := First (Expressions (Nam));
12967 end if;
12969 -- Through indirection, the type may actually be a limited view of a
12970 -- concurrent type. When compiling a call, the non-limited view of the
12971 -- type is visible.
12973 if From_Limited_With (Etype (Concval)) then
12974 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
12975 end if;
12976 end Extract_Entry;
12978 -------------------
12979 -- Family_Offset --
12980 -------------------
12982 function Family_Offset
12983 (Loc : Source_Ptr;
12984 Hi : Node_Id;
12985 Lo : Node_Id;
12986 Ttyp : Entity_Id;
12987 Cap : Boolean) return Node_Id
12989 Ityp : Entity_Id;
12990 Real_Hi : Node_Id;
12991 Real_Lo : Node_Id;
12993 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12994 -- If one of the bounds is a reference to a discriminant, replace with
12995 -- corresponding discriminal of type. Within the body of a task retrieve
12996 -- the renamed discriminant by simple visibility, using its generated
12997 -- name. Within a protected object, find the original discriminant and
12998 -- replace it with the discriminal of the current protected operation.
13000 ------------------------------
13001 -- Convert_Discriminant_Ref --
13002 ------------------------------
13004 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
13005 Loc : constant Source_Ptr := Sloc (Bound);
13006 B : Node_Id;
13007 D : Entity_Id;
13009 begin
13010 if Is_Entity_Name (Bound)
13011 and then Ekind (Entity (Bound)) = E_Discriminant
13012 then
13013 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
13014 B := Make_Identifier (Loc, Chars (Entity (Bound)));
13015 Find_Direct_Name (B);
13017 elsif Is_Protected_Type (Ttyp) then
13018 D := First_Discriminant (Ttyp);
13019 while Chars (D) /= Chars (Entity (Bound)) loop
13020 Next_Discriminant (D);
13021 end loop;
13023 B := New_Occurrence_Of (Discriminal (D), Loc);
13025 else
13026 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
13027 end if;
13029 elsif Nkind (Bound) = N_Attribute_Reference then
13030 return Bound;
13032 else
13033 B := New_Copy_Tree (Bound);
13034 end if;
13036 return
13037 Make_Attribute_Reference (Loc,
13038 Attribute_Name => Name_Pos,
13039 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
13040 Expressions => New_List (B));
13041 end Convert_Discriminant_Ref;
13043 -- Start of processing for Family_Offset
13045 begin
13046 Real_Hi := Convert_Discriminant_Ref (Hi);
13047 Real_Lo := Convert_Discriminant_Ref (Lo);
13049 if Cap then
13050 if Is_Task_Type (Ttyp) then
13051 Ityp := RTE (RE_Task_Entry_Index);
13052 else
13053 Ityp := RTE (RE_Protected_Entry_Index);
13054 end if;
13056 Real_Hi :=
13057 Make_Attribute_Reference (Loc,
13058 Prefix => New_Occurrence_Of (Ityp, Loc),
13059 Attribute_Name => Name_Min,
13060 Expressions => New_List (
13061 Real_Hi,
13062 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
13064 Real_Lo :=
13065 Make_Attribute_Reference (Loc,
13066 Prefix => New_Occurrence_Of (Ityp, Loc),
13067 Attribute_Name => Name_Max,
13068 Expressions => New_List (
13069 Real_Lo,
13070 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
13071 end if;
13073 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
13074 end Family_Offset;
13076 -----------------
13077 -- Family_Size --
13078 -----------------
13080 function Family_Size
13081 (Loc : Source_Ptr;
13082 Hi : Node_Id;
13083 Lo : Node_Id;
13084 Ttyp : Entity_Id;
13085 Cap : Boolean) return Node_Id
13087 Ityp : Entity_Id;
13089 begin
13090 if Is_Task_Type (Ttyp) then
13091 Ityp := RTE (RE_Task_Entry_Index);
13092 else
13093 Ityp := RTE (RE_Protected_Entry_Index);
13094 end if;
13096 return
13097 Make_Attribute_Reference (Loc,
13098 Prefix => New_Occurrence_Of (Ityp, Loc),
13099 Attribute_Name => Name_Max,
13100 Expressions => New_List (
13101 Make_Op_Add (Loc,
13102 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13103 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13104 Make_Integer_Literal (Loc, 0)));
13105 end Family_Size;
13107 ----------------------------
13108 -- Find_Enclosing_Context --
13109 ----------------------------
13111 procedure Find_Enclosing_Context
13112 (N : Node_Id;
13113 Context : out Node_Id;
13114 Context_Id : out Entity_Id;
13115 Context_Decls : out List_Id)
13117 begin
13118 -- Traverse the parent chain looking for an enclosing body, block,
13119 -- package or return statement.
13121 Context := Parent (N);
13122 while Present (Context) loop
13123 if Nkind_In (Context, N_Entry_Body,
13124 N_Extended_Return_Statement,
13125 N_Package_Body,
13126 N_Package_Declaration,
13127 N_Subprogram_Body,
13128 N_Task_Body)
13129 then
13130 exit;
13132 -- Do not consider block created to protect a list of statements with
13133 -- an Abort_Defer / Abort_Undefer_Direct pair.
13135 elsif Nkind (Context) = N_Block_Statement
13136 and then not Is_Abort_Block (Context)
13137 then
13138 exit;
13139 end if;
13141 Context := Parent (Context);
13142 end loop;
13144 pragma Assert (Present (Context));
13146 -- Extract the constituents of the context
13148 if Nkind (Context) = N_Extended_Return_Statement then
13149 Context_Decls := Return_Object_Declarations (Context);
13150 Context_Id := Return_Statement_Entity (Context);
13152 -- Package declarations and bodies use a common library-level activation
13153 -- chain or task master, therefore return the package declaration as the
13154 -- proper carrier for the appropriate flag.
13156 elsif Nkind (Context) = N_Package_Body then
13157 Context_Decls := Declarations (Context);
13158 Context_Id := Corresponding_Spec (Context);
13159 Context := Parent (Context_Id);
13161 if Nkind (Context) = N_Defining_Program_Unit_Name then
13162 Context := Parent (Parent (Context));
13163 else
13164 Context := Parent (Context);
13165 end if;
13167 elsif Nkind (Context) = N_Package_Declaration then
13168 Context_Decls := Visible_Declarations (Specification (Context));
13169 Context_Id := Defining_Unit_Name (Specification (Context));
13171 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13172 Context_Id := Defining_Identifier (Context_Id);
13173 end if;
13175 else
13176 if Nkind (Context) = N_Block_Statement then
13177 Context_Id := Entity (Identifier (Context));
13179 elsif Nkind (Context) = N_Entry_Body then
13180 Context_Id := Defining_Identifier (Context);
13182 elsif Nkind (Context) = N_Subprogram_Body then
13183 if Present (Corresponding_Spec (Context)) then
13184 Context_Id := Corresponding_Spec (Context);
13185 else
13186 Context_Id := Defining_Unit_Name (Specification (Context));
13188 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13189 Context_Id := Defining_Identifier (Context_Id);
13190 end if;
13191 end if;
13193 elsif Nkind (Context) = N_Task_Body then
13194 Context_Id := Corresponding_Spec (Context);
13196 else
13197 raise Program_Error;
13198 end if;
13200 Context_Decls := Declarations (Context);
13201 end if;
13203 pragma Assert (Present (Context_Id));
13204 pragma Assert (Present (Context_Decls));
13205 end Find_Enclosing_Context;
13207 -----------------------
13208 -- Find_Master_Scope --
13209 -----------------------
13211 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13212 S : Entity_Id;
13214 begin
13215 -- In Ada 2005, the master is the innermost enclosing scope that is not
13216 -- transient. If the enclosing block is the rewriting of a call or the
13217 -- scope is an extended return statement this is valid master. The
13218 -- master in an extended return is only used within the return, and is
13219 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13220 -- now before that overwriting occurs.
13222 S := Scope (E);
13224 if Ada_Version >= Ada_2005 then
13225 while Is_Internal (S) loop
13226 if Nkind (Parent (S)) = N_Block_Statement
13227 and then
13228 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
13229 then
13230 exit;
13232 elsif Ekind (S) = E_Return_Statement then
13233 exit;
13235 else
13236 S := Scope (S);
13237 end if;
13238 end loop;
13239 end if;
13241 return S;
13242 end Find_Master_Scope;
13244 -------------------------------
13245 -- First_Protected_Operation --
13246 -------------------------------
13248 function First_Protected_Operation (D : List_Id) return Node_Id is
13249 First_Op : Node_Id;
13251 begin
13252 First_Op := First (D);
13253 while Present (First_Op)
13254 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
13255 loop
13256 Next (First_Op);
13257 end loop;
13259 return First_Op;
13260 end First_Protected_Operation;
13262 ---------------------------------------
13263 -- Install_Private_Data_Declarations --
13264 ---------------------------------------
13266 procedure Install_Private_Data_Declarations
13267 (Loc : Source_Ptr;
13268 Spec_Id : Entity_Id;
13269 Conc_Typ : Entity_Id;
13270 Body_Nod : Node_Id;
13271 Decls : List_Id;
13272 Barrier : Boolean := False;
13273 Family : Boolean := False)
13275 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13276 Decl : Node_Id;
13277 Def : Node_Id;
13278 Insert_Node : Node_Id := Empty;
13279 Obj_Ent : Entity_Id;
13281 procedure Add (Decl : Node_Id);
13282 -- Add a single declaration after Insert_Node. If this is the first
13283 -- addition, Decl is added to the front of Decls and it becomes the
13284 -- insertion node.
13286 function Replace_Bound (Bound : Node_Id) return Node_Id;
13287 -- The bounds of an entry index may depend on discriminants, create a
13288 -- reference to the corresponding prival. Otherwise return a duplicate
13289 -- of the original bound.
13291 ---------
13292 -- Add --
13293 ---------
13295 procedure Add (Decl : Node_Id) is
13296 begin
13297 if No (Insert_Node) then
13298 Prepend_To (Decls, Decl);
13299 else
13300 Insert_After (Insert_Node, Decl);
13301 end if;
13303 Insert_Node := Decl;
13304 end Add;
13306 -------------------
13307 -- Replace_Bound --
13308 -------------------
13310 function Replace_Bound (Bound : Node_Id) return Node_Id is
13311 begin
13312 if Nkind (Bound) = N_Identifier
13313 and then Is_Discriminal (Entity (Bound))
13314 then
13315 return Make_Identifier (Loc, Chars (Entity (Bound)));
13316 else
13317 return Duplicate_Subexpr (Bound);
13318 end if;
13319 end Replace_Bound;
13321 -- Start of processing for Install_Private_Data_Declarations
13323 begin
13324 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13325 -- formal parameter _O, _object or _task depending on the context.
13327 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13329 -- Special processing of _O for barrier functions, protected entries
13330 -- and families.
13332 if Barrier
13333 or else
13334 (Is_Protected
13335 and then
13336 (Ekind (Spec_Id) = E_Entry
13337 or else Ekind (Spec_Id) = E_Entry_Family))
13338 then
13339 declare
13340 Conc_Rec : constant Entity_Id :=
13341 Corresponding_Record_Type (Conc_Typ);
13342 Typ_Id : constant Entity_Id :=
13343 Make_Defining_Identifier (Loc,
13344 New_External_Name (Chars (Conc_Rec), 'P'));
13345 begin
13346 -- Generate:
13347 -- type prot_typVP is access prot_typV;
13349 Decl :=
13350 Make_Full_Type_Declaration (Loc,
13351 Defining_Identifier => Typ_Id,
13352 Type_Definition =>
13353 Make_Access_To_Object_Definition (Loc,
13354 Subtype_Indication =>
13355 New_Occurrence_Of (Conc_Rec, Loc)));
13356 Add (Decl);
13358 -- Generate:
13359 -- _object : prot_typVP := prot_typV (_O);
13361 Decl :=
13362 Make_Object_Declaration (Loc,
13363 Defining_Identifier =>
13364 Make_Defining_Identifier (Loc, Name_uObject),
13365 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13366 Expression =>
13367 Unchecked_Convert_To (Typ_Id,
13368 New_Occurrence_Of (Obj_Ent, Loc)));
13369 Add (Decl);
13371 -- Set the reference to the concurrent object
13373 Obj_Ent := Defining_Identifier (Decl);
13374 end;
13375 end if;
13377 -- Step 2: Create the Protection object and build its declaration for
13378 -- any protected entry (family) of subprogram. Note for the lock-free
13379 -- implementation, the Protection object is not needed anymore.
13381 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13382 declare
13383 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13384 Prot_Typ : RE_Id;
13386 begin
13387 Set_Protection_Object (Spec_Id, Prot_Ent);
13389 -- Determine the proper protection type
13391 if Has_Attach_Handler (Conc_Typ)
13392 and then not Restricted_Profile
13393 then
13394 Prot_Typ := RE_Static_Interrupt_Protection;
13396 elsif Has_Interrupt_Handler (Conc_Typ)
13397 and then not Restriction_Active (No_Dynamic_Attachment)
13398 then
13399 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13401 else
13402 case Corresponding_Runtime_Package (Conc_Typ) is
13403 when System_Tasking_Protected_Objects_Entries =>
13404 Prot_Typ := RE_Protection_Entries;
13406 when System_Tasking_Protected_Objects_Single_Entry =>
13407 Prot_Typ := RE_Protection_Entry;
13409 when System_Tasking_Protected_Objects =>
13410 Prot_Typ := RE_Protection;
13412 when others =>
13413 raise Program_Error;
13414 end case;
13415 end if;
13417 -- Generate:
13418 -- conc_typR : protection_typ renames _object._object;
13420 Decl :=
13421 Make_Object_Renaming_Declaration (Loc,
13422 Defining_Identifier => Prot_Ent,
13423 Subtype_Mark =>
13424 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13425 Name =>
13426 Make_Selected_Component (Loc,
13427 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13428 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13429 Add (Decl);
13430 end;
13431 end if;
13433 -- Step 3: Add discriminant renamings (if any)
13435 if Has_Discriminants (Conc_Typ) then
13436 declare
13437 D : Entity_Id;
13439 begin
13440 D := First_Discriminant (Conc_Typ);
13441 while Present (D) loop
13443 -- Adjust the source location
13445 Set_Sloc (Discriminal (D), Loc);
13447 -- Generate:
13448 -- discr_name : discr_typ renames _object.discr_name;
13449 -- or
13450 -- discr_name : discr_typ renames _task.discr_name;
13452 Decl :=
13453 Make_Object_Renaming_Declaration (Loc,
13454 Defining_Identifier => Discriminal (D),
13455 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13456 Name =>
13457 Make_Selected_Component (Loc,
13458 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13459 Selector_Name => Make_Identifier (Loc, Chars (D))));
13460 Add (Decl);
13462 -- Set debug info needed on this renaming declaration even
13463 -- though it does not come from source, so that the debugger
13464 -- will get the right information for these generated names.
13466 Set_Debug_Info_Needed (Discriminal (D));
13468 Next_Discriminant (D);
13469 end loop;
13470 end;
13471 end if;
13473 -- Step 4: Add private component renamings (if any)
13475 if Is_Protected then
13476 Def := Protected_Definition (Parent (Conc_Typ));
13478 if Present (Private_Declarations (Def)) then
13479 declare
13480 Comp : Node_Id;
13481 Comp_Id : Entity_Id;
13482 Decl_Id : Entity_Id;
13484 begin
13485 Comp := First (Private_Declarations (Def));
13486 while Present (Comp) loop
13487 if Nkind (Comp) = N_Component_Declaration then
13488 Comp_Id := Defining_Identifier (Comp);
13489 Decl_Id :=
13490 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13492 -- Minimal decoration
13494 if Ekind (Spec_Id) = E_Function then
13495 Set_Ekind (Decl_Id, E_Constant);
13496 else
13497 Set_Ekind (Decl_Id, E_Variable);
13498 end if;
13500 Set_Prival (Comp_Id, Decl_Id);
13501 Set_Prival_Link (Decl_Id, Comp_Id);
13502 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13504 -- Generate:
13505 -- comp_name : comp_typ renames _object.comp_name;
13507 Decl :=
13508 Make_Object_Renaming_Declaration (Loc,
13509 Defining_Identifier => Decl_Id,
13510 Subtype_Mark =>
13511 New_Occurrence_Of (Etype (Comp_Id), Loc),
13512 Name =>
13513 Make_Selected_Component (Loc,
13514 Prefix =>
13515 New_Occurrence_Of (Obj_Ent, Loc),
13516 Selector_Name =>
13517 Make_Identifier (Loc, Chars (Comp_Id))));
13518 Add (Decl);
13519 end if;
13521 Next (Comp);
13522 end loop;
13523 end;
13524 end if;
13525 end if;
13527 -- Step 5: Add the declaration of the entry index and the associated
13528 -- type for barrier functions and entry families.
13530 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13531 declare
13532 E : constant Entity_Id := Index_Object (Spec_Id);
13533 Index : constant Entity_Id :=
13534 Defining_Identifier
13535 (Entry_Index_Specification
13536 (Entry_Body_Formal_Part (Body_Nod)));
13537 Index_Con : constant Entity_Id :=
13538 Make_Defining_Identifier (Loc, Chars (Index));
13539 High : Node_Id;
13540 Index_Typ : Entity_Id;
13541 Low : Node_Id;
13543 begin
13544 -- Minimal decoration
13546 Set_Ekind (Index_Con, E_Constant);
13547 Set_Entry_Index_Constant (Index, Index_Con);
13548 Set_Discriminal_Link (Index_Con, Index);
13550 -- Retrieve the bounds of the entry family
13552 High := Type_High_Bound (Etype (Index));
13553 Low := Type_Low_Bound (Etype (Index));
13555 -- In the simple case the entry family is given by a subtype mark
13556 -- and the index constant has the same type.
13558 if Is_Entity_Name (Original_Node (
13559 Discrete_Subtype_Definition (Parent (Index))))
13560 then
13561 Index_Typ := Etype (Index);
13563 -- Otherwise a new subtype declaration is required
13565 else
13566 High := Replace_Bound (High);
13567 Low := Replace_Bound (Low);
13569 Index_Typ := Make_Temporary (Loc, 'J');
13571 -- Generate:
13572 -- subtype Jnn is <Etype of Index> range Low .. High;
13574 Decl :=
13575 Make_Subtype_Declaration (Loc,
13576 Defining_Identifier => Index_Typ,
13577 Subtype_Indication =>
13578 Make_Subtype_Indication (Loc,
13579 Subtype_Mark =>
13580 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13581 Constraint =>
13582 Make_Range_Constraint (Loc,
13583 Range_Expression =>
13584 Make_Range (Loc, Low, High))));
13585 Add (Decl);
13586 end if;
13588 Set_Etype (Index_Con, Index_Typ);
13590 -- Create the object which designates the index:
13591 -- J : constant Jnn :=
13592 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13594 -- where Jnn is the subtype created above or the original type of
13595 -- the index, _E is a formal of the protected body subprogram and
13596 -- <index expr> is the index of the first family member.
13598 Decl :=
13599 Make_Object_Declaration (Loc,
13600 Defining_Identifier => Index_Con,
13601 Constant_Present => True,
13602 Object_Definition =>
13603 New_Occurrence_Of (Index_Typ, Loc),
13605 Expression =>
13606 Make_Attribute_Reference (Loc,
13607 Prefix =>
13608 New_Occurrence_Of (Index_Typ, Loc),
13609 Attribute_Name => Name_Val,
13611 Expressions => New_List (
13613 Make_Op_Add (Loc,
13614 Left_Opnd =>
13615 Make_Op_Subtract (Loc,
13616 Left_Opnd => New_Occurrence_Of (E, Loc),
13617 Right_Opnd =>
13618 Entry_Index_Expression (Loc,
13619 Defining_Identifier (Body_Nod),
13620 Empty, Conc_Typ)),
13622 Right_Opnd =>
13623 Make_Attribute_Reference (Loc,
13624 Prefix =>
13625 New_Occurrence_Of (Index_Typ, Loc),
13626 Attribute_Name => Name_Pos,
13627 Expressions => New_List (
13628 Make_Attribute_Reference (Loc,
13629 Prefix =>
13630 New_Occurrence_Of (Index_Typ, Loc),
13631 Attribute_Name => Name_First)))))));
13632 Add (Decl);
13633 end;
13634 end if;
13635 end Install_Private_Data_Declarations;
13637 ---------------------------------
13638 -- Is_Potentially_Large_Family --
13639 ---------------------------------
13641 function Is_Potentially_Large_Family
13642 (Base_Index : Entity_Id;
13643 Conctyp : Entity_Id;
13644 Lo : Node_Id;
13645 Hi : Node_Id) return Boolean
13647 begin
13648 return Scope (Base_Index) = Standard_Standard
13649 and then Base_Index = Base_Type (Standard_Integer)
13650 and then Has_Discriminants (Conctyp)
13651 and then
13652 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13653 and then
13654 (Denotes_Discriminant (Lo, True)
13655 or else
13656 Denotes_Discriminant (Hi, True));
13657 end Is_Potentially_Large_Family;
13659 -------------------------------------
13660 -- Is_Private_Primitive_Subprogram --
13661 -------------------------------------
13663 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13664 begin
13665 return
13666 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13667 and then Is_Private_Primitive (Id);
13668 end Is_Private_Primitive_Subprogram;
13670 ------------------
13671 -- Index_Object --
13672 ------------------
13674 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13675 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13676 Formal : Entity_Id;
13678 begin
13679 Formal := First_Formal (Bod_Subp);
13680 while Present (Formal) loop
13682 -- Look for formal parameter _E
13684 if Chars (Formal) = Name_uE then
13685 return Formal;
13686 end if;
13688 Next_Formal (Formal);
13689 end loop;
13691 -- A protected body subprogram should always have the parameter in
13692 -- question.
13694 raise Program_Error;
13695 end Index_Object;
13697 --------------------------------
13698 -- Make_Initialize_Protection --
13699 --------------------------------
13701 function Make_Initialize_Protection
13702 (Protect_Rec : Entity_Id) return List_Id
13704 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13705 P_Arr : Entity_Id;
13706 Pdec : Node_Id;
13707 Ptyp : constant Node_Id :=
13708 Corresponding_Concurrent_Type (Protect_Rec);
13709 Args : List_Id;
13710 L : constant List_Id := New_List;
13711 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13712 Prio_Type : Entity_Id;
13713 Prio_Var : Entity_Id := Empty;
13714 Restricted : constant Boolean := Restricted_Profile;
13716 begin
13717 -- We may need two calls to properly initialize the object, one to
13718 -- Initialize_Protection, and possibly one to Install_Handlers if we
13719 -- have a pragma Attach_Handler.
13721 -- Get protected declaration. In the case of a task type declaration,
13722 -- this is simply the parent of the protected type entity. In the single
13723 -- protected object declaration, this parent will be the implicit type,
13724 -- and we can find the corresponding single protected object declaration
13725 -- by searching forward in the declaration list in the tree.
13727 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13728 -- of this type should have been removed during semantic analysis.
13730 Pdec := Parent (Ptyp);
13731 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13732 N_Single_Protected_Declaration)
13733 loop
13734 Next (Pdec);
13735 end loop;
13737 -- Build the parameter list for the call. Note that _Init is the name
13738 -- of the formal for the object to be initialized, which is the task
13739 -- value record itself.
13741 Args := New_List;
13743 -- For lock-free implementation, skip initializations of the Protection
13744 -- object.
13746 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13748 -- Object parameter. This is a pointer to the object of type
13749 -- Protection used by the GNARL to control the protected object.
13751 Append_To (Args,
13752 Make_Attribute_Reference (Loc,
13753 Prefix =>
13754 Make_Selected_Component (Loc,
13755 Prefix => Make_Identifier (Loc, Name_uInit),
13756 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13757 Attribute_Name => Name_Unchecked_Access));
13759 -- Priority parameter. Set to Unspecified_Priority unless there is a
13760 -- Priority rep item, in which case we take the value from the pragma
13761 -- or attribute definition clause, or there is an Interrupt_Priority
13762 -- rep item and no Priority rep item, and we set the ceiling to
13763 -- Interrupt_Priority'Last, an implementation-defined value, see
13764 -- (RM D.3(10)).
13766 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13767 declare
13768 Prio_Clause : constant Node_Id :=
13769 Get_Rep_Item
13770 (Ptyp, Name_Priority, Check_Parents => False);
13772 Prio : Node_Id;
13774 begin
13775 -- Pragma Priority
13777 if Nkind (Prio_Clause) = N_Pragma then
13778 Prio :=
13779 Expression
13780 (First (Pragma_Argument_Associations (Prio_Clause)));
13782 -- Get_Rep_Item returns either priority pragma
13784 if Pragma_Name (Prio_Clause) = Name_Priority then
13785 Prio_Type := RTE (RE_Any_Priority);
13786 else
13787 Prio_Type := RTE (RE_Interrupt_Priority);
13788 end if;
13790 -- Attribute definition clause Priority
13792 else
13793 if Chars (Prio_Clause) = Name_Priority then
13794 Prio_Type := RTE (RE_Any_Priority);
13795 else
13796 Prio_Type := RTE (RE_Interrupt_Priority);
13797 end if;
13799 Prio := Expression (Prio_Clause);
13800 end if;
13802 -- Always create a locale variable to capture the priority.
13803 -- The priority is also passed to Install_Restriced_Handlers.
13804 -- Note that it is really necessary to create this variable
13805 -- explicitly. It might be thought that removing side effects
13806 -- would the appropriate approach, but that could generate
13807 -- declarations improperly placed in the enclosing scope.
13809 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13810 Append_To (L,
13811 Make_Object_Declaration (Loc,
13812 Defining_Identifier => Prio_Var,
13813 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13814 Expression => Relocate_Node (Prio)));
13816 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13817 end;
13819 -- When no priority is specified but an xx_Handler pragma is, we
13820 -- default to System.Interrupts.Default_Interrupt_Priority, see
13821 -- D.3(10).
13823 elsif Has_Attach_Handler (Ptyp)
13824 or else Has_Interrupt_Handler (Ptyp)
13825 then
13826 Append_To (Args,
13827 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
13829 -- Normal case, no priority or xx_Handler specified, default priority
13831 else
13832 Append_To (Args,
13833 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
13834 end if;
13836 -- Deadline_Floor parameter for GNAT_Ravenscar_EDF runtimes
13838 if Restricted_Profile and Task_Dispatching_Policy = 'E' then
13839 Deadline_Floor : declare
13840 Item : constant Node_Id :=
13841 Get_Rep_Item
13842 (Ptyp, Name_Deadline_Floor, Check_Parents => False);
13844 Deadline : Node_Id;
13846 begin
13847 if Present (Item) then
13849 -- Pragma Deadline_Floor
13851 if Nkind (Item) = N_Pragma then
13852 Deadline :=
13853 Expression
13854 (First (Pragma_Argument_Associations (Item)));
13856 -- Attribute definition clause Deadline_Floor
13858 else
13859 pragma Assert
13860 (Nkind (Item) = N_Attribute_Definition_Clause);
13862 Deadline := Expression (Item);
13863 end if;
13865 Append_To (Args, Deadline);
13867 -- Unusual case: default deadline
13869 else
13870 Append_To (Args,
13871 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
13872 end if;
13873 end Deadline_Floor;
13874 end if;
13876 -- Test for Compiler_Info parameter. This parameter allows entry body
13877 -- procedures and barrier functions to be called from the runtime. It
13878 -- is a pointer to the record generated by the compiler to represent
13879 -- the protected object.
13881 -- A protected type without entries that covers an interface and
13882 -- overrides the abstract routines with protected procedures is
13883 -- considered equivalent to a protected type with entries in the
13884 -- context of dispatching select statements.
13886 -- Protected types with interrupt handlers (when not using a
13887 -- restricted profile) are also considered equivalent to protected
13888 -- types with entries.
13890 -- The types which are used (Static_Interrupt_Protection and
13891 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13893 declare
13894 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13896 Called_Subp : RE_Id;
13898 begin
13899 case Pkg_Id is
13900 when System_Tasking_Protected_Objects_Entries =>
13901 Called_Subp := RE_Initialize_Protection_Entries;
13903 -- Argument Compiler_Info
13905 Append_To (Args,
13906 Make_Attribute_Reference (Loc,
13907 Prefix => Make_Identifier (Loc, Name_uInit),
13908 Attribute_Name => Name_Address));
13910 when System_Tasking_Protected_Objects_Single_Entry =>
13911 Called_Subp := RE_Initialize_Protection_Entry;
13913 -- Argument Compiler_Info
13915 Append_To (Args,
13916 Make_Attribute_Reference (Loc,
13917 Prefix => Make_Identifier (Loc, Name_uInit),
13918 Attribute_Name => Name_Address));
13920 when System_Tasking_Protected_Objects =>
13921 Called_Subp := RE_Initialize_Protection;
13923 when others =>
13924 raise Program_Error;
13925 end case;
13927 -- Entry_Queue_Maxes parameter. This is an access to an array of
13928 -- naturals representing the entry queue maximums for each entry
13929 -- in the protected type. Zero represents no max. The access is
13930 -- null if there is no limit for all entries (usual case).
13932 if Has_Entry
13933 and then Pkg_Id = System_Tasking_Protected_Objects_Entries
13934 then
13935 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
13936 Append_To (Args,
13937 Make_Attribute_Reference (Loc,
13938 Prefix =>
13939 New_Occurrence_Of
13940 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
13941 Attribute_Name => Name_Unrestricted_Access));
13942 else
13943 Append_To (Args, Make_Null (Loc));
13944 end if;
13946 -- Edge cases exist where entry initialization functions are
13947 -- called, but no entries exist, so null is appended.
13949 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13950 Append_To (Args, Make_Null (Loc));
13951 end if;
13953 -- Entry_Bodies parameter. This is a pointer to an array of
13954 -- pointers to the entry body procedures and barrier functions of
13955 -- the object. If the protected type has no entries this object
13956 -- will not exist, in this case, pass a null (it can happen when
13957 -- there are protected interrupt handlers or interfaces).
13959 if Has_Entry then
13960 P_Arr := Entry_Bodies_Array (Ptyp);
13962 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
13963 -- multiple entries).
13965 Append_To (Args,
13966 Make_Attribute_Reference (Loc,
13967 Prefix => New_Occurrence_Of (P_Arr, Loc),
13968 Attribute_Name => Name_Unrestricted_Access));
13970 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13972 -- Find index mapping function (clumsy but ok for now)
13974 while Ekind (P_Arr) /= E_Function loop
13975 Next_Entity (P_Arr);
13976 end loop;
13978 Append_To (Args,
13979 Make_Attribute_Reference (Loc,
13980 Prefix => New_Occurrence_Of (P_Arr, Loc),
13981 Attribute_Name => Name_Unrestricted_Access));
13982 end if;
13984 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
13986 -- This is the case where we have a protected object with
13987 -- interfaces and no entries, and the single entry restriction
13988 -- is in effect. We pass a null pointer for the entry
13989 -- parameter because there is no actual entry.
13991 Append_To (Args, Make_Null (Loc));
13993 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13995 -- This is the case where we have a protected object with no
13996 -- entries and:
13997 -- - either interrupt handlers with non restricted profile,
13998 -- - or interfaces
13999 -- Note that the types which are used for interrupt handlers
14000 -- (Static/Dynamic_Interrupt_Protection) are derived from
14001 -- Protection_Entries. We pass two null pointers because there
14002 -- is no actual entry, and the initialization procedure needs
14003 -- both Entry_Bodies and Find_Body_Index.
14005 Append_To (Args, Make_Null (Loc));
14006 Append_To (Args, Make_Null (Loc));
14007 end if;
14009 Append_To (L,
14010 Make_Procedure_Call_Statement (Loc,
14011 Name =>
14012 New_Occurrence_Of (RTE (Called_Subp), Loc),
14013 Parameter_Associations => Args));
14014 end;
14015 end if;
14017 if Has_Attach_Handler (Ptyp) then
14019 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
14020 -- make the following call:
14022 -- Install_Handlers (_object,
14023 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14025 -- or, in the case of Ravenscar:
14027 -- Install_Restricted_Handlers
14028 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
14030 declare
14031 Args : constant List_Id := New_List;
14032 Table : constant List_Id := New_List;
14033 Ritem : Node_Id := First_Rep_Item (Ptyp);
14035 begin
14036 -- Build the Priority parameter (only for ravenscar)
14038 if Restricted then
14040 -- Priority comes from a pragma
14042 if Present (Prio_Var) then
14043 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14045 -- Priority is the default one
14047 else
14048 Append_To (Args,
14049 New_Occurrence_Of
14050 (RTE (RE_Default_Interrupt_Priority), Loc));
14051 end if;
14052 end if;
14054 -- Build the Attach_Handler table argument
14056 while Present (Ritem) loop
14057 if Nkind (Ritem) = N_Pragma
14058 and then Pragma_Name (Ritem) = Name_Attach_Handler
14059 then
14060 declare
14061 Handler : constant Node_Id :=
14062 First (Pragma_Argument_Associations (Ritem));
14064 Interrupt : constant Node_Id := Next (Handler);
14065 Expr : constant Node_Id := Expression (Interrupt);
14067 begin
14068 Append_To (Table,
14069 Make_Aggregate (Loc, Expressions => New_List (
14070 Unchecked_Convert_To
14071 (RTE (RE_System_Interrupt_Id), Expr),
14072 Make_Attribute_Reference (Loc,
14073 Prefix =>
14074 Make_Selected_Component (Loc,
14075 Prefix =>
14076 Make_Identifier (Loc, Name_uInit),
14077 Selector_Name =>
14078 Duplicate_Subexpr_No_Checks
14079 (Expression (Handler))),
14080 Attribute_Name => Name_Access))));
14081 end;
14082 end if;
14084 Next_Rep_Item (Ritem);
14085 end loop;
14087 -- Append the table argument we just built
14089 Append_To (Args, Make_Aggregate (Loc, Table));
14091 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14092 -- call to the statements.
14094 if Restricted then
14095 -- Call a simplified version of Install_Handlers to be used
14096 -- when the Ravenscar restrictions are in effect
14097 -- (Install_Restricted_Handlers).
14099 Append_To (L,
14100 Make_Procedure_Call_Statement (Loc,
14101 Name =>
14102 New_Occurrence_Of
14103 (RTE (RE_Install_Restricted_Handlers), Loc),
14104 Parameter_Associations => Args));
14106 else
14107 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14109 -- First, prepends the _object argument
14111 Prepend_To (Args,
14112 Make_Attribute_Reference (Loc,
14113 Prefix =>
14114 Make_Selected_Component (Loc,
14115 Prefix => Make_Identifier (Loc, Name_uInit),
14116 Selector_Name =>
14117 Make_Identifier (Loc, Name_uObject)),
14118 Attribute_Name => Name_Unchecked_Access));
14119 end if;
14121 -- Then, insert call to Install_Handlers
14123 Append_To (L,
14124 Make_Procedure_Call_Statement (Loc,
14125 Name =>
14126 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14127 Parameter_Associations => Args));
14128 end if;
14129 end;
14130 end if;
14132 return L;
14133 end Make_Initialize_Protection;
14135 ---------------------------
14136 -- Make_Task_Create_Call --
14137 ---------------------------
14139 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14140 Loc : constant Source_Ptr := Sloc (Task_Rec);
14141 Args : List_Id;
14142 Ecount : Node_Id;
14143 Name : Node_Id;
14144 Tdec : Node_Id;
14145 Tdef : Node_Id;
14146 Tnam : Name_Id;
14147 Ttyp : Node_Id;
14149 begin
14150 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14151 Tnam := Chars (Ttyp);
14153 -- Get task declaration. In the case of a task type declaration, this is
14154 -- simply the parent of the task type entity. In the single task
14155 -- declaration, this parent will be the implicit type, and we can find
14156 -- the corresponding single task declaration by searching forward in the
14157 -- declaration list in the tree.
14159 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14160 -- this type should have been removed during semantic analysis.
14162 Tdec := Parent (Ttyp);
14163 while not Nkind_In (Tdec, N_Task_Type_Declaration,
14164 N_Single_Task_Declaration)
14165 loop
14166 Next (Tdec);
14167 end loop;
14169 -- Now we can find the task definition from this declaration
14171 Tdef := Task_Definition (Tdec);
14173 -- Build the parameter list for the call. Note that _Init is the name
14174 -- of the formal for the object to be initialized, which is the task
14175 -- value record itself.
14177 Args := New_List;
14179 -- Priority parameter. Set to Unspecified_Priority unless there is a
14180 -- Priority rep item, in which case we take the value from the rep item.
14181 -- Not used on Ravenscar_EDF profile.
14183 if not (Restricted_Profile and then Task_Dispatching_Policy = 'E') then
14184 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14185 Append_To (Args,
14186 Make_Selected_Component (Loc,
14187 Prefix => Make_Identifier (Loc, Name_uInit),
14188 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14189 else
14190 Append_To (Args,
14191 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14192 end if;
14193 end if;
14195 -- Optional Stack parameter
14197 if Restricted_Profile then
14199 -- If the stack has been preallocated by the expander then
14200 -- pass its address. Otherwise, pass a null address.
14202 if Preallocated_Stacks_On_Target then
14203 Append_To (Args,
14204 Make_Attribute_Reference (Loc,
14205 Prefix =>
14206 Make_Selected_Component (Loc,
14207 Prefix => Make_Identifier (Loc, Name_uInit),
14208 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14209 Attribute_Name => Name_Address));
14211 else
14212 Append_To (Args,
14213 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14214 end if;
14215 end if;
14217 -- Size parameter. If no Storage_Size pragma is present, then
14218 -- the size is taken from the taskZ variable for the type, which
14219 -- is either Unspecified_Size, or has been reset by the use of
14220 -- a Storage_Size attribute definition clause. If a pragma is
14221 -- present, then the size is taken from the _Size field of the
14222 -- task value record, which was set from the pragma value.
14224 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14225 Append_To (Args,
14226 Make_Selected_Component (Loc,
14227 Prefix => Make_Identifier (Loc, Name_uInit),
14228 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14230 else
14231 Append_To (Args,
14232 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14233 end if;
14235 -- Secondary_Stack parameter used for restricted profiles
14237 if Restricted_Profile then
14239 -- If the secondary stack has been allocated by the expander then
14240 -- pass its access pointer. Otherwise, pass null.
14242 if Create_Secondary_Stack_For_Task (Ttyp) then
14243 Append_To (Args,
14244 Make_Attribute_Reference (Loc,
14245 Prefix =>
14246 Make_Selected_Component (Loc,
14247 Prefix => Make_Identifier (Loc, Name_uInit),
14248 Selector_Name =>
14249 Make_Identifier (Loc, Name_uSecondary_Stack)),
14250 Attribute_Name => Name_Unrestricted_Access));
14252 else
14253 Append_To (Args, Make_Null (Loc));
14254 end if;
14255 end if;
14257 -- Secondary_Stack_Size parameter. Set RE_Unspecified_Size unless there
14258 -- is a Secondary_Stack_Size pragma, in which case take the value from
14259 -- the pragma. If the restriction No_Secondary_Stack is active then a
14260 -- size of 0 is passed regardless to prevent the allocation of the
14261 -- unused stack.
14263 if Restriction_Active (No_Secondary_Stack) then
14264 Append_To (Args, Make_Integer_Literal (Loc, 0));
14266 elsif Has_Rep_Pragma
14267 (Ttyp, Name_Secondary_Stack_Size, Check_Parents => False)
14268 then
14269 Append_To (Args,
14270 Make_Selected_Component (Loc,
14271 Prefix => Make_Identifier (Loc, Name_uInit),
14272 Selector_Name =>
14273 Make_Identifier (Loc, Name_uSecondary_Stack_Size)));
14275 else
14276 Append_To (Args,
14277 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
14278 end if;
14280 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14281 -- Task_Info pragma, in which case we take the value from the pragma.
14283 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14284 Append_To (Args,
14285 Make_Selected_Component (Loc,
14286 Prefix => Make_Identifier (Loc, Name_uInit),
14287 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14289 else
14290 Append_To (Args,
14291 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14292 end if;
14294 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14295 -- in which case we take the value from the rep item. The parameter is
14296 -- passed as an Integer because in the case of unspecified CPU the
14297 -- value is not in the range of CPU_Range.
14299 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14300 Append_To (Args,
14301 Convert_To (Standard_Integer,
14302 Make_Selected_Component (Loc,
14303 Prefix => Make_Identifier (Loc, Name_uInit),
14304 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14305 else
14306 Append_To (Args,
14307 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14308 end if;
14310 if not Restricted_Profile or else Task_Dispatching_Policy = 'E' then
14312 -- Deadline parameter. If no Relative_Deadline pragma is present,
14313 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14314 -- the deadline is taken from the _Relative_Deadline field of the
14315 -- task value record, which was set from the pragma value. Note that
14316 -- this parameter must not be generated for the restricted profiles
14317 -- since Ravenscar does not allow deadlines.
14319 -- Case where pragma Relative_Deadline applies: use given value
14321 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14322 Append_To (Args,
14323 Make_Selected_Component (Loc,
14324 Prefix => Make_Identifier (Loc, Name_uInit),
14325 Selector_Name =>
14326 Make_Identifier (Loc, Name_uRelative_Deadline)));
14328 -- No pragma Relative_Deadline apply to the task
14330 else
14331 Append_To (Args,
14332 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14333 end if;
14334 end if;
14336 if not Restricted_Profile then
14338 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14339 -- present, then the dispatching domain is null. If a rep item is
14340 -- present, then the dispatching domain is taken from the
14341 -- _Dispatching_Domain field of the task value record, which was set
14342 -- from the rep item value.
14344 -- Case where Dispatching_Domain rep item applies: use given value
14346 if Has_Rep_Item
14347 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14348 then
14349 Append_To (Args,
14350 Make_Selected_Component (Loc,
14351 Prefix =>
14352 Make_Identifier (Loc, Name_uInit),
14353 Selector_Name =>
14354 Make_Identifier (Loc, Name_uDispatching_Domain)));
14356 -- No pragma or aspect Dispatching_Domain applies to the task
14358 else
14359 Append_To (Args, Make_Null (Loc));
14360 end if;
14362 -- Number of entries. This is an expression of the form:
14364 -- n + _Init.a'Length + _Init.a'B'Length + ...
14366 -- where a,b... are the entry family names for the task definition
14368 Ecount :=
14369 Build_Entry_Count_Expression
14370 (Ttyp,
14371 Component_Items
14372 (Component_List
14373 (Type_Definition
14374 (Parent (Corresponding_Record_Type (Ttyp))))),
14375 Loc);
14376 Append_To (Args, Ecount);
14378 -- Master parameter. This is a reference to the _Master parameter of
14379 -- the initialization procedure, except in the case of the pragma
14380 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14381 -- System.Tasking.Library_Task_Level.
14383 if Restriction_Active (No_Task_Hierarchy) = False then
14384 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14385 else
14386 Append_To (Args,
14387 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14388 end if;
14389 end if;
14391 -- State parameter. This is a pointer to the task body procedure. The
14392 -- required value is obtained by taking 'Unrestricted_Access of the task
14393 -- body procedure and converting it (with an unchecked conversion) to
14394 -- the type required by the task kernel. For further details, see the
14395 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14396 -- than 'Address in order to avoid creating trampolines.
14398 declare
14399 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14400 Subp_Ptr_Typ : constant Node_Id :=
14401 Create_Itype (E_Access_Subprogram_Type, Tdec);
14402 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14404 begin
14405 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14406 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14408 -- Be sure to freeze a reference to the access-to-subprogram type,
14409 -- otherwise gigi will complain that it's in the wrong scope, because
14410 -- it's actually inside the init procedure for the record type that
14411 -- corresponds to the task type.
14413 Set_Itype (Ref, Subp_Ptr_Typ);
14414 Append_Freeze_Action (Task_Rec, Ref);
14416 Append_To (Args,
14417 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14418 Make_Qualified_Expression (Loc,
14419 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14420 Expression =>
14421 Make_Attribute_Reference (Loc,
14422 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14423 Attribute_Name => Name_Unrestricted_Access))));
14424 end;
14426 -- Discriminants parameter. This is just the address of the task
14427 -- value record itself (which contains the discriminant values
14429 Append_To (Args,
14430 Make_Attribute_Reference (Loc,
14431 Prefix => Make_Identifier (Loc, Name_uInit),
14432 Attribute_Name => Name_Address));
14434 -- Elaborated parameter. This is an access to the elaboration Boolean
14436 Append_To (Args,
14437 Make_Attribute_Reference (Loc,
14438 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14439 Attribute_Name => Name_Unchecked_Access));
14441 -- Add Chain parameter (not done for sequential elaboration policy, see
14442 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14444 if Partition_Elaboration_Policy /= 'S' then
14445 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14446 end if;
14448 -- Task name parameter. Take this from the _Task_Id parameter to the
14449 -- init call unless there is a Task_Name pragma, in which case we take
14450 -- the value from the pragma.
14452 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14453 -- Copy expression in full, because it may be dynamic and have
14454 -- side effects.
14456 Append_To (Args,
14457 New_Copy_Tree
14458 (Expression
14459 (First
14460 (Pragma_Argument_Associations
14461 (Get_Rep_Pragma
14462 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14464 else
14465 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14466 end if;
14468 -- Created_Task parameter. This is the _Task_Id field of the task
14469 -- record value
14471 Append_To (Args,
14472 Make_Selected_Component (Loc,
14473 Prefix => Make_Identifier (Loc, Name_uInit),
14474 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14476 declare
14477 Create_RE : RE_Id;
14479 begin
14480 if Restricted_Profile then
14481 if Partition_Elaboration_Policy = 'S' then
14482 Create_RE := RE_Create_Restricted_Task_Sequential;
14483 else
14484 Create_RE := RE_Create_Restricted_Task;
14485 end if;
14486 else
14487 Create_RE := RE_Create_Task;
14488 end if;
14490 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14491 end;
14493 return
14494 Make_Procedure_Call_Statement (Loc,
14495 Name => Name,
14496 Parameter_Associations => Args);
14497 end Make_Task_Create_Call;
14499 ------------------------------
14500 -- Next_Protected_Operation --
14501 ------------------------------
14503 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14504 Next_Op : Node_Id;
14506 begin
14507 -- Check whether there is a subsequent body for a protected operation
14508 -- in the current protected body. In Ada2012 that includes expression
14509 -- functions that are completions.
14511 Next_Op := Next (N);
14512 while Present (Next_Op)
14513 and then not Nkind_In (Next_Op,
14514 N_Subprogram_Body, N_Entry_Body, N_Expression_Function)
14515 loop
14516 Next (Next_Op);
14517 end loop;
14519 return Next_Op;
14520 end Next_Protected_Operation;
14522 ---------------------
14523 -- Null_Statements --
14524 ---------------------
14526 function Null_Statements (Stats : List_Id) return Boolean is
14527 Stmt : Node_Id;
14529 begin
14530 Stmt := First (Stats);
14531 while Nkind (Stmt) /= N_Empty
14532 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14533 or else
14534 (Nkind (Stmt) = N_Pragma
14535 and then
14536 Nam_In (Pragma_Name_Unmapped (Stmt),
14537 Name_Unreferenced,
14538 Name_Unmodified,
14539 Name_Warnings)))
14540 loop
14541 Next (Stmt);
14542 end loop;
14544 return Nkind (Stmt) = N_Empty;
14545 end Null_Statements;
14547 --------------------------
14548 -- Parameter_Block_Pack --
14549 --------------------------
14551 function Parameter_Block_Pack
14552 (Loc : Source_Ptr;
14553 Blk_Typ : Entity_Id;
14554 Actuals : List_Id;
14555 Formals : List_Id;
14556 Decls : List_Id;
14557 Stmts : List_Id) return Node_Id
14559 Actual : Entity_Id;
14560 Expr : Node_Id := Empty;
14561 Formal : Entity_Id;
14562 Has_Param : Boolean := False;
14563 P : Entity_Id;
14564 Params : List_Id;
14565 Temp_Asn : Node_Id;
14566 Temp_Nam : Node_Id;
14568 begin
14569 Actual := First (Actuals);
14570 Formal := Defining_Identifier (First (Formals));
14571 Params := New_List;
14572 while Present (Actual) loop
14573 if Is_By_Copy_Type (Etype (Actual)) then
14574 -- Generate:
14575 -- Jnn : aliased <formal-type>
14577 Temp_Nam := Make_Temporary (Loc, 'J');
14579 Append_To (Decls,
14580 Make_Object_Declaration (Loc,
14581 Aliased_Present => True,
14582 Defining_Identifier => Temp_Nam,
14583 Object_Definition =>
14584 New_Occurrence_Of (Etype (Formal), Loc)));
14586 -- The object is initialized with an explicit assignment
14587 -- later. Indicate that it does not need an initialization
14588 -- to prevent spurious warnings if the type excludes null.
14590 Set_No_Initialization (Last (Decls));
14592 if Ekind (Formal) /= E_Out_Parameter then
14594 -- Generate:
14595 -- Jnn := <actual>
14597 Temp_Asn :=
14598 New_Occurrence_Of (Temp_Nam, Loc);
14600 Set_Assignment_OK (Temp_Asn);
14602 Append_To (Stmts,
14603 Make_Assignment_Statement (Loc,
14604 Name => Temp_Asn,
14605 Expression => New_Copy_Tree (Actual)));
14606 end if;
14608 -- If the actual is not controlling, generate:
14610 -- Jnn'unchecked_access
14612 -- and add it to aggegate for access to formals. Note that the
14613 -- actual may be by-copy but still be a controlling actual if it
14614 -- is an access to class-wide interface.
14616 if not Is_Controlling_Actual (Actual) then
14617 Append_To (Params,
14618 Make_Attribute_Reference (Loc,
14619 Attribute_Name => Name_Unchecked_Access,
14620 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14622 Has_Param := True;
14623 end if;
14625 -- The controlling parameter is omitted
14627 else
14628 if not Is_Controlling_Actual (Actual) then
14629 Append_To (Params,
14630 Make_Reference (Loc, New_Copy_Tree (Actual)));
14632 Has_Param := True;
14633 end if;
14634 end if;
14636 Next_Actual (Actual);
14637 Next_Formal_With_Extras (Formal);
14638 end loop;
14640 if Has_Param then
14641 Expr := Make_Aggregate (Loc, Params);
14642 end if;
14644 -- Generate:
14645 -- P : Ann := (
14646 -- J1'unchecked_access;
14647 -- <actual2>'reference;
14648 -- ...);
14650 P := Make_Temporary (Loc, 'P');
14652 Append_To (Decls,
14653 Make_Object_Declaration (Loc,
14654 Defining_Identifier => P,
14655 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14656 Expression => Expr));
14658 return P;
14659 end Parameter_Block_Pack;
14661 ----------------------------
14662 -- Parameter_Block_Unpack --
14663 ----------------------------
14665 function Parameter_Block_Unpack
14666 (Loc : Source_Ptr;
14667 P : Entity_Id;
14668 Actuals : List_Id;
14669 Formals : List_Id) return List_Id
14671 Actual : Entity_Id;
14672 Asnmt : Node_Id;
14673 Formal : Entity_Id;
14674 Has_Asnmt : Boolean := False;
14675 Result : constant List_Id := New_List;
14677 begin
14678 Actual := First (Actuals);
14679 Formal := Defining_Identifier (First (Formals));
14680 while Present (Actual) loop
14681 if Is_By_Copy_Type (Etype (Actual))
14682 and then Ekind (Formal) /= E_In_Parameter
14683 then
14684 -- Generate:
14685 -- <actual> := P.<formal>;
14687 Asnmt :=
14688 Make_Assignment_Statement (Loc,
14689 Name =>
14690 New_Copy (Actual),
14691 Expression =>
14692 Make_Explicit_Dereference (Loc,
14693 Make_Selected_Component (Loc,
14694 Prefix =>
14695 New_Occurrence_Of (P, Loc),
14696 Selector_Name =>
14697 Make_Identifier (Loc, Chars (Formal)))));
14699 Set_Assignment_OK (Name (Asnmt));
14700 Append_To (Result, Asnmt);
14702 Has_Asnmt := True;
14703 end if;
14705 Next_Actual (Actual);
14706 Next_Formal_With_Extras (Formal);
14707 end loop;
14709 if Has_Asnmt then
14710 return Result;
14711 else
14712 return New_List (Make_Null_Statement (Loc));
14713 end if;
14714 end Parameter_Block_Unpack;
14716 ----------------------
14717 -- Set_Discriminals --
14718 ----------------------
14720 procedure Set_Discriminals (Dec : Node_Id) is
14721 D : Entity_Id;
14722 Pdef : Entity_Id;
14723 D_Minal : Entity_Id;
14725 begin
14726 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14727 Pdef := Defining_Identifier (Dec);
14729 if Has_Discriminants (Pdef) then
14730 D := First_Discriminant (Pdef);
14731 while Present (D) loop
14732 D_Minal :=
14733 Make_Defining_Identifier (Sloc (D),
14734 Chars => New_External_Name (Chars (D), 'D'));
14736 Set_Ekind (D_Minal, E_Constant);
14737 Set_Etype (D_Minal, Etype (D));
14738 Set_Scope (D_Minal, Pdef);
14739 Set_Discriminal (D, D_Minal);
14740 Set_Discriminal_Link (D_Minal, D);
14742 Next_Discriminant (D);
14743 end loop;
14744 end if;
14745 end Set_Discriminals;
14747 -----------------------
14748 -- Trivial_Accept_OK --
14749 -----------------------
14751 function Trivial_Accept_OK return Boolean is
14752 begin
14753 case Opt.Task_Dispatching_Policy is
14755 -- If we have the default task dispatching policy in effect, we can
14756 -- definitely do the optimization (one way of looking at this is to
14757 -- think of the formal definition of the default policy being allowed
14758 -- to run any task it likes after a rendezvous, so even if notionally
14759 -- a full rescheduling occurs, we can say that our dispatching policy
14760 -- (i.e. the default dispatching policy) reorders the queue to be the
14761 -- same as just before the call.
14763 when ' ' =>
14764 return True;
14766 -- FIFO_Within_Priorities certainly does not permit this
14767 -- optimization since the Rendezvous is a scheduling action that may
14768 -- require some other task to be run.
14770 when 'F' =>
14771 return False;
14773 -- For now, disallow the optimization for all other policies. This
14774 -- may be over-conservative, but it is certainly not incorrect.
14776 when others =>
14777 return False;
14778 end case;
14779 end Trivial_Accept_OK;
14781 end Exp_Ch9;