PR target/81369
[official-gcc.git] / gcc / ada / exp_ch9.adb
blobecca4c3534c59bd5fef0ba09534d993ce91f67ef
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-2017, 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_Disp; use Exp_Disp;
35 with Exp_Sel; use Exp_Sel;
36 with Exp_Smem; use Exp_Smem;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Freeze; use Freeze;
40 with Hostparm;
41 with Itypes; use Itypes;
42 with Namet; use Namet;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Restrict; use Restrict;
47 with Rident; use Rident;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Ch6; use Sem_Ch6;
52 with Sem_Ch8; use Sem_Ch8;
53 with Sem_Ch9; use Sem_Ch9;
54 with Sem_Ch11; use Sem_Ch11;
55 with Sem_Elab; use Sem_Elab;
56 with Sem_Eval; use Sem_Eval;
57 with Sem_Res; use Sem_Res;
58 with Sem_Util; use Sem_Util;
59 with Sinfo; use Sinfo;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Targparm; use Targparm;
63 with Tbuild; use Tbuild;
64 with Uintp; use Uintp;
65 with Validsw; use Validsw;
67 package body Exp_Ch9 is
69 -- The following constant establishes the upper bound for the index of
70 -- an entry family. It is used to limit the allocated size of protected
71 -- types with defaulted discriminant of an integer type, when the bound
72 -- of some entry family depends on a discriminant. The limitation to entry
73 -- families of 128K should be reasonable in all cases, and is a documented
74 -- implementation restriction.
76 Entry_Family_Bound : constant Pos := 2**16;
78 -----------------------
79 -- Local Subprograms --
80 -----------------------
82 function Actual_Index_Expression
83 (Sloc : Source_Ptr;
84 Ent : Entity_Id;
85 Index : Node_Id;
86 Tsk : Entity_Id) return Node_Id;
87 -- Compute the index position for an entry call. Tsk is the target task. If
88 -- the bounds of some entry family depend on discriminants, the expression
89 -- computed by this function uses the discriminants of the target task.
91 procedure Add_Object_Pointer
92 (Loc : Source_Ptr;
93 Conc_Typ : Entity_Id;
94 Decls : List_Id);
95 -- Prepend an object pointer declaration to the declaration list Decls.
96 -- This object pointer is initialized to a type conversion of the System.
97 -- Address pointer passed to entry barrier functions and entry body
98 -- procedures.
100 procedure Add_Formal_Renamings
101 (Spec : Node_Id;
102 Decls : List_Id;
103 Ent : Entity_Id;
104 Loc : Source_Ptr);
105 -- Create renaming declarations for the formals, inside the procedure that
106 -- implements an entry body. The renamings make the original names of the
107 -- formals accessible to gdb, and serve no other purpose.
108 -- Spec is the specification of the procedure being built.
109 -- Decls is the list of declarations to be enhanced.
110 -- Ent is the entity for the original entry body.
112 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
113 -- Transform accept statement into a block with added exception handler.
114 -- Used both for simple accept statements and for accept alternatives in
115 -- select statements. Astat is the accept statement.
117 function Build_Barrier_Function
118 (N : Node_Id;
119 Ent : Entity_Id;
120 Pid : Node_Id) return Node_Id;
121 -- Build the function body returning the value of the barrier expression
122 -- for the specified entry body.
124 function Build_Barrier_Function_Specification
125 (Loc : Source_Ptr;
126 Def_Id : Entity_Id) return Node_Id;
127 -- Build a specification for a function implementing the protected entry
128 -- barrier of the specified entry body.
130 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id);
131 -- Build the body of a wrapper procedure for an entry or entry family that
132 -- has contract cases, preconditions, or postconditions. The body gathers
133 -- the executable contract items and expands them in the usual way, and
134 -- performs the entry call itself. This way preconditions are evaluated
135 -- before the call is queued. E is the entry in question, and Decl is the
136 -- enclosing synchronized type declaration at whose freeze point the
137 -- generated body is analyzed.
139 function Build_Corresponding_Record
140 (N : Node_Id;
141 Ctyp : Node_Id;
142 Loc : Source_Ptr) return Node_Id;
143 -- Common to tasks and protected types. Copy discriminant specifications,
144 -- build record declaration. N is the type declaration, Ctyp is the
145 -- concurrent entity (task type or protected type).
147 function Build_Dispatching_Tag_Check
148 (K : Entity_Id;
149 N : Node_Id) return Node_Id;
150 -- Utility to create the tree to check whether the dispatching call in
151 -- a timed entry call, a conditional entry call, or an asynchronous
152 -- transfer of control is a call to a primitive of a non-synchronized type.
153 -- K is the temporary that holds the tagged kind of the target object, and
154 -- N is the enclosing construct.
156 function Build_Entry_Count_Expression
157 (Concurrent_Type : Node_Id;
158 Component_List : List_Id;
159 Loc : Source_Ptr) return Node_Id;
160 -- Compute number of entries for concurrent object. This is a count of
161 -- simple entries, followed by an expression that computes the length
162 -- of the range of each entry family. A single array with that size is
163 -- allocated for each concurrent object of the type.
165 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
166 -- Build the function that translates the entry index in the call
167 -- (which depends on the size of entry families) into an index into the
168 -- Entry_Bodies_Array, to determine the body and barrier function used
169 -- in a protected entry call. A pointer to this function appears in every
170 -- protected object.
172 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
173 -- Build subprogram declaration for previous one
175 function Build_Lock_Free_Protected_Subprogram_Body
176 (N : Node_Id;
177 Prot_Typ : Node_Id;
178 Unprot_Spec : Node_Id) return Node_Id;
179 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
180 -- the subprogram specification of the unprotected version of N. Transform
181 -- N such that it invokes the unprotected version of the body.
183 function Build_Lock_Free_Unprotected_Subprogram_Body
184 (N : Node_Id;
185 Prot_Typ : Node_Id) return Node_Id;
186 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
187 -- of N where the original statements of N are synchronized through atomic
188 -- actions such as compare and exchange. Prior to invoking this routine, it
189 -- has been established that N can be implemented in a lock-free fashion.
191 function Build_Parameter_Block
192 (Loc : Source_Ptr;
193 Actuals : List_Id;
194 Formals : List_Id;
195 Decls : List_Id) return Entity_Id;
196 -- Generate an access type for each actual parameter in the list Actuals.
197 -- Create an encapsulating record that contains all the actuals and return
198 -- its type. Generate:
199 -- type Ann1 is access all <actual1-type>
200 -- ...
201 -- type AnnN is access all <actualN-type>
202 -- type Pnn is record
203 -- <formal1> : Ann1;
204 -- ...
205 -- <formalN> : AnnN;
206 -- end record;
208 function Build_Protected_Entry
209 (N : Node_Id;
210 Ent : Entity_Id;
211 Pid : Node_Id) return Node_Id;
212 -- Build the procedure implementing the statement sequence of the specified
213 -- entry body.
215 function Build_Protected_Entry_Specification
216 (Loc : Source_Ptr;
217 Def_Id : Entity_Id;
218 Ent_Id : Entity_Id) return Node_Id;
219 -- Build a specification for the procedure implementing the statements of
220 -- the specified entry body. Add attributes associating it with the entry
221 -- defining identifier Ent_Id.
223 function Build_Protected_Spec
224 (N : Node_Id;
225 Obj_Type : Entity_Id;
226 Ident : Entity_Id;
227 Unprotected : Boolean := False) return List_Id;
228 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
229 -- Subprogram_Type. Builds signature of protected subprogram, adding the
230 -- formal that corresponds to the object itself. For an access to protected
231 -- subprogram, there is no object type to specify, so the parameter has
232 -- type Address and mode In. An indirect call through such a pointer will
233 -- convert the address to a reference to the actual object. The object is
234 -- a limited record and therefore a by_reference type.
236 function Build_Protected_Subprogram_Body
237 (N : Node_Id;
238 Pid : Node_Id;
239 N_Op_Spec : Node_Id) return Node_Id;
240 -- This function is used to construct the protected version of a protected
241 -- subprogram. Its statement sequence first defers abort, then locks the
242 -- associated protected object, and then enters a block that contains a
243 -- call to the unprotected version of the subprogram (for details, see
244 -- Build_Unprotected_Subprogram_Body). This block statement requires a
245 -- cleanup handler that unlocks the object in all cases. For details,
246 -- see Exp_Ch7.Expand_Cleanup_Actions.
248 function Build_Renamed_Formal_Declaration
249 (New_F : Entity_Id;
250 Formal : Entity_Id;
251 Comp : Entity_Id;
252 Renamed_Formal : Node_Id) return Node_Id;
253 -- Create a renaming declaration for a formal, within a protected entry
254 -- body or an accept body. The renamed object is a component of the
255 -- parameter block that is a parameter in the entry call.
257 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
258 -- does not dereference the corresponding component to prevent an illegal
259 -- use of the incomplete type (AI05-0151).
261 function Build_Selected_Name
262 (Prefix : Entity_Id;
263 Selector : Entity_Id;
264 Append_Char : Character := ' ') return Name_Id;
265 -- Build a name in the form of Prefix__Selector, with an optional character
266 -- appended. This is used for internal subprograms generated for operations
267 -- of protected types, including barrier functions. For the subprograms
268 -- generated for entry bodies and entry barriers, the generated name
269 -- includes a sequence number that makes names unique in the presence of
270 -- entry overloading. This is necessary because entry body procedures and
271 -- barrier functions all have the same signature.
273 procedure Build_Simple_Entry_Call
274 (N : Node_Id;
275 Concval : Node_Id;
276 Ename : Node_Id;
277 Index : Node_Id);
278 -- Some comments here would be useful ???
280 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
281 -- This routine constructs a specification for the procedure that we will
282 -- build for the task body for task type T. The spec has the form:
284 -- procedure tnameB (_Task : access tnameV);
286 -- where name is the character name taken from the task type entity that
287 -- is passed as the argument to the procedure, and tnameV is the task
288 -- value type that is associated with the task type.
290 function Build_Unprotected_Subprogram_Body
291 (N : Node_Id;
292 Pid : Node_Id) return Node_Id;
293 -- This routine constructs the unprotected version of a protected
294 -- subprogram body, which is contains all of the code in the original,
295 -- unexpanded body. This is the version of the protected subprogram that is
296 -- called from all protected operations on the same object, including the
297 -- protected version of the same subprogram.
299 procedure Build_Wrapper_Bodies
300 (Loc : Source_Ptr;
301 Typ : Entity_Id;
302 N : Node_Id);
303 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
304 -- record of a concurrent type. N is the insertion node where all bodies
305 -- will be placed. This routine builds the bodies of the subprograms which
306 -- serve as an indirection mechanism to overriding primitives of concurrent
307 -- types, entries and protected procedures. Any new body is analyzed.
309 procedure Build_Wrapper_Specs
310 (Loc : Source_Ptr;
311 Typ : Entity_Id;
312 N : in out Node_Id);
313 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
314 -- record of a concurrent type. N is the insertion node where all specs
315 -- will be placed. This routine builds the specs of the subprograms which
316 -- serve as an indirection mechanism to overriding primitives of concurrent
317 -- types, entries and protected procedures. Any new spec is analyzed.
319 procedure Collect_Entry_Families
320 (Loc : Source_Ptr;
321 Cdecls : List_Id;
322 Current_Node : in out Node_Id;
323 Conctyp : Entity_Id);
324 -- For each entry family in a concurrent type, create an anonymous array
325 -- type of the right size, and add a component to the corresponding_record.
327 function Concurrent_Object
328 (Spec_Id : Entity_Id;
329 Conc_Typ : Entity_Id) return Entity_Id;
330 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
331 -- the entity associated with the concurrent object in the Protected_Body_
332 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
333 -- denotes formal parameter _O, _object or _task.
335 function Copy_Result_Type (Res : Node_Id) return Node_Id;
336 -- Copy the result type of a function specification, when building the
337 -- internal operation corresponding to a protected function, or when
338 -- expanding an access to protected function. If the result is an anonymous
339 -- access to subprogram itself, we need to create a new signature with the
340 -- same parameter names and the same resolved types, but with new entities
341 -- for the formals.
343 procedure Debug_Private_Data_Declarations (Decls : List_Id);
344 -- Decls is a list which may contain the declarations created by Install_
345 -- Private_Data_Declarations. All generated entities are marked as needing
346 -- debug info and debug nodes are manually generation where necessary. This
347 -- step of the expansion must to be done after private data has been moved
348 -- to its final resting scope to ensure proper visibility of debug objects.
350 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
351 -- If control flow optimizations are suppressed, and Alt is an accept,
352 -- delay, or entry call alternative with no trailing statements, insert
353 -- a null trailing statement with the given Loc (which is the sloc of
354 -- the accept, delay, or entry call statement). There might not be any
355 -- generated code for the accept, delay, or entry call itself (the effect
356 -- of these statements is part of the general processsing done for the
357 -- enclosing selective accept, timed entry call, or asynchronous select),
358 -- and the null statement is there to carry the sloc of that statement to
359 -- the back-end for trace-based coverage analysis purposes.
361 procedure Extract_Dispatching_Call
362 (N : Node_Id;
363 Call_Ent : out Entity_Id;
364 Object : out Entity_Id;
365 Actuals : out List_Id;
366 Formals : out List_Id);
367 -- Given a dispatching call, extract the entity of the name of the call,
368 -- its actual dispatching object, its actual parameters and the formal
369 -- parameters of the overridden interface-level version. If the type of
370 -- the dispatching object is an access type then an explicit dereference
371 -- is returned in Object.
373 procedure Extract_Entry
374 (N : Node_Id;
375 Concval : out Node_Id;
376 Ename : out Node_Id;
377 Index : out Node_Id);
378 -- Given an entry call, returns the associated concurrent object, the entry
379 -- name, and the entry family index.
381 function Family_Offset
382 (Loc : Source_Ptr;
383 Hi : Node_Id;
384 Lo : Node_Id;
385 Ttyp : Entity_Id;
386 Cap : Boolean) return Node_Id;
387 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
388 -- accept statement, or the upper bound in the discrete subtype of an entry
389 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
390 -- type of the entry. If Cap is true, the result is capped according to
391 -- Entry_Family_Bound.
393 function Family_Size
394 (Loc : Source_Ptr;
395 Hi : Node_Id;
396 Lo : Node_Id;
397 Ttyp : Entity_Id;
398 Cap : Boolean) return Node_Id;
399 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
400 -- family, and handle properly the superflat case. This is equivalent to
401 -- the use of 'Length on the index type, but must use Family_Offset to
402 -- handle properly the case of bounds that depend on discriminants. If
403 -- Cap is true, the result is capped according to Entry_Family_Bound.
405 procedure Find_Enclosing_Context
406 (N : Node_Id;
407 Context : out Node_Id;
408 Context_Id : out Entity_Id;
409 Context_Decls : out List_Id);
410 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
411 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
412 -- nearest enclosing body, block, package, or return statement and return
413 -- its constituents. Context is the enclosing construct, Context_Id is
414 -- the scope of Context_Id and Context_Decls is the declarative list of
415 -- Context.
417 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
418 -- Given a subprogram identifier, return the entity which is associated
419 -- with the protection entry index in the Protected_Body_Subprogram or
420 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
421 -- parameter _E.
423 function Is_Potentially_Large_Family
424 (Base_Index : Entity_Id;
425 Conctyp : Entity_Id;
426 Lo : Node_Id;
427 Hi : Node_Id) return Boolean;
429 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
430 -- Determine whether Id is a function or a procedure and is marked as a
431 -- private primitive.
433 function Null_Statements (Stats : List_Id) return Boolean;
434 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
435 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
436 -- to still count as null. Returns True for a null sequence. The argument
437 -- is the list of statements from the DO-END sequence.
439 function Parameter_Block_Pack
440 (Loc : Source_Ptr;
441 Blk_Typ : Entity_Id;
442 Actuals : List_Id;
443 Formals : List_Id;
444 Decls : List_Id;
445 Stmts : List_Id) return Entity_Id;
446 -- Set the components of the generated parameter block with the values
447 -- of the actual parameters. Generate aliased temporaries to capture the
448 -- values for types that are passed by copy. Otherwise generate a reference
449 -- to the actual's value. Return the address of the aggregate block.
450 -- Generate:
451 -- Jnn1 : alias <formal-type1>;
452 -- Jnn1 := <actual1>;
453 -- ...
454 -- P : Blk_Typ := (
455 -- Jnn1'unchecked_access;
456 -- <actual2>'reference;
457 -- ...);
459 function Parameter_Block_Unpack
460 (Loc : Source_Ptr;
461 P : Entity_Id;
462 Actuals : List_Id;
463 Formals : List_Id) return List_Id;
464 -- Retrieve the values of the components from the parameter block and
465 -- assign then to the original actual parameters. Generate:
466 -- <actual1> := P.<formal1>;
467 -- ...
468 -- <actualN> := P.<formalN>;
470 function Trivial_Accept_OK return Boolean;
471 -- If there is no DO-END block for an accept, or if the DO-END block has
472 -- only null statements, then it is possible to do the Rendezvous with much
473 -- less overhead using the Accept_Trivial routine in the run-time library.
474 -- However, this is not always a valid optimization. Whether it is valid or
475 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
476 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
477 -- a rescheduling is required, so this optimization is not allowed. This
478 -- function returns True if the optimization is permitted.
480 -----------------------------
481 -- Actual_Index_Expression --
482 -----------------------------
484 function Actual_Index_Expression
485 (Sloc : Source_Ptr;
486 Ent : Entity_Id;
487 Index : Node_Id;
488 Tsk : Entity_Id) return Node_Id
490 Ttyp : constant Entity_Id := Etype (Tsk);
491 Expr : Node_Id;
492 Num : Node_Id;
493 Lo : Node_Id;
494 Hi : Node_Id;
495 Prev : Entity_Id;
496 S : Node_Id;
498 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
499 -- Compute difference between bounds of entry family
501 --------------------------
502 -- Actual_Family_Offset --
503 --------------------------
505 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
507 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
508 -- Replace a reference to a discriminant with a selected component
509 -- denoting the discriminant of the target task.
511 -----------------------------
512 -- Actual_Discriminant_Ref --
513 -----------------------------
515 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
516 Typ : constant Entity_Id := Etype (Bound);
517 B : Node_Id;
519 begin
520 if not Is_Entity_Name (Bound)
521 or else Ekind (Entity (Bound)) /= E_Discriminant
522 then
523 if Nkind (Bound) = N_Attribute_Reference then
524 return Bound;
525 else
526 B := New_Copy_Tree (Bound);
527 end if;
529 else
530 B :=
531 Make_Selected_Component (Sloc,
532 Prefix => New_Copy_Tree (Tsk),
533 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
535 Analyze_And_Resolve (B, Typ);
536 end if;
538 return
539 Make_Attribute_Reference (Sloc,
540 Attribute_Name => Name_Pos,
541 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
542 Expressions => New_List (B));
543 end Actual_Discriminant_Ref;
545 -- Start of processing for Actual_Family_Offset
547 begin
548 return
549 Make_Op_Subtract (Sloc,
550 Left_Opnd => Actual_Discriminant_Ref (Hi),
551 Right_Opnd => Actual_Discriminant_Ref (Lo));
552 end Actual_Family_Offset;
554 -- Start of processing for Actual_Index_Expression
556 begin
557 -- The queues of entries and entry families appear in textual order in
558 -- the associated record. The entry index is computed as the sum of the
559 -- number of queues for all entries that precede the designated one, to
560 -- which is added the index expression, if this expression denotes a
561 -- member of a family.
563 -- The following is a place holder for the count of simple entries
565 Num := Make_Integer_Literal (Sloc, 1);
567 -- We construct an expression which is a series of addition operations.
568 -- See comments in Entry_Index_Expression, which is identical in
569 -- structure.
571 if Present (Index) then
572 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
574 Expr :=
575 Make_Op_Add (Sloc,
576 Left_Opnd => Num,
577 Right_Opnd =>
578 Actual_Family_Offset (
579 Make_Attribute_Reference (Sloc,
580 Attribute_Name => Name_Pos,
581 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
582 Expressions => New_List (Relocate_Node (Index))),
583 Type_Low_Bound (S)));
584 else
585 Expr := Num;
586 end if;
588 -- Now add lengths of preceding entries and entry families
590 Prev := First_Entity (Ttyp);
591 while Chars (Prev) /= Chars (Ent)
592 or else (Ekind (Prev) /= Ekind (Ent))
593 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
594 loop
595 if Ekind (Prev) = E_Entry then
596 Set_Intval (Num, Intval (Num) + 1);
598 elsif Ekind (Prev) = E_Entry_Family then
599 S :=
600 Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
602 -- The need for the following full view retrieval stems from this
603 -- complex case of nested generics and tasking:
605 -- generic
606 -- type Formal_Index is range <>;
607 -- ...
608 -- package Outer is
609 -- type Index is private;
610 -- generic
611 -- ...
612 -- package Inner is
613 -- procedure P;
614 -- end Inner;
615 -- private
616 -- type Index is new Formal_Index range 1 .. 10;
617 -- end Outer;
619 -- package body Outer is
620 -- task type T is
621 -- entry Fam (Index); -- (2)
622 -- entry E;
623 -- end T;
624 -- package body Inner is -- (3)
625 -- procedure P is
626 -- begin
627 -- T.E; -- (1)
628 -- end P;
629 -- end Inner;
630 -- ...
632 -- We are currently building the index expression for the entry
633 -- call "T.E" (1). Part of the expansion must mention the range
634 -- of the discrete type "Index" (2) of entry family "Fam".
636 -- However only the private view of type "Index" is available to
637 -- the inner generic (3) because there was no prior mention of
638 -- the type inside "Inner". This visibility requirement is
639 -- implicit and cannot be detected during the construction of
640 -- the generic trees and needs special handling.
642 if In_Instance_Body
643 and then Is_Private_Type (S)
644 and then Present (Full_View (S))
645 then
646 S := Full_View (S);
647 end if;
649 Lo := Type_Low_Bound (S);
650 Hi := Type_High_Bound (S);
652 Expr :=
653 Make_Op_Add (Sloc,
654 Left_Opnd => Expr,
655 Right_Opnd =>
656 Make_Op_Add (Sloc,
657 Left_Opnd => Actual_Family_Offset (Hi, Lo),
658 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
660 -- Other components are anonymous types to be ignored
662 else
663 null;
664 end if;
666 Next_Entity (Prev);
667 end loop;
669 return Expr;
670 end Actual_Index_Expression;
672 --------------------------
673 -- Add_Formal_Renamings --
674 --------------------------
676 procedure Add_Formal_Renamings
677 (Spec : Node_Id;
678 Decls : List_Id;
679 Ent : Entity_Id;
680 Loc : Source_Ptr)
682 Ptr : constant Entity_Id :=
683 Defining_Identifier
684 (Next (First (Parameter_Specifications (Spec))));
685 -- The name of the formal that holds the address of the parameter block
686 -- for the call.
688 Comp : Entity_Id;
689 Decl : Node_Id;
690 Formal : Entity_Id;
691 New_F : Entity_Id;
692 Renamed_Formal : Node_Id;
694 begin
695 Formal := First_Formal (Ent);
696 while Present (Formal) loop
697 Comp := Entry_Component (Formal);
698 New_F :=
699 Make_Defining_Identifier (Sloc (Formal),
700 Chars => Chars (Formal));
701 Set_Etype (New_F, Etype (Formal));
702 Set_Scope (New_F, Ent);
704 -- Now we set debug info needed on New_F even though it does not come
705 -- from source, so that the debugger will get the right information
706 -- for these generated names.
708 Set_Debug_Info_Needed (New_F);
710 if Ekind (Formal) = E_In_Parameter then
711 Set_Ekind (New_F, E_Constant);
712 else
713 Set_Ekind (New_F, E_Variable);
714 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
715 end if;
717 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
719 Renamed_Formal :=
720 Make_Selected_Component (Loc,
721 Prefix =>
722 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
723 Make_Identifier (Loc, Chars (Ptr))),
724 Selector_Name => New_Occurrence_Of (Comp, Loc));
726 Decl :=
727 Build_Renamed_Formal_Declaration
728 (New_F, Formal, Comp, Renamed_Formal);
730 Append (Decl, Decls);
731 Set_Renamed_Object (Formal, New_F);
732 Next_Formal (Formal);
733 end loop;
734 end Add_Formal_Renamings;
736 ------------------------
737 -- Add_Object_Pointer --
738 ------------------------
740 procedure Add_Object_Pointer
741 (Loc : Source_Ptr;
742 Conc_Typ : Entity_Id;
743 Decls : List_Id)
745 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
746 Decl : Node_Id;
747 Obj_Ptr : Node_Id;
749 begin
750 -- Create the renaming declaration for the Protection object of a
751 -- protected type. _Object is used by Complete_Entry_Body.
752 -- ??? An attempt to make this a renaming was unsuccessful.
754 -- Build the entity for the access type
756 Obj_Ptr :=
757 Make_Defining_Identifier (Loc,
758 New_External_Name (Chars (Rec_Typ), 'P'));
760 -- Generate:
761 -- _object : poVP := poVP!O;
763 Decl :=
764 Make_Object_Declaration (Loc,
765 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
766 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
767 Expression =>
768 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
769 Set_Debug_Info_Needed (Defining_Identifier (Decl));
770 Prepend_To (Decls, Decl);
772 -- Generate:
773 -- type poVP is access poV;
775 Decl :=
776 Make_Full_Type_Declaration (Loc,
777 Defining_Identifier =>
778 Obj_Ptr,
779 Type_Definition =>
780 Make_Access_To_Object_Definition (Loc,
781 Subtype_Indication =>
782 New_Occurrence_Of (Rec_Typ, Loc)));
783 Set_Debug_Info_Needed (Defining_Identifier (Decl));
784 Prepend_To (Decls, Decl);
785 end Add_Object_Pointer;
787 -----------------------
788 -- Build_Accept_Body --
789 -----------------------
791 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
792 Loc : constant Source_Ptr := Sloc (Astat);
793 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
794 New_S : Node_Id;
795 Hand : Node_Id;
796 Call : Node_Id;
797 Ohandle : Node_Id;
799 begin
800 -- At the end of the statement sequence, Complete_Rendezvous is called.
801 -- A label skipping the Complete_Rendezvous, and all other accept
802 -- processing, has already been added for the expansion of requeue
803 -- statements. The Sloc is copied from the last statement since it
804 -- is really part of this last statement.
806 Call :=
807 Build_Runtime_Call
808 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
809 Insert_Before (Last (Statements (Stats)), Call);
810 Analyze (Call);
812 -- If exception handlers are present, then append Complete_Rendezvous
813 -- calls to the handlers, and construct the required outer block. As
814 -- above, the Sloc is copied from the last statement in the sequence.
816 if Present (Exception_Handlers (Stats)) then
817 Hand := First (Exception_Handlers (Stats));
818 while Present (Hand) loop
819 Call :=
820 Build_Runtime_Call
821 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
822 Append (Call, Statements (Hand));
823 Analyze (Call);
824 Next (Hand);
825 end loop;
827 New_S :=
828 Make_Handled_Sequence_Of_Statements (Loc,
829 Statements => New_List (
830 Make_Block_Statement (Loc,
831 Handled_Statement_Sequence => Stats)));
833 else
834 New_S := Stats;
835 end if;
837 -- At this stage we know that the new statement sequence does
838 -- not have an exception handler part, so we supply one to call
839 -- Exceptional_Complete_Rendezvous. This handler is
841 -- when all others =>
842 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
844 -- We handle Abort_Signal to make sure that we properly catch the abort
845 -- case and wake up the caller.
847 Ohandle := Make_Others_Choice (Loc);
848 Set_All_Others (Ohandle);
850 Set_Exception_Handlers (New_S,
851 New_List (
852 Make_Implicit_Exception_Handler (Loc,
853 Exception_Choices => New_List (Ohandle),
855 Statements => New_List (
856 Make_Procedure_Call_Statement (Sloc (Stats),
857 Name => New_Occurrence_Of (
858 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
859 Parameter_Associations => New_List (
860 Make_Function_Call (Sloc (Stats),
861 Name =>
862 New_Occurrence_Of
863 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))))))));
865 Set_Parent (New_S, Astat); -- temp parent for Analyze call
866 Analyze_Exception_Handlers (Exception_Handlers (New_S));
867 Expand_Exception_Handlers (New_S);
869 -- Exceptional_Complete_Rendezvous must be called with abort still
870 -- deferred, which is the case for a "when all others" handler.
872 return New_S;
873 end Build_Accept_Body;
875 -----------------------------------
876 -- Build_Activation_Chain_Entity --
877 -----------------------------------
879 procedure Build_Activation_Chain_Entity (N : Node_Id) is
880 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
881 -- Determine whether an extended return statement has activation chain
883 --------------------------
884 -- Has_Activation_Chain --
885 --------------------------
887 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
888 Decl : Node_Id;
890 begin
891 Decl := First (Return_Object_Declarations (Stmt));
892 while Present (Decl) loop
893 if Nkind (Decl) = N_Object_Declaration
894 and then Chars (Defining_Identifier (Decl)) = Name_uChain
895 then
896 return True;
897 end if;
899 Next (Decl);
900 end loop;
902 return False;
903 end Has_Activation_Chain;
905 -- Local variables
907 Context : Node_Id;
908 Context_Id : Entity_Id;
909 Decls : List_Id;
911 -- Start of processing for Build_Activation_Chain_Entity
913 begin
914 -- Activation chain is never used for sequential elaboration policy, see
915 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
917 if Partition_Elaboration_Policy = 'S' then
918 return;
919 end if;
921 Find_Enclosing_Context (N, Context, Context_Id, Decls);
923 -- If activation chain entity has not been declared already, create one
925 if Nkind (Context) = N_Extended_Return_Statement
926 or else No (Activation_Chain_Entity (Context))
927 then
928 -- Since extended return statements do not store the entity of the
929 -- chain, examine the return object declarations to avoid creating
930 -- a duplicate.
932 if Nkind (Context) = N_Extended_Return_Statement
933 and then Has_Activation_Chain (Context)
934 then
935 return;
936 end if;
938 declare
939 Loc : constant Source_Ptr := Sloc (Context);
940 Chain : Entity_Id;
941 Decl : Node_Id;
943 begin
944 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
946 -- Note: An extended return statement is not really a task
947 -- activator, but it does have an activation chain on which to
948 -- store the tasks temporarily. On successful return, the tasks
949 -- on this chain are moved to the chain passed in by the caller.
950 -- We do not build an Activation_Chain_Entity for an extended
951 -- return statement, because we do not want to build a call to
952 -- Activate_Tasks. Task activation is the responsibility of the
953 -- caller.
955 if Nkind (Context) /= N_Extended_Return_Statement then
956 Set_Activation_Chain_Entity (Context, Chain);
957 end if;
959 Decl :=
960 Make_Object_Declaration (Loc,
961 Defining_Identifier => Chain,
962 Aliased_Present => True,
963 Object_Definition =>
964 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
966 Prepend_To (Decls, Decl);
968 -- Ensure that _chain appears in the proper scope of the context
970 if Context_Id /= Current_Scope then
971 Push_Scope (Context_Id);
972 Analyze (Decl);
973 Pop_Scope;
974 else
975 Analyze (Decl);
976 end if;
977 end;
978 end if;
979 end Build_Activation_Chain_Entity;
981 ----------------------------
982 -- Build_Barrier_Function --
983 ----------------------------
985 function Build_Barrier_Function
986 (N : Node_Id;
987 Ent : Entity_Id;
988 Pid : Node_Id) return Node_Id
990 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
991 Cond : constant Node_Id := Condition (Ent_Formals);
992 Loc : constant Source_Ptr := Sloc (Cond);
993 Func_Id : constant Entity_Id := Barrier_Function (Ent);
994 Op_Decls : constant List_Id := New_List;
995 Stmt : Node_Id;
996 Func_Body : Node_Id;
998 begin
999 -- Add a declaration for the Protection object, renaming declarations
1000 -- for the discriminals and privals and finally a declaration for the
1001 -- entry family index (if applicable).
1003 Install_Private_Data_Declarations (Sloc (N),
1004 Spec_Id => Func_Id,
1005 Conc_Typ => Pid,
1006 Body_Nod => N,
1007 Decls => Op_Decls,
1008 Barrier => True,
1009 Family => Ekind (Ent) = E_Entry_Family);
1011 -- If compiling with -fpreserve-control-flow, make sure we insert an
1012 -- IF statement so that the back-end knows to generate a conditional
1013 -- branch instruction, even if the condition is just the name of a
1014 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1015 -- such redundant IF statements under -fpreserve-control-flow
1016 -- (whether coming from this routine, or directly from source).
1018 if Opt.Suppress_Control_Flow_Optimizations then
1019 Stmt :=
1020 Make_Implicit_If_Statement (Cond,
1021 Condition => Cond,
1022 Then_Statements => New_List (
1023 Make_Simple_Return_Statement (Loc,
1024 New_Occurrence_Of (Standard_True, Loc))),
1026 Else_Statements => New_List (
1027 Make_Simple_Return_Statement (Loc,
1028 New_Occurrence_Of (Standard_False, Loc))));
1030 else
1031 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1032 end if;
1034 -- Note: the condition in the barrier function needs to be properly
1035 -- processed for the C/Fortran boolean possibility, but this happens
1036 -- automatically since the return statement does this normalization.
1038 Func_Body :=
1039 Make_Subprogram_Body (Loc,
1040 Specification =>
1041 Build_Barrier_Function_Specification (Loc,
1042 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1043 Declarations => Op_Decls,
1044 Handled_Statement_Sequence =>
1045 Make_Handled_Sequence_Of_Statements (Loc,
1046 Statements => New_List (Stmt)));
1047 Set_Is_Entry_Barrier_Function (Func_Body);
1049 return Func_Body;
1050 end Build_Barrier_Function;
1052 ------------------------------------------
1053 -- Build_Barrier_Function_Specification --
1054 ------------------------------------------
1056 function Build_Barrier_Function_Specification
1057 (Loc : Source_Ptr;
1058 Def_Id : Entity_Id) return Node_Id
1060 begin
1061 Set_Debug_Info_Needed (Def_Id);
1063 return
1064 Make_Function_Specification (Loc,
1065 Defining_Unit_Name => Def_Id,
1066 Parameter_Specifications => New_List (
1067 Make_Parameter_Specification (Loc,
1068 Defining_Identifier =>
1069 Make_Defining_Identifier (Loc, Name_uO),
1070 Parameter_Type =>
1071 New_Occurrence_Of (RTE (RE_Address), Loc)),
1073 Make_Parameter_Specification (Loc,
1074 Defining_Identifier =>
1075 Make_Defining_Identifier (Loc, Name_uE),
1076 Parameter_Type =>
1077 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1079 Result_Definition =>
1080 New_Occurrence_Of (Standard_Boolean, Loc));
1081 end Build_Barrier_Function_Specification;
1083 --------------------------
1084 -- Build_Call_With_Task --
1085 --------------------------
1087 function Build_Call_With_Task
1088 (N : Node_Id;
1089 E : Entity_Id) return Node_Id
1091 Loc : constant Source_Ptr := Sloc (N);
1092 begin
1093 return
1094 Make_Function_Call (Loc,
1095 Name => New_Occurrence_Of (E, Loc),
1096 Parameter_Associations => New_List (Concurrent_Ref (N)));
1097 end Build_Call_With_Task;
1099 -----------------------------
1100 -- Build_Class_Wide_Master --
1101 -----------------------------
1103 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1104 Loc : constant Source_Ptr := Sloc (Typ);
1105 Master_Decl : Node_Id;
1106 Master_Id : Entity_Id;
1107 Master_Scope : Entity_Id;
1108 Name_Id : Node_Id;
1109 Related_Node : Node_Id;
1110 Ren_Decl : Node_Id;
1112 begin
1113 -- Nothing to do if there is no task hierarchy
1115 if Restriction_Active (No_Task_Hierarchy) then
1116 return;
1117 end if;
1119 -- Find the declaration that created the access type, which is either a
1120 -- type declaration, or an object declaration with an access definition,
1121 -- in which case the type is anonymous.
1123 if Is_Itype (Typ) then
1124 Related_Node := Associated_Node_For_Itype (Typ);
1125 else
1126 Related_Node := Parent (Typ);
1127 end if;
1129 Master_Scope := Find_Master_Scope (Typ);
1131 -- Nothing to do if the master scope already contains a _master entity.
1132 -- The only exception to this is the following scenario:
1134 -- Source_Scope
1135 -- Transient_Scope_1
1136 -- _master
1138 -- Transient_Scope_2
1139 -- use of master
1141 -- In this case the source scope is marked as having the master entity
1142 -- even though the actual declaration appears inside an inner scope. If
1143 -- the second transient scope requires a _master, it cannot use the one
1144 -- already declared because the entity is not visible.
1146 Name_Id := Make_Identifier (Loc, Name_uMaster);
1147 Master_Decl := Empty;
1149 if not Has_Master_Entity (Master_Scope)
1150 or else No (Current_Entity_In_Scope (Name_Id))
1151 then
1152 begin
1153 Set_Has_Master_Entity (Master_Scope);
1155 -- Generate:
1156 -- _master : constant Integer := Current_Master.all;
1158 Master_Decl :=
1159 Make_Object_Declaration (Loc,
1160 Defining_Identifier =>
1161 Make_Defining_Identifier (Loc, Name_uMaster),
1162 Constant_Present => True,
1163 Object_Definition =>
1164 New_Occurrence_Of (Standard_Integer, Loc),
1165 Expression =>
1166 Make_Explicit_Dereference (Loc,
1167 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1169 Insert_Action (Find_Hook_Context (Related_Node), Master_Decl);
1170 Analyze (Master_Decl);
1172 -- Mark the containing scope as a task master. Masters associated
1173 -- with return statements are already marked at this stage (see
1174 -- Analyze_Subprogram_Body).
1176 if Ekind (Current_Scope) /= E_Return_Statement then
1177 declare
1178 Par : Node_Id := Related_Node;
1180 begin
1181 while Nkind (Par) /= N_Compilation_Unit loop
1182 Par := Parent (Par);
1184 -- If we fall off the top, we are at the outer level,
1185 -- and the environment task is our effective master,
1186 -- so nothing to mark.
1188 if Nkind_In (Par, N_Block_Statement,
1189 N_Subprogram_Body,
1190 N_Task_Body)
1191 then
1192 Set_Is_Task_Master (Par);
1193 exit;
1194 end if;
1195 end loop;
1196 end;
1197 end if;
1198 end;
1199 end if;
1201 Master_Id :=
1202 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1204 -- Generate:
1205 -- typeMnn renames _master;
1207 Ren_Decl :=
1208 Make_Object_Renaming_Declaration (Loc,
1209 Defining_Identifier => Master_Id,
1210 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1211 Name => Name_Id);
1213 -- If the master is declared locally, add the renaming declaration
1214 -- immediately after it, to prevent access-before-elaboration in the
1215 -- back-end.
1217 if Present (Master_Decl) then
1218 Insert_After (Master_Decl, Ren_Decl);
1219 Analyze (Ren_Decl);
1221 else
1222 Insert_Action (Related_Node, Ren_Decl);
1223 end if;
1225 Set_Master_Id (Typ, Master_Id);
1226 end Build_Class_Wide_Master;
1228 ----------------------------
1229 -- Build_Contract_Wrapper --
1230 ----------------------------
1232 procedure Build_Contract_Wrapper (E : Entity_Id; Decl : Node_Id) is
1233 Conc_Typ : constant Entity_Id := Scope (E);
1234 Loc : constant Source_Ptr := Sloc (E);
1236 procedure Add_Discriminant_Renamings
1237 (Obj_Id : Entity_Id;
1238 Decls : List_Id);
1239 -- Add renaming declarations for all discriminants of concurrent type
1240 -- Conc_Typ. Obj_Id is the entity of the wrapper formal parameter which
1241 -- represents the concurrent object.
1243 procedure Add_Matching_Formals
1244 (Formals : List_Id;
1245 Actuals : in out List_Id);
1246 -- Add formal parameters that match those of entry E to list Formals.
1247 -- The routine also adds matching actuals for the new formals to list
1248 -- Actuals.
1250 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id);
1251 -- Relocate pragma Prag to list To. The routine creates a new list if
1252 -- To does not exist.
1254 --------------------------------
1255 -- Add_Discriminant_Renamings --
1256 --------------------------------
1258 procedure Add_Discriminant_Renamings
1259 (Obj_Id : Entity_Id;
1260 Decls : List_Id)
1262 Discr : Entity_Id;
1264 begin
1265 -- Inspect the discriminants of the concurrent type and generate a
1266 -- renaming for each one.
1268 if Has_Discriminants (Conc_Typ) then
1269 Discr := First_Discriminant (Conc_Typ);
1270 while Present (Discr) loop
1271 Prepend_To (Decls,
1272 Make_Object_Renaming_Declaration (Loc,
1273 Defining_Identifier =>
1274 Make_Defining_Identifier (Loc, Chars (Discr)),
1275 Subtype_Mark =>
1276 New_Occurrence_Of (Etype (Discr), Loc),
1277 Name =>
1278 Make_Selected_Component (Loc,
1279 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1280 Selector_Name =>
1281 Make_Identifier (Loc, Chars (Discr)))));
1283 Next_Discriminant (Discr);
1284 end loop;
1285 end if;
1286 end Add_Discriminant_Renamings;
1288 --------------------------
1289 -- Add_Matching_Formals --
1290 --------------------------
1292 procedure Add_Matching_Formals
1293 (Formals : List_Id;
1294 Actuals : in out List_Id)
1296 Formal : Entity_Id;
1297 New_Formal : Entity_Id;
1299 begin
1300 -- Inspect the formal parameters of the entry and generate a new
1301 -- matching formal with the same name for the wrapper. A reference
1302 -- to the new formal becomes an actual in the entry call.
1304 Formal := First_Formal (E);
1305 while Present (Formal) loop
1306 New_Formal := Make_Defining_Identifier (Loc, Chars (Formal));
1307 Append_To (Formals,
1308 Make_Parameter_Specification (Loc,
1309 Defining_Identifier => New_Formal,
1310 In_Present => In_Present (Parent (Formal)),
1311 Out_Present => Out_Present (Parent (Formal)),
1312 Parameter_Type =>
1313 New_Occurrence_Of (Etype (Formal), Loc)));
1315 if No (Actuals) then
1316 Actuals := New_List;
1317 end if;
1319 Append_To (Actuals, New_Occurrence_Of (New_Formal, Loc));
1320 Next_Formal (Formal);
1321 end loop;
1322 end Add_Matching_Formals;
1324 ---------------------
1325 -- Transfer_Pragma --
1326 ---------------------
1328 procedure Transfer_Pragma (Prag : Node_Id; To : in out List_Id) is
1329 New_Prag : Node_Id;
1331 begin
1332 if No (To) then
1333 To := New_List;
1334 end if;
1336 New_Prag := Relocate_Node (Prag);
1338 Set_Analyzed (New_Prag, False);
1339 Append (New_Prag, To);
1340 end Transfer_Pragma;
1342 -- Local variables
1344 Items : constant Node_Id := Contract (E);
1345 Actuals : List_Id := No_List;
1346 Call : Node_Id;
1347 Call_Nam : Node_Id;
1348 Decls : List_Id := No_List;
1349 Formals : List_Id;
1350 Has_Pragma : Boolean := False;
1351 Index_Id : Entity_Id;
1352 Obj_Id : Entity_Id;
1353 Prag : Node_Id;
1354 Wrapper_Id : Entity_Id;
1356 -- Start of processing for Build_Contract_Wrapper
1358 begin
1359 -- This routine generates a specialized wrapper for a protected or task
1360 -- entry [family] which implements precondition/postcondition semantics.
1361 -- Preconditions and case guards of contract cases are checked before
1362 -- the protected action or rendezvous takes place. Postconditions and
1363 -- consequences of contract cases are checked after the protected action
1364 -- or rendezvous takes place. The structure of the generated wrapper is
1365 -- as follows:
1367 -- procedure Wrapper
1368 -- (Obj_Id : Conc_Typ; -- concurrent object
1369 -- [Index : Index_Typ;] -- index of entry family
1370 -- [Formal_1 : ...; -- parameters of original entry
1371 -- Formal_N : ...])
1372 -- is
1373 -- [Discr_1 : ... renames Obj_Id.Discr_1; -- discriminant
1374 -- Discr_N : ... renames Obj_Id.Discr_N;] -- renamings
1376 -- <precondition checks>
1377 -- <case guard checks>
1379 -- procedure _Postconditions is
1380 -- begin
1381 -- <postcondition checks>
1382 -- <consequence checks>
1383 -- end _Postconditions;
1385 -- begin
1386 -- Entry_Call (Obj_Id, [Index,] [Formal_1, Formal_N]);
1387 -- _Postconditions;
1388 -- end Wrapper;
1390 -- Create the wrapper only when the entry has at least one executable
1391 -- contract item such as contract cases, precondition or postcondition.
1393 if Present (Items) then
1395 -- Inspect the list of pre/postconditions and transfer all available
1396 -- pragmas to the declarative list of the wrapper.
1398 Prag := Pre_Post_Conditions (Items);
1399 while Present (Prag) loop
1400 if Nam_In (Pragma_Name_Unmapped (Prag),
1401 Name_Postcondition, Name_Precondition)
1402 and then Is_Checked (Prag)
1403 then
1404 Has_Pragma := True;
1405 Transfer_Pragma (Prag, To => Decls);
1406 end if;
1408 Prag := Next_Pragma (Prag);
1409 end loop;
1411 -- Inspect the list of test/contract cases and transfer only contract
1412 -- cases pragmas to the declarative part of the wrapper.
1414 Prag := Contract_Test_Cases (Items);
1415 while Present (Prag) loop
1416 if Pragma_Name (Prag) = Name_Contract_Cases
1417 and then Is_Checked (Prag)
1418 then
1419 Has_Pragma := True;
1420 Transfer_Pragma (Prag, To => Decls);
1421 end if;
1423 Prag := Next_Pragma (Prag);
1424 end loop;
1425 end if;
1427 -- The entry lacks executable contract items and a wrapper is not needed
1429 if not Has_Pragma then
1430 return;
1431 end if;
1433 -- Create the profile of the wrapper. The first formal parameter is the
1434 -- concurrent object.
1436 Obj_Id :=
1437 Make_Defining_Identifier (Loc,
1438 Chars => New_External_Name (Chars (Conc_Typ), 'A'));
1440 Formals := New_List (
1441 Make_Parameter_Specification (Loc,
1442 Defining_Identifier => Obj_Id,
1443 Out_Present => True,
1444 In_Present => True,
1445 Parameter_Type => New_Occurrence_Of (Conc_Typ, Loc)));
1447 -- Construct the call to the original entry. The call will be gradually
1448 -- augmented with an optional entry index and extra parameters.
1450 Call_Nam :=
1451 Make_Selected_Component (Loc,
1452 Prefix => New_Occurrence_Of (Obj_Id, Loc),
1453 Selector_Name => New_Occurrence_Of (E, Loc));
1455 -- When creating a wrapper for an entry family, the second formal is the
1456 -- entry index.
1458 if Ekind (E) = E_Entry_Family then
1459 Index_Id := Make_Defining_Identifier (Loc, Name_I);
1461 Append_To (Formals,
1462 Make_Parameter_Specification (Loc,
1463 Defining_Identifier => Index_Id,
1464 Parameter_Type =>
1465 New_Occurrence_Of (Entry_Index_Type (E), Loc)));
1467 -- The call to the original entry becomes an indexed component to
1468 -- accommodate the entry index.
1470 Call_Nam :=
1471 Make_Indexed_Component (Loc,
1472 Prefix => Call_Nam,
1473 Expressions => New_List (New_Occurrence_Of (Index_Id, Loc)));
1474 end if;
1476 -- Add formal parameters to match those of the entry and build actuals
1477 -- for the entry call.
1479 Add_Matching_Formals (Formals, Actuals);
1481 Call :=
1482 Make_Procedure_Call_Statement (Loc,
1483 Name => Call_Nam,
1484 Parameter_Associations => Actuals);
1486 -- Add renaming declarations for the discriminants of the enclosing type
1487 -- as the various contract items may reference them.
1489 Add_Discriminant_Renamings (Obj_Id, Decls);
1491 Wrapper_Id :=
1492 Make_Defining_Identifier (Loc, New_External_Name (Chars (E), 'E'));
1493 Set_Contract_Wrapper (E, Wrapper_Id);
1494 Set_Is_Entry_Wrapper (Wrapper_Id);
1496 -- The wrapper body is analyzed when the enclosing type is frozen
1498 Append_Freeze_Action (Defining_Entity (Decl),
1499 Make_Subprogram_Body (Loc,
1500 Specification =>
1501 Make_Procedure_Specification (Loc,
1502 Defining_Unit_Name => Wrapper_Id,
1503 Parameter_Specifications => Formals),
1504 Declarations => Decls,
1505 Handled_Statement_Sequence =>
1506 Make_Handled_Sequence_Of_Statements (Loc,
1507 Statements => New_List (Call))));
1508 end Build_Contract_Wrapper;
1510 --------------------------------
1511 -- Build_Corresponding_Record --
1512 --------------------------------
1514 function Build_Corresponding_Record
1515 (N : Node_Id;
1516 Ctyp : Entity_Id;
1517 Loc : Source_Ptr) return Node_Id
1519 Rec_Ent : constant Entity_Id :=
1520 Make_Defining_Identifier
1521 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1522 Disc : Entity_Id;
1523 Dlist : List_Id;
1524 New_Disc : Entity_Id;
1525 Cdecls : List_Id;
1527 begin
1528 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1529 Set_Ekind (Rec_Ent, E_Record_Type);
1530 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1531 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1532 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1533 Set_Stored_Constraint (Rec_Ent, No_Elist);
1534 Cdecls := New_List;
1536 -- Use discriminals to create list of discriminants for record, and
1537 -- create new discriminals for use in default expressions, etc. It is
1538 -- worth noting that a task discriminant gives rise to 5 entities;
1540 -- a) The original discriminant.
1541 -- b) The discriminal for use in the task.
1542 -- c) The discriminant of the corresponding record.
1543 -- d) The discriminal for the init proc of the corresponding record.
1544 -- e) The local variable that renames the discriminant in the procedure
1545 -- for the task body.
1547 -- In fact the discriminals b) are used in the renaming declarations
1548 -- for e). See details in einfo (Handling of Discriminants).
1550 if Present (Discriminant_Specifications (N)) then
1551 Dlist := New_List;
1552 Disc := First_Discriminant (Ctyp);
1554 while Present (Disc) loop
1555 New_Disc := CR_Discriminant (Disc);
1557 Append_To (Dlist,
1558 Make_Discriminant_Specification (Loc,
1559 Defining_Identifier => New_Disc,
1560 Discriminant_Type =>
1561 New_Occurrence_Of (Etype (Disc), Loc),
1562 Expression =>
1563 New_Copy (Discriminant_Default_Value (Disc))));
1565 Next_Discriminant (Disc);
1566 end loop;
1568 else
1569 Dlist := No_List;
1570 end if;
1572 -- Now we can construct the record type declaration. Note that this
1573 -- record is "limited tagged". It is "limited" to reflect the underlying
1574 -- limitedness of the task or protected object that it represents, and
1575 -- ensuring for example that it is properly passed by reference. It is
1576 -- "tagged" to give support to dispatching calls through interfaces. We
1577 -- propagate here the list of interfaces covered by the concurrent type
1578 -- (Ada 2005: AI-345).
1580 return
1581 Make_Full_Type_Declaration (Loc,
1582 Defining_Identifier => Rec_Ent,
1583 Discriminant_Specifications => Dlist,
1584 Type_Definition =>
1585 Make_Record_Definition (Loc,
1586 Component_List =>
1587 Make_Component_List (Loc, Component_Items => Cdecls),
1588 Tagged_Present =>
1589 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1590 Interface_List => Interface_List (N),
1591 Limited_Present => True));
1592 end Build_Corresponding_Record;
1594 ---------------------------------
1595 -- Build_Dispatching_Tag_Check --
1596 ---------------------------------
1598 function Build_Dispatching_Tag_Check
1599 (K : Entity_Id;
1600 N : Node_Id) return Node_Id
1602 Loc : constant Source_Ptr := Sloc (N);
1604 begin
1605 return
1606 Make_Op_Or (Loc,
1607 Make_Op_Eq (Loc,
1608 Left_Opnd =>
1609 New_Occurrence_Of (K, Loc),
1610 Right_Opnd =>
1611 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1613 Make_Op_Eq (Loc,
1614 Left_Opnd =>
1615 New_Occurrence_Of (K, Loc),
1616 Right_Opnd =>
1617 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1618 end Build_Dispatching_Tag_Check;
1620 ----------------------------------
1621 -- Build_Entry_Count_Expression --
1622 ----------------------------------
1624 function Build_Entry_Count_Expression
1625 (Concurrent_Type : Node_Id;
1626 Component_List : List_Id;
1627 Loc : Source_Ptr) return Node_Id
1629 Eindx : Nat;
1630 Ent : Entity_Id;
1631 Ecount : Node_Id;
1632 Comp : Node_Id;
1633 Lo : Node_Id;
1634 Hi : Node_Id;
1635 Typ : Entity_Id;
1636 Large : Boolean;
1638 begin
1639 -- Count number of non-family entries
1641 Eindx := 0;
1642 Ent := First_Entity (Concurrent_Type);
1643 while Present (Ent) loop
1644 if Ekind (Ent) = E_Entry then
1645 Eindx := Eindx + 1;
1646 end if;
1648 Next_Entity (Ent);
1649 end loop;
1651 Ecount := Make_Integer_Literal (Loc, Eindx);
1653 -- Loop through entry families building the addition nodes
1655 Ent := First_Entity (Concurrent_Type);
1656 Comp := First (Component_List);
1657 while Present (Ent) loop
1658 if Ekind (Ent) = E_Entry_Family then
1659 while Chars (Ent) /= Chars (Defining_Identifier (Comp)) loop
1660 Next (Comp);
1661 end loop;
1663 Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
1664 Hi := Type_High_Bound (Typ);
1665 Lo := Type_Low_Bound (Typ);
1666 Large := Is_Potentially_Large_Family
1667 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1668 Ecount :=
1669 Make_Op_Add (Loc,
1670 Left_Opnd => Ecount,
1671 Right_Opnd =>
1672 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1673 end if;
1675 Next_Entity (Ent);
1676 end loop;
1678 return Ecount;
1679 end Build_Entry_Count_Expression;
1681 ---------------------------
1682 -- Build_Parameter_Block --
1683 ---------------------------
1685 function Build_Parameter_Block
1686 (Loc : Source_Ptr;
1687 Actuals : List_Id;
1688 Formals : List_Id;
1689 Decls : List_Id) return Entity_Id
1691 Actual : Entity_Id;
1692 Comp_Nam : Node_Id;
1693 Comps : List_Id;
1694 Formal : Entity_Id;
1695 Has_Comp : Boolean := False;
1696 Rec_Nam : Node_Id;
1698 begin
1699 Actual := First (Actuals);
1700 Comps := New_List;
1701 Formal := Defining_Identifier (First (Formals));
1703 while Present (Actual) loop
1704 if not Is_Controlling_Actual (Actual) then
1706 -- Generate:
1707 -- type Ann is access all <actual-type>
1709 Comp_Nam := Make_Temporary (Loc, 'A');
1710 Set_Is_Param_Block_Component_Type (Comp_Nam);
1712 Append_To (Decls,
1713 Make_Full_Type_Declaration (Loc,
1714 Defining_Identifier => Comp_Nam,
1715 Type_Definition =>
1716 Make_Access_To_Object_Definition (Loc,
1717 All_Present => True,
1718 Constant_Present => Ekind (Formal) = E_In_Parameter,
1719 Subtype_Indication =>
1720 New_Occurrence_Of (Etype (Actual), Loc))));
1722 -- Generate:
1723 -- Param : Ann;
1725 Append_To (Comps,
1726 Make_Component_Declaration (Loc,
1727 Defining_Identifier =>
1728 Make_Defining_Identifier (Loc, Chars (Formal)),
1729 Component_Definition =>
1730 Make_Component_Definition (Loc,
1731 Aliased_Present =>
1732 False,
1733 Subtype_Indication =>
1734 New_Occurrence_Of (Comp_Nam, Loc))));
1736 Has_Comp := True;
1737 end if;
1739 Next_Actual (Actual);
1740 Next_Formal_With_Extras (Formal);
1741 end loop;
1743 Rec_Nam := Make_Temporary (Loc, 'P');
1745 if Has_Comp then
1747 -- Generate:
1748 -- type Pnn is record
1749 -- Param1 : Ann1;
1750 -- ...
1751 -- ParamN : AnnN;
1753 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1754 -- the original parameter names and Ann1 .. AnnN are the access to
1755 -- actual types.
1757 Append_To (Decls,
1758 Make_Full_Type_Declaration (Loc,
1759 Defining_Identifier =>
1760 Rec_Nam,
1761 Type_Definition =>
1762 Make_Record_Definition (Loc,
1763 Component_List =>
1764 Make_Component_List (Loc, Comps))));
1765 else
1766 -- Generate:
1767 -- type Pnn is null record;
1769 Append_To (Decls,
1770 Make_Full_Type_Declaration (Loc,
1771 Defining_Identifier =>
1772 Rec_Nam,
1773 Type_Definition =>
1774 Make_Record_Definition (Loc,
1775 Null_Present => True,
1776 Component_List => Empty)));
1777 end if;
1779 return Rec_Nam;
1780 end Build_Parameter_Block;
1782 --------------------------------------
1783 -- Build_Renamed_Formal_Declaration --
1784 --------------------------------------
1786 function Build_Renamed_Formal_Declaration
1787 (New_F : Entity_Id;
1788 Formal : Entity_Id;
1789 Comp : Entity_Id;
1790 Renamed_Formal : Node_Id) return Node_Id
1792 Loc : constant Source_Ptr := Sloc (New_F);
1793 Decl : Node_Id;
1795 begin
1796 -- If the formal is a tagged incomplete type, it is already passed
1797 -- by reference, so it is sufficient to rename the pointer component
1798 -- that corresponds to the actual. Otherwise we need to dereference
1799 -- the pointer component to obtain the actual.
1801 if Is_Incomplete_Type (Etype (Formal))
1802 and then Is_Tagged_Type (Etype (Formal))
1803 then
1804 Decl :=
1805 Make_Object_Renaming_Declaration (Loc,
1806 Defining_Identifier => New_F,
1807 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
1808 Name => Renamed_Formal);
1810 else
1811 Decl :=
1812 Make_Object_Renaming_Declaration (Loc,
1813 Defining_Identifier => New_F,
1814 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
1815 Name =>
1816 Make_Explicit_Dereference (Loc, Renamed_Formal));
1817 end if;
1819 return Decl;
1820 end Build_Renamed_Formal_Declaration;
1822 --------------------------
1823 -- Build_Wrapper_Bodies --
1824 --------------------------
1826 procedure Build_Wrapper_Bodies
1827 (Loc : Source_Ptr;
1828 Typ : Entity_Id;
1829 N : Node_Id)
1831 Rec_Typ : Entity_Id;
1833 function Build_Wrapper_Body
1834 (Loc : Source_Ptr;
1835 Subp_Id : Entity_Id;
1836 Obj_Typ : Entity_Id;
1837 Formals : List_Id) return Node_Id;
1838 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
1839 -- associated with a protected or task type. Subp_Id is the subprogram
1840 -- name which will be wrapped. Obj_Typ is the type of the new formal
1841 -- parameter which handles dispatching and object notation. Formals are
1842 -- the original formals of Subp_Id which will be explicitly replicated.
1844 ------------------------
1845 -- Build_Wrapper_Body --
1846 ------------------------
1848 function Build_Wrapper_Body
1849 (Loc : Source_Ptr;
1850 Subp_Id : Entity_Id;
1851 Obj_Typ : Entity_Id;
1852 Formals : List_Id) return Node_Id
1854 Body_Spec : Node_Id;
1856 begin
1857 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
1859 -- The subprogram is not overriding or is not a primitive declared
1860 -- between two views.
1862 if No (Body_Spec) then
1863 return Empty;
1864 end if;
1866 declare
1867 Actuals : List_Id := No_List;
1868 Conv_Id : Node_Id;
1869 First_Form : Node_Id;
1870 Formal : Node_Id;
1871 Nam : Node_Id;
1873 begin
1874 -- Map formals to actuals. Use the list built for the wrapper
1875 -- spec, skipping the object notation parameter.
1877 First_Form := First (Parameter_Specifications (Body_Spec));
1879 Formal := First_Form;
1880 Next (Formal);
1882 if Present (Formal) then
1883 Actuals := New_List;
1884 while Present (Formal) loop
1885 Append_To (Actuals,
1886 Make_Identifier (Loc,
1887 Chars => Chars (Defining_Identifier (Formal))));
1888 Next (Formal);
1889 end loop;
1890 end if;
1892 -- Special processing for primitives declared between a private
1893 -- type and its completion: the wrapper needs a properly typed
1894 -- parameter if the wrapped operation has a controlling first
1895 -- parameter. Note that this might not be the case for a function
1896 -- with a controlling result.
1898 if Is_Private_Primitive_Subprogram (Subp_Id) then
1899 if No (Actuals) then
1900 Actuals := New_List;
1901 end if;
1903 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
1904 Prepend_To (Actuals,
1905 Unchecked_Convert_To
1906 (Corresponding_Concurrent_Type (Obj_Typ),
1907 Make_Identifier (Loc, Name_uO)));
1909 else
1910 Prepend_To (Actuals,
1911 Make_Identifier (Loc,
1912 Chars => Chars (Defining_Identifier (First_Form))));
1913 end if;
1915 Nam := New_Occurrence_Of (Subp_Id, Loc);
1916 else
1917 -- An access-to-variable object parameter requires an explicit
1918 -- dereference in the unchecked conversion. This case occurs
1919 -- when a protected entry wrapper must override an interface
1920 -- level procedure with interface access as first parameter.
1922 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
1924 if Nkind (Parameter_Type (First_Form)) =
1925 N_Access_Definition
1926 then
1927 Conv_Id :=
1928 Make_Explicit_Dereference (Loc,
1929 Prefix => Make_Identifier (Loc, Name_uO));
1930 else
1931 Conv_Id := Make_Identifier (Loc, Name_uO);
1932 end if;
1934 Nam :=
1935 Make_Selected_Component (Loc,
1936 Prefix =>
1937 Unchecked_Convert_To
1938 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
1939 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
1940 end if;
1942 -- Create the subprogram body. For a function, the call to the
1943 -- actual subprogram has to be converted to the corresponding
1944 -- record if it is a controlling result.
1946 if Ekind (Subp_Id) = E_Function then
1947 declare
1948 Res : Node_Id;
1950 begin
1951 Res :=
1952 Make_Function_Call (Loc,
1953 Name => Nam,
1954 Parameter_Associations => Actuals);
1956 if Has_Controlling_Result (Subp_Id) then
1957 Res :=
1958 Unchecked_Convert_To
1959 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
1960 end if;
1962 return
1963 Make_Subprogram_Body (Loc,
1964 Specification => Body_Spec,
1965 Declarations => Empty_List,
1966 Handled_Statement_Sequence =>
1967 Make_Handled_Sequence_Of_Statements (Loc,
1968 Statements => New_List (
1969 Make_Simple_Return_Statement (Loc, Res))));
1970 end;
1972 else
1973 return
1974 Make_Subprogram_Body (Loc,
1975 Specification => Body_Spec,
1976 Declarations => Empty_List,
1977 Handled_Statement_Sequence =>
1978 Make_Handled_Sequence_Of_Statements (Loc,
1979 Statements => New_List (
1980 Make_Procedure_Call_Statement (Loc,
1981 Name => Nam,
1982 Parameter_Associations => Actuals))));
1983 end if;
1984 end;
1985 end Build_Wrapper_Body;
1987 -- Start of processing for Build_Wrapper_Bodies
1989 begin
1990 if Is_Concurrent_Type (Typ) then
1991 Rec_Typ := Corresponding_Record_Type (Typ);
1992 else
1993 Rec_Typ := Typ;
1994 end if;
1996 -- Generate wrapper bodies for a concurrent type which implements an
1997 -- interface.
1999 if Present (Interfaces (Rec_Typ)) then
2000 declare
2001 Insert_Nod : Node_Id;
2002 Prim : Entity_Id;
2003 Prim_Elmt : Elmt_Id;
2004 Prim_Decl : Node_Id;
2005 Subp : Entity_Id;
2006 Wrap_Body : Node_Id;
2007 Wrap_Id : Entity_Id;
2009 begin
2010 Insert_Nod := N;
2012 -- Examine all primitive operations of the corresponding record
2013 -- type, looking for wrapper specs. Generate bodies in order to
2014 -- complete them.
2016 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
2017 while Present (Prim_Elmt) loop
2018 Prim := Node (Prim_Elmt);
2020 if (Ekind (Prim) = E_Function
2021 or else Ekind (Prim) = E_Procedure)
2022 and then Is_Primitive_Wrapper (Prim)
2023 then
2024 Subp := Wrapped_Entity (Prim);
2025 Prim_Decl := Parent (Parent (Prim));
2027 Wrap_Body :=
2028 Build_Wrapper_Body (Loc,
2029 Subp_Id => Subp,
2030 Obj_Typ => Rec_Typ,
2031 Formals => Parameter_Specifications (Parent (Subp)));
2032 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
2034 Set_Corresponding_Spec (Wrap_Body, Prim);
2035 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
2037 Insert_After (Insert_Nod, Wrap_Body);
2038 Insert_Nod := Wrap_Body;
2040 Analyze (Wrap_Body);
2041 end if;
2043 Next_Elmt (Prim_Elmt);
2044 end loop;
2045 end;
2046 end if;
2047 end Build_Wrapper_Bodies;
2049 ------------------------
2050 -- Build_Wrapper_Spec --
2051 ------------------------
2053 function Build_Wrapper_Spec
2054 (Subp_Id : Entity_Id;
2055 Obj_Typ : Entity_Id;
2056 Formals : List_Id) return Node_Id
2058 function Overriding_Possible
2059 (Iface_Op : Entity_Id;
2060 Wrapper : Entity_Id) return Boolean;
2061 -- Determine whether a primitive operation can be overridden by Wrapper.
2062 -- Iface_Op is the candidate primitive operation of an interface type,
2063 -- Wrapper is the generated entry wrapper.
2065 function Replicate_Formals
2066 (Loc : Source_Ptr;
2067 Formals : List_Id) return List_Id;
2068 -- An explicit parameter replication is required due to the Is_Entry_
2069 -- Formal flag being set for all the formals of an entry. The explicit
2070 -- replication removes the flag that would otherwise cause a different
2071 -- path of analysis.
2073 -------------------------
2074 -- Overriding_Possible --
2075 -------------------------
2077 function Overriding_Possible
2078 (Iface_Op : Entity_Id;
2079 Wrapper : Entity_Id) return Boolean
2081 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
2082 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
2084 function Type_Conformant_Parameters
2085 (Iface_Op_Params : List_Id;
2086 Wrapper_Params : List_Id) return Boolean;
2087 -- Determine whether the parameters of the generated entry wrapper
2088 -- and those of a primitive operation are type conformant. During
2089 -- this check, the first parameter of the primitive operation is
2090 -- skipped if it is a controlling argument: protected functions
2091 -- may have a controlling result.
2093 --------------------------------
2094 -- Type_Conformant_Parameters --
2095 --------------------------------
2097 function Type_Conformant_Parameters
2098 (Iface_Op_Params : List_Id;
2099 Wrapper_Params : List_Id) return Boolean
2101 Iface_Op_Param : Node_Id;
2102 Iface_Op_Typ : Entity_Id;
2103 Wrapper_Param : Node_Id;
2104 Wrapper_Typ : Entity_Id;
2106 begin
2107 -- Skip the first (controlling) parameter of primitive operation
2109 Iface_Op_Param := First (Iface_Op_Params);
2111 if Present (First_Formal (Iface_Op))
2112 and then Is_Controlling_Formal (First_Formal (Iface_Op))
2113 then
2114 Iface_Op_Param := Next (Iface_Op_Param);
2115 end if;
2117 Wrapper_Param := First (Wrapper_Params);
2118 while Present (Iface_Op_Param)
2119 and then Present (Wrapper_Param)
2120 loop
2121 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
2122 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
2124 -- The two parameters must be mode conformant
2126 if not Conforming_Types
2127 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
2128 then
2129 return False;
2130 end if;
2132 Next (Iface_Op_Param);
2133 Next (Wrapper_Param);
2134 end loop;
2136 -- One of the lists is longer than the other
2138 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
2139 return False;
2140 end if;
2142 return True;
2143 end Type_Conformant_Parameters;
2145 -- Start of processing for Overriding_Possible
2147 begin
2148 if Chars (Iface_Op) /= Chars (Wrapper) then
2149 return False;
2150 end if;
2152 -- If an inherited subprogram is implemented by a protected procedure
2153 -- or an entry, then the first parameter of the inherited subprogram
2154 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
2156 if Ekind (Iface_Op) = E_Procedure
2157 and then Present (Parameter_Specifications (Iface_Op_Spec))
2158 then
2159 declare
2160 Obj_Param : constant Node_Id :=
2161 First (Parameter_Specifications (Iface_Op_Spec));
2162 begin
2163 if not Out_Present (Obj_Param)
2164 and then Nkind (Parameter_Type (Obj_Param)) /=
2165 N_Access_Definition
2166 then
2167 return False;
2168 end if;
2169 end;
2170 end if;
2172 return
2173 Type_Conformant_Parameters
2174 (Parameter_Specifications (Iface_Op_Spec),
2175 Parameter_Specifications (Wrapper_Spec));
2176 end Overriding_Possible;
2178 -----------------------
2179 -- Replicate_Formals --
2180 -----------------------
2182 function Replicate_Formals
2183 (Loc : Source_Ptr;
2184 Formals : List_Id) return List_Id
2186 New_Formals : constant List_Id := New_List;
2187 Formal : Node_Id;
2188 Param_Type : Node_Id;
2190 begin
2191 Formal := First (Formals);
2193 -- Skip the object parameter when dealing with primitives declared
2194 -- between two views.
2196 if Is_Private_Primitive_Subprogram (Subp_Id)
2197 and then not Has_Controlling_Result (Subp_Id)
2198 then
2199 Formal := Next (Formal);
2200 end if;
2202 while Present (Formal) loop
2204 -- Create an explicit copy of the entry parameter
2206 -- When creating the wrapper subprogram for a primitive operation
2207 -- of a protected interface we must construct an equivalent
2208 -- signature to that of the overriding operation. For regular
2209 -- parameters we can just use the type of the formal, but for
2210 -- access to subprogram parameters we need to reanalyze the
2211 -- parameter type to create local entities for the signature of
2212 -- the subprogram type. Using the entities of the overriding
2213 -- subprogram will result in out-of-scope errors in the back-end.
2215 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2216 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2217 else
2218 Param_Type :=
2219 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2220 end if;
2222 Append_To (New_Formals,
2223 Make_Parameter_Specification (Loc,
2224 Defining_Identifier =>
2225 Make_Defining_Identifier (Loc,
2226 Chars => Chars (Defining_Identifier (Formal))),
2227 In_Present => In_Present (Formal),
2228 Out_Present => Out_Present (Formal),
2229 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2230 Parameter_Type => Param_Type));
2232 Next (Formal);
2233 end loop;
2235 return New_Formals;
2236 end Replicate_Formals;
2238 -- Local variables
2240 Loc : constant Source_Ptr := Sloc (Subp_Id);
2241 First_Param : Node_Id := Empty;
2242 Iface : Entity_Id;
2243 Iface_Elmt : Elmt_Id;
2244 Iface_Op : Entity_Id;
2245 Iface_Op_Elmt : Elmt_Id;
2246 Overridden_Subp : Entity_Id;
2248 -- Start of processing for Build_Wrapper_Spec
2250 begin
2251 -- No point in building wrappers for untagged concurrent types
2253 pragma Assert (Is_Tagged_Type (Obj_Typ));
2255 -- Check if this subprogram has a profile that matches some interface
2256 -- primitive.
2258 Check_Synchronized_Overriding (Subp_Id, Overridden_Subp);
2260 if Present (Overridden_Subp) then
2261 First_Param :=
2262 First (Parameter_Specifications (Parent (Overridden_Subp)));
2264 -- An entry or a protected procedure can override a routine where the
2265 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2266 -- type. Since the wrapper must have the exact same signature as that of
2267 -- the overridden subprogram, we try to find the overriding candidate
2268 -- and use its controlling formal.
2270 -- Check every implemented interface
2272 elsif Present (Interfaces (Obj_Typ)) then
2273 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2274 Search : while Present (Iface_Elmt) loop
2275 Iface := Node (Iface_Elmt);
2277 -- Check every interface primitive
2279 if Present (Primitive_Operations (Iface)) then
2280 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2281 while Present (Iface_Op_Elmt) loop
2282 Iface_Op := Node (Iface_Op_Elmt);
2284 -- Ignore predefined primitives
2286 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2287 Iface_Op := Ultimate_Alias (Iface_Op);
2289 -- The current primitive operation can be overridden by
2290 -- the generated entry wrapper.
2292 if Overriding_Possible (Iface_Op, Subp_Id) then
2293 First_Param :=
2294 First (Parameter_Specifications (Parent (Iface_Op)));
2296 exit Search;
2297 end if;
2298 end if;
2300 Next_Elmt (Iface_Op_Elmt);
2301 end loop;
2302 end if;
2304 Next_Elmt (Iface_Elmt);
2305 end loop Search;
2306 end if;
2308 -- Do not generate the wrapper if no interface primitive is covered by
2309 -- the subprogram and it is not a primitive declared between two views
2310 -- (see Process_Full_View).
2312 if No (First_Param)
2313 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2314 then
2315 return Empty;
2316 end if;
2318 declare
2319 Wrapper_Id : constant Entity_Id :=
2320 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2321 New_Formals : List_Id;
2322 Obj_Param : Node_Id;
2323 Obj_Param_Typ : Entity_Id;
2325 begin
2326 -- Minimum decoration is needed to catch the entity in
2327 -- Sem_Ch6.Override_Dispatching_Operation.
2329 if Ekind (Subp_Id) = E_Function then
2330 Set_Ekind (Wrapper_Id, E_Function);
2331 else
2332 Set_Ekind (Wrapper_Id, E_Procedure);
2333 end if;
2335 Set_Is_Primitive_Wrapper (Wrapper_Id);
2336 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2337 Set_Is_Private_Primitive (Wrapper_Id,
2338 Is_Private_Primitive_Subprogram (Subp_Id));
2340 -- Process the formals
2342 New_Formals := Replicate_Formals (Loc, Formals);
2344 -- A function with a controlling result and no first controlling
2345 -- formal needs no additional parameter.
2347 if Has_Controlling_Result (Subp_Id)
2348 and then
2349 (No (First_Formal (Subp_Id))
2350 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2351 then
2352 null;
2354 -- Routine Subp_Id has been found to override an interface primitive.
2355 -- If the interface operation has an access parameter, create a copy
2356 -- of it, with the same null exclusion indicator if present.
2358 elsif Present (First_Param) then
2359 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2360 Obj_Param_Typ :=
2361 Make_Access_Definition (Loc,
2362 Subtype_Mark =>
2363 New_Occurrence_Of (Obj_Typ, Loc),
2364 Null_Exclusion_Present =>
2365 Null_Exclusion_Present (Parameter_Type (First_Param)),
2366 Constant_Present =>
2367 Constant_Present (Parameter_Type (First_Param)));
2368 else
2369 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2370 end if;
2372 Obj_Param :=
2373 Make_Parameter_Specification (Loc,
2374 Defining_Identifier =>
2375 Make_Defining_Identifier (Loc,
2376 Chars => Name_uO),
2377 In_Present => In_Present (First_Param),
2378 Out_Present => Out_Present (First_Param),
2379 Parameter_Type => Obj_Param_Typ);
2381 Prepend_To (New_Formals, Obj_Param);
2383 -- If we are dealing with a primitive declared between two views,
2384 -- implemented by a synchronized operation, we need to create
2385 -- a default parameter. The mode of the parameter must match that
2386 -- of the primitive operation.
2388 else
2389 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2391 Obj_Param :=
2392 Make_Parameter_Specification (Loc,
2393 Defining_Identifier =>
2394 Make_Defining_Identifier (Loc, Name_uO),
2395 In_Present =>
2396 In_Present (Parent (First_Entity (Subp_Id))),
2397 Out_Present => Ekind (Subp_Id) /= E_Function,
2398 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2400 Prepend_To (New_Formals, Obj_Param);
2401 end if;
2403 -- Build the final spec. If it is a function with a controlling
2404 -- result, it is a primitive operation of the corresponding
2405 -- record type, so mark the spec accordingly.
2407 if Ekind (Subp_Id) = E_Function then
2408 declare
2409 Res_Def : Node_Id;
2411 begin
2412 if Has_Controlling_Result (Subp_Id) then
2413 Res_Def :=
2414 New_Occurrence_Of
2415 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2416 else
2417 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2418 end if;
2420 return
2421 Make_Function_Specification (Loc,
2422 Defining_Unit_Name => Wrapper_Id,
2423 Parameter_Specifications => New_Formals,
2424 Result_Definition => Res_Def);
2425 end;
2426 else
2427 return
2428 Make_Procedure_Specification (Loc,
2429 Defining_Unit_Name => Wrapper_Id,
2430 Parameter_Specifications => New_Formals);
2431 end if;
2432 end;
2433 end Build_Wrapper_Spec;
2435 -------------------------
2436 -- Build_Wrapper_Specs --
2437 -------------------------
2439 procedure Build_Wrapper_Specs
2440 (Loc : Source_Ptr;
2441 Typ : Entity_Id;
2442 N : in out Node_Id)
2444 Def : Node_Id;
2445 Rec_Typ : Entity_Id;
2446 procedure Scan_Declarations (L : List_Id);
2447 -- Common processing for visible and private declarations
2448 -- of a protected type.
2450 procedure Scan_Declarations (L : List_Id) is
2451 Decl : Node_Id;
2452 Wrap_Decl : Node_Id;
2453 Wrap_Spec : Node_Id;
2455 begin
2456 if No (L) then
2457 return;
2458 end if;
2460 Decl := First (L);
2461 while Present (Decl) loop
2462 Wrap_Spec := Empty;
2464 if Nkind (Decl) = N_Entry_Declaration
2465 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2466 then
2467 Wrap_Spec :=
2468 Build_Wrapper_Spec
2469 (Subp_Id => Defining_Identifier (Decl),
2470 Obj_Typ => Rec_Typ,
2471 Formals => Parameter_Specifications (Decl));
2473 elsif Nkind (Decl) = N_Subprogram_Declaration then
2474 Wrap_Spec :=
2475 Build_Wrapper_Spec
2476 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2477 Obj_Typ => Rec_Typ,
2478 Formals =>
2479 Parameter_Specifications (Specification (Decl)));
2480 end if;
2482 if Present (Wrap_Spec) then
2483 Wrap_Decl :=
2484 Make_Subprogram_Declaration (Loc,
2485 Specification => Wrap_Spec);
2487 Insert_After (N, Wrap_Decl);
2488 N := Wrap_Decl;
2490 Analyze (Wrap_Decl);
2491 end if;
2493 Next (Decl);
2494 end loop;
2495 end Scan_Declarations;
2497 -- start of processing for Build_Wrapper_Specs
2499 begin
2500 if Is_Protected_Type (Typ) then
2501 Def := Protected_Definition (Parent (Typ));
2502 else pragma Assert (Is_Task_Type (Typ));
2503 Def := Task_Definition (Parent (Typ));
2504 end if;
2506 Rec_Typ := Corresponding_Record_Type (Typ);
2508 -- Generate wrapper specs for a concurrent type which implements an
2509 -- interface. Operations in both the visible and private parts may
2510 -- implement progenitor operations.
2512 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2513 Scan_Declarations (Visible_Declarations (Def));
2514 Scan_Declarations (Private_Declarations (Def));
2515 end if;
2516 end Build_Wrapper_Specs;
2518 ---------------------------
2519 -- Build_Find_Body_Index --
2520 ---------------------------
2522 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2523 Loc : constant Source_Ptr := Sloc (Typ);
2524 Ent : Entity_Id;
2525 E_Typ : Entity_Id;
2526 Has_F : Boolean := False;
2527 Index : Nat;
2528 If_St : Node_Id := Empty;
2529 Lo : Node_Id;
2530 Hi : Node_Id;
2531 Decls : List_Id := New_List;
2532 Ret : Node_Id;
2533 Spec : Node_Id;
2534 Siz : Node_Id := Empty;
2536 procedure Add_If_Clause (Expr : Node_Id);
2537 -- Add test for range of current entry
2539 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2540 -- If a bound of an entry is given by a discriminant, retrieve the
2541 -- actual value of the discriminant from the enclosing object.
2543 -------------------
2544 -- Add_If_Clause --
2545 -------------------
2547 procedure Add_If_Clause (Expr : Node_Id) is
2548 Cond : Node_Id;
2549 Stats : constant List_Id :=
2550 New_List (
2551 Make_Simple_Return_Statement (Loc,
2552 Expression => Make_Integer_Literal (Loc, Index + 1)));
2554 begin
2555 -- Index for current entry body
2557 Index := Index + 1;
2559 -- Compute total length of entry queues so far
2561 if No (Siz) then
2562 Siz := Expr;
2563 else
2564 Siz :=
2565 Make_Op_Add (Loc,
2566 Left_Opnd => Siz,
2567 Right_Opnd => Expr);
2568 end if;
2570 Cond :=
2571 Make_Op_Le (Loc,
2572 Left_Opnd => Make_Identifier (Loc, Name_uE),
2573 Right_Opnd => Siz);
2575 -- Map entry queue indexes in the range of the current family
2576 -- into the current index, that designates the entry body.
2578 if No (If_St) then
2579 If_St :=
2580 Make_Implicit_If_Statement (Typ,
2581 Condition => Cond,
2582 Then_Statements => Stats,
2583 Elsif_Parts => New_List);
2584 Ret := If_St;
2586 else
2587 Append_To (Elsif_Parts (If_St),
2588 Make_Elsif_Part (Loc,
2589 Condition => Cond,
2590 Then_Statements => Stats));
2591 end if;
2592 end Add_If_Clause;
2594 ------------------------------
2595 -- Convert_Discriminant_Ref --
2596 ------------------------------
2598 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2599 B : Node_Id;
2601 begin
2602 if Is_Entity_Name (Bound)
2603 and then Ekind (Entity (Bound)) = E_Discriminant
2604 then
2605 B :=
2606 Make_Selected_Component (Loc,
2607 Prefix =>
2608 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2609 Make_Explicit_Dereference (Loc,
2610 Make_Identifier (Loc, Name_uObject))),
2611 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2612 Set_Etype (B, Etype (Entity (Bound)));
2613 else
2614 B := New_Copy_Tree (Bound);
2615 end if;
2617 return B;
2618 end Convert_Discriminant_Ref;
2620 -- Start of processing for Build_Find_Body_Index
2622 begin
2623 Spec := Build_Find_Body_Index_Spec (Typ);
2625 Ent := First_Entity (Typ);
2626 while Present (Ent) loop
2627 if Ekind (Ent) = E_Entry_Family then
2628 Has_F := True;
2629 exit;
2630 end if;
2632 Next_Entity (Ent);
2633 end loop;
2635 if not Has_F then
2637 -- If the protected type has no entry families, there is a one-one
2638 -- correspondence between entry queue and entry body.
2640 Ret :=
2641 Make_Simple_Return_Statement (Loc,
2642 Expression => Make_Identifier (Loc, Name_uE));
2644 else
2645 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2646 -- the following:
2648 -- if E <= l1 then return 1;
2649 -- elsif E <= l1 + l2 then return 2;
2650 -- ...
2652 Index := 0;
2653 Siz := Empty;
2654 Ent := First_Entity (Typ);
2656 Add_Object_Pointer (Loc, Typ, Decls);
2658 while Present (Ent) loop
2659 if Ekind (Ent) = E_Entry then
2660 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2662 elsif Ekind (Ent) = E_Entry_Family then
2663 E_Typ := Etype (Discrete_Subtype_Definition (Parent (Ent)));
2664 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2665 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2666 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2667 end if;
2669 Next_Entity (Ent);
2670 end loop;
2672 if Index = 1 then
2673 Decls := New_List;
2674 Ret :=
2675 Make_Simple_Return_Statement (Loc,
2676 Expression => Make_Integer_Literal (Loc, 1));
2678 elsif Nkind (Ret) = N_If_Statement then
2680 -- Ranges are in increasing order, so last one doesn't need guard
2682 declare
2683 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2684 begin
2685 Remove (Nod);
2686 Set_Else_Statements (Ret, Then_Statements (Nod));
2687 end;
2688 end if;
2689 end if;
2691 return
2692 Make_Subprogram_Body (Loc,
2693 Specification => Spec,
2694 Declarations => Decls,
2695 Handled_Statement_Sequence =>
2696 Make_Handled_Sequence_Of_Statements (Loc,
2697 Statements => New_List (Ret)));
2698 end Build_Find_Body_Index;
2700 --------------------------------
2701 -- Build_Find_Body_Index_Spec --
2702 --------------------------------
2704 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2705 Loc : constant Source_Ptr := Sloc (Typ);
2706 Id : constant Entity_Id :=
2707 Make_Defining_Identifier (Loc,
2708 Chars => New_External_Name (Chars (Typ), 'F'));
2709 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2710 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2712 begin
2713 return
2714 Make_Function_Specification (Loc,
2715 Defining_Unit_Name => Id,
2716 Parameter_Specifications => New_List (
2717 Make_Parameter_Specification (Loc,
2718 Defining_Identifier => Parm1,
2719 Parameter_Type =>
2720 New_Occurrence_Of (RTE (RE_Address), Loc)),
2722 Make_Parameter_Specification (Loc,
2723 Defining_Identifier => Parm2,
2724 Parameter_Type =>
2725 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
2727 Result_Definition => New_Occurrence_Of (
2728 RTE (RE_Protected_Entry_Index), Loc));
2729 end Build_Find_Body_Index_Spec;
2731 -----------------------------------------------
2732 -- Build_Lock_Free_Protected_Subprogram_Body --
2733 -----------------------------------------------
2735 function Build_Lock_Free_Protected_Subprogram_Body
2736 (N : Node_Id;
2737 Prot_Typ : Node_Id;
2738 Unprot_Spec : Node_Id) return Node_Id
2740 Actuals : constant List_Id := New_List;
2741 Loc : constant Source_Ptr := Sloc (N);
2742 Spec : constant Node_Id := Specification (N);
2743 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
2744 Formal : Node_Id;
2745 Prot_Spec : Node_Id;
2746 Stmt : Node_Id;
2748 begin
2749 -- Create the protected version of the body
2751 Prot_Spec :=
2752 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
2754 -- Build the actual parameters which appear in the call to the
2755 -- unprotected version of the body.
2757 Formal := First (Parameter_Specifications (Prot_Spec));
2758 while Present (Formal) loop
2759 Append_To (Actuals,
2760 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2762 Next (Formal);
2763 end loop;
2765 -- Function case, generate:
2766 -- return <Unprot_Func_Call>;
2768 if Nkind (Spec) = N_Function_Specification then
2769 Stmt :=
2770 Make_Simple_Return_Statement (Loc,
2771 Expression =>
2772 Make_Function_Call (Loc,
2773 Name =>
2774 Make_Identifier (Loc, Chars (Unprot_Id)),
2775 Parameter_Associations => Actuals));
2777 -- Procedure case, call the unprotected version
2779 else
2780 Stmt :=
2781 Make_Procedure_Call_Statement (Loc,
2782 Name =>
2783 Make_Identifier (Loc, Chars (Unprot_Id)),
2784 Parameter_Associations => Actuals);
2785 end if;
2787 return
2788 Make_Subprogram_Body (Loc,
2789 Declarations => Empty_List,
2790 Specification => Prot_Spec,
2791 Handled_Statement_Sequence =>
2792 Make_Handled_Sequence_Of_Statements (Loc,
2793 Statements => New_List (Stmt)));
2794 end Build_Lock_Free_Protected_Subprogram_Body;
2796 -------------------------------------------------
2797 -- Build_Lock_Free_Unprotected_Subprogram_Body --
2798 -------------------------------------------------
2800 -- Procedures which meet the lock-free implementation requirements and
2801 -- reference a unique scalar component Comp are expanded in the following
2802 -- manner:
2804 -- procedure P (...) is
2805 -- Expected_Comp : constant Comp_Type :=
2806 -- Comp_Type
2807 -- (System.Atomic_Primitives.Lock_Free_Read_N
2808 -- (_Object.Comp'Address));
2809 -- begin
2810 -- loop
2811 -- declare
2812 -- <original declarations before the object renaming declaration
2813 -- of Comp>
2815 -- Desired_Comp : Comp_Type := Expected_Comp;
2816 -- Comp : Comp_Type renames Desired_Comp;
2818 -- <original delarations after the object renaming declaration
2819 -- of Comp>
2821 -- begin
2822 -- <original statements>
2823 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
2824 -- (_Object.Comp'Address,
2825 -- Interfaces.Unsigned_N (Expected_Comp),
2826 -- Interfaces.Unsigned_N (Desired_Comp));
2827 -- end;
2828 -- end loop;
2829 -- end P;
2831 -- Each return and raise statement of P is transformed into an atomic
2832 -- status check:
2834 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2835 -- (_Object.Comp'Address,
2836 -- Interfaces.Unsigned_N (Expected_Comp),
2837 -- Interfaces.Unsigned_N (Desired_Comp));
2838 -- then
2839 -- <original statement>
2840 -- else
2841 -- goto L0;
2842 -- end if;
2844 -- Functions which meet the lock-free implementation requirements and
2845 -- reference a unique scalar component Comp are expanded in the following
2846 -- manner:
2848 -- function F (...) return ... is
2849 -- <original declarations before the object renaming declaration
2850 -- of Comp>
2852 -- Expected_Comp : constant Comp_Type :=
2853 -- Comp_Type
2854 -- (System.Atomic_Primitives.Lock_Free_Read_N
2855 -- (_Object.Comp'Address));
2856 -- Comp : Comp_Type renames Expected_Comp;
2858 -- <original delarations after the object renaming declaration of
2859 -- Comp>
2861 -- begin
2862 -- <original statements>
2863 -- end F;
2865 function Build_Lock_Free_Unprotected_Subprogram_Body
2866 (N : Node_Id;
2867 Prot_Typ : Node_Id) return Node_Id
2869 function Referenced_Component (N : Node_Id) return Entity_Id;
2870 -- Subprograms which meet the lock-free implementation criteria are
2871 -- allowed to reference only one unique component. Return the prival
2872 -- of the said component.
2874 --------------------------
2875 -- Referenced_Component --
2876 --------------------------
2878 function Referenced_Component (N : Node_Id) return Entity_Id is
2879 Comp : Entity_Id;
2880 Decl : Node_Id;
2881 Source_Comp : Entity_Id := Empty;
2883 begin
2884 -- Find the unique source component which N references in its
2885 -- statements.
2887 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
2888 declare
2889 Element : Lock_Free_Subprogram renames
2890 Lock_Free_Subprogram_Table.Table (Index);
2891 begin
2892 if Element.Sub_Body = N then
2893 Source_Comp := Element.Comp_Id;
2894 exit;
2895 end if;
2896 end;
2897 end loop;
2899 if No (Source_Comp) then
2900 return Empty;
2901 end if;
2903 -- Find the prival which corresponds to the source component within
2904 -- the declarations of N.
2906 Decl := First (Declarations (N));
2907 while Present (Decl) loop
2909 -- Privals appear as object renamings
2911 if Nkind (Decl) = N_Object_Renaming_Declaration then
2912 Comp := Defining_Identifier (Decl);
2914 if Present (Prival_Link (Comp))
2915 and then Prival_Link (Comp) = Source_Comp
2916 then
2917 return Comp;
2918 end if;
2919 end if;
2921 Next (Decl);
2922 end loop;
2924 return Empty;
2925 end Referenced_Component;
2927 -- Local variables
2929 Comp : constant Entity_Id := Referenced_Component (N);
2930 Loc : constant Source_Ptr := Sloc (N);
2931 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
2932 Decls : List_Id := Declarations (N);
2934 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
2936 begin
2937 -- Add renamings for the protection object, discriminals, privals, and
2938 -- the entry index constant for use by debugger.
2940 Debug_Private_Data_Declarations (Decls);
2942 -- Perform the lock-free expansion when the subprogram references a
2943 -- protected component.
2945 if Present (Comp) then
2946 Protected_Component_Ref : declare
2947 Comp_Decl : constant Node_Id := Parent (Comp);
2948 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
2949 Comp_Type : constant Entity_Id := Etype (Comp);
2951 Is_Procedure : constant Boolean :=
2952 Ekind (Corresponding_Spec (N)) = E_Procedure;
2953 -- Indicates if N is a protected procedure body
2955 Block_Decls : List_Id := No_List;
2956 Try_Write : Entity_Id;
2957 Desired_Comp : Entity_Id;
2958 Decl : Node_Id;
2959 Label : Node_Id;
2960 Label_Id : Entity_Id := Empty;
2961 Read : Entity_Id;
2962 Expected_Comp : Entity_Id;
2963 Stmt : Node_Id;
2964 Stmts : List_Id :=
2965 New_Copy_List (Statements (Hand_Stmt_Seq));
2966 Typ_Size : Int;
2967 Unsigned : Entity_Id;
2969 function Process_Node (N : Node_Id) return Traverse_Result;
2970 -- Transform a single node if it is a return statement, a raise
2971 -- statement or a reference to Comp.
2973 procedure Process_Stmts (Stmts : List_Id);
2974 -- Given a statement sequence Stmts, wrap any return or raise
2975 -- statements in the following manner:
2977 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2978 -- (_Object.Comp'Address,
2979 -- Interfaces.Unsigned_N (Expected_Comp),
2980 -- Interfaces.Unsigned_N (Desired_Comp))
2981 -- then
2982 -- <Stmt>;
2983 -- else
2984 -- goto L0;
2985 -- end if;
2987 ------------------
2988 -- Process_Node --
2989 ------------------
2991 function Process_Node (N : Node_Id) return Traverse_Result is
2993 procedure Wrap_Statement (Stmt : Node_Id);
2994 -- Wrap an arbitrary statement inside an if statement where the
2995 -- condition does an atomic check on the state of the object.
2997 --------------------
2998 -- Wrap_Statement --
2999 --------------------
3001 procedure Wrap_Statement (Stmt : Node_Id) is
3002 begin
3003 -- The first time through, create the declaration of a label
3004 -- which is used to skip the remainder of source statements
3005 -- if the state of the object has changed.
3007 if No (Label_Id) then
3008 Label_Id :=
3009 Make_Identifier (Loc, New_External_Name ('L', 0));
3010 Set_Entity (Label_Id,
3011 Make_Defining_Identifier (Loc, Chars (Label_Id)));
3012 end if;
3014 -- Generate:
3015 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
3016 -- (_Object.Comp'Address,
3017 -- Interfaces.Unsigned_N (Expected_Comp),
3018 -- Interfaces.Unsigned_N (Desired_Comp))
3019 -- then
3020 -- <Stmt>;
3021 -- else
3022 -- goto L0;
3023 -- end if;
3025 Rewrite (Stmt,
3026 Make_Implicit_If_Statement (N,
3027 Condition =>
3028 Make_Function_Call (Loc,
3029 Name =>
3030 New_Occurrence_Of (Try_Write, Loc),
3031 Parameter_Associations => New_List (
3032 Make_Attribute_Reference (Loc,
3033 Prefix => Relocate_Node (Comp_Sel_Nam),
3034 Attribute_Name => Name_Address),
3036 Unchecked_Convert_To (Unsigned,
3037 New_Occurrence_Of (Expected_Comp, Loc)),
3039 Unchecked_Convert_To (Unsigned,
3040 New_Occurrence_Of (Desired_Comp, Loc)))),
3042 Then_Statements => New_List (Relocate_Node (Stmt)),
3044 Else_Statements => New_List (
3045 Make_Goto_Statement (Loc,
3046 Name =>
3047 New_Occurrence_Of (Entity (Label_Id), Loc)))));
3048 end Wrap_Statement;
3050 -- Start of processing for Process_Node
3052 begin
3053 -- Wrap each return and raise statement that appear inside a
3054 -- procedure. Skip the last return statement which is added by
3055 -- default since it is transformed into an exit statement.
3057 if Is_Procedure
3058 and then ((Nkind (N) = N_Simple_Return_Statement
3059 and then N /= Last (Stmts))
3060 or else Nkind (N) = N_Extended_Return_Statement
3061 or else (Nkind_In (N, N_Raise_Constraint_Error,
3062 N_Raise_Program_Error,
3063 N_Raise_Statement,
3064 N_Raise_Storage_Error)
3065 and then Comes_From_Source (N)))
3066 then
3067 Wrap_Statement (N);
3068 return Skip;
3069 end if;
3071 -- Force reanalysis
3073 Set_Analyzed (N, False);
3075 return OK;
3076 end Process_Node;
3078 procedure Process_Nodes is new Traverse_Proc (Process_Node);
3080 -------------------
3081 -- Process_Stmts --
3082 -------------------
3084 procedure Process_Stmts (Stmts : List_Id) is
3085 Stmt : Node_Id;
3086 begin
3087 Stmt := First (Stmts);
3088 while Present (Stmt) loop
3089 Process_Nodes (Stmt);
3090 Next (Stmt);
3091 end loop;
3092 end Process_Stmts;
3094 -- Start of processing for Protected_Component_Ref
3096 begin
3097 -- Get the type size
3099 if Known_Static_Esize (Comp_Type) then
3100 Typ_Size := UI_To_Int (Esize (Comp_Type));
3102 -- If the Esize (Object_Size) is unknown at compile time, look at
3103 -- the RM_Size (Value_Size) since it may have been set by an
3104 -- explicit representation clause.
3106 elsif Known_Static_RM_Size (Comp_Type) then
3107 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
3109 -- Should not happen since this has already been checked in
3110 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
3112 else
3113 raise Program_Error;
3114 end if;
3116 -- Retrieve all relevant atomic routines and types
3118 case Typ_Size is
3119 when 8 =>
3120 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
3121 Read := RTE (RE_Lock_Free_Read_8);
3122 Unsigned := RTE (RE_Uint8);
3124 when 16 =>
3125 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
3126 Read := RTE (RE_Lock_Free_Read_16);
3127 Unsigned := RTE (RE_Uint16);
3129 when 32 =>
3130 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
3131 Read := RTE (RE_Lock_Free_Read_32);
3132 Unsigned := RTE (RE_Uint32);
3134 when 64 =>
3135 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
3136 Read := RTE (RE_Lock_Free_Read_64);
3137 Unsigned := RTE (RE_Uint64);
3139 when others =>
3140 raise Program_Error;
3141 end case;
3143 -- Generate:
3144 -- Expected_Comp : constant Comp_Type :=
3145 -- Comp_Type
3146 -- (System.Atomic_Primitives.Lock_Free_Read_N
3147 -- (_Object.Comp'Address));
3149 Expected_Comp :=
3150 Make_Defining_Identifier (Loc,
3151 New_External_Name (Chars (Comp), Suffix => "_saved"));
3153 Decl :=
3154 Make_Object_Declaration (Loc,
3155 Defining_Identifier => Expected_Comp,
3156 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3157 Constant_Present => True,
3158 Expression =>
3159 Unchecked_Convert_To (Comp_Type,
3160 Make_Function_Call (Loc,
3161 Name => New_Occurrence_Of (Read, Loc),
3162 Parameter_Associations => New_List (
3163 Make_Attribute_Reference (Loc,
3164 Prefix => Relocate_Node (Comp_Sel_Nam),
3165 Attribute_Name => Name_Address)))));
3167 -- Protected procedures
3169 if Is_Procedure then
3170 -- Move the original declarations inside the generated block
3172 Block_Decls := Decls;
3174 -- Reset the declarations list of the protected procedure to
3175 -- contain only Decl.
3177 Decls := New_List (Decl);
3179 -- Generate:
3180 -- Desired_Comp : Comp_Type := Expected_Comp;
3182 Desired_Comp :=
3183 Make_Defining_Identifier (Loc,
3184 New_External_Name (Chars (Comp), Suffix => "_current"));
3186 -- Insert the declarations of Expected_Comp and Desired_Comp in
3187 -- the block declarations right before the renaming of the
3188 -- protected component.
3190 Insert_Before (Comp_Decl,
3191 Make_Object_Declaration (Loc,
3192 Defining_Identifier => Desired_Comp,
3193 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3194 Expression =>
3195 New_Occurrence_Of (Expected_Comp, Loc)));
3197 -- Protected function
3199 else
3200 Desired_Comp := Expected_Comp;
3202 -- Insert the declaration of Expected_Comp in the function
3203 -- declarations right before the renaming of the protected
3204 -- component.
3206 Insert_Before (Comp_Decl, Decl);
3207 end if;
3209 -- Rewrite the protected component renaming declaration to be a
3210 -- renaming of Desired_Comp.
3212 -- Generate:
3213 -- Comp : Comp_Type renames Desired_Comp;
3215 Rewrite (Comp_Decl,
3216 Make_Object_Renaming_Declaration (Loc,
3217 Defining_Identifier =>
3218 Defining_Identifier (Comp_Decl),
3219 Subtype_Mark =>
3220 New_Occurrence_Of (Comp_Type, Loc),
3221 Name =>
3222 New_Occurrence_Of (Desired_Comp, Loc)));
3224 -- Wrap any return or raise statements in Stmts in same the manner
3225 -- described in Process_Stmts.
3227 Process_Stmts (Stmts);
3229 -- Generate:
3230 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3231 -- (_Object.Comp'Address,
3232 -- Interfaces.Unsigned_N (Expected_Comp),
3233 -- Interfaces.Unsigned_N (Desired_Comp))
3235 if Is_Procedure then
3236 Stmt :=
3237 Make_Exit_Statement (Loc,
3238 Condition =>
3239 Make_Function_Call (Loc,
3240 Name =>
3241 New_Occurrence_Of (Try_Write, Loc),
3242 Parameter_Associations => New_List (
3243 Make_Attribute_Reference (Loc,
3244 Prefix => Relocate_Node (Comp_Sel_Nam),
3245 Attribute_Name => Name_Address),
3247 Unchecked_Convert_To (Unsigned,
3248 New_Occurrence_Of (Expected_Comp, Loc)),
3250 Unchecked_Convert_To (Unsigned,
3251 New_Occurrence_Of (Desired_Comp, Loc)))));
3253 -- Small optimization: transform the default return statement
3254 -- of a procedure into the atomic exit statement.
3256 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3257 Rewrite (Last (Stmts), Stmt);
3258 else
3259 Append_To (Stmts, Stmt);
3260 end if;
3261 end if;
3263 -- Create the declaration of the label used to skip the rest of
3264 -- the source statements when the object state changes.
3266 if Present (Label_Id) then
3267 Label := Make_Label (Loc, Label_Id);
3268 Append_To (Decls,
3269 Make_Implicit_Label_Declaration (Loc,
3270 Defining_Identifier => Entity (Label_Id),
3271 Label_Construct => Label));
3272 Append_To (Stmts, Label);
3273 end if;
3275 -- Generate:
3276 -- loop
3277 -- declare
3278 -- <Decls>
3279 -- begin
3280 -- <Stmts>
3281 -- end;
3282 -- end loop;
3284 if Is_Procedure then
3285 Stmts :=
3286 New_List (
3287 Make_Loop_Statement (Loc,
3288 Statements => New_List (
3289 Make_Block_Statement (Loc,
3290 Declarations => Block_Decls,
3291 Handled_Statement_Sequence =>
3292 Make_Handled_Sequence_Of_Statements (Loc,
3293 Statements => Stmts))),
3294 End_Label => Empty));
3295 end if;
3297 Hand_Stmt_Seq :=
3298 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3299 end Protected_Component_Ref;
3300 end if;
3302 -- Make an unprotected version of the subprogram for use within the same
3303 -- object, with new name and extra parameter representing the object.
3305 return
3306 Make_Subprogram_Body (Loc,
3307 Specification =>
3308 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3309 Declarations => Decls,
3310 Handled_Statement_Sequence => Hand_Stmt_Seq);
3311 end Build_Lock_Free_Unprotected_Subprogram_Body;
3313 -------------------------
3314 -- Build_Master_Entity --
3315 -------------------------
3317 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3318 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3319 Context : Node_Id;
3320 Context_Id : Entity_Id;
3321 Decl : Node_Id;
3322 Decls : List_Id;
3323 Par : Node_Id;
3325 begin
3326 if Is_Itype (Obj_Or_Typ) then
3327 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3328 else
3329 Par := Parent (Obj_Or_Typ);
3330 end if;
3332 -- When creating a master for a record component which is either a task
3333 -- or access-to-task, the enclosing record is the master scope and the
3334 -- proper insertion point is the component list.
3336 if Is_Record_Type (Current_Scope) then
3337 Context := Par;
3338 Context_Id := Current_Scope;
3339 Decls := List_Containing (Context);
3341 -- Default case for object declarations and access types. Note that the
3342 -- context is updated to the nearest enclosing body, block, package, or
3343 -- return statement.
3345 else
3346 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3347 end if;
3349 -- Nothing to do if the context already has a master
3351 if Has_Master_Entity (Context_Id) then
3352 return;
3354 -- Nothing to do if tasks or tasking hierarchies are prohibited
3356 elsif Restriction_Active (No_Tasking)
3357 or else Restriction_Active (No_Task_Hierarchy)
3358 then
3359 return;
3360 end if;
3362 -- Create a master, generate:
3363 -- _Master : constant Master_Id := Current_Master.all;
3365 Decl :=
3366 Make_Object_Declaration (Loc,
3367 Defining_Identifier =>
3368 Make_Defining_Identifier (Loc, Name_uMaster),
3369 Constant_Present => True,
3370 Object_Definition => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3371 Expression =>
3372 Make_Explicit_Dereference (Loc,
3373 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
3375 -- The master is inserted at the start of the declarative list of the
3376 -- context.
3378 Prepend_To (Decls, Decl);
3380 -- In certain cases where transient scopes are involved, the immediate
3381 -- scope is not always the proper master scope. Ensure that the master
3382 -- declaration and entity appear in the same context.
3384 if Context_Id /= Current_Scope then
3385 Push_Scope (Context_Id);
3386 Analyze (Decl);
3387 Pop_Scope;
3388 else
3389 Analyze (Decl);
3390 end if;
3392 -- Mark the enclosing scope and its associated construct as being task
3393 -- masters.
3395 Set_Has_Master_Entity (Context_Id);
3397 while Present (Context)
3398 and then Nkind (Context) /= N_Compilation_Unit
3399 loop
3400 if Nkind_In (Context, N_Block_Statement,
3401 N_Subprogram_Body,
3402 N_Task_Body)
3403 then
3404 Set_Is_Task_Master (Context);
3405 exit;
3407 elsif Nkind (Parent (Context)) = N_Subunit then
3408 Context := Corresponding_Stub (Parent (Context));
3409 end if;
3411 Context := Parent (Context);
3412 end loop;
3413 end Build_Master_Entity;
3415 ---------------------------
3416 -- Build_Master_Renaming --
3417 ---------------------------
3419 procedure Build_Master_Renaming
3420 (Ptr_Typ : Entity_Id;
3421 Ins_Nod : Node_Id := Empty)
3423 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3424 Context : Node_Id;
3425 Master_Decl : Node_Id;
3426 Master_Id : Entity_Id;
3428 begin
3429 -- Nothing to do if tasks or tasking hierarchies are prohibited
3431 if Restriction_Active (No_Tasking)
3432 or else Restriction_Active (No_Task_Hierarchy)
3433 then
3434 return;
3435 end if;
3437 -- Determine the proper context to insert the master renaming
3439 if Present (Ins_Nod) then
3440 Context := Ins_Nod;
3441 elsif Is_Itype (Ptr_Typ) then
3442 Context := Associated_Node_For_Itype (Ptr_Typ);
3443 else
3444 Context := Parent (Ptr_Typ);
3445 end if;
3447 -- Generate:
3448 -- <Ptr_Typ>M : Master_Id renames _Master;
3450 Master_Id :=
3451 Make_Defining_Identifier (Loc,
3452 New_External_Name (Chars (Ptr_Typ), 'M'));
3454 Master_Decl :=
3455 Make_Object_Renaming_Declaration (Loc,
3456 Defining_Identifier => Master_Id,
3457 Subtype_Mark => New_Occurrence_Of (RTE (RE_Master_Id), Loc),
3458 Name => Make_Identifier (Loc, Name_uMaster));
3460 Insert_Action (Context, Master_Decl);
3462 -- The renamed master now services the access type
3464 Set_Master_Id (Ptr_Typ, Master_Id);
3465 end Build_Master_Renaming;
3467 -----------------------------------------
3468 -- Build_Private_Protected_Declaration --
3469 -----------------------------------------
3471 function Build_Private_Protected_Declaration
3472 (N : Node_Id) return Entity_Id
3474 Loc : constant Source_Ptr := Sloc (N);
3475 Body_Id : constant Entity_Id := Defining_Entity (N);
3476 Decl : Node_Id;
3477 Plist : List_Id;
3478 Formal : Entity_Id;
3479 New_Spec : Node_Id;
3480 Spec_Id : Entity_Id;
3482 begin
3483 Formal := First_Formal (Body_Id);
3485 -- The protected operation always has at least one formal, namely the
3486 -- object itself, but it is only placed in the parameter list if
3487 -- expansion is enabled.
3489 if Present (Formal) or else Expander_Active then
3490 Plist := Copy_Parameter_List (Body_Id);
3491 else
3492 Plist := No_List;
3493 end if;
3495 if Nkind (Specification (N)) = N_Procedure_Specification then
3496 New_Spec :=
3497 Make_Procedure_Specification (Loc,
3498 Defining_Unit_Name =>
3499 Make_Defining_Identifier (Sloc (Body_Id),
3500 Chars => Chars (Body_Id)),
3501 Parameter_Specifications =>
3502 Plist);
3503 else
3504 New_Spec :=
3505 Make_Function_Specification (Loc,
3506 Defining_Unit_Name =>
3507 Make_Defining_Identifier (Sloc (Body_Id),
3508 Chars => Chars (Body_Id)),
3509 Parameter_Specifications => Plist,
3510 Result_Definition =>
3511 New_Occurrence_Of (Etype (Body_Id), Loc));
3512 end if;
3514 Decl := Make_Subprogram_Declaration (Loc, Specification => New_Spec);
3515 Insert_Before (N, Decl);
3516 Spec_Id := Defining_Unit_Name (New_Spec);
3518 -- Indicate that the entity comes from source, to ensure that cross-
3519 -- reference information is properly generated. The body itself is
3520 -- rewritten during expansion, and the body entity will not appear in
3521 -- calls to the operation.
3523 Set_Comes_From_Source (Spec_Id, True);
3524 Analyze (Decl);
3525 Set_Has_Completion (Spec_Id);
3526 Set_Convention (Spec_Id, Convention_Protected);
3527 return Spec_Id;
3528 end Build_Private_Protected_Declaration;
3530 ---------------------------
3531 -- Build_Protected_Entry --
3532 ---------------------------
3534 function Build_Protected_Entry
3535 (N : Node_Id;
3536 Ent : Entity_Id;
3537 Pid : Node_Id) return Node_Id
3539 Bod_Decls : constant List_Id := New_List;
3540 Decls : constant List_Id := Declarations (N);
3541 End_Lab : constant Node_Id :=
3542 End_Label (Handled_Statement_Sequence (N));
3543 End_Loc : constant Source_Ptr :=
3544 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3545 -- Used for the generated call to Complete_Entry_Body
3547 Loc : constant Source_Ptr := Sloc (N);
3549 Bod_Id : Entity_Id;
3550 Bod_Spec : Node_Id;
3551 Bod_Stmts : List_Id;
3552 Complete : Node_Id;
3553 Ohandle : Node_Id;
3555 EH_Loc : Source_Ptr;
3556 -- Used for the exception handler, inserted at end of the body
3558 begin
3559 -- Set the source location on the exception handler only when debugging
3560 -- the expanded code (see Make_Implicit_Exception_Handler).
3562 if Debug_Generated_Code then
3563 EH_Loc := End_Loc;
3565 -- Otherwise the inserted code should not be visible to the debugger
3567 else
3568 EH_Loc := No_Location;
3569 end if;
3571 Bod_Id :=
3572 Make_Defining_Identifier (Loc,
3573 Chars => Chars (Protected_Body_Subprogram (Ent)));
3574 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3576 -- Add the following declarations:
3578 -- type poVP is access poV;
3579 -- _object : poVP := poVP (_O);
3581 -- where _O is the formal parameter associated with the concurrent
3582 -- object. These declarations are needed for Complete_Entry_Body.
3584 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3586 -- Add renamings for all formals, the Protection object, discriminals,
3587 -- privals and the entry index constant for use by debugger.
3589 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3590 Debug_Private_Data_Declarations (Decls);
3592 -- Put the declarations and the statements from the entry
3594 Bod_Stmts :=
3595 New_List (
3596 Make_Block_Statement (Loc,
3597 Declarations => Decls,
3598 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3600 case Corresponding_Runtime_Package (Pid) is
3601 when System_Tasking_Protected_Objects_Entries =>
3602 Append_To (Bod_Stmts,
3603 Make_Procedure_Call_Statement (End_Loc,
3604 Name =>
3605 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
3606 Parameter_Associations => New_List (
3607 Make_Attribute_Reference (End_Loc,
3608 Prefix =>
3609 Make_Selected_Component (End_Loc,
3610 Prefix =>
3611 Make_Identifier (End_Loc, Name_uObject),
3612 Selector_Name =>
3613 Make_Identifier (End_Loc, Name_uObject)),
3614 Attribute_Name => Name_Unchecked_Access))));
3616 when System_Tasking_Protected_Objects_Single_Entry =>
3618 -- Historically, a call to Complete_Single_Entry_Body was
3619 -- inserted, but it was a null procedure.
3621 null;
3623 when others =>
3624 raise Program_Error;
3625 end case;
3627 -- When exceptions can not be propagated, we never need to call
3628 -- Exception_Complete_Entry_Body.
3630 if No_Exception_Handlers_Set then
3631 return
3632 Make_Subprogram_Body (Loc,
3633 Specification => Bod_Spec,
3634 Declarations => Bod_Decls,
3635 Handled_Statement_Sequence =>
3636 Make_Handled_Sequence_Of_Statements (Loc,
3637 Statements => Bod_Stmts,
3638 End_Label => End_Lab));
3640 else
3641 Ohandle := Make_Others_Choice (Loc);
3642 Set_All_Others (Ohandle);
3644 case Corresponding_Runtime_Package (Pid) is
3645 when System_Tasking_Protected_Objects_Entries =>
3646 Complete :=
3647 New_Occurrence_Of
3648 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3650 when System_Tasking_Protected_Objects_Single_Entry =>
3651 Complete :=
3652 New_Occurrence_Of
3653 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3655 when others =>
3656 raise Program_Error;
3657 end case;
3659 -- Establish link between subprogram body entity and source entry
3661 Set_Corresponding_Protected_Entry (Bod_Id, Ent);
3663 -- Create body of entry procedure. The renaming declarations are
3664 -- placed ahead of the block that contains the actual entry body.
3666 return
3667 Make_Subprogram_Body (Loc,
3668 Specification => Bod_Spec,
3669 Declarations => Bod_Decls,
3670 Handled_Statement_Sequence =>
3671 Make_Handled_Sequence_Of_Statements (Loc,
3672 Statements => Bod_Stmts,
3673 End_Label => End_Lab,
3674 Exception_Handlers => New_List (
3675 Make_Implicit_Exception_Handler (EH_Loc,
3676 Exception_Choices => New_List (Ohandle),
3678 Statements => New_List (
3679 Make_Procedure_Call_Statement (EH_Loc,
3680 Name => Complete,
3681 Parameter_Associations => New_List (
3682 Make_Attribute_Reference (EH_Loc,
3683 Prefix =>
3684 Make_Selected_Component (EH_Loc,
3685 Prefix =>
3686 Make_Identifier (EH_Loc, Name_uObject),
3687 Selector_Name =>
3688 Make_Identifier (EH_Loc, Name_uObject)),
3689 Attribute_Name => Name_Unchecked_Access),
3691 Make_Function_Call (EH_Loc,
3692 Name =>
3693 New_Occurrence_Of
3694 (RTE (RE_Get_GNAT_Exception), Loc)))))))));
3695 end if;
3696 end Build_Protected_Entry;
3698 -----------------------------------------
3699 -- Build_Protected_Entry_Specification --
3700 -----------------------------------------
3702 function Build_Protected_Entry_Specification
3703 (Loc : Source_Ptr;
3704 Def_Id : Entity_Id;
3705 Ent_Id : Entity_Id) return Node_Id
3707 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3709 begin
3710 Set_Debug_Info_Needed (Def_Id);
3712 if Present (Ent_Id) then
3713 Append_Elmt (P, Accept_Address (Ent_Id));
3714 end if;
3716 return
3717 Make_Procedure_Specification (Loc,
3718 Defining_Unit_Name => Def_Id,
3719 Parameter_Specifications => New_List (
3720 Make_Parameter_Specification (Loc,
3721 Defining_Identifier =>
3722 Make_Defining_Identifier (Loc, Name_uO),
3723 Parameter_Type =>
3724 New_Occurrence_Of (RTE (RE_Address), Loc)),
3726 Make_Parameter_Specification (Loc,
3727 Defining_Identifier => P,
3728 Parameter_Type =>
3729 New_Occurrence_Of (RTE (RE_Address), Loc)),
3731 Make_Parameter_Specification (Loc,
3732 Defining_Identifier =>
3733 Make_Defining_Identifier (Loc, Name_uE),
3734 Parameter_Type =>
3735 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
3736 end Build_Protected_Entry_Specification;
3738 --------------------------
3739 -- Build_Protected_Spec --
3740 --------------------------
3742 function Build_Protected_Spec
3743 (N : Node_Id;
3744 Obj_Type : Entity_Id;
3745 Ident : Entity_Id;
3746 Unprotected : Boolean := False) return List_Id
3748 Loc : constant Source_Ptr := Sloc (N);
3749 Decl : Node_Id;
3750 Formal : Entity_Id;
3751 New_Plist : List_Id;
3752 New_Param : Node_Id;
3754 begin
3755 New_Plist := New_List;
3757 Formal := First_Formal (Ident);
3758 while Present (Formal) loop
3759 New_Param :=
3760 Make_Parameter_Specification (Loc,
3761 Defining_Identifier =>
3762 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)),
3763 Aliased_Present => Aliased_Present (Parent (Formal)),
3764 In_Present => In_Present (Parent (Formal)),
3765 Out_Present => Out_Present (Parent (Formal)),
3766 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc));
3768 if Unprotected then
3769 Set_Protected_Formal (Formal, Defining_Identifier (New_Param));
3770 end if;
3772 Append (New_Param, New_Plist);
3773 Next_Formal (Formal);
3774 end loop;
3776 -- If the subprogram is a procedure and the context is not an access
3777 -- to protected subprogram, the parameter is in-out. Otherwise it is
3778 -- an in parameter.
3780 Decl :=
3781 Make_Parameter_Specification (Loc,
3782 Defining_Identifier =>
3783 Make_Defining_Identifier (Loc, Name_uObject),
3784 In_Present => True,
3785 Out_Present =>
3786 (Etype (Ident) = Standard_Void_Type
3787 and then not Is_RTE (Obj_Type, RE_Address)),
3788 Parameter_Type =>
3789 New_Occurrence_Of (Obj_Type, Loc));
3790 Set_Debug_Info_Needed (Defining_Identifier (Decl));
3791 Prepend_To (New_Plist, Decl);
3793 return New_Plist;
3794 end Build_Protected_Spec;
3796 ---------------------------------------
3797 -- Build_Protected_Sub_Specification --
3798 ---------------------------------------
3800 function Build_Protected_Sub_Specification
3801 (N : Node_Id;
3802 Prot_Typ : Entity_Id;
3803 Mode : Subprogram_Protection_Mode) return Node_Id
3805 Loc : constant Source_Ptr := Sloc (N);
3806 Decl : Node_Id;
3807 Def_Id : Entity_Id;
3808 New_Id : Entity_Id;
3809 New_Plist : List_Id;
3810 New_Spec : Node_Id;
3812 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
3813 (Dispatching_Mode => ' ',
3814 Protected_Mode => 'P',
3815 Unprotected_Mode => 'N');
3817 begin
3818 if Ekind (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
3819 then
3820 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
3821 else
3822 Decl := N;
3823 end if;
3825 Def_Id := Defining_Unit_Name (Specification (Decl));
3827 New_Plist :=
3828 Build_Protected_Spec
3829 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
3830 Mode = Unprotected_Mode);
3831 New_Id :=
3832 Make_Defining_Identifier (Loc,
3833 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
3835 -- Reference the original nondispatching subprogram since the analysis
3836 -- of the object.operation notation may need its original name (see
3837 -- Sem_Ch4.Names_Match).
3839 if Mode = Dispatching_Mode then
3840 Set_Ekind (New_Id, Ekind (Def_Id));
3841 Set_Original_Protected_Subprogram (New_Id, Def_Id);
3842 end if;
3844 -- The unprotected operation carries the user code, and debugging
3845 -- information must be generated for it, even though this spec does
3846 -- not come from source. It is also convenient to allow gdb to step
3847 -- into the protected operation, even though it only contains lock/
3848 -- unlock calls.
3850 Set_Debug_Info_Needed (New_Id);
3852 -- If a pragma Eliminate applies to the source entity, the internal
3853 -- subprograms will be eliminated as well.
3855 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
3857 if Nkind (Specification (Decl)) = N_Procedure_Specification then
3858 New_Spec :=
3859 Make_Procedure_Specification (Loc,
3860 Defining_Unit_Name => New_Id,
3861 Parameter_Specifications => New_Plist);
3863 -- Create a new specification for the anonymous subprogram type
3865 else
3866 New_Spec :=
3867 Make_Function_Specification (Loc,
3868 Defining_Unit_Name => New_Id,
3869 Parameter_Specifications => New_Plist,
3870 Result_Definition =>
3871 Copy_Result_Type (Result_Definition (Specification (Decl))));
3873 Set_Return_Present (Defining_Unit_Name (New_Spec));
3874 end if;
3876 return New_Spec;
3877 end Build_Protected_Sub_Specification;
3879 -------------------------------------
3880 -- Build_Protected_Subprogram_Body --
3881 -------------------------------------
3883 function Build_Protected_Subprogram_Body
3884 (N : Node_Id;
3885 Pid : Node_Id;
3886 N_Op_Spec : Node_Id) return Node_Id
3888 Exc_Safe : constant Boolean := not Might_Raise (N);
3889 -- True if N cannot raise an exception
3891 Loc : constant Source_Ptr := Sloc (N);
3892 Op_Spec : constant Node_Id := Specification (N);
3893 P_Op_Spec : constant Node_Id :=
3894 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
3896 Lock_Kind : RE_Id;
3897 Lock_Name : Node_Id;
3898 Lock_Stmt : Node_Id;
3899 Object_Parm : Node_Id;
3900 Pformal : Node_Id;
3901 R : Node_Id;
3902 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
3903 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
3904 Stmts : List_Id;
3905 Sub_Body : Node_Id;
3906 Uactuals : List_Id;
3907 Unprot_Call : Node_Id;
3909 begin
3910 -- Build a list of the formal parameters of the protected version of
3911 -- the subprogram to use as the actual parameters of the unprotected
3912 -- version.
3914 Uactuals := New_List;
3915 Pformal := First (Parameter_Specifications (P_Op_Spec));
3916 while Present (Pformal) loop
3917 Append_To (Uactuals,
3918 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
3919 Next (Pformal);
3920 end loop;
3922 -- Make a call to the unprotected version of the subprogram built above
3923 -- for use by the protected version built below.
3925 if Nkind (Op_Spec) = N_Function_Specification then
3926 if Exc_Safe then
3927 R := Make_Temporary (Loc, 'R');
3929 Unprot_Call :=
3930 Make_Object_Declaration (Loc,
3931 Defining_Identifier => R,
3932 Constant_Present => True,
3933 Object_Definition =>
3934 New_Copy (Result_Definition (N_Op_Spec)),
3935 Expression =>
3936 Make_Function_Call (Loc,
3937 Name =>
3938 Make_Identifier (Loc,
3939 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3940 Parameter_Associations => Uactuals));
3942 Return_Stmt :=
3943 Make_Simple_Return_Statement (Loc,
3944 Expression => New_Occurrence_Of (R, Loc));
3946 else
3947 Unprot_Call :=
3948 Make_Simple_Return_Statement (Loc,
3949 Expression =>
3950 Make_Function_Call (Loc,
3951 Name =>
3952 Make_Identifier (Loc,
3953 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3954 Parameter_Associations => Uactuals));
3955 end if;
3957 Lock_Kind := RE_Lock_Read_Only;
3959 else
3960 Unprot_Call :=
3961 Make_Procedure_Call_Statement (Loc,
3962 Name =>
3963 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
3964 Parameter_Associations => Uactuals);
3966 Lock_Kind := RE_Lock;
3967 end if;
3969 -- Wrap call in block that will be covered by an at_end handler
3971 if not Exc_Safe then
3972 Unprot_Call :=
3973 Make_Block_Statement (Loc,
3974 Handled_Statement_Sequence =>
3975 Make_Handled_Sequence_Of_Statements (Loc,
3976 Statements => New_List (Unprot_Call)));
3977 end if;
3979 -- Make the protected subprogram body. This locks the protected
3980 -- object and calls the unprotected version of the subprogram.
3982 case Corresponding_Runtime_Package (Pid) is
3983 when System_Tasking_Protected_Objects_Entries =>
3984 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
3986 when System_Tasking_Protected_Objects_Single_Entry =>
3987 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
3989 when System_Tasking_Protected_Objects =>
3990 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
3992 when others =>
3993 raise Program_Error;
3994 end case;
3996 Object_Parm :=
3997 Make_Attribute_Reference (Loc,
3998 Prefix =>
3999 Make_Selected_Component (Loc,
4000 Prefix => Make_Identifier (Loc, Name_uObject),
4001 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4002 Attribute_Name => Name_Unchecked_Access);
4004 Lock_Stmt :=
4005 Make_Procedure_Call_Statement (Loc,
4006 Name => Lock_Name,
4007 Parameter_Associations => New_List (Object_Parm));
4009 if Abort_Allowed then
4010 Stmts := New_List (
4011 Build_Runtime_Call (Loc, RE_Abort_Defer),
4012 Lock_Stmt);
4014 else
4015 Stmts := New_List (Lock_Stmt);
4016 end if;
4018 if not Exc_Safe then
4019 Append (Unprot_Call, Stmts);
4020 else
4021 if Nkind (Op_Spec) = N_Function_Specification then
4022 Pre_Stmts := Stmts;
4023 Stmts := Empty_List;
4024 else
4025 Append (Unprot_Call, Stmts);
4026 end if;
4028 -- Historical note: Previously, call to the cleanup was inserted
4029 -- here. This is now done by Build_Protected_Subprogram_Call_Cleanup,
4030 -- which is also shared by the 'not Exc_Safe' path.
4032 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
4034 if Nkind (Op_Spec) = N_Function_Specification then
4035 Append_To (Stmts, Return_Stmt);
4036 Append_To (Pre_Stmts,
4037 Make_Block_Statement (Loc,
4038 Declarations => New_List (Unprot_Call),
4039 Handled_Statement_Sequence =>
4040 Make_Handled_Sequence_Of_Statements (Loc,
4041 Statements => Stmts)));
4042 Stmts := Pre_Stmts;
4043 end if;
4044 end if;
4046 Sub_Body :=
4047 Make_Subprogram_Body (Loc,
4048 Declarations => Empty_List,
4049 Specification => P_Op_Spec,
4050 Handled_Statement_Sequence =>
4051 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
4053 -- Mark this subprogram as a protected subprogram body so that the
4054 -- cleanup will be inserted. This is done only in the 'not Exc_Safe'
4055 -- path as otherwise the cleanup has already been inserted.
4057 if not Exc_Safe then
4058 Set_Is_Protected_Subprogram_Body (Sub_Body);
4059 end if;
4061 return Sub_Body;
4062 end Build_Protected_Subprogram_Body;
4064 -------------------------------------
4065 -- Build_Protected_Subprogram_Call --
4066 -------------------------------------
4068 procedure Build_Protected_Subprogram_Call
4069 (N : Node_Id;
4070 Name : Node_Id;
4071 Rec : Node_Id;
4072 External : Boolean := True)
4074 Loc : constant Source_Ptr := Sloc (N);
4075 Sub : constant Entity_Id := Entity (Name);
4076 New_Sub : Node_Id;
4077 Params : List_Id;
4079 begin
4080 if External then
4081 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
4082 else
4083 New_Sub :=
4084 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
4085 end if;
4087 if Present (Parameter_Associations (N)) then
4088 Params := New_Copy_List_Tree (Parameter_Associations (N));
4089 else
4090 Params := New_List;
4091 end if;
4093 -- If the type is an untagged derived type, convert to the root type,
4094 -- which is the one on which the operations are defined.
4096 if Nkind (Rec) = N_Unchecked_Type_Conversion
4097 and then not Is_Tagged_Type (Etype (Rec))
4098 and then Is_Derived_Type (Etype (Rec))
4099 then
4100 Set_Etype (Rec, Root_Type (Etype (Rec)));
4101 Set_Subtype_Mark (Rec,
4102 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
4103 end if;
4105 Prepend (Rec, Params);
4107 if Ekind (Sub) = E_Procedure then
4108 Rewrite (N,
4109 Make_Procedure_Call_Statement (Loc,
4110 Name => New_Sub,
4111 Parameter_Associations => Params));
4113 else
4114 pragma Assert (Ekind (Sub) = E_Function);
4115 Rewrite (N,
4116 Make_Function_Call (Loc,
4117 Name => New_Sub,
4118 Parameter_Associations => Params));
4120 -- Preserve type of call for subsequent processing (required for
4121 -- call to Wrap_Transient_Expression in the case of a shared passive
4122 -- protected).
4124 Set_Etype (N, Etype (New_Sub));
4125 end if;
4127 if External
4128 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4129 and then Is_Entity_Name (Expression (Rec))
4130 and then Is_Shared_Passive (Entity (Expression (Rec)))
4131 then
4132 Add_Shared_Var_Lock_Procs (N);
4133 end if;
4134 end Build_Protected_Subprogram_Call;
4136 ---------------------------------------------
4137 -- Build_Protected_Subprogram_Call_Cleanup --
4138 ---------------------------------------------
4140 procedure Build_Protected_Subprogram_Call_Cleanup
4141 (Op_Spec : Node_Id;
4142 Conc_Typ : Node_Id;
4143 Loc : Source_Ptr;
4144 Stmts : List_Id)
4146 Nam : Node_Id;
4148 begin
4149 -- If the associated protected object has entries, a protected
4150 -- procedure has to service entry queues. In this case generate:
4152 -- Service_Entries (_object._object'Access);
4154 if Nkind (Op_Spec) = N_Procedure_Specification
4155 and then Has_Entries (Conc_Typ)
4156 then
4157 case Corresponding_Runtime_Package (Conc_Typ) is
4158 when System_Tasking_Protected_Objects_Entries =>
4159 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4161 when System_Tasking_Protected_Objects_Single_Entry =>
4162 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4164 when others =>
4165 raise Program_Error;
4166 end case;
4168 Append_To (Stmts,
4169 Make_Procedure_Call_Statement (Loc,
4170 Name => Nam,
4171 Parameter_Associations => New_List (
4172 Make_Attribute_Reference (Loc,
4173 Prefix =>
4174 Make_Selected_Component (Loc,
4175 Prefix => Make_Identifier (Loc, Name_uObject),
4176 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4177 Attribute_Name => Name_Unchecked_Access))));
4179 else
4180 -- Generate:
4181 -- Unlock (_object._object'Access);
4183 case Corresponding_Runtime_Package (Conc_Typ) is
4184 when System_Tasking_Protected_Objects_Entries =>
4185 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4187 when System_Tasking_Protected_Objects_Single_Entry =>
4188 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4190 when System_Tasking_Protected_Objects =>
4191 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4193 when others =>
4194 raise Program_Error;
4195 end case;
4197 Append_To (Stmts,
4198 Make_Procedure_Call_Statement (Loc,
4199 Name => Nam,
4200 Parameter_Associations => New_List (
4201 Make_Attribute_Reference (Loc,
4202 Prefix =>
4203 Make_Selected_Component (Loc,
4204 Prefix => Make_Identifier (Loc, Name_uObject),
4205 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4206 Attribute_Name => Name_Unchecked_Access))));
4207 end if;
4209 -- Generate:
4210 -- Abort_Undefer;
4212 if Abort_Allowed then
4213 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
4214 end if;
4215 end Build_Protected_Subprogram_Call_Cleanup;
4217 -------------------------
4218 -- Build_Selected_Name --
4219 -------------------------
4221 function Build_Selected_Name
4222 (Prefix : Entity_Id;
4223 Selector : Entity_Id;
4224 Append_Char : Character := ' ') return Name_Id
4226 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4227 Select_Len : Natural;
4229 begin
4230 Get_Name_String (Chars (Selector));
4231 Select_Len := Name_Len;
4232 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4233 Get_Name_String (Chars (Prefix));
4235 -- If scope is anonymous type, discard suffix to recover name of
4236 -- single protected object. Otherwise use protected type name.
4238 if Name_Buffer (Name_Len) = 'T' then
4239 Name_Len := Name_Len - 1;
4240 end if;
4242 Add_Str_To_Name_Buffer ("__");
4243 for J in 1 .. Select_Len loop
4244 Add_Char_To_Name_Buffer (Select_Buffer (J));
4245 end loop;
4247 -- Now add the Append_Char if specified. The encoding to follow
4248 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4249 -- then the entity is associated to a protected type subprogram.
4250 -- Otherwise, it is a protected type entry. For each case, the
4251 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4253 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4255 if Append_Char /= ' ' then
4256 if Append_Char = 'P' or Append_Char = 'N' then
4257 Add_Char_To_Name_Buffer (Append_Char);
4258 return Name_Find;
4259 else
4260 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4261 return New_External_Name (Name_Find, ' ', -1);
4262 end if;
4263 else
4264 return Name_Find;
4265 end if;
4266 end Build_Selected_Name;
4268 -----------------------------
4269 -- Build_Simple_Entry_Call --
4270 -----------------------------
4272 -- A task entry call is converted to a call to Call_Simple
4274 -- declare
4275 -- P : parms := (parm, parm, parm);
4276 -- begin
4277 -- Call_Simple (acceptor-task, entry-index, P'Address);
4278 -- parm := P.param;
4279 -- parm := P.param;
4280 -- ...
4281 -- end;
4283 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4284 -- the parameters, and the constructed aggregate value contains either the
4285 -- parameters or, in the case of non-elementary types, references to these
4286 -- parameters. Then the address of this aggregate is passed to the runtime
4287 -- routine, along with the task id value and the task entry index value.
4288 -- Pnn is only required if parameters are present.
4290 -- The assignments after the call are present only in the case of in-out
4291 -- or out parameters for elementary types, and are used to assign back the
4292 -- resulting values of such parameters.
4294 -- Note: the reason that we insert a block here is that in the context
4295 -- of selects, conditional entry calls etc. the entry call statement
4296 -- appears on its own, not as an element of a list.
4298 -- A protected entry call is converted to a Protected_Entry_Call:
4300 -- declare
4301 -- P : E1_Params := (param, param, param);
4302 -- Pnn : Boolean;
4303 -- Bnn : Communications_Block;
4305 -- declare
4306 -- P : E1_Params := (param, param, param);
4307 -- Bnn : Communications_Block;
4309 -- begin
4310 -- Protected_Entry_Call (
4311 -- Object => po._object'Access,
4312 -- E => <entry index>;
4313 -- Uninterpreted_Data => P'Address;
4314 -- Mode => Simple_Call;
4315 -- Block => Bnn);
4316 -- parm := P.param;
4317 -- parm := P.param;
4318 -- ...
4319 -- end;
4321 procedure Build_Simple_Entry_Call
4322 (N : Node_Id;
4323 Concval : Node_Id;
4324 Ename : Node_Id;
4325 Index : Node_Id)
4327 begin
4328 Expand_Call (N);
4330 -- If call has been inlined, nothing left to do
4332 if Nkind (N) = N_Block_Statement then
4333 return;
4334 end if;
4336 -- Convert entry call to Call_Simple call
4338 declare
4339 Loc : constant Source_Ptr := Sloc (N);
4340 Parms : constant List_Id := Parameter_Associations (N);
4341 Stats : constant List_Id := New_List;
4342 Actual : Node_Id;
4343 Call : Node_Id;
4344 Comm_Name : Entity_Id;
4345 Conctyp : Node_Id;
4346 Decls : List_Id;
4347 Ent : Entity_Id;
4348 Ent_Acc : Entity_Id;
4349 Formal : Node_Id;
4350 Iface_Tag : Entity_Id;
4351 Iface_Typ : Entity_Id;
4352 N_Node : Node_Id;
4353 N_Var : Node_Id;
4354 P : Entity_Id;
4355 Parm1 : Node_Id;
4356 Parm2 : Node_Id;
4357 Parm3 : Node_Id;
4358 Pdecl : Node_Id;
4359 Plist : List_Id;
4360 X : Entity_Id;
4361 Xdecl : Node_Id;
4363 begin
4364 -- Simple entry and entry family cases merge here
4366 Ent := Entity (Ename);
4367 Ent_Acc := Entry_Parameters_Type (Ent);
4368 Conctyp := Etype (Concval);
4370 -- If prefix is an access type, dereference to obtain the task type
4372 if Is_Access_Type (Conctyp) then
4373 Conctyp := Designated_Type (Conctyp);
4374 end if;
4376 -- Special case for protected subprogram calls
4378 if Is_Protected_Type (Conctyp)
4379 and then Is_Subprogram (Entity (Ename))
4380 then
4381 if not Is_Eliminated (Entity (Ename)) then
4382 Build_Protected_Subprogram_Call
4383 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4384 Analyze (N);
4385 end if;
4387 return;
4388 end if;
4390 -- First parameter is the Task_Id value from the task value or the
4391 -- Object from the protected object value, obtained by selecting
4392 -- the _Task_Id or _Object from the result of doing an unchecked
4393 -- conversion to convert the value to the corresponding record type.
4395 if Nkind (Concval) = N_Function_Call
4396 and then Is_Task_Type (Conctyp)
4397 and then Ada_Version >= Ada_2005
4398 then
4399 declare
4400 ExpR : constant Node_Id := Relocate_Node (Concval);
4401 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4402 Decl : Node_Id;
4404 begin
4405 Decl :=
4406 Make_Object_Declaration (Loc,
4407 Defining_Identifier => Obj,
4408 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4409 Expression => ExpR);
4410 Set_Etype (Obj, Conctyp);
4411 Decls := New_List (Decl);
4412 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4413 end;
4415 else
4416 Decls := New_List;
4417 end if;
4419 Parm1 := Concurrent_Ref (Concval);
4421 -- Second parameter is the entry index, computed by the routine
4422 -- provided for this purpose. The value of this expression is
4423 -- assigned to an intermediate variable to assure that any entry
4424 -- family index expressions are evaluated before the entry
4425 -- parameters.
4427 if not Is_Protected_Type (Conctyp)
4428 or else
4429 Corresponding_Runtime_Package (Conctyp) =
4430 System_Tasking_Protected_Objects_Entries
4431 then
4432 X := Make_Defining_Identifier (Loc, Name_uX);
4434 Xdecl :=
4435 Make_Object_Declaration (Loc,
4436 Defining_Identifier => X,
4437 Object_Definition =>
4438 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4439 Expression => Actual_Index_Expression (
4440 Loc, Entity (Ename), Index, Concval));
4442 Append_To (Decls, Xdecl);
4443 Parm2 := New_Occurrence_Of (X, Loc);
4445 else
4446 Xdecl := Empty;
4447 Parm2 := Empty;
4448 end if;
4450 -- The third parameter is the packaged parameters. If there are
4451 -- none, then it is just the null address, since nothing is passed.
4453 if No (Parms) then
4454 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4455 P := Empty;
4457 -- Case of parameters present, where third argument is the address
4458 -- of a packaged record containing the required parameter values.
4460 else
4461 -- First build a list of parameter values, which are references to
4462 -- objects of the parameter types.
4464 Plist := New_List;
4466 Actual := First_Actual (N);
4467 Formal := First_Formal (Ent);
4468 while Present (Actual) loop
4470 -- If it is a by-copy type, copy it to a new variable. The
4471 -- packaged record has a field that points to this variable.
4473 if Is_By_Copy_Type (Etype (Actual)) then
4474 N_Node :=
4475 Make_Object_Declaration (Loc,
4476 Defining_Identifier => Make_Temporary (Loc, 'J'),
4477 Aliased_Present => True,
4478 Object_Definition =>
4479 New_Occurrence_Of (Etype (Formal), Loc));
4481 -- Mark the object as not needing initialization since the
4482 -- initialization is performed separately, avoiding errors
4483 -- on cases such as formals of null-excluding access types.
4485 Set_No_Initialization (N_Node);
4487 -- We must make a separate assignment statement for the
4488 -- case of limited types. We cannot assign it unless the
4489 -- Assignment_OK flag is set first. An out formal of an
4490 -- access type or whose type has a Default_Value must also
4491 -- be initialized from the actual (see RM 6.4.1 (13-13.1)),
4492 -- but no constraint, predicate, or null-exclusion check is
4493 -- applied before the call.
4495 if Ekind (Formal) /= E_Out_Parameter
4496 or else Is_Access_Type (Etype (Formal))
4497 or else
4498 (Is_Scalar_Type (Etype (Formal))
4499 and then
4500 Present (Default_Aspect_Value (Etype (Formal))))
4501 then
4502 N_Var :=
4503 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4504 Set_Assignment_OK (N_Var);
4505 Append_To (Stats,
4506 Make_Assignment_Statement (Loc,
4507 Name => N_Var,
4508 Expression => Relocate_Node (Actual)));
4510 -- Mark the object as internal, so we don't later reset
4511 -- No_Initialization flag in Default_Initialize_Object,
4512 -- which would lead to needless default initialization.
4513 -- We don't set this outside the if statement, because
4514 -- out scalar parameters without Default_Value do require
4515 -- default initialization if Initialize_Scalars applies.
4517 Set_Is_Internal (Defining_Identifier (N_Node));
4519 -- If actual is an out parameter of a null-excluding
4520 -- access type, there is access check on entry, so set
4521 -- Suppress_Assignment_Checks on the generated statement
4522 -- that assigns the actual to the parameter block.
4524 Set_Suppress_Assignment_Checks (Last (Stats));
4525 end if;
4527 Append (N_Node, Decls);
4529 Append_To (Plist,
4530 Make_Attribute_Reference (Loc,
4531 Attribute_Name => Name_Unchecked_Access,
4532 Prefix =>
4533 New_Occurrence_Of
4534 (Defining_Identifier (N_Node), Loc)));
4536 else
4537 -- Interface class-wide formal
4539 if Ada_Version >= Ada_2005
4540 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4541 and then Is_Interface (Etype (Formal))
4542 then
4543 Iface_Typ := Etype (Etype (Formal));
4545 -- Generate:
4546 -- formal_iface_type! (actual.iface_tag)'reference
4548 Iface_Tag :=
4549 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4550 pragma Assert (Present (Iface_Tag));
4552 Append_To (Plist,
4553 Make_Reference (Loc,
4554 Unchecked_Convert_To (Iface_Typ,
4555 Make_Selected_Component (Loc,
4556 Prefix =>
4557 Relocate_Node (Actual),
4558 Selector_Name =>
4559 New_Occurrence_Of (Iface_Tag, Loc)))));
4560 else
4561 -- Generate:
4562 -- actual'reference
4564 Append_To (Plist,
4565 Make_Reference (Loc, Relocate_Node (Actual)));
4566 end if;
4567 end if;
4569 Next_Actual (Actual);
4570 Next_Formal_With_Extras (Formal);
4571 end loop;
4573 -- Now build the declaration of parameters initialized with the
4574 -- aggregate containing this constructed parameter list.
4576 P := Make_Defining_Identifier (Loc, Name_uP);
4578 Pdecl :=
4579 Make_Object_Declaration (Loc,
4580 Defining_Identifier => P,
4581 Object_Definition =>
4582 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4583 Expression =>
4584 Make_Aggregate (Loc, Expressions => Plist));
4586 Parm3 :=
4587 Make_Attribute_Reference (Loc,
4588 Prefix => New_Occurrence_Of (P, Loc),
4589 Attribute_Name => Name_Address);
4591 Append (Pdecl, Decls);
4592 end if;
4594 -- Now we can create the call, case of protected type
4596 if Is_Protected_Type (Conctyp) then
4597 case Corresponding_Runtime_Package (Conctyp) is
4598 when System_Tasking_Protected_Objects_Entries =>
4600 -- Change the type of the index declaration
4602 Set_Object_Definition (Xdecl,
4603 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4605 -- Some additional declarations for protected entry calls
4607 if No (Decls) then
4608 Decls := New_List;
4609 end if;
4611 -- Bnn : Communications_Block;
4613 Comm_Name := Make_Temporary (Loc, 'B');
4615 Append_To (Decls,
4616 Make_Object_Declaration (Loc,
4617 Defining_Identifier => Comm_Name,
4618 Object_Definition =>
4619 New_Occurrence_Of
4620 (RTE (RE_Communication_Block), Loc)));
4622 -- Some additional statements for protected entry calls
4624 -- Protected_Entry_Call
4625 -- (Object => po._object'Access,
4626 -- E => <entry index>;
4627 -- Uninterpreted_Data => P'Address;
4628 -- Mode => Simple_Call;
4629 -- Block => Bnn);
4631 Call :=
4632 Make_Procedure_Call_Statement (Loc,
4633 Name =>
4634 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
4636 Parameter_Associations => New_List (
4637 Make_Attribute_Reference (Loc,
4638 Attribute_Name => Name_Unchecked_Access,
4639 Prefix => Parm1),
4640 Parm2,
4641 Parm3,
4642 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
4643 New_Occurrence_Of (Comm_Name, Loc)));
4645 when System_Tasking_Protected_Objects_Single_Entry =>
4647 -- Protected_Single_Entry_Call
4648 -- (Object => po._object'Access,
4649 -- Uninterpreted_Data => P'Address);
4651 Call :=
4652 Make_Procedure_Call_Statement (Loc,
4653 Name =>
4654 New_Occurrence_Of
4655 (RTE (RE_Protected_Single_Entry_Call), Loc),
4657 Parameter_Associations => New_List (
4658 Make_Attribute_Reference (Loc,
4659 Attribute_Name => Name_Unchecked_Access,
4660 Prefix => Parm1),
4661 Parm3));
4663 when others =>
4664 raise Program_Error;
4665 end case;
4667 -- Case of task type
4669 else
4670 Call :=
4671 Make_Procedure_Call_Statement (Loc,
4672 Name =>
4673 New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
4674 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4676 end if;
4678 Append_To (Stats, Call);
4680 -- If there are out or in/out parameters by copy add assignment
4681 -- statements for the result values.
4683 if Present (Parms) then
4684 Actual := First_Actual (N);
4685 Formal := First_Formal (Ent);
4687 Set_Assignment_OK (Actual);
4688 while Present (Actual) loop
4689 if Is_By_Copy_Type (Etype (Actual))
4690 and then Ekind (Formal) /= E_In_Parameter
4691 then
4692 N_Node :=
4693 Make_Assignment_Statement (Loc,
4694 Name => New_Copy (Actual),
4695 Expression =>
4696 Make_Explicit_Dereference (Loc,
4697 Make_Selected_Component (Loc,
4698 Prefix => New_Occurrence_Of (P, Loc),
4699 Selector_Name =>
4700 Make_Identifier (Loc, Chars (Formal)))));
4702 -- In all cases (including limited private types) we want
4703 -- the assignment to be valid.
4705 Set_Assignment_OK (Name (N_Node));
4707 -- If the call is the triggering alternative in an
4708 -- asynchronous select, or the entry_call alternative of a
4709 -- conditional entry call, the assignments for in-out
4710 -- parameters are incorporated into the statement list that
4711 -- follows, so that there are executed only if the entry
4712 -- call succeeds.
4714 if (Nkind (Parent (N)) = N_Triggering_Alternative
4715 and then N = Triggering_Statement (Parent (N)))
4716 or else
4717 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4718 and then N = Entry_Call_Statement (Parent (N)))
4719 then
4720 if No (Statements (Parent (N))) then
4721 Set_Statements (Parent (N), New_List);
4722 end if;
4724 Prepend (N_Node, Statements (Parent (N)));
4726 else
4727 Insert_After (Call, N_Node);
4728 end if;
4729 end if;
4731 Next_Actual (Actual);
4732 Next_Formal_With_Extras (Formal);
4733 end loop;
4734 end if;
4736 -- Finally, create block and analyze it
4738 Rewrite (N,
4739 Make_Block_Statement (Loc,
4740 Declarations => Decls,
4741 Handled_Statement_Sequence =>
4742 Make_Handled_Sequence_Of_Statements (Loc,
4743 Statements => Stats)));
4745 Analyze (N);
4746 end;
4747 end Build_Simple_Entry_Call;
4749 --------------------------------
4750 -- Build_Task_Activation_Call --
4751 --------------------------------
4753 procedure Build_Task_Activation_Call (N : Node_Id) is
4754 Loc : constant Source_Ptr := Sloc (N);
4755 Chain : Entity_Id;
4756 Call : Node_Id;
4757 Name : Node_Id;
4758 P : Node_Id;
4760 begin
4761 -- For sequential elaboration policy, all the tasks will be activated at
4762 -- the end of the elaboration.
4764 if Partition_Elaboration_Policy = 'S' then
4765 return;
4766 end if;
4768 -- Get the activation chain entity. Except in the case of a package
4769 -- body, this is in the node that was passed. For a package body, we
4770 -- have to find the corresponding package declaration node.
4772 if Nkind (N) = N_Package_Body then
4773 P := Corresponding_Spec (N);
4774 loop
4775 P := Parent (P);
4776 exit when Nkind (P) = N_Package_Declaration;
4777 end loop;
4779 Chain := Activation_Chain_Entity (P);
4781 else
4782 Chain := Activation_Chain_Entity (N);
4783 end if;
4785 if Present (Chain) then
4786 if Restricted_Profile then
4787 Name := New_Occurrence_Of
4788 (RTE (RE_Activate_Restricted_Tasks), Loc);
4789 else
4790 Name := New_Occurrence_Of
4791 (RTE (RE_Activate_Tasks), Loc);
4792 end if;
4794 Call :=
4795 Make_Procedure_Call_Statement (Loc,
4796 Name => Name,
4797 Parameter_Associations =>
4798 New_List (Make_Attribute_Reference (Loc,
4799 Prefix => New_Occurrence_Of (Chain, Loc),
4800 Attribute_Name => Name_Unchecked_Access)));
4802 if Nkind (N) = N_Package_Declaration then
4803 if Present (Corresponding_Body (N)) then
4804 null;
4806 elsif Present (Private_Declarations (Specification (N))) then
4807 Append (Call, Private_Declarations (Specification (N)));
4809 else
4810 Append (Call, Visible_Declarations (Specification (N)));
4811 end if;
4813 else
4814 if Present (Handled_Statement_Sequence (N)) then
4816 -- The call goes at the start of the statement sequence after
4817 -- the start of exception range label if one is present.
4819 declare
4820 Stm : Node_Id;
4822 begin
4823 Stm := First (Statements (Handled_Statement_Sequence (N)));
4825 -- A special case, skip exception range label if one is
4826 -- present (from front end zcx processing).
4828 if Nkind (Stm) = N_Label and then Exception_Junk (Stm) then
4829 Next (Stm);
4830 end if;
4832 -- Another special case, if the first statement is a block
4833 -- from optimization of a local raise to a goto, then the
4834 -- call goes inside this block.
4836 if Nkind (Stm) = N_Block_Statement
4837 and then Exception_Junk (Stm)
4838 then
4839 Stm :=
4840 First (Statements (Handled_Statement_Sequence (Stm)));
4841 end if;
4843 -- Insertion point is after any exception label pushes,
4844 -- since we want it covered by any local handlers.
4846 while Nkind (Stm) in N_Push_xxx_Label loop
4847 Next (Stm);
4848 end loop;
4850 -- Now we have the proper insertion point
4852 Insert_Before (Stm, Call);
4853 end;
4855 else
4856 Set_Handled_Statement_Sequence (N,
4857 Make_Handled_Sequence_Of_Statements (Loc,
4858 Statements => New_List (Call)));
4859 end if;
4860 end if;
4862 Analyze (Call);
4863 Check_Task_Activation (N);
4864 end if;
4865 end Build_Task_Activation_Call;
4867 -------------------------------
4868 -- Build_Task_Allocate_Block --
4869 -------------------------------
4871 procedure Build_Task_Allocate_Block
4872 (Actions : List_Id;
4873 N : Node_Id;
4874 Args : List_Id)
4876 T : constant Entity_Id := Entity (Expression (N));
4877 Init : constant Entity_Id := Base_Init_Proc (T);
4878 Loc : constant Source_Ptr := Sloc (N);
4879 Chain : constant Entity_Id :=
4880 Make_Defining_Identifier (Loc, Name_uChain);
4881 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4882 Block : Node_Id;
4884 begin
4885 Block :=
4886 Make_Block_Statement (Loc,
4887 Identifier => New_Occurrence_Of (Blkent, Loc),
4888 Declarations => New_List (
4890 -- _Chain : Activation_Chain;
4892 Make_Object_Declaration (Loc,
4893 Defining_Identifier => Chain,
4894 Aliased_Present => True,
4895 Object_Definition =>
4896 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
4898 Handled_Statement_Sequence =>
4899 Make_Handled_Sequence_Of_Statements (Loc,
4901 Statements => New_List (
4903 -- Init (Args);
4905 Make_Procedure_Call_Statement (Loc,
4906 Name => New_Occurrence_Of (Init, Loc),
4907 Parameter_Associations => Args),
4909 -- Activate_Tasks (_Chain);
4911 Make_Procedure_Call_Statement (Loc,
4912 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4913 Parameter_Associations => New_List (
4914 Make_Attribute_Reference (Loc,
4915 Prefix => New_Occurrence_Of (Chain, Loc),
4916 Attribute_Name => Name_Unchecked_Access))))),
4918 Has_Created_Identifier => True,
4919 Is_Task_Allocation_Block => True);
4921 Append_To (Actions,
4922 Make_Implicit_Label_Declaration (Loc,
4923 Defining_Identifier => Blkent,
4924 Label_Construct => Block));
4926 Append_To (Actions, Block);
4928 Set_Activation_Chain_Entity (Block, Chain);
4929 end Build_Task_Allocate_Block;
4931 -----------------------------------------------
4932 -- Build_Task_Allocate_Block_With_Init_Stmts --
4933 -----------------------------------------------
4935 procedure Build_Task_Allocate_Block_With_Init_Stmts
4936 (Actions : List_Id;
4937 N : Node_Id;
4938 Init_Stmts : List_Id)
4940 Loc : constant Source_Ptr := Sloc (N);
4941 Chain : constant Entity_Id :=
4942 Make_Defining_Identifier (Loc, Name_uChain);
4943 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4944 Block : Node_Id;
4946 begin
4947 Append_To (Init_Stmts,
4948 Make_Procedure_Call_Statement (Loc,
4949 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4950 Parameter_Associations => New_List (
4951 Make_Attribute_Reference (Loc,
4952 Prefix => New_Occurrence_Of (Chain, Loc),
4953 Attribute_Name => Name_Unchecked_Access))));
4955 Block :=
4956 Make_Block_Statement (Loc,
4957 Identifier => New_Occurrence_Of (Blkent, Loc),
4958 Declarations => New_List (
4960 -- _Chain : Activation_Chain;
4962 Make_Object_Declaration (Loc,
4963 Defining_Identifier => Chain,
4964 Aliased_Present => True,
4965 Object_Definition =>
4966 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
4968 Handled_Statement_Sequence =>
4969 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
4971 Has_Created_Identifier => True,
4972 Is_Task_Allocation_Block => True);
4974 Append_To (Actions,
4975 Make_Implicit_Label_Declaration (Loc,
4976 Defining_Identifier => Blkent,
4977 Label_Construct => Block));
4979 Append_To (Actions, Block);
4981 Set_Activation_Chain_Entity (Block, Chain);
4982 end Build_Task_Allocate_Block_With_Init_Stmts;
4984 -----------------------------------
4985 -- Build_Task_Proc_Specification --
4986 -----------------------------------
4988 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
4989 Loc : constant Source_Ptr := Sloc (T);
4990 Spec_Id : Entity_Id;
4992 begin
4993 -- Case of explicit task type, suffix TB
4995 if Comes_From_Source (T) then
4996 Spec_Id :=
4997 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
4999 -- Case of anonymous task type, suffix B
5001 else
5002 Spec_Id :=
5003 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
5004 end if;
5006 Set_Is_Internal (Spec_Id);
5008 -- Associate the procedure with the task, if this is the declaration
5009 -- (and not the body) of the procedure.
5011 if No (Task_Body_Procedure (T)) then
5012 Set_Task_Body_Procedure (T, Spec_Id);
5013 end if;
5015 return
5016 Make_Procedure_Specification (Loc,
5017 Defining_Unit_Name => Spec_Id,
5018 Parameter_Specifications => New_List (
5019 Make_Parameter_Specification (Loc,
5020 Defining_Identifier =>
5021 Make_Defining_Identifier (Loc, Name_uTask),
5022 Parameter_Type =>
5023 Make_Access_Definition (Loc,
5024 Subtype_Mark =>
5025 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
5026 end Build_Task_Proc_Specification;
5028 ---------------------------------------
5029 -- Build_Unprotected_Subprogram_Body --
5030 ---------------------------------------
5032 function Build_Unprotected_Subprogram_Body
5033 (N : Node_Id;
5034 Pid : Node_Id) return Node_Id
5036 Decls : constant List_Id := Declarations (N);
5038 begin
5039 -- Add renamings for the Protection object, discriminals, privals, and
5040 -- the entry index constant for use by debugger.
5042 Debug_Private_Data_Declarations (Decls);
5044 -- Make an unprotected version of the subprogram for use within the same
5045 -- object, with a new name and an additional parameter representing the
5046 -- object.
5048 return
5049 Make_Subprogram_Body (Sloc (N),
5050 Specification =>
5051 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
5052 Declarations => Decls,
5053 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
5054 end Build_Unprotected_Subprogram_Body;
5056 ----------------------------
5057 -- Collect_Entry_Families --
5058 ----------------------------
5060 procedure Collect_Entry_Families
5061 (Loc : Source_Ptr;
5062 Cdecls : List_Id;
5063 Current_Node : in out Node_Id;
5064 Conctyp : Entity_Id)
5066 Efam : Entity_Id;
5067 Efam_Decl : Node_Id;
5068 Efam_Type : Entity_Id;
5070 begin
5071 Efam := First_Entity (Conctyp);
5072 while Present (Efam) loop
5073 if Ekind (Efam) = E_Entry_Family then
5074 Efam_Type := Make_Temporary (Loc, 'F');
5076 declare
5077 Bas : Entity_Id :=
5078 Base_Type
5079 (Etype (Discrete_Subtype_Definition (Parent (Efam))));
5081 Bas_Decl : Node_Id := Empty;
5082 Lo, Hi : Node_Id;
5084 begin
5085 Get_Index_Bounds
5086 (Discrete_Subtype_Definition (Parent (Efam)), Lo, Hi);
5088 if Is_Potentially_Large_Family (Bas, Conctyp, Lo, Hi) then
5089 Bas := Make_Temporary (Loc, 'B');
5091 Bas_Decl :=
5092 Make_Subtype_Declaration (Loc,
5093 Defining_Identifier => Bas,
5094 Subtype_Indication =>
5095 Make_Subtype_Indication (Loc,
5096 Subtype_Mark =>
5097 New_Occurrence_Of (Standard_Integer, Loc),
5098 Constraint =>
5099 Make_Range_Constraint (Loc,
5100 Range_Expression => Make_Range (Loc,
5101 Make_Integer_Literal
5102 (Loc, -Entry_Family_Bound),
5103 Make_Integer_Literal
5104 (Loc, Entry_Family_Bound - 1)))));
5106 Insert_After (Current_Node, Bas_Decl);
5107 Current_Node := Bas_Decl;
5108 Analyze (Bas_Decl);
5109 end if;
5111 Efam_Decl :=
5112 Make_Full_Type_Declaration (Loc,
5113 Defining_Identifier => Efam_Type,
5114 Type_Definition =>
5115 Make_Unconstrained_Array_Definition (Loc,
5116 Subtype_Marks =>
5117 (New_List (New_Occurrence_Of (Bas, Loc))),
5119 Component_Definition =>
5120 Make_Component_Definition (Loc,
5121 Aliased_Present => False,
5122 Subtype_Indication =>
5123 New_Occurrence_Of (Standard_Character, Loc))));
5124 end;
5126 Insert_After (Current_Node, Efam_Decl);
5127 Current_Node := Efam_Decl;
5128 Analyze (Efam_Decl);
5130 Append_To (Cdecls,
5131 Make_Component_Declaration (Loc,
5132 Defining_Identifier =>
5133 Make_Defining_Identifier (Loc, Chars (Efam)),
5135 Component_Definition =>
5136 Make_Component_Definition (Loc,
5137 Aliased_Present => False,
5138 Subtype_Indication =>
5139 Make_Subtype_Indication (Loc,
5140 Subtype_Mark =>
5141 New_Occurrence_Of (Efam_Type, Loc),
5143 Constraint =>
5144 Make_Index_Or_Discriminant_Constraint (Loc,
5145 Constraints => New_List (
5146 New_Occurrence_Of
5147 (Etype (Discrete_Subtype_Definition
5148 (Parent (Efam))), Loc)))))));
5150 end if;
5152 Next_Entity (Efam);
5153 end loop;
5154 end Collect_Entry_Families;
5156 -----------------------
5157 -- Concurrent_Object --
5158 -----------------------
5160 function Concurrent_Object
5161 (Spec_Id : Entity_Id;
5162 Conc_Typ : Entity_Id) return Entity_Id
5164 begin
5165 -- Parameter _O or _object
5167 if Is_Protected_Type (Conc_Typ) then
5168 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5170 -- Parameter _task
5172 else
5173 pragma Assert (Is_Task_Type (Conc_Typ));
5174 return First_Formal (Task_Body_Procedure (Conc_Typ));
5175 end if;
5176 end Concurrent_Object;
5178 ----------------------
5179 -- Copy_Result_Type --
5180 ----------------------
5182 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5183 New_Res : constant Node_Id := New_Copy_Tree (Res);
5184 Par_Spec : Node_Id;
5185 Formal : Entity_Id;
5187 begin
5188 -- If the result type is an access_to_subprogram, we must create new
5189 -- entities for its spec.
5191 if Nkind (New_Res) = N_Access_Definition
5192 and then Present (Access_To_Subprogram_Definition (New_Res))
5193 then
5194 -- Provide new entities for the formals
5196 Par_Spec := First (Parameter_Specifications
5197 (Access_To_Subprogram_Definition (New_Res)));
5198 while Present (Par_Spec) loop
5199 Formal := Defining_Identifier (Par_Spec);
5200 Set_Defining_Identifier (Par_Spec,
5201 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5202 Next (Par_Spec);
5203 end loop;
5204 end if;
5206 return New_Res;
5207 end Copy_Result_Type;
5209 --------------------
5210 -- Concurrent_Ref --
5211 --------------------
5213 -- The expression returned for a reference to a concurrent object has the
5214 -- form:
5216 -- taskV!(name)._Task_Id
5218 -- for a task, and
5220 -- objectV!(name)._Object
5222 -- for a protected object. For the case of an access to a concurrent
5223 -- object, there is an extra explicit dereference:
5225 -- taskV!(name.all)._Task_Id
5226 -- objectV!(name.all)._Object
5228 -- here taskV and objectV are the types for the associated records, which
5229 -- contain the required _Task_Id and _Object fields for tasks and protected
5230 -- objects, respectively.
5232 -- For the case of a task type name, the expression is
5234 -- Self;
5236 -- i.e. a call to the Self function which returns precisely this Task_Id
5238 -- For the case of a protected type name, the expression is
5240 -- objectR
5242 -- which is a renaming of the _object field of the current object
5243 -- record, passed into protected operations as a parameter.
5245 function Concurrent_Ref (N : Node_Id) return Node_Id is
5246 Loc : constant Source_Ptr := Sloc (N);
5247 Ntyp : constant Entity_Id := Etype (N);
5248 Dtyp : Entity_Id;
5249 Sel : Name_Id;
5251 function Is_Current_Task (T : Entity_Id) return Boolean;
5252 -- Check whether the reference is to the immediately enclosing task
5253 -- type, or to an outer one (rare but legal).
5255 ---------------------
5256 -- Is_Current_Task --
5257 ---------------------
5259 function Is_Current_Task (T : Entity_Id) return Boolean is
5260 Scop : Entity_Id;
5262 begin
5263 Scop := Current_Scope;
5264 while Present (Scop) and then Scop /= Standard_Standard loop
5265 if Scop = T then
5266 return True;
5268 elsif Is_Task_Type (Scop) then
5269 return False;
5271 -- If this is a procedure nested within the task type, we must
5272 -- assume that it can be called from an inner task, and therefore
5273 -- cannot treat it as a local reference.
5275 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5276 return False;
5278 else
5279 Scop := Scope (Scop);
5280 end if;
5281 end loop;
5283 -- We know that we are within the task body, so should have found it
5284 -- in scope.
5286 raise Program_Error;
5287 end Is_Current_Task;
5289 -- Start of processing for Concurrent_Ref
5291 begin
5292 if Is_Access_Type (Ntyp) then
5293 Dtyp := Designated_Type (Ntyp);
5295 if Is_Protected_Type (Dtyp) then
5296 Sel := Name_uObject;
5297 else
5298 Sel := Name_uTask_Id;
5299 end if;
5301 return
5302 Make_Selected_Component (Loc,
5303 Prefix =>
5304 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5305 Make_Explicit_Dereference (Loc, N)),
5306 Selector_Name => Make_Identifier (Loc, Sel));
5308 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5309 if Is_Task_Type (Entity (N)) then
5311 if Is_Current_Task (Entity (N)) then
5312 return
5313 Make_Function_Call (Loc,
5314 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5316 else
5317 declare
5318 Decl : Node_Id;
5319 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5320 T_Body : constant Node_Id :=
5321 Parent (Corresponding_Body (Parent (Entity (N))));
5323 begin
5324 Decl :=
5325 Make_Object_Declaration (Loc,
5326 Defining_Identifier => T_Self,
5327 Object_Definition =>
5328 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5329 Expression =>
5330 Make_Function_Call (Loc,
5331 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5332 Prepend (Decl, Declarations (T_Body));
5333 Analyze (Decl);
5334 Set_Scope (T_Self, Entity (N));
5335 return New_Occurrence_Of (T_Self, Loc);
5336 end;
5337 end if;
5339 else
5340 pragma Assert (Is_Protected_Type (Entity (N)));
5342 return
5343 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5344 end if;
5346 else
5347 if Is_Protected_Type (Ntyp) then
5348 Sel := Name_uObject;
5349 elsif Is_Task_Type (Ntyp) then
5350 Sel := Name_uTask_Id;
5351 else
5352 raise Program_Error;
5353 end if;
5355 return
5356 Make_Selected_Component (Loc,
5357 Prefix =>
5358 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5359 New_Copy_Tree (N)),
5360 Selector_Name => Make_Identifier (Loc, Sel));
5361 end if;
5362 end Concurrent_Ref;
5364 ------------------------
5365 -- Convert_Concurrent --
5366 ------------------------
5368 function Convert_Concurrent
5369 (N : Node_Id;
5370 Typ : Entity_Id) return Node_Id
5372 begin
5373 if not Is_Concurrent_Type (Typ) then
5374 return N;
5375 else
5376 return
5377 Unchecked_Convert_To
5378 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5379 end if;
5380 end Convert_Concurrent;
5382 -------------------------------------
5383 -- Debug_Private_Data_Declarations --
5384 -------------------------------------
5386 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5387 Debug_Nod : Node_Id;
5388 Decl : Node_Id;
5390 begin
5391 Decl := First (Decls);
5392 while Present (Decl) and then not Comes_From_Source (Decl) loop
5394 -- Declaration for concurrent entity _object and its access type,
5395 -- along with the entry index subtype:
5396 -- type prot_typVP is access prot_typV;
5397 -- _object : prot_typVP := prot_typV (_O);
5398 -- subtype Jnn is <Type of Index> range Low .. High;
5400 if Nkind_In (Decl, N_Full_Type_Declaration, N_Object_Declaration) then
5401 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5403 -- Declaration for the Protection object, discriminals, privals, and
5404 -- entry index constant:
5405 -- conc_typR : protection_typ renames _object._object;
5406 -- discr_nameD : discr_typ renames _object.discr_name;
5407 -- discr_nameD : discr_typ renames _task.discr_name;
5408 -- prival_name : comp_typ renames _object.comp_name;
5409 -- J : constant Jnn :=
5410 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5412 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5413 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5414 Debug_Nod := Debug_Renaming_Declaration (Decl);
5416 if Present (Debug_Nod) then
5417 Insert_After (Decl, Debug_Nod);
5418 end if;
5419 end if;
5421 Next (Decl);
5422 end loop;
5423 end Debug_Private_Data_Declarations;
5425 ------------------------------
5426 -- Ensure_Statement_Present --
5427 ------------------------------
5429 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5430 Stmt : Node_Id;
5432 begin
5433 if Opt.Suppress_Control_Flow_Optimizations
5434 and then Is_Empty_List (Statements (Alt))
5435 then
5436 Stmt := Make_Null_Statement (Loc);
5438 -- Mark NULL statement as coming from source so that it is not
5439 -- eliminated by GIGI.
5441 -- Another covert channel. If this is a requirement, it must be
5442 -- documented in sinfo/einfo ???
5444 Set_Comes_From_Source (Stmt, True);
5446 Set_Statements (Alt, New_List (Stmt));
5447 end if;
5448 end Ensure_Statement_Present;
5450 ----------------------------
5451 -- Entry_Index_Expression --
5452 ----------------------------
5454 function Entry_Index_Expression
5455 (Sloc : Source_Ptr;
5456 Ent : Entity_Id;
5457 Index : Node_Id;
5458 Ttyp : Entity_Id) return Node_Id
5460 Expr : Node_Id;
5461 Num : Node_Id;
5462 Lo : Node_Id;
5463 Hi : Node_Id;
5464 Prev : Entity_Id;
5465 S : Node_Id;
5467 begin
5468 -- The queues of entries and entry families appear in textual order in
5469 -- the associated record. The entry index is computed as the sum of the
5470 -- number of queues for all entries that precede the designated one, to
5471 -- which is added the index expression, if this expression denotes a
5472 -- member of a family.
5474 -- The following is a place holder for the count of simple entries
5476 Num := Make_Integer_Literal (Sloc, 1);
5478 -- We construct an expression which is a series of addition operations.
5479 -- The first operand is the number of single entries that precede this
5480 -- one, the second operand is the index value relative to the start of
5481 -- the referenced family, and the remaining operands are the lengths of
5482 -- the entry families that precede this entry, i.e. the constructed
5483 -- expression is:
5485 -- number_simple_entries +
5486 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5487 -- family'length + ...
5489 -- where index-value is the given index value, and s is the index
5490 -- subtype (we have to use pos because the subtype might be an
5491 -- enumeration type preventing direct subtraction). Note that the task
5492 -- entry array is one-indexed.
5494 -- The upper bound of the entry family may be a discriminant, so we
5495 -- retrieve the lower bound explicitly to compute offset, rather than
5496 -- using the index subtype which may mention a discriminant.
5498 if Present (Index) then
5499 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Ent)));
5501 Expr :=
5502 Make_Op_Add (Sloc,
5503 Left_Opnd => Num,
5504 Right_Opnd =>
5505 Family_Offset
5506 (Sloc,
5507 Make_Attribute_Reference (Sloc,
5508 Attribute_Name => Name_Pos,
5509 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5510 Expressions => New_List (Relocate_Node (Index))),
5511 Type_Low_Bound (S),
5512 Ttyp,
5513 False));
5514 else
5515 Expr := Num;
5516 end if;
5518 -- Now add lengths of preceding entries and entry families
5520 Prev := First_Entity (Ttyp);
5521 while Chars (Prev) /= Chars (Ent)
5522 or else (Ekind (Prev) /= Ekind (Ent))
5523 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5524 loop
5525 if Ekind (Prev) = E_Entry then
5526 Set_Intval (Num, Intval (Num) + 1);
5528 elsif Ekind (Prev) = E_Entry_Family then
5529 S := Etype (Discrete_Subtype_Definition (Declaration_Node (Prev)));
5530 Lo := Type_Low_Bound (S);
5531 Hi := Type_High_Bound (S);
5533 Expr :=
5534 Make_Op_Add (Sloc,
5535 Left_Opnd => Expr,
5536 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5538 -- Other components are anonymous types to be ignored
5540 else
5541 null;
5542 end if;
5544 Next_Entity (Prev);
5545 end loop;
5547 return Expr;
5548 end Entry_Index_Expression;
5550 ---------------------------
5551 -- Establish_Task_Master --
5552 ---------------------------
5554 procedure Establish_Task_Master (N : Node_Id) is
5555 Call : Node_Id;
5557 begin
5558 if Restriction_Active (No_Task_Hierarchy) = False then
5559 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5561 -- The block may have no declarations (and nevertheless be a task
5562 -- master) if it contains a call that may return an object that
5563 -- contains tasks.
5565 if No (Declarations (N)) then
5566 Set_Declarations (N, New_List (Call));
5567 else
5568 Prepend_To (Declarations (N), Call);
5569 end if;
5571 Analyze (Call);
5572 end if;
5573 end Establish_Task_Master;
5575 --------------------------------
5576 -- Expand_Accept_Declarations --
5577 --------------------------------
5579 -- Part of the expansion of an accept statement involves the creation of
5580 -- a declaration that can be referenced from the statement sequence of
5581 -- the accept:
5583 -- Ann : Address;
5585 -- This declaration is inserted immediately before the accept statement
5586 -- and it is important that it be inserted before the statements of the
5587 -- statement sequence are analyzed. Thus it would be too late to create
5588 -- this declaration in the Expand_N_Accept_Statement routine, which is
5589 -- why there is a separate procedure to be called directly from Sem_Ch9.
5591 -- Ann is used to hold the address of the record containing the parameters
5592 -- (see Expand_N_Entry_Call for more details on how this record is built).
5593 -- References to the parameters do an unchecked conversion of this address
5594 -- to a pointer to the required record type, and then access the field that
5595 -- holds the value of the required parameter. The entity for the address
5596 -- variable is held as the top stack element (i.e. the last element) of the
5597 -- Accept_Address stack in the corresponding entry entity, and this element
5598 -- must be set in place before the statements are processed.
5600 -- The above description applies to the case of a stand alone accept
5601 -- statement, i.e. one not appearing as part of a select alternative.
5603 -- For the case of an accept that appears as part of a select alternative
5604 -- of a selective accept, we must still create the declaration right away,
5605 -- since Ann is needed immediately, but there is an important difference:
5607 -- The declaration is inserted before the selective accept, not before
5608 -- the accept statement (which is not part of a list anyway, and so would
5609 -- not accommodate inserted declarations)
5611 -- We only need one address variable for the entire selective accept. So
5612 -- the Ann declaration is created only for the first accept alternative,
5613 -- and subsequent accept alternatives reference the same Ann variable.
5615 -- We can distinguish the two cases by seeing whether the accept statement
5616 -- is part of a list. If not, then it must be in an accept alternative.
5618 -- To expand the requeue statement, a label is provided at the end of the
5619 -- accept statement or alternative of which it is a part, so that the
5620 -- statement can be skipped after the requeue is complete. This label is
5621 -- created here rather than during the expansion of the accept statement,
5622 -- because it will be needed by any requeue statements within the accept,
5623 -- which are expanded before the accept.
5625 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5626 Loc : constant Source_Ptr := Sloc (N);
5627 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5628 Ann : Entity_Id := Empty;
5629 Adecl : Node_Id;
5630 Lab : Node_Id;
5631 Ldecl : Node_Id;
5632 Ldecl2 : Node_Id;
5634 begin
5635 if Expander_Active then
5637 -- If we have no handled statement sequence, we may need to build
5638 -- a dummy sequence consisting of a null statement. This can be
5639 -- skipped if the trivial accept optimization is permitted.
5641 if not Trivial_Accept_OK
5642 and then (No (Stats) or else Null_Statements (Statements (Stats)))
5643 then
5644 Set_Handled_Statement_Sequence (N,
5645 Make_Handled_Sequence_Of_Statements (Loc,
5646 Statements => New_List (Make_Null_Statement (Loc))));
5647 end if;
5649 -- Create and declare two labels to be placed at the end of the
5650 -- accept statement. The first label is used to allow requeues to
5651 -- skip the remainder of entry processing. The second label is used
5652 -- to skip the remainder of entry processing if the rendezvous
5653 -- completes in the middle of the accept body.
5655 if Present (Handled_Statement_Sequence (N)) then
5656 declare
5657 Ent : Entity_Id;
5659 begin
5660 Ent := Make_Temporary (Loc, 'L');
5661 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5662 Ldecl :=
5663 Make_Implicit_Label_Declaration (Loc,
5664 Defining_Identifier => Ent,
5665 Label_Construct => Lab);
5666 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5668 Ent := Make_Temporary (Loc, 'L');
5669 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5670 Ldecl2 :=
5671 Make_Implicit_Label_Declaration (Loc,
5672 Defining_Identifier => Ent,
5673 Label_Construct => Lab);
5674 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5675 end;
5677 else
5678 Ldecl := Empty;
5679 Ldecl2 := Empty;
5680 end if;
5682 -- Case of stand alone accept statement
5684 if Is_List_Member (N) then
5686 if Present (Handled_Statement_Sequence (N)) then
5687 Ann := Make_Temporary (Loc, 'A');
5689 Adecl :=
5690 Make_Object_Declaration (Loc,
5691 Defining_Identifier => Ann,
5692 Object_Definition =>
5693 New_Occurrence_Of (RTE (RE_Address), Loc));
5695 Insert_Before_And_Analyze (N, Adecl);
5696 Insert_Before_And_Analyze (N, Ldecl);
5697 Insert_Before_And_Analyze (N, Ldecl2);
5698 end if;
5700 -- Case of accept statement which is in an accept alternative
5702 else
5703 declare
5704 Acc_Alt : constant Node_Id := Parent (N);
5705 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5706 Alt : Node_Id;
5708 begin
5709 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5710 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5712 -- ??? Consider a single label for select statements
5714 if Present (Handled_Statement_Sequence (N)) then
5715 Prepend (Ldecl2,
5716 Statements (Handled_Statement_Sequence (N)));
5717 Analyze (Ldecl2);
5719 Prepend (Ldecl,
5720 Statements (Handled_Statement_Sequence (N)));
5721 Analyze (Ldecl);
5722 end if;
5724 -- Find first accept alternative of the selective accept. A
5725 -- valid selective accept must have at least one accept in it.
5727 Alt := First (Select_Alternatives (Sel_Acc));
5729 while Nkind (Alt) /= N_Accept_Alternative loop
5730 Next (Alt);
5731 end loop;
5733 -- If this is the first accept statement, then we have to
5734 -- create the Ann variable, as for the stand alone case, except
5735 -- that it is inserted before the selective accept. Similarly,
5736 -- a label for requeue expansion must be declared.
5738 if N = Accept_Statement (Alt) then
5739 Ann := Make_Temporary (Loc, 'A');
5740 Adecl :=
5741 Make_Object_Declaration (Loc,
5742 Defining_Identifier => Ann,
5743 Object_Definition =>
5744 New_Occurrence_Of (RTE (RE_Address), Loc));
5746 Insert_Before_And_Analyze (Sel_Acc, Adecl);
5748 -- If this is not the first accept statement, then find the Ann
5749 -- variable allocated by the first accept and use it.
5751 else
5752 Ann :=
5753 Node (Last_Elmt (Accept_Address
5754 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5755 end if;
5756 end;
5757 end if;
5759 -- Merge here with Ann either created or referenced, and Adecl
5760 -- pointing to the corresponding declaration. Remaining processing
5761 -- is the same for the two cases.
5763 if Present (Ann) then
5764 Append_Elmt (Ann, Accept_Address (Ent));
5765 Set_Debug_Info_Needed (Ann);
5766 end if;
5768 -- Create renaming declarations for the entry formals. Each reference
5769 -- to a formal becomes a dereference of a component of the parameter
5770 -- block, whose address is held in Ann. These declarations are
5771 -- eventually inserted into the accept block, and analyzed there so
5772 -- that they have the proper scope for gdb and do not conflict with
5773 -- other declarations.
5775 if Present (Parameter_Specifications (N))
5776 and then Present (Handled_Statement_Sequence (N))
5777 then
5778 declare
5779 Comp : Entity_Id;
5780 Decl : Node_Id;
5781 Formal : Entity_Id;
5782 New_F : Entity_Id;
5783 Renamed_Formal : Node_Id;
5785 begin
5786 Push_Scope (Ent);
5787 Formal := First_Formal (Ent);
5789 while Present (Formal) loop
5790 Comp := Entry_Component (Formal);
5791 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
5793 Set_Etype (New_F, Etype (Formal));
5794 Set_Scope (New_F, Ent);
5796 -- Now we set debug info needed on New_F even though it does
5797 -- not come from source, so that the debugger will get the
5798 -- right information for these generated names.
5800 Set_Debug_Info_Needed (New_F);
5802 if Ekind (Formal) = E_In_Parameter then
5803 Set_Ekind (New_F, E_Constant);
5804 else
5805 Set_Ekind (New_F, E_Variable);
5806 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5807 end if;
5809 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5811 Renamed_Formal :=
5812 Make_Selected_Component (Loc,
5813 Prefix =>
5814 Unchecked_Convert_To (
5815 Entry_Parameters_Type (Ent),
5816 New_Occurrence_Of (Ann, Loc)),
5817 Selector_Name =>
5818 New_Occurrence_Of (Comp, Loc));
5820 Decl :=
5821 Build_Renamed_Formal_Declaration
5822 (New_F, Formal, Comp, Renamed_Formal);
5824 if No (Declarations (N)) then
5825 Set_Declarations (N, New_List);
5826 end if;
5828 Append (Decl, Declarations (N));
5829 Set_Renamed_Object (Formal, New_F);
5830 Next_Formal (Formal);
5831 end loop;
5833 End_Scope;
5834 end;
5835 end if;
5836 end if;
5837 end Expand_Accept_Declarations;
5839 ---------------------------------------------
5840 -- Expand_Access_Protected_Subprogram_Type --
5841 ---------------------------------------------
5843 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5844 Loc : constant Source_Ptr := Sloc (N);
5845 T : constant Entity_Id := Defining_Identifier (N);
5846 D_T : constant Entity_Id := Designated_Type (T);
5847 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
5848 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
5849 P_List : constant List_Id :=
5850 Build_Protected_Spec (N, RTE (RE_Address), D_T, False);
5852 Comps : List_Id;
5853 Decl1 : Node_Id;
5854 Decl2 : Node_Id;
5855 Def1 : Node_Id;
5857 begin
5858 -- Create access to subprogram with full signature
5860 if Etype (D_T) /= Standard_Void_Type then
5861 Def1 :=
5862 Make_Access_Function_Definition (Loc,
5863 Parameter_Specifications => P_List,
5864 Result_Definition =>
5865 Copy_Result_Type (Result_Definition (Type_Definition (N))));
5867 else
5868 Def1 :=
5869 Make_Access_Procedure_Definition (Loc,
5870 Parameter_Specifications => P_List);
5871 end if;
5873 Decl1 :=
5874 Make_Full_Type_Declaration (Loc,
5875 Defining_Identifier => D_T2,
5876 Type_Definition => Def1);
5878 -- Declare the new types before the original one since the latter will
5879 -- refer to them through the Equivalent_Type slot.
5881 Insert_Before_And_Analyze (N, Decl1);
5883 -- Associate the access to subprogram with its original access to
5884 -- protected subprogram type. Needed by the backend to know that this
5885 -- type corresponds with an access to protected subprogram type.
5887 Set_Original_Access_Type (D_T2, T);
5889 -- Create Equivalent_Type, a record with two components for an access to
5890 -- object and an access to subprogram.
5892 Comps := New_List (
5893 Make_Component_Declaration (Loc,
5894 Defining_Identifier => Make_Temporary (Loc, 'P'),
5895 Component_Definition =>
5896 Make_Component_Definition (Loc,
5897 Aliased_Present => False,
5898 Subtype_Indication =>
5899 New_Occurrence_Of (RTE (RE_Address), Loc))),
5901 Make_Component_Declaration (Loc,
5902 Defining_Identifier => Make_Temporary (Loc, 'S'),
5903 Component_Definition =>
5904 Make_Component_Definition (Loc,
5905 Aliased_Present => False,
5906 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
5908 Decl2 :=
5909 Make_Full_Type_Declaration (Loc,
5910 Defining_Identifier => E_T,
5911 Type_Definition =>
5912 Make_Record_Definition (Loc,
5913 Component_List =>
5914 Make_Component_List (Loc, Component_Items => Comps)));
5916 Insert_Before_And_Analyze (N, Decl2);
5917 Set_Equivalent_Type (T, E_T);
5918 end Expand_Access_Protected_Subprogram_Type;
5920 --------------------------
5921 -- Expand_Entry_Barrier --
5922 --------------------------
5924 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
5925 Cond : constant Node_Id := Condition (Entry_Body_Formal_Part (N));
5926 Prot : constant Entity_Id := Scope (Ent);
5927 Spec_Decl : constant Node_Id := Parent (Prot);
5929 Func_Id : Entity_Id := Empty;
5930 -- The entity of the barrier function
5932 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
5933 -- Check whether entity in Barrier is external to protected type.
5934 -- If so, barrier may not be properly synchronized.
5936 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
5937 -- Check whether N follows the Pure_Barriers restriction. Return OK if
5938 -- so.
5940 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean;
5941 -- Check whether entity name N denotes a component of the protected
5942 -- object. This is used to check the Simple_Barrier restriction.
5944 ----------------------
5945 -- Is_Global_Entity --
5946 ----------------------
5948 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
5949 E : Entity_Id;
5950 S : Entity_Id;
5952 begin
5953 if Is_Entity_Name (N) and then Present (Entity (N)) then
5954 E := Entity (N);
5955 S := Scope (E);
5957 if Ekind (E) = E_Variable then
5959 -- If the variable is local to the barrier function generated
5960 -- during expansion, it is ok. If expansion is not performed,
5961 -- then Func is Empty so this test cannot succeed.
5963 if Scope (E) = Func_Id then
5964 null;
5966 -- A protected call from a barrier to another object is ok
5968 elsif Ekind (Etype (E)) = E_Protected_Type then
5969 null;
5971 -- If the variable is within the package body we consider
5972 -- this safe. This is a common (if dubious) idiom.
5974 elsif S = Scope (Prot)
5975 and then Ekind_In (S, E_Package, E_Generic_Package)
5976 and then Nkind (Parent (E)) = N_Object_Declaration
5977 and then Nkind (Parent (Parent (E))) = N_Package_Body
5978 then
5979 null;
5981 else
5982 Error_Msg_N ("potentially unsynchronized barrier??", N);
5983 Error_Msg_N ("\& should be private component of type??", N);
5984 end if;
5985 end if;
5986 end if;
5988 return OK;
5989 end Is_Global_Entity;
5991 procedure Check_Unprotected_Barrier is
5992 new Traverse_Proc (Is_Global_Entity);
5994 ----------------------------
5995 -- Is_Simple_Barrier_Name --
5996 ----------------------------
5998 function Is_Simple_Barrier_Name (N : Node_Id) return Boolean is
5999 Renamed : Node_Id;
6001 begin
6002 -- Check for case of _object.all.field (note that the explicit
6003 -- dereference gets inserted by analyze/expand of _object.field).
6005 if Expander_Active then
6006 Renamed := Renamed_Object (Entity (N));
6008 return
6009 Present (Renamed)
6010 and then Nkind (Renamed) = N_Selected_Component
6011 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
6012 else
6013 return Scope (Entity (N)) = Current_Scope;
6014 end if;
6015 end Is_Simple_Barrier_Name;
6017 ---------------------
6018 -- Is_Pure_Barrier --
6019 ---------------------
6021 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
6022 function Is_Count_Attribute (N : Node_Id) return Boolean;
6023 -- Check whether N is part of an expansion of the Count attribute.
6024 -- Return True if N represents the expanded function call.
6026 ------------------------
6027 -- Is_Count_Attribute --
6028 ------------------------
6030 function Is_Count_Attribute (N : Node_Id) return Boolean is
6031 begin
6032 return
6033 Nkind (N) = N_Function_Call
6034 and then Present (Original_Node (N))
6035 and then Nkind (Original_Node (N)) = N_Attribute_Reference
6036 and then Attribute_Name (Original_Node (N)) = Name_Count;
6037 end Is_Count_Attribute;
6039 -- Start of processing for Is_Pure_Barrier
6041 begin
6042 case Nkind (N) is
6043 when N_Expanded_Name
6044 | N_Identifier
6046 if No (Entity (N)) then
6047 return Abandon;
6048 end if;
6050 if Present (Parent (N))
6051 and then Is_Count_Attribute (Parent (N))
6052 then
6053 return OK;
6054 end if;
6056 case Ekind (Entity (N)) is
6057 when E_Constant
6058 | E_Discriminant
6059 | E_Enumeration_Literal
6060 | E_Named_Integer
6061 | E_Named_Real
6063 return OK;
6065 when E_Component
6066 | E_Variable
6068 -- A variable in the protected type is expanded as a
6069 -- component.
6071 if Is_Simple_Barrier_Name (N) then
6072 return OK;
6073 end if;
6075 when others =>
6076 null;
6077 end case;
6079 when N_Function_Call =>
6080 if Is_Count_Attribute (N) then
6081 return OK;
6082 end if;
6084 when N_Character_Literal
6085 | N_Integer_Literal
6086 | N_Real_Literal
6088 return OK;
6090 when N_Op_Boolean
6091 | N_Op_Not
6093 if Ekind (Entity (N)) = E_Operator then
6094 return OK;
6095 end if;
6097 when N_Short_Circuit =>
6098 return OK;
6100 when others =>
6101 null;
6102 end case;
6104 return Abandon;
6105 end Is_Pure_Barrier;
6107 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6109 -- Local variables
6111 Cond_Id : Entity_Id;
6112 Entry_Body : Node_Id;
6113 Func_Body : Node_Id;
6114 pragma Warnings (Off, Func_Body);
6116 -- Start of processing for Expand_Entry_Barrier
6118 begin
6119 if No_Run_Time_Mode then
6120 Error_Msg_CRT ("entry barrier", N);
6121 return;
6122 end if;
6124 -- The body of the entry barrier must be analyzed in the context of the
6125 -- protected object, but its scope is external to it, just as any other
6126 -- unprotected version of a protected operation. The specification has
6127 -- been produced when the protected type declaration was elaborated. We
6128 -- build the body, insert it in the enclosing scope, but analyze it in
6129 -- the current context. A more uniform approach would be to treat the
6130 -- barrier just as a protected function, and discard the protected
6131 -- version of it because it is never called.
6133 if Expander_Active then
6134 Func_Body := Build_Barrier_Function (N, Ent, Prot);
6135 Func_Id := Barrier_Function (Ent);
6136 Set_Corresponding_Spec (Func_Body, Func_Id);
6138 Entry_Body := Parent (Corresponding_Body (Spec_Decl));
6140 if Nkind (Parent (Entry_Body)) = N_Subunit then
6141 Entry_Body := Corresponding_Stub (Parent (Entry_Body));
6142 end if;
6144 Insert_Before_And_Analyze (Entry_Body, Func_Body);
6146 Set_Discriminals (Spec_Decl);
6147 Set_Scope (Func_Id, Scope (Prot));
6149 else
6150 Analyze_And_Resolve (Cond, Any_Boolean);
6151 end if;
6153 -- Check Pure_Barriers restriction
6155 if Check_Pure_Barriers (Cond) = Abandon then
6156 Check_Restriction (Pure_Barriers, Cond);
6157 end if;
6159 -- The Ravenscar profile restricts barriers to simple variables declared
6160 -- within the protected object. We also allow Boolean constants, since
6161 -- these appear in several published examples and are also allowed by
6162 -- other compilers.
6164 -- Note that after analysis variables in this context will be replaced
6165 -- by the corresponding prival, that is to say a renaming of a selected
6166 -- component of the form _Object.Var. If expansion is disabled, as
6167 -- within a generic, we check that the entity appears in the current
6168 -- scope.
6170 if Is_Entity_Name (Cond) then
6171 Cond_Id := Entity (Cond);
6173 -- Perform a small optimization of simple barrier functions. If the
6174 -- scope of the condition's entity is not the barrier function, then
6175 -- the condition does not depend on any of the generated renamings.
6176 -- If this is the case, eliminate the renamings as they are useless.
6177 -- This optimization is not performed when the condition was folded
6178 -- and validity checks are in effect because the original condition
6179 -- may have produced at least one check that depends on the generated
6180 -- renamings.
6182 if Expander_Active
6183 and then Scope (Cond_Id) /= Func_Id
6184 and then not Validity_Check_Operands
6185 then
6186 Set_Declarations (Func_Body, Empty_List);
6187 end if;
6189 if Cond_Id = Standard_False or else Cond_Id = Standard_True then
6190 return;
6192 elsif Is_Simple_Barrier_Name (Cond) then
6193 return;
6194 end if;
6195 end if;
6197 -- It is not a boolean variable or literal, so check the restriction.
6198 -- Note that it is safe to be calling Check_Restriction from here, even
6199 -- though this is part of the expander, since Expand_Entry_Barrier is
6200 -- called from Sem_Ch9 even in -gnatc mode.
6202 Check_Restriction (Simple_Barriers, Cond);
6204 -- Emit warning if barrier contains global entities and is thus
6205 -- potentially unsynchronized.
6207 Check_Unprotected_Barrier (Cond);
6208 end Expand_Entry_Barrier;
6210 ------------------------------
6211 -- Expand_N_Abort_Statement --
6212 ------------------------------
6214 -- Expand abort T1, T2, .. Tn; into:
6215 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6217 procedure Expand_N_Abort_Statement (N : Node_Id) is
6218 Loc : constant Source_Ptr := Sloc (N);
6219 Tlist : constant List_Id := Names (N);
6220 Count : Nat;
6221 Aggr : Node_Id;
6222 Tasknm : Node_Id;
6224 begin
6225 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6226 Count := 0;
6228 Tasknm := First (Tlist);
6230 while Present (Tasknm) loop
6231 Count := Count + 1;
6233 -- A task interface class-wide type object is being aborted. Retrieve
6234 -- its _task_id by calling a dispatching routine.
6236 if Ada_Version >= Ada_2005
6237 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6238 and then Is_Interface (Etype (Tasknm))
6239 and then Is_Task_Interface (Etype (Tasknm))
6240 then
6241 Append_To (Component_Associations (Aggr),
6242 Make_Component_Association (Loc,
6243 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6244 Expression =>
6246 -- Task_Id (Tasknm._disp_get_task_id)
6248 Make_Unchecked_Type_Conversion (Loc,
6249 Subtype_Mark =>
6250 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
6251 Expression =>
6252 Make_Selected_Component (Loc,
6253 Prefix => New_Copy_Tree (Tasknm),
6254 Selector_Name =>
6255 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6257 else
6258 Append_To (Component_Associations (Aggr),
6259 Make_Component_Association (Loc,
6260 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6261 Expression => Concurrent_Ref (Tasknm)));
6262 end if;
6264 Next (Tasknm);
6265 end loop;
6267 Rewrite (N,
6268 Make_Procedure_Call_Statement (Loc,
6269 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6270 Parameter_Associations => New_List (
6271 Make_Qualified_Expression (Loc,
6272 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6273 Expression => Aggr))));
6275 Analyze (N);
6276 end Expand_N_Abort_Statement;
6278 -------------------------------
6279 -- Expand_N_Accept_Statement --
6280 -------------------------------
6282 -- This procedure handles expansion of accept statements that stand alone,
6283 -- i.e. they are not part of an accept alternative. The expansion of
6284 -- accept statement in accept alternatives is handled by the routines
6285 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6286 -- following description applies only to stand alone accept statements.
6288 -- If there is no handled statement sequence, or only null statements, then
6289 -- this is called a trivial accept, and the expansion is:
6291 -- Accept_Trivial (entry-index)
6293 -- If there is a handled statement sequence, then the expansion is:
6295 -- Ann : Address;
6296 -- {Lnn : Label}
6298 -- begin
6299 -- begin
6300 -- Accept_Call (entry-index, Ann);
6301 -- Renaming_Declarations for formals
6302 -- <statement sequence from N_Accept_Statement node>
6303 -- Complete_Rendezvous;
6304 -- <<Lnn>>
6306 -- exception
6307 -- when ... =>
6308 -- <exception handler from N_Accept_Statement node>
6309 -- Complete_Rendezvous;
6310 -- when ... =>
6311 -- <exception handler from N_Accept_Statement node>
6312 -- Complete_Rendezvous;
6313 -- ...
6314 -- end;
6316 -- exception
6317 -- when all others =>
6318 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6319 -- end;
6321 -- The first three declarations were already inserted ahead of the accept
6322 -- statement by the Expand_Accept_Declarations procedure, which was called
6323 -- directly from the semantics during analysis of the accept statement,
6324 -- before analyzing its contained statements.
6326 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6327 -- from possible expansion activity (the original source of course does
6328 -- not have any declarations associated with the accept statement, since
6329 -- an accept statement has no declarative part). In particular, if the
6330 -- expander is active, the first such declaration is the declaration of
6331 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6333 -- The two blocks are merged into a single block if the inner block has
6334 -- no exception handlers, but otherwise two blocks are required, since
6335 -- exceptions might be raised in the exception handlers of the inner
6336 -- block, and Exceptional_Complete_Rendezvous must be called.
6338 procedure Expand_N_Accept_Statement (N : Node_Id) is
6339 Loc : constant Source_Ptr := Sloc (N);
6340 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6341 Ename : constant Node_Id := Entry_Direct_Name (N);
6342 Eindx : constant Node_Id := Entry_Index (N);
6343 Eent : constant Entity_Id := Entity (Ename);
6344 Acstack : constant Elist_Id := Accept_Address (Eent);
6345 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6346 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6347 Blkent : Entity_Id;
6348 Call : Node_Id;
6349 Block : Node_Id;
6351 begin
6352 -- If the accept statement is not part of a list, then its parent must
6353 -- be an accept alternative, and, as described above, we do not do any
6354 -- expansion for such accept statements at this level.
6356 if not Is_List_Member (N) then
6357 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6358 return;
6360 -- Trivial accept case (no statement sequence, or null statements).
6361 -- If the accept statement has declarations, then just insert them
6362 -- before the procedure call.
6364 elsif Trivial_Accept_OK
6365 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6366 then
6367 -- Remove declarations for renamings, because the parameter block
6368 -- will not be assigned.
6370 declare
6371 D : Node_Id;
6372 Next_D : Node_Id;
6374 begin
6375 D := First (Declarations (N));
6376 while Present (D) loop
6377 Next_D := Next (D);
6378 if Nkind (D) = N_Object_Renaming_Declaration then
6379 Remove (D);
6380 end if;
6382 D := Next_D;
6383 end loop;
6384 end;
6386 if Present (Declarations (N)) then
6387 Insert_Actions (N, Declarations (N));
6388 end if;
6390 Rewrite (N,
6391 Make_Procedure_Call_Statement (Loc,
6392 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6393 Parameter_Associations => New_List (
6394 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6396 Analyze (N);
6398 -- Discard Entry_Address that was created for it, so it will not be
6399 -- emitted if this accept statement is in the statement part of a
6400 -- delay alternative.
6402 if Present (Stats) then
6403 Remove_Last_Elmt (Acstack);
6404 end if;
6406 -- Case of statement sequence present
6408 else
6409 -- Construct the block, using the declarations from the accept
6410 -- statement if any to initialize the declarations of the block.
6412 Blkent := Make_Temporary (Loc, 'A');
6413 Set_Ekind (Blkent, E_Block);
6414 Set_Etype (Blkent, Standard_Void_Type);
6415 Set_Scope (Blkent, Current_Scope);
6417 Block :=
6418 Make_Block_Statement (Loc,
6419 Identifier => New_Occurrence_Of (Blkent, Loc),
6420 Declarations => Declarations (N),
6421 Handled_Statement_Sequence => Build_Accept_Body (N));
6423 -- For the analysis of the generated declarations, the parent node
6424 -- must be properly set.
6426 Set_Parent (Block, Parent (N));
6428 -- Prepend call to Accept_Call to main statement sequence If the
6429 -- accept has exception handlers, the statement sequence is wrapped
6430 -- in a block. Insert call and renaming declarations in the
6431 -- declarations of the block, so they are elaborated before the
6432 -- handlers.
6434 Call :=
6435 Make_Procedure_Call_Statement (Loc,
6436 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6437 Parameter_Associations => New_List (
6438 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6439 New_Occurrence_Of (Ann, Loc)));
6441 if Parent (Stats) = N then
6442 Prepend (Call, Statements (Stats));
6443 else
6444 Set_Declarations (Parent (Stats), New_List (Call));
6445 end if;
6447 Analyze (Call);
6449 Push_Scope (Blkent);
6451 declare
6452 D : Node_Id;
6453 Next_D : Node_Id;
6454 Typ : Entity_Id;
6456 begin
6457 D := First (Declarations (N));
6458 while Present (D) loop
6459 Next_D := Next (D);
6461 if Nkind (D) = N_Object_Renaming_Declaration then
6463 -- The renaming declarations for the formals were created
6464 -- during analysis of the accept statement, and attached to
6465 -- the list of declarations. Place them now in the context
6466 -- of the accept block or subprogram.
6468 Remove (D);
6469 Typ := Entity (Subtype_Mark (D));
6470 Insert_After (Call, D);
6471 Analyze (D);
6473 -- If the formal is class_wide, it does not have an actual
6474 -- subtype. The analysis of the renaming declaration creates
6475 -- one, but we need to retain the class-wide nature of the
6476 -- entity.
6478 if Is_Class_Wide_Type (Typ) then
6479 Set_Etype (Defining_Identifier (D), Typ);
6480 end if;
6482 end if;
6484 D := Next_D;
6485 end loop;
6486 end;
6488 End_Scope;
6490 -- Replace the accept statement by the new block
6492 Rewrite (N, Block);
6493 Analyze (N);
6495 -- Last step is to unstack the Accept_Address value
6497 Remove_Last_Elmt (Acstack);
6498 end if;
6499 end Expand_N_Accept_Statement;
6501 ----------------------------------
6502 -- Expand_N_Asynchronous_Select --
6503 ----------------------------------
6505 -- This procedure assumes that the trigger statement is an entry call or
6506 -- a dispatching procedure call. A delay alternative should already have
6507 -- been expanded into an entry call to the appropriate delay object Wait
6508 -- entry.
6510 -- If the trigger is a task entry call, the select is implemented with
6511 -- a Task_Entry_Call:
6513 -- declare
6514 -- B : Boolean;
6515 -- C : Boolean;
6516 -- P : parms := (parm, parm, parm);
6518 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6520 -- procedure _clean is
6521 -- begin
6522 -- ...
6523 -- Cancel_Task_Entry_Call (C);
6524 -- ...
6525 -- end _clean;
6527 -- begin
6528 -- Abort_Defer;
6529 -- Task_Entry_Call
6530 -- (<acceptor-task>, -- Acceptor
6531 -- <entry-index>, -- E
6532 -- P'Address, -- Uninterpreted_Data
6533 -- Asynchronous_Call, -- Mode
6534 -- B); -- Rendezvous_Successful
6536 -- begin
6537 -- begin
6538 -- Abort_Undefer;
6539 -- <abortable-part>
6540 -- at end
6541 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6542 -- end;
6543 -- exception
6544 -- when Abort_Signal => Abort_Undefer;
6545 -- end;
6547 -- parm := P.param;
6548 -- parm := P.param;
6549 -- ...
6550 -- if not C then
6551 -- <triggered-statements>
6552 -- end if;
6553 -- end;
6555 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6556 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6557 -- as follows:
6559 -- declare
6560 -- P : parms := (parm, parm, parm);
6561 -- begin
6562 -- Call_Simple (acceptor-task, entry-index, P'Address);
6563 -- parm := P.param;
6564 -- parm := P.param;
6565 -- ...
6566 -- end;
6568 -- so the task at hand is to convert the latter expansion into the former
6570 -- If the trigger is a protected entry call, the select is implemented
6571 -- with Protected_Entry_Call:
6573 -- declare
6574 -- P : E1_Params := (param, param, param);
6575 -- Bnn : Communications_Block;
6577 -- begin
6578 -- declare
6580 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6582 -- procedure _clean is
6583 -- begin
6584 -- ...
6585 -- if Enqueued (Bnn) then
6586 -- Cancel_Protected_Entry_Call (Bnn);
6587 -- end if;
6588 -- ...
6589 -- end _clean;
6591 -- begin
6592 -- begin
6593 -- Protected_Entry_Call
6594 -- (po._object'Access, -- Object
6595 -- <entry index>, -- E
6596 -- P'Address, -- Uninterpreted_Data
6597 -- Asynchronous_Call, -- Mode
6598 -- Bnn); -- Block
6600 -- if Enqueued (Bnn) then
6601 -- <abortable-part>
6602 -- end if;
6603 -- at end
6604 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6605 -- end;
6606 -- exception
6607 -- when Abort_Signal => Abort_Undefer;
6608 -- end;
6610 -- if not Cancelled (Bnn) then
6611 -- <triggered-statements>
6612 -- end if;
6613 -- end;
6615 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6616 -- entry call:
6618 -- declare
6619 -- P : E1_Params := (param, param, param);
6620 -- Bnn : Communications_Block;
6622 -- begin
6623 -- Protected_Entry_Call
6624 -- (po._object'Access, -- Object
6625 -- <entry index>, -- E
6626 -- P'Address, -- Uninterpreted_Data
6627 -- Simple_Call, -- Mode
6628 -- Bnn); -- Block
6629 -- parm := P.param;
6630 -- parm := P.param;
6631 -- ...
6632 -- end;
6634 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6635 -- expanded into:
6637 -- declare
6638 -- B : Boolean := False;
6639 -- Bnn : Communication_Block;
6640 -- C : Ada.Tags.Prim_Op_Kind;
6641 -- D : System.Storage_Elements.Dummy_Communication_Block;
6642 -- K : Ada.Tags.Tagged_Kind :=
6643 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6644 -- P : Parameters := (Param1 .. ParamN);
6645 -- S : Integer;
6646 -- U : Boolean;
6648 -- begin
6649 -- if K = Ada.Tags.TK_Limited_Tagged
6650 -- or else K = Ada.Tags.TK_Tagged
6651 -- then
6652 -- <dispatching-call>;
6653 -- <triggering-statements>;
6655 -- else
6656 -- S :=
6657 -- Ada.Tags.Get_Offset_Index
6658 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6660 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6662 -- if C = POK_Protected_Entry then
6663 -- declare
6664 -- procedure _clean is
6665 -- begin
6666 -- if Enqueued (Bnn) then
6667 -- Cancel_Protected_Entry_Call (Bnn);
6668 -- end if;
6669 -- end _clean;
6671 -- begin
6672 -- begin
6673 -- _Disp_Asynchronous_Select
6674 -- (<object>, S, P'Address, D, B);
6675 -- Bnn := Communication_Block (D);
6677 -- Param1 := P.Param1;
6678 -- ...
6679 -- ParamN := P.ParamN;
6681 -- if Enqueued (Bnn) then
6682 -- <abortable-statements>
6683 -- end if;
6684 -- at end
6685 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6686 -- end;
6687 -- exception
6688 -- when Abort_Signal => Abort_Undefer;
6689 -- end;
6691 -- if not Cancelled (Bnn) then
6692 -- <triggering-statements>
6693 -- end if;
6695 -- elsif C = POK_Task_Entry then
6696 -- declare
6697 -- procedure _clean is
6698 -- begin
6699 -- Cancel_Task_Entry_Call (U);
6700 -- end _clean;
6702 -- begin
6703 -- Abort_Defer;
6705 -- _Disp_Asynchronous_Select
6706 -- (<object>, S, P'Address, D, B);
6707 -- Bnn := Communication_Bloc (D);
6709 -- Param1 := P.Param1;
6710 -- ...
6711 -- ParamN := P.ParamN;
6713 -- begin
6714 -- begin
6715 -- Abort_Undefer;
6716 -- <abortable-statements>
6717 -- at end
6718 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6719 -- end;
6720 -- exception
6721 -- when Abort_Signal => Abort_Undefer;
6722 -- end;
6724 -- if not U then
6725 -- <triggering-statements>
6726 -- end if;
6727 -- end;
6729 -- else
6730 -- <dispatching-call>;
6731 -- <triggering-statements>
6732 -- end if;
6733 -- end if;
6734 -- end;
6736 -- The job is to convert this to the asynchronous form
6738 -- If the trigger is a delay statement, it will have been expanded into
6739 -- a call to one of the GNARL delay procedures. This routine will convert
6740 -- this into a protected entry call on a delay object and then continue
6741 -- processing as for a protected entry call trigger. This requires
6742 -- declaring a Delay_Block object and adding a pointer to this object to
6743 -- the parameter list of the delay procedure to form the parameter list of
6744 -- the entry call. This object is used by the runtime to queue the delay
6745 -- request.
6747 -- For a description of the use of P and the assignments after the call,
6748 -- see Expand_N_Entry_Call_Statement.
6750 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6751 Loc : constant Source_Ptr := Sloc (N);
6752 Abrt : constant Node_Id := Abortable_Part (N);
6753 Trig : constant Node_Id := Triggering_Alternative (N);
6755 Abort_Block_Ent : Entity_Id;
6756 Abortable_Block : Node_Id;
6757 Actuals : List_Id;
6758 Astats : List_Id;
6759 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6760 Blk_Typ : Entity_Id;
6761 Call : Node_Id;
6762 Call_Ent : Entity_Id;
6763 Cancel_Param : Entity_Id;
6764 Cleanup_Block : Node_Id;
6765 Cleanup_Block_Ent : Entity_Id;
6766 Cleanup_Stmts : List_Id;
6767 Conc_Typ_Stmts : List_Id;
6768 Concval : Node_Id;
6769 Dblock_Ent : Entity_Id;
6770 Decl : Node_Id;
6771 Decls : List_Id;
6772 Ecall : Node_Id;
6773 Ename : Node_Id;
6774 Enqueue_Call : Node_Id;
6775 Formals : List_Id;
6776 Hdle : List_Id;
6777 Handler_Stmt : Node_Id;
6778 Index : Node_Id;
6779 Lim_Typ_Stmts : List_Id;
6780 N_Orig : Node_Id;
6781 Obj : Entity_Id;
6782 Param : Node_Id;
6783 Params : List_Id;
6784 Pdef : Entity_Id;
6785 ProtE_Stmts : List_Id;
6786 ProtP_Stmts : List_Id;
6787 Stmt : Node_Id;
6788 Stmts : List_Id;
6789 TaskE_Stmts : List_Id;
6790 Tstats : List_Id;
6792 B : Entity_Id; -- Call status flag
6793 Bnn : Entity_Id; -- Communication block
6794 C : Entity_Id; -- Call kind
6795 K : Entity_Id; -- Tagged kind
6796 P : Entity_Id; -- Parameter block
6797 S : Entity_Id; -- Primitive operation slot
6798 T : Entity_Id; -- Additional status flag
6800 procedure Rewrite_Abortable_Part;
6801 -- If the trigger is a dispatching call, the expansion inserts multiple
6802 -- copies of the abortable part. This is both inefficient, and may lead
6803 -- to duplicate definitions that the back-end will reject, when the
6804 -- abortable part includes loops. This procedure rewrites the abortable
6805 -- part into a call to a generated procedure.
6807 ----------------------------
6808 -- Rewrite_Abortable_Part --
6809 ----------------------------
6811 procedure Rewrite_Abortable_Part is
6812 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6813 Decl : Node_Id;
6815 begin
6816 Decl :=
6817 Make_Subprogram_Body (Loc,
6818 Specification =>
6819 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6820 Declarations => New_List,
6821 Handled_Statement_Sequence =>
6822 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6823 Insert_Before (N, Decl);
6824 Analyze (Decl);
6826 -- Rewrite abortable part into a call to this procedure
6828 Astats :=
6829 New_List (
6830 Make_Procedure_Call_Statement (Loc,
6831 Name => New_Occurrence_Of (Proc, Loc)));
6832 end Rewrite_Abortable_Part;
6834 -- Start of processing for Expand_N_Asynchronous_Select
6836 begin
6837 -- Asynchronous select is not supported on restricted runtimes. Don't
6838 -- try to expand.
6840 if Restricted_Profile then
6841 return;
6842 end if;
6844 Process_Statements_For_Controlled_Objects (Trig);
6845 Process_Statements_For_Controlled_Objects (Abrt);
6847 Ecall := Triggering_Statement (Trig);
6849 Ensure_Statement_Present (Sloc (Ecall), Trig);
6851 -- Retrieve Astats and Tstats now because the finalization machinery may
6852 -- wrap them in blocks.
6854 Astats := Statements (Abrt);
6855 Tstats := Statements (Trig);
6857 -- The arguments in the call may require dynamic allocation, and the
6858 -- call statement may have been transformed into a block. The block
6859 -- may contain additional declarations for internal entities, and the
6860 -- original call is found by sequential search.
6862 if Nkind (Ecall) = N_Block_Statement then
6863 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6864 while not Nkind_In (Ecall, N_Procedure_Call_Statement,
6865 N_Entry_Call_Statement)
6866 loop
6867 Next (Ecall);
6868 end loop;
6869 end if;
6871 -- This is either a dispatching call or a delay statement used as a
6872 -- trigger which was expanded into a procedure call.
6874 if Nkind (Ecall) = N_Procedure_Call_Statement then
6875 if Ada_Version >= Ada_2005
6876 and then
6877 (No (Original_Node (Ecall))
6878 or else not Nkind_In (Original_Node (Ecall),
6879 N_Delay_Relative_Statement,
6880 N_Delay_Until_Statement))
6881 then
6882 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6884 Rewrite_Abortable_Part;
6885 Decls := New_List;
6886 Stmts := New_List;
6888 -- Call status flag processing, generate:
6889 -- B : Boolean := False;
6891 B := Build_B (Loc, Decls);
6893 -- Communication block processing, generate:
6894 -- Bnn : Communication_Block;
6896 Bnn := Make_Temporary (Loc, 'B');
6897 Append_To (Decls,
6898 Make_Object_Declaration (Loc,
6899 Defining_Identifier => Bnn,
6900 Object_Definition =>
6901 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
6903 -- Call kind processing, generate:
6904 -- C : Ada.Tags.Prim_Op_Kind;
6906 C := Build_C (Loc, Decls);
6908 -- Tagged kind processing, generate:
6909 -- K : Ada.Tags.Tagged_Kind :=
6910 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6912 -- Dummy communication block, generate:
6913 -- D : Dummy_Communication_Block;
6915 Append_To (Decls,
6916 Make_Object_Declaration (Loc,
6917 Defining_Identifier =>
6918 Make_Defining_Identifier (Loc, Name_uD),
6919 Object_Definition =>
6920 New_Occurrence_Of
6921 (RTE (RE_Dummy_Communication_Block), Loc)));
6923 K := Build_K (Loc, Decls, Obj);
6925 -- Parameter block processing
6927 Blk_Typ := Build_Parameter_Block
6928 (Loc, Actuals, Formals, Decls);
6929 P := Parameter_Block_Pack
6930 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6932 -- Dispatch table slot processing, generate:
6933 -- S : Integer;
6935 S := Build_S (Loc, Decls);
6937 -- Additional status flag processing, generate:
6938 -- Tnn : Boolean;
6940 T := Make_Temporary (Loc, 'T');
6941 Append_To (Decls,
6942 Make_Object_Declaration (Loc,
6943 Defining_Identifier => T,
6944 Object_Definition =>
6945 New_Occurrence_Of (Standard_Boolean, Loc)));
6947 ------------------------------
6948 -- Protected entry handling --
6949 ------------------------------
6951 -- Generate:
6952 -- Param1 := P.Param1;
6953 -- ...
6954 -- ParamN := P.ParamN;
6956 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6958 -- Generate:
6959 -- Bnn := Communication_Block (D);
6961 Prepend_To (Cleanup_Stmts,
6962 Make_Assignment_Statement (Loc,
6963 Name => New_Occurrence_Of (Bnn, Loc),
6964 Expression =>
6965 Make_Unchecked_Type_Conversion (Loc,
6966 Subtype_Mark =>
6967 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
6968 Expression => Make_Identifier (Loc, Name_uD))));
6970 -- Generate:
6971 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6973 Prepend_To (Cleanup_Stmts,
6974 Make_Procedure_Call_Statement (Loc,
6975 Name =>
6976 New_Occurrence_Of
6977 (Find_Prim_Op
6978 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
6979 Loc),
6980 Parameter_Associations =>
6981 New_List (
6982 New_Copy_Tree (Obj), -- <object>
6983 New_Occurrence_Of (S, Loc), -- S
6984 Make_Attribute_Reference (Loc, -- P'Address
6985 Prefix => New_Occurrence_Of (P, Loc),
6986 Attribute_Name => Name_Address),
6987 Make_Identifier (Loc, Name_uD), -- D
6988 New_Occurrence_Of (B, Loc)))); -- B
6990 -- Generate:
6991 -- if Enqueued (Bnn) then
6992 -- <abortable-statements>
6993 -- end if;
6995 Append_To (Cleanup_Stmts,
6996 Make_Implicit_If_Statement (N,
6997 Condition =>
6998 Make_Function_Call (Loc,
6999 Name =>
7000 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7001 Parameter_Associations =>
7002 New_List (New_Occurrence_Of (Bnn, Loc))),
7004 Then_Statements =>
7005 New_Copy_List_Tree (Astats)));
7007 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7008 -- will then generate a _clean for the communication block Bnn.
7010 -- Generate:
7011 -- declare
7012 -- procedure _clean is
7013 -- begin
7014 -- if Enqueued (Bnn) then
7015 -- Cancel_Protected_Entry_Call (Bnn);
7016 -- end if;
7017 -- end _clean;
7018 -- begin
7019 -- Cleanup_Stmts
7020 -- at end
7021 -- _clean;
7022 -- end;
7024 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7025 Cleanup_Block :=
7026 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7028 -- Wrap the cleanup block in an exception handling block
7030 -- Generate:
7031 -- begin
7032 -- Cleanup_Block
7033 -- exception
7034 -- when Abort_Signal => Abort_Undefer;
7035 -- end;
7037 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7038 ProtE_Stmts :=
7039 New_List (
7040 Make_Implicit_Label_Declaration (Loc,
7041 Defining_Identifier => Abort_Block_Ent),
7043 Build_Abort_Block
7044 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7046 -- Generate:
7047 -- if not Cancelled (Bnn) then
7048 -- <triggering-statements>
7049 -- end if;
7051 Append_To (ProtE_Stmts,
7052 Make_Implicit_If_Statement (N,
7053 Condition =>
7054 Make_Op_Not (Loc,
7055 Right_Opnd =>
7056 Make_Function_Call (Loc,
7057 Name =>
7058 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7059 Parameter_Associations =>
7060 New_List (New_Occurrence_Of (Bnn, Loc)))),
7062 Then_Statements =>
7063 New_Copy_List_Tree (Tstats)));
7065 -------------------------
7066 -- Task entry handling --
7067 -------------------------
7069 -- Generate:
7070 -- Param1 := P.Param1;
7071 -- ...
7072 -- ParamN := P.ParamN;
7074 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7076 -- Generate:
7077 -- Bnn := Communication_Block (D);
7079 Append_To (TaskE_Stmts,
7080 Make_Assignment_Statement (Loc,
7081 Name =>
7082 New_Occurrence_Of (Bnn, Loc),
7083 Expression =>
7084 Make_Unchecked_Type_Conversion (Loc,
7085 Subtype_Mark =>
7086 New_Occurrence_Of (RTE (RE_Communication_Block), Loc),
7087 Expression => Make_Identifier (Loc, Name_uD))));
7089 -- Generate:
7090 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7092 Prepend_To (TaskE_Stmts,
7093 Make_Procedure_Call_Statement (Loc,
7094 Name =>
7095 New_Occurrence_Of (
7096 Find_Prim_Op (Etype (Etype (Obj)),
7097 Name_uDisp_Asynchronous_Select),
7098 Loc),
7100 Parameter_Associations => New_List (
7101 New_Copy_Tree (Obj), -- <object>
7102 New_Occurrence_Of (S, Loc), -- S
7103 Make_Attribute_Reference (Loc, -- P'Address
7104 Prefix => New_Occurrence_Of (P, Loc),
7105 Attribute_Name => Name_Address),
7106 Make_Identifier (Loc, Name_uD), -- D
7107 New_Occurrence_Of (B, Loc)))); -- B
7109 -- Generate:
7110 -- Abort_Defer;
7112 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7114 -- Generate:
7115 -- Abort_Undefer;
7116 -- <abortable-statements>
7118 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7120 Prepend_To
7121 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7123 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7124 -- will generate a _clean for the additional status flag.
7126 -- Generate:
7127 -- declare
7128 -- procedure _clean is
7129 -- begin
7130 -- Cancel_Task_Entry_Call (U);
7131 -- end _clean;
7132 -- begin
7133 -- Cleanup_Stmts
7134 -- at end
7135 -- _clean;
7136 -- end;
7138 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7139 Cleanup_Block :=
7140 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7142 -- Wrap the cleanup block in an exception handling block
7144 -- Generate:
7145 -- begin
7146 -- Cleanup_Block
7147 -- exception
7148 -- when Abort_Signal => Abort_Undefer;
7149 -- end;
7151 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7153 Append_To (TaskE_Stmts,
7154 Make_Implicit_Label_Declaration (Loc,
7155 Defining_Identifier => Abort_Block_Ent));
7157 Append_To (TaskE_Stmts,
7158 Build_Abort_Block
7159 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7161 -- Generate:
7162 -- if not T then
7163 -- <triggering-statements>
7164 -- end if;
7166 Append_To (TaskE_Stmts,
7167 Make_Implicit_If_Statement (N,
7168 Condition =>
7169 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7171 Then_Statements =>
7172 New_Copy_List_Tree (Tstats)));
7174 ----------------------------------
7175 -- Protected procedure handling --
7176 ----------------------------------
7178 -- Generate:
7179 -- <dispatching-call>;
7180 -- <triggering-statements>
7182 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7183 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7185 -- Generate:
7186 -- S := Ada.Tags.Get_Offset_Index
7187 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7189 Conc_Typ_Stmts :=
7190 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7192 -- Generate:
7193 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7195 Append_To (Conc_Typ_Stmts,
7196 Make_Procedure_Call_Statement (Loc,
7197 Name =>
7198 New_Occurrence_Of
7199 (Find_Prim_Op (Etype (Etype (Obj)),
7200 Name_uDisp_Get_Prim_Op_Kind),
7201 Loc),
7202 Parameter_Associations =>
7203 New_List (
7204 New_Copy_Tree (Obj),
7205 New_Occurrence_Of (S, Loc),
7206 New_Occurrence_Of (C, Loc))));
7208 -- Generate:
7209 -- if C = POK_Procedure_Entry then
7210 -- ProtE_Stmts
7211 -- elsif C = POK_Task_Entry then
7212 -- TaskE_Stmts
7213 -- else
7214 -- ProtP_Stmts
7215 -- end if;
7217 Append_To (Conc_Typ_Stmts,
7218 Make_Implicit_If_Statement (N,
7219 Condition =>
7220 Make_Op_Eq (Loc,
7221 Left_Opnd =>
7222 New_Occurrence_Of (C, Loc),
7223 Right_Opnd =>
7224 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7226 Then_Statements =>
7227 ProtE_Stmts,
7229 Elsif_Parts =>
7230 New_List (
7231 Make_Elsif_Part (Loc,
7232 Condition =>
7233 Make_Op_Eq (Loc,
7234 Left_Opnd =>
7235 New_Occurrence_Of (C, Loc),
7236 Right_Opnd =>
7237 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7239 Then_Statements =>
7240 TaskE_Stmts)),
7242 Else_Statements =>
7243 ProtP_Stmts));
7245 -- Generate:
7246 -- <dispatching-call>;
7247 -- <triggering-statements>
7249 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7250 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7252 -- Generate:
7253 -- if K = Ada.Tags.TK_Limited_Tagged
7254 -- or else K = Ada.Tags.TK_Tagged
7255 -- then
7256 -- Lim_Typ_Stmts
7257 -- else
7258 -- Conc_Typ_Stmts
7259 -- end if;
7261 Append_To (Stmts,
7262 Make_Implicit_If_Statement (N,
7263 Condition => Build_Dispatching_Tag_Check (K, N),
7264 Then_Statements => Lim_Typ_Stmts,
7265 Else_Statements => Conc_Typ_Stmts));
7267 Rewrite (N,
7268 Make_Block_Statement (Loc,
7269 Declarations =>
7270 Decls,
7271 Handled_Statement_Sequence =>
7272 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7274 Analyze (N);
7275 return;
7277 -- Delay triggering statement processing
7279 else
7280 -- Add a Delay_Block object to the parameter list of the delay
7281 -- procedure to form the parameter list of the Wait entry call.
7283 Dblock_Ent := Make_Temporary (Loc, 'D');
7285 Pdef := Entity (Name (Ecall));
7287 if Is_RTE (Pdef, RO_CA_Delay_For) then
7288 Enqueue_Call :=
7289 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7291 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7292 Enqueue_Call :=
7293 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7295 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7296 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7297 end if;
7299 Append_To (Parameter_Associations (Ecall),
7300 Make_Attribute_Reference (Loc,
7301 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7302 Attribute_Name => Name_Unchecked_Access));
7304 -- Create the inner block to protect the abortable part
7306 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7308 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7310 Abortable_Block :=
7311 Make_Block_Statement (Loc,
7312 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7313 Handled_Statement_Sequence =>
7314 Make_Handled_Sequence_Of_Statements (Loc,
7315 Statements => Astats),
7316 Has_Created_Identifier => True,
7317 Is_Asynchronous_Call_Block => True);
7319 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7321 Rewrite (Ecall,
7322 Make_Implicit_If_Statement (N,
7323 Condition =>
7324 Make_Function_Call (Loc,
7325 Name => Enqueue_Call,
7326 Parameter_Associations => Parameter_Associations (Ecall)),
7327 Then_Statements =>
7328 New_List (Make_Block_Statement (Loc,
7329 Handled_Statement_Sequence =>
7330 Make_Handled_Sequence_Of_Statements (Loc,
7331 Statements => New_List (
7332 Make_Implicit_Label_Declaration (Loc,
7333 Defining_Identifier => Blk_Ent,
7334 Label_Construct => Abortable_Block),
7335 Abortable_Block),
7336 Exception_Handlers => Hdle)))));
7338 Stmts := New_List (Ecall);
7340 -- Construct statement sequence for new block
7342 Append_To (Stmts,
7343 Make_Implicit_If_Statement (N,
7344 Condition =>
7345 Make_Function_Call (Loc,
7346 Name => New_Occurrence_Of (
7347 RTE (RE_Timed_Out), Loc),
7348 Parameter_Associations => New_List (
7349 Make_Attribute_Reference (Loc,
7350 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7351 Attribute_Name => Name_Unchecked_Access))),
7352 Then_Statements => Tstats));
7354 -- The result is the new block
7356 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7358 Rewrite (N,
7359 Make_Block_Statement (Loc,
7360 Declarations => New_List (
7361 Make_Object_Declaration (Loc,
7362 Defining_Identifier => Dblock_Ent,
7363 Aliased_Present => True,
7364 Object_Definition =>
7365 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7367 Handled_Statement_Sequence =>
7368 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7370 Analyze (N);
7371 return;
7372 end if;
7374 else
7375 N_Orig := N;
7376 end if;
7378 Extract_Entry (Ecall, Concval, Ename, Index);
7379 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7381 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7382 Decls := Declarations (Ecall);
7384 if Is_Protected_Type (Etype (Concval)) then
7386 -- Get the declarations of the block expanded from the entry call
7388 Decl := First (Decls);
7389 while Present (Decl)
7390 and then (Nkind (Decl) /= N_Object_Declaration
7391 or else not Is_RTE (Etype (Object_Definition (Decl)),
7392 RE_Communication_Block))
7393 loop
7394 Next (Decl);
7395 end loop;
7397 pragma Assert (Present (Decl));
7398 Cancel_Param := Defining_Identifier (Decl);
7400 -- Change the mode of the Protected_Entry_Call call
7402 -- Protected_Entry_Call (
7403 -- Object => po._object'Access,
7404 -- E => <entry index>;
7405 -- Uninterpreted_Data => P'Address;
7406 -- Mode => Asynchronous_Call;
7407 -- Block => Bnn);
7409 -- Skip assignments to temporaries created for in-out parameters
7411 -- This makes unwarranted assumptions about the shape of the expanded
7412 -- tree for the call, and should be cleaned up ???
7414 Stmt := First (Stmts);
7415 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7416 Next (Stmt);
7417 end loop;
7419 Call := Stmt;
7421 Param := First (Parameter_Associations (Call));
7422 while Present (Param)
7423 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7424 loop
7425 Next (Param);
7426 end loop;
7428 pragma Assert (Present (Param));
7429 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7430 Analyze (Param);
7432 -- Append an if statement to execute the abortable part
7434 -- Generate:
7435 -- if Enqueued (Bnn) then
7437 Append_To (Stmts,
7438 Make_Implicit_If_Statement (N,
7439 Condition =>
7440 Make_Function_Call (Loc,
7441 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7442 Parameter_Associations => New_List (
7443 New_Occurrence_Of (Cancel_Param, Loc))),
7444 Then_Statements => Astats));
7446 Abortable_Block :=
7447 Make_Block_Statement (Loc,
7448 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7449 Handled_Statement_Sequence =>
7450 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7451 Has_Created_Identifier => True,
7452 Is_Asynchronous_Call_Block => True);
7454 -- Aborts are not deferred at beginning of exception handlers in
7455 -- ZCX mode.
7457 if ZCX_Exceptions then
7458 Handler_Stmt := Make_Null_Statement (Loc);
7460 else
7461 Handler_Stmt := Build_Runtime_Call (Loc, RE_Abort_Undefer);
7462 end if;
7464 Stmts := New_List (
7465 Make_Block_Statement (Loc,
7466 Handled_Statement_Sequence =>
7467 Make_Handled_Sequence_Of_Statements (Loc,
7468 Statements => New_List (
7469 Make_Implicit_Label_Declaration (Loc,
7470 Defining_Identifier => Blk_Ent,
7471 Label_Construct => Abortable_Block),
7472 Abortable_Block),
7474 -- exception
7476 Exception_Handlers => New_List (
7477 Make_Implicit_Exception_Handler (Loc,
7479 -- when Abort_Signal =>
7480 -- Abort_Undefer.all;
7482 Exception_Choices =>
7483 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7484 Statements => New_List (Handler_Stmt))))),
7486 -- if not Cancelled (Bnn) then
7487 -- triggered statements
7488 -- end if;
7490 Make_Implicit_If_Statement (N,
7491 Condition => Make_Op_Not (Loc,
7492 Right_Opnd =>
7493 Make_Function_Call (Loc,
7494 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7495 Parameter_Associations => New_List (
7496 New_Occurrence_Of (Cancel_Param, Loc)))),
7497 Then_Statements => Tstats));
7499 -- Asynchronous task entry call
7501 else
7502 if No (Decls) then
7503 Decls := New_List;
7504 end if;
7506 B := Make_Defining_Identifier (Loc, Name_uB);
7508 -- Insert declaration of B in declarations of existing block
7510 Prepend_To (Decls,
7511 Make_Object_Declaration (Loc,
7512 Defining_Identifier => B,
7513 Object_Definition =>
7514 New_Occurrence_Of (Standard_Boolean, Loc)));
7516 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7518 -- Insert the declaration of C in the declarations of the existing
7519 -- block. The variable is initialized to something (True or False,
7520 -- does not matter) to prevent CodePeer from complaining about a
7521 -- possible read of an uninitialized variable.
7523 Prepend_To (Decls,
7524 Make_Object_Declaration (Loc,
7525 Defining_Identifier => Cancel_Param,
7526 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7527 Expression => New_Occurrence_Of (Standard_False, Loc),
7528 Has_Init_Expression => True));
7530 -- Remove and save the call to Call_Simple
7532 Stmt := First (Stmts);
7534 -- Skip assignments to temporaries created for in-out parameters.
7535 -- This makes unwarranted assumptions about the shape of the expanded
7536 -- tree for the call, and should be cleaned up ???
7538 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7539 Next (Stmt);
7540 end loop;
7542 Call := Stmt;
7544 -- Create the inner block to protect the abortable part
7546 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7548 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7550 Abortable_Block :=
7551 Make_Block_Statement (Loc,
7552 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7553 Handled_Statement_Sequence =>
7554 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7555 Has_Created_Identifier => True,
7556 Is_Asynchronous_Call_Block => True);
7558 Insert_After (Call,
7559 Make_Block_Statement (Loc,
7560 Handled_Statement_Sequence =>
7561 Make_Handled_Sequence_Of_Statements (Loc,
7562 Statements => New_List (
7563 Make_Implicit_Label_Declaration (Loc,
7564 Defining_Identifier => Blk_Ent,
7565 Label_Construct => Abortable_Block),
7566 Abortable_Block),
7567 Exception_Handlers => Hdle)));
7569 -- Create new call statement
7571 Params := Parameter_Associations (Call);
7573 Append_To (Params,
7574 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7575 Append_To (Params, New_Occurrence_Of (B, Loc));
7577 Rewrite (Call,
7578 Make_Procedure_Call_Statement (Loc,
7579 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7580 Parameter_Associations => Params));
7582 -- Construct statement sequence for new block
7584 Append_To (Stmts,
7585 Make_Implicit_If_Statement (N,
7586 Condition =>
7587 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7588 Then_Statements => Tstats));
7590 -- Protected the call against abort
7592 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7593 end if;
7595 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7597 -- The result is the new block
7599 Rewrite (N_Orig,
7600 Make_Block_Statement (Loc,
7601 Declarations => Decls,
7602 Handled_Statement_Sequence =>
7603 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7605 Analyze (N_Orig);
7606 end Expand_N_Asynchronous_Select;
7608 -------------------------------------
7609 -- Expand_N_Conditional_Entry_Call --
7610 -------------------------------------
7612 -- The conditional task entry call is converted to a call to
7613 -- Task_Entry_Call:
7615 -- declare
7616 -- B : Boolean;
7617 -- P : parms := (parm, parm, parm);
7619 -- begin
7620 -- Task_Entry_Call
7621 -- (<acceptor-task>, -- Acceptor
7622 -- <entry-index>, -- E
7623 -- P'Address, -- Uninterpreted_Data
7624 -- Conditional_Call, -- Mode
7625 -- B); -- Rendezvous_Successful
7626 -- parm := P.param;
7627 -- parm := P.param;
7628 -- ...
7629 -- if B then
7630 -- normal-statements
7631 -- else
7632 -- else-statements
7633 -- end if;
7634 -- end;
7636 -- For a description of the use of P and the assignments after the call,
7637 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7638 -- conditional entry call has already been expanded (by the Expand_N_Entry
7639 -- _Call_Statement procedure) as follows:
7641 -- declare
7642 -- P : parms := (parm, parm, parm);
7643 -- begin
7644 -- ... info for in-out parameters
7645 -- Call_Simple (acceptor-task, entry-index, P'Address);
7646 -- parm := P.param;
7647 -- parm := P.param;
7648 -- ...
7649 -- end;
7651 -- so the task at hand is to convert the latter expansion into the former
7653 -- The conditional protected entry call is converted to a call to
7654 -- Protected_Entry_Call:
7656 -- declare
7657 -- P : parms := (parm, parm, parm);
7658 -- Bnn : Communications_Block;
7660 -- begin
7661 -- Protected_Entry_Call
7662 -- (po._object'Access, -- Object
7663 -- <entry index>, -- E
7664 -- P'Address, -- Uninterpreted_Data
7665 -- Conditional_Call, -- Mode
7666 -- Bnn); -- Block
7667 -- parm := P.param;
7668 -- parm := P.param;
7669 -- ...
7670 -- if Cancelled (Bnn) then
7671 -- else-statements
7672 -- else
7673 -- normal-statements
7674 -- end if;
7675 -- end;
7677 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7678 -- into:
7680 -- declare
7681 -- B : Boolean := False;
7682 -- C : Ada.Tags.Prim_Op_Kind;
7683 -- K : Ada.Tags.Tagged_Kind :=
7684 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7685 -- P : Parameters := (Param1 .. ParamN);
7686 -- S : Integer;
7688 -- begin
7689 -- if K = Ada.Tags.TK_Limited_Tagged
7690 -- or else K = Ada.Tags.TK_Tagged
7691 -- then
7692 -- <dispatching-call>;
7693 -- <triggering-statements>
7695 -- else
7696 -- S :=
7697 -- Ada.Tags.Get_Offset_Index
7698 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7700 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7702 -- if C = POK_Protected_Entry
7703 -- or else C = POK_Task_Entry
7704 -- then
7705 -- Param1 := P.Param1;
7706 -- ...
7707 -- ParamN := P.ParamN;
7708 -- end if;
7710 -- if B then
7711 -- if C = POK_Procedure
7712 -- or else C = POK_Protected_Procedure
7713 -- or else C = POK_Task_Procedure
7714 -- then
7715 -- <dispatching-call>;
7716 -- end if;
7718 -- <triggering-statements>
7719 -- else
7720 -- <else-statements>
7721 -- end if;
7722 -- end if;
7723 -- end;
7725 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7726 Loc : constant Source_Ptr := Sloc (N);
7727 Alt : constant Node_Id := Entry_Call_Alternative (N);
7728 Blk : Node_Id := Entry_Call_Statement (Alt);
7730 Actuals : List_Id;
7731 Blk_Typ : Entity_Id;
7732 Call : Node_Id;
7733 Call_Ent : Entity_Id;
7734 Conc_Typ_Stmts : List_Id;
7735 Decl : Node_Id;
7736 Decls : List_Id;
7737 Formals : List_Id;
7738 Lim_Typ_Stmts : List_Id;
7739 N_Stats : List_Id;
7740 Obj : Entity_Id;
7741 Param : Node_Id;
7742 Params : List_Id;
7743 Stmt : Node_Id;
7744 Stmts : List_Id;
7745 Transient_Blk : Node_Id;
7746 Unpack : List_Id;
7748 B : Entity_Id; -- Call status flag
7749 C : Entity_Id; -- Call kind
7750 K : Entity_Id; -- Tagged kind
7751 P : Entity_Id; -- Parameter block
7752 S : Entity_Id; -- Primitive operation slot
7754 begin
7755 Process_Statements_For_Controlled_Objects (N);
7757 if Ada_Version >= Ada_2005
7758 and then Nkind (Blk) = N_Procedure_Call_Statement
7759 then
7760 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7762 Decls := New_List;
7763 Stmts := New_List;
7765 -- Call status flag processing, generate:
7766 -- B : Boolean := False;
7768 B := Build_B (Loc, Decls);
7770 -- Call kind processing, generate:
7771 -- C : Ada.Tags.Prim_Op_Kind;
7773 C := Build_C (Loc, Decls);
7775 -- Tagged kind processing, generate:
7776 -- K : Ada.Tags.Tagged_Kind :=
7777 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7779 K := Build_K (Loc, Decls, Obj);
7781 -- Parameter block processing
7783 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7784 P := Parameter_Block_Pack
7785 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7787 -- Dispatch table slot processing, generate:
7788 -- S : Integer;
7790 S := Build_S (Loc, Decls);
7792 -- Generate:
7793 -- S := Ada.Tags.Get_Offset_Index
7794 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7796 Conc_Typ_Stmts :=
7797 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7799 -- Generate:
7800 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7802 Append_To (Conc_Typ_Stmts,
7803 Make_Procedure_Call_Statement (Loc,
7804 Name =>
7805 New_Occurrence_Of (
7806 Find_Prim_Op (Etype (Etype (Obj)),
7807 Name_uDisp_Conditional_Select),
7808 Loc),
7809 Parameter_Associations =>
7810 New_List (
7811 New_Copy_Tree (Obj), -- <object>
7812 New_Occurrence_Of (S, Loc), -- S
7813 Make_Attribute_Reference (Loc, -- P'Address
7814 Prefix => New_Occurrence_Of (P, Loc),
7815 Attribute_Name => Name_Address),
7816 New_Occurrence_Of (C, Loc), -- C
7817 New_Occurrence_Of (B, Loc)))); -- B
7819 -- Generate:
7820 -- if C = POK_Protected_Entry
7821 -- or else C = POK_Task_Entry
7822 -- then
7823 -- Param1 := P.Param1;
7824 -- ...
7825 -- ParamN := P.ParamN;
7826 -- end if;
7828 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7830 -- Generate the if statement only when the packed parameters need
7831 -- explicit assignments to their corresponding actuals.
7833 if Present (Unpack) then
7834 Append_To (Conc_Typ_Stmts,
7835 Make_Implicit_If_Statement (N,
7836 Condition =>
7837 Make_Or_Else (Loc,
7838 Left_Opnd =>
7839 Make_Op_Eq (Loc,
7840 Left_Opnd =>
7841 New_Occurrence_Of (C, Loc),
7842 Right_Opnd =>
7843 New_Occurrence_Of (RTE (
7844 RE_POK_Protected_Entry), Loc)),
7846 Right_Opnd =>
7847 Make_Op_Eq (Loc,
7848 Left_Opnd =>
7849 New_Occurrence_Of (C, Loc),
7850 Right_Opnd =>
7851 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
7853 Then_Statements => Unpack));
7854 end if;
7856 -- Generate:
7857 -- if B then
7858 -- if C = POK_Procedure
7859 -- or else C = POK_Protected_Procedure
7860 -- or else C = POK_Task_Procedure
7861 -- then
7862 -- <dispatching-call>
7863 -- end if;
7864 -- <normal-statements>
7865 -- else
7866 -- <else-statements>
7867 -- end if;
7869 N_Stats := New_Copy_List_Tree (Statements (Alt));
7871 Prepend_To (N_Stats,
7872 Make_Implicit_If_Statement (N,
7873 Condition =>
7874 Make_Or_Else (Loc,
7875 Left_Opnd =>
7876 Make_Op_Eq (Loc,
7877 Left_Opnd =>
7878 New_Occurrence_Of (C, Loc),
7879 Right_Opnd =>
7880 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
7882 Right_Opnd =>
7883 Make_Or_Else (Loc,
7884 Left_Opnd =>
7885 Make_Op_Eq (Loc,
7886 Left_Opnd =>
7887 New_Occurrence_Of (C, Loc),
7888 Right_Opnd =>
7889 New_Occurrence_Of (RTE (
7890 RE_POK_Protected_Procedure), Loc)),
7892 Right_Opnd =>
7893 Make_Op_Eq (Loc,
7894 Left_Opnd =>
7895 New_Occurrence_Of (C, Loc),
7896 Right_Opnd =>
7897 New_Occurrence_Of (RTE (
7898 RE_POK_Task_Procedure), Loc)))),
7900 Then_Statements =>
7901 New_List (Blk)));
7903 Append_To (Conc_Typ_Stmts,
7904 Make_Implicit_If_Statement (N,
7905 Condition => New_Occurrence_Of (B, Loc),
7906 Then_Statements => N_Stats,
7907 Else_Statements => Else_Statements (N)));
7909 -- Generate:
7910 -- <dispatching-call>;
7911 -- <triggering-statements>
7913 Lim_Typ_Stmts := New_Copy_List_Tree (Statements (Alt));
7914 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Blk));
7916 -- Generate:
7917 -- if K = Ada.Tags.TK_Limited_Tagged
7918 -- or else K = Ada.Tags.TK_Tagged
7919 -- then
7920 -- Lim_Typ_Stmts
7921 -- else
7922 -- Conc_Typ_Stmts
7923 -- end if;
7925 Append_To (Stmts,
7926 Make_Implicit_If_Statement (N,
7927 Condition => Build_Dispatching_Tag_Check (K, N),
7928 Then_Statements => Lim_Typ_Stmts,
7929 Else_Statements => Conc_Typ_Stmts));
7931 Rewrite (N,
7932 Make_Block_Statement (Loc,
7933 Declarations =>
7934 Decls,
7935 Handled_Statement_Sequence =>
7936 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7938 -- As described above, the entry alternative is transformed into a
7939 -- block that contains the gnulli call, and possibly assignment
7940 -- statements for in-out parameters. The gnulli call may itself be
7941 -- rewritten into a transient block if some unconstrained parameters
7942 -- require it. We need to retrieve the call to complete its parameter
7943 -- list.
7945 else
7946 Transient_Blk :=
7947 First_Real_Statement (Handled_Statement_Sequence (Blk));
7949 if Present (Transient_Blk)
7950 and then Nkind (Transient_Blk) = N_Block_Statement
7951 then
7952 Blk := Transient_Blk;
7953 end if;
7955 Stmts := Statements (Handled_Statement_Sequence (Blk));
7956 Stmt := First (Stmts);
7957 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7958 Next (Stmt);
7959 end loop;
7961 Call := Stmt;
7962 Params := Parameter_Associations (Call);
7964 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7966 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7968 Param := First (Params);
7969 while Present (Param)
7970 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7971 loop
7972 Next (Param);
7973 end loop;
7975 pragma Assert (Present (Param));
7976 Rewrite (Param,
7977 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
7979 Analyze (Param);
7981 -- Find the Communication_Block parameter for the call to the
7982 -- Cancelled function.
7984 Decl := First (Declarations (Blk));
7985 while Present (Decl)
7986 and then not Is_RTE (Etype (Object_Definition (Decl)),
7987 RE_Communication_Block)
7988 loop
7989 Next (Decl);
7990 end loop;
7992 -- Add an if statement to execute the else part if the call
7993 -- does not succeed (as indicated by the Cancelled predicate).
7995 Append_To (Stmts,
7996 Make_Implicit_If_Statement (N,
7997 Condition => Make_Function_Call (Loc,
7998 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7999 Parameter_Associations => New_List (
8000 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8001 Then_Statements => Else_Statements (N),
8002 Else_Statements => Statements (Alt)));
8004 else
8005 B := Make_Defining_Identifier (Loc, Name_uB);
8007 -- Insert declaration of B in declarations of existing block
8009 if No (Declarations (Blk)) then
8010 Set_Declarations (Blk, New_List);
8011 end if;
8013 Prepend_To (Declarations (Blk),
8014 Make_Object_Declaration (Loc,
8015 Defining_Identifier => B,
8016 Object_Definition =>
8017 New_Occurrence_Of (Standard_Boolean, Loc)));
8019 -- Create new call statement
8021 Append_To (Params,
8022 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8023 Append_To (Params, New_Occurrence_Of (B, Loc));
8025 Rewrite (Call,
8026 Make_Procedure_Call_Statement (Loc,
8027 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8028 Parameter_Associations => Params));
8030 -- Construct statement sequence for new block
8032 Append_To (Stmts,
8033 Make_Implicit_If_Statement (N,
8034 Condition => New_Occurrence_Of (B, Loc),
8035 Then_Statements => Statements (Alt),
8036 Else_Statements => Else_Statements (N)));
8037 end if;
8039 -- The result is the new block
8041 Rewrite (N,
8042 Make_Block_Statement (Loc,
8043 Declarations => Declarations (Blk),
8044 Handled_Statement_Sequence =>
8045 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8046 end if;
8048 Analyze (N);
8049 end Expand_N_Conditional_Entry_Call;
8051 ---------------------------------------
8052 -- Expand_N_Delay_Relative_Statement --
8053 ---------------------------------------
8055 -- Delay statement is implemented as a procedure call to Delay_For
8056 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8057 -- simple delays imposed by the use of Protected Objects.
8059 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8060 Loc : constant Source_Ptr := Sloc (N);
8061 Proc : Entity_Id;
8063 begin
8064 -- Try to use System.Relative_Delays.Delay_For only if available. This
8065 -- is the implementation used on restricted platforms when Ada.Calendar
8066 -- is not available.
8068 if RTE_Available (RO_RD_Delay_For) then
8069 Proc := RTE (RO_RD_Delay_For);
8071 -- Otherwise, use Ada.Calendar.Delays.Delay_For and emit an error
8072 -- message if not available.
8074 else
8075 Proc := RTE (RO_CA_Delay_For);
8076 end if;
8078 Rewrite (N,
8079 Make_Procedure_Call_Statement (Loc,
8080 Name => New_Occurrence_Of (Proc, Loc),
8081 Parameter_Associations => New_List (Expression (N))));
8082 Analyze (N);
8083 end Expand_N_Delay_Relative_Statement;
8085 ------------------------------------
8086 -- Expand_N_Delay_Until_Statement --
8087 ------------------------------------
8089 -- Delay Until statement is implemented as a procedure call to
8090 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8092 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8093 Loc : constant Source_Ptr := Sloc (N);
8094 Typ : Entity_Id;
8096 begin
8097 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8098 Typ := RTE (RO_CA_Delay_Until);
8099 else
8100 Typ := RTE (RO_RT_Delay_Until);
8101 end if;
8103 Rewrite (N,
8104 Make_Procedure_Call_Statement (Loc,
8105 Name => New_Occurrence_Of (Typ, Loc),
8106 Parameter_Associations => New_List (Expression (N))));
8108 Analyze (N);
8109 end Expand_N_Delay_Until_Statement;
8111 -------------------------
8112 -- Expand_N_Entry_Body --
8113 -------------------------
8115 procedure Expand_N_Entry_Body (N : Node_Id) is
8116 begin
8117 -- Associate discriminals with the next protected operation body to be
8118 -- expanded.
8120 if Present (Next_Protected_Operation (N)) then
8121 Set_Discriminals (Parent (Current_Scope));
8122 end if;
8123 end Expand_N_Entry_Body;
8125 -----------------------------------
8126 -- Expand_N_Entry_Call_Statement --
8127 -----------------------------------
8129 -- An entry call is expanded into GNARLI calls to implement a simple entry
8130 -- call (see Build_Simple_Entry_Call).
8132 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8133 Concval : Node_Id;
8134 Ename : Node_Id;
8135 Index : Node_Id;
8137 begin
8138 if No_Run_Time_Mode then
8139 Error_Msg_CRT ("entry call", N);
8140 return;
8141 end if;
8143 -- If this entry call is part of an asynchronous select, don't expand it
8144 -- here; it will be expanded with the select statement. Don't expand
8145 -- timed entry calls either, as they are translated into asynchronous
8146 -- entry calls.
8148 -- ??? This whole approach is questionable; it may be better to go back
8149 -- to allowing the expansion to take place and then attempting to fix it
8150 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8151 -- whether the expanded call is on a task or protected entry.
8153 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8154 or else N /= Triggering_Statement (Parent (N)))
8155 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8156 or else N /= Entry_Call_Statement (Parent (N))
8157 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8158 then
8159 Extract_Entry (N, Concval, Ename, Index);
8160 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8161 end if;
8162 end Expand_N_Entry_Call_Statement;
8164 --------------------------------
8165 -- Expand_N_Entry_Declaration --
8166 --------------------------------
8168 -- If there are parameters, then first, each of the formals is marked by
8169 -- setting Is_Entry_Formal. Next a record type is built which is used to
8170 -- hold the parameter values. The name of this record type is entryP where
8171 -- entry is the name of the entry, with an additional corresponding access
8172 -- type called entryPA. The record type has matching components for each
8173 -- formal (the component names are the same as the formal names). For
8174 -- elementary types, the component type matches the formal type. For
8175 -- composite types, an access type is declared (with the name formalA)
8176 -- which designates the formal type, and the type of the component is this
8177 -- access type. Finally the Entry_Component of each formal is set to
8178 -- reference the corresponding record component.
8180 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8181 Loc : constant Source_Ptr := Sloc (N);
8182 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8183 Components : List_Id;
8184 Formal : Node_Id;
8185 Ftype : Entity_Id;
8186 Last_Decl : Node_Id;
8187 Component : Entity_Id;
8188 Ctype : Entity_Id;
8189 Decl : Node_Id;
8190 Rec_Ent : Entity_Id;
8191 Acc_Ent : Entity_Id;
8193 begin
8194 Formal := First_Formal (Entry_Ent);
8195 Last_Decl := N;
8197 -- Most processing is done only if parameters are present
8199 if Present (Formal) then
8200 Components := New_List;
8202 -- Loop through formals
8204 while Present (Formal) loop
8205 Set_Is_Entry_Formal (Formal);
8206 Component :=
8207 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8208 Set_Entry_Component (Formal, Component);
8209 Set_Entry_Formal (Component, Formal);
8210 Ftype := Etype (Formal);
8212 -- Declare new access type and then append
8214 Ctype := Make_Temporary (Loc, 'A');
8215 Set_Is_Param_Block_Component_Type (Ctype);
8217 Decl :=
8218 Make_Full_Type_Declaration (Loc,
8219 Defining_Identifier => Ctype,
8220 Type_Definition =>
8221 Make_Access_To_Object_Definition (Loc,
8222 All_Present => True,
8223 Constant_Present => Ekind (Formal) = E_In_Parameter,
8224 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8226 Insert_After (Last_Decl, Decl);
8227 Last_Decl := Decl;
8229 Append_To (Components,
8230 Make_Component_Declaration (Loc,
8231 Defining_Identifier => Component,
8232 Component_Definition =>
8233 Make_Component_Definition (Loc,
8234 Aliased_Present => False,
8235 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8237 Next_Formal_With_Extras (Formal);
8238 end loop;
8240 -- Create the Entry_Parameter_Record declaration
8242 Rec_Ent := Make_Temporary (Loc, 'P');
8244 Decl :=
8245 Make_Full_Type_Declaration (Loc,
8246 Defining_Identifier => Rec_Ent,
8247 Type_Definition =>
8248 Make_Record_Definition (Loc,
8249 Component_List =>
8250 Make_Component_List (Loc,
8251 Component_Items => Components)));
8253 Insert_After (Last_Decl, Decl);
8254 Last_Decl := Decl;
8256 -- Construct and link in the corresponding access type
8258 Acc_Ent := Make_Temporary (Loc, 'A');
8260 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8262 Decl :=
8263 Make_Full_Type_Declaration (Loc,
8264 Defining_Identifier => Acc_Ent,
8265 Type_Definition =>
8266 Make_Access_To_Object_Definition (Loc,
8267 All_Present => True,
8268 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8270 Insert_After (Last_Decl, Decl);
8271 end if;
8272 end Expand_N_Entry_Declaration;
8274 -----------------------------
8275 -- Expand_N_Protected_Body --
8276 -----------------------------
8278 -- Protected bodies are expanded to the completion of the subprograms
8279 -- created for the corresponding protected type. These are a protected and
8280 -- unprotected version of each protected subprogram in the object, a
8281 -- function to calculate each entry barrier, and a procedure to execute the
8282 -- sequence of statements of each protected entry body. For example, for
8283 -- protected type ptype:
8285 -- function entB
8286 -- (O : System.Address;
8287 -- E : Protected_Entry_Index)
8288 -- return Boolean
8289 -- is
8290 -- <discriminant renamings>
8291 -- <private object renamings>
8292 -- begin
8293 -- return <barrier expression>;
8294 -- end entB;
8296 -- procedure pprocN (_object : in out poV;...) is
8297 -- <discriminant renamings>
8298 -- <private object renamings>
8299 -- begin
8300 -- <sequence of statements>
8301 -- end pprocN;
8303 -- procedure pprocP (_object : in out poV;...) is
8304 -- procedure _clean is
8305 -- Pn : Boolean;
8306 -- begin
8307 -- ptypeS (_object, Pn);
8308 -- Unlock (_object._object'Access);
8309 -- Abort_Undefer.all;
8310 -- end _clean;
8312 -- begin
8313 -- Abort_Defer.all;
8314 -- Lock (_object._object'Access);
8315 -- pprocN (_object;...);
8316 -- at end
8317 -- _clean;
8318 -- end pproc;
8320 -- function pfuncN (_object : poV;...) return Return_Type is
8321 -- <discriminant renamings>
8322 -- <private object renamings>
8323 -- begin
8324 -- <sequence of statements>
8325 -- end pfuncN;
8327 -- function pfuncP (_object : poV) return Return_Type is
8328 -- procedure _clean is
8329 -- begin
8330 -- Unlock (_object._object'Access);
8331 -- Abort_Undefer.all;
8332 -- end _clean;
8334 -- begin
8335 -- Abort_Defer.all;
8336 -- Lock (_object._object'Access);
8337 -- return pfuncN (_object);
8339 -- at end
8340 -- _clean;
8341 -- end pfunc;
8343 -- procedure entE
8344 -- (O : System.Address;
8345 -- P : System.Address;
8346 -- E : Protected_Entry_Index)
8347 -- is
8348 -- <discriminant renamings>
8349 -- <private object renamings>
8350 -- type poVP is access poV;
8351 -- _Object : ptVP := ptVP!(O);
8353 -- begin
8354 -- begin
8355 -- <statement sequence>
8356 -- Complete_Entry_Body (_Object._Object);
8357 -- exception
8358 -- when all others =>
8359 -- Exceptional_Complete_Entry_Body (
8360 -- _Object._Object, Get_GNAT_Exception);
8361 -- end;
8362 -- end entE;
8364 -- The type poV is the record created for the protected type to hold
8365 -- the state of the protected object.
8367 procedure Expand_N_Protected_Body (N : Node_Id) is
8368 Loc : constant Source_Ptr := Sloc (N);
8369 Pid : constant Entity_Id := Corresponding_Spec (N);
8371 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8372 -- This flag indicates whether the lock free implementation is active
8374 Current_Node : Node_Id;
8375 Disp_Op_Body : Node_Id;
8376 New_Op_Body : Node_Id;
8377 Op_Body : Node_Id;
8378 Op_Id : Entity_Id;
8380 function Build_Dispatching_Subprogram_Body
8381 (N : Node_Id;
8382 Pid : Node_Id;
8383 Prot_Bod : Node_Id) return Node_Id;
8384 -- Build a dispatching version of the protected subprogram body. The
8385 -- newly generated subprogram contains a call to the original protected
8386 -- body. The following code is generated:
8388 -- function <protected-function-name> (Param1 .. ParamN) return
8389 -- <return-type> is
8390 -- begin
8391 -- return <protected-function-name>P (Param1 .. ParamN);
8392 -- end <protected-function-name>;
8394 -- or
8396 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8397 -- begin
8398 -- <protected-procedure-name>P (Param1 .. ParamN);
8399 -- end <protected-procedure-name>
8401 ---------------------------------------
8402 -- Build_Dispatching_Subprogram_Body --
8403 ---------------------------------------
8405 function Build_Dispatching_Subprogram_Body
8406 (N : Node_Id;
8407 Pid : Node_Id;
8408 Prot_Bod : Node_Id) return Node_Id
8410 Loc : constant Source_Ptr := Sloc (N);
8411 Actuals : List_Id;
8412 Formal : Node_Id;
8413 Spec : Node_Id;
8414 Stmts : List_Id;
8416 begin
8417 -- Generate a specification without a letter suffix in order to
8418 -- override an interface function or procedure.
8420 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8422 -- The formal parameters become the actuals of the protected function
8423 -- or procedure call.
8425 Actuals := New_List;
8426 Formal := First (Parameter_Specifications (Spec));
8427 while Present (Formal) loop
8428 Append_To (Actuals,
8429 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8430 Next (Formal);
8431 end loop;
8433 if Nkind (Spec) = N_Procedure_Specification then
8434 Stmts :=
8435 New_List (
8436 Make_Procedure_Call_Statement (Loc,
8437 Name =>
8438 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8439 Parameter_Associations => Actuals));
8441 else
8442 pragma Assert (Nkind (Spec) = N_Function_Specification);
8444 Stmts :=
8445 New_List (
8446 Make_Simple_Return_Statement (Loc,
8447 Expression =>
8448 Make_Function_Call (Loc,
8449 Name =>
8450 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8451 Parameter_Associations => Actuals)));
8452 end if;
8454 return
8455 Make_Subprogram_Body (Loc,
8456 Declarations => Empty_List,
8457 Specification => Spec,
8458 Handled_Statement_Sequence =>
8459 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8460 end Build_Dispatching_Subprogram_Body;
8462 -- Start of processing for Expand_N_Protected_Body
8464 begin
8465 if No_Run_Time_Mode then
8466 Error_Msg_CRT ("protected body", N);
8467 return;
8468 end if;
8470 -- This is the proper body corresponding to a stub. The declarations
8471 -- must be inserted at the point of the stub, which in turn is in the
8472 -- declarative part of the parent unit.
8474 if Nkind (Parent (N)) = N_Subunit then
8475 Current_Node := Corresponding_Stub (Parent (N));
8476 else
8477 Current_Node := N;
8478 end if;
8480 Op_Body := First (Declarations (N));
8482 -- The protected body is replaced with the bodies of its protected
8483 -- operations, and the declarations for internal objects that may
8484 -- have been created for entry family bounds.
8486 Rewrite (N, Make_Null_Statement (Sloc (N)));
8487 Analyze (N);
8489 while Present (Op_Body) loop
8490 case Nkind (Op_Body) is
8491 when N_Subprogram_Declaration =>
8492 null;
8494 when N_Subprogram_Body =>
8496 -- Do not create bodies for eliminated operations
8498 if not Is_Eliminated (Defining_Entity (Op_Body))
8499 and then not Is_Eliminated (Corresponding_Spec (Op_Body))
8500 then
8501 if Lock_Free_Active then
8502 New_Op_Body :=
8503 Build_Lock_Free_Unprotected_Subprogram_Body
8504 (Op_Body, Pid);
8505 else
8506 New_Op_Body :=
8507 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8508 end if;
8510 Insert_After (Current_Node, New_Op_Body);
8511 Current_Node := New_Op_Body;
8512 Analyze (New_Op_Body);
8514 -- Build the corresponding protected operation. It may
8515 -- appear that this is needed only if this is a visible
8516 -- operation of the type, or if it is an interrupt handler,
8517 -- and this was the strategy used previously in GNAT.
8519 -- However, the operation may be exported through a 'Access
8520 -- to an external caller. This is the common idiom in code
8521 -- that uses the Ada 2005 Timing_Events package. As a result
8522 -- we need to produce the protected body for both visible
8523 -- and private operations, as well as operations that only
8524 -- have a body in the source, and for which we create a
8525 -- declaration in the protected body itself.
8527 if Present (Corresponding_Spec (Op_Body)) then
8528 if Lock_Free_Active then
8529 New_Op_Body :=
8530 Build_Lock_Free_Protected_Subprogram_Body
8531 (Op_Body, Pid, Specification (New_Op_Body));
8532 else
8533 New_Op_Body :=
8534 Build_Protected_Subprogram_Body
8535 (Op_Body, Pid, Specification (New_Op_Body));
8536 end if;
8538 Insert_After (Current_Node, New_Op_Body);
8539 Analyze (New_Op_Body);
8541 Current_Node := New_Op_Body;
8543 -- Generate an overriding primitive operation body for
8544 -- this subprogram if the protected type implements an
8545 -- interface.
8547 if Ada_Version >= Ada_2005
8548 and then
8549 Present (Interfaces (Corresponding_Record_Type (Pid)))
8550 then
8551 Disp_Op_Body :=
8552 Build_Dispatching_Subprogram_Body
8553 (Op_Body, Pid, New_Op_Body);
8555 Insert_After (Current_Node, Disp_Op_Body);
8556 Analyze (Disp_Op_Body);
8558 Current_Node := Disp_Op_Body;
8559 end if;
8560 end if;
8561 end if;
8563 when N_Entry_Body =>
8564 Op_Id := Defining_Identifier (Op_Body);
8565 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8567 Insert_After (Current_Node, New_Op_Body);
8568 Current_Node := New_Op_Body;
8569 Analyze (New_Op_Body);
8571 when N_Implicit_Label_Declaration =>
8572 null;
8574 when N_Itype_Reference =>
8575 Insert_After (Current_Node, New_Copy (Op_Body));
8577 when N_Freeze_Entity =>
8578 New_Op_Body := New_Copy (Op_Body);
8580 if Present (Entity (Op_Body))
8581 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8582 then
8583 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8584 end if;
8586 Insert_After (Current_Node, New_Op_Body);
8587 Current_Node := New_Op_Body;
8588 Analyze (New_Op_Body);
8590 when N_Pragma =>
8591 New_Op_Body := New_Copy (Op_Body);
8592 Insert_After (Current_Node, New_Op_Body);
8593 Current_Node := New_Op_Body;
8594 Analyze (New_Op_Body);
8596 when N_Object_Declaration =>
8597 pragma Assert (not Comes_From_Source (Op_Body));
8598 New_Op_Body := New_Copy (Op_Body);
8599 Insert_After (Current_Node, New_Op_Body);
8600 Current_Node := New_Op_Body;
8601 Analyze (New_Op_Body);
8603 when others =>
8604 raise Program_Error;
8605 end case;
8607 Next (Op_Body);
8608 end loop;
8610 -- Finally, create the body of the function that maps an entry index
8611 -- into the corresponding body index, except when there is no entry, or
8612 -- in a Ravenscar-like profile.
8614 if Corresponding_Runtime_Package (Pid) =
8615 System_Tasking_Protected_Objects_Entries
8616 then
8617 New_Op_Body := Build_Find_Body_Index (Pid);
8618 Insert_After (Current_Node, New_Op_Body);
8619 Current_Node := New_Op_Body;
8620 Analyze (New_Op_Body);
8621 end if;
8623 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8624 -- protected body. At this point all wrapper specs have been created,
8625 -- frozen and included in the dispatch table for the protected type.
8627 if Ada_Version >= Ada_2005 then
8628 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8629 end if;
8630 end Expand_N_Protected_Body;
8632 -----------------------------------------
8633 -- Expand_N_Protected_Type_Declaration --
8634 -----------------------------------------
8636 -- First we create a corresponding record type declaration used to
8637 -- represent values of this protected type.
8638 -- The general form of this type declaration is
8640 -- type poV (discriminants) is record
8641 -- _Object : aliased <kind>Protection
8642 -- [(<entry count> [, <handler count>])];
8643 -- [entry_family : array (bounds) of Void;]
8644 -- <private data fields>
8645 -- end record;
8647 -- The discriminants are present only if the corresponding protected type
8648 -- has discriminants, and they exactly mirror the protected type
8649 -- discriminants. The private data fields similarly mirror the private
8650 -- declarations of the protected type.
8652 -- The Object field is always present. It contains RTS specific data used
8653 -- to control the protected object. It is declared as Aliased so that it
8654 -- can be passed as a pointer to the RTS. This allows the protected record
8655 -- to be referenced within RTS data structures. An appropriate Protection
8656 -- type and discriminant are generated.
8658 -- The Service field is present for protected objects with entries. It
8659 -- contains sufficient information to allow the entry service procedure for
8660 -- this object to be called when the object is not known till runtime.
8662 -- One entry_family component is present for each entry family in the
8663 -- task definition (see Expand_N_Task_Type_Declaration).
8665 -- When a protected object is declared, an instance of the protected type
8666 -- value record is created. The elaboration of this declaration creates the
8667 -- correct bounds for the entry families, and also evaluates the priority
8668 -- expression if needed. The initialization routine for the protected type
8669 -- itself then calls Initialize_Protection with appropriate parameters to
8670 -- initialize the value of the Task_Id field. Install_Handlers may be also
8671 -- called if a pragma Attach_Handler applies.
8673 -- Note: this record is passed to the subprograms created by the expansion
8674 -- of protected subprograms and entries. It is an in parameter to protected
8675 -- functions and an in out parameter to procedures and entry bodies. The
8676 -- Entity_Id for this created record type is placed in the
8677 -- Corresponding_Record_Type field of the associated protected type entity.
8679 -- Next we create a procedure specifications for protected subprograms and
8680 -- entry bodies. For each protected subprograms two subprograms are
8681 -- created, an unprotected and a protected version. The unprotected version
8682 -- is called from within other operations of the same protected object.
8684 -- We also build the call to register the procedure if a pragma
8685 -- Interrupt_Handler applies.
8687 -- A single subprogram is created to service all entry bodies; it has an
8688 -- additional boolean out parameter indicating that the previous entry call
8689 -- made by the current task was serviced immediately, i.e. not by proxy.
8690 -- The O parameter contains a pointer to a record object of the type
8691 -- described above. An untyped interface is used here to allow this
8692 -- procedure to be called in places where the type of the object to be
8693 -- serviced is not known. This must be done, for example, when a call that
8694 -- may have been requeued is cancelled; the corresponding object must be
8695 -- serviced, but which object that is not known till runtime.
8697 -- procedure ptypeS
8698 -- (O : System.Address; P : out Boolean);
8699 -- procedure pprocN (_object : in out poV);
8700 -- procedure pproc (_object : in out poV);
8701 -- function pfuncN (_object : poV);
8702 -- function pfunc (_object : poV);
8703 -- ...
8705 -- Note that this must come after the record type declaration, since
8706 -- the specs refer to this type.
8708 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8709 Discr_Map : constant Elist_Id := New_Elmt_List;
8710 Loc : constant Source_Ptr := Sloc (N);
8711 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8713 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8714 -- This flag indicates whether the lock free implementation is active
8716 Pdef : constant Node_Id := Protected_Definition (N);
8717 -- This contains two lists; one for visible and one for private decls
8719 Current_Node : Node_Id := N;
8720 E_Count : Int;
8721 Entries_Aggr : Node_Id;
8723 procedure Check_Inlining (Subp : Entity_Id);
8724 -- If the original operation has a pragma Inline, propagate the flag
8725 -- to the internal body, for possible inlining later on. The source
8726 -- operation is invisible to the back-end and is never actually called.
8728 procedure Expand_Entry_Declaration (Decl : Node_Id);
8729 -- Create the entry barrier and the procedure body for entry declaration
8730 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
8732 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8733 -- When compiling under the Ravenscar profile, private components must
8734 -- have a static size, or else a protected object will require heap
8735 -- allocation, violating the corresponding restriction. It is preferable
8736 -- to make this check here, because it provides a better error message
8737 -- than the back-end, which refers to the object as a whole.
8739 procedure Register_Handler;
8740 -- For a protected operation that is an interrupt handler, add the
8741 -- freeze action that will register it as such.
8743 --------------------
8744 -- Check_Inlining --
8745 --------------------
8747 procedure Check_Inlining (Subp : Entity_Id) is
8748 begin
8749 if Is_Inlined (Subp) then
8750 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8751 Set_Is_Inlined (Subp, False);
8752 end if;
8753 end Check_Inlining;
8755 ---------------------------
8756 -- Static_Component_Size --
8757 ---------------------------
8759 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8760 Typ : constant Entity_Id := Etype (Comp);
8761 C : Entity_Id;
8763 begin
8764 if Is_Scalar_Type (Typ) then
8765 return True;
8767 elsif Is_Array_Type (Typ) then
8768 return Compile_Time_Known_Bounds (Typ);
8770 elsif Is_Record_Type (Typ) then
8771 C := First_Component (Typ);
8772 while Present (C) loop
8773 if not Static_Component_Size (C) then
8774 return False;
8775 end if;
8777 Next_Component (C);
8778 end loop;
8780 return True;
8782 -- Any other type will be checked by the back-end
8784 else
8785 return True;
8786 end if;
8787 end Static_Component_Size;
8789 ------------------------------
8790 -- Expand_Entry_Declaration --
8791 ------------------------------
8793 procedure Expand_Entry_Declaration (Decl : Node_Id) is
8794 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
8795 Bar_Id : Entity_Id;
8796 Bod_Id : Entity_Id;
8797 Subp : Node_Id;
8799 begin
8800 E_Count := E_Count + 1;
8802 -- Create the protected body subprogram
8804 Bod_Id :=
8805 Make_Defining_Identifier (Loc,
8806 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
8807 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
8809 Subp :=
8810 Make_Subprogram_Declaration (Loc,
8811 Specification =>
8812 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
8814 Insert_After (Current_Node, Subp);
8815 Current_Node := Subp;
8817 Analyze (Subp);
8819 -- Build a wrapper procedure to handle contract cases, preconditions,
8820 -- and postconditions.
8822 Build_Contract_Wrapper (Ent_Id, N);
8824 -- Create the barrier function
8826 Bar_Id :=
8827 Make_Defining_Identifier (Loc,
8828 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
8829 Set_Barrier_Function (Ent_Id, Bar_Id);
8831 Subp :=
8832 Make_Subprogram_Declaration (Loc,
8833 Specification =>
8834 Build_Barrier_Function_Specification (Loc, Bar_Id));
8835 Set_Is_Entry_Barrier_Function (Subp);
8837 Insert_After (Current_Node, Subp);
8838 Current_Node := Subp;
8840 Analyze (Subp);
8842 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
8843 Set_Scope (Bar_Id, Scope (Ent_Id));
8845 -- Collect pointers to the protected subprogram and the barrier
8846 -- of the current entry, for insertion into Entry_Bodies_Array.
8848 Append_To (Expressions (Entries_Aggr),
8849 Make_Aggregate (Loc,
8850 Expressions => New_List (
8851 Make_Attribute_Reference (Loc,
8852 Prefix => New_Occurrence_Of (Bar_Id, Loc),
8853 Attribute_Name => Name_Unrestricted_Access),
8854 Make_Attribute_Reference (Loc,
8855 Prefix => New_Occurrence_Of (Bod_Id, Loc),
8856 Attribute_Name => Name_Unrestricted_Access))));
8857 end Expand_Entry_Declaration;
8859 ----------------------
8860 -- Register_Handler --
8861 ----------------------
8863 procedure Register_Handler is
8865 -- All semantic checks already done in Sem_Prag
8867 Prot_Proc : constant Entity_Id :=
8868 Defining_Unit_Name (Specification (Current_Node));
8870 Proc_Address : constant Node_Id :=
8871 Make_Attribute_Reference (Loc,
8872 Prefix =>
8873 New_Occurrence_Of (Prot_Proc, Loc),
8874 Attribute_Name => Name_Address);
8876 RTS_Call : constant Entity_Id :=
8877 Make_Procedure_Call_Statement (Loc,
8878 Name =>
8879 New_Occurrence_Of
8880 (RTE (RE_Register_Interrupt_Handler), Loc),
8881 Parameter_Associations => New_List (Proc_Address));
8882 begin
8883 Append_Freeze_Action (Prot_Proc, RTS_Call);
8884 end Register_Handler;
8886 -- Local variables
8888 Body_Arr : Node_Id;
8889 Body_Id : Entity_Id;
8890 Cdecls : List_Id;
8891 Comp : Node_Id;
8892 Expr : Node_Id;
8893 New_Priv : Node_Id;
8894 Obj_Def : Node_Id;
8895 Object_Comp : Node_Id;
8896 Priv : Node_Id;
8897 Rec_Decl : Node_Id;
8898 Sub : Node_Id;
8900 -- Start of processing for Expand_N_Protected_Type_Declaration
8902 begin
8903 if Present (Corresponding_Record_Type (Prot_Typ)) then
8904 return;
8905 else
8906 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8907 end if;
8909 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8911 Qualify_Entity_Names (N);
8913 -- If the type has discriminants, their occurrences in the declaration
8914 -- have been replaced by the corresponding discriminals. For components
8915 -- that are constrained by discriminants, their homologues in the
8916 -- corresponding record type must refer to the discriminants of that
8917 -- record, so we must apply a new renaming to subtypes_indications:
8919 -- protected discriminant => discriminal => record discriminant
8921 -- This replacement is not applied to default expressions, for which
8922 -- the discriminal is correct.
8924 if Has_Discriminants (Prot_Typ) then
8925 declare
8926 Disc : Entity_Id;
8927 Decl : Node_Id;
8929 begin
8930 Disc := First_Discriminant (Prot_Typ);
8931 Decl := First (Discriminant_Specifications (Rec_Decl));
8932 while Present (Disc) loop
8933 Append_Elmt (Discriminal (Disc), Discr_Map);
8934 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
8935 Next_Discriminant (Disc);
8936 Next (Decl);
8937 end loop;
8938 end;
8939 end if;
8941 -- Fill in the component declarations
8943 -- Add components for entry families. For each entry family, create an
8944 -- anonymous type declaration with the same size, and analyze the type.
8946 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
8948 pragma Assert (Present (Pdef));
8950 Insert_After (Current_Node, Rec_Decl);
8951 Current_Node := Rec_Decl;
8953 -- Add private field components
8955 if Present (Private_Declarations (Pdef)) then
8956 Priv := First (Private_Declarations (Pdef));
8957 while Present (Priv) loop
8958 if Nkind (Priv) = N_Component_Declaration then
8959 if not Static_Component_Size (Defining_Identifier (Priv)) then
8961 -- When compiling for a restricted profile, the private
8962 -- components must have a static size. If not, this is an
8963 -- error for a single protected declaration, and rates a
8964 -- warning on a protected type declaration.
8966 if not Comes_From_Source (Prot_Typ) then
8968 -- It's ok to be checking this restriction at expansion
8969 -- time, because this is only for the restricted profile,
8970 -- which is not subject to strict RM conformance, so it
8971 -- is OK to miss this check in -gnatc mode.
8973 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
8974 Check_Restriction
8975 (No_Implicit_Protected_Object_Allocations, Priv);
8977 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
8978 if not Discriminated_Size (Defining_Identifier (Priv))
8979 then
8980 -- Any object of the type will be non-static
8982 Error_Msg_N ("component has non-static size??", Priv);
8983 Error_Msg_NE
8984 ("\creation of protected object of type& will "
8985 & "violate restriction "
8986 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
8987 else
8988 -- Object will be non-static if discriminants are
8990 Error_Msg_NE
8991 ("creation of protected object of type& with "
8992 & "non-static discriminants will violate "
8993 & "restriction No_Implicit_Heap_Allocations??",
8994 Priv, Prot_Typ);
8995 end if;
8997 -- Likewise for No_Implicit_Protected_Object_Allocations
8999 elsif Restriction_Active
9000 (No_Implicit_Protected_Object_Allocations)
9001 then
9002 if not Discriminated_Size (Defining_Identifier (Priv))
9003 then
9004 -- Any object of the type will be non-static
9006 Error_Msg_N ("component has non-static size??", Priv);
9007 Error_Msg_NE
9008 ("\creation of protected object of type& will "
9009 & "violate restriction "
9010 & "No_Implicit_Protected_Object_Allocations??",
9011 Priv, Prot_Typ);
9012 else
9013 -- Object will be non-static if discriminants are
9015 Error_Msg_NE
9016 ("creation of protected object of type& with "
9017 & "non-static discriminants will violate "
9018 & "restriction "
9019 & "No_Implicit_Protected_Object_Allocations??",
9020 Priv, Prot_Typ);
9021 end if;
9022 end if;
9023 end if;
9025 -- The component definition consists of a subtype indication,
9026 -- or (in Ada 2005) an access definition. Make a copy of the
9027 -- proper definition.
9029 declare
9030 Old_Comp : constant Node_Id := Component_Definition (Priv);
9031 Oent : constant Entity_Id := Defining_Identifier (Priv);
9032 Nent : constant Entity_Id :=
9033 Make_Defining_Identifier (Sloc (Oent),
9034 Chars => Chars (Oent));
9035 New_Comp : Node_Id;
9037 begin
9038 if Present (Subtype_Indication (Old_Comp)) then
9039 New_Comp :=
9040 Make_Component_Definition (Sloc (Oent),
9041 Aliased_Present => False,
9042 Subtype_Indication =>
9043 New_Copy_Tree
9044 (Subtype_Indication (Old_Comp), Discr_Map));
9045 else
9046 New_Comp :=
9047 Make_Component_Definition (Sloc (Oent),
9048 Aliased_Present => False,
9049 Access_Definition =>
9050 New_Copy_Tree
9051 (Access_Definition (Old_Comp), Discr_Map));
9052 end if;
9054 New_Priv :=
9055 Make_Component_Declaration (Loc,
9056 Defining_Identifier => Nent,
9057 Component_Definition => New_Comp,
9058 Expression => Expression (Priv));
9060 Set_Has_Per_Object_Constraint (Nent,
9061 Has_Per_Object_Constraint (Oent));
9063 Append_To (Cdecls, New_Priv);
9064 end;
9066 elsif Nkind (Priv) = N_Subprogram_Declaration then
9068 -- Make the unprotected version of the subprogram available
9069 -- for expansion of intra object calls. There is need for
9070 -- a protected version only if the subprogram is an interrupt
9071 -- handler, otherwise this operation can only be called from
9072 -- within the body.
9074 Sub :=
9075 Make_Subprogram_Declaration (Loc,
9076 Specification =>
9077 Build_Protected_Sub_Specification
9078 (Priv, Prot_Typ, Unprotected_Mode));
9080 Insert_After (Current_Node, Sub);
9081 Analyze (Sub);
9083 Set_Protected_Body_Subprogram
9084 (Defining_Unit_Name (Specification (Priv)),
9085 Defining_Unit_Name (Specification (Sub)));
9086 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9087 Current_Node := Sub;
9089 Sub :=
9090 Make_Subprogram_Declaration (Loc,
9091 Specification =>
9092 Build_Protected_Sub_Specification
9093 (Priv, Prot_Typ, Protected_Mode));
9095 Insert_After (Current_Node, Sub);
9096 Analyze (Sub);
9097 Current_Node := Sub;
9099 if Is_Interrupt_Handler
9100 (Defining_Unit_Name (Specification (Priv)))
9101 then
9102 if not Restricted_Profile then
9103 Register_Handler;
9104 end if;
9105 end if;
9106 end if;
9108 Next (Priv);
9109 end loop;
9110 end if;
9112 -- Except for the lock-free implementation, append the _Object field
9113 -- with the right type to the component list. We need to compute the
9114 -- number of entries, and in some cases the number of Attach_Handler
9115 -- pragmas.
9117 if not Lock_Free_Active then
9118 declare
9119 Entry_Count_Expr : constant Node_Id :=
9120 Build_Entry_Count_Expression
9121 (Prot_Typ, Cdecls, Loc);
9122 Num_Attach_Handler : Nat := 0;
9123 Protection_Subtype : Node_Id;
9124 Ritem : Node_Id;
9126 begin
9127 if Has_Attach_Handler (Prot_Typ) then
9128 Ritem := First_Rep_Item (Prot_Typ);
9129 while Present (Ritem) loop
9130 if Nkind (Ritem) = N_Pragma
9131 and then Pragma_Name (Ritem) = Name_Attach_Handler
9132 then
9133 Num_Attach_Handler := Num_Attach_Handler + 1;
9134 end if;
9136 Next_Rep_Item (Ritem);
9137 end loop;
9138 end if;
9140 -- Determine the proper protection type. There are two special
9141 -- cases: 1) when the protected type has dynamic interrupt
9142 -- handlers, and 2) when it has static handlers and we use a
9143 -- restricted profile.
9145 if Has_Attach_Handler (Prot_Typ)
9146 and then not Restricted_Profile
9147 then
9148 Protection_Subtype :=
9149 Make_Subtype_Indication (Loc,
9150 Subtype_Mark =>
9151 New_Occurrence_Of
9152 (RTE (RE_Static_Interrupt_Protection), Loc),
9153 Constraint =>
9154 Make_Index_Or_Discriminant_Constraint (Loc,
9155 Constraints => New_List (
9156 Entry_Count_Expr,
9157 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9159 elsif Has_Interrupt_Handler (Prot_Typ)
9160 and then not Restriction_Active (No_Dynamic_Attachment)
9161 then
9162 Protection_Subtype :=
9163 Make_Subtype_Indication (Loc,
9164 Subtype_Mark =>
9165 New_Occurrence_Of
9166 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9167 Constraint =>
9168 Make_Index_Or_Discriminant_Constraint (Loc,
9169 Constraints => New_List (Entry_Count_Expr)));
9171 else
9172 case Corresponding_Runtime_Package (Prot_Typ) is
9173 when System_Tasking_Protected_Objects_Entries =>
9174 Protection_Subtype :=
9175 Make_Subtype_Indication (Loc,
9176 Subtype_Mark =>
9177 New_Occurrence_Of
9178 (RTE (RE_Protection_Entries), Loc),
9179 Constraint =>
9180 Make_Index_Or_Discriminant_Constraint (Loc,
9181 Constraints => New_List (Entry_Count_Expr)));
9183 when System_Tasking_Protected_Objects_Single_Entry =>
9184 Protection_Subtype :=
9185 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9187 when System_Tasking_Protected_Objects =>
9188 Protection_Subtype :=
9189 New_Occurrence_Of (RTE (RE_Protection), Loc);
9191 when others =>
9192 raise Program_Error;
9193 end case;
9194 end if;
9196 Object_Comp :=
9197 Make_Component_Declaration (Loc,
9198 Defining_Identifier =>
9199 Make_Defining_Identifier (Loc, Name_uObject),
9200 Component_Definition =>
9201 Make_Component_Definition (Loc,
9202 Aliased_Present => True,
9203 Subtype_Indication => Protection_Subtype));
9204 end;
9206 -- Put the _Object component after the private component so that it
9207 -- be finalized early as required by 9.4 (20)
9209 Append_To (Cdecls, Object_Comp);
9210 end if;
9212 -- Analyze the record declaration immediately after construction,
9213 -- because the initialization procedure is needed for single object
9214 -- declarations before the next entity is analyzed (the freeze call
9215 -- that generates this initialization procedure is found below).
9217 Analyze (Rec_Decl, Suppress => All_Checks);
9219 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9220 -- the corresponding record is frozen. If any wrappers are generated,
9221 -- Current_Node is updated accordingly.
9223 if Ada_Version >= Ada_2005 then
9224 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9225 end if;
9227 -- Collect pointers to entry bodies and their barriers, to be placed
9228 -- in the Entry_Bodies_Array for the type. For each entry/family we
9229 -- add an expression to the aggregate which is the initial value of
9230 -- this array. The array is declared after all protected subprograms.
9232 if Has_Entries (Prot_Typ) then
9233 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9234 else
9235 Entries_Aggr := Empty;
9236 end if;
9238 -- Build two new procedure specifications for each protected subprogram;
9239 -- one to call from outside the object and one to call from inside.
9240 -- Build a barrier function and an entry body action procedure
9241 -- specification for each protected entry. Initialize the entry body
9242 -- array. If subprogram is flagged as eliminated, do not generate any
9243 -- internal operations.
9245 E_Count := 0;
9246 Comp := First (Visible_Declarations (Pdef));
9247 while Present (Comp) loop
9248 if Nkind (Comp) = N_Subprogram_Declaration then
9249 Sub :=
9250 Make_Subprogram_Declaration (Loc,
9251 Specification =>
9252 Build_Protected_Sub_Specification
9253 (Comp, Prot_Typ, Unprotected_Mode));
9255 Insert_After (Current_Node, Sub);
9256 Analyze (Sub);
9258 Set_Protected_Body_Subprogram
9259 (Defining_Unit_Name (Specification (Comp)),
9260 Defining_Unit_Name (Specification (Sub)));
9261 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9263 -- Make the protected version of the subprogram available for
9264 -- expansion of external calls.
9266 Current_Node := Sub;
9268 Sub :=
9269 Make_Subprogram_Declaration (Loc,
9270 Specification =>
9271 Build_Protected_Sub_Specification
9272 (Comp, Prot_Typ, Protected_Mode));
9274 Insert_After (Current_Node, Sub);
9275 Analyze (Sub);
9277 Current_Node := Sub;
9279 -- Generate an overriding primitive operation specification for
9280 -- this subprogram if the protected type implements an interface
9281 -- and Build_Wrapper_Spec did not generate its wrapper.
9283 if Ada_Version >= Ada_2005
9284 and then
9285 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9286 then
9287 declare
9288 Found : Boolean := False;
9289 Prim_Elmt : Elmt_Id;
9290 Prim_Op : Node_Id;
9292 begin
9293 Prim_Elmt :=
9294 First_Elmt
9295 (Primitive_Operations
9296 (Corresponding_Record_Type (Prot_Typ)));
9298 while Present (Prim_Elmt) loop
9299 Prim_Op := Node (Prim_Elmt);
9301 if Is_Primitive_Wrapper (Prim_Op)
9302 and then Wrapped_Entity (Prim_Op) =
9303 Defining_Entity (Specification (Comp))
9304 then
9305 Found := True;
9306 exit;
9307 end if;
9309 Next_Elmt (Prim_Elmt);
9310 end loop;
9312 if not Found then
9313 Sub :=
9314 Make_Subprogram_Declaration (Loc,
9315 Specification =>
9316 Build_Protected_Sub_Specification
9317 (Comp, Prot_Typ, Dispatching_Mode));
9319 Insert_After (Current_Node, Sub);
9320 Analyze (Sub);
9322 Current_Node := Sub;
9323 end if;
9324 end;
9325 end if;
9327 -- If a pragma Interrupt_Handler applies, build and add a call to
9328 -- Register_Interrupt_Handler to the freezing actions of the
9329 -- protected version (Current_Node) of the subprogram:
9331 -- system.interrupts.register_interrupt_handler
9332 -- (prot_procP'address);
9334 if not Restricted_Profile
9335 and then Is_Interrupt_Handler
9336 (Defining_Unit_Name (Specification (Comp)))
9337 then
9338 Register_Handler;
9339 end if;
9341 elsif Nkind (Comp) = N_Entry_Declaration then
9342 Expand_Entry_Declaration (Comp);
9343 end if;
9345 Next (Comp);
9346 end loop;
9348 -- If there are some private entry declarations, expand it as if they
9349 -- were visible entries.
9351 if Present (Private_Declarations (Pdef)) then
9352 Comp := First (Private_Declarations (Pdef));
9353 while Present (Comp) loop
9354 if Nkind (Comp) = N_Entry_Declaration then
9355 Expand_Entry_Declaration (Comp);
9356 end if;
9358 Next (Comp);
9359 end loop;
9360 end if;
9362 -- Create the declaration of an array object which contains the values
9363 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9364 -- type. This object is later passed to the appropriate protected object
9365 -- initialization routine.
9367 if Has_Entries (Prot_Typ)
9368 and then Corresponding_Runtime_Package (Prot_Typ) =
9369 System_Tasking_Protected_Objects_Entries
9370 then
9371 declare
9372 Count : Int;
9373 Item : Entity_Id;
9374 Max_Vals : Node_Id;
9375 Maxes : List_Id;
9376 Maxes_Id : Entity_Id;
9377 Need_Array : Boolean := False;
9379 begin
9380 -- First check if there is any Max_Queue_Length pragma
9382 Item := First_Entity (Prot_Typ);
9383 while Present (Item) loop
9384 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9385 Need_Array := True;
9386 exit;
9387 end if;
9389 Next_Entity (Item);
9390 end loop;
9392 -- Gather the Max_Queue_Length values of all entries in a list. A
9393 -- value of zero indicates that the entry has no limitation on its
9394 -- queue length.
9396 if Need_Array then
9397 Count := 0;
9398 Item := First_Entity (Prot_Typ);
9399 Maxes := New_List;
9400 while Present (Item) loop
9401 if Is_Entry (Item) then
9402 Count := Count + 1;
9403 Append_To (Maxes,
9404 Make_Integer_Literal
9405 (Loc, Get_Max_Queue_Length (Item)));
9406 end if;
9408 Next_Entity (Item);
9409 end loop;
9411 -- Create the declaration of the array object. Generate:
9413 -- Maxes_Id : aliased constant
9414 -- Protected_Entry_Queue_Max_Array
9415 -- (1 .. Count) := (..., ...);
9417 Maxes_Id :=
9418 Make_Defining_Identifier (Loc,
9419 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9421 Max_Vals :=
9422 Make_Object_Declaration (Loc,
9423 Defining_Identifier => Maxes_Id,
9424 Aliased_Present => True,
9425 Constant_Present => True,
9426 Object_Definition =>
9427 Make_Subtype_Indication (Loc,
9428 Subtype_Mark =>
9429 New_Occurrence_Of
9430 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9431 Constraint =>
9432 Make_Index_Or_Discriminant_Constraint (Loc,
9433 Constraints => New_List (
9434 Make_Range (Loc,
9435 Make_Integer_Literal (Loc, 1),
9436 Make_Integer_Literal (Loc, Count))))),
9437 Expression => Make_Aggregate (Loc, Maxes));
9439 -- A pointer to this array will be placed in the corresponding
9440 -- record by its initialization procedure so this needs to be
9441 -- analyzed here.
9443 Insert_After (Current_Node, Max_Vals);
9444 Current_Node := Max_Vals;
9445 Analyze (Max_Vals);
9447 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9448 end if;
9449 end;
9450 end if;
9452 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9453 -- all protected subprograms have been collected.
9455 if Has_Entries (Prot_Typ) then
9456 Body_Id :=
9457 Make_Defining_Identifier (Sloc (Prot_Typ),
9458 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9460 case Corresponding_Runtime_Package (Prot_Typ) is
9461 when System_Tasking_Protected_Objects_Entries =>
9462 Expr := Entries_Aggr;
9463 Obj_Def :=
9464 Make_Subtype_Indication (Loc,
9465 Subtype_Mark =>
9466 New_Occurrence_Of
9467 (RTE (RE_Protected_Entry_Body_Array), Loc),
9468 Constraint =>
9469 Make_Index_Or_Discriminant_Constraint (Loc,
9470 Constraints => New_List (
9471 Make_Range (Loc,
9472 Make_Integer_Literal (Loc, 1),
9473 Make_Integer_Literal (Loc, E_Count)))));
9475 when System_Tasking_Protected_Objects_Single_Entry =>
9476 Expr := Remove_Head (Expressions (Entries_Aggr));
9477 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9479 when others =>
9480 raise Program_Error;
9481 end case;
9483 Body_Arr :=
9484 Make_Object_Declaration (Loc,
9485 Defining_Identifier => Body_Id,
9486 Aliased_Present => True,
9487 Constant_Present => True,
9488 Object_Definition => Obj_Def,
9489 Expression => Expr);
9491 -- A pointer to this array will be placed in the corresponding record
9492 -- by its initialization procedure so this needs to be analyzed here.
9494 Insert_After (Current_Node, Body_Arr);
9495 Current_Node := Body_Arr;
9496 Analyze (Body_Arr);
9498 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9500 -- Finally, build the function that maps an entry index into the
9501 -- corresponding body. A pointer to this function is placed in each
9502 -- object of the type. Except for a ravenscar-like profile (no abort,
9503 -- no entry queue, 1 entry)
9505 if Corresponding_Runtime_Package (Prot_Typ) =
9506 System_Tasking_Protected_Objects_Entries
9507 then
9508 Sub :=
9509 Make_Subprogram_Declaration (Loc,
9510 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9512 Insert_After (Current_Node, Sub);
9513 Analyze (Sub);
9514 end if;
9515 end if;
9516 end Expand_N_Protected_Type_Declaration;
9518 --------------------------------
9519 -- Expand_N_Requeue_Statement --
9520 --------------------------------
9522 -- A nondispatching requeue statement is expanded into one of four GNARLI
9523 -- operations, depending on the source and destination (task or protected
9524 -- object). A dispatching requeue statement is expanded into a call to the
9525 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9526 -- jump around the remainder of processing for the original entry and, if
9527 -- the destination is (different) protected object, to attempt to service
9528 -- it. The following illustrates the various cases:
9530 -- procedure entE
9531 -- (O : System.Address;
9532 -- P : System.Address;
9533 -- E : Protected_Entry_Index)
9534 -- is
9535 -- <discriminant renamings>
9536 -- <private object renamings>
9537 -- type poVP is access poV;
9538 -- _object : ptVP := ptVP!(O);
9540 -- begin
9541 -- begin
9542 -- <start of statement sequence for entry>
9544 -- -- Requeue from one protected entry body to another protected
9545 -- -- entry.
9547 -- Requeue_Protected_Entry (
9548 -- _object._object'Access,
9549 -- new._object'Access,
9550 -- E,
9551 -- Abort_Present);
9552 -- return;
9554 -- <some more of the statement sequence for entry>
9556 -- -- Requeue from an entry body to a task entry
9558 -- Requeue_Protected_To_Task_Entry (
9559 -- New._task_id,
9560 -- E,
9561 -- Abort_Present);
9562 -- return;
9564 -- <rest of statement sequence for entry>
9565 -- Complete_Entry_Body (_object._object);
9567 -- exception
9568 -- when all others =>
9569 -- Exceptional_Complete_Entry_Body (
9570 -- _object._object, Get_GNAT_Exception);
9571 -- end;
9572 -- end entE;
9574 -- Requeue of a task entry call to a task entry
9576 -- Accept_Call (E, Ann);
9577 -- <start of statement sequence for accept statement>
9578 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9579 -- goto Lnn;
9580 -- <rest of statement sequence for accept statement>
9581 -- <<Lnn>>
9582 -- Complete_Rendezvous;
9584 -- exception
9585 -- when all others =>
9586 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9588 -- Requeue of a task entry call to a protected entry
9590 -- Accept_Call (E, Ann);
9591 -- <start of statement sequence for accept statement>
9592 -- Requeue_Task_To_Protected_Entry (
9593 -- new._object'Access,
9594 -- E,
9595 -- Abort_Present);
9596 -- newS (new, Pnn);
9597 -- goto Lnn;
9598 -- <rest of statement sequence for accept statement>
9599 -- <<Lnn>>
9600 -- Complete_Rendezvous;
9602 -- exception
9603 -- when all others =>
9604 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9606 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9607 -- marked by pragma Implemented (XXX, By_Entry).
9609 -- The requeue is inside a protected entry:
9611 -- procedure entE
9612 -- (O : System.Address;
9613 -- P : System.Address;
9614 -- E : Protected_Entry_Index)
9615 -- is
9616 -- <discriminant renamings>
9617 -- <private object renamings>
9618 -- type poVP is access poV;
9619 -- _object : ptVP := ptVP!(O);
9621 -- begin
9622 -- begin
9623 -- <start of statement sequence for entry>
9625 -- _Disp_Requeue
9626 -- (<interface class-wide object>,
9627 -- True,
9628 -- _object'Address,
9629 -- Ada.Tags.Get_Offset_Index
9630 -- (Tag (_object),
9631 -- <interface dispatch table index of target entry>),
9632 -- Abort_Present);
9633 -- return;
9635 -- <rest of statement sequence for entry>
9636 -- Complete_Entry_Body (_object._object);
9638 -- exception
9639 -- when all others =>
9640 -- Exceptional_Complete_Entry_Body (
9641 -- _object._object, Get_GNAT_Exception);
9642 -- end;
9643 -- end entE;
9645 -- The requeue is inside a task entry:
9647 -- Accept_Call (E, Ann);
9648 -- <start of statement sequence for accept statement>
9649 -- _Disp_Requeue
9650 -- (<interface class-wide object>,
9651 -- False,
9652 -- null,
9653 -- Ada.Tags.Get_Offset_Index
9654 -- (Tag (_object),
9655 -- <interface dispatch table index of target entrt>),
9656 -- Abort_Present);
9657 -- newS (new, Pnn);
9658 -- goto Lnn;
9659 -- <rest of statement sequence for accept statement>
9660 -- <<Lnn>>
9661 -- Complete_Rendezvous;
9663 -- exception
9664 -- when all others =>
9665 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9667 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9668 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9669 -- statement is replaced by a dispatching call with actual parameters taken
9670 -- from the inner-most accept statement or entry body.
9672 -- Target.Primitive (Param1, ..., ParamN);
9674 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9675 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9676 -- at all.
9678 -- declare
9679 -- S : constant Offset_Index :=
9680 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9681 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9683 -- begin
9684 -- if C = POK_Protected_Entry
9685 -- or else C = POK_Task_Entry
9686 -- then
9687 -- <statements for dispatching requeue>
9689 -- elsif C = POK_Protected_Procedure then
9690 -- <dispatching call equivalent>
9692 -- else
9693 -- raise Program_Error;
9694 -- end if;
9695 -- end;
9697 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9698 Loc : constant Source_Ptr := Sloc (N);
9699 Conc_Typ : Entity_Id;
9700 Concval : Node_Id;
9701 Ename : Node_Id;
9702 Index : Node_Id;
9703 Old_Typ : Entity_Id;
9705 function Build_Dispatching_Call_Equivalent return Node_Id;
9706 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9707 -- the form Concval.Ename. It is statically known that Ename is allowed
9708 -- to be implemented by a protected procedure. Create a dispatching call
9709 -- equivalent of Concval.Ename taking the actual parameters from the
9710 -- inner-most accept statement or entry body.
9712 function Build_Dispatching_Requeue return Node_Id;
9713 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9714 -- the form Concval.Ename. It is statically known that Ename is allowed
9715 -- to be implemented by a protected or a task entry. Create a call to
9716 -- primitive _Disp_Requeue which handles the low-level actions.
9718 function Build_Dispatching_Requeue_To_Any return Node_Id;
9719 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9720 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9721 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9722 -- determines at runtime whether Ename denotes an entry or a procedure
9723 -- and perform the appropriate kind of dispatching select.
9725 function Build_Normal_Requeue return Node_Id;
9726 -- N denotes a nondispatching requeue statement to either a task or a
9727 -- protected entry. Build the appropriate runtime call to perform the
9728 -- action.
9730 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9731 -- For a protected entry, create a return statement to skip the rest of
9732 -- the entry body. Otherwise, create a goto statement to skip the rest
9733 -- of a task accept statement. The lookup for the enclosing entry body
9734 -- or accept statement starts from Search.
9736 ---------------------------------------
9737 -- Build_Dispatching_Call_Equivalent --
9738 ---------------------------------------
9740 function Build_Dispatching_Call_Equivalent return Node_Id is
9741 Call_Ent : constant Entity_Id := Entity (Ename);
9742 Obj : constant Node_Id := Original_Node (Concval);
9743 Acc_Ent : Node_Id;
9744 Actuals : List_Id;
9745 Formal : Node_Id;
9746 Formals : List_Id;
9748 begin
9749 -- Climb the parent chain looking for the inner-most entry body or
9750 -- accept statement.
9752 Acc_Ent := N;
9753 while Present (Acc_Ent)
9754 and then not Nkind_In (Acc_Ent, N_Accept_Statement,
9755 N_Entry_Body)
9756 loop
9757 Acc_Ent := Parent (Acc_Ent);
9758 end loop;
9760 -- A requeue statement should be housed inside an entry body or an
9761 -- accept statement at some level. If this is not the case, then the
9762 -- tree is malformed.
9764 pragma Assert (Present (Acc_Ent));
9766 -- Recover the list of formal parameters
9768 if Nkind (Acc_Ent) = N_Entry_Body then
9769 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9770 end if;
9772 Formals := Parameter_Specifications (Acc_Ent);
9774 -- Create the actual parameters for the dispatching call. These are
9775 -- simply copies of the entry body or accept statement formals in the
9776 -- same order as they appear.
9778 Actuals := No_List;
9780 if Present (Formals) then
9781 Actuals := New_List;
9782 Formal := First (Formals);
9783 while Present (Formal) loop
9784 Append_To (Actuals,
9785 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9786 Next (Formal);
9787 end loop;
9788 end if;
9790 -- Generate:
9791 -- Obj.Call_Ent (Actuals);
9793 return
9794 Make_Procedure_Call_Statement (Loc,
9795 Name =>
9796 Make_Selected_Component (Loc,
9797 Prefix => Make_Identifier (Loc, Chars (Obj)),
9798 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9800 Parameter_Associations => Actuals);
9801 end Build_Dispatching_Call_Equivalent;
9803 -------------------------------
9804 -- Build_Dispatching_Requeue --
9805 -------------------------------
9807 function Build_Dispatching_Requeue return Node_Id is
9808 Params : constant List_Id := New_List;
9810 begin
9811 -- Process the "with abort" parameter
9813 Prepend_To (Params,
9814 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
9816 -- Process the entry wrapper's position in the primary dispatch
9817 -- table parameter. Generate:
9819 -- Ada.Tags.Get_Entry_Index
9820 -- (T => To_Tag_Ptr (Obj'Address).all,
9821 -- Position =>
9822 -- Ada.Tags.Get_Offset_Index
9823 -- (Ada.Tags.Tag (Concval),
9824 -- <interface dispatch table position of Ename>));
9826 -- Note that Obj'Address is recursively expanded into a call to
9827 -- Base_Address (Obj).
9829 if Tagged_Type_Expansion then
9830 Prepend_To (Params,
9831 Make_Function_Call (Loc,
9832 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9833 Parameter_Associations => New_List (
9835 Make_Explicit_Dereference (Loc,
9836 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9837 Make_Attribute_Reference (Loc,
9838 Prefix => New_Copy_Tree (Concval),
9839 Attribute_Name => Name_Address))),
9841 Make_Function_Call (Loc,
9842 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9843 Parameter_Associations => New_List (
9844 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9845 Make_Integer_Literal (Loc,
9846 DT_Position (Entity (Ename))))))));
9848 -- VM targets
9850 else
9851 Prepend_To (Params,
9852 Make_Function_Call (Loc,
9853 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9854 Parameter_Associations => New_List (
9856 Make_Attribute_Reference (Loc,
9857 Prefix => Concval,
9858 Attribute_Name => Name_Tag),
9860 Make_Function_Call (Loc,
9861 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9863 Parameter_Associations => New_List (
9865 -- Obj_Tag
9867 Make_Attribute_Reference (Loc,
9868 Prefix => Concval,
9869 Attribute_Name => Name_Tag),
9871 -- Tag_Typ
9873 Make_Attribute_Reference (Loc,
9874 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
9875 Attribute_Name => Name_Tag),
9877 -- Position
9879 Make_Integer_Literal (Loc,
9880 DT_Position (Entity (Ename))))))));
9881 end if;
9883 -- Specific actuals for protected to XXX requeue
9885 if Is_Protected_Type (Old_Typ) then
9886 Prepend_To (Params,
9887 Make_Attribute_Reference (Loc, -- _object'Address
9888 Prefix =>
9889 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9890 Attribute_Name => Name_Address));
9892 Prepend_To (Params, -- True
9893 New_Occurrence_Of (Standard_True, Loc));
9895 -- Specific actuals for task to XXX requeue
9897 else
9898 pragma Assert (Is_Task_Type (Old_Typ));
9900 Prepend_To (Params, -- null
9901 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
9903 Prepend_To (Params, -- False
9904 New_Occurrence_Of (Standard_False, Loc));
9905 end if;
9907 -- Add the object parameter
9909 Prepend_To (Params, New_Copy_Tree (Concval));
9911 -- Generate:
9912 -- _Disp_Requeue (<Params>);
9914 -- Find entity for Disp_Requeue operation, which belongs to
9915 -- the type and may not be directly visible.
9917 declare
9918 Elmt : Elmt_Id;
9919 Op : Entity_Id;
9920 pragma Warnings (Off, Op);
9922 begin
9923 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
9924 while Present (Elmt) loop
9925 Op := Node (Elmt);
9926 exit when Chars (Op) = Name_uDisp_Requeue;
9927 Next_Elmt (Elmt);
9928 end loop;
9930 return
9931 Make_Procedure_Call_Statement (Loc,
9932 Name => New_Occurrence_Of (Op, Loc),
9933 Parameter_Associations => Params);
9934 end;
9935 end Build_Dispatching_Requeue;
9937 --------------------------------------
9938 -- Build_Dispatching_Requeue_To_Any --
9939 --------------------------------------
9941 function Build_Dispatching_Requeue_To_Any return Node_Id is
9942 Call_Ent : constant Entity_Id := Entity (Ename);
9943 Obj : constant Node_Id := Original_Node (Concval);
9944 Skip : constant Node_Id := Build_Skip_Statement (N);
9945 C : Entity_Id;
9946 Decls : List_Id;
9947 S : Entity_Id;
9948 Stmts : List_Id;
9950 begin
9951 Decls := New_List;
9952 Stmts := New_List;
9954 -- Dispatch table slot processing, generate:
9955 -- S : Integer;
9957 S := Build_S (Loc, Decls);
9959 -- Call kind processing, generate:
9960 -- C : Ada.Tags.Prim_Op_Kind;
9962 C := Build_C (Loc, Decls);
9964 -- Generate:
9965 -- S := Ada.Tags.Get_Offset_Index
9966 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
9968 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
9970 -- Generate:
9971 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
9973 Append_To (Stmts,
9974 Make_Procedure_Call_Statement (Loc,
9975 Name =>
9976 New_Occurrence_Of (
9977 Find_Prim_Op (Etype (Etype (Obj)),
9978 Name_uDisp_Get_Prim_Op_Kind),
9979 Loc),
9980 Parameter_Associations => New_List (
9981 New_Copy_Tree (Obj),
9982 New_Occurrence_Of (S, Loc),
9983 New_Occurrence_Of (C, Loc))));
9985 Append_To (Stmts,
9987 -- if C = POK_Protected_Entry
9988 -- or else C = POK_Task_Entry
9989 -- then
9991 Make_Implicit_If_Statement (N,
9992 Condition =>
9993 Make_Op_Or (Loc,
9994 Left_Opnd =>
9995 Make_Op_Eq (Loc,
9996 Left_Opnd =>
9997 New_Occurrence_Of (C, Loc),
9998 Right_Opnd =>
9999 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10001 Right_Opnd =>
10002 Make_Op_Eq (Loc,
10003 Left_Opnd =>
10004 New_Occurrence_Of (C, Loc),
10005 Right_Opnd =>
10006 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10008 -- Dispatching requeue equivalent
10010 Then_Statements => New_List (
10011 Build_Dispatching_Requeue,
10012 Skip),
10014 -- elsif C = POK_Protected_Procedure then
10016 Elsif_Parts => New_List (
10017 Make_Elsif_Part (Loc,
10018 Condition =>
10019 Make_Op_Eq (Loc,
10020 Left_Opnd =>
10021 New_Occurrence_Of (C, Loc),
10022 Right_Opnd =>
10023 New_Occurrence_Of (
10024 RTE (RE_POK_Protected_Procedure), Loc)),
10026 -- Dispatching call equivalent
10028 Then_Statements => New_List (
10029 Build_Dispatching_Call_Equivalent))),
10031 -- else
10032 -- raise Program_Error;
10033 -- end if;
10035 Else_Statements => New_List (
10036 Make_Raise_Program_Error (Loc,
10037 Reason => PE_Explicit_Raise))));
10039 -- Wrap everything into a block
10041 return
10042 Make_Block_Statement (Loc,
10043 Declarations => Decls,
10044 Handled_Statement_Sequence =>
10045 Make_Handled_Sequence_Of_Statements (Loc,
10046 Statements => Stmts));
10047 end Build_Dispatching_Requeue_To_Any;
10049 --------------------------
10050 -- Build_Normal_Requeue --
10051 --------------------------
10053 function Build_Normal_Requeue return Node_Id is
10054 Params : constant List_Id := New_List;
10055 Param : Node_Id;
10056 RT_Call : Node_Id;
10058 begin
10059 -- Process the "with abort" parameter
10061 Prepend_To (Params,
10062 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10064 -- Add the index expression to the parameters. It is common among all
10065 -- four cases.
10067 Prepend_To (Params,
10068 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10070 if Is_Protected_Type (Old_Typ) then
10071 declare
10072 Self_Param : Node_Id;
10074 begin
10075 Self_Param :=
10076 Make_Attribute_Reference (Loc,
10077 Prefix =>
10078 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10079 Attribute_Name =>
10080 Name_Unchecked_Access);
10082 -- Protected to protected requeue
10084 if Is_Protected_Type (Conc_Typ) then
10085 RT_Call :=
10086 New_Occurrence_Of (
10087 RTE (RE_Requeue_Protected_Entry), Loc);
10089 Param :=
10090 Make_Attribute_Reference (Loc,
10091 Prefix =>
10092 Concurrent_Ref (Concval),
10093 Attribute_Name =>
10094 Name_Unchecked_Access);
10096 -- Protected to task requeue
10098 else pragma Assert (Is_Task_Type (Conc_Typ));
10099 RT_Call :=
10100 New_Occurrence_Of (
10101 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10103 Param := Concurrent_Ref (Concval);
10104 end if;
10106 Prepend_To (Params, Param);
10107 Prepend_To (Params, Self_Param);
10108 end;
10110 else pragma Assert (Is_Task_Type (Old_Typ));
10112 -- Task to protected requeue
10114 if Is_Protected_Type (Conc_Typ) then
10115 RT_Call :=
10116 New_Occurrence_Of (
10117 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10119 Param :=
10120 Make_Attribute_Reference (Loc,
10121 Prefix =>
10122 Concurrent_Ref (Concval),
10123 Attribute_Name =>
10124 Name_Unchecked_Access);
10126 -- Task to task requeue
10128 else pragma Assert (Is_Task_Type (Conc_Typ));
10129 RT_Call :=
10130 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10132 Param := Concurrent_Ref (Concval);
10133 end if;
10135 Prepend_To (Params, Param);
10136 end if;
10138 return
10139 Make_Procedure_Call_Statement (Loc,
10140 Name => RT_Call,
10141 Parameter_Associations => Params);
10142 end Build_Normal_Requeue;
10144 --------------------------
10145 -- Build_Skip_Statement --
10146 --------------------------
10148 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10149 Skip_Stmt : Node_Id;
10151 begin
10152 -- Build a return statement to skip the rest of the entire body
10154 if Is_Protected_Type (Old_Typ) then
10155 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10157 -- If the requeue is within a task, find the end label of the
10158 -- enclosing accept statement and create a goto statement to it.
10160 else
10161 declare
10162 Acc : Node_Id;
10163 Label : Node_Id;
10165 begin
10166 -- Climb the parent chain looking for the enclosing accept
10167 -- statement.
10169 Acc := Parent (Search);
10170 while Present (Acc)
10171 and then Nkind (Acc) /= N_Accept_Statement
10172 loop
10173 Acc := Parent (Acc);
10174 end loop;
10176 -- The last statement is the second label used for completing
10177 -- the rendezvous the usual way. The label we are looking for
10178 -- is right before it.
10180 Label :=
10181 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10183 pragma Assert (Nkind (Label) = N_Label);
10185 -- Generate a goto statement to skip the rest of the accept
10187 Skip_Stmt :=
10188 Make_Goto_Statement (Loc,
10189 Name =>
10190 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10191 end;
10192 end if;
10194 Set_Analyzed (Skip_Stmt);
10196 return Skip_Stmt;
10197 end Build_Skip_Statement;
10199 -- Start of processing for Expand_N_Requeue_Statement
10201 begin
10202 -- Extract the components of the entry call
10204 Extract_Entry (N, Concval, Ename, Index);
10205 Conc_Typ := Etype (Concval);
10207 -- If the prefix is an access to class-wide type, dereference to get
10208 -- object and entry type.
10210 if Is_Access_Type (Conc_Typ) then
10211 Conc_Typ := Designated_Type (Conc_Typ);
10212 Rewrite (Concval,
10213 Make_Explicit_Dereference (Loc, Relocate_Node (Concval)));
10214 Analyze_And_Resolve (Concval, Conc_Typ);
10215 end if;
10217 -- Examine the scope stack in order to find nearest enclosing protected
10218 -- or task type. This will constitute our invocation source.
10220 Old_Typ := Current_Scope;
10221 while Present (Old_Typ)
10222 and then not Is_Protected_Type (Old_Typ)
10223 and then not Is_Task_Type (Old_Typ)
10224 loop
10225 Old_Typ := Scope (Old_Typ);
10226 end loop;
10228 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10229 -- Concval.Ename where the type of Concval is class-wide concurrent
10230 -- interface.
10232 if Ada_Version >= Ada_2012
10233 and then Present (Concval)
10234 and then Is_Class_Wide_Type (Conc_Typ)
10235 and then Is_Concurrent_Interface (Conc_Typ)
10236 then
10237 declare
10238 Has_Impl : Boolean := False;
10239 Impl_Kind : Name_Id := No_Name;
10241 begin
10242 -- Check whether the Ename is flagged by pragma Implemented
10244 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10245 Has_Impl := True;
10246 Impl_Kind := Implementation_Kind (Entity (Ename));
10247 end if;
10249 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10250 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10252 if Has_Impl and then Impl_Kind = Name_By_Entry then
10253 Rewrite (N, Build_Dispatching_Requeue);
10254 Analyze (N);
10255 Insert_After (N, Build_Skip_Statement (N));
10257 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10258 -- a protected procedure. In this case the requeue is transformed
10259 -- into a dispatching call.
10261 elsif Has_Impl
10262 and then Impl_Kind = Name_By_Protected_Procedure
10263 then
10264 Rewrite (N, Build_Dispatching_Call_Equivalent);
10265 Analyze (N);
10267 -- The procedure_or_entry_NAME's implementation kind is either
10268 -- By_Any, Optional, or pragma Implemented was not applied at all.
10269 -- In this case a runtime test determines whether Ename denotes an
10270 -- entry or a protected procedure and performs the appropriate
10271 -- call.
10273 else
10274 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10275 Analyze (N);
10276 end if;
10277 end;
10279 -- Processing for regular (nondispatching) requeues
10281 else
10282 Rewrite (N, Build_Normal_Requeue);
10283 Analyze (N);
10284 Insert_After (N, Build_Skip_Statement (N));
10285 end if;
10286 end Expand_N_Requeue_Statement;
10288 -------------------------------
10289 -- Expand_N_Selective_Accept --
10290 -------------------------------
10292 procedure Expand_N_Selective_Accept (N : Node_Id) is
10293 Loc : constant Source_Ptr := Sloc (N);
10294 Alts : constant List_Id := Select_Alternatives (N);
10296 -- Note: in the below declarations a lot of new lists are allocated
10297 -- unconditionally which may well not end up being used. That's not
10298 -- a good idea since it wastes space gratuitously ???
10300 Accept_Case : List_Id;
10301 Accept_List : constant List_Id := New_List;
10303 Alt : Node_Id;
10304 Alt_List : constant List_Id := New_List;
10305 Alt_Stats : List_Id;
10306 Ann : Entity_Id := Empty;
10308 Check_Guard : Boolean := True;
10310 Decls : constant List_Id := New_List;
10311 Stats : constant List_Id := New_List;
10312 Body_List : constant List_Id := New_List;
10313 Trailing_List : constant List_Id := New_List;
10315 Choices : List_Id;
10316 Else_Present : Boolean := False;
10317 Terminate_Alt : Node_Id := Empty;
10318 Select_Mode : Node_Id;
10320 Delay_Case : List_Id;
10321 Delay_Count : Integer := 0;
10322 Delay_Val : Entity_Id;
10323 Delay_Index : Entity_Id;
10324 Delay_Min : Entity_Id;
10325 Delay_Num : Pos := 1;
10326 Delay_Alt_List : List_Id := New_List;
10327 Delay_List : constant List_Id := New_List;
10328 D : Entity_Id;
10329 M : Entity_Id;
10331 First_Delay : Boolean := True;
10332 Guard_Open : Entity_Id;
10334 End_Lab : Node_Id;
10335 Index : Pos := 1;
10336 Lab : Node_Id;
10337 Num_Alts : Nat;
10338 Num_Accept : Nat := 0;
10339 Proc : Node_Id;
10340 Time_Type : Entity_Id;
10341 Select_Call : Node_Id;
10343 Qnam : constant Entity_Id :=
10344 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10346 Xnam : constant Entity_Id :=
10347 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10349 -----------------------
10350 -- Local subprograms --
10351 -----------------------
10353 function Accept_Or_Raise return List_Id;
10354 -- For the rare case where delay alternatives all have guards, and
10355 -- all of them are closed, it is still possible that there were open
10356 -- accept alternatives with no callers. We must reexamine the
10357 -- Accept_List, and execute a selective wait with no else if some
10358 -- accept is open. If none, we raise program_error.
10360 procedure Add_Accept (Alt : Node_Id);
10361 -- Process a single accept statement in a select alternative. Build
10362 -- procedure for body of accept, and add entry to dispatch table with
10363 -- expression for guard, in preparation for call to run time select.
10365 function Make_And_Declare_Label (Num : Int) return Node_Id;
10366 -- Manufacture a label using Num as a serial number and declare it.
10367 -- The declaration is appended to Decls. The label marks the trailing
10368 -- statements of an accept or delay alternative.
10370 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10371 -- Build call to Selective_Wait runtime routine
10373 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10374 -- Add code to compare value of delay with previous values, and
10375 -- generate case entry for trailing statements.
10377 procedure Process_Accept_Alternative
10378 (Alt : Node_Id;
10379 Index : Int;
10380 Proc : Node_Id);
10381 -- Add code to call corresponding procedure, and branch to
10382 -- trailing statements, if any.
10384 ---------------------
10385 -- Accept_Or_Raise --
10386 ---------------------
10388 function Accept_Or_Raise return List_Id is
10389 Cond : Node_Id;
10390 Stats : List_Id;
10391 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10393 begin
10394 -- We generate the following:
10396 -- for J in q'range loop
10397 -- if q(J).S /=null_task_entry then
10398 -- selective_wait (simple_mode,...);
10399 -- done := True;
10400 -- exit;
10401 -- end if;
10402 -- end loop;
10404 -- if no rendez_vous then
10405 -- raise program_error;
10406 -- end if;
10408 -- Note that the code needs to know that the selector name
10409 -- in an Accept_Alternative is named S.
10411 Cond := Make_Op_Ne (Loc,
10412 Left_Opnd =>
10413 Make_Selected_Component (Loc,
10414 Prefix =>
10415 Make_Indexed_Component (Loc,
10416 Prefix => New_Occurrence_Of (Qnam, Loc),
10417 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10418 Selector_Name => Make_Identifier (Loc, Name_S)),
10419 Right_Opnd =>
10420 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10422 Stats := New_List (
10423 Make_Implicit_Loop_Statement (N,
10424 Iteration_Scheme =>
10425 Make_Iteration_Scheme (Loc,
10426 Loop_Parameter_Specification =>
10427 Make_Loop_Parameter_Specification (Loc,
10428 Defining_Identifier => J,
10429 Discrete_Subtype_Definition =>
10430 Make_Attribute_Reference (Loc,
10431 Prefix => New_Occurrence_Of (Qnam, Loc),
10432 Attribute_Name => Name_Range,
10433 Expressions => New_List (
10434 Make_Integer_Literal (Loc, 1))))),
10436 Statements => New_List (
10437 Make_Implicit_If_Statement (N,
10438 Condition => Cond,
10439 Then_Statements => New_List (
10440 Make_Select_Call (
10441 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10442 Make_Exit_Statement (Loc))))));
10444 Append_To (Stats,
10445 Make_Raise_Program_Error (Loc,
10446 Condition => Make_Op_Eq (Loc,
10447 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10448 Right_Opnd =>
10449 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10450 Reason => PE_All_Guards_Closed));
10452 return Stats;
10453 end Accept_Or_Raise;
10455 ----------------
10456 -- Add_Accept --
10457 ----------------
10459 procedure Add_Accept (Alt : Node_Id) is
10460 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10461 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10462 Eloc : constant Source_Ptr := Sloc (Ename);
10463 Eent : constant Entity_Id := Entity (Ename);
10464 Index : constant Node_Id := Entry_Index (Acc_Stm);
10465 Null_Body : Node_Id;
10466 Proc_Body : Node_Id;
10467 PB_Ent : Entity_Id;
10468 Expr : Node_Id;
10469 Call : Node_Id;
10471 begin
10472 if No (Ann) then
10473 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10474 end if;
10476 if Present (Condition (Alt)) then
10477 Expr :=
10478 Make_If_Expression (Eloc, New_List (
10479 Condition (Alt),
10480 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10481 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10482 else
10483 Expr :=
10484 Entry_Index_Expression
10485 (Eloc, Eent, Index, Scope (Eent));
10486 end if;
10488 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10489 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10491 -- Always add call to Abort_Undefer when generating code, since
10492 -- this is what the runtime expects (abort deferred in
10493 -- Selective_Wait). In CodePeer mode this only confuses the
10494 -- analysis with unknown calls, so don't do it.
10496 if not CodePeer_Mode then
10497 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10498 Insert_Before
10499 (First (Statements (Handled_Statement_Sequence
10500 (Accept_Statement (Alt)))),
10501 Call);
10502 Analyze (Call);
10503 end if;
10505 PB_Ent :=
10506 Make_Defining_Identifier (Eloc,
10507 New_External_Name (Chars (Ename), 'A', Num_Accept));
10509 if Comes_From_Source (Alt) then
10510 Set_Debug_Info_Needed (PB_Ent);
10511 end if;
10513 Proc_Body :=
10514 Make_Subprogram_Body (Eloc,
10515 Specification =>
10516 Make_Procedure_Specification (Eloc,
10517 Defining_Unit_Name => PB_Ent),
10518 Declarations => Declarations (Acc_Stm),
10519 Handled_Statement_Sequence =>
10520 Build_Accept_Body (Accept_Statement (Alt)));
10522 -- During the analysis of the body of the accept statement, any
10523 -- zero cost exception handler records were collected in the
10524 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10525 -- This is where we move them to where they belong, namely the
10526 -- newly created procedure.
10528 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10529 Append (Proc_Body, Body_List);
10531 else
10532 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10534 -- if accept statement has declarations, insert above, given that
10535 -- we are not creating a body for the accept.
10537 if Present (Declarations (Acc_Stm)) then
10538 Insert_Actions (N, Declarations (Acc_Stm));
10539 end if;
10540 end if;
10542 Append_To (Accept_List,
10543 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10545 Num_Accept := Num_Accept + 1;
10546 end Add_Accept;
10548 ----------------------------
10549 -- Make_And_Declare_Label --
10550 ----------------------------
10552 function Make_And_Declare_Label (Num : Int) return Node_Id is
10553 Lab_Id : Node_Id;
10555 begin
10556 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10557 Lab :=
10558 Make_Label (Loc, Lab_Id);
10560 Append_To (Decls,
10561 Make_Implicit_Label_Declaration (Loc,
10562 Defining_Identifier =>
10563 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10564 Label_Construct => Lab));
10566 return Lab;
10567 end Make_And_Declare_Label;
10569 ----------------------
10570 -- Make_Select_Call --
10571 ----------------------
10573 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10574 Params : constant List_Id := New_List;
10576 begin
10577 Append_To (Params,
10578 Make_Attribute_Reference (Loc,
10579 Prefix => New_Occurrence_Of (Qnam, Loc),
10580 Attribute_Name => Name_Unchecked_Access));
10581 Append_To (Params, Select_Mode);
10582 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10583 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10585 return
10586 Make_Procedure_Call_Statement (Loc,
10587 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10588 Parameter_Associations => Params);
10589 end Make_Select_Call;
10591 --------------------------------
10592 -- Process_Accept_Alternative --
10593 --------------------------------
10595 procedure Process_Accept_Alternative
10596 (Alt : Node_Id;
10597 Index : Int;
10598 Proc : Node_Id)
10600 Astmt : constant Node_Id := Accept_Statement (Alt);
10601 Alt_Stats : List_Id;
10603 begin
10604 Adjust_Condition (Condition (Alt));
10606 -- Accept with body
10608 if Present (Handled_Statement_Sequence (Astmt)) then
10609 Alt_Stats :=
10610 New_List (
10611 Make_Procedure_Call_Statement (Sloc (Proc),
10612 Name =>
10613 New_Occurrence_Of
10614 (Defining_Unit_Name (Specification (Proc)),
10615 Sloc (Proc))));
10617 -- Accept with no body (followed by trailing statements)
10619 else
10620 Alt_Stats := Empty_List;
10621 end if;
10623 Ensure_Statement_Present (Sloc (Astmt), Alt);
10625 -- After the call, if any, branch to trailing statements, if any.
10626 -- We create a label for each, as well as the corresponding label
10627 -- declaration.
10629 if not Is_Empty_List (Statements (Alt)) then
10630 Lab := Make_And_Declare_Label (Index);
10631 Append (Lab, Trailing_List);
10632 Append_List (Statements (Alt), Trailing_List);
10633 Append_To (Trailing_List,
10634 Make_Goto_Statement (Loc,
10635 Name => New_Copy (Identifier (End_Lab))));
10637 else
10638 Lab := End_Lab;
10639 end if;
10641 Append_To (Alt_Stats,
10642 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10644 Append_To (Alt_List,
10645 Make_Case_Statement_Alternative (Loc,
10646 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10647 Statements => Alt_Stats));
10648 end Process_Accept_Alternative;
10650 -------------------------------
10651 -- Process_Delay_Alternative --
10652 -------------------------------
10654 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10655 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10656 Cond : Node_Id;
10657 Delay_Alt : List_Id;
10659 begin
10660 -- Deal with C/Fortran boolean as delay condition
10662 Adjust_Condition (Condition (Alt));
10664 -- Determine the smallest specified delay
10666 -- for each delay alternative generate:
10668 -- if guard-expression then
10669 -- Delay_Val := delay-expression;
10670 -- Guard_Open := True;
10671 -- if Delay_Val < Delay_Min then
10672 -- Delay_Min := Delay_Val;
10673 -- Delay_Index := Index;
10674 -- end if;
10675 -- end if;
10677 -- The enclosing if-statement is omitted if there is no guard
10679 if Delay_Count = 1 or else First_Delay then
10680 First_Delay := False;
10682 Delay_Alt := New_List (
10683 Make_Assignment_Statement (Loc,
10684 Name => New_Occurrence_Of (Delay_Min, Loc),
10685 Expression => Expression (Delay_Statement (Alt))));
10687 if Delay_Count > 1 then
10688 Append_To (Delay_Alt,
10689 Make_Assignment_Statement (Loc,
10690 Name => New_Occurrence_Of (Delay_Index, Loc),
10691 Expression => Make_Integer_Literal (Loc, Index)));
10692 end if;
10694 else
10695 Delay_Alt := New_List (
10696 Make_Assignment_Statement (Loc,
10697 Name => New_Occurrence_Of (Delay_Val, Loc),
10698 Expression => Expression (Delay_Statement (Alt))));
10700 if Time_Type = Standard_Duration then
10701 Cond :=
10702 Make_Op_Lt (Loc,
10703 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
10704 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
10706 else
10707 -- The scope of the time type must define a comparison
10708 -- operator. The scope itself may not be visible, so we
10709 -- construct a node with entity information to insure that
10710 -- semantic analysis can find the proper operator.
10712 Cond :=
10713 Make_Function_Call (Loc,
10714 Name => Make_Selected_Component (Loc,
10715 Prefix =>
10716 New_Occurrence_Of (Scope (Time_Type), Loc),
10717 Selector_Name =>
10718 Make_Operator_Symbol (Loc,
10719 Chars => Name_Op_Lt,
10720 Strval => No_String)),
10721 Parameter_Associations =>
10722 New_List (
10723 New_Occurrence_Of (Delay_Val, Loc),
10724 New_Occurrence_Of (Delay_Min, Loc)));
10726 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10727 end if;
10729 Append_To (Delay_Alt,
10730 Make_Implicit_If_Statement (N,
10731 Condition => Cond,
10732 Then_Statements => New_List (
10733 Make_Assignment_Statement (Loc,
10734 Name => New_Occurrence_Of (Delay_Min, Loc),
10735 Expression => New_Occurrence_Of (Delay_Val, Loc)),
10737 Make_Assignment_Statement (Loc,
10738 Name => New_Occurrence_Of (Delay_Index, Loc),
10739 Expression => Make_Integer_Literal (Loc, Index)))));
10740 end if;
10742 if Check_Guard then
10743 Append_To (Delay_Alt,
10744 Make_Assignment_Statement (Loc,
10745 Name => New_Occurrence_Of (Guard_Open, Loc),
10746 Expression => New_Occurrence_Of (Standard_True, Loc)));
10747 end if;
10749 if Present (Condition (Alt)) then
10750 Delay_Alt := New_List (
10751 Make_Implicit_If_Statement (N,
10752 Condition => Condition (Alt),
10753 Then_Statements => Delay_Alt));
10754 end if;
10756 Append_List (Delay_Alt, Delay_List);
10758 Ensure_Statement_Present (Dloc, Alt);
10760 -- If the delay alternative has a statement part, add choice to the
10761 -- case statements for delays.
10763 if not Is_Empty_List (Statements (Alt)) then
10765 if Delay_Count = 1 then
10766 Append_List (Statements (Alt), Delay_Alt_List);
10768 else
10769 Append_To (Delay_Alt_List,
10770 Make_Case_Statement_Alternative (Loc,
10771 Discrete_Choices => New_List (
10772 Make_Integer_Literal (Loc, Index)),
10773 Statements => Statements (Alt)));
10774 end if;
10776 elsif Delay_Count = 1 then
10778 -- If the single delay has no trailing statements, add a branch
10779 -- to the exit label to the selective wait.
10781 Delay_Alt_List := New_List (
10782 Make_Goto_Statement (Loc,
10783 Name => New_Copy (Identifier (End_Lab))));
10785 end if;
10786 end Process_Delay_Alternative;
10788 -- Start of processing for Expand_N_Selective_Accept
10790 begin
10791 Process_Statements_For_Controlled_Objects (N);
10793 -- First insert some declarations before the select. The first is:
10795 -- Ann : Address
10797 -- This variable holds the parameters passed to the accept body. This
10798 -- declaration has already been inserted by the time we get here by
10799 -- a call to Expand_Accept_Declarations made from the semantics when
10800 -- processing the first accept statement contained in the select. We
10801 -- can find this entity as Accept_Address (E), where E is any of the
10802 -- entries references by contained accept statements.
10804 -- The first step is to scan the list of Selective_Accept_Statements
10805 -- to find this entity, and also count the number of accepts, and
10806 -- determine if terminated, delay or else is present:
10808 Num_Alts := 0;
10810 Alt := First (Alts);
10811 while Present (Alt) loop
10812 Process_Statements_For_Controlled_Objects (Alt);
10814 if Nkind (Alt) = N_Accept_Alternative then
10815 Add_Accept (Alt);
10817 elsif Nkind (Alt) = N_Delay_Alternative then
10818 Delay_Count := Delay_Count + 1;
10820 -- If the delays are relative delays, the delay expressions have
10821 -- type Standard_Duration. Otherwise they must have some time type
10822 -- recognized by GNAT.
10824 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10825 Time_Type := Standard_Duration;
10826 else
10827 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10829 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10830 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10831 then
10832 null;
10833 else
10834 Error_Msg_NE (
10835 "& is not a time type (RM 9.6(6))",
10836 Expression (Delay_Statement (Alt)), Time_Type);
10837 Time_Type := Standard_Duration;
10838 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10839 end if;
10840 end if;
10842 if No (Condition (Alt)) then
10844 -- This guard will always be open
10846 Check_Guard := False;
10847 end if;
10849 elsif Nkind (Alt) = N_Terminate_Alternative then
10850 Adjust_Condition (Condition (Alt));
10851 Terminate_Alt := Alt;
10852 end if;
10854 Num_Alts := Num_Alts + 1;
10855 Next (Alt);
10856 end loop;
10858 Else_Present := Present (Else_Statements (N));
10860 -- At the same time (see procedure Add_Accept) we build the accept list:
10862 -- Qnn : Accept_List (1 .. num-select) := (
10863 -- (null-body, entry-index),
10864 -- (null-body, entry-index),
10865 -- ..
10866 -- (null_body, entry-index));
10868 -- In the above declaration, null-body is True if the corresponding
10869 -- accept has no body, and false otherwise. The entry is either the
10870 -- entry index expression if there is no guard, or if a guard is
10871 -- present, then an if expression of the form:
10873 -- (if guard then entry-index else Null_Task_Entry)
10875 -- If a guard is statically known to be false, the entry can simply
10876 -- be omitted from the accept list.
10878 Append_To (Decls,
10879 Make_Object_Declaration (Loc,
10880 Defining_Identifier => Qnam,
10881 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10882 Aliased_Present => True,
10883 Expression =>
10884 Make_Qualified_Expression (Loc,
10885 Subtype_Mark =>
10886 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10887 Expression =>
10888 Make_Aggregate (Loc, Expressions => Accept_List))));
10890 -- Then we declare the variable that holds the index for the accept
10891 -- that will be selected for service:
10893 -- Xnn : Select_Index;
10895 Append_To (Decls,
10896 Make_Object_Declaration (Loc,
10897 Defining_Identifier => Xnam,
10898 Object_Definition =>
10899 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
10900 Expression =>
10901 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
10903 -- After this follow procedure declarations for each accept body
10905 -- procedure Pnn is
10906 -- begin
10907 -- ...
10908 -- end;
10910 -- where the ... are statements from the corresponding procedure body.
10911 -- No parameters are involved, since the parameters are passed via Ann
10912 -- and the parameter references have already been expanded to be direct
10913 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10914 -- any embedded tasking statements (which would normally be illegal in
10915 -- procedures), have been converted to calls to the tasking runtime so
10916 -- there is no problem in putting them into procedures.
10918 -- The original accept statement has been expanded into a block in
10919 -- the same fashion as for simple accepts (see Build_Accept_Body).
10921 -- Note: we don't really need to build these procedures for the case
10922 -- where no delay statement is present, but it is just as easy to
10923 -- build them unconditionally, and not significantly inefficient,
10924 -- since if they are short they will be inlined anyway.
10926 -- The procedure declarations have been assembled in Body_List
10928 -- If delays are present, we must compute the required delay.
10929 -- We first generate the declarations:
10931 -- Delay_Index : Boolean := 0;
10932 -- Delay_Min : Some_Time_Type.Time;
10933 -- Delay_Val : Some_Time_Type.Time;
10935 -- Delay_Index will be set to the index of the minimum delay, i.e. the
10936 -- active delay that is actually chosen as the basis for the possible
10937 -- delay if an immediate rendez-vous is not possible.
10939 -- In the most common case there is a single delay statement, and this
10940 -- is handled specially.
10942 if Delay_Count > 0 then
10944 -- Generate the required declarations
10946 Delay_Val :=
10947 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
10948 Delay_Index :=
10949 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
10950 Delay_Min :=
10951 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
10953 Append_To (Decls,
10954 Make_Object_Declaration (Loc,
10955 Defining_Identifier => Delay_Val,
10956 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
10958 Append_To (Decls,
10959 Make_Object_Declaration (Loc,
10960 Defining_Identifier => Delay_Index,
10961 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
10962 Expression => Make_Integer_Literal (Loc, 0)));
10964 Append_To (Decls,
10965 Make_Object_Declaration (Loc,
10966 Defining_Identifier => Delay_Min,
10967 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
10968 Expression =>
10969 Unchecked_Convert_To (Time_Type,
10970 Make_Attribute_Reference (Loc,
10971 Prefix =>
10972 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
10973 Attribute_Name => Name_Last))));
10975 -- Create Duration and Delay_Mode objects used for passing a delay
10976 -- value to RTS
10978 D := Make_Temporary (Loc, 'D');
10979 M := Make_Temporary (Loc, 'M');
10981 declare
10982 Discr : Entity_Id;
10984 begin
10985 -- Note that these values are defined in s-osprim.ads and must
10986 -- be kept in sync:
10988 -- Relative : constant := 0;
10989 -- Absolute_Calendar : constant := 1;
10990 -- Absolute_RT : constant := 2;
10992 if Time_Type = Standard_Duration then
10993 Discr := Make_Integer_Literal (Loc, 0);
10995 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
10996 Discr := Make_Integer_Literal (Loc, 1);
10998 else
10999 pragma Assert
11000 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11001 Discr := Make_Integer_Literal (Loc, 2);
11002 end if;
11004 Append_To (Decls,
11005 Make_Object_Declaration (Loc,
11006 Defining_Identifier => D,
11007 Object_Definition =>
11008 New_Occurrence_Of (Standard_Duration, Loc)));
11010 Append_To (Decls,
11011 Make_Object_Declaration (Loc,
11012 Defining_Identifier => M,
11013 Object_Definition =>
11014 New_Occurrence_Of (Standard_Integer, Loc),
11015 Expression => Discr));
11016 end;
11018 if Check_Guard then
11019 Guard_Open :=
11020 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11022 Append_To (Decls,
11023 Make_Object_Declaration (Loc,
11024 Defining_Identifier => Guard_Open,
11025 Object_Definition =>
11026 New_Occurrence_Of (Standard_Boolean, Loc),
11027 Expression =>
11028 New_Occurrence_Of (Standard_False, Loc)));
11029 end if;
11031 -- Delay_Count is zero, don't need M and D set (suppress warning)
11033 else
11034 M := Empty;
11035 D := Empty;
11036 end if;
11038 if Present (Terminate_Alt) then
11040 -- If the terminate alternative guard is False, use
11041 -- Simple_Mode; otherwise use Terminate_Mode.
11043 if Present (Condition (Terminate_Alt)) then
11044 Select_Mode := Make_If_Expression (Loc,
11045 New_List (Condition (Terminate_Alt),
11046 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11047 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11048 else
11049 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11050 end if;
11052 elsif Else_Present or Delay_Count > 0 then
11053 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11055 else
11056 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11057 end if;
11059 Select_Call := Make_Select_Call (Select_Mode);
11060 Append (Select_Call, Stats);
11062 -- Now generate code to act on the result. There is an entry
11063 -- in this case for each accept statement with a non-null body,
11064 -- followed by a branch to the statements that follow the Accept.
11065 -- In the absence of delay alternatives, we generate:
11067 -- case X is
11068 -- when No_Rendezvous => -- omitted if simple mode
11069 -- goto Lab0;
11071 -- when 1 =>
11072 -- P1n;
11073 -- goto Lab1;
11075 -- when 2 =>
11076 -- P2n;
11077 -- goto Lab2;
11079 -- when others =>
11080 -- goto Exit;
11081 -- end case;
11083 -- Lab0: Else_Statements;
11084 -- goto exit;
11086 -- Lab1: Trailing_Statements1;
11087 -- goto Exit;
11089 -- Lab2: Trailing_Statements2;
11090 -- goto Exit;
11091 -- ...
11092 -- Exit:
11094 -- Generate label for common exit
11096 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11098 -- First entry is the default case, when no rendezvous is possible
11100 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11102 if Else_Present then
11104 -- If no rendezvous is possible, the else part is executed
11106 Lab := Make_And_Declare_Label (0);
11107 Alt_Stats := New_List (
11108 Make_Goto_Statement (Loc,
11109 Name => New_Copy (Identifier (Lab))));
11111 Append (Lab, Trailing_List);
11112 Append_List (Else_Statements (N), Trailing_List);
11113 Append_To (Trailing_List,
11114 Make_Goto_Statement (Loc,
11115 Name => New_Copy (Identifier (End_Lab))));
11116 else
11117 Alt_Stats := New_List (
11118 Make_Goto_Statement (Loc,
11119 Name => New_Copy (Identifier (End_Lab))));
11120 end if;
11122 Append_To (Alt_List,
11123 Make_Case_Statement_Alternative (Loc,
11124 Discrete_Choices => Choices,
11125 Statements => Alt_Stats));
11127 -- We make use of the fact that Accept_Index is an integer type, and
11128 -- generate successive literals for entries for each accept. Only those
11129 -- for which there is a body or trailing statements get a case entry.
11131 Alt := First (Select_Alternatives (N));
11132 Proc := First (Body_List);
11133 while Present (Alt) loop
11135 if Nkind (Alt) = N_Accept_Alternative then
11136 Process_Accept_Alternative (Alt, Index, Proc);
11137 Index := Index + 1;
11139 if Present
11140 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11141 then
11142 Next (Proc);
11143 end if;
11145 elsif Nkind (Alt) = N_Delay_Alternative then
11146 Process_Delay_Alternative (Alt, Delay_Num);
11147 Delay_Num := Delay_Num + 1;
11148 end if;
11150 Next (Alt);
11151 end loop;
11153 -- An others choice is always added to the main case, as well
11154 -- as the delay case (to satisfy the compiler).
11156 Append_To (Alt_List,
11157 Make_Case_Statement_Alternative (Loc,
11158 Discrete_Choices =>
11159 New_List (Make_Others_Choice (Loc)),
11160 Statements =>
11161 New_List (Make_Goto_Statement (Loc,
11162 Name => New_Copy (Identifier (End_Lab))))));
11164 Accept_Case := New_List (
11165 Make_Case_Statement (Loc,
11166 Expression => New_Occurrence_Of (Xnam, Loc),
11167 Alternatives => Alt_List));
11169 Append_List (Trailing_List, Accept_Case);
11170 Append_List (Body_List, Decls);
11172 -- Construct case statement for trailing statements of delay
11173 -- alternatives, if there are several of them.
11175 if Delay_Count > 1 then
11176 Append_To (Delay_Alt_List,
11177 Make_Case_Statement_Alternative (Loc,
11178 Discrete_Choices =>
11179 New_List (Make_Others_Choice (Loc)),
11180 Statements =>
11181 New_List (Make_Null_Statement (Loc))));
11183 Delay_Case := New_List (
11184 Make_Case_Statement (Loc,
11185 Expression => New_Occurrence_Of (Delay_Index, Loc),
11186 Alternatives => Delay_Alt_List));
11187 else
11188 Delay_Case := Delay_Alt_List;
11189 end if;
11191 -- If there are no delay alternatives, we append the case statement
11192 -- to the statement list.
11194 if Delay_Count = 0 then
11195 Append_List (Accept_Case, Stats);
11197 -- Delay alternatives present
11199 else
11200 -- If delay alternatives are present we generate:
11202 -- find minimum delay.
11203 -- DX := minimum delay;
11204 -- M := <delay mode>;
11205 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11206 -- DX, MX, X);
11208 -- if X = No_Rendezvous then
11209 -- case statement for delay statements.
11210 -- else
11211 -- case statement for accept alternatives.
11212 -- end if;
11214 declare
11215 Cases : Node_Id;
11216 Stmt : Node_Id;
11217 Parms : List_Id;
11218 Parm : Node_Id;
11219 Conv : Node_Id;
11221 begin
11222 -- The type of the delay expression is known to be legal
11224 if Time_Type = Standard_Duration then
11225 Conv := New_Occurrence_Of (Delay_Min, Loc);
11227 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11228 Conv := Make_Function_Call (Loc,
11229 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11230 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11232 else
11233 pragma Assert
11234 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11236 Conv := Make_Function_Call (Loc,
11237 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11238 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11239 end if;
11241 Stmt := Make_Assignment_Statement (Loc,
11242 Name => New_Occurrence_Of (D, Loc),
11243 Expression => Conv);
11245 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11247 Parms := Parameter_Associations (Select_Call);
11249 Parm := First (Parms);
11250 while Present (Parm) and then Parm /= Select_Mode loop
11251 Next (Parm);
11252 end loop;
11254 pragma Assert (Present (Parm));
11255 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11256 Analyze (Parm);
11258 -- Prepare two new parameters of Duration and Delay_Mode type
11259 -- which represent the value and the mode of the minimum delay.
11261 Next (Parm);
11262 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11263 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11265 -- Create a call to RTS
11267 Rewrite (Select_Call,
11268 Make_Procedure_Call_Statement (Loc,
11269 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11270 Parameter_Associations => Parms));
11272 -- This new call should follow the calculation of the minimum
11273 -- delay.
11275 Insert_List_Before (Select_Call, Delay_List);
11277 if Check_Guard then
11278 Stmt :=
11279 Make_Implicit_If_Statement (N,
11280 Condition => New_Occurrence_Of (Guard_Open, Loc),
11281 Then_Statements => New_List (
11282 New_Copy_Tree (Stmt),
11283 New_Copy_Tree (Select_Call)),
11284 Else_Statements => Accept_Or_Raise);
11285 Rewrite (Select_Call, Stmt);
11286 else
11287 Insert_Before (Select_Call, Stmt);
11288 end if;
11290 Cases :=
11291 Make_Implicit_If_Statement (N,
11292 Condition => Make_Op_Eq (Loc,
11293 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11294 Right_Opnd =>
11295 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11297 Then_Statements => Delay_Case,
11298 Else_Statements => Accept_Case);
11300 Append (Cases, Stats);
11301 end;
11302 end if;
11304 Append (End_Lab, Stats);
11306 -- Replace accept statement with appropriate block
11308 Rewrite (N,
11309 Make_Block_Statement (Loc,
11310 Declarations => Decls,
11311 Handled_Statement_Sequence =>
11312 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11313 Analyze (N);
11315 -- Note: have to worry more about abort deferral in above code ???
11317 -- Final step is to unstack the Accept_Address entries for all accept
11318 -- statements appearing in accept alternatives in the select statement
11320 Alt := First (Alts);
11321 while Present (Alt) loop
11322 if Nkind (Alt) = N_Accept_Alternative then
11323 Remove_Last_Elmt (Accept_Address
11324 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11325 end if;
11327 Next (Alt);
11328 end loop;
11329 end Expand_N_Selective_Accept;
11331 -------------------------------------------
11332 -- Expand_N_Single_Protected_Declaration --
11333 -------------------------------------------
11335 -- A single protected declaration should never be present after semantic
11336 -- analysis because it is transformed into a protected type declaration
11337 -- and an accompanying anonymous object. This routine ensures that the
11338 -- transformation takes place.
11340 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11341 begin
11342 raise Program_Error;
11343 end Expand_N_Single_Protected_Declaration;
11345 --------------------------------------
11346 -- Expand_N_Single_Task_Declaration --
11347 --------------------------------------
11349 -- A single task declaration should never be present after semantic
11350 -- analysis because it is transformed into a task type declaration and
11351 -- an accompanying anonymous object. This routine ensures that the
11352 -- transformation takes place.
11354 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11355 begin
11356 raise Program_Error;
11357 end Expand_N_Single_Task_Declaration;
11359 ------------------------
11360 -- Expand_N_Task_Body --
11361 ------------------------
11363 -- Given a task body
11365 -- task body tname is
11366 -- <declarations>
11367 -- begin
11368 -- <statements>
11369 -- end x;
11371 -- This expansion routine converts it into a procedure and sets the
11372 -- elaboration flag for the procedure to true, to represent the fact
11373 -- that the task body is now elaborated:
11375 -- procedure tnameB (_Task : access tnameV) is
11376 -- discriminal : dtype renames _Task.discriminant;
11378 -- procedure _clean is
11379 -- begin
11380 -- Abort_Defer.all;
11381 -- Complete_Task;
11382 -- Abort_Undefer.all;
11383 -- return;
11384 -- end _clean;
11386 -- begin
11387 -- Abort_Undefer.all;
11388 -- <declarations>
11389 -- System.Task_Stages.Complete_Activation;
11390 -- <statements>
11391 -- at end
11392 -- _clean;
11393 -- end tnameB;
11395 -- tnameE := True;
11397 -- In addition, if the task body is an activator, then a call to activate
11398 -- tasks is added at the start of the statements, before the call to
11399 -- Complete_Activation, and if in addition the task is a master then it
11400 -- must be established as a master. These calls are inserted and analyzed
11401 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11402 -- expanded.
11404 -- There is one discriminal declaration line generated for each
11405 -- discriminant that is present to provide an easy reference point for
11406 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11408 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11409 -- task body procedures have a profile (Arg : System.Address). That is
11410 -- needed because GNARLI has to use the same access-to-subprogram type
11411 -- for all task types. We depend here on knowing that in GNAT, passing
11412 -- an address argument by value is identical to passing a record value
11413 -- by access (in either case a single pointer is passed), so even though
11414 -- this procedure has the wrong profile. In fact it's all OK, since the
11415 -- callings sequence is identical.
11417 procedure Expand_N_Task_Body (N : Node_Id) is
11418 Loc : constant Source_Ptr := Sloc (N);
11419 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11420 Call : Node_Id;
11421 New_N : Node_Id;
11423 Insert_Nod : Node_Id;
11424 -- Used to determine the proper location of wrapper body insertions
11426 begin
11427 -- if no task body procedure, means we had an error in configurable
11428 -- run-time mode, and there is no point in proceeding further.
11430 if No (Task_Body_Procedure (Ttyp)) then
11431 return;
11432 end if;
11434 -- Add renaming declarations for discriminals and a declaration for the
11435 -- entry family index (if applicable).
11437 Install_Private_Data_Declarations
11438 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11440 -- Add a call to Abort_Undefer at the very beginning of the task
11441 -- body since this body is called with abort still deferred.
11443 if Abort_Allowed then
11444 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11445 Insert_Before
11446 (First (Statements (Handled_Statement_Sequence (N))), Call);
11447 Analyze (Call);
11448 end if;
11450 -- The statement part has already been protected with an at_end and
11451 -- cleanup actions. The call to Complete_Activation must be placed
11452 -- at the head of the sequence of statements of that block. The
11453 -- declarations have been merged in this sequence of statements but
11454 -- the first real statement is accessible from the First_Real_Statement
11455 -- field (which was set for exactly this purpose).
11457 if Restricted_Profile then
11458 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11459 else
11460 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11461 end if;
11463 Insert_Before
11464 (First_Real_Statement (Handled_Statement_Sequence (N)), Call);
11465 Analyze (Call);
11467 New_N :=
11468 Make_Subprogram_Body (Loc,
11469 Specification => Build_Task_Proc_Specification (Ttyp),
11470 Declarations => Declarations (N),
11471 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11472 Set_Is_Task_Body_Procedure (New_N);
11474 -- If the task contains generic instantiations, cleanup actions are
11475 -- delayed until after instantiation. Transfer the activation chain to
11476 -- the subprogram, to insure that the activation call is properly
11477 -- generated. It the task body contains inner tasks, indicate that the
11478 -- subprogram is a task master.
11480 if Delay_Cleanups (Ttyp) then
11481 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11482 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11483 end if;
11485 Rewrite (N, New_N);
11486 Analyze (N);
11488 -- Set elaboration flag immediately after task body. If the body is a
11489 -- subunit, the flag is set in the declarative part containing the stub.
11491 if Nkind (Parent (N)) /= N_Subunit then
11492 Insert_After (N,
11493 Make_Assignment_Statement (Loc,
11494 Name =>
11495 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11496 Expression => New_Occurrence_Of (Standard_True, Loc)));
11497 end if;
11499 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11500 -- the task body. At this point all wrapper specs have been created,
11501 -- frozen and included in the dispatch table for the task type.
11503 if Ada_Version >= Ada_2005 then
11504 if Nkind (Parent (N)) = N_Subunit then
11505 Insert_Nod := Corresponding_Stub (Parent (N));
11506 else
11507 Insert_Nod := N;
11508 end if;
11510 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11511 end if;
11512 end Expand_N_Task_Body;
11514 ------------------------------------
11515 -- Expand_N_Task_Type_Declaration --
11516 ------------------------------------
11518 -- We have several things to do. First we must create a Boolean flag used
11519 -- to mark if the body is elaborated yet. This variable gets set to True
11520 -- when the body of the task is elaborated (we can't rely on the normal
11521 -- ABE mechanism for the task body, since we need to pass an access to
11522 -- this elaboration boolean to the runtime routines).
11524 -- taskE : aliased Boolean := False;
11526 -- Next a variable is declared to hold the task stack size (either the
11527 -- default : Unspecified_Size, or a value that is set by a pragma
11528 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11529 -- the variable is initialized with this value:
11531 -- taskZ : Size_Type := Unspecified_Size;
11532 -- or
11533 -- taskZ : Size_Type := Size_Type (size_expression);
11535 -- Note: No variable is needed to hold the task relative deadline since
11536 -- its value would never be static because the parameter is of a private
11537 -- type (Ada.Real_Time.Time_Span).
11539 -- Next we create a corresponding record type declaration used to represent
11540 -- values of this task. The general form of this type declaration is
11542 -- type taskV (discriminants) is record
11543 -- _Task_Id : Task_Id;
11544 -- entry_family : array (bounds) of Void;
11545 -- _Priority : Integer := priority_expression;
11546 -- _Size : Size_Type := size_expression;
11547 -- _Secondary_Stack_Size : Size_Type := size_expression;
11548 -- _Task_Info : Task_Info_Type := task_info_expression;
11549 -- _CPU : Integer := cpu_range_expression;
11550 -- _Relative_Deadline : Time_Span := time_span_expression;
11551 -- _Domain : Dispatching_Domain := dd_expression;
11552 -- end record;
11554 -- The discriminants are present only if the corresponding task type has
11555 -- discriminants, and they exactly mirror the task type discriminants.
11557 -- The Id field is always present. It contains the Task_Id value, as set by
11558 -- the call to Create_Task. Note that although the task is limited, the
11559 -- task value record type is not limited, so there is no problem in passing
11560 -- this field as an out parameter to Create_Task.
11562 -- One entry_family component is present for each entry family in the task
11563 -- definition. The bounds correspond to the bounds of the entry family
11564 -- (which may depend on discriminants). The element type is void, since we
11565 -- only need the bounds information for determining the entry index. Note
11566 -- that the use of an anonymous array would normally be illegal in this
11567 -- context, but this is a parser check, and the semantics is quite prepared
11568 -- to handle such a case.
11570 -- The _Size field is present only if a Storage_Size pragma appears in the
11571 -- task definition. The expression captures the argument that was present
11572 -- in the pragma, and is used to override the task stack size otherwise
11573 -- associated with the task type.
11575 -- The _Secondary_Stack_Size field is present only the task entity has a
11576 -- Secondary_Stack_Size rep item. It will be filled at the freeze point,
11577 -- when the record init proc is built, to capture the expression of the
11578 -- rep item (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot
11579 -- be filled here since aspect evaluations are delayed till the freeze
11580 -- point.
11582 -- The _Priority field is present only if the task entity has a Priority or
11583 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11584 -- definition clause). It will be filled at the freeze point, when the
11585 -- record init proc is built, to capture the expression of the rep item
11586 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11587 -- here since aspect evaluations are delayed till the freeze point.
11589 -- The _Task_Info field is present only if a Task_Info pragma appears in
11590 -- the task definition. The expression captures the argument that was
11591 -- present in the pragma, and is used to provide the Task_Image parameter
11592 -- to the call to Create_Task.
11594 -- The _CPU field is present only if the task entity has a CPU rep item
11595 -- (pragma, aspect specification or attribute definition clause). It will
11596 -- be filled at the freeze point, when the record init proc is built, to
11597 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11598 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11599 -- are delayed till the freeze point.
11601 -- The _Relative_Deadline field is present only if a Relative_Deadline
11602 -- pragma appears in the task definition. The expression captures the
11603 -- argument that was present in the pragma, and is used to provide the
11604 -- Relative_Deadline parameter to the call to Create_Task.
11606 -- The _Domain field is present only if the task entity has a
11607 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11608 -- definition clause). It will be filled at the freeze point, when the
11609 -- record init proc is built, to capture the expression of the rep item
11610 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11611 -- here since aspect evaluations are delayed till the freeze point.
11613 -- When a task is declared, an instance of the task value record is
11614 -- created. The elaboration of this declaration creates the correct bounds
11615 -- for the entry families, and also evaluates the size, priority, and
11616 -- task_Info expressions if needed. The initialization routine for the task
11617 -- type itself then calls Create_Task with appropriate parameters to
11618 -- initialize the value of the Task_Id field.
11620 -- Note: the address of this record is passed as the "Discriminants"
11621 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11622 -- body procedure, it does not matter that it does not quite match the
11623 -- GNARLI model of what is being passed (the record contains more than just
11624 -- the discriminants, but the discriminants can be found from the record
11625 -- value).
11627 -- The Entity_Id for this created record type is placed in the
11628 -- Corresponding_Record_Type field of the associated task type entity.
11630 -- Next we create a procedure specification for the task body procedure:
11632 -- procedure taskB (_Task : access taskV);
11634 -- Note that this must come after the record type declaration, since
11635 -- the spec refers to this type. It turns out that the initialization
11636 -- procedure for the value type references the task body spec, but that's
11637 -- fine, since it won't be generated till the freeze point for the type,
11638 -- which is certainly after the task body spec declaration.
11640 -- Finally, we set the task index value field of the entry attribute in
11641 -- the case of a simple entry.
11643 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11644 Loc : constant Source_Ptr := Sloc (N);
11645 TaskId : constant Entity_Id := Defining_Identifier (N);
11646 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11647 Tasknm : constant Name_Id := Chars (Tasktyp);
11648 Taskdef : constant Node_Id := Task_Definition (N);
11650 Body_Decl : Node_Id;
11651 Cdecls : List_Id;
11652 Decl_Stack : Node_Id;
11653 Elab_Decl : Node_Id;
11654 Ent_Stack : Entity_Id;
11655 Proc_Spec : Node_Id;
11656 Rec_Decl : Node_Id;
11657 Rec_Ent : Entity_Id;
11658 Size_Decl : Entity_Id;
11659 Task_Size : Node_Id;
11661 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11662 -- Searches the task definition T for the first occurrence of the pragma
11663 -- Relative Deadline. The caller has ensured that the pragma is present
11664 -- in the task definition. Note that this routine cannot be implemented
11665 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11666 -- not chained because their expansion into a procedure call statement
11667 -- would cause a break in the chain.
11669 ----------------------------------
11670 -- Get_Relative_Deadline_Pragma --
11671 ----------------------------------
11673 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11674 N : Node_Id;
11676 begin
11677 N := First (Visible_Declarations (T));
11678 while Present (N) loop
11679 if Nkind (N) = N_Pragma
11680 and then Pragma_Name (N) = Name_Relative_Deadline
11681 then
11682 return N;
11683 end if;
11685 Next (N);
11686 end loop;
11688 N := First (Private_Declarations (T));
11689 while Present (N) loop
11690 if Nkind (N) = N_Pragma
11691 and then Pragma_Name (N) = Name_Relative_Deadline
11692 then
11693 return N;
11694 end if;
11696 Next (N);
11697 end loop;
11699 raise Program_Error;
11700 end Get_Relative_Deadline_Pragma;
11702 -- Start of processing for Expand_N_Task_Type_Declaration
11704 begin
11705 -- If already expanded, nothing to do
11707 if Present (Corresponding_Record_Type (Tasktyp)) then
11708 return;
11709 end if;
11711 -- Here we will do the expansion
11713 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11715 Rec_Ent := Defining_Identifier (Rec_Decl);
11716 Cdecls := Component_Items (Component_List
11717 (Type_Definition (Rec_Decl)));
11719 Qualify_Entity_Names (N);
11721 -- First create the elaboration variable
11723 Elab_Decl :=
11724 Make_Object_Declaration (Loc,
11725 Defining_Identifier =>
11726 Make_Defining_Identifier (Sloc (Tasktyp),
11727 Chars => New_External_Name (Tasknm, 'E')),
11728 Aliased_Present => True,
11729 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
11730 Expression => New_Occurrence_Of (Standard_False, Loc));
11732 Insert_After (N, Elab_Decl);
11734 -- Next create the declaration of the size variable (tasknmZ)
11736 Set_Storage_Size_Variable (Tasktyp,
11737 Make_Defining_Identifier (Sloc (Tasktyp),
11738 Chars => New_External_Name (Tasknm, 'Z')));
11740 if Present (Taskdef)
11741 and then Has_Storage_Size_Pragma (Taskdef)
11742 and then
11743 Is_OK_Static_Expression
11744 (Expression
11745 (First (Pragma_Argument_Associations
11746 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11747 then
11748 Size_Decl :=
11749 Make_Object_Declaration (Loc,
11750 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11751 Object_Definition =>
11752 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11753 Expression =>
11754 Convert_To (RTE (RE_Size_Type),
11755 Relocate_Node
11756 (Expression (First (Pragma_Argument_Associations
11757 (Get_Rep_Pragma
11758 (TaskId, Name_Storage_Size)))))));
11760 else
11761 Size_Decl :=
11762 Make_Object_Declaration (Loc,
11763 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11764 Object_Definition =>
11765 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11766 Expression =>
11767 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
11768 end if;
11770 Insert_After (Elab_Decl, Size_Decl);
11772 -- Next build the rest of the corresponding record declaration. This is
11773 -- done last, since the corresponding record initialization procedure
11774 -- will reference the previously created entities.
11776 -- Fill in the component declarations -- first the _Task_Id field
11778 Append_To (Cdecls,
11779 Make_Component_Declaration (Loc,
11780 Defining_Identifier =>
11781 Make_Defining_Identifier (Loc, Name_uTask_Id),
11782 Component_Definition =>
11783 Make_Component_Definition (Loc,
11784 Aliased_Present => False,
11785 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
11786 Loc))));
11788 -- Declare static ATCB (that is, created by the expander) if we are
11789 -- using the Restricted run time.
11791 if Restricted_Profile then
11792 Append_To (Cdecls,
11793 Make_Component_Declaration (Loc,
11794 Defining_Identifier =>
11795 Make_Defining_Identifier (Loc, Name_uATCB),
11797 Component_Definition =>
11798 Make_Component_Definition (Loc,
11799 Aliased_Present => True,
11800 Subtype_Indication => Make_Subtype_Indication (Loc,
11801 Subtype_Mark =>
11802 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11804 Constraint =>
11805 Make_Index_Or_Discriminant_Constraint (Loc,
11806 Constraints =>
11807 New_List (Make_Integer_Literal (Loc, 0)))))));
11809 end if;
11811 -- Declare static stack (that is, created by the expander) if we are
11812 -- using the Restricted run time on a bare board configuration.
11814 if Restricted_Profile and then Preallocated_Stacks_On_Target then
11816 -- First we need to extract the appropriate stack size
11818 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11820 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11821 declare
11822 Expr_N : constant Node_Id :=
11823 Expression (First (
11824 Pragma_Argument_Associations (
11825 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11826 Etyp : constant Entity_Id := Etype (Expr_N);
11827 P : constant Node_Id := Parent (Expr_N);
11829 begin
11830 -- The stack is defined inside the corresponding record.
11831 -- Therefore if the size of the stack is set by means of
11832 -- a discriminant, we must reference the discriminant of the
11833 -- corresponding record type.
11835 if Nkind (Expr_N) in N_Has_Entity
11836 and then Present (Discriminal_Link (Entity (Expr_N)))
11837 then
11838 Task_Size :=
11839 New_Occurrence_Of
11840 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11841 Loc);
11842 Set_Parent (Task_Size, P);
11843 Set_Etype (Task_Size, Etyp);
11844 Set_Analyzed (Task_Size);
11846 else
11847 Task_Size := Relocate_Node (Expr_N);
11848 end if;
11849 end;
11851 else
11852 Task_Size :=
11853 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
11854 end if;
11856 Decl_Stack := Make_Component_Declaration (Loc,
11857 Defining_Identifier => Ent_Stack,
11859 Component_Definition =>
11860 Make_Component_Definition (Loc,
11861 Aliased_Present => True,
11862 Subtype_Indication => Make_Subtype_Indication (Loc,
11863 Subtype_Mark =>
11864 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11866 Constraint =>
11867 Make_Index_Or_Discriminant_Constraint (Loc,
11868 Constraints => New_List (Make_Range (Loc,
11869 Low_Bound => Make_Integer_Literal (Loc, 1),
11870 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11871 Task_Size)))))));
11873 Append_To (Cdecls, Decl_Stack);
11875 -- The appropriate alignment for the stack is ensured by the run-time
11876 -- code in charge of task creation.
11878 end if;
11880 -- Add components for entry families
11882 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
11884 -- Add the _Priority component if a Interrupt_Priority or Priority rep
11885 -- item is present.
11887 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
11888 Append_To (Cdecls,
11889 Make_Component_Declaration (Loc,
11890 Defining_Identifier =>
11891 Make_Defining_Identifier (Loc, Name_uPriority),
11892 Component_Definition =>
11893 Make_Component_Definition (Loc,
11894 Aliased_Present => False,
11895 Subtype_Indication =>
11896 New_Occurrence_Of (Standard_Integer, Loc))));
11897 end if;
11899 -- Add the _Size component if a Storage_Size pragma is present
11901 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11902 Append_To (Cdecls,
11903 Make_Component_Declaration (Loc,
11904 Defining_Identifier =>
11905 Make_Defining_Identifier (Loc, Name_uSize),
11907 Component_Definition =>
11908 Make_Component_Definition (Loc,
11909 Aliased_Present => False,
11910 Subtype_Indication =>
11911 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
11913 Expression =>
11914 Convert_To (RTE (RE_Size_Type),
11915 Relocate_Node (
11916 Expression (First (
11917 Pragma_Argument_Associations (
11918 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
11919 end if;
11921 -- Add the _Secondary_Stack_Size component if a Secondary_Stack_Size
11922 -- rep item is present.
11924 if Has_Rep_Item
11925 (TaskId, Name_Secondary_Stack_Size, Check_Parents => False)
11926 then
11927 Append_To (Cdecls,
11928 Make_Component_Declaration (Loc,
11929 Defining_Identifier =>
11930 Make_Defining_Identifier (Loc, Name_uSecondary_Stack_Size),
11932 Component_Definition =>
11933 Make_Component_Definition (Loc,
11934 Aliased_Present => False,
11935 Subtype_Indication =>
11936 New_Occurrence_Of (RTE (RE_Size_Type), Loc))));
11937 end if;
11939 -- Add the _Task_Info component if a Task_Info pragma is present
11941 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
11942 Append_To (Cdecls,
11943 Make_Component_Declaration (Loc,
11944 Defining_Identifier =>
11945 Make_Defining_Identifier (Loc, Name_uTask_Info),
11947 Component_Definition =>
11948 Make_Component_Definition (Loc,
11949 Aliased_Present => False,
11950 Subtype_Indication =>
11951 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
11953 Expression => New_Copy (
11954 Expression (First (
11955 Pragma_Argument_Associations (
11956 Get_Rep_Pragma
11957 (TaskId, Name_Task_Info, Check_Parents => False)))))));
11958 end if;
11960 -- Add the _CPU component if a CPU rep item is present
11962 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
11963 Append_To (Cdecls,
11964 Make_Component_Declaration (Loc,
11965 Defining_Identifier =>
11966 Make_Defining_Identifier (Loc, Name_uCPU),
11968 Component_Definition =>
11969 Make_Component_Definition (Loc,
11970 Aliased_Present => False,
11971 Subtype_Indication =>
11972 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
11973 end if;
11975 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
11976 -- present. If we are using a restricted run time this component will
11977 -- not be added (deadlines are not allowed by the Ravenscar profile),
11978 -- unless the task dispatching policy is EDF (for GNAT_Ravenscar_EDF
11979 -- profile).
11981 if (not Restricted_Profile or else Task_Dispatching_Policy = 'E')
11982 and then Present (Taskdef)
11983 and then Has_Relative_Deadline_Pragma (Taskdef)
11984 then
11985 Append_To (Cdecls,
11986 Make_Component_Declaration (Loc,
11987 Defining_Identifier =>
11988 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
11990 Component_Definition =>
11991 Make_Component_Definition (Loc,
11992 Aliased_Present => False,
11993 Subtype_Indication =>
11994 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
11996 Expression =>
11997 Convert_To (RTE (RE_Time_Span),
11998 Relocate_Node (
11999 Expression (First (
12000 Pragma_Argument_Associations (
12001 Get_Relative_Deadline_Pragma (Taskdef))))))));
12002 end if;
12004 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12005 -- item is present. If we are using a restricted run time this component
12006 -- will not be added (dispatching domains are not allowed by the
12007 -- Ravenscar profile).
12009 if not Restricted_Profile
12010 and then
12011 Has_Rep_Item
12012 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12013 then
12014 Append_To (Cdecls,
12015 Make_Component_Declaration (Loc,
12016 Defining_Identifier =>
12017 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12019 Component_Definition =>
12020 Make_Component_Definition (Loc,
12021 Aliased_Present => False,
12022 Subtype_Indication =>
12023 New_Occurrence_Of
12024 (RTE (RE_Dispatching_Domain_Access), Loc))));
12025 end if;
12027 Insert_After (Size_Decl, Rec_Decl);
12029 -- Analyze the record declaration immediately after construction,
12030 -- because the initialization procedure is needed for single task
12031 -- declarations before the next entity is analyzed.
12033 Analyze (Rec_Decl);
12035 -- Create the declaration of the task body procedure
12037 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12038 Body_Decl :=
12039 Make_Subprogram_Declaration (Loc,
12040 Specification => Proc_Spec);
12041 Set_Is_Task_Body_Procedure (Body_Decl);
12043 Insert_After (Rec_Decl, Body_Decl);
12045 -- The subprogram does not comes from source, so we have to indicate the
12046 -- need for debugging information explicitly.
12048 if Comes_From_Source (Original_Node (N)) then
12049 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12050 end if;
12052 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12053 -- the corresponding record has been frozen.
12055 if Ada_Version >= Ada_2005 then
12056 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12057 end if;
12059 -- Ada 2005 (AI-345): We must defer freezing to allow further
12060 -- declaration of primitive subprograms covering task interfaces
12062 if Ada_Version <= Ada_95 then
12064 -- Now we can freeze the corresponding record. This needs manually
12065 -- freezing, since it is really part of the task type, and the task
12066 -- type is frozen at this stage. We of course need the initialization
12067 -- procedure for this corresponding record type and we won't get it
12068 -- in time if we don't freeze now.
12070 declare
12071 L : constant List_Id := Freeze_Entity (Rec_Ent, N);
12072 begin
12073 if Is_Non_Empty_List (L) then
12074 Insert_List_After (Body_Decl, L);
12075 end if;
12076 end;
12077 end if;
12079 -- Complete the expansion of access types to the current task type, if
12080 -- any were declared.
12082 Expand_Previous_Access_Type (Tasktyp);
12084 -- Create wrappers for entries that have contract cases, preconditions
12085 -- and postconditions.
12087 declare
12088 Ent : Entity_Id;
12090 begin
12091 Ent := First_Entity (Tasktyp);
12092 while Present (Ent) loop
12093 if Ekind_In (Ent, E_Entry, E_Entry_Family) then
12094 Build_Contract_Wrapper (Ent, N);
12095 end if;
12097 Next_Entity (Ent);
12098 end loop;
12099 end;
12100 end Expand_N_Task_Type_Declaration;
12102 -------------------------------
12103 -- Expand_N_Timed_Entry_Call --
12104 -------------------------------
12106 -- A timed entry call in normal case is not implemented using ATC mechanism
12107 -- anymore for efficiency reason.
12109 -- select
12110 -- T.E;
12111 -- S1;
12112 -- or
12113 -- delay D;
12114 -- S2;
12115 -- end select;
12117 -- is expanded as follows:
12119 -- 1) When T.E is a task entry_call;
12121 -- declare
12122 -- B : Boolean;
12123 -- X : Task_Entry_Index := <entry index>;
12124 -- DX : Duration := To_Duration (D);
12125 -- M : Delay_Mode := <discriminant>;
12126 -- P : parms := (parm, parm, parm);
12128 -- begin
12129 -- Timed_Protected_Entry_Call
12130 -- (<acceptor-task>, X, P'Address, DX, M, B);
12131 -- if B then
12132 -- S1;
12133 -- else
12134 -- S2;
12135 -- end if;
12136 -- end;
12138 -- 2) When T.E is a protected entry_call;
12140 -- declare
12141 -- B : Boolean;
12142 -- X : Protected_Entry_Index := <entry index>;
12143 -- DX : Duration := To_Duration (D);
12144 -- M : Delay_Mode := <discriminant>;
12145 -- P : parms := (parm, parm, parm);
12147 -- begin
12148 -- Timed_Protected_Entry_Call
12149 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12150 -- if B then
12151 -- S1;
12152 -- else
12153 -- S2;
12154 -- end if;
12155 -- end;
12157 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12158 -- is no delay and the triggering statements are executed. We first
12159 -- determine the kind of the triggering call and then execute a
12160 -- synchronized operation or a direct call.
12162 -- declare
12163 -- B : Boolean := False;
12164 -- C : Ada.Tags.Prim_Op_Kind;
12165 -- DX : Duration := To_Duration (D)
12166 -- K : Ada.Tags.Tagged_Kind :=
12167 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12168 -- M : Integer :=...;
12169 -- P : Parameters := (Param1 .. ParamN);
12170 -- S : Integer;
12172 -- begin
12173 -- if K = Ada.Tags.TK_Limited_Tagged
12174 -- or else K = Ada.Tags.TK_Tagged
12175 -- then
12176 -- <dispatching-call>;
12177 -- B := True;
12179 -- else
12180 -- S :=
12181 -- Ada.Tags.Get_Offset_Index
12182 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12184 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12186 -- if C = POK_Protected_Entry
12187 -- or else C = POK_Task_Entry
12188 -- then
12189 -- Param1 := P.Param1;
12190 -- ...
12191 -- ParamN := P.ParamN;
12192 -- end if;
12194 -- if B then
12195 -- if C = POK_Procedure
12196 -- or else C = POK_Protected_Procedure
12197 -- or else C = POK_Task_Procedure
12198 -- then
12199 -- <dispatching-call>;
12200 -- end if;
12201 -- end if;
12202 -- end if;
12204 -- if B then
12205 -- <triggering-statements>
12206 -- else
12207 -- <timed-statements>
12208 -- end if;
12209 -- end;
12211 -- The triggering statement and the sequence of timed statements have not
12212 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12213 -- global references if within an instantiation.
12215 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12216 Loc : constant Source_Ptr := Sloc (N);
12218 Actuals : List_Id;
12219 Blk_Typ : Entity_Id;
12220 Call : Node_Id;
12221 Call_Ent : Entity_Id;
12222 Conc_Typ_Stmts : List_Id;
12223 Concval : Node_Id;
12224 D_Alt : constant Node_Id := Delay_Alternative (N);
12225 D_Conv : Node_Id;
12226 D_Disc : Node_Id;
12227 D_Stat : Node_Id := Delay_Statement (D_Alt);
12228 D_Stats : List_Id;
12229 D_Type : Entity_Id;
12230 Decls : List_Id;
12231 Dummy : Node_Id;
12232 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12233 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12234 E_Stats : List_Id;
12235 Ename : Node_Id;
12236 Formals : List_Id;
12237 Index : Node_Id;
12238 Is_Disp_Select : Boolean;
12239 Lim_Typ_Stmts : List_Id;
12240 N_Stats : List_Id;
12241 Obj : Entity_Id;
12242 Param : Node_Id;
12243 Params : List_Id;
12244 Stmt : Node_Id;
12245 Stmts : List_Id;
12246 Unpack : List_Id;
12248 B : Entity_Id; -- Call status flag
12249 C : Entity_Id; -- Call kind
12250 D : Entity_Id; -- Delay
12251 K : Entity_Id; -- Tagged kind
12252 M : Entity_Id; -- Delay mode
12253 P : Entity_Id; -- Parameter block
12254 S : Entity_Id; -- Primitive operation slot
12256 -- Start of processing for Expand_N_Timed_Entry_Call
12258 begin
12259 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12260 -- was already reported on spec, so do not attempt to expand the call.
12262 if Restriction_Active (No_Select_Statements) then
12263 return;
12264 end if;
12266 Process_Statements_For_Controlled_Objects (E_Alt);
12267 Process_Statements_For_Controlled_Objects (D_Alt);
12269 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12271 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12272 -- may wrap them in blocks.
12274 E_Stats := Statements (E_Alt);
12275 D_Stats := Statements (D_Alt);
12277 -- The arguments in the call may require dynamic allocation, and the
12278 -- call statement may have been transformed into a block. The block
12279 -- may contain additional declarations for internal entities, and the
12280 -- original call is found by sequential search.
12282 if Nkind (E_Call) = N_Block_Statement then
12283 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12284 while not Nkind_In (E_Call, N_Procedure_Call_Statement,
12285 N_Entry_Call_Statement)
12286 loop
12287 Next (E_Call);
12288 end loop;
12289 end if;
12291 Is_Disp_Select :=
12292 Ada_Version >= Ada_2005
12293 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12295 if Is_Disp_Select then
12296 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12297 Decls := New_List;
12299 Stmts := New_List;
12301 -- Generate:
12302 -- B : Boolean := False;
12304 B := Build_B (Loc, Decls);
12306 -- Generate:
12307 -- C : Ada.Tags.Prim_Op_Kind;
12309 C := Build_C (Loc, Decls);
12311 -- Because the analysis of all statements was disabled, manually
12312 -- analyze the delay statement.
12314 Analyze (D_Stat);
12315 D_Stat := Original_Node (D_Stat);
12317 else
12318 -- Build an entry call using Simple_Entry_Call
12320 Extract_Entry (E_Call, Concval, Ename, Index);
12321 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12323 Decls := Declarations (E_Call);
12324 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12326 if No (Decls) then
12327 Decls := New_List;
12328 end if;
12330 -- Generate:
12331 -- B : Boolean;
12333 B := Make_Defining_Identifier (Loc, Name_uB);
12335 Prepend_To (Decls,
12336 Make_Object_Declaration (Loc,
12337 Defining_Identifier => B,
12338 Object_Definition =>
12339 New_Occurrence_Of (Standard_Boolean, Loc)));
12340 end if;
12342 -- Duration and mode processing
12344 D_Type := Base_Type (Etype (Expression (D_Stat)));
12346 -- Use the type of the delay expression (Calendar or Real_Time) to
12347 -- generate the appropriate conversion.
12349 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12350 D_Disc := Make_Integer_Literal (Loc, 0);
12351 D_Conv := Relocate_Node (Expression (D_Stat));
12353 elsif Is_RTE (D_Type, RO_CA_Time) then
12354 D_Disc := Make_Integer_Literal (Loc, 1);
12355 D_Conv :=
12356 Make_Function_Call (Loc,
12357 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12358 Parameter_Associations =>
12359 New_List (New_Copy (Expression (D_Stat))));
12361 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12362 D_Disc := Make_Integer_Literal (Loc, 2);
12363 D_Conv :=
12364 Make_Function_Call (Loc,
12365 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12366 Parameter_Associations =>
12367 New_List (New_Copy (Expression (D_Stat))));
12368 end if;
12370 D := Make_Temporary (Loc, 'D');
12372 -- Generate:
12373 -- D : Duration;
12375 Append_To (Decls,
12376 Make_Object_Declaration (Loc,
12377 Defining_Identifier => D,
12378 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12380 M := Make_Temporary (Loc, 'M');
12382 -- Generate:
12383 -- M : Integer := (0 | 1 | 2);
12385 Append_To (Decls,
12386 Make_Object_Declaration (Loc,
12387 Defining_Identifier => M,
12388 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12389 Expression => D_Disc));
12391 -- Do the assignment at this stage only because the evaluation of the
12392 -- expression must not occur before (see ACVC C97302A).
12394 Append_To (Stmts,
12395 Make_Assignment_Statement (Loc,
12396 Name => New_Occurrence_Of (D, Loc),
12397 Expression => D_Conv));
12399 -- Parameter block processing
12401 -- Manually create the parameter block for dispatching calls. In the
12402 -- case of entries, the block has already been created during the call
12403 -- to Build_Simple_Entry_Call.
12405 if Is_Disp_Select then
12407 -- Tagged kind processing, generate:
12408 -- K : Ada.Tags.Tagged_Kind :=
12409 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12411 K := Build_K (Loc, Decls, Obj);
12413 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12414 P :=
12415 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12417 -- Dispatch table slot processing, generate:
12418 -- S : Integer;
12420 S := Build_S (Loc, Decls);
12422 -- Generate:
12423 -- S := Ada.Tags.Get_Offset_Index
12424 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12426 Conc_Typ_Stmts :=
12427 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12429 -- Generate:
12430 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12432 -- where Obj is the controlling formal parameter, S is the dispatch
12433 -- table slot number of the dispatching operation, P is the wrapped
12434 -- parameter block, D is the duration, M is the duration mode, C is
12435 -- the call kind and B is the call status.
12437 Params := New_List;
12439 Append_To (Params, New_Copy_Tree (Obj));
12440 Append_To (Params, New_Occurrence_Of (S, Loc));
12441 Append_To (Params,
12442 Make_Attribute_Reference (Loc,
12443 Prefix => New_Occurrence_Of (P, Loc),
12444 Attribute_Name => Name_Address));
12445 Append_To (Params, New_Occurrence_Of (D, Loc));
12446 Append_To (Params, New_Occurrence_Of (M, Loc));
12447 Append_To (Params, New_Occurrence_Of (C, Loc));
12448 Append_To (Params, New_Occurrence_Of (B, Loc));
12450 Append_To (Conc_Typ_Stmts,
12451 Make_Procedure_Call_Statement (Loc,
12452 Name =>
12453 New_Occurrence_Of
12454 (Find_Prim_Op
12455 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12456 Parameter_Associations => Params));
12458 -- Generate:
12459 -- if C = POK_Protected_Entry
12460 -- or else C = POK_Task_Entry
12461 -- then
12462 -- Param1 := P.Param1;
12463 -- ...
12464 -- ParamN := P.ParamN;
12465 -- end if;
12467 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12469 -- Generate the if statement only when the packed parameters need
12470 -- explicit assignments to their corresponding actuals.
12472 if Present (Unpack) then
12473 Append_To (Conc_Typ_Stmts,
12474 Make_Implicit_If_Statement (N,
12476 Condition =>
12477 Make_Or_Else (Loc,
12478 Left_Opnd =>
12479 Make_Op_Eq (Loc,
12480 Left_Opnd => New_Occurrence_Of (C, Loc),
12481 Right_Opnd =>
12482 New_Occurrence_Of
12483 (RTE (RE_POK_Protected_Entry), Loc)),
12485 Right_Opnd =>
12486 Make_Op_Eq (Loc,
12487 Left_Opnd => New_Occurrence_Of (C, Loc),
12488 Right_Opnd =>
12489 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12491 Then_Statements => Unpack));
12492 end if;
12494 -- Generate:
12496 -- if B then
12497 -- if C = POK_Procedure
12498 -- or else C = POK_Protected_Procedure
12499 -- or else C = POK_Task_Procedure
12500 -- then
12501 -- <dispatching-call>
12502 -- end if;
12503 -- end if;
12505 N_Stats := New_List (
12506 Make_Implicit_If_Statement (N,
12507 Condition =>
12508 Make_Or_Else (Loc,
12509 Left_Opnd =>
12510 Make_Op_Eq (Loc,
12511 Left_Opnd => New_Occurrence_Of (C, Loc),
12512 Right_Opnd =>
12513 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12515 Right_Opnd =>
12516 Make_Or_Else (Loc,
12517 Left_Opnd =>
12518 Make_Op_Eq (Loc,
12519 Left_Opnd => New_Occurrence_Of (C, Loc),
12520 Right_Opnd =>
12521 New_Occurrence_Of (RTE (
12522 RE_POK_Protected_Procedure), Loc)),
12523 Right_Opnd =>
12524 Make_Op_Eq (Loc,
12525 Left_Opnd => New_Occurrence_Of (C, Loc),
12526 Right_Opnd =>
12527 New_Occurrence_Of
12528 (RTE (RE_POK_Task_Procedure), Loc)))),
12530 Then_Statements => New_List (E_Call)));
12532 Append_To (Conc_Typ_Stmts,
12533 Make_Implicit_If_Statement (N,
12534 Condition => New_Occurrence_Of (B, Loc),
12535 Then_Statements => N_Stats));
12537 -- Generate:
12538 -- <dispatching-call>;
12539 -- B := True;
12541 Lim_Typ_Stmts :=
12542 New_List (New_Copy_Tree (E_Call),
12543 Make_Assignment_Statement (Loc,
12544 Name => New_Occurrence_Of (B, Loc),
12545 Expression => New_Occurrence_Of (Standard_True, Loc)));
12547 -- Generate:
12548 -- if K = Ada.Tags.TK_Limited_Tagged
12549 -- or else K = Ada.Tags.TK_Tagged
12550 -- then
12551 -- Lim_Typ_Stmts
12552 -- else
12553 -- Conc_Typ_Stmts
12554 -- end if;
12556 Append_To (Stmts,
12557 Make_Implicit_If_Statement (N,
12558 Condition => Build_Dispatching_Tag_Check (K, N),
12559 Then_Statements => Lim_Typ_Stmts,
12560 Else_Statements => Conc_Typ_Stmts));
12562 -- Generate:
12564 -- if B then
12565 -- <triggering-statements>
12566 -- else
12567 -- <timed-statements>
12568 -- end if;
12570 Append_To (Stmts,
12571 Make_Implicit_If_Statement (N,
12572 Condition => New_Occurrence_Of (B, Loc),
12573 Then_Statements => E_Stats,
12574 Else_Statements => D_Stats));
12576 else
12577 -- Simple case of a nondispatching trigger. Skip assignments to
12578 -- temporaries created for in-out parameters.
12580 -- This makes unwarranted assumptions about the shape of the expanded
12581 -- tree for the call, and should be cleaned up ???
12583 Stmt := First (Stmts);
12584 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12585 Next (Stmt);
12586 end loop;
12588 -- Do the assignment at this stage only because the evaluation
12589 -- of the expression must not occur before (see ACVC C97302A).
12591 Insert_Before (Stmt,
12592 Make_Assignment_Statement (Loc,
12593 Name => New_Occurrence_Of (D, Loc),
12594 Expression => D_Conv));
12596 Call := Stmt;
12597 Params := Parameter_Associations (Call);
12599 -- For a protected type, we build a Timed_Protected_Entry_Call
12601 if Is_Protected_Type (Etype (Concval)) then
12603 -- Create a new call statement
12605 Param := First (Params);
12606 while Present (Param)
12607 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12608 loop
12609 Next (Param);
12610 end loop;
12612 Dummy := Remove_Next (Next (Param));
12614 -- Remove garbage is following the Cancel_Param if present
12616 Dummy := Next (Param);
12618 -- Remove the mode of the Protected_Entry_Call call, then remove
12619 -- the Communication_Block of the Protected_Entry_Call call, and
12620 -- finally add Duration and a Delay_Mode parameter
12622 pragma Assert (Present (Param));
12623 Rewrite (Param, New_Occurrence_Of (D, Loc));
12625 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12627 -- Add a Boolean flag for successful entry call
12629 Append_To (Params, New_Occurrence_Of (B, Loc));
12631 case Corresponding_Runtime_Package (Etype (Concval)) is
12632 when System_Tasking_Protected_Objects_Entries =>
12633 Rewrite (Call,
12634 Make_Procedure_Call_Statement (Loc,
12635 Name =>
12636 New_Occurrence_Of
12637 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12638 Parameter_Associations => Params));
12640 when others =>
12641 raise Program_Error;
12642 end case;
12644 -- For the task case, build a Timed_Task_Entry_Call
12646 else
12647 -- Create a new call statement
12649 Append_To (Params, New_Occurrence_Of (D, Loc));
12650 Append_To (Params, New_Occurrence_Of (M, Loc));
12651 Append_To (Params, New_Occurrence_Of (B, Loc));
12653 Rewrite (Call,
12654 Make_Procedure_Call_Statement (Loc,
12655 Name =>
12656 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
12657 Parameter_Associations => Params));
12658 end if;
12660 Append_To (Stmts,
12661 Make_Implicit_If_Statement (N,
12662 Condition => New_Occurrence_Of (B, Loc),
12663 Then_Statements => E_Stats,
12664 Else_Statements => D_Stats));
12665 end if;
12667 Rewrite (N,
12668 Make_Block_Statement (Loc,
12669 Declarations => Decls,
12670 Handled_Statement_Sequence =>
12671 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12673 Analyze (N);
12674 end Expand_N_Timed_Entry_Call;
12676 ----------------------------------------
12677 -- Expand_Protected_Body_Declarations --
12678 ----------------------------------------
12680 procedure Expand_Protected_Body_Declarations
12681 (N : Node_Id;
12682 Spec_Id : Entity_Id)
12684 begin
12685 if No_Run_Time_Mode then
12686 Error_Msg_CRT ("protected body", N);
12687 return;
12689 elsif Expander_Active then
12691 -- Associate discriminals with the first subprogram or entry body to
12692 -- be expanded.
12694 if Present (First_Protected_Operation (Declarations (N))) then
12695 Set_Discriminals (Parent (Spec_Id));
12696 end if;
12697 end if;
12698 end Expand_Protected_Body_Declarations;
12700 -------------------------
12701 -- External_Subprogram --
12702 -------------------------
12704 function External_Subprogram (E : Entity_Id) return Entity_Id is
12705 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12707 begin
12708 -- The internal and external subprograms follow each other on the entity
12709 -- chain. Note that previously private operations had no separate
12710 -- external subprogram. We now create one in all cases, because a
12711 -- private operation may actually appear in an external call, through
12712 -- a 'Access reference used for a callback.
12714 -- If the operation is a function that returns an anonymous access type,
12715 -- the corresponding itype appears before the operation, and must be
12716 -- skipped.
12718 -- This mechanism is fragile, there should be a real link between the
12719 -- two versions of the operation, but there is no place to put it ???
12721 if Is_Access_Type (Next_Entity (Subp)) then
12722 return Next_Entity (Next_Entity (Subp));
12723 else
12724 return Next_Entity (Subp);
12725 end if;
12726 end External_Subprogram;
12728 ------------------------------
12729 -- Extract_Dispatching_Call --
12730 ------------------------------
12732 procedure Extract_Dispatching_Call
12733 (N : Node_Id;
12734 Call_Ent : out Entity_Id;
12735 Object : out Entity_Id;
12736 Actuals : out List_Id;
12737 Formals : out List_Id)
12739 Call_Nam : Node_Id;
12741 begin
12742 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12744 if Present (Original_Node (N)) then
12745 Call_Nam := Name (Original_Node (N));
12746 else
12747 Call_Nam := Name (N);
12748 end if;
12750 -- Retrieve the name of the dispatching procedure. It contains the
12751 -- dispatch table slot number.
12753 loop
12754 case Nkind (Call_Nam) is
12755 when N_Identifier =>
12756 exit;
12758 when N_Selected_Component =>
12759 Call_Nam := Selector_Name (Call_Nam);
12761 when others =>
12762 raise Program_Error;
12763 end case;
12764 end loop;
12766 Actuals := Parameter_Associations (N);
12767 Call_Ent := Entity (Call_Nam);
12768 Formals := Parameter_Specifications (Parent (Call_Ent));
12769 Object := First (Actuals);
12771 if Present (Original_Node (Object)) then
12772 Object := Original_Node (Object);
12773 end if;
12775 -- If the type of the dispatching object is an access type then return
12776 -- an explicit dereference.
12778 if Is_Access_Type (Etype (Object)) then
12779 Object := Make_Explicit_Dereference (Sloc (N), Object);
12780 Analyze (Object);
12781 end if;
12782 end Extract_Dispatching_Call;
12784 -------------------
12785 -- Extract_Entry --
12786 -------------------
12788 procedure Extract_Entry
12789 (N : Node_Id;
12790 Concval : out Node_Id;
12791 Ename : out Node_Id;
12792 Index : out Node_Id)
12794 Nam : constant Node_Id := Name (N);
12796 begin
12797 -- For a simple entry, the name is a selected component, with the
12798 -- prefix being the task value, and the selector being the entry.
12800 if Nkind (Nam) = N_Selected_Component then
12801 Concval := Prefix (Nam);
12802 Ename := Selector_Name (Nam);
12803 Index := Empty;
12805 -- For a member of an entry family, the name is an indexed component
12806 -- where the prefix is a selected component, whose prefix in turn is
12807 -- the task value, and whose selector is the entry family. The single
12808 -- expression in the expressions list of the indexed component is the
12809 -- subscript for the family.
12811 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12812 Concval := Prefix (Prefix (Nam));
12813 Ename := Selector_Name (Prefix (Nam));
12814 Index := First (Expressions (Nam));
12815 end if;
12817 -- Through indirection, the type may actually be a limited view of a
12818 -- concurrent type. When compiling a call, the non-limited view of the
12819 -- type is visible.
12821 if From_Limited_With (Etype (Concval)) then
12822 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
12823 end if;
12824 end Extract_Entry;
12826 -------------------
12827 -- Family_Offset --
12828 -------------------
12830 function Family_Offset
12831 (Loc : Source_Ptr;
12832 Hi : Node_Id;
12833 Lo : Node_Id;
12834 Ttyp : Entity_Id;
12835 Cap : Boolean) return Node_Id
12837 Ityp : Entity_Id;
12838 Real_Hi : Node_Id;
12839 Real_Lo : Node_Id;
12841 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12842 -- If one of the bounds is a reference to a discriminant, replace with
12843 -- corresponding discriminal of type. Within the body of a task retrieve
12844 -- the renamed discriminant by simple visibility, using its generated
12845 -- name. Within a protected object, find the original discriminant and
12846 -- replace it with the discriminal of the current protected operation.
12848 ------------------------------
12849 -- Convert_Discriminant_Ref --
12850 ------------------------------
12852 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
12853 Loc : constant Source_Ptr := Sloc (Bound);
12854 B : Node_Id;
12855 D : Entity_Id;
12857 begin
12858 if Is_Entity_Name (Bound)
12859 and then Ekind (Entity (Bound)) = E_Discriminant
12860 then
12861 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
12862 B := Make_Identifier (Loc, Chars (Entity (Bound)));
12863 Find_Direct_Name (B);
12865 elsif Is_Protected_Type (Ttyp) then
12866 D := First_Discriminant (Ttyp);
12867 while Chars (D) /= Chars (Entity (Bound)) loop
12868 Next_Discriminant (D);
12869 end loop;
12871 B := New_Occurrence_Of (Discriminal (D), Loc);
12873 else
12874 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
12875 end if;
12877 elsif Nkind (Bound) = N_Attribute_Reference then
12878 return Bound;
12880 else
12881 B := New_Copy_Tree (Bound);
12882 end if;
12884 return
12885 Make_Attribute_Reference (Loc,
12886 Attribute_Name => Name_Pos,
12887 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
12888 Expressions => New_List (B));
12889 end Convert_Discriminant_Ref;
12891 -- Start of processing for Family_Offset
12893 begin
12894 Real_Hi := Convert_Discriminant_Ref (Hi);
12895 Real_Lo := Convert_Discriminant_Ref (Lo);
12897 if Cap then
12898 if Is_Task_Type (Ttyp) then
12899 Ityp := RTE (RE_Task_Entry_Index);
12900 else
12901 Ityp := RTE (RE_Protected_Entry_Index);
12902 end if;
12904 Real_Hi :=
12905 Make_Attribute_Reference (Loc,
12906 Prefix => New_Occurrence_Of (Ityp, Loc),
12907 Attribute_Name => Name_Min,
12908 Expressions => New_List (
12909 Real_Hi,
12910 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
12912 Real_Lo :=
12913 Make_Attribute_Reference (Loc,
12914 Prefix => New_Occurrence_Of (Ityp, Loc),
12915 Attribute_Name => Name_Max,
12916 Expressions => New_List (
12917 Real_Lo,
12918 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
12919 end if;
12921 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
12922 end Family_Offset;
12924 -----------------
12925 -- Family_Size --
12926 -----------------
12928 function Family_Size
12929 (Loc : Source_Ptr;
12930 Hi : Node_Id;
12931 Lo : Node_Id;
12932 Ttyp : Entity_Id;
12933 Cap : Boolean) return Node_Id
12935 Ityp : Entity_Id;
12937 begin
12938 if Is_Task_Type (Ttyp) then
12939 Ityp := RTE (RE_Task_Entry_Index);
12940 else
12941 Ityp := RTE (RE_Protected_Entry_Index);
12942 end if;
12944 return
12945 Make_Attribute_Reference (Loc,
12946 Prefix => New_Occurrence_Of (Ityp, Loc),
12947 Attribute_Name => Name_Max,
12948 Expressions => New_List (
12949 Make_Op_Add (Loc,
12950 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
12951 Right_Opnd => Make_Integer_Literal (Loc, 1)),
12952 Make_Integer_Literal (Loc, 0)));
12953 end Family_Size;
12955 ----------------------------
12956 -- Find_Enclosing_Context --
12957 ----------------------------
12959 procedure Find_Enclosing_Context
12960 (N : Node_Id;
12961 Context : out Node_Id;
12962 Context_Id : out Entity_Id;
12963 Context_Decls : out List_Id)
12965 begin
12966 -- Traverse the parent chain looking for an enclosing body, block,
12967 -- package or return statement.
12969 Context := Parent (N);
12970 while Present (Context) loop
12971 if Nkind_In (Context, N_Entry_Body,
12972 N_Extended_Return_Statement,
12973 N_Package_Body,
12974 N_Package_Declaration,
12975 N_Subprogram_Body,
12976 N_Task_Body)
12977 then
12978 exit;
12980 -- Do not consider block created to protect a list of statements with
12981 -- an Abort_Defer / Abort_Undefer_Direct pair.
12983 elsif Nkind (Context) = N_Block_Statement
12984 and then not Is_Abort_Block (Context)
12985 then
12986 exit;
12987 end if;
12989 Context := Parent (Context);
12990 end loop;
12992 pragma Assert (Present (Context));
12994 -- Extract the constituents of the context
12996 if Nkind (Context) = N_Extended_Return_Statement then
12997 Context_Decls := Return_Object_Declarations (Context);
12998 Context_Id := Return_Statement_Entity (Context);
13000 -- Package declarations and bodies use a common library-level activation
13001 -- chain or task master, therefore return the package declaration as the
13002 -- proper carrier for the appropriate flag.
13004 elsif Nkind (Context) = N_Package_Body then
13005 Context_Decls := Declarations (Context);
13006 Context_Id := Corresponding_Spec (Context);
13007 Context := Parent (Context_Id);
13009 if Nkind (Context) = N_Defining_Program_Unit_Name then
13010 Context := Parent (Parent (Context));
13011 else
13012 Context := Parent (Context);
13013 end if;
13015 elsif Nkind (Context) = N_Package_Declaration then
13016 Context_Decls := Visible_Declarations (Specification (Context));
13017 Context_Id := Defining_Unit_Name (Specification (Context));
13019 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13020 Context_Id := Defining_Identifier (Context_Id);
13021 end if;
13023 else
13024 if Nkind (Context) = N_Block_Statement then
13025 Context_Id := Entity (Identifier (Context));
13027 elsif Nkind (Context) = N_Entry_Body then
13028 Context_Id := Defining_Identifier (Context);
13030 elsif Nkind (Context) = N_Subprogram_Body then
13031 if Present (Corresponding_Spec (Context)) then
13032 Context_Id := Corresponding_Spec (Context);
13033 else
13034 Context_Id := Defining_Unit_Name (Specification (Context));
13036 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13037 Context_Id := Defining_Identifier (Context_Id);
13038 end if;
13039 end if;
13041 elsif Nkind (Context) = N_Task_Body then
13042 Context_Id := Corresponding_Spec (Context);
13044 else
13045 raise Program_Error;
13046 end if;
13048 Context_Decls := Declarations (Context);
13049 end if;
13051 pragma Assert (Present (Context_Id));
13052 pragma Assert (Present (Context_Decls));
13053 end Find_Enclosing_Context;
13055 -----------------------
13056 -- Find_Master_Scope --
13057 -----------------------
13059 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13060 S : Entity_Id;
13062 begin
13063 -- In Ada 2005, the master is the innermost enclosing scope that is not
13064 -- transient. If the enclosing block is the rewriting of a call or the
13065 -- scope is an extended return statement this is valid master. The
13066 -- master in an extended return is only used within the return, and is
13067 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13068 -- now before that overwriting occurs.
13070 S := Scope (E);
13072 if Ada_Version >= Ada_2005 then
13073 while Is_Internal (S) loop
13074 if Nkind (Parent (S)) = N_Block_Statement
13075 and then
13076 Nkind (Original_Node (Parent (S))) = N_Procedure_Call_Statement
13077 then
13078 exit;
13080 elsif Ekind (S) = E_Return_Statement then
13081 exit;
13083 else
13084 S := Scope (S);
13085 end if;
13086 end loop;
13087 end if;
13089 return S;
13090 end Find_Master_Scope;
13092 -------------------------------
13093 -- First_Protected_Operation --
13094 -------------------------------
13096 function First_Protected_Operation (D : List_Id) return Node_Id is
13097 First_Op : Node_Id;
13099 begin
13100 First_Op := First (D);
13101 while Present (First_Op)
13102 and then not Nkind_In (First_Op, N_Subprogram_Body, N_Entry_Body)
13103 loop
13104 Next (First_Op);
13105 end loop;
13107 return First_Op;
13108 end First_Protected_Operation;
13110 ---------------------------------------
13111 -- Install_Private_Data_Declarations --
13112 ---------------------------------------
13114 procedure Install_Private_Data_Declarations
13115 (Loc : Source_Ptr;
13116 Spec_Id : Entity_Id;
13117 Conc_Typ : Entity_Id;
13118 Body_Nod : Node_Id;
13119 Decls : List_Id;
13120 Barrier : Boolean := False;
13121 Family : Boolean := False)
13123 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13124 Decl : Node_Id;
13125 Def : Node_Id;
13126 Insert_Node : Node_Id := Empty;
13127 Obj_Ent : Entity_Id;
13129 procedure Add (Decl : Node_Id);
13130 -- Add a single declaration after Insert_Node. If this is the first
13131 -- addition, Decl is added to the front of Decls and it becomes the
13132 -- insertion node.
13134 function Replace_Bound (Bound : Node_Id) return Node_Id;
13135 -- The bounds of an entry index may depend on discriminants, create a
13136 -- reference to the corresponding prival. Otherwise return a duplicate
13137 -- of the original bound.
13139 ---------
13140 -- Add --
13141 ---------
13143 procedure Add (Decl : Node_Id) is
13144 begin
13145 if No (Insert_Node) then
13146 Prepend_To (Decls, Decl);
13147 else
13148 Insert_After (Insert_Node, Decl);
13149 end if;
13151 Insert_Node := Decl;
13152 end Add;
13154 --------------------------
13155 -- Replace_Discriminant --
13156 --------------------------
13158 function Replace_Bound (Bound : Node_Id) return Node_Id is
13159 begin
13160 if Nkind (Bound) = N_Identifier
13161 and then Is_Discriminal (Entity (Bound))
13162 then
13163 return Make_Identifier (Loc, Chars (Entity (Bound)));
13164 else
13165 return Duplicate_Subexpr (Bound);
13166 end if;
13167 end Replace_Bound;
13169 -- Start of processing for Install_Private_Data_Declarations
13171 begin
13172 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13173 -- formal parameter _O, _object or _task depending on the context.
13175 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13177 -- Special processing of _O for barrier functions, protected entries
13178 -- and families.
13180 if Barrier
13181 or else
13182 (Is_Protected
13183 and then
13184 (Ekind (Spec_Id) = E_Entry
13185 or else Ekind (Spec_Id) = E_Entry_Family))
13186 then
13187 declare
13188 Conc_Rec : constant Entity_Id :=
13189 Corresponding_Record_Type (Conc_Typ);
13190 Typ_Id : constant Entity_Id :=
13191 Make_Defining_Identifier (Loc,
13192 New_External_Name (Chars (Conc_Rec), 'P'));
13193 begin
13194 -- Generate:
13195 -- type prot_typVP is access prot_typV;
13197 Decl :=
13198 Make_Full_Type_Declaration (Loc,
13199 Defining_Identifier => Typ_Id,
13200 Type_Definition =>
13201 Make_Access_To_Object_Definition (Loc,
13202 Subtype_Indication =>
13203 New_Occurrence_Of (Conc_Rec, Loc)));
13204 Add (Decl);
13206 -- Generate:
13207 -- _object : prot_typVP := prot_typV (_O);
13209 Decl :=
13210 Make_Object_Declaration (Loc,
13211 Defining_Identifier =>
13212 Make_Defining_Identifier (Loc, Name_uObject),
13213 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13214 Expression =>
13215 Unchecked_Convert_To (Typ_Id,
13216 New_Occurrence_Of (Obj_Ent, Loc)));
13217 Add (Decl);
13219 -- Set the reference to the concurrent object
13221 Obj_Ent := Defining_Identifier (Decl);
13222 end;
13223 end if;
13225 -- Step 2: Create the Protection object and build its declaration for
13226 -- any protected entry (family) of subprogram. Note for the lock-free
13227 -- implementation, the Protection object is not needed anymore.
13229 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13230 declare
13231 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13232 Prot_Typ : RE_Id;
13234 begin
13235 Set_Protection_Object (Spec_Id, Prot_Ent);
13237 -- Determine the proper protection type
13239 if Has_Attach_Handler (Conc_Typ)
13240 and then not Restricted_Profile
13241 then
13242 Prot_Typ := RE_Static_Interrupt_Protection;
13244 elsif Has_Interrupt_Handler (Conc_Typ)
13245 and then not Restriction_Active (No_Dynamic_Attachment)
13246 then
13247 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13249 else
13250 case Corresponding_Runtime_Package (Conc_Typ) is
13251 when System_Tasking_Protected_Objects_Entries =>
13252 Prot_Typ := RE_Protection_Entries;
13254 when System_Tasking_Protected_Objects_Single_Entry =>
13255 Prot_Typ := RE_Protection_Entry;
13257 when System_Tasking_Protected_Objects =>
13258 Prot_Typ := RE_Protection;
13260 when others =>
13261 raise Program_Error;
13262 end case;
13263 end if;
13265 -- Generate:
13266 -- conc_typR : protection_typ renames _object._object;
13268 Decl :=
13269 Make_Object_Renaming_Declaration (Loc,
13270 Defining_Identifier => Prot_Ent,
13271 Subtype_Mark =>
13272 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13273 Name =>
13274 Make_Selected_Component (Loc,
13275 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13276 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13277 Add (Decl);
13278 end;
13279 end if;
13281 -- Step 3: Add discriminant renamings (if any)
13283 if Has_Discriminants (Conc_Typ) then
13284 declare
13285 D : Entity_Id;
13287 begin
13288 D := First_Discriminant (Conc_Typ);
13289 while Present (D) loop
13291 -- Adjust the source location
13293 Set_Sloc (Discriminal (D), Loc);
13295 -- Generate:
13296 -- discr_name : discr_typ renames _object.discr_name;
13297 -- or
13298 -- discr_name : discr_typ renames _task.discr_name;
13300 Decl :=
13301 Make_Object_Renaming_Declaration (Loc,
13302 Defining_Identifier => Discriminal (D),
13303 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13304 Name =>
13305 Make_Selected_Component (Loc,
13306 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13307 Selector_Name => Make_Identifier (Loc, Chars (D))));
13308 Add (Decl);
13310 Next_Discriminant (D);
13311 end loop;
13312 end;
13313 end if;
13315 -- Step 4: Add private component renamings (if any)
13317 if Is_Protected then
13318 Def := Protected_Definition (Parent (Conc_Typ));
13320 if Present (Private_Declarations (Def)) then
13321 declare
13322 Comp : Node_Id;
13323 Comp_Id : Entity_Id;
13324 Decl_Id : Entity_Id;
13326 begin
13327 Comp := First (Private_Declarations (Def));
13328 while Present (Comp) loop
13329 if Nkind (Comp) = N_Component_Declaration then
13330 Comp_Id := Defining_Identifier (Comp);
13331 Decl_Id :=
13332 Make_Defining_Identifier (Loc, Chars (Comp_Id));
13334 -- Minimal decoration
13336 if Ekind (Spec_Id) = E_Function then
13337 Set_Ekind (Decl_Id, E_Constant);
13338 else
13339 Set_Ekind (Decl_Id, E_Variable);
13340 end if;
13342 Set_Prival (Comp_Id, Decl_Id);
13343 Set_Prival_Link (Decl_Id, Comp_Id);
13344 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13346 -- Generate:
13347 -- comp_name : comp_typ renames _object.comp_name;
13349 Decl :=
13350 Make_Object_Renaming_Declaration (Loc,
13351 Defining_Identifier => Decl_Id,
13352 Subtype_Mark =>
13353 New_Occurrence_Of (Etype (Comp_Id), Loc),
13354 Name =>
13355 Make_Selected_Component (Loc,
13356 Prefix =>
13357 New_Occurrence_Of (Obj_Ent, Loc),
13358 Selector_Name =>
13359 Make_Identifier (Loc, Chars (Comp_Id))));
13360 Add (Decl);
13361 end if;
13363 Next (Comp);
13364 end loop;
13365 end;
13366 end if;
13367 end if;
13369 -- Step 5: Add the declaration of the entry index and the associated
13370 -- type for barrier functions and entry families.
13372 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13373 declare
13374 E : constant Entity_Id := Index_Object (Spec_Id);
13375 Index : constant Entity_Id :=
13376 Defining_Identifier
13377 (Entry_Index_Specification
13378 (Entry_Body_Formal_Part (Body_Nod)));
13379 Index_Con : constant Entity_Id :=
13380 Make_Defining_Identifier (Loc, Chars (Index));
13381 High : Node_Id;
13382 Index_Typ : Entity_Id;
13383 Low : Node_Id;
13385 begin
13386 -- Minimal decoration
13388 Set_Ekind (Index_Con, E_Constant);
13389 Set_Entry_Index_Constant (Index, Index_Con);
13390 Set_Discriminal_Link (Index_Con, Index);
13392 -- Retrieve the bounds of the entry family
13394 High := Type_High_Bound (Etype (Index));
13395 Low := Type_Low_Bound (Etype (Index));
13397 -- In the simple case the entry family is given by a subtype mark
13398 -- and the index constant has the same type.
13400 if Is_Entity_Name (Original_Node (
13401 Discrete_Subtype_Definition (Parent (Index))))
13402 then
13403 Index_Typ := Etype (Index);
13405 -- Otherwise a new subtype declaration is required
13407 else
13408 High := Replace_Bound (High);
13409 Low := Replace_Bound (Low);
13411 Index_Typ := Make_Temporary (Loc, 'J');
13413 -- Generate:
13414 -- subtype Jnn is <Etype of Index> range Low .. High;
13416 Decl :=
13417 Make_Subtype_Declaration (Loc,
13418 Defining_Identifier => Index_Typ,
13419 Subtype_Indication =>
13420 Make_Subtype_Indication (Loc,
13421 Subtype_Mark =>
13422 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13423 Constraint =>
13424 Make_Range_Constraint (Loc,
13425 Range_Expression =>
13426 Make_Range (Loc, Low, High))));
13427 Add (Decl);
13428 end if;
13430 Set_Etype (Index_Con, Index_Typ);
13432 -- Create the object which designates the index:
13433 -- J : constant Jnn :=
13434 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13436 -- where Jnn is the subtype created above or the original type of
13437 -- the index, _E is a formal of the protected body subprogram and
13438 -- <index expr> is the index of the first family member.
13440 Decl :=
13441 Make_Object_Declaration (Loc,
13442 Defining_Identifier => Index_Con,
13443 Constant_Present => True,
13444 Object_Definition =>
13445 New_Occurrence_Of (Index_Typ, Loc),
13447 Expression =>
13448 Make_Attribute_Reference (Loc,
13449 Prefix =>
13450 New_Occurrence_Of (Index_Typ, Loc),
13451 Attribute_Name => Name_Val,
13453 Expressions => New_List (
13455 Make_Op_Add (Loc,
13456 Left_Opnd =>
13457 Make_Op_Subtract (Loc,
13458 Left_Opnd => New_Occurrence_Of (E, Loc),
13459 Right_Opnd =>
13460 Entry_Index_Expression (Loc,
13461 Defining_Identifier (Body_Nod),
13462 Empty, Conc_Typ)),
13464 Right_Opnd =>
13465 Make_Attribute_Reference (Loc,
13466 Prefix =>
13467 New_Occurrence_Of (Index_Typ, Loc),
13468 Attribute_Name => Name_Pos,
13469 Expressions => New_List (
13470 Make_Attribute_Reference (Loc,
13471 Prefix =>
13472 New_Occurrence_Of (Index_Typ, Loc),
13473 Attribute_Name => Name_First)))))));
13474 Add (Decl);
13475 end;
13476 end if;
13477 end Install_Private_Data_Declarations;
13479 ---------------------------------
13480 -- Is_Potentially_Large_Family --
13481 ---------------------------------
13483 function Is_Potentially_Large_Family
13484 (Base_Index : Entity_Id;
13485 Conctyp : Entity_Id;
13486 Lo : Node_Id;
13487 Hi : Node_Id) return Boolean
13489 begin
13490 return Scope (Base_Index) = Standard_Standard
13491 and then Base_Index = Base_Type (Standard_Integer)
13492 and then Has_Discriminants (Conctyp)
13493 and then
13494 Present (Discriminant_Default_Value (First_Discriminant (Conctyp)))
13495 and then
13496 (Denotes_Discriminant (Lo, True)
13497 or else
13498 Denotes_Discriminant (Hi, True));
13499 end Is_Potentially_Large_Family;
13501 -------------------------------------
13502 -- Is_Private_Primitive_Subprogram --
13503 -------------------------------------
13505 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13506 begin
13507 return
13508 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13509 and then Is_Private_Primitive (Id);
13510 end Is_Private_Primitive_Subprogram;
13512 ------------------
13513 -- Index_Object --
13514 ------------------
13516 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13517 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13518 Formal : Entity_Id;
13520 begin
13521 Formal := First_Formal (Bod_Subp);
13522 while Present (Formal) loop
13524 -- Look for formal parameter _E
13526 if Chars (Formal) = Name_uE then
13527 return Formal;
13528 end if;
13530 Next_Formal (Formal);
13531 end loop;
13533 -- A protected body subprogram should always have the parameter in
13534 -- question.
13536 raise Program_Error;
13537 end Index_Object;
13539 --------------------------------
13540 -- Make_Initialize_Protection --
13541 --------------------------------
13543 function Make_Initialize_Protection
13544 (Protect_Rec : Entity_Id) return List_Id
13546 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13547 P_Arr : Entity_Id;
13548 Pdec : Node_Id;
13549 Ptyp : constant Node_Id :=
13550 Corresponding_Concurrent_Type (Protect_Rec);
13551 Args : List_Id;
13552 L : constant List_Id := New_List;
13553 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13554 Prio_Type : Entity_Id;
13555 Prio_Var : Entity_Id := Empty;
13556 Restricted : constant Boolean := Restricted_Profile;
13558 begin
13559 -- We may need two calls to properly initialize the object, one to
13560 -- Initialize_Protection, and possibly one to Install_Handlers if we
13561 -- have a pragma Attach_Handler.
13563 -- Get protected declaration. In the case of a task type declaration,
13564 -- this is simply the parent of the protected type entity. In the single
13565 -- protected object declaration, this parent will be the implicit type,
13566 -- and we can find the corresponding single protected object declaration
13567 -- by searching forward in the declaration list in the tree.
13569 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13570 -- of this type should have been removed during semantic analysis.
13572 Pdec := Parent (Ptyp);
13573 while not Nkind_In (Pdec, N_Protected_Type_Declaration,
13574 N_Single_Protected_Declaration)
13575 loop
13576 Next (Pdec);
13577 end loop;
13579 -- Build the parameter list for the call. Note that _Init is the name
13580 -- of the formal for the object to be initialized, which is the task
13581 -- value record itself.
13583 Args := New_List;
13585 -- For lock-free implementation, skip initializations of the Protection
13586 -- object.
13588 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13590 -- Object parameter. This is a pointer to the object of type
13591 -- Protection used by the GNARL to control the protected object.
13593 Append_To (Args,
13594 Make_Attribute_Reference (Loc,
13595 Prefix =>
13596 Make_Selected_Component (Loc,
13597 Prefix => Make_Identifier (Loc, Name_uInit),
13598 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13599 Attribute_Name => Name_Unchecked_Access));
13601 -- Priority parameter. Set to Unspecified_Priority unless there is a
13602 -- Priority rep item, in which case we take the value from the pragma
13603 -- or attribute definition clause, or there is an Interrupt_Priority
13604 -- rep item and no Priority rep item, and we set the ceiling to
13605 -- Interrupt_Priority'Last, an implementation-defined value, see
13606 -- (RM D.3(10)).
13608 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13609 declare
13610 Prio_Clause : constant Node_Id :=
13611 Get_Rep_Item
13612 (Ptyp, Name_Priority, Check_Parents => False);
13614 Prio : Node_Id;
13616 begin
13617 -- Pragma Priority
13619 if Nkind (Prio_Clause) = N_Pragma then
13620 Prio :=
13621 Expression
13622 (First (Pragma_Argument_Associations (Prio_Clause)));
13624 -- Get_Rep_Item returns either priority pragma
13626 if Pragma_Name (Prio_Clause) = Name_Priority then
13627 Prio_Type := RTE (RE_Any_Priority);
13628 else
13629 Prio_Type := RTE (RE_Interrupt_Priority);
13630 end if;
13632 -- Attribute definition clause Priority
13634 else
13635 if Chars (Prio_Clause) = Name_Priority then
13636 Prio_Type := RTE (RE_Any_Priority);
13637 else
13638 Prio_Type := RTE (RE_Interrupt_Priority);
13639 end if;
13641 Prio := Expression (Prio_Clause);
13642 end if;
13644 -- Always create a locale variable to capture the priority.
13645 -- The priority is also passed to Install_Restriced_Handlers.
13646 -- Note that it is really necessary to create this variable
13647 -- explicitly. It might be thought that removing side effects
13648 -- would the appropriate approach, but that could generate
13649 -- declarations improperly placed in the enclosing scope.
13651 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13652 Append_To (L,
13653 Make_Object_Declaration (Loc,
13654 Defining_Identifier => Prio_Var,
13655 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13656 Expression => Relocate_Node (Prio)));
13658 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13659 end;
13661 -- When no priority is specified but an xx_Handler pragma is, we
13662 -- default to System.Interrupts.Default_Interrupt_Priority, see
13663 -- D.3(10).
13665 elsif Has_Attach_Handler (Ptyp)
13666 or else Has_Interrupt_Handler (Ptyp)
13667 then
13668 Append_To (Args,
13669 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
13671 -- Normal case, no priority or xx_Handler specified, default priority
13673 else
13674 Append_To (Args,
13675 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
13676 end if;
13678 -- Deadline_Floor parameter for GNAT_Ravenscar_EDF runtimes
13680 if Restricted_Profile and Task_Dispatching_Policy = 'E' then
13681 Deadline_Floor : declare
13682 Item : constant Node_Id :=
13683 Get_Rep_Item
13684 (Ptyp, Name_Deadline_Floor, Check_Parents => False);
13686 Deadline : Node_Id;
13688 begin
13689 if Present (Item) then
13691 -- Pragma Deadline_Floor
13693 if Nkind (Item) = N_Pragma then
13694 Deadline :=
13695 Expression
13696 (First (Pragma_Argument_Associations (Item)));
13698 -- Attribute definition clause Deadline_Floor
13700 else
13701 pragma Assert
13702 (Nkind (Item) = N_Attribute_Definition_Clause);
13704 Deadline := Expression (Item);
13705 end if;
13707 Append_To (Args, Deadline);
13709 -- Unusual case: default deadline
13711 else
13712 Append_To (Args,
13713 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
13714 end if;
13715 end Deadline_Floor;
13716 end if;
13718 -- Test for Compiler_Info parameter. This parameter allows entry body
13719 -- procedures and barrier functions to be called from the runtime. It
13720 -- is a pointer to the record generated by the compiler to represent
13721 -- the protected object.
13723 -- A protected type without entries that covers an interface and
13724 -- overrides the abstract routines with protected procedures is
13725 -- considered equivalent to a protected type with entries in the
13726 -- context of dispatching select statements.
13728 -- Protected types with interrupt handlers (when not using a
13729 -- restricted profile) are also considered equivalent to protected
13730 -- types with entries.
13732 -- The types which are used (Static_Interrupt_Protection and
13733 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13735 declare
13736 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13738 Called_Subp : RE_Id;
13740 begin
13741 case Pkg_Id is
13742 when System_Tasking_Protected_Objects_Entries =>
13743 Called_Subp := RE_Initialize_Protection_Entries;
13745 -- Argument Compiler_Info
13747 Append_To (Args,
13748 Make_Attribute_Reference (Loc,
13749 Prefix => Make_Identifier (Loc, Name_uInit),
13750 Attribute_Name => Name_Address));
13752 when System_Tasking_Protected_Objects_Single_Entry =>
13753 Called_Subp := RE_Initialize_Protection_Entry;
13755 -- Argument Compiler_Info
13757 Append_To (Args,
13758 Make_Attribute_Reference (Loc,
13759 Prefix => Make_Identifier (Loc, Name_uInit),
13760 Attribute_Name => Name_Address));
13762 when System_Tasking_Protected_Objects =>
13763 Called_Subp := RE_Initialize_Protection;
13765 when others =>
13766 raise Program_Error;
13767 end case;
13769 -- Entry_Queue_Maxes parameter. This is an access to an array of
13770 -- naturals representing the entry queue maximums for each entry
13771 -- in the protected type. Zero represents no max. The access is
13772 -- null if there is no limit for all entries (usual case).
13774 if Has_Entry
13775 and then Pkg_Id = System_Tasking_Protected_Objects_Entries
13776 then
13777 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
13778 Append_To (Args,
13779 Make_Attribute_Reference (Loc,
13780 Prefix =>
13781 New_Occurrence_Of
13782 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
13783 Attribute_Name => Name_Unrestricted_Access));
13784 else
13785 Append_To (Args, Make_Null (Loc));
13786 end if;
13788 -- Edge cases exist where entry initialization functions are
13789 -- called, but no entries exist, so null is appended.
13791 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13792 Append_To (Args, Make_Null (Loc));
13793 end if;
13795 -- Entry_Bodies parameter. This is a pointer to an array of
13796 -- pointers to the entry body procedures and barrier functions of
13797 -- the object. If the protected type has no entries this object
13798 -- will not exist, in this case, pass a null (it can happen when
13799 -- there are protected interrupt handlers or interfaces).
13801 if Has_Entry then
13802 P_Arr := Entry_Bodies_Array (Ptyp);
13804 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
13805 -- multiple entries).
13807 Append_To (Args,
13808 Make_Attribute_Reference (Loc,
13809 Prefix => New_Occurrence_Of (P_Arr, Loc),
13810 Attribute_Name => Name_Unrestricted_Access));
13812 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13814 -- Find index mapping function (clumsy but ok for now)
13816 while Ekind (P_Arr) /= E_Function loop
13817 Next_Entity (P_Arr);
13818 end loop;
13820 Append_To (Args,
13821 Make_Attribute_Reference (Loc,
13822 Prefix => New_Occurrence_Of (P_Arr, Loc),
13823 Attribute_Name => Name_Unrestricted_Access));
13824 end if;
13826 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
13828 -- This is the case where we have a protected object with
13829 -- interfaces and no entries, and the single entry restriction
13830 -- is in effect. We pass a null pointer for the entry
13831 -- parameter because there is no actual entry.
13833 Append_To (Args, Make_Null (Loc));
13835 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13837 -- This is the case where we have a protected object with no
13838 -- entries and:
13839 -- - either interrupt handlers with non restricted profile,
13840 -- - or interfaces
13841 -- Note that the types which are used for interrupt handlers
13842 -- (Static/Dynamic_Interrupt_Protection) are derived from
13843 -- Protection_Entries. We pass two null pointers because there
13844 -- is no actual entry, and the initialization procedure needs
13845 -- both Entry_Bodies and Find_Body_Index.
13847 Append_To (Args, Make_Null (Loc));
13848 Append_To (Args, Make_Null (Loc));
13849 end if;
13851 Append_To (L,
13852 Make_Procedure_Call_Statement (Loc,
13853 Name =>
13854 New_Occurrence_Of (RTE (Called_Subp), Loc),
13855 Parameter_Associations => Args));
13856 end;
13857 end if;
13859 if Has_Attach_Handler (Ptyp) then
13861 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
13862 -- make the following call:
13864 -- Install_Handlers (_object,
13865 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
13867 -- or, in the case of Ravenscar:
13869 -- Install_Restricted_Handlers
13870 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
13872 declare
13873 Args : constant List_Id := New_List;
13874 Table : constant List_Id := New_List;
13875 Ritem : Node_Id := First_Rep_Item (Ptyp);
13877 begin
13878 -- Build the Priority parameter (only for ravenscar)
13880 if Restricted then
13882 -- Priority comes from a pragma
13884 if Present (Prio_Var) then
13885 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13887 -- Priority is the default one
13889 else
13890 Append_To (Args,
13891 New_Occurrence_Of
13892 (RTE (RE_Default_Interrupt_Priority), Loc));
13893 end if;
13894 end if;
13896 -- Build the Attach_Handler table argument
13898 while Present (Ritem) loop
13899 if Nkind (Ritem) = N_Pragma
13900 and then Pragma_Name (Ritem) = Name_Attach_Handler
13901 then
13902 declare
13903 Handler : constant Node_Id :=
13904 First (Pragma_Argument_Associations (Ritem));
13906 Interrupt : constant Node_Id := Next (Handler);
13907 Expr : constant Node_Id := Expression (Interrupt);
13909 begin
13910 Append_To (Table,
13911 Make_Aggregate (Loc, Expressions => New_List (
13912 Unchecked_Convert_To
13913 (RTE (RE_System_Interrupt_Id), Expr),
13914 Make_Attribute_Reference (Loc,
13915 Prefix =>
13916 Make_Selected_Component (Loc,
13917 Prefix =>
13918 Make_Identifier (Loc, Name_uInit),
13919 Selector_Name =>
13920 Duplicate_Subexpr_No_Checks
13921 (Expression (Handler))),
13922 Attribute_Name => Name_Access))));
13923 end;
13924 end if;
13926 Next_Rep_Item (Ritem);
13927 end loop;
13929 -- Append the table argument we just built
13931 Append_To (Args, Make_Aggregate (Loc, Table));
13933 -- Append the Install_Handlers (or Install_Restricted_Handlers)
13934 -- call to the statements.
13936 if Restricted then
13937 -- Call a simplified version of Install_Handlers to be used
13938 -- when the Ravenscar restrictions are in effect
13939 -- (Install_Restricted_Handlers).
13941 Append_To (L,
13942 Make_Procedure_Call_Statement (Loc,
13943 Name =>
13944 New_Occurrence_Of
13945 (RTE (RE_Install_Restricted_Handlers), Loc),
13946 Parameter_Associations => Args));
13948 else
13949 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13951 -- First, prepends the _object argument
13953 Prepend_To (Args,
13954 Make_Attribute_Reference (Loc,
13955 Prefix =>
13956 Make_Selected_Component (Loc,
13957 Prefix => Make_Identifier (Loc, Name_uInit),
13958 Selector_Name =>
13959 Make_Identifier (Loc, Name_uObject)),
13960 Attribute_Name => Name_Unchecked_Access));
13961 end if;
13963 -- Then, insert call to Install_Handlers
13965 Append_To (L,
13966 Make_Procedure_Call_Statement (Loc,
13967 Name =>
13968 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
13969 Parameter_Associations => Args));
13970 end if;
13971 end;
13972 end if;
13974 return L;
13975 end Make_Initialize_Protection;
13977 ---------------------------
13978 -- Make_Task_Create_Call --
13979 ---------------------------
13981 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
13982 Loc : constant Source_Ptr := Sloc (Task_Rec);
13983 Args : List_Id;
13984 Ecount : Node_Id;
13985 Name : Node_Id;
13986 Tdec : Node_Id;
13987 Tdef : Node_Id;
13988 Tnam : Name_Id;
13989 Ttyp : Node_Id;
13991 begin
13992 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
13993 Tnam := Chars (Ttyp);
13995 -- Get task declaration. In the case of a task type declaration, this is
13996 -- simply the parent of the task type entity. In the single task
13997 -- declaration, this parent will be the implicit type, and we can find
13998 -- the corresponding single task declaration by searching forward in the
13999 -- declaration list in the tree.
14001 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14002 -- this type should have been removed during semantic analysis.
14004 Tdec := Parent (Ttyp);
14005 while not Nkind_In (Tdec, N_Task_Type_Declaration,
14006 N_Single_Task_Declaration)
14007 loop
14008 Next (Tdec);
14009 end loop;
14011 -- Now we can find the task definition from this declaration
14013 Tdef := Task_Definition (Tdec);
14015 -- Build the parameter list for the call. Note that _Init is the name
14016 -- of the formal for the object to be initialized, which is the task
14017 -- value record itself.
14019 Args := New_List;
14021 -- Priority parameter. Set to Unspecified_Priority unless there is a
14022 -- Priority rep item, in which case we take the value from the rep item.
14023 -- Not used on Ravenscar_EDF profile.
14025 if not (Restricted_Profile and then Task_Dispatching_Policy = 'E') then
14026 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14027 Append_To (Args,
14028 Make_Selected_Component (Loc,
14029 Prefix => Make_Identifier (Loc, Name_uInit),
14030 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14031 else
14032 Append_To (Args,
14033 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14034 end if;
14035 end if;
14037 -- Optional Stack parameter
14039 if Restricted_Profile then
14041 -- If the stack has been preallocated by the expander then
14042 -- pass its address. Otherwise, pass a null address.
14044 if Preallocated_Stacks_On_Target then
14045 Append_To (Args,
14046 Make_Attribute_Reference (Loc,
14047 Prefix =>
14048 Make_Selected_Component (Loc,
14049 Prefix => Make_Identifier (Loc, Name_uInit),
14050 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14051 Attribute_Name => Name_Address));
14053 else
14054 Append_To (Args,
14055 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14056 end if;
14057 end if;
14059 -- Size parameter. If no Storage_Size pragma is present, then
14060 -- the size is taken from the taskZ variable for the type, which
14061 -- is either Unspecified_Size, or has been reset by the use of
14062 -- a Storage_Size attribute definition clause. If a pragma is
14063 -- present, then the size is taken from the _Size field of the
14064 -- task value record, which was set from the pragma value.
14066 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14067 Append_To (Args,
14068 Make_Selected_Component (Loc,
14069 Prefix => Make_Identifier (Loc, Name_uInit),
14070 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14072 else
14073 Append_To (Args,
14074 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14075 end if;
14077 -- Secondary_Stack_Size parameter. Set Default_Secondary_Stack_Size
14078 -- unless there is a Secondary_Stack_Size rep item, in which case we
14079 -- take the value from the rep item. If the restriction
14080 -- No_Secondary_Stack is active then a size of 0 is passed regardless
14081 -- to prevent the allocation of the unused stack.
14083 if Restriction_Active (No_Secondary_Stack) then
14084 Append_To (Args, Make_Integer_Literal (Loc, 0));
14086 elsif Has_Rep_Item
14087 (Ttyp, Name_Secondary_Stack_Size, Check_Parents => False)
14088 then
14089 Append_To (Args,
14090 Make_Selected_Component (Loc,
14091 Prefix => Make_Identifier (Loc, Name_uInit),
14092 Selector_Name =>
14093 Make_Identifier (Loc, Name_uSecondary_Stack_Size)));
14095 else
14096 Append_To (Args,
14097 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
14098 end if;
14100 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14101 -- Task_Info pragma, in which case we take the value from the pragma.
14103 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14104 Append_To (Args,
14105 Make_Selected_Component (Loc,
14106 Prefix => Make_Identifier (Loc, Name_uInit),
14107 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14109 else
14110 Append_To (Args,
14111 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14112 end if;
14114 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14115 -- in which case we take the value from the rep item. The parameter is
14116 -- passed as an Integer because in the case of unspecified CPU the
14117 -- value is not in the range of CPU_Range.
14119 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14120 Append_To (Args,
14121 Convert_To (Standard_Integer,
14122 Make_Selected_Component (Loc,
14123 Prefix => Make_Identifier (Loc, Name_uInit),
14124 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14125 else
14126 Append_To (Args,
14127 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14128 end if;
14130 if not Restricted_Profile or else Task_Dispatching_Policy = 'E' then
14132 -- Deadline parameter. If no Relative_Deadline pragma is present,
14133 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14134 -- the deadline is taken from the _Relative_Deadline field of the
14135 -- task value record, which was set from the pragma value. Note that
14136 -- this parameter must not be generated for the restricted profiles
14137 -- since Ravenscar does not allow deadlines.
14139 -- Case where pragma Relative_Deadline applies: use given value
14141 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14142 Append_To (Args,
14143 Make_Selected_Component (Loc,
14144 Prefix => Make_Identifier (Loc, Name_uInit),
14145 Selector_Name =>
14146 Make_Identifier (Loc, Name_uRelative_Deadline)));
14148 -- No pragma Relative_Deadline apply to the task
14150 else
14151 Append_To (Args,
14152 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14153 end if;
14154 end if;
14156 if not Restricted_Profile then
14158 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14159 -- present, then the dispatching domain is null. If a rep item is
14160 -- present, then the dispatching domain is taken from the
14161 -- _Dispatching_Domain field of the task value record, which was set
14162 -- from the rep item value.
14164 -- Case where Dispatching_Domain rep item applies: use given value
14166 if Has_Rep_Item
14167 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14168 then
14169 Append_To (Args,
14170 Make_Selected_Component (Loc,
14171 Prefix =>
14172 Make_Identifier (Loc, Name_uInit),
14173 Selector_Name =>
14174 Make_Identifier (Loc, Name_uDispatching_Domain)));
14176 -- No pragma or aspect Dispatching_Domain applies to the task
14178 else
14179 Append_To (Args, Make_Null (Loc));
14180 end if;
14182 -- Number of entries. This is an expression of the form:
14184 -- n + _Init.a'Length + _Init.a'B'Length + ...
14186 -- where a,b... are the entry family names for the task definition
14188 Ecount :=
14189 Build_Entry_Count_Expression
14190 (Ttyp,
14191 Component_Items
14192 (Component_List
14193 (Type_Definition
14194 (Parent (Corresponding_Record_Type (Ttyp))))),
14195 Loc);
14196 Append_To (Args, Ecount);
14198 -- Master parameter. This is a reference to the _Master parameter of
14199 -- the initialization procedure, except in the case of the pragma
14200 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14201 -- System.Tasking.Library_Task_Level.
14203 if Restriction_Active (No_Task_Hierarchy) = False then
14204 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14205 else
14206 Append_To (Args,
14207 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
14208 end if;
14209 end if;
14211 -- State parameter. This is a pointer to the task body procedure. The
14212 -- required value is obtained by taking 'Unrestricted_Access of the task
14213 -- body procedure and converting it (with an unchecked conversion) to
14214 -- the type required by the task kernel. For further details, see the
14215 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14216 -- than 'Address in order to avoid creating trampolines.
14218 declare
14219 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14220 Subp_Ptr_Typ : constant Node_Id :=
14221 Create_Itype (E_Access_Subprogram_Type, Tdec);
14222 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14224 begin
14225 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14226 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14228 -- Be sure to freeze a reference to the access-to-subprogram type,
14229 -- otherwise gigi will complain that it's in the wrong scope, because
14230 -- it's actually inside the init procedure for the record type that
14231 -- corresponds to the task type.
14233 Set_Itype (Ref, Subp_Ptr_Typ);
14234 Append_Freeze_Action (Task_Rec, Ref);
14236 Append_To (Args,
14237 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14238 Make_Qualified_Expression (Loc,
14239 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14240 Expression =>
14241 Make_Attribute_Reference (Loc,
14242 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14243 Attribute_Name => Name_Unrestricted_Access))));
14244 end;
14246 -- Discriminants parameter. This is just the address of the task
14247 -- value record itself (which contains the discriminant values
14249 Append_To (Args,
14250 Make_Attribute_Reference (Loc,
14251 Prefix => Make_Identifier (Loc, Name_uInit),
14252 Attribute_Name => Name_Address));
14254 -- Elaborated parameter. This is an access to the elaboration Boolean
14256 Append_To (Args,
14257 Make_Attribute_Reference (Loc,
14258 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14259 Attribute_Name => Name_Unchecked_Access));
14261 -- Add Chain parameter (not done for sequential elaboration policy, see
14262 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14264 if Partition_Elaboration_Policy /= 'S' then
14265 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14266 end if;
14268 -- Task name parameter. Take this from the _Task_Id parameter to the
14269 -- init call unless there is a Task_Name pragma, in which case we take
14270 -- the value from the pragma.
14272 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14273 -- Copy expression in full, because it may be dynamic and have
14274 -- side effects.
14276 Append_To (Args,
14277 New_Copy_Tree
14278 (Expression
14279 (First
14280 (Pragma_Argument_Associations
14281 (Get_Rep_Pragma
14282 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14284 else
14285 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14286 end if;
14288 -- Created_Task parameter. This is the _Task_Id field of the task
14289 -- record value
14291 Append_To (Args,
14292 Make_Selected_Component (Loc,
14293 Prefix => Make_Identifier (Loc, Name_uInit),
14294 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14296 declare
14297 Create_RE : RE_Id;
14299 begin
14300 if Restricted_Profile then
14301 if Partition_Elaboration_Policy = 'S' then
14302 Create_RE := RE_Create_Restricted_Task_Sequential;
14303 else
14304 Create_RE := RE_Create_Restricted_Task;
14305 end if;
14306 else
14307 Create_RE := RE_Create_Task;
14308 end if;
14310 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14311 end;
14313 return
14314 Make_Procedure_Call_Statement (Loc,
14315 Name => Name,
14316 Parameter_Associations => Args);
14317 end Make_Task_Create_Call;
14319 ------------------------------
14320 -- Next_Protected_Operation --
14321 ------------------------------
14323 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14324 Next_Op : Node_Id;
14326 begin
14327 -- Check whether there is a subsequent body for a protected operation
14328 -- in the current protected body. In Ada2012 that includes expression
14329 -- functions that are completions.
14331 Next_Op := Next (N);
14332 while Present (Next_Op)
14333 and then not Nkind_In (Next_Op,
14334 N_Subprogram_Body, N_Entry_Body, N_Expression_Function)
14335 loop
14336 Next (Next_Op);
14337 end loop;
14339 return Next_Op;
14340 end Next_Protected_Operation;
14342 ---------------------
14343 -- Null_Statements --
14344 ---------------------
14346 function Null_Statements (Stats : List_Id) return Boolean is
14347 Stmt : Node_Id;
14349 begin
14350 Stmt := First (Stats);
14351 while Nkind (Stmt) /= N_Empty
14352 and then (Nkind_In (Stmt, N_Null_Statement, N_Label)
14353 or else
14354 (Nkind (Stmt) = N_Pragma
14355 and then
14356 Nam_In (Pragma_Name_Unmapped (Stmt),
14357 Name_Unreferenced,
14358 Name_Unmodified,
14359 Name_Warnings)))
14360 loop
14361 Next (Stmt);
14362 end loop;
14364 return Nkind (Stmt) = N_Empty;
14365 end Null_Statements;
14367 --------------------------
14368 -- Parameter_Block_Pack --
14369 --------------------------
14371 function Parameter_Block_Pack
14372 (Loc : Source_Ptr;
14373 Blk_Typ : Entity_Id;
14374 Actuals : List_Id;
14375 Formals : List_Id;
14376 Decls : List_Id;
14377 Stmts : List_Id) return Node_Id
14379 Actual : Entity_Id;
14380 Expr : Node_Id := Empty;
14381 Formal : Entity_Id;
14382 Has_Param : Boolean := False;
14383 P : Entity_Id;
14384 Params : List_Id;
14385 Temp_Asn : Node_Id;
14386 Temp_Nam : Node_Id;
14388 begin
14389 Actual := First (Actuals);
14390 Formal := Defining_Identifier (First (Formals));
14391 Params := New_List;
14392 while Present (Actual) loop
14393 if Is_By_Copy_Type (Etype (Actual)) then
14394 -- Generate:
14395 -- Jnn : aliased <formal-type>
14397 Temp_Nam := Make_Temporary (Loc, 'J');
14399 Append_To (Decls,
14400 Make_Object_Declaration (Loc,
14401 Aliased_Present => True,
14402 Defining_Identifier => Temp_Nam,
14403 Object_Definition =>
14404 New_Occurrence_Of (Etype (Formal), Loc)));
14406 if Ekind (Formal) /= E_Out_Parameter then
14408 -- Generate:
14409 -- Jnn := <actual>
14411 Temp_Asn :=
14412 New_Occurrence_Of (Temp_Nam, Loc);
14414 Set_Assignment_OK (Temp_Asn);
14416 Append_To (Stmts,
14417 Make_Assignment_Statement (Loc,
14418 Name => Temp_Asn,
14419 Expression => New_Copy_Tree (Actual)));
14420 end if;
14422 -- Generate:
14423 -- Jnn'unchecked_access
14425 Append_To (Params,
14426 Make_Attribute_Reference (Loc,
14427 Attribute_Name => Name_Unchecked_Access,
14428 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14430 Has_Param := True;
14432 -- The controlling parameter is omitted
14434 else
14435 if not Is_Controlling_Actual (Actual) then
14436 Append_To (Params,
14437 Make_Reference (Loc, New_Copy_Tree (Actual)));
14439 Has_Param := True;
14440 end if;
14441 end if;
14443 Next_Actual (Actual);
14444 Next_Formal_With_Extras (Formal);
14445 end loop;
14447 if Has_Param then
14448 Expr := Make_Aggregate (Loc, Params);
14449 end if;
14451 -- Generate:
14452 -- P : Ann := (
14453 -- J1'unchecked_access;
14454 -- <actual2>'reference;
14455 -- ...);
14457 P := Make_Temporary (Loc, 'P');
14459 Append_To (Decls,
14460 Make_Object_Declaration (Loc,
14461 Defining_Identifier => P,
14462 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14463 Expression => Expr));
14465 return P;
14466 end Parameter_Block_Pack;
14468 ----------------------------
14469 -- Parameter_Block_Unpack --
14470 ----------------------------
14472 function Parameter_Block_Unpack
14473 (Loc : Source_Ptr;
14474 P : Entity_Id;
14475 Actuals : List_Id;
14476 Formals : List_Id) return List_Id
14478 Actual : Entity_Id;
14479 Asnmt : Node_Id;
14480 Formal : Entity_Id;
14481 Has_Asnmt : Boolean := False;
14482 Result : constant List_Id := New_List;
14484 begin
14485 Actual := First (Actuals);
14486 Formal := Defining_Identifier (First (Formals));
14487 while Present (Actual) loop
14488 if Is_By_Copy_Type (Etype (Actual))
14489 and then Ekind (Formal) /= E_In_Parameter
14490 then
14491 -- Generate:
14492 -- <actual> := P.<formal>;
14494 Asnmt :=
14495 Make_Assignment_Statement (Loc,
14496 Name =>
14497 New_Copy (Actual),
14498 Expression =>
14499 Make_Explicit_Dereference (Loc,
14500 Make_Selected_Component (Loc,
14501 Prefix =>
14502 New_Occurrence_Of (P, Loc),
14503 Selector_Name =>
14504 Make_Identifier (Loc, Chars (Formal)))));
14506 Set_Assignment_OK (Name (Asnmt));
14507 Append_To (Result, Asnmt);
14509 Has_Asnmt := True;
14510 end if;
14512 Next_Actual (Actual);
14513 Next_Formal_With_Extras (Formal);
14514 end loop;
14516 if Has_Asnmt then
14517 return Result;
14518 else
14519 return New_List (Make_Null_Statement (Loc));
14520 end if;
14521 end Parameter_Block_Unpack;
14523 ----------------------
14524 -- Set_Discriminals --
14525 ----------------------
14527 procedure Set_Discriminals (Dec : Node_Id) is
14528 D : Entity_Id;
14529 Pdef : Entity_Id;
14530 D_Minal : Entity_Id;
14532 begin
14533 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14534 Pdef := Defining_Identifier (Dec);
14536 if Has_Discriminants (Pdef) then
14537 D := First_Discriminant (Pdef);
14538 while Present (D) loop
14539 D_Minal :=
14540 Make_Defining_Identifier (Sloc (D),
14541 Chars => New_External_Name (Chars (D), 'D'));
14543 Set_Ekind (D_Minal, E_Constant);
14544 Set_Etype (D_Minal, Etype (D));
14545 Set_Scope (D_Minal, Pdef);
14546 Set_Discriminal (D, D_Minal);
14547 Set_Discriminal_Link (D_Minal, D);
14549 Next_Discriminant (D);
14550 end loop;
14551 end if;
14552 end Set_Discriminals;
14554 -----------------------
14555 -- Trivial_Accept_OK --
14556 -----------------------
14558 function Trivial_Accept_OK return Boolean is
14559 begin
14560 case Opt.Task_Dispatching_Policy is
14562 -- If we have the default task dispatching policy in effect, we can
14563 -- definitely do the optimization (one way of looking at this is to
14564 -- think of the formal definition of the default policy being allowed
14565 -- to run any task it likes after a rendezvous, so even if notionally
14566 -- a full rescheduling occurs, we can say that our dispatching policy
14567 -- (i.e. the default dispatching policy) reorders the queue to be the
14568 -- same as just before the call.
14570 when ' ' =>
14571 return True;
14573 -- FIFO_Within_Priorities certainly does not permit this
14574 -- optimization since the Rendezvous is a scheduling action that may
14575 -- require some other task to be run.
14577 when 'F' =>
14578 return False;
14580 -- For now, disallow the optimization for all other policies. This
14581 -- may be over-conservative, but it is certainly not incorrect.
14583 when others =>
14584 return False;
14585 end case;
14586 end Trivial_Accept_OK;
14588 end Exp_Ch9;