Fix Ada runtime library breakage on Solaris
[official-gcc.git] / gcc / ada / exp_ch9.adb
blob051b1df060f89c89f806a7ef9b26d8f2f8d63458
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-2024, 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 Accessibility; use Accessibility;
27 with Atree; use Atree;
28 with Aspects; use Aspects;
29 with Checks; use Checks;
30 with Contracts; use Contracts;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Ch3; use Exp_Ch3;
37 with Exp_Ch6; use Exp_Ch6;
38 with Exp_Ch11; use Exp_Ch11;
39 with Exp_Dbug; use Exp_Dbug;
40 with Exp_Sel; use Exp_Sel;
41 with Exp_Smem; use Exp_Smem;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Hostparm;
45 with Itypes; use Itypes;
46 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
49 with Opt; use Opt;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Ch5; use Sem_Ch5;
56 with Sem_Ch6; use Sem_Ch6;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch9; use Sem_Ch9;
59 with Sem_Ch11; use Sem_Ch11;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Res; use Sem_Res;
64 with Sem_Util; use Sem_Util;
65 with Sinfo; use Sinfo;
66 with Sinfo.Nodes; use Sinfo.Nodes;
67 with Sinfo.Utils; use Sinfo.Utils;
68 with Snames; use Snames;
69 with Stand; use Stand;
70 with Targparm; use Targparm;
71 with Tbuild; use Tbuild;
72 with Uintp; use Uintp;
73 with Validsw; use Validsw;
75 package body Exp_Ch9 is
77 -- The following constant establishes the upper bound for the index of
78 -- an entry family. It is used to limit the allocated size of protected
79 -- types with defaulted discriminant of an integer type, when the bound
80 -- of some entry family depends on a discriminant. The limitation to entry
81 -- families of 128K should be reasonable in all cases, and is a documented
82 -- implementation restriction.
84 Entry_Family_Bound : constant Pos := 2**16;
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
90 function Actual_Index_Expression
91 (Sloc : Source_Ptr;
92 Ent : Entity_Id;
93 Index : Node_Id;
94 Tsk : Entity_Id) return Node_Id;
95 -- Compute the index position for an entry call. Tsk is the target task. If
96 -- the bounds of some entry family depend on discriminants, the expression
97 -- computed by this function uses the discriminants of the target task.
99 procedure Add_Object_Pointer
100 (Loc : Source_Ptr;
101 Conc_Typ : Entity_Id;
102 Decls : List_Id);
103 -- Prepend an object pointer declaration to the declaration list Decls.
104 -- This object pointer is initialized to a type conversion of the System.
105 -- Address pointer passed to entry barrier functions and entry body
106 -- procedures.
108 procedure Add_Formal_Renamings
109 (Spec : Node_Id;
110 Decls : List_Id;
111 Ent : Entity_Id;
112 Loc : Source_Ptr);
113 -- Create renaming declarations for the formals, inside the procedure that
114 -- implements an entry body. The renamings make the original names of the
115 -- formals accessible to gdb, and serve no other purpose.
116 -- Spec is the specification of the procedure being built.
117 -- Decls is the list of declarations to be enhanced.
118 -- Ent is the entity for the original entry body.
120 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
121 -- Transform accept statement into a block with added exception handler.
122 -- Used both for simple accept statements and for accept alternatives in
123 -- select statements. Astat is the accept statement.
125 function Build_Barrier_Function
126 (N : Node_Id;
127 Ent : Entity_Id;
128 Pid : Entity_Id) return Node_Id;
129 -- Build the function body returning the value of the barrier expression
130 -- for the specified entry body.
132 function Build_Barrier_Function_Specification
133 (Loc : Source_Ptr;
134 Def_Id : Entity_Id) return Node_Id;
135 -- Build a specification for a function implementing the protected entry
136 -- barrier of the specified entry body.
138 function Build_Corresponding_Record
139 (N : Node_Id;
140 Ctyp : Entity_Id;
141 Loc : Source_Ptr) return Node_Id;
142 -- Common to tasks and protected types. Copy discriminant specifications,
143 -- build record declaration. N is the type declaration, Ctyp is the
144 -- concurrent entity (task type or protected type).
146 function Build_Dispatching_Tag_Check
147 (K : Entity_Id;
148 N : Node_Id) return Node_Id;
149 -- Utility to create the tree to check whether the dispatching call in
150 -- a timed entry call, a conditional entry call, or an asynchronous
151 -- transfer of control is a call to a primitive of a non-synchronized type.
152 -- K is the temporary that holds the tagged kind of the target object, and
153 -- N is the enclosing construct.
155 function Build_Entry_Count_Expression
156 (Concurrent_Type : Entity_Id;
157 Loc : Source_Ptr) return Node_Id;
158 -- Compute number of entries for concurrent object. This is a count of
159 -- simple entries, followed by an expression that computes the length
160 -- of the range of each entry family. A single array with that size is
161 -- allocated for each concurrent object of the type.
163 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
164 -- Build the function that translates the entry index in the call
165 -- (which depends on the size of entry families) into an index into the
166 -- Entry_Bodies_Array, to determine the body and barrier function used
167 -- in a protected entry call. A pointer to this function appears in every
168 -- protected object.
170 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
171 -- Build subprogram declaration for previous one
173 function Build_Lock_Free_Protected_Subprogram_Body
174 (N : Node_Id;
175 Prot_Typ : Node_Id;
176 Unprot_Spec : Node_Id) return Node_Id;
177 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
178 -- the subprogram specification of the unprotected version of N. Transform
179 -- N such that it invokes the unprotected version of the body.
181 function Build_Lock_Free_Unprotected_Subprogram_Body
182 (N : Node_Id;
183 Prot_Typ : Node_Id) return Node_Id;
184 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
185 -- of N where the original statements of N are synchronized through atomic
186 -- actions such as compare and exchange. Prior to invoking this routine, it
187 -- has been established that N can be implemented in a lock-free fashion.
189 function Build_Parameter_Block
190 (Loc : Source_Ptr;
191 Actuals : List_Id;
192 Formals : List_Id;
193 Decls : List_Id) return Entity_Id;
194 -- Generate an access type for each actual parameter in the list Actuals.
195 -- Create an encapsulating record that contains all the actuals and return
196 -- its type. Generate:
197 -- type Ann1 is access all <actual1-type>
198 -- ...
199 -- type AnnN is access all <actualN-type>
200 -- type Pnn is record
201 -- <formal1> : Ann1;
202 -- ...
203 -- <formalN> : AnnN;
204 -- end record;
206 function Build_Protected_Entry
207 (N : Node_Id;
208 Ent : Entity_Id;
209 Pid : Node_Id) return Node_Id;
210 -- Build the procedure implementing the statement sequence of the specified
211 -- entry body.
213 function Build_Protected_Entry_Specification
214 (Loc : Source_Ptr;
215 Def_Id : Entity_Id;
216 Ent_Id : Entity_Id) return Node_Id;
217 -- Build a specification for the procedure implementing the statements of
218 -- the specified entry body. Add attributes associating it with the entry
219 -- defining identifier Ent_Id.
221 function Build_Protected_Spec
222 (N : Node_Id;
223 Obj_Type : Entity_Id;
224 Ident : Entity_Id;
225 Unprotected : Boolean := False) return List_Id;
226 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
227 -- Subprogram_Type. Builds signature of protected subprogram, adding the
228 -- formal that corresponds to the object itself. For an access to protected
229 -- subprogram, there is no object type to specify, so the parameter has
230 -- type Address and mode In. An indirect call through such a pointer will
231 -- convert the address to a reference to the actual object. The object is
232 -- a limited record and therefore a by_reference type.
234 function Build_Protected_Subprogram_Body
235 (N : Node_Id;
236 Pid : Node_Id;
237 N_Op_Spec : Node_Id) return Node_Id;
238 -- This function is used to construct the protected version of a protected
239 -- subprogram. Its statement sequence first defers abort, then locks the
240 -- associated protected object, and then enters a block that contains a
241 -- call to the unprotected version of the subprogram (for details, see
242 -- Build_Unprotected_Subprogram_Body). This block statement requires a
243 -- cleanup handler that unlocks the object in all cases. For details,
244 -- see Exp_Ch7.Expand_Cleanup_Actions.
246 function Build_Renamed_Formal_Declaration
247 (New_F : Entity_Id;
248 Formal : Entity_Id;
249 Comp : Entity_Id;
250 Renamed_Formal : Node_Id) return Node_Id;
251 -- Create a renaming declaration for a formal, within a protected entry
252 -- body or an accept body. The renamed object is a component of the
253 -- parameter block that is a parameter in the entry call.
255 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
256 -- does not dereference the corresponding component to prevent an illegal
257 -- use of the incomplete type (AI05-0151).
259 function Build_Selected_Name
260 (Prefix : Entity_Id;
261 Selector : Entity_Id;
262 Append_Char : Character := ' ') return Name_Id;
263 -- Build a name in the form of Prefix__Selector, with an optional character
264 -- appended. This is used for internal subprograms generated for operations
265 -- of protected types, including barrier functions. For the subprograms
266 -- generated for entry bodies and entry barriers, the generated name
267 -- includes a sequence number that makes names unique in the presence of
268 -- entry overloading. This is necessary because entry body procedures and
269 -- barrier functions all have the same signature.
271 procedure Build_Simple_Entry_Call
272 (N : Node_Id;
273 Concval : Node_Id;
274 Ename : Node_Id;
275 Index : Node_Id);
276 -- Build the call corresponding to the task entry call. N is the task entry
277 -- call, Concval is the concurrent object, Ename is the entry name and
278 -- Index is the entry family index.
279 -- Note that N might be expanded into an N_Block_Statement if it gets
280 -- inlined.
282 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
283 -- This routine constructs a specification for the procedure that we will
284 -- build for the task body for task type T. The spec has the form:
286 -- procedure tnameB (_Task : access tnameV);
288 -- where name is the character name taken from the task type entity that
289 -- is passed as the argument to the procedure, and tnameV is the task
290 -- value type that is associated with the task type.
292 function Build_Unprotected_Subprogram_Body
293 (N : Node_Id;
294 Pid : Node_Id) return Node_Id;
295 -- This routine constructs the unprotected version of a protected
296 -- subprogram body, which contains all of the code in the original,
297 -- unexpanded body. This is the version of the protected subprogram that is
298 -- called from all protected operations on the same object, including the
299 -- protected version of the same subprogram.
301 procedure Build_Wrapper_Bodies
302 (Loc : Source_Ptr;
303 Typ : Entity_Id;
304 N : Node_Id);
305 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
306 -- record of a concurrent type. N is the insertion node where all bodies
307 -- will be placed. This routine builds the bodies of the subprograms which
308 -- serve as an indirection mechanism to overriding primitives of concurrent
309 -- types, entries and protected procedures. Any new body is analyzed.
311 procedure Build_Wrapper_Specs
312 (Loc : Source_Ptr;
313 Typ : Entity_Id;
314 N : in out Node_Id);
315 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
316 -- record of a concurrent type. N is the insertion node where all specs
317 -- will be placed. This routine builds the specs of the subprograms which
318 -- serve as an indirection mechanism to overriding primitives of concurrent
319 -- types, entries and protected procedures. Any new spec is analyzed.
321 procedure Collect_Entry_Families
322 (Loc : Source_Ptr;
323 Cdecls : List_Id;
324 Current_Node : in out Node_Id;
325 Conctyp : Entity_Id);
326 -- For each entry family in a concurrent type, create an anonymous array
327 -- type of the right size, and add a component to the corresponding_record.
329 function Concurrent_Object
330 (Spec_Id : Entity_Id;
331 Conc_Typ : Entity_Id) return Entity_Id;
332 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
333 -- the entity associated with the concurrent object in the Protected_Body_
334 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
335 -- denotes formal parameter _O, _object or _task.
337 function Copy_Result_Type (Res : Node_Id) return Node_Id;
338 -- Copy the result type of a function specification, when building the
339 -- internal operation corresponding to a protected function, or when
340 -- expanding an access to protected function. If the result is an anonymous
341 -- access to subprogram itself, we need to create a new signature with the
342 -- same parameter names and the same resolved types, but with new entities
343 -- for the formals.
345 function Create_Secondary_Stack_For_Task (T : Node_Id) return Boolean;
346 -- Return whether a secondary stack for the task T should be created by the
347 -- expander. The secondary stack for a task will be created by the expander
348 -- if the size of the stack has been specified by the Secondary_Stack_Size
349 -- representation aspect and either the No_Implicit_Heap_Allocations or
350 -- No_Implicit_Task_Allocations restrictions are in effect and the
351 -- No_Secondary_Stack restriction is not.
353 procedure Debug_Private_Data_Declarations (Decls : List_Id);
354 -- Decls is a list which may contain the declarations created by Install_
355 -- Private_Data_Declarations. All generated entities are marked as needing
356 -- debug info and debug nodes are manually generation where necessary. This
357 -- step of the expansion must to be done after private data has been moved
358 -- to its final resting scope to ensure proper visibility of debug objects.
360 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
361 -- If control flow optimizations are suppressed, and Alt is an accept,
362 -- delay, or entry call alternative with no trailing statements, insert
363 -- a null trailing statement with the given Loc (which is the sloc of
364 -- the accept, delay, or entry call statement). There might not be any
365 -- generated code for the accept, delay, or entry call itself (the effect
366 -- of these statements is part of the general processing done for the
367 -- enclosing selective accept, timed entry call, or asynchronous select),
368 -- and the null statement is there to carry the sloc of that statement to
369 -- the back-end for trace-based coverage analysis purposes.
371 procedure Extract_Dispatching_Call
372 (N : Node_Id;
373 Call_Ent : out Entity_Id;
374 Object : out Entity_Id;
375 Actuals : out List_Id;
376 Formals : out List_Id);
377 -- Given a dispatching call, extract the entity of the name of the call,
378 -- its actual dispatching object, its actual parameters and the formal
379 -- parameters of the overridden interface-level version. If the type of
380 -- the dispatching object is an access type then an explicit dereference
381 -- is returned in Object.
383 procedure Extract_Entry
384 (N : Node_Id;
385 Concval : out Node_Id;
386 Ename : out Node_Id;
387 Index : out Node_Id);
388 -- Given an entry call, returns the associated concurrent object, the entry
389 -- name, and the entry family index.
391 function Family_Offset
392 (Loc : Source_Ptr;
393 Hi : Node_Id;
394 Lo : Node_Id;
395 Ttyp : Entity_Id;
396 Cap : Boolean) return Node_Id;
397 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
398 -- accept statement, or the upper bound in the discrete subtype of an entry
399 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
400 -- type of the entry. If Cap is true, the result is capped according to
401 -- Entry_Family_Bound.
403 function Family_Size
404 (Loc : Source_Ptr;
405 Hi : Node_Id;
406 Lo : Node_Id;
407 Ttyp : Entity_Id;
408 Cap : Boolean) return Node_Id;
409 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
410 -- family, and handle properly the superflat case. This is equivalent to
411 -- the use of 'Length on the index type, but must use Family_Offset to
412 -- handle properly the case of bounds that depend on discriminants. If
413 -- Cap is true, the result is capped according to Entry_Family_Bound.
415 procedure Find_Enclosing_Context
416 (N : Node_Id;
417 Context : out Node_Id;
418 Context_Id : out Entity_Id;
419 Context_Decls : out List_Id);
420 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
421 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
422 -- nearest enclosing body, block, package, or return statement and return
423 -- its constituents. Context is the enclosing construct, Context_Id is
424 -- the scope of Context_Id and Context_Decls is the declarative list of
425 -- Context.
427 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
428 -- Given a subprogram identifier, return the entity which is associated
429 -- with the protection entry index in the Protected_Body_Subprogram or
430 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
431 -- parameter _E.
433 function Is_Potentially_Large_Family
434 (Base_Index : Entity_Id;
435 Conctyp : Entity_Id;
436 Lo : Node_Id;
437 Hi : Node_Id) return Boolean;
438 -- Determine whether an entry family is potentially large because one of
439 -- its bounds denotes a discrminant.
441 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
442 -- Determine whether Id is a function or a procedure and is marked as a
443 -- private primitive.
445 function Null_Statements (Stats : List_Id) return Boolean;
446 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
447 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
448 -- to still count as null. Returns True for a null sequence. The argument
449 -- is the list of statements from the DO-END sequence.
451 function Parameter_Block_Pack
452 (Loc : Source_Ptr;
453 Blk_Typ : Entity_Id;
454 Actuals : List_Id;
455 Formals : List_Id;
456 Decls : List_Id;
457 Stmts : List_Id) return Entity_Id;
458 -- Set the components of the generated parameter block with the values
459 -- of the actual parameters. Generate aliased temporaries to capture the
460 -- values for types that are passed by copy. Otherwise generate a reference
461 -- to the actual's value. Return the address of the aggregate block.
462 -- Generate:
463 -- Jnn1 : alias <formal-type1>;
464 -- Jnn1 := <actual1>;
465 -- ...
466 -- P : Blk_Typ := (
467 -- Jnn1'unchecked_access;
468 -- <actual2>'reference;
469 -- ...);
471 function Parameter_Block_Unpack
472 (Loc : Source_Ptr;
473 P : Entity_Id;
474 Actuals : List_Id;
475 Formals : List_Id) return List_Id;
476 -- Retrieve the values of the components from the parameter block and
477 -- assign then to the original actual parameters. Generate:
478 -- <actual1> := P.<formal1>;
479 -- ...
480 -- <actualN> := P.<formalN>;
482 procedure Reset_Scopes_To (Bod : Node_Id; E : Entity_Id);
483 -- Reset the scope of declarations and blocks at the top level of Bod to
484 -- be E. Bod is either a block or a subprogram body. Used after expanding
485 -- various kinds of entry bodies into their corresponding constructs. This
486 -- is needed during unnesting to determine whether a body generated for an
487 -- entry or an accept alternative includes uplevel references.
489 function Trivial_Accept_OK return Boolean;
490 -- If there is no DO-END block for an accept, or if the DO-END block has
491 -- only null statements, then it is possible to do the Rendezvous with much
492 -- less overhead using the Accept_Trivial routine in the run-time library.
493 -- However, this is not always a valid optimization. Whether it is valid or
494 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
495 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
496 -- a rescheduling is required, so this optimization is not allowed. This
497 -- function returns True if the optimization is permitted.
499 -----------------------------
500 -- Actual_Index_Expression --
501 -----------------------------
503 function Actual_Index_Expression
504 (Sloc : Source_Ptr;
505 Ent : Entity_Id;
506 Index : Node_Id;
507 Tsk : Entity_Id) return Node_Id
509 Ttyp : constant Entity_Id := Etype (Tsk);
510 Expr : Node_Id;
511 Num : Node_Id;
512 Lo : Node_Id;
513 Hi : Node_Id;
514 Prev : Entity_Id;
515 S : Node_Id;
517 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
518 -- Compute difference between bounds of entry family
520 --------------------------
521 -- Actual_Family_Offset --
522 --------------------------
524 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
526 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
527 -- Replace a reference to a discriminant with a selected component
528 -- denoting the discriminant of the target task.
530 -----------------------------
531 -- Actual_Discriminant_Ref --
532 -----------------------------
534 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
535 Typ : constant Entity_Id := Etype (Bound);
536 B : Node_Id;
538 begin
539 if not Is_Entity_Name (Bound)
540 or else Ekind (Entity (Bound)) /= E_Discriminant
541 then
542 if Nkind (Bound) = N_Attribute_Reference then
543 return Bound;
544 else
545 B := New_Copy_Tree (Bound);
546 end if;
548 else
549 B :=
550 Make_Selected_Component (Sloc,
551 Prefix => New_Copy_Tree (Tsk),
552 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
554 Analyze_And_Resolve (B, Typ);
555 end if;
557 return
558 Make_Attribute_Reference (Sloc,
559 Attribute_Name => Name_Pos,
560 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
561 Expressions => New_List (B));
562 end Actual_Discriminant_Ref;
564 -- Start of processing for Actual_Family_Offset
566 begin
567 return
568 Make_Op_Subtract (Sloc,
569 Left_Opnd => Actual_Discriminant_Ref (Hi),
570 Right_Opnd => Actual_Discriminant_Ref (Lo));
571 end Actual_Family_Offset;
573 -- Start of processing for Actual_Index_Expression
575 begin
576 -- The queues of entries and entry families appear in textual order in
577 -- the associated record. The entry index is computed as the sum of the
578 -- number of queues for all entries that precede the designated one, to
579 -- which is added the index expression, if this expression denotes a
580 -- member of a family.
582 -- The following is a place holder for the count of simple entries
584 Num := Make_Integer_Literal (Sloc, 1);
586 -- We construct an expression which is a series of addition operations.
587 -- See comments in Entry_Index_Expression, which is identical in
588 -- structure.
590 if Present (Index) then
591 S := Entry_Index_Type (Ent);
593 -- First make sure the index is in range if requested. The index type
594 -- has been directly set on the prefix, see Resolve_Entry.
596 if Do_Range_Check (Index) then
597 Generate_Range_Check
598 (Index, Etype (Prefix (Parent (Index))), CE_Range_Check_Failed);
599 end if;
601 Expr :=
602 Make_Op_Add (Sloc,
603 Left_Opnd => Num,
604 Right_Opnd =>
605 Actual_Family_Offset (
606 Make_Attribute_Reference (Sloc,
607 Attribute_Name => Name_Pos,
608 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
609 Expressions => New_List (Relocate_Node (Index))),
610 Type_Low_Bound (S)));
611 else
612 Expr := Num;
613 end if;
615 -- Now add lengths of preceding entries and entry families
617 Prev := First_Entity (Ttyp);
618 while Chars (Prev) /= Chars (Ent)
619 or else Ekind (Prev) /= Ekind (Ent)
620 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
621 loop
622 if Ekind (Prev) = E_Entry then
623 Set_Intval (Num, Intval (Num) + 1);
625 elsif Ekind (Prev) = E_Entry_Family then
626 S := Entry_Index_Type (Prev);
628 -- The need for the following full view retrieval stems from this
629 -- complex case of nested generics and tasking:
631 -- generic
632 -- type Formal_Index is range <>;
633 -- ...
634 -- package Outer is
635 -- type Index is private;
636 -- generic
637 -- ...
638 -- package Inner is
639 -- procedure P;
640 -- end Inner;
641 -- private
642 -- type Index is new Formal_Index range 1 .. 10;
643 -- end Outer;
645 -- package body Outer is
646 -- task type T is
647 -- entry Fam (Index); -- (2)
648 -- entry E;
649 -- end T;
650 -- package body Inner is -- (3)
651 -- procedure P is
652 -- begin
653 -- T.E; -- (1)
654 -- end P;
655 -- end Inner;
656 -- ...
658 -- We are currently building the index expression for the entry
659 -- call "T.E" (1). Part of the expansion must mention the range
660 -- of the discrete type "Index" (2) of entry family "Fam".
662 -- However only the private view of type "Index" is available to
663 -- the inner generic (3) because there was no prior mention of
664 -- the type inside "Inner". This visibility requirement is
665 -- implicit and cannot be detected during the construction of
666 -- the generic trees and needs special handling.
668 if In_Instance_Body
669 and then Is_Private_Type (S)
670 and then Present (Full_View (S))
671 then
672 S := Full_View (S);
673 end if;
675 Lo := Type_Low_Bound (S);
676 Hi := Type_High_Bound (S);
678 Expr :=
679 Make_Op_Add (Sloc,
680 Left_Opnd => Expr,
681 Right_Opnd =>
682 Make_Op_Add (Sloc,
683 Left_Opnd => Actual_Family_Offset (Hi, Lo),
684 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
686 -- Other components are anonymous types to be ignored
688 else
689 null;
690 end if;
692 Next_Entity (Prev);
693 end loop;
695 return Expr;
696 end Actual_Index_Expression;
698 --------------------------
699 -- Add_Formal_Renamings --
700 --------------------------
702 procedure Add_Formal_Renamings
703 (Spec : Node_Id;
704 Decls : List_Id;
705 Ent : Entity_Id;
706 Loc : Source_Ptr)
708 Ptr : constant Entity_Id :=
709 Defining_Identifier
710 (Next (First (Parameter_Specifications (Spec))));
711 -- The name of the formal that holds the address of the parameter block
712 -- for the call.
714 Comp : Entity_Id;
715 Decl : Node_Id;
716 Formal : Entity_Id;
717 New_F : Entity_Id;
718 Renamed_Formal : Node_Id;
720 begin
721 Formal := First_Formal (Ent);
722 while Present (Formal) loop
723 Comp := Entry_Component (Formal);
724 New_F :=
725 Make_Defining_Identifier (Sloc (Formal),
726 Chars => Chars (Formal));
727 Set_Etype (New_F, Etype (Formal));
728 Set_Scope (New_F, Ent);
730 -- Now we set debug info needed on New_F even though it does not come
731 -- from source, so that the debugger will get the right information
732 -- for these generated names.
734 Set_Debug_Info_Needed (New_F);
736 if Ekind (Formal) = E_In_Parameter then
737 Mutate_Ekind (New_F, E_Constant);
738 else
739 Mutate_Ekind (New_F, E_Variable);
740 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
741 end if;
743 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
745 Renamed_Formal :=
746 Make_Selected_Component (Loc,
747 Prefix =>
748 Make_Explicit_Dereference (Loc,
749 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
750 Make_Identifier (Loc, Chars (Ptr)))),
751 Selector_Name => New_Occurrence_Of (Comp, Loc));
753 Decl :=
754 Build_Renamed_Formal_Declaration
755 (New_F, Formal, Comp, Renamed_Formal);
757 Append (Decl, Decls);
758 Set_Renamed_Object (Formal, New_F);
759 Next_Formal (Formal);
760 end loop;
761 end Add_Formal_Renamings;
763 ------------------------
764 -- Add_Object_Pointer --
765 ------------------------
767 procedure Add_Object_Pointer
768 (Loc : Source_Ptr;
769 Conc_Typ : Entity_Id;
770 Decls : List_Id)
772 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
773 Decl : Node_Id;
774 Obj_Ptr : Node_Id;
776 begin
777 -- Create the renaming declaration for the Protection object of a
778 -- protected type. _Object is used by Complete_Entry_Body.
779 -- ??? An attempt to make this a renaming was unsuccessful.
781 -- Build the entity for the access type
783 Obj_Ptr :=
784 Make_Defining_Identifier (Loc,
785 New_External_Name (Chars (Rec_Typ), 'P'));
787 -- Generate:
788 -- _object : poVP := poVP!O;
790 Decl :=
791 Make_Object_Declaration (Loc,
792 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
793 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
794 Expression =>
795 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
796 Set_Debug_Info_Needed (Defining_Identifier (Decl));
797 Prepend_To (Decls, Decl);
799 -- Generate:
800 -- type poVP is access poV;
802 Decl :=
803 Make_Full_Type_Declaration (Loc,
804 Defining_Identifier =>
805 Obj_Ptr,
806 Type_Definition =>
807 Make_Access_To_Object_Definition (Loc,
808 Subtype_Indication =>
809 New_Occurrence_Of (Rec_Typ, Loc)));
810 Set_Debug_Info_Needed (Defining_Identifier (Decl));
811 Prepend_To (Decls, Decl);
812 end Add_Object_Pointer;
814 -----------------------
815 -- Build_Accept_Body --
816 -----------------------
818 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
819 Loc : constant Source_Ptr := Sloc (Astat);
820 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
821 New_S : Node_Id;
822 Hand : Node_Id;
823 Call : Node_Id;
824 Ohandle : Node_Id;
826 begin
827 -- At the end of the statement sequence, Complete_Rendezvous is called.
828 -- A label skipping the Complete_Rendezvous, and all other accept
829 -- processing, has already been added for the expansion of requeue
830 -- statements. The Sloc is copied from the last statement since it
831 -- is really part of this last statement.
833 Call :=
834 Build_Runtime_Call
835 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
836 Insert_Before (Last (Statements (Stats)), Call);
837 Analyze (Call);
839 -- Ada 2022 (AI12-0279)
841 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
842 and then RTE_Available (RE_Yield)
843 then
844 Insert_Action_After (Call,
845 Make_Procedure_Call_Statement (Loc,
846 New_Occurrence_Of (RTE (RE_Yield), Loc)));
847 end if;
849 -- If exception handlers are present, then append Complete_Rendezvous
850 -- calls to the handlers, and construct the required outer block. As
851 -- above, the Sloc is copied from the last statement in the sequence.
853 if Present (Exception_Handlers (Stats)) then
854 Hand := First (Exception_Handlers (Stats));
855 while Present (Hand) loop
856 Call :=
857 Build_Runtime_Call
858 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
859 Append (Call, Statements (Hand));
860 Analyze (Call);
862 -- Ada 2022 (AI12-0279)
864 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
865 and then RTE_Available (RE_Yield)
866 then
867 Insert_Action_After (Call,
868 Make_Procedure_Call_Statement (Loc,
869 New_Occurrence_Of (RTE (RE_Yield), Loc)));
870 end if;
872 Next (Hand);
873 end loop;
875 New_S :=
876 Make_Handled_Sequence_Of_Statements (Loc,
877 Statements => New_List (
878 Make_Block_Statement (Loc,
879 Handled_Statement_Sequence => Stats)));
881 else
882 New_S := Stats;
883 end if;
885 -- At this stage we know that the new statement sequence does
886 -- not have an exception handler part, so we supply one to call
887 -- Exceptional_Complete_Rendezvous. This handler is
889 -- when all others =>
890 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
892 -- We handle Abort_Signal to make sure that we properly catch the abort
893 -- case and wake up the caller.
895 Call :=
896 Make_Procedure_Call_Statement (Sloc (Stats),
897 Name => New_Occurrence_Of (
898 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
899 Parameter_Associations => New_List (
900 Make_Function_Call (Sloc (Stats),
901 Name =>
902 New_Occurrence_Of
903 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))));
905 Ohandle := Make_Others_Choice (Loc);
906 Set_All_Others (Ohandle);
908 Set_Exception_Handlers (New_S,
909 New_List (
910 Make_Implicit_Exception_Handler (Loc,
911 Exception_Choices => New_List (Ohandle),
913 Statements => New_List (Call))));
915 -- Ada 2022 (AI12-0279)
917 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
918 and then RTE_Available (RE_Yield)
919 then
920 Insert_Action_After (Call,
921 Make_Procedure_Call_Statement (Loc,
922 New_Occurrence_Of (RTE (RE_Yield), Loc)));
923 end if;
925 Set_Parent (New_S, Astat); -- temp parent for Analyze call
926 Analyze_Exception_Handlers (Exception_Handlers (New_S));
927 Expand_Exception_Handlers (New_S);
929 -- Exceptional_Complete_Rendezvous must be called with abort still
930 -- deferred, which is the case for a "when all others" handler.
932 return New_S;
933 end Build_Accept_Body;
935 -----------------------------------
936 -- Build_Activation_Chain_Entity --
937 -----------------------------------
939 procedure Build_Activation_Chain_Entity (N : Node_Id) is
940 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
941 -- Determine whether an extended return statement has activation chain
943 --------------------------
944 -- Has_Activation_Chain --
945 --------------------------
947 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
948 Decl : Node_Id;
950 begin
951 Decl := First (Return_Object_Declarations (Stmt));
952 while Present (Decl) loop
953 if Nkind (Decl) = N_Object_Declaration
954 and then Chars (Defining_Identifier (Decl)) = Name_uChain
955 then
956 return True;
957 end if;
959 Next (Decl);
960 end loop;
962 return False;
963 end Has_Activation_Chain;
965 -- Local variables
967 Context : Node_Id;
968 Context_Id : Entity_Id;
969 Decls : List_Id;
971 -- Start of processing for Build_Activation_Chain_Entity
973 begin
974 -- No action needed if the run-time has no tasking support
976 if Global_No_Tasking then
977 return;
978 end if;
980 -- Activation chain is never used for sequential elaboration policy, see
981 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
983 if Partition_Elaboration_Policy = 'S' then
984 return;
985 end if;
987 Find_Enclosing_Context (N, Context, Context_Id, Decls);
989 -- If activation chain entity has not been declared already, create one
991 if Nkind (Context) = N_Extended_Return_Statement
992 or else No (Activation_Chain_Entity (Context))
993 then
994 -- Since extended return statements do not store the entity of the
995 -- chain, examine the return object declarations to avoid creating
996 -- a duplicate.
998 if Nkind (Context) = N_Extended_Return_Statement
999 and then Has_Activation_Chain (Context)
1000 then
1001 return;
1002 end if;
1004 declare
1005 Loc : constant Source_Ptr := Sloc (Context);
1006 Chain : Entity_Id;
1007 Decl : Node_Id;
1009 begin
1010 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
1012 -- Note: An extended return statement is not really a task
1013 -- activator, but it does have an activation chain on which to
1014 -- store the tasks temporarily. On successful return, the tasks
1015 -- on this chain are moved to the chain passed in by the caller.
1016 -- We do not build an Activation_Chain_Entity for an extended
1017 -- return statement, because we do not want to build a call to
1018 -- Activate_Tasks. Task activation is the responsibility of the
1019 -- caller.
1021 if Nkind (Context) /= N_Extended_Return_Statement then
1022 Set_Activation_Chain_Entity (Context, Chain);
1023 end if;
1025 Decl :=
1026 Make_Object_Declaration (Loc,
1027 Defining_Identifier => Chain,
1028 Aliased_Present => True,
1029 Object_Definition =>
1030 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
1032 Prepend_To (Decls, Decl);
1034 -- Ensure that _chain appears in the proper scope of the context
1036 if Context_Id /= Current_Scope then
1037 Push_Scope (Context_Id);
1038 Analyze (Decl);
1039 Pop_Scope;
1040 else
1041 Analyze (Decl);
1042 end if;
1043 end;
1044 end if;
1045 end Build_Activation_Chain_Entity;
1047 ----------------------------
1048 -- Build_Barrier_Function --
1049 ----------------------------
1051 function Build_Barrier_Function
1052 (N : Node_Id;
1053 Ent : Entity_Id;
1054 Pid : Entity_Id) return Node_Id
1056 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
1057 Cond : constant Node_Id := Condition (Ent_Formals);
1058 Loc : constant Source_Ptr := Sloc (Cond);
1059 Func_Id : constant Entity_Id := Barrier_Function (Ent);
1060 Op_Decls : constant List_Id := New_List;
1061 Stmt : Node_Id;
1062 Func_Body : Node_Id;
1064 begin
1065 -- Add a declaration for the Protection object, renaming declarations
1066 -- for the discriminals and privals and finally a declaration for the
1067 -- entry family index (if applicable).
1069 Install_Private_Data_Declarations (Sloc (N),
1070 Spec_Id => Func_Id,
1071 Conc_Typ => Pid,
1072 Body_Nod => N,
1073 Decls => Op_Decls,
1074 Barrier => True,
1075 Family => Ekind (Ent) = E_Entry_Family);
1077 -- If compiling with -fpreserve-control-flow, make sure we insert an
1078 -- IF statement so that the back-end knows to generate a conditional
1079 -- branch instruction, even if the condition is just the name of a
1080 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1081 -- such redundant IF statements under -fpreserve-control-flow
1082 -- (whether coming from this routine, or directly from source).
1084 if Opt.Suppress_Control_Flow_Optimizations then
1085 Stmt :=
1086 Make_Implicit_If_Statement (Cond,
1087 Condition => Cond,
1088 Then_Statements => New_List (
1089 Make_Simple_Return_Statement (Loc,
1090 New_Occurrence_Of (Standard_True, Loc))),
1092 Else_Statements => New_List (
1093 Make_Simple_Return_Statement (Loc,
1094 New_Occurrence_Of (Standard_False, Loc))));
1096 else
1097 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1098 end if;
1100 -- Note: the condition in the barrier function needs to be properly
1101 -- processed for the C/Fortran boolean possibility, but this happens
1102 -- automatically since the return statement does this normalization.
1104 Func_Body :=
1105 Make_Subprogram_Body (Loc,
1106 Specification =>
1107 Build_Barrier_Function_Specification (Loc,
1108 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1109 Declarations => Op_Decls,
1110 Handled_Statement_Sequence =>
1111 Make_Handled_Sequence_Of_Statements (Loc,
1112 Statements => New_List (Stmt)));
1113 Set_Is_Entry_Barrier_Function (Func_Body);
1115 return Func_Body;
1116 end Build_Barrier_Function;
1118 ------------------------------------------
1119 -- Build_Barrier_Function_Specification --
1120 ------------------------------------------
1122 function Build_Barrier_Function_Specification
1123 (Loc : Source_Ptr;
1124 Def_Id : Entity_Id) return Node_Id
1126 begin
1127 Set_Debug_Info_Needed (Def_Id);
1129 return
1130 Make_Function_Specification (Loc,
1131 Defining_Unit_Name => Def_Id,
1132 Parameter_Specifications => New_List (
1133 Make_Parameter_Specification (Loc,
1134 Defining_Identifier =>
1135 Make_Defining_Identifier (Loc, Name_uO),
1136 Parameter_Type =>
1137 New_Occurrence_Of (RTE (RE_Address), Loc)),
1139 Make_Parameter_Specification (Loc,
1140 Defining_Identifier =>
1141 Make_Defining_Identifier (Loc, Name_uE),
1142 Parameter_Type =>
1143 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1145 Result_Definition =>
1146 New_Occurrence_Of (Standard_Boolean, Loc));
1147 end Build_Barrier_Function_Specification;
1149 --------------------------
1150 -- Build_Call_With_Task --
1151 --------------------------
1153 function Build_Call_With_Task
1154 (N : Node_Id;
1155 E : Entity_Id) return Node_Id
1157 Loc : constant Source_Ptr := Sloc (N);
1158 begin
1159 return
1160 Make_Function_Call (Loc,
1161 Name => New_Occurrence_Of (E, Loc),
1162 Parameter_Associations => New_List (Concurrent_Ref (N)));
1163 end Build_Call_With_Task;
1165 -----------------------------
1166 -- Build_Class_Wide_Master --
1167 -----------------------------
1169 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1170 Loc : constant Source_Ptr := Sloc (Typ);
1171 Master_Decl : Node_Id;
1172 Master_Id : Entity_Id;
1173 Master_Scope : Entity_Id;
1174 Name_Id : Node_Id;
1175 Related_Node : Node_Id;
1176 Ren_Decl : Node_Id;
1178 begin
1179 -- No action needed if the run-time has no tasking support
1181 if Global_No_Tasking then
1182 return;
1183 end if;
1185 -- Find the declaration that created the access type, which is either a
1186 -- type declaration, or an object declaration with an access definition,
1187 -- in which case the type is anonymous.
1189 if Is_Itype (Typ) then
1190 Related_Node := Associated_Node_For_Itype (Typ);
1191 else
1192 Related_Node := Parent (Typ);
1193 end if;
1195 Master_Scope := Find_Master_Scope (Typ);
1197 -- Nothing to do if the master scope already contains a _master entity.
1198 -- The only exception to this is the following scenario:
1200 -- Source_Scope
1201 -- Transient_Scope_1
1202 -- _master
1204 -- Transient_Scope_2
1205 -- use of master
1207 -- In this case the source scope is marked as having the master entity
1208 -- even though the actual declaration appears inside an inner scope. If
1209 -- the second transient scope requires a _master, it cannot use the one
1210 -- already declared because the entity is not visible.
1212 Name_Id := Make_Identifier (Loc, Name_uMaster);
1213 Master_Decl := Empty;
1215 if not Has_Master_Entity (Master_Scope)
1216 or else No (Current_Entity_In_Scope (Name_Id))
1217 then
1218 declare
1219 Ins_Nod : Node_Id;
1220 Par_Nod : Node_Id;
1222 begin
1223 Master_Decl := Build_Master_Declaration (Loc);
1225 -- Ensure that the master declaration is placed before its use
1227 Ins_Nod := Find_Hook_Context (Related_Node);
1228 while not Is_List_Member (Ins_Nod) loop
1229 Ins_Nod := Parent (Ins_Nod);
1230 end loop;
1232 Par_Nod := Parent (List_Containing (Ins_Nod));
1234 -- For internal blocks created by Wrap_Loop_Statement, Wrap_
1235 -- Statements_In_Block, and Build_Abort_Undefer_Block, remember
1236 -- that they have a task master entity declaration; required by
1237 -- Build_Master_Entity to avoid creating another master entity,
1238 -- and also ensures that subsequent calls to Find_Master_Scope
1239 -- return this scope as the master scope of Typ.
1241 if Is_Internal_Block (Par_Nod) then
1242 Set_Has_Master_Entity (Entity (Identifier (Par_Nod)));
1244 elsif Nkind (Par_Nod) = N_Handled_Sequence_Of_Statements
1245 and then Is_Internal_Block (Parent (Par_Nod))
1246 then
1247 Set_Has_Master_Entity (Entity (Identifier (Parent (Par_Nod))));
1249 -- Otherwise remember that this scope has an associated task
1250 -- master entity declaration.
1252 else
1253 Set_Has_Master_Entity (Master_Scope);
1254 end if;
1256 Insert_Before (First (List_Containing (Ins_Nod)), Master_Decl);
1257 Analyze (Master_Decl);
1259 -- Mark the containing scope as a task master. Masters associated
1260 -- with return statements are already marked at this stage (see
1261 -- Analyze_Subprogram_Body).
1263 if Ekind (Current_Scope) /= E_Return_Statement then
1264 declare
1265 Par : Node_Id := Related_Node;
1267 begin
1268 while Nkind (Par) /= N_Compilation_Unit loop
1269 Par := Parent (Par);
1271 -- If we fall off the top, we are at the outer level,
1272 -- and the environment task is our effective master,
1273 -- so nothing to mark.
1275 if Nkind (Par) in
1276 N_Block_Statement | N_Subprogram_Body | N_Task_Body
1277 then
1278 Set_Is_Task_Master (Par);
1279 exit;
1280 end if;
1281 end loop;
1282 end;
1283 end if;
1284 end;
1285 end if;
1287 Master_Id :=
1288 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1290 -- Generate:
1291 -- typeMnn renames _master;
1293 Ren_Decl :=
1294 Make_Object_Renaming_Declaration (Loc,
1295 Defining_Identifier => Master_Id,
1296 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1297 Name => Name_Id);
1299 -- If the master is declared locally, add the renaming declaration
1300 -- immediately after it, to prevent access-before-elaboration in the
1301 -- back-end.
1303 if Present (Master_Decl) then
1304 Insert_After (Master_Decl, Ren_Decl);
1305 Analyze (Ren_Decl);
1307 else
1308 Insert_Action (Related_Node, Ren_Decl);
1309 end if;
1311 Set_Master_Id (Typ, Master_Id);
1312 end Build_Class_Wide_Master;
1314 --------------------------------
1315 -- Build_Corresponding_Record --
1316 --------------------------------
1318 function Build_Corresponding_Record
1319 (N : Node_Id;
1320 Ctyp : Entity_Id;
1321 Loc : Source_Ptr) return Node_Id
1323 Rec_Ent : constant Entity_Id :=
1324 Make_Defining_Identifier
1325 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1326 Disc : Entity_Id;
1327 Dlist : List_Id;
1328 New_Disc : Entity_Id;
1329 Cdecls : List_Id;
1331 begin
1332 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1333 Mutate_Ekind (Rec_Ent, E_Record_Type);
1334 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1335 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1336 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1337 Set_Stored_Constraint (Rec_Ent, No_Elist);
1338 Cdecls := New_List;
1340 -- Use discriminals to create list of discriminants for record, and
1341 -- create new discriminals for use in default expressions, etc. It is
1342 -- worth noting that a task discriminant gives rise to 5 entities;
1344 -- a) The original discriminant.
1345 -- b) The discriminal for use in the task.
1346 -- c) The discriminant of the corresponding record.
1347 -- d) The discriminal for the init proc of the corresponding record.
1348 -- e) The local variable that renames the discriminant in the procedure
1349 -- for the task body.
1351 -- In fact the discriminals b) are used in the renaming declarations
1352 -- for e). See details in einfo (Handling of Discriminants).
1354 if Present (Discriminant_Specifications (N)) then
1355 Dlist := New_List;
1356 Disc := First_Discriminant (Ctyp);
1358 while Present (Disc) loop
1359 New_Disc := CR_Discriminant (Disc);
1361 Append_To (Dlist,
1362 Make_Discriminant_Specification (Loc,
1363 Defining_Identifier => New_Disc,
1364 Discriminant_Type =>
1365 New_Occurrence_Of (Etype (Disc), Loc),
1366 Expression =>
1367 New_Copy (Discriminant_Default_Value (Disc))));
1369 Next_Discriminant (Disc);
1370 end loop;
1372 else
1373 Dlist := No_List;
1374 end if;
1376 -- Now we can construct the record type declaration. Note that this
1377 -- record is "limited tagged". It is "limited" to reflect the underlying
1378 -- limitedness of the task or protected object that it represents, and
1379 -- ensuring for example that it is properly passed by reference. It is
1380 -- "tagged" to give support to dispatching calls through interfaces. We
1381 -- propagate here the list of interfaces covered by the concurrent type
1382 -- (Ada 2005: AI-345).
1384 return
1385 Make_Full_Type_Declaration (Loc,
1386 Defining_Identifier => Rec_Ent,
1387 Discriminant_Specifications => Dlist,
1388 Type_Definition =>
1389 Make_Record_Definition (Loc,
1390 Component_List =>
1391 Make_Component_List (Loc, Component_Items => Cdecls),
1392 Tagged_Present =>
1393 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1394 Interface_List => Interface_List (N),
1395 Limited_Present => True));
1396 end Build_Corresponding_Record;
1398 ---------------------------------
1399 -- Build_Dispatching_Tag_Check --
1400 ---------------------------------
1402 function Build_Dispatching_Tag_Check
1403 (K : Entity_Id;
1404 N : Node_Id) return Node_Id
1406 Loc : constant Source_Ptr := Sloc (N);
1408 begin
1409 return
1410 Make_Op_Or (Loc,
1411 Make_Op_Eq (Loc,
1412 Left_Opnd =>
1413 New_Occurrence_Of (K, Loc),
1414 Right_Opnd =>
1415 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1417 Make_Op_Eq (Loc,
1418 Left_Opnd =>
1419 New_Occurrence_Of (K, Loc),
1420 Right_Opnd =>
1421 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1422 end Build_Dispatching_Tag_Check;
1424 ----------------------------------
1425 -- Build_Entry_Count_Expression --
1426 ----------------------------------
1428 function Build_Entry_Count_Expression
1429 (Concurrent_Type : Entity_Id;
1430 Loc : Source_Ptr) return Node_Id
1432 Eindx : Nat;
1433 Ent : Entity_Id;
1434 Ecount : Node_Id;
1435 Lo : Node_Id;
1436 Hi : Node_Id;
1437 Typ : Entity_Id;
1438 Large : Boolean;
1440 begin
1441 -- Count number of non-family entries
1443 Eindx := 0;
1444 Ent := First_Entity (Concurrent_Type);
1445 while Present (Ent) loop
1446 if Ekind (Ent) = E_Entry then
1447 Eindx := Eindx + 1;
1448 end if;
1450 Next_Entity (Ent);
1451 end loop;
1453 Ecount := Make_Integer_Literal (Loc, Eindx);
1455 -- Loop through entry families building the addition nodes
1457 Ent := First_Entity (Concurrent_Type);
1458 while Present (Ent) loop
1459 if Ekind (Ent) = E_Entry_Family then
1460 Typ := Entry_Index_Type (Ent);
1461 Hi := Type_High_Bound (Typ);
1462 Lo := Type_Low_Bound (Typ);
1463 Large := Is_Potentially_Large_Family
1464 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1465 Ecount :=
1466 Make_Op_Add (Loc,
1467 Left_Opnd => Ecount,
1468 Right_Opnd =>
1469 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1470 end if;
1472 Next_Entity (Ent);
1473 end loop;
1475 return Ecount;
1476 end Build_Entry_Count_Expression;
1478 ------------------------------
1479 -- Build_Master_Declaration --
1480 ------------------------------
1482 function Build_Master_Declaration (Loc : Source_Ptr) return Node_Id is
1483 Master_Decl : Node_Id;
1485 begin
1486 -- Generate a dummy master if tasks or tasking hierarchies are
1487 -- prohibited.
1489 -- _Master : constant Integer := Library_Task_Level;
1491 if not Tasking_Allowed
1492 or else Restrictions.Set (No_Task_Hierarchy)
1493 or else not RTE_Available (RE_Current_Master)
1494 then
1495 Master_Decl :=
1496 Make_Object_Declaration (Loc,
1497 Defining_Identifier =>
1498 Make_Defining_Identifier (Loc, Name_uMaster),
1499 Constant_Present => True,
1500 Object_Definition =>
1501 New_Occurrence_Of (Standard_Integer, Loc),
1502 Expression =>
1503 Make_Integer_Literal (Loc, Library_Task_Level));
1505 -- Generate:
1506 -- _master : constant Integer := Current_Master.all;
1508 else
1509 Master_Decl :=
1510 Make_Object_Declaration (Loc,
1511 Defining_Identifier =>
1512 Make_Defining_Identifier (Loc, Name_uMaster),
1513 Constant_Present => True,
1514 Object_Definition =>
1515 New_Occurrence_Of (Standard_Integer, Loc),
1516 Expression =>
1517 Make_Explicit_Dereference (Loc,
1518 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1519 end if;
1521 return Master_Decl;
1522 end Build_Master_Declaration;
1524 ---------------------------
1525 -- Build_Parameter_Block --
1526 ---------------------------
1528 function Build_Parameter_Block
1529 (Loc : Source_Ptr;
1530 Actuals : List_Id;
1531 Formals : List_Id;
1532 Decls : List_Id) return Entity_Id
1534 Actual : Entity_Id;
1535 Comp_Nam : Node_Id;
1536 Comps : List_Id;
1537 Formal : Entity_Id;
1538 Has_Comp : Boolean := False;
1539 Rec_Nam : Node_Id;
1541 begin
1542 Actual := First (Actuals);
1543 Comps := New_List;
1544 Formal := Defining_Identifier (First (Formals));
1546 while Present (Actual) loop
1547 if not Is_Controlling_Actual (Actual) then
1549 -- Generate:
1550 -- type Ann is access all <actual-type>
1552 Comp_Nam := Make_Temporary (Loc, 'A');
1553 Set_Is_Param_Block_Component_Type (Comp_Nam);
1555 Append_To (Decls,
1556 Make_Full_Type_Declaration (Loc,
1557 Defining_Identifier => Comp_Nam,
1558 Type_Definition =>
1559 Make_Access_To_Object_Definition (Loc,
1560 All_Present => True,
1561 Constant_Present => Ekind (Formal) = E_In_Parameter,
1562 Subtype_Indication =>
1563 New_Occurrence_Of (Etype (Actual), Loc))));
1565 -- Generate:
1566 -- Param : Ann;
1568 Append_To (Comps,
1569 Make_Component_Declaration (Loc,
1570 Defining_Identifier =>
1571 Make_Defining_Identifier (Loc, Chars (Formal)),
1572 Component_Definition =>
1573 Make_Component_Definition (Loc,
1574 Aliased_Present =>
1575 False,
1576 Subtype_Indication =>
1577 New_Occurrence_Of (Comp_Nam, Loc))));
1579 Has_Comp := True;
1580 end if;
1582 Next_Actual (Actual);
1583 Next_Formal_With_Extras (Formal);
1584 end loop;
1586 Rec_Nam := Make_Temporary (Loc, 'P');
1588 if Has_Comp then
1590 -- Generate:
1591 -- type Pnn is record
1592 -- Param1 : Ann1;
1593 -- ...
1594 -- ParamN : AnnN;
1596 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1597 -- the original parameter names and Ann1 .. AnnN are the access to
1598 -- actual types.
1600 Append_To (Decls,
1601 Make_Full_Type_Declaration (Loc,
1602 Defining_Identifier =>
1603 Rec_Nam,
1604 Type_Definition =>
1605 Make_Record_Definition (Loc,
1606 Component_List =>
1607 Make_Component_List (Loc, Comps))));
1608 else
1609 -- Generate:
1610 -- type Pnn is null record;
1612 Append_To (Decls,
1613 Make_Full_Type_Declaration (Loc,
1614 Defining_Identifier =>
1615 Rec_Nam,
1616 Type_Definition =>
1617 Make_Record_Definition (Loc,
1618 Null_Present => True,
1619 Component_List => Empty)));
1620 end if;
1622 return Rec_Nam;
1623 end Build_Parameter_Block;
1625 --------------------------------------
1626 -- Build_Renamed_Formal_Declaration --
1627 --------------------------------------
1629 function Build_Renamed_Formal_Declaration
1630 (New_F : Entity_Id;
1631 Formal : Entity_Id;
1632 Comp : Entity_Id;
1633 Renamed_Formal : Node_Id) return Node_Id
1635 Loc : constant Source_Ptr := Sloc (New_F);
1636 Decl : Node_Id;
1638 begin
1639 -- If the formal is a tagged incomplete type, it is already passed
1640 -- by reference, so it is sufficient to rename the pointer component
1641 -- that corresponds to the actual. Otherwise we need to dereference
1642 -- the pointer component to obtain the actual.
1644 if Is_Incomplete_Type (Etype (Formal))
1645 and then Is_Tagged_Type (Etype (Formal))
1646 then
1647 Decl :=
1648 Make_Object_Renaming_Declaration (Loc,
1649 Defining_Identifier => New_F,
1650 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
1651 Name => Renamed_Formal);
1653 else
1654 Decl :=
1655 Make_Object_Renaming_Declaration (Loc,
1656 Defining_Identifier => New_F,
1657 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
1658 Name =>
1659 Make_Explicit_Dereference (Loc, Renamed_Formal));
1660 end if;
1662 return Decl;
1663 end Build_Renamed_Formal_Declaration;
1665 --------------------------
1666 -- Build_Wrapper_Bodies --
1667 --------------------------
1669 procedure Build_Wrapper_Bodies
1670 (Loc : Source_Ptr;
1671 Typ : Entity_Id;
1672 N : Node_Id)
1674 Rec_Typ : Entity_Id;
1676 function Build_Wrapper_Body
1677 (Loc : Source_Ptr;
1678 Subp_Id : Entity_Id;
1679 Obj_Typ : Entity_Id;
1680 Formals : List_Id) return Node_Id;
1681 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
1682 -- associated with a protected or task type. Subp_Id is the subprogram
1683 -- name which will be wrapped. Obj_Typ is the type of the new formal
1684 -- parameter which handles dispatching and object notation. Formals are
1685 -- the original formals of Subp_Id which will be explicitly replicated.
1687 ------------------------
1688 -- Build_Wrapper_Body --
1689 ------------------------
1691 function Build_Wrapper_Body
1692 (Loc : Source_Ptr;
1693 Subp_Id : Entity_Id;
1694 Obj_Typ : Entity_Id;
1695 Formals : List_Id) return Node_Id
1697 Body_Spec : Node_Id;
1699 begin
1700 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
1702 -- The subprogram is not overriding or is not a primitive declared
1703 -- between two views.
1705 if No (Body_Spec) then
1706 return Empty;
1707 end if;
1709 declare
1710 Actuals : List_Id := No_List;
1711 Conv_Id : Node_Id;
1712 First_Form : Node_Id;
1713 Formal : Node_Id;
1714 Nam : Node_Id;
1716 begin
1717 -- Map formals to actuals. Use the list built for the wrapper
1718 -- spec, skipping the object notation parameter.
1720 First_Form := First (Parameter_Specifications (Body_Spec));
1722 Formal := First_Form;
1723 Next (Formal);
1725 if Present (Formal) then
1726 Actuals := New_List;
1727 while Present (Formal) loop
1728 Append_To (Actuals,
1729 Make_Identifier (Loc,
1730 Chars => Chars (Defining_Identifier (Formal))));
1731 Next (Formal);
1732 end loop;
1733 end if;
1735 -- Special processing for primitives declared between a private
1736 -- type and its completion: the wrapper needs a properly typed
1737 -- parameter if the wrapped operation has a controlling first
1738 -- parameter. Note that this might not be the case for a function
1739 -- with a controlling result.
1741 if Is_Private_Primitive_Subprogram (Subp_Id) then
1742 if No (Actuals) then
1743 Actuals := New_List;
1744 end if;
1746 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
1747 Prepend_To (Actuals,
1748 Unchecked_Convert_To
1749 (Corresponding_Concurrent_Type (Obj_Typ),
1750 Make_Identifier (Loc, Name_uO)));
1752 else
1753 Prepend_To (Actuals,
1754 Make_Identifier (Loc,
1755 Chars => Chars (Defining_Identifier (First_Form))));
1756 end if;
1758 Nam := New_Occurrence_Of (Subp_Id, Loc);
1759 else
1760 -- An access-to-variable object parameter requires an explicit
1761 -- dereference in the unchecked conversion. This case occurs
1762 -- when a protected entry wrapper must override an interface
1763 -- level procedure with interface access as first parameter.
1765 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
1767 if Nkind (Parameter_Type (First_Form)) =
1768 N_Access_Definition
1769 then
1770 Conv_Id :=
1771 Make_Explicit_Dereference (Loc,
1772 Prefix => Make_Identifier (Loc, Name_uO));
1773 else
1774 Conv_Id := Make_Identifier (Loc, Name_uO);
1775 end if;
1777 Nam :=
1778 Make_Selected_Component (Loc,
1779 Prefix =>
1780 Unchecked_Convert_To
1781 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
1782 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
1783 end if;
1785 -- Create the subprogram body. For a function, the call to the
1786 -- actual subprogram has to be converted to the corresponding
1787 -- record if it is a controlling result.
1789 if Ekind (Subp_Id) = E_Function then
1790 declare
1791 Res : Node_Id;
1793 begin
1794 Res :=
1795 Make_Function_Call (Loc,
1796 Name => Nam,
1797 Parameter_Associations => Actuals);
1799 if Has_Controlling_Result (Subp_Id) then
1800 Res :=
1801 Unchecked_Convert_To
1802 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
1803 end if;
1805 return
1806 Make_Subprogram_Body (Loc,
1807 Specification => Body_Spec,
1808 Declarations => Empty_List,
1809 Handled_Statement_Sequence =>
1810 Make_Handled_Sequence_Of_Statements (Loc,
1811 Statements => New_List (
1812 Make_Simple_Return_Statement (Loc, Res))));
1813 end;
1815 else
1816 return
1817 Make_Subprogram_Body (Loc,
1818 Specification => Body_Spec,
1819 Declarations => Empty_List,
1820 Handled_Statement_Sequence =>
1821 Make_Handled_Sequence_Of_Statements (Loc,
1822 Statements => New_List (
1823 Make_Procedure_Call_Statement (Loc,
1824 Name => Nam,
1825 Parameter_Associations => Actuals))));
1826 end if;
1827 end;
1828 end Build_Wrapper_Body;
1830 -- Start of processing for Build_Wrapper_Bodies
1832 begin
1833 if Is_Concurrent_Type (Typ) then
1834 Rec_Typ := Corresponding_Record_Type (Typ);
1835 else
1836 Rec_Typ := Typ;
1837 end if;
1839 -- Generate wrapper bodies for a concurrent type which implements an
1840 -- interface.
1842 if Present (Interfaces (Rec_Typ)) then
1843 declare
1844 Insert_Nod : Node_Id;
1845 Prim : Entity_Id;
1846 Prim_Elmt : Elmt_Id;
1847 Prim_Decl : Node_Id;
1848 Subp : Entity_Id;
1849 Wrap_Body : Node_Id;
1850 Wrap_Id : Entity_Id;
1852 begin
1853 Insert_Nod := N;
1855 -- Examine all primitive operations of the corresponding record
1856 -- type, looking for wrapper specs. Generate bodies in order to
1857 -- complete them.
1859 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
1860 while Present (Prim_Elmt) loop
1861 Prim := Node (Prim_Elmt);
1863 if (Ekind (Prim) = E_Function
1864 or else Ekind (Prim) = E_Procedure)
1865 and then Is_Primitive_Wrapper (Prim)
1866 then
1867 Subp := Wrapped_Entity (Prim);
1868 Prim_Decl := Parent (Parent (Prim));
1870 Wrap_Body :=
1871 Build_Wrapper_Body (Loc,
1872 Subp_Id => Subp,
1873 Obj_Typ => Rec_Typ,
1874 Formals => Parameter_Specifications (Parent (Subp)));
1875 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
1877 Set_Corresponding_Spec (Wrap_Body, Prim);
1878 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
1880 Insert_After (Insert_Nod, Wrap_Body);
1881 Insert_Nod := Wrap_Body;
1883 Analyze (Wrap_Body);
1884 end if;
1886 Next_Elmt (Prim_Elmt);
1887 end loop;
1888 end;
1889 end if;
1890 end Build_Wrapper_Bodies;
1892 ------------------------
1893 -- Build_Wrapper_Spec --
1894 ------------------------
1896 function Build_Wrapper_Spec
1897 (Subp_Id : Entity_Id;
1898 Obj_Typ : Entity_Id;
1899 Formals : List_Id) return Node_Id
1901 function Overriding_Possible
1902 (Iface_Op : Entity_Id;
1903 Wrapper : Entity_Id) return Boolean;
1904 -- Determine whether a primitive operation can be overridden by Wrapper.
1905 -- Iface_Op is the candidate primitive operation of an interface type,
1906 -- Wrapper is the generated entry wrapper.
1908 function Replicate_Formals
1909 (Loc : Source_Ptr;
1910 Formals : List_Id) return List_Id;
1911 -- An explicit parameter replication is required due to the Is_Entry_
1912 -- Formal flag being set for all the formals of an entry. The explicit
1913 -- replication removes the flag that would otherwise cause a different
1914 -- path of analysis.
1916 -------------------------
1917 -- Overriding_Possible --
1918 -------------------------
1920 function Overriding_Possible
1921 (Iface_Op : Entity_Id;
1922 Wrapper : Entity_Id) return Boolean
1924 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
1925 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
1927 function Type_Conformant_Parameters
1928 (Iface_Op_Params : List_Id;
1929 Wrapper_Params : List_Id) return Boolean;
1930 -- Determine whether the parameters of the generated entry wrapper
1931 -- and those of a primitive operation are type conformant. During
1932 -- this check, the first parameter of the primitive operation is
1933 -- skipped if it is a controlling argument: protected functions
1934 -- may have a controlling result.
1936 --------------------------------
1937 -- Type_Conformant_Parameters --
1938 --------------------------------
1940 function Type_Conformant_Parameters
1941 (Iface_Op_Params : List_Id;
1942 Wrapper_Params : List_Id) return Boolean
1944 Iface_Op_Param : Node_Id;
1945 Iface_Op_Typ : Entity_Id;
1946 Wrapper_Param : Node_Id;
1947 Wrapper_Typ : Entity_Id;
1949 begin
1950 -- Skip the first (controlling) parameter of primitive operation
1952 Iface_Op_Param := First (Iface_Op_Params);
1954 if Present (First_Formal (Iface_Op))
1955 and then Is_Controlling_Formal (First_Formal (Iface_Op))
1956 then
1957 Next (Iface_Op_Param);
1958 end if;
1960 Wrapper_Param := First (Wrapper_Params);
1961 while Present (Iface_Op_Param)
1962 and then Present (Wrapper_Param)
1963 loop
1964 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
1965 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
1967 -- The two parameters must be mode conformant
1969 if not Conforming_Types
1970 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
1971 then
1972 return False;
1973 end if;
1975 Next (Iface_Op_Param);
1976 Next (Wrapper_Param);
1977 end loop;
1979 -- One of the lists is longer than the other
1981 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
1982 return False;
1983 end if;
1985 return True;
1986 end Type_Conformant_Parameters;
1988 -- Start of processing for Overriding_Possible
1990 begin
1991 if Chars (Iface_Op) /= Chars (Wrapper) then
1992 return False;
1993 end if;
1995 -- If an inherited subprogram is implemented by a protected procedure
1996 -- or an entry, then the first parameter of the inherited subprogram
1997 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
1999 if Ekind (Iface_Op) = E_Procedure
2000 and then Present (Parameter_Specifications (Iface_Op_Spec))
2001 then
2002 declare
2003 Obj_Param : constant Node_Id :=
2004 First (Parameter_Specifications (Iface_Op_Spec));
2005 begin
2006 if not Out_Present (Obj_Param)
2007 and then Nkind (Parameter_Type (Obj_Param)) /=
2008 N_Access_Definition
2009 then
2010 return False;
2011 end if;
2012 end;
2013 end if;
2015 return
2016 Type_Conformant_Parameters
2017 (Parameter_Specifications (Iface_Op_Spec),
2018 Parameter_Specifications (Wrapper_Spec));
2019 end Overriding_Possible;
2021 -----------------------
2022 -- Replicate_Formals --
2023 -----------------------
2025 function Replicate_Formals
2026 (Loc : Source_Ptr;
2027 Formals : List_Id) return List_Id
2029 New_Formals : constant List_Id := New_List;
2030 Formal : Node_Id;
2031 Param_Type : Node_Id;
2033 begin
2034 Formal := First (Formals);
2036 -- Skip the object parameter when dealing with primitives declared
2037 -- between two views.
2039 if Is_Private_Primitive_Subprogram (Subp_Id)
2040 and then not Has_Controlling_Result (Subp_Id)
2041 then
2042 Next (Formal);
2043 end if;
2045 while Present (Formal) loop
2047 -- Create an explicit copy of the entry parameter
2049 -- When creating the wrapper subprogram for a primitive operation
2050 -- of a protected interface we must construct an equivalent
2051 -- signature to that of the overriding operation. For regular
2052 -- parameters we can just use the type of the formal, but for
2053 -- access to subprogram parameters we need to reanalyze the
2054 -- parameter type to create local entities for the signature of
2055 -- the subprogram type. Using the entities of the overriding
2056 -- subprogram will result in out-of-scope errors in the back-end.
2058 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2059 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2060 else
2061 Param_Type :=
2062 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2063 end if;
2065 Append_To (New_Formals,
2066 Make_Parameter_Specification (Loc,
2067 Defining_Identifier =>
2068 Make_Defining_Identifier (Loc,
2069 Chars => Chars (Defining_Identifier (Formal))),
2070 In_Present => In_Present (Formal),
2071 Out_Present => Out_Present (Formal),
2072 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2073 Parameter_Type => Param_Type));
2075 Next (Formal);
2076 end loop;
2078 return New_Formals;
2079 end Replicate_Formals;
2081 -- Local variables
2083 Loc : constant Source_Ptr := Sloc (Subp_Id);
2084 First_Param : Node_Id := Empty;
2085 Iface : Entity_Id;
2086 Iface_Elmt : Elmt_Id;
2087 Iface_Op : Entity_Id;
2088 Iface_Op_Elmt : Elmt_Id;
2089 Overridden_Subp : Entity_Id;
2091 -- Start of processing for Build_Wrapper_Spec
2093 begin
2094 -- No point in building wrappers for untagged concurrent types
2096 pragma Assert (Is_Tagged_Type (Obj_Typ));
2098 -- Check if this subprogram has a profile that matches some interface
2099 -- primitive.
2101 Check_Synchronized_Overriding (Subp_Id, Overridden_Subp);
2103 if Present (Overridden_Subp) then
2104 First_Param :=
2105 First (Parameter_Specifications (Parent (Overridden_Subp)));
2107 -- An entry or a protected procedure can override a routine where the
2108 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2109 -- type. Since the wrapper must have the exact same signature as that of
2110 -- the overridden subprogram, we try to find the overriding candidate
2111 -- and use its controlling formal.
2113 -- Check every implemented interface
2115 elsif Present (Interfaces (Obj_Typ)) then
2116 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2117 Search : while Present (Iface_Elmt) loop
2118 Iface := Node (Iface_Elmt);
2120 -- Check every interface primitive
2122 if Present (Primitive_Operations (Iface)) then
2123 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2124 while Present (Iface_Op_Elmt) loop
2125 Iface_Op := Node (Iface_Op_Elmt);
2127 -- Ignore predefined primitives
2129 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2130 Iface_Op := Ultimate_Alias (Iface_Op);
2132 -- The current primitive operation can be overridden by
2133 -- the generated entry wrapper.
2135 if Overriding_Possible (Iface_Op, Subp_Id) then
2136 First_Param :=
2137 First (Parameter_Specifications (Parent (Iface_Op)));
2139 exit Search;
2140 end if;
2141 end if;
2143 Next_Elmt (Iface_Op_Elmt);
2144 end loop;
2145 end if;
2147 Next_Elmt (Iface_Elmt);
2148 end loop Search;
2149 end if;
2151 -- Do not generate the wrapper if no interface primitive is covered by
2152 -- the subprogram and it is not a primitive declared between two views
2153 -- (see Process_Full_View).
2155 if No (First_Param)
2156 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2157 then
2158 return Empty;
2159 end if;
2161 declare
2162 Wrapper_Id : constant Entity_Id :=
2163 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2164 New_Formals : List_Id;
2165 Obj_Param : Node_Id;
2166 Obj_Param_Typ : Entity_Id;
2168 begin
2169 -- Minimum decoration is needed to catch the entity in
2170 -- Sem_Ch6.Override_Dispatching_Operation.
2172 if Ekind (Subp_Id) = E_Function then
2173 Mutate_Ekind (Wrapper_Id, E_Function);
2174 else
2175 Mutate_Ekind (Wrapper_Id, E_Procedure);
2176 end if;
2178 Set_Is_Primitive_Wrapper (Wrapper_Id);
2179 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2180 Set_Is_Private_Primitive (Wrapper_Id,
2181 Is_Private_Primitive_Subprogram (Subp_Id));
2183 -- Process the formals
2185 New_Formals := Replicate_Formals (Loc, Formals);
2187 -- A function with a controlling result and no first controlling
2188 -- formal needs no additional parameter.
2190 if Has_Controlling_Result (Subp_Id)
2191 and then
2192 (No (First_Formal (Subp_Id))
2193 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2194 then
2195 null;
2197 -- Routine Subp_Id has been found to override an interface primitive.
2198 -- If the interface operation has an access parameter, create a copy
2199 -- of it, with the same null exclusion indicator if present.
2201 elsif Present (First_Param) then
2202 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2203 Obj_Param_Typ :=
2204 Make_Access_Definition (Loc,
2205 Subtype_Mark =>
2206 New_Occurrence_Of (Obj_Typ, Loc),
2207 Null_Exclusion_Present =>
2208 Null_Exclusion_Present (Parameter_Type (First_Param)),
2209 Constant_Present =>
2210 Constant_Present (Parameter_Type (First_Param)));
2211 else
2212 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2213 end if;
2215 Obj_Param :=
2216 Make_Parameter_Specification (Loc,
2217 Defining_Identifier =>
2218 Make_Defining_Identifier (Loc,
2219 Chars => Name_uO),
2220 In_Present => In_Present (First_Param),
2221 Out_Present => Out_Present (First_Param),
2222 Parameter_Type => Obj_Param_Typ);
2224 Prepend_To (New_Formals, Obj_Param);
2226 -- If we are dealing with a primitive declared between two views,
2227 -- implemented by a synchronized operation, we need to create
2228 -- a default parameter. The mode of the parameter must match that
2229 -- of the primitive operation.
2231 else
2232 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2234 Obj_Param :=
2235 Make_Parameter_Specification (Loc,
2236 Defining_Identifier =>
2237 Make_Defining_Identifier (Loc, Name_uO),
2238 In_Present =>
2239 In_Present (Parent (First_Entity (Subp_Id))),
2240 Out_Present => Ekind (Subp_Id) /= E_Function,
2241 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2243 Prepend_To (New_Formals, Obj_Param);
2244 end if;
2246 -- Build the final spec. If it is a function with a controlling
2247 -- result, it is a primitive operation of the corresponding
2248 -- record type, so mark the spec accordingly.
2250 if Ekind (Subp_Id) = E_Function then
2251 declare
2252 Res_Def : Node_Id;
2254 begin
2255 if Has_Controlling_Result (Subp_Id) then
2256 Res_Def :=
2257 New_Occurrence_Of
2258 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2259 else
2260 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2261 end if;
2263 return
2264 Make_Function_Specification (Loc,
2265 Defining_Unit_Name => Wrapper_Id,
2266 Parameter_Specifications => New_Formals,
2267 Result_Definition => Res_Def);
2268 end;
2269 else
2270 return
2271 Make_Procedure_Specification (Loc,
2272 Defining_Unit_Name => Wrapper_Id,
2273 Parameter_Specifications => New_Formals);
2274 end if;
2275 end;
2276 end Build_Wrapper_Spec;
2278 -------------------------
2279 -- Build_Wrapper_Specs --
2280 -------------------------
2282 procedure Build_Wrapper_Specs
2283 (Loc : Source_Ptr;
2284 Typ : Entity_Id;
2285 N : in out Node_Id)
2287 Def : Node_Id;
2288 Rec_Typ : Entity_Id;
2289 procedure Scan_Declarations (L : List_Id);
2290 -- Common processing for visible and private declarations
2291 -- of a protected type.
2293 procedure Scan_Declarations (L : List_Id) is
2294 Decl : Node_Id;
2295 Wrap_Decl : Node_Id;
2296 Wrap_Spec : Node_Id;
2298 begin
2299 if No (L) then
2300 return;
2301 end if;
2303 Decl := First (L);
2304 while Present (Decl) loop
2305 Wrap_Spec := Empty;
2307 if Nkind (Decl) = N_Entry_Declaration
2308 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2309 then
2310 Wrap_Spec :=
2311 Build_Wrapper_Spec
2312 (Subp_Id => Defining_Identifier (Decl),
2313 Obj_Typ => Rec_Typ,
2314 Formals => Parameter_Specifications (Decl));
2316 elsif Nkind (Decl) = N_Subprogram_Declaration then
2317 Wrap_Spec :=
2318 Build_Wrapper_Spec
2319 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2320 Obj_Typ => Rec_Typ,
2321 Formals =>
2322 Parameter_Specifications (Specification (Decl)));
2323 end if;
2325 if Present (Wrap_Spec) then
2326 Wrap_Decl :=
2327 Make_Subprogram_Declaration (Loc,
2328 Specification => Wrap_Spec);
2330 Insert_After (N, Wrap_Decl);
2331 N := Wrap_Decl;
2333 Analyze (Wrap_Decl);
2334 end if;
2336 Next (Decl);
2337 end loop;
2338 end Scan_Declarations;
2340 -- start of processing for Build_Wrapper_Specs
2342 begin
2343 if Is_Protected_Type (Typ) then
2344 Def := Protected_Definition (Parent (Typ));
2345 else pragma Assert (Is_Task_Type (Typ));
2346 Def := Task_Definition (Parent (Typ));
2347 end if;
2349 Rec_Typ := Corresponding_Record_Type (Typ);
2351 -- Generate wrapper specs for a concurrent type which implements an
2352 -- interface. Operations in both the visible and private parts may
2353 -- implement progenitor operations.
2355 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2356 Scan_Declarations (Visible_Declarations (Def));
2357 Scan_Declarations (Private_Declarations (Def));
2358 end if;
2359 end Build_Wrapper_Specs;
2361 ---------------------------
2362 -- Build_Find_Body_Index --
2363 ---------------------------
2365 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2366 Loc : constant Source_Ptr := Sloc (Typ);
2367 Ent : Entity_Id;
2368 E_Typ : Entity_Id;
2369 Has_F : Boolean := False;
2370 Index : Nat;
2371 If_St : Node_Id := Empty;
2372 Lo : Node_Id;
2373 Hi : Node_Id;
2374 Decls : List_Id := New_List;
2375 Ret : Node_Id := Empty;
2376 Spec : Node_Id;
2377 Siz : Node_Id := Empty;
2379 procedure Add_If_Clause (Expr : Node_Id);
2380 -- Add test for range of current entry
2382 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2383 -- If a bound of an entry is given by a discriminant, retrieve the
2384 -- actual value of the discriminant from the enclosing object.
2386 -------------------
2387 -- Add_If_Clause --
2388 -------------------
2390 procedure Add_If_Clause (Expr : Node_Id) is
2391 Cond : Node_Id;
2392 Stats : constant List_Id :=
2393 New_List (
2394 Make_Simple_Return_Statement (Loc,
2395 Expression => Make_Integer_Literal (Loc, Index + 1)));
2397 begin
2398 -- Index for current entry body
2400 Index := Index + 1;
2402 -- Compute total length of entry queues so far
2404 if No (Siz) then
2405 Siz := Expr;
2406 else
2407 Siz :=
2408 Make_Op_Add (Loc,
2409 Left_Opnd => Siz,
2410 Right_Opnd => Expr);
2411 end if;
2413 Cond :=
2414 Make_Op_Le (Loc,
2415 Left_Opnd => Make_Identifier (Loc, Name_uE),
2416 Right_Opnd => Siz);
2418 -- Map entry queue indexes in the range of the current family
2419 -- into the current index, that designates the entry body.
2421 if No (If_St) then
2422 If_St :=
2423 Make_Implicit_If_Statement (Typ,
2424 Condition => Cond,
2425 Then_Statements => Stats,
2426 Elsif_Parts => New_List);
2427 Ret := If_St;
2429 else
2430 Append_To (Elsif_Parts (If_St),
2431 Make_Elsif_Part (Loc,
2432 Condition => Cond,
2433 Then_Statements => Stats));
2434 end if;
2435 end Add_If_Clause;
2437 ------------------------------
2438 -- Convert_Discriminant_Ref --
2439 ------------------------------
2441 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2442 B : Node_Id;
2444 begin
2445 if Is_Entity_Name (Bound)
2446 and then Ekind (Entity (Bound)) = E_Discriminant
2447 then
2448 B :=
2449 Make_Selected_Component (Loc,
2450 Prefix =>
2451 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2452 Make_Explicit_Dereference (Loc,
2453 Make_Identifier (Loc, Name_uObject))),
2454 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2455 Set_Etype (B, Etype (Entity (Bound)));
2456 else
2457 B := New_Copy_Tree (Bound);
2458 end if;
2460 return B;
2461 end Convert_Discriminant_Ref;
2463 -- Start of processing for Build_Find_Body_Index
2465 begin
2466 Spec := Build_Find_Body_Index_Spec (Typ);
2468 Ent := First_Entity (Typ);
2469 while Present (Ent) loop
2470 if Ekind (Ent) = E_Entry_Family then
2471 Has_F := True;
2472 exit;
2473 end if;
2475 Next_Entity (Ent);
2476 end loop;
2478 if not Has_F then
2480 -- If the protected type has no entry families, there is a one-one
2481 -- correspondence between entry queue and entry body.
2483 Ret :=
2484 Make_Simple_Return_Statement (Loc,
2485 Expression => Make_Identifier (Loc, Name_uE));
2487 else
2488 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2489 -- the following:
2491 -- if E <= l1 then return 1;
2492 -- elsif E <= l1 + l2 then return 2;
2493 -- ...
2495 Index := 0;
2496 Siz := Empty;
2497 Ent := First_Entity (Typ);
2499 Add_Object_Pointer (Loc, Typ, Decls);
2501 while Present (Ent) loop
2502 if Ekind (Ent) = E_Entry then
2503 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2505 elsif Ekind (Ent) = E_Entry_Family then
2506 E_Typ := Entry_Index_Type (Ent);
2507 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2508 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2509 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2510 end if;
2512 Next_Entity (Ent);
2513 end loop;
2515 if Index = 1 then
2516 Decls := New_List;
2517 Ret :=
2518 Make_Simple_Return_Statement (Loc,
2519 Expression => Make_Integer_Literal (Loc, 1));
2521 else
2522 -- Ranges are in increasing order, so last one doesn't need guard
2524 declare
2525 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2526 begin
2527 Remove (Nod);
2528 Set_Else_Statements (Ret, Then_Statements (Nod));
2530 -- If Elsif_Parts becomes empty then remove it entirely, as
2531 -- otherwise we would violate the invariant of If_Statement
2532 -- node described in Sinfo.
2534 if Is_Empty_List (Elsif_Parts (Ret)) then
2535 pragma Assert (Elsif_Parts (Ret) /= No_List);
2536 Set_Elsif_Parts (Ret, No_List);
2537 end if;
2538 end;
2539 end if;
2540 end if;
2542 return
2543 Make_Subprogram_Body (Loc,
2544 Specification => Spec,
2545 Declarations => Decls,
2546 Handled_Statement_Sequence =>
2547 Make_Handled_Sequence_Of_Statements (Loc,
2548 Statements => New_List (Ret)));
2549 end Build_Find_Body_Index;
2551 --------------------------------
2552 -- Build_Find_Body_Index_Spec --
2553 --------------------------------
2555 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2556 Loc : constant Source_Ptr := Sloc (Typ);
2557 Id : constant Entity_Id :=
2558 Make_Defining_Identifier (Loc,
2559 Chars => New_External_Name (Chars (Typ), 'F'));
2560 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2561 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2563 begin
2564 return
2565 Make_Function_Specification (Loc,
2566 Defining_Unit_Name => Id,
2567 Parameter_Specifications => New_List (
2568 Make_Parameter_Specification (Loc,
2569 Defining_Identifier => Parm1,
2570 Parameter_Type =>
2571 New_Occurrence_Of (RTE (RE_Address), Loc)),
2573 Make_Parameter_Specification (Loc,
2574 Defining_Identifier => Parm2,
2575 Parameter_Type =>
2576 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
2578 Result_Definition => New_Occurrence_Of (
2579 RTE (RE_Protected_Entry_Index), Loc));
2580 end Build_Find_Body_Index_Spec;
2582 -----------------------------------------------
2583 -- Build_Lock_Free_Protected_Subprogram_Body --
2584 -----------------------------------------------
2586 function Build_Lock_Free_Protected_Subprogram_Body
2587 (N : Node_Id;
2588 Prot_Typ : Node_Id;
2589 Unprot_Spec : Node_Id) return Node_Id
2591 Actuals : constant List_Id := New_List;
2592 Loc : constant Source_Ptr := Sloc (N);
2593 Spec : constant Node_Id := Specification (N);
2594 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
2595 Formal : Node_Id;
2596 Prot_Spec : Node_Id;
2597 Stmt : Node_Id;
2599 begin
2600 -- Create the protected version of the body
2602 Prot_Spec :=
2603 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
2605 -- Build the actual parameters which appear in the call to the
2606 -- unprotected version of the body.
2608 Formal := First (Parameter_Specifications (Prot_Spec));
2609 while Present (Formal) loop
2610 Append_To (Actuals,
2611 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2613 Next (Formal);
2614 end loop;
2616 -- Function case, generate:
2617 -- return <Unprot_Func_Call>;
2619 if Nkind (Spec) = N_Function_Specification then
2620 Stmt :=
2621 Make_Simple_Return_Statement (Loc,
2622 Expression =>
2623 Make_Function_Call (Loc,
2624 Name =>
2625 Make_Identifier (Loc, Chars (Unprot_Id)),
2626 Parameter_Associations => Actuals));
2628 -- Procedure case, call the unprotected version
2630 else
2631 Stmt :=
2632 Make_Procedure_Call_Statement (Loc,
2633 Name =>
2634 Make_Identifier (Loc, Chars (Unprot_Id)),
2635 Parameter_Associations => Actuals);
2636 end if;
2638 return
2639 Make_Subprogram_Body (Loc,
2640 Declarations => Empty_List,
2641 Specification => Prot_Spec,
2642 Handled_Statement_Sequence =>
2643 Make_Handled_Sequence_Of_Statements (Loc,
2644 Statements => New_List (Stmt)));
2645 end Build_Lock_Free_Protected_Subprogram_Body;
2647 -------------------------------------------------
2648 -- Build_Lock_Free_Unprotected_Subprogram_Body --
2649 -------------------------------------------------
2651 -- Procedures which meet the lock-free implementation requirements and
2652 -- reference a unique scalar component Comp are expanded in the following
2653 -- manner:
2655 -- procedure P (...) is
2656 -- Expected_Comp : constant Comp_Type :=
2657 -- Comp_Type
2658 -- (System.Atomic_Primitives.Lock_Free_Read_N
2659 -- (_Object.Comp'Address));
2660 -- begin
2661 -- loop
2662 -- declare
2663 -- <original declarations before the object renaming declaration
2664 -- of Comp>
2666 -- Desired_Comp : Comp_Type := Expected_Comp;
2667 -- Comp : Comp_Type renames Desired_Comp;
2669 -- <original declarations after the object renaming declaration
2670 -- of Comp>
2672 -- begin
2673 -- <original statements>
2674 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
2675 -- (_Object.Comp'Address,
2676 -- Interfaces.Unsigned_N (Expected_Comp),
2677 -- Interfaces.Unsigned_N (Desired_Comp));
2678 -- end;
2679 -- end loop;
2680 -- end P;
2682 -- Each return and raise statement of P is transformed into an atomic
2683 -- status check:
2685 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2686 -- (_Object.Comp'Address,
2687 -- Interfaces.Unsigned_N (Expected_Comp),
2688 -- Interfaces.Unsigned_N (Desired_Comp));
2689 -- then
2690 -- <original statement>
2691 -- else
2692 -- goto L0;
2693 -- end if;
2695 -- Functions which meet the lock-free implementation requirements and
2696 -- reference a unique scalar component Comp are expanded in the following
2697 -- manner:
2699 -- function F (...) return ... is
2700 -- <original declarations before the object renaming declaration
2701 -- of Comp>
2703 -- Expected_Comp : constant Comp_Type :=
2704 -- Comp_Type
2705 -- (System.Atomic_Primitives.Lock_Free_Read_N
2706 -- (_Object.Comp'Address));
2707 -- Comp : Comp_Type renames Expected_Comp;
2709 -- <original declarations after the object renaming declaration of
2710 -- Comp>
2712 -- begin
2713 -- <original statements>
2714 -- end F;
2716 function Build_Lock_Free_Unprotected_Subprogram_Body
2717 (N : Node_Id;
2718 Prot_Typ : Node_Id) return Node_Id
2720 function Referenced_Component (N : Node_Id) return Entity_Id;
2721 -- Subprograms which meet the lock-free implementation criteria are
2722 -- allowed to reference only one unique component. Return the prival
2723 -- of the said component.
2725 --------------------------
2726 -- Referenced_Component --
2727 --------------------------
2729 function Referenced_Component (N : Node_Id) return Entity_Id is
2730 Comp : Entity_Id;
2731 Decl : Node_Id;
2732 Source_Comp : Entity_Id := Empty;
2734 begin
2735 -- Find the unique source component which N references in its
2736 -- statements.
2738 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
2739 declare
2740 Element : Lock_Free_Subprogram renames
2741 Lock_Free_Subprogram_Table.Table (Index);
2742 begin
2743 if Element.Sub_Body = N then
2744 Source_Comp := Element.Comp_Id;
2745 exit;
2746 end if;
2747 end;
2748 end loop;
2750 if No (Source_Comp) then
2751 return Empty;
2752 end if;
2754 -- Find the prival which corresponds to the source component within
2755 -- the declarations of N.
2757 Decl := First (Declarations (N));
2758 while Present (Decl) loop
2760 -- Privals appear as object renamings
2762 if Nkind (Decl) = N_Object_Renaming_Declaration then
2763 Comp := Defining_Identifier (Decl);
2765 if Present (Prival_Link (Comp))
2766 and then Prival_Link (Comp) = Source_Comp
2767 then
2768 return Comp;
2769 end if;
2770 end if;
2772 Next (Decl);
2773 end loop;
2775 return Empty;
2776 end Referenced_Component;
2778 -- Local variables
2780 Comp : constant Entity_Id := Referenced_Component (N);
2781 Loc : constant Source_Ptr := Sloc (N);
2782 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
2783 Decls : List_Id := Declarations (N);
2785 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
2787 begin
2788 -- Add renamings for the protection object, discriminals, privals, and
2789 -- the entry index constant for use by debugger.
2791 Debug_Private_Data_Declarations (Decls);
2793 -- Perform the lock-free expansion when the subprogram references a
2794 -- protected component.
2796 if Present (Comp) then
2797 Protected_Component_Ref : declare
2798 Comp_Decl : constant Node_Id := Parent (Comp);
2799 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
2800 Comp_Type : constant Entity_Id := Etype (Comp);
2802 Is_Procedure : constant Boolean :=
2803 Ekind (Corresponding_Spec (N)) = E_Procedure;
2804 -- Indicates if N is a protected procedure body
2806 Block_Decls : List_Id := No_List;
2807 Try_Write : Entity_Id;
2808 Desired_Comp : Entity_Id;
2809 Decl : Node_Id;
2810 Label : Node_Id;
2811 Label_Id : Entity_Id := Empty;
2812 Read : Entity_Id;
2813 Expected_Comp : Entity_Id;
2814 Stmt : Node_Id;
2815 Stmts : List_Id :=
2816 New_Copy_List_Tree (Statements (Hand_Stmt_Seq));
2817 Typ_Size : Int;
2818 Unsigned : Entity_Id;
2820 function Process_Node (N : Node_Id) return Traverse_Result;
2821 -- Transform a single node if it is a return statement, a raise
2822 -- statement or a reference to Comp.
2824 procedure Process_Stmts (Stmts : List_Id);
2825 -- Given a statement sequence Stmts, wrap any return or raise
2826 -- statements in the following manner:
2828 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2829 -- (_Object.Comp'Address,
2830 -- Interfaces.Unsigned_N (Expected_Comp),
2831 -- Interfaces.Unsigned_N (Desired_Comp))
2832 -- then
2833 -- <Stmt>;
2834 -- else
2835 -- goto L0;
2836 -- end if;
2838 ------------------
2839 -- Process_Node --
2840 ------------------
2842 function Process_Node (N : Node_Id) return Traverse_Result is
2844 procedure Wrap_Statement (Stmt : Node_Id);
2845 -- Wrap an arbitrary statement inside an if statement where the
2846 -- condition does an atomic check on the state of the object.
2848 --------------------
2849 -- Wrap_Statement --
2850 --------------------
2852 procedure Wrap_Statement (Stmt : Node_Id) is
2853 begin
2854 -- The first time through, create the declaration of a label
2855 -- which is used to skip the remainder of source statements
2856 -- if the state of the object has changed.
2858 if No (Label_Id) then
2859 Label_Id :=
2860 Make_Identifier (Loc, New_External_Name ('L', 0));
2861 Set_Entity (Label_Id,
2862 Make_Defining_Identifier (Loc, Chars (Label_Id)));
2863 end if;
2865 -- Generate:
2866 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2867 -- (_Object.Comp'Address,
2868 -- Interfaces.Unsigned_N (Expected_Comp),
2869 -- Interfaces.Unsigned_N (Desired_Comp))
2870 -- then
2871 -- <Stmt>;
2872 -- else
2873 -- goto L0;
2874 -- end if;
2876 Rewrite (Stmt,
2877 Make_Implicit_If_Statement (N,
2878 Condition =>
2879 Make_Function_Call (Loc,
2880 Name =>
2881 New_Occurrence_Of (Try_Write, Loc),
2882 Parameter_Associations => New_List (
2883 Make_Attribute_Reference (Loc,
2884 Prefix => Relocate_Node (Comp_Sel_Nam),
2885 Attribute_Name => Name_Address),
2887 Unchecked_Convert_To (Unsigned,
2888 New_Occurrence_Of (Expected_Comp, Loc)),
2890 Unchecked_Convert_To (Unsigned,
2891 New_Occurrence_Of (Desired_Comp, Loc)))),
2893 Then_Statements => New_List (Relocate_Node (Stmt)),
2895 Else_Statements => New_List (
2896 Make_Goto_Statement (Loc,
2897 Name =>
2898 New_Occurrence_Of (Entity (Label_Id), Loc)))));
2899 end Wrap_Statement;
2901 -- Start of processing for Process_Node
2903 begin
2904 -- Wrap each return and raise statement that appear inside a
2905 -- procedure. Skip the last return statement which is added by
2906 -- default since it is transformed into an exit statement.
2908 if Is_Procedure
2909 and then ((Nkind (N) = N_Simple_Return_Statement
2910 and then N /= Last (Stmts))
2911 or else Nkind (N) = N_Extended_Return_Statement
2912 or else (Nkind (N) in
2913 N_Raise_xxx_Error | N_Raise_Statement
2914 and then Comes_From_Source (N)))
2915 then
2916 Wrap_Statement (N);
2917 return Skip;
2918 end if;
2920 -- Force reanalysis
2922 Set_Analyzed (N, False);
2924 return OK;
2925 end Process_Node;
2927 procedure Process_Nodes is new Traverse_Proc (Process_Node);
2929 -------------------
2930 -- Process_Stmts --
2931 -------------------
2933 procedure Process_Stmts (Stmts : List_Id) is
2934 Stmt : Node_Id;
2935 begin
2936 Stmt := First (Stmts);
2937 while Present (Stmt) loop
2938 Process_Nodes (Stmt);
2939 Next (Stmt);
2940 end loop;
2941 end Process_Stmts;
2943 -- Start of processing for Protected_Component_Ref
2945 begin
2946 -- Get the type size
2948 if Known_Static_Esize (Comp_Type) then
2949 Typ_Size := UI_To_Int (Esize (Comp_Type));
2951 -- If the Esize (Object_Size) is unknown at compile time, look at
2952 -- the RM_Size (Value_Size) since it may have been set by an
2953 -- explicit representation clause.
2955 elsif Known_Static_RM_Size (Comp_Type) then
2956 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
2958 -- Should not happen since this has already been checked in
2959 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
2961 else
2962 raise Program_Error;
2963 end if;
2965 -- Retrieve all relevant atomic routines and types
2967 case Typ_Size is
2968 when 8 =>
2969 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
2970 Read := RTE (RE_Lock_Free_Read_8);
2971 Unsigned := RTE (RE_Uint8);
2973 when 16 =>
2974 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
2975 Read := RTE (RE_Lock_Free_Read_16);
2976 Unsigned := RTE (RE_Uint16);
2978 when 32 =>
2979 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
2980 Read := RTE (RE_Lock_Free_Read_32);
2981 Unsigned := RTE (RE_Uint32);
2983 when 64 =>
2984 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
2985 Read := RTE (RE_Lock_Free_Read_64);
2986 Unsigned := RTE (RE_Uint64);
2988 when others =>
2989 raise Program_Error;
2990 end case;
2992 -- Generate:
2993 -- Expected_Comp : constant Comp_Type :=
2994 -- Comp_Type
2995 -- (System.Atomic_Primitives.Lock_Free_Read_N
2996 -- (_Object.Comp'Address));
2998 Expected_Comp :=
2999 Make_Defining_Identifier (Loc,
3000 New_External_Name (Chars (Comp), Suffix => "_saved"));
3002 Decl :=
3003 Make_Object_Declaration (Loc,
3004 Defining_Identifier => Expected_Comp,
3005 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3006 Constant_Present => True,
3007 Expression =>
3008 Unchecked_Convert_To (Comp_Type,
3009 Make_Function_Call (Loc,
3010 Name => New_Occurrence_Of (Read, Loc),
3011 Parameter_Associations => New_List (
3012 Make_Attribute_Reference (Loc,
3013 Prefix => Relocate_Node (Comp_Sel_Nam),
3014 Attribute_Name => Name_Address)))));
3016 -- Protected procedures
3018 if Is_Procedure then
3019 -- Move the original declarations inside the generated block
3021 Block_Decls := Decls;
3023 -- Reset the declarations list of the protected procedure to
3024 -- contain only Decl.
3026 Decls := New_List (Decl);
3028 -- Generate:
3029 -- Desired_Comp : Comp_Type := Expected_Comp;
3031 Desired_Comp :=
3032 Make_Defining_Identifier (Loc,
3033 New_External_Name (Chars (Comp), Suffix => "_current"));
3035 -- Insert the declarations of Expected_Comp and Desired_Comp in
3036 -- the block declarations right before the renaming of the
3037 -- protected component.
3039 Insert_Before (Comp_Decl,
3040 Make_Object_Declaration (Loc,
3041 Defining_Identifier => Desired_Comp,
3042 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3043 Expression =>
3044 New_Occurrence_Of (Expected_Comp, Loc)));
3046 -- Protected function
3048 else
3049 Desired_Comp := Expected_Comp;
3051 -- Insert the declaration of Expected_Comp in the function
3052 -- declarations right before the renaming of the protected
3053 -- component.
3055 Insert_Before (Comp_Decl, Decl);
3056 end if;
3058 -- Rewrite the protected component renaming declaration to be a
3059 -- renaming of Desired_Comp.
3061 -- Generate:
3062 -- Comp : Comp_Type renames Desired_Comp;
3064 Rewrite (Comp_Decl,
3065 Make_Object_Renaming_Declaration (Loc,
3066 Defining_Identifier =>
3067 Defining_Identifier (Comp_Decl),
3068 Subtype_Mark =>
3069 New_Occurrence_Of (Comp_Type, Loc),
3070 Name =>
3071 New_Occurrence_Of (Desired_Comp, Loc)));
3073 -- Wrap any return or raise statements in Stmts in same the manner
3074 -- described in Process_Stmts.
3076 Process_Stmts (Stmts);
3078 -- Generate:
3079 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3080 -- (_Object.Comp'Address,
3081 -- Interfaces.Unsigned_N (Expected_Comp),
3082 -- Interfaces.Unsigned_N (Desired_Comp))
3084 if Is_Procedure then
3085 Stmt :=
3086 Make_Exit_Statement (Loc,
3087 Condition =>
3088 Make_Function_Call (Loc,
3089 Name =>
3090 New_Occurrence_Of (Try_Write, Loc),
3091 Parameter_Associations => New_List (
3092 Make_Attribute_Reference (Loc,
3093 Prefix => Relocate_Node (Comp_Sel_Nam),
3094 Attribute_Name => Name_Address),
3096 Unchecked_Convert_To (Unsigned,
3097 New_Occurrence_Of (Expected_Comp, Loc)),
3099 Unchecked_Convert_To (Unsigned,
3100 New_Occurrence_Of (Desired_Comp, Loc)))));
3102 -- Small optimization: transform the default return statement
3103 -- of a procedure into the atomic exit statement.
3105 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3106 Rewrite (Last (Stmts), Stmt);
3107 else
3108 Append_To (Stmts, Stmt);
3109 end if;
3110 end if;
3112 -- Create the declaration of the label used to skip the rest of
3113 -- the source statements when the object state changes.
3115 if Present (Label_Id) then
3116 Label := Make_Label (Loc, Label_Id);
3117 Append_To (Decls,
3118 Make_Implicit_Label_Declaration (Loc,
3119 Defining_Identifier => Entity (Label_Id),
3120 Label_Construct => Label));
3121 Append_To (Stmts, Label);
3122 end if;
3124 -- Generate:
3125 -- loop
3126 -- declare
3127 -- <Decls>
3128 -- begin
3129 -- <Stmts>
3130 -- end;
3131 -- end loop;
3133 if Is_Procedure then
3134 Stmts :=
3135 New_List (
3136 Make_Loop_Statement (Loc,
3137 Statements => New_List (
3138 Make_Block_Statement (Loc,
3139 Declarations => Block_Decls,
3140 Handled_Statement_Sequence =>
3141 Make_Handled_Sequence_Of_Statements (Loc,
3142 Statements => Stmts))),
3143 End_Label => Empty));
3144 end if;
3146 Hand_Stmt_Seq :=
3147 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3148 end Protected_Component_Ref;
3149 end if;
3151 -- Make an unprotected version of the subprogram for use within the same
3152 -- object, with new name and extra parameter representing the object.
3154 return
3155 Make_Subprogram_Body (Loc,
3156 Specification =>
3157 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3158 Declarations => Decls,
3159 Handled_Statement_Sequence => Hand_Stmt_Seq);
3160 end Build_Lock_Free_Unprotected_Subprogram_Body;
3162 -------------------------
3163 -- Build_Master_Entity --
3164 -------------------------
3166 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3167 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3168 Context : Node_Id;
3169 Context_Id : Entity_Id;
3170 Decl : Node_Id;
3171 Decls : List_Id;
3172 Par : Node_Id;
3174 begin
3175 -- No action needed if the run-time has no tasking support
3177 if Global_No_Tasking then
3178 return;
3179 end if;
3181 if Is_Itype (Obj_Or_Typ) then
3182 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3183 else
3184 Par := Parent (Obj_Or_Typ);
3185 end if;
3187 -- When creating a master for a record component which is either a task
3188 -- or access-to-task, the enclosing record is the master scope and the
3189 -- proper insertion point is the component list.
3191 if Is_Record_Type (Current_Scope) then
3192 Context := Par;
3193 Context_Id := Current_Scope;
3194 Decls := List_Containing (Context);
3196 -- Default case for object declarations and access types. Note that the
3197 -- context is updated to the nearest enclosing body, block, package, or
3198 -- return statement.
3200 else
3201 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3202 end if;
3204 -- When the enclosing context is a BIP function whose result type has
3205 -- tasks, the function has an extra formal that is the master of the
3206 -- tasks to be created by its returned object (that is, when its
3207 -- enclosing context is a return statement). However, if the body of
3208 -- the function creates tasks before its return statements, such tasks
3209 -- need their own master.
3211 if Has_Master_Entity (Context_Id)
3212 and then Ekind (Context_Id) = E_Function
3213 and then Is_Build_In_Place_Function (Context_Id)
3214 and then Needs_BIP_Task_Actuals (Context_Id)
3215 then
3216 -- No need to add it again if previously added
3218 declare
3219 Master_Present : Boolean;
3221 begin
3222 -- Handle transient scopes
3224 if Context_Id /= Current_Scope then
3225 Push_Scope (Context_Id);
3226 Master_Present :=
3227 Present (Current_Entity_In_Scope (Name_uMaster));
3228 Pop_Scope;
3229 else
3230 Master_Present :=
3231 Present (Current_Entity_In_Scope (Name_uMaster));
3232 end if;
3234 if Master_Present then
3235 return;
3236 end if;
3237 end;
3239 -- Nothing to do if the context already has a master; internally built
3240 -- finalizers don't need a master.
3242 elsif Has_Master_Entity (Context_Id)
3243 or else Is_Finalizer (Context_Id)
3244 then
3245 return;
3246 end if;
3248 Decl := Build_Master_Declaration (Loc);
3250 -- The master is inserted at the start of the declarative list of the
3251 -- context.
3253 Prepend_To (Decls, Decl);
3255 -- In certain cases where transient scopes are involved, the immediate
3256 -- scope is not always the proper master scope. Ensure that the master
3257 -- declaration and entity appear in the same context.
3259 if Context_Id /= Current_Scope then
3260 Push_Scope (Context_Id);
3261 Analyze (Decl);
3262 Pop_Scope;
3263 else
3264 Analyze (Decl);
3265 end if;
3267 -- Mark the enclosing scope and its associated construct as being task
3268 -- masters.
3270 Set_Has_Master_Entity (Context_Id);
3272 while Present (Context)
3273 and then Nkind (Context) /= N_Compilation_Unit
3274 loop
3275 if Nkind (Context) in
3276 N_Block_Statement | N_Subprogram_Body | N_Task_Body
3277 then
3278 Set_Is_Task_Master (Context);
3279 exit;
3281 elsif Nkind (Parent (Context)) = N_Subunit then
3282 Context := Corresponding_Stub (Parent (Context));
3283 end if;
3285 Context := Parent (Context);
3286 end loop;
3287 end Build_Master_Entity;
3289 ---------------------------
3290 -- Build_Master_Renaming --
3291 ---------------------------
3293 procedure Build_Master_Renaming
3294 (Ptr_Typ : Entity_Id;
3295 Ins_Nod : Node_Id := Empty)
3297 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3298 Context : Node_Id;
3299 Master_Decl : Node_Id;
3300 Master_Id : Entity_Id;
3302 begin
3303 -- No action needed if the run-time has no tasking support
3305 if Global_No_Tasking then
3306 return;
3307 end if;
3309 -- Determine the proper context to insert the master renaming
3311 if Present (Ins_Nod) then
3312 Context := Ins_Nod;
3314 elsif Is_Itype (Ptr_Typ) then
3315 Context := Associated_Node_For_Itype (Ptr_Typ);
3317 -- When the context references a discriminant or a component of a
3318 -- private type and we are processing declarations in the private
3319 -- part of the enclosing package, we must insert the master renaming
3320 -- before the full declaration of the private type; otherwise the
3321 -- master renaming would be inserted in the public part of the
3322 -- package (and hence before the declaration of _master).
3324 if In_Private_Part (Current_Scope) then
3325 declare
3326 Ctx : Node_Id := Context;
3328 begin
3329 if Nkind (Context) = N_Discriminant_Specification then
3330 Ctx := Parent (Ctx);
3331 else
3332 while Nkind (Ctx) in
3333 N_Component_Declaration | N_Component_List
3334 loop
3335 Ctx := Parent (Ctx);
3336 end loop;
3337 end if;
3339 if Nkind (Ctx) in N_Private_Type_Declaration
3340 | N_Private_Extension_Declaration
3341 then
3342 Context := Parent (Full_View (Defining_Identifier (Ctx)));
3343 end if;
3344 end;
3345 end if;
3347 else
3348 Context := Parent (Ptr_Typ);
3349 end if;
3351 -- Generate:
3352 -- <Ptr_Typ>M : Master_Id renames _Master;
3353 -- and add a numeric suffix to the name to ensure that it is
3354 -- unique in case other access types in nested constructs
3355 -- are homonyms of this one.
3357 Master_Id :=
3358 Make_Defining_Identifier (Loc,
3359 New_External_Name (Chars (Ptr_Typ), 'M', -1));
3361 Master_Decl :=
3362 Make_Object_Renaming_Declaration (Loc,
3363 Defining_Identifier => Master_Id,
3364 Subtype_Mark =>
3365 New_Occurrence_Of (Standard_Integer, Loc),
3366 Name => Make_Identifier (Loc, Name_uMaster));
3368 Insert_Action (Context, Master_Decl);
3370 -- The renamed master now services the access type
3372 Set_Master_Id (Ptr_Typ, Master_Id);
3373 end Build_Master_Renaming;
3375 ---------------------------
3376 -- Build_Protected_Entry --
3377 ---------------------------
3379 function Build_Protected_Entry
3380 (N : Node_Id;
3381 Ent : Entity_Id;
3382 Pid : Node_Id) return Node_Id
3384 Bod_Decls : constant List_Id := New_List;
3385 Decls : constant List_Id := Declarations (N);
3386 End_Lab : constant Node_Id :=
3387 End_Label (Handled_Statement_Sequence (N));
3388 End_Loc : constant Source_Ptr :=
3389 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3390 -- Used for the generated call to Complete_Entry_Body
3392 Loc : constant Source_Ptr := Sloc (N);
3394 Block_Id : Entity_Id;
3395 Bod_Id : Entity_Id;
3396 Bod_Spec : Node_Id;
3397 Bod_Stmts : List_Id;
3398 Complete : Node_Id;
3399 Ohandle : Node_Id;
3400 Proc_Body : Node_Id;
3402 EH_Loc : Source_Ptr;
3403 -- Used for the exception handler, inserted at end of the body
3405 begin
3406 -- Set the source location on the exception handler only when debugging
3407 -- the expanded code (see Make_Implicit_Exception_Handler).
3409 if Debug_Generated_Code then
3410 EH_Loc := End_Loc;
3412 -- Otherwise the inserted code should not be visible to the debugger
3414 else
3415 EH_Loc := No_Location;
3416 end if;
3418 Bod_Id :=
3419 Make_Defining_Identifier (Loc,
3420 Chars => Chars (Protected_Body_Subprogram (Ent)));
3421 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3423 -- Add the following declarations:
3425 -- type poVP is access poV;
3426 -- _object : poVP := poVP (_O);
3428 -- where _O is the formal parameter associated with the concurrent
3429 -- object. These declarations are needed for Complete_Entry_Body.
3431 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3433 -- Add renamings for all formals, the Protection object, discriminals,
3434 -- privals and the entry index constant for use by debugger.
3436 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3437 Debug_Private_Data_Declarations (Decls);
3439 -- Put the declarations and the statements from the entry
3441 Bod_Stmts :=
3442 New_List (
3443 Make_Block_Statement (Loc,
3444 Declarations => Decls,
3445 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3447 -- Analyze now and reset scopes for declarations so that Scope fields
3448 -- currently denoting the entry will now denote the block scope, and
3449 -- the block's scope will be set to the new procedure entity.
3451 Analyze_Statements (Bod_Stmts);
3453 Block_Id := Entity (Identifier (First (Bod_Stmts)));
3455 Set_Scope (Block_Id, Protected_Body_Subprogram (Ent));
3456 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Corresponding_Spec (N)));
3458 Reset_Scopes_To (First (Bod_Stmts), Block_Id);
3459 Set_At_End_Proc (First (Bod_Stmts), At_End_Proc (N));
3461 case Corresponding_Runtime_Package (Pid) is
3462 when System_Tasking_Protected_Objects_Entries =>
3463 Append_To (Bod_Stmts,
3464 Make_Procedure_Call_Statement (End_Loc,
3465 Name =>
3466 New_Occurrence_Of (RTE (RE_Complete_Entry_Body), Loc),
3467 Parameter_Associations => New_List (
3468 Make_Attribute_Reference (End_Loc,
3469 Prefix =>
3470 Make_Selected_Component (End_Loc,
3471 Prefix =>
3472 Make_Identifier (End_Loc, Name_uObject),
3473 Selector_Name =>
3474 Make_Identifier (End_Loc, Name_uObject)),
3475 Attribute_Name => Name_Unchecked_Access))));
3477 when System_Tasking_Protected_Objects_Single_Entry =>
3479 -- Historically, a call to Complete_Single_Entry_Body was
3480 -- inserted, but it was a null procedure.
3482 null;
3484 when others =>
3485 raise Program_Error;
3486 end case;
3488 -- When exceptions cannot be propagated, we never need to call
3489 -- Exception_Complete_Entry_Body.
3491 if No_Exception_Handlers_Set then
3492 return
3493 Make_Subprogram_Body (Loc,
3494 Specification => Bod_Spec,
3495 Declarations => Bod_Decls,
3496 Handled_Statement_Sequence =>
3497 Make_Handled_Sequence_Of_Statements (Loc,
3498 Statements => Bod_Stmts,
3499 End_Label => End_Lab));
3501 else
3502 Ohandle := Make_Others_Choice (Loc);
3503 Set_All_Others (Ohandle);
3505 case Corresponding_Runtime_Package (Pid) is
3506 when System_Tasking_Protected_Objects_Entries =>
3507 Complete :=
3508 New_Occurrence_Of
3509 (RTE (RE_Exceptional_Complete_Entry_Body), Loc);
3511 when System_Tasking_Protected_Objects_Single_Entry =>
3512 Complete :=
3513 New_Occurrence_Of
3514 (RTE (RE_Exceptional_Complete_Single_Entry_Body), Loc);
3516 when others =>
3517 raise Program_Error;
3518 end case;
3520 -- Create body of entry procedure. The renaming declarations are
3521 -- placed ahead of the block that contains the actual entry body.
3523 Proc_Body :=
3524 Make_Subprogram_Body (Loc,
3525 Specification => Bod_Spec,
3526 Declarations => Bod_Decls,
3527 Handled_Statement_Sequence =>
3528 Make_Handled_Sequence_Of_Statements (Loc,
3529 Statements => Bod_Stmts,
3530 End_Label => End_Lab,
3531 Exception_Handlers => New_List (
3532 Make_Implicit_Exception_Handler (EH_Loc,
3533 Exception_Choices => New_List (Ohandle),
3535 Statements => New_List (
3536 Make_Procedure_Call_Statement (EH_Loc,
3537 Name => Complete,
3538 Parameter_Associations => New_List (
3539 Make_Attribute_Reference (EH_Loc,
3540 Prefix =>
3541 Make_Selected_Component (EH_Loc,
3542 Prefix =>
3543 Make_Identifier (EH_Loc, Name_uObject),
3544 Selector_Name =>
3545 Make_Identifier (EH_Loc, Name_uObject)),
3546 Attribute_Name => Name_Unchecked_Access),
3548 Make_Function_Call (EH_Loc,
3549 Name =>
3550 New_Occurrence_Of
3551 (RTE (RE_Get_GNAT_Exception), Loc)))))))));
3553 -- Establish link between subprogram body and source entry body
3555 Set_Corresponding_Entry_Body (Proc_Body, N);
3557 Reset_Scopes_To (Proc_Body, Protected_Body_Subprogram (Ent));
3558 return Proc_Body;
3559 end if;
3560 end Build_Protected_Entry;
3562 -----------------------------------------
3563 -- Build_Protected_Entry_Specification --
3564 -----------------------------------------
3566 function Build_Protected_Entry_Specification
3567 (Loc : Source_Ptr;
3568 Def_Id : Entity_Id;
3569 Ent_Id : Entity_Id) return Node_Id
3571 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3573 begin
3574 Set_Debug_Info_Needed (Def_Id);
3576 if Present (Ent_Id) then
3577 Append_Elmt (P, Accept_Address (Ent_Id));
3578 end if;
3580 return
3581 Make_Procedure_Specification (Loc,
3582 Defining_Unit_Name => Def_Id,
3583 Parameter_Specifications => New_List (
3584 Make_Parameter_Specification (Loc,
3585 Defining_Identifier =>
3586 Make_Defining_Identifier (Loc, Name_uO),
3587 Parameter_Type =>
3588 New_Occurrence_Of (RTE (RE_Address), Loc)),
3590 Make_Parameter_Specification (Loc,
3591 Defining_Identifier => P,
3592 Parameter_Type =>
3593 New_Occurrence_Of (RTE (RE_Address), Loc)),
3595 Make_Parameter_Specification (Loc,
3596 Defining_Identifier =>
3597 Make_Defining_Identifier (Loc, Name_uE),
3598 Parameter_Type =>
3599 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
3600 end Build_Protected_Entry_Specification;
3602 --------------------------
3603 -- Build_Protected_Spec --
3604 --------------------------
3606 function Build_Protected_Spec
3607 (N : Node_Id;
3608 Obj_Type : Entity_Id;
3609 Ident : Entity_Id;
3610 Unprotected : Boolean := False) return List_Id
3612 Loc : constant Source_Ptr := Sloc (N);
3614 Decl : Node_Id;
3615 Formal : Entity_Id;
3616 New_Formal : Entity_Id;
3617 New_Plist : List_Id;
3619 begin
3620 New_Plist := New_List;
3622 Formal := First_Formal (Ident);
3623 while Present (Formal) loop
3624 New_Formal :=
3625 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
3626 Set_Comes_From_Source (New_Formal, Comes_From_Source (Formal));
3628 if Unprotected then
3629 Mutate_Ekind (New_Formal, Ekind (Formal));
3630 Set_Protected_Formal (Formal, New_Formal);
3631 end if;
3633 Append_To (New_Plist,
3634 Make_Parameter_Specification (Loc,
3635 Defining_Identifier => New_Formal,
3636 Aliased_Present => Aliased_Present (Parent (Formal)),
3637 In_Present => In_Present (Parent (Formal)),
3638 Out_Present => Out_Present (Parent (Formal)),
3639 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc)));
3641 Next_Formal (Formal);
3642 end loop;
3644 -- If the subprogram is a procedure and the context is not an access
3645 -- to protected subprogram, the parameter is in-out. Otherwise it is
3646 -- an in parameter.
3648 Decl :=
3649 Make_Parameter_Specification (Loc,
3650 Defining_Identifier =>
3651 Make_Defining_Identifier (Loc, Name_uObject),
3652 In_Present => True,
3653 Out_Present =>
3654 (Etype (Ident) = Standard_Void_Type
3655 and then not Is_RTE (Obj_Type, RE_Address)),
3656 Parameter_Type =>
3657 New_Occurrence_Of (Obj_Type, Loc));
3658 Set_Debug_Info_Needed (Defining_Identifier (Decl));
3659 Prepend_To (New_Plist, Decl);
3661 return New_Plist;
3662 end Build_Protected_Spec;
3664 ---------------------------------------
3665 -- Build_Protected_Sub_Specification --
3666 ---------------------------------------
3668 function Build_Protected_Sub_Specification
3669 (N : Node_Id;
3670 Prot_Typ : Entity_Id;
3671 Mode : Subprogram_Protection_Mode) return Node_Id
3673 Loc : constant Source_Ptr := Sloc (N);
3674 Decl : Node_Id;
3675 Def_Id : Entity_Id;
3676 New_Id : Entity_Id;
3677 New_Plist : List_Id;
3678 New_Spec : Node_Id;
3680 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
3681 (Dispatching_Mode => ' ',
3682 Protected_Mode => 'P',
3683 Unprotected_Mode => 'N');
3685 begin
3686 if Ekind (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
3687 then
3688 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
3689 else
3690 Decl := N;
3691 end if;
3693 Def_Id := Defining_Unit_Name (Specification (Decl));
3695 New_Plist :=
3696 Build_Protected_Spec
3697 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
3698 Mode = Unprotected_Mode);
3699 New_Id :=
3700 Make_Defining_Identifier (Loc,
3701 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
3703 -- Reference the original nondispatching subprogram since the analysis
3704 -- of the object.operation notation may need its original name (see
3705 -- Sem_Ch4.Names_Match).
3707 if Mode = Dispatching_Mode then
3708 Mutate_Ekind (New_Id, Ekind (Def_Id));
3709 Set_Original_Protected_Subprogram (New_Id, Def_Id);
3710 end if;
3712 -- Link the protected or unprotected version to the original subprogram
3713 -- it emulates.
3715 Mutate_Ekind (New_Id, Ekind (Def_Id));
3716 Set_Protected_Subprogram (New_Id, Def_Id);
3718 -- The unprotected operation carries the user code, and debugging
3719 -- information must be generated for it, even though this spec does
3720 -- not come from source. It is also convenient to allow gdb to step
3721 -- into the protected operation, even though it only contains lock/
3722 -- unlock calls.
3724 Set_Debug_Info_Needed (New_Id);
3726 -- If a pragma Eliminate applies to the source entity, the internal
3727 -- subprograms will be eliminated as well.
3729 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
3731 -- It seems we should set Has_Nested_Subprogram here, but instead we
3732 -- currently set it in Expand_N_Protected_Body, because the entity
3733 -- created here isn't the one that Corresponding_Spec of the body
3734 -- will later be set to, and that's the entity where it's needed. ???
3736 Set_Has_Nested_Subprogram (New_Id, Has_Nested_Subprogram (Def_Id));
3738 if Nkind (Specification (Decl)) = N_Procedure_Specification then
3739 New_Spec :=
3740 Make_Procedure_Specification (Loc,
3741 Defining_Unit_Name => New_Id,
3742 Parameter_Specifications => New_Plist);
3744 -- Create a new specification for the anonymous subprogram type
3746 else
3747 New_Spec :=
3748 Make_Function_Specification (Loc,
3749 Defining_Unit_Name => New_Id,
3750 Parameter_Specifications => New_Plist,
3751 Result_Definition =>
3752 Copy_Result_Type (Result_Definition (Specification (Decl))));
3754 Set_Return_Present (Defining_Unit_Name (New_Spec));
3755 end if;
3757 return New_Spec;
3758 end Build_Protected_Sub_Specification;
3760 -------------------------------------
3761 -- Build_Protected_Subprogram_Body --
3762 -------------------------------------
3764 function Build_Protected_Subprogram_Body
3765 (N : Node_Id;
3766 Pid : Node_Id;
3767 N_Op_Spec : Node_Id) return Node_Id
3769 Might_Raise : constant Boolean := Sem_Util.Might_Raise (N);
3771 Loc : constant Source_Ptr := Sloc (N);
3772 Op_Spec : constant Node_Id := Specification (N);
3773 P_Op_Spec : constant Node_Id :=
3774 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
3776 Lock_Kind : RE_Id;
3777 Lock_Name : Node_Id;
3778 Lock_Stmt : Node_Id;
3779 Object_Parm : Node_Id;
3780 Pformal : Node_Id;
3781 R : Node_Id;
3782 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
3783 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
3784 Stmts : List_Id;
3785 Sub_Body : Node_Id;
3786 Uactuals : List_Id;
3787 Unprot_Call : Node_Id;
3789 begin
3790 -- Build a list of the formal parameters of the protected version of
3791 -- the subprogram to use as the actual parameters of the unprotected
3792 -- version.
3794 Uactuals := New_List;
3795 Pformal := First (Parameter_Specifications (P_Op_Spec));
3796 while Present (Pformal) loop
3797 Append_To (Uactuals,
3798 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
3799 Next (Pformal);
3800 end loop;
3802 -- Make a call to the unprotected version of the subprogram built above
3803 -- for use by the protected version built below.
3805 if Nkind (Op_Spec) = N_Function_Specification then
3806 if Might_Raise then
3807 Unprot_Call :=
3808 Make_Simple_Return_Statement (Loc,
3809 Expression =>
3810 Make_Function_Call (Loc,
3811 Name =>
3812 Make_Identifier (Loc,
3813 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3814 Parameter_Associations => Uactuals));
3816 else
3817 R := Make_Temporary (Loc, 'R');
3819 Unprot_Call :=
3820 Make_Object_Declaration (Loc,
3821 Defining_Identifier => R,
3822 Constant_Present => True,
3823 Object_Definition =>
3824 New_Copy (Result_Definition (N_Op_Spec)),
3825 Expression =>
3826 Make_Function_Call (Loc,
3827 Name =>
3828 Make_Identifier (Loc,
3829 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3830 Parameter_Associations => Uactuals));
3832 Return_Stmt :=
3833 Make_Simple_Return_Statement (Loc,
3834 Expression => New_Occurrence_Of (R, Loc));
3835 end if;
3837 if Has_Enabled_Aspect (Pid, Aspect_Exclusive_Functions) then
3838 Lock_Kind := RE_Lock;
3839 else
3840 Lock_Kind := RE_Lock_Read_Only;
3841 end if;
3842 else
3843 Unprot_Call :=
3844 Make_Procedure_Call_Statement (Loc,
3845 Name =>
3846 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
3847 Parameter_Associations => Uactuals);
3849 Lock_Kind := RE_Lock;
3850 end if;
3852 -- Wrap call in block that will be covered by an at_end handler
3854 if Might_Raise then
3855 Unprot_Call :=
3856 Make_Block_Statement (Loc,
3857 Handled_Statement_Sequence =>
3858 Make_Handled_Sequence_Of_Statements (Loc,
3859 Statements => New_List (Unprot_Call)));
3860 end if;
3862 -- Make the protected subprogram body. This locks the protected
3863 -- object and calls the unprotected version of the subprogram.
3865 case Corresponding_Runtime_Package (Pid) is
3866 when System_Tasking_Protected_Objects_Entries =>
3867 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
3869 when System_Tasking_Protected_Objects_Single_Entry =>
3870 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
3872 when System_Tasking_Protected_Objects =>
3873 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
3875 when others =>
3876 raise Program_Error;
3877 end case;
3879 Object_Parm :=
3880 Make_Attribute_Reference (Loc,
3881 Prefix =>
3882 Make_Selected_Component (Loc,
3883 Prefix => Make_Identifier (Loc, Name_uObject),
3884 Selector_Name => Make_Identifier (Loc, Name_uObject)),
3885 Attribute_Name => Name_Unchecked_Access);
3887 Lock_Stmt :=
3888 Make_Procedure_Call_Statement (Loc,
3889 Name => Lock_Name,
3890 Parameter_Associations => New_List (Object_Parm));
3892 if Abort_Allowed then
3893 Stmts := New_List (
3894 Build_Runtime_Call (Loc, RE_Abort_Defer),
3895 Lock_Stmt);
3897 else
3898 Stmts := New_List (Lock_Stmt);
3899 end if;
3901 if Might_Raise then
3902 Append (Unprot_Call, Stmts);
3903 else
3904 if Nkind (Op_Spec) = N_Function_Specification then
3905 Pre_Stmts := Stmts;
3906 Stmts := Empty_List;
3907 else
3908 Append (Unprot_Call, Stmts);
3909 end if;
3911 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
3913 if Nkind (Op_Spec) = N_Function_Specification then
3914 Append_To (Stmts, Return_Stmt);
3915 Append_To (Pre_Stmts,
3916 Make_Block_Statement (Loc,
3917 Declarations => New_List (Unprot_Call),
3918 Handled_Statement_Sequence =>
3919 Make_Handled_Sequence_Of_Statements (Loc,
3920 Statements => Stmts)));
3921 Stmts := Pre_Stmts;
3922 end if;
3923 end if;
3925 Sub_Body :=
3926 Make_Subprogram_Body (Loc,
3927 Declarations => Empty_List,
3928 Specification => P_Op_Spec,
3929 Handled_Statement_Sequence =>
3930 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
3932 -- Mark this subprogram as a protected subprogram body so that the
3933 -- cleanup will be inserted. This is done only in the Might_Raise
3934 -- case because otherwise the cleanup has already been inserted.
3936 if Might_Raise then
3937 Set_Is_Protected_Subprogram_Body (Sub_Body);
3938 end if;
3940 return Sub_Body;
3941 end Build_Protected_Subprogram_Body;
3943 -------------------------------------
3944 -- Build_Protected_Subprogram_Call --
3945 -------------------------------------
3947 procedure Build_Protected_Subprogram_Call
3948 (N : Node_Id;
3949 Name : Node_Id;
3950 Rec : Node_Id;
3951 External : Boolean := True)
3953 Loc : constant Source_Ptr := Sloc (N);
3954 Sub : constant Entity_Id := Entity (Name);
3955 New_Sub : Node_Id;
3956 Params : List_Id;
3958 begin
3959 if External then
3960 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
3961 else
3962 New_Sub :=
3963 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
3964 end if;
3966 if Present (Parameter_Associations (N)) then
3967 Params := New_Copy_List_Tree (Parameter_Associations (N));
3968 else
3969 Params := New_List;
3970 end if;
3972 -- If the type is an untagged derived type, convert to the root type,
3973 -- which is the one on which the operations are defined.
3975 if Nkind (Rec) = N_Unchecked_Type_Conversion
3976 and then not Is_Tagged_Type (Etype (Rec))
3977 and then Is_Derived_Type (Etype (Rec))
3978 then
3979 Set_Etype (Rec, Root_Type (Etype (Rec)));
3980 Set_Subtype_Mark (Rec,
3981 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
3982 end if;
3984 Prepend (Rec, Params);
3986 if Ekind (Sub) = E_Procedure then
3987 Rewrite (N,
3988 Make_Procedure_Call_Statement (Loc,
3989 Name => New_Sub,
3990 Parameter_Associations => Params));
3992 else
3993 pragma Assert (Ekind (Sub) = E_Function);
3994 Rewrite (N,
3995 Make_Function_Call (Loc,
3996 Name => New_Sub,
3997 Parameter_Associations => Params));
3999 -- Preserve type of call for subsequent processing (required for
4000 -- call to Wrap_Transient_Expression in the case of a shared passive
4001 -- protected).
4003 Set_Etype (N, Etype (New_Sub));
4004 end if;
4006 if External
4007 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4008 and then Is_Entity_Name (Expression (Rec))
4009 and then Is_Shared_Passive (Entity (Expression (Rec)))
4010 then
4011 Add_Shared_Var_Lock_Procs (N);
4012 end if;
4013 end Build_Protected_Subprogram_Call;
4015 ---------------------------------------------
4016 -- Build_Protected_Subprogram_Call_Cleanup --
4017 ---------------------------------------------
4019 procedure Build_Protected_Subprogram_Call_Cleanup
4020 (Op_Spec : Node_Id;
4021 Conc_Typ : Node_Id;
4022 Loc : Source_Ptr;
4023 Stmts : List_Id)
4025 Nam : Node_Id;
4027 begin
4028 -- If the associated protected object has entries, the expanded
4029 -- exclusive protected operation has to service entry queues. In
4030 -- this case generate:
4032 -- Service_Entries (_object._object'Access);
4034 if (Nkind (Op_Spec) = N_Procedure_Specification
4035 or else
4036 (Nkind (Op_Spec) = N_Function_Specification
4037 and then
4038 Has_Enabled_Aspect
4039 (Conc_Typ, Aspect_Exclusive_Functions)))
4040 and then Has_Entries (Conc_Typ)
4041 then
4042 case Corresponding_Runtime_Package (Conc_Typ) is
4043 when System_Tasking_Protected_Objects_Entries =>
4044 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4046 when System_Tasking_Protected_Objects_Single_Entry =>
4047 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4049 when others =>
4050 raise Program_Error;
4051 end case;
4053 Append_To (Stmts,
4054 Make_Procedure_Call_Statement (Loc,
4055 Name => Nam,
4056 Parameter_Associations => New_List (
4057 Make_Attribute_Reference (Loc,
4058 Prefix =>
4059 Make_Selected_Component (Loc,
4060 Prefix => Make_Identifier (Loc, Name_uObject),
4061 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4062 Attribute_Name => Name_Unchecked_Access))));
4064 else
4065 -- Generate:
4066 -- Unlock (_object._object'Access);
4068 case Corresponding_Runtime_Package (Conc_Typ) is
4069 when System_Tasking_Protected_Objects_Entries =>
4070 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4072 when System_Tasking_Protected_Objects_Single_Entry =>
4073 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4075 when System_Tasking_Protected_Objects =>
4076 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4078 when others =>
4079 raise Program_Error;
4080 end case;
4082 Append_To (Stmts,
4083 Make_Procedure_Call_Statement (Loc,
4084 Name => Nam,
4085 Parameter_Associations => New_List (
4086 Make_Attribute_Reference (Loc,
4087 Prefix =>
4088 Make_Selected_Component (Loc,
4089 Prefix => Make_Identifier (Loc, Name_uObject),
4090 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4091 Attribute_Name => Name_Unchecked_Access))));
4092 end if;
4094 -- Generate:
4095 -- Abort_Undefer;
4097 if Abort_Allowed then
4098 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
4099 end if;
4100 end Build_Protected_Subprogram_Call_Cleanup;
4102 -------------------------
4103 -- Build_Selected_Name --
4104 -------------------------
4106 function Build_Selected_Name
4107 (Prefix : Entity_Id;
4108 Selector : Entity_Id;
4109 Append_Char : Character := ' ') return Name_Id
4111 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4112 Select_Len : Natural;
4114 begin
4115 Get_Name_String (Chars (Selector));
4116 Select_Len := Name_Len;
4117 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4118 Get_Name_String (Chars (Prefix));
4120 -- If scope is anonymous type, discard suffix to recover name of
4121 -- single protected object. Otherwise use protected type name.
4123 if Name_Buffer (Name_Len) = 'T' then
4124 Name_Len := Name_Len - 1;
4125 end if;
4127 Add_Str_To_Name_Buffer ("__");
4128 for J in 1 .. Select_Len loop
4129 Add_Char_To_Name_Buffer (Select_Buffer (J));
4130 end loop;
4132 -- Now add the Append_Char if specified. The encoding to follow
4133 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4134 -- then the entity is associated to a protected type subprogram.
4135 -- Otherwise, it is a protected type entry. For each case, the
4136 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4138 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4140 if Append_Char /= ' ' then
4141 if Append_Char in 'P' | 'N' then
4142 Add_Char_To_Name_Buffer (Append_Char);
4143 return Name_Find;
4144 else
4145 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4146 return New_External_Name (Name_Find, ' ', -1);
4147 end if;
4148 else
4149 return Name_Find;
4150 end if;
4151 end Build_Selected_Name;
4153 -----------------------------
4154 -- Build_Simple_Entry_Call --
4155 -----------------------------
4157 -- A task entry call is converted to a call to Call_Simple
4159 -- declare
4160 -- P : parms := (parm, parm, parm);
4161 -- begin
4162 -- Call_Simple (acceptor-task, entry-index, P'Address);
4163 -- parm := P.param;
4164 -- parm := P.param;
4165 -- ...
4166 -- end;
4168 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4169 -- the parameters, and the constructed aggregate value contains either the
4170 -- parameters or, in the case of non-elementary types, references to these
4171 -- parameters. Then the address of this aggregate is passed to the runtime
4172 -- routine, along with the task id value and the task entry index value.
4173 -- Pnn is only required if parameters are present.
4175 -- The assignments after the call are present only in the case of in-out
4176 -- or out parameters for elementary types, and are used to assign back the
4177 -- resulting values of such parameters.
4179 -- Note: the reason that we insert a block here is that in the context
4180 -- of selects, conditional entry calls etc. the entry call statement
4181 -- appears on its own, not as an element of a list.
4183 -- A protected entry call is converted to a Protected_Entry_Call:
4185 -- declare
4186 -- P : E1_Params := (param, param, param);
4187 -- Pnn : Boolean;
4188 -- Bnn : Communications_Block;
4190 -- declare
4191 -- P : E1_Params := (param, param, param);
4192 -- Bnn : Communications_Block;
4194 -- begin
4195 -- Protected_Entry_Call (
4196 -- Object => po._object'Access,
4197 -- E => <entry index>;
4198 -- Uninterpreted_Data => P'Address;
4199 -- Mode => Simple_Call;
4200 -- Block => Bnn);
4201 -- parm := P.param;
4202 -- parm := P.param;
4203 -- ...
4204 -- end;
4206 procedure Build_Simple_Entry_Call
4207 (N : Node_Id;
4208 Concval : Node_Id;
4209 Ename : Node_Id;
4210 Index : Node_Id)
4212 begin
4213 Expand_Call (N);
4215 -- If call has been inlined, nothing left to do
4217 if Nkind (N) = N_Block_Statement then
4218 return;
4219 end if;
4221 -- Convert entry call to Call_Simple call
4223 declare
4224 Loc : constant Source_Ptr := Sloc (N);
4225 Parms : constant List_Id := Parameter_Associations (N);
4226 Stats : constant List_Id := New_List;
4227 Actual : Node_Id;
4228 Call : Node_Id;
4229 Comm_Name : Entity_Id;
4230 Conctyp : Node_Id;
4231 Decls : List_Id;
4232 Ent : Entity_Id;
4233 Ent_Acc : Entity_Id;
4234 Formal : Node_Id;
4235 Iface_Tag : Entity_Id;
4236 Iface_Typ : Entity_Id;
4237 N_Node : Node_Id;
4238 N_Var : Node_Id;
4239 P : Entity_Id;
4240 Parm1 : Node_Id;
4241 Parm2 : Node_Id;
4242 Parm3 : Node_Id;
4243 Pdecl : Node_Id;
4244 Plist : List_Id;
4245 X : Entity_Id;
4246 Xdecl : Node_Id;
4248 begin
4249 -- Simple entry and entry family cases merge here
4251 Ent := Entity (Ename);
4252 Ent_Acc := Entry_Parameters_Type (Ent);
4253 Conctyp := Etype (Concval);
4255 -- Special case for protected subprogram calls
4257 if Is_Protected_Type (Conctyp)
4258 and then Is_Subprogram (Entity (Ename))
4259 then
4260 if not Is_Eliminated (Entity (Ename)) then
4261 Build_Protected_Subprogram_Call
4262 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4263 Analyze (N);
4264 end if;
4266 return;
4267 end if;
4269 -- First parameter is the Task_Id value from the task value or the
4270 -- Object from the protected object value, obtained by selecting
4271 -- the _Task_Id or _Object from the result of doing an unchecked
4272 -- conversion to convert the value to the corresponding record type.
4274 if Nkind (Concval) = N_Function_Call
4275 and then Is_Task_Type (Conctyp)
4276 and then Ada_Version >= Ada_2005
4277 then
4278 declare
4279 ExpR : constant Node_Id := Relocate_Node (Concval);
4280 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4281 Decl : Node_Id;
4283 begin
4284 Decl :=
4285 Make_Object_Declaration (Loc,
4286 Defining_Identifier => Obj,
4287 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4288 Expression => ExpR);
4289 Set_Etype (Obj, Conctyp);
4290 Decls := New_List (Decl);
4291 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4292 end;
4294 else
4295 Decls := New_List;
4296 end if;
4298 Parm1 := Concurrent_Ref (Concval);
4300 -- Second parameter is the entry index, computed by the routine
4301 -- provided for this purpose. The value of this expression is
4302 -- assigned to an intermediate variable to assure that any entry
4303 -- family index expressions are evaluated before the entry
4304 -- parameters.
4306 if not Is_Protected_Type (Conctyp)
4307 or else
4308 Corresponding_Runtime_Package (Conctyp) =
4309 System_Tasking_Protected_Objects_Entries
4310 then
4311 X := Make_Defining_Identifier (Loc, Name_uX);
4313 Xdecl :=
4314 Make_Object_Declaration (Loc,
4315 Defining_Identifier => X,
4316 Object_Definition =>
4317 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4318 Expression => Actual_Index_Expression (
4319 Loc, Entity (Ename), Index, Concval));
4321 Append_To (Decls, Xdecl);
4322 Parm2 := New_Occurrence_Of (X, Loc);
4324 else
4325 Xdecl := Empty;
4326 Parm2 := Empty;
4327 end if;
4329 -- The third parameter is the packaged parameters. If there are
4330 -- none, then it is just the null address, since nothing is passed.
4332 if No (Parms) then
4333 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4334 P := Empty;
4336 -- Case of parameters present, where third argument is the address
4337 -- of a packaged record containing the required parameter values.
4339 else
4340 -- First build a list of parameter values, which are references to
4341 -- objects of the parameter types.
4343 Plist := New_List;
4345 Actual := First_Actual (N);
4346 Formal := First_Formal (Ent);
4347 while Present (Actual) loop
4349 -- If it is a by-copy type, copy it to a new variable. The
4350 -- packaged record has a field that points to this variable.
4352 if Is_By_Copy_Type (Etype (Actual)) then
4353 N_Node :=
4354 Make_Object_Declaration (Loc,
4355 Defining_Identifier => Make_Temporary (Loc, 'J'),
4356 Aliased_Present => True,
4357 Object_Definition =>
4358 New_Occurrence_Of (Etype (Formal), Loc));
4360 -- Mark the object as not needing initialization since the
4361 -- initialization is performed separately, avoiding errors
4362 -- on cases such as formals of null-excluding access types.
4364 Set_No_Initialization (N_Node);
4366 -- We must make a separate assignment statement for the
4367 -- case of limited types. We cannot assign it unless the
4368 -- Assignment_OK flag is set first. An out formal of an
4369 -- access type or whose type has a Default_Value must also
4370 -- be initialized from the actual (see RM 6.4.1 (13-13.1)),
4371 -- but no constraint, predicate, or null-exclusion check is
4372 -- applied before the call.
4374 if Ekind (Formal) /= E_Out_Parameter
4375 or else Is_Access_Type (Etype (Formal))
4376 or else
4377 (Is_Scalar_Type (Etype (Formal))
4378 and then
4379 Present (Default_Aspect_Value (Etype (Formal))))
4380 then
4381 N_Var :=
4382 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4383 Set_Assignment_OK (N_Var);
4384 Append_To (Stats,
4385 Make_Assignment_Statement (Loc,
4386 Name => N_Var,
4387 Expression => Relocate_Node (Actual)));
4389 -- Mark the object as internal, so we don't later reset
4390 -- No_Initialization flag in Default_Initialize_Object,
4391 -- which would lead to needless default initialization.
4392 -- We don't set this outside the if statement, because
4393 -- out scalar parameters without Default_Value do require
4394 -- default initialization if Initialize_Scalars applies.
4396 Set_Is_Internal (Defining_Identifier (N_Node));
4398 -- If actual is an out parameter of a null-excluding
4399 -- access type, there is access check on entry, so set
4400 -- Suppress_Assignment_Checks on the generated statement
4401 -- that assigns the actual to the parameter block.
4403 Set_Suppress_Assignment_Checks (Last (Stats));
4404 end if;
4406 Append (N_Node, Decls);
4408 Append_To (Plist,
4409 Make_Attribute_Reference (Loc,
4410 Attribute_Name => Name_Unchecked_Access,
4411 Prefix =>
4412 New_Occurrence_Of
4413 (Defining_Identifier (N_Node), Loc)));
4415 else
4416 -- Interface class-wide formal
4418 if Ada_Version >= Ada_2005
4419 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4420 and then Is_Interface (Etype (Formal))
4421 then
4422 Iface_Typ := Etype (Etype (Formal));
4424 -- Generate:
4425 -- formal_iface_type! (actual.iface_tag)'reference
4427 Iface_Tag :=
4428 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4429 pragma Assert (Present (Iface_Tag));
4431 Append_To (Plist,
4432 Make_Reference (Loc,
4433 Unchecked_Convert_To (Iface_Typ,
4434 Make_Selected_Component (Loc,
4435 Prefix =>
4436 Relocate_Node (Actual),
4437 Selector_Name =>
4438 New_Occurrence_Of (Iface_Tag, Loc)))));
4439 else
4440 -- Generate:
4441 -- actual'reference
4443 Append_To (Plist,
4444 Make_Reference (Loc, Relocate_Node (Actual)));
4445 end if;
4446 end if;
4448 Next_Actual (Actual);
4449 Next_Formal_With_Extras (Formal);
4450 end loop;
4452 -- Now build the declaration of parameters initialized with the
4453 -- aggregate containing this constructed parameter list.
4455 P := Make_Defining_Identifier (Loc, Name_uP);
4457 Pdecl :=
4458 Make_Object_Declaration (Loc,
4459 Defining_Identifier => P,
4460 Object_Definition =>
4461 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4462 Expression =>
4463 Make_Aggregate (Loc, Expressions => Plist));
4465 Parm3 :=
4466 Make_Attribute_Reference (Loc,
4467 Prefix => New_Occurrence_Of (P, Loc),
4468 Attribute_Name => Name_Address);
4470 Append (Pdecl, Decls);
4471 end if;
4473 -- Now we can create the call, case of protected type
4475 if Is_Protected_Type (Conctyp) then
4476 case Corresponding_Runtime_Package (Conctyp) is
4477 when System_Tasking_Protected_Objects_Entries =>
4479 -- Change the type of the index declaration
4481 Set_Object_Definition (Xdecl,
4482 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4484 -- Some additional declarations for protected entry calls
4486 if No (Decls) then
4487 Decls := New_List;
4488 end if;
4490 -- Bnn : Communications_Block;
4492 Comm_Name := Make_Temporary (Loc, 'B');
4494 Append_To (Decls,
4495 Make_Object_Declaration (Loc,
4496 Defining_Identifier => Comm_Name,
4497 Object_Definition =>
4498 New_Occurrence_Of
4499 (RTE (RE_Communication_Block), Loc)));
4501 -- Some additional statements for protected entry calls
4503 -- Protected_Entry_Call
4504 -- (Object => po._object'Access,
4505 -- E => <entry index>;
4506 -- Uninterpreted_Data => P'Address;
4507 -- Mode => Simple_Call;
4508 -- Block => Bnn);
4510 Call :=
4511 Make_Procedure_Call_Statement (Loc,
4512 Name =>
4513 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
4515 Parameter_Associations => New_List (
4516 Make_Attribute_Reference (Loc,
4517 Attribute_Name => Name_Unchecked_Access,
4518 Prefix => Parm1),
4519 Parm2,
4520 Parm3,
4521 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
4522 New_Occurrence_Of (Comm_Name, Loc)));
4524 when System_Tasking_Protected_Objects_Single_Entry =>
4526 -- Protected_Single_Entry_Call
4527 -- (Object => po._object'Access,
4528 -- Uninterpreted_Data => P'Address);
4530 Call :=
4531 Make_Procedure_Call_Statement (Loc,
4532 Name =>
4533 New_Occurrence_Of
4534 (RTE (RE_Protected_Single_Entry_Call), Loc),
4536 Parameter_Associations => New_List (
4537 Make_Attribute_Reference (Loc,
4538 Attribute_Name => Name_Unchecked_Access,
4539 Prefix => Parm1),
4540 Parm3));
4542 when others =>
4543 raise Program_Error;
4544 end case;
4546 -- Case of task type
4548 else
4549 Call :=
4550 Make_Procedure_Call_Statement (Loc,
4551 Name =>
4552 New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
4553 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4555 end if;
4557 Append_To (Stats, Call);
4559 -- If there are out or in/out parameters by copy add assignment
4560 -- statements for the result values.
4562 if Present (Parms) then
4563 Actual := First_Actual (N);
4564 Formal := First_Formal (Ent);
4566 Set_Assignment_OK (Actual);
4567 while Present (Actual) loop
4568 if Is_By_Copy_Type (Etype (Actual))
4569 and then Ekind (Formal) /= E_In_Parameter
4570 then
4571 N_Node :=
4572 Make_Assignment_Statement (Loc,
4573 Name => New_Copy (Actual),
4574 Expression =>
4575 Make_Explicit_Dereference (Loc,
4576 Make_Selected_Component (Loc,
4577 Prefix => New_Occurrence_Of (P, Loc),
4578 Selector_Name =>
4579 Make_Identifier (Loc, Chars (Formal)))));
4581 -- In all cases (including limited private types) we want
4582 -- the assignment to be valid.
4584 Set_Assignment_OK (Name (N_Node));
4586 -- If the call is the triggering alternative in an
4587 -- asynchronous select, or the entry_call alternative of a
4588 -- conditional entry call, the assignments for in-out
4589 -- parameters are incorporated into the statement list that
4590 -- follows, so that there are executed only if the entry
4591 -- call succeeds.
4593 if (Nkind (Parent (N)) = N_Triggering_Alternative
4594 and then N = Triggering_Statement (Parent (N)))
4595 or else
4596 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4597 and then N = Entry_Call_Statement (Parent (N)))
4598 then
4599 if No (Statements (Parent (N))) then
4600 Set_Statements (Parent (N), New_List);
4601 end if;
4603 Prepend (N_Node, Statements (Parent (N)));
4605 else
4606 Insert_After (Call, N_Node);
4607 end if;
4608 end if;
4610 Next_Actual (Actual);
4611 Next_Formal_With_Extras (Formal);
4612 end loop;
4613 end if;
4615 -- Finally, create block and analyze it
4617 Rewrite (N,
4618 Make_Block_Statement (Loc,
4619 Declarations => Decls,
4620 Handled_Statement_Sequence =>
4621 Make_Handled_Sequence_Of_Statements (Loc,
4622 Statements => Stats)));
4624 Analyze (N);
4625 end;
4626 end Build_Simple_Entry_Call;
4628 --------------------------------
4629 -- Build_Task_Activation_Call --
4630 --------------------------------
4632 procedure Build_Task_Activation_Call (N : Node_Id) is
4633 function Activation_Call_Loc return Source_Ptr;
4634 -- Find a suitable source location for the activation call
4636 -------------------------
4637 -- Activation_Call_Loc --
4638 -------------------------
4640 function Activation_Call_Loc return Source_Ptr is
4641 begin
4642 -- The activation call must carry the location of the "end" keyword
4643 -- when the context is a package declaration.
4645 if Nkind (N) = N_Package_Declaration then
4646 return End_Keyword_Location (N);
4648 -- Otherwise the activation call must carry the location of the
4649 -- "begin" keyword.
4651 else
4652 return Begin_Keyword_Location (N);
4653 end if;
4654 end Activation_Call_Loc;
4656 -- Local variables
4658 Chain : Entity_Id;
4659 Call : Node_Id;
4660 Loc : Source_Ptr;
4661 Name : Node_Id;
4662 Owner : Node_Id;
4663 Stmt : Node_Id;
4665 -- Start of processing for Build_Task_Activation_Call
4667 begin
4668 -- For sequential elaboration policy, all the tasks will be activated at
4669 -- the end of the elaboration.
4671 if Partition_Elaboration_Policy = 'S' then
4672 return;
4674 -- Do not create an activation call for a package spec if the package
4675 -- has a completing body. The activation call will be inserted after
4676 -- the "begin" of the body.
4678 elsif Nkind (N) = N_Package_Declaration
4679 and then Present (Corresponding_Body (N))
4680 then
4681 return;
4682 end if;
4684 -- Obtain the activation chain entity. Block statements, entry bodies,
4685 -- subprogram bodies, and task bodies keep the entity in their nodes.
4686 -- Package bodies on the other hand store it in the declaration of the
4687 -- corresponding package spec.
4689 Owner := N;
4691 if Nkind (Owner) = N_Package_Body then
4692 Owner := Unit_Declaration_Node (Corresponding_Spec (Owner));
4693 end if;
4695 Chain := Activation_Chain_Entity (Owner);
4697 -- Nothing to do when there are no tasks to activate. This is indicated
4698 -- by a missing activation chain entity; also skip generating it when
4699 -- it is a ghost entity.
4701 if No (Chain) or else Is_Ignored_Ghost_Entity (Chain) then
4702 return;
4704 -- The availability of the activation chain entity does not ensure
4705 -- that we have tasks to activate because it may have been declared
4706 -- by the frontend to pass a required extra formal to a build-in-place
4707 -- subprogram call. If we are within the scope of a protected type and
4708 -- pragma Detect_Blocking is active we can assume that no tasks will be
4709 -- activated; if tasks are created in a protected object and this pragma
4710 -- is active then the frontend emits a warning and Program_Error is
4711 -- raised at runtime.
4713 elsif Detect_Blocking and then Within_Protected_Type (Current_Scope) then
4714 return;
4715 end if;
4717 -- The location of the activation call must be as close as possible to
4718 -- the intended semantic location of the activation because the ABE
4719 -- mechanism relies heavily on accurate locations.
4721 Loc := Activation_Call_Loc;
4723 if Restricted_Profile then
4724 Name := New_Occurrence_Of (RTE (RE_Activate_Restricted_Tasks), Loc);
4725 else
4726 Name := New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc);
4727 end if;
4729 Call :=
4730 Make_Procedure_Call_Statement (Loc,
4731 Name => Name,
4732 Parameter_Associations =>
4733 New_List (Make_Attribute_Reference (Loc,
4734 Prefix => New_Occurrence_Of (Chain, Loc),
4735 Attribute_Name => Name_Unchecked_Access)));
4737 if Nkind (N) = N_Package_Declaration then
4738 if Present (Private_Declarations (Specification (N))) then
4739 Append (Call, Private_Declarations (Specification (N)));
4740 else
4741 Append (Call, Visible_Declarations (Specification (N)));
4742 end if;
4744 else
4745 -- The call goes at the start of the statement sequence after the
4746 -- start of exception range label if one is present.
4748 if Present (Handled_Statement_Sequence (N)) then
4749 Stmt := First (Statements (Handled_Statement_Sequence (N)));
4751 -- A special case, skip exception range label if one is present
4752 -- (from front end zcx processing).
4754 if Nkind (Stmt) = N_Label and then Exception_Junk (Stmt) then
4755 Next (Stmt);
4756 end if;
4758 -- Another special case, if the first statement is a block from
4759 -- optimization of a local raise to a goto, then the call goes
4760 -- inside this block.
4762 if Nkind (Stmt) = N_Block_Statement
4763 and then Exception_Junk (Stmt)
4764 then
4765 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
4766 end if;
4768 -- Insertion point is after any exception label pushes, since we
4769 -- want it covered by any local handlers.
4771 while Nkind (Stmt) in N_Push_xxx_Label loop
4772 Next (Stmt);
4773 end loop;
4775 -- Now we have the proper insertion point
4777 Insert_Before (Stmt, Call);
4779 else
4780 Set_Handled_Statement_Sequence (N,
4781 Make_Handled_Sequence_Of_Statements (Loc,
4782 Statements => New_List (Call)));
4783 end if;
4784 end if;
4786 Analyze (Call);
4788 if Legacy_Elaboration_Checks then
4789 Check_Task_Activation (N);
4790 end if;
4791 end Build_Task_Activation_Call;
4793 -------------------------------
4794 -- Build_Task_Allocate_Block --
4795 -------------------------------
4797 procedure Build_Task_Allocate_Block
4798 (Actions : List_Id;
4799 N : Node_Id;
4800 Init_Stmts : List_Id)
4802 Loc : constant Source_Ptr := Sloc (N);
4803 Chain : constant Entity_Id :=
4804 Make_Defining_Identifier (Loc, Name_uChain);
4805 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4806 Block : Node_Id;
4808 begin
4809 Append_To (Init_Stmts,
4810 Make_Procedure_Call_Statement (Loc,
4811 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4812 Parameter_Associations => New_List (
4813 Make_Attribute_Reference (Loc,
4814 Prefix => New_Occurrence_Of (Chain, Loc),
4815 Attribute_Name => Name_Unchecked_Access))));
4817 Block :=
4818 Make_Block_Statement (Loc,
4819 Identifier => New_Occurrence_Of (Blkent, Loc),
4820 Declarations => New_List (
4822 -- _Chain : Activation_Chain;
4824 Make_Object_Declaration (Loc,
4825 Defining_Identifier => Chain,
4826 Aliased_Present => True,
4827 Object_Definition =>
4828 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
4830 Handled_Statement_Sequence =>
4831 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
4833 Has_Created_Identifier => True,
4834 Is_Task_Allocation_Block => True);
4836 Append_To (Actions,
4837 Make_Implicit_Label_Declaration (Loc,
4838 Defining_Identifier => Blkent,
4839 Label_Construct => Block));
4841 Append_To (Actions, Block);
4843 Set_Activation_Chain_Entity (Block, Chain);
4844 end Build_Task_Allocate_Block;
4846 -----------------------------------
4847 -- Build_Task_Proc_Specification --
4848 -----------------------------------
4850 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
4851 Loc : constant Source_Ptr := Sloc (T);
4852 Spec_Id : Entity_Id;
4854 begin
4855 -- Case of explicit task type, suffix TB
4857 if Comes_From_Source (T) then
4858 Spec_Id :=
4859 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
4861 -- Case of anonymous task type, suffix B
4863 else
4864 Spec_Id :=
4865 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
4866 end if;
4868 Set_Is_Internal (Spec_Id);
4870 -- Associate the procedure with the task, if this is the declaration
4871 -- (and not the body) of the procedure.
4873 if No (Task_Body_Procedure (T)) then
4874 Set_Task_Body_Procedure (T, Spec_Id);
4875 end if;
4877 return
4878 Make_Procedure_Specification (Loc,
4879 Defining_Unit_Name => Spec_Id,
4880 Parameter_Specifications => New_List (
4881 Make_Parameter_Specification (Loc,
4882 Defining_Identifier =>
4883 Make_Defining_Identifier (Loc, Name_uTask),
4884 Parameter_Type =>
4885 Make_Access_Definition (Loc,
4886 Subtype_Mark =>
4887 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
4888 end Build_Task_Proc_Specification;
4890 ---------------------------------------
4891 -- Build_Unprotected_Subprogram_Body --
4892 ---------------------------------------
4894 function Build_Unprotected_Subprogram_Body
4895 (N : Node_Id;
4896 Pid : Node_Id) return Node_Id
4898 Decls : constant List_Id := Declarations (N);
4900 begin
4901 -- Add renamings for the Protection object, discriminals, privals, and
4902 -- the entry index constant for use by debugger.
4904 Debug_Private_Data_Declarations (Decls);
4906 -- Make an unprotected version of the subprogram for use within the same
4907 -- object, with a new name and an additional parameter representing the
4908 -- object.
4910 return
4911 Make_Subprogram_Body (Sloc (N),
4912 Specification =>
4913 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
4914 Declarations => Decls,
4915 Handled_Statement_Sequence => Handled_Statement_Sequence (N),
4916 At_End_Proc => At_End_Proc (N));
4917 end Build_Unprotected_Subprogram_Body;
4919 ----------------------------
4920 -- Collect_Entry_Families --
4921 ----------------------------
4923 procedure Collect_Entry_Families
4924 (Loc : Source_Ptr;
4925 Cdecls : List_Id;
4926 Current_Node : in out Node_Id;
4927 Conctyp : Entity_Id)
4929 Efam : Entity_Id;
4930 Efam_Decl : Node_Id;
4931 Efam_Type : Entity_Id;
4933 begin
4934 Efam := First_Entity (Conctyp);
4935 while Present (Efam) loop
4936 if Ekind (Efam) = E_Entry_Family then
4937 Efam_Type := Make_Temporary (Loc, 'F');
4939 declare
4940 Eityp : constant Entity_Id := Entry_Index_Type (Efam);
4941 Lo : constant Node_Id := Type_Low_Bound (Eityp);
4942 Hi : constant Node_Id := Type_High_Bound (Eityp);
4943 Bdecl : Node_Id;
4944 Bityp : Entity_Id;
4946 begin
4947 Bityp := Base_Type (Eityp);
4949 if Is_Potentially_Large_Family (Bityp, Conctyp, Lo, Hi) then
4950 Bityp := Make_Temporary (Loc, 'B');
4952 Bdecl :=
4953 Make_Subtype_Declaration (Loc,
4954 Defining_Identifier => Bityp,
4955 Subtype_Indication =>
4956 Make_Subtype_Indication (Loc,
4957 Subtype_Mark =>
4958 New_Occurrence_Of (Standard_Integer, Loc),
4959 Constraint =>
4960 Make_Range_Constraint (Loc,
4961 Range_Expression => Make_Range (Loc,
4962 Make_Integer_Literal
4963 (Loc, -Entry_Family_Bound),
4964 Make_Integer_Literal
4965 (Loc, Entry_Family_Bound - 1)))));
4967 Insert_After (Current_Node, Bdecl);
4968 Current_Node := Bdecl;
4969 Analyze (Bdecl);
4970 end if;
4972 Efam_Decl :=
4973 Make_Full_Type_Declaration (Loc,
4974 Defining_Identifier => Efam_Type,
4975 Type_Definition =>
4976 Make_Unconstrained_Array_Definition (Loc,
4977 Subtype_Marks =>
4978 (New_List (New_Occurrence_Of (Bityp, Loc))),
4980 Component_Definition =>
4981 Make_Component_Definition (Loc,
4982 Aliased_Present => False,
4983 Subtype_Indication =>
4984 New_Occurrence_Of (Standard_Character, Loc))));
4985 end;
4987 Insert_After (Current_Node, Efam_Decl);
4988 Current_Node := Efam_Decl;
4989 Analyze (Efam_Decl);
4991 Append_To (Cdecls,
4992 Make_Component_Declaration (Loc,
4993 Defining_Identifier =>
4994 Make_Defining_Identifier (Loc, Chars (Efam)),
4996 Component_Definition =>
4997 Make_Component_Definition (Loc,
4998 Aliased_Present => False,
4999 Subtype_Indication =>
5000 Make_Subtype_Indication (Loc,
5001 Subtype_Mark =>
5002 New_Occurrence_Of (Efam_Type, Loc),
5004 Constraint =>
5005 Make_Index_Or_Discriminant_Constraint (Loc,
5006 Constraints => New_List (
5007 New_Occurrence_Of (Entry_Index_Type (Efam),
5008 Loc)))))));
5009 end if;
5011 Next_Entity (Efam);
5012 end loop;
5013 end Collect_Entry_Families;
5015 -----------------------
5016 -- Concurrent_Object --
5017 -----------------------
5019 function Concurrent_Object
5020 (Spec_Id : Entity_Id;
5021 Conc_Typ : Entity_Id) return Entity_Id
5023 begin
5024 -- Parameter _O or _object
5026 if Is_Protected_Type (Conc_Typ) then
5027 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5029 -- Parameter _task
5031 else
5032 pragma Assert (Is_Task_Type (Conc_Typ));
5033 return First_Formal (Task_Body_Procedure (Conc_Typ));
5034 end if;
5035 end Concurrent_Object;
5037 ----------------------
5038 -- Copy_Result_Type --
5039 ----------------------
5041 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5042 New_Res : constant Node_Id := New_Copy_Tree (Res);
5043 Par_Spec : Node_Id;
5044 Formal : Entity_Id;
5046 begin
5047 -- If the result type is an access_to_subprogram, we must create new
5048 -- entities for its spec.
5050 if Nkind (New_Res) = N_Access_Definition
5051 and then Present (Access_To_Subprogram_Definition (New_Res))
5052 then
5053 -- Provide new entities for the formals
5055 Par_Spec := First (Parameter_Specifications
5056 (Access_To_Subprogram_Definition (New_Res)));
5057 while Present (Par_Spec) loop
5058 Formal := Defining_Identifier (Par_Spec);
5059 Set_Defining_Identifier (Par_Spec,
5060 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5061 Next (Par_Spec);
5062 end loop;
5063 end if;
5065 return New_Res;
5066 end Copy_Result_Type;
5068 --------------------
5069 -- Concurrent_Ref --
5070 --------------------
5072 -- The expression returned for a reference to a concurrent object has the
5073 -- form:
5075 -- taskV!(name)._Task_Id
5077 -- for a task, and
5079 -- objectV!(name)._Object
5081 -- for a protected object. For the case of an access to a concurrent
5082 -- object, there is an extra explicit dereference:
5084 -- taskV!(name.all)._Task_Id
5085 -- objectV!(name.all)._Object
5087 -- here taskV and objectV are the types for the associated records, which
5088 -- contain the required _Task_Id and _Object fields for tasks and protected
5089 -- objects, respectively.
5091 -- For the case of a task type name, the expression is
5093 -- Self;
5095 -- i.e. a call to the Self function which returns precisely this Task_Id
5097 -- For the case of a protected type name, the expression is
5099 -- objectR
5101 -- which is a renaming of the _object field of the current object
5102 -- record, passed into protected operations as a parameter.
5104 function Concurrent_Ref (N : Node_Id) return Node_Id is
5105 Loc : constant Source_Ptr := Sloc (N);
5106 Ntyp : constant Entity_Id := Etype (N);
5107 Dtyp : Entity_Id;
5108 Sel : Name_Id;
5110 function Is_Current_Task (T : Entity_Id) return Boolean;
5111 -- Check whether the reference is to the immediately enclosing task
5112 -- type, or to an outer one (rare but legal).
5114 ---------------------
5115 -- Is_Current_Task --
5116 ---------------------
5118 function Is_Current_Task (T : Entity_Id) return Boolean is
5119 Scop : Entity_Id;
5121 begin
5122 Scop := Current_Scope;
5123 while Present (Scop) and then Scop /= Standard_Standard loop
5124 if Scop = T then
5125 return True;
5127 elsif Is_Task_Type (Scop) then
5128 return False;
5130 -- If this is a procedure nested within the task type, we must
5131 -- assume that it can be called from an inner task, and therefore
5132 -- cannot treat it as a local reference.
5134 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5135 return False;
5137 else
5138 Scop := Scope (Scop);
5139 end if;
5140 end loop;
5142 -- We know that we are within the task body, so should have found it
5143 -- in scope.
5145 raise Program_Error;
5146 end Is_Current_Task;
5148 -- Start of processing for Concurrent_Ref
5150 begin
5151 if Is_Access_Type (Ntyp) then
5152 Dtyp := Designated_Type (Ntyp);
5154 if Is_Protected_Type (Dtyp) then
5155 Sel := Name_uObject;
5156 else
5157 Sel := Name_uTask_Id;
5158 end if;
5160 return
5161 Make_Selected_Component (Loc,
5162 Prefix =>
5163 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5164 Make_Explicit_Dereference (Loc, N)),
5165 Selector_Name => Make_Identifier (Loc, Sel));
5167 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5168 if Is_Task_Type (Entity (N)) then
5170 if Is_Current_Task (Entity (N)) then
5171 return
5172 Make_Function_Call (Loc,
5173 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5175 else
5176 declare
5177 Decl : Node_Id;
5178 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5179 T_Body : constant Node_Id :=
5180 Parent (Corresponding_Body (Parent (Entity (N))));
5182 begin
5183 Decl :=
5184 Make_Object_Declaration (Loc,
5185 Defining_Identifier => T_Self,
5186 Object_Definition =>
5187 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5188 Expression =>
5189 Make_Function_Call (Loc,
5190 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5191 Prepend (Decl, Declarations (T_Body));
5192 Analyze (Decl);
5193 Set_Scope (T_Self, Entity (N));
5194 return New_Occurrence_Of (T_Self, Loc);
5195 end;
5196 end if;
5198 else
5199 pragma Assert (Is_Protected_Type (Entity (N)));
5201 return
5202 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5203 end if;
5205 else
5206 if Is_Protected_Type (Ntyp) then
5207 Sel := Name_uObject;
5208 elsif Is_Task_Type (Ntyp) then
5209 Sel := Name_uTask_Id;
5210 else
5211 raise Program_Error;
5212 end if;
5214 return
5215 Make_Selected_Component (Loc,
5216 Prefix =>
5217 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5218 New_Copy_Tree (N)),
5219 Selector_Name => Make_Identifier (Loc, Sel));
5220 end if;
5221 end Concurrent_Ref;
5223 ------------------------
5224 -- Convert_Concurrent --
5225 ------------------------
5227 function Convert_Concurrent
5228 (N : Node_Id;
5229 Typ : Entity_Id) return Node_Id
5231 begin
5232 if not Is_Concurrent_Type (Typ) then
5233 return N;
5234 else
5235 return
5236 Unchecked_Convert_To
5237 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5238 end if;
5239 end Convert_Concurrent;
5241 -------------------------------------
5242 -- Create_Secondary_Stack_For_Task --
5243 -------------------------------------
5245 function Create_Secondary_Stack_For_Task (T : Node_Id) return Boolean is
5246 begin
5247 return
5248 (Restriction_Active (No_Implicit_Heap_Allocations)
5249 or else Restriction_Active (No_Implicit_Task_Allocations))
5250 and then not Restriction_Active (No_Secondary_Stack)
5251 and then Has_Rep_Pragma
5252 (T, Name_Secondary_Stack_Size, Check_Parents => False);
5253 end Create_Secondary_Stack_For_Task;
5255 -------------------------------------
5256 -- Debug_Private_Data_Declarations --
5257 -------------------------------------
5259 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5260 Debug_Nod : Node_Id;
5261 Decl : Node_Id;
5263 begin
5264 Decl := First (Decls);
5265 while Present (Decl) and then not Comes_From_Source (Decl) loop
5267 -- Declaration for concurrent entity _object and its access type,
5268 -- along with the entry index subtype:
5269 -- type prot_typVP is access prot_typV;
5270 -- _object : prot_typVP := prot_typV (_O);
5271 -- subtype Jnn is <Type of Index> range Low .. High;
5273 if Nkind (Decl) in N_Full_Type_Declaration | N_Object_Declaration then
5274 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5276 -- Declaration for the Protection object, discriminals, privals, and
5277 -- entry index constant:
5278 -- conc_typR : protection_typ renames _object._object;
5279 -- discr_nameD : discr_typ renames _object.discr_name;
5280 -- discr_nameD : discr_typ renames _task.discr_name;
5281 -- prival_name : comp_typ renames _object.comp_name;
5282 -- J : constant Jnn :=
5283 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5285 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5286 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5287 Debug_Nod := Debug_Renaming_Declaration (Decl);
5289 if Present (Debug_Nod) then
5290 Insert_After (Decl, Debug_Nod);
5291 end if;
5292 end if;
5294 Next (Decl);
5295 end loop;
5296 end Debug_Private_Data_Declarations;
5298 ------------------------------
5299 -- Ensure_Statement_Present --
5300 ------------------------------
5302 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5303 Stmt : Node_Id;
5305 begin
5306 if Opt.Suppress_Control_Flow_Optimizations
5307 and then Is_Empty_List (Statements (Alt))
5308 then
5309 Stmt := Make_Null_Statement (Loc);
5311 -- Mark NULL statement as coming from source so that it is not
5312 -- eliminated by GIGI.
5314 -- Another covert channel. If this is a requirement, it must be
5315 -- documented in sinfo/einfo ???
5317 Set_Comes_From_Source (Stmt, True);
5319 Set_Statements (Alt, New_List (Stmt));
5320 end if;
5321 end Ensure_Statement_Present;
5323 ----------------------------
5324 -- Entry_Index_Expression --
5325 ----------------------------
5327 function Entry_Index_Expression
5328 (Sloc : Source_Ptr;
5329 Ent : Entity_Id;
5330 Index : Node_Id;
5331 Ttyp : Entity_Id) return Node_Id
5333 Expr : Node_Id;
5334 Num : Node_Id;
5335 Lo : Node_Id;
5336 Hi : Node_Id;
5337 Prev : Entity_Id;
5338 S : Node_Id;
5340 begin
5341 -- The queues of entries and entry families appear in textual order in
5342 -- the associated record. The entry index is computed as the sum of the
5343 -- number of queues for all entries that precede the designated one, to
5344 -- which is added the index expression, if this expression denotes a
5345 -- member of a family.
5347 -- The following is a place holder for the count of simple entries
5349 Num := Make_Integer_Literal (Sloc, 1);
5351 -- We construct an expression which is a series of addition operations.
5352 -- The first operand is the number of single entries that precede this
5353 -- one, the second operand is the index value relative to the start of
5354 -- the referenced family, and the remaining operands are the lengths of
5355 -- the entry families that precede this entry, i.e. the constructed
5356 -- expression is:
5358 -- number_simple_entries +
5359 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5360 -- family'length + ...
5362 -- where index-value is the given index value, and s is the index
5363 -- subtype (we have to use pos because the subtype might be an
5364 -- enumeration type preventing direct subtraction). Note that the task
5365 -- entry array is one-indexed.
5367 -- The upper bound of the entry family may be a discriminant, so we
5368 -- retrieve the lower bound explicitly to compute offset, rather than
5369 -- using the index subtype which may mention a discriminant.
5371 if Present (Index) then
5372 S := Entry_Index_Type (Ent);
5374 -- First make sure the index is in range if requested. The index type
5375 -- is the pristine Entry_Index_Type of the entry.
5377 if Do_Range_Check (Index) then
5378 Generate_Range_Check (Index, S, CE_Range_Check_Failed);
5379 end if;
5381 Expr :=
5382 Make_Op_Add (Sloc,
5383 Left_Opnd => Num,
5384 Right_Opnd =>
5385 Family_Offset
5386 (Sloc,
5387 Make_Attribute_Reference (Sloc,
5388 Attribute_Name => Name_Pos,
5389 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5390 Expressions => New_List (Relocate_Node (Index))),
5391 Type_Low_Bound (S),
5392 Ttyp,
5393 False));
5394 else
5395 Expr := Num;
5396 end if;
5398 -- Now add lengths of preceding entries and entry families
5400 Prev := First_Entity (Ttyp);
5401 while Chars (Prev) /= Chars (Ent)
5402 or else Ekind (Prev) /= Ekind (Ent)
5403 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5404 loop
5405 if Ekind (Prev) = E_Entry then
5406 Set_Intval (Num, Intval (Num) + 1);
5408 elsif Ekind (Prev) = E_Entry_Family then
5409 S := Entry_Index_Type (Prev);
5410 Lo := Type_Low_Bound (S);
5411 Hi := Type_High_Bound (S);
5413 Expr :=
5414 Make_Op_Add (Sloc,
5415 Left_Opnd => Expr,
5416 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5418 -- Other components are anonymous types to be ignored
5420 else
5421 null;
5422 end if;
5424 Next_Entity (Prev);
5425 end loop;
5427 return Expr;
5428 end Entry_Index_Expression;
5430 ---------------------------
5431 -- Establish_Task_Master --
5432 ---------------------------
5434 procedure Establish_Task_Master (N : Node_Id) is
5435 Call : Node_Id;
5437 begin
5438 if Restriction_Active (No_Task_Hierarchy) = False then
5439 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5441 -- The block may have no declarations (and nevertheless be a task
5442 -- master) if it contains a call that may return an object that
5443 -- contains tasks.
5445 if No (Declarations (N)) then
5446 Set_Declarations (N, New_List (Call));
5447 else
5448 Prepend_To (Declarations (N), Call);
5449 end if;
5451 Analyze (Call);
5452 end if;
5453 end Establish_Task_Master;
5455 --------------------------------
5456 -- Expand_Accept_Declarations --
5457 --------------------------------
5459 -- Part of the expansion of an accept statement involves the creation of
5460 -- a declaration that can be referenced from the statement sequence of
5461 -- the accept:
5463 -- Ann : Address;
5465 -- This declaration is inserted immediately before the accept statement
5466 -- and it is important that it be inserted before the statements of the
5467 -- statement sequence are analyzed. Thus it would be too late to create
5468 -- this declaration in the Expand_N_Accept_Statement routine, which is
5469 -- why there is a separate procedure to be called directly from Sem_Ch9.
5471 -- Ann is used to hold the address of the record containing the parameters
5472 -- (see Expand_N_Entry_Call for more details on how this record is built).
5473 -- References to the parameters do an unchecked conversion of this address
5474 -- to a pointer to the required record type, and then access the field that
5475 -- holds the value of the required parameter. The entity for the address
5476 -- variable is held as the top stack element (i.e. the last element) of the
5477 -- Accept_Address stack in the corresponding entry entity, and this element
5478 -- must be set in place before the statements are processed.
5480 -- The above description applies to the case of a stand alone accept
5481 -- statement, i.e. one not appearing as part of a select alternative.
5483 -- For the case of an accept that appears as part of a select alternative
5484 -- of a selective accept, we must still create the declaration right away,
5485 -- since Ann is needed immediately, but there is an important difference:
5487 -- The declaration is inserted before the selective accept, not before
5488 -- the accept statement (which is not part of a list anyway, and so would
5489 -- not accommodate inserted declarations)
5491 -- We only need one address variable for the entire selective accept. So
5492 -- the Ann declaration is created only for the first accept alternative,
5493 -- and subsequent accept alternatives reference the same Ann variable.
5495 -- We can distinguish the two cases by seeing whether the accept statement
5496 -- is part of a list. If not, then it must be in an accept alternative.
5498 -- To expand the requeue statement, a label is provided at the end of the
5499 -- accept statement or alternative of which it is a part, so that the
5500 -- statement can be skipped after the requeue is complete. This label is
5501 -- created here rather than during the expansion of the accept statement,
5502 -- because it will be needed by any requeue statements within the accept,
5503 -- which are expanded before the accept.
5505 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5506 Loc : constant Source_Ptr := Sloc (N);
5507 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5508 Ann : Entity_Id := Empty;
5509 Adecl : Node_Id;
5510 Lab : Node_Id;
5511 Ldecl : Node_Id;
5512 Ldecl2 : Node_Id;
5514 begin
5515 if Expander_Active then
5517 -- If we have no handled statement sequence, we may need to build
5518 -- a dummy sequence consisting of a null statement. This can be
5519 -- skipped if the trivial accept optimization is permitted.
5521 if not Trivial_Accept_OK
5522 and then (No (Stats) or else Null_Statements (Statements (Stats)))
5523 then
5524 Set_Handled_Statement_Sequence (N,
5525 Make_Handled_Sequence_Of_Statements (Loc,
5526 Statements => New_List (Make_Null_Statement (Loc))));
5527 end if;
5529 -- Create and declare two labels to be placed at the end of the
5530 -- accept statement. The first label is used to allow requeues to
5531 -- skip the remainder of entry processing. The second label is used
5532 -- to skip the remainder of entry processing if the rendezvous
5533 -- completes in the middle of the accept body.
5535 if Present (Handled_Statement_Sequence (N)) then
5536 declare
5537 Ent : Entity_Id;
5539 begin
5540 Ent := Make_Temporary (Loc, 'L');
5541 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5542 Ldecl :=
5543 Make_Implicit_Label_Declaration (Loc,
5544 Defining_Identifier => Ent,
5545 Label_Construct => Lab);
5546 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5548 Ent := Make_Temporary (Loc, 'L');
5549 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5550 Ldecl2 :=
5551 Make_Implicit_Label_Declaration (Loc,
5552 Defining_Identifier => Ent,
5553 Label_Construct => Lab);
5554 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5555 end;
5557 else
5558 Ldecl := Empty;
5559 Ldecl2 := Empty;
5560 end if;
5562 -- Case of stand alone accept statement
5564 if Is_List_Member (N) then
5566 if Present (Handled_Statement_Sequence (N)) then
5567 Ann := Make_Temporary (Loc, 'A');
5569 Adecl :=
5570 Make_Object_Declaration (Loc,
5571 Defining_Identifier => Ann,
5572 Object_Definition =>
5573 New_Occurrence_Of (RTE (RE_Address), Loc));
5575 Insert_Before_And_Analyze (N, Adecl);
5576 Insert_Before_And_Analyze (N, Ldecl);
5577 Insert_Before_And_Analyze (N, Ldecl2);
5578 end if;
5580 -- Case of accept statement which is in an accept alternative
5582 else
5583 declare
5584 Acc_Alt : constant Node_Id := Parent (N);
5585 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5586 Alt : Node_Id;
5588 begin
5589 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5590 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5592 -- ??? Consider a single label for select statements
5594 if Present (Handled_Statement_Sequence (N)) then
5595 Prepend (Ldecl2,
5596 Statements (Handled_Statement_Sequence (N)));
5597 Analyze (Ldecl2);
5599 Prepend (Ldecl,
5600 Statements (Handled_Statement_Sequence (N)));
5601 Analyze (Ldecl);
5602 end if;
5604 -- Find first accept alternative of the selective accept. A
5605 -- valid selective accept must have at least one accept in it.
5607 Alt := First (Select_Alternatives (Sel_Acc));
5609 while Nkind (Alt) /= N_Accept_Alternative loop
5610 Next (Alt);
5611 end loop;
5613 -- If this is the first accept statement, then we have to
5614 -- create the Ann variable, as for the stand alone case, except
5615 -- that it is inserted before the selective accept. Similarly,
5616 -- a label for requeue expansion must be declared.
5618 if N = Accept_Statement (Alt) then
5619 Ann := Make_Temporary (Loc, 'A');
5620 Adecl :=
5621 Make_Object_Declaration (Loc,
5622 Defining_Identifier => Ann,
5623 Object_Definition =>
5624 New_Occurrence_Of (RTE (RE_Address), Loc));
5626 Insert_Before_And_Analyze (Sel_Acc, Adecl);
5628 -- If this is not the first accept statement, then find the Ann
5629 -- variable allocated by the first accept and use it.
5631 else
5632 Ann :=
5633 Node (Last_Elmt (Accept_Address
5634 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5635 end if;
5636 end;
5637 end if;
5639 -- Merge here with Ann either created or referenced, and Adecl
5640 -- pointing to the corresponding declaration. Remaining processing
5641 -- is the same for the two cases.
5643 if Present (Ann) then
5644 Append_Elmt (Ann, Accept_Address (Ent));
5645 Set_Debug_Info_Needed (Ann);
5646 end if;
5648 -- Create renaming declarations for the entry formals. Each reference
5649 -- to a formal becomes a dereference of a component of the parameter
5650 -- block, whose address is held in Ann. These declarations are
5651 -- eventually inserted into the accept block, and analyzed there so
5652 -- that they have the proper scope for gdb and do not conflict with
5653 -- other declarations.
5655 if Present (Parameter_Specifications (N))
5656 and then Present (Handled_Statement_Sequence (N))
5657 then
5658 declare
5659 Comp : Entity_Id;
5660 Decl : Node_Id;
5661 Formal : Entity_Id;
5662 New_F : Entity_Id;
5663 Renamed_Formal : Node_Id;
5665 begin
5666 Push_Scope (Ent);
5667 Formal := First_Formal (Ent);
5669 while Present (Formal) loop
5670 Comp := Entry_Component (Formal);
5671 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
5673 Set_Etype (New_F, Etype (Formal));
5674 Set_Scope (New_F, Ent);
5676 -- Now we set debug info needed on New_F even though it does
5677 -- not come from source, so that the debugger will get the
5678 -- right information for these generated names.
5680 Set_Debug_Info_Needed (New_F);
5682 if Ekind (Formal) = E_In_Parameter then
5683 Mutate_Ekind (New_F, E_Constant);
5684 else
5685 Mutate_Ekind (New_F, E_Variable);
5686 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5687 end if;
5689 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5691 Renamed_Formal :=
5692 Make_Selected_Component (Loc,
5693 Prefix =>
5694 Make_Explicit_Dereference (Loc,
5695 Unchecked_Convert_To (
5696 Entry_Parameters_Type (Ent),
5697 New_Occurrence_Of (Ann, Loc))),
5698 Selector_Name =>
5699 New_Occurrence_Of (Comp, Loc));
5701 Decl :=
5702 Build_Renamed_Formal_Declaration
5703 (New_F, Formal, Comp, Renamed_Formal);
5705 if No (Declarations (N)) then
5706 Set_Declarations (N, New_List);
5707 end if;
5709 Append (Decl, Declarations (N));
5710 Set_Renamed_Object (Formal, New_F);
5711 Next_Formal (Formal);
5712 end loop;
5714 End_Scope;
5715 end;
5716 end if;
5717 end if;
5718 end Expand_Accept_Declarations;
5720 ---------------------------------------------
5721 -- Expand_Access_Protected_Subprogram_Type --
5722 ---------------------------------------------
5724 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5725 Loc : constant Source_Ptr := Sloc (N);
5726 T : constant Entity_Id := Defining_Identifier (N);
5727 D_T : constant Entity_Id := Designated_Type (T);
5728 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
5729 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
5730 P_List : constant List_Id :=
5731 Build_Protected_Spec (N, RTE (RE_Address), D_T, False);
5733 Comps : List_Id;
5734 Decl1 : Node_Id;
5735 Decl2 : Node_Id;
5736 Def1 : Node_Id;
5738 begin
5739 -- Create access to subprogram with full signature
5741 if Etype (D_T) /= Standard_Void_Type then
5742 Def1 :=
5743 Make_Access_Function_Definition (Loc,
5744 Parameter_Specifications => P_List,
5745 Result_Definition =>
5746 Copy_Result_Type (Result_Definition (Type_Definition (N))));
5748 else
5749 Def1 :=
5750 Make_Access_Procedure_Definition (Loc,
5751 Parameter_Specifications => P_List);
5752 end if;
5754 Decl1 :=
5755 Make_Full_Type_Declaration (Loc,
5756 Defining_Identifier => D_T2,
5757 Type_Definition => Def1);
5759 -- Declare the new types before the original one since the latter will
5760 -- refer to them through the Equivalent_Type slot.
5762 Insert_Before_And_Analyze (N, Decl1);
5764 -- Associate the access to subprogram with its original access to
5765 -- protected subprogram type. Needed by the backend to know that this
5766 -- type corresponds with an access to protected subprogram type.
5768 Set_Original_Access_Type (D_T2, T);
5770 -- Create Equivalent_Type, a record with two components for an access to
5771 -- object and an access to subprogram.
5773 Comps := New_List (
5774 Make_Component_Declaration (Loc,
5775 Defining_Identifier => Make_Temporary (Loc, 'P'),
5776 Component_Definition =>
5777 Make_Component_Definition (Loc,
5778 Aliased_Present => False,
5779 Subtype_Indication =>
5780 New_Occurrence_Of (RTE (RE_Address), Loc))),
5782 Make_Component_Declaration (Loc,
5783 Defining_Identifier => Make_Temporary (Loc, 'S'),
5784 Component_Definition =>
5785 Make_Component_Definition (Loc,
5786 Aliased_Present => False,
5787 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
5789 Decl2 :=
5790 Make_Full_Type_Declaration (Loc,
5791 Defining_Identifier => E_T,
5792 Type_Definition =>
5793 Make_Record_Definition (Loc,
5794 Component_List =>
5795 Make_Component_List (Loc, Component_Items => Comps)));
5797 Insert_Before_And_Analyze (N, Decl2);
5798 Set_Equivalent_Type (T, E_T);
5799 end Expand_Access_Protected_Subprogram_Type;
5801 --------------------------
5802 -- Expand_Entry_Barrier --
5803 --------------------------
5805 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
5806 Cond : constant Node_Id := Condition (Entry_Body_Formal_Part (N));
5807 Prot : constant Entity_Id := Scope (Ent);
5808 Spec_Decl : constant Node_Id := Parent (Prot);
5810 Func_Id : Entity_Id := Empty;
5811 -- The entity of the barrier function
5813 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
5814 -- Check whether entity in Barrier is external to protected type.
5815 -- If so, barrier may not be properly synchronized.
5817 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
5818 -- Check whether N meets the Pure_Barriers restriction. Return OK if
5819 -- so.
5821 function Is_Simple_Barrier (N : Node_Id) return Boolean;
5822 -- Check whether N meets the Simple_Barriers restriction. Return OK if
5823 -- so.
5825 ----------------------
5826 -- Is_Global_Entity --
5827 ----------------------
5829 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
5830 E : Entity_Id;
5831 S : Entity_Id;
5833 begin
5834 if Is_Entity_Name (N) and then Present (Entity (N)) then
5835 E := Entity (N);
5836 S := Scope (E);
5838 if Ekind (E) = E_Variable then
5840 -- If the variable is local to the barrier function generated
5841 -- during expansion, it is ok. If expansion is not performed,
5842 -- then Func is Empty so this test cannot succeed.
5844 if Scope (E) = Func_Id then
5845 null;
5847 -- A protected call from a barrier to another object is ok
5849 elsif Ekind (Etype (E)) = E_Protected_Type then
5850 null;
5852 -- If the variable is within the package body we consider
5853 -- this safe. This is a common (if dubious) idiom.
5855 elsif S = Scope (Prot)
5856 and then Is_Package_Or_Generic_Package (S)
5857 and then Nkind (Parent (E)) = N_Object_Declaration
5858 and then Nkind (Parent (Parent (E))) = N_Package_Body
5859 then
5860 null;
5862 else
5863 Error_Msg_N ("potentially unsynchronized barrier??", N);
5864 Error_Msg_N ("\& should be private component of type??", N);
5865 end if;
5866 end if;
5867 end if;
5869 return OK;
5870 end Is_Global_Entity;
5872 procedure Check_Unprotected_Barrier is
5873 new Traverse_Proc (Is_Global_Entity);
5875 -----------------------
5876 -- Is_Simple_Barrier --
5877 -----------------------
5879 function Is_Simple_Barrier (N : Node_Id) return Boolean is
5880 Renamed : Node_Id;
5882 begin
5883 if Is_Static_Expression (N) then
5884 return True;
5885 elsif Ada_Version >= Ada_2022
5886 and then Nkind (N) in N_Selected_Component | N_Indexed_Component
5887 and then Statically_Names_Object (N)
5888 then
5889 -- Restriction relaxed in Ada 2022 to allow statically named
5890 -- subcomponents.
5891 return Is_Simple_Barrier (Prefix (N));
5892 end if;
5894 -- Check if the name is a component of the protected object. If
5895 -- the expander is active, the component has been transformed into a
5896 -- renaming of _object.all.component. Original_Node is needed in case
5897 -- validity checking is enabled, in which case the simple object
5898 -- reference will have been rewritten.
5900 if Expander_Active then
5902 -- The expanded name may have been constant folded in which case
5903 -- the original node is not necessarily an entity name (e.g. an
5904 -- indexed component).
5906 if not Is_Entity_Name (Original_Node (N)) then
5907 return False;
5908 end if;
5910 Renamed := Renamed_Object (Entity (Original_Node (N)));
5912 return
5913 Present (Renamed)
5914 and then Nkind (Renamed) = N_Selected_Component
5915 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
5916 elsif not Is_Entity_Name (N) then
5917 return False;
5918 else
5919 return Is_Protected_Component (Entity (N));
5920 end if;
5921 end Is_Simple_Barrier;
5923 ---------------------
5924 -- Is_Pure_Barrier --
5925 ---------------------
5927 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
5928 begin
5929 case Nkind (N) is
5930 when N_Expanded_Name
5931 | N_Identifier
5934 -- Because of N_Expanded_Name case, return Skip instead of OK.
5936 if No (Entity (N)) then
5937 return Abandon;
5939 elsif Is_Numeric_Type (Entity (N)) then
5940 return Skip;
5941 end if;
5943 case Ekind (Entity (N)) is
5944 when E_Constant
5945 | E_Discriminant
5947 return Skip;
5949 when E_Enumeration_Literal
5950 | E_Named_Integer
5951 | E_Named_Real
5953 if not Is_OK_Static_Expression (N) then
5954 return Abandon;
5955 end if;
5956 return Skip;
5958 when E_Component =>
5959 return Skip;
5961 when E_Variable =>
5962 if Is_Simple_Barrier (N) then
5963 return Skip;
5964 end if;
5966 when E_Function =>
5968 -- The count attribute has been transformed into run-time
5969 -- calls.
5971 if Is_RTE (Entity (N), RE_Protected_Count)
5972 or else Is_RTE (Entity (N), RE_Protected_Count_Entry)
5973 then
5974 return Skip;
5975 end if;
5977 when others =>
5978 null;
5979 end case;
5981 when N_Function_Call =>
5983 -- Function call checks are carried out as part of the analysis
5984 -- of the function call name.
5986 return OK;
5988 when N_Character_Literal
5989 | N_Integer_Literal
5990 | N_Real_Literal
5992 return OK;
5994 when N_Op_Boolean
5995 | N_Op_Not
5997 if Ekind (Entity (N)) = E_Operator then
5998 return OK;
5999 end if;
6001 when N_Short_Circuit
6002 | N_If_Expression
6003 | N_Case_Expression
6005 return OK;
6007 when N_Indexed_Component | N_Selected_Component =>
6008 if Statically_Names_Object (N) then
6009 return Is_Pure_Barrier (Prefix (N));
6010 else
6011 return Abandon;
6012 end if;
6014 when N_Case_Expression_Alternative =>
6015 -- do not traverse Discrete_Choices subtree
6016 if Is_Pure_Barrier (Expression (N)) /= Abandon then
6017 return Skip;
6018 end if;
6020 when N_Expression_With_Actions =>
6021 -- this may occur in the case of a Count attribute reference
6022 if Is_Rewrite_Substitution (N)
6023 and then Is_Pure_Barrier (Original_Node (N)) /= Abandon
6024 then
6025 return Skip;
6026 end if;
6028 when N_Membership_Test =>
6029 if Is_Pure_Barrier (Left_Opnd (N)) /= Abandon
6030 and then All_Membership_Choices_Static (N)
6031 then
6032 return Skip;
6033 end if;
6035 when N_Type_Conversion =>
6037 -- Conversions to Universal_Integer do not raise constraint
6038 -- errors. Likewise if the expression's type is statically
6039 -- compatible with the target's type.
6041 if Etype (N) = Universal_Integer
6042 or else Subtypes_Statically_Compatible
6043 (Etype (Expression (N)), Etype (N))
6044 then
6045 return OK;
6046 end if;
6048 when N_Unchecked_Type_Conversion =>
6049 return OK;
6051 when others =>
6052 null;
6053 end case;
6055 return Abandon;
6056 end Is_Pure_Barrier;
6058 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6060 -- Local variables
6062 Entry_Body : Node_Id;
6063 Func_Body : Node_Id := Empty;
6065 -- Start of processing for Expand_Entry_Barrier
6067 begin
6068 if No_Run_Time_Mode then
6069 Error_Msg_CRT ("entry barrier", N);
6070 return;
6071 end if;
6073 -- Prevent cascaded errors
6075 if Nkind (Cond) = N_Error then
6076 return;
6077 end if;
6079 -- The body of the entry barrier must be analyzed in the context of the
6080 -- protected object, but its scope is external to it, just as any other
6081 -- unprotected version of a protected operation. The specification has
6082 -- been produced when the protected type declaration was elaborated. We
6083 -- build the body, insert it in the enclosing scope, but analyze it in
6084 -- the current context. A more uniform approach would be to treat the
6085 -- barrier just as a protected function, and discard the protected
6086 -- version of it because it is never called.
6088 if Expander_Active then
6089 Func_Body := Build_Barrier_Function (N, Ent, Prot);
6090 Func_Id := Barrier_Function (Ent);
6091 Set_Corresponding_Spec (Func_Body, Func_Id);
6093 Entry_Body := Parent (Corresponding_Body (Spec_Decl));
6095 if Nkind (Parent (Entry_Body)) = N_Subunit then
6096 Entry_Body := Corresponding_Stub (Parent (Entry_Body));
6097 end if;
6099 Insert_Before_And_Analyze (Entry_Body, Func_Body);
6101 Set_Discriminals (Spec_Decl);
6102 Set_Scope (Func_Id, Scope (Prot));
6104 else
6105 Analyze_And_Resolve (Cond, Any_Boolean);
6106 end if;
6108 -- Check Simple_Barriers and Pure_Barriers restrictions.
6109 -- Note that it is safe to be calling Check_Restriction from here, even
6110 -- though this is part of the expander, since Expand_Entry_Barrier is
6111 -- called from Sem_Ch9 even in -gnatc mode.
6113 if not Is_Simple_Barrier (Cond) then
6114 -- flag restriction violation
6115 Check_Restriction (Simple_Barriers, Cond);
6116 end if;
6118 if Check_Pure_Barriers (Cond) = Abandon then
6119 -- flag restriction violation
6120 Check_Restriction (Pure_Barriers, Cond);
6122 -- Emit warning if barrier contains global entities and is thus
6123 -- potentially unsynchronized (if Pure_Barriers restrictions
6124 -- are met then no need to check for this).
6125 Check_Unprotected_Barrier (Cond);
6126 end if;
6128 -- Perform a small optimization of simple barrier functions. If the
6129 -- scope of the condition's entity is not the barrier function, then
6130 -- the condition does not depend on any of the generated renamings.
6131 -- If this is the case, eliminate the renamings as they are useless.
6132 -- This optimization is not performed when the condition was folded
6133 -- and validity checks are in effect because the original condition
6134 -- may have produced at least one check that depends on the generated
6135 -- renamings.
6137 if Expander_Active
6138 and then Is_Entity_Name (Cond)
6139 and then Scope (Entity (Cond)) /= Func_Id
6140 and then not Validity_Check_Operands
6141 then
6142 Set_Declarations (Func_Body, Empty_List);
6143 end if;
6144 end Expand_Entry_Barrier;
6146 ------------------------------
6147 -- Expand_N_Abort_Statement --
6148 ------------------------------
6150 -- Expand abort T1, T2, .. Tn; into:
6151 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6153 procedure Expand_N_Abort_Statement (N : Node_Id) is
6154 Loc : constant Source_Ptr := Sloc (N);
6155 Tlist : constant List_Id := Names (N);
6156 Count : Nat;
6157 Aggr : Node_Id;
6158 Tasknm : Node_Id;
6160 begin
6161 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6162 Count := 0;
6164 Tasknm := First (Tlist);
6166 while Present (Tasknm) loop
6167 Count := Count + 1;
6169 -- A task interface class-wide type object is being aborted. Retrieve
6170 -- its _task_id by calling a dispatching routine.
6172 if Ada_Version >= Ada_2005
6173 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6174 and then Is_Interface (Etype (Tasknm))
6175 and then Is_Task_Interface (Etype (Tasknm))
6176 then
6177 Append_To (Component_Associations (Aggr),
6178 Make_Component_Association (Loc,
6179 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6180 Expression =>
6182 -- Task_Id (Tasknm._disp_get_task_id)
6184 Unchecked_Convert_To
6185 (RTE (RO_ST_Task_Id),
6186 Make_Selected_Component (Loc,
6187 Prefix => New_Copy_Tree (Tasknm),
6188 Selector_Name =>
6189 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6191 else
6192 Append_To (Component_Associations (Aggr),
6193 Make_Component_Association (Loc,
6194 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6195 Expression => Concurrent_Ref (Tasknm)));
6196 end if;
6198 Next (Tasknm);
6199 end loop;
6201 Rewrite (N,
6202 Make_Procedure_Call_Statement (Loc,
6203 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6204 Parameter_Associations => New_List (
6205 Make_Qualified_Expression (Loc,
6206 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6207 Expression => Aggr))));
6209 Analyze (N);
6210 end Expand_N_Abort_Statement;
6212 -------------------------------
6213 -- Expand_N_Accept_Statement --
6214 -------------------------------
6216 -- This procedure handles expansion of accept statements that stand alone,
6217 -- i.e. they are not part of an accept alternative. The expansion of
6218 -- accept statement in accept alternatives is handled by the routines
6219 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6220 -- following description applies only to stand alone accept statements.
6222 -- If there is no handled statement sequence, or only null statements, then
6223 -- this is called a trivial accept, and the expansion is:
6225 -- Accept_Trivial (entry-index)
6227 -- If there is a handled statement sequence, then the expansion is:
6229 -- Ann : Address;
6230 -- {Lnn : Label}
6232 -- begin
6233 -- begin
6234 -- Accept_Call (entry-index, Ann);
6235 -- Renaming_Declarations for formals
6236 -- <statement sequence from N_Accept_Statement node>
6237 -- Complete_Rendezvous;
6238 -- <<Lnn>>
6240 -- exception
6241 -- when ... =>
6242 -- <exception handler from N_Accept_Statement node>
6243 -- Complete_Rendezvous;
6244 -- when ... =>
6245 -- <exception handler from N_Accept_Statement node>
6246 -- Complete_Rendezvous;
6247 -- ...
6248 -- end;
6250 -- exception
6251 -- when all others =>
6252 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6253 -- end;
6255 -- The first three declarations were already inserted ahead of the accept
6256 -- statement by the Expand_Accept_Declarations procedure, which was called
6257 -- directly from the semantics during analysis of the accept statement,
6258 -- before analyzing its contained statements.
6260 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6261 -- from possible expansion activity (the original source of course does
6262 -- not have any declarations associated with the accept statement, since
6263 -- an accept statement has no declarative part). In particular, if the
6264 -- expander is active, the first such declaration is the declaration of
6265 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6267 -- The two blocks are merged into a single block if the inner block has
6268 -- no exception handlers, but otherwise two blocks are required, since
6269 -- exceptions might be raised in the exception handlers of the inner
6270 -- block, and Exceptional_Complete_Rendezvous must be called.
6272 procedure Expand_N_Accept_Statement (N : Node_Id) is
6273 Loc : constant Source_Ptr := Sloc (N);
6274 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6275 Ename : constant Node_Id := Entry_Direct_Name (N);
6276 Eindx : constant Node_Id := Entry_Index (N);
6277 Eent : constant Entity_Id := Entity (Ename);
6278 Acstack : constant Elist_Id := Accept_Address (Eent);
6279 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6280 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6281 Blkent : Entity_Id;
6282 Call : Node_Id;
6283 Block : Node_Id;
6285 begin
6286 -- If the accept statement is not part of a list, then its parent must
6287 -- be an accept alternative, and, as described above, we do not do any
6288 -- expansion for such accept statements at this level.
6290 if not Is_List_Member (N) then
6291 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6292 return;
6294 -- Trivial accept case (no statement sequence, or null statements).
6295 -- If the accept statement has declarations, then just insert them
6296 -- before the procedure call.
6298 elsif Trivial_Accept_OK
6299 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6300 then
6301 -- Remove declarations for renamings, because the parameter block
6302 -- will not be assigned.
6304 declare
6305 D : Node_Id;
6306 Next_D : Node_Id;
6308 begin
6309 D := First (Declarations (N));
6310 while Present (D) loop
6311 Next_D := Next (D);
6312 if Nkind (D) = N_Object_Renaming_Declaration then
6313 Remove (D);
6314 end if;
6316 D := Next_D;
6317 end loop;
6318 end;
6320 if Present (Declarations (N)) then
6321 Insert_Actions (N, Declarations (N));
6322 end if;
6324 Rewrite (N,
6325 Make_Procedure_Call_Statement (Loc,
6326 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6327 Parameter_Associations => New_List (
6328 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6330 Analyze (N);
6332 -- Ada 2022 (AI12-0279)
6334 if Has_Yield_Aspect (Eent)
6335 and then RTE_Available (RE_Yield)
6336 then
6337 Insert_Action_After (N,
6338 Make_Procedure_Call_Statement (Loc,
6339 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6340 end if;
6342 -- Discard Entry_Address that was created for it, so it will not be
6343 -- emitted if this accept statement is in the statement part of a
6344 -- delay alternative.
6346 if Present (Stats) then
6347 Remove_Last_Elmt (Acstack);
6348 end if;
6350 -- Case of statement sequence present
6352 else
6353 -- Construct the block, using the declarations from the accept
6354 -- statement if any to initialize the declarations of the block.
6356 Blkent := Make_Temporary (Loc, 'A');
6357 Mutate_Ekind (Blkent, E_Block);
6358 Set_Etype (Blkent, Standard_Void_Type);
6359 Set_Scope (Blkent, Current_Scope);
6361 Block :=
6362 Make_Block_Statement (Loc,
6363 Identifier => New_Occurrence_Of (Blkent, Loc),
6364 Declarations => Declarations (N),
6365 Handled_Statement_Sequence => Build_Accept_Body (N));
6367 -- Reset the Scope of local entities associated with the accept
6368 -- statement (that currently reference the entry scope) to the
6369 -- block scope, to avoid having references to the locals treated
6370 -- as up-level references.
6372 Reset_Scopes_To (Block, Blkent);
6374 -- For the analysis of the generated declarations, the parent node
6375 -- must be properly set.
6377 Set_Parent (Block, Parent (N));
6378 Set_Parent (Blkent, Block);
6380 -- Prepend call to Accept_Call to main statement sequence If the
6381 -- accept has exception handlers, the statement sequence is wrapped
6382 -- in a block. Insert call and renaming declarations in the
6383 -- declarations of the block, so they are elaborated before the
6384 -- handlers.
6386 Call :=
6387 Make_Procedure_Call_Statement (Loc,
6388 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6389 Parameter_Associations => New_List (
6390 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6391 New_Occurrence_Of (Ann, Loc)));
6393 if Parent (Stats) = N then
6394 Prepend (Call, Statements (Stats));
6395 else
6396 Set_Declarations (Parent (Stats), New_List (Call));
6397 end if;
6399 Analyze (Call);
6401 Push_Scope (Blkent);
6403 declare
6404 D : Node_Id;
6405 Next_D : Node_Id;
6406 Typ : Entity_Id;
6408 begin
6409 D := First (Declarations (N));
6410 while Present (D) loop
6411 Next_D := Next (D);
6413 if Nkind (D) = N_Object_Renaming_Declaration then
6415 -- The renaming declarations for the formals were created
6416 -- during analysis of the accept statement, and attached to
6417 -- the list of declarations. Place them now in the context
6418 -- of the accept block or subprogram.
6420 Remove (D);
6421 Typ := Entity (Subtype_Mark (D));
6422 Insert_After (Call, D);
6423 Analyze (D);
6425 -- If the formal is class_wide, it does not have an actual
6426 -- subtype. The analysis of the renaming declaration creates
6427 -- one, but we need to retain the class-wide nature of the
6428 -- entity.
6430 if Is_Class_Wide_Type (Typ) then
6431 Set_Etype (Defining_Identifier (D), Typ);
6432 end if;
6434 end if;
6436 D := Next_D;
6437 end loop;
6438 end;
6440 End_Scope;
6442 -- Replace the accept statement by the new block
6444 Rewrite (N, Block);
6445 Analyze (N);
6447 -- Last step is to unstack the Accept_Address value
6449 Remove_Last_Elmt (Acstack);
6450 end if;
6451 end Expand_N_Accept_Statement;
6453 ----------------------------------
6454 -- Expand_N_Asynchronous_Select --
6455 ----------------------------------
6457 -- This procedure assumes that the trigger statement is an entry call or
6458 -- a dispatching procedure call. A delay alternative should already have
6459 -- been expanded into an entry call to the appropriate delay object Wait
6460 -- entry.
6462 -- If the trigger is a task entry call, the select is implemented with
6463 -- a Task_Entry_Call:
6465 -- declare
6466 -- B : Boolean;
6467 -- C : Boolean;
6468 -- P : parms := (parm, parm, parm);
6470 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6472 -- procedure _clean is
6473 -- begin
6474 -- ...
6475 -- Cancel_Task_Entry_Call (C);
6476 -- ...
6477 -- end _clean;
6479 -- begin
6480 -- Abort_Defer;
6481 -- Task_Entry_Call
6482 -- (<acceptor-task>, -- Acceptor
6483 -- <entry-index>, -- E
6484 -- P'Address, -- Uninterpreted_Data
6485 -- Asynchronous_Call, -- Mode
6486 -- B); -- Rendezvous_Successful
6488 -- begin
6489 -- begin
6490 -- Abort_Undefer;
6491 -- <abortable-part>
6492 -- at end
6493 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6494 -- end;
6495 -- exception
6496 -- when Abort_Signal => Abort_Undefer;
6497 -- end;
6499 -- parm := P.param;
6500 -- parm := P.param;
6501 -- ...
6502 -- if not C then
6503 -- <triggered-statements>
6504 -- end if;
6505 -- end;
6507 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6508 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6509 -- as follows:
6511 -- declare
6512 -- P : parms := (parm, parm, parm);
6513 -- begin
6514 -- Call_Simple (acceptor-task, entry-index, P'Address);
6515 -- parm := P.param;
6516 -- parm := P.param;
6517 -- ...
6518 -- end;
6520 -- so the task at hand is to convert the latter expansion into the former
6522 -- If the trigger is a protected entry call, the select is implemented
6523 -- with Protected_Entry_Call:
6525 -- declare
6526 -- P : E1_Params := (param, param, param);
6527 -- Bnn : Communications_Block;
6529 -- begin
6530 -- declare
6532 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6534 -- procedure _clean is
6535 -- begin
6536 -- ...
6537 -- if Enqueued (Bnn) then
6538 -- Cancel_Protected_Entry_Call (Bnn);
6539 -- end if;
6540 -- ...
6541 -- end _clean;
6543 -- begin
6544 -- begin
6545 -- Protected_Entry_Call
6546 -- (po._object'Access, -- Object
6547 -- <entry index>, -- E
6548 -- P'Address, -- Uninterpreted_Data
6549 -- Asynchronous_Call, -- Mode
6550 -- Bnn); -- Block
6552 -- if Enqueued (Bnn) then
6553 -- <abortable-part>
6554 -- end if;
6555 -- at end
6556 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6557 -- end;
6558 -- exception
6559 -- when Abort_Signal => Abort_Undefer;
6560 -- end;
6562 -- if not Cancelled (Bnn) then
6563 -- <triggered-statements>
6564 -- end if;
6565 -- end;
6567 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6568 -- entry call:
6570 -- declare
6571 -- P : E1_Params := (param, param, param);
6572 -- Bnn : Communications_Block;
6574 -- begin
6575 -- Protected_Entry_Call
6576 -- (po._object'Access, -- Object
6577 -- <entry index>, -- E
6578 -- P'Address, -- Uninterpreted_Data
6579 -- Simple_Call, -- Mode
6580 -- Bnn); -- Block
6581 -- parm := P.param;
6582 -- parm := P.param;
6583 -- ...
6584 -- end;
6586 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6587 -- expanded into:
6589 -- declare
6590 -- B : Boolean := False;
6591 -- Bnn : Communication_Block;
6592 -- C : Ada.Tags.Prim_Op_Kind;
6593 -- D : System.Storage_Elements.Dummy_Communication_Block;
6594 -- K : Ada.Tags.Tagged_Kind :=
6595 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6596 -- P : Parameters := (Param1 .. ParamN);
6597 -- S : Integer;
6598 -- U : Boolean;
6600 -- begin
6601 -- if K = Ada.Tags.TK_Limited_Tagged
6602 -- or else K = Ada.Tags.TK_Tagged
6603 -- then
6604 -- <dispatching-call>;
6605 -- <triggering-statements>;
6607 -- else
6608 -- S :=
6609 -- Ada.Tags.Get_Offset_Index
6610 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6612 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6614 -- if C = POK_Protected_Entry then
6615 -- declare
6616 -- procedure _clean is
6617 -- begin
6618 -- if Enqueued (Bnn) then
6619 -- Cancel_Protected_Entry_Call (Bnn);
6620 -- end if;
6621 -- end _clean;
6623 -- begin
6624 -- begin
6625 -- _Disp_Asynchronous_Select
6626 -- (<object>, S, P'Address, D, B);
6627 -- Bnn := Communication_Block (D);
6629 -- Param1 := P.Param1;
6630 -- ...
6631 -- ParamN := P.ParamN;
6633 -- if Enqueued (Bnn) then
6634 -- <abortable-statements>
6635 -- end if;
6636 -- at end
6637 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6638 -- end;
6639 -- exception
6640 -- when Abort_Signal => Abort_Undefer;
6641 -- end;
6643 -- if not Cancelled (Bnn) then
6644 -- <triggering-statements>
6645 -- end if;
6647 -- elsif C = POK_Task_Entry then
6648 -- declare
6649 -- procedure _clean is
6650 -- begin
6651 -- Cancel_Task_Entry_Call (U);
6652 -- end _clean;
6654 -- begin
6655 -- Abort_Defer;
6657 -- _Disp_Asynchronous_Select
6658 -- (<object>, S, P'Address, D, B);
6659 -- Bnn := Communication_Bloc (D);
6661 -- Param1 := P.Param1;
6662 -- ...
6663 -- ParamN := P.ParamN;
6665 -- begin
6666 -- begin
6667 -- Abort_Undefer;
6668 -- <abortable-statements>
6669 -- at end
6670 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6671 -- end;
6672 -- exception
6673 -- when Abort_Signal => Abort_Undefer;
6674 -- end;
6676 -- if not U then
6677 -- <triggering-statements>
6678 -- end if;
6679 -- end;
6681 -- else
6682 -- <dispatching-call>;
6683 -- <triggering-statements>
6684 -- end if;
6685 -- end if;
6686 -- end;
6688 -- The job is to convert this to the asynchronous form
6690 -- If the trigger is a delay statement, it will have been expanded into
6691 -- a call to one of the GNARL delay procedures. This routine will convert
6692 -- this into a protected entry call on a delay object and then continue
6693 -- processing as for a protected entry call trigger. This requires
6694 -- declaring a Delay_Block object and adding a pointer to this object to
6695 -- the parameter list of the delay procedure to form the parameter list of
6696 -- the entry call. This object is used by the runtime to queue the delay
6697 -- request.
6699 -- For a description of the use of P and the assignments after the call,
6700 -- see Expand_N_Entry_Call_Statement.
6702 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6703 Loc : constant Source_Ptr := Sloc (N);
6704 Abrt : constant Node_Id := Abortable_Part (N);
6705 Trig : constant Node_Id := Triggering_Alternative (N);
6707 Abort_Block_Ent : Entity_Id;
6708 Abortable_Block : Node_Id;
6709 Actuals : List_Id;
6710 Astats : List_Id;
6711 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6712 Blk_Typ : Entity_Id;
6713 Call : Node_Id;
6714 Call_Ent : Entity_Id;
6715 Cancel_Param : Entity_Id;
6716 Cleanup_Block : Node_Id;
6717 Cleanup_Block_Ent : Entity_Id;
6718 Cleanup_Stmts : List_Id;
6719 Conc_Typ_Stmts : List_Id;
6720 Concval : Node_Id;
6721 Dblock_Ent : Entity_Id;
6722 Decl : Node_Id;
6723 Decls : List_Id;
6724 Ecall : Node_Id;
6725 Ename : Node_Id;
6726 Enqueue_Call : Node_Id;
6727 Formals : List_Id;
6728 Hdle : List_Id;
6729 Index : Node_Id;
6730 Lim_Typ_Stmts : List_Id;
6731 N_Orig : Node_Id;
6732 Obj : Entity_Id;
6733 Param : Node_Id;
6734 Params : List_Id;
6735 Pdef : Entity_Id;
6736 ProtE_Stmts : List_Id;
6737 ProtP_Stmts : List_Id;
6738 Stmt : Node_Id;
6739 Stmts : List_Id;
6740 TaskE_Stmts : List_Id;
6741 Tstats : List_Id;
6743 B : Entity_Id; -- Call status flag
6744 Bnn : Entity_Id; -- Communication block
6745 C : Entity_Id; -- Call kind
6746 K : Entity_Id; -- Tagged kind
6747 P : Entity_Id; -- Parameter block
6748 S : Entity_Id; -- Primitive operation slot
6749 T : Entity_Id; -- Additional status flag
6751 procedure Rewrite_Abortable_Part;
6752 -- If the trigger is a dispatching call, the expansion inserts multiple
6753 -- copies of the abortable part. This is both inefficient, and may lead
6754 -- to duplicate definitions that the back-end will reject, when the
6755 -- abortable part includes loops. This procedure rewrites the abortable
6756 -- part into a call to a generated procedure.
6758 ----------------------------
6759 -- Rewrite_Abortable_Part --
6760 ----------------------------
6762 procedure Rewrite_Abortable_Part is
6763 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6764 Decl : Node_Id;
6766 begin
6767 Decl :=
6768 Make_Subprogram_Body (Loc,
6769 Specification =>
6770 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6771 Declarations => New_List,
6772 Handled_Statement_Sequence =>
6773 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6774 Insert_Before (N, Decl);
6775 Analyze (Decl);
6777 -- Rewrite abortable part into a call to this procedure
6779 Astats :=
6780 New_List (
6781 Make_Procedure_Call_Statement (Loc,
6782 Name => New_Occurrence_Of (Proc, Loc)));
6783 end Rewrite_Abortable_Part;
6785 -- Start of processing for Expand_N_Asynchronous_Select
6787 begin
6788 -- Asynchronous select is not supported on restricted runtimes. Don't
6789 -- try to expand.
6791 if Restricted_Profile then
6792 return;
6793 end if;
6795 Process_Statements_For_Controlled_Objects (Trig);
6796 Process_Statements_For_Controlled_Objects (Abrt);
6798 Ecall := Triggering_Statement (Trig);
6800 Ensure_Statement_Present (Sloc (Ecall), Trig);
6802 -- Retrieve Astats and Tstats now because the finalization machinery may
6803 -- wrap them in blocks.
6805 Astats := Statements (Abrt);
6806 Tstats := Statements (Trig);
6808 -- The arguments in the call may require dynamic allocation, and the
6809 -- call statement may have been transformed into a block. The block
6810 -- may contain additional declarations for internal entities, and the
6811 -- original call is found by sequential search.
6813 if Nkind (Ecall) = N_Block_Statement then
6814 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6815 while Nkind (Ecall) not in
6816 N_Procedure_Call_Statement | N_Entry_Call_Statement
6817 loop
6818 Next (Ecall);
6819 end loop;
6820 end if;
6822 -- This is either a dispatching call or a delay statement used as a
6823 -- trigger which was expanded into a procedure call.
6825 if Nkind (Ecall) = N_Procedure_Call_Statement then
6826 if Ada_Version >= Ada_2005
6827 and then
6828 (No (Original_Node (Ecall))
6829 or else Nkind (Original_Node (Ecall)) not in N_Delay_Statement)
6830 then
6831 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6833 Rewrite_Abortable_Part;
6834 Decls := New_List;
6835 Stmts := New_List;
6837 -- Call status flag processing, generate:
6838 -- B : Boolean := False;
6840 B := Build_B (Loc, Decls);
6842 -- Communication block processing, generate:
6843 -- Bnn : Communication_Block;
6845 Bnn := Make_Temporary (Loc, 'B');
6846 Append_To (Decls,
6847 Make_Object_Declaration (Loc,
6848 Defining_Identifier => Bnn,
6849 Object_Definition =>
6850 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
6852 -- Call kind processing, generate:
6853 -- C : Ada.Tags.Prim_Op_Kind;
6855 C := Build_C (Loc, Decls);
6857 -- Tagged kind processing, generate:
6858 -- K : Ada.Tags.Tagged_Kind :=
6859 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6861 -- Dummy communication block, generate:
6862 -- D : Dummy_Communication_Block;
6864 Append_To (Decls,
6865 Make_Object_Declaration (Loc,
6866 Defining_Identifier =>
6867 Make_Defining_Identifier (Loc, Name_uD),
6868 Object_Definition =>
6869 New_Occurrence_Of
6870 (RTE (RE_Dummy_Communication_Block), Loc)));
6872 K := Build_K (Loc, Decls, Obj);
6874 -- Parameter block processing
6876 Blk_Typ := Build_Parameter_Block
6877 (Loc, Actuals, Formals, Decls);
6878 P := Parameter_Block_Pack
6879 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6881 -- Dispatch table slot processing, generate:
6882 -- S : Integer;
6884 S := Build_S (Loc, Decls);
6886 -- Additional status flag processing, generate:
6887 -- Tnn : Boolean;
6889 T := Make_Temporary (Loc, 'T');
6890 Append_To (Decls,
6891 Make_Object_Declaration (Loc,
6892 Defining_Identifier => T,
6893 Object_Definition =>
6894 New_Occurrence_Of (Standard_Boolean, Loc)));
6896 ------------------------------
6897 -- Protected entry handling --
6898 ------------------------------
6900 -- Generate:
6901 -- Param1 := P.Param1;
6902 -- ...
6903 -- ParamN := P.ParamN;
6905 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6907 -- Generate:
6908 -- Bnn := Communication_Block (D);
6910 Prepend_To (Cleanup_Stmts,
6911 Make_Assignment_Statement (Loc,
6912 Name => New_Occurrence_Of (Bnn, Loc),
6913 Expression =>
6914 Unchecked_Convert_To
6915 (RTE (RE_Communication_Block),
6916 Make_Identifier (Loc, Name_uD))));
6918 -- Generate:
6919 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6921 Prepend_To (Cleanup_Stmts,
6922 Make_Procedure_Call_Statement (Loc,
6923 Name =>
6924 New_Occurrence_Of
6925 (Find_Prim_Op
6926 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
6927 Loc),
6928 Parameter_Associations =>
6929 New_List (
6930 New_Copy_Tree (Obj), -- <object>
6931 New_Occurrence_Of (S, Loc), -- S
6932 Make_Attribute_Reference (Loc, -- P'Address
6933 Prefix => New_Occurrence_Of (P, Loc),
6934 Attribute_Name => Name_Address),
6935 Make_Identifier (Loc, Name_uD), -- D
6936 New_Occurrence_Of (B, Loc)))); -- B
6938 -- Generate:
6939 -- if Enqueued (Bnn) then
6940 -- <abortable-statements>
6941 -- end if;
6943 Append_To (Cleanup_Stmts,
6944 Make_Implicit_If_Statement (N,
6945 Condition =>
6946 Make_Function_Call (Loc,
6947 Name =>
6948 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
6949 Parameter_Associations =>
6950 New_List (New_Occurrence_Of (Bnn, Loc))),
6952 Then_Statements =>
6953 New_Copy_List_Tree (Astats)));
6955 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
6956 -- will then generate a _clean for the communication block Bnn.
6958 -- Generate:
6959 -- declare
6960 -- procedure _clean is
6961 -- begin
6962 -- if Enqueued (Bnn) then
6963 -- Cancel_Protected_Entry_Call (Bnn);
6964 -- end if;
6965 -- end _clean;
6966 -- begin
6967 -- Cleanup_Stmts
6968 -- at end
6969 -- _clean;
6970 -- end;
6972 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
6973 Cleanup_Block :=
6974 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
6976 -- Wrap the cleanup block in an exception handling block
6978 -- Generate:
6979 -- begin
6980 -- Cleanup_Block
6981 -- exception
6982 -- when Abort_Signal => Abort_Undefer;
6983 -- end;
6985 Abort_Block_Ent := Make_Temporary (Loc, 'A');
6986 ProtE_Stmts :=
6987 New_List (
6988 Make_Implicit_Label_Declaration (Loc,
6989 Defining_Identifier => Abort_Block_Ent),
6991 Build_Abort_Block
6992 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
6994 -- Generate:
6995 -- if not Cancelled (Bnn) then
6996 -- <triggering-statements>
6997 -- end if;
6999 Append_To (ProtE_Stmts,
7000 Make_Implicit_If_Statement (N,
7001 Condition =>
7002 Make_Op_Not (Loc,
7003 Right_Opnd =>
7004 Make_Function_Call (Loc,
7005 Name =>
7006 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7007 Parameter_Associations =>
7008 New_List (New_Occurrence_Of (Bnn, Loc)))),
7010 Then_Statements =>
7011 New_Copy_List_Tree (Tstats)));
7013 -------------------------
7014 -- Task entry handling --
7015 -------------------------
7017 -- Generate:
7018 -- Param1 := P.Param1;
7019 -- ...
7020 -- ParamN := P.ParamN;
7022 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7024 -- Generate:
7025 -- Bnn := Communication_Block (D);
7027 Append_To (TaskE_Stmts,
7028 Make_Assignment_Statement (Loc,
7029 Name =>
7030 New_Occurrence_Of (Bnn, Loc),
7031 Expression =>
7032 Unchecked_Convert_To
7033 (RTE (RE_Communication_Block),
7034 Make_Identifier (Loc, Name_uD))));
7036 -- Generate:
7037 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7039 Prepend_To (TaskE_Stmts,
7040 Make_Procedure_Call_Statement (Loc,
7041 Name =>
7042 New_Occurrence_Of (
7043 Find_Prim_Op (Etype (Etype (Obj)),
7044 Name_uDisp_Asynchronous_Select),
7045 Loc),
7047 Parameter_Associations => New_List (
7048 New_Copy_Tree (Obj), -- <object>
7049 New_Occurrence_Of (S, Loc), -- S
7050 Make_Attribute_Reference (Loc, -- P'Address
7051 Prefix => New_Occurrence_Of (P, Loc),
7052 Attribute_Name => Name_Address),
7053 Make_Identifier (Loc, Name_uD), -- D
7054 New_Occurrence_Of (B, Loc)))); -- B
7056 -- Generate:
7057 -- Abort_Defer;
7059 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7061 -- Generate:
7062 -- Abort_Undefer;
7063 -- <abortable-statements>
7065 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7067 Prepend_To
7068 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7070 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7071 -- will generate a _clean for the additional status flag.
7073 -- Generate:
7074 -- declare
7075 -- procedure _clean is
7076 -- begin
7077 -- Cancel_Task_Entry_Call (U);
7078 -- end _clean;
7079 -- begin
7080 -- Cleanup_Stmts
7081 -- at end
7082 -- _clean;
7083 -- end;
7085 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7086 Cleanup_Block :=
7087 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7089 -- Wrap the cleanup block in an exception handling block
7091 -- Generate:
7092 -- begin
7093 -- Cleanup_Block
7094 -- exception
7095 -- when Abort_Signal => Abort_Undefer;
7096 -- end;
7098 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7100 Append_To (TaskE_Stmts,
7101 Make_Implicit_Label_Declaration (Loc,
7102 Defining_Identifier => Abort_Block_Ent));
7104 Append_To (TaskE_Stmts,
7105 Build_Abort_Block
7106 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7108 -- Generate:
7109 -- if not T then
7110 -- <triggering-statements>
7111 -- end if;
7113 Append_To (TaskE_Stmts,
7114 Make_Implicit_If_Statement (N,
7115 Condition =>
7116 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7118 Then_Statements =>
7119 New_Copy_List_Tree (Tstats)));
7121 ----------------------------------
7122 -- Protected procedure handling --
7123 ----------------------------------
7125 -- Generate:
7126 -- <dispatching-call>;
7127 -- <triggering-statements>
7129 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7130 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7132 -- Generate:
7133 -- S := Ada.Tags.Get_Offset_Index
7134 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7136 Conc_Typ_Stmts :=
7137 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7139 -- Generate:
7140 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7142 Append_To (Conc_Typ_Stmts,
7143 Make_Procedure_Call_Statement (Loc,
7144 Name =>
7145 New_Occurrence_Of
7146 (Find_Prim_Op (Etype (Etype (Obj)),
7147 Name_uDisp_Get_Prim_Op_Kind),
7148 Loc),
7149 Parameter_Associations =>
7150 New_List (
7151 New_Copy_Tree (Obj),
7152 New_Occurrence_Of (S, Loc),
7153 New_Occurrence_Of (C, Loc))));
7155 -- Generate:
7156 -- if C = POK_Procedure_Entry then
7157 -- ProtE_Stmts
7158 -- elsif C = POK_Task_Entry then
7159 -- TaskE_Stmts
7160 -- else
7161 -- ProtP_Stmts
7162 -- end if;
7164 Append_To (Conc_Typ_Stmts,
7165 Make_Implicit_If_Statement (N,
7166 Condition =>
7167 Make_Op_Eq (Loc,
7168 Left_Opnd =>
7169 New_Occurrence_Of (C, Loc),
7170 Right_Opnd =>
7171 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7173 Then_Statements =>
7174 ProtE_Stmts,
7176 Elsif_Parts =>
7177 New_List (
7178 Make_Elsif_Part (Loc,
7179 Condition =>
7180 Make_Op_Eq (Loc,
7181 Left_Opnd =>
7182 New_Occurrence_Of (C, Loc),
7183 Right_Opnd =>
7184 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7186 Then_Statements =>
7187 TaskE_Stmts)),
7189 Else_Statements =>
7190 ProtP_Stmts));
7192 -- Generate:
7193 -- <dispatching-call>;
7194 -- <triggering-statements>
7196 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7197 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7199 -- Generate:
7200 -- if K = Ada.Tags.TK_Limited_Tagged
7201 -- or else K = Ada.Tags.TK_Tagged
7202 -- then
7203 -- Lim_Typ_Stmts
7204 -- else
7205 -- Conc_Typ_Stmts
7206 -- end if;
7208 Append_To (Stmts,
7209 Make_Implicit_If_Statement (N,
7210 Condition => Build_Dispatching_Tag_Check (K, N),
7211 Then_Statements => Lim_Typ_Stmts,
7212 Else_Statements => Conc_Typ_Stmts));
7214 Rewrite (N,
7215 Make_Block_Statement (Loc,
7216 Declarations =>
7217 Decls,
7218 Handled_Statement_Sequence =>
7219 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7221 Analyze (N);
7222 return;
7224 -- Delay triggering statement processing
7226 else
7227 -- Add a Delay_Block object to the parameter list of the delay
7228 -- procedure to form the parameter list of the Wait entry call.
7230 Dblock_Ent := Make_Temporary (Loc, 'D');
7232 Pdef := Entity (Name (Ecall));
7234 if Is_RTE (Pdef, RO_CA_Delay_For) then
7235 Enqueue_Call :=
7236 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7238 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7239 Enqueue_Call :=
7240 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7242 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7243 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7244 end if;
7246 Append_To (Parameter_Associations (Ecall),
7247 Make_Attribute_Reference (Loc,
7248 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7249 Attribute_Name => Name_Unchecked_Access));
7251 -- Create the inner block to protect the abortable part
7253 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7255 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7257 Abortable_Block :=
7258 Make_Block_Statement (Loc,
7259 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7260 Handled_Statement_Sequence =>
7261 Make_Handled_Sequence_Of_Statements (Loc,
7262 Statements => Astats),
7263 Has_Created_Identifier => True,
7264 Is_Asynchronous_Call_Block => True);
7266 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7268 Rewrite (Ecall,
7269 Make_Implicit_If_Statement (N,
7270 Condition =>
7271 Make_Function_Call (Loc,
7272 Name => Enqueue_Call,
7273 Parameter_Associations => Parameter_Associations (Ecall)),
7274 Then_Statements =>
7275 New_List (Make_Block_Statement (Loc,
7276 Handled_Statement_Sequence =>
7277 Make_Handled_Sequence_Of_Statements (Loc,
7278 Statements => New_List (
7279 Make_Implicit_Label_Declaration (Loc,
7280 Defining_Identifier => Blk_Ent,
7281 Label_Construct => Abortable_Block),
7282 Abortable_Block),
7283 Exception_Handlers => Hdle)))));
7285 Stmts := New_List (Ecall);
7287 -- Construct statement sequence for new block
7289 Append_To (Stmts,
7290 Make_Implicit_If_Statement (N,
7291 Condition =>
7292 Make_Function_Call (Loc,
7293 Name => New_Occurrence_Of (
7294 RTE (RE_Timed_Out), Loc),
7295 Parameter_Associations => New_List (
7296 Make_Attribute_Reference (Loc,
7297 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7298 Attribute_Name => Name_Unchecked_Access))),
7299 Then_Statements => Tstats));
7301 -- The result is the new block
7303 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7305 Rewrite (N,
7306 Make_Block_Statement (Loc,
7307 Declarations => New_List (
7308 Make_Object_Declaration (Loc,
7309 Defining_Identifier => Dblock_Ent,
7310 Aliased_Present => True,
7311 Object_Definition =>
7312 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7314 Handled_Statement_Sequence =>
7315 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7317 Analyze (N);
7318 return;
7319 end if;
7321 else
7322 N_Orig := N;
7323 end if;
7325 Extract_Entry (Ecall, Concval, Ename, Index);
7326 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7328 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7329 Decls := Declarations (Ecall);
7331 if Is_Protected_Type (Etype (Concval)) then
7333 -- Get the declarations of the block expanded from the entry call
7335 Decl := First (Decls);
7336 while Present (Decl)
7337 and then (Nkind (Decl) /= N_Object_Declaration
7338 or else not Is_RTE (Etype (Object_Definition (Decl)),
7339 RE_Communication_Block))
7340 loop
7341 Next (Decl);
7342 end loop;
7344 pragma Assert (Present (Decl));
7345 Cancel_Param := Defining_Identifier (Decl);
7347 -- Change the mode of the Protected_Entry_Call call
7349 -- Protected_Entry_Call (
7350 -- Object => po._object'Access,
7351 -- E => <entry index>;
7352 -- Uninterpreted_Data => P'Address;
7353 -- Mode => Asynchronous_Call;
7354 -- Block => Bnn);
7356 -- Skip assignments to temporaries created for in-out parameters
7358 -- This makes unwarranted assumptions about the shape of the expanded
7359 -- tree for the call, and should be cleaned up ???
7361 Stmt := First (Stmts);
7362 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7363 Next (Stmt);
7364 end loop;
7366 Call := Stmt;
7368 Param := First (Parameter_Associations (Call));
7369 while Present (Param)
7370 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7371 loop
7372 Next (Param);
7373 end loop;
7375 pragma Assert (Present (Param));
7376 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7377 Analyze (Param);
7379 -- Append an if statement to execute the abortable part
7381 -- Generate:
7382 -- if Enqueued (Bnn) then
7384 Append_To (Stmts,
7385 Make_Implicit_If_Statement (N,
7386 Condition =>
7387 Make_Function_Call (Loc,
7388 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7389 Parameter_Associations => New_List (
7390 New_Occurrence_Of (Cancel_Param, Loc))),
7391 Then_Statements => Astats));
7393 Abortable_Block :=
7394 Make_Block_Statement (Loc,
7395 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7396 Handled_Statement_Sequence =>
7397 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7398 Has_Created_Identifier => True,
7399 Is_Asynchronous_Call_Block => True);
7401 Stmts := New_List (
7402 Make_Block_Statement (Loc,
7403 Handled_Statement_Sequence =>
7404 Make_Handled_Sequence_Of_Statements (Loc,
7405 Statements => New_List (
7406 Make_Implicit_Label_Declaration (Loc,
7407 Defining_Identifier => Blk_Ent,
7408 Label_Construct => Abortable_Block),
7409 Abortable_Block),
7411 -- exception
7413 Exception_Handlers => New_List (
7414 Make_Implicit_Exception_Handler (Loc,
7416 -- when Abort_Signal =>
7417 -- null;
7419 Exception_Choices =>
7420 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7421 Statements => New_List (Make_Null_Statement (Loc)))))),
7423 -- if not Cancelled (Bnn) then
7424 -- triggered statements
7425 -- end if;
7427 Make_Implicit_If_Statement (N,
7428 Condition => Make_Op_Not (Loc,
7429 Right_Opnd =>
7430 Make_Function_Call (Loc,
7431 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7432 Parameter_Associations => New_List (
7433 New_Occurrence_Of (Cancel_Param, Loc)))),
7434 Then_Statements => Tstats));
7436 -- Asynchronous task entry call
7438 else
7439 if No (Decls) then
7440 Decls := New_List;
7441 end if;
7443 B := Make_Defining_Identifier (Loc, Name_uB);
7445 -- Insert declaration of B in declarations of existing block
7447 Prepend_To (Decls,
7448 Make_Object_Declaration (Loc,
7449 Defining_Identifier => B,
7450 Object_Definition =>
7451 New_Occurrence_Of (Standard_Boolean, Loc)));
7453 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7455 -- Insert the declaration of C in the declarations of the existing
7456 -- block. The variable is initialized to something (True or False,
7457 -- does not matter) to prevent CodePeer from complaining about a
7458 -- possible read of an uninitialized variable.
7460 Prepend_To (Decls,
7461 Make_Object_Declaration (Loc,
7462 Defining_Identifier => Cancel_Param,
7463 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7464 Expression => New_Occurrence_Of (Standard_False, Loc),
7465 Has_Init_Expression => True));
7467 -- Remove and save the call to Call_Simple
7469 Stmt := First (Stmts);
7471 -- Skip assignments to temporaries created for in-out parameters.
7472 -- This makes unwarranted assumptions about the shape of the expanded
7473 -- tree for the call, and should be cleaned up ???
7475 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7476 Next (Stmt);
7477 end loop;
7479 Call := Stmt;
7481 -- Create the inner block to protect the abortable part
7483 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7485 if Abort_Allowed then
7486 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7487 end if;
7489 Abortable_Block :=
7490 Make_Block_Statement (Loc,
7491 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7492 Handled_Statement_Sequence =>
7493 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7494 Has_Created_Identifier => True,
7495 Is_Asynchronous_Call_Block => True);
7497 Insert_After (Call,
7498 Make_Block_Statement (Loc,
7499 Handled_Statement_Sequence =>
7500 Make_Handled_Sequence_Of_Statements (Loc,
7501 Statements => New_List (
7502 Make_Implicit_Label_Declaration (Loc,
7503 Defining_Identifier => Blk_Ent,
7504 Label_Construct => Abortable_Block),
7505 Abortable_Block),
7506 Exception_Handlers => Hdle)));
7508 -- Create new call statement
7510 Params := Parameter_Associations (Call);
7512 Append_To (Params,
7513 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7514 Append_To (Params, New_Occurrence_Of (B, Loc));
7516 Rewrite (Call,
7517 Make_Procedure_Call_Statement (Loc,
7518 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7519 Parameter_Associations => Params));
7521 -- Construct statement sequence for new block
7523 Append_To (Stmts,
7524 Make_Implicit_If_Statement (N,
7525 Condition =>
7526 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7527 Then_Statements => Tstats));
7529 -- Protected the call against abort
7531 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7532 end if;
7534 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7536 -- The result is the new block
7538 Rewrite (N_Orig,
7539 Make_Block_Statement (Loc,
7540 Declarations => Decls,
7541 Handled_Statement_Sequence =>
7542 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7544 Analyze (N_Orig);
7545 end Expand_N_Asynchronous_Select;
7547 -------------------------------------
7548 -- Expand_N_Conditional_Entry_Call --
7549 -------------------------------------
7551 -- The conditional task entry call is converted to a call to
7552 -- Task_Entry_Call:
7554 -- declare
7555 -- B : Boolean;
7556 -- P : parms := (parm, parm, parm);
7558 -- begin
7559 -- Task_Entry_Call
7560 -- (<acceptor-task>, -- Acceptor
7561 -- <entry-index>, -- E
7562 -- P'Address, -- Uninterpreted_Data
7563 -- Conditional_Call, -- Mode
7564 -- B); -- Rendezvous_Successful
7565 -- parm := P.param;
7566 -- parm := P.param;
7567 -- ...
7568 -- if B then
7569 -- normal-statements
7570 -- else
7571 -- else-statements
7572 -- end if;
7573 -- end;
7575 -- For a description of the use of P and the assignments after the call,
7576 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7577 -- conditional entry call has already been expanded (by the Expand_N_Entry
7578 -- _Call_Statement procedure) as follows:
7580 -- declare
7581 -- P : parms := (parm, parm, parm);
7582 -- begin
7583 -- ... info for in-out parameters
7584 -- Call_Simple (acceptor-task, entry-index, P'Address);
7585 -- parm := P.param;
7586 -- parm := P.param;
7587 -- ...
7588 -- end;
7590 -- so the task at hand is to convert the latter expansion into the former
7592 -- The conditional protected entry call is converted to a call to
7593 -- Protected_Entry_Call:
7595 -- declare
7596 -- P : parms := (parm, parm, parm);
7597 -- Bnn : Communications_Block;
7599 -- begin
7600 -- Protected_Entry_Call
7601 -- (po._object'Access, -- Object
7602 -- <entry index>, -- E
7603 -- P'Address, -- Uninterpreted_Data
7604 -- Conditional_Call, -- Mode
7605 -- Bnn); -- Block
7606 -- parm := P.param;
7607 -- parm := P.param;
7608 -- ...
7609 -- if Cancelled (Bnn) then
7610 -- else-statements
7611 -- else
7612 -- normal-statements
7613 -- end if;
7614 -- end;
7616 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7617 -- into:
7619 -- declare
7620 -- B : Boolean := False;
7621 -- C : Ada.Tags.Prim_Op_Kind;
7622 -- K : Ada.Tags.Tagged_Kind :=
7623 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7624 -- P : Parameters := (Param1 .. ParamN);
7625 -- S : Integer;
7627 -- begin
7628 -- if K = Ada.Tags.TK_Limited_Tagged
7629 -- or else K = Ada.Tags.TK_Tagged
7630 -- then
7631 -- <dispatching-call>;
7632 -- -- <triggering-statements> (code factorized after if-stmt)
7634 -- else
7635 -- S :=
7636 -- Ada.Tags.Get_Offset_Index
7637 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7639 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7641 -- if C = POK_Protected_Entry
7642 -- or else C = POK_Task_Entry
7643 -- then
7644 -- Param1 := P.Param1;
7645 -- ...
7646 -- ParamN := P.ParamN;
7647 -- end if;
7649 -- if B then
7650 -- if C = POK_Procedure
7651 -- or else C = POK_Protected_Procedure
7652 -- or else C = POK_Task_Procedure
7653 -- then
7654 -- <dispatching-call>;
7655 -- end if;
7657 -- -- <triggering-statements> (code factorized after if-stmt)
7658 -- else
7659 -- <else-statements>
7660 -- goto L0; -- skip triggering statements
7661 -- end if;
7662 -- end if;
7663 -- <triggering-statements>
7664 -- L0:
7665 -- end;
7667 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7668 Loc : constant Source_Ptr := Sloc (N);
7669 Alt : constant Node_Id := Entry_Call_Alternative (N);
7670 Blk : Node_Id := Entry_Call_Statement (Alt);
7672 Actuals : List_Id;
7673 Blk_Typ : Entity_Id;
7674 Call : Node_Id;
7675 Call_Ent : Entity_Id;
7676 Conc_Typ_Stmts : List_Id;
7677 Decl : Node_Id;
7678 Decls : List_Id;
7679 Formals : List_Id;
7680 Label : Node_Id;
7681 Label_Id : Entity_Id := Empty;
7682 Lim_Typ_Stmts : List_Id;
7683 N_Stats : List_Id;
7684 Obj : Entity_Id;
7685 Param : Node_Id;
7686 Params : List_Id;
7687 Stmt : Node_Id;
7688 Stmts : List_Id;
7689 Transient_Blk : Node_Id;
7690 Unpack : List_Id;
7692 B : Entity_Id; -- Call status flag
7693 C : Entity_Id; -- Call kind
7694 K : Entity_Id; -- Tagged kind
7695 P : Entity_Id; -- Parameter block
7696 S : Entity_Id; -- Primitive operation slot
7698 begin
7699 Process_Statements_For_Controlled_Objects (N);
7701 if Ada_Version >= Ada_2005
7702 and then Nkind (Blk) = N_Procedure_Call_Statement
7703 then
7704 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7706 Decls := New_List;
7707 Stmts := New_List;
7709 -- Call status flag processing, generate:
7710 -- B : Boolean := False;
7712 B := Build_B (Loc, Decls);
7714 -- Call kind processing, generate:
7715 -- C : Ada.Tags.Prim_Op_Kind;
7717 C := Build_C (Loc, Decls);
7719 -- Tagged kind processing, generate:
7720 -- K : Ada.Tags.Tagged_Kind :=
7721 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7723 K := Build_K (Loc, Decls, Obj);
7725 -- Parameter block processing
7727 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7728 P := Parameter_Block_Pack
7729 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7731 -- Dispatch table slot processing, generate:
7732 -- S : Integer;
7734 S := Build_S (Loc, Decls);
7736 -- Generate:
7737 -- S := Ada.Tags.Get_Offset_Index
7738 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7740 Conc_Typ_Stmts :=
7741 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7743 -- Generate:
7744 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7746 Append_To (Conc_Typ_Stmts,
7747 Make_Procedure_Call_Statement (Loc,
7748 Name =>
7749 New_Occurrence_Of (
7750 Find_Prim_Op (Etype (Etype (Obj)),
7751 Name_uDisp_Conditional_Select),
7752 Loc),
7753 Parameter_Associations =>
7754 New_List (
7755 New_Copy_Tree (Obj), -- <object>
7756 New_Occurrence_Of (S, Loc), -- S
7757 Make_Attribute_Reference (Loc, -- P'Address
7758 Prefix => New_Occurrence_Of (P, Loc),
7759 Attribute_Name => Name_Address),
7760 New_Occurrence_Of (C, Loc), -- C
7761 New_Occurrence_Of (B, Loc)))); -- B
7763 -- Generate:
7764 -- if C = POK_Protected_Entry
7765 -- or else C = POK_Task_Entry
7766 -- then
7767 -- Param1 := P.Param1;
7768 -- ...
7769 -- ParamN := P.ParamN;
7770 -- end if;
7772 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7774 -- Generate the if statement only when the packed parameters need
7775 -- explicit assignments to their corresponding actuals.
7777 if Present (Unpack) then
7778 Append_To (Conc_Typ_Stmts,
7779 Make_Implicit_If_Statement (N,
7780 Condition =>
7781 Make_Or_Else (Loc,
7782 Left_Opnd =>
7783 Make_Op_Eq (Loc,
7784 Left_Opnd =>
7785 New_Occurrence_Of (C, Loc),
7786 Right_Opnd =>
7787 New_Occurrence_Of (RTE (
7788 RE_POK_Protected_Entry), Loc)),
7790 Right_Opnd =>
7791 Make_Op_Eq (Loc,
7792 Left_Opnd =>
7793 New_Occurrence_Of (C, Loc),
7794 Right_Opnd =>
7795 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
7797 Then_Statements => Unpack));
7798 end if;
7800 -- Generate:
7801 -- if B then
7802 -- if C = POK_Procedure
7803 -- or else C = POK_Protected_Procedure
7804 -- or else C = POK_Task_Procedure
7805 -- then
7806 -- <dispatching-call>
7807 -- end if;
7808 -- -- <triggering-stataments> (code factorized after if-stmt)
7809 -- else
7810 -- <else-statements>
7811 -- goto L0; -- skip triggering statements
7812 -- end if;
7814 N_Stats := New_List;
7816 Prepend_To (N_Stats,
7817 Make_Implicit_If_Statement (N,
7818 Condition =>
7819 Make_Or_Else (Loc,
7820 Left_Opnd =>
7821 Make_Op_Eq (Loc,
7822 Left_Opnd =>
7823 New_Occurrence_Of (C, Loc),
7824 Right_Opnd =>
7825 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
7827 Right_Opnd =>
7828 Make_Or_Else (Loc,
7829 Left_Opnd =>
7830 Make_Op_Eq (Loc,
7831 Left_Opnd =>
7832 New_Occurrence_Of (C, Loc),
7833 Right_Opnd =>
7834 New_Occurrence_Of (RTE (
7835 RE_POK_Protected_Procedure), Loc)),
7837 Right_Opnd =>
7838 Make_Op_Eq (Loc,
7839 Left_Opnd =>
7840 New_Occurrence_Of (C, Loc),
7841 Right_Opnd =>
7842 New_Occurrence_Of (RTE (
7843 RE_POK_Task_Procedure), Loc)))),
7845 Then_Statements =>
7846 New_List (Blk)));
7848 Label_Id := Make_Identifier (Loc, New_External_Name ('L', 0));
7849 Set_Entity (Label_Id,
7850 Make_Defining_Identifier (Loc, Chars (Label_Id)));
7852 Append_To (Else_Statements (N),
7853 Make_Goto_Statement (Loc,
7854 Name => New_Occurrence_Of (Entity (Label_Id), Loc)));
7856 Append_To (Conc_Typ_Stmts,
7857 Make_Implicit_If_Statement (N,
7858 Condition => New_Occurrence_Of (B, Loc),
7859 Then_Statements => N_Stats,
7860 Else_Statements => Else_Statements (N)));
7862 -- Generate:
7863 -- <dispatching-call>;
7864 -- -- <triggering-statements> (code factorized after if-stmt)
7866 Lim_Typ_Stmts := New_List (New_Copy_Tree (Blk));
7868 -- Generate:
7869 -- if K = Ada.Tags.TK_Limited_Tagged
7870 -- or else K = Ada.Tags.TK_Tagged
7871 -- then
7872 -- Lim_Typ_Stmts
7873 -- else
7874 -- Conc_Typ_Stmts
7875 -- end if;
7877 Append_To (Stmts,
7878 Make_Implicit_If_Statement (N,
7879 Condition => Build_Dispatching_Tag_Check (K, N),
7880 Then_Statements => Lim_Typ_Stmts,
7881 Else_Statements => Conc_Typ_Stmts));
7883 Label := Make_Label (Loc, Label_Id);
7884 Append_To (Decls,
7885 Make_Implicit_Label_Declaration (Loc,
7886 Defining_Identifier => Entity (Label_Id),
7887 Label_Construct => Label));
7889 Append_List_To (Stmts, Statements (Alt)); -- triggering-statements
7890 Append_To (Stmts, Label);
7892 Rewrite (N,
7893 Make_Block_Statement (Loc,
7894 Declarations =>
7895 Decls,
7896 Handled_Statement_Sequence =>
7897 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7899 -- As described above, the entry alternative is transformed into a
7900 -- block that contains the gnulli call, and possibly assignment
7901 -- statements for in-out parameters. The gnulli call may itself be
7902 -- rewritten into a transient block if some unconstrained parameters
7903 -- require it. We need to retrieve the call to complete its parameter
7904 -- list.
7906 else
7907 Transient_Blk :=
7908 First (Statements (Handled_Statement_Sequence (Blk)));
7910 if Present (Transient_Blk)
7911 and then Nkind (Transient_Blk) = N_Block_Statement
7912 then
7913 Blk := Transient_Blk;
7914 end if;
7916 Stmts := Statements (Handled_Statement_Sequence (Blk));
7917 Stmt := First (Stmts);
7918 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7919 Next (Stmt);
7920 end loop;
7922 Call := Stmt;
7923 Params := Parameter_Associations (Call);
7925 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
7927 -- Substitute Conditional_Entry_Call for Simple_Call parameter
7929 Param := First (Params);
7930 while Present (Param)
7931 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7932 loop
7933 Next (Param);
7934 end loop;
7936 pragma Assert (Present (Param));
7937 Rewrite (Param,
7938 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
7940 Analyze (Param);
7942 -- Find the Communication_Block parameter for the call to the
7943 -- Cancelled function.
7945 Decl := First (Declarations (Blk));
7946 while Present (Decl)
7947 and then not Is_RTE (Etype (Object_Definition (Decl)),
7948 RE_Communication_Block)
7949 loop
7950 Next (Decl);
7951 end loop;
7953 -- Add an if statement to execute the else part if the call
7954 -- does not succeed (as indicated by the Cancelled predicate).
7956 Append_To (Stmts,
7957 Make_Implicit_If_Statement (N,
7958 Condition => Make_Function_Call (Loc,
7959 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7960 Parameter_Associations => New_List (
7961 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
7962 Then_Statements => Else_Statements (N),
7963 Else_Statements => Statements (Alt)));
7965 else
7966 B := Make_Defining_Identifier (Loc, Name_uB);
7968 -- Insert declaration of B in declarations of existing block
7970 if No (Declarations (Blk)) then
7971 Set_Declarations (Blk, New_List);
7972 end if;
7974 Prepend_To (Declarations (Blk),
7975 Make_Object_Declaration (Loc,
7976 Defining_Identifier => B,
7977 Object_Definition =>
7978 New_Occurrence_Of (Standard_Boolean, Loc)));
7980 -- Create new call statement
7982 Append_To (Params,
7983 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
7984 Append_To (Params, New_Occurrence_Of (B, Loc));
7986 Rewrite (Call,
7987 Make_Procedure_Call_Statement (Loc,
7988 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7989 Parameter_Associations => Params));
7991 -- Construct statement sequence for new block
7993 Append_To (Stmts,
7994 Make_Implicit_If_Statement (N,
7995 Condition => New_Occurrence_Of (B, Loc),
7996 Then_Statements => Statements (Alt),
7997 Else_Statements => Else_Statements (N)));
7998 end if;
8000 -- The result is the new block
8002 Rewrite (N,
8003 Make_Block_Statement (Loc,
8004 Declarations => Declarations (Blk),
8005 Handled_Statement_Sequence =>
8006 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8007 end if;
8009 Analyze (N);
8011 Reset_Scopes_To (N, Entity (Identifier (N)));
8012 end Expand_N_Conditional_Entry_Call;
8014 ---------------------------------------
8015 -- Expand_N_Delay_Relative_Statement --
8016 ---------------------------------------
8018 -- Delay statement is implemented as a procedure call to Delay_For
8019 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8020 -- simple delays imposed by the use of Protected Objects.
8022 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8023 Loc : constant Source_Ptr := Sloc (N);
8024 Proc : Entity_Id;
8026 begin
8027 -- Try to use Ada.Calendar.Delays.Delay_For if available.
8029 if RTE_Available (RO_CA_Delay_For) then
8030 Proc := RTE (RO_CA_Delay_For);
8032 -- Otherwise, use System.Relative_Delays.Delay_For and emit an error
8033 -- message if not available. This is the implementation used on
8034 -- restricted platforms when Ada.Calendar is not available.
8036 else
8037 Proc := RTE (RO_RD_Delay_For);
8038 end if;
8040 Rewrite (N,
8041 Make_Procedure_Call_Statement (Loc,
8042 Name => New_Occurrence_Of (Proc, Loc),
8043 Parameter_Associations => New_List (Expression (N))));
8044 Analyze (N);
8045 end Expand_N_Delay_Relative_Statement;
8047 ------------------------------------
8048 -- Expand_N_Delay_Until_Statement --
8049 ------------------------------------
8051 -- Delay Until statement is implemented as a procedure call to
8052 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8054 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8055 Loc : constant Source_Ptr := Sloc (N);
8056 Typ : Entity_Id;
8058 begin
8059 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8060 Typ := RTE (RO_CA_Delay_Until);
8061 else
8062 Typ := RTE (RO_RT_Delay_Until);
8063 end if;
8065 Rewrite (N,
8066 Make_Procedure_Call_Statement (Loc,
8067 Name => New_Occurrence_Of (Typ, Loc),
8068 Parameter_Associations => New_List (Expression (N))));
8070 Analyze (N);
8071 end Expand_N_Delay_Until_Statement;
8073 -------------------------
8074 -- Expand_N_Entry_Body --
8075 -------------------------
8077 procedure Expand_N_Entry_Body (N : Node_Id) is
8078 begin
8079 -- Associate discriminals with the next protected operation body to be
8080 -- expanded.
8082 if Present (Next_Protected_Operation (N)) then
8083 Set_Discriminals (Parent (Current_Scope));
8084 end if;
8085 end Expand_N_Entry_Body;
8087 -----------------------------------
8088 -- Expand_N_Entry_Call_Statement --
8089 -----------------------------------
8091 -- An entry call is expanded into GNARLI calls to implement a simple entry
8092 -- call (see Build_Simple_Entry_Call).
8094 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8095 Concval : Node_Id;
8096 Ename : Node_Id;
8097 Index : Node_Id;
8099 begin
8100 if No_Run_Time_Mode then
8101 Error_Msg_CRT ("entry call", N);
8102 return;
8103 end if;
8105 -- If this entry call is part of an asynchronous select, don't expand it
8106 -- here; it will be expanded with the select statement. Don't expand
8107 -- timed entry calls either, as they are translated into asynchronous
8108 -- entry calls.
8110 -- ??? This whole approach is questionable; it may be better to go back
8111 -- to allowing the expansion to take place and then attempting to fix it
8112 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8113 -- whether the expanded call is on a task or protected entry.
8115 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8116 or else N /= Triggering_Statement (Parent (N)))
8117 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8118 or else N /= Entry_Call_Statement (Parent (N))
8119 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8120 then
8121 Extract_Entry (N, Concval, Ename, Index);
8122 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8123 end if;
8124 end Expand_N_Entry_Call_Statement;
8126 --------------------------------
8127 -- Expand_N_Entry_Declaration --
8128 --------------------------------
8130 -- If there are parameters, then first, each of the formals is marked by
8131 -- setting Is_Entry_Formal. Next a record type is built which is used to
8132 -- hold the parameter values. The name of this record type is entryP where
8133 -- entry is the name of the entry, with an additional corresponding access
8134 -- type called entryPA. The record type has matching components for each
8135 -- formal (the component names are the same as the formal names). For
8136 -- elementary types, the component type matches the formal type. For
8137 -- composite types, an access type is declared (with the name formalA)
8138 -- which designates the formal type, and the type of the component is this
8139 -- access type. Finally the Entry_Component of each formal is set to
8140 -- reference the corresponding record component.
8142 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8143 Loc : constant Source_Ptr := Sloc (N);
8144 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8145 Components : List_Id;
8146 Formal : Node_Id;
8147 Ftype : Entity_Id;
8148 Last_Decl : Node_Id;
8149 Component : Entity_Id;
8150 Ctype : Entity_Id;
8151 Decl : Node_Id;
8152 Rec_Ent : Entity_Id;
8153 Acc_Ent : Entity_Id;
8155 begin
8156 Formal := First_Formal (Entry_Ent);
8157 Last_Decl := N;
8159 -- Most processing is done only if parameters are present
8161 if Present (Formal) then
8162 Components := New_List;
8164 -- Loop through formals
8166 while Present (Formal) loop
8167 Set_Is_Entry_Formal (Formal);
8168 Component :=
8169 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8170 Set_Entry_Component (Formal, Component);
8171 Set_Entry_Formal (Component, Formal);
8172 Ftype := Etype (Formal);
8174 -- Declare new access type and then append
8176 Ctype := Make_Temporary (Loc, 'A');
8177 Set_Is_Param_Block_Component_Type (Ctype);
8179 Decl :=
8180 Make_Full_Type_Declaration (Loc,
8181 Defining_Identifier => Ctype,
8182 Type_Definition =>
8183 Make_Access_To_Object_Definition (Loc,
8184 All_Present => True,
8185 Constant_Present => Ekind (Formal) = E_In_Parameter,
8186 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8188 Insert_After (Last_Decl, Decl);
8189 Last_Decl := Decl;
8191 Append_To (Components,
8192 Make_Component_Declaration (Loc,
8193 Defining_Identifier => Component,
8194 Component_Definition =>
8195 Make_Component_Definition (Loc,
8196 Aliased_Present => False,
8197 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8199 Next_Formal_With_Extras (Formal);
8200 end loop;
8202 -- Create the Entry_Parameter_Record declaration
8204 Rec_Ent := Make_Temporary (Loc, 'P');
8206 Decl :=
8207 Make_Full_Type_Declaration (Loc,
8208 Defining_Identifier => Rec_Ent,
8209 Type_Definition =>
8210 Make_Record_Definition (Loc,
8211 Component_List =>
8212 Make_Component_List (Loc,
8213 Component_Items => Components)));
8215 Insert_After (Last_Decl, Decl);
8216 Last_Decl := Decl;
8218 -- Construct and link in the corresponding access type
8220 Acc_Ent := Make_Temporary (Loc, 'A');
8222 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8224 Decl :=
8225 Make_Full_Type_Declaration (Loc,
8226 Defining_Identifier => Acc_Ent,
8227 Type_Definition =>
8228 Make_Access_To_Object_Definition (Loc,
8229 All_Present => True,
8230 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8232 Insert_After (Last_Decl, Decl);
8233 end if;
8234 end Expand_N_Entry_Declaration;
8236 -----------------------------
8237 -- Expand_N_Protected_Body --
8238 -----------------------------
8240 -- Protected bodies are expanded to the completion of the subprograms
8241 -- created for the corresponding protected type. These are a protected and
8242 -- unprotected version of each protected subprogram in the object, a
8243 -- function to calculate each entry barrier, and a procedure to execute the
8244 -- sequence of statements of each protected entry body. For example, for
8245 -- protected type ptype:
8247 -- function entB
8248 -- (O : System.Address;
8249 -- E : Protected_Entry_Index)
8250 -- return Boolean
8251 -- is
8252 -- <discriminant renamings>
8253 -- <private object renamings>
8254 -- begin
8255 -- return <barrier expression>;
8256 -- end entB;
8258 -- procedure pprocN (_object : in out poV;...) is
8259 -- <discriminant renamings>
8260 -- <private object renamings>
8261 -- begin
8262 -- <sequence of statements>
8263 -- end pprocN;
8265 -- procedure pprocP (_object : in out poV;...) is
8266 -- procedure _clean is
8267 -- Pn : Boolean;
8268 -- begin
8269 -- ptypeS (_object, Pn);
8270 -- Unlock (_object._object'Access);
8271 -- Abort_Undefer.all;
8272 -- end _clean;
8274 -- begin
8275 -- Abort_Defer.all;
8276 -- Lock (_object._object'Access);
8277 -- pprocN (_object;...);
8278 -- at end
8279 -- _clean;
8280 -- end pproc;
8282 -- function pfuncN (_object : poV;...) return Return_Type is
8283 -- <discriminant renamings>
8284 -- <private object renamings>
8285 -- begin
8286 -- <sequence of statements>
8287 -- end pfuncN;
8289 -- function pfuncP (_object : poV) return Return_Type is
8290 -- procedure _clean is
8291 -- begin
8292 -- Unlock (_object._object'Access);
8293 -- Abort_Undefer.all;
8294 -- end _clean;
8296 -- begin
8297 -- Abort_Defer.all;
8298 -- Lock (_object._object'Access);
8299 -- return pfuncN (_object);
8301 -- at end
8302 -- _clean;
8303 -- end pfunc;
8305 -- procedure entE
8306 -- (O : System.Address;
8307 -- P : System.Address;
8308 -- E : Protected_Entry_Index)
8309 -- is
8310 -- <discriminant renamings>
8311 -- <private object renamings>
8312 -- type poVP is access poV;
8313 -- _Object : ptVP := ptVP!(O);
8315 -- begin
8316 -- begin
8317 -- <statement sequence>
8318 -- Complete_Entry_Body (_Object._Object);
8319 -- exception
8320 -- when all others =>
8321 -- Exceptional_Complete_Entry_Body (
8322 -- _Object._Object, Get_GNAT_Exception);
8323 -- end;
8324 -- end entE;
8326 -- The type poV is the record created for the protected type to hold
8327 -- the state of the protected object.
8329 procedure Expand_N_Protected_Body (N : Node_Id) is
8330 Loc : constant Source_Ptr := Sloc (N);
8331 Pid : constant Entity_Id := Corresponding_Spec (N);
8333 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8334 -- This flag indicates whether the lock free implementation is active
8336 Current_Node : Node_Id;
8337 Disp_Op_Body : Node_Id;
8338 New_Op_Body : Node_Id;
8339 New_Op_Spec : Node_Id;
8340 Op_Body : Node_Id;
8341 Op_Decl : Node_Id;
8342 Op_Id : Entity_Id;
8343 Op_Spec : Entity_Id;
8345 function Build_Dispatching_Subprogram_Body
8346 (N : Node_Id;
8347 Pid : Node_Id;
8348 Prot_Bod : Node_Id) return Node_Id;
8349 -- Build a dispatching version of the protected subprogram body. The
8350 -- newly generated subprogram contains a call to the original protected
8351 -- body. The following code is generated:
8353 -- function <protected-function-name> (Param1 .. ParamN) return
8354 -- <return-type> is
8355 -- begin
8356 -- return <protected-function-name>P (Param1 .. ParamN);
8357 -- end <protected-function-name>;
8359 -- or
8361 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8362 -- begin
8363 -- <protected-procedure-name>P (Param1 .. ParamN);
8364 -- end <protected-procedure-name>
8366 ---------------------------------------
8367 -- Build_Dispatching_Subprogram_Body --
8368 ---------------------------------------
8370 function Build_Dispatching_Subprogram_Body
8371 (N : Node_Id;
8372 Pid : Node_Id;
8373 Prot_Bod : Node_Id) return Node_Id
8375 Loc : constant Source_Ptr := Sloc (N);
8376 Actuals : List_Id;
8377 Formal : Node_Id;
8378 Spec : Node_Id;
8379 Stmts : List_Id;
8381 begin
8382 -- Generate a specification without a letter suffix in order to
8383 -- override an interface function or procedure.
8385 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8387 -- The formal parameters become the actuals of the protected function
8388 -- or procedure call.
8390 Actuals := New_List;
8391 Formal := First (Parameter_Specifications (Spec));
8392 while Present (Formal) loop
8393 Append_To (Actuals,
8394 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8395 Next (Formal);
8396 end loop;
8398 if Nkind (Spec) = N_Procedure_Specification then
8399 Stmts :=
8400 New_List (
8401 Make_Procedure_Call_Statement (Loc,
8402 Name =>
8403 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8404 Parameter_Associations => Actuals));
8406 else
8407 pragma Assert (Nkind (Spec) = N_Function_Specification);
8409 Stmts :=
8410 New_List (
8411 Make_Simple_Return_Statement (Loc,
8412 Expression =>
8413 Make_Function_Call (Loc,
8414 Name =>
8415 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8416 Parameter_Associations => Actuals)));
8417 end if;
8419 return
8420 Make_Subprogram_Body (Loc,
8421 Declarations => Empty_List,
8422 Specification => Spec,
8423 Handled_Statement_Sequence =>
8424 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8425 end Build_Dispatching_Subprogram_Body;
8427 -- Start of processing for Expand_N_Protected_Body
8429 begin
8430 if No_Run_Time_Mode then
8431 Error_Msg_CRT ("protected body", N);
8432 return;
8433 end if;
8435 -- This is the proper body corresponding to a stub. The declarations
8436 -- must be inserted at the point of the stub, which in turn is in the
8437 -- declarative part of the parent unit.
8439 if Nkind (Parent (N)) = N_Subunit then
8440 Current_Node := Corresponding_Stub (Parent (N));
8441 else
8442 Current_Node := N;
8443 end if;
8445 Op_Body := First (Declarations (N));
8447 -- The protected body is replaced with the bodies of its protected
8448 -- operations, and the declarations for internal objects that may
8449 -- have been created for entry family bounds.
8451 Rewrite (N, Make_Null_Statement (Sloc (N)));
8452 Analyze (N);
8454 while Present (Op_Body) loop
8455 case Nkind (Op_Body) is
8456 when N_Subprogram_Declaration =>
8457 null;
8459 when N_Subprogram_Body =>
8460 Op_Spec := Corresponding_Spec (Op_Body);
8462 -- Do not create bodies for eliminated operations
8464 if not Is_Eliminated (Defining_Entity (Op_Body))
8465 and then not Is_Eliminated (Op_Spec)
8466 then
8467 if Lock_Free_Active then
8468 New_Op_Body :=
8469 Build_Lock_Free_Unprotected_Subprogram_Body
8470 (Op_Body, Pid);
8471 else
8472 New_Op_Body :=
8473 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8474 end if;
8476 Insert_After (Current_Node, New_Op_Body);
8477 Current_Node := New_Op_Body;
8478 Analyze (New_Op_Body);
8480 New_Op_Spec := Corresponding_Spec (New_Op_Body);
8482 -- When the original subprogram body has nested subprograms,
8483 -- the new body also has them, so set the flag accordingly.
8485 Set_Has_Nested_Subprogram
8486 (New_Op_Spec, Has_Nested_Subprogram (New_Op_Spec));
8488 -- Similarly, when the original subprogram body uses the
8489 -- secondary stack, the new body also does. This is needed
8490 -- when the cleanup actions of the subprogram are delayed
8491 -- because it contains a package instance with a body.
8493 Set_Uses_Sec_Stack (New_Op_Spec, Uses_Sec_Stack (Op_Spec));
8495 -- Now reset the scopes of the top-level nested subprograms
8496 -- and other declaration entities so that they now refer to
8497 -- the new body's entity (it would preferable to do this
8498 -- within Build_Protected_Sub_Specification, which is called
8499 -- from Build_Unprotected_Subprogram_Body, but the needed
8500 -- subprogram entity isn't available via Corresponding_Spec
8501 -- until after the above Analyze call).
8503 Reset_Scopes_To (New_Op_Body, New_Op_Spec);
8505 -- Build the corresponding protected operation. This is
8506 -- needed only if this is a public or private operation of
8507 -- the type.
8509 Op_Decl := Unit_Declaration_Node (Op_Spec);
8511 if Nkind (Parent (Op_Decl)) = N_Protected_Definition then
8512 if Lock_Free_Active then
8513 New_Op_Body :=
8514 Build_Lock_Free_Protected_Subprogram_Body
8515 (Op_Body, Pid, Specification (New_Op_Body));
8516 else
8517 New_Op_Body :=
8518 Build_Protected_Subprogram_Body
8519 (Op_Body, Pid, Specification (New_Op_Body));
8520 end if;
8522 Insert_After (Current_Node, New_Op_Body);
8523 Current_Node := New_Op_Body;
8524 Analyze (New_Op_Body);
8526 -- Generate an overriding primitive operation body for
8527 -- this subprogram if the protected type implements
8528 -- an interface.
8530 if Ada_Version >= Ada_2005
8531 and then
8532 Present (Interfaces (Corresponding_Record_Type (Pid)))
8533 then
8534 Disp_Op_Body :=
8535 Build_Dispatching_Subprogram_Body (
8536 Op_Body, Pid, New_Op_Body);
8538 Insert_After (Current_Node, Disp_Op_Body);
8539 Current_Node := Disp_Op_Body;
8540 Analyze (Disp_Op_Body);
8541 end if;
8542 end if;
8543 end if;
8545 when N_Entry_Body =>
8546 Op_Id := Defining_Identifier (Op_Body);
8547 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8549 Insert_After (Current_Node, New_Op_Body);
8550 Current_Node := New_Op_Body;
8551 Analyze (New_Op_Body);
8553 when N_Implicit_Label_Declaration =>
8554 null;
8556 when N_Call_Marker
8557 | N_Itype_Reference
8559 New_Op_Body := New_Copy (Op_Body);
8560 Insert_After (Current_Node, New_Op_Body);
8561 Current_Node := New_Op_Body;
8563 when N_Freeze_Entity =>
8564 New_Op_Body := New_Copy (Op_Body);
8566 if Present (Entity (Op_Body))
8567 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8568 then
8569 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8570 end if;
8572 Insert_After (Current_Node, New_Op_Body);
8573 Current_Node := New_Op_Body;
8574 Analyze (New_Op_Body);
8576 when N_Pragma =>
8577 New_Op_Body := New_Copy (Op_Body);
8578 Insert_After (Current_Node, New_Op_Body);
8579 Current_Node := New_Op_Body;
8580 Analyze (New_Op_Body);
8582 when N_Object_Declaration =>
8583 pragma Assert (not Comes_From_Source (Op_Body));
8584 New_Op_Body := New_Copy (Op_Body);
8585 Insert_After (Current_Node, New_Op_Body);
8586 Current_Node := New_Op_Body;
8587 Analyze (New_Op_Body);
8589 when others =>
8590 raise Program_Error;
8591 end case;
8593 Next (Op_Body);
8594 end loop;
8596 -- Finally, create the body of the function that maps an entry index
8597 -- into the corresponding body index, except when there is no entry, or
8598 -- in a Ravenscar-like profile.
8600 if Corresponding_Runtime_Package (Pid) =
8601 System_Tasking_Protected_Objects_Entries
8602 then
8603 New_Op_Body := Build_Find_Body_Index (Pid);
8604 Insert_After (Current_Node, New_Op_Body);
8605 Current_Node := New_Op_Body;
8606 Analyze (New_Op_Body);
8607 end if;
8609 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8610 -- protected body. At this point all wrapper specs have been created,
8611 -- frozen and included in the dispatch table for the protected type.
8613 if Ada_Version >= Ada_2005 then
8614 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8615 end if;
8616 end Expand_N_Protected_Body;
8618 -----------------------------------------
8619 -- Expand_N_Protected_Type_Declaration --
8620 -----------------------------------------
8622 -- First we create a corresponding record type declaration used to
8623 -- represent values of this protected type.
8624 -- The general form of this type declaration is
8626 -- type poV (discriminants) is record
8627 -- _Object : aliased <kind>Protection
8628 -- [(<entry count> [, <handler count>])];
8629 -- [entry_family : array (bounds) of Void;]
8630 -- <private data fields>
8631 -- end record;
8633 -- The discriminants are present only if the corresponding protected type
8634 -- has discriminants, and they exactly mirror the protected type
8635 -- discriminants. The private data fields similarly mirror the private
8636 -- declarations of the protected type.
8638 -- The Object field is always present. It contains RTS specific data used
8639 -- to control the protected object. It is declared as Aliased so that it
8640 -- can be passed as a pointer to the RTS. This allows the protected record
8641 -- to be referenced within RTS data structures. An appropriate Protection
8642 -- type and discriminant are generated.
8644 -- The Service field is present for protected objects with entries. It
8645 -- contains sufficient information to allow the entry service procedure for
8646 -- this object to be called when the object is not known till runtime.
8648 -- One entry_family component is present for each entry family in the
8649 -- task definition (see Expand_N_Task_Type_Declaration).
8651 -- When a protected object is declared, an instance of the protected type
8652 -- value record is created. The elaboration of this declaration creates the
8653 -- correct bounds for the entry families, and also evaluates the priority
8654 -- expression if needed. The initialization routine for the protected type
8655 -- itself then calls Initialize_Protection with appropriate parameters to
8656 -- initialize the value of the Task_Id field. Install_Handlers may be also
8657 -- called if a pragma Attach_Handler applies.
8659 -- Note: this record is passed to the subprograms created by the expansion
8660 -- of protected subprograms and entries. It is an in parameter to protected
8661 -- functions and an in out parameter to procedures and entry bodies. The
8662 -- Entity_Id for this created record type is placed in the
8663 -- Corresponding_Record_Type field of the associated protected type entity.
8665 -- Next we create a procedure specifications for protected subprograms and
8666 -- entry bodies. For each protected subprograms two subprograms are
8667 -- created, an unprotected and a protected version. The unprotected version
8668 -- is called from within other operations of the same protected object.
8670 -- We also build the call to register the procedure if a pragma
8671 -- Interrupt_Handler applies.
8673 -- A single subprogram is created to service all entry bodies; it has an
8674 -- additional boolean out parameter indicating that the previous entry call
8675 -- made by the current task was serviced immediately, i.e. not by proxy.
8676 -- The O parameter contains a pointer to a record object of the type
8677 -- described above. An untyped interface is used here to allow this
8678 -- procedure to be called in places where the type of the object to be
8679 -- serviced is not known. This must be done, for example, when a call that
8680 -- may have been requeued is cancelled; the corresponding object must be
8681 -- serviced, but which object that is not known till runtime.
8683 -- procedure ptypeS
8684 -- (O : System.Address; P : out Boolean);
8685 -- procedure pprocN (_object : in out poV);
8686 -- procedure pproc (_object : in out poV);
8687 -- function pfuncN (_object : poV);
8688 -- function pfunc (_object : poV);
8689 -- ...
8691 -- Note that this must come after the record type declaration, since
8692 -- the specs refer to this type.
8694 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8695 Discr_Map : constant Elist_Id := New_Elmt_List;
8696 Loc : constant Source_Ptr := Sloc (N);
8697 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8699 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8700 -- This flag indicates whether the lock free implementation is active
8702 Pdef : constant Node_Id := Protected_Definition (N);
8703 -- This contains two lists; one for visible and one for private decls
8705 Current_Node : Node_Id := N;
8706 E_Count : Int;
8707 Entries_Aggr : Node_Id;
8708 Rec_Decl : Node_Id;
8709 Rec_Id : Entity_Id;
8711 procedure Check_Inlining (Subp : Entity_Id);
8712 -- If the original operation has a pragma Inline, propagate the flag
8713 -- to the internal body, for possible inlining later on. The source
8714 -- operation is invisible to the back-end and is never actually called.
8716 procedure Expand_Entry_Declaration (Decl : Node_Id);
8717 -- Create the entry barrier and the procedure body for entry declaration
8718 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
8720 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8721 -- When compiling under the Ravenscar profile, private components must
8722 -- have a static size, or else a protected object will require heap
8723 -- allocation, violating the corresponding restriction. It is preferable
8724 -- to make this check here, because it provides a better error message
8725 -- than the back-end, which refers to the object as a whole.
8727 procedure Register_Handler;
8728 -- For a protected operation that is an interrupt handler, add the
8729 -- freeze action that will register it as such.
8731 procedure Replace_Access_Definition (Comp : Node_Id);
8732 -- If a private component of the type is an access to itself, this
8733 -- is not a reference to the current instance, but an access type out
8734 -- of which one might construct a list. If such a component exists, we
8735 -- create an incomplete type for the equivalent record type, and
8736 -- a named access type for it, that replaces the access definition
8737 -- of the original component. This is similar to what is done for
8738 -- records in Check_Anonymous_Access_Components, but simpler, because
8739 -- the corresponding record type has no previous declaration.
8740 -- This needs to be done only once, even if there are several such
8741 -- access components. The following entity stores the constructed
8742 -- access type.
8744 Acc_T : Entity_Id := Empty;
8746 --------------------
8747 -- Check_Inlining --
8748 --------------------
8750 procedure Check_Inlining (Subp : Entity_Id) is
8751 begin
8752 if Is_Inlined (Subp) then
8753 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8754 Set_Is_Inlined (Subp, False);
8755 end if;
8757 if Has_Pragma_No_Inline (Subp) then
8758 Set_Has_Pragma_No_Inline (Protected_Body_Subprogram (Subp));
8759 end if;
8760 end Check_Inlining;
8762 ---------------------------
8763 -- Static_Component_Size --
8764 ---------------------------
8766 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8767 Typ : constant Entity_Id := Etype (Comp);
8768 C : Entity_Id;
8770 begin
8771 if Is_Scalar_Type (Typ) then
8772 return True;
8774 elsif Is_Array_Type (Typ) then
8775 return Compile_Time_Known_Bounds (Typ);
8777 elsif Is_Record_Type (Typ) then
8778 C := First_Component (Typ);
8779 while Present (C) loop
8780 if not Static_Component_Size (C) then
8781 return False;
8782 end if;
8784 Next_Component (C);
8785 end loop;
8787 return True;
8789 -- Any other type will be checked by the back-end
8791 else
8792 return True;
8793 end if;
8794 end Static_Component_Size;
8796 ------------------------------
8797 -- Expand_Entry_Declaration --
8798 ------------------------------
8800 procedure Expand_Entry_Declaration (Decl : Node_Id) is
8801 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
8802 Bar_Id : Entity_Id;
8803 Bod_Id : Entity_Id;
8804 Subp : Node_Id;
8806 begin
8807 E_Count := E_Count + 1;
8809 -- Create the protected body subprogram
8811 Bod_Id :=
8812 Make_Defining_Identifier (Loc,
8813 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
8814 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
8816 Subp :=
8817 Make_Subprogram_Declaration (Loc,
8818 Specification =>
8819 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
8821 Insert_After (Current_Node, Subp);
8822 Current_Node := Subp;
8824 Analyze (Subp);
8826 -- Build a wrapper procedure to handle contract cases, preconditions,
8827 -- and postconditions.
8829 Build_Entry_Contract_Wrapper (Ent_Id, N);
8831 -- Create the barrier function
8833 Bar_Id :=
8834 Make_Defining_Identifier (Loc,
8835 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
8836 Set_Barrier_Function (Ent_Id, Bar_Id);
8838 Subp :=
8839 Make_Subprogram_Declaration (Loc,
8840 Specification =>
8841 Build_Barrier_Function_Specification (Loc, Bar_Id));
8842 Set_Is_Entry_Barrier_Function (Subp);
8844 Insert_After (Current_Node, Subp);
8845 Current_Node := Subp;
8847 Analyze (Subp);
8849 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
8850 Set_Scope (Bar_Id, Scope (Ent_Id));
8852 -- Collect pointers to the protected subprogram and the barrier
8853 -- of the current entry, for insertion into Entry_Bodies_Array.
8855 Append_To (Expressions (Entries_Aggr),
8856 Make_Aggregate (Loc,
8857 Expressions => New_List (
8858 Make_Attribute_Reference (Loc,
8859 Prefix => New_Occurrence_Of (Bar_Id, Loc),
8860 Attribute_Name => Name_Unrestricted_Access),
8861 Make_Attribute_Reference (Loc,
8862 Prefix => New_Occurrence_Of (Bod_Id, Loc),
8863 Attribute_Name => Name_Unrestricted_Access))));
8864 end Expand_Entry_Declaration;
8866 ----------------------
8867 -- Register_Handler --
8868 ----------------------
8870 procedure Register_Handler is
8872 -- All semantic checks already done in Sem_Prag
8874 Prot_Proc : constant Entity_Id :=
8875 Defining_Unit_Name (Specification (Current_Node));
8877 Proc_Address : constant Node_Id :=
8878 Make_Attribute_Reference (Loc,
8879 Prefix =>
8880 New_Occurrence_Of (Prot_Proc, Loc),
8881 Attribute_Name => Name_Address);
8883 RTS_Call : constant Entity_Id :=
8884 Make_Procedure_Call_Statement (Loc,
8885 Name =>
8886 New_Occurrence_Of
8887 (RTE (RE_Register_Interrupt_Handler), Loc),
8888 Parameter_Associations => New_List (Proc_Address));
8889 begin
8890 Append_Freeze_Action (Prot_Proc, RTS_Call);
8891 end Register_Handler;
8893 -------------------------------
8894 -- Replace_Access_Definition --
8895 -------------------------------
8897 procedure Replace_Access_Definition (Comp : Node_Id) is
8898 Loc : constant Source_Ptr := Sloc (Comp);
8899 Inc_T : Node_Id;
8900 Inc_D : Node_Id;
8901 Acc_Def : Node_Id;
8902 Acc_D : Node_Id;
8904 begin
8905 if No (Acc_T) then
8906 Inc_T := Make_Defining_Identifier (Loc, Chars (Rec_Id));
8907 Inc_D := Make_Incomplete_Type_Declaration (Loc, Inc_T);
8908 Acc_T := Make_Temporary (Loc, 'S');
8909 Acc_Def :=
8910 Make_Access_To_Object_Definition (Loc,
8911 Subtype_Indication => New_Occurrence_Of (Inc_T, Loc));
8912 Acc_D :=
8913 Make_Full_Type_Declaration (Loc,
8914 Defining_Identifier => Acc_T,
8915 Type_Definition => Acc_Def);
8917 Insert_Before (Rec_Decl, Inc_D);
8918 Analyze (Inc_D);
8920 Insert_Before (Rec_Decl, Acc_D);
8921 Analyze (Acc_D);
8922 end if;
8924 Set_Access_Definition (Comp, Empty);
8925 Set_Subtype_Indication (Comp, New_Occurrence_Of (Acc_T, Loc));
8926 end Replace_Access_Definition;
8928 -- Local variables
8930 Body_Arr : Node_Id;
8931 Body_Id : Entity_Id;
8932 Cdecls : List_Id;
8933 Comp : Node_Id;
8934 Expr : Node_Id;
8935 New_Priv : Node_Id;
8936 Obj_Def : Node_Id;
8937 Object_Comp : Node_Id;
8938 Priv : Node_Id;
8939 Sub : Node_Id;
8941 -- Start of processing for Expand_N_Protected_Type_Declaration
8943 begin
8944 if Present (Corresponding_Record_Type (Prot_Typ)) then
8945 return;
8946 else
8947 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
8948 Rec_Id := Defining_Identifier (Rec_Decl);
8949 end if;
8951 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
8953 Qualify_Entity_Names (N);
8955 -- If the type has discriminants, their occurrences in the declaration
8956 -- have been replaced by the corresponding discriminals. For components
8957 -- that are constrained by discriminants, their homologues in the
8958 -- corresponding record type must refer to the discriminants of that
8959 -- record, so we must apply a new renaming to subtypes_indications:
8961 -- protected discriminant => discriminal => record discriminant
8963 -- This replacement is not applied to default expressions, for which
8964 -- the discriminal is correct.
8966 if Has_Discriminants (Prot_Typ) then
8967 declare
8968 Disc : Entity_Id;
8969 Decl : Node_Id;
8971 begin
8972 Disc := First_Discriminant (Prot_Typ);
8973 Decl := First (Discriminant_Specifications (Rec_Decl));
8974 while Present (Disc) loop
8975 Append_Elmt (Discriminal (Disc), Discr_Map);
8976 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
8977 Next_Discriminant (Disc);
8978 Next (Decl);
8979 end loop;
8980 end;
8981 end if;
8983 -- Fill in the component declarations
8985 -- Add components for entry families. For each entry family, create an
8986 -- anonymous type declaration with the same size, and analyze the type.
8988 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
8990 pragma Assert (Present (Pdef));
8992 Insert_After (Current_Node, Rec_Decl);
8993 Current_Node := Rec_Decl;
8995 -- Add private field components
8997 Priv := First (Private_Declarations (Pdef));
8998 while Present (Priv) loop
8999 if Nkind (Priv) = N_Component_Declaration then
9000 if not Static_Component_Size (Defining_Identifier (Priv)) then
9002 -- When compiling for a restricted profile, the private
9003 -- components must have a static size. If not, this is an error
9004 -- for a single protected declaration, and rates a warning on a
9005 -- protected type declaration.
9007 if not Comes_From_Source (Prot_Typ) then
9009 -- It's ok to be checking this restriction at expansion
9010 -- time, because this is only for the restricted profile,
9011 -- which is not subject to strict RM conformance, so it
9012 -- is OK to miss this check in -gnatc mode.
9014 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9015 Check_Restriction
9016 (No_Implicit_Protected_Object_Allocations, Priv);
9018 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9019 if not Discriminated_Size (Defining_Identifier (Priv))
9020 then
9021 -- Any object of the type will be non-static
9023 Error_Msg_N ("component has non-static size??", Priv);
9024 Error_Msg_NE
9025 ("\creation of protected object of type& will "
9026 & "violate restriction "
9027 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9028 else
9029 -- Object will be non-static if discriminants are
9031 Error_Msg_NE
9032 ("creation of protected object of type& with "
9033 & "non-static discriminants will violate "
9034 & "restriction No_Implicit_Heap_Allocations??",
9035 Priv, Prot_Typ);
9036 end if;
9038 -- Likewise for No_Implicit_Protected_Object_Allocations
9040 elsif Restriction_Active
9041 (No_Implicit_Protected_Object_Allocations)
9042 then
9043 if not Discriminated_Size (Defining_Identifier (Priv)) then
9044 -- Any object of the type will be non-static
9046 Error_Msg_N ("component has non-static size??", Priv);
9047 Error_Msg_NE
9048 ("\creation of protected object of type& will violate "
9049 & "restriction "
9050 & "No_Implicit_Protected_Object_Allocations??",
9051 Priv, Prot_Typ);
9052 else
9053 -- Object will be non-static if discriminants are
9055 Error_Msg_NE
9056 ("creation of protected object of type& with "
9057 & "non-static discriminants will violate restriction "
9058 & "No_Implicit_Protected_Object_Allocations??",
9059 Priv, Prot_Typ);
9060 end if;
9061 end if;
9062 end if;
9064 -- The component definition consists of a subtype indication, or
9065 -- (in Ada 2005) an access definition. Make a copy of the proper
9066 -- definition.
9068 declare
9069 Old_Comp : constant Node_Id := Component_Definition (Priv);
9070 Oent : constant Entity_Id := Defining_Identifier (Priv);
9071 Nent : constant Entity_Id :=
9072 Make_Defining_Identifier (Sloc (Oent),
9073 Chars => Chars (Oent));
9074 New_Comp : Node_Id;
9076 begin
9077 if Present (Subtype_Indication (Old_Comp)) then
9078 New_Comp :=
9079 Make_Component_Definition (Sloc (Oent),
9080 Aliased_Present => False,
9081 Subtype_Indication =>
9082 New_Copy_Tree
9083 (Subtype_Indication (Old_Comp), Discr_Map));
9084 else
9085 New_Comp :=
9086 Make_Component_Definition (Sloc (Oent),
9087 Aliased_Present => False,
9088 Access_Definition =>
9089 New_Copy_Tree
9090 (Access_Definition (Old_Comp), Discr_Map));
9092 -- A self-reference in the private part becomes a
9093 -- self-reference to the corresponding record.
9095 if Entity (Subtype_Mark (Access_Definition (New_Comp)))
9096 = Prot_Typ
9097 then
9098 Replace_Access_Definition (New_Comp);
9099 end if;
9100 end if;
9102 New_Priv :=
9103 Make_Component_Declaration (Loc,
9104 Defining_Identifier => Nent,
9105 Component_Definition => New_Comp,
9106 Expression => Expression (Priv));
9108 Set_Has_Per_Object_Constraint (Nent,
9109 Has_Per_Object_Constraint (Oent));
9111 Append_To (Cdecls, New_Priv);
9112 end;
9114 elsif Nkind (Priv) = N_Subprogram_Declaration then
9116 -- Make the unprotected version of the subprogram available for
9117 -- expansion of intra object calls. There is need for a protected
9118 -- version only if the subprogram is an interrupt handler,
9119 -- otherwise this operation can only be called from within the
9120 -- body.
9122 Sub :=
9123 Make_Subprogram_Declaration (Loc,
9124 Specification =>
9125 Build_Protected_Sub_Specification
9126 (Priv, Prot_Typ, Unprotected_Mode));
9128 Insert_After (Current_Node, Sub);
9129 Analyze (Sub);
9131 Set_Protected_Body_Subprogram
9132 (Defining_Unit_Name (Specification (Priv)),
9133 Defining_Unit_Name (Specification (Sub)));
9134 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9135 Current_Node := Sub;
9137 Sub :=
9138 Make_Subprogram_Declaration (Loc,
9139 Specification =>
9140 Build_Protected_Sub_Specification
9141 (Priv, Prot_Typ, Protected_Mode));
9143 Insert_After (Current_Node, Sub);
9144 Analyze (Sub);
9145 Current_Node := Sub;
9147 if Is_Interrupt_Handler
9148 (Defining_Unit_Name (Specification (Priv)))
9149 then
9150 if not Restricted_Profile then
9151 Register_Handler;
9152 end if;
9153 end if;
9154 end if;
9156 Next (Priv);
9157 end loop;
9159 -- Except for the lock-free implementation, append the _Object field
9160 -- with the right type to the component list. We need to compute the
9161 -- number of entries, and in some cases the number of Attach_Handler
9162 -- pragmas.
9164 if not Lock_Free_Active then
9165 declare
9166 Entry_Count_Expr : constant Node_Id :=
9167 Build_Entry_Count_Expression
9168 (Prot_Typ, Loc);
9169 Num_Attach_Handler : Nat := 0;
9170 Protection_Subtype : Node_Id;
9171 Ritem : Node_Id;
9173 begin
9174 if Has_Attach_Handler (Prot_Typ) then
9175 Ritem := First_Rep_Item (Prot_Typ);
9176 while Present (Ritem) loop
9177 if Nkind (Ritem) = N_Pragma
9178 and then Pragma_Name (Ritem) = Name_Attach_Handler
9179 then
9180 Num_Attach_Handler := Num_Attach_Handler + 1;
9181 end if;
9183 Next_Rep_Item (Ritem);
9184 end loop;
9185 end if;
9187 -- Determine the proper protection type. There are two special
9188 -- cases: 1) when the protected type has dynamic interrupt
9189 -- handlers, and 2) when it has static handlers and we use a
9190 -- restricted profile.
9192 if Has_Attach_Handler (Prot_Typ)
9193 and then not Restricted_Profile
9194 then
9195 Protection_Subtype :=
9196 Make_Subtype_Indication (Loc,
9197 Subtype_Mark =>
9198 New_Occurrence_Of
9199 (RTE (RE_Static_Interrupt_Protection), Loc),
9200 Constraint =>
9201 Make_Index_Or_Discriminant_Constraint (Loc,
9202 Constraints => New_List (
9203 Entry_Count_Expr,
9204 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9206 elsif Has_Interrupt_Handler (Prot_Typ)
9207 and then not Restriction_Active (No_Dynamic_Attachment)
9208 then
9209 Protection_Subtype :=
9210 Make_Subtype_Indication (Loc,
9211 Subtype_Mark =>
9212 New_Occurrence_Of
9213 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9214 Constraint =>
9215 Make_Index_Or_Discriminant_Constraint (Loc,
9216 Constraints => New_List (Entry_Count_Expr)));
9218 else
9219 case Corresponding_Runtime_Package (Prot_Typ) is
9220 when System_Tasking_Protected_Objects_Entries =>
9221 Protection_Subtype :=
9222 Make_Subtype_Indication (Loc,
9223 Subtype_Mark =>
9224 New_Occurrence_Of
9225 (RTE (RE_Protection_Entries), Loc),
9226 Constraint =>
9227 Make_Index_Or_Discriminant_Constraint (Loc,
9228 Constraints => New_List (Entry_Count_Expr)));
9230 when System_Tasking_Protected_Objects_Single_Entry =>
9231 Protection_Subtype :=
9232 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9234 when System_Tasking_Protected_Objects =>
9235 Protection_Subtype :=
9236 New_Occurrence_Of (RTE (RE_Protection), Loc);
9238 when others =>
9239 raise Program_Error;
9240 end case;
9241 end if;
9243 Object_Comp :=
9244 Make_Component_Declaration (Loc,
9245 Defining_Identifier =>
9246 Make_Defining_Identifier (Loc, Name_uObject),
9247 Component_Definition =>
9248 Make_Component_Definition (Loc,
9249 Aliased_Present => True,
9250 Subtype_Indication => Protection_Subtype));
9251 end;
9253 -- Put the _Object component after the private component so that it
9254 -- be finalized early as required by 9.4 (20)
9256 Append_To (Cdecls, Object_Comp);
9257 end if;
9259 -- Analyze the record declaration immediately after construction,
9260 -- because the initialization procedure is needed for single object
9261 -- declarations before the next entity is analyzed (the freeze call
9262 -- that generates this initialization procedure is found below).
9264 Analyze (Rec_Decl, Suppress => All_Checks);
9266 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9267 -- the corresponding record is frozen. If any wrappers are generated,
9268 -- Current_Node is updated accordingly.
9270 if Ada_Version >= Ada_2005 then
9271 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9272 end if;
9274 -- Collect pointers to entry bodies and their barriers, to be placed
9275 -- in the Entry_Bodies_Array for the type. For each entry/family we
9276 -- add an expression to the aggregate which is the initial value of
9277 -- this array. The array is declared after all protected subprograms.
9279 if Has_Entries (Prot_Typ) then
9280 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9281 else
9282 Entries_Aggr := Empty;
9283 end if;
9285 -- Build two new procedure specifications for each protected subprogram;
9286 -- one to call from outside the object and one to call from inside.
9287 -- Build a barrier function and an entry body action procedure
9288 -- specification for each protected entry. Initialize the entry body
9289 -- array. If subprogram is flagged as eliminated, do not generate any
9290 -- internal operations.
9292 E_Count := 0;
9293 Comp := First (Visible_Declarations (Pdef));
9294 while Present (Comp) loop
9295 if Nkind (Comp) = N_Subprogram_Declaration then
9296 Sub :=
9297 Make_Subprogram_Declaration (Loc,
9298 Specification =>
9299 Build_Protected_Sub_Specification
9300 (Comp, Prot_Typ, Unprotected_Mode));
9302 Insert_After (Current_Node, Sub);
9303 Analyze (Sub);
9305 Set_Protected_Body_Subprogram
9306 (Defining_Unit_Name (Specification (Comp)),
9307 Defining_Unit_Name (Specification (Sub)));
9308 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9310 -- Make the protected version of the subprogram available for
9311 -- expansion of external calls.
9313 Current_Node := Sub;
9315 Sub :=
9316 Make_Subprogram_Declaration (Loc,
9317 Specification =>
9318 Build_Protected_Sub_Specification
9319 (Comp, Prot_Typ, Protected_Mode));
9321 Insert_After (Current_Node, Sub);
9322 Analyze (Sub);
9324 Current_Node := Sub;
9326 -- Generate an overriding primitive operation specification for
9327 -- this subprogram if the protected type implements an interface
9328 -- and Build_Wrapper_Spec did not generate its wrapper.
9330 if Ada_Version >= Ada_2005
9331 and then
9332 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9333 then
9334 declare
9335 Found : Boolean := False;
9336 Prim_Elmt : Elmt_Id;
9337 Prim_Op : Node_Id;
9339 begin
9340 Prim_Elmt :=
9341 First_Elmt
9342 (Primitive_Operations
9343 (Corresponding_Record_Type (Prot_Typ)));
9345 while Present (Prim_Elmt) loop
9346 Prim_Op := Node (Prim_Elmt);
9348 if Is_Primitive_Wrapper (Prim_Op)
9349 and then Wrapped_Entity (Prim_Op) =
9350 Defining_Entity (Specification (Comp))
9351 then
9352 Found := True;
9353 exit;
9354 end if;
9356 Next_Elmt (Prim_Elmt);
9357 end loop;
9359 if not Found then
9360 Sub :=
9361 Make_Subprogram_Declaration (Loc,
9362 Specification =>
9363 Build_Protected_Sub_Specification
9364 (Comp, Prot_Typ, Dispatching_Mode));
9366 Insert_After (Current_Node, Sub);
9367 Analyze (Sub);
9369 Current_Node := Sub;
9370 end if;
9371 end;
9372 end if;
9374 -- If a pragma Interrupt_Handler applies, build and add a call to
9375 -- Register_Interrupt_Handler to the freezing actions of the
9376 -- protected version (Current_Node) of the subprogram:
9378 -- system.interrupts.register_interrupt_handler
9379 -- (prot_procP'address);
9381 if not Restricted_Profile
9382 and then Is_Interrupt_Handler
9383 (Defining_Unit_Name (Specification (Comp)))
9384 then
9385 Register_Handler;
9386 end if;
9388 elsif Nkind (Comp) = N_Entry_Declaration then
9389 Expand_Entry_Declaration (Comp);
9390 end if;
9392 Next (Comp);
9393 end loop;
9395 -- If there are some private entry declarations, expand it as if they
9396 -- were visible entries.
9398 Comp := First (Private_Declarations (Pdef));
9399 while Present (Comp) loop
9400 if Nkind (Comp) = N_Entry_Declaration then
9401 Expand_Entry_Declaration (Comp);
9402 end if;
9404 Next (Comp);
9405 end loop;
9407 -- Create the declaration of an array object which contains the values
9408 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9409 -- type. This object is later passed to the appropriate protected object
9410 -- initialization routine.
9412 if Has_Entries (Prot_Typ)
9413 and then Corresponding_Runtime_Package (Prot_Typ) =
9414 System_Tasking_Protected_Objects_Entries
9415 then
9416 declare
9417 Count : Int;
9418 Item : Entity_Id;
9419 Max_Vals : Node_Id;
9420 Maxes : List_Id;
9421 Maxes_Id : Entity_Id;
9422 Need_Array : Boolean := False;
9424 begin
9425 -- First check if there is any Max_Queue_Length pragma
9427 Item := First_Entity (Prot_Typ);
9428 while Present (Item) loop
9429 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9430 Need_Array := True;
9431 exit;
9432 end if;
9434 Next_Entity (Item);
9435 end loop;
9437 -- Gather the Max_Queue_Length values of all entries in a list. A
9438 -- value of zero indicates that the entry has no limitation on its
9439 -- queue length.
9441 if Need_Array then
9442 Count := 0;
9443 Item := First_Entity (Prot_Typ);
9444 Maxes := New_List;
9445 while Present (Item) loop
9446 if Is_Entry (Item) then
9447 Count := Count + 1;
9448 Append_To (Maxes,
9449 Make_Integer_Literal
9450 (Loc, Get_Max_Queue_Length (Item)));
9451 end if;
9453 Next_Entity (Item);
9454 end loop;
9456 -- Create the declaration of the array object. Generate:
9458 -- Maxes_Id : aliased constant
9459 -- Protected_Entry_Queue_Max_Array
9460 -- (1 .. Count) := (..., ...);
9462 Maxes_Id :=
9463 Make_Defining_Identifier (Loc,
9464 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9466 Max_Vals :=
9467 Make_Object_Declaration (Loc,
9468 Defining_Identifier => Maxes_Id,
9469 Aliased_Present => True,
9470 Constant_Present => True,
9471 Object_Definition =>
9472 Make_Subtype_Indication (Loc,
9473 Subtype_Mark =>
9474 New_Occurrence_Of
9475 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9476 Constraint =>
9477 Make_Index_Or_Discriminant_Constraint (Loc,
9478 Constraints => New_List (
9479 Make_Range (Loc,
9480 Make_Integer_Literal (Loc, 1),
9481 Make_Integer_Literal (Loc, Count))))),
9482 Expression => Make_Aggregate (Loc, Maxes));
9484 -- A pointer to this array will be placed in the corresponding
9485 -- record by its initialization procedure so this needs to be
9486 -- analyzed here.
9488 Insert_After (Current_Node, Max_Vals);
9489 Current_Node := Max_Vals;
9490 Analyze (Max_Vals);
9492 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9493 end if;
9494 end;
9495 end if;
9497 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9498 -- all protected subprograms have been collected.
9500 if Has_Entries (Prot_Typ) then
9501 Body_Id :=
9502 Make_Defining_Identifier (Sloc (Prot_Typ),
9503 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9505 case Corresponding_Runtime_Package (Prot_Typ) is
9506 when System_Tasking_Protected_Objects_Entries =>
9507 Expr := Entries_Aggr;
9508 Obj_Def :=
9509 Make_Subtype_Indication (Loc,
9510 Subtype_Mark =>
9511 New_Occurrence_Of
9512 (RTE (RE_Protected_Entry_Body_Array), Loc),
9513 Constraint =>
9514 Make_Index_Or_Discriminant_Constraint (Loc,
9515 Constraints => New_List (
9516 Make_Range (Loc,
9517 Make_Integer_Literal (Loc, 1),
9518 Make_Integer_Literal (Loc, E_Count)))));
9520 when System_Tasking_Protected_Objects_Single_Entry =>
9521 Expr := Remove_Head (Expressions (Entries_Aggr));
9522 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9524 when others =>
9525 raise Program_Error;
9526 end case;
9528 Body_Arr :=
9529 Make_Object_Declaration (Loc,
9530 Defining_Identifier => Body_Id,
9531 Aliased_Present => True,
9532 Constant_Present => True,
9533 Object_Definition => Obj_Def,
9534 Expression => Expr);
9536 -- A pointer to this array will be placed in the corresponding record
9537 -- by its initialization procedure so this needs to be analyzed here.
9539 Insert_After (Current_Node, Body_Arr);
9540 Current_Node := Body_Arr;
9541 Analyze (Body_Arr);
9543 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9545 -- Finally, build the function that maps an entry index into the
9546 -- corresponding body. A pointer to this function is placed in each
9547 -- object of the type. Except for a ravenscar-like profile (no abort,
9548 -- no entry queue, 1 entry)
9550 if Corresponding_Runtime_Package (Prot_Typ) =
9551 System_Tasking_Protected_Objects_Entries
9552 then
9553 Sub :=
9554 Make_Subprogram_Declaration (Loc,
9555 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9557 Insert_After (Current_Node, Sub);
9558 Analyze (Sub);
9559 end if;
9560 end if;
9561 end Expand_N_Protected_Type_Declaration;
9563 --------------------------------
9564 -- Expand_N_Requeue_Statement --
9565 --------------------------------
9567 -- A nondispatching requeue statement is expanded into one of four GNARLI
9568 -- operations, depending on the source and destination (task or protected
9569 -- object). A dispatching requeue statement is expanded into a call to the
9570 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9571 -- jump around the remainder of processing for the original entry and, if
9572 -- the destination is (different) protected object, to attempt to service
9573 -- it. The following illustrates the various cases:
9575 -- procedure entE
9576 -- (O : System.Address;
9577 -- P : System.Address;
9578 -- E : Protected_Entry_Index)
9579 -- is
9580 -- <discriminant renamings>
9581 -- <private object renamings>
9582 -- type poVP is access poV;
9583 -- _object : ptVP := ptVP!(O);
9585 -- begin
9586 -- begin
9587 -- <start of statement sequence for entry>
9589 -- -- Requeue from one protected entry body to another protected
9590 -- -- entry.
9592 -- Requeue_Protected_Entry (
9593 -- _object._object'Access,
9594 -- new._object'Access,
9595 -- E,
9596 -- Abort_Present);
9597 -- return;
9599 -- <some more of the statement sequence for entry>
9601 -- -- Requeue from an entry body to a task entry
9603 -- Requeue_Protected_To_Task_Entry (
9604 -- New._task_id,
9605 -- E,
9606 -- Abort_Present);
9607 -- return;
9609 -- <rest of statement sequence for entry>
9610 -- Complete_Entry_Body (_object._object);
9612 -- exception
9613 -- when all others =>
9614 -- Exceptional_Complete_Entry_Body (
9615 -- _object._object, Get_GNAT_Exception);
9616 -- end;
9617 -- end entE;
9619 -- Requeue of a task entry call to a task entry
9621 -- Accept_Call (E, Ann);
9622 -- <start of statement sequence for accept statement>
9623 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9624 -- goto Lnn;
9625 -- <rest of statement sequence for accept statement>
9626 -- <<Lnn>>
9627 -- Complete_Rendezvous;
9629 -- exception
9630 -- when all others =>
9631 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9633 -- Requeue of a task entry call to a protected entry
9635 -- Accept_Call (E, Ann);
9636 -- <start of statement sequence for accept statement>
9637 -- Requeue_Task_To_Protected_Entry (
9638 -- new._object'Access,
9639 -- E,
9640 -- Abort_Present);
9641 -- newS (new, Pnn);
9642 -- goto Lnn;
9643 -- <rest of statement sequence for accept statement>
9644 -- <<Lnn>>
9645 -- Complete_Rendezvous;
9647 -- exception
9648 -- when all others =>
9649 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9651 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9652 -- marked by pragma Implemented (XXX, By_Entry).
9654 -- The requeue is inside a protected entry:
9656 -- procedure entE
9657 -- (O : System.Address;
9658 -- P : System.Address;
9659 -- E : Protected_Entry_Index)
9660 -- is
9661 -- <discriminant renamings>
9662 -- <private object renamings>
9663 -- type poVP is access poV;
9664 -- _object : ptVP := ptVP!(O);
9666 -- begin
9667 -- begin
9668 -- <start of statement sequence for entry>
9670 -- _Disp_Requeue
9671 -- (<interface class-wide object>,
9672 -- True,
9673 -- _object'Address,
9674 -- Ada.Tags.Get_Offset_Index
9675 -- (Tag (_object),
9676 -- <interface dispatch table index of target entry>),
9677 -- Abort_Present);
9678 -- return;
9680 -- <rest of statement sequence for entry>
9681 -- Complete_Entry_Body (_object._object);
9683 -- exception
9684 -- when all others =>
9685 -- Exceptional_Complete_Entry_Body (
9686 -- _object._object, Get_GNAT_Exception);
9687 -- end;
9688 -- end entE;
9690 -- The requeue is inside a task entry:
9692 -- Accept_Call (E, Ann);
9693 -- <start of statement sequence for accept statement>
9694 -- _Disp_Requeue
9695 -- (<interface class-wide object>,
9696 -- False,
9697 -- null,
9698 -- Ada.Tags.Get_Offset_Index
9699 -- (Tag (_object),
9700 -- <interface dispatch table index of target entrt>),
9701 -- Abort_Present);
9702 -- newS (new, Pnn);
9703 -- goto Lnn;
9704 -- <rest of statement sequence for accept statement>
9705 -- <<Lnn>>
9706 -- Complete_Rendezvous;
9708 -- exception
9709 -- when all others =>
9710 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9712 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9713 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9714 -- statement is replaced by a dispatching call with actual parameters taken
9715 -- from the inner-most accept statement or entry body.
9717 -- Target.Primitive (Param1, ..., ParamN);
9719 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9720 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9721 -- at all.
9723 -- declare
9724 -- S : constant Offset_Index :=
9725 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9726 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9728 -- begin
9729 -- if C = POK_Protected_Entry
9730 -- or else C = POK_Task_Entry
9731 -- then
9732 -- <statements for dispatching requeue>
9734 -- elsif C = POK_Protected_Procedure then
9735 -- <dispatching call equivalent>
9737 -- else
9738 -- raise Program_Error;
9739 -- end if;
9740 -- end;
9742 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9743 Loc : constant Source_Ptr := Sloc (N);
9744 Conc_Typ : Entity_Id;
9745 Concval : Node_Id;
9746 Ename : Node_Id;
9747 Enc_Subp : Entity_Id;
9748 Index : Node_Id;
9749 Old_Typ : Entity_Id;
9751 function Build_Dispatching_Call_Equivalent return Node_Id;
9752 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9753 -- the form Concval.Ename. It is statically known that Ename is allowed
9754 -- to be implemented by a protected procedure. Create a dispatching call
9755 -- equivalent of Concval.Ename taking the actual parameters from the
9756 -- inner-most accept statement or entry body.
9758 function Build_Dispatching_Requeue return Node_Id;
9759 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9760 -- the form Concval.Ename. It is statically known that Ename is allowed
9761 -- to be implemented by a protected or a task entry. Create a call to
9762 -- primitive _Disp_Requeue which handles the low-level actions.
9764 function Build_Dispatching_Requeue_To_Any return Node_Id;
9765 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9766 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9767 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9768 -- determines at runtime whether Ename denotes an entry or a procedure
9769 -- and perform the appropriate kind of dispatching select.
9771 function Build_Normal_Requeue return Node_Id;
9772 -- N denotes a nondispatching requeue statement to either a task or a
9773 -- protected entry. Build the appropriate runtime call to perform the
9774 -- action.
9776 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9777 -- For a protected entry, create a return statement to skip the rest of
9778 -- the entry body. Otherwise, create a goto statement to skip the rest
9779 -- of a task accept statement. The lookup for the enclosing entry body
9780 -- or accept statement starts from Search.
9782 ---------------------------------------
9783 -- Build_Dispatching_Call_Equivalent --
9784 ---------------------------------------
9786 function Build_Dispatching_Call_Equivalent return Node_Id is
9787 Call_Ent : constant Entity_Id := Entity (Ename);
9788 Obj : constant Node_Id := Original_Node (Concval);
9789 Acc_Ent : Node_Id;
9790 Actuals : List_Id;
9791 Formal : Node_Id;
9792 Formals : List_Id;
9794 begin
9795 -- Climb the parent chain looking for the inner-most entry body or
9796 -- accept statement.
9798 Acc_Ent := N;
9799 while Present (Acc_Ent)
9800 and then Nkind (Acc_Ent) not in N_Accept_Statement | N_Entry_Body
9801 loop
9802 Acc_Ent := Parent (Acc_Ent);
9803 end loop;
9805 -- A requeue statement should be housed inside an entry body or an
9806 -- accept statement at some level. If this is not the case, then the
9807 -- tree is malformed.
9809 pragma Assert (Present (Acc_Ent));
9811 -- Recover the list of formal parameters
9813 if Nkind (Acc_Ent) = N_Entry_Body then
9814 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9815 end if;
9817 Formals := Parameter_Specifications (Acc_Ent);
9819 -- Create the actual parameters for the dispatching call. These are
9820 -- simply copies of the entry body or accept statement formals in the
9821 -- same order as they appear.
9823 Actuals := No_List;
9825 if Present (Formals) then
9826 Actuals := New_List;
9827 Formal := First (Formals);
9828 while Present (Formal) loop
9829 Append_To (Actuals,
9830 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9831 Next (Formal);
9832 end loop;
9833 end if;
9835 -- Generate:
9836 -- Obj.Call_Ent (Actuals);
9838 return
9839 Make_Procedure_Call_Statement (Loc,
9840 Name =>
9841 Make_Selected_Component (Loc,
9842 Prefix => Make_Identifier (Loc, Chars (Obj)),
9843 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9845 Parameter_Associations => Actuals);
9846 end Build_Dispatching_Call_Equivalent;
9848 -------------------------------
9849 -- Build_Dispatching_Requeue --
9850 -------------------------------
9852 function Build_Dispatching_Requeue return Node_Id is
9853 Params : constant List_Id := New_List;
9855 begin
9856 -- Process the "with abort" parameter
9858 Prepend_To (Params,
9859 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
9861 -- Process the entry wrapper's position in the primary dispatch
9862 -- table parameter. Generate:
9864 -- Ada.Tags.Get_Entry_Index
9865 -- (T => To_Tag_Ptr (Obj'Address).all,
9866 -- Position =>
9867 -- Ada.Tags.Get_Offset_Index
9868 -- (Ada.Tags.Tag (Concval),
9869 -- <interface dispatch table position of Ename>));
9871 -- Note that Obj'Address is recursively expanded into a call to
9872 -- Base_Address (Obj).
9874 if Tagged_Type_Expansion then
9875 Prepend_To (Params,
9876 Make_Function_Call (Loc,
9877 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9878 Parameter_Associations => New_List (
9880 Make_Explicit_Dereference (Loc,
9881 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9882 Make_Attribute_Reference (Loc,
9883 Prefix => New_Copy_Tree (Concval),
9884 Attribute_Name => Name_Address))),
9886 Make_Function_Call (Loc,
9887 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9888 Parameter_Associations => New_List (
9889 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9890 Make_Integer_Literal (Loc,
9891 DT_Position (Entity (Ename))))))));
9893 -- VM targets
9895 else
9896 Prepend_To (Params,
9897 Make_Function_Call (Loc,
9898 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9899 Parameter_Associations => New_List (
9901 Make_Attribute_Reference (Loc,
9902 Prefix => Concval,
9903 Attribute_Name => Name_Tag),
9905 Make_Function_Call (Loc,
9906 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9908 Parameter_Associations => New_List (
9910 -- Obj_Tag
9912 Make_Attribute_Reference (Loc,
9913 Prefix => Concval,
9914 Attribute_Name => Name_Tag),
9916 -- Tag_Typ
9918 Make_Attribute_Reference (Loc,
9919 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
9920 Attribute_Name => Name_Tag),
9922 -- Position
9924 Make_Integer_Literal (Loc,
9925 DT_Position (Entity (Ename))))))));
9926 end if;
9928 -- Specific actuals for protected to XXX requeue
9930 if Is_Protected_Type (Old_Typ) then
9931 Prepend_To (Params,
9932 Make_Attribute_Reference (Loc, -- _object'Address
9933 Prefix =>
9934 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
9935 Attribute_Name => Name_Address));
9937 Prepend_To (Params, -- True
9938 New_Occurrence_Of (Standard_True, Loc));
9940 -- Specific actuals for task to XXX requeue
9942 else
9943 pragma Assert (Is_Task_Type (Old_Typ));
9945 Prepend_To (Params, -- null
9946 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
9948 Prepend_To (Params, -- False
9949 New_Occurrence_Of (Standard_False, Loc));
9950 end if;
9952 -- Add the object parameter
9954 Prepend_To (Params, New_Copy_Tree (Concval));
9956 -- Generate:
9957 -- _Disp_Requeue (<Params>);
9959 -- Find entity for Disp_Requeue operation, which belongs to
9960 -- the type and may not be directly visible.
9962 declare
9963 Elmt : Elmt_Id;
9964 Op : Entity_Id := Empty;
9966 begin
9967 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
9968 while Present (Elmt) loop
9969 Op := Node (Elmt);
9970 exit when Chars (Op) = Name_uDisp_Requeue;
9971 Next_Elmt (Elmt);
9972 end loop;
9974 pragma Assert (Present (Op));
9976 return
9977 Make_Procedure_Call_Statement (Loc,
9978 Name => New_Occurrence_Of (Op, Loc),
9979 Parameter_Associations => Params);
9980 end;
9981 end Build_Dispatching_Requeue;
9983 --------------------------------------
9984 -- Build_Dispatching_Requeue_To_Any --
9985 --------------------------------------
9987 function Build_Dispatching_Requeue_To_Any return Node_Id is
9988 Call_Ent : constant Entity_Id := Entity (Ename);
9989 Obj : constant Node_Id := Original_Node (Concval);
9990 Skip : constant Node_Id := Build_Skip_Statement (N);
9991 C : Entity_Id;
9992 Decls : List_Id;
9993 S : Entity_Id;
9994 Stmts : List_Id;
9996 begin
9997 Decls := New_List;
9998 Stmts := New_List;
10000 -- Dispatch table slot processing, generate:
10001 -- S : Integer;
10003 S := Build_S (Loc, Decls);
10005 -- Call kind processing, generate:
10006 -- C : Ada.Tags.Prim_Op_Kind;
10008 C := Build_C (Loc, Decls);
10010 -- Generate:
10011 -- S := Ada.Tags.Get_Offset_Index
10012 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10014 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10016 -- Generate:
10017 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10019 Append_To (Stmts,
10020 Make_Procedure_Call_Statement (Loc,
10021 Name =>
10022 New_Occurrence_Of (
10023 Find_Prim_Op (Etype (Etype (Obj)),
10024 Name_uDisp_Get_Prim_Op_Kind),
10025 Loc),
10026 Parameter_Associations => New_List (
10027 New_Copy_Tree (Obj),
10028 New_Occurrence_Of (S, Loc),
10029 New_Occurrence_Of (C, Loc))));
10031 Append_To (Stmts,
10033 -- if C = POK_Protected_Entry
10034 -- or else C = POK_Task_Entry
10035 -- then
10037 Make_Implicit_If_Statement (N,
10038 Condition =>
10039 Make_Op_Or (Loc,
10040 Left_Opnd =>
10041 Make_Op_Eq (Loc,
10042 Left_Opnd =>
10043 New_Occurrence_Of (C, Loc),
10044 Right_Opnd =>
10045 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10047 Right_Opnd =>
10048 Make_Op_Eq (Loc,
10049 Left_Opnd =>
10050 New_Occurrence_Of (C, Loc),
10051 Right_Opnd =>
10052 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10054 -- Dispatching requeue equivalent
10056 Then_Statements => New_List (
10057 Build_Dispatching_Requeue,
10058 Skip),
10060 -- elsif C = POK_Protected_Procedure then
10062 Elsif_Parts => New_List (
10063 Make_Elsif_Part (Loc,
10064 Condition =>
10065 Make_Op_Eq (Loc,
10066 Left_Opnd =>
10067 New_Occurrence_Of (C, Loc),
10068 Right_Opnd =>
10069 New_Occurrence_Of (
10070 RTE (RE_POK_Protected_Procedure), Loc)),
10072 -- Dispatching call equivalent
10074 Then_Statements => New_List (
10075 Build_Dispatching_Call_Equivalent))),
10077 -- else
10078 -- raise Program_Error;
10079 -- end if;
10081 Else_Statements => New_List (
10082 Make_Raise_Program_Error (Loc,
10083 Reason => PE_Explicit_Raise))));
10085 -- Wrap everything into a block
10087 return
10088 Make_Block_Statement (Loc,
10089 Declarations => Decls,
10090 Handled_Statement_Sequence =>
10091 Make_Handled_Sequence_Of_Statements (Loc,
10092 Statements => Stmts));
10093 end Build_Dispatching_Requeue_To_Any;
10095 --------------------------
10096 -- Build_Normal_Requeue --
10097 --------------------------
10099 function Build_Normal_Requeue return Node_Id is
10100 Params : constant List_Id := New_List;
10101 Param : Node_Id;
10102 RT_Call : Node_Id;
10104 begin
10105 -- Process the "with abort" parameter
10107 Prepend_To (Params,
10108 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10110 -- Add the index expression to the parameters. It is common among all
10111 -- four cases.
10113 Prepend_To (Params,
10114 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10116 if Is_Protected_Type (Old_Typ) then
10117 declare
10118 Self_Param : Node_Id;
10120 begin
10121 Self_Param :=
10122 Make_Attribute_Reference (Loc,
10123 Prefix =>
10124 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10125 Attribute_Name =>
10126 Name_Unchecked_Access);
10128 -- Protected to protected requeue
10130 if Is_Protected_Type (Conc_Typ) then
10131 RT_Call :=
10132 New_Occurrence_Of (
10133 RTE (RE_Requeue_Protected_Entry), Loc);
10135 Param :=
10136 Make_Attribute_Reference (Loc,
10137 Prefix =>
10138 Concurrent_Ref (Concval),
10139 Attribute_Name =>
10140 Name_Unchecked_Access);
10142 -- Protected to task requeue
10144 else pragma Assert (Is_Task_Type (Conc_Typ));
10145 RT_Call :=
10146 New_Occurrence_Of (
10147 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10149 Param := Concurrent_Ref (Concval);
10150 end if;
10152 Prepend_To (Params, Param);
10153 Prepend_To (Params, Self_Param);
10154 end;
10156 else pragma Assert (Is_Task_Type (Old_Typ));
10158 -- Task to protected requeue
10160 if Is_Protected_Type (Conc_Typ) then
10161 RT_Call :=
10162 New_Occurrence_Of (
10163 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10165 Param :=
10166 Make_Attribute_Reference (Loc,
10167 Prefix =>
10168 Concurrent_Ref (Concval),
10169 Attribute_Name =>
10170 Name_Unchecked_Access);
10172 -- Task to task requeue
10174 else pragma Assert (Is_Task_Type (Conc_Typ));
10175 RT_Call :=
10176 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10178 Param := Concurrent_Ref (Concval);
10179 end if;
10181 Prepend_To (Params, Param);
10182 end if;
10184 return
10185 Make_Procedure_Call_Statement (Loc,
10186 Name => RT_Call,
10187 Parameter_Associations => Params);
10188 end Build_Normal_Requeue;
10190 --------------------------
10191 -- Build_Skip_Statement --
10192 --------------------------
10194 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10195 Skip_Stmt : Node_Id;
10197 begin
10198 -- Build a return statement to skip the rest of the entire body
10200 if Is_Protected_Type (Old_Typ) then
10201 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10203 -- If the requeue is within a task, find the end label of the
10204 -- enclosing accept statement and create a goto statement to it.
10206 else
10207 declare
10208 Acc : Node_Id;
10209 Label : Node_Id;
10211 begin
10212 -- Climb the parent chain looking for the enclosing accept
10213 -- statement.
10215 Acc := Parent (Search);
10216 while Present (Acc)
10217 and then Nkind (Acc) /= N_Accept_Statement
10218 loop
10219 Acc := Parent (Acc);
10220 end loop;
10222 -- The last statement is the second label used for completing
10223 -- the rendezvous the usual way. The label we are looking for
10224 -- is right before it.
10226 Label :=
10227 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10229 pragma Assert (Nkind (Label) = N_Label);
10231 -- Generate a goto statement to skip the rest of the accept
10233 Skip_Stmt :=
10234 Make_Goto_Statement (Loc,
10235 Name =>
10236 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10237 end;
10238 end if;
10240 Set_Analyzed (Skip_Stmt);
10242 return Skip_Stmt;
10243 end Build_Skip_Statement;
10245 -- Start of processing for Expand_N_Requeue_Statement
10247 begin
10248 -- Extract the components of the entry call
10250 Extract_Entry (N, Concval, Ename, Index);
10251 Conc_Typ := Etype (Concval);
10253 -- Examine the scope stack in order to find nearest enclosing concurrent
10254 -- type. This will constitute our invocation source.
10256 Old_Typ := Current_Scope;
10257 while Present (Old_Typ)
10258 and then not Is_Concurrent_Type (Old_Typ)
10259 loop
10260 Old_Typ := Scope (Old_Typ);
10261 end loop;
10263 -- Obtain the innermost enclosing callable construct for use in
10264 -- generating a dynamic accessibility check.
10266 Enc_Subp := Current_Scope;
10268 if Ekind (Enc_Subp) not in Entry_Kind | Subprogram_Kind then
10269 Enc_Subp := Enclosing_Subprogram (Enc_Subp);
10270 end if;
10272 -- Generate a dynamic accessibility check on the target object
10274 Insert_Before_And_Analyze (N,
10275 Make_Raise_Program_Error (Loc,
10276 Condition =>
10277 Make_Op_Gt (Loc,
10278 Left_Opnd => Accessibility_Level (Name (N), Dynamic_Level),
10279 Right_Opnd => Make_Integer_Literal (Loc,
10280 Scope_Depth (Enc_Subp))),
10281 Reason => PE_Accessibility_Check_Failed));
10283 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10284 -- Concval.Ename where the type of Concval is class-wide concurrent
10285 -- interface.
10287 if Ada_Version >= Ada_2012
10288 and then Present (Concval)
10289 and then Is_Class_Wide_Type (Conc_Typ)
10290 and then Is_Concurrent_Interface (Conc_Typ)
10291 then
10292 declare
10293 Has_Impl : Boolean := False;
10294 Impl_Kind : Name_Id := No_Name;
10296 begin
10297 -- Check whether the Ename is flagged by pragma Implemented
10299 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10300 Has_Impl := True;
10301 Impl_Kind := Implementation_Kind (Entity (Ename));
10302 end if;
10304 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10305 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10307 if Has_Impl and then Impl_Kind = Name_By_Entry then
10308 Rewrite (N, Build_Dispatching_Requeue);
10309 Analyze (N);
10310 Insert_After (N, Build_Skip_Statement (N));
10312 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10313 -- a protected procedure. In this case the requeue is transformed
10314 -- into a dispatching call.
10316 elsif Has_Impl
10317 and then Impl_Kind = Name_By_Protected_Procedure
10318 then
10319 Rewrite (N, Build_Dispatching_Call_Equivalent);
10320 Analyze (N);
10322 -- The procedure_or_entry_NAME's implementation kind is either
10323 -- By_Any, Optional, or pragma Implemented was not applied at all.
10324 -- In this case a runtime test determines whether Ename denotes an
10325 -- entry or a protected procedure and performs the appropriate
10326 -- call.
10328 else
10329 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10330 Analyze (N);
10331 end if;
10332 end;
10334 -- Processing for regular (nondispatching) requeues
10336 else
10337 Rewrite (N, Build_Normal_Requeue);
10338 Analyze (N);
10339 Insert_After (N, Build_Skip_Statement (N));
10340 end if;
10341 end Expand_N_Requeue_Statement;
10343 -------------------------------
10344 -- Expand_N_Selective_Accept --
10345 -------------------------------
10347 procedure Expand_N_Selective_Accept (N : Node_Id) is
10348 Loc : constant Source_Ptr := Sloc (N);
10349 Alts : constant List_Id := Select_Alternatives (N);
10351 -- Note: in the below declarations a lot of new lists are allocated
10352 -- unconditionally which may well not end up being used. That's not
10353 -- a good idea since it wastes space gratuitously ???
10355 Accept_Case : List_Id;
10356 Accept_List : constant List_Id := New_List;
10358 Alt : Node_Id;
10359 Alt_List : constant List_Id := New_List;
10360 Alt_Stats : List_Id;
10361 Ann : Entity_Id := Empty;
10363 Check_Guard : Boolean := True;
10365 Decls : constant List_Id := New_List;
10366 Stats : constant List_Id := New_List;
10367 Body_List : constant List_Id := New_List;
10368 Trailing_List : constant List_Id := New_List;
10370 Choices : List_Id;
10371 Else_Present : Boolean := False;
10372 Terminate_Alt : Node_Id := Empty;
10373 Select_Mode : Node_Id;
10375 Delay_Case : List_Id;
10376 Delay_Count : Integer := 0;
10377 Delay_Val : Entity_Id;
10378 Delay_Index : Entity_Id;
10379 Delay_Min : Entity_Id;
10380 Delay_Num : Pos := 1;
10381 Delay_Alt_List : List_Id := New_List;
10382 Delay_List : constant List_Id := New_List;
10383 D : Entity_Id;
10384 M : Entity_Id;
10386 First_Delay : Boolean := True;
10387 Guard_Open : Entity_Id;
10389 End_Lab : Node_Id;
10390 Index : Pos := 1;
10391 Lab : Node_Id;
10392 Num_Alts : Nat;
10393 Num_Accept : Nat := 0;
10394 Proc : Node_Id;
10395 Time_Type : Entity_Id := Empty;
10396 Select_Call : Node_Id;
10398 Qnam : constant Entity_Id :=
10399 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10401 Xnam : constant Entity_Id :=
10402 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10404 -----------------------
10405 -- Local subprograms --
10406 -----------------------
10408 function Accept_Or_Raise return List_Id;
10409 -- For the rare case where delay alternatives all have guards, and
10410 -- all of them are closed, it is still possible that there were open
10411 -- accept alternatives with no callers. We must reexamine the
10412 -- Accept_List, and execute a selective wait with no else if some
10413 -- accept is open. If none, we raise program_error.
10415 procedure Add_Accept (Alt : Node_Id);
10416 -- Process a single accept statement in a select alternative. Build
10417 -- procedure for body of accept, and add entry to dispatch table with
10418 -- expression for guard, in preparation for call to run time select.
10420 function Make_And_Declare_Label (Num : Int) return Node_Id;
10421 -- Manufacture a label using Num as a serial number and declare it.
10422 -- The declaration is appended to Decls. The label marks the trailing
10423 -- statements of an accept or delay alternative.
10425 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10426 -- Build call to Selective_Wait runtime routine
10428 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10429 -- Add code to compare value of delay with previous values, and
10430 -- generate case entry for trailing statements.
10432 procedure Process_Accept_Alternative
10433 (Alt : Node_Id;
10434 Index : Int;
10435 Proc : Node_Id);
10436 -- Add code to call corresponding procedure, and branch to
10437 -- trailing statements, if any.
10439 ---------------------
10440 -- Accept_Or_Raise --
10441 ---------------------
10443 function Accept_Or_Raise return List_Id is
10444 Cond : Node_Id;
10445 Stats : List_Id;
10446 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10448 begin
10449 -- We generate the following:
10451 -- for J in q'range loop
10452 -- if q(J).S /=null_task_entry then
10453 -- selective_wait (simple_mode,...);
10454 -- done := True;
10455 -- exit;
10456 -- end if;
10457 -- end loop;
10459 -- if no rendez_vous then
10460 -- raise program_error;
10461 -- end if;
10463 -- Note that the code needs to know that the selector name
10464 -- in an Accept_Alternative is named S.
10466 Cond := Make_Op_Ne (Loc,
10467 Left_Opnd =>
10468 Make_Selected_Component (Loc,
10469 Prefix =>
10470 Make_Indexed_Component (Loc,
10471 Prefix => New_Occurrence_Of (Qnam, Loc),
10472 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10473 Selector_Name => Make_Identifier (Loc, Name_S)),
10474 Right_Opnd =>
10475 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10477 Stats := New_List (
10478 Make_Implicit_Loop_Statement (N,
10479 Iteration_Scheme =>
10480 Make_Iteration_Scheme (Loc,
10481 Loop_Parameter_Specification =>
10482 Make_Loop_Parameter_Specification (Loc,
10483 Defining_Identifier => J,
10484 Discrete_Subtype_Definition =>
10485 Make_Attribute_Reference (Loc,
10486 Prefix => New_Occurrence_Of (Qnam, Loc),
10487 Attribute_Name => Name_Range,
10488 Expressions => New_List (
10489 Make_Integer_Literal (Loc, 1))))),
10491 Statements => New_List (
10492 Make_Implicit_If_Statement (N,
10493 Condition => Cond,
10494 Then_Statements => New_List (
10495 Make_Select_Call (
10496 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10497 Make_Exit_Statement (Loc))))));
10499 Append_To (Stats,
10500 Make_Raise_Program_Error (Loc,
10501 Condition => Make_Op_Eq (Loc,
10502 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10503 Right_Opnd =>
10504 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10505 Reason => PE_All_Guards_Closed));
10507 return Stats;
10508 end Accept_Or_Raise;
10510 ----------------
10511 -- Add_Accept --
10512 ----------------
10514 procedure Add_Accept (Alt : Node_Id) is
10515 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10516 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10517 Eloc : constant Source_Ptr := Sloc (Ename);
10518 Eent : constant Entity_Id := Entity (Ename);
10519 Index : constant Node_Id := Entry_Index (Acc_Stm);
10521 Call : Node_Id;
10522 Expr : Node_Id;
10523 Null_Body : Node_Id;
10524 PB_Ent : Entity_Id;
10525 Proc_Body : Node_Id;
10527 -- Start of processing for Add_Accept
10529 begin
10530 if No (Ann) then
10531 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10532 end if;
10534 if Present (Condition (Alt)) then
10535 Expr :=
10536 Make_If_Expression (Eloc, New_List (
10537 Condition (Alt),
10538 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10539 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10540 else
10541 Expr := Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent));
10542 end if;
10544 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10545 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10547 -- Always add call to Abort_Undefer when generating code, since
10548 -- this is what the runtime expects (abort deferred in
10549 -- Selective_Wait). In CodePeer mode this only confuses the
10550 -- analysis with unknown calls, so don't do it.
10552 if not CodePeer_Mode then
10553 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10554 Insert_Before
10555 (First (Statements (Handled_Statement_Sequence
10556 (Accept_Statement (Alt)))),
10557 Call);
10558 Analyze (Call);
10559 end if;
10561 PB_Ent :=
10562 Make_Defining_Identifier (Eloc,
10563 New_External_Name (Chars (Ename), 'A', Num_Accept));
10565 -- Link the acceptor to the original receiving entry
10567 Mutate_Ekind (PB_Ent, E_Procedure);
10568 Set_Receiving_Entry (PB_Ent, Eent);
10570 if Comes_From_Source (Alt) then
10571 Set_Debug_Info_Needed (PB_Ent);
10572 end if;
10574 Proc_Body :=
10575 Make_Subprogram_Body (Eloc,
10576 Specification =>
10577 Make_Procedure_Specification (Eloc,
10578 Defining_Unit_Name => PB_Ent),
10579 Declarations => Declarations (Acc_Stm),
10580 Handled_Statement_Sequence =>
10581 Build_Accept_Body (Accept_Statement (Alt)));
10583 Reset_Scopes_To (Proc_Body, PB_Ent);
10585 -- During the analysis of the body of the accept statement, any
10586 -- zero cost exception handler records were collected in the
10587 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10588 -- This is where we move them to where they belong, namely the
10589 -- newly created procedure.
10591 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10592 Append (Proc_Body, Body_List);
10594 else
10595 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10597 -- if accept statement has declarations, insert above, given that
10598 -- we are not creating a body for the accept.
10600 if Present (Declarations (Acc_Stm)) then
10601 Insert_Actions (N, Declarations (Acc_Stm));
10602 end if;
10603 end if;
10605 Append_To (Accept_List,
10606 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10608 Num_Accept := Num_Accept + 1;
10609 end Add_Accept;
10611 ----------------------------
10612 -- Make_And_Declare_Label --
10613 ----------------------------
10615 function Make_And_Declare_Label (Num : Int) return Node_Id is
10616 Lab_Id : Node_Id;
10618 begin
10619 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10620 Lab :=
10621 Make_Label (Loc, Lab_Id);
10623 Append_To (Decls,
10624 Make_Implicit_Label_Declaration (Loc,
10625 Defining_Identifier =>
10626 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10627 Label_Construct => Lab));
10629 return Lab;
10630 end Make_And_Declare_Label;
10632 ----------------------
10633 -- Make_Select_Call --
10634 ----------------------
10636 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10637 Params : constant List_Id := New_List;
10639 begin
10640 Append_To (Params,
10641 Make_Attribute_Reference (Loc,
10642 Prefix => New_Occurrence_Of (Qnam, Loc),
10643 Attribute_Name => Name_Unchecked_Access));
10644 Append_To (Params, Select_Mode);
10645 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10646 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10648 return
10649 Make_Procedure_Call_Statement (Loc,
10650 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10651 Parameter_Associations => Params);
10652 end Make_Select_Call;
10654 --------------------------------
10655 -- Process_Accept_Alternative --
10656 --------------------------------
10658 procedure Process_Accept_Alternative
10659 (Alt : Node_Id;
10660 Index : Int;
10661 Proc : Node_Id)
10663 Astmt : constant Node_Id := Accept_Statement (Alt);
10664 Alt_Stats : List_Id;
10666 begin
10667 Adjust_Condition (Condition (Alt));
10669 -- Accept with body
10671 if Present (Handled_Statement_Sequence (Astmt)) then
10672 Alt_Stats :=
10673 New_List (
10674 Make_Procedure_Call_Statement (Sloc (Proc),
10675 Name =>
10676 New_Occurrence_Of
10677 (Defining_Unit_Name (Specification (Proc)),
10678 Sloc (Proc))));
10680 -- Accept with no body (followed by trailing statements)
10682 else
10683 declare
10684 Entry_Id : constant Entity_Id :=
10685 Entity (Entry_Direct_Name (Accept_Statement (Alt)));
10686 begin
10687 -- Ada 2022 (AI12-0279)
10689 if Has_Yield_Aspect (Entry_Id)
10690 and then RTE_Available (RE_Yield)
10691 then
10692 Alt_Stats :=
10693 New_List (
10694 Make_Procedure_Call_Statement (Sloc (Proc),
10695 New_Occurrence_Of (RTE (RE_Yield), Sloc (Proc))));
10696 else
10697 Alt_Stats := Empty_List;
10698 end if;
10699 end;
10700 end if;
10702 Ensure_Statement_Present (Sloc (Astmt), Alt);
10704 -- After the call, if any, branch to trailing statements, if any.
10705 -- We create a label for each, as well as the corresponding label
10706 -- declaration.
10708 if not Is_Empty_List (Statements (Alt)) then
10709 Lab := Make_And_Declare_Label (Index);
10710 Append (Lab, Trailing_List);
10711 Append_List (Statements (Alt), Trailing_List);
10712 Append_To (Trailing_List,
10713 Make_Goto_Statement (Loc,
10714 Name => New_Copy (Identifier (End_Lab))));
10716 else
10717 Lab := End_Lab;
10718 end if;
10720 Append_To (Alt_Stats,
10721 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10723 Append_To (Alt_List,
10724 Make_Case_Statement_Alternative (Loc,
10725 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10726 Statements => Alt_Stats));
10727 end Process_Accept_Alternative;
10729 -------------------------------
10730 -- Process_Delay_Alternative --
10731 -------------------------------
10733 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10734 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10735 Cond : Node_Id;
10736 Delay_Alt : List_Id;
10738 begin
10739 -- Deal with C/Fortran boolean as delay condition
10741 Adjust_Condition (Condition (Alt));
10743 -- Determine the smallest specified delay
10745 -- for each delay alternative generate:
10747 -- if guard-expression then
10748 -- Delay_Val := delay-expression;
10749 -- Guard_Open := True;
10750 -- if Delay_Val < Delay_Min then
10751 -- Delay_Min := Delay_Val;
10752 -- Delay_Index := Index;
10753 -- end if;
10754 -- end if;
10756 -- The enclosing if-statement is omitted if there is no guard
10758 if Delay_Count = 1 or else First_Delay then
10759 First_Delay := False;
10761 Delay_Alt := New_List (
10762 Make_Assignment_Statement (Loc,
10763 Name => New_Occurrence_Of (Delay_Min, Loc),
10764 Expression => Expression (Delay_Statement (Alt))));
10766 if Delay_Count > 1 then
10767 Append_To (Delay_Alt,
10768 Make_Assignment_Statement (Loc,
10769 Name => New_Occurrence_Of (Delay_Index, Loc),
10770 Expression => Make_Integer_Literal (Loc, Index)));
10771 end if;
10773 else
10774 Delay_Alt := New_List (
10775 Make_Assignment_Statement (Loc,
10776 Name => New_Occurrence_Of (Delay_Val, Loc),
10777 Expression => Expression (Delay_Statement (Alt))));
10779 if Time_Type = Standard_Duration then
10780 Cond :=
10781 Make_Op_Lt (Loc,
10782 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
10783 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
10785 else
10786 -- The scope of the time type must define a comparison
10787 -- operator. The scope itself may not be visible, so we
10788 -- construct a node with entity information to insure that
10789 -- semantic analysis can find the proper operator.
10791 Cond :=
10792 Make_Function_Call (Loc,
10793 Name => Make_Selected_Component (Loc,
10794 Prefix =>
10795 New_Occurrence_Of (Scope (Time_Type), Loc),
10796 Selector_Name =>
10797 Make_Operator_Symbol (Loc,
10798 Chars => Name_Op_Lt,
10799 Strval => No_String)),
10800 Parameter_Associations =>
10801 New_List (
10802 New_Occurrence_Of (Delay_Val, Loc),
10803 New_Occurrence_Of (Delay_Min, Loc)));
10805 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10806 end if;
10808 Append_To (Delay_Alt,
10809 Make_Implicit_If_Statement (N,
10810 Condition => Cond,
10811 Then_Statements => New_List (
10812 Make_Assignment_Statement (Loc,
10813 Name => New_Occurrence_Of (Delay_Min, Loc),
10814 Expression => New_Occurrence_Of (Delay_Val, Loc)),
10816 Make_Assignment_Statement (Loc,
10817 Name => New_Occurrence_Of (Delay_Index, Loc),
10818 Expression => Make_Integer_Literal (Loc, Index)))));
10819 end if;
10821 if Check_Guard then
10822 Append_To (Delay_Alt,
10823 Make_Assignment_Statement (Loc,
10824 Name => New_Occurrence_Of (Guard_Open, Loc),
10825 Expression => New_Occurrence_Of (Standard_True, Loc)));
10826 end if;
10828 if Present (Condition (Alt)) then
10829 Delay_Alt := New_List (
10830 Make_Implicit_If_Statement (N,
10831 Condition => Condition (Alt),
10832 Then_Statements => Delay_Alt));
10833 end if;
10835 Append_List (Delay_Alt, Delay_List);
10837 Ensure_Statement_Present (Dloc, Alt);
10839 -- If the delay alternative has a statement part, add choice to the
10840 -- case statements for delays.
10842 if not Is_Empty_List (Statements (Alt)) then
10844 if Delay_Count = 1 then
10845 Append_List (Statements (Alt), Delay_Alt_List);
10847 else
10848 Append_To (Delay_Alt_List,
10849 Make_Case_Statement_Alternative (Loc,
10850 Discrete_Choices => New_List (
10851 Make_Integer_Literal (Loc, Index)),
10852 Statements => Statements (Alt)));
10853 end if;
10855 elsif Delay_Count = 1 then
10857 -- If the single delay has no trailing statements, add a branch
10858 -- to the exit label to the selective wait.
10860 Delay_Alt_List := New_List (
10861 Make_Goto_Statement (Loc,
10862 Name => New_Copy (Identifier (End_Lab))));
10864 end if;
10865 end Process_Delay_Alternative;
10867 -- Start of processing for Expand_N_Selective_Accept
10869 begin
10870 Process_Statements_For_Controlled_Objects (N);
10872 -- First insert some declarations before the select. The first is:
10874 -- Ann : Address
10876 -- This variable holds the parameters passed to the accept body. This
10877 -- declaration has already been inserted by the time we get here by
10878 -- a call to Expand_Accept_Declarations made from the semantics when
10879 -- processing the first accept statement contained in the select. We
10880 -- can find this entity as Accept_Address (E), where E is any of the
10881 -- entries references by contained accept statements.
10883 -- The first step is to scan the list of Selective_Accept_Statements
10884 -- to find this entity, and also count the number of accepts, and
10885 -- determine if terminated, delay or else is present:
10887 Num_Alts := 0;
10889 Alt := First (Alts);
10890 while Present (Alt) loop
10891 Process_Statements_For_Controlled_Objects (Alt);
10893 if Nkind (Alt) = N_Accept_Alternative then
10894 Add_Accept (Alt);
10896 elsif Nkind (Alt) = N_Delay_Alternative then
10897 Delay_Count := Delay_Count + 1;
10899 -- If the delays are relative delays, the delay expressions have
10900 -- type Standard_Duration. Otherwise they must have some time type
10901 -- recognized by GNAT.
10903 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10904 Time_Type := Standard_Duration;
10905 else
10906 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10908 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10909 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10910 then
10911 null;
10912 else
10913 -- Move this check to sem???
10914 Error_Msg_NE (
10915 "& is not a time type (RM 9.6(6))",
10916 Expression (Delay_Statement (Alt)), Time_Type);
10917 Time_Type := Standard_Duration;
10918 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10919 end if;
10920 end if;
10922 if No (Condition (Alt)) then
10924 -- This guard will always be open
10926 Check_Guard := False;
10927 end if;
10929 elsif Nkind (Alt) = N_Terminate_Alternative then
10930 Adjust_Condition (Condition (Alt));
10931 Terminate_Alt := Alt;
10932 end if;
10934 Num_Alts := Num_Alts + 1;
10935 Next (Alt);
10936 end loop;
10938 Else_Present := Present (Else_Statements (N));
10940 -- At the same time (see procedure Add_Accept) we build the accept list:
10942 -- Qnn : Accept_List (1 .. num-select) := (
10943 -- (null-body, entry-index),
10944 -- (null-body, entry-index),
10945 -- ..
10946 -- (null_body, entry-index));
10948 -- In the above declaration, null-body is True if the corresponding
10949 -- accept has no body, and false otherwise. The entry is either the
10950 -- entry index expression if there is no guard, or if a guard is
10951 -- present, then an if expression of the form:
10953 -- (if guard then entry-index else Null_Task_Entry)
10955 -- If a guard is statically known to be false, the entry can simply
10956 -- be omitted from the accept list.
10958 Append_To (Decls,
10959 Make_Object_Declaration (Loc,
10960 Defining_Identifier => Qnam,
10961 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10962 Aliased_Present => True,
10963 Expression =>
10964 Make_Qualified_Expression (Loc,
10965 Subtype_Mark =>
10966 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
10967 Expression =>
10968 Make_Aggregate (Loc, Expressions => Accept_List))));
10970 -- Then we declare the variable that holds the index for the accept
10971 -- that will be selected for service:
10973 -- Xnn : Select_Index;
10975 Append_To (Decls,
10976 Make_Object_Declaration (Loc,
10977 Defining_Identifier => Xnam,
10978 Object_Definition =>
10979 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
10980 Expression =>
10981 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
10983 -- After this follow procedure declarations for each accept body
10985 -- procedure Pnn is
10986 -- begin
10987 -- ...
10988 -- end;
10990 -- where the ... are statements from the corresponding procedure body.
10991 -- No parameters are involved, since the parameters are passed via Ann
10992 -- and the parameter references have already been expanded to be direct
10993 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
10994 -- any embedded tasking statements (which would normally be illegal in
10995 -- procedures), have been converted to calls to the tasking runtime so
10996 -- there is no problem in putting them into procedures.
10998 -- The original accept statement has been expanded into a block in
10999 -- the same fashion as for simple accepts (see Build_Accept_Body).
11001 -- Note: we don't really need to build these procedures for the case
11002 -- where no delay statement is present, but it is just as easy to
11003 -- build them unconditionally, and not significantly inefficient,
11004 -- since if they are short they will be inlined anyway.
11006 -- The procedure declarations have been assembled in Body_List
11008 -- If delays are present, we must compute the required delay.
11009 -- We first generate the declarations:
11011 -- Delay_Index : Boolean := 0;
11012 -- Delay_Min : Some_Time_Type.Time;
11013 -- Delay_Val : Some_Time_Type.Time;
11015 -- Delay_Index will be set to the index of the minimum delay, i.e. the
11016 -- active delay that is actually chosen as the basis for the possible
11017 -- delay if an immediate rendez-vous is not possible.
11019 -- In the most common case there is a single delay statement, and this
11020 -- is handled specially.
11022 if Delay_Count > 0 then
11024 -- Generate the required declarations
11026 Delay_Val :=
11027 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11028 Delay_Index :=
11029 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11030 Delay_Min :=
11031 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11033 pragma Assert (Present (Time_Type));
11035 Append_To (Decls,
11036 Make_Object_Declaration (Loc,
11037 Defining_Identifier => Delay_Val,
11038 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11040 Append_To (Decls,
11041 Make_Object_Declaration (Loc,
11042 Defining_Identifier => Delay_Index,
11043 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11044 Expression => Make_Integer_Literal (Loc, 0)));
11046 Append_To (Decls,
11047 Make_Object_Declaration (Loc,
11048 Defining_Identifier => Delay_Min,
11049 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11050 Expression =>
11051 Unchecked_Convert_To (Time_Type,
11052 Make_Attribute_Reference (Loc,
11053 Prefix =>
11054 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11055 Attribute_Name => Name_Last))));
11057 -- Create Duration and Delay_Mode objects used for passing a delay
11058 -- value to RTS
11060 D := Make_Temporary (Loc, 'D');
11061 M := Make_Temporary (Loc, 'M');
11063 declare
11064 Discr : Entity_Id;
11066 begin
11067 -- Note that these values are defined in s-osprim.ads and must
11068 -- be kept in sync:
11070 -- Relative : constant := 0;
11071 -- Absolute_Calendar : constant := 1;
11072 -- Absolute_RT : constant := 2;
11074 if Time_Type = Standard_Duration then
11075 Discr := Make_Integer_Literal (Loc, 0);
11077 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11078 Discr := Make_Integer_Literal (Loc, 1);
11080 else
11081 pragma Assert
11082 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11083 Discr := Make_Integer_Literal (Loc, 2);
11084 end if;
11086 Append_To (Decls,
11087 Make_Object_Declaration (Loc,
11088 Defining_Identifier => D,
11089 Object_Definition =>
11090 New_Occurrence_Of (Standard_Duration, Loc)));
11092 Append_To (Decls,
11093 Make_Object_Declaration (Loc,
11094 Defining_Identifier => M,
11095 Object_Definition =>
11096 New_Occurrence_Of (Standard_Integer, Loc),
11097 Expression => Discr));
11098 end;
11100 if Check_Guard then
11101 Guard_Open :=
11102 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11104 Append_To (Decls,
11105 Make_Object_Declaration (Loc,
11106 Defining_Identifier => Guard_Open,
11107 Object_Definition =>
11108 New_Occurrence_Of (Standard_Boolean, Loc),
11109 Expression =>
11110 New_Occurrence_Of (Standard_False, Loc)));
11111 end if;
11113 -- Delay_Count is zero, don't need M and D set (suppress warning)
11115 else
11116 M := Empty;
11117 D := Empty;
11118 end if;
11120 if Present (Terminate_Alt) then
11122 -- If the terminate alternative guard is False, use
11123 -- Simple_Mode; otherwise use Terminate_Mode.
11125 if Present (Condition (Terminate_Alt)) then
11126 Select_Mode := Make_If_Expression (Loc,
11127 New_List (Condition (Terminate_Alt),
11128 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11129 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11130 else
11131 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11132 end if;
11134 elsif Else_Present or Delay_Count > 0 then
11135 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11137 else
11138 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11139 end if;
11141 Select_Call := Make_Select_Call (Select_Mode);
11142 Append (Select_Call, Stats);
11144 -- Now generate code to act on the result. There is an entry
11145 -- in this case for each accept statement with a non-null body,
11146 -- followed by a branch to the statements that follow the Accept.
11147 -- In the absence of delay alternatives, we generate:
11149 -- case X is
11150 -- when No_Rendezvous => -- omitted if simple mode
11151 -- goto Lab0;
11153 -- when 1 =>
11154 -- P1n;
11155 -- goto Lab1;
11157 -- when 2 =>
11158 -- P2n;
11159 -- goto Lab2;
11161 -- when others =>
11162 -- goto Exit;
11163 -- end case;
11165 -- Lab0: Else_Statements;
11166 -- goto exit;
11168 -- Lab1: Trailing_Statements1;
11169 -- goto Exit;
11171 -- Lab2: Trailing_Statements2;
11172 -- goto Exit;
11173 -- ...
11174 -- Exit:
11176 -- Generate label for common exit
11178 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11180 -- First entry is the default case, when no rendezvous is possible
11182 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11184 if Else_Present then
11186 -- If no rendezvous is possible, the else part is executed
11188 Lab := Make_And_Declare_Label (0);
11189 Alt_Stats := New_List (
11190 Make_Goto_Statement (Loc,
11191 Name => New_Copy (Identifier (Lab))));
11193 Append (Lab, Trailing_List);
11194 Append_List (Else_Statements (N), Trailing_List);
11195 Append_To (Trailing_List,
11196 Make_Goto_Statement (Loc,
11197 Name => New_Copy (Identifier (End_Lab))));
11198 else
11199 Alt_Stats := New_List (
11200 Make_Goto_Statement (Loc,
11201 Name => New_Copy (Identifier (End_Lab))));
11202 end if;
11204 Append_To (Alt_List,
11205 Make_Case_Statement_Alternative (Loc,
11206 Discrete_Choices => Choices,
11207 Statements => Alt_Stats));
11209 -- We make use of the fact that Accept_Index is an integer type, and
11210 -- generate successive literals for entries for each accept. Only those
11211 -- for which there is a body or trailing statements get a case entry.
11213 Alt := First (Select_Alternatives (N));
11214 Proc := First (Body_List);
11215 while Present (Alt) loop
11217 if Nkind (Alt) = N_Accept_Alternative then
11218 Process_Accept_Alternative (Alt, Index, Proc);
11219 Index := Index + 1;
11221 if Present
11222 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11223 then
11224 Next (Proc);
11225 end if;
11227 elsif Nkind (Alt) = N_Delay_Alternative then
11228 Process_Delay_Alternative (Alt, Delay_Num);
11229 Delay_Num := Delay_Num + 1;
11230 end if;
11232 Next (Alt);
11233 end loop;
11235 -- An others choice is always added to the main case, as well
11236 -- as the delay case (to satisfy the compiler).
11238 Append_To (Alt_List,
11239 Make_Case_Statement_Alternative (Loc,
11240 Discrete_Choices =>
11241 New_List (Make_Others_Choice (Loc)),
11242 Statements =>
11243 New_List (Make_Goto_Statement (Loc,
11244 Name => New_Copy (Identifier (End_Lab))))));
11246 Accept_Case := New_List (
11247 Make_Case_Statement (Loc,
11248 Expression => New_Occurrence_Of (Xnam, Loc),
11249 Alternatives => Alt_List));
11251 Append_List (Trailing_List, Accept_Case);
11252 Append_List (Body_List, Decls);
11254 -- Construct case statement for trailing statements of delay
11255 -- alternatives, if there are several of them.
11257 if Delay_Count > 1 then
11258 Append_To (Delay_Alt_List,
11259 Make_Case_Statement_Alternative (Loc,
11260 Discrete_Choices =>
11261 New_List (Make_Others_Choice (Loc)),
11262 Statements =>
11263 New_List (Make_Null_Statement (Loc))));
11265 Delay_Case := New_List (
11266 Make_Case_Statement (Loc,
11267 Expression => New_Occurrence_Of (Delay_Index, Loc),
11268 Alternatives => Delay_Alt_List));
11269 else
11270 Delay_Case := Delay_Alt_List;
11271 end if;
11273 -- If there are no delay alternatives, we append the case statement
11274 -- to the statement list.
11276 if Delay_Count = 0 then
11277 Append_List (Accept_Case, Stats);
11279 -- Delay alternatives present
11281 else
11282 -- If delay alternatives are present we generate:
11284 -- find minimum delay.
11285 -- DX := minimum delay;
11286 -- M := <delay mode>;
11287 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11288 -- DX, MX, X);
11290 -- if X = No_Rendezvous then
11291 -- case statement for delay statements.
11292 -- else
11293 -- case statement for accept alternatives.
11294 -- end if;
11296 declare
11297 Cases : Node_Id;
11298 Stmt : Node_Id;
11299 Parms : List_Id;
11300 Parm : Node_Id;
11301 Conv : Node_Id;
11303 begin
11304 -- The type of the delay expression is known to be legal
11306 if Time_Type = Standard_Duration then
11307 Conv := New_Occurrence_Of (Delay_Min, Loc);
11309 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11310 Conv := Make_Function_Call (Loc,
11311 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11312 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11314 else
11315 pragma Assert
11316 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11318 Conv := Make_Function_Call (Loc,
11319 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11320 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11321 end if;
11323 Stmt := Make_Assignment_Statement (Loc,
11324 Name => New_Occurrence_Of (D, Loc),
11325 Expression => Conv);
11327 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11329 Parms := Parameter_Associations (Select_Call);
11331 Parm := First (Parms);
11332 while Present (Parm) and then Parm /= Select_Mode loop
11333 Next (Parm);
11334 end loop;
11336 pragma Assert (Present (Parm));
11337 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11338 Analyze (Parm);
11340 -- Prepare two new parameters of Duration and Delay_Mode type
11341 -- which represent the value and the mode of the minimum delay.
11343 Next (Parm);
11344 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11345 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11347 -- Create a call to RTS
11349 Rewrite (Select_Call,
11350 Make_Procedure_Call_Statement (Loc,
11351 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11352 Parameter_Associations => Parms));
11354 -- This new call should follow the calculation of the minimum
11355 -- delay.
11357 Insert_List_Before (Select_Call, Delay_List);
11359 if Check_Guard then
11360 Stmt :=
11361 Make_Implicit_If_Statement (N,
11362 Condition => New_Occurrence_Of (Guard_Open, Loc),
11363 Then_Statements => New_List (
11364 New_Copy_Tree (Stmt),
11365 New_Copy_Tree (Select_Call)),
11366 Else_Statements => Accept_Or_Raise);
11367 Rewrite (Select_Call, Stmt);
11368 else
11369 Insert_Before (Select_Call, Stmt);
11370 end if;
11372 Cases :=
11373 Make_Implicit_If_Statement (N,
11374 Condition => Make_Op_Eq (Loc,
11375 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11376 Right_Opnd =>
11377 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11379 Then_Statements => Delay_Case,
11380 Else_Statements => Accept_Case);
11382 Append (Cases, Stats);
11383 end;
11384 end if;
11386 Append (End_Lab, Stats);
11388 -- Replace accept statement with appropriate block
11390 Rewrite (N,
11391 Make_Block_Statement (Loc,
11392 Declarations => Decls,
11393 Handled_Statement_Sequence =>
11394 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11395 Analyze (N);
11397 -- Note: have to worry more about abort deferral in above code ???
11399 -- Final step is to unstack the Accept_Address entries for all accept
11400 -- statements appearing in accept alternatives in the select statement
11402 Alt := First (Alts);
11403 while Present (Alt) loop
11404 if Nkind (Alt) = N_Accept_Alternative then
11405 Remove_Last_Elmt (Accept_Address
11406 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11407 end if;
11409 Next (Alt);
11410 end loop;
11411 end Expand_N_Selective_Accept;
11413 -------------------------------------------
11414 -- Expand_N_Single_Protected_Declaration --
11415 -------------------------------------------
11417 -- A single protected declaration should never be present after semantic
11418 -- analysis because it is transformed into a protected type declaration
11419 -- and an accompanying anonymous object. This routine ensures that the
11420 -- transformation takes place.
11422 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11423 begin
11424 raise Program_Error;
11425 end Expand_N_Single_Protected_Declaration;
11427 --------------------------------------
11428 -- Expand_N_Single_Task_Declaration --
11429 --------------------------------------
11431 -- A single task declaration should never be present after semantic
11432 -- analysis because it is transformed into a task type declaration and
11433 -- an accompanying anonymous object. This routine ensures that the
11434 -- transformation takes place.
11436 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11437 begin
11438 raise Program_Error;
11439 end Expand_N_Single_Task_Declaration;
11441 ------------------------
11442 -- Expand_N_Task_Body --
11443 ------------------------
11445 -- Given a task body
11447 -- task body tname is
11448 -- <declarations>
11449 -- begin
11450 -- <statements>
11451 -- end x;
11453 -- This expansion routine converts it into a procedure and sets the
11454 -- elaboration flag for the procedure to true, to represent the fact
11455 -- that the task body is now elaborated:
11457 -- procedure tnameB (_Task : access tnameV) is
11458 -- discriminal : dtype renames _Task.discriminant;
11460 -- procedure _clean is
11461 -- begin
11462 -- Abort_Defer.all;
11463 -- Complete_Task;
11464 -- Abort_Undefer.all;
11465 -- return;
11466 -- end _clean;
11468 -- begin
11469 -- Abort_Undefer.all;
11470 -- <declarations>
11471 -- System.Task_Stages.Complete_Activation;
11472 -- <statements>
11473 -- at end
11474 -- _clean;
11475 -- end tnameB;
11477 -- tnameE := True;
11479 -- In addition, if the task body is an activator, then a call to activate
11480 -- tasks is added at the start of the statements, before the call to
11481 -- Complete_Activation, and if in addition the task is a master then it
11482 -- must be established as a master. These calls are inserted and analyzed
11483 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11484 -- expanded.
11486 -- There is one discriminal declaration line generated for each
11487 -- discriminant that is present to provide an easy reference point for
11488 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11490 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11491 -- task body procedures have a profile (Arg : System.Address). That is
11492 -- needed because GNARLI has to use the same access-to-subprogram type
11493 -- for all task types. We depend here on knowing that in GNAT, passing
11494 -- an address argument by value is identical to passing a record value
11495 -- by access (in either case a single pointer is passed), so even though
11496 -- this procedure has the wrong profile. In fact it's all OK, since the
11497 -- callings sequence is identical.
11499 procedure Expand_N_Task_Body (N : Node_Id) is
11500 Loc : constant Source_Ptr := Sloc (N);
11501 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11502 Call : Node_Id;
11503 New_N : Node_Id;
11505 Insert_Nod : Node_Id;
11506 -- Used to determine the proper location of wrapper body insertions
11508 begin
11509 -- if no task body procedure, means we had an error in configurable
11510 -- run-time mode, and there is no point in proceeding further.
11512 if No (Task_Body_Procedure (Ttyp)) then
11513 return;
11514 end if;
11516 -- Add renaming declarations for discriminals and a declaration for the
11517 -- entry family index (if applicable).
11519 Install_Private_Data_Declarations
11520 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11522 -- Add a call to Abort_Undefer at the very beginning of the task
11523 -- body since this body is called with abort still deferred.
11525 if Abort_Allowed then
11526 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11527 Prepend (Call, Declarations (N));
11528 Analyze (Call);
11529 end if;
11531 -- Place call to Complete_Activation at the head of the statement list.
11533 if Restricted_Profile then
11534 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11535 else
11536 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11537 end if;
11539 Insert_Before
11540 (First (Statements (Handled_Statement_Sequence (N))), Call);
11541 Analyze (Call);
11543 New_N :=
11544 Make_Subprogram_Body (Loc,
11545 Specification => Build_Task_Proc_Specification (Ttyp),
11546 Declarations => Declarations (N),
11547 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11548 Set_Is_Task_Body_Procedure (New_N);
11549 Set_At_End_Proc (New_N, At_End_Proc (N));
11551 -- If the task contains generic instantiations, cleanup actions are
11552 -- delayed until after instantiation. Transfer the activation chain to
11553 -- the subprogram, to insure that the activation call is properly
11554 -- generated. It the task body contains inner tasks, indicate that the
11555 -- subprogram is a task master.
11557 if Delay_Cleanups (Ttyp) then
11558 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11559 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11560 end if;
11562 Rewrite (N, New_N);
11563 Analyze (N);
11565 -- Set elaboration flag immediately after task body. If the body is a
11566 -- subunit, the flag is set in the declarative part containing the stub.
11568 if Nkind (Parent (N)) /= N_Subunit then
11569 Insert_After (N,
11570 Make_Assignment_Statement (Loc,
11571 Name =>
11572 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11573 Expression => New_Occurrence_Of (Standard_True, Loc)));
11574 end if;
11576 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11577 -- the task body. At this point all wrapper specs have been created,
11578 -- frozen and included in the dispatch table for the task type.
11580 if Ada_Version >= Ada_2005 then
11581 if Nkind (Parent (N)) = N_Subunit then
11582 Insert_Nod := Corresponding_Stub (Parent (N));
11583 else
11584 Insert_Nod := N;
11585 end if;
11587 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11588 end if;
11589 end Expand_N_Task_Body;
11591 ------------------------------------
11592 -- Expand_N_Task_Type_Declaration --
11593 ------------------------------------
11595 -- We have several things to do. First we must create a Boolean flag used
11596 -- to mark if the body is elaborated yet. This variable gets set to True
11597 -- when the body of the task is elaborated (we can't rely on the normal
11598 -- ABE mechanism for the task body, since we need to pass an access to
11599 -- this elaboration boolean to the runtime routines).
11601 -- taskE : aliased Boolean := False;
11603 -- Next a variable is declared to hold the task stack size (either the
11604 -- default : Unspecified_Size, or a value that is set by a pragma
11605 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11606 -- the variable is initialized with this value:
11608 -- taskZ : Size_Type := Unspecified_Size;
11609 -- or
11610 -- taskZ : Size_Type := Size_Type (size_expression);
11612 -- Note: No variable is needed to hold the task relative deadline since
11613 -- its value would never be static because the parameter is of a private
11614 -- type (Ada.Real_Time.Time_Span).
11616 -- Next we create a corresponding record type declaration used to represent
11617 -- values of this task. The general form of this type declaration is
11619 -- type taskV (discriminants) is record
11620 -- _Task_Id : Task_Id;
11621 -- entry_family : array (bounds) of Void;
11622 -- _Priority : Integer := priority_expression;
11623 -- _Size : Size_Type := size_expression;
11624 -- _Secondary_Stack_Size : Size_Type := size_expression;
11625 -- _Task_Info : Task_Info_Type := task_info_expression;
11626 -- _CPU : Integer := cpu_range_expression;
11627 -- _Relative_Deadline : Time_Span := time_span_expression;
11628 -- _Domain : Dispatching_Domain := dd_expression;
11629 -- end record;
11631 -- The discriminants are present only if the corresponding task type has
11632 -- discriminants, and they exactly mirror the task type discriminants.
11634 -- The Id field is always present. It contains the Task_Id value, as set by
11635 -- the call to Create_Task. Note that although the task is limited, the
11636 -- task value record type is not limited, so there is no problem in passing
11637 -- this field as an out parameter to Create_Task.
11639 -- One entry_family component is present for each entry family in the task
11640 -- definition. The bounds correspond to the bounds of the entry family
11641 -- (which may depend on discriminants). The element type is void, since we
11642 -- only need the bounds information for determining the entry index. Note
11643 -- that the use of an anonymous array would normally be illegal in this
11644 -- context, but this is a parser check, and the semantics is quite prepared
11645 -- to handle such a case.
11647 -- The _Size field is present only if a Storage_Size pragma appears in the
11648 -- task definition. The expression captures the argument that was present
11649 -- in the pragma, and is used to override the task stack size otherwise
11650 -- associated with the task type.
11652 -- The _Secondary_Stack_Size field is present only the task entity has a
11653 -- Secondary_Stack_Size rep item. It will be filled at the freeze point,
11654 -- when the record init proc is built, to capture the expression of the
11655 -- rep item (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot
11656 -- be filled here since aspect evaluations are delayed till the freeze
11657 -- point.
11659 -- The _Priority field is present only if the task entity has a Priority or
11660 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11661 -- definition clause). It will be filled at the freeze point, when the
11662 -- record init proc is built, to capture the expression of the rep item
11663 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11664 -- here since aspect evaluations are delayed till the freeze point.
11666 -- The _Task_Info field is present only if a Task_Info pragma appears in
11667 -- the task definition. The expression captures the argument that was
11668 -- present in the pragma, and is used to provide the Task_Image parameter
11669 -- to the call to Create_Task.
11671 -- The _CPU field is present only if the task entity has a CPU rep item
11672 -- (pragma, aspect specification or attribute definition clause). It will
11673 -- be filled at the freeze point, when the record init proc is built, to
11674 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11675 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11676 -- are delayed till the freeze point.
11678 -- The _Relative_Deadline field is present only if a Relative_Deadline
11679 -- pragma appears in the task definition. The expression captures the
11680 -- argument that was present in the pragma, and is used to provide the
11681 -- Relative_Deadline parameter to the call to Create_Task.
11683 -- The _Domain field is present only if the task entity has a
11684 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11685 -- definition clause). It will be filled at the freeze point, when the
11686 -- record init proc is built, to capture the expression of the rep item
11687 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11688 -- here since aspect evaluations are delayed till the freeze point.
11690 -- When a task is declared, an instance of the task value record is
11691 -- created. The elaboration of this declaration creates the correct bounds
11692 -- for the entry families, and also evaluates the size, priority, and
11693 -- task_Info expressions if needed. The initialization routine for the task
11694 -- type itself then calls Create_Task with appropriate parameters to
11695 -- initialize the value of the Task_Id field.
11697 -- Note: the address of this record is passed as the "Discriminants"
11698 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11699 -- body procedure, it does not matter that it does not quite match the
11700 -- GNARLI model of what is being passed (the record contains more than just
11701 -- the discriminants, but the discriminants can be found from the record
11702 -- value).
11704 -- The Entity_Id for this created record type is placed in the
11705 -- Corresponding_Record_Type field of the associated task type entity.
11707 -- Next we create a procedure specification for the task body procedure:
11709 -- procedure taskB (_Task : access taskV);
11711 -- Note that this must come after the record type declaration, since
11712 -- the spec refers to this type. It turns out that the initialization
11713 -- procedure for the value type references the task body spec, but that's
11714 -- fine, since it won't be generated till the freeze point for the type,
11715 -- which is certainly after the task body spec declaration.
11717 -- Finally, we set the task index value field of the entry attribute in
11718 -- the case of a simple entry.
11720 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11721 Loc : constant Source_Ptr := Sloc (N);
11722 TaskId : constant Entity_Id := Defining_Identifier (N);
11723 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11724 Tasknm : constant Name_Id := Chars (Tasktyp);
11725 Taskdef : constant Node_Id := Task_Definition (N);
11727 Body_Decl : Node_Id;
11728 Cdecls : List_Id;
11729 Decl_Stack : Node_Id;
11730 Decl_SS : Node_Id;
11731 Elab_Decl : Node_Id;
11732 Ent_Stack : Entity_Id;
11733 Proc_Spec : Node_Id;
11734 Rec_Decl : Node_Id;
11735 Rec_Ent : Entity_Id;
11736 Size_Decl : Entity_Id;
11737 Task_Size : Node_Id;
11739 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11740 -- Searches the task definition T for the first occurrence of the pragma
11741 -- Relative Deadline. The caller has ensured that the pragma is present
11742 -- in the task definition. Note that this routine cannot be implemented
11743 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11744 -- not chained because their expansion into a procedure call statement
11745 -- would cause a break in the chain.
11747 ----------------------------------
11748 -- Get_Relative_Deadline_Pragma --
11749 ----------------------------------
11751 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11752 N : Node_Id;
11754 begin
11755 N := First (Visible_Declarations (T));
11756 while Present (N) loop
11757 if Nkind (N) = N_Pragma
11758 and then Pragma_Name (N) = Name_Relative_Deadline
11759 then
11760 return N;
11761 end if;
11763 Next (N);
11764 end loop;
11766 N := First (Private_Declarations (T));
11767 while Present (N) loop
11768 if Nkind (N) = N_Pragma
11769 and then Pragma_Name (N) = Name_Relative_Deadline
11770 then
11771 return N;
11772 end if;
11774 Next (N);
11775 end loop;
11777 raise Program_Error;
11778 end Get_Relative_Deadline_Pragma;
11780 -- Start of processing for Expand_N_Task_Type_Declaration
11782 begin
11783 -- If already expanded, nothing to do
11785 if Present (Corresponding_Record_Type (Tasktyp)) then
11786 return;
11787 end if;
11789 -- Here we will do the expansion
11791 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11793 Rec_Ent := Defining_Identifier (Rec_Decl);
11794 Cdecls := Component_Items (Component_List
11795 (Type_Definition (Rec_Decl)));
11797 Qualify_Entity_Names (N);
11799 -- First create the elaboration variable
11801 Elab_Decl :=
11802 Make_Object_Declaration (Loc,
11803 Defining_Identifier =>
11804 Make_Defining_Identifier (Sloc (Tasktyp),
11805 Chars => New_External_Name (Tasknm, 'E')),
11806 Aliased_Present => True,
11807 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
11808 Expression => New_Occurrence_Of (Standard_False, Loc));
11810 Insert_After (N, Elab_Decl);
11812 -- Next create the declaration of the size variable (tasknmZ)
11814 Set_Storage_Size_Variable (Tasktyp,
11815 Make_Defining_Identifier (Sloc (Tasktyp),
11816 Chars => New_External_Name (Tasknm, 'Z')));
11818 if Present (Taskdef)
11819 and then Has_Storage_Size_Pragma (Taskdef)
11820 and then
11821 Is_OK_Static_Expression
11822 (Expression
11823 (First (Pragma_Argument_Associations
11824 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11825 then
11826 Size_Decl :=
11827 Make_Object_Declaration (Loc,
11828 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11829 Object_Definition =>
11830 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11831 Expression =>
11832 Convert_To (RTE (RE_Size_Type),
11833 Relocate_Node
11834 (Expression (First (Pragma_Argument_Associations
11835 (Get_Rep_Pragma
11836 (TaskId, Name_Storage_Size)))))));
11838 else
11839 Size_Decl :=
11840 Make_Object_Declaration (Loc,
11841 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11842 Object_Definition =>
11843 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11844 Expression =>
11845 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
11846 end if;
11848 Insert_After (Elab_Decl, Size_Decl);
11850 -- Next build the rest of the corresponding record declaration. This is
11851 -- done last, since the corresponding record initialization procedure
11852 -- will reference the previously created entities.
11854 -- Fill in the component declarations -- first the _Task_Id field
11856 Append_To (Cdecls,
11857 Make_Component_Declaration (Loc,
11858 Defining_Identifier =>
11859 Make_Defining_Identifier (Loc, Name_uTask_Id),
11860 Component_Definition =>
11861 Make_Component_Definition (Loc,
11862 Aliased_Present => False,
11863 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
11864 Loc))));
11866 -- Declare static ATCB (that is, created by the expander) if we are
11867 -- using the Restricted run time.
11869 if Restricted_Profile then
11870 Append_To (Cdecls,
11871 Make_Component_Declaration (Loc,
11872 Defining_Identifier =>
11873 Make_Defining_Identifier (Loc, Name_uATCB),
11875 Component_Definition =>
11876 Make_Component_Definition (Loc,
11877 Aliased_Present => True,
11878 Subtype_Indication => Make_Subtype_Indication (Loc,
11879 Subtype_Mark =>
11880 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11882 Constraint =>
11883 Make_Index_Or_Discriminant_Constraint (Loc,
11884 Constraints =>
11885 New_List (Make_Integer_Literal (Loc, 0)))))));
11887 end if;
11889 -- Declare static stack (that is, created by the expander) if we are
11890 -- using the Restricted run time on a bare board configuration.
11892 if Restricted_Profile and then Preallocated_Stacks_On_Target then
11894 -- First we need to extract the appropriate stack size
11896 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11898 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11899 declare
11900 Expr_N : constant Node_Id :=
11901 Expression (First (
11902 Pragma_Argument_Associations (
11903 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11904 Etyp : constant Entity_Id := Etype (Expr_N);
11905 P : constant Node_Id := Parent (Expr_N);
11907 begin
11908 -- The stack is defined inside the corresponding record.
11909 -- Therefore if the size of the stack is set by means of
11910 -- a discriminant, we must reference the discriminant of the
11911 -- corresponding record type.
11913 if Nkind (Expr_N) in N_Has_Entity
11914 and then Present (Discriminal_Link (Entity (Expr_N)))
11915 then
11916 Task_Size :=
11917 New_Occurrence_Of
11918 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11919 Loc);
11920 Set_Parent (Task_Size, P);
11921 Set_Etype (Task_Size, Etyp);
11922 Set_Analyzed (Task_Size);
11924 else
11925 Task_Size := New_Copy_Tree (Expr_N);
11926 end if;
11927 end;
11929 else
11930 Task_Size :=
11931 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
11932 end if;
11934 Decl_Stack := Make_Component_Declaration (Loc,
11935 Defining_Identifier => Ent_Stack,
11937 Component_Definition =>
11938 Make_Component_Definition (Loc,
11939 Aliased_Present => True,
11940 Subtype_Indication => Make_Subtype_Indication (Loc,
11941 Subtype_Mark =>
11942 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
11944 Constraint =>
11945 Make_Index_Or_Discriminant_Constraint (Loc,
11946 Constraints => New_List (Make_Range (Loc,
11947 Low_Bound => Make_Integer_Literal (Loc, 1),
11948 High_Bound => Convert_To (RTE (RE_Storage_Offset),
11949 Task_Size)))))));
11951 Append_To (Cdecls, Decl_Stack);
11953 -- The appropriate alignment for the stack is ensured by the run-time
11954 -- code in charge of task creation.
11956 end if;
11958 -- Declare a static secondary stack if the conditions for a statically
11959 -- generated stack are met.
11961 if Create_Secondary_Stack_For_Task (TaskId) then
11962 declare
11963 Size_Expr : constant Node_Id :=
11964 Expression (First (
11965 Pragma_Argument_Associations (
11966 Get_Rep_Pragma (TaskId,
11967 Name_Secondary_Stack_Size))));
11969 Stack_Size : Node_Id;
11971 begin
11972 -- The secondary stack is defined inside the corresponding
11973 -- record. Therefore if the size of the stack is set by means
11974 -- of a discriminant, we must reference the discriminant of the
11975 -- corresponding record type.
11977 if Nkind (Size_Expr) in N_Has_Entity
11978 and then Present (Discriminal_Link (Entity (Size_Expr)))
11979 then
11980 Stack_Size :=
11981 New_Occurrence_Of
11982 (CR_Discriminant (Discriminal_Link (Entity (Size_Expr))),
11983 Loc);
11984 Set_Parent (Stack_Size, Parent (Size_Expr));
11985 Set_Etype (Stack_Size, Etype (Size_Expr));
11986 Set_Analyzed (Stack_Size);
11988 else
11989 Stack_Size := New_Copy_Tree (Size_Expr);
11990 end if;
11992 -- Create the secondary stack for the task
11994 Decl_SS :=
11995 Make_Component_Declaration (Loc,
11996 Defining_Identifier =>
11997 Make_Defining_Identifier (Loc, Name_uSecondary_Stack),
11998 Component_Definition =>
11999 Make_Component_Definition (Loc,
12000 Aliased_Present => True,
12001 Subtype_Indication =>
12002 Make_Subtype_Indication (Loc,
12003 Subtype_Mark =>
12004 New_Occurrence_Of (RTE (RE_SS_Stack), Loc),
12005 Constraint =>
12006 Make_Index_Or_Discriminant_Constraint (Loc,
12007 Constraints => New_List (
12008 Convert_To (RTE (RE_Size_Type),
12009 Stack_Size))))));
12011 Append_To (Cdecls, Decl_SS);
12012 end;
12013 end if;
12015 -- Add components for entry families
12017 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
12019 -- Add the _Priority component if a Interrupt_Priority or Priority rep
12020 -- item is present.
12022 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
12023 Append_To (Cdecls,
12024 Make_Component_Declaration (Loc,
12025 Defining_Identifier =>
12026 Make_Defining_Identifier (Loc, Name_uPriority),
12027 Component_Definition =>
12028 Make_Component_Definition (Loc,
12029 Aliased_Present => False,
12030 Subtype_Indication =>
12031 New_Occurrence_Of (Standard_Integer, Loc))));
12032 end if;
12034 -- Add the _Size component if a Storage_Size pragma is present
12036 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12037 Append_To (Cdecls,
12038 Make_Component_Declaration (Loc,
12039 Defining_Identifier =>
12040 Make_Defining_Identifier (Loc, Name_uSize),
12042 Component_Definition =>
12043 Make_Component_Definition (Loc,
12044 Aliased_Present => False,
12045 Subtype_Indication =>
12046 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
12048 Expression =>
12049 Convert_To (RTE (RE_Size_Type),
12050 New_Copy_Tree (
12051 Expression (First (
12052 Pragma_Argument_Associations (
12053 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
12054 end if;
12056 -- Add the _Secondary_Stack_Size component if a Secondary_Stack_Size
12057 -- pragma is present.
12059 if Has_Rep_Pragma
12060 (TaskId, Name_Secondary_Stack_Size, Check_Parents => False)
12061 then
12062 Append_To (Cdecls,
12063 Make_Component_Declaration (Loc,
12064 Defining_Identifier =>
12065 Make_Defining_Identifier (Loc, Name_uSecondary_Stack_Size),
12067 Component_Definition =>
12068 Make_Component_Definition (Loc,
12069 Aliased_Present => False,
12070 Subtype_Indication =>
12071 New_Occurrence_Of (RTE (RE_Size_Type), Loc))));
12072 end if;
12074 -- Add the _Task_Info component if a Task_Info pragma is present
12076 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
12077 Append_To (Cdecls,
12078 Make_Component_Declaration (Loc,
12079 Defining_Identifier =>
12080 Make_Defining_Identifier (Loc, Name_uTask_Info),
12082 Component_Definition =>
12083 Make_Component_Definition (Loc,
12084 Aliased_Present => False,
12085 Subtype_Indication =>
12086 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12088 Expression => New_Copy (
12089 Expression (First (
12090 Pragma_Argument_Associations (
12091 Get_Rep_Pragma
12092 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12093 end if;
12095 -- Add the _CPU component if a CPU rep item is present
12097 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12098 Append_To (Cdecls,
12099 Make_Component_Declaration (Loc,
12100 Defining_Identifier =>
12101 Make_Defining_Identifier (Loc, Name_uCPU),
12103 Component_Definition =>
12104 Make_Component_Definition (Loc,
12105 Aliased_Present => False,
12106 Subtype_Indication =>
12107 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12108 end if;
12110 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12111 -- present. If we are using a restricted run time this component will
12112 -- not be added (deadlines are not allowed by the Ravenscar profile),
12113 -- unless the task dispatching policy is EDF (for GNAT_Ravenscar_EDF
12114 -- profile).
12116 if (not Restricted_Profile or else Task_Dispatching_Policy = 'E')
12117 and then Present (Taskdef)
12118 and then Has_Relative_Deadline_Pragma (Taskdef)
12119 then
12120 Append_To (Cdecls,
12121 Make_Component_Declaration (Loc,
12122 Defining_Identifier =>
12123 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12125 Component_Definition =>
12126 Make_Component_Definition (Loc,
12127 Aliased_Present => False,
12128 Subtype_Indication =>
12129 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12131 Expression =>
12132 Convert_To (RTE (RE_Time_Span),
12133 New_Copy_Tree (
12134 Expression (First (
12135 Pragma_Argument_Associations (
12136 Get_Relative_Deadline_Pragma (Taskdef))))))));
12137 end if;
12139 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12140 -- item is present. If we are using a restricted run time this component
12141 -- will not be added (dispatching domains are not allowed by the
12142 -- Ravenscar profile).
12144 if not Restricted_Profile
12145 and then
12146 Has_Rep_Item
12147 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12148 then
12149 Append_To (Cdecls,
12150 Make_Component_Declaration (Loc,
12151 Defining_Identifier =>
12152 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12154 Component_Definition =>
12155 Make_Component_Definition (Loc,
12156 Aliased_Present => False,
12157 Subtype_Indication =>
12158 New_Occurrence_Of
12159 (RTE (RE_Dispatching_Domain_Access), Loc))));
12160 end if;
12162 Insert_After (Size_Decl, Rec_Decl);
12164 -- Analyze the record declaration immediately after construction,
12165 -- because the initialization procedure is needed for single task
12166 -- declarations before the next entity is analyzed.
12168 Analyze (Rec_Decl);
12170 -- Create the declaration of the task body procedure
12172 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12173 Body_Decl :=
12174 Make_Subprogram_Declaration (Loc,
12175 Specification => Proc_Spec);
12176 Set_Is_Task_Body_Procedure (Body_Decl);
12178 Insert_After (Rec_Decl, Body_Decl);
12180 -- The subprogram does not comes from source, so we have to indicate the
12181 -- need for debugging information explicitly.
12183 if Comes_From_Source (Original_Node (N)) then
12184 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12185 end if;
12187 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12188 -- the corresponding record has been frozen.
12190 if Ada_Version >= Ada_2005 then
12191 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12192 end if;
12194 -- Ada 2005 (AI-345): We must defer freezing to allow further
12195 -- declaration of primitive subprograms covering task interfaces
12197 if Ada_Version <= Ada_95 then
12199 -- Now we can freeze the corresponding record. This needs manually
12200 -- freezing, since it is really part of the task type, and the task
12201 -- type is frozen at this stage. We of course need the initialization
12202 -- procedure for this corresponding record type and we won't get it
12203 -- in time if we don't freeze now.
12205 Insert_List_After (Body_Decl, List => Freeze_Entity (Rec_Ent, N));
12206 end if;
12208 -- Complete the expansion of access types to the current task type, if
12209 -- any were declared.
12211 Expand_Previous_Access_Type (Tasktyp);
12213 -- Create wrappers for entries that have contract cases, preconditions
12214 -- and postconditions.
12216 declare
12217 Ent : Entity_Id;
12219 begin
12220 Ent := First_Entity (Tasktyp);
12221 while Present (Ent) loop
12222 if Ekind (Ent) in E_Entry | E_Entry_Family then
12223 Build_Entry_Contract_Wrapper (Ent, N);
12224 end if;
12226 Next_Entity (Ent);
12227 end loop;
12228 end;
12229 end Expand_N_Task_Type_Declaration;
12231 -------------------------------
12232 -- Expand_N_Timed_Entry_Call --
12233 -------------------------------
12235 -- A timed entry call in normal case is not implemented using ATC mechanism
12236 -- anymore for efficiency reason.
12238 -- select
12239 -- T.E;
12240 -- S1;
12241 -- or
12242 -- delay D;
12243 -- S2;
12244 -- end select;
12246 -- is expanded as follows:
12248 -- 1) When T.E is a task entry_call;
12250 -- declare
12251 -- B : Boolean;
12252 -- X : Task_Entry_Index := <entry index>;
12253 -- DX : Duration := To_Duration (D);
12254 -- M : Delay_Mode := <discriminant>;
12255 -- P : parms := (parm, parm, parm);
12257 -- begin
12258 -- Timed_Protected_Entry_Call
12259 -- (<acceptor-task>, X, P'Address, DX, M, B);
12260 -- if B then
12261 -- S1;
12262 -- else
12263 -- S2;
12264 -- end if;
12265 -- end;
12267 -- 2) When T.E is a protected entry_call;
12269 -- declare
12270 -- B : Boolean;
12271 -- X : Protected_Entry_Index := <entry index>;
12272 -- DX : Duration := To_Duration (D);
12273 -- M : Delay_Mode := <discriminant>;
12274 -- P : parms := (parm, parm, parm);
12276 -- begin
12277 -- Timed_Protected_Entry_Call
12278 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12279 -- if B then
12280 -- S1;
12281 -- else
12282 -- S2;
12283 -- end if;
12284 -- end;
12286 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12287 -- is no delay and the triggering statements are executed. We first
12288 -- determine the kind of the triggering call and then execute a
12289 -- synchronized operation or a direct call.
12291 -- declare
12292 -- B : Boolean := False;
12293 -- C : Ada.Tags.Prim_Op_Kind;
12294 -- DX : Duration := To_Duration (D)
12295 -- K : Ada.Tags.Tagged_Kind :=
12296 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12297 -- M : Integer :=...;
12298 -- P : Parameters := (Param1 .. ParamN);
12299 -- S : Integer;
12301 -- begin
12302 -- if K = Ada.Tags.TK_Limited_Tagged
12303 -- or else K = Ada.Tags.TK_Tagged
12304 -- then
12305 -- <dispatching-call>;
12306 -- B := True;
12308 -- else
12309 -- S :=
12310 -- Ada.Tags.Get_Offset_Index
12311 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12313 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12315 -- if C = POK_Protected_Entry
12316 -- or else C = POK_Task_Entry
12317 -- then
12318 -- Param1 := P.Param1;
12319 -- ...
12320 -- ParamN := P.ParamN;
12321 -- end if;
12323 -- if B then
12324 -- if C = POK_Procedure
12325 -- or else C = POK_Protected_Procedure
12326 -- or else C = POK_Task_Procedure
12327 -- then
12328 -- <dispatching-call>;
12329 -- end if;
12330 -- end if;
12331 -- end if;
12333 -- if B then
12334 -- <triggering-statements>
12335 -- else
12336 -- <timed-statements>
12337 -- end if;
12338 -- end;
12340 -- The triggering statement and the sequence of timed statements have not
12341 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12342 -- global references if within an instantiation.
12344 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12345 Actuals : List_Id;
12346 Blk_Typ : Entity_Id;
12347 Call : Node_Id;
12348 Call_Ent : Entity_Id;
12349 Conc_Typ_Stmts : List_Id;
12350 Concval : Node_Id := Empty; -- init to avoid warning
12351 D_Alt : constant Node_Id := Delay_Alternative (N);
12352 D_Conv : Node_Id;
12353 D_Disc : Node_Id;
12354 D_Stat : Node_Id := Delay_Statement (D_Alt);
12355 D_Stats : List_Id;
12356 D_Type : Entity_Id;
12357 Decls : List_Id;
12358 Dummy : Node_Id;
12359 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12360 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12361 E_Stats : List_Id;
12362 Ename : Node_Id;
12363 Formals : List_Id;
12364 Index : Node_Id;
12365 Is_Disp_Select : Boolean;
12366 Lim_Typ_Stmts : List_Id;
12367 Loc : constant Source_Ptr := Sloc (D_Stat);
12368 N_Stats : List_Id;
12369 Obj : Entity_Id;
12370 Param : Node_Id;
12371 Params : List_Id;
12372 Stmt : Node_Id;
12373 Stmts : List_Id;
12374 Unpack : List_Id;
12376 B : Entity_Id; -- Call status flag
12377 C : Entity_Id; -- Call kind
12378 D : Entity_Id; -- Delay
12379 K : Entity_Id; -- Tagged kind
12380 M : Entity_Id; -- Delay mode
12381 P : Entity_Id; -- Parameter block
12382 S : Entity_Id; -- Primitive operation slot
12384 -- Start of processing for Expand_N_Timed_Entry_Call
12386 begin
12387 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12388 -- was already reported on spec, so do not attempt to expand the call.
12390 if Restriction_Active (No_Select_Statements) then
12391 return;
12392 end if;
12394 Process_Statements_For_Controlled_Objects (E_Alt);
12395 Process_Statements_For_Controlled_Objects (D_Alt);
12397 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12399 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12400 -- may wrap them in blocks.
12402 E_Stats := Statements (E_Alt);
12403 D_Stats := Statements (D_Alt);
12405 -- The arguments in the call may require dynamic allocation, and the
12406 -- call statement may have been transformed into a block. The block
12407 -- may contain additional declarations for internal entities, and the
12408 -- original call is found by sequential search.
12410 if Nkind (E_Call) = N_Block_Statement then
12411 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12412 while Nkind (E_Call) not in
12413 N_Procedure_Call_Statement | N_Entry_Call_Statement
12414 loop
12415 Next (E_Call);
12416 end loop;
12417 end if;
12419 Is_Disp_Select :=
12420 Ada_Version >= Ada_2005
12421 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12423 if Is_Disp_Select then
12424 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12425 Decls := New_List;
12427 Stmts := New_List;
12429 -- Generate:
12430 -- B : Boolean := False;
12432 B := Build_B (Loc, Decls);
12434 -- Generate:
12435 -- C : Ada.Tags.Prim_Op_Kind;
12437 C := Build_C (Loc, Decls);
12439 -- Because the analysis of all statements was disabled, manually
12440 -- analyze the delay statement.
12442 Analyze (D_Stat);
12443 D_Stat := Original_Node (D_Stat);
12445 else
12446 -- Build an entry call using Simple_Entry_Call
12448 Extract_Entry (E_Call, Concval, Ename, Index);
12449 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12451 Decls := Declarations (E_Call);
12452 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12454 if No (Decls) then
12455 Decls := New_List;
12456 end if;
12458 -- Generate:
12459 -- B : Boolean;
12461 B := Make_Defining_Identifier (Loc, Name_uB);
12463 Prepend_To (Decls,
12464 Make_Object_Declaration (Loc,
12465 Defining_Identifier => B,
12466 Object_Definition =>
12467 New_Occurrence_Of (Standard_Boolean, Loc)));
12468 end if;
12470 -- Duration and mode processing
12472 D_Type := Base_Type (Etype (Expression (D_Stat)));
12474 -- Use the type of the delay expression (Calendar or Real_Time) to
12475 -- generate the appropriate conversion.
12477 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12478 D_Disc := Make_Integer_Literal (Loc, 0);
12479 D_Conv := Relocate_Node (Expression (D_Stat));
12481 elsif Is_RTE (D_Type, RO_CA_Time) then
12482 D_Disc := Make_Integer_Literal (Loc, 1);
12483 D_Conv :=
12484 Make_Function_Call (Loc,
12485 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12486 Parameter_Associations =>
12487 New_List (New_Copy (Expression (D_Stat))));
12489 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12490 D_Disc := Make_Integer_Literal (Loc, 2);
12491 D_Conv :=
12492 Make_Function_Call (Loc,
12493 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12494 Parameter_Associations =>
12495 New_List (New_Copy (Expression (D_Stat))));
12496 end if;
12498 D := Make_Temporary (Loc, 'D');
12500 -- Generate:
12501 -- D : Duration;
12503 Append_To (Decls,
12504 Make_Object_Declaration (Loc,
12505 Defining_Identifier => D,
12506 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12508 M := Make_Temporary (Loc, 'M');
12510 -- Generate:
12511 -- M : Integer := (0 | 1 | 2);
12513 Append_To (Decls,
12514 Make_Object_Declaration (Loc,
12515 Defining_Identifier => M,
12516 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12517 Expression => D_Disc));
12519 -- Parameter block processing
12521 -- Manually create the parameter block for dispatching calls. In the
12522 -- case of entries, the block has already been created during the call
12523 -- to Build_Simple_Entry_Call.
12525 if Is_Disp_Select then
12527 -- Compute the delay at this stage because the evaluation of its
12528 -- expression must not occur earlier (see ACVC C97302A).
12530 Append_To (Stmts,
12531 Make_Assignment_Statement (Loc,
12532 Name => New_Occurrence_Of (D, Loc),
12533 Expression => D_Conv));
12535 -- Tagged kind processing, generate:
12536 -- K : Ada.Tags.Tagged_Kind :=
12537 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12539 K := Build_K (Loc, Decls, Obj);
12541 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12542 P :=
12543 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12545 -- Dispatch table slot processing, generate:
12546 -- S : Integer;
12548 S := Build_S (Loc, Decls);
12550 -- Generate:
12551 -- S := Ada.Tags.Get_Offset_Index
12552 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12554 Conc_Typ_Stmts :=
12555 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12557 -- Generate:
12558 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12560 -- where Obj is the controlling formal parameter, S is the dispatch
12561 -- table slot number of the dispatching operation, P is the wrapped
12562 -- parameter block, D is the duration, M is the duration mode, C is
12563 -- the call kind and B is the call status.
12565 Params := New_List;
12567 Append_To (Params, New_Copy_Tree (Obj));
12568 Append_To (Params, New_Occurrence_Of (S, Loc));
12569 Append_To (Params,
12570 Make_Attribute_Reference (Loc,
12571 Prefix => New_Occurrence_Of (P, Loc),
12572 Attribute_Name => Name_Address));
12573 Append_To (Params, New_Occurrence_Of (D, Loc));
12574 Append_To (Params, New_Occurrence_Of (M, Loc));
12575 Append_To (Params, New_Occurrence_Of (C, Loc));
12576 Append_To (Params, New_Occurrence_Of (B, Loc));
12578 Append_To (Conc_Typ_Stmts,
12579 Make_Procedure_Call_Statement (Loc,
12580 Name =>
12581 New_Occurrence_Of
12582 (Find_Prim_Op
12583 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12584 Parameter_Associations => Params));
12586 -- Generate:
12587 -- if C = POK_Protected_Entry
12588 -- or else C = POK_Task_Entry
12589 -- then
12590 -- Param1 := P.Param1;
12591 -- ...
12592 -- ParamN := P.ParamN;
12593 -- end if;
12595 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12597 -- Generate the if statement only when the packed parameters need
12598 -- explicit assignments to their corresponding actuals.
12600 if Present (Unpack) then
12601 Append_To (Conc_Typ_Stmts,
12602 Make_Implicit_If_Statement (N,
12604 Condition =>
12605 Make_Or_Else (Loc,
12606 Left_Opnd =>
12607 Make_Op_Eq (Loc,
12608 Left_Opnd => New_Occurrence_Of (C, Loc),
12609 Right_Opnd =>
12610 New_Occurrence_Of
12611 (RTE (RE_POK_Protected_Entry), Loc)),
12613 Right_Opnd =>
12614 Make_Op_Eq (Loc,
12615 Left_Opnd => New_Occurrence_Of (C, Loc),
12616 Right_Opnd =>
12617 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12619 Then_Statements => Unpack));
12620 end if;
12622 -- Generate:
12624 -- if B then
12625 -- if C = POK_Procedure
12626 -- or else C = POK_Protected_Procedure
12627 -- or else C = POK_Task_Procedure
12628 -- then
12629 -- <dispatching-call>
12630 -- end if;
12631 -- end if;
12633 N_Stats := New_List (
12634 Make_Implicit_If_Statement (N,
12635 Condition =>
12636 Make_Or_Else (Loc,
12637 Left_Opnd =>
12638 Make_Op_Eq (Loc,
12639 Left_Opnd => New_Occurrence_Of (C, Loc),
12640 Right_Opnd =>
12641 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12643 Right_Opnd =>
12644 Make_Or_Else (Loc,
12645 Left_Opnd =>
12646 Make_Op_Eq (Loc,
12647 Left_Opnd => New_Occurrence_Of (C, Loc),
12648 Right_Opnd =>
12649 New_Occurrence_Of (RTE (
12650 RE_POK_Protected_Procedure), Loc)),
12651 Right_Opnd =>
12652 Make_Op_Eq (Loc,
12653 Left_Opnd => New_Occurrence_Of (C, Loc),
12654 Right_Opnd =>
12655 New_Occurrence_Of
12656 (RTE (RE_POK_Task_Procedure), Loc)))),
12658 Then_Statements => New_List (E_Call)));
12660 Append_To (Conc_Typ_Stmts,
12661 Make_Implicit_If_Statement (N,
12662 Condition => New_Occurrence_Of (B, Loc),
12663 Then_Statements => N_Stats));
12665 -- Generate:
12666 -- <dispatching-call>;
12667 -- B := True;
12669 Lim_Typ_Stmts :=
12670 New_List (New_Copy_Tree (E_Call),
12671 Make_Assignment_Statement (Loc,
12672 Name => New_Occurrence_Of (B, Loc),
12673 Expression => New_Occurrence_Of (Standard_True, Loc)));
12675 -- Generate:
12676 -- if K = Ada.Tags.TK_Limited_Tagged
12677 -- or else K = Ada.Tags.TK_Tagged
12678 -- then
12679 -- Lim_Typ_Stmts
12680 -- else
12681 -- Conc_Typ_Stmts
12682 -- end if;
12684 Append_To (Stmts,
12685 Make_Implicit_If_Statement (N,
12686 Condition => Build_Dispatching_Tag_Check (K, N),
12687 Then_Statements => Lim_Typ_Stmts,
12688 Else_Statements => Conc_Typ_Stmts));
12690 -- Generate:
12692 -- if B then
12693 -- <triggering-statements>
12694 -- else
12695 -- <timed-statements>
12696 -- end if;
12698 Append_To (Stmts,
12699 Make_Implicit_If_Statement (N,
12700 Condition => New_Occurrence_Of (B, Loc),
12701 Then_Statements => E_Stats,
12702 Else_Statements => D_Stats));
12704 else
12705 -- Simple case of a nondispatching trigger. Skip assignments to
12706 -- temporaries created for in-out parameters.
12708 -- This makes unwarranted assumptions about the shape of the expanded
12709 -- tree for the call, and should be cleaned up ???
12711 Stmt := First (Stmts);
12712 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12713 Next (Stmt);
12714 end loop;
12716 -- Compute the delay at this stage because the evaluation of
12717 -- its expression must not occur earlier (see ACVC C97302A).
12719 Insert_Before (Stmt,
12720 Make_Assignment_Statement (Loc,
12721 Name => New_Occurrence_Of (D, Loc),
12722 Expression => D_Conv));
12724 Call := Stmt;
12725 Params := Parameter_Associations (Call);
12727 -- For a protected type, we build a Timed_Protected_Entry_Call
12729 if Is_Protected_Type (Etype (Concval)) then
12731 -- Create a new call statement
12733 Param := First (Params);
12734 while Present (Param)
12735 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12736 loop
12737 Next (Param);
12738 end loop;
12740 Dummy := Remove_Next (Next (Param));
12742 -- Remove garbage is following the Cancel_Param if present
12744 Dummy := Next (Param);
12746 -- Remove the mode of the Protected_Entry_Call call, then remove
12747 -- the Communication_Block of the Protected_Entry_Call call, and
12748 -- finally add Duration and a Delay_Mode parameter
12750 pragma Assert (Present (Param));
12751 Rewrite (Param, New_Occurrence_Of (D, Loc));
12753 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12755 -- Add a Boolean flag for successful entry call
12757 Append_To (Params, New_Occurrence_Of (B, Loc));
12759 case Corresponding_Runtime_Package (Etype (Concval)) is
12760 when System_Tasking_Protected_Objects_Entries =>
12761 Rewrite (Call,
12762 Make_Procedure_Call_Statement (Loc,
12763 Name =>
12764 New_Occurrence_Of
12765 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12766 Parameter_Associations => Params));
12768 when others =>
12769 raise Program_Error;
12770 end case;
12772 -- For the task case, build a Timed_Task_Entry_Call
12774 else
12775 -- Create a new call statement
12777 Append_To (Params, New_Occurrence_Of (D, Loc));
12778 Append_To (Params, New_Occurrence_Of (M, Loc));
12779 Append_To (Params, New_Occurrence_Of (B, Loc));
12781 Rewrite (Call,
12782 Make_Procedure_Call_Statement (Loc,
12783 Name =>
12784 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
12785 Parameter_Associations => Params));
12786 end if;
12788 Append_To (Stmts,
12789 Make_Implicit_If_Statement (N,
12790 Condition => New_Occurrence_Of (B, Loc),
12791 Then_Statements => E_Stats,
12792 Else_Statements => D_Stats));
12793 end if;
12795 Rewrite (N,
12796 Make_Block_Statement (Loc,
12797 Declarations => Decls,
12798 Handled_Statement_Sequence =>
12799 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12801 Analyze (N);
12803 -- Some items in Decls used to be in the N_Block in E_Call that is
12804 -- constructed in Expand_Entry_Call, and are now in the new Block
12805 -- into which N has been rewritten. Adjust their scopes to reflect that.
12807 if Nkind (E_Call) = N_Block_Statement then
12808 Obj := First_Entity (Entity (Identifier (E_Call)));
12809 while Present (Obj) loop
12810 Set_Scope (Obj, Entity (Identifier (N)));
12811 Next_Entity (Obj);
12812 end loop;
12813 end if;
12815 Reset_Scopes_To (N, Entity (Identifier (N)));
12816 end Expand_N_Timed_Entry_Call;
12818 ----------------------------------------
12819 -- Expand_Protected_Body_Declarations --
12820 ----------------------------------------
12822 procedure Expand_Protected_Body_Declarations
12823 (N : Node_Id;
12824 Spec_Id : Entity_Id)
12826 begin
12827 if No_Run_Time_Mode then
12828 Error_Msg_CRT ("protected body", N);
12829 return;
12831 elsif Expander_Active then
12833 -- Associate discriminals with the first subprogram or entry body to
12834 -- be expanded.
12836 if Present (First_Protected_Operation (Declarations (N))) then
12837 Set_Discriminals (Parent (Spec_Id));
12838 end if;
12839 end if;
12840 end Expand_Protected_Body_Declarations;
12842 -------------------------
12843 -- External_Subprogram --
12844 -------------------------
12846 function External_Subprogram (E : Entity_Id) return Entity_Id is
12847 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12849 begin
12850 -- The internal and external subprograms follow each other on the entity
12851 -- chain. Note that previously private operations had no separate
12852 -- external subprogram. We now create one in all cases, because a
12853 -- private operation may actually appear in an external call, through
12854 -- a 'Access reference used for a callback.
12856 -- If the operation is a function that returns an anonymous access type,
12857 -- the corresponding itype appears before the operation, and must be
12858 -- skipped.
12860 -- This mechanism is fragile, there should be a real link between the
12861 -- two versions of the operation, but there is no place to put it ???
12863 if Is_Access_Type (Next_Entity (Subp)) then
12864 return Next_Entity (Next_Entity (Subp));
12865 else
12866 return Next_Entity (Subp);
12867 end if;
12868 end External_Subprogram;
12870 ------------------------------
12871 -- Extract_Dispatching_Call --
12872 ------------------------------
12874 procedure Extract_Dispatching_Call
12875 (N : Node_Id;
12876 Call_Ent : out Entity_Id;
12877 Object : out Entity_Id;
12878 Actuals : out List_Id;
12879 Formals : out List_Id)
12881 Call_Nam : Node_Id;
12883 begin
12884 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12886 if Present (Original_Node (N)) then
12887 Call_Nam := Name (Original_Node (N));
12888 else
12889 Call_Nam := Name (N);
12890 end if;
12892 -- Retrieve the name of the dispatching procedure. It contains the
12893 -- dispatch table slot number.
12895 loop
12896 case Nkind (Call_Nam) is
12897 when N_Identifier =>
12898 exit;
12900 when N_Selected_Component =>
12901 Call_Nam := Selector_Name (Call_Nam);
12903 when others =>
12904 raise Program_Error;
12905 end case;
12906 end loop;
12908 Actuals := Parameter_Associations (N);
12909 Call_Ent := Entity (Call_Nam);
12910 Formals := Parameter_Specifications (Parent (Call_Ent));
12911 Object := First (Actuals);
12913 if Present (Original_Node (Object)) then
12914 Object := Original_Node (Object);
12915 end if;
12917 -- If the type of the dispatching object is an access type then return
12918 -- an explicit dereference of a copy of the object, and note that this
12919 -- is the controlling actual of the call.
12921 if Is_Access_Type (Etype (Object)) then
12922 Object :=
12923 Make_Explicit_Dereference (Sloc (N), New_Copy_Tree (Object));
12924 Analyze (Object);
12925 Set_Is_Controlling_Actual (Object);
12926 end if;
12927 end Extract_Dispatching_Call;
12929 -------------------
12930 -- Extract_Entry --
12931 -------------------
12933 procedure Extract_Entry
12934 (N : Node_Id;
12935 Concval : out Node_Id;
12936 Ename : out Node_Id;
12937 Index : out Node_Id)
12939 Nam : constant Node_Id := Name (N);
12941 begin
12942 -- For a simple entry, the name is a selected component, with the
12943 -- prefix being the task value, and the selector being the entry.
12945 if Nkind (Nam) = N_Selected_Component then
12946 Concval := Prefix (Nam);
12947 Ename := Selector_Name (Nam);
12948 Index := Empty;
12950 -- For a member of an entry family, the name is an indexed component
12951 -- where the prefix is a selected component, whose prefix in turn is
12952 -- the task value, and whose selector is the entry family. The single
12953 -- expression in the expressions list of the indexed component is the
12954 -- subscript for the family.
12956 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
12957 Concval := Prefix (Prefix (Nam));
12958 Ename := Selector_Name (Prefix (Nam));
12959 Index := First (Expressions (Nam));
12960 end if;
12962 -- Through indirection, the type may actually be a limited view of a
12963 -- concurrent type. When compiling a call, the non-limited view of the
12964 -- type is visible.
12966 if From_Limited_With (Etype (Concval)) then
12967 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
12968 end if;
12969 end Extract_Entry;
12971 -------------------
12972 -- Family_Offset --
12973 -------------------
12975 function Family_Offset
12976 (Loc : Source_Ptr;
12977 Hi : Node_Id;
12978 Lo : Node_Id;
12979 Ttyp : Entity_Id;
12980 Cap : Boolean) return Node_Id
12982 Ityp : Entity_Id;
12983 Real_Hi : Node_Id;
12984 Real_Lo : Node_Id;
12986 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
12987 -- If one of the bounds is a reference to a discriminant, replace with
12988 -- corresponding discriminal of type. Within the body of a task retrieve
12989 -- the renamed discriminant by simple visibility, using its generated
12990 -- name. Within a protected object, find the original discriminant and
12991 -- replace it with the discriminal of the current protected operation.
12993 ------------------------------
12994 -- Convert_Discriminant_Ref --
12995 ------------------------------
12997 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
12998 Loc : constant Source_Ptr := Sloc (Bound);
12999 B : Node_Id;
13000 D : Entity_Id;
13002 begin
13003 if Is_Entity_Name (Bound)
13004 and then Ekind (Entity (Bound)) = E_Discriminant
13005 then
13006 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
13007 B := Make_Identifier (Loc, Chars (Entity (Bound)));
13008 Find_Direct_Name (B);
13010 elsif Is_Protected_Type (Ttyp) then
13011 D := First_Discriminant (Ttyp);
13012 while Chars (D) /= Chars (Entity (Bound)) loop
13013 Next_Discriminant (D);
13014 end loop;
13016 B := New_Occurrence_Of (Discriminal (D), Loc);
13018 else
13019 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
13020 end if;
13022 elsif Nkind (Bound) = N_Attribute_Reference then
13023 return Bound;
13025 else
13026 B := New_Copy_Tree (Bound);
13027 end if;
13029 return
13030 Make_Attribute_Reference (Loc,
13031 Attribute_Name => Name_Pos,
13032 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
13033 Expressions => New_List (B));
13034 end Convert_Discriminant_Ref;
13036 -- Start of processing for Family_Offset
13038 begin
13039 Real_Hi := Convert_Discriminant_Ref (Hi);
13040 Real_Lo := Convert_Discriminant_Ref (Lo);
13042 if Cap then
13043 if Is_Task_Type (Ttyp) then
13044 Ityp := RTE (RE_Task_Entry_Index);
13045 else
13046 Ityp := RTE (RE_Protected_Entry_Index);
13047 end if;
13049 Real_Hi :=
13050 Make_Attribute_Reference (Loc,
13051 Prefix => New_Occurrence_Of (Ityp, Loc),
13052 Attribute_Name => Name_Min,
13053 Expressions => New_List (
13054 Real_Hi,
13055 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
13057 Real_Lo :=
13058 Make_Attribute_Reference (Loc,
13059 Prefix => New_Occurrence_Of (Ityp, Loc),
13060 Attribute_Name => Name_Max,
13061 Expressions => New_List (
13062 Real_Lo,
13063 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
13064 end if;
13066 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
13067 end Family_Offset;
13069 -----------------
13070 -- Family_Size --
13071 -----------------
13073 function Family_Size
13074 (Loc : Source_Ptr;
13075 Hi : Node_Id;
13076 Lo : Node_Id;
13077 Ttyp : Entity_Id;
13078 Cap : Boolean) return Node_Id
13080 Ityp : Entity_Id;
13082 begin
13083 if Is_Task_Type (Ttyp) then
13084 Ityp := RTE (RE_Task_Entry_Index);
13085 else
13086 Ityp := RTE (RE_Protected_Entry_Index);
13087 end if;
13089 return
13090 Make_Attribute_Reference (Loc,
13091 Prefix => New_Occurrence_Of (Ityp, Loc),
13092 Attribute_Name => Name_Max,
13093 Expressions => New_List (
13094 Make_Op_Add (Loc,
13095 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13096 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13097 Make_Integer_Literal (Loc, 0)));
13098 end Family_Size;
13100 ----------------------------
13101 -- Find_Enclosing_Context --
13102 ----------------------------
13104 procedure Find_Enclosing_Context
13105 (N : Node_Id;
13106 Context : out Node_Id;
13107 Context_Id : out Entity_Id;
13108 Context_Decls : out List_Id)
13110 begin
13111 -- Traverse the parent chain looking for an enclosing body, block,
13112 -- package or return statement.
13114 Context := Parent (N);
13115 while Present (Context) loop
13116 if Nkind (Context) in N_Entry_Body
13117 | N_Extended_Return_Statement
13118 | N_Package_Body
13119 | N_Package_Declaration
13120 | N_Subprogram_Body
13121 | N_Task_Body
13122 then
13123 exit;
13125 -- Do not consider block created to protect a list of statements with
13126 -- an Abort_Defer / Abort_Undefer_Direct pair.
13128 elsif Nkind (Context) = N_Block_Statement
13129 and then not Is_Abort_Block (Context)
13130 then
13131 exit;
13132 end if;
13134 Context := Parent (Context);
13135 end loop;
13137 pragma Assert (Present (Context));
13139 -- Extract the constituents of the context
13141 if Nkind (Context) = N_Extended_Return_Statement then
13142 Context_Decls := Return_Object_Declarations (Context);
13143 Context_Id := Return_Statement_Entity (Context);
13145 -- Package declarations and bodies use a common library-level activation
13146 -- chain or task master, therefore return the package declaration as the
13147 -- proper carrier for the appropriate flag.
13149 elsif Nkind (Context) = N_Package_Body then
13150 Context_Decls := Declarations (Context);
13151 Context_Id := Corresponding_Spec (Context);
13152 Context := Parent (Context_Id);
13154 if Nkind (Context) = N_Defining_Program_Unit_Name then
13155 Context := Parent (Parent (Context));
13156 else
13157 Context := Parent (Context);
13158 end if;
13160 elsif Nkind (Context) = N_Package_Declaration then
13161 Context_Decls := Visible_Declarations (Specification (Context));
13162 Context_Id := Defining_Unit_Name (Specification (Context));
13164 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13165 Context_Id := Defining_Identifier (Context_Id);
13166 end if;
13168 else
13169 if Nkind (Context) = N_Block_Statement then
13170 Context_Id := Entity (Identifier (Context));
13172 if No (Declarations (Context)) then
13173 Set_Declarations (Context, New_List);
13174 end if;
13176 elsif Nkind (Context) = N_Entry_Body then
13177 Context_Id := Defining_Identifier (Context);
13179 elsif Nkind (Context) = N_Subprogram_Body then
13180 if Present (Corresponding_Spec (Context)) then
13181 Context_Id := Corresponding_Spec (Context);
13182 else
13183 Context_Id := Defining_Unit_Name (Specification (Context));
13185 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13186 Context_Id := Defining_Identifier (Context_Id);
13187 end if;
13188 end if;
13190 elsif Nkind (Context) = N_Task_Body then
13191 Context_Id := Corresponding_Spec (Context);
13193 else
13194 raise Program_Error;
13195 end if;
13197 Context_Decls := Declarations (Context);
13198 end if;
13200 pragma Assert (Present (Context_Id));
13201 pragma Assert (Present (Context_Decls));
13202 end Find_Enclosing_Context;
13204 -----------------------
13205 -- Find_Master_Scope --
13206 -----------------------
13208 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13209 S : Entity_Id;
13211 begin
13212 -- In Ada 2005, the master is the innermost enclosing scope that is not
13213 -- transient. If the enclosing block is the rewriting of a call or the
13214 -- scope is an extended return statement this is valid master. The
13215 -- master in an extended return is only used within the return, and is
13216 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13217 -- now before that overwriting occurs.
13219 S := Scope (E);
13221 if Ada_Version >= Ada_2005 then
13222 while Is_Internal (S) loop
13223 if Nkind (Parent (S)) = N_Block_Statement
13224 and then Has_Master_Entity (S)
13225 then
13226 exit;
13228 elsif Ekind (S) = E_Return_Statement then
13229 exit;
13231 else
13232 S := Scope (S);
13233 end if;
13234 end loop;
13235 end if;
13237 return S;
13238 end Find_Master_Scope;
13240 -------------------------------
13241 -- First_Protected_Operation --
13242 -------------------------------
13244 function First_Protected_Operation (D : List_Id) return Node_Id is
13245 First_Op : Node_Id;
13247 begin
13248 First_Op := First (D);
13249 while Present (First_Op)
13250 and then Nkind (First_Op) not in N_Subprogram_Body | N_Entry_Body
13251 loop
13252 Next (First_Op);
13253 end loop;
13255 return First_Op;
13256 end First_Protected_Operation;
13258 ---------------------------------------
13259 -- Install_Private_Data_Declarations --
13260 ---------------------------------------
13262 procedure Install_Private_Data_Declarations
13263 (Loc : Source_Ptr;
13264 Spec_Id : Entity_Id;
13265 Conc_Typ : Entity_Id;
13266 Body_Nod : Node_Id;
13267 Decls : List_Id;
13268 Barrier : Boolean := False;
13269 Family : Boolean := False)
13271 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13272 Decl : Node_Id;
13273 Def : Node_Id;
13274 Insert_Node : Node_Id := Empty;
13275 Obj_Ent : Entity_Id;
13277 procedure Add (Decl : Node_Id);
13278 -- Add a single declaration after Insert_Node. If this is the first
13279 -- addition, Decl is added to the front of Decls and it becomes the
13280 -- insertion node.
13282 function Replace_Bound (Bound : Node_Id) return Node_Id;
13283 -- The bounds of an entry index may depend on discriminants, create a
13284 -- reference to the corresponding prival. Otherwise return a duplicate
13285 -- of the original bound.
13287 ---------
13288 -- Add --
13289 ---------
13291 procedure Add (Decl : Node_Id) is
13292 begin
13293 if No (Insert_Node) then
13294 Prepend_To (Decls, Decl);
13295 else
13296 Insert_After (Insert_Node, Decl);
13297 end if;
13299 Insert_Node := Decl;
13300 end Add;
13302 -------------------
13303 -- Replace_Bound --
13304 -------------------
13306 function Replace_Bound (Bound : Node_Id) return Node_Id is
13307 begin
13308 if Nkind (Bound) = N_Identifier
13309 and then Is_Discriminal (Entity (Bound))
13310 then
13311 return Make_Identifier (Loc, Chars (Entity (Bound)));
13312 else
13313 return Duplicate_Subexpr (Bound);
13314 end if;
13315 end Replace_Bound;
13317 -- Start of processing for Install_Private_Data_Declarations
13319 begin
13320 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13321 -- formal parameter _O, _object or _task depending on the context.
13323 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13325 -- Special processing of _O for barrier functions, protected entries
13326 -- and families.
13328 if Barrier
13329 or else
13330 (Is_Protected
13331 and then
13332 (Ekind (Spec_Id) = E_Entry
13333 or else Ekind (Spec_Id) = E_Entry_Family))
13334 then
13335 declare
13336 Conc_Rec : constant Entity_Id :=
13337 Corresponding_Record_Type (Conc_Typ);
13338 Typ_Id : constant Entity_Id :=
13339 Make_Defining_Identifier (Loc,
13340 New_External_Name (Chars (Conc_Rec), 'P'));
13341 begin
13342 -- Generate:
13343 -- type prot_typVP is access prot_typV;
13345 Decl :=
13346 Make_Full_Type_Declaration (Loc,
13347 Defining_Identifier => Typ_Id,
13348 Type_Definition =>
13349 Make_Access_To_Object_Definition (Loc,
13350 Subtype_Indication =>
13351 New_Occurrence_Of (Conc_Rec, Loc)));
13352 Add (Decl);
13354 -- Generate:
13355 -- _object : prot_typVP := prot_typV (_O);
13357 Decl :=
13358 Make_Object_Declaration (Loc,
13359 Defining_Identifier =>
13360 Make_Defining_Identifier (Loc, Name_uObject),
13361 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13362 Expression =>
13363 Unchecked_Convert_To (Typ_Id,
13364 New_Occurrence_Of (Obj_Ent, Loc)));
13365 Add (Decl);
13367 -- Set the reference to the concurrent object
13369 Obj_Ent := Defining_Identifier (Decl);
13370 end;
13371 end if;
13373 -- Step 2: Create the Protection object and build its declaration for
13374 -- any protected entry (family) of subprogram. Note for the lock-free
13375 -- implementation, the Protection object is not needed anymore.
13377 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13378 declare
13379 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13380 Prot_Typ : RE_Id;
13382 begin
13383 Set_Protection_Object (Spec_Id, Prot_Ent);
13385 -- Determine the proper protection type
13387 if Has_Attach_Handler (Conc_Typ)
13388 and then not Restricted_Profile
13389 then
13390 Prot_Typ := RE_Static_Interrupt_Protection;
13392 elsif Has_Interrupt_Handler (Conc_Typ)
13393 and then not Restriction_Active (No_Dynamic_Attachment)
13394 then
13395 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13397 else
13398 case Corresponding_Runtime_Package (Conc_Typ) is
13399 when System_Tasking_Protected_Objects_Entries =>
13400 Prot_Typ := RE_Protection_Entries;
13402 when System_Tasking_Protected_Objects_Single_Entry =>
13403 Prot_Typ := RE_Protection_Entry;
13405 when System_Tasking_Protected_Objects =>
13406 Prot_Typ := RE_Protection;
13408 when others =>
13409 raise Program_Error;
13410 end case;
13411 end if;
13413 -- Generate:
13414 -- conc_typR : protection_typ renames _object._object;
13416 Decl :=
13417 Make_Object_Renaming_Declaration (Loc,
13418 Defining_Identifier => Prot_Ent,
13419 Subtype_Mark =>
13420 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13421 Name =>
13422 Make_Selected_Component (Loc,
13423 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13424 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13426 Add (Decl);
13427 end;
13428 end if;
13430 -- Step 3: Add discriminant renamings (if any)
13432 if Has_Discriminants (Conc_Typ) then
13433 declare
13434 D : Entity_Id;
13436 begin
13437 D := First_Discriminant (Conc_Typ);
13438 while Present (D) loop
13440 -- Adjust the source location
13442 Set_Sloc (Discriminal (D), Loc);
13444 -- Generate:
13445 -- discr_name : discr_typ renames _object.discr_name;
13446 -- or
13447 -- discr_name : discr_typ renames _task.discr_name;
13449 Decl :=
13450 Make_Object_Renaming_Declaration (Loc,
13451 Defining_Identifier => Discriminal (D),
13452 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13453 Name =>
13454 Make_Selected_Component (Loc,
13455 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13456 Selector_Name => Make_Identifier (Loc, Chars (D))));
13458 Add (Decl);
13460 -- Set debug info needed on this renaming declaration even
13461 -- though it does not come from source, so that the debugger
13462 -- will get the right information for these generated names.
13464 Set_Debug_Info_Needed (Discriminal (D));
13466 Next_Discriminant (D);
13467 end loop;
13468 end;
13469 end if;
13471 -- Step 4: Add private component renamings (if any)
13473 if Is_Protected then
13474 Def := Protected_Definition (Parent (Conc_Typ));
13476 if Present (Private_Declarations (Def)) then
13477 declare
13478 Comp : Node_Id;
13479 Comp_Id : Entity_Id;
13480 Decl_Id : Entity_Id;
13481 Nam : Name_Id;
13483 begin
13484 Comp := First (Private_Declarations (Def));
13485 while Present (Comp) loop
13486 if Nkind (Comp) = N_Component_Declaration then
13487 Comp_Id := Defining_Identifier (Comp);
13488 Nam := Chars (Comp_Id);
13489 Decl_Id := Make_Defining_Identifier (Sloc (Comp_Id), Nam);
13491 -- Minimal decoration
13493 if Ekind (Spec_Id) = E_Function then
13494 Mutate_Ekind (Decl_Id, E_Constant);
13495 else
13496 Mutate_Ekind (Decl_Id, E_Variable);
13497 end if;
13499 Set_Prival (Comp_Id, Decl_Id);
13500 Set_Prival_Link (Decl_Id, Comp_Id);
13501 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13502 Set_Is_Independent (Decl_Id, Is_Independent (Comp_Id));
13504 -- Copy the Comes_From_Source flag of the component, as
13505 -- the renaming may be the only entity directly seen by
13506 -- the user in the context, but do not warn for it.
13508 Set_Comes_From_Source
13509 (Decl_Id, Comes_From_Source (Comp_Id));
13510 Set_Warnings_Off (Decl_Id);
13512 -- Generate:
13513 -- comp_name : comp_typ renames _object.comp_name;
13515 Decl :=
13516 Make_Object_Renaming_Declaration (Loc,
13517 Defining_Identifier => Decl_Id,
13518 Subtype_Mark =>
13519 New_Occurrence_Of (Etype (Comp_Id), Loc),
13520 Name =>
13521 Make_Selected_Component (Loc,
13522 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13523 Selector_Name => Make_Identifier (Loc, Nam)));
13525 Add (Decl);
13526 end if;
13528 Next (Comp);
13529 end loop;
13530 end;
13531 end if;
13532 end if;
13534 -- Step 5: Add the declaration of the entry index and the associated
13535 -- type for barrier functions and entry families.
13537 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13538 declare
13539 E : constant Entity_Id := Index_Object (Spec_Id);
13540 Index : constant Entity_Id :=
13541 Defining_Identifier
13542 (Entry_Index_Specification
13543 (Entry_Body_Formal_Part (Body_Nod)));
13544 Index_Con : constant Entity_Id :=
13545 Make_Defining_Identifier (Loc, Chars (Index));
13546 High : Node_Id;
13547 Index_Typ : Entity_Id;
13548 Low : Node_Id;
13550 begin
13551 -- Minimal decoration
13553 Mutate_Ekind (Index_Con, E_Constant);
13554 Set_Entry_Index_Constant (Index, Index_Con);
13555 Set_Discriminal_Link (Index_Con, Index);
13557 -- Retrieve the bounds of the entry family
13559 High := Type_High_Bound (Etype (Index));
13560 Low := Type_Low_Bound (Etype (Index));
13562 -- In the simple case the entry family is given by a subtype mark
13563 -- and the index constant has the same type.
13565 if Is_Entity_Name (Original_Node (
13566 Discrete_Subtype_Definition (Parent (Index))))
13567 then
13568 Index_Typ := Etype (Index);
13570 -- Otherwise a new subtype declaration is required
13572 else
13573 High := Replace_Bound (High);
13574 Low := Replace_Bound (Low);
13576 Index_Typ := Make_Temporary (Loc, 'J');
13578 -- Generate:
13579 -- subtype Jnn is <Etype of Index> range Low .. High;
13581 Decl :=
13582 Make_Subtype_Declaration (Loc,
13583 Defining_Identifier => Index_Typ,
13584 Subtype_Indication =>
13585 Make_Subtype_Indication (Loc,
13586 Subtype_Mark =>
13587 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13588 Constraint =>
13589 Make_Range_Constraint (Loc,
13590 Range_Expression =>
13591 Make_Range (Loc, Low, High))));
13592 Add (Decl);
13593 end if;
13595 Set_Etype (Index_Con, Index_Typ);
13597 -- Create the object which designates the index:
13598 -- J : constant Jnn :=
13599 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13601 -- where Jnn is the subtype created above or the original type of
13602 -- the index, _E is a formal of the protected body subprogram and
13603 -- <index expr> is the index of the first family member.
13605 Decl :=
13606 Make_Object_Declaration (Loc,
13607 Defining_Identifier => Index_Con,
13608 Constant_Present => True,
13609 Object_Definition =>
13610 New_Occurrence_Of (Index_Typ, Loc),
13612 Expression =>
13613 Make_Attribute_Reference (Loc,
13614 Prefix =>
13615 New_Occurrence_Of (Index_Typ, Loc),
13616 Attribute_Name => Name_Val,
13618 Expressions => New_List (
13620 Make_Op_Add (Loc,
13621 Left_Opnd =>
13622 Make_Op_Subtract (Loc,
13623 Left_Opnd => New_Occurrence_Of (E, Loc),
13624 Right_Opnd =>
13625 Entry_Index_Expression (Loc,
13626 Defining_Identifier (Body_Nod),
13627 Empty, Conc_Typ)),
13629 Right_Opnd =>
13630 Make_Attribute_Reference (Loc,
13631 Prefix =>
13632 New_Occurrence_Of (Index_Typ, Loc),
13633 Attribute_Name => Name_Pos,
13634 Expressions => New_List (
13635 Make_Attribute_Reference (Loc,
13636 Prefix =>
13637 New_Occurrence_Of (Index_Typ, Loc),
13638 Attribute_Name => Name_First)))))));
13639 Add (Decl);
13640 end;
13641 end if;
13642 end Install_Private_Data_Declarations;
13644 ---------------------------------
13645 -- Is_Potentially_Large_Family --
13646 ---------------------------------
13648 function Is_Potentially_Large_Family
13649 (Base_Index : Entity_Id;
13650 Conctyp : Entity_Id;
13651 Lo : Node_Id;
13652 Hi : Node_Id) return Boolean
13654 begin
13655 return Scope (Base_Index) = Standard_Standard
13656 and then Base_Index = Base_Type (Standard_Integer)
13657 and then Has_Defaulted_Discriminants (Conctyp)
13658 and then
13659 (Denotes_Discriminant (Lo, True)
13660 or else
13661 Denotes_Discriminant (Hi, True));
13662 end Is_Potentially_Large_Family;
13664 -------------------------------------
13665 -- Is_Private_Primitive_Subprogram --
13666 -------------------------------------
13668 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13669 begin
13670 return
13671 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13672 and then Is_Private_Primitive (Id);
13673 end Is_Private_Primitive_Subprogram;
13675 ------------------
13676 -- Index_Object --
13677 ------------------
13679 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13680 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13681 Formal : Entity_Id;
13683 begin
13684 Formal := First_Formal (Bod_Subp);
13685 while Present (Formal) loop
13687 -- Look for formal parameter _E
13689 if Chars (Formal) = Name_uE then
13690 return Formal;
13691 end if;
13693 Next_Formal (Formal);
13694 end loop;
13696 -- A protected body subprogram should always have the parameter in
13697 -- question.
13699 raise Program_Error;
13700 end Index_Object;
13702 --------------------------------
13703 -- Make_Initialize_Protection --
13704 --------------------------------
13706 function Make_Initialize_Protection
13707 (Protect_Rec : Entity_Id) return List_Id
13709 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13710 P_Arr : Entity_Id;
13711 Pdec : Node_Id;
13712 Ptyp : constant Node_Id :=
13713 Corresponding_Concurrent_Type (Protect_Rec);
13714 Args : List_Id;
13715 L : constant List_Id := New_List;
13716 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13717 Prio_Type : Entity_Id;
13718 Prio_Var : Entity_Id := Empty;
13719 Restricted : constant Boolean := Restricted_Profile;
13721 begin
13722 -- We may need two calls to properly initialize the object, one to
13723 -- Initialize_Protection, and possibly one to Install_Handlers if we
13724 -- have a pragma Attach_Handler.
13726 -- Get protected declaration. In the case of a task type declaration,
13727 -- this is simply the parent of the protected type entity. In the single
13728 -- protected object declaration, this parent will be the implicit type,
13729 -- and we can find the corresponding single protected object declaration
13730 -- by searching forward in the declaration list in the tree.
13732 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13733 -- of this type should have been removed during semantic analysis.
13735 Pdec := Parent (Ptyp);
13736 while Nkind (Pdec) not in
13737 N_Protected_Type_Declaration | N_Single_Protected_Declaration
13738 loop
13739 Next (Pdec);
13740 end loop;
13742 -- Build the parameter list for the call. Note that _Init is the name
13743 -- of the formal for the object to be initialized, which is the task
13744 -- value record itself.
13746 Args := New_List;
13748 -- For lock-free implementation, skip initializations of the Protection
13749 -- object.
13751 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13753 -- Object parameter. This is a pointer to the object of type
13754 -- Protection used by the GNARL to control the protected object.
13756 Append_To (Args,
13757 Make_Attribute_Reference (Loc,
13758 Prefix =>
13759 Make_Selected_Component (Loc,
13760 Prefix => Make_Identifier (Loc, Name_uInit),
13761 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13762 Attribute_Name => Name_Unchecked_Access));
13764 -- Priority parameter. Set to Unspecified_Priority unless there is a
13765 -- Priority rep item, in which case we take the value from the pragma
13766 -- or attribute definition clause, or there is an Interrupt_Priority
13767 -- rep item and no Priority rep item, and we set the ceiling to
13768 -- Interrupt_Priority'Last, an implementation-defined value, see
13769 -- (RM D.3(10)).
13771 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13772 declare
13773 Prio_Clause : constant Node_Id :=
13774 Get_Rep_Item
13775 (Ptyp, Name_Priority, Check_Parents => False);
13777 Prio : Node_Id;
13779 begin
13780 -- Pragma Priority
13782 if Nkind (Prio_Clause) = N_Pragma then
13783 Prio :=
13784 Expression
13785 (First (Pragma_Argument_Associations (Prio_Clause)));
13787 -- Get_Rep_Item returns either priority pragma
13789 if Pragma_Name (Prio_Clause) = Name_Priority then
13790 Prio_Type := RTE (RE_Any_Priority);
13791 else
13792 Prio_Type := RTE (RE_Interrupt_Priority);
13793 end if;
13795 -- Attribute definition clause Priority
13797 else
13798 if Chars (Prio_Clause) = Name_Priority then
13799 Prio_Type := RTE (RE_Any_Priority);
13800 else
13801 Prio_Type := RTE (RE_Interrupt_Priority);
13802 end if;
13804 Prio := Expression (Prio_Clause);
13805 end if;
13807 -- Always create a locale variable to capture the priority.
13808 -- The priority is also passed to Install_Restriced_Handlers.
13809 -- Note that it is really necessary to create this variable
13810 -- explicitly. It might be thought that removing side effects
13811 -- would the appropriate approach, but that could generate
13812 -- declarations improperly placed in the enclosing scope.
13814 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13815 Append_To (L,
13816 Make_Object_Declaration (Loc,
13817 Defining_Identifier => Prio_Var,
13818 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13819 Expression => Relocate_Node (Prio)));
13821 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13822 end;
13824 -- When no priority is specified but an xx_Handler pragma is, we
13825 -- default to System.Interrupts.Default_Interrupt_Priority, see
13826 -- D.3(10).
13828 elsif Has_Attach_Handler (Ptyp)
13829 or else Has_Interrupt_Handler (Ptyp)
13830 then
13831 Append_To (Args,
13832 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
13834 -- Normal case, no priority or xx_Handler specified, default priority
13836 else
13837 Append_To (Args,
13838 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
13839 end if;
13841 -- Deadline_Floor parameter for GNAT_Ravenscar_EDF runtimes
13843 if Restricted_Profile and Task_Dispatching_Policy = 'E' then
13844 Deadline_Floor : declare
13845 Item : constant Node_Id :=
13846 Get_Rep_Item
13847 (Ptyp, Name_Deadline_Floor, Check_Parents => False);
13849 Deadline : Node_Id;
13851 begin
13852 if Present (Item) then
13854 -- Pragma Deadline_Floor
13856 if Nkind (Item) = N_Pragma then
13857 Deadline :=
13858 Expression
13859 (First (Pragma_Argument_Associations (Item)));
13861 -- Attribute definition clause Deadline_Floor
13863 else
13864 pragma Assert
13865 (Nkind (Item) = N_Attribute_Definition_Clause);
13867 Deadline := Expression (Item);
13868 end if;
13870 Append_To (Args, Deadline);
13872 -- Unusual case: default deadline
13874 else
13875 Append_To (Args,
13876 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
13877 end if;
13878 end Deadline_Floor;
13879 end if;
13881 -- Test for Compiler_Info parameter. This parameter allows entry body
13882 -- procedures and barrier functions to be called from the runtime. It
13883 -- is a pointer to the record generated by the compiler to represent
13884 -- the protected object.
13886 -- A protected type without entries that covers an interface and
13887 -- overrides the abstract routines with protected procedures is
13888 -- considered equivalent to a protected type with entries in the
13889 -- context of dispatching select statements.
13891 -- Protected types with interrupt handlers (when not using a
13892 -- restricted profile) are also considered equivalent to protected
13893 -- types with entries.
13895 -- The types which are used (Static_Interrupt_Protection and
13896 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13898 declare
13899 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13901 Called_Subp : RE_Id;
13903 begin
13904 case Pkg_Id is
13905 when System_Tasking_Protected_Objects_Entries =>
13906 Called_Subp := RE_Initialize_Protection_Entries;
13908 -- Argument Compiler_Info
13910 Append_To (Args,
13911 Make_Attribute_Reference (Loc,
13912 Prefix => Make_Identifier (Loc, Name_uInit),
13913 Attribute_Name => Name_Address));
13915 when System_Tasking_Protected_Objects_Single_Entry =>
13916 Called_Subp := RE_Initialize_Protection_Entry;
13918 -- Argument Compiler_Info
13920 Append_To (Args,
13921 Make_Attribute_Reference (Loc,
13922 Prefix => Make_Identifier (Loc, Name_uInit),
13923 Attribute_Name => Name_Address));
13925 when System_Tasking_Protected_Objects =>
13926 Called_Subp := RE_Initialize_Protection;
13928 when others =>
13929 raise Program_Error;
13930 end case;
13932 -- Entry_Queue_Maxes parameter. This is an access to an array of
13933 -- naturals representing the entry queue maximums for each entry
13934 -- in the protected type. Zero represents no max. The access is
13935 -- null if there is no limit for all entries (usual case).
13937 if Has_Entry
13938 and then Pkg_Id = System_Tasking_Protected_Objects_Entries
13939 then
13940 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
13941 Append_To (Args,
13942 Make_Attribute_Reference (Loc,
13943 Prefix =>
13944 New_Occurrence_Of
13945 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
13946 Attribute_Name => Name_Unrestricted_Access));
13947 else
13948 Append_To (Args, Make_Null (Loc));
13949 end if;
13951 -- Edge cases exist where entry initialization functions are
13952 -- called, but no entries exist, so null is appended.
13954 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
13955 Append_To (Args, Make_Null (Loc));
13956 end if;
13958 -- Entry_Bodies parameter. This is a pointer to an array of
13959 -- pointers to the entry body procedures and barrier functions of
13960 -- the object. If the protected type has no entries this object
13961 -- will not exist, in this case, pass a null (it can happen when
13962 -- there are protected interrupt handlers or interfaces).
13964 if Has_Entry then
13965 P_Arr := Entry_Bodies_Array (Ptyp);
13967 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
13968 -- multiple entries).
13970 Append_To (Args,
13971 Make_Attribute_Reference (Loc,
13972 Prefix => New_Occurrence_Of (P_Arr, Loc),
13973 Attribute_Name => Name_Unrestricted_Access));
13975 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
13977 -- Find index mapping function (clumsy but ok for now)
13979 while Ekind (P_Arr) /= E_Function loop
13980 Next_Entity (P_Arr);
13981 end loop;
13983 Append_To (Args,
13984 Make_Attribute_Reference (Loc,
13985 Prefix => New_Occurrence_Of (P_Arr, Loc),
13986 Attribute_Name => Name_Unrestricted_Access));
13987 end if;
13989 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
13991 -- This is the case where we have a protected object with
13992 -- interfaces and no entries, and the single entry restriction
13993 -- is in effect. We pass a null pointer for the entry
13994 -- parameter because there is no actual entry.
13996 Append_To (Args, Make_Null (Loc));
13998 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14000 -- This is the case where we have a protected object with no
14001 -- entries and:
14002 -- - either interrupt handlers with non restricted profile,
14003 -- - or interfaces
14004 -- Note that the types which are used for interrupt handlers
14005 -- (Static/Dynamic_Interrupt_Protection) are derived from
14006 -- Protection_Entries. We pass two null pointers because there
14007 -- is no actual entry, and the initialization procedure needs
14008 -- both Entry_Bodies and Find_Body_Index.
14010 Append_To (Args, Make_Null (Loc));
14011 Append_To (Args, Make_Null (Loc));
14012 end if;
14014 Append_To (L,
14015 Make_Procedure_Call_Statement (Loc,
14016 Name =>
14017 New_Occurrence_Of (RTE (Called_Subp), Loc),
14018 Parameter_Associations => Args));
14019 end;
14020 end if;
14022 if Has_Attach_Handler (Ptyp) then
14024 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
14025 -- make the following call:
14027 -- Install_Handlers (_object,
14028 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14030 -- or, in the case of Ravenscar:
14032 -- Install_Restricted_Handlers
14033 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
14035 declare
14036 Args : constant List_Id := New_List;
14037 Table : constant List_Id := New_List;
14038 Ritem : Node_Id := First_Rep_Item (Ptyp);
14040 begin
14041 -- Build the Priority parameter (only for ravenscar)
14043 if Restricted then
14045 -- Priority comes from a pragma
14047 if Present (Prio_Var) then
14048 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14050 -- Priority is the default one
14052 else
14053 Append_To (Args,
14054 New_Occurrence_Of
14055 (RTE (RE_Default_Interrupt_Priority), Loc));
14056 end if;
14057 end if;
14059 -- Build the Attach_Handler table argument
14061 while Present (Ritem) loop
14062 if Nkind (Ritem) = N_Pragma
14063 and then Pragma_Name (Ritem) = Name_Attach_Handler
14064 then
14065 declare
14066 Handler : constant Node_Id :=
14067 First (Pragma_Argument_Associations (Ritem));
14069 Interrupt : constant Node_Id := Next (Handler);
14070 Expr : constant Node_Id := Expression (Interrupt);
14072 begin
14073 Append_To (Table,
14074 Make_Aggregate (Loc, Expressions => New_List (
14075 Unchecked_Convert_To
14076 (RTE (RE_System_Interrupt_Id), Expr),
14077 Make_Attribute_Reference (Loc,
14078 Prefix =>
14079 Make_Selected_Component (Loc,
14080 Prefix =>
14081 Make_Identifier (Loc, Name_uInit),
14082 Selector_Name =>
14083 Duplicate_Subexpr_No_Checks
14084 (Expression (Handler))),
14085 Attribute_Name => Name_Access))));
14086 end;
14087 end if;
14089 Next_Rep_Item (Ritem);
14090 end loop;
14092 -- Append the table argument we just built
14094 Append_To (Args, Make_Aggregate (Loc, Table));
14096 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14097 -- call to the statements.
14099 if Restricted then
14100 -- Call a simplified version of Install_Handlers to be used
14101 -- when the Ravenscar restrictions are in effect
14102 -- (Install_Restricted_Handlers).
14104 Append_To (L,
14105 Make_Procedure_Call_Statement (Loc,
14106 Name =>
14107 New_Occurrence_Of
14108 (RTE (RE_Install_Restricted_Handlers), Loc),
14109 Parameter_Associations => Args));
14111 else
14112 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14114 -- First, prepends the _object argument
14116 Prepend_To (Args,
14117 Make_Attribute_Reference (Loc,
14118 Prefix =>
14119 Make_Selected_Component (Loc,
14120 Prefix => Make_Identifier (Loc, Name_uInit),
14121 Selector_Name =>
14122 Make_Identifier (Loc, Name_uObject)),
14123 Attribute_Name => Name_Unchecked_Access));
14124 end if;
14126 -- Then, insert call to Install_Handlers
14128 Append_To (L,
14129 Make_Procedure_Call_Statement (Loc,
14130 Name =>
14131 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14132 Parameter_Associations => Args));
14133 end if;
14134 end;
14135 end if;
14137 return L;
14138 end Make_Initialize_Protection;
14140 ---------------------------
14141 -- Make_Task_Create_Call --
14142 ---------------------------
14144 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14145 Loc : constant Source_Ptr := Sloc (Task_Rec);
14146 Args : List_Id;
14147 Ecount : Node_Id;
14148 Name : Node_Id;
14149 Tdec : Node_Id;
14150 Tdef : Node_Id;
14151 Tnam : Name_Id;
14152 Ttyp : Entity_Id;
14154 begin
14155 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14156 Tnam := Chars (Ttyp);
14158 -- Get task declaration. In the case of a task type declaration, this is
14159 -- simply the parent of the task type entity. In the single task
14160 -- declaration, this parent will be the implicit type, and we can find
14161 -- the corresponding single task declaration by searching forward in the
14162 -- declaration list in the tree.
14164 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14165 -- this type should have been removed during semantic analysis.
14167 Tdec := Parent (Ttyp);
14168 while Nkind (Tdec) not in
14169 N_Task_Type_Declaration | N_Single_Task_Declaration
14170 loop
14171 Next (Tdec);
14172 end loop;
14174 -- Now we can find the task definition from this declaration
14176 Tdef := Task_Definition (Tdec);
14178 -- Build the parameter list for the call. Note that _Init is the name
14179 -- of the formal for the object to be initialized, which is the task
14180 -- value record itself.
14182 Args := New_List;
14184 -- Priority parameter. Set to Unspecified_Priority unless there is a
14185 -- Priority rep item, in which case we take the value from the rep item.
14186 -- Not used on Ravenscar_EDF profile.
14188 if not (Restricted_Profile and then Task_Dispatching_Policy = 'E') then
14189 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14190 Append_To (Args,
14191 Make_Selected_Component (Loc,
14192 Prefix => Make_Identifier (Loc, Name_uInit),
14193 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14194 else
14195 Append_To (Args,
14196 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14197 end if;
14198 end if;
14200 -- Optional Stack parameter
14202 if Restricted_Profile then
14204 -- If the stack has been preallocated by the expander then
14205 -- pass its address. Otherwise, pass a null address.
14207 if Preallocated_Stacks_On_Target then
14208 Append_To (Args,
14209 Make_Attribute_Reference (Loc,
14210 Prefix =>
14211 Make_Selected_Component (Loc,
14212 Prefix => Make_Identifier (Loc, Name_uInit),
14213 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14214 Attribute_Name => Name_Address));
14216 else
14217 Append_To (Args,
14218 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14219 end if;
14220 end if;
14222 -- Size parameter. If no Storage_Size pragma is present, then
14223 -- the size is taken from the taskZ variable for the type, which
14224 -- is either Unspecified_Size, or has been reset by the use of
14225 -- a Storage_Size attribute definition clause. If a pragma is
14226 -- present, then the size is taken from the _Size field of the
14227 -- task value record, which was set from the pragma value.
14229 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14230 Append_To (Args,
14231 Make_Selected_Component (Loc,
14232 Prefix => Make_Identifier (Loc, Name_uInit),
14233 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14235 else
14236 Append_To (Args,
14237 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14238 end if;
14240 -- Secondary_Stack parameter used for restricted profiles
14242 if Restricted_Profile then
14244 -- If the secondary stack has been allocated by the expander then
14245 -- pass its access pointer. Otherwise, pass null.
14247 if Create_Secondary_Stack_For_Task (Ttyp) then
14248 Append_To (Args,
14249 Make_Attribute_Reference (Loc,
14250 Prefix =>
14251 Make_Selected_Component (Loc,
14252 Prefix => Make_Identifier (Loc, Name_uInit),
14253 Selector_Name =>
14254 Make_Identifier (Loc, Name_uSecondary_Stack)),
14255 Attribute_Name => Name_Unrestricted_Access));
14257 else
14258 Append_To (Args, Make_Null (Loc));
14259 end if;
14260 end if;
14262 -- Secondary_Stack_Size parameter. Set RE_Unspecified_Size unless there
14263 -- is a Secondary_Stack_Size pragma, in which case take the value from
14264 -- the pragma. If the restriction No_Secondary_Stack is active then a
14265 -- size of 0 is passed regardless to prevent the allocation of the
14266 -- unused stack.
14268 if Restriction_Active (No_Secondary_Stack) then
14269 Append_To (Args, Make_Integer_Literal (Loc, 0));
14271 elsif Has_Rep_Pragma
14272 (Ttyp, Name_Secondary_Stack_Size, Check_Parents => False)
14273 then
14274 Append_To (Args,
14275 Make_Selected_Component (Loc,
14276 Prefix => Make_Identifier (Loc, Name_uInit),
14277 Selector_Name =>
14278 Make_Identifier (Loc, Name_uSecondary_Stack_Size)));
14280 else
14281 Append_To (Args,
14282 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
14283 end if;
14285 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14286 -- Task_Info pragma, in which case we take the value from the pragma.
14288 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14289 Append_To (Args,
14290 Make_Selected_Component (Loc,
14291 Prefix => Make_Identifier (Loc, Name_uInit),
14292 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14294 else
14295 Append_To (Args,
14296 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14297 end if;
14299 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14300 -- in which case we take the value from the rep item. The parameter is
14301 -- passed as an Integer because in the case of unspecified CPU the
14302 -- value is not in the range of CPU_Range.
14304 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14305 Append_To (Args,
14306 Convert_To (Standard_Integer,
14307 Make_Selected_Component (Loc,
14308 Prefix => Make_Identifier (Loc, Name_uInit),
14309 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14310 else
14311 Append_To (Args,
14312 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14313 end if;
14315 if not Restricted_Profile or else Task_Dispatching_Policy = 'E' then
14317 -- Deadline parameter. If no Relative_Deadline pragma is present,
14318 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14319 -- the deadline is taken from the _Relative_Deadline field of the
14320 -- task value record, which was set from the pragma value. Note that
14321 -- this parameter must not be generated for the restricted profiles
14322 -- since Ravenscar does not allow deadlines.
14324 -- Case where pragma Relative_Deadline applies: use given value
14326 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14327 Append_To (Args,
14328 Make_Selected_Component (Loc,
14329 Prefix => Make_Identifier (Loc, Name_uInit),
14330 Selector_Name =>
14331 Make_Identifier (Loc, Name_uRelative_Deadline)));
14333 -- No pragma Relative_Deadline apply to the task
14335 else
14336 Append_To (Args,
14337 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14338 end if;
14339 end if;
14341 if not Restricted_Profile then
14343 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14344 -- present, then the dispatching domain is null. If a rep item is
14345 -- present, then the dispatching domain is taken from the
14346 -- _Dispatching_Domain field of the task value record, which was set
14347 -- from the rep item value.
14349 -- Case where Dispatching_Domain rep item applies: use given value
14351 if Has_Rep_Item
14352 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14353 then
14354 Append_To (Args,
14355 Make_Selected_Component (Loc,
14356 Prefix =>
14357 Make_Identifier (Loc, Name_uInit),
14358 Selector_Name =>
14359 Make_Identifier (Loc, Name_uDispatching_Domain)));
14361 -- No pragma or aspect Dispatching_Domain applies to the task
14363 else
14364 Append_To (Args, Make_Null (Loc));
14365 end if;
14367 -- Number of entries. This is an expression of the form:
14369 -- n + _Init.a'Length + _Init.a'B'Length + ...
14371 -- where a,b... are the entry family names for the task definition
14373 Ecount := Build_Entry_Count_Expression (Ttyp, Loc);
14374 Append_To (Args, Ecount);
14376 -- Master parameter. This is a reference to the _Master parameter of
14377 -- the initialization procedure, except in the case of the pragma
14378 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14379 -- System.Tasking.Library_Task_Level.
14381 if Restriction_Active (No_Task_Hierarchy) = False then
14382 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14383 else
14384 Append_To (Args, Make_Integer_Literal (Loc, Library_Task_Level));
14385 end if;
14386 end if;
14388 -- State parameter. This is a pointer to the task body procedure. The
14389 -- required value is obtained by taking 'Unrestricted_Access of the task
14390 -- body procedure and converting it (with an unchecked conversion) to
14391 -- the type required by the task kernel. For further details, see the
14392 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14393 -- than 'Address in order to avoid creating trampolines.
14395 declare
14396 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14397 Subp_Ptr_Typ : constant Node_Id :=
14398 Create_Itype (E_Access_Subprogram_Type, Tdec);
14399 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14401 begin
14402 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14403 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14405 -- Be sure to freeze a reference to the access-to-subprogram type,
14406 -- otherwise gigi will complain that it's in the wrong scope, because
14407 -- it's actually inside the init procedure for the record type that
14408 -- corresponds to the task type.
14410 Set_Itype (Ref, Subp_Ptr_Typ);
14411 Append_Freeze_Action (Task_Rec, Ref);
14413 Append_To (Args,
14414 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14415 Make_Qualified_Expression (Loc,
14416 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14417 Expression =>
14418 Make_Attribute_Reference (Loc,
14419 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14420 Attribute_Name => Name_Unrestricted_Access))));
14421 end;
14423 -- Discriminants parameter. This is just the address of the task
14424 -- value record itself (which contains the discriminant values
14426 Append_To (Args,
14427 Make_Attribute_Reference (Loc,
14428 Prefix => Make_Identifier (Loc, Name_uInit),
14429 Attribute_Name => Name_Address));
14431 -- Elaborated parameter. This is an access to the elaboration Boolean
14433 Append_To (Args,
14434 Make_Attribute_Reference (Loc,
14435 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14436 Attribute_Name => Name_Unchecked_Access));
14438 -- Add Chain parameter (not done for sequential elaboration policy, see
14439 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14441 if Partition_Elaboration_Policy /= 'S' then
14442 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14443 end if;
14445 -- Task name parameter. Take this from the _Task_Id parameter to the
14446 -- init call unless there is a Task_Name pragma, in which case we take
14447 -- the value from the pragma.
14449 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14450 -- Copy expression in full, because it may be dynamic and have
14451 -- side effects.
14453 Append_To (Args,
14454 New_Copy_Tree
14455 (Expression
14456 (First
14457 (Pragma_Argument_Associations
14458 (Get_Rep_Pragma
14459 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14461 else
14462 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14463 end if;
14465 -- Created_Task parameter. This is the _Task_Id field of the task
14466 -- record value
14468 Append_To (Args,
14469 Make_Selected_Component (Loc,
14470 Prefix => Make_Identifier (Loc, Name_uInit),
14471 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14473 declare
14474 Create_RE : RE_Id;
14476 begin
14477 if Restricted_Profile then
14478 if Partition_Elaboration_Policy = 'S' then
14479 Create_RE := RE_Create_Restricted_Task_Sequential;
14480 else
14481 Create_RE := RE_Create_Restricted_Task;
14482 end if;
14483 else
14484 Create_RE := RE_Create_Task;
14485 end if;
14487 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14488 end;
14490 return
14491 Make_Procedure_Call_Statement (Loc,
14492 Name => Name,
14493 Parameter_Associations => Args);
14494 end Make_Task_Create_Call;
14496 ------------------------------
14497 -- Next_Protected_Operation --
14498 ------------------------------
14500 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14501 Next_Op : Node_Id;
14503 begin
14504 -- Check whether there is a subsequent body for a protected operation
14505 -- in the current protected body. In Ada2012 that includes expression
14506 -- functions that are completions.
14508 Next_Op := Next (N);
14509 while Present (Next_Op)
14510 and then Nkind (Next_Op) not in
14511 N_Subprogram_Body | N_Entry_Body | N_Expression_Function
14512 loop
14513 Next (Next_Op);
14514 end loop;
14516 return Next_Op;
14517 end Next_Protected_Operation;
14519 ---------------------
14520 -- Null_Statements --
14521 ---------------------
14523 function Null_Statements (Stats : List_Id) return Boolean is
14524 Stmt : Node_Id;
14526 begin
14527 Stmt := First (Stats);
14528 while Nkind (Stmt) /= N_Empty
14529 and then (Nkind (Stmt) in N_Null_Statement | N_Label
14530 or else
14531 (Nkind (Stmt) = N_Pragma
14532 and then
14533 Pragma_Name_Unmapped (Stmt) in Name_Unreferenced
14534 | Name_Unmodified
14535 | Name_Warnings))
14536 loop
14537 Next (Stmt);
14538 end loop;
14540 return Nkind (Stmt) = N_Empty;
14541 end Null_Statements;
14543 --------------------------
14544 -- Parameter_Block_Pack --
14545 --------------------------
14547 function Parameter_Block_Pack
14548 (Loc : Source_Ptr;
14549 Blk_Typ : Entity_Id;
14550 Actuals : List_Id;
14551 Formals : List_Id;
14552 Decls : List_Id;
14553 Stmts : List_Id) return Entity_Id
14555 Actual : Entity_Id;
14556 Expr : Node_Id := Empty;
14557 Formal : Entity_Id;
14558 Has_Param : Boolean := False;
14559 P : Entity_Id;
14560 Params : List_Id;
14561 Temp_Asn : Node_Id;
14562 Temp_Nam : Node_Id;
14564 begin
14565 Actual := First (Actuals);
14566 Formal := Defining_Identifier (First (Formals));
14567 Params := New_List;
14568 while Present (Actual) loop
14569 if Is_By_Copy_Type (Etype (Actual)) then
14570 -- Generate:
14571 -- Jnn : aliased <formal-type>
14573 Temp_Nam := Make_Temporary (Loc, 'J');
14575 Append_To (Decls,
14576 Make_Object_Declaration (Loc,
14577 Aliased_Present => True,
14578 Defining_Identifier => Temp_Nam,
14579 Object_Definition =>
14580 New_Occurrence_Of (Etype (Formal), Loc)));
14582 -- The object is initialized with an explicit assignment
14583 -- later. Indicate that it does not need an initialization
14584 -- to prevent spurious warnings if the type excludes null.
14586 Set_No_Initialization (Last (Decls));
14588 if Ekind (Formal) /= E_Out_Parameter then
14590 -- Generate:
14591 -- Jnn := <actual>
14593 Temp_Asn :=
14594 New_Occurrence_Of (Temp_Nam, Loc);
14596 Set_Assignment_OK (Temp_Asn);
14598 Append_To (Stmts,
14599 Make_Assignment_Statement (Loc,
14600 Name => Temp_Asn,
14601 Expression => New_Copy_Tree (Actual)));
14602 end if;
14604 -- If the actual is not controlling, generate:
14606 -- Jnn'unchecked_access
14608 -- and add it to aggegate for access to formals. Note that the
14609 -- actual may be by-copy but still be a controlling actual if it
14610 -- is an access to class-wide interface.
14612 if not Is_Controlling_Actual (Actual) then
14613 Append_To (Params,
14614 Make_Attribute_Reference (Loc,
14615 Attribute_Name => Name_Unchecked_Access,
14616 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14618 Has_Param := True;
14619 end if;
14621 -- The controlling parameter is omitted
14623 else
14624 if not Is_Controlling_Actual (Actual) then
14625 Append_To (Params,
14626 Make_Reference (Loc, New_Copy_Tree (Actual)));
14628 Has_Param := True;
14629 end if;
14630 end if;
14632 Next_Actual (Actual);
14633 Next_Formal_With_Extras (Formal);
14634 end loop;
14636 if Has_Param then
14637 Expr := Make_Aggregate (Loc, Params);
14638 end if;
14640 -- Generate:
14641 -- P : Ann := (
14642 -- J1'unchecked_access;
14643 -- <actual2>'reference;
14644 -- ...);
14646 P := Make_Temporary (Loc, 'P');
14648 Append_To (Decls,
14649 Make_Object_Declaration (Loc,
14650 Defining_Identifier => P,
14651 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14652 Expression => Expr));
14654 return P;
14655 end Parameter_Block_Pack;
14657 ----------------------------
14658 -- Parameter_Block_Unpack --
14659 ----------------------------
14661 function Parameter_Block_Unpack
14662 (Loc : Source_Ptr;
14663 P : Entity_Id;
14664 Actuals : List_Id;
14665 Formals : List_Id) return List_Id
14667 Actual : Entity_Id;
14668 Asnmt : Node_Id;
14669 Formal : Entity_Id;
14670 Has_Asnmt : Boolean := False;
14671 Result : constant List_Id := New_List;
14673 begin
14674 Actual := First (Actuals);
14675 Formal := Defining_Identifier (First (Formals));
14676 while Present (Actual) loop
14677 if Is_By_Copy_Type (Etype (Actual))
14678 and then Ekind (Formal) /= E_In_Parameter
14679 then
14680 -- Generate:
14681 -- <actual> := P.<formal>;
14683 Asnmt :=
14684 Make_Assignment_Statement (Loc,
14685 Name =>
14686 New_Copy (Actual),
14687 Expression =>
14688 Make_Explicit_Dereference (Loc,
14689 Make_Selected_Component (Loc,
14690 Prefix =>
14691 New_Occurrence_Of (P, Loc),
14692 Selector_Name =>
14693 Make_Identifier (Loc, Chars (Formal)))));
14695 Set_Assignment_OK (Name (Asnmt));
14696 Append_To (Result, Asnmt);
14698 Has_Asnmt := True;
14699 end if;
14701 Next_Actual (Actual);
14702 Next_Formal_With_Extras (Formal);
14703 end loop;
14705 if Has_Asnmt then
14706 return Result;
14707 else
14708 return New_List (Make_Null_Statement (Loc));
14709 end if;
14710 end Parameter_Block_Unpack;
14712 ---------------------
14713 -- Reset_Scopes_To --
14714 ---------------------
14716 procedure Reset_Scopes_To (Bod : Node_Id; E : Entity_Id) is
14717 function Reset_Scope (N : Node_Id) return Traverse_Result;
14718 -- Temporaries may have been declared during expansion of the procedure
14719 -- created for an entry body or an accept alternative. Indicate that
14720 -- their scope is the new body, to ensure proper generation of uplevel
14721 -- references where needed during unnesting.
14723 procedure Reset_Scopes is new Traverse_Proc (Reset_Scope);
14725 -----------------
14726 -- Reset_Scope --
14727 -----------------
14729 function Reset_Scope (N : Node_Id) return Traverse_Result is
14730 Decl : Node_Id;
14732 begin
14733 -- If this is a block statement with an Identifier, it forms a scope,
14734 -- so we want to reset its scope but not look inside.
14736 if N /= Bod
14737 and then Nkind (N) = N_Block_Statement
14738 and then Present (Identifier (N))
14739 then
14740 Set_Scope (Entity (Identifier (N)), E);
14741 return Skip;
14743 -- Ditto for a package declaration or a full type declaration, etc.
14745 elsif (Nkind (N) = N_Package_Declaration
14746 and then N /= Specification (N))
14747 or else Nkind (N) in N_Declaration
14748 or else Nkind (N) in N_Renaming_Declaration
14749 then
14750 Set_Scope (Defining_Entity (N), E);
14751 return Skip;
14753 elsif N = Bod then
14755 -- Scan declarations in new body. Declarations in the statement
14756 -- part will be handled during later traversal.
14758 Decl := First (Declarations (N));
14759 while Present (Decl) loop
14760 Reset_Scopes (Decl);
14761 Next (Decl);
14762 end loop;
14764 elsif Nkind (N) = N_Freeze_Entity then
14766 -- Scan the actions associated with a freeze node, which may
14767 -- actually be declarations with entities that need to have
14768 -- their scopes reset.
14770 Decl := First (Actions (N));
14771 while Present (Decl) loop
14772 Reset_Scopes (Decl);
14773 Next (Decl);
14774 end loop;
14776 elsif N /= Bod and then Nkind (N) in N_Proper_Body then
14778 -- A subprogram without a separate declaration may be encountered,
14779 -- and we need to reset the subprogram's entity's scope.
14781 if Nkind (N) = N_Subprogram_Body then
14782 Set_Scope (Defining_Entity (Specification (N)), E);
14783 end if;
14785 return Skip;
14786 end if;
14788 return OK;
14789 end Reset_Scope;
14791 -- Start of processing for Reset_Scopes_To
14793 begin
14794 Reset_Scopes (Bod);
14795 end Reset_Scopes_To;
14797 ----------------------
14798 -- Set_Discriminals --
14799 ----------------------
14801 procedure Set_Discriminals (Dec : Node_Id) is
14802 D : Entity_Id;
14803 Pdef : Entity_Id;
14804 D_Minal : Entity_Id;
14806 begin
14807 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14808 Pdef := Defining_Identifier (Dec);
14810 if Has_Discriminants (Pdef) then
14811 D := First_Discriminant (Pdef);
14812 while Present (D) loop
14813 D_Minal :=
14814 Make_Defining_Identifier (Sloc (D),
14815 Chars => New_External_Name (Chars (D), 'D'));
14817 Mutate_Ekind (D_Minal, E_Constant);
14818 Set_Etype (D_Minal, Etype (D));
14819 Set_Scope (D_Minal, Pdef);
14820 Set_Discriminal (D, D_Minal);
14821 Set_Discriminal_Link (D_Minal, D);
14823 Next_Discriminant (D);
14824 end loop;
14825 end if;
14826 end Set_Discriminals;
14828 -----------------------
14829 -- Trivial_Accept_OK --
14830 -----------------------
14832 function Trivial_Accept_OK return Boolean is
14833 begin
14834 case Opt.Task_Dispatching_Policy is
14836 -- If we have the default task dispatching policy in effect, we can
14837 -- definitely do the optimization (one way of looking at this is to
14838 -- think of the formal definition of the default policy being allowed
14839 -- to run any task it likes after a rendezvous, so even if notionally
14840 -- a full rescheduling occurs, we can say that our dispatching policy
14841 -- (i.e. the default dispatching policy) reorders the queue to be the
14842 -- same as just before the call.
14844 when ' ' =>
14845 return True;
14847 -- FIFO_Within_Priorities certainly does not permit this
14848 -- optimization since the Rendezvous is a scheduling action that may
14849 -- require some other task to be run.
14851 when 'F' =>
14852 return False;
14854 -- For now, disallow the optimization for all other policies. This
14855 -- may be over-conservative, but it is certainly not incorrect.
14857 when others =>
14858 return False;
14859 end case;
14860 end Trivial_Accept_OK;
14862 end Exp_Ch9;