ada: Fix renaming of predefined equality operator for unchecked union types
[official-gcc.git] / gcc / ada / exp_ch9.adb
blobb0e3632b8c83166978024ec0287a4ba5b642dbd5
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-2023, 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_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Freeze; use Freeze;
45 with Hostparm;
46 with Itypes; use Itypes;
47 with Namet; use Namet;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
50 with Opt; use Opt;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
54 with Sem; use Sem;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Ch5; use Sem_Ch5;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch8; use Sem_Ch8;
59 with Sem_Ch9; use Sem_Ch9;
60 with Sem_Ch11; use Sem_Ch11;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Elab; use Sem_Elab;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Res; use Sem_Res;
65 with Sem_Util; use Sem_Util;
66 with Sinfo; use Sinfo;
67 with Sinfo.Nodes; use Sinfo.Nodes;
68 with Sinfo.Utils; use Sinfo.Utils;
69 with Snames; use Snames;
70 with Stand; use Stand;
71 with Targparm; use Targparm;
72 with Tbuild; use Tbuild;
73 with Uintp; use Uintp;
74 with Validsw; use Validsw;
76 package body Exp_Ch9 is
78 -- The following constant establishes the upper bound for the index of
79 -- an entry family. It is used to limit the allocated size of protected
80 -- types with defaulted discriminant of an integer type, when the bound
81 -- of some entry family depends on a discriminant. The limitation to entry
82 -- families of 128K should be reasonable in all cases, and is a documented
83 -- implementation restriction.
85 Entry_Family_Bound : constant Pos := 2**16;
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
91 function Actual_Index_Expression
92 (Sloc : Source_Ptr;
93 Ent : Entity_Id;
94 Index : Node_Id;
95 Tsk : Entity_Id) return Node_Id;
96 -- Compute the index position for an entry call. Tsk is the target task. If
97 -- the bounds of some entry family depend on discriminants, the expression
98 -- computed by this function uses the discriminants of the target task.
100 procedure Add_Object_Pointer
101 (Loc : Source_Ptr;
102 Conc_Typ : Entity_Id;
103 Decls : List_Id);
104 -- Prepend an object pointer declaration to the declaration list Decls.
105 -- This object pointer is initialized to a type conversion of the System.
106 -- Address pointer passed to entry barrier functions and entry body
107 -- procedures.
109 procedure Add_Formal_Renamings
110 (Spec : Node_Id;
111 Decls : List_Id;
112 Ent : Entity_Id;
113 Loc : Source_Ptr);
114 -- Create renaming declarations for the formals, inside the procedure that
115 -- implements an entry body. The renamings make the original names of the
116 -- formals accessible to gdb, and serve no other purpose.
117 -- Spec is the specification of the procedure being built.
118 -- Decls is the list of declarations to be enhanced.
119 -- Ent is the entity for the original entry body.
121 function Build_Accept_Body (Astat : Node_Id) return Node_Id;
122 -- Transform accept statement into a block with added exception handler.
123 -- Used both for simple accept statements and for accept alternatives in
124 -- select statements. Astat is the accept statement.
126 function Build_Barrier_Function
127 (N : Node_Id;
128 Ent : Entity_Id;
129 Pid : Entity_Id) return Node_Id;
130 -- Build the function body returning the value of the barrier expression
131 -- for the specified entry body.
133 function Build_Barrier_Function_Specification
134 (Loc : Source_Ptr;
135 Def_Id : Entity_Id) return Node_Id;
136 -- Build a specification for a function implementing the protected entry
137 -- barrier of the specified entry body.
139 function Build_Corresponding_Record
140 (N : Node_Id;
141 Ctyp : Entity_Id;
142 Loc : Source_Ptr) return Node_Id;
143 -- Common to tasks and protected types. Copy discriminant specifications,
144 -- build record declaration. N is the type declaration, Ctyp is the
145 -- concurrent entity (task type or protected type).
147 function Build_Dispatching_Tag_Check
148 (K : Entity_Id;
149 N : Node_Id) return Node_Id;
150 -- Utility to create the tree to check whether the dispatching call in
151 -- a timed entry call, a conditional entry call, or an asynchronous
152 -- transfer of control is a call to a primitive of a non-synchronized type.
153 -- K is the temporary that holds the tagged kind of the target object, and
154 -- N is the enclosing construct.
156 function Build_Entry_Count_Expression
157 (Concurrent_Type : Entity_Id;
158 Loc : Source_Ptr) return Node_Id;
159 -- Compute number of entries for concurrent object. This is a count of
160 -- simple entries, followed by an expression that computes the length
161 -- of the range of each entry family. A single array with that size is
162 -- allocated for each concurrent object of the type.
164 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id;
165 -- Build the function that translates the entry index in the call
166 -- (which depends on the size of entry families) into an index into the
167 -- Entry_Bodies_Array, to determine the body and barrier function used
168 -- in a protected entry call. A pointer to this function appears in every
169 -- protected object.
171 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id;
172 -- Build subprogram declaration for previous one
174 function Build_Lock_Free_Protected_Subprogram_Body
175 (N : Node_Id;
176 Prot_Typ : Node_Id;
177 Unprot_Spec : Node_Id) return Node_Id;
178 -- N denotes a subprogram body of protected type Prot_Typ. Unprot_Spec is
179 -- the subprogram specification of the unprotected version of N. Transform
180 -- N such that it invokes the unprotected version of the body.
182 function Build_Lock_Free_Unprotected_Subprogram_Body
183 (N : Node_Id;
184 Prot_Typ : Node_Id) return Node_Id;
185 -- N denotes a subprogram body of protected type Prot_Typ. Build a version
186 -- of N where the original statements of N are synchronized through atomic
187 -- actions such as compare and exchange. Prior to invoking this routine, it
188 -- has been established that N can be implemented in a lock-free fashion.
190 function Build_Parameter_Block
191 (Loc : Source_Ptr;
192 Actuals : List_Id;
193 Formals : List_Id;
194 Decls : List_Id) return Entity_Id;
195 -- Generate an access type for each actual parameter in the list Actuals.
196 -- Create an encapsulating record that contains all the actuals and return
197 -- its type. Generate:
198 -- type Ann1 is access all <actual1-type>
199 -- ...
200 -- type AnnN is access all <actualN-type>
201 -- type Pnn is record
202 -- <formal1> : Ann1;
203 -- ...
204 -- <formalN> : AnnN;
205 -- end record;
207 function Build_Protected_Entry
208 (N : Node_Id;
209 Ent : Entity_Id;
210 Pid : Node_Id) return Node_Id;
211 -- Build the procedure implementing the statement sequence of the specified
212 -- entry body.
214 function Build_Protected_Entry_Specification
215 (Loc : Source_Ptr;
216 Def_Id : Entity_Id;
217 Ent_Id : Entity_Id) return Node_Id;
218 -- Build a specification for the procedure implementing the statements of
219 -- the specified entry body. Add attributes associating it with the entry
220 -- defining identifier Ent_Id.
222 function Build_Protected_Spec
223 (N : Node_Id;
224 Obj_Type : Entity_Id;
225 Ident : Entity_Id;
226 Unprotected : Boolean := False) return List_Id;
227 -- Utility shared by Build_Protected_Sub_Spec and Expand_Access_Protected_
228 -- Subprogram_Type. Builds signature of protected subprogram, adding the
229 -- formal that corresponds to the object itself. For an access to protected
230 -- subprogram, there is no object type to specify, so the parameter has
231 -- type Address and mode In. An indirect call through such a pointer will
232 -- convert the address to a reference to the actual object. The object is
233 -- a limited record and therefore a by_reference type.
235 function Build_Protected_Subprogram_Body
236 (N : Node_Id;
237 Pid : Node_Id;
238 N_Op_Spec : Node_Id) return Node_Id;
239 -- This function is used to construct the protected version of a protected
240 -- subprogram. Its statement sequence first defers abort, then locks the
241 -- associated protected object, and then enters a block that contains a
242 -- call to the unprotected version of the subprogram (for details, see
243 -- Build_Unprotected_Subprogram_Body). This block statement requires a
244 -- cleanup handler that unlocks the object in all cases. For details,
245 -- see Exp_Ch7.Expand_Cleanup_Actions.
247 function Build_Renamed_Formal_Declaration
248 (New_F : Entity_Id;
249 Formal : Entity_Id;
250 Comp : Entity_Id;
251 Renamed_Formal : Node_Id) return Node_Id;
252 -- Create a renaming declaration for a formal, within a protected entry
253 -- body or an accept body. The renamed object is a component of the
254 -- parameter block that is a parameter in the entry call.
256 -- In Ada 2012, if the formal is an incomplete tagged type, the renaming
257 -- does not dereference the corresponding component to prevent an illegal
258 -- use of the incomplete type (AI05-0151).
260 function Build_Selected_Name
261 (Prefix : Entity_Id;
262 Selector : Entity_Id;
263 Append_Char : Character := ' ') return Name_Id;
264 -- Build a name in the form of Prefix__Selector, with an optional character
265 -- appended. This is used for internal subprograms generated for operations
266 -- of protected types, including barrier functions. For the subprograms
267 -- generated for entry bodies and entry barriers, the generated name
268 -- includes a sequence number that makes names unique in the presence of
269 -- entry overloading. This is necessary because entry body procedures and
270 -- barrier functions all have the same signature.
272 procedure Build_Simple_Entry_Call
273 (N : Node_Id;
274 Concval : Node_Id;
275 Ename : Node_Id;
276 Index : Node_Id);
277 -- Build the call corresponding to the task entry call. N is the task entry
278 -- call, Concval is the concurrent object, Ename is the entry name and
279 -- Index is the entry family index.
280 -- Note that N might be expanded into an N_Block_Statement if it gets
281 -- inlined.
283 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id;
284 -- This routine constructs a specification for the procedure that we will
285 -- build for the task body for task type T. The spec has the form:
287 -- procedure tnameB (_Task : access tnameV);
289 -- where name is the character name taken from the task type entity that
290 -- is passed as the argument to the procedure, and tnameV is the task
291 -- value type that is associated with the task type.
293 function Build_Unprotected_Subprogram_Body
294 (N : Node_Id;
295 Pid : Node_Id) return Node_Id;
296 -- This routine constructs the unprotected version of a protected
297 -- subprogram body, which contains all of the code in the original,
298 -- unexpanded body. This is the version of the protected subprogram that is
299 -- called from all protected operations on the same object, including the
300 -- protected version of the same subprogram.
302 procedure Build_Wrapper_Bodies
303 (Loc : Source_Ptr;
304 Typ : Entity_Id;
305 N : Node_Id);
306 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
307 -- record of a concurrent type. N is the insertion node where all bodies
308 -- will be placed. This routine builds the bodies of the subprograms which
309 -- serve as an indirection mechanism to overriding primitives of concurrent
310 -- types, entries and protected procedures. Any new body is analyzed.
312 procedure Build_Wrapper_Specs
313 (Loc : Source_Ptr;
314 Typ : Entity_Id;
315 N : in out Node_Id);
316 -- Ada 2005 (AI-345): Typ is either a concurrent type or the corresponding
317 -- record of a concurrent type. N is the insertion node where all specs
318 -- will be placed. This routine builds the specs of the subprograms which
319 -- serve as an indirection mechanism to overriding primitives of concurrent
320 -- types, entries and protected procedures. Any new spec is analyzed.
322 procedure Collect_Entry_Families
323 (Loc : Source_Ptr;
324 Cdecls : List_Id;
325 Current_Node : in out Node_Id;
326 Conctyp : Entity_Id);
327 -- For each entry family in a concurrent type, create an anonymous array
328 -- type of the right size, and add a component to the corresponding_record.
330 function Concurrent_Object
331 (Spec_Id : Entity_Id;
332 Conc_Typ : Entity_Id) return Entity_Id;
333 -- Given a subprogram entity Spec_Id and concurrent type Conc_Typ, return
334 -- the entity associated with the concurrent object in the Protected_Body_
335 -- Subprogram or the Task_Body_Procedure of Spec_Id. The returned entity
336 -- denotes formal parameter _O, _object or _task.
338 function Copy_Result_Type (Res : Node_Id) return Node_Id;
339 -- Copy the result type of a function specification, when building the
340 -- internal operation corresponding to a protected function, or when
341 -- expanding an access to protected function. If the result is an anonymous
342 -- access to subprogram itself, we need to create a new signature with the
343 -- same parameter names and the same resolved types, but with new entities
344 -- for the formals.
346 function Create_Secondary_Stack_For_Task (T : Node_Id) return Boolean;
347 -- Return whether a secondary stack for the task T should be created by the
348 -- expander. The secondary stack for a task will be created by the expander
349 -- if the size of the stack has been specified by the Secondary_Stack_Size
350 -- representation aspect and either the No_Implicit_Heap_Allocations or
351 -- No_Implicit_Task_Allocations restrictions are in effect and the
352 -- No_Secondary_Stack restriction is not.
354 procedure Debug_Private_Data_Declarations (Decls : List_Id);
355 -- Decls is a list which may contain the declarations created by Install_
356 -- Private_Data_Declarations. All generated entities are marked as needing
357 -- debug info and debug nodes are manually generation where necessary. This
358 -- step of the expansion must to be done after private data has been moved
359 -- to its final resting scope to ensure proper visibility of debug objects.
361 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id);
362 -- If control flow optimizations are suppressed, and Alt is an accept,
363 -- delay, or entry call alternative with no trailing statements, insert
364 -- a null trailing statement with the given Loc (which is the sloc of
365 -- the accept, delay, or entry call statement). There might not be any
366 -- generated code for the accept, delay, or entry call itself (the effect
367 -- of these statements is part of the general processing done for the
368 -- enclosing selective accept, timed entry call, or asynchronous select),
369 -- and the null statement is there to carry the sloc of that statement to
370 -- the back-end for trace-based coverage analysis purposes.
372 procedure Extract_Dispatching_Call
373 (N : Node_Id;
374 Call_Ent : out Entity_Id;
375 Object : out Entity_Id;
376 Actuals : out List_Id;
377 Formals : out List_Id);
378 -- Given a dispatching call, extract the entity of the name of the call,
379 -- its actual dispatching object, its actual parameters and the formal
380 -- parameters of the overridden interface-level version. If the type of
381 -- the dispatching object is an access type then an explicit dereference
382 -- is returned in Object.
384 procedure Extract_Entry
385 (N : Node_Id;
386 Concval : out Node_Id;
387 Ename : out Node_Id;
388 Index : out Node_Id);
389 -- Given an entry call, returns the associated concurrent object, the entry
390 -- name, and the entry family index.
392 function Family_Offset
393 (Loc : Source_Ptr;
394 Hi : Node_Id;
395 Lo : Node_Id;
396 Ttyp : Entity_Id;
397 Cap : Boolean) return Node_Id;
398 -- Compute (Hi - Lo) for two entry family indexes. Hi is the index in an
399 -- accept statement, or the upper bound in the discrete subtype of an entry
400 -- declaration. Lo is the corresponding lower bound. Ttyp is the concurrent
401 -- type of the entry. If Cap is true, the result is capped according to
402 -- Entry_Family_Bound.
404 function Family_Size
405 (Loc : Source_Ptr;
406 Hi : Node_Id;
407 Lo : Node_Id;
408 Ttyp : Entity_Id;
409 Cap : Boolean) return Node_Id;
410 -- Compute (Hi - Lo) + 1 Max 0, to determine the number of entries in a
411 -- family, and handle properly the superflat case. This is equivalent to
412 -- the use of 'Length on the index type, but must use Family_Offset to
413 -- handle properly the case of bounds that depend on discriminants. If
414 -- Cap is true, the result is capped according to Entry_Family_Bound.
416 procedure Find_Enclosing_Context
417 (N : Node_Id;
418 Context : out Node_Id;
419 Context_Id : out Entity_Id;
420 Context_Decls : out List_Id);
421 -- Subsidiary routine to procedures Build_Activation_Chain_Entity and
422 -- Build_Master_Entity. Given an arbitrary node in the tree, find the
423 -- nearest enclosing body, block, package, or return statement and return
424 -- its constituents. Context is the enclosing construct, Context_Id is
425 -- the scope of Context_Id and Context_Decls is the declarative list of
426 -- Context.
428 function Index_Object (Spec_Id : Entity_Id) return Entity_Id;
429 -- Given a subprogram identifier, return the entity which is associated
430 -- with the protection entry index in the Protected_Body_Subprogram or
431 -- the Task_Body_Procedure of Spec_Id. The returned entity denotes formal
432 -- parameter _E.
434 function Is_Potentially_Large_Family
435 (Base_Index : Entity_Id;
436 Conctyp : Entity_Id;
437 Lo : Node_Id;
438 Hi : Node_Id) return Boolean;
439 -- Determine whether an entry family is potentially large because one of
440 -- its bounds denotes a discrminant.
442 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean;
443 -- Determine whether Id is a function or a procedure and is marked as a
444 -- private primitive.
446 function Null_Statements (Stats : List_Id) return Boolean;
447 -- Used to check DO-END sequence. Checks for equivalent of DO NULL; END.
448 -- Allows labels, and pragma Warnings/Unreferenced in the sequence as well
449 -- to still count as null. Returns True for a null sequence. The argument
450 -- is the list of statements from the DO-END sequence.
452 function Parameter_Block_Pack
453 (Loc : Source_Ptr;
454 Blk_Typ : Entity_Id;
455 Actuals : List_Id;
456 Formals : List_Id;
457 Decls : List_Id;
458 Stmts : List_Id) return Entity_Id;
459 -- Set the components of the generated parameter block with the values
460 -- of the actual parameters. Generate aliased temporaries to capture the
461 -- values for types that are passed by copy. Otherwise generate a reference
462 -- to the actual's value. Return the address of the aggregate block.
463 -- Generate:
464 -- Jnn1 : alias <formal-type1>;
465 -- Jnn1 := <actual1>;
466 -- ...
467 -- P : Blk_Typ := (
468 -- Jnn1'unchecked_access;
469 -- <actual2>'reference;
470 -- ...);
472 function Parameter_Block_Unpack
473 (Loc : Source_Ptr;
474 P : Entity_Id;
475 Actuals : List_Id;
476 Formals : List_Id) return List_Id;
477 -- Retrieve the values of the components from the parameter block and
478 -- assign then to the original actual parameters. Generate:
479 -- <actual1> := P.<formal1>;
480 -- ...
481 -- <actualN> := P.<formalN>;
483 procedure Reset_Scopes_To (Bod : Node_Id; E : Entity_Id);
484 -- Reset the scope of declarations and blocks at the top level of Bod to
485 -- be E. Bod is either a block or a subprogram body. Used after expanding
486 -- various kinds of entry bodies into their corresponding constructs. This
487 -- is needed during unnesting to determine whether a body generated for an
488 -- entry or an accept alternative includes uplevel references.
490 function Trivial_Accept_OK return Boolean;
491 -- If there is no DO-END block for an accept, or if the DO-END block has
492 -- only null statements, then it is possible to do the Rendezvous with much
493 -- less overhead using the Accept_Trivial routine in the run-time library.
494 -- However, this is not always a valid optimization. Whether it is valid or
495 -- not depends on the Task_Dispatching_Policy. The issue is whether a full
496 -- rescheduling action is required or not. In FIFO_Within_Priorities, such
497 -- a rescheduling is required, so this optimization is not allowed. This
498 -- function returns True if the optimization is permitted.
500 -----------------------------
501 -- Actual_Index_Expression --
502 -----------------------------
504 function Actual_Index_Expression
505 (Sloc : Source_Ptr;
506 Ent : Entity_Id;
507 Index : Node_Id;
508 Tsk : Entity_Id) return Node_Id
510 Ttyp : constant Entity_Id := Etype (Tsk);
511 Expr : Node_Id;
512 Num : Node_Id;
513 Lo : Node_Id;
514 Hi : Node_Id;
515 Prev : Entity_Id;
516 S : Node_Id;
518 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id;
519 -- Compute difference between bounds of entry family
521 --------------------------
522 -- Actual_Family_Offset --
523 --------------------------
525 function Actual_Family_Offset (Hi, Lo : Node_Id) return Node_Id is
527 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
528 -- Replace a reference to a discriminant with a selected component
529 -- denoting the discriminant of the target task.
531 -----------------------------
532 -- Actual_Discriminant_Ref --
533 -----------------------------
535 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
536 Typ : constant Entity_Id := Etype (Bound);
537 B : Node_Id;
539 begin
540 if not Is_Entity_Name (Bound)
541 or else Ekind (Entity (Bound)) /= E_Discriminant
542 then
543 if Nkind (Bound) = N_Attribute_Reference then
544 return Bound;
545 else
546 B := New_Copy_Tree (Bound);
547 end if;
549 else
550 B :=
551 Make_Selected_Component (Sloc,
552 Prefix => New_Copy_Tree (Tsk),
553 Selector_Name => New_Occurrence_Of (Entity (Bound), Sloc));
555 Analyze_And_Resolve (B, Typ);
556 end if;
558 return
559 Make_Attribute_Reference (Sloc,
560 Attribute_Name => Name_Pos,
561 Prefix => New_Occurrence_Of (Etype (Bound), Sloc),
562 Expressions => New_List (B));
563 end Actual_Discriminant_Ref;
565 -- Start of processing for Actual_Family_Offset
567 begin
568 return
569 Make_Op_Subtract (Sloc,
570 Left_Opnd => Actual_Discriminant_Ref (Hi),
571 Right_Opnd => Actual_Discriminant_Ref (Lo));
572 end Actual_Family_Offset;
574 -- Start of processing for Actual_Index_Expression
576 begin
577 -- The queues of entries and entry families appear in textual order in
578 -- the associated record. The entry index is computed as the sum of the
579 -- number of queues for all entries that precede the designated one, to
580 -- which is added the index expression, if this expression denotes a
581 -- member of a family.
583 -- The following is a place holder for the count of simple entries
585 Num := Make_Integer_Literal (Sloc, 1);
587 -- We construct an expression which is a series of addition operations.
588 -- See comments in Entry_Index_Expression, which is identical in
589 -- structure.
591 if Present (Index) then
592 S := Entry_Index_Type (Ent);
594 -- First make sure the index is in range if requested. The index type
595 -- has been directly set on the prefix, see Resolve_Entry.
597 if Do_Range_Check (Index) then
598 Generate_Range_Check
599 (Index, Etype (Prefix (Parent (Index))), CE_Range_Check_Failed);
600 end if;
602 Expr :=
603 Make_Op_Add (Sloc,
604 Left_Opnd => Num,
605 Right_Opnd =>
606 Actual_Family_Offset (
607 Make_Attribute_Reference (Sloc,
608 Attribute_Name => Name_Pos,
609 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
610 Expressions => New_List (Relocate_Node (Index))),
611 Type_Low_Bound (S)));
612 else
613 Expr := Num;
614 end if;
616 -- Now add lengths of preceding entries and entry families
618 Prev := First_Entity (Ttyp);
619 while Chars (Prev) /= Chars (Ent)
620 or else Ekind (Prev) /= Ekind (Ent)
621 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
622 loop
623 if Ekind (Prev) = E_Entry then
624 Set_Intval (Num, Intval (Num) + 1);
626 elsif Ekind (Prev) = E_Entry_Family then
627 S := Entry_Index_Type (Prev);
629 -- The need for the following full view retrieval stems from this
630 -- complex case of nested generics and tasking:
632 -- generic
633 -- type Formal_Index is range <>;
634 -- ...
635 -- package Outer is
636 -- type Index is private;
637 -- generic
638 -- ...
639 -- package Inner is
640 -- procedure P;
641 -- end Inner;
642 -- private
643 -- type Index is new Formal_Index range 1 .. 10;
644 -- end Outer;
646 -- package body Outer is
647 -- task type T is
648 -- entry Fam (Index); -- (2)
649 -- entry E;
650 -- end T;
651 -- package body Inner is -- (3)
652 -- procedure P is
653 -- begin
654 -- T.E; -- (1)
655 -- end P;
656 -- end Inner;
657 -- ...
659 -- We are currently building the index expression for the entry
660 -- call "T.E" (1). Part of the expansion must mention the range
661 -- of the discrete type "Index" (2) of entry family "Fam".
663 -- However only the private view of type "Index" is available to
664 -- the inner generic (3) because there was no prior mention of
665 -- the type inside "Inner". This visibility requirement is
666 -- implicit and cannot be detected during the construction of
667 -- the generic trees and needs special handling.
669 if In_Instance_Body
670 and then Is_Private_Type (S)
671 and then Present (Full_View (S))
672 then
673 S := Full_View (S);
674 end if;
676 Lo := Type_Low_Bound (S);
677 Hi := Type_High_Bound (S);
679 Expr :=
680 Make_Op_Add (Sloc,
681 Left_Opnd => Expr,
682 Right_Opnd =>
683 Make_Op_Add (Sloc,
684 Left_Opnd => Actual_Family_Offset (Hi, Lo),
685 Right_Opnd => Make_Integer_Literal (Sloc, 1)));
687 -- Other components are anonymous types to be ignored
689 else
690 null;
691 end if;
693 Next_Entity (Prev);
694 end loop;
696 return Expr;
697 end Actual_Index_Expression;
699 --------------------------
700 -- Add_Formal_Renamings --
701 --------------------------
703 procedure Add_Formal_Renamings
704 (Spec : Node_Id;
705 Decls : List_Id;
706 Ent : Entity_Id;
707 Loc : Source_Ptr)
709 Ptr : constant Entity_Id :=
710 Defining_Identifier
711 (Next (First (Parameter_Specifications (Spec))));
712 -- The name of the formal that holds the address of the parameter block
713 -- for the call.
715 Comp : Entity_Id;
716 Decl : Node_Id;
717 Formal : Entity_Id;
718 New_F : Entity_Id;
719 Renamed_Formal : Node_Id;
721 begin
722 Formal := First_Formal (Ent);
723 while Present (Formal) loop
724 Comp := Entry_Component (Formal);
725 New_F :=
726 Make_Defining_Identifier (Sloc (Formal),
727 Chars => Chars (Formal));
728 Set_Etype (New_F, Etype (Formal));
729 Set_Scope (New_F, Ent);
731 -- Now we set debug info needed on New_F even though it does not come
732 -- from source, so that the debugger will get the right information
733 -- for these generated names.
735 Set_Debug_Info_Needed (New_F);
737 if Ekind (Formal) = E_In_Parameter then
738 Mutate_Ekind (New_F, E_Constant);
739 else
740 Mutate_Ekind (New_F, E_Variable);
741 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
742 end if;
744 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
746 Renamed_Formal :=
747 Make_Selected_Component (Loc,
748 Prefix =>
749 Make_Explicit_Dereference (Loc,
750 Unchecked_Convert_To (Entry_Parameters_Type (Ent),
751 Make_Identifier (Loc, Chars (Ptr)))),
752 Selector_Name => New_Occurrence_Of (Comp, Loc));
754 Decl :=
755 Build_Renamed_Formal_Declaration
756 (New_F, Formal, Comp, Renamed_Formal);
758 Append (Decl, Decls);
759 Set_Renamed_Object (Formal, New_F);
760 Next_Formal (Formal);
761 end loop;
762 end Add_Formal_Renamings;
764 ------------------------
765 -- Add_Object_Pointer --
766 ------------------------
768 procedure Add_Object_Pointer
769 (Loc : Source_Ptr;
770 Conc_Typ : Entity_Id;
771 Decls : List_Id)
773 Rec_Typ : constant Entity_Id := Corresponding_Record_Type (Conc_Typ);
774 Decl : Node_Id;
775 Obj_Ptr : Node_Id;
777 begin
778 -- Create the renaming declaration for the Protection object of a
779 -- protected type. _Object is used by Complete_Entry_Body.
780 -- ??? An attempt to make this a renaming was unsuccessful.
782 -- Build the entity for the access type
784 Obj_Ptr :=
785 Make_Defining_Identifier (Loc,
786 New_External_Name (Chars (Rec_Typ), 'P'));
788 -- Generate:
789 -- _object : poVP := poVP!O;
791 Decl :=
792 Make_Object_Declaration (Loc,
793 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uObject),
794 Object_Definition => New_Occurrence_Of (Obj_Ptr, Loc),
795 Expression =>
796 Unchecked_Convert_To (Obj_Ptr, Make_Identifier (Loc, Name_uO)));
797 Set_Debug_Info_Needed (Defining_Identifier (Decl));
798 Prepend_To (Decls, Decl);
800 -- Generate:
801 -- type poVP is access poV;
803 Decl :=
804 Make_Full_Type_Declaration (Loc,
805 Defining_Identifier =>
806 Obj_Ptr,
807 Type_Definition =>
808 Make_Access_To_Object_Definition (Loc,
809 Subtype_Indication =>
810 New_Occurrence_Of (Rec_Typ, Loc)));
811 Set_Debug_Info_Needed (Defining_Identifier (Decl));
812 Prepend_To (Decls, Decl);
813 end Add_Object_Pointer;
815 -----------------------
816 -- Build_Accept_Body --
817 -----------------------
819 function Build_Accept_Body (Astat : Node_Id) return Node_Id is
820 Loc : constant Source_Ptr := Sloc (Astat);
821 Stats : constant Node_Id := Handled_Statement_Sequence (Astat);
822 New_S : Node_Id;
823 Hand : Node_Id;
824 Call : Node_Id;
825 Ohandle : Node_Id;
827 begin
828 -- At the end of the statement sequence, Complete_Rendezvous is called.
829 -- A label skipping the Complete_Rendezvous, and all other accept
830 -- processing, has already been added for the expansion of requeue
831 -- statements. The Sloc is copied from the last statement since it
832 -- is really part of this last statement.
834 Call :=
835 Build_Runtime_Call
836 (Sloc (Last (Statements (Stats))), RE_Complete_Rendezvous);
837 Insert_Before (Last (Statements (Stats)), Call);
838 Analyze (Call);
840 -- Ada 2022 (AI12-0279)
842 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
843 and then RTE_Available (RE_Yield)
844 then
845 Insert_Action_After (Call,
846 Make_Procedure_Call_Statement (Loc,
847 New_Occurrence_Of (RTE (RE_Yield), Loc)));
848 end if;
850 -- If exception handlers are present, then append Complete_Rendezvous
851 -- calls to the handlers, and construct the required outer block. As
852 -- above, the Sloc is copied from the last statement in the sequence.
854 if Present (Exception_Handlers (Stats)) then
855 Hand := First (Exception_Handlers (Stats));
856 while Present (Hand) loop
857 Call :=
858 Build_Runtime_Call
859 (Sloc (Last (Statements (Hand))), RE_Complete_Rendezvous);
860 Append (Call, Statements (Hand));
861 Analyze (Call);
863 -- Ada 2022 (AI12-0279)
865 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
866 and then RTE_Available (RE_Yield)
867 then
868 Insert_Action_After (Call,
869 Make_Procedure_Call_Statement (Loc,
870 New_Occurrence_Of (RTE (RE_Yield), Loc)));
871 end if;
873 Next (Hand);
874 end loop;
876 New_S :=
877 Make_Handled_Sequence_Of_Statements (Loc,
878 Statements => New_List (
879 Make_Block_Statement (Loc,
880 Handled_Statement_Sequence => Stats)));
882 else
883 New_S := Stats;
884 end if;
886 -- At this stage we know that the new statement sequence does
887 -- not have an exception handler part, so we supply one to call
888 -- Exceptional_Complete_Rendezvous. This handler is
890 -- when all others =>
891 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
893 -- We handle Abort_Signal to make sure that we properly catch the abort
894 -- case and wake up the caller.
896 Call :=
897 Make_Procedure_Call_Statement (Sloc (Stats),
898 Name => New_Occurrence_Of (
899 RTE (RE_Exceptional_Complete_Rendezvous), Sloc (Stats)),
900 Parameter_Associations => New_List (
901 Make_Function_Call (Sloc (Stats),
902 Name =>
903 New_Occurrence_Of
904 (RTE (RE_Get_GNAT_Exception), Sloc (Stats)))));
906 Ohandle := Make_Others_Choice (Loc);
907 Set_All_Others (Ohandle);
909 Set_Exception_Handlers (New_S,
910 New_List (
911 Make_Implicit_Exception_Handler (Loc,
912 Exception_Choices => New_List (Ohandle),
914 Statements => New_List (Call))));
916 -- Ada 2022 (AI12-0279)
918 if Has_Yield_Aspect (Entity (Entry_Direct_Name (Astat)))
919 and then RTE_Available (RE_Yield)
920 then
921 Insert_Action_After (Call,
922 Make_Procedure_Call_Statement (Loc,
923 New_Occurrence_Of (RTE (RE_Yield), Loc)));
924 end if;
926 Set_Parent (New_S, Astat); -- temp parent for Analyze call
927 Analyze_Exception_Handlers (Exception_Handlers (New_S));
928 Expand_Exception_Handlers (New_S);
930 -- Exceptional_Complete_Rendezvous must be called with abort still
931 -- deferred, which is the case for a "when all others" handler.
933 return New_S;
934 end Build_Accept_Body;
936 -----------------------------------
937 -- Build_Activation_Chain_Entity --
938 -----------------------------------
940 procedure Build_Activation_Chain_Entity (N : Node_Id) is
941 function Has_Activation_Chain (Stmt : Node_Id) return Boolean;
942 -- Determine whether an extended return statement has activation chain
944 --------------------------
945 -- Has_Activation_Chain --
946 --------------------------
948 function Has_Activation_Chain (Stmt : Node_Id) return Boolean is
949 Decl : Node_Id;
951 begin
952 Decl := First (Return_Object_Declarations (Stmt));
953 while Present (Decl) loop
954 if Nkind (Decl) = N_Object_Declaration
955 and then Chars (Defining_Identifier (Decl)) = Name_uChain
956 then
957 return True;
958 end if;
960 Next (Decl);
961 end loop;
963 return False;
964 end Has_Activation_Chain;
966 -- Local variables
968 Context : Node_Id;
969 Context_Id : Entity_Id;
970 Decls : List_Id;
972 -- Start of processing for Build_Activation_Chain_Entity
974 begin
975 -- No action needed if the run-time has no tasking support
977 if Global_No_Tasking then
978 return;
979 end if;
981 -- Activation chain is never used for sequential elaboration policy, see
982 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
984 if Partition_Elaboration_Policy = 'S' then
985 return;
986 end if;
988 Find_Enclosing_Context (N, Context, Context_Id, Decls);
990 -- If activation chain entity has not been declared already, create one
992 if Nkind (Context) = N_Extended_Return_Statement
993 or else No (Activation_Chain_Entity (Context))
994 then
995 -- Since extended return statements do not store the entity of the
996 -- chain, examine the return object declarations to avoid creating
997 -- a duplicate.
999 if Nkind (Context) = N_Extended_Return_Statement
1000 and then Has_Activation_Chain (Context)
1001 then
1002 return;
1003 end if;
1005 declare
1006 Loc : constant Source_Ptr := Sloc (Context);
1007 Chain : Entity_Id;
1008 Decl : Node_Id;
1010 begin
1011 Chain := Make_Defining_Identifier (Sloc (N), Name_uChain);
1013 -- Note: An extended return statement is not really a task
1014 -- activator, but it does have an activation chain on which to
1015 -- store the tasks temporarily. On successful return, the tasks
1016 -- on this chain are moved to the chain passed in by the caller.
1017 -- We do not build an Activation_Chain_Entity for an extended
1018 -- return statement, because we do not want to build a call to
1019 -- Activate_Tasks. Task activation is the responsibility of the
1020 -- caller.
1022 if Nkind (Context) /= N_Extended_Return_Statement then
1023 Set_Activation_Chain_Entity (Context, Chain);
1024 end if;
1026 Decl :=
1027 Make_Object_Declaration (Loc,
1028 Defining_Identifier => Chain,
1029 Aliased_Present => True,
1030 Object_Definition =>
1031 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc));
1033 Prepend_To (Decls, Decl);
1035 -- Ensure that _chain appears in the proper scope of the context
1037 if Context_Id /= Current_Scope then
1038 Push_Scope (Context_Id);
1039 Analyze (Decl);
1040 Pop_Scope;
1041 else
1042 Analyze (Decl);
1043 end if;
1044 end;
1045 end if;
1046 end Build_Activation_Chain_Entity;
1048 ----------------------------
1049 -- Build_Barrier_Function --
1050 ----------------------------
1052 function Build_Barrier_Function
1053 (N : Node_Id;
1054 Ent : Entity_Id;
1055 Pid : Entity_Id) return Node_Id
1057 Ent_Formals : constant Node_Id := Entry_Body_Formal_Part (N);
1058 Cond : constant Node_Id := Condition (Ent_Formals);
1059 Loc : constant Source_Ptr := Sloc (Cond);
1060 Func_Id : constant Entity_Id := Barrier_Function (Ent);
1061 Op_Decls : constant List_Id := New_List;
1062 Stmt : Node_Id;
1063 Func_Body : Node_Id;
1065 begin
1066 -- Add a declaration for the Protection object, renaming declarations
1067 -- for the discriminals and privals and finally a declaration for the
1068 -- entry family index (if applicable).
1070 Install_Private_Data_Declarations (Sloc (N),
1071 Spec_Id => Func_Id,
1072 Conc_Typ => Pid,
1073 Body_Nod => N,
1074 Decls => Op_Decls,
1075 Barrier => True,
1076 Family => Ekind (Ent) = E_Entry_Family);
1078 -- If compiling with -fpreserve-control-flow, make sure we insert an
1079 -- IF statement so that the back-end knows to generate a conditional
1080 -- branch instruction, even if the condition is just the name of a
1081 -- boolean object. Note that Expand_N_If_Statement knows to preserve
1082 -- such redundant IF statements under -fpreserve-control-flow
1083 -- (whether coming from this routine, or directly from source).
1085 if Opt.Suppress_Control_Flow_Optimizations then
1086 Stmt :=
1087 Make_Implicit_If_Statement (Cond,
1088 Condition => Cond,
1089 Then_Statements => New_List (
1090 Make_Simple_Return_Statement (Loc,
1091 New_Occurrence_Of (Standard_True, Loc))),
1093 Else_Statements => New_List (
1094 Make_Simple_Return_Statement (Loc,
1095 New_Occurrence_Of (Standard_False, Loc))));
1097 else
1098 Stmt := Make_Simple_Return_Statement (Loc, Cond);
1099 end if;
1101 -- Note: the condition in the barrier function needs to be properly
1102 -- processed for the C/Fortran boolean possibility, but this happens
1103 -- automatically since the return statement does this normalization.
1105 Func_Body :=
1106 Make_Subprogram_Body (Loc,
1107 Specification =>
1108 Build_Barrier_Function_Specification (Loc,
1109 Make_Defining_Identifier (Loc, Chars (Func_Id))),
1110 Declarations => Op_Decls,
1111 Handled_Statement_Sequence =>
1112 Make_Handled_Sequence_Of_Statements (Loc,
1113 Statements => New_List (Stmt)));
1114 Set_Is_Entry_Barrier_Function (Func_Body);
1116 return Func_Body;
1117 end Build_Barrier_Function;
1119 ------------------------------------------
1120 -- Build_Barrier_Function_Specification --
1121 ------------------------------------------
1123 function Build_Barrier_Function_Specification
1124 (Loc : Source_Ptr;
1125 Def_Id : Entity_Id) return Node_Id
1127 begin
1128 Set_Debug_Info_Needed (Def_Id);
1130 return
1131 Make_Function_Specification (Loc,
1132 Defining_Unit_Name => Def_Id,
1133 Parameter_Specifications => New_List (
1134 Make_Parameter_Specification (Loc,
1135 Defining_Identifier =>
1136 Make_Defining_Identifier (Loc, Name_uO),
1137 Parameter_Type =>
1138 New_Occurrence_Of (RTE (RE_Address), Loc)),
1140 Make_Parameter_Specification (Loc,
1141 Defining_Identifier =>
1142 Make_Defining_Identifier (Loc, Name_uE),
1143 Parameter_Type =>
1144 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
1146 Result_Definition =>
1147 New_Occurrence_Of (Standard_Boolean, Loc));
1148 end Build_Barrier_Function_Specification;
1150 --------------------------
1151 -- Build_Call_With_Task --
1152 --------------------------
1154 function Build_Call_With_Task
1155 (N : Node_Id;
1156 E : Entity_Id) return Node_Id
1158 Loc : constant Source_Ptr := Sloc (N);
1159 begin
1160 return
1161 Make_Function_Call (Loc,
1162 Name => New_Occurrence_Of (E, Loc),
1163 Parameter_Associations => New_List (Concurrent_Ref (N)));
1164 end Build_Call_With_Task;
1166 -----------------------------
1167 -- Build_Class_Wide_Master --
1168 -----------------------------
1170 procedure Build_Class_Wide_Master (Typ : Entity_Id) is
1171 Loc : constant Source_Ptr := Sloc (Typ);
1172 Master_Decl : Node_Id;
1173 Master_Id : Entity_Id;
1174 Master_Scope : Entity_Id;
1175 Name_Id : Node_Id;
1176 Related_Node : Node_Id;
1177 Ren_Decl : Node_Id;
1179 begin
1180 -- No action needed if the run-time has no tasking support
1182 if Global_No_Tasking then
1183 return;
1184 end if;
1186 -- Find the declaration that created the access type, which is either a
1187 -- type declaration, or an object declaration with an access definition,
1188 -- in which case the type is anonymous.
1190 if Is_Itype (Typ) then
1191 Related_Node := Associated_Node_For_Itype (Typ);
1192 else
1193 Related_Node := Parent (Typ);
1194 end if;
1196 Master_Scope := Find_Master_Scope (Typ);
1198 -- Nothing to do if the master scope already contains a _master entity.
1199 -- The only exception to this is the following scenario:
1201 -- Source_Scope
1202 -- Transient_Scope_1
1203 -- _master
1205 -- Transient_Scope_2
1206 -- use of master
1208 -- In this case the source scope is marked as having the master entity
1209 -- even though the actual declaration appears inside an inner scope. If
1210 -- the second transient scope requires a _master, it cannot use the one
1211 -- already declared because the entity is not visible.
1213 Name_Id := Make_Identifier (Loc, Name_uMaster);
1214 Master_Decl := Empty;
1216 if not Has_Master_Entity (Master_Scope)
1217 or else No (Current_Entity_In_Scope (Name_Id))
1218 then
1219 declare
1220 Ins_Nod : Node_Id;
1221 Par_Nod : Node_Id;
1223 begin
1224 Master_Decl := Build_Master_Declaration (Loc);
1226 -- Ensure that the master declaration is placed before its use
1228 Ins_Nod := Find_Hook_Context (Related_Node);
1229 while not Is_List_Member (Ins_Nod) loop
1230 Ins_Nod := Parent (Ins_Nod);
1231 end loop;
1233 Par_Nod := Parent (List_Containing (Ins_Nod));
1235 -- For internal blocks created by Wrap_Loop_Statement, Wrap_
1236 -- Statements_In_Block, and Build_Abort_Undefer_Block, remember
1237 -- that they have a task master entity declaration; required by
1238 -- Build_Master_Entity to avoid creating another master entity,
1239 -- and also ensures that subsequent calls to Find_Master_Scope
1240 -- return this scope as the master scope of Typ.
1242 if Is_Internal_Block (Par_Nod) then
1243 Set_Has_Master_Entity (Entity (Identifier (Par_Nod)));
1245 elsif Nkind (Par_Nod) = N_Handled_Sequence_Of_Statements
1246 and then Is_Internal_Block (Parent (Par_Nod))
1247 then
1248 Set_Has_Master_Entity (Entity (Identifier (Parent (Par_Nod))));
1250 -- Otherwise remember that this scope has an associated task
1251 -- master entity declaration.
1253 else
1254 Set_Has_Master_Entity (Master_Scope);
1255 end if;
1257 Insert_Before (First (List_Containing (Ins_Nod)), Master_Decl);
1258 Analyze (Master_Decl);
1260 -- Mark the containing scope as a task master. Masters associated
1261 -- with return statements are already marked at this stage (see
1262 -- Analyze_Subprogram_Body).
1264 if Ekind (Current_Scope) /= E_Return_Statement then
1265 declare
1266 Par : Node_Id := Related_Node;
1268 begin
1269 while Nkind (Par) /= N_Compilation_Unit loop
1270 Par := Parent (Par);
1272 -- If we fall off the top, we are at the outer level,
1273 -- and the environment task is our effective master,
1274 -- so nothing to mark.
1276 if Nkind (Par) in
1277 N_Block_Statement | N_Subprogram_Body | N_Task_Body
1278 then
1279 Set_Is_Task_Master (Par);
1280 exit;
1281 end if;
1282 end loop;
1283 end;
1284 end if;
1285 end;
1286 end if;
1288 Master_Id :=
1289 Make_Defining_Identifier (Loc, New_External_Name (Chars (Typ), 'M'));
1291 -- Generate:
1292 -- typeMnn renames _master;
1294 Ren_Decl :=
1295 Make_Object_Renaming_Declaration (Loc,
1296 Defining_Identifier => Master_Id,
1297 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
1298 Name => Name_Id);
1300 -- If the master is declared locally, add the renaming declaration
1301 -- immediately after it, to prevent access-before-elaboration in the
1302 -- back-end.
1304 if Present (Master_Decl) then
1305 Insert_After (Master_Decl, Ren_Decl);
1306 Analyze (Ren_Decl);
1308 else
1309 Insert_Action (Related_Node, Ren_Decl);
1310 end if;
1312 Set_Master_Id (Typ, Master_Id);
1313 end Build_Class_Wide_Master;
1315 --------------------------------
1316 -- Build_Corresponding_Record --
1317 --------------------------------
1319 function Build_Corresponding_Record
1320 (N : Node_Id;
1321 Ctyp : Entity_Id;
1322 Loc : Source_Ptr) return Node_Id
1324 Rec_Ent : constant Entity_Id :=
1325 Make_Defining_Identifier
1326 (Loc, New_External_Name (Chars (Ctyp), 'V'));
1327 Disc : Entity_Id;
1328 Dlist : List_Id;
1329 New_Disc : Entity_Id;
1330 Cdecls : List_Id;
1332 begin
1333 Set_Corresponding_Record_Type (Ctyp, Rec_Ent);
1334 Mutate_Ekind (Rec_Ent, E_Record_Type);
1335 Set_Has_Delayed_Freeze (Rec_Ent, Has_Delayed_Freeze (Ctyp));
1336 Set_Is_Concurrent_Record_Type (Rec_Ent, True);
1337 Set_Corresponding_Concurrent_Type (Rec_Ent, Ctyp);
1338 Set_Stored_Constraint (Rec_Ent, No_Elist);
1339 Cdecls := New_List;
1341 -- Use discriminals to create list of discriminants for record, and
1342 -- create new discriminals for use in default expressions, etc. It is
1343 -- worth noting that a task discriminant gives rise to 5 entities;
1345 -- a) The original discriminant.
1346 -- b) The discriminal for use in the task.
1347 -- c) The discriminant of the corresponding record.
1348 -- d) The discriminal for the init proc of the corresponding record.
1349 -- e) The local variable that renames the discriminant in the procedure
1350 -- for the task body.
1352 -- In fact the discriminals b) are used in the renaming declarations
1353 -- for e). See details in einfo (Handling of Discriminants).
1355 if Present (Discriminant_Specifications (N)) then
1356 Dlist := New_List;
1357 Disc := First_Discriminant (Ctyp);
1359 while Present (Disc) loop
1360 New_Disc := CR_Discriminant (Disc);
1362 Append_To (Dlist,
1363 Make_Discriminant_Specification (Loc,
1364 Defining_Identifier => New_Disc,
1365 Discriminant_Type =>
1366 New_Occurrence_Of (Etype (Disc), Loc),
1367 Expression =>
1368 New_Copy (Discriminant_Default_Value (Disc))));
1370 Next_Discriminant (Disc);
1371 end loop;
1373 else
1374 Dlist := No_List;
1375 end if;
1377 -- Now we can construct the record type declaration. Note that this
1378 -- record is "limited tagged". It is "limited" to reflect the underlying
1379 -- limitedness of the task or protected object that it represents, and
1380 -- ensuring for example that it is properly passed by reference. It is
1381 -- "tagged" to give support to dispatching calls through interfaces. We
1382 -- propagate here the list of interfaces covered by the concurrent type
1383 -- (Ada 2005: AI-345).
1385 return
1386 Make_Full_Type_Declaration (Loc,
1387 Defining_Identifier => Rec_Ent,
1388 Discriminant_Specifications => Dlist,
1389 Type_Definition =>
1390 Make_Record_Definition (Loc,
1391 Component_List =>
1392 Make_Component_List (Loc, Component_Items => Cdecls),
1393 Tagged_Present =>
1394 Ada_Version >= Ada_2005 and then Is_Tagged_Type (Ctyp),
1395 Interface_List => Interface_List (N),
1396 Limited_Present => True));
1397 end Build_Corresponding_Record;
1399 ---------------------------------
1400 -- Build_Dispatching_Tag_Check --
1401 ---------------------------------
1403 function Build_Dispatching_Tag_Check
1404 (K : Entity_Id;
1405 N : Node_Id) return Node_Id
1407 Loc : constant Source_Ptr := Sloc (N);
1409 begin
1410 return
1411 Make_Op_Or (Loc,
1412 Make_Op_Eq (Loc,
1413 Left_Opnd =>
1414 New_Occurrence_Of (K, Loc),
1415 Right_Opnd =>
1416 New_Occurrence_Of (RTE (RE_TK_Limited_Tagged), Loc)),
1418 Make_Op_Eq (Loc,
1419 Left_Opnd =>
1420 New_Occurrence_Of (K, Loc),
1421 Right_Opnd =>
1422 New_Occurrence_Of (RTE (RE_TK_Tagged), Loc)));
1423 end Build_Dispatching_Tag_Check;
1425 ----------------------------------
1426 -- Build_Entry_Count_Expression --
1427 ----------------------------------
1429 function Build_Entry_Count_Expression
1430 (Concurrent_Type : Entity_Id;
1431 Loc : Source_Ptr) return Node_Id
1433 Eindx : Nat;
1434 Ent : Entity_Id;
1435 Ecount : Node_Id;
1436 Lo : Node_Id;
1437 Hi : Node_Id;
1438 Typ : Entity_Id;
1439 Large : Boolean;
1441 begin
1442 -- Count number of non-family entries
1444 Eindx := 0;
1445 Ent := First_Entity (Concurrent_Type);
1446 while Present (Ent) loop
1447 if Ekind (Ent) = E_Entry then
1448 Eindx := Eindx + 1;
1449 end if;
1451 Next_Entity (Ent);
1452 end loop;
1454 Ecount := Make_Integer_Literal (Loc, Eindx);
1456 -- Loop through entry families building the addition nodes
1458 Ent := First_Entity (Concurrent_Type);
1459 while Present (Ent) loop
1460 if Ekind (Ent) = E_Entry_Family then
1461 Typ := Entry_Index_Type (Ent);
1462 Hi := Type_High_Bound (Typ);
1463 Lo := Type_Low_Bound (Typ);
1464 Large := Is_Potentially_Large_Family
1465 (Base_Type (Typ), Concurrent_Type, Lo, Hi);
1466 Ecount :=
1467 Make_Op_Add (Loc,
1468 Left_Opnd => Ecount,
1469 Right_Opnd =>
1470 Family_Size (Loc, Hi, Lo, Concurrent_Type, Large));
1471 end if;
1473 Next_Entity (Ent);
1474 end loop;
1476 return Ecount;
1477 end Build_Entry_Count_Expression;
1479 ------------------------------
1480 -- Build_Master_Declaration --
1481 ------------------------------
1483 function Build_Master_Declaration (Loc : Source_Ptr) return Node_Id is
1484 Master_Decl : Node_Id;
1486 begin
1487 -- Generate a dummy master if tasks or tasking hierarchies are
1488 -- prohibited.
1490 -- _Master : constant Integer := Library_Task_Level;
1492 if not Tasking_Allowed
1493 or else Restrictions.Set (No_Task_Hierarchy)
1494 or else not RTE_Available (RE_Current_Master)
1495 then
1496 Master_Decl :=
1497 Make_Object_Declaration (Loc,
1498 Defining_Identifier =>
1499 Make_Defining_Identifier (Loc, Name_uMaster),
1500 Constant_Present => True,
1501 Object_Definition =>
1502 New_Occurrence_Of (Standard_Integer, Loc),
1503 Expression =>
1504 Make_Integer_Literal (Loc, Library_Task_Level));
1506 -- Generate:
1507 -- _master : constant Integer := Current_Master.all;
1509 else
1510 Master_Decl :=
1511 Make_Object_Declaration (Loc,
1512 Defining_Identifier =>
1513 Make_Defining_Identifier (Loc, Name_uMaster),
1514 Constant_Present => True,
1515 Object_Definition =>
1516 New_Occurrence_Of (Standard_Integer, Loc),
1517 Expression =>
1518 Make_Explicit_Dereference (Loc,
1519 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
1520 end if;
1522 return Master_Decl;
1523 end Build_Master_Declaration;
1525 ---------------------------
1526 -- Build_Parameter_Block --
1527 ---------------------------
1529 function Build_Parameter_Block
1530 (Loc : Source_Ptr;
1531 Actuals : List_Id;
1532 Formals : List_Id;
1533 Decls : List_Id) return Entity_Id
1535 Actual : Entity_Id;
1536 Comp_Nam : Node_Id;
1537 Comps : List_Id;
1538 Formal : Entity_Id;
1539 Has_Comp : Boolean := False;
1540 Rec_Nam : Node_Id;
1542 begin
1543 Actual := First (Actuals);
1544 Comps := New_List;
1545 Formal := Defining_Identifier (First (Formals));
1547 while Present (Actual) loop
1548 if not Is_Controlling_Actual (Actual) then
1550 -- Generate:
1551 -- type Ann is access all <actual-type>
1553 Comp_Nam := Make_Temporary (Loc, 'A');
1554 Set_Is_Param_Block_Component_Type (Comp_Nam);
1556 Append_To (Decls,
1557 Make_Full_Type_Declaration (Loc,
1558 Defining_Identifier => Comp_Nam,
1559 Type_Definition =>
1560 Make_Access_To_Object_Definition (Loc,
1561 All_Present => True,
1562 Constant_Present => Ekind (Formal) = E_In_Parameter,
1563 Subtype_Indication =>
1564 New_Occurrence_Of (Etype (Actual), Loc))));
1566 -- Generate:
1567 -- Param : Ann;
1569 Append_To (Comps,
1570 Make_Component_Declaration (Loc,
1571 Defining_Identifier =>
1572 Make_Defining_Identifier (Loc, Chars (Formal)),
1573 Component_Definition =>
1574 Make_Component_Definition (Loc,
1575 Aliased_Present =>
1576 False,
1577 Subtype_Indication =>
1578 New_Occurrence_Of (Comp_Nam, Loc))));
1580 Has_Comp := True;
1581 end if;
1583 Next_Actual (Actual);
1584 Next_Formal_With_Extras (Formal);
1585 end loop;
1587 Rec_Nam := Make_Temporary (Loc, 'P');
1589 if Has_Comp then
1591 -- Generate:
1592 -- type Pnn is record
1593 -- Param1 : Ann1;
1594 -- ...
1595 -- ParamN : AnnN;
1597 -- where Pnn is a parameter wrapping record, Param1 .. ParamN are
1598 -- the original parameter names and Ann1 .. AnnN are the access to
1599 -- actual types.
1601 Append_To (Decls,
1602 Make_Full_Type_Declaration (Loc,
1603 Defining_Identifier =>
1604 Rec_Nam,
1605 Type_Definition =>
1606 Make_Record_Definition (Loc,
1607 Component_List =>
1608 Make_Component_List (Loc, Comps))));
1609 else
1610 -- Generate:
1611 -- type Pnn is null record;
1613 Append_To (Decls,
1614 Make_Full_Type_Declaration (Loc,
1615 Defining_Identifier =>
1616 Rec_Nam,
1617 Type_Definition =>
1618 Make_Record_Definition (Loc,
1619 Null_Present => True,
1620 Component_List => Empty)));
1621 end if;
1623 return Rec_Nam;
1624 end Build_Parameter_Block;
1626 --------------------------------------
1627 -- Build_Renamed_Formal_Declaration --
1628 --------------------------------------
1630 function Build_Renamed_Formal_Declaration
1631 (New_F : Entity_Id;
1632 Formal : Entity_Id;
1633 Comp : Entity_Id;
1634 Renamed_Formal : Node_Id) return Node_Id
1636 Loc : constant Source_Ptr := Sloc (New_F);
1637 Decl : Node_Id;
1639 begin
1640 -- If the formal is a tagged incomplete type, it is already passed
1641 -- by reference, so it is sufficient to rename the pointer component
1642 -- that corresponds to the actual. Otherwise we need to dereference
1643 -- the pointer component to obtain the actual.
1645 if Is_Incomplete_Type (Etype (Formal))
1646 and then Is_Tagged_Type (Etype (Formal))
1647 then
1648 Decl :=
1649 Make_Object_Renaming_Declaration (Loc,
1650 Defining_Identifier => New_F,
1651 Subtype_Mark => New_Occurrence_Of (Etype (Comp), Loc),
1652 Name => Renamed_Formal);
1654 else
1655 Decl :=
1656 Make_Object_Renaming_Declaration (Loc,
1657 Defining_Identifier => New_F,
1658 Subtype_Mark => New_Occurrence_Of (Etype (Formal), Loc),
1659 Name =>
1660 Make_Explicit_Dereference (Loc, Renamed_Formal));
1661 end if;
1663 return Decl;
1664 end Build_Renamed_Formal_Declaration;
1666 --------------------------
1667 -- Build_Wrapper_Bodies --
1668 --------------------------
1670 procedure Build_Wrapper_Bodies
1671 (Loc : Source_Ptr;
1672 Typ : Entity_Id;
1673 N : Node_Id)
1675 Rec_Typ : Entity_Id;
1677 function Build_Wrapper_Body
1678 (Loc : Source_Ptr;
1679 Subp_Id : Entity_Id;
1680 Obj_Typ : Entity_Id;
1681 Formals : List_Id) return Node_Id;
1682 -- Ada 2005 (AI-345): Build the body that wraps a primitive operation
1683 -- associated with a protected or task type. Subp_Id is the subprogram
1684 -- name which will be wrapped. Obj_Typ is the type of the new formal
1685 -- parameter which handles dispatching and object notation. Formals are
1686 -- the original formals of Subp_Id which will be explicitly replicated.
1688 ------------------------
1689 -- Build_Wrapper_Body --
1690 ------------------------
1692 function Build_Wrapper_Body
1693 (Loc : Source_Ptr;
1694 Subp_Id : Entity_Id;
1695 Obj_Typ : Entity_Id;
1696 Formals : List_Id) return Node_Id
1698 Body_Spec : Node_Id;
1700 begin
1701 Body_Spec := Build_Wrapper_Spec (Subp_Id, Obj_Typ, Formals);
1703 -- The subprogram is not overriding or is not a primitive declared
1704 -- between two views.
1706 if No (Body_Spec) then
1707 return Empty;
1708 end if;
1710 declare
1711 Actuals : List_Id := No_List;
1712 Conv_Id : Node_Id;
1713 First_Form : Node_Id;
1714 Formal : Node_Id;
1715 Nam : Node_Id;
1717 begin
1718 -- Map formals to actuals. Use the list built for the wrapper
1719 -- spec, skipping the object notation parameter.
1721 First_Form := First (Parameter_Specifications (Body_Spec));
1723 Formal := First_Form;
1724 Next (Formal);
1726 if Present (Formal) then
1727 Actuals := New_List;
1728 while Present (Formal) loop
1729 Append_To (Actuals,
1730 Make_Identifier (Loc,
1731 Chars => Chars (Defining_Identifier (Formal))));
1732 Next (Formal);
1733 end loop;
1734 end if;
1736 -- Special processing for primitives declared between a private
1737 -- type and its completion: the wrapper needs a properly typed
1738 -- parameter if the wrapped operation has a controlling first
1739 -- parameter. Note that this might not be the case for a function
1740 -- with a controlling result.
1742 if Is_Private_Primitive_Subprogram (Subp_Id) then
1743 if No (Actuals) then
1744 Actuals := New_List;
1745 end if;
1747 if Is_Controlling_Formal (First_Formal (Subp_Id)) then
1748 Prepend_To (Actuals,
1749 Unchecked_Convert_To
1750 (Corresponding_Concurrent_Type (Obj_Typ),
1751 Make_Identifier (Loc, Name_uO)));
1753 else
1754 Prepend_To (Actuals,
1755 Make_Identifier (Loc,
1756 Chars => Chars (Defining_Identifier (First_Form))));
1757 end if;
1759 Nam := New_Occurrence_Of (Subp_Id, Loc);
1760 else
1761 -- An access-to-variable object parameter requires an explicit
1762 -- dereference in the unchecked conversion. This case occurs
1763 -- when a protected entry wrapper must override an interface
1764 -- level procedure with interface access as first parameter.
1766 -- O.all.Subp_Id (Formal_1, ..., Formal_N)
1768 if Nkind (Parameter_Type (First_Form)) =
1769 N_Access_Definition
1770 then
1771 Conv_Id :=
1772 Make_Explicit_Dereference (Loc,
1773 Prefix => Make_Identifier (Loc, Name_uO));
1774 else
1775 Conv_Id := Make_Identifier (Loc, Name_uO);
1776 end if;
1778 Nam :=
1779 Make_Selected_Component (Loc,
1780 Prefix =>
1781 Unchecked_Convert_To
1782 (Corresponding_Concurrent_Type (Obj_Typ), Conv_Id),
1783 Selector_Name => New_Occurrence_Of (Subp_Id, Loc));
1784 end if;
1786 -- Create the subprogram body. For a function, the call to the
1787 -- actual subprogram has to be converted to the corresponding
1788 -- record if it is a controlling result.
1790 if Ekind (Subp_Id) = E_Function then
1791 declare
1792 Res : Node_Id;
1794 begin
1795 Res :=
1796 Make_Function_Call (Loc,
1797 Name => Nam,
1798 Parameter_Associations => Actuals);
1800 if Has_Controlling_Result (Subp_Id) then
1801 Res :=
1802 Unchecked_Convert_To
1803 (Corresponding_Record_Type (Etype (Subp_Id)), Res);
1804 end if;
1806 return
1807 Make_Subprogram_Body (Loc,
1808 Specification => Body_Spec,
1809 Declarations => Empty_List,
1810 Handled_Statement_Sequence =>
1811 Make_Handled_Sequence_Of_Statements (Loc,
1812 Statements => New_List (
1813 Make_Simple_Return_Statement (Loc, Res))));
1814 end;
1816 else
1817 return
1818 Make_Subprogram_Body (Loc,
1819 Specification => Body_Spec,
1820 Declarations => Empty_List,
1821 Handled_Statement_Sequence =>
1822 Make_Handled_Sequence_Of_Statements (Loc,
1823 Statements => New_List (
1824 Make_Procedure_Call_Statement (Loc,
1825 Name => Nam,
1826 Parameter_Associations => Actuals))));
1827 end if;
1828 end;
1829 end Build_Wrapper_Body;
1831 -- Start of processing for Build_Wrapper_Bodies
1833 begin
1834 if Is_Concurrent_Type (Typ) then
1835 Rec_Typ := Corresponding_Record_Type (Typ);
1836 else
1837 Rec_Typ := Typ;
1838 end if;
1840 -- Generate wrapper bodies for a concurrent type which implements an
1841 -- interface.
1843 if Present (Interfaces (Rec_Typ)) then
1844 declare
1845 Insert_Nod : Node_Id;
1846 Prim : Entity_Id;
1847 Prim_Elmt : Elmt_Id;
1848 Prim_Decl : Node_Id;
1849 Subp : Entity_Id;
1850 Wrap_Body : Node_Id;
1851 Wrap_Id : Entity_Id;
1853 begin
1854 Insert_Nod := N;
1856 -- Examine all primitive operations of the corresponding record
1857 -- type, looking for wrapper specs. Generate bodies in order to
1858 -- complete them.
1860 Prim_Elmt := First_Elmt (Primitive_Operations (Rec_Typ));
1861 while Present (Prim_Elmt) loop
1862 Prim := Node (Prim_Elmt);
1864 if (Ekind (Prim) = E_Function
1865 or else Ekind (Prim) = E_Procedure)
1866 and then Is_Primitive_Wrapper (Prim)
1867 then
1868 Subp := Wrapped_Entity (Prim);
1869 Prim_Decl := Parent (Parent (Prim));
1871 Wrap_Body :=
1872 Build_Wrapper_Body (Loc,
1873 Subp_Id => Subp,
1874 Obj_Typ => Rec_Typ,
1875 Formals => Parameter_Specifications (Parent (Subp)));
1876 Wrap_Id := Defining_Unit_Name (Specification (Wrap_Body));
1878 Set_Corresponding_Spec (Wrap_Body, Prim);
1879 Set_Corresponding_Body (Prim_Decl, Wrap_Id);
1881 Insert_After (Insert_Nod, Wrap_Body);
1882 Insert_Nod := Wrap_Body;
1884 Analyze (Wrap_Body);
1885 end if;
1887 Next_Elmt (Prim_Elmt);
1888 end loop;
1889 end;
1890 end if;
1891 end Build_Wrapper_Bodies;
1893 ------------------------
1894 -- Build_Wrapper_Spec --
1895 ------------------------
1897 function Build_Wrapper_Spec
1898 (Subp_Id : Entity_Id;
1899 Obj_Typ : Entity_Id;
1900 Formals : List_Id) return Node_Id
1902 function Overriding_Possible
1903 (Iface_Op : Entity_Id;
1904 Wrapper : Entity_Id) return Boolean;
1905 -- Determine whether a primitive operation can be overridden by Wrapper.
1906 -- Iface_Op is the candidate primitive operation of an interface type,
1907 -- Wrapper is the generated entry wrapper.
1909 function Replicate_Formals
1910 (Loc : Source_Ptr;
1911 Formals : List_Id) return List_Id;
1912 -- An explicit parameter replication is required due to the Is_Entry_
1913 -- Formal flag being set for all the formals of an entry. The explicit
1914 -- replication removes the flag that would otherwise cause a different
1915 -- path of analysis.
1917 -------------------------
1918 -- Overriding_Possible --
1919 -------------------------
1921 function Overriding_Possible
1922 (Iface_Op : Entity_Id;
1923 Wrapper : Entity_Id) return Boolean
1925 Iface_Op_Spec : constant Node_Id := Parent (Iface_Op);
1926 Wrapper_Spec : constant Node_Id := Parent (Wrapper);
1928 function Type_Conformant_Parameters
1929 (Iface_Op_Params : List_Id;
1930 Wrapper_Params : List_Id) return Boolean;
1931 -- Determine whether the parameters of the generated entry wrapper
1932 -- and those of a primitive operation are type conformant. During
1933 -- this check, the first parameter of the primitive operation is
1934 -- skipped if it is a controlling argument: protected functions
1935 -- may have a controlling result.
1937 --------------------------------
1938 -- Type_Conformant_Parameters --
1939 --------------------------------
1941 function Type_Conformant_Parameters
1942 (Iface_Op_Params : List_Id;
1943 Wrapper_Params : List_Id) return Boolean
1945 Iface_Op_Param : Node_Id;
1946 Iface_Op_Typ : Entity_Id;
1947 Wrapper_Param : Node_Id;
1948 Wrapper_Typ : Entity_Id;
1950 begin
1951 -- Skip the first (controlling) parameter of primitive operation
1953 Iface_Op_Param := First (Iface_Op_Params);
1955 if Present (First_Formal (Iface_Op))
1956 and then Is_Controlling_Formal (First_Formal (Iface_Op))
1957 then
1958 Next (Iface_Op_Param);
1959 end if;
1961 Wrapper_Param := First (Wrapper_Params);
1962 while Present (Iface_Op_Param)
1963 and then Present (Wrapper_Param)
1964 loop
1965 Iface_Op_Typ := Find_Parameter_Type (Iface_Op_Param);
1966 Wrapper_Typ := Find_Parameter_Type (Wrapper_Param);
1968 -- The two parameters must be mode conformant
1970 if not Conforming_Types
1971 (Iface_Op_Typ, Wrapper_Typ, Mode_Conformant)
1972 then
1973 return False;
1974 end if;
1976 Next (Iface_Op_Param);
1977 Next (Wrapper_Param);
1978 end loop;
1980 -- One of the lists is longer than the other
1982 if Present (Iface_Op_Param) or else Present (Wrapper_Param) then
1983 return False;
1984 end if;
1986 return True;
1987 end Type_Conformant_Parameters;
1989 -- Start of processing for Overriding_Possible
1991 begin
1992 if Chars (Iface_Op) /= Chars (Wrapper) then
1993 return False;
1994 end if;
1996 -- If an inherited subprogram is implemented by a protected procedure
1997 -- or an entry, then the first parameter of the inherited subprogram
1998 -- must be of mode OUT or IN OUT, or access-to-variable parameter.
2000 if Ekind (Iface_Op) = E_Procedure
2001 and then Present (Parameter_Specifications (Iface_Op_Spec))
2002 then
2003 declare
2004 Obj_Param : constant Node_Id :=
2005 First (Parameter_Specifications (Iface_Op_Spec));
2006 begin
2007 if not Out_Present (Obj_Param)
2008 and then Nkind (Parameter_Type (Obj_Param)) /=
2009 N_Access_Definition
2010 then
2011 return False;
2012 end if;
2013 end;
2014 end if;
2016 return
2017 Type_Conformant_Parameters
2018 (Parameter_Specifications (Iface_Op_Spec),
2019 Parameter_Specifications (Wrapper_Spec));
2020 end Overriding_Possible;
2022 -----------------------
2023 -- Replicate_Formals --
2024 -----------------------
2026 function Replicate_Formals
2027 (Loc : Source_Ptr;
2028 Formals : List_Id) return List_Id
2030 New_Formals : constant List_Id := New_List;
2031 Formal : Node_Id;
2032 Param_Type : Node_Id;
2034 begin
2035 Formal := First (Formals);
2037 -- Skip the object parameter when dealing with primitives declared
2038 -- between two views.
2040 if Is_Private_Primitive_Subprogram (Subp_Id)
2041 and then not Has_Controlling_Result (Subp_Id)
2042 then
2043 Next (Formal);
2044 end if;
2046 while Present (Formal) loop
2048 -- Create an explicit copy of the entry parameter
2050 -- When creating the wrapper subprogram for a primitive operation
2051 -- of a protected interface we must construct an equivalent
2052 -- signature to that of the overriding operation. For regular
2053 -- parameters we can just use the type of the formal, but for
2054 -- access to subprogram parameters we need to reanalyze the
2055 -- parameter type to create local entities for the signature of
2056 -- the subprogram type. Using the entities of the overriding
2057 -- subprogram will result in out-of-scope errors in the back-end.
2059 if Nkind (Parameter_Type (Formal)) = N_Access_Definition then
2060 Param_Type := Copy_Separate_Tree (Parameter_Type (Formal));
2061 else
2062 Param_Type :=
2063 New_Occurrence_Of (Etype (Parameter_Type (Formal)), Loc);
2064 end if;
2066 Append_To (New_Formals,
2067 Make_Parameter_Specification (Loc,
2068 Defining_Identifier =>
2069 Make_Defining_Identifier (Loc,
2070 Chars => Chars (Defining_Identifier (Formal))),
2071 In_Present => In_Present (Formal),
2072 Out_Present => Out_Present (Formal),
2073 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
2074 Parameter_Type => Param_Type));
2076 Next (Formal);
2077 end loop;
2079 return New_Formals;
2080 end Replicate_Formals;
2082 -- Local variables
2084 Loc : constant Source_Ptr := Sloc (Subp_Id);
2085 First_Param : Node_Id := Empty;
2086 Iface : Entity_Id;
2087 Iface_Elmt : Elmt_Id;
2088 Iface_Op : Entity_Id;
2089 Iface_Op_Elmt : Elmt_Id;
2090 Overridden_Subp : Entity_Id;
2092 -- Start of processing for Build_Wrapper_Spec
2094 begin
2095 -- No point in building wrappers for untagged concurrent types
2097 pragma Assert (Is_Tagged_Type (Obj_Typ));
2099 -- Check if this subprogram has a profile that matches some interface
2100 -- primitive.
2102 Check_Synchronized_Overriding (Subp_Id, Overridden_Subp);
2104 if Present (Overridden_Subp) then
2105 First_Param :=
2106 First (Parameter_Specifications (Parent (Overridden_Subp)));
2108 -- An entry or a protected procedure can override a routine where the
2109 -- controlling formal is either IN OUT, OUT or is of access-to-variable
2110 -- type. Since the wrapper must have the exact same signature as that of
2111 -- the overridden subprogram, we try to find the overriding candidate
2112 -- and use its controlling formal.
2114 -- Check every implemented interface
2116 elsif Present (Interfaces (Obj_Typ)) then
2117 Iface_Elmt := First_Elmt (Interfaces (Obj_Typ));
2118 Search : while Present (Iface_Elmt) loop
2119 Iface := Node (Iface_Elmt);
2121 -- Check every interface primitive
2123 if Present (Primitive_Operations (Iface)) then
2124 Iface_Op_Elmt := First_Elmt (Primitive_Operations (Iface));
2125 while Present (Iface_Op_Elmt) loop
2126 Iface_Op := Node (Iface_Op_Elmt);
2128 -- Ignore predefined primitives
2130 if not Is_Predefined_Dispatching_Operation (Iface_Op) then
2131 Iface_Op := Ultimate_Alias (Iface_Op);
2133 -- The current primitive operation can be overridden by
2134 -- the generated entry wrapper.
2136 if Overriding_Possible (Iface_Op, Subp_Id) then
2137 First_Param :=
2138 First (Parameter_Specifications (Parent (Iface_Op)));
2140 exit Search;
2141 end if;
2142 end if;
2144 Next_Elmt (Iface_Op_Elmt);
2145 end loop;
2146 end if;
2148 Next_Elmt (Iface_Elmt);
2149 end loop Search;
2150 end if;
2152 -- Do not generate the wrapper if no interface primitive is covered by
2153 -- the subprogram and it is not a primitive declared between two views
2154 -- (see Process_Full_View).
2156 if No (First_Param)
2157 and then not Is_Private_Primitive_Subprogram (Subp_Id)
2158 then
2159 return Empty;
2160 end if;
2162 declare
2163 Wrapper_Id : constant Entity_Id :=
2164 Make_Defining_Identifier (Loc, Chars (Subp_Id));
2165 New_Formals : List_Id;
2166 Obj_Param : Node_Id;
2167 Obj_Param_Typ : Entity_Id;
2169 begin
2170 -- Minimum decoration is needed to catch the entity in
2171 -- Sem_Ch6.Override_Dispatching_Operation.
2173 if Ekind (Subp_Id) = E_Function then
2174 Mutate_Ekind (Wrapper_Id, E_Function);
2175 else
2176 Mutate_Ekind (Wrapper_Id, E_Procedure);
2177 end if;
2179 Set_Is_Primitive_Wrapper (Wrapper_Id);
2180 Set_Wrapped_Entity (Wrapper_Id, Subp_Id);
2181 Set_Is_Private_Primitive (Wrapper_Id,
2182 Is_Private_Primitive_Subprogram (Subp_Id));
2184 -- Process the formals
2186 New_Formals := Replicate_Formals (Loc, Formals);
2188 -- A function with a controlling result and no first controlling
2189 -- formal needs no additional parameter.
2191 if Has_Controlling_Result (Subp_Id)
2192 and then
2193 (No (First_Formal (Subp_Id))
2194 or else not Is_Controlling_Formal (First_Formal (Subp_Id)))
2195 then
2196 null;
2198 -- Routine Subp_Id has been found to override an interface primitive.
2199 -- If the interface operation has an access parameter, create a copy
2200 -- of it, with the same null exclusion indicator if present.
2202 elsif Present (First_Param) then
2203 if Nkind (Parameter_Type (First_Param)) = N_Access_Definition then
2204 Obj_Param_Typ :=
2205 Make_Access_Definition (Loc,
2206 Subtype_Mark =>
2207 New_Occurrence_Of (Obj_Typ, Loc),
2208 Null_Exclusion_Present =>
2209 Null_Exclusion_Present (Parameter_Type (First_Param)),
2210 Constant_Present =>
2211 Constant_Present (Parameter_Type (First_Param)));
2212 else
2213 Obj_Param_Typ := New_Occurrence_Of (Obj_Typ, Loc);
2214 end if;
2216 Obj_Param :=
2217 Make_Parameter_Specification (Loc,
2218 Defining_Identifier =>
2219 Make_Defining_Identifier (Loc,
2220 Chars => Name_uO),
2221 In_Present => In_Present (First_Param),
2222 Out_Present => Out_Present (First_Param),
2223 Parameter_Type => Obj_Param_Typ);
2225 Prepend_To (New_Formals, Obj_Param);
2227 -- If we are dealing with a primitive declared between two views,
2228 -- implemented by a synchronized operation, we need to create
2229 -- a default parameter. The mode of the parameter must match that
2230 -- of the primitive operation.
2232 else
2233 pragma Assert (Is_Private_Primitive_Subprogram (Subp_Id));
2235 Obj_Param :=
2236 Make_Parameter_Specification (Loc,
2237 Defining_Identifier =>
2238 Make_Defining_Identifier (Loc, Name_uO),
2239 In_Present =>
2240 In_Present (Parent (First_Entity (Subp_Id))),
2241 Out_Present => Ekind (Subp_Id) /= E_Function,
2242 Parameter_Type => New_Occurrence_Of (Obj_Typ, Loc));
2244 Prepend_To (New_Formals, Obj_Param);
2245 end if;
2247 -- Build the final spec. If it is a function with a controlling
2248 -- result, it is a primitive operation of the corresponding
2249 -- record type, so mark the spec accordingly.
2251 if Ekind (Subp_Id) = E_Function then
2252 declare
2253 Res_Def : Node_Id;
2255 begin
2256 if Has_Controlling_Result (Subp_Id) then
2257 Res_Def :=
2258 New_Occurrence_Of
2259 (Corresponding_Record_Type (Etype (Subp_Id)), Loc);
2260 else
2261 Res_Def := New_Copy (Result_Definition (Parent (Subp_Id)));
2262 end if;
2264 return
2265 Make_Function_Specification (Loc,
2266 Defining_Unit_Name => Wrapper_Id,
2267 Parameter_Specifications => New_Formals,
2268 Result_Definition => Res_Def);
2269 end;
2270 else
2271 return
2272 Make_Procedure_Specification (Loc,
2273 Defining_Unit_Name => Wrapper_Id,
2274 Parameter_Specifications => New_Formals);
2275 end if;
2276 end;
2277 end Build_Wrapper_Spec;
2279 -------------------------
2280 -- Build_Wrapper_Specs --
2281 -------------------------
2283 procedure Build_Wrapper_Specs
2284 (Loc : Source_Ptr;
2285 Typ : Entity_Id;
2286 N : in out Node_Id)
2288 Def : Node_Id;
2289 Rec_Typ : Entity_Id;
2290 procedure Scan_Declarations (L : List_Id);
2291 -- Common processing for visible and private declarations
2292 -- of a protected type.
2294 procedure Scan_Declarations (L : List_Id) is
2295 Decl : Node_Id;
2296 Wrap_Decl : Node_Id;
2297 Wrap_Spec : Node_Id;
2299 begin
2300 if No (L) then
2301 return;
2302 end if;
2304 Decl := First (L);
2305 while Present (Decl) loop
2306 Wrap_Spec := Empty;
2308 if Nkind (Decl) = N_Entry_Declaration
2309 and then Ekind (Defining_Identifier (Decl)) = E_Entry
2310 then
2311 Wrap_Spec :=
2312 Build_Wrapper_Spec
2313 (Subp_Id => Defining_Identifier (Decl),
2314 Obj_Typ => Rec_Typ,
2315 Formals => Parameter_Specifications (Decl));
2317 elsif Nkind (Decl) = N_Subprogram_Declaration then
2318 Wrap_Spec :=
2319 Build_Wrapper_Spec
2320 (Subp_Id => Defining_Unit_Name (Specification (Decl)),
2321 Obj_Typ => Rec_Typ,
2322 Formals =>
2323 Parameter_Specifications (Specification (Decl)));
2324 end if;
2326 if Present (Wrap_Spec) then
2327 Wrap_Decl :=
2328 Make_Subprogram_Declaration (Loc,
2329 Specification => Wrap_Spec);
2331 Insert_After (N, Wrap_Decl);
2332 N := Wrap_Decl;
2334 Analyze (Wrap_Decl);
2335 end if;
2337 Next (Decl);
2338 end loop;
2339 end Scan_Declarations;
2341 -- start of processing for Build_Wrapper_Specs
2343 begin
2344 if Is_Protected_Type (Typ) then
2345 Def := Protected_Definition (Parent (Typ));
2346 else pragma Assert (Is_Task_Type (Typ));
2347 Def := Task_Definition (Parent (Typ));
2348 end if;
2350 Rec_Typ := Corresponding_Record_Type (Typ);
2352 -- Generate wrapper specs for a concurrent type which implements an
2353 -- interface. Operations in both the visible and private parts may
2354 -- implement progenitor operations.
2356 if Present (Interfaces (Rec_Typ)) and then Present (Def) then
2357 Scan_Declarations (Visible_Declarations (Def));
2358 Scan_Declarations (Private_Declarations (Def));
2359 end if;
2360 end Build_Wrapper_Specs;
2362 ---------------------------
2363 -- Build_Find_Body_Index --
2364 ---------------------------
2366 function Build_Find_Body_Index (Typ : Entity_Id) return Node_Id is
2367 Loc : constant Source_Ptr := Sloc (Typ);
2368 Ent : Entity_Id;
2369 E_Typ : Entity_Id;
2370 Has_F : Boolean := False;
2371 Index : Nat;
2372 If_St : Node_Id := Empty;
2373 Lo : Node_Id;
2374 Hi : Node_Id;
2375 Decls : List_Id := New_List;
2376 Ret : Node_Id := Empty;
2377 Spec : Node_Id;
2378 Siz : Node_Id := Empty;
2380 procedure Add_If_Clause (Expr : Node_Id);
2381 -- Add test for range of current entry
2383 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
2384 -- If a bound of an entry is given by a discriminant, retrieve the
2385 -- actual value of the discriminant from the enclosing object.
2387 -------------------
2388 -- Add_If_Clause --
2389 -------------------
2391 procedure Add_If_Clause (Expr : Node_Id) is
2392 Cond : Node_Id;
2393 Stats : constant List_Id :=
2394 New_List (
2395 Make_Simple_Return_Statement (Loc,
2396 Expression => Make_Integer_Literal (Loc, Index + 1)));
2398 begin
2399 -- Index for current entry body
2401 Index := Index + 1;
2403 -- Compute total length of entry queues so far
2405 if No (Siz) then
2406 Siz := Expr;
2407 else
2408 Siz :=
2409 Make_Op_Add (Loc,
2410 Left_Opnd => Siz,
2411 Right_Opnd => Expr);
2412 end if;
2414 Cond :=
2415 Make_Op_Le (Loc,
2416 Left_Opnd => Make_Identifier (Loc, Name_uE),
2417 Right_Opnd => Siz);
2419 -- Map entry queue indexes in the range of the current family
2420 -- into the current index, that designates the entry body.
2422 if No (If_St) then
2423 If_St :=
2424 Make_Implicit_If_Statement (Typ,
2425 Condition => Cond,
2426 Then_Statements => Stats,
2427 Elsif_Parts => New_List);
2428 Ret := If_St;
2430 else
2431 Append_To (Elsif_Parts (If_St),
2432 Make_Elsif_Part (Loc,
2433 Condition => Cond,
2434 Then_Statements => Stats));
2435 end if;
2436 end Add_If_Clause;
2438 ------------------------------
2439 -- Convert_Discriminant_Ref --
2440 ------------------------------
2442 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
2443 B : Node_Id;
2445 begin
2446 if Is_Entity_Name (Bound)
2447 and then Ekind (Entity (Bound)) = E_Discriminant
2448 then
2449 B :=
2450 Make_Selected_Component (Loc,
2451 Prefix =>
2452 Unchecked_Convert_To (Corresponding_Record_Type (Typ),
2453 Make_Explicit_Dereference (Loc,
2454 Make_Identifier (Loc, Name_uObject))),
2455 Selector_Name => Make_Identifier (Loc, Chars (Bound)));
2456 Set_Etype (B, Etype (Entity (Bound)));
2457 else
2458 B := New_Copy_Tree (Bound);
2459 end if;
2461 return B;
2462 end Convert_Discriminant_Ref;
2464 -- Start of processing for Build_Find_Body_Index
2466 begin
2467 Spec := Build_Find_Body_Index_Spec (Typ);
2469 Ent := First_Entity (Typ);
2470 while Present (Ent) loop
2471 if Ekind (Ent) = E_Entry_Family then
2472 Has_F := True;
2473 exit;
2474 end if;
2476 Next_Entity (Ent);
2477 end loop;
2479 if not Has_F then
2481 -- If the protected type has no entry families, there is a one-one
2482 -- correspondence between entry queue and entry body.
2484 Ret :=
2485 Make_Simple_Return_Statement (Loc,
2486 Expression => Make_Identifier (Loc, Name_uE));
2488 else
2489 -- Suppose entries e1, e2, ... have size l1, l2, ... we generate
2490 -- the following:
2492 -- if E <= l1 then return 1;
2493 -- elsif E <= l1 + l2 then return 2;
2494 -- ...
2496 Index := 0;
2497 Siz := Empty;
2498 Ent := First_Entity (Typ);
2500 Add_Object_Pointer (Loc, Typ, Decls);
2502 while Present (Ent) loop
2503 if Ekind (Ent) = E_Entry then
2504 Add_If_Clause (Make_Integer_Literal (Loc, 1));
2506 elsif Ekind (Ent) = E_Entry_Family then
2507 E_Typ := Entry_Index_Type (Ent);
2508 Hi := Convert_Discriminant_Ref (Type_High_Bound (E_Typ));
2509 Lo := Convert_Discriminant_Ref (Type_Low_Bound (E_Typ));
2510 Add_If_Clause (Family_Size (Loc, Hi, Lo, Typ, False));
2511 end if;
2513 Next_Entity (Ent);
2514 end loop;
2516 if Index = 1 then
2517 Decls := New_List;
2518 Ret :=
2519 Make_Simple_Return_Statement (Loc,
2520 Expression => Make_Integer_Literal (Loc, 1));
2522 else
2523 -- Ranges are in increasing order, so last one doesn't need guard
2525 declare
2526 Nod : constant Node_Id := Last (Elsif_Parts (Ret));
2527 begin
2528 Remove (Nod);
2529 Set_Else_Statements (Ret, Then_Statements (Nod));
2531 -- If Elsif_Parts becomes empty then remove it entirely, as
2532 -- otherwise we would violate the invariant of If_Statement
2533 -- node described in Sinfo.
2535 if Is_Empty_List (Elsif_Parts (Ret)) then
2536 pragma Assert (Elsif_Parts (Ret) /= No_List);
2537 Set_Elsif_Parts (Ret, No_List);
2538 end if;
2539 end;
2540 end if;
2541 end if;
2543 return
2544 Make_Subprogram_Body (Loc,
2545 Specification => Spec,
2546 Declarations => Decls,
2547 Handled_Statement_Sequence =>
2548 Make_Handled_Sequence_Of_Statements (Loc,
2549 Statements => New_List (Ret)));
2550 end Build_Find_Body_Index;
2552 --------------------------------
2553 -- Build_Find_Body_Index_Spec --
2554 --------------------------------
2556 function Build_Find_Body_Index_Spec (Typ : Entity_Id) return Node_Id is
2557 Loc : constant Source_Ptr := Sloc (Typ);
2558 Id : constant Entity_Id :=
2559 Make_Defining_Identifier (Loc,
2560 Chars => New_External_Name (Chars (Typ), 'F'));
2561 Parm1 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uO);
2562 Parm2 : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uE);
2564 begin
2565 return
2566 Make_Function_Specification (Loc,
2567 Defining_Unit_Name => Id,
2568 Parameter_Specifications => New_List (
2569 Make_Parameter_Specification (Loc,
2570 Defining_Identifier => Parm1,
2571 Parameter_Type =>
2572 New_Occurrence_Of (RTE (RE_Address), Loc)),
2574 Make_Parameter_Specification (Loc,
2575 Defining_Identifier => Parm2,
2576 Parameter_Type =>
2577 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))),
2579 Result_Definition => New_Occurrence_Of (
2580 RTE (RE_Protected_Entry_Index), Loc));
2581 end Build_Find_Body_Index_Spec;
2583 -----------------------------------------------
2584 -- Build_Lock_Free_Protected_Subprogram_Body --
2585 -----------------------------------------------
2587 function Build_Lock_Free_Protected_Subprogram_Body
2588 (N : Node_Id;
2589 Prot_Typ : Node_Id;
2590 Unprot_Spec : Node_Id) return Node_Id
2592 Actuals : constant List_Id := New_List;
2593 Loc : constant Source_Ptr := Sloc (N);
2594 Spec : constant Node_Id := Specification (N);
2595 Unprot_Id : constant Entity_Id := Defining_Unit_Name (Unprot_Spec);
2596 Formal : Node_Id;
2597 Prot_Spec : Node_Id;
2598 Stmt : Node_Id;
2600 begin
2601 -- Create the protected version of the body
2603 Prot_Spec :=
2604 Build_Protected_Sub_Specification (N, Prot_Typ, Protected_Mode);
2606 -- Build the actual parameters which appear in the call to the
2607 -- unprotected version of the body.
2609 Formal := First (Parameter_Specifications (Prot_Spec));
2610 while Present (Formal) loop
2611 Append_To (Actuals,
2612 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
2614 Next (Formal);
2615 end loop;
2617 -- Function case, generate:
2618 -- return <Unprot_Func_Call>;
2620 if Nkind (Spec) = N_Function_Specification then
2621 Stmt :=
2622 Make_Simple_Return_Statement (Loc,
2623 Expression =>
2624 Make_Function_Call (Loc,
2625 Name =>
2626 Make_Identifier (Loc, Chars (Unprot_Id)),
2627 Parameter_Associations => Actuals));
2629 -- Procedure case, call the unprotected version
2631 else
2632 Stmt :=
2633 Make_Procedure_Call_Statement (Loc,
2634 Name =>
2635 Make_Identifier (Loc, Chars (Unprot_Id)),
2636 Parameter_Associations => Actuals);
2637 end if;
2639 return
2640 Make_Subprogram_Body (Loc,
2641 Declarations => Empty_List,
2642 Specification => Prot_Spec,
2643 Handled_Statement_Sequence =>
2644 Make_Handled_Sequence_Of_Statements (Loc,
2645 Statements => New_List (Stmt)));
2646 end Build_Lock_Free_Protected_Subprogram_Body;
2648 -------------------------------------------------
2649 -- Build_Lock_Free_Unprotected_Subprogram_Body --
2650 -------------------------------------------------
2652 -- Procedures which meet the lock-free implementation requirements and
2653 -- reference a unique scalar component Comp are expanded in the following
2654 -- manner:
2656 -- procedure P (...) is
2657 -- Expected_Comp : constant Comp_Type :=
2658 -- Comp_Type
2659 -- (System.Atomic_Primitives.Lock_Free_Read_N
2660 -- (_Object.Comp'Address));
2661 -- begin
2662 -- loop
2663 -- declare
2664 -- <original declarations before the object renaming declaration
2665 -- of Comp>
2667 -- Desired_Comp : Comp_Type := Expected_Comp;
2668 -- Comp : Comp_Type renames Desired_Comp;
2670 -- <original declarations after the object renaming declaration
2671 -- of Comp>
2673 -- begin
2674 -- <original statements>
2675 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
2676 -- (_Object.Comp'Address,
2677 -- Interfaces.Unsigned_N (Expected_Comp),
2678 -- Interfaces.Unsigned_N (Desired_Comp));
2679 -- end;
2680 -- end loop;
2681 -- end P;
2683 -- Each return and raise statement of P is transformed into an atomic
2684 -- status check:
2686 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2687 -- (_Object.Comp'Address,
2688 -- Interfaces.Unsigned_N (Expected_Comp),
2689 -- Interfaces.Unsigned_N (Desired_Comp));
2690 -- then
2691 -- <original statement>
2692 -- else
2693 -- goto L0;
2694 -- end if;
2696 -- Functions which meet the lock-free implementation requirements and
2697 -- reference a unique scalar component Comp are expanded in the following
2698 -- manner:
2700 -- function F (...) return ... is
2701 -- <original declarations before the object renaming declaration
2702 -- of Comp>
2704 -- Expected_Comp : constant Comp_Type :=
2705 -- Comp_Type
2706 -- (System.Atomic_Primitives.Lock_Free_Read_N
2707 -- (_Object.Comp'Address));
2708 -- Comp : Comp_Type renames Expected_Comp;
2710 -- <original declarations after the object renaming declaration of
2711 -- Comp>
2713 -- begin
2714 -- <original statements>
2715 -- end F;
2717 function Build_Lock_Free_Unprotected_Subprogram_Body
2718 (N : Node_Id;
2719 Prot_Typ : Node_Id) return Node_Id
2721 function Referenced_Component (N : Node_Id) return Entity_Id;
2722 -- Subprograms which meet the lock-free implementation criteria are
2723 -- allowed to reference only one unique component. Return the prival
2724 -- of the said component.
2726 --------------------------
2727 -- Referenced_Component --
2728 --------------------------
2730 function Referenced_Component (N : Node_Id) return Entity_Id is
2731 Comp : Entity_Id;
2732 Decl : Node_Id;
2733 Source_Comp : Entity_Id := Empty;
2735 begin
2736 -- Find the unique source component which N references in its
2737 -- statements.
2739 for Index in 1 .. Lock_Free_Subprogram_Table.Last loop
2740 declare
2741 Element : Lock_Free_Subprogram renames
2742 Lock_Free_Subprogram_Table.Table (Index);
2743 begin
2744 if Element.Sub_Body = N then
2745 Source_Comp := Element.Comp_Id;
2746 exit;
2747 end if;
2748 end;
2749 end loop;
2751 if No (Source_Comp) then
2752 return Empty;
2753 end if;
2755 -- Find the prival which corresponds to the source component within
2756 -- the declarations of N.
2758 Decl := First (Declarations (N));
2759 while Present (Decl) loop
2761 -- Privals appear as object renamings
2763 if Nkind (Decl) = N_Object_Renaming_Declaration then
2764 Comp := Defining_Identifier (Decl);
2766 if Present (Prival_Link (Comp))
2767 and then Prival_Link (Comp) = Source_Comp
2768 then
2769 return Comp;
2770 end if;
2771 end if;
2773 Next (Decl);
2774 end loop;
2776 return Empty;
2777 end Referenced_Component;
2779 -- Local variables
2781 Comp : constant Entity_Id := Referenced_Component (N);
2782 Loc : constant Source_Ptr := Sloc (N);
2783 Hand_Stmt_Seq : Node_Id := Handled_Statement_Sequence (N);
2784 Decls : List_Id := Declarations (N);
2786 -- Start of processing for Build_Lock_Free_Unprotected_Subprogram_Body
2788 begin
2789 -- Add renamings for the protection object, discriminals, privals, and
2790 -- the entry index constant for use by debugger.
2792 Debug_Private_Data_Declarations (Decls);
2794 -- Perform the lock-free expansion when the subprogram references a
2795 -- protected component.
2797 if Present (Comp) then
2798 Protected_Component_Ref : declare
2799 Comp_Decl : constant Node_Id := Parent (Comp);
2800 Comp_Sel_Nam : constant Node_Id := Name (Comp_Decl);
2801 Comp_Type : constant Entity_Id := Etype (Comp);
2803 Is_Procedure : constant Boolean :=
2804 Ekind (Corresponding_Spec (N)) = E_Procedure;
2805 -- Indicates if N is a protected procedure body
2807 Block_Decls : List_Id := No_List;
2808 Try_Write : Entity_Id;
2809 Desired_Comp : Entity_Id;
2810 Decl : Node_Id;
2811 Label : Node_Id;
2812 Label_Id : Entity_Id := Empty;
2813 Read : Entity_Id;
2814 Expected_Comp : Entity_Id;
2815 Stmt : Node_Id;
2816 Stmts : List_Id :=
2817 New_Copy_List_Tree (Statements (Hand_Stmt_Seq));
2818 Typ_Size : Int;
2819 Unsigned : Entity_Id;
2821 function Process_Node (N : Node_Id) return Traverse_Result;
2822 -- Transform a single node if it is a return statement, a raise
2823 -- statement or a reference to Comp.
2825 procedure Process_Stmts (Stmts : List_Id);
2826 -- Given a statement sequence Stmts, wrap any return or raise
2827 -- statements in the following manner:
2829 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2830 -- (_Object.Comp'Address,
2831 -- Interfaces.Unsigned_N (Expected_Comp),
2832 -- Interfaces.Unsigned_N (Desired_Comp))
2833 -- then
2834 -- <Stmt>;
2835 -- else
2836 -- goto L0;
2837 -- end if;
2839 ------------------
2840 -- Process_Node --
2841 ------------------
2843 function Process_Node (N : Node_Id) return Traverse_Result is
2845 procedure Wrap_Statement (Stmt : Node_Id);
2846 -- Wrap an arbitrary statement inside an if statement where the
2847 -- condition does an atomic check on the state of the object.
2849 --------------------
2850 -- Wrap_Statement --
2851 --------------------
2853 procedure Wrap_Statement (Stmt : Node_Id) is
2854 begin
2855 -- The first time through, create the declaration of a label
2856 -- which is used to skip the remainder of source statements
2857 -- if the state of the object has changed.
2859 if No (Label_Id) then
2860 Label_Id :=
2861 Make_Identifier (Loc, New_External_Name ('L', 0));
2862 Set_Entity (Label_Id,
2863 Make_Defining_Identifier (Loc, Chars (Label_Id)));
2864 end if;
2866 -- Generate:
2867 -- if System.Atomic_Primitives.Lock_Free_Try_Write_N
2868 -- (_Object.Comp'Address,
2869 -- Interfaces.Unsigned_N (Expected_Comp),
2870 -- Interfaces.Unsigned_N (Desired_Comp))
2871 -- then
2872 -- <Stmt>;
2873 -- else
2874 -- goto L0;
2875 -- end if;
2877 Rewrite (Stmt,
2878 Make_Implicit_If_Statement (N,
2879 Condition =>
2880 Make_Function_Call (Loc,
2881 Name =>
2882 New_Occurrence_Of (Try_Write, Loc),
2883 Parameter_Associations => New_List (
2884 Make_Attribute_Reference (Loc,
2885 Prefix => Relocate_Node (Comp_Sel_Nam),
2886 Attribute_Name => Name_Address),
2888 Unchecked_Convert_To (Unsigned,
2889 New_Occurrence_Of (Expected_Comp, Loc)),
2891 Unchecked_Convert_To (Unsigned,
2892 New_Occurrence_Of (Desired_Comp, Loc)))),
2894 Then_Statements => New_List (Relocate_Node (Stmt)),
2896 Else_Statements => New_List (
2897 Make_Goto_Statement (Loc,
2898 Name =>
2899 New_Occurrence_Of (Entity (Label_Id), Loc)))));
2900 end Wrap_Statement;
2902 -- Start of processing for Process_Node
2904 begin
2905 -- Wrap each return and raise statement that appear inside a
2906 -- procedure. Skip the last return statement which is added by
2907 -- default since it is transformed into an exit statement.
2909 if Is_Procedure
2910 and then ((Nkind (N) = N_Simple_Return_Statement
2911 and then N /= Last (Stmts))
2912 or else Nkind (N) = N_Extended_Return_Statement
2913 or else (Nkind (N) in
2914 N_Raise_xxx_Error | N_Raise_Statement
2915 and then Comes_From_Source (N)))
2916 then
2917 Wrap_Statement (N);
2918 return Skip;
2919 end if;
2921 -- Force reanalysis
2923 Set_Analyzed (N, False);
2925 return OK;
2926 end Process_Node;
2928 procedure Process_Nodes is new Traverse_Proc (Process_Node);
2930 -------------------
2931 -- Process_Stmts --
2932 -------------------
2934 procedure Process_Stmts (Stmts : List_Id) is
2935 Stmt : Node_Id;
2936 begin
2937 Stmt := First (Stmts);
2938 while Present (Stmt) loop
2939 Process_Nodes (Stmt);
2940 Next (Stmt);
2941 end loop;
2942 end Process_Stmts;
2944 -- Start of processing for Protected_Component_Ref
2946 begin
2947 -- Get the type size
2949 if Known_Static_Esize (Comp_Type) then
2950 Typ_Size := UI_To_Int (Esize (Comp_Type));
2952 -- If the Esize (Object_Size) is unknown at compile time, look at
2953 -- the RM_Size (Value_Size) since it may have been set by an
2954 -- explicit representation clause.
2956 elsif Known_Static_RM_Size (Comp_Type) then
2957 Typ_Size := UI_To_Int (RM_Size (Comp_Type));
2959 -- Should not happen since this has already been checked in
2960 -- Allows_Lock_Free_Implementation (see Sem_Ch9).
2962 else
2963 raise Program_Error;
2964 end if;
2966 -- Retrieve all relevant atomic routines and types
2968 case Typ_Size is
2969 when 8 =>
2970 Try_Write := RTE (RE_Lock_Free_Try_Write_8);
2971 Read := RTE (RE_Lock_Free_Read_8);
2972 Unsigned := RTE (RE_Uint8);
2974 when 16 =>
2975 Try_Write := RTE (RE_Lock_Free_Try_Write_16);
2976 Read := RTE (RE_Lock_Free_Read_16);
2977 Unsigned := RTE (RE_Uint16);
2979 when 32 =>
2980 Try_Write := RTE (RE_Lock_Free_Try_Write_32);
2981 Read := RTE (RE_Lock_Free_Read_32);
2982 Unsigned := RTE (RE_Uint32);
2984 when 64 =>
2985 Try_Write := RTE (RE_Lock_Free_Try_Write_64);
2986 Read := RTE (RE_Lock_Free_Read_64);
2987 Unsigned := RTE (RE_Uint64);
2989 when others =>
2990 raise Program_Error;
2991 end case;
2993 -- Generate:
2994 -- Expected_Comp : constant Comp_Type :=
2995 -- Comp_Type
2996 -- (System.Atomic_Primitives.Lock_Free_Read_N
2997 -- (_Object.Comp'Address));
2999 Expected_Comp :=
3000 Make_Defining_Identifier (Loc,
3001 New_External_Name (Chars (Comp), Suffix => "_saved"));
3003 Decl :=
3004 Make_Object_Declaration (Loc,
3005 Defining_Identifier => Expected_Comp,
3006 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3007 Constant_Present => True,
3008 Expression =>
3009 Unchecked_Convert_To (Comp_Type,
3010 Make_Function_Call (Loc,
3011 Name => New_Occurrence_Of (Read, Loc),
3012 Parameter_Associations => New_List (
3013 Make_Attribute_Reference (Loc,
3014 Prefix => Relocate_Node (Comp_Sel_Nam),
3015 Attribute_Name => Name_Address)))));
3017 -- Protected procedures
3019 if Is_Procedure then
3020 -- Move the original declarations inside the generated block
3022 Block_Decls := Decls;
3024 -- Reset the declarations list of the protected procedure to
3025 -- contain only Decl.
3027 Decls := New_List (Decl);
3029 -- Generate:
3030 -- Desired_Comp : Comp_Type := Expected_Comp;
3032 Desired_Comp :=
3033 Make_Defining_Identifier (Loc,
3034 New_External_Name (Chars (Comp), Suffix => "_current"));
3036 -- Insert the declarations of Expected_Comp and Desired_Comp in
3037 -- the block declarations right before the renaming of the
3038 -- protected component.
3040 Insert_Before (Comp_Decl,
3041 Make_Object_Declaration (Loc,
3042 Defining_Identifier => Desired_Comp,
3043 Object_Definition => New_Occurrence_Of (Comp_Type, Loc),
3044 Expression =>
3045 New_Occurrence_Of (Expected_Comp, Loc)));
3047 -- Protected function
3049 else
3050 Desired_Comp := Expected_Comp;
3052 -- Insert the declaration of Expected_Comp in the function
3053 -- declarations right before the renaming of the protected
3054 -- component.
3056 Insert_Before (Comp_Decl, Decl);
3057 end if;
3059 -- Rewrite the protected component renaming declaration to be a
3060 -- renaming of Desired_Comp.
3062 -- Generate:
3063 -- Comp : Comp_Type renames Desired_Comp;
3065 Rewrite (Comp_Decl,
3066 Make_Object_Renaming_Declaration (Loc,
3067 Defining_Identifier =>
3068 Defining_Identifier (Comp_Decl),
3069 Subtype_Mark =>
3070 New_Occurrence_Of (Comp_Type, Loc),
3071 Name =>
3072 New_Occurrence_Of (Desired_Comp, Loc)));
3074 -- Wrap any return or raise statements in Stmts in same the manner
3075 -- described in Process_Stmts.
3077 Process_Stmts (Stmts);
3079 -- Generate:
3080 -- exit when System.Atomic_Primitives.Lock_Free_Try_Write_N
3081 -- (_Object.Comp'Address,
3082 -- Interfaces.Unsigned_N (Expected_Comp),
3083 -- Interfaces.Unsigned_N (Desired_Comp))
3085 if Is_Procedure then
3086 Stmt :=
3087 Make_Exit_Statement (Loc,
3088 Condition =>
3089 Make_Function_Call (Loc,
3090 Name =>
3091 New_Occurrence_Of (Try_Write, Loc),
3092 Parameter_Associations => New_List (
3093 Make_Attribute_Reference (Loc,
3094 Prefix => Relocate_Node (Comp_Sel_Nam),
3095 Attribute_Name => Name_Address),
3097 Unchecked_Convert_To (Unsigned,
3098 New_Occurrence_Of (Expected_Comp, Loc)),
3100 Unchecked_Convert_To (Unsigned,
3101 New_Occurrence_Of (Desired_Comp, Loc)))));
3103 -- Small optimization: transform the default return statement
3104 -- of a procedure into the atomic exit statement.
3106 if Nkind (Last (Stmts)) = N_Simple_Return_Statement then
3107 Rewrite (Last (Stmts), Stmt);
3108 else
3109 Append_To (Stmts, Stmt);
3110 end if;
3111 end if;
3113 -- Create the declaration of the label used to skip the rest of
3114 -- the source statements when the object state changes.
3116 if Present (Label_Id) then
3117 Label := Make_Label (Loc, Label_Id);
3118 Append_To (Decls,
3119 Make_Implicit_Label_Declaration (Loc,
3120 Defining_Identifier => Entity (Label_Id),
3121 Label_Construct => Label));
3122 Append_To (Stmts, Label);
3123 end if;
3125 -- Generate:
3126 -- loop
3127 -- declare
3128 -- <Decls>
3129 -- begin
3130 -- <Stmts>
3131 -- end;
3132 -- end loop;
3134 if Is_Procedure then
3135 Stmts :=
3136 New_List (
3137 Make_Loop_Statement (Loc,
3138 Statements => New_List (
3139 Make_Block_Statement (Loc,
3140 Declarations => Block_Decls,
3141 Handled_Statement_Sequence =>
3142 Make_Handled_Sequence_Of_Statements (Loc,
3143 Statements => Stmts))),
3144 End_Label => Empty));
3145 end if;
3147 Hand_Stmt_Seq :=
3148 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts);
3149 end Protected_Component_Ref;
3150 end if;
3152 -- Make an unprotected version of the subprogram for use within the same
3153 -- object, with new name and extra parameter representing the object.
3155 return
3156 Make_Subprogram_Body (Loc,
3157 Specification =>
3158 Build_Protected_Sub_Specification (N, Prot_Typ, Unprotected_Mode),
3159 Declarations => Decls,
3160 Handled_Statement_Sequence => Hand_Stmt_Seq);
3161 end Build_Lock_Free_Unprotected_Subprogram_Body;
3163 -------------------------
3164 -- Build_Master_Entity --
3165 -------------------------
3167 procedure Build_Master_Entity (Obj_Or_Typ : Entity_Id) is
3168 Loc : constant Source_Ptr := Sloc (Obj_Or_Typ);
3169 Context : Node_Id;
3170 Context_Id : Entity_Id;
3171 Decl : Node_Id;
3172 Decls : List_Id;
3173 Par : Node_Id;
3175 begin
3176 -- No action needed if the run-time has no tasking support
3178 if Global_No_Tasking then
3179 return;
3180 end if;
3182 if Is_Itype (Obj_Or_Typ) then
3183 Par := Associated_Node_For_Itype (Obj_Or_Typ);
3184 else
3185 Par := Parent (Obj_Or_Typ);
3186 end if;
3188 -- When creating a master for a record component which is either a task
3189 -- or access-to-task, the enclosing record is the master scope and the
3190 -- proper insertion point is the component list.
3192 if Is_Record_Type (Current_Scope) then
3193 Context := Par;
3194 Context_Id := Current_Scope;
3195 Decls := List_Containing (Context);
3197 -- Default case for object declarations and access types. Note that the
3198 -- context is updated to the nearest enclosing body, block, package, or
3199 -- return statement.
3201 else
3202 Find_Enclosing_Context (Par, Context, Context_Id, Decls);
3203 end if;
3205 -- When the enclosing context is a BIP function whose result type has
3206 -- tasks, the function has an extra formal that is the master of the
3207 -- tasks to be created by its returned object (that is, when its
3208 -- enclosing context is a return statement). However, if the body of
3209 -- the function creates tasks before its return statements, such tasks
3210 -- need their own master.
3212 if Has_Master_Entity (Context_Id)
3213 and then Ekind (Context_Id) = E_Function
3214 and then Is_Build_In_Place_Function (Context_Id)
3215 and then Needs_BIP_Task_Actuals (Context_Id)
3216 then
3217 -- No need to add it again if previously added
3219 declare
3220 Master_Present : Boolean;
3222 begin
3223 -- Handle transient scopes
3225 if Context_Id /= Current_Scope then
3226 Push_Scope (Context_Id);
3227 Master_Present :=
3228 Present (Current_Entity_In_Scope (Name_uMaster));
3229 Pop_Scope;
3230 else
3231 Master_Present :=
3232 Present (Current_Entity_In_Scope (Name_uMaster));
3233 end if;
3235 if Master_Present then
3236 return;
3237 end if;
3238 end;
3240 -- Nothing to do if the context already has a master; internally built
3241 -- finalizers don't need a master.
3243 elsif Has_Master_Entity (Context_Id)
3244 or else Is_Finalizer (Context_Id)
3245 then
3246 return;
3247 end if;
3249 Decl := Build_Master_Declaration (Loc);
3251 -- The master is inserted at the start of the declarative list of the
3252 -- context.
3254 Prepend_To (Decls, Decl);
3256 -- In certain cases where transient scopes are involved, the immediate
3257 -- scope is not always the proper master scope. Ensure that the master
3258 -- declaration and entity appear in the same context.
3260 if Context_Id /= Current_Scope then
3261 Push_Scope (Context_Id);
3262 Analyze (Decl);
3263 Pop_Scope;
3264 else
3265 Analyze (Decl);
3266 end if;
3268 -- Mark the enclosing scope and its associated construct as being task
3269 -- masters.
3271 Set_Has_Master_Entity (Context_Id);
3273 while Present (Context)
3274 and then Nkind (Context) /= N_Compilation_Unit
3275 loop
3276 if Nkind (Context) in
3277 N_Block_Statement | N_Subprogram_Body | N_Task_Body
3278 then
3279 Set_Is_Task_Master (Context);
3280 exit;
3282 elsif Nkind (Parent (Context)) = N_Subunit then
3283 Context := Corresponding_Stub (Parent (Context));
3284 end if;
3286 Context := Parent (Context);
3287 end loop;
3288 end Build_Master_Entity;
3290 ---------------------------
3291 -- Build_Master_Renaming --
3292 ---------------------------
3294 procedure Build_Master_Renaming
3295 (Ptr_Typ : Entity_Id;
3296 Ins_Nod : Node_Id := Empty)
3298 Loc : constant Source_Ptr := Sloc (Ptr_Typ);
3299 Context : Node_Id;
3300 Master_Decl : Node_Id;
3301 Master_Id : Entity_Id;
3303 begin
3304 -- No action needed if the run-time has no tasking support
3306 if Global_No_Tasking then
3307 return;
3308 end if;
3310 -- Determine the proper context to insert the master renaming
3312 if Present (Ins_Nod) then
3313 Context := Ins_Nod;
3315 elsif Is_Itype (Ptr_Typ) then
3316 Context := Associated_Node_For_Itype (Ptr_Typ);
3318 -- When the context references a discriminant or a component of a
3319 -- private type and we are processing declarations in the private
3320 -- part of the enclosing package, we must insert the master renaming
3321 -- before the full declaration of the private type; otherwise the
3322 -- master renaming would be inserted in the public part of the
3323 -- package (and hence before the declaration of _master).
3325 if In_Private_Part (Current_Scope) then
3326 declare
3327 Ctx : Node_Id := Context;
3329 begin
3330 if Nkind (Context) = N_Discriminant_Specification then
3331 Ctx := Parent (Ctx);
3332 else
3333 while Nkind (Ctx) in
3334 N_Component_Declaration | N_Component_List
3335 loop
3336 Ctx := Parent (Ctx);
3337 end loop;
3338 end if;
3340 if Nkind (Ctx) in N_Private_Type_Declaration
3341 | N_Private_Extension_Declaration
3342 then
3343 Context := Parent (Full_View (Defining_Identifier (Ctx)));
3344 end if;
3345 end;
3346 end if;
3348 else
3349 Context := Parent (Ptr_Typ);
3350 end if;
3352 -- Generate:
3353 -- <Ptr_Typ>M : Master_Id renames _Master;
3354 -- and add a numeric suffix to the name to ensure that it is
3355 -- unique in case other access types in nested constructs
3356 -- are homonyms of this one.
3358 Master_Id :=
3359 Make_Defining_Identifier (Loc,
3360 New_External_Name (Chars (Ptr_Typ), 'M', -1));
3362 Master_Decl :=
3363 Make_Object_Renaming_Declaration (Loc,
3364 Defining_Identifier => Master_Id,
3365 Subtype_Mark =>
3366 New_Occurrence_Of (Standard_Integer, Loc),
3367 Name => Make_Identifier (Loc, Name_uMaster));
3369 Insert_Action (Context, Master_Decl);
3371 -- The renamed master now services the access type
3373 Set_Master_Id (Ptr_Typ, Master_Id);
3374 end Build_Master_Renaming;
3376 ---------------------------
3377 -- Build_Protected_Entry --
3378 ---------------------------
3380 function Build_Protected_Entry
3381 (N : Node_Id;
3382 Ent : Entity_Id;
3383 Pid : Node_Id) return Node_Id
3385 Bod_Decls : constant List_Id := New_List;
3386 Decls : constant List_Id := Declarations (N);
3387 End_Lab : constant Node_Id :=
3388 End_Label (Handled_Statement_Sequence (N));
3389 End_Loc : constant Source_Ptr :=
3390 Sloc (Last (Statements (Handled_Statement_Sequence (N))));
3391 -- Used for the generated call to Complete_Entry_Body
3393 Loc : constant Source_Ptr := Sloc (N);
3395 Block_Id : Entity_Id;
3396 Bod_Id : Entity_Id;
3397 Bod_Spec : Node_Id;
3398 Bod_Stmts : List_Id;
3399 Complete : Node_Id;
3400 Ohandle : Node_Id;
3401 Proc_Body : Node_Id;
3403 EH_Loc : Source_Ptr;
3404 -- Used for the exception handler, inserted at end of the body
3406 begin
3407 -- Set the source location on the exception handler only when debugging
3408 -- the expanded code (see Make_Implicit_Exception_Handler).
3410 if Debug_Generated_Code then
3411 EH_Loc := End_Loc;
3413 -- Otherwise the inserted code should not be visible to the debugger
3415 else
3416 EH_Loc := No_Location;
3417 end if;
3419 Bod_Id :=
3420 Make_Defining_Identifier (Loc,
3421 Chars => Chars (Protected_Body_Subprogram (Ent)));
3422 Bod_Spec := Build_Protected_Entry_Specification (Loc, Bod_Id, Empty);
3424 -- Add the following declarations:
3426 -- type poVP is access poV;
3427 -- _object : poVP := poVP (_O);
3429 -- where _O is the formal parameter associated with the concurrent
3430 -- object. These declarations are needed for Complete_Entry_Body.
3432 Add_Object_Pointer (Loc, Pid, Bod_Decls);
3434 -- Add renamings for all formals, the Protection object, discriminals,
3435 -- privals and the entry index constant for use by debugger.
3437 Add_Formal_Renamings (Bod_Spec, Bod_Decls, Ent, Loc);
3438 Debug_Private_Data_Declarations (Decls);
3440 -- Put the declarations and the statements from the entry
3442 Bod_Stmts :=
3443 New_List (
3444 Make_Block_Statement (Loc,
3445 Declarations => Decls,
3446 Handled_Statement_Sequence => Handled_Statement_Sequence (N)));
3448 -- Analyze now and reset scopes for declarations so that Scope fields
3449 -- currently denoting the entry will now denote the block scope, and
3450 -- the block's scope will be set to the new procedure entity.
3452 Analyze_Statements (Bod_Stmts);
3454 Block_Id := Entity (Identifier (First (Bod_Stmts)));
3456 Set_Scope (Block_Id, Protected_Body_Subprogram (Ent));
3457 Set_Uses_Sec_Stack (Block_Id, Uses_Sec_Stack (Corresponding_Spec (N)));
3459 Reset_Scopes_To (First (Bod_Stmts), Block_Id);
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);
3556 Set_At_End_Proc (Proc_Body, At_End_Proc (N));
3558 Reset_Scopes_To (Proc_Body, Protected_Body_Subprogram (Ent));
3559 return Proc_Body;
3560 end if;
3561 end Build_Protected_Entry;
3563 -----------------------------------------
3564 -- Build_Protected_Entry_Specification --
3565 -----------------------------------------
3567 function Build_Protected_Entry_Specification
3568 (Loc : Source_Ptr;
3569 Def_Id : Entity_Id;
3570 Ent_Id : Entity_Id) return Node_Id
3572 P : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uP);
3574 begin
3575 Set_Debug_Info_Needed (Def_Id);
3577 if Present (Ent_Id) then
3578 Append_Elmt (P, Accept_Address (Ent_Id));
3579 end if;
3581 return
3582 Make_Procedure_Specification (Loc,
3583 Defining_Unit_Name => Def_Id,
3584 Parameter_Specifications => New_List (
3585 Make_Parameter_Specification (Loc,
3586 Defining_Identifier =>
3587 Make_Defining_Identifier (Loc, Name_uO),
3588 Parameter_Type =>
3589 New_Occurrence_Of (RTE (RE_Address), Loc)),
3591 Make_Parameter_Specification (Loc,
3592 Defining_Identifier => P,
3593 Parameter_Type =>
3594 New_Occurrence_Of (RTE (RE_Address), Loc)),
3596 Make_Parameter_Specification (Loc,
3597 Defining_Identifier =>
3598 Make_Defining_Identifier (Loc, Name_uE),
3599 Parameter_Type =>
3600 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc))));
3601 end Build_Protected_Entry_Specification;
3603 --------------------------
3604 -- Build_Protected_Spec --
3605 --------------------------
3607 function Build_Protected_Spec
3608 (N : Node_Id;
3609 Obj_Type : Entity_Id;
3610 Ident : Entity_Id;
3611 Unprotected : Boolean := False) return List_Id
3613 Loc : constant Source_Ptr := Sloc (N);
3615 Decl : Node_Id;
3616 Formal : Entity_Id;
3617 New_Formal : Entity_Id;
3618 New_Plist : List_Id;
3620 begin
3621 New_Plist := New_List;
3623 Formal := First_Formal (Ident);
3624 while Present (Formal) loop
3625 New_Formal :=
3626 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
3627 Set_Comes_From_Source (New_Formal, Comes_From_Source (Formal));
3629 if Unprotected then
3630 Mutate_Ekind (New_Formal, Ekind (Formal));
3631 Set_Protected_Formal (Formal, New_Formal);
3632 end if;
3634 Append_To (New_Plist,
3635 Make_Parameter_Specification (Loc,
3636 Defining_Identifier => New_Formal,
3637 Aliased_Present => Aliased_Present (Parent (Formal)),
3638 In_Present => In_Present (Parent (Formal)),
3639 Out_Present => Out_Present (Parent (Formal)),
3640 Parameter_Type => New_Occurrence_Of (Etype (Formal), Loc)));
3642 Next_Formal (Formal);
3643 end loop;
3645 -- If the subprogram is a procedure and the context is not an access
3646 -- to protected subprogram, the parameter is in-out. Otherwise it is
3647 -- an in parameter.
3649 Decl :=
3650 Make_Parameter_Specification (Loc,
3651 Defining_Identifier =>
3652 Make_Defining_Identifier (Loc, Name_uObject),
3653 In_Present => True,
3654 Out_Present =>
3655 (Etype (Ident) = Standard_Void_Type
3656 and then not Is_RTE (Obj_Type, RE_Address)),
3657 Parameter_Type =>
3658 New_Occurrence_Of (Obj_Type, Loc));
3659 Set_Debug_Info_Needed (Defining_Identifier (Decl));
3660 Prepend_To (New_Plist, Decl);
3662 return New_Plist;
3663 end Build_Protected_Spec;
3665 ---------------------------------------
3666 -- Build_Protected_Sub_Specification --
3667 ---------------------------------------
3669 function Build_Protected_Sub_Specification
3670 (N : Node_Id;
3671 Prot_Typ : Entity_Id;
3672 Mode : Subprogram_Protection_Mode) return Node_Id
3674 Loc : constant Source_Ptr := Sloc (N);
3675 Decl : Node_Id;
3676 Def_Id : Entity_Id;
3677 New_Id : Entity_Id;
3678 New_Plist : List_Id;
3679 New_Spec : Node_Id;
3681 Append_Chr : constant array (Subprogram_Protection_Mode) of Character :=
3682 (Dispatching_Mode => ' ',
3683 Protected_Mode => 'P',
3684 Unprotected_Mode => 'N');
3686 begin
3687 if Ekind (Defining_Unit_Name (Specification (N))) = E_Subprogram_Body
3688 then
3689 Decl := Unit_Declaration_Node (Corresponding_Spec (N));
3690 else
3691 Decl := N;
3692 end if;
3694 Def_Id := Defining_Unit_Name (Specification (Decl));
3696 New_Plist :=
3697 Build_Protected_Spec
3698 (Decl, Corresponding_Record_Type (Prot_Typ), Def_Id,
3699 Mode = Unprotected_Mode);
3700 New_Id :=
3701 Make_Defining_Identifier (Loc,
3702 Chars => Build_Selected_Name (Prot_Typ, Def_Id, Append_Chr (Mode)));
3704 -- Reference the original nondispatching subprogram since the analysis
3705 -- of the object.operation notation may need its original name (see
3706 -- Sem_Ch4.Names_Match).
3708 if Mode = Dispatching_Mode then
3709 Mutate_Ekind (New_Id, Ekind (Def_Id));
3710 Set_Original_Protected_Subprogram (New_Id, Def_Id);
3711 end if;
3713 -- Link the protected or unprotected version to the original subprogram
3714 -- it emulates.
3716 Mutate_Ekind (New_Id, Ekind (Def_Id));
3717 Set_Protected_Subprogram (New_Id, Def_Id);
3719 -- The unprotected operation carries the user code, and debugging
3720 -- information must be generated for it, even though this spec does
3721 -- not come from source. It is also convenient to allow gdb to step
3722 -- into the protected operation, even though it only contains lock/
3723 -- unlock calls.
3725 Set_Debug_Info_Needed (New_Id);
3727 -- If a pragma Eliminate applies to the source entity, the internal
3728 -- subprograms will be eliminated as well.
3730 Set_Is_Eliminated (New_Id, Is_Eliminated (Def_Id));
3732 -- It seems we should set Has_Nested_Subprogram here, but instead we
3733 -- currently set it in Expand_N_Protected_Body, because the entity
3734 -- created here isn't the one that Corresponding_Spec of the body
3735 -- will later be set to, and that's the entity where it's needed. ???
3737 Set_Has_Nested_Subprogram (New_Id, Has_Nested_Subprogram (Def_Id));
3739 if Nkind (Specification (Decl)) = N_Procedure_Specification then
3740 New_Spec :=
3741 Make_Procedure_Specification (Loc,
3742 Defining_Unit_Name => New_Id,
3743 Parameter_Specifications => New_Plist);
3745 -- Create a new specification for the anonymous subprogram type
3747 else
3748 New_Spec :=
3749 Make_Function_Specification (Loc,
3750 Defining_Unit_Name => New_Id,
3751 Parameter_Specifications => New_Plist,
3752 Result_Definition =>
3753 Copy_Result_Type (Result_Definition (Specification (Decl))));
3755 Set_Return_Present (Defining_Unit_Name (New_Spec));
3756 end if;
3758 return New_Spec;
3759 end Build_Protected_Sub_Specification;
3761 -------------------------------------
3762 -- Build_Protected_Subprogram_Body --
3763 -------------------------------------
3765 function Build_Protected_Subprogram_Body
3766 (N : Node_Id;
3767 Pid : Node_Id;
3768 N_Op_Spec : Node_Id) return Node_Id
3770 Might_Raise : constant Boolean := Sem_Util.Might_Raise (N);
3772 Loc : constant Source_Ptr := Sloc (N);
3773 Op_Spec : constant Node_Id := Specification (N);
3774 P_Op_Spec : constant Node_Id :=
3775 Build_Protected_Sub_Specification (N, Pid, Protected_Mode);
3777 Lock_Kind : RE_Id;
3778 Lock_Name : Node_Id;
3779 Lock_Stmt : Node_Id;
3780 Object_Parm : Node_Id;
3781 Pformal : Node_Id;
3782 R : Node_Id;
3783 Return_Stmt : Node_Id := Empty; -- init to avoid gcc 3 warning
3784 Pre_Stmts : List_Id := No_List; -- init to avoid gcc 3 warning
3785 Stmts : List_Id;
3786 Sub_Body : Node_Id;
3787 Uactuals : List_Id;
3788 Unprot_Call : Node_Id;
3790 begin
3791 -- Build a list of the formal parameters of the protected version of
3792 -- the subprogram to use as the actual parameters of the unprotected
3793 -- version.
3795 Uactuals := New_List;
3796 Pformal := First (Parameter_Specifications (P_Op_Spec));
3797 while Present (Pformal) loop
3798 Append_To (Uactuals,
3799 Make_Identifier (Loc, Chars (Defining_Identifier (Pformal))));
3800 Next (Pformal);
3801 end loop;
3803 -- Make a call to the unprotected version of the subprogram built above
3804 -- for use by the protected version built below.
3806 if Nkind (Op_Spec) = N_Function_Specification then
3807 if Might_Raise then
3808 Unprot_Call :=
3809 Make_Simple_Return_Statement (Loc,
3810 Expression =>
3811 Make_Function_Call (Loc,
3812 Name =>
3813 Make_Identifier (Loc,
3814 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3815 Parameter_Associations => Uactuals));
3817 else
3818 R := Make_Temporary (Loc, 'R');
3820 Unprot_Call :=
3821 Make_Object_Declaration (Loc,
3822 Defining_Identifier => R,
3823 Constant_Present => True,
3824 Object_Definition =>
3825 New_Copy (Result_Definition (N_Op_Spec)),
3826 Expression =>
3827 Make_Function_Call (Loc,
3828 Name =>
3829 Make_Identifier (Loc,
3830 Chars => Chars (Defining_Unit_Name (N_Op_Spec))),
3831 Parameter_Associations => Uactuals));
3833 Return_Stmt :=
3834 Make_Simple_Return_Statement (Loc,
3835 Expression => New_Occurrence_Of (R, Loc));
3836 end if;
3838 if Has_Aspect (Pid, Aspect_Exclusive_Functions)
3839 and then
3840 (No (Find_Value_Of_Aspect (Pid, Aspect_Exclusive_Functions))
3841 or else
3842 Is_True (Static_Boolean (Find_Value_Of_Aspect
3843 (Pid, Aspect_Exclusive_Functions))))
3844 then
3845 Lock_Kind := RE_Lock;
3846 else
3847 Lock_Kind := RE_Lock_Read_Only;
3848 end if;
3849 else
3850 Unprot_Call :=
3851 Make_Procedure_Call_Statement (Loc,
3852 Name =>
3853 Make_Identifier (Loc, Chars (Defining_Unit_Name (N_Op_Spec))),
3854 Parameter_Associations => Uactuals);
3856 Lock_Kind := RE_Lock;
3857 end if;
3859 -- Wrap call in block that will be covered by an at_end handler
3861 if Might_Raise then
3862 Unprot_Call :=
3863 Make_Block_Statement (Loc,
3864 Handled_Statement_Sequence =>
3865 Make_Handled_Sequence_Of_Statements (Loc,
3866 Statements => New_List (Unprot_Call)));
3867 end if;
3869 -- Make the protected subprogram body. This locks the protected
3870 -- object and calls the unprotected version of the subprogram.
3872 case Corresponding_Runtime_Package (Pid) is
3873 when System_Tasking_Protected_Objects_Entries =>
3874 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entries), Loc);
3876 when System_Tasking_Protected_Objects_Single_Entry =>
3877 Lock_Name := New_Occurrence_Of (RTE (RE_Lock_Entry), Loc);
3879 when System_Tasking_Protected_Objects =>
3880 Lock_Name := New_Occurrence_Of (RTE (Lock_Kind), Loc);
3882 when others =>
3883 raise Program_Error;
3884 end case;
3886 Object_Parm :=
3887 Make_Attribute_Reference (Loc,
3888 Prefix =>
3889 Make_Selected_Component (Loc,
3890 Prefix => Make_Identifier (Loc, Name_uObject),
3891 Selector_Name => Make_Identifier (Loc, Name_uObject)),
3892 Attribute_Name => Name_Unchecked_Access);
3894 Lock_Stmt :=
3895 Make_Procedure_Call_Statement (Loc,
3896 Name => Lock_Name,
3897 Parameter_Associations => New_List (Object_Parm));
3899 if Abort_Allowed then
3900 Stmts := New_List (
3901 Build_Runtime_Call (Loc, RE_Abort_Defer),
3902 Lock_Stmt);
3904 else
3905 Stmts := New_List (Lock_Stmt);
3906 end if;
3908 if Might_Raise then
3909 Append (Unprot_Call, Stmts);
3910 else
3911 if Nkind (Op_Spec) = N_Function_Specification then
3912 Pre_Stmts := Stmts;
3913 Stmts := Empty_List;
3914 else
3915 Append (Unprot_Call, Stmts);
3916 end if;
3918 Build_Protected_Subprogram_Call_Cleanup (Op_Spec, Pid, Loc, Stmts);
3920 if Nkind (Op_Spec) = N_Function_Specification then
3921 Append_To (Stmts, Return_Stmt);
3922 Append_To (Pre_Stmts,
3923 Make_Block_Statement (Loc,
3924 Declarations => New_List (Unprot_Call),
3925 Handled_Statement_Sequence =>
3926 Make_Handled_Sequence_Of_Statements (Loc,
3927 Statements => Stmts)));
3928 Stmts := Pre_Stmts;
3929 end if;
3930 end if;
3932 Sub_Body :=
3933 Make_Subprogram_Body (Loc,
3934 Declarations => Empty_List,
3935 Specification => P_Op_Spec,
3936 Handled_Statement_Sequence =>
3937 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts));
3939 -- Mark this subprogram as a protected subprogram body so that the
3940 -- cleanup will be inserted. This is done only in the Might_Raise
3941 -- case because otherwise the cleanup has already been inserted.
3943 if Might_Raise then
3944 Set_Is_Protected_Subprogram_Body (Sub_Body);
3945 end if;
3947 return Sub_Body;
3948 end Build_Protected_Subprogram_Body;
3950 -------------------------------------
3951 -- Build_Protected_Subprogram_Call --
3952 -------------------------------------
3954 procedure Build_Protected_Subprogram_Call
3955 (N : Node_Id;
3956 Name : Node_Id;
3957 Rec : Node_Id;
3958 External : Boolean := True)
3960 Loc : constant Source_Ptr := Sloc (N);
3961 Sub : constant Entity_Id := Entity (Name);
3962 New_Sub : Node_Id;
3963 Params : List_Id;
3965 begin
3966 if External then
3967 New_Sub := New_Occurrence_Of (External_Subprogram (Sub), Loc);
3968 else
3969 New_Sub :=
3970 New_Occurrence_Of (Protected_Body_Subprogram (Sub), Loc);
3971 end if;
3973 if Present (Parameter_Associations (N)) then
3974 Params := New_Copy_List_Tree (Parameter_Associations (N));
3975 else
3976 Params := New_List;
3977 end if;
3979 -- If the type is an untagged derived type, convert to the root type,
3980 -- which is the one on which the operations are defined.
3982 if Nkind (Rec) = N_Unchecked_Type_Conversion
3983 and then not Is_Tagged_Type (Etype (Rec))
3984 and then Is_Derived_Type (Etype (Rec))
3985 then
3986 Set_Etype (Rec, Root_Type (Etype (Rec)));
3987 Set_Subtype_Mark (Rec,
3988 New_Occurrence_Of (Root_Type (Etype (Rec)), Sloc (N)));
3989 end if;
3991 Prepend (Rec, Params);
3993 if Ekind (Sub) = E_Procedure then
3994 Rewrite (N,
3995 Make_Procedure_Call_Statement (Loc,
3996 Name => New_Sub,
3997 Parameter_Associations => Params));
3999 else
4000 pragma Assert (Ekind (Sub) = E_Function);
4001 Rewrite (N,
4002 Make_Function_Call (Loc,
4003 Name => New_Sub,
4004 Parameter_Associations => Params));
4006 -- Preserve type of call for subsequent processing (required for
4007 -- call to Wrap_Transient_Expression in the case of a shared passive
4008 -- protected).
4010 Set_Etype (N, Etype (New_Sub));
4011 end if;
4013 if External
4014 and then Nkind (Rec) = N_Unchecked_Type_Conversion
4015 and then Is_Entity_Name (Expression (Rec))
4016 and then Is_Shared_Passive (Entity (Expression (Rec)))
4017 then
4018 Add_Shared_Var_Lock_Procs (N);
4019 end if;
4020 end Build_Protected_Subprogram_Call;
4022 ---------------------------------------------
4023 -- Build_Protected_Subprogram_Call_Cleanup --
4024 ---------------------------------------------
4026 procedure Build_Protected_Subprogram_Call_Cleanup
4027 (Op_Spec : Node_Id;
4028 Conc_Typ : Node_Id;
4029 Loc : Source_Ptr;
4030 Stmts : List_Id)
4032 Nam : Node_Id;
4034 begin
4035 -- If the associated protected object has entries, a protected
4036 -- procedure has to service entry queues. In this case generate:
4038 -- Service_Entries (_object._object'Access);
4040 if Nkind (Op_Spec) = N_Procedure_Specification
4041 and then Has_Entries (Conc_Typ)
4042 then
4043 case Corresponding_Runtime_Package (Conc_Typ) is
4044 when System_Tasking_Protected_Objects_Entries =>
4045 Nam := New_Occurrence_Of (RTE (RE_Service_Entries), Loc);
4047 when System_Tasking_Protected_Objects_Single_Entry =>
4048 Nam := New_Occurrence_Of (RTE (RE_Service_Entry), Loc);
4050 when others =>
4051 raise Program_Error;
4052 end case;
4054 Append_To (Stmts,
4055 Make_Procedure_Call_Statement (Loc,
4056 Name => Nam,
4057 Parameter_Associations => New_List (
4058 Make_Attribute_Reference (Loc,
4059 Prefix =>
4060 Make_Selected_Component (Loc,
4061 Prefix => Make_Identifier (Loc, Name_uObject),
4062 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4063 Attribute_Name => Name_Unchecked_Access))));
4065 else
4066 -- Generate:
4067 -- Unlock (_object._object'Access);
4069 case Corresponding_Runtime_Package (Conc_Typ) is
4070 when System_Tasking_Protected_Objects_Entries =>
4071 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entries), Loc);
4073 when System_Tasking_Protected_Objects_Single_Entry =>
4074 Nam := New_Occurrence_Of (RTE (RE_Unlock_Entry), Loc);
4076 when System_Tasking_Protected_Objects =>
4077 Nam := New_Occurrence_Of (RTE (RE_Unlock), Loc);
4079 when others =>
4080 raise Program_Error;
4081 end case;
4083 Append_To (Stmts,
4084 Make_Procedure_Call_Statement (Loc,
4085 Name => Nam,
4086 Parameter_Associations => New_List (
4087 Make_Attribute_Reference (Loc,
4088 Prefix =>
4089 Make_Selected_Component (Loc,
4090 Prefix => Make_Identifier (Loc, Name_uObject),
4091 Selector_Name => Make_Identifier (Loc, Name_uObject)),
4092 Attribute_Name => Name_Unchecked_Access))));
4093 end if;
4095 -- Generate:
4096 -- Abort_Undefer;
4098 if Abort_Allowed then
4099 Append_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
4100 end if;
4101 end Build_Protected_Subprogram_Call_Cleanup;
4103 -------------------------
4104 -- Build_Selected_Name --
4105 -------------------------
4107 function Build_Selected_Name
4108 (Prefix : Entity_Id;
4109 Selector : Entity_Id;
4110 Append_Char : Character := ' ') return Name_Id
4112 Select_Buffer : String (1 .. Hostparm.Max_Name_Length);
4113 Select_Len : Natural;
4115 begin
4116 Get_Name_String (Chars (Selector));
4117 Select_Len := Name_Len;
4118 Select_Buffer (1 .. Select_Len) := Name_Buffer (1 .. Name_Len);
4119 Get_Name_String (Chars (Prefix));
4121 -- If scope is anonymous type, discard suffix to recover name of
4122 -- single protected object. Otherwise use protected type name.
4124 if Name_Buffer (Name_Len) = 'T' then
4125 Name_Len := Name_Len - 1;
4126 end if;
4128 Add_Str_To_Name_Buffer ("__");
4129 for J in 1 .. Select_Len loop
4130 Add_Char_To_Name_Buffer (Select_Buffer (J));
4131 end loop;
4133 -- Now add the Append_Char if specified. The encoding to follow
4134 -- depends on the type of entity. If Append_Char is either 'N' or 'P',
4135 -- then the entity is associated to a protected type subprogram.
4136 -- Otherwise, it is a protected type entry. For each case, the
4137 -- encoding to follow for the suffix is documented in exp_dbug.ads.
4139 -- It would be better to encapsulate this as a routine in Exp_Dbug ???
4141 if Append_Char /= ' ' then
4142 if Append_Char in 'P' | 'N' then
4143 Add_Char_To_Name_Buffer (Append_Char);
4144 return Name_Find;
4145 else
4146 Add_Str_To_Name_Buffer ((1 => '_', 2 => Append_Char));
4147 return New_External_Name (Name_Find, ' ', -1);
4148 end if;
4149 else
4150 return Name_Find;
4151 end if;
4152 end Build_Selected_Name;
4154 -----------------------------
4155 -- Build_Simple_Entry_Call --
4156 -----------------------------
4158 -- A task entry call is converted to a call to Call_Simple
4160 -- declare
4161 -- P : parms := (parm, parm, parm);
4162 -- begin
4163 -- Call_Simple (acceptor-task, entry-index, P'Address);
4164 -- parm := P.param;
4165 -- parm := P.param;
4166 -- ...
4167 -- end;
4169 -- Here Pnn is an aggregate of the type constructed for the entry to hold
4170 -- the parameters, and the constructed aggregate value contains either the
4171 -- parameters or, in the case of non-elementary types, references to these
4172 -- parameters. Then the address of this aggregate is passed to the runtime
4173 -- routine, along with the task id value and the task entry index value.
4174 -- Pnn is only required if parameters are present.
4176 -- The assignments after the call are present only in the case of in-out
4177 -- or out parameters for elementary types, and are used to assign back the
4178 -- resulting values of such parameters.
4180 -- Note: the reason that we insert a block here is that in the context
4181 -- of selects, conditional entry calls etc. the entry call statement
4182 -- appears on its own, not as an element of a list.
4184 -- A protected entry call is converted to a Protected_Entry_Call:
4186 -- declare
4187 -- P : E1_Params := (param, param, param);
4188 -- Pnn : Boolean;
4189 -- Bnn : Communications_Block;
4191 -- declare
4192 -- P : E1_Params := (param, param, param);
4193 -- Bnn : Communications_Block;
4195 -- begin
4196 -- Protected_Entry_Call (
4197 -- Object => po._object'Access,
4198 -- E => <entry index>;
4199 -- Uninterpreted_Data => P'Address;
4200 -- Mode => Simple_Call;
4201 -- Block => Bnn);
4202 -- parm := P.param;
4203 -- parm := P.param;
4204 -- ...
4205 -- end;
4207 procedure Build_Simple_Entry_Call
4208 (N : Node_Id;
4209 Concval : Node_Id;
4210 Ename : Node_Id;
4211 Index : Node_Id)
4213 begin
4214 Expand_Call (N);
4216 -- If call has been inlined, nothing left to do
4218 if Nkind (N) = N_Block_Statement then
4219 return;
4220 end if;
4222 -- Convert entry call to Call_Simple call
4224 declare
4225 Loc : constant Source_Ptr := Sloc (N);
4226 Parms : constant List_Id := Parameter_Associations (N);
4227 Stats : constant List_Id := New_List;
4228 Actual : Node_Id;
4229 Call : Node_Id;
4230 Comm_Name : Entity_Id;
4231 Conctyp : Node_Id;
4232 Decls : List_Id;
4233 Ent : Entity_Id;
4234 Ent_Acc : Entity_Id;
4235 Formal : Node_Id;
4236 Iface_Tag : Entity_Id;
4237 Iface_Typ : Entity_Id;
4238 N_Node : Node_Id;
4239 N_Var : Node_Id;
4240 P : Entity_Id;
4241 Parm1 : Node_Id;
4242 Parm2 : Node_Id;
4243 Parm3 : Node_Id;
4244 Pdecl : Node_Id;
4245 Plist : List_Id;
4246 X : Entity_Id;
4247 Xdecl : Node_Id;
4249 begin
4250 -- Simple entry and entry family cases merge here
4252 Ent := Entity (Ename);
4253 Ent_Acc := Entry_Parameters_Type (Ent);
4254 Conctyp := Etype (Concval);
4256 -- Special case for protected subprogram calls
4258 if Is_Protected_Type (Conctyp)
4259 and then Is_Subprogram (Entity (Ename))
4260 then
4261 if not Is_Eliminated (Entity (Ename)) then
4262 Build_Protected_Subprogram_Call
4263 (N, Ename, Convert_Concurrent (Concval, Conctyp));
4264 Analyze (N);
4265 end if;
4267 return;
4268 end if;
4270 -- First parameter is the Task_Id value from the task value or the
4271 -- Object from the protected object value, obtained by selecting
4272 -- the _Task_Id or _Object from the result of doing an unchecked
4273 -- conversion to convert the value to the corresponding record type.
4275 if Nkind (Concval) = N_Function_Call
4276 and then Is_Task_Type (Conctyp)
4277 and then Ada_Version >= Ada_2005
4278 then
4279 declare
4280 ExpR : constant Node_Id := Relocate_Node (Concval);
4281 Obj : constant Entity_Id := Make_Temporary (Loc, 'F', ExpR);
4282 Decl : Node_Id;
4284 begin
4285 Decl :=
4286 Make_Object_Declaration (Loc,
4287 Defining_Identifier => Obj,
4288 Object_Definition => New_Occurrence_Of (Conctyp, Loc),
4289 Expression => ExpR);
4290 Set_Etype (Obj, Conctyp);
4291 Decls := New_List (Decl);
4292 Rewrite (Concval, New_Occurrence_Of (Obj, Loc));
4293 end;
4295 else
4296 Decls := New_List;
4297 end if;
4299 Parm1 := Concurrent_Ref (Concval);
4301 -- Second parameter is the entry index, computed by the routine
4302 -- provided for this purpose. The value of this expression is
4303 -- assigned to an intermediate variable to assure that any entry
4304 -- family index expressions are evaluated before the entry
4305 -- parameters.
4307 if not Is_Protected_Type (Conctyp)
4308 or else
4309 Corresponding_Runtime_Package (Conctyp) =
4310 System_Tasking_Protected_Objects_Entries
4311 then
4312 X := Make_Defining_Identifier (Loc, Name_uX);
4314 Xdecl :=
4315 Make_Object_Declaration (Loc,
4316 Defining_Identifier => X,
4317 Object_Definition =>
4318 New_Occurrence_Of (RTE (RE_Task_Entry_Index), Loc),
4319 Expression => Actual_Index_Expression (
4320 Loc, Entity (Ename), Index, Concval));
4322 Append_To (Decls, Xdecl);
4323 Parm2 := New_Occurrence_Of (X, Loc);
4325 else
4326 Xdecl := Empty;
4327 Parm2 := Empty;
4328 end if;
4330 -- The third parameter is the packaged parameters. If there are
4331 -- none, then it is just the null address, since nothing is passed.
4333 if No (Parms) then
4334 Parm3 := New_Occurrence_Of (RTE (RE_Null_Address), Loc);
4335 P := Empty;
4337 -- Case of parameters present, where third argument is the address
4338 -- of a packaged record containing the required parameter values.
4340 else
4341 -- First build a list of parameter values, which are references to
4342 -- objects of the parameter types.
4344 Plist := New_List;
4346 Actual := First_Actual (N);
4347 Formal := First_Formal (Ent);
4348 while Present (Actual) loop
4350 -- If it is a by-copy type, copy it to a new variable. The
4351 -- packaged record has a field that points to this variable.
4353 if Is_By_Copy_Type (Etype (Actual)) then
4354 N_Node :=
4355 Make_Object_Declaration (Loc,
4356 Defining_Identifier => Make_Temporary (Loc, 'J'),
4357 Aliased_Present => True,
4358 Object_Definition =>
4359 New_Occurrence_Of (Etype (Formal), Loc));
4361 -- Mark the object as not needing initialization since the
4362 -- initialization is performed separately, avoiding errors
4363 -- on cases such as formals of null-excluding access types.
4365 Set_No_Initialization (N_Node);
4367 -- We must make a separate assignment statement for the
4368 -- case of limited types. We cannot assign it unless the
4369 -- Assignment_OK flag is set first. An out formal of an
4370 -- access type or whose type has a Default_Value must also
4371 -- be initialized from the actual (see RM 6.4.1 (13-13.1)),
4372 -- but no constraint, predicate, or null-exclusion check is
4373 -- applied before the call.
4375 if Ekind (Formal) /= E_Out_Parameter
4376 or else Is_Access_Type (Etype (Formal))
4377 or else
4378 (Is_Scalar_Type (Etype (Formal))
4379 and then
4380 Present (Default_Aspect_Value (Etype (Formal))))
4381 then
4382 N_Var :=
4383 New_Occurrence_Of (Defining_Identifier (N_Node), Loc);
4384 Set_Assignment_OK (N_Var);
4385 Append_To (Stats,
4386 Make_Assignment_Statement (Loc,
4387 Name => N_Var,
4388 Expression => Relocate_Node (Actual)));
4390 -- Mark the object as internal, so we don't later reset
4391 -- No_Initialization flag in Default_Initialize_Object,
4392 -- which would lead to needless default initialization.
4393 -- We don't set this outside the if statement, because
4394 -- out scalar parameters without Default_Value do require
4395 -- default initialization if Initialize_Scalars applies.
4397 Set_Is_Internal (Defining_Identifier (N_Node));
4399 -- If actual is an out parameter of a null-excluding
4400 -- access type, there is access check on entry, so set
4401 -- Suppress_Assignment_Checks on the generated statement
4402 -- that assigns the actual to the parameter block.
4404 Set_Suppress_Assignment_Checks (Last (Stats));
4405 end if;
4407 Append (N_Node, Decls);
4409 Append_To (Plist,
4410 Make_Attribute_Reference (Loc,
4411 Attribute_Name => Name_Unchecked_Access,
4412 Prefix =>
4413 New_Occurrence_Of
4414 (Defining_Identifier (N_Node), Loc)));
4416 else
4417 -- Interface class-wide formal
4419 if Ada_Version >= Ada_2005
4420 and then Ekind (Etype (Formal)) = E_Class_Wide_Type
4421 and then Is_Interface (Etype (Formal))
4422 then
4423 Iface_Typ := Etype (Etype (Formal));
4425 -- Generate:
4426 -- formal_iface_type! (actual.iface_tag)'reference
4428 Iface_Tag :=
4429 Find_Interface_Tag (Etype (Actual), Iface_Typ);
4430 pragma Assert (Present (Iface_Tag));
4432 Append_To (Plist,
4433 Make_Reference (Loc,
4434 Unchecked_Convert_To (Iface_Typ,
4435 Make_Selected_Component (Loc,
4436 Prefix =>
4437 Relocate_Node (Actual),
4438 Selector_Name =>
4439 New_Occurrence_Of (Iface_Tag, Loc)))));
4440 else
4441 -- Generate:
4442 -- actual'reference
4444 Append_To (Plist,
4445 Make_Reference (Loc, Relocate_Node (Actual)));
4446 end if;
4447 end if;
4449 Next_Actual (Actual);
4450 Next_Formal_With_Extras (Formal);
4451 end loop;
4453 -- Now build the declaration of parameters initialized with the
4454 -- aggregate containing this constructed parameter list.
4456 P := Make_Defining_Identifier (Loc, Name_uP);
4458 Pdecl :=
4459 Make_Object_Declaration (Loc,
4460 Defining_Identifier => P,
4461 Object_Definition =>
4462 New_Occurrence_Of (Designated_Type (Ent_Acc), Loc),
4463 Expression =>
4464 Make_Aggregate (Loc, Expressions => Plist));
4466 Parm3 :=
4467 Make_Attribute_Reference (Loc,
4468 Prefix => New_Occurrence_Of (P, Loc),
4469 Attribute_Name => Name_Address);
4471 Append (Pdecl, Decls);
4472 end if;
4474 -- Now we can create the call, case of protected type
4476 if Is_Protected_Type (Conctyp) then
4477 case Corresponding_Runtime_Package (Conctyp) is
4478 when System_Tasking_Protected_Objects_Entries =>
4480 -- Change the type of the index declaration
4482 Set_Object_Definition (Xdecl,
4483 New_Occurrence_Of (RTE (RE_Protected_Entry_Index), Loc));
4485 -- Some additional declarations for protected entry calls
4487 if No (Decls) then
4488 Decls := New_List;
4489 end if;
4491 -- Bnn : Communications_Block;
4493 Comm_Name := Make_Temporary (Loc, 'B');
4495 Append_To (Decls,
4496 Make_Object_Declaration (Loc,
4497 Defining_Identifier => Comm_Name,
4498 Object_Definition =>
4499 New_Occurrence_Of
4500 (RTE (RE_Communication_Block), Loc)));
4502 -- Some additional statements for protected entry calls
4504 -- Protected_Entry_Call
4505 -- (Object => po._object'Access,
4506 -- E => <entry index>;
4507 -- Uninterpreted_Data => P'Address;
4508 -- Mode => Simple_Call;
4509 -- Block => Bnn);
4511 Call :=
4512 Make_Procedure_Call_Statement (Loc,
4513 Name =>
4514 New_Occurrence_Of (RTE (RE_Protected_Entry_Call), Loc),
4516 Parameter_Associations => New_List (
4517 Make_Attribute_Reference (Loc,
4518 Attribute_Name => Name_Unchecked_Access,
4519 Prefix => Parm1),
4520 Parm2,
4521 Parm3,
4522 New_Occurrence_Of (RTE (RE_Simple_Call), Loc),
4523 New_Occurrence_Of (Comm_Name, Loc)));
4525 when System_Tasking_Protected_Objects_Single_Entry =>
4527 -- Protected_Single_Entry_Call
4528 -- (Object => po._object'Access,
4529 -- Uninterpreted_Data => P'Address);
4531 Call :=
4532 Make_Procedure_Call_Statement (Loc,
4533 Name =>
4534 New_Occurrence_Of
4535 (RTE (RE_Protected_Single_Entry_Call), Loc),
4537 Parameter_Associations => New_List (
4538 Make_Attribute_Reference (Loc,
4539 Attribute_Name => Name_Unchecked_Access,
4540 Prefix => Parm1),
4541 Parm3));
4543 when others =>
4544 raise Program_Error;
4545 end case;
4547 -- Case of task type
4549 else
4550 Call :=
4551 Make_Procedure_Call_Statement (Loc,
4552 Name =>
4553 New_Occurrence_Of (RTE (RE_Call_Simple), Loc),
4554 Parameter_Associations => New_List (Parm1, Parm2, Parm3));
4556 end if;
4558 Append_To (Stats, Call);
4560 -- If there are out or in/out parameters by copy add assignment
4561 -- statements for the result values.
4563 if Present (Parms) then
4564 Actual := First_Actual (N);
4565 Formal := First_Formal (Ent);
4567 Set_Assignment_OK (Actual);
4568 while Present (Actual) loop
4569 if Is_By_Copy_Type (Etype (Actual))
4570 and then Ekind (Formal) /= E_In_Parameter
4571 then
4572 N_Node :=
4573 Make_Assignment_Statement (Loc,
4574 Name => New_Copy (Actual),
4575 Expression =>
4576 Make_Explicit_Dereference (Loc,
4577 Make_Selected_Component (Loc,
4578 Prefix => New_Occurrence_Of (P, Loc),
4579 Selector_Name =>
4580 Make_Identifier (Loc, Chars (Formal)))));
4582 -- In all cases (including limited private types) we want
4583 -- the assignment to be valid.
4585 Set_Assignment_OK (Name (N_Node));
4587 -- If the call is the triggering alternative in an
4588 -- asynchronous select, or the entry_call alternative of a
4589 -- conditional entry call, the assignments for in-out
4590 -- parameters are incorporated into the statement list that
4591 -- follows, so that there are executed only if the entry
4592 -- call succeeds.
4594 if (Nkind (Parent (N)) = N_Triggering_Alternative
4595 and then N = Triggering_Statement (Parent (N)))
4596 or else
4597 (Nkind (Parent (N)) = N_Entry_Call_Alternative
4598 and then N = Entry_Call_Statement (Parent (N)))
4599 then
4600 if No (Statements (Parent (N))) then
4601 Set_Statements (Parent (N), New_List);
4602 end if;
4604 Prepend (N_Node, Statements (Parent (N)));
4606 else
4607 Insert_After (Call, N_Node);
4608 end if;
4609 end if;
4611 Next_Actual (Actual);
4612 Next_Formal_With_Extras (Formal);
4613 end loop;
4614 end if;
4616 -- Finally, create block and analyze it
4618 Rewrite (N,
4619 Make_Block_Statement (Loc,
4620 Declarations => Decls,
4621 Handled_Statement_Sequence =>
4622 Make_Handled_Sequence_Of_Statements (Loc,
4623 Statements => Stats)));
4625 Analyze (N);
4626 end;
4627 end Build_Simple_Entry_Call;
4629 --------------------------------
4630 -- Build_Task_Activation_Call --
4631 --------------------------------
4633 procedure Build_Task_Activation_Call (N : Node_Id) is
4634 function Activation_Call_Loc return Source_Ptr;
4635 -- Find a suitable source location for the activation call
4637 -------------------------
4638 -- Activation_Call_Loc --
4639 -------------------------
4641 function Activation_Call_Loc return Source_Ptr is
4642 begin
4643 -- The activation call must carry the location of the "end" keyword
4644 -- when the context is a package declaration.
4646 if Nkind (N) = N_Package_Declaration then
4647 return End_Keyword_Location (N);
4649 -- Otherwise the activation call must carry the location of the
4650 -- "begin" keyword.
4652 else
4653 return Begin_Keyword_Location (N);
4654 end if;
4655 end Activation_Call_Loc;
4657 -- Local variables
4659 Chain : Entity_Id;
4660 Call : Node_Id;
4661 Loc : Source_Ptr;
4662 Name : Node_Id;
4663 Owner : Node_Id;
4664 Stmt : Node_Id;
4666 -- Start of processing for Build_Task_Activation_Call
4668 begin
4669 -- For sequential elaboration policy, all the tasks will be activated at
4670 -- the end of the elaboration.
4672 if Partition_Elaboration_Policy = 'S' then
4673 return;
4675 -- Do not create an activation call for a package spec if the package
4676 -- has a completing body. The activation call will be inserted after
4677 -- the "begin" of the body.
4679 elsif Nkind (N) = N_Package_Declaration
4680 and then Present (Corresponding_Body (N))
4681 then
4682 return;
4683 end if;
4685 -- Obtain the activation chain entity. Block statements, entry bodies,
4686 -- subprogram bodies, and task bodies keep the entity in their nodes.
4687 -- Package bodies on the other hand store it in the declaration of the
4688 -- corresponding package spec.
4690 Owner := N;
4692 if Nkind (Owner) = N_Package_Body then
4693 Owner := Unit_Declaration_Node (Corresponding_Spec (Owner));
4694 end if;
4696 Chain := Activation_Chain_Entity (Owner);
4698 -- Nothing to do when there are no tasks to activate. This is indicated
4699 -- by a missing activation chain entity; also skip generating it when
4700 -- it is a ghost entity.
4702 if No (Chain) or else Is_Ignored_Ghost_Entity (Chain) then
4703 return;
4705 -- The availability of the activation chain entity does not ensure
4706 -- that we have tasks to activate because it may have been declared
4707 -- by the frontend to pass a required extra formal to a build-in-place
4708 -- subprogram call. If we are within the scope of a protected type and
4709 -- pragma Detect_Blocking is active we can assume that no tasks will be
4710 -- activated; if tasks are created in a protected object and this pragma
4711 -- is active then the frontend emits a warning and Program_Error is
4712 -- raised at runtime.
4714 elsif Detect_Blocking and then Within_Protected_Type (Current_Scope) then
4715 return;
4716 end if;
4718 -- The location of the activation call must be as close as possible to
4719 -- the intended semantic location of the activation because the ABE
4720 -- mechanism relies heavily on accurate locations.
4722 Loc := Activation_Call_Loc;
4724 if Restricted_Profile then
4725 Name := New_Occurrence_Of (RTE (RE_Activate_Restricted_Tasks), Loc);
4726 else
4727 Name := New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc);
4728 end if;
4730 Call :=
4731 Make_Procedure_Call_Statement (Loc,
4732 Name => Name,
4733 Parameter_Associations =>
4734 New_List (Make_Attribute_Reference (Loc,
4735 Prefix => New_Occurrence_Of (Chain, Loc),
4736 Attribute_Name => Name_Unchecked_Access)));
4738 if Nkind (N) = N_Package_Declaration then
4739 if Present (Private_Declarations (Specification (N))) then
4740 Append (Call, Private_Declarations (Specification (N)));
4741 else
4742 Append (Call, Visible_Declarations (Specification (N)));
4743 end if;
4745 else
4746 -- The call goes at the start of the statement sequence after the
4747 -- start of exception range label if one is present.
4749 if Present (Handled_Statement_Sequence (N)) then
4750 Stmt := First (Statements (Handled_Statement_Sequence (N)));
4752 -- A special case, skip exception range label if one is present
4753 -- (from front end zcx processing).
4755 if Nkind (Stmt) = N_Label and then Exception_Junk (Stmt) then
4756 Next (Stmt);
4757 end if;
4759 -- Another special case, if the first statement is a block from
4760 -- optimization of a local raise to a goto, then the call goes
4761 -- inside this block.
4763 if Nkind (Stmt) = N_Block_Statement
4764 and then Exception_Junk (Stmt)
4765 then
4766 Stmt := First (Statements (Handled_Statement_Sequence (Stmt)));
4767 end if;
4769 -- Insertion point is after any exception label pushes, since we
4770 -- want it covered by any local handlers.
4772 while Nkind (Stmt) in N_Push_xxx_Label loop
4773 Next (Stmt);
4774 end loop;
4776 -- Now we have the proper insertion point
4778 Insert_Before (Stmt, Call);
4780 else
4781 Set_Handled_Statement_Sequence (N,
4782 Make_Handled_Sequence_Of_Statements (Loc,
4783 Statements => New_List (Call)));
4784 end if;
4785 end if;
4787 Analyze (Call);
4789 if Legacy_Elaboration_Checks then
4790 Check_Task_Activation (N);
4791 end if;
4792 end Build_Task_Activation_Call;
4794 -------------------------------
4795 -- Build_Task_Allocate_Block --
4796 -------------------------------
4798 procedure Build_Task_Allocate_Block
4799 (Actions : List_Id;
4800 N : Node_Id;
4801 Args : List_Id)
4803 T : constant Entity_Id := Entity (Expression (N));
4804 Init : constant Entity_Id := Base_Init_Proc (T);
4805 Loc : constant Source_Ptr := Sloc (N);
4806 Chain : constant Entity_Id :=
4807 Make_Defining_Identifier (Loc, Name_uChain);
4808 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4809 Block : Node_Id;
4811 begin
4812 Block :=
4813 Make_Block_Statement (Loc,
4814 Identifier => New_Occurrence_Of (Blkent, Loc),
4815 Declarations => New_List (
4817 -- _Chain : Activation_Chain;
4819 Make_Object_Declaration (Loc,
4820 Defining_Identifier => Chain,
4821 Aliased_Present => True,
4822 Object_Definition =>
4823 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
4825 Handled_Statement_Sequence =>
4826 Make_Handled_Sequence_Of_Statements (Loc,
4828 Statements => New_List (
4830 -- Init (Args);
4832 Make_Procedure_Call_Statement (Loc,
4833 Name => New_Occurrence_Of (Init, Loc),
4834 Parameter_Associations => Args),
4836 -- Activate_Tasks (_Chain);
4838 Make_Procedure_Call_Statement (Loc,
4839 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4840 Parameter_Associations => New_List (
4841 Make_Attribute_Reference (Loc,
4842 Prefix => New_Occurrence_Of (Chain, Loc),
4843 Attribute_Name => Name_Unchecked_Access))))),
4845 Has_Created_Identifier => True,
4846 Is_Task_Allocation_Block => True);
4848 Append_To (Actions,
4849 Make_Implicit_Label_Declaration (Loc,
4850 Defining_Identifier => Blkent,
4851 Label_Construct => Block));
4853 Append_To (Actions, Block);
4855 Set_Activation_Chain_Entity (Block, Chain);
4856 end Build_Task_Allocate_Block;
4858 -----------------------------------------------
4859 -- Build_Task_Allocate_Block_With_Init_Stmts --
4860 -----------------------------------------------
4862 procedure Build_Task_Allocate_Block_With_Init_Stmts
4863 (Actions : List_Id;
4864 N : Node_Id;
4865 Init_Stmts : List_Id)
4867 Loc : constant Source_Ptr := Sloc (N);
4868 Chain : constant Entity_Id :=
4869 Make_Defining_Identifier (Loc, Name_uChain);
4870 Blkent : constant Entity_Id := Make_Temporary (Loc, 'A');
4871 Block : Node_Id;
4873 begin
4874 Append_To (Init_Stmts,
4875 Make_Procedure_Call_Statement (Loc,
4876 Name => New_Occurrence_Of (RTE (RE_Activate_Tasks), Loc),
4877 Parameter_Associations => New_List (
4878 Make_Attribute_Reference (Loc,
4879 Prefix => New_Occurrence_Of (Chain, Loc),
4880 Attribute_Name => Name_Unchecked_Access))));
4882 Block :=
4883 Make_Block_Statement (Loc,
4884 Identifier => New_Occurrence_Of (Blkent, Loc),
4885 Declarations => New_List (
4887 -- _Chain : Activation_Chain;
4889 Make_Object_Declaration (Loc,
4890 Defining_Identifier => Chain,
4891 Aliased_Present => True,
4892 Object_Definition =>
4893 New_Occurrence_Of (RTE (RE_Activation_Chain), Loc))),
4895 Handled_Statement_Sequence =>
4896 Make_Handled_Sequence_Of_Statements (Loc, Init_Stmts),
4898 Has_Created_Identifier => True,
4899 Is_Task_Allocation_Block => True);
4901 Append_To (Actions,
4902 Make_Implicit_Label_Declaration (Loc,
4903 Defining_Identifier => Blkent,
4904 Label_Construct => Block));
4906 Append_To (Actions, Block);
4908 Set_Activation_Chain_Entity (Block, Chain);
4909 end Build_Task_Allocate_Block_With_Init_Stmts;
4911 -----------------------------------
4912 -- Build_Task_Proc_Specification --
4913 -----------------------------------
4915 function Build_Task_Proc_Specification (T : Entity_Id) return Node_Id is
4916 Loc : constant Source_Ptr := Sloc (T);
4917 Spec_Id : Entity_Id;
4919 begin
4920 -- Case of explicit task type, suffix TB
4922 if Comes_From_Source (T) then
4923 Spec_Id :=
4924 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), "TB"));
4926 -- Case of anonymous task type, suffix B
4928 else
4929 Spec_Id :=
4930 Make_Defining_Identifier (Loc, New_External_Name (Chars (T), 'B'));
4931 end if;
4933 Set_Is_Internal (Spec_Id);
4935 -- Associate the procedure with the task, if this is the declaration
4936 -- (and not the body) of the procedure.
4938 if No (Task_Body_Procedure (T)) then
4939 Set_Task_Body_Procedure (T, Spec_Id);
4940 end if;
4942 return
4943 Make_Procedure_Specification (Loc,
4944 Defining_Unit_Name => Spec_Id,
4945 Parameter_Specifications => New_List (
4946 Make_Parameter_Specification (Loc,
4947 Defining_Identifier =>
4948 Make_Defining_Identifier (Loc, Name_uTask),
4949 Parameter_Type =>
4950 Make_Access_Definition (Loc,
4951 Subtype_Mark =>
4952 New_Occurrence_Of (Corresponding_Record_Type (T), Loc)))));
4953 end Build_Task_Proc_Specification;
4955 ---------------------------------------
4956 -- Build_Unprotected_Subprogram_Body --
4957 ---------------------------------------
4959 function Build_Unprotected_Subprogram_Body
4960 (N : Node_Id;
4961 Pid : Node_Id) return Node_Id
4963 Decls : constant List_Id := Declarations (N);
4965 begin
4966 -- Add renamings for the Protection object, discriminals, privals, and
4967 -- the entry index constant for use by debugger.
4969 Debug_Private_Data_Declarations (Decls);
4971 -- Make an unprotected version of the subprogram for use within the same
4972 -- object, with a new name and an additional parameter representing the
4973 -- object.
4975 return
4976 Make_Subprogram_Body (Sloc (N),
4977 Specification =>
4978 Build_Protected_Sub_Specification (N, Pid, Unprotected_Mode),
4979 Declarations => Decls,
4980 Handled_Statement_Sequence => Handled_Statement_Sequence (N),
4981 At_End_Proc => At_End_Proc (N));
4982 end Build_Unprotected_Subprogram_Body;
4984 ----------------------------
4985 -- Collect_Entry_Families --
4986 ----------------------------
4988 procedure Collect_Entry_Families
4989 (Loc : Source_Ptr;
4990 Cdecls : List_Id;
4991 Current_Node : in out Node_Id;
4992 Conctyp : Entity_Id)
4994 Efam : Entity_Id;
4995 Efam_Decl : Node_Id;
4996 Efam_Type : Entity_Id;
4998 begin
4999 Efam := First_Entity (Conctyp);
5000 while Present (Efam) loop
5001 if Ekind (Efam) = E_Entry_Family then
5002 Efam_Type := Make_Temporary (Loc, 'F');
5004 declare
5005 Eityp : constant Entity_Id := Entry_Index_Type (Efam);
5006 Lo : constant Node_Id := Type_Low_Bound (Eityp);
5007 Hi : constant Node_Id := Type_High_Bound (Eityp);
5008 Bdecl : Node_Id;
5009 Bityp : Entity_Id;
5011 begin
5012 Bityp := Base_Type (Eityp);
5014 if Is_Potentially_Large_Family (Bityp, Conctyp, Lo, Hi) then
5015 Bityp := Make_Temporary (Loc, 'B');
5017 Bdecl :=
5018 Make_Subtype_Declaration (Loc,
5019 Defining_Identifier => Bityp,
5020 Subtype_Indication =>
5021 Make_Subtype_Indication (Loc,
5022 Subtype_Mark =>
5023 New_Occurrence_Of (Standard_Integer, Loc),
5024 Constraint =>
5025 Make_Range_Constraint (Loc,
5026 Range_Expression => Make_Range (Loc,
5027 Make_Integer_Literal
5028 (Loc, -Entry_Family_Bound),
5029 Make_Integer_Literal
5030 (Loc, Entry_Family_Bound - 1)))));
5032 Insert_After (Current_Node, Bdecl);
5033 Current_Node := Bdecl;
5034 Analyze (Bdecl);
5035 end if;
5037 Efam_Decl :=
5038 Make_Full_Type_Declaration (Loc,
5039 Defining_Identifier => Efam_Type,
5040 Type_Definition =>
5041 Make_Unconstrained_Array_Definition (Loc,
5042 Subtype_Marks =>
5043 (New_List (New_Occurrence_Of (Bityp, Loc))),
5045 Component_Definition =>
5046 Make_Component_Definition (Loc,
5047 Aliased_Present => False,
5048 Subtype_Indication =>
5049 New_Occurrence_Of (Standard_Character, Loc))));
5050 end;
5052 Insert_After (Current_Node, Efam_Decl);
5053 Current_Node := Efam_Decl;
5054 Analyze (Efam_Decl);
5056 Append_To (Cdecls,
5057 Make_Component_Declaration (Loc,
5058 Defining_Identifier =>
5059 Make_Defining_Identifier (Loc, Chars (Efam)),
5061 Component_Definition =>
5062 Make_Component_Definition (Loc,
5063 Aliased_Present => False,
5064 Subtype_Indication =>
5065 Make_Subtype_Indication (Loc,
5066 Subtype_Mark =>
5067 New_Occurrence_Of (Efam_Type, Loc),
5069 Constraint =>
5070 Make_Index_Or_Discriminant_Constraint (Loc,
5071 Constraints => New_List (
5072 New_Occurrence_Of (Entry_Index_Type (Efam),
5073 Loc)))))));
5074 end if;
5076 Next_Entity (Efam);
5077 end loop;
5078 end Collect_Entry_Families;
5080 -----------------------
5081 -- Concurrent_Object --
5082 -----------------------
5084 function Concurrent_Object
5085 (Spec_Id : Entity_Id;
5086 Conc_Typ : Entity_Id) return Entity_Id
5088 begin
5089 -- Parameter _O or _object
5091 if Is_Protected_Type (Conc_Typ) then
5092 return First_Formal (Protected_Body_Subprogram (Spec_Id));
5094 -- Parameter _task
5096 else
5097 pragma Assert (Is_Task_Type (Conc_Typ));
5098 return First_Formal (Task_Body_Procedure (Conc_Typ));
5099 end if;
5100 end Concurrent_Object;
5102 ----------------------
5103 -- Copy_Result_Type --
5104 ----------------------
5106 function Copy_Result_Type (Res : Node_Id) return Node_Id is
5107 New_Res : constant Node_Id := New_Copy_Tree (Res);
5108 Par_Spec : Node_Id;
5109 Formal : Entity_Id;
5111 begin
5112 -- If the result type is an access_to_subprogram, we must create new
5113 -- entities for its spec.
5115 if Nkind (New_Res) = N_Access_Definition
5116 and then Present (Access_To_Subprogram_Definition (New_Res))
5117 then
5118 -- Provide new entities for the formals
5120 Par_Spec := First (Parameter_Specifications
5121 (Access_To_Subprogram_Definition (New_Res)));
5122 while Present (Par_Spec) loop
5123 Formal := Defining_Identifier (Par_Spec);
5124 Set_Defining_Identifier (Par_Spec,
5125 Make_Defining_Identifier (Sloc (Formal), Chars (Formal)));
5126 Next (Par_Spec);
5127 end loop;
5128 end if;
5130 return New_Res;
5131 end Copy_Result_Type;
5133 --------------------
5134 -- Concurrent_Ref --
5135 --------------------
5137 -- The expression returned for a reference to a concurrent object has the
5138 -- form:
5140 -- taskV!(name)._Task_Id
5142 -- for a task, and
5144 -- objectV!(name)._Object
5146 -- for a protected object. For the case of an access to a concurrent
5147 -- object, there is an extra explicit dereference:
5149 -- taskV!(name.all)._Task_Id
5150 -- objectV!(name.all)._Object
5152 -- here taskV and objectV are the types for the associated records, which
5153 -- contain the required _Task_Id and _Object fields for tasks and protected
5154 -- objects, respectively.
5156 -- For the case of a task type name, the expression is
5158 -- Self;
5160 -- i.e. a call to the Self function which returns precisely this Task_Id
5162 -- For the case of a protected type name, the expression is
5164 -- objectR
5166 -- which is a renaming of the _object field of the current object
5167 -- record, passed into protected operations as a parameter.
5169 function Concurrent_Ref (N : Node_Id) return Node_Id is
5170 Loc : constant Source_Ptr := Sloc (N);
5171 Ntyp : constant Entity_Id := Etype (N);
5172 Dtyp : Entity_Id;
5173 Sel : Name_Id;
5175 function Is_Current_Task (T : Entity_Id) return Boolean;
5176 -- Check whether the reference is to the immediately enclosing task
5177 -- type, or to an outer one (rare but legal).
5179 ---------------------
5180 -- Is_Current_Task --
5181 ---------------------
5183 function Is_Current_Task (T : Entity_Id) return Boolean is
5184 Scop : Entity_Id;
5186 begin
5187 Scop := Current_Scope;
5188 while Present (Scop) and then Scop /= Standard_Standard loop
5189 if Scop = T then
5190 return True;
5192 elsif Is_Task_Type (Scop) then
5193 return False;
5195 -- If this is a procedure nested within the task type, we must
5196 -- assume that it can be called from an inner task, and therefore
5197 -- cannot treat it as a local reference.
5199 elsif Is_Overloadable (Scop) and then In_Open_Scopes (T) then
5200 return False;
5202 else
5203 Scop := Scope (Scop);
5204 end if;
5205 end loop;
5207 -- We know that we are within the task body, so should have found it
5208 -- in scope.
5210 raise Program_Error;
5211 end Is_Current_Task;
5213 -- Start of processing for Concurrent_Ref
5215 begin
5216 if Is_Access_Type (Ntyp) then
5217 Dtyp := Designated_Type (Ntyp);
5219 if Is_Protected_Type (Dtyp) then
5220 Sel := Name_uObject;
5221 else
5222 Sel := Name_uTask_Id;
5223 end if;
5225 return
5226 Make_Selected_Component (Loc,
5227 Prefix =>
5228 Unchecked_Convert_To (Corresponding_Record_Type (Dtyp),
5229 Make_Explicit_Dereference (Loc, N)),
5230 Selector_Name => Make_Identifier (Loc, Sel));
5232 elsif Is_Entity_Name (N) and then Is_Concurrent_Type (Entity (N)) then
5233 if Is_Task_Type (Entity (N)) then
5235 if Is_Current_Task (Entity (N)) then
5236 return
5237 Make_Function_Call (Loc,
5238 Name => New_Occurrence_Of (RTE (RE_Self), Loc));
5240 else
5241 declare
5242 Decl : Node_Id;
5243 T_Self : constant Entity_Id := Make_Temporary (Loc, 'T');
5244 T_Body : constant Node_Id :=
5245 Parent (Corresponding_Body (Parent (Entity (N))));
5247 begin
5248 Decl :=
5249 Make_Object_Declaration (Loc,
5250 Defining_Identifier => T_Self,
5251 Object_Definition =>
5252 New_Occurrence_Of (RTE (RO_ST_Task_Id), Loc),
5253 Expression =>
5254 Make_Function_Call (Loc,
5255 Name => New_Occurrence_Of (RTE (RE_Self), Loc)));
5256 Prepend (Decl, Declarations (T_Body));
5257 Analyze (Decl);
5258 Set_Scope (T_Self, Entity (N));
5259 return New_Occurrence_Of (T_Self, Loc);
5260 end;
5261 end if;
5263 else
5264 pragma Assert (Is_Protected_Type (Entity (N)));
5266 return
5267 New_Occurrence_Of (Find_Protection_Object (Current_Scope), Loc);
5268 end if;
5270 else
5271 if Is_Protected_Type (Ntyp) then
5272 Sel := Name_uObject;
5273 elsif Is_Task_Type (Ntyp) then
5274 Sel := Name_uTask_Id;
5275 else
5276 raise Program_Error;
5277 end if;
5279 return
5280 Make_Selected_Component (Loc,
5281 Prefix =>
5282 Unchecked_Convert_To (Corresponding_Record_Type (Ntyp),
5283 New_Copy_Tree (N)),
5284 Selector_Name => Make_Identifier (Loc, Sel));
5285 end if;
5286 end Concurrent_Ref;
5288 ------------------------
5289 -- Convert_Concurrent --
5290 ------------------------
5292 function Convert_Concurrent
5293 (N : Node_Id;
5294 Typ : Entity_Id) return Node_Id
5296 begin
5297 if not Is_Concurrent_Type (Typ) then
5298 return N;
5299 else
5300 return
5301 Unchecked_Convert_To
5302 (Corresponding_Record_Type (Typ), New_Copy_Tree (N));
5303 end if;
5304 end Convert_Concurrent;
5306 -------------------------------------
5307 -- Create_Secondary_Stack_For_Task --
5308 -------------------------------------
5310 function Create_Secondary_Stack_For_Task (T : Node_Id) return Boolean is
5311 begin
5312 return
5313 (Restriction_Active (No_Implicit_Heap_Allocations)
5314 or else Restriction_Active (No_Implicit_Task_Allocations))
5315 and then not Restriction_Active (No_Secondary_Stack)
5316 and then Has_Rep_Pragma
5317 (T, Name_Secondary_Stack_Size, Check_Parents => False);
5318 end Create_Secondary_Stack_For_Task;
5320 -------------------------------------
5321 -- Debug_Private_Data_Declarations --
5322 -------------------------------------
5324 procedure Debug_Private_Data_Declarations (Decls : List_Id) is
5325 Debug_Nod : Node_Id;
5326 Decl : Node_Id;
5328 begin
5329 Decl := First (Decls);
5330 while Present (Decl) and then not Comes_From_Source (Decl) loop
5332 -- Declaration for concurrent entity _object and its access type,
5333 -- along with the entry index subtype:
5334 -- type prot_typVP is access prot_typV;
5335 -- _object : prot_typVP := prot_typV (_O);
5336 -- subtype Jnn is <Type of Index> range Low .. High;
5338 if Nkind (Decl) in N_Full_Type_Declaration | N_Object_Declaration then
5339 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5341 -- Declaration for the Protection object, discriminals, privals, and
5342 -- entry index constant:
5343 -- conc_typR : protection_typ renames _object._object;
5344 -- discr_nameD : discr_typ renames _object.discr_name;
5345 -- discr_nameD : discr_typ renames _task.discr_name;
5346 -- prival_name : comp_typ renames _object.comp_name;
5347 -- J : constant Jnn :=
5348 -- Jnn'Val (_E - <Index expression> + Jnn'Pos (Jnn'First));
5350 elsif Nkind (Decl) = N_Object_Renaming_Declaration then
5351 Set_Debug_Info_Needed (Defining_Identifier (Decl));
5352 Debug_Nod := Debug_Renaming_Declaration (Decl);
5354 if Present (Debug_Nod) then
5355 Insert_After (Decl, Debug_Nod);
5356 end if;
5357 end if;
5359 Next (Decl);
5360 end loop;
5361 end Debug_Private_Data_Declarations;
5363 ------------------------------
5364 -- Ensure_Statement_Present --
5365 ------------------------------
5367 procedure Ensure_Statement_Present (Loc : Source_Ptr; Alt : Node_Id) is
5368 Stmt : Node_Id;
5370 begin
5371 if Opt.Suppress_Control_Flow_Optimizations
5372 and then Is_Empty_List (Statements (Alt))
5373 then
5374 Stmt := Make_Null_Statement (Loc);
5376 -- Mark NULL statement as coming from source so that it is not
5377 -- eliminated by GIGI.
5379 -- Another covert channel. If this is a requirement, it must be
5380 -- documented in sinfo/einfo ???
5382 Set_Comes_From_Source (Stmt, True);
5384 Set_Statements (Alt, New_List (Stmt));
5385 end if;
5386 end Ensure_Statement_Present;
5388 ----------------------------
5389 -- Entry_Index_Expression --
5390 ----------------------------
5392 function Entry_Index_Expression
5393 (Sloc : Source_Ptr;
5394 Ent : Entity_Id;
5395 Index : Node_Id;
5396 Ttyp : Entity_Id) return Node_Id
5398 Expr : Node_Id;
5399 Num : Node_Id;
5400 Lo : Node_Id;
5401 Hi : Node_Id;
5402 Prev : Entity_Id;
5403 S : Node_Id;
5405 begin
5406 -- The queues of entries and entry families appear in textual order in
5407 -- the associated record. The entry index is computed as the sum of the
5408 -- number of queues for all entries that precede the designated one, to
5409 -- which is added the index expression, if this expression denotes a
5410 -- member of a family.
5412 -- The following is a place holder for the count of simple entries
5414 Num := Make_Integer_Literal (Sloc, 1);
5416 -- We construct an expression which is a series of addition operations.
5417 -- The first operand is the number of single entries that precede this
5418 -- one, the second operand is the index value relative to the start of
5419 -- the referenced family, and the remaining operands are the lengths of
5420 -- the entry families that precede this entry, i.e. the constructed
5421 -- expression is:
5423 -- number_simple_entries +
5424 -- (s'pos (index-value) - s'pos (family'first)) + 1 +
5425 -- family'length + ...
5427 -- where index-value is the given index value, and s is the index
5428 -- subtype (we have to use pos because the subtype might be an
5429 -- enumeration type preventing direct subtraction). Note that the task
5430 -- entry array is one-indexed.
5432 -- The upper bound of the entry family may be a discriminant, so we
5433 -- retrieve the lower bound explicitly to compute offset, rather than
5434 -- using the index subtype which may mention a discriminant.
5436 if Present (Index) then
5437 S := Entry_Index_Type (Ent);
5439 -- First make sure the index is in range if requested. The index type
5440 -- is the pristine Entry_Index_Type of the entry.
5442 if Do_Range_Check (Index) then
5443 Generate_Range_Check (Index, S, CE_Range_Check_Failed);
5444 end if;
5446 Expr :=
5447 Make_Op_Add (Sloc,
5448 Left_Opnd => Num,
5449 Right_Opnd =>
5450 Family_Offset
5451 (Sloc,
5452 Make_Attribute_Reference (Sloc,
5453 Attribute_Name => Name_Pos,
5454 Prefix => New_Occurrence_Of (Base_Type (S), Sloc),
5455 Expressions => New_List (Relocate_Node (Index))),
5456 Type_Low_Bound (S),
5457 Ttyp,
5458 False));
5459 else
5460 Expr := Num;
5461 end if;
5463 -- Now add lengths of preceding entries and entry families
5465 Prev := First_Entity (Ttyp);
5466 while Chars (Prev) /= Chars (Ent)
5467 or else Ekind (Prev) /= Ekind (Ent)
5468 or else not Sem_Ch6.Type_Conformant (Ent, Prev)
5469 loop
5470 if Ekind (Prev) = E_Entry then
5471 Set_Intval (Num, Intval (Num) + 1);
5473 elsif Ekind (Prev) = E_Entry_Family then
5474 S := Entry_Index_Type (Prev);
5475 Lo := Type_Low_Bound (S);
5476 Hi := Type_High_Bound (S);
5478 Expr :=
5479 Make_Op_Add (Sloc,
5480 Left_Opnd => Expr,
5481 Right_Opnd => Family_Size (Sloc, Hi, Lo, Ttyp, False));
5483 -- Other components are anonymous types to be ignored
5485 else
5486 null;
5487 end if;
5489 Next_Entity (Prev);
5490 end loop;
5492 return Expr;
5493 end Entry_Index_Expression;
5495 ---------------------------
5496 -- Establish_Task_Master --
5497 ---------------------------
5499 procedure Establish_Task_Master (N : Node_Id) is
5500 Call : Node_Id;
5502 begin
5503 if Restriction_Active (No_Task_Hierarchy) = False then
5504 Call := Build_Runtime_Call (Sloc (N), RE_Enter_Master);
5506 -- The block may have no declarations (and nevertheless be a task
5507 -- master) if it contains a call that may return an object that
5508 -- contains tasks.
5510 if No (Declarations (N)) then
5511 Set_Declarations (N, New_List (Call));
5512 else
5513 Prepend_To (Declarations (N), Call);
5514 end if;
5516 Analyze (Call);
5517 end if;
5518 end Establish_Task_Master;
5520 --------------------------------
5521 -- Expand_Accept_Declarations --
5522 --------------------------------
5524 -- Part of the expansion of an accept statement involves the creation of
5525 -- a declaration that can be referenced from the statement sequence of
5526 -- the accept:
5528 -- Ann : Address;
5530 -- This declaration is inserted immediately before the accept statement
5531 -- and it is important that it be inserted before the statements of the
5532 -- statement sequence are analyzed. Thus it would be too late to create
5533 -- this declaration in the Expand_N_Accept_Statement routine, which is
5534 -- why there is a separate procedure to be called directly from Sem_Ch9.
5536 -- Ann is used to hold the address of the record containing the parameters
5537 -- (see Expand_N_Entry_Call for more details on how this record is built).
5538 -- References to the parameters do an unchecked conversion of this address
5539 -- to a pointer to the required record type, and then access the field that
5540 -- holds the value of the required parameter. The entity for the address
5541 -- variable is held as the top stack element (i.e. the last element) of the
5542 -- Accept_Address stack in the corresponding entry entity, and this element
5543 -- must be set in place before the statements are processed.
5545 -- The above description applies to the case of a stand alone accept
5546 -- statement, i.e. one not appearing as part of a select alternative.
5548 -- For the case of an accept that appears as part of a select alternative
5549 -- of a selective accept, we must still create the declaration right away,
5550 -- since Ann is needed immediately, but there is an important difference:
5552 -- The declaration is inserted before the selective accept, not before
5553 -- the accept statement (which is not part of a list anyway, and so would
5554 -- not accommodate inserted declarations)
5556 -- We only need one address variable for the entire selective accept. So
5557 -- the Ann declaration is created only for the first accept alternative,
5558 -- and subsequent accept alternatives reference the same Ann variable.
5560 -- We can distinguish the two cases by seeing whether the accept statement
5561 -- is part of a list. If not, then it must be in an accept alternative.
5563 -- To expand the requeue statement, a label is provided at the end of the
5564 -- accept statement or alternative of which it is a part, so that the
5565 -- statement can be skipped after the requeue is complete. This label is
5566 -- created here rather than during the expansion of the accept statement,
5567 -- because it will be needed by any requeue statements within the accept,
5568 -- which are expanded before the accept.
5570 procedure Expand_Accept_Declarations (N : Node_Id; Ent : Entity_Id) is
5571 Loc : constant Source_Ptr := Sloc (N);
5572 Stats : constant Node_Id := Handled_Statement_Sequence (N);
5573 Ann : Entity_Id := Empty;
5574 Adecl : Node_Id;
5575 Lab : Node_Id;
5576 Ldecl : Node_Id;
5577 Ldecl2 : Node_Id;
5579 begin
5580 if Expander_Active then
5582 -- If we have no handled statement sequence, we may need to build
5583 -- a dummy sequence consisting of a null statement. This can be
5584 -- skipped if the trivial accept optimization is permitted.
5586 if not Trivial_Accept_OK
5587 and then (No (Stats) or else Null_Statements (Statements (Stats)))
5588 then
5589 Set_Handled_Statement_Sequence (N,
5590 Make_Handled_Sequence_Of_Statements (Loc,
5591 Statements => New_List (Make_Null_Statement (Loc))));
5592 end if;
5594 -- Create and declare two labels to be placed at the end of the
5595 -- accept statement. The first label is used to allow requeues to
5596 -- skip the remainder of entry processing. The second label is used
5597 -- to skip the remainder of entry processing if the rendezvous
5598 -- completes in the middle of the accept body.
5600 if Present (Handled_Statement_Sequence (N)) then
5601 declare
5602 Ent : Entity_Id;
5604 begin
5605 Ent := Make_Temporary (Loc, 'L');
5606 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5607 Ldecl :=
5608 Make_Implicit_Label_Declaration (Loc,
5609 Defining_Identifier => Ent,
5610 Label_Construct => Lab);
5611 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5613 Ent := Make_Temporary (Loc, 'L');
5614 Lab := Make_Label (Loc, New_Occurrence_Of (Ent, Loc));
5615 Ldecl2 :=
5616 Make_Implicit_Label_Declaration (Loc,
5617 Defining_Identifier => Ent,
5618 Label_Construct => Lab);
5619 Append (Lab, Statements (Handled_Statement_Sequence (N)));
5620 end;
5622 else
5623 Ldecl := Empty;
5624 Ldecl2 := Empty;
5625 end if;
5627 -- Case of stand alone accept statement
5629 if Is_List_Member (N) then
5631 if Present (Handled_Statement_Sequence (N)) then
5632 Ann := Make_Temporary (Loc, 'A');
5634 Adecl :=
5635 Make_Object_Declaration (Loc,
5636 Defining_Identifier => Ann,
5637 Object_Definition =>
5638 New_Occurrence_Of (RTE (RE_Address), Loc));
5640 Insert_Before_And_Analyze (N, Adecl);
5641 Insert_Before_And_Analyze (N, Ldecl);
5642 Insert_Before_And_Analyze (N, Ldecl2);
5643 end if;
5645 -- Case of accept statement which is in an accept alternative
5647 else
5648 declare
5649 Acc_Alt : constant Node_Id := Parent (N);
5650 Sel_Acc : constant Node_Id := Parent (Acc_Alt);
5651 Alt : Node_Id;
5653 begin
5654 pragma Assert (Nkind (Acc_Alt) = N_Accept_Alternative);
5655 pragma Assert (Nkind (Sel_Acc) = N_Selective_Accept);
5657 -- ??? Consider a single label for select statements
5659 if Present (Handled_Statement_Sequence (N)) then
5660 Prepend (Ldecl2,
5661 Statements (Handled_Statement_Sequence (N)));
5662 Analyze (Ldecl2);
5664 Prepend (Ldecl,
5665 Statements (Handled_Statement_Sequence (N)));
5666 Analyze (Ldecl);
5667 end if;
5669 -- Find first accept alternative of the selective accept. A
5670 -- valid selective accept must have at least one accept in it.
5672 Alt := First (Select_Alternatives (Sel_Acc));
5674 while Nkind (Alt) /= N_Accept_Alternative loop
5675 Next (Alt);
5676 end loop;
5678 -- If this is the first accept statement, then we have to
5679 -- create the Ann variable, as for the stand alone case, except
5680 -- that it is inserted before the selective accept. Similarly,
5681 -- a label for requeue expansion must be declared.
5683 if N = Accept_Statement (Alt) then
5684 Ann := Make_Temporary (Loc, 'A');
5685 Adecl :=
5686 Make_Object_Declaration (Loc,
5687 Defining_Identifier => Ann,
5688 Object_Definition =>
5689 New_Occurrence_Of (RTE (RE_Address), Loc));
5691 Insert_Before_And_Analyze (Sel_Acc, Adecl);
5693 -- If this is not the first accept statement, then find the Ann
5694 -- variable allocated by the first accept and use it.
5696 else
5697 Ann :=
5698 Node (Last_Elmt (Accept_Address
5699 (Entity (Entry_Direct_Name (Accept_Statement (Alt))))));
5700 end if;
5701 end;
5702 end if;
5704 -- Merge here with Ann either created or referenced, and Adecl
5705 -- pointing to the corresponding declaration. Remaining processing
5706 -- is the same for the two cases.
5708 if Present (Ann) then
5709 Append_Elmt (Ann, Accept_Address (Ent));
5710 Set_Debug_Info_Needed (Ann);
5711 end if;
5713 -- Create renaming declarations for the entry formals. Each reference
5714 -- to a formal becomes a dereference of a component of the parameter
5715 -- block, whose address is held in Ann. These declarations are
5716 -- eventually inserted into the accept block, and analyzed there so
5717 -- that they have the proper scope for gdb and do not conflict with
5718 -- other declarations.
5720 if Present (Parameter_Specifications (N))
5721 and then Present (Handled_Statement_Sequence (N))
5722 then
5723 declare
5724 Comp : Entity_Id;
5725 Decl : Node_Id;
5726 Formal : Entity_Id;
5727 New_F : Entity_Id;
5728 Renamed_Formal : Node_Id;
5730 begin
5731 Push_Scope (Ent);
5732 Formal := First_Formal (Ent);
5734 while Present (Formal) loop
5735 Comp := Entry_Component (Formal);
5736 New_F := Make_Defining_Identifier (Loc, Chars (Formal));
5738 Set_Etype (New_F, Etype (Formal));
5739 Set_Scope (New_F, Ent);
5741 -- Now we set debug info needed on New_F even though it does
5742 -- not come from source, so that the debugger will get the
5743 -- right information for these generated names.
5745 Set_Debug_Info_Needed (New_F);
5747 if Ekind (Formal) = E_In_Parameter then
5748 Mutate_Ekind (New_F, E_Constant);
5749 else
5750 Mutate_Ekind (New_F, E_Variable);
5751 Set_Extra_Constrained (New_F, Extra_Constrained (Formal));
5752 end if;
5754 Set_Actual_Subtype (New_F, Actual_Subtype (Formal));
5756 Renamed_Formal :=
5757 Make_Selected_Component (Loc,
5758 Prefix =>
5759 Make_Explicit_Dereference (Loc,
5760 Unchecked_Convert_To (
5761 Entry_Parameters_Type (Ent),
5762 New_Occurrence_Of (Ann, Loc))),
5763 Selector_Name =>
5764 New_Occurrence_Of (Comp, Loc));
5766 Decl :=
5767 Build_Renamed_Formal_Declaration
5768 (New_F, Formal, Comp, Renamed_Formal);
5770 if No (Declarations (N)) then
5771 Set_Declarations (N, New_List);
5772 end if;
5774 Append (Decl, Declarations (N));
5775 Set_Renamed_Object (Formal, New_F);
5776 Next_Formal (Formal);
5777 end loop;
5779 End_Scope;
5780 end;
5781 end if;
5782 end if;
5783 end Expand_Accept_Declarations;
5785 ---------------------------------------------
5786 -- Expand_Access_Protected_Subprogram_Type --
5787 ---------------------------------------------
5789 procedure Expand_Access_Protected_Subprogram_Type (N : Node_Id) is
5790 Loc : constant Source_Ptr := Sloc (N);
5791 T : constant Entity_Id := Defining_Identifier (N);
5792 D_T : constant Entity_Id := Designated_Type (T);
5793 D_T2 : constant Entity_Id := Make_Temporary (Loc, 'D');
5794 E_T : constant Entity_Id := Make_Temporary (Loc, 'E');
5795 P_List : constant List_Id :=
5796 Build_Protected_Spec (N, RTE (RE_Address), D_T, False);
5798 Comps : List_Id;
5799 Decl1 : Node_Id;
5800 Decl2 : Node_Id;
5801 Def1 : Node_Id;
5803 begin
5804 -- Create access to subprogram with full signature
5806 if Etype (D_T) /= Standard_Void_Type then
5807 Def1 :=
5808 Make_Access_Function_Definition (Loc,
5809 Parameter_Specifications => P_List,
5810 Result_Definition =>
5811 Copy_Result_Type (Result_Definition (Type_Definition (N))));
5813 else
5814 Def1 :=
5815 Make_Access_Procedure_Definition (Loc,
5816 Parameter_Specifications => P_List);
5817 end if;
5819 Decl1 :=
5820 Make_Full_Type_Declaration (Loc,
5821 Defining_Identifier => D_T2,
5822 Type_Definition => Def1);
5824 -- Declare the new types before the original one since the latter will
5825 -- refer to them through the Equivalent_Type slot.
5827 Insert_Before_And_Analyze (N, Decl1);
5829 -- Associate the access to subprogram with its original access to
5830 -- protected subprogram type. Needed by the backend to know that this
5831 -- type corresponds with an access to protected subprogram type.
5833 Set_Original_Access_Type (D_T2, T);
5835 -- Create Equivalent_Type, a record with two components for an access to
5836 -- object and an access to subprogram.
5838 Comps := New_List (
5839 Make_Component_Declaration (Loc,
5840 Defining_Identifier => Make_Temporary (Loc, 'P'),
5841 Component_Definition =>
5842 Make_Component_Definition (Loc,
5843 Aliased_Present => False,
5844 Subtype_Indication =>
5845 New_Occurrence_Of (RTE (RE_Address), Loc))),
5847 Make_Component_Declaration (Loc,
5848 Defining_Identifier => Make_Temporary (Loc, 'S'),
5849 Component_Definition =>
5850 Make_Component_Definition (Loc,
5851 Aliased_Present => False,
5852 Subtype_Indication => New_Occurrence_Of (D_T2, Loc))));
5854 Decl2 :=
5855 Make_Full_Type_Declaration (Loc,
5856 Defining_Identifier => E_T,
5857 Type_Definition =>
5858 Make_Record_Definition (Loc,
5859 Component_List =>
5860 Make_Component_List (Loc, Component_Items => Comps)));
5862 Insert_Before_And_Analyze (N, Decl2);
5863 Set_Equivalent_Type (T, E_T);
5864 end Expand_Access_Protected_Subprogram_Type;
5866 --------------------------
5867 -- Expand_Entry_Barrier --
5868 --------------------------
5870 procedure Expand_Entry_Barrier (N : Node_Id; Ent : Entity_Id) is
5871 Cond : constant Node_Id := Condition (Entry_Body_Formal_Part (N));
5872 Prot : constant Entity_Id := Scope (Ent);
5873 Spec_Decl : constant Node_Id := Parent (Prot);
5875 Func_Id : Entity_Id := Empty;
5876 -- The entity of the barrier function
5878 function Is_Global_Entity (N : Node_Id) return Traverse_Result;
5879 -- Check whether entity in Barrier is external to protected type.
5880 -- If so, barrier may not be properly synchronized.
5882 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result;
5883 -- Check whether N meets the Pure_Barriers restriction. Return OK if
5884 -- so.
5886 function Is_Simple_Barrier (N : Node_Id) return Boolean;
5887 -- Check whether N meets the Simple_Barriers restriction. Return OK if
5888 -- so.
5890 ----------------------
5891 -- Is_Global_Entity --
5892 ----------------------
5894 function Is_Global_Entity (N : Node_Id) return Traverse_Result is
5895 E : Entity_Id;
5896 S : Entity_Id;
5898 begin
5899 if Is_Entity_Name (N) and then Present (Entity (N)) then
5900 E := Entity (N);
5901 S := Scope (E);
5903 if Ekind (E) = E_Variable then
5905 -- If the variable is local to the barrier function generated
5906 -- during expansion, it is ok. If expansion is not performed,
5907 -- then Func is Empty so this test cannot succeed.
5909 if Scope (E) = Func_Id then
5910 null;
5912 -- A protected call from a barrier to another object is ok
5914 elsif Ekind (Etype (E)) = E_Protected_Type then
5915 null;
5917 -- If the variable is within the package body we consider
5918 -- this safe. This is a common (if dubious) idiom.
5920 elsif S = Scope (Prot)
5921 and then Is_Package_Or_Generic_Package (S)
5922 and then Nkind (Parent (E)) = N_Object_Declaration
5923 and then Nkind (Parent (Parent (E))) = N_Package_Body
5924 then
5925 null;
5927 else
5928 Error_Msg_N ("potentially unsynchronized barrier??", N);
5929 Error_Msg_N ("\& should be private component of type??", N);
5930 end if;
5931 end if;
5932 end if;
5934 return OK;
5935 end Is_Global_Entity;
5937 procedure Check_Unprotected_Barrier is
5938 new Traverse_Proc (Is_Global_Entity);
5940 -----------------------
5941 -- Is_Simple_Barrier --
5942 -----------------------
5944 function Is_Simple_Barrier (N : Node_Id) return Boolean is
5945 Renamed : Node_Id;
5947 begin
5948 if Is_Static_Expression (N) then
5949 return True;
5950 elsif Ada_Version >= Ada_2022
5951 and then Nkind (N) in N_Selected_Component | N_Indexed_Component
5952 and then Statically_Names_Object (N)
5953 then
5954 -- Restriction relaxed in Ada 2022 to allow statically named
5955 -- subcomponents.
5956 return Is_Simple_Barrier (Prefix (N));
5957 end if;
5959 -- Check if the name is a component of the protected object. If
5960 -- the expander is active, the component has been transformed into a
5961 -- renaming of _object.all.component. Original_Node is needed in case
5962 -- validity checking is enabled, in which case the simple object
5963 -- reference will have been rewritten.
5965 if Expander_Active then
5967 -- The expanded name may have been constant folded in which case
5968 -- the original node is not necessarily an entity name (e.g. an
5969 -- indexed component).
5971 if not Is_Entity_Name (Original_Node (N)) then
5972 return False;
5973 end if;
5975 Renamed := Renamed_Object (Entity (Original_Node (N)));
5977 return
5978 Present (Renamed)
5979 and then Nkind (Renamed) = N_Selected_Component
5980 and then Chars (Prefix (Prefix (Renamed))) = Name_uObject;
5981 elsif not Is_Entity_Name (N) then
5982 return False;
5983 else
5984 return Is_Protected_Component (Entity (N));
5985 end if;
5986 end Is_Simple_Barrier;
5988 ---------------------
5989 -- Is_Pure_Barrier --
5990 ---------------------
5992 function Is_Pure_Barrier (N : Node_Id) return Traverse_Result is
5993 begin
5994 case Nkind (N) is
5995 when N_Expanded_Name
5996 | N_Identifier
5999 -- Because of N_Expanded_Name case, return Skip instead of OK.
6001 if No (Entity (N)) then
6002 return Abandon;
6004 elsif Is_Numeric_Type (Entity (N)) then
6005 return Skip;
6006 end if;
6008 case Ekind (Entity (N)) is
6009 when E_Constant
6010 | E_Discriminant
6012 return Skip;
6014 when E_Enumeration_Literal
6015 | E_Named_Integer
6016 | E_Named_Real
6018 if not Is_OK_Static_Expression (N) then
6019 return Abandon;
6020 end if;
6021 return Skip;
6023 when E_Component =>
6024 return Skip;
6026 when E_Variable =>
6027 if Is_Simple_Barrier (N) then
6028 return Skip;
6029 end if;
6031 when E_Function =>
6033 -- The count attribute has been transformed into run-time
6034 -- calls.
6036 if Is_RTE (Entity (N), RE_Protected_Count)
6037 or else Is_RTE (Entity (N), RE_Protected_Count_Entry)
6038 then
6039 return Skip;
6040 end if;
6042 when others =>
6043 null;
6044 end case;
6046 when N_Function_Call =>
6048 -- Function call checks are carried out as part of the analysis
6049 -- of the function call name.
6051 return OK;
6053 when N_Character_Literal
6054 | N_Integer_Literal
6055 | N_Real_Literal
6057 return OK;
6059 when N_Op_Boolean
6060 | N_Op_Not
6062 if Ekind (Entity (N)) = E_Operator then
6063 return OK;
6064 end if;
6066 when N_Short_Circuit
6067 | N_If_Expression
6068 | N_Case_Expression
6070 return OK;
6072 when N_Indexed_Component | N_Selected_Component =>
6073 if Statically_Names_Object (N) then
6074 return Is_Pure_Barrier (Prefix (N));
6075 else
6076 return Abandon;
6077 end if;
6079 when N_Case_Expression_Alternative =>
6080 -- do not traverse Discrete_Choices subtree
6081 if Is_Pure_Barrier (Expression (N)) /= Abandon then
6082 return Skip;
6083 end if;
6085 when N_Expression_With_Actions =>
6086 -- this may occur in the case of a Count attribute reference
6087 if Is_Rewrite_Substitution (N)
6088 and then Is_Pure_Barrier (Original_Node (N)) /= Abandon
6089 then
6090 return Skip;
6091 end if;
6093 when N_Membership_Test =>
6094 if Is_Pure_Barrier (Left_Opnd (N)) /= Abandon
6095 and then All_Membership_Choices_Static (N)
6096 then
6097 return Skip;
6098 end if;
6100 when N_Type_Conversion =>
6102 -- Conversions to Universal_Integer do not raise constraint
6103 -- errors. Likewise if the expression's type is statically
6104 -- compatible with the target's type.
6106 if Etype (N) = Universal_Integer
6107 or else Subtypes_Statically_Compatible
6108 (Etype (Expression (N)), Etype (N))
6109 then
6110 return OK;
6111 end if;
6113 when N_Unchecked_Type_Conversion =>
6114 return OK;
6116 when others =>
6117 null;
6118 end case;
6120 return Abandon;
6121 end Is_Pure_Barrier;
6123 function Check_Pure_Barriers is new Traverse_Func (Is_Pure_Barrier);
6125 -- Local variables
6127 Cond_Id : Entity_Id;
6128 Entry_Body : Node_Id;
6129 Func_Body : Node_Id := Empty;
6131 -- Start of processing for Expand_Entry_Barrier
6133 begin
6134 if No_Run_Time_Mode then
6135 Error_Msg_CRT ("entry barrier", N);
6136 return;
6137 end if;
6139 -- Prevent cascaded errors
6141 if Nkind (Cond) = N_Error then
6142 return;
6143 end if;
6145 -- The body of the entry barrier must be analyzed in the context of the
6146 -- protected object, but its scope is external to it, just as any other
6147 -- unprotected version of a protected operation. The specification has
6148 -- been produced when the protected type declaration was elaborated. We
6149 -- build the body, insert it in the enclosing scope, but analyze it in
6150 -- the current context. A more uniform approach would be to treat the
6151 -- barrier just as a protected function, and discard the protected
6152 -- version of it because it is never called.
6154 if Expander_Active then
6155 Func_Body := Build_Barrier_Function (N, Ent, Prot);
6156 Func_Id := Barrier_Function (Ent);
6157 Set_Corresponding_Spec (Func_Body, Func_Id);
6159 Entry_Body := Parent (Corresponding_Body (Spec_Decl));
6161 if Nkind (Parent (Entry_Body)) = N_Subunit then
6162 Entry_Body := Corresponding_Stub (Parent (Entry_Body));
6163 end if;
6165 Insert_Before_And_Analyze (Entry_Body, Func_Body);
6167 Set_Discriminals (Spec_Decl);
6168 Set_Scope (Func_Id, Scope (Prot));
6170 else
6171 Analyze_And_Resolve (Cond, Any_Boolean);
6172 end if;
6174 -- Check Simple_Barriers and Pure_Barriers restrictions.
6175 -- Note that it is safe to be calling Check_Restriction from here, even
6176 -- though this is part of the expander, since Expand_Entry_Barrier is
6177 -- called from Sem_Ch9 even in -gnatc mode.
6179 if not Is_Simple_Barrier (Cond) then
6180 -- flag restriction violation
6181 Check_Restriction (Simple_Barriers, Cond);
6182 end if;
6184 if Check_Pure_Barriers (Cond) = Abandon then
6185 -- flag restriction violation
6186 Check_Restriction (Pure_Barriers, Cond);
6188 -- Emit warning if barrier contains global entities and is thus
6189 -- potentially unsynchronized (if Pure_Barriers restrictions
6190 -- are met then no need to check for this).
6191 Check_Unprotected_Barrier (Cond);
6192 end if;
6194 if Is_Entity_Name (Cond) then
6195 Cond_Id := Entity (Cond);
6197 -- Perform a small optimization of simple barrier functions. If the
6198 -- scope of the condition's entity is not the barrier function, then
6199 -- the condition does not depend on any of the generated renamings.
6200 -- If this is the case, eliminate the renamings as they are useless.
6201 -- This optimization is not performed when the condition was folded
6202 -- and validity checks are in effect because the original condition
6203 -- may have produced at least one check that depends on the generated
6204 -- renamings.
6206 if Expander_Active
6207 and then Scope (Cond_Id) /= Func_Id
6208 and then not Validity_Check_Operands
6209 then
6210 Set_Declarations (Func_Body, Empty_List);
6211 end if;
6213 -- Note that after analysis variables in this context will be
6214 -- replaced by the corresponding prival, that is to say a renaming
6215 -- of a selected component of the form _Object.Var. If expansion is
6216 -- disabled, as within a generic, we check that the entity appears in
6217 -- the current scope.
6218 end if;
6219 end Expand_Entry_Barrier;
6221 ------------------------------
6222 -- Expand_N_Abort_Statement --
6223 ------------------------------
6225 -- Expand abort T1, T2, .. Tn; into:
6226 -- Abort_Tasks (Task_List'(1 => T1.Task_Id, 2 => T2.Task_Id ...))
6228 procedure Expand_N_Abort_Statement (N : Node_Id) is
6229 Loc : constant Source_Ptr := Sloc (N);
6230 Tlist : constant List_Id := Names (N);
6231 Count : Nat;
6232 Aggr : Node_Id;
6233 Tasknm : Node_Id;
6235 begin
6236 Aggr := Make_Aggregate (Loc, Component_Associations => New_List);
6237 Count := 0;
6239 Tasknm := First (Tlist);
6241 while Present (Tasknm) loop
6242 Count := Count + 1;
6244 -- A task interface class-wide type object is being aborted. Retrieve
6245 -- its _task_id by calling a dispatching routine.
6247 if Ada_Version >= Ada_2005
6248 and then Ekind (Etype (Tasknm)) = E_Class_Wide_Type
6249 and then Is_Interface (Etype (Tasknm))
6250 and then Is_Task_Interface (Etype (Tasknm))
6251 then
6252 Append_To (Component_Associations (Aggr),
6253 Make_Component_Association (Loc,
6254 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6255 Expression =>
6257 -- Task_Id (Tasknm._disp_get_task_id)
6259 Unchecked_Convert_To
6260 (RTE (RO_ST_Task_Id),
6261 Make_Selected_Component (Loc,
6262 Prefix => New_Copy_Tree (Tasknm),
6263 Selector_Name =>
6264 Make_Identifier (Loc, Name_uDisp_Get_Task_Id)))));
6266 else
6267 Append_To (Component_Associations (Aggr),
6268 Make_Component_Association (Loc,
6269 Choices => New_List (Make_Integer_Literal (Loc, Count)),
6270 Expression => Concurrent_Ref (Tasknm)));
6271 end if;
6273 Next (Tasknm);
6274 end loop;
6276 Rewrite (N,
6277 Make_Procedure_Call_Statement (Loc,
6278 Name => New_Occurrence_Of (RTE (RE_Abort_Tasks), Loc),
6279 Parameter_Associations => New_List (
6280 Make_Qualified_Expression (Loc,
6281 Subtype_Mark => New_Occurrence_Of (RTE (RE_Task_List), Loc),
6282 Expression => Aggr))));
6284 Analyze (N);
6285 end Expand_N_Abort_Statement;
6287 -------------------------------
6288 -- Expand_N_Accept_Statement --
6289 -------------------------------
6291 -- This procedure handles expansion of accept statements that stand alone,
6292 -- i.e. they are not part of an accept alternative. The expansion of
6293 -- accept statement in accept alternatives is handled by the routines
6294 -- Expand_N_Accept_Alternative and Expand_N_Selective_Accept. The
6295 -- following description applies only to stand alone accept statements.
6297 -- If there is no handled statement sequence, or only null statements, then
6298 -- this is called a trivial accept, and the expansion is:
6300 -- Accept_Trivial (entry-index)
6302 -- If there is a handled statement sequence, then the expansion is:
6304 -- Ann : Address;
6305 -- {Lnn : Label}
6307 -- begin
6308 -- begin
6309 -- Accept_Call (entry-index, Ann);
6310 -- Renaming_Declarations for formals
6311 -- <statement sequence from N_Accept_Statement node>
6312 -- Complete_Rendezvous;
6313 -- <<Lnn>>
6315 -- exception
6316 -- when ... =>
6317 -- <exception handler from N_Accept_Statement node>
6318 -- Complete_Rendezvous;
6319 -- when ... =>
6320 -- <exception handler from N_Accept_Statement node>
6321 -- Complete_Rendezvous;
6322 -- ...
6323 -- end;
6325 -- exception
6326 -- when all others =>
6327 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
6328 -- end;
6330 -- The first three declarations were already inserted ahead of the accept
6331 -- statement by the Expand_Accept_Declarations procedure, which was called
6332 -- directly from the semantics during analysis of the accept statement,
6333 -- before analyzing its contained statements.
6335 -- The declarations from the N_Accept_Statement, as noted in Sinfo, come
6336 -- from possible expansion activity (the original source of course does
6337 -- not have any declarations associated with the accept statement, since
6338 -- an accept statement has no declarative part). In particular, if the
6339 -- expander is active, the first such declaration is the declaration of
6340 -- the Accept_Params_Ptr entity (see Sem_Ch9.Analyze_Accept_Statement).
6342 -- The two blocks are merged into a single block if the inner block has
6343 -- no exception handlers, but otherwise two blocks are required, since
6344 -- exceptions might be raised in the exception handlers of the inner
6345 -- block, and Exceptional_Complete_Rendezvous must be called.
6347 procedure Expand_N_Accept_Statement (N : Node_Id) is
6348 Loc : constant Source_Ptr := Sloc (N);
6349 Stats : constant Node_Id := Handled_Statement_Sequence (N);
6350 Ename : constant Node_Id := Entry_Direct_Name (N);
6351 Eindx : constant Node_Id := Entry_Index (N);
6352 Eent : constant Entity_Id := Entity (Ename);
6353 Acstack : constant Elist_Id := Accept_Address (Eent);
6354 Ann : constant Entity_Id := Node (Last_Elmt (Acstack));
6355 Ttyp : constant Entity_Id := Etype (Scope (Eent));
6356 Blkent : Entity_Id;
6357 Call : Node_Id;
6358 Block : Node_Id;
6360 begin
6361 -- If the accept statement is not part of a list, then its parent must
6362 -- be an accept alternative, and, as described above, we do not do any
6363 -- expansion for such accept statements at this level.
6365 if not Is_List_Member (N) then
6366 pragma Assert (Nkind (Parent (N)) = N_Accept_Alternative);
6367 return;
6369 -- Trivial accept case (no statement sequence, or null statements).
6370 -- If the accept statement has declarations, then just insert them
6371 -- before the procedure call.
6373 elsif Trivial_Accept_OK
6374 and then (No (Stats) or else Null_Statements (Statements (Stats)))
6375 then
6376 -- Remove declarations for renamings, because the parameter block
6377 -- will not be assigned.
6379 declare
6380 D : Node_Id;
6381 Next_D : Node_Id;
6383 begin
6384 D := First (Declarations (N));
6385 while Present (D) loop
6386 Next_D := Next (D);
6387 if Nkind (D) = N_Object_Renaming_Declaration then
6388 Remove (D);
6389 end if;
6391 D := Next_D;
6392 end loop;
6393 end;
6395 if Present (Declarations (N)) then
6396 Insert_Actions (N, Declarations (N));
6397 end if;
6399 Rewrite (N,
6400 Make_Procedure_Call_Statement (Loc,
6401 Name => New_Occurrence_Of (RTE (RE_Accept_Trivial), Loc),
6402 Parameter_Associations => New_List (
6403 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp))));
6405 Analyze (N);
6407 -- Ada 2022 (AI12-0279)
6409 if Has_Yield_Aspect (Eent)
6410 and then RTE_Available (RE_Yield)
6411 then
6412 Insert_Action_After (N,
6413 Make_Procedure_Call_Statement (Loc,
6414 New_Occurrence_Of (RTE (RE_Yield), Loc)));
6415 end if;
6417 -- Discard Entry_Address that was created for it, so it will not be
6418 -- emitted if this accept statement is in the statement part of a
6419 -- delay alternative.
6421 if Present (Stats) then
6422 Remove_Last_Elmt (Acstack);
6423 end if;
6425 -- Case of statement sequence present
6427 else
6428 -- Construct the block, using the declarations from the accept
6429 -- statement if any to initialize the declarations of the block.
6431 Blkent := Make_Temporary (Loc, 'A');
6432 Mutate_Ekind (Blkent, E_Block);
6433 Set_Etype (Blkent, Standard_Void_Type);
6434 Set_Scope (Blkent, Current_Scope);
6436 Block :=
6437 Make_Block_Statement (Loc,
6438 Identifier => New_Occurrence_Of (Blkent, Loc),
6439 Declarations => Declarations (N),
6440 Handled_Statement_Sequence => Build_Accept_Body (N));
6442 -- Reset the Scope of local entities associated with the accept
6443 -- statement (that currently reference the entry scope) to the
6444 -- block scope, to avoid having references to the locals treated
6445 -- as up-level references.
6447 Reset_Scopes_To (Block, Blkent);
6449 -- For the analysis of the generated declarations, the parent node
6450 -- must be properly set.
6452 Set_Parent (Block, Parent (N));
6453 Set_Parent (Blkent, Block);
6455 -- Prepend call to Accept_Call to main statement sequence If the
6456 -- accept has exception handlers, the statement sequence is wrapped
6457 -- in a block. Insert call and renaming declarations in the
6458 -- declarations of the block, so they are elaborated before the
6459 -- handlers.
6461 Call :=
6462 Make_Procedure_Call_Statement (Loc,
6463 Name => New_Occurrence_Of (RTE (RE_Accept_Call), Loc),
6464 Parameter_Associations => New_List (
6465 Entry_Index_Expression (Loc, Entity (Ename), Eindx, Ttyp),
6466 New_Occurrence_Of (Ann, Loc)));
6468 if Parent (Stats) = N then
6469 Prepend (Call, Statements (Stats));
6470 else
6471 Set_Declarations (Parent (Stats), New_List (Call));
6472 end if;
6474 Analyze (Call);
6476 Push_Scope (Blkent);
6478 declare
6479 D : Node_Id;
6480 Next_D : Node_Id;
6481 Typ : Entity_Id;
6483 begin
6484 D := First (Declarations (N));
6485 while Present (D) loop
6486 Next_D := Next (D);
6488 if Nkind (D) = N_Object_Renaming_Declaration then
6490 -- The renaming declarations for the formals were created
6491 -- during analysis of the accept statement, and attached to
6492 -- the list of declarations. Place them now in the context
6493 -- of the accept block or subprogram.
6495 Remove (D);
6496 Typ := Entity (Subtype_Mark (D));
6497 Insert_After (Call, D);
6498 Analyze (D);
6500 -- If the formal is class_wide, it does not have an actual
6501 -- subtype. The analysis of the renaming declaration creates
6502 -- one, but we need to retain the class-wide nature of the
6503 -- entity.
6505 if Is_Class_Wide_Type (Typ) then
6506 Set_Etype (Defining_Identifier (D), Typ);
6507 end if;
6509 end if;
6511 D := Next_D;
6512 end loop;
6513 end;
6515 End_Scope;
6517 -- Replace the accept statement by the new block
6519 Rewrite (N, Block);
6520 Analyze (N);
6522 -- Last step is to unstack the Accept_Address value
6524 Remove_Last_Elmt (Acstack);
6525 end if;
6526 end Expand_N_Accept_Statement;
6528 ----------------------------------
6529 -- Expand_N_Asynchronous_Select --
6530 ----------------------------------
6532 -- This procedure assumes that the trigger statement is an entry call or
6533 -- a dispatching procedure call. A delay alternative should already have
6534 -- been expanded into an entry call to the appropriate delay object Wait
6535 -- entry.
6537 -- If the trigger is a task entry call, the select is implemented with
6538 -- a Task_Entry_Call:
6540 -- declare
6541 -- B : Boolean;
6542 -- C : Boolean;
6543 -- P : parms := (parm, parm, parm);
6545 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6547 -- procedure _clean is
6548 -- begin
6549 -- ...
6550 -- Cancel_Task_Entry_Call (C);
6551 -- ...
6552 -- end _clean;
6554 -- begin
6555 -- Abort_Defer;
6556 -- Task_Entry_Call
6557 -- (<acceptor-task>, -- Acceptor
6558 -- <entry-index>, -- E
6559 -- P'Address, -- Uninterpreted_Data
6560 -- Asynchronous_Call, -- Mode
6561 -- B); -- Rendezvous_Successful
6563 -- begin
6564 -- begin
6565 -- Abort_Undefer;
6566 -- <abortable-part>
6567 -- at end
6568 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6569 -- end;
6570 -- exception
6571 -- when Abort_Signal => Abort_Undefer;
6572 -- end;
6574 -- parm := P.param;
6575 -- parm := P.param;
6576 -- ...
6577 -- if not C then
6578 -- <triggered-statements>
6579 -- end if;
6580 -- end;
6582 -- Note that Build_Simple_Entry_Call is used to expand the entry of the
6583 -- asynchronous entry call (by Expand_N_Entry_Call_Statement procedure)
6584 -- as follows:
6586 -- declare
6587 -- P : parms := (parm, parm, parm);
6588 -- begin
6589 -- Call_Simple (acceptor-task, entry-index, P'Address);
6590 -- parm := P.param;
6591 -- parm := P.param;
6592 -- ...
6593 -- end;
6595 -- so the task at hand is to convert the latter expansion into the former
6597 -- If the trigger is a protected entry call, the select is implemented
6598 -- with Protected_Entry_Call:
6600 -- declare
6601 -- P : E1_Params := (param, param, param);
6602 -- Bnn : Communications_Block;
6604 -- begin
6605 -- declare
6607 -- -- Clean is added by Exp_Ch7.Expand_Cleanup_Actions
6609 -- procedure _clean is
6610 -- begin
6611 -- ...
6612 -- if Enqueued (Bnn) then
6613 -- Cancel_Protected_Entry_Call (Bnn);
6614 -- end if;
6615 -- ...
6616 -- end _clean;
6618 -- begin
6619 -- begin
6620 -- Protected_Entry_Call
6621 -- (po._object'Access, -- Object
6622 -- <entry index>, -- E
6623 -- P'Address, -- Uninterpreted_Data
6624 -- Asynchronous_Call, -- Mode
6625 -- Bnn); -- Block
6627 -- if Enqueued (Bnn) then
6628 -- <abortable-part>
6629 -- end if;
6630 -- at end
6631 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6632 -- end;
6633 -- exception
6634 -- when Abort_Signal => Abort_Undefer;
6635 -- end;
6637 -- if not Cancelled (Bnn) then
6638 -- <triggered-statements>
6639 -- end if;
6640 -- end;
6642 -- Build_Simple_Entry_Call is used to expand the all to a simple protected
6643 -- entry call:
6645 -- declare
6646 -- P : E1_Params := (param, param, param);
6647 -- Bnn : Communications_Block;
6649 -- begin
6650 -- Protected_Entry_Call
6651 -- (po._object'Access, -- Object
6652 -- <entry index>, -- E
6653 -- P'Address, -- Uninterpreted_Data
6654 -- Simple_Call, -- Mode
6655 -- Bnn); -- Block
6656 -- parm := P.param;
6657 -- parm := P.param;
6658 -- ...
6659 -- end;
6661 -- Ada 2005 (AI-345): If the trigger is a dispatching call, the select is
6662 -- expanded into:
6664 -- declare
6665 -- B : Boolean := False;
6666 -- Bnn : Communication_Block;
6667 -- C : Ada.Tags.Prim_Op_Kind;
6668 -- D : System.Storage_Elements.Dummy_Communication_Block;
6669 -- K : Ada.Tags.Tagged_Kind :=
6670 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6671 -- P : Parameters := (Param1 .. ParamN);
6672 -- S : Integer;
6673 -- U : Boolean;
6675 -- begin
6676 -- if K = Ada.Tags.TK_Limited_Tagged
6677 -- or else K = Ada.Tags.TK_Tagged
6678 -- then
6679 -- <dispatching-call>;
6680 -- <triggering-statements>;
6682 -- else
6683 -- S :=
6684 -- Ada.Tags.Get_Offset_Index
6685 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
6687 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
6689 -- if C = POK_Protected_Entry then
6690 -- declare
6691 -- procedure _clean is
6692 -- begin
6693 -- if Enqueued (Bnn) then
6694 -- Cancel_Protected_Entry_Call (Bnn);
6695 -- end if;
6696 -- end _clean;
6698 -- begin
6699 -- begin
6700 -- _Disp_Asynchronous_Select
6701 -- (<object>, S, P'Address, D, B);
6702 -- Bnn := Communication_Block (D);
6704 -- Param1 := P.Param1;
6705 -- ...
6706 -- ParamN := P.ParamN;
6708 -- if Enqueued (Bnn) then
6709 -- <abortable-statements>
6710 -- end if;
6711 -- at end
6712 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6713 -- end;
6714 -- exception
6715 -- when Abort_Signal => Abort_Undefer;
6716 -- end;
6718 -- if not Cancelled (Bnn) then
6719 -- <triggering-statements>
6720 -- end if;
6722 -- elsif C = POK_Task_Entry then
6723 -- declare
6724 -- procedure _clean is
6725 -- begin
6726 -- Cancel_Task_Entry_Call (U);
6727 -- end _clean;
6729 -- begin
6730 -- Abort_Defer;
6732 -- _Disp_Asynchronous_Select
6733 -- (<object>, S, P'Address, D, B);
6734 -- Bnn := Communication_Bloc (D);
6736 -- Param1 := P.Param1;
6737 -- ...
6738 -- ParamN := P.ParamN;
6740 -- begin
6741 -- begin
6742 -- Abort_Undefer;
6743 -- <abortable-statements>
6744 -- at end
6745 -- _clean; -- Added by Exp_Ch7.Expand_Cleanup_Actions
6746 -- end;
6747 -- exception
6748 -- when Abort_Signal => Abort_Undefer;
6749 -- end;
6751 -- if not U then
6752 -- <triggering-statements>
6753 -- end if;
6754 -- end;
6756 -- else
6757 -- <dispatching-call>;
6758 -- <triggering-statements>
6759 -- end if;
6760 -- end if;
6761 -- end;
6763 -- The job is to convert this to the asynchronous form
6765 -- If the trigger is a delay statement, it will have been expanded into
6766 -- a call to one of the GNARL delay procedures. This routine will convert
6767 -- this into a protected entry call on a delay object and then continue
6768 -- processing as for a protected entry call trigger. This requires
6769 -- declaring a Delay_Block object and adding a pointer to this object to
6770 -- the parameter list of the delay procedure to form the parameter list of
6771 -- the entry call. This object is used by the runtime to queue the delay
6772 -- request.
6774 -- For a description of the use of P and the assignments after the call,
6775 -- see Expand_N_Entry_Call_Statement.
6777 procedure Expand_N_Asynchronous_Select (N : Node_Id) is
6778 Loc : constant Source_Ptr := Sloc (N);
6779 Abrt : constant Node_Id := Abortable_Part (N);
6780 Trig : constant Node_Id := Triggering_Alternative (N);
6782 Abort_Block_Ent : Entity_Id;
6783 Abortable_Block : Node_Id;
6784 Actuals : List_Id;
6785 Astats : List_Id;
6786 Blk_Ent : constant Entity_Id := Make_Temporary (Loc, 'A');
6787 Blk_Typ : Entity_Id;
6788 Call : Node_Id;
6789 Call_Ent : Entity_Id;
6790 Cancel_Param : Entity_Id;
6791 Cleanup_Block : Node_Id;
6792 Cleanup_Block_Ent : Entity_Id;
6793 Cleanup_Stmts : List_Id;
6794 Conc_Typ_Stmts : List_Id;
6795 Concval : Node_Id;
6796 Dblock_Ent : Entity_Id;
6797 Decl : Node_Id;
6798 Decls : List_Id;
6799 Ecall : Node_Id;
6800 Ename : Node_Id;
6801 Enqueue_Call : Node_Id;
6802 Formals : List_Id;
6803 Hdle : List_Id;
6804 Index : Node_Id;
6805 Lim_Typ_Stmts : List_Id;
6806 N_Orig : Node_Id;
6807 Obj : Entity_Id;
6808 Param : Node_Id;
6809 Params : List_Id;
6810 Pdef : Entity_Id;
6811 ProtE_Stmts : List_Id;
6812 ProtP_Stmts : List_Id;
6813 Stmt : Node_Id;
6814 Stmts : List_Id;
6815 TaskE_Stmts : List_Id;
6816 Tstats : List_Id;
6818 B : Entity_Id; -- Call status flag
6819 Bnn : Entity_Id; -- Communication block
6820 C : Entity_Id; -- Call kind
6821 K : Entity_Id; -- Tagged kind
6822 P : Entity_Id; -- Parameter block
6823 S : Entity_Id; -- Primitive operation slot
6824 T : Entity_Id; -- Additional status flag
6826 procedure Rewrite_Abortable_Part;
6827 -- If the trigger is a dispatching call, the expansion inserts multiple
6828 -- copies of the abortable part. This is both inefficient, and may lead
6829 -- to duplicate definitions that the back-end will reject, when the
6830 -- abortable part includes loops. This procedure rewrites the abortable
6831 -- part into a call to a generated procedure.
6833 ----------------------------
6834 -- Rewrite_Abortable_Part --
6835 ----------------------------
6837 procedure Rewrite_Abortable_Part is
6838 Proc : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
6839 Decl : Node_Id;
6841 begin
6842 Decl :=
6843 Make_Subprogram_Body (Loc,
6844 Specification =>
6845 Make_Procedure_Specification (Loc, Defining_Unit_Name => Proc),
6846 Declarations => New_List,
6847 Handled_Statement_Sequence =>
6848 Make_Handled_Sequence_Of_Statements (Loc, Astats));
6849 Insert_Before (N, Decl);
6850 Analyze (Decl);
6852 -- Rewrite abortable part into a call to this procedure
6854 Astats :=
6855 New_List (
6856 Make_Procedure_Call_Statement (Loc,
6857 Name => New_Occurrence_Of (Proc, Loc)));
6858 end Rewrite_Abortable_Part;
6860 -- Start of processing for Expand_N_Asynchronous_Select
6862 begin
6863 -- Asynchronous select is not supported on restricted runtimes. Don't
6864 -- try to expand.
6866 if Restricted_Profile then
6867 return;
6868 end if;
6870 Process_Statements_For_Controlled_Objects (Trig);
6871 Process_Statements_For_Controlled_Objects (Abrt);
6873 Ecall := Triggering_Statement (Trig);
6875 Ensure_Statement_Present (Sloc (Ecall), Trig);
6877 -- Retrieve Astats and Tstats now because the finalization machinery may
6878 -- wrap them in blocks.
6880 Astats := Statements (Abrt);
6881 Tstats := Statements (Trig);
6883 -- The arguments in the call may require dynamic allocation, and the
6884 -- call statement may have been transformed into a block. The block
6885 -- may contain additional declarations for internal entities, and the
6886 -- original call is found by sequential search.
6888 if Nkind (Ecall) = N_Block_Statement then
6889 Ecall := First (Statements (Handled_Statement_Sequence (Ecall)));
6890 while Nkind (Ecall) not in
6891 N_Procedure_Call_Statement | N_Entry_Call_Statement
6892 loop
6893 Next (Ecall);
6894 end loop;
6895 end if;
6897 -- This is either a dispatching call or a delay statement used as a
6898 -- trigger which was expanded into a procedure call.
6900 if Nkind (Ecall) = N_Procedure_Call_Statement then
6901 if Ada_Version >= Ada_2005
6902 and then
6903 (No (Original_Node (Ecall))
6904 or else Nkind (Original_Node (Ecall)) not in N_Delay_Statement)
6905 then
6906 Extract_Dispatching_Call (Ecall, Call_Ent, Obj, Actuals, Formals);
6908 Rewrite_Abortable_Part;
6909 Decls := New_List;
6910 Stmts := New_List;
6912 -- Call status flag processing, generate:
6913 -- B : Boolean := False;
6915 B := Build_B (Loc, Decls);
6917 -- Communication block processing, generate:
6918 -- Bnn : Communication_Block;
6920 Bnn := Make_Temporary (Loc, 'B');
6921 Append_To (Decls,
6922 Make_Object_Declaration (Loc,
6923 Defining_Identifier => Bnn,
6924 Object_Definition =>
6925 New_Occurrence_Of (RTE (RE_Communication_Block), Loc)));
6927 -- Call kind processing, generate:
6928 -- C : Ada.Tags.Prim_Op_Kind;
6930 C := Build_C (Loc, Decls);
6932 -- Tagged kind processing, generate:
6933 -- K : Ada.Tags.Tagged_Kind :=
6934 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
6936 -- Dummy communication block, generate:
6937 -- D : Dummy_Communication_Block;
6939 Append_To (Decls,
6940 Make_Object_Declaration (Loc,
6941 Defining_Identifier =>
6942 Make_Defining_Identifier (Loc, Name_uD),
6943 Object_Definition =>
6944 New_Occurrence_Of
6945 (RTE (RE_Dummy_Communication_Block), Loc)));
6947 K := Build_K (Loc, Decls, Obj);
6949 -- Parameter block processing
6951 Blk_Typ := Build_Parameter_Block
6952 (Loc, Actuals, Formals, Decls);
6953 P := Parameter_Block_Pack
6954 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
6956 -- Dispatch table slot processing, generate:
6957 -- S : Integer;
6959 S := Build_S (Loc, Decls);
6961 -- Additional status flag processing, generate:
6962 -- Tnn : Boolean;
6964 T := Make_Temporary (Loc, 'T');
6965 Append_To (Decls,
6966 Make_Object_Declaration (Loc,
6967 Defining_Identifier => T,
6968 Object_Definition =>
6969 New_Occurrence_Of (Standard_Boolean, Loc)));
6971 ------------------------------
6972 -- Protected entry handling --
6973 ------------------------------
6975 -- Generate:
6976 -- Param1 := P.Param1;
6977 -- ...
6978 -- ParamN := P.ParamN;
6980 Cleanup_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
6982 -- Generate:
6983 -- Bnn := Communication_Block (D);
6985 Prepend_To (Cleanup_Stmts,
6986 Make_Assignment_Statement (Loc,
6987 Name => New_Occurrence_Of (Bnn, Loc),
6988 Expression =>
6989 Unchecked_Convert_To
6990 (RTE (RE_Communication_Block),
6991 Make_Identifier (Loc, Name_uD))));
6993 -- Generate:
6994 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
6996 Prepend_To (Cleanup_Stmts,
6997 Make_Procedure_Call_Statement (Loc,
6998 Name =>
6999 New_Occurrence_Of
7000 (Find_Prim_Op
7001 (Etype (Etype (Obj)), Name_uDisp_Asynchronous_Select),
7002 Loc),
7003 Parameter_Associations =>
7004 New_List (
7005 New_Copy_Tree (Obj), -- <object>
7006 New_Occurrence_Of (S, Loc), -- S
7007 Make_Attribute_Reference (Loc, -- P'Address
7008 Prefix => New_Occurrence_Of (P, Loc),
7009 Attribute_Name => Name_Address),
7010 Make_Identifier (Loc, Name_uD), -- D
7011 New_Occurrence_Of (B, Loc)))); -- B
7013 -- Generate:
7014 -- if Enqueued (Bnn) then
7015 -- <abortable-statements>
7016 -- end if;
7018 Append_To (Cleanup_Stmts,
7019 Make_Implicit_If_Statement (N,
7020 Condition =>
7021 Make_Function_Call (Loc,
7022 Name =>
7023 New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7024 Parameter_Associations =>
7025 New_List (New_Occurrence_Of (Bnn, Loc))),
7027 Then_Statements =>
7028 New_Copy_List_Tree (Astats)));
7030 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7031 -- will then generate a _clean for the communication block Bnn.
7033 -- Generate:
7034 -- declare
7035 -- procedure _clean is
7036 -- begin
7037 -- if Enqueued (Bnn) then
7038 -- Cancel_Protected_Entry_Call (Bnn);
7039 -- end if;
7040 -- end _clean;
7041 -- begin
7042 -- Cleanup_Stmts
7043 -- at end
7044 -- _clean;
7045 -- end;
7047 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7048 Cleanup_Block :=
7049 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, Bnn);
7051 -- Wrap the cleanup block in an exception handling block
7053 -- Generate:
7054 -- begin
7055 -- Cleanup_Block
7056 -- exception
7057 -- when Abort_Signal => Abort_Undefer;
7058 -- end;
7060 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7061 ProtE_Stmts :=
7062 New_List (
7063 Make_Implicit_Label_Declaration (Loc,
7064 Defining_Identifier => Abort_Block_Ent),
7066 Build_Abort_Block
7067 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7069 -- Generate:
7070 -- if not Cancelled (Bnn) then
7071 -- <triggering-statements>
7072 -- end if;
7074 Append_To (ProtE_Stmts,
7075 Make_Implicit_If_Statement (N,
7076 Condition =>
7077 Make_Op_Not (Loc,
7078 Right_Opnd =>
7079 Make_Function_Call (Loc,
7080 Name =>
7081 New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7082 Parameter_Associations =>
7083 New_List (New_Occurrence_Of (Bnn, Loc)))),
7085 Then_Statements =>
7086 New_Copy_List_Tree (Tstats)));
7088 -------------------------
7089 -- Task entry handling --
7090 -------------------------
7092 -- Generate:
7093 -- Param1 := P.Param1;
7094 -- ...
7095 -- ParamN := P.ParamN;
7097 TaskE_Stmts := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7099 -- Generate:
7100 -- Bnn := Communication_Block (D);
7102 Append_To (TaskE_Stmts,
7103 Make_Assignment_Statement (Loc,
7104 Name =>
7105 New_Occurrence_Of (Bnn, Loc),
7106 Expression =>
7107 Unchecked_Convert_To
7108 (RTE (RE_Communication_Block),
7109 Make_Identifier (Loc, Name_uD))));
7111 -- Generate:
7112 -- _Disp_Asynchronous_Select (<object>, S, P'Address, D, B);
7114 Prepend_To (TaskE_Stmts,
7115 Make_Procedure_Call_Statement (Loc,
7116 Name =>
7117 New_Occurrence_Of (
7118 Find_Prim_Op (Etype (Etype (Obj)),
7119 Name_uDisp_Asynchronous_Select),
7120 Loc),
7122 Parameter_Associations => New_List (
7123 New_Copy_Tree (Obj), -- <object>
7124 New_Occurrence_Of (S, Loc), -- S
7125 Make_Attribute_Reference (Loc, -- P'Address
7126 Prefix => New_Occurrence_Of (P, Loc),
7127 Attribute_Name => Name_Address),
7128 Make_Identifier (Loc, Name_uD), -- D
7129 New_Occurrence_Of (B, Loc)))); -- B
7131 -- Generate:
7132 -- Abort_Defer;
7134 Prepend_To (TaskE_Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7136 -- Generate:
7137 -- Abort_Undefer;
7138 -- <abortable-statements>
7140 Cleanup_Stmts := New_Copy_List_Tree (Astats);
7142 Prepend_To
7143 (Cleanup_Stmts, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7145 -- Wrap the statements in a block. Exp_Ch7.Expand_Cleanup_Actions
7146 -- will generate a _clean for the additional status flag.
7148 -- Generate:
7149 -- declare
7150 -- procedure _clean is
7151 -- begin
7152 -- Cancel_Task_Entry_Call (U);
7153 -- end _clean;
7154 -- begin
7155 -- Cleanup_Stmts
7156 -- at end
7157 -- _clean;
7158 -- end;
7160 Cleanup_Block_Ent := Make_Temporary (Loc, 'C');
7161 Cleanup_Block :=
7162 Build_Cleanup_Block (Loc, Cleanup_Block_Ent, Cleanup_Stmts, T);
7164 -- Wrap the cleanup block in an exception handling block
7166 -- Generate:
7167 -- begin
7168 -- Cleanup_Block
7169 -- exception
7170 -- when Abort_Signal => Abort_Undefer;
7171 -- end;
7173 Abort_Block_Ent := Make_Temporary (Loc, 'A');
7175 Append_To (TaskE_Stmts,
7176 Make_Implicit_Label_Declaration (Loc,
7177 Defining_Identifier => Abort_Block_Ent));
7179 Append_To (TaskE_Stmts,
7180 Build_Abort_Block
7181 (Loc, Abort_Block_Ent, Cleanup_Block_Ent, Cleanup_Block));
7183 -- Generate:
7184 -- if not T then
7185 -- <triggering-statements>
7186 -- end if;
7188 Append_To (TaskE_Stmts,
7189 Make_Implicit_If_Statement (N,
7190 Condition =>
7191 Make_Op_Not (Loc, Right_Opnd => New_Occurrence_Of (T, Loc)),
7193 Then_Statements =>
7194 New_Copy_List_Tree (Tstats)));
7196 ----------------------------------
7197 -- Protected procedure handling --
7198 ----------------------------------
7200 -- Generate:
7201 -- <dispatching-call>;
7202 -- <triggering-statements>
7204 ProtP_Stmts := New_Copy_List_Tree (Tstats);
7205 Prepend_To (ProtP_Stmts, New_Copy_Tree (Ecall));
7207 -- Generate:
7208 -- S := Ada.Tags.Get_Offset_Index
7209 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7211 Conc_Typ_Stmts :=
7212 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7214 -- Generate:
7215 -- _Disp_Get_Prim_Op_Kind (<object>, S, C);
7217 Append_To (Conc_Typ_Stmts,
7218 Make_Procedure_Call_Statement (Loc,
7219 Name =>
7220 New_Occurrence_Of
7221 (Find_Prim_Op (Etype (Etype (Obj)),
7222 Name_uDisp_Get_Prim_Op_Kind),
7223 Loc),
7224 Parameter_Associations =>
7225 New_List (
7226 New_Copy_Tree (Obj),
7227 New_Occurrence_Of (S, Loc),
7228 New_Occurrence_Of (C, Loc))));
7230 -- Generate:
7231 -- if C = POK_Procedure_Entry then
7232 -- ProtE_Stmts
7233 -- elsif C = POK_Task_Entry then
7234 -- TaskE_Stmts
7235 -- else
7236 -- ProtP_Stmts
7237 -- end if;
7239 Append_To (Conc_Typ_Stmts,
7240 Make_Implicit_If_Statement (N,
7241 Condition =>
7242 Make_Op_Eq (Loc,
7243 Left_Opnd =>
7244 New_Occurrence_Of (C, Loc),
7245 Right_Opnd =>
7246 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
7248 Then_Statements =>
7249 ProtE_Stmts,
7251 Elsif_Parts =>
7252 New_List (
7253 Make_Elsif_Part (Loc,
7254 Condition =>
7255 Make_Op_Eq (Loc,
7256 Left_Opnd =>
7257 New_Occurrence_Of (C, Loc),
7258 Right_Opnd =>
7259 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc)),
7261 Then_Statements =>
7262 TaskE_Stmts)),
7264 Else_Statements =>
7265 ProtP_Stmts));
7267 -- Generate:
7268 -- <dispatching-call>;
7269 -- <triggering-statements>
7271 Lim_Typ_Stmts := New_Copy_List_Tree (Tstats);
7272 Prepend_To (Lim_Typ_Stmts, New_Copy_Tree (Ecall));
7274 -- Generate:
7275 -- if K = Ada.Tags.TK_Limited_Tagged
7276 -- or else K = Ada.Tags.TK_Tagged
7277 -- then
7278 -- Lim_Typ_Stmts
7279 -- else
7280 -- Conc_Typ_Stmts
7281 -- end if;
7283 Append_To (Stmts,
7284 Make_Implicit_If_Statement (N,
7285 Condition => Build_Dispatching_Tag_Check (K, N),
7286 Then_Statements => Lim_Typ_Stmts,
7287 Else_Statements => Conc_Typ_Stmts));
7289 Rewrite (N,
7290 Make_Block_Statement (Loc,
7291 Declarations =>
7292 Decls,
7293 Handled_Statement_Sequence =>
7294 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7296 Analyze (N);
7297 return;
7299 -- Delay triggering statement processing
7301 else
7302 -- Add a Delay_Block object to the parameter list of the delay
7303 -- procedure to form the parameter list of the Wait entry call.
7305 Dblock_Ent := Make_Temporary (Loc, 'D');
7307 Pdef := Entity (Name (Ecall));
7309 if Is_RTE (Pdef, RO_CA_Delay_For) then
7310 Enqueue_Call :=
7311 New_Occurrence_Of (RTE (RE_Enqueue_Duration), Loc);
7313 elsif Is_RTE (Pdef, RO_CA_Delay_Until) then
7314 Enqueue_Call :=
7315 New_Occurrence_Of (RTE (RE_Enqueue_Calendar), Loc);
7317 else pragma Assert (Is_RTE (Pdef, RO_RT_Delay_Until));
7318 Enqueue_Call := New_Occurrence_Of (RTE (RE_Enqueue_RT), Loc);
7319 end if;
7321 Append_To (Parameter_Associations (Ecall),
7322 Make_Attribute_Reference (Loc,
7323 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7324 Attribute_Name => Name_Unchecked_Access));
7326 -- Create the inner block to protect the abortable part
7328 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7330 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7332 Abortable_Block :=
7333 Make_Block_Statement (Loc,
7334 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7335 Handled_Statement_Sequence =>
7336 Make_Handled_Sequence_Of_Statements (Loc,
7337 Statements => Astats),
7338 Has_Created_Identifier => True,
7339 Is_Asynchronous_Call_Block => True);
7341 -- Append call to if Enqueue (When, DB'Unchecked_Access) then
7343 Rewrite (Ecall,
7344 Make_Implicit_If_Statement (N,
7345 Condition =>
7346 Make_Function_Call (Loc,
7347 Name => Enqueue_Call,
7348 Parameter_Associations => Parameter_Associations (Ecall)),
7349 Then_Statements =>
7350 New_List (Make_Block_Statement (Loc,
7351 Handled_Statement_Sequence =>
7352 Make_Handled_Sequence_Of_Statements (Loc,
7353 Statements => New_List (
7354 Make_Implicit_Label_Declaration (Loc,
7355 Defining_Identifier => Blk_Ent,
7356 Label_Construct => Abortable_Block),
7357 Abortable_Block),
7358 Exception_Handlers => Hdle)))));
7360 Stmts := New_List (Ecall);
7362 -- Construct statement sequence for new block
7364 Append_To (Stmts,
7365 Make_Implicit_If_Statement (N,
7366 Condition =>
7367 Make_Function_Call (Loc,
7368 Name => New_Occurrence_Of (
7369 RTE (RE_Timed_Out), Loc),
7370 Parameter_Associations => New_List (
7371 Make_Attribute_Reference (Loc,
7372 Prefix => New_Occurrence_Of (Dblock_Ent, Loc),
7373 Attribute_Name => Name_Unchecked_Access))),
7374 Then_Statements => Tstats));
7376 -- The result is the new block
7378 Set_Entry_Cancel_Parameter (Blk_Ent, Dblock_Ent);
7380 Rewrite (N,
7381 Make_Block_Statement (Loc,
7382 Declarations => New_List (
7383 Make_Object_Declaration (Loc,
7384 Defining_Identifier => Dblock_Ent,
7385 Aliased_Present => True,
7386 Object_Definition =>
7387 New_Occurrence_Of (RTE (RE_Delay_Block), Loc))),
7389 Handled_Statement_Sequence =>
7390 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7392 Analyze (N);
7393 return;
7394 end if;
7396 else
7397 N_Orig := N;
7398 end if;
7400 Extract_Entry (Ecall, Concval, Ename, Index);
7401 Build_Simple_Entry_Call (Ecall, Concval, Ename, Index);
7403 Stmts := Statements (Handled_Statement_Sequence (Ecall));
7404 Decls := Declarations (Ecall);
7406 if Is_Protected_Type (Etype (Concval)) then
7408 -- Get the declarations of the block expanded from the entry call
7410 Decl := First (Decls);
7411 while Present (Decl)
7412 and then (Nkind (Decl) /= N_Object_Declaration
7413 or else not Is_RTE (Etype (Object_Definition (Decl)),
7414 RE_Communication_Block))
7415 loop
7416 Next (Decl);
7417 end loop;
7419 pragma Assert (Present (Decl));
7420 Cancel_Param := Defining_Identifier (Decl);
7422 -- Change the mode of the Protected_Entry_Call call
7424 -- Protected_Entry_Call (
7425 -- Object => po._object'Access,
7426 -- E => <entry index>;
7427 -- Uninterpreted_Data => P'Address;
7428 -- Mode => Asynchronous_Call;
7429 -- Block => Bnn);
7431 -- Skip assignments to temporaries created for in-out parameters
7433 -- This makes unwarranted assumptions about the shape of the expanded
7434 -- tree for the call, and should be cleaned up ???
7436 Stmt := First (Stmts);
7437 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7438 Next (Stmt);
7439 end loop;
7441 Call := Stmt;
7443 Param := First (Parameter_Associations (Call));
7444 while Present (Param)
7445 and then not Is_RTE (Etype (Param), RE_Call_Modes)
7446 loop
7447 Next (Param);
7448 end loop;
7450 pragma Assert (Present (Param));
7451 Rewrite (Param, New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7452 Analyze (Param);
7454 -- Append an if statement to execute the abortable part
7456 -- Generate:
7457 -- if Enqueued (Bnn) then
7459 Append_To (Stmts,
7460 Make_Implicit_If_Statement (N,
7461 Condition =>
7462 Make_Function_Call (Loc,
7463 Name => New_Occurrence_Of (RTE (RE_Enqueued), Loc),
7464 Parameter_Associations => New_List (
7465 New_Occurrence_Of (Cancel_Param, Loc))),
7466 Then_Statements => Astats));
7468 Abortable_Block :=
7469 Make_Block_Statement (Loc,
7470 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7471 Handled_Statement_Sequence =>
7472 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stmts),
7473 Has_Created_Identifier => True,
7474 Is_Asynchronous_Call_Block => True);
7476 Stmts := New_List (
7477 Make_Block_Statement (Loc,
7478 Handled_Statement_Sequence =>
7479 Make_Handled_Sequence_Of_Statements (Loc,
7480 Statements => New_List (
7481 Make_Implicit_Label_Declaration (Loc,
7482 Defining_Identifier => Blk_Ent,
7483 Label_Construct => Abortable_Block),
7484 Abortable_Block),
7486 -- exception
7488 Exception_Handlers => New_List (
7489 Make_Implicit_Exception_Handler (Loc,
7491 -- when Abort_Signal =>
7492 -- null;
7494 Exception_Choices =>
7495 New_List (New_Occurrence_Of (Stand.Abort_Signal, Loc)),
7496 Statements => New_List (Make_Null_Statement (Loc)))))),
7498 -- if not Cancelled (Bnn) then
7499 -- triggered statements
7500 -- end if;
7502 Make_Implicit_If_Statement (N,
7503 Condition => Make_Op_Not (Loc,
7504 Right_Opnd =>
7505 Make_Function_Call (Loc,
7506 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
7507 Parameter_Associations => New_List (
7508 New_Occurrence_Of (Cancel_Param, Loc)))),
7509 Then_Statements => Tstats));
7511 -- Asynchronous task entry call
7513 else
7514 if No (Decls) then
7515 Decls := New_List;
7516 end if;
7518 B := Make_Defining_Identifier (Loc, Name_uB);
7520 -- Insert declaration of B in declarations of existing block
7522 Prepend_To (Decls,
7523 Make_Object_Declaration (Loc,
7524 Defining_Identifier => B,
7525 Object_Definition =>
7526 New_Occurrence_Of (Standard_Boolean, Loc)));
7528 Cancel_Param := Make_Defining_Identifier (Loc, Name_uC);
7530 -- Insert the declaration of C in the declarations of the existing
7531 -- block. The variable is initialized to something (True or False,
7532 -- does not matter) to prevent CodePeer from complaining about a
7533 -- possible read of an uninitialized variable.
7535 Prepend_To (Decls,
7536 Make_Object_Declaration (Loc,
7537 Defining_Identifier => Cancel_Param,
7538 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
7539 Expression => New_Occurrence_Of (Standard_False, Loc),
7540 Has_Init_Expression => True));
7542 -- Remove and save the call to Call_Simple
7544 Stmt := First (Stmts);
7546 -- Skip assignments to temporaries created for in-out parameters.
7547 -- This makes unwarranted assumptions about the shape of the expanded
7548 -- tree for the call, and should be cleaned up ???
7550 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7551 Next (Stmt);
7552 end loop;
7554 Call := Stmt;
7556 -- Create the inner block to protect the abortable part
7558 Hdle := New_List (Build_Abort_Block_Handler (Loc));
7560 if Abort_Allowed then
7561 Prepend_To (Astats, Build_Runtime_Call (Loc, RE_Abort_Undefer));
7562 end if;
7564 Abortable_Block :=
7565 Make_Block_Statement (Loc,
7566 Identifier => New_Occurrence_Of (Blk_Ent, Loc),
7567 Handled_Statement_Sequence =>
7568 Make_Handled_Sequence_Of_Statements (Loc, Statements => Astats),
7569 Has_Created_Identifier => True,
7570 Is_Asynchronous_Call_Block => True);
7572 Insert_After (Call,
7573 Make_Block_Statement (Loc,
7574 Handled_Statement_Sequence =>
7575 Make_Handled_Sequence_Of_Statements (Loc,
7576 Statements => New_List (
7577 Make_Implicit_Label_Declaration (Loc,
7578 Defining_Identifier => Blk_Ent,
7579 Label_Construct => Abortable_Block),
7580 Abortable_Block),
7581 Exception_Handlers => Hdle)));
7583 -- Create new call statement
7585 Params := Parameter_Associations (Call);
7587 Append_To (Params,
7588 New_Occurrence_Of (RTE (RE_Asynchronous_Call), Loc));
7589 Append_To (Params, New_Occurrence_Of (B, Loc));
7591 Rewrite (Call,
7592 Make_Procedure_Call_Statement (Loc,
7593 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
7594 Parameter_Associations => Params));
7596 -- Construct statement sequence for new block
7598 Append_To (Stmts,
7599 Make_Implicit_If_Statement (N,
7600 Condition =>
7601 Make_Op_Not (Loc, New_Occurrence_Of (Cancel_Param, Loc)),
7602 Then_Statements => Tstats));
7604 -- Protected the call against abort
7606 Prepend_To (Stmts, Build_Runtime_Call (Loc, RE_Abort_Defer));
7607 end if;
7609 Set_Entry_Cancel_Parameter (Blk_Ent, Cancel_Param);
7611 -- The result is the new block
7613 Rewrite (N_Orig,
7614 Make_Block_Statement (Loc,
7615 Declarations => Decls,
7616 Handled_Statement_Sequence =>
7617 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7619 Analyze (N_Orig);
7620 end Expand_N_Asynchronous_Select;
7622 -------------------------------------
7623 -- Expand_N_Conditional_Entry_Call --
7624 -------------------------------------
7626 -- The conditional task entry call is converted to a call to
7627 -- Task_Entry_Call:
7629 -- declare
7630 -- B : Boolean;
7631 -- P : parms := (parm, parm, parm);
7633 -- begin
7634 -- Task_Entry_Call
7635 -- (<acceptor-task>, -- Acceptor
7636 -- <entry-index>, -- E
7637 -- P'Address, -- Uninterpreted_Data
7638 -- Conditional_Call, -- Mode
7639 -- B); -- Rendezvous_Successful
7640 -- parm := P.param;
7641 -- parm := P.param;
7642 -- ...
7643 -- if B then
7644 -- normal-statements
7645 -- else
7646 -- else-statements
7647 -- end if;
7648 -- end;
7650 -- For a description of the use of P and the assignments after the call,
7651 -- see Expand_N_Entry_Call_Statement. Note that the entry call of the
7652 -- conditional entry call has already been expanded (by the Expand_N_Entry
7653 -- _Call_Statement procedure) as follows:
7655 -- declare
7656 -- P : parms := (parm, parm, parm);
7657 -- begin
7658 -- ... info for in-out parameters
7659 -- Call_Simple (acceptor-task, entry-index, P'Address);
7660 -- parm := P.param;
7661 -- parm := P.param;
7662 -- ...
7663 -- end;
7665 -- so the task at hand is to convert the latter expansion into the former
7667 -- The conditional protected entry call is converted to a call to
7668 -- Protected_Entry_Call:
7670 -- declare
7671 -- P : parms := (parm, parm, parm);
7672 -- Bnn : Communications_Block;
7674 -- begin
7675 -- Protected_Entry_Call
7676 -- (po._object'Access, -- Object
7677 -- <entry index>, -- E
7678 -- P'Address, -- Uninterpreted_Data
7679 -- Conditional_Call, -- Mode
7680 -- Bnn); -- Block
7681 -- parm := P.param;
7682 -- parm := P.param;
7683 -- ...
7684 -- if Cancelled (Bnn) then
7685 -- else-statements
7686 -- else
7687 -- normal-statements
7688 -- end if;
7689 -- end;
7691 -- Ada 2005 (AI-345): A dispatching conditional entry call is converted
7692 -- into:
7694 -- declare
7695 -- B : Boolean := False;
7696 -- C : Ada.Tags.Prim_Op_Kind;
7697 -- K : Ada.Tags.Tagged_Kind :=
7698 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7699 -- P : Parameters := (Param1 .. ParamN);
7700 -- S : Integer;
7702 -- begin
7703 -- if K = Ada.Tags.TK_Limited_Tagged
7704 -- or else K = Ada.Tags.TK_Tagged
7705 -- then
7706 -- <dispatching-call>;
7707 -- -- <triggering-statements> (code factorized after if-stmt)
7709 -- else
7710 -- S :=
7711 -- Ada.Tags.Get_Offset_Index
7712 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
7714 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7716 -- if C = POK_Protected_Entry
7717 -- or else C = POK_Task_Entry
7718 -- then
7719 -- Param1 := P.Param1;
7720 -- ...
7721 -- ParamN := P.ParamN;
7722 -- end if;
7724 -- if B then
7725 -- if C = POK_Procedure
7726 -- or else C = POK_Protected_Procedure
7727 -- or else C = POK_Task_Procedure
7728 -- then
7729 -- <dispatching-call>;
7730 -- end if;
7732 -- -- <triggering-statements> (code factorized after if-stmt)
7733 -- else
7734 -- <else-statements>
7735 -- goto L0; -- skip triggering statements
7736 -- end if;
7737 -- end if;
7738 -- <triggering-statements>
7739 -- L0:
7740 -- end;
7742 procedure Expand_N_Conditional_Entry_Call (N : Node_Id) is
7743 Loc : constant Source_Ptr := Sloc (N);
7744 Alt : constant Node_Id := Entry_Call_Alternative (N);
7745 Blk : Node_Id := Entry_Call_Statement (Alt);
7747 Actuals : List_Id;
7748 Blk_Typ : Entity_Id;
7749 Call : Node_Id;
7750 Call_Ent : Entity_Id;
7751 Conc_Typ_Stmts : List_Id;
7752 Decl : Node_Id;
7753 Decls : List_Id;
7754 Formals : List_Id;
7755 Label : Node_Id;
7756 Label_Id : Entity_Id := Empty;
7757 Lim_Typ_Stmts : List_Id;
7758 N_Stats : List_Id;
7759 Obj : Entity_Id;
7760 Param : Node_Id;
7761 Params : List_Id;
7762 Stmt : Node_Id;
7763 Stmts : List_Id;
7764 Transient_Blk : Node_Id;
7765 Unpack : List_Id;
7767 B : Entity_Id; -- Call status flag
7768 C : Entity_Id; -- Call kind
7769 K : Entity_Id; -- Tagged kind
7770 P : Entity_Id; -- Parameter block
7771 S : Entity_Id; -- Primitive operation slot
7773 begin
7774 Process_Statements_For_Controlled_Objects (N);
7776 if Ada_Version >= Ada_2005
7777 and then Nkind (Blk) = N_Procedure_Call_Statement
7778 then
7779 Extract_Dispatching_Call (Blk, Call_Ent, Obj, Actuals, Formals);
7781 Decls := New_List;
7782 Stmts := New_List;
7784 -- Call status flag processing, generate:
7785 -- B : Boolean := False;
7787 B := Build_B (Loc, Decls);
7789 -- Call kind processing, generate:
7790 -- C : Ada.Tags.Prim_Op_Kind;
7792 C := Build_C (Loc, Decls);
7794 -- Tagged kind processing, generate:
7795 -- K : Ada.Tags.Tagged_Kind :=
7796 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
7798 K := Build_K (Loc, Decls, Obj);
7800 -- Parameter block processing
7802 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
7803 P := Parameter_Block_Pack
7804 (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
7806 -- Dispatch table slot processing, generate:
7807 -- S : Integer;
7809 S := Build_S (Loc, Decls);
7811 -- Generate:
7812 -- S := Ada.Tags.Get_Offset_Index
7813 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
7815 Conc_Typ_Stmts :=
7816 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
7818 -- Generate:
7819 -- _Disp_Conditional_Select (<object>, S, P'Address, C, B);
7821 Append_To (Conc_Typ_Stmts,
7822 Make_Procedure_Call_Statement (Loc,
7823 Name =>
7824 New_Occurrence_Of (
7825 Find_Prim_Op (Etype (Etype (Obj)),
7826 Name_uDisp_Conditional_Select),
7827 Loc),
7828 Parameter_Associations =>
7829 New_List (
7830 New_Copy_Tree (Obj), -- <object>
7831 New_Occurrence_Of (S, Loc), -- S
7832 Make_Attribute_Reference (Loc, -- P'Address
7833 Prefix => New_Occurrence_Of (P, Loc),
7834 Attribute_Name => Name_Address),
7835 New_Occurrence_Of (C, Loc), -- C
7836 New_Occurrence_Of (B, Loc)))); -- B
7838 -- Generate:
7839 -- if C = POK_Protected_Entry
7840 -- or else C = POK_Task_Entry
7841 -- then
7842 -- Param1 := P.Param1;
7843 -- ...
7844 -- ParamN := P.ParamN;
7845 -- end if;
7847 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
7849 -- Generate the if statement only when the packed parameters need
7850 -- explicit assignments to their corresponding actuals.
7852 if Present (Unpack) then
7853 Append_To (Conc_Typ_Stmts,
7854 Make_Implicit_If_Statement (N,
7855 Condition =>
7856 Make_Or_Else (Loc,
7857 Left_Opnd =>
7858 Make_Op_Eq (Loc,
7859 Left_Opnd =>
7860 New_Occurrence_Of (C, Loc),
7861 Right_Opnd =>
7862 New_Occurrence_Of (RTE (
7863 RE_POK_Protected_Entry), Loc)),
7865 Right_Opnd =>
7866 Make_Op_Eq (Loc,
7867 Left_Opnd =>
7868 New_Occurrence_Of (C, Loc),
7869 Right_Opnd =>
7870 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
7872 Then_Statements => Unpack));
7873 end if;
7875 -- Generate:
7876 -- if B then
7877 -- if C = POK_Procedure
7878 -- or else C = POK_Protected_Procedure
7879 -- or else C = POK_Task_Procedure
7880 -- then
7881 -- <dispatching-call>
7882 -- end if;
7883 -- -- <triggering-stataments> (code factorized after if-stmt)
7884 -- else
7885 -- <else-statements>
7886 -- goto L0; -- skip triggering statements
7887 -- end if;
7889 N_Stats := New_List;
7891 Prepend_To (N_Stats,
7892 Make_Implicit_If_Statement (N,
7893 Condition =>
7894 Make_Or_Else (Loc,
7895 Left_Opnd =>
7896 Make_Op_Eq (Loc,
7897 Left_Opnd =>
7898 New_Occurrence_Of (C, Loc),
7899 Right_Opnd =>
7900 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
7902 Right_Opnd =>
7903 Make_Or_Else (Loc,
7904 Left_Opnd =>
7905 Make_Op_Eq (Loc,
7906 Left_Opnd =>
7907 New_Occurrence_Of (C, Loc),
7908 Right_Opnd =>
7909 New_Occurrence_Of (RTE (
7910 RE_POK_Protected_Procedure), Loc)),
7912 Right_Opnd =>
7913 Make_Op_Eq (Loc,
7914 Left_Opnd =>
7915 New_Occurrence_Of (C, Loc),
7916 Right_Opnd =>
7917 New_Occurrence_Of (RTE (
7918 RE_POK_Task_Procedure), Loc)))),
7920 Then_Statements =>
7921 New_List (Blk)));
7923 Label_Id := Make_Identifier (Loc, New_External_Name ('L', 0));
7924 Set_Entity (Label_Id,
7925 Make_Defining_Identifier (Loc, Chars (Label_Id)));
7927 Append_To (Else_Statements (N),
7928 Make_Goto_Statement (Loc,
7929 Name => New_Occurrence_Of (Entity (Label_Id), Loc)));
7931 Append_To (Conc_Typ_Stmts,
7932 Make_Implicit_If_Statement (N,
7933 Condition => New_Occurrence_Of (B, Loc),
7934 Then_Statements => N_Stats,
7935 Else_Statements => Else_Statements (N)));
7937 -- Generate:
7938 -- <dispatching-call>;
7939 -- -- <triggering-statements> (code factorized after if-stmt)
7941 Lim_Typ_Stmts := New_List (New_Copy_Tree (Blk));
7943 -- Generate:
7944 -- if K = Ada.Tags.TK_Limited_Tagged
7945 -- or else K = Ada.Tags.TK_Tagged
7946 -- then
7947 -- Lim_Typ_Stmts
7948 -- else
7949 -- Conc_Typ_Stmts
7950 -- end if;
7952 Append_To (Stmts,
7953 Make_Implicit_If_Statement (N,
7954 Condition => Build_Dispatching_Tag_Check (K, N),
7955 Then_Statements => Lim_Typ_Stmts,
7956 Else_Statements => Conc_Typ_Stmts));
7958 Label := Make_Label (Loc, Label_Id);
7959 Append_To (Decls,
7960 Make_Implicit_Label_Declaration (Loc,
7961 Defining_Identifier => Entity (Label_Id),
7962 Label_Construct => Label));
7964 Append_List_To (Stmts, Statements (Alt)); -- triggering-statements
7965 Append_To (Stmts, Label);
7967 Rewrite (N,
7968 Make_Block_Statement (Loc,
7969 Declarations =>
7970 Decls,
7971 Handled_Statement_Sequence =>
7972 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
7974 -- As described above, the entry alternative is transformed into a
7975 -- block that contains the gnulli call, and possibly assignment
7976 -- statements for in-out parameters. The gnulli call may itself be
7977 -- rewritten into a transient block if some unconstrained parameters
7978 -- require it. We need to retrieve the call to complete its parameter
7979 -- list.
7981 else
7982 Transient_Blk :=
7983 First (Statements (Handled_Statement_Sequence (Blk)));
7985 if Present (Transient_Blk)
7986 and then Nkind (Transient_Blk) = N_Block_Statement
7987 then
7988 Blk := Transient_Blk;
7989 end if;
7991 Stmts := Statements (Handled_Statement_Sequence (Blk));
7992 Stmt := First (Stmts);
7993 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
7994 Next (Stmt);
7995 end loop;
7997 Call := Stmt;
7998 Params := Parameter_Associations (Call);
8000 if Is_RTE (Entity (Name (Call)), RE_Protected_Entry_Call) then
8002 -- Substitute Conditional_Entry_Call for Simple_Call parameter
8004 Param := First (Params);
8005 while Present (Param)
8006 and then not Is_RTE (Etype (Param), RE_Call_Modes)
8007 loop
8008 Next (Param);
8009 end loop;
8011 pragma Assert (Present (Param));
8012 Rewrite (Param,
8013 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8015 Analyze (Param);
8017 -- Find the Communication_Block parameter for the call to the
8018 -- Cancelled function.
8020 Decl := First (Declarations (Blk));
8021 while Present (Decl)
8022 and then not Is_RTE (Etype (Object_Definition (Decl)),
8023 RE_Communication_Block)
8024 loop
8025 Next (Decl);
8026 end loop;
8028 -- Add an if statement to execute the else part if the call
8029 -- does not succeed (as indicated by the Cancelled predicate).
8031 Append_To (Stmts,
8032 Make_Implicit_If_Statement (N,
8033 Condition => Make_Function_Call (Loc,
8034 Name => New_Occurrence_Of (RTE (RE_Cancelled), Loc),
8035 Parameter_Associations => New_List (
8036 New_Occurrence_Of (Defining_Identifier (Decl), Loc))),
8037 Then_Statements => Else_Statements (N),
8038 Else_Statements => Statements (Alt)));
8040 else
8041 B := Make_Defining_Identifier (Loc, Name_uB);
8043 -- Insert declaration of B in declarations of existing block
8045 if No (Declarations (Blk)) then
8046 Set_Declarations (Blk, New_List);
8047 end if;
8049 Prepend_To (Declarations (Blk),
8050 Make_Object_Declaration (Loc,
8051 Defining_Identifier => B,
8052 Object_Definition =>
8053 New_Occurrence_Of (Standard_Boolean, Loc)));
8055 -- Create new call statement
8057 Append_To (Params,
8058 New_Occurrence_Of (RTE (RE_Conditional_Call), Loc));
8059 Append_To (Params, New_Occurrence_Of (B, Loc));
8061 Rewrite (Call,
8062 Make_Procedure_Call_Statement (Loc,
8063 Name => New_Occurrence_Of (RTE (RE_Task_Entry_Call), Loc),
8064 Parameter_Associations => Params));
8066 -- Construct statement sequence for new block
8068 Append_To (Stmts,
8069 Make_Implicit_If_Statement (N,
8070 Condition => New_Occurrence_Of (B, Loc),
8071 Then_Statements => Statements (Alt),
8072 Else_Statements => Else_Statements (N)));
8073 end if;
8075 -- The result is the new block
8077 Rewrite (N,
8078 Make_Block_Statement (Loc,
8079 Declarations => Declarations (Blk),
8080 Handled_Statement_Sequence =>
8081 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
8082 end if;
8084 Analyze (N);
8086 Reset_Scopes_To (N, Entity (Identifier (N)));
8087 end Expand_N_Conditional_Entry_Call;
8089 ---------------------------------------
8090 -- Expand_N_Delay_Relative_Statement --
8091 ---------------------------------------
8093 -- Delay statement is implemented as a procedure call to Delay_For
8094 -- defined in Ada.Calendar.Delays in order to reduce the overhead of
8095 -- simple delays imposed by the use of Protected Objects.
8097 procedure Expand_N_Delay_Relative_Statement (N : Node_Id) is
8098 Loc : constant Source_Ptr := Sloc (N);
8099 Proc : Entity_Id;
8101 begin
8102 -- Try to use Ada.Calendar.Delays.Delay_For if available.
8104 if RTE_Available (RO_CA_Delay_For) then
8105 Proc := RTE (RO_CA_Delay_For);
8107 -- Otherwise, use System.Relative_Delays.Delay_For and emit an error
8108 -- message if not available. This is the implementation used on
8109 -- restricted platforms when Ada.Calendar is not available.
8111 else
8112 Proc := RTE (RO_RD_Delay_For);
8113 end if;
8115 Rewrite (N,
8116 Make_Procedure_Call_Statement (Loc,
8117 Name => New_Occurrence_Of (Proc, Loc),
8118 Parameter_Associations => New_List (Expression (N))));
8119 Analyze (N);
8120 end Expand_N_Delay_Relative_Statement;
8122 ------------------------------------
8123 -- Expand_N_Delay_Until_Statement --
8124 ------------------------------------
8126 -- Delay Until statement is implemented as a procedure call to
8127 -- Delay_Until defined in Ada.Calendar.Delays and Ada.Real_Time.Delays.
8129 procedure Expand_N_Delay_Until_Statement (N : Node_Id) is
8130 Loc : constant Source_Ptr := Sloc (N);
8131 Typ : Entity_Id;
8133 begin
8134 if Is_RTE (Base_Type (Etype (Expression (N))), RO_CA_Time) then
8135 Typ := RTE (RO_CA_Delay_Until);
8136 else
8137 Typ := RTE (RO_RT_Delay_Until);
8138 end if;
8140 Rewrite (N,
8141 Make_Procedure_Call_Statement (Loc,
8142 Name => New_Occurrence_Of (Typ, Loc),
8143 Parameter_Associations => New_List (Expression (N))));
8145 Analyze (N);
8146 end Expand_N_Delay_Until_Statement;
8148 -------------------------
8149 -- Expand_N_Entry_Body --
8150 -------------------------
8152 procedure Expand_N_Entry_Body (N : Node_Id) is
8153 begin
8154 -- Associate discriminals with the next protected operation body to be
8155 -- expanded.
8157 if Present (Next_Protected_Operation (N)) then
8158 Set_Discriminals (Parent (Current_Scope));
8159 end if;
8160 end Expand_N_Entry_Body;
8162 -----------------------------------
8163 -- Expand_N_Entry_Call_Statement --
8164 -----------------------------------
8166 -- An entry call is expanded into GNARLI calls to implement a simple entry
8167 -- call (see Build_Simple_Entry_Call).
8169 procedure Expand_N_Entry_Call_Statement (N : Node_Id) is
8170 Concval : Node_Id;
8171 Ename : Node_Id;
8172 Index : Node_Id;
8174 begin
8175 if No_Run_Time_Mode then
8176 Error_Msg_CRT ("entry call", N);
8177 return;
8178 end if;
8180 -- If this entry call is part of an asynchronous select, don't expand it
8181 -- here; it will be expanded with the select statement. Don't expand
8182 -- timed entry calls either, as they are translated into asynchronous
8183 -- entry calls.
8185 -- ??? This whole approach is questionable; it may be better to go back
8186 -- to allowing the expansion to take place and then attempting to fix it
8187 -- up in Expand_N_Asynchronous_Select. The tricky part is figuring out
8188 -- whether the expanded call is on a task or protected entry.
8190 if (Nkind (Parent (N)) /= N_Triggering_Alternative
8191 or else N /= Triggering_Statement (Parent (N)))
8192 and then (Nkind (Parent (N)) /= N_Entry_Call_Alternative
8193 or else N /= Entry_Call_Statement (Parent (N))
8194 or else Nkind (Parent (Parent (N))) /= N_Timed_Entry_Call)
8195 then
8196 Extract_Entry (N, Concval, Ename, Index);
8197 Build_Simple_Entry_Call (N, Concval, Ename, Index);
8198 end if;
8199 end Expand_N_Entry_Call_Statement;
8201 --------------------------------
8202 -- Expand_N_Entry_Declaration --
8203 --------------------------------
8205 -- If there are parameters, then first, each of the formals is marked by
8206 -- setting Is_Entry_Formal. Next a record type is built which is used to
8207 -- hold the parameter values. The name of this record type is entryP where
8208 -- entry is the name of the entry, with an additional corresponding access
8209 -- type called entryPA. The record type has matching components for each
8210 -- formal (the component names are the same as the formal names). For
8211 -- elementary types, the component type matches the formal type. For
8212 -- composite types, an access type is declared (with the name formalA)
8213 -- which designates the formal type, and the type of the component is this
8214 -- access type. Finally the Entry_Component of each formal is set to
8215 -- reference the corresponding record component.
8217 procedure Expand_N_Entry_Declaration (N : Node_Id) is
8218 Loc : constant Source_Ptr := Sloc (N);
8219 Entry_Ent : constant Entity_Id := Defining_Identifier (N);
8220 Components : List_Id;
8221 Formal : Node_Id;
8222 Ftype : Entity_Id;
8223 Last_Decl : Node_Id;
8224 Component : Entity_Id;
8225 Ctype : Entity_Id;
8226 Decl : Node_Id;
8227 Rec_Ent : Entity_Id;
8228 Acc_Ent : Entity_Id;
8230 begin
8231 Formal := First_Formal (Entry_Ent);
8232 Last_Decl := N;
8234 -- Most processing is done only if parameters are present
8236 if Present (Formal) then
8237 Components := New_List;
8239 -- Loop through formals
8241 while Present (Formal) loop
8242 Set_Is_Entry_Formal (Formal);
8243 Component :=
8244 Make_Defining_Identifier (Sloc (Formal), Chars (Formal));
8245 Set_Entry_Component (Formal, Component);
8246 Set_Entry_Formal (Component, Formal);
8247 Ftype := Etype (Formal);
8249 -- Declare new access type and then append
8251 Ctype := Make_Temporary (Loc, 'A');
8252 Set_Is_Param_Block_Component_Type (Ctype);
8254 Decl :=
8255 Make_Full_Type_Declaration (Loc,
8256 Defining_Identifier => Ctype,
8257 Type_Definition =>
8258 Make_Access_To_Object_Definition (Loc,
8259 All_Present => True,
8260 Constant_Present => Ekind (Formal) = E_In_Parameter,
8261 Subtype_Indication => New_Occurrence_Of (Ftype, Loc)));
8263 Insert_After (Last_Decl, Decl);
8264 Last_Decl := Decl;
8266 Append_To (Components,
8267 Make_Component_Declaration (Loc,
8268 Defining_Identifier => Component,
8269 Component_Definition =>
8270 Make_Component_Definition (Loc,
8271 Aliased_Present => False,
8272 Subtype_Indication => New_Occurrence_Of (Ctype, Loc))));
8274 Next_Formal_With_Extras (Formal);
8275 end loop;
8277 -- Create the Entry_Parameter_Record declaration
8279 Rec_Ent := Make_Temporary (Loc, 'P');
8281 Decl :=
8282 Make_Full_Type_Declaration (Loc,
8283 Defining_Identifier => Rec_Ent,
8284 Type_Definition =>
8285 Make_Record_Definition (Loc,
8286 Component_List =>
8287 Make_Component_List (Loc,
8288 Component_Items => Components)));
8290 Insert_After (Last_Decl, Decl);
8291 Last_Decl := Decl;
8293 -- Construct and link in the corresponding access type
8295 Acc_Ent := Make_Temporary (Loc, 'A');
8297 Set_Entry_Parameters_Type (Entry_Ent, Acc_Ent);
8299 Decl :=
8300 Make_Full_Type_Declaration (Loc,
8301 Defining_Identifier => Acc_Ent,
8302 Type_Definition =>
8303 Make_Access_To_Object_Definition (Loc,
8304 All_Present => True,
8305 Subtype_Indication => New_Occurrence_Of (Rec_Ent, Loc)));
8307 Insert_After (Last_Decl, Decl);
8308 end if;
8309 end Expand_N_Entry_Declaration;
8311 -----------------------------
8312 -- Expand_N_Protected_Body --
8313 -----------------------------
8315 -- Protected bodies are expanded to the completion of the subprograms
8316 -- created for the corresponding protected type. These are a protected and
8317 -- unprotected version of each protected subprogram in the object, a
8318 -- function to calculate each entry barrier, and a procedure to execute the
8319 -- sequence of statements of each protected entry body. For example, for
8320 -- protected type ptype:
8322 -- function entB
8323 -- (O : System.Address;
8324 -- E : Protected_Entry_Index)
8325 -- return Boolean
8326 -- is
8327 -- <discriminant renamings>
8328 -- <private object renamings>
8329 -- begin
8330 -- return <barrier expression>;
8331 -- end entB;
8333 -- procedure pprocN (_object : in out poV;...) is
8334 -- <discriminant renamings>
8335 -- <private object renamings>
8336 -- begin
8337 -- <sequence of statements>
8338 -- end pprocN;
8340 -- procedure pprocP (_object : in out poV;...) is
8341 -- procedure _clean is
8342 -- Pn : Boolean;
8343 -- begin
8344 -- ptypeS (_object, Pn);
8345 -- Unlock (_object._object'Access);
8346 -- Abort_Undefer.all;
8347 -- end _clean;
8349 -- begin
8350 -- Abort_Defer.all;
8351 -- Lock (_object._object'Access);
8352 -- pprocN (_object;...);
8353 -- at end
8354 -- _clean;
8355 -- end pproc;
8357 -- function pfuncN (_object : poV;...) return Return_Type is
8358 -- <discriminant renamings>
8359 -- <private object renamings>
8360 -- begin
8361 -- <sequence of statements>
8362 -- end pfuncN;
8364 -- function pfuncP (_object : poV) return Return_Type is
8365 -- procedure _clean is
8366 -- begin
8367 -- Unlock (_object._object'Access);
8368 -- Abort_Undefer.all;
8369 -- end _clean;
8371 -- begin
8372 -- Abort_Defer.all;
8373 -- Lock (_object._object'Access);
8374 -- return pfuncN (_object);
8376 -- at end
8377 -- _clean;
8378 -- end pfunc;
8380 -- procedure entE
8381 -- (O : System.Address;
8382 -- P : System.Address;
8383 -- E : Protected_Entry_Index)
8384 -- is
8385 -- <discriminant renamings>
8386 -- <private object renamings>
8387 -- type poVP is access poV;
8388 -- _Object : ptVP := ptVP!(O);
8390 -- begin
8391 -- begin
8392 -- <statement sequence>
8393 -- Complete_Entry_Body (_Object._Object);
8394 -- exception
8395 -- when all others =>
8396 -- Exceptional_Complete_Entry_Body (
8397 -- _Object._Object, Get_GNAT_Exception);
8398 -- end;
8399 -- end entE;
8401 -- The type poV is the record created for the protected type to hold
8402 -- the state of the protected object.
8404 procedure Expand_N_Protected_Body (N : Node_Id) is
8405 Loc : constant Source_Ptr := Sloc (N);
8406 Pid : constant Entity_Id := Corresponding_Spec (N);
8408 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Pid);
8409 -- This flag indicates whether the lock free implementation is active
8411 Current_Node : Node_Id;
8412 Disp_Op_Body : Node_Id;
8413 New_Op_Body : Node_Id;
8414 New_Op_Spec : Node_Id;
8415 Op_Body : Node_Id;
8416 Op_Decl : Node_Id;
8417 Op_Id : Entity_Id;
8418 Op_Spec : Entity_Id;
8420 function Build_Dispatching_Subprogram_Body
8421 (N : Node_Id;
8422 Pid : Node_Id;
8423 Prot_Bod : Node_Id) return Node_Id;
8424 -- Build a dispatching version of the protected subprogram body. The
8425 -- newly generated subprogram contains a call to the original protected
8426 -- body. The following code is generated:
8428 -- function <protected-function-name> (Param1 .. ParamN) return
8429 -- <return-type> is
8430 -- begin
8431 -- return <protected-function-name>P (Param1 .. ParamN);
8432 -- end <protected-function-name>;
8434 -- or
8436 -- procedure <protected-procedure-name> (Param1 .. ParamN) is
8437 -- begin
8438 -- <protected-procedure-name>P (Param1 .. ParamN);
8439 -- end <protected-procedure-name>
8441 ---------------------------------------
8442 -- Build_Dispatching_Subprogram_Body --
8443 ---------------------------------------
8445 function Build_Dispatching_Subprogram_Body
8446 (N : Node_Id;
8447 Pid : Node_Id;
8448 Prot_Bod : Node_Id) return Node_Id
8450 Loc : constant Source_Ptr := Sloc (N);
8451 Actuals : List_Id;
8452 Formal : Node_Id;
8453 Spec : Node_Id;
8454 Stmts : List_Id;
8456 begin
8457 -- Generate a specification without a letter suffix in order to
8458 -- override an interface function or procedure.
8460 Spec := Build_Protected_Sub_Specification (N, Pid, Dispatching_Mode);
8462 -- The formal parameters become the actuals of the protected function
8463 -- or procedure call.
8465 Actuals := New_List;
8466 Formal := First (Parameter_Specifications (Spec));
8467 while Present (Formal) loop
8468 Append_To (Actuals,
8469 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
8470 Next (Formal);
8471 end loop;
8473 if Nkind (Spec) = N_Procedure_Specification then
8474 Stmts :=
8475 New_List (
8476 Make_Procedure_Call_Statement (Loc,
8477 Name =>
8478 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8479 Parameter_Associations => Actuals));
8481 else
8482 pragma Assert (Nkind (Spec) = N_Function_Specification);
8484 Stmts :=
8485 New_List (
8486 Make_Simple_Return_Statement (Loc,
8487 Expression =>
8488 Make_Function_Call (Loc,
8489 Name =>
8490 New_Occurrence_Of (Corresponding_Spec (Prot_Bod), Loc),
8491 Parameter_Associations => Actuals)));
8492 end if;
8494 return
8495 Make_Subprogram_Body (Loc,
8496 Declarations => Empty_List,
8497 Specification => Spec,
8498 Handled_Statement_Sequence =>
8499 Make_Handled_Sequence_Of_Statements (Loc, Stmts));
8500 end Build_Dispatching_Subprogram_Body;
8502 -- Start of processing for Expand_N_Protected_Body
8504 begin
8505 if No_Run_Time_Mode then
8506 Error_Msg_CRT ("protected body", N);
8507 return;
8508 end if;
8510 -- This is the proper body corresponding to a stub. The declarations
8511 -- must be inserted at the point of the stub, which in turn is in the
8512 -- declarative part of the parent unit.
8514 if Nkind (Parent (N)) = N_Subunit then
8515 Current_Node := Corresponding_Stub (Parent (N));
8516 else
8517 Current_Node := N;
8518 end if;
8520 Op_Body := First (Declarations (N));
8522 -- The protected body is replaced with the bodies of its protected
8523 -- operations, and the declarations for internal objects that may
8524 -- have been created for entry family bounds.
8526 Rewrite (N, Make_Null_Statement (Sloc (N)));
8527 Analyze (N);
8529 while Present (Op_Body) loop
8530 case Nkind (Op_Body) is
8531 when N_Subprogram_Declaration =>
8532 null;
8534 when N_Subprogram_Body =>
8535 Op_Spec := Corresponding_Spec (Op_Body);
8537 -- Do not create bodies for eliminated operations
8539 if not Is_Eliminated (Defining_Entity (Op_Body))
8540 and then not Is_Eliminated (Op_Spec)
8541 then
8542 if Lock_Free_Active then
8543 New_Op_Body :=
8544 Build_Lock_Free_Unprotected_Subprogram_Body
8545 (Op_Body, Pid);
8546 else
8547 New_Op_Body :=
8548 Build_Unprotected_Subprogram_Body (Op_Body, Pid);
8549 end if;
8551 Insert_After (Current_Node, New_Op_Body);
8552 Current_Node := New_Op_Body;
8553 Analyze (New_Op_Body);
8555 New_Op_Spec := Corresponding_Spec (New_Op_Body);
8557 -- When the original subprogram body has nested subprograms,
8558 -- the new body also has them, so set the flag accordingly.
8560 Set_Has_Nested_Subprogram
8561 (New_Op_Spec, Has_Nested_Subprogram (New_Op_Spec));
8563 -- Similarly, when the original subprogram body uses the
8564 -- secondary stack, the new body also does. This is needed
8565 -- when the cleanup actions of the subprogram are delayed
8566 -- because it contains a package instance with a body.
8568 Set_Uses_Sec_Stack (New_Op_Spec, Uses_Sec_Stack (Op_Spec));
8570 -- Now reset the scopes of the top-level nested subprograms
8571 -- and other declaration entities so that they now refer to
8572 -- the new body's entity (it would preferable to do this
8573 -- within Build_Protected_Sub_Specification, which is called
8574 -- from Build_Unprotected_Subprogram_Body, but the needed
8575 -- subprogram entity isn't available via Corresponding_Spec
8576 -- until after the above Analyze call).
8578 Reset_Scopes_To (New_Op_Body, New_Op_Spec);
8580 -- Build the corresponding protected operation. This is
8581 -- needed only if this is a public or private operation of
8582 -- the type.
8584 Op_Decl := Unit_Declaration_Node (Op_Spec);
8586 if Nkind (Parent (Op_Decl)) = N_Protected_Definition then
8587 if Lock_Free_Active then
8588 New_Op_Body :=
8589 Build_Lock_Free_Protected_Subprogram_Body
8590 (Op_Body, Pid, Specification (New_Op_Body));
8591 else
8592 New_Op_Body :=
8593 Build_Protected_Subprogram_Body
8594 (Op_Body, Pid, Specification (New_Op_Body));
8595 end if;
8597 Insert_After (Current_Node, New_Op_Body);
8598 Current_Node := New_Op_Body;
8599 Analyze (New_Op_Body);
8601 -- Generate an overriding primitive operation body for
8602 -- this subprogram if the protected type implements
8603 -- an interface.
8605 if Ada_Version >= Ada_2005
8606 and then
8607 Present (Interfaces (Corresponding_Record_Type (Pid)))
8608 then
8609 Disp_Op_Body :=
8610 Build_Dispatching_Subprogram_Body (
8611 Op_Body, Pid, New_Op_Body);
8613 Insert_After (Current_Node, Disp_Op_Body);
8614 Current_Node := Disp_Op_Body;
8615 Analyze (Disp_Op_Body);
8616 end if;
8617 end if;
8618 end if;
8620 when N_Entry_Body =>
8621 Op_Id := Defining_Identifier (Op_Body);
8622 New_Op_Body := Build_Protected_Entry (Op_Body, Op_Id, Pid);
8624 Insert_After (Current_Node, New_Op_Body);
8625 Current_Node := New_Op_Body;
8626 Analyze (New_Op_Body);
8628 when N_Implicit_Label_Declaration =>
8629 null;
8631 when N_Call_Marker
8632 | N_Itype_Reference
8634 New_Op_Body := New_Copy (Op_Body);
8635 Insert_After (Current_Node, New_Op_Body);
8636 Current_Node := New_Op_Body;
8638 when N_Freeze_Entity =>
8639 New_Op_Body := New_Copy (Op_Body);
8641 if Present (Entity (Op_Body))
8642 and then Freeze_Node (Entity (Op_Body)) = Op_Body
8643 then
8644 Set_Freeze_Node (Entity (Op_Body), New_Op_Body);
8645 end if;
8647 Insert_After (Current_Node, New_Op_Body);
8648 Current_Node := New_Op_Body;
8649 Analyze (New_Op_Body);
8651 when N_Pragma =>
8652 New_Op_Body := New_Copy (Op_Body);
8653 Insert_After (Current_Node, New_Op_Body);
8654 Current_Node := New_Op_Body;
8655 Analyze (New_Op_Body);
8657 when N_Object_Declaration =>
8658 pragma Assert (not Comes_From_Source (Op_Body));
8659 New_Op_Body := New_Copy (Op_Body);
8660 Insert_After (Current_Node, New_Op_Body);
8661 Current_Node := New_Op_Body;
8662 Analyze (New_Op_Body);
8664 when others =>
8665 raise Program_Error;
8666 end case;
8668 Next (Op_Body);
8669 end loop;
8671 -- Finally, create the body of the function that maps an entry index
8672 -- into the corresponding body index, except when there is no entry, or
8673 -- in a Ravenscar-like profile.
8675 if Corresponding_Runtime_Package (Pid) =
8676 System_Tasking_Protected_Objects_Entries
8677 then
8678 New_Op_Body := Build_Find_Body_Index (Pid);
8679 Insert_After (Current_Node, New_Op_Body);
8680 Current_Node := New_Op_Body;
8681 Analyze (New_Op_Body);
8682 end if;
8684 -- Ada 2005 (AI-345): Construct the primitive wrapper bodies after the
8685 -- protected body. At this point all wrapper specs have been created,
8686 -- frozen and included in the dispatch table for the protected type.
8688 if Ada_Version >= Ada_2005 then
8689 Build_Wrapper_Bodies (Loc, Pid, Current_Node);
8690 end if;
8691 end Expand_N_Protected_Body;
8693 -----------------------------------------
8694 -- Expand_N_Protected_Type_Declaration --
8695 -----------------------------------------
8697 -- First we create a corresponding record type declaration used to
8698 -- represent values of this protected type.
8699 -- The general form of this type declaration is
8701 -- type poV (discriminants) is record
8702 -- _Object : aliased <kind>Protection
8703 -- [(<entry count> [, <handler count>])];
8704 -- [entry_family : array (bounds) of Void;]
8705 -- <private data fields>
8706 -- end record;
8708 -- The discriminants are present only if the corresponding protected type
8709 -- has discriminants, and they exactly mirror the protected type
8710 -- discriminants. The private data fields similarly mirror the private
8711 -- declarations of the protected type.
8713 -- The Object field is always present. It contains RTS specific data used
8714 -- to control the protected object. It is declared as Aliased so that it
8715 -- can be passed as a pointer to the RTS. This allows the protected record
8716 -- to be referenced within RTS data structures. An appropriate Protection
8717 -- type and discriminant are generated.
8719 -- The Service field is present for protected objects with entries. It
8720 -- contains sufficient information to allow the entry service procedure for
8721 -- this object to be called when the object is not known till runtime.
8723 -- One entry_family component is present for each entry family in the
8724 -- task definition (see Expand_N_Task_Type_Declaration).
8726 -- When a protected object is declared, an instance of the protected type
8727 -- value record is created. The elaboration of this declaration creates the
8728 -- correct bounds for the entry families, and also evaluates the priority
8729 -- expression if needed. The initialization routine for the protected type
8730 -- itself then calls Initialize_Protection with appropriate parameters to
8731 -- initialize the value of the Task_Id field. Install_Handlers may be also
8732 -- called if a pragma Attach_Handler applies.
8734 -- Note: this record is passed to the subprograms created by the expansion
8735 -- of protected subprograms and entries. It is an in parameter to protected
8736 -- functions and an in out parameter to procedures and entry bodies. The
8737 -- Entity_Id for this created record type is placed in the
8738 -- Corresponding_Record_Type field of the associated protected type entity.
8740 -- Next we create a procedure specifications for protected subprograms and
8741 -- entry bodies. For each protected subprograms two subprograms are
8742 -- created, an unprotected and a protected version. The unprotected version
8743 -- is called from within other operations of the same protected object.
8745 -- We also build the call to register the procedure if a pragma
8746 -- Interrupt_Handler applies.
8748 -- A single subprogram is created to service all entry bodies; it has an
8749 -- additional boolean out parameter indicating that the previous entry call
8750 -- made by the current task was serviced immediately, i.e. not by proxy.
8751 -- The O parameter contains a pointer to a record object of the type
8752 -- described above. An untyped interface is used here to allow this
8753 -- procedure to be called in places where the type of the object to be
8754 -- serviced is not known. This must be done, for example, when a call that
8755 -- may have been requeued is cancelled; the corresponding object must be
8756 -- serviced, but which object that is not known till runtime.
8758 -- procedure ptypeS
8759 -- (O : System.Address; P : out Boolean);
8760 -- procedure pprocN (_object : in out poV);
8761 -- procedure pproc (_object : in out poV);
8762 -- function pfuncN (_object : poV);
8763 -- function pfunc (_object : poV);
8764 -- ...
8766 -- Note that this must come after the record type declaration, since
8767 -- the specs refer to this type.
8769 procedure Expand_N_Protected_Type_Declaration (N : Node_Id) is
8770 Discr_Map : constant Elist_Id := New_Elmt_List;
8771 Loc : constant Source_Ptr := Sloc (N);
8772 Prot_Typ : constant Entity_Id := Defining_Identifier (N);
8774 Lock_Free_Active : constant Boolean := Uses_Lock_Free (Prot_Typ);
8775 -- This flag indicates whether the lock free implementation is active
8777 Pdef : constant Node_Id := Protected_Definition (N);
8778 -- This contains two lists; one for visible and one for private decls
8780 Current_Node : Node_Id := N;
8781 E_Count : Int;
8782 Entries_Aggr : Node_Id;
8783 Rec_Decl : Node_Id;
8784 Rec_Id : Entity_Id;
8786 procedure Check_Inlining (Subp : Entity_Id);
8787 -- If the original operation has a pragma Inline, propagate the flag
8788 -- to the internal body, for possible inlining later on. The source
8789 -- operation is invisible to the back-end and is never actually called.
8791 procedure Expand_Entry_Declaration (Decl : Node_Id);
8792 -- Create the entry barrier and the procedure body for entry declaration
8793 -- Decl. All generated subprograms are added to Entry_Bodies_Array.
8795 function Static_Component_Size (Comp : Entity_Id) return Boolean;
8796 -- When compiling under the Ravenscar profile, private components must
8797 -- have a static size, or else a protected object will require heap
8798 -- allocation, violating the corresponding restriction. It is preferable
8799 -- to make this check here, because it provides a better error message
8800 -- than the back-end, which refers to the object as a whole.
8802 procedure Register_Handler;
8803 -- For a protected operation that is an interrupt handler, add the
8804 -- freeze action that will register it as such.
8806 procedure Replace_Access_Definition (Comp : Node_Id);
8807 -- If a private component of the type is an access to itself, this
8808 -- is not a reference to the current instance, but an access type out
8809 -- of which one might construct a list. If such a component exists, we
8810 -- create an incomplete type for the equivalent record type, and
8811 -- a named access type for it, that replaces the access definition
8812 -- of the original component. This is similar to what is done for
8813 -- records in Check_Anonymous_Access_Components, but simpler, because
8814 -- the corresponding record type has no previous declaration.
8815 -- This needs to be done only once, even if there are several such
8816 -- access components. The following entity stores the constructed
8817 -- access type.
8819 Acc_T : Entity_Id := Empty;
8821 --------------------
8822 -- Check_Inlining --
8823 --------------------
8825 procedure Check_Inlining (Subp : Entity_Id) is
8826 begin
8827 if Is_Inlined (Subp) then
8828 Set_Is_Inlined (Protected_Body_Subprogram (Subp));
8829 Set_Is_Inlined (Subp, False);
8830 end if;
8832 if Has_Pragma_No_Inline (Subp) then
8833 Set_Has_Pragma_No_Inline (Protected_Body_Subprogram (Subp));
8834 end if;
8835 end Check_Inlining;
8837 ---------------------------
8838 -- Static_Component_Size --
8839 ---------------------------
8841 function Static_Component_Size (Comp : Entity_Id) return Boolean is
8842 Typ : constant Entity_Id := Etype (Comp);
8843 C : Entity_Id;
8845 begin
8846 if Is_Scalar_Type (Typ) then
8847 return True;
8849 elsif Is_Array_Type (Typ) then
8850 return Compile_Time_Known_Bounds (Typ);
8852 elsif Is_Record_Type (Typ) then
8853 C := First_Component (Typ);
8854 while Present (C) loop
8855 if not Static_Component_Size (C) then
8856 return False;
8857 end if;
8859 Next_Component (C);
8860 end loop;
8862 return True;
8864 -- Any other type will be checked by the back-end
8866 else
8867 return True;
8868 end if;
8869 end Static_Component_Size;
8871 ------------------------------
8872 -- Expand_Entry_Declaration --
8873 ------------------------------
8875 procedure Expand_Entry_Declaration (Decl : Node_Id) is
8876 Ent_Id : constant Entity_Id := Defining_Entity (Decl);
8877 Bar_Id : Entity_Id;
8878 Bod_Id : Entity_Id;
8879 Subp : Node_Id;
8881 begin
8882 E_Count := E_Count + 1;
8884 -- Create the protected body subprogram
8886 Bod_Id :=
8887 Make_Defining_Identifier (Loc,
8888 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'E'));
8889 Set_Protected_Body_Subprogram (Ent_Id, Bod_Id);
8891 Subp :=
8892 Make_Subprogram_Declaration (Loc,
8893 Specification =>
8894 Build_Protected_Entry_Specification (Loc, Bod_Id, Ent_Id));
8896 Insert_After (Current_Node, Subp);
8897 Current_Node := Subp;
8899 Analyze (Subp);
8901 -- Build a wrapper procedure to handle contract cases, preconditions,
8902 -- and postconditions.
8904 Build_Entry_Contract_Wrapper (Ent_Id, N);
8906 -- Create the barrier function
8908 Bar_Id :=
8909 Make_Defining_Identifier (Loc,
8910 Chars => Build_Selected_Name (Prot_Typ, Ent_Id, 'B'));
8911 Set_Barrier_Function (Ent_Id, Bar_Id);
8913 Subp :=
8914 Make_Subprogram_Declaration (Loc,
8915 Specification =>
8916 Build_Barrier_Function_Specification (Loc, Bar_Id));
8917 Set_Is_Entry_Barrier_Function (Subp);
8919 Insert_After (Current_Node, Subp);
8920 Current_Node := Subp;
8922 Analyze (Subp);
8924 Set_Protected_Body_Subprogram (Bar_Id, Bar_Id);
8925 Set_Scope (Bar_Id, Scope (Ent_Id));
8927 -- Collect pointers to the protected subprogram and the barrier
8928 -- of the current entry, for insertion into Entry_Bodies_Array.
8930 Append_To (Expressions (Entries_Aggr),
8931 Make_Aggregate (Loc,
8932 Expressions => New_List (
8933 Make_Attribute_Reference (Loc,
8934 Prefix => New_Occurrence_Of (Bar_Id, Loc),
8935 Attribute_Name => Name_Unrestricted_Access),
8936 Make_Attribute_Reference (Loc,
8937 Prefix => New_Occurrence_Of (Bod_Id, Loc),
8938 Attribute_Name => Name_Unrestricted_Access))));
8939 end Expand_Entry_Declaration;
8941 ----------------------
8942 -- Register_Handler --
8943 ----------------------
8945 procedure Register_Handler is
8947 -- All semantic checks already done in Sem_Prag
8949 Prot_Proc : constant Entity_Id :=
8950 Defining_Unit_Name (Specification (Current_Node));
8952 Proc_Address : constant Node_Id :=
8953 Make_Attribute_Reference (Loc,
8954 Prefix =>
8955 New_Occurrence_Of (Prot_Proc, Loc),
8956 Attribute_Name => Name_Address);
8958 RTS_Call : constant Entity_Id :=
8959 Make_Procedure_Call_Statement (Loc,
8960 Name =>
8961 New_Occurrence_Of
8962 (RTE (RE_Register_Interrupt_Handler), Loc),
8963 Parameter_Associations => New_List (Proc_Address));
8964 begin
8965 Append_Freeze_Action (Prot_Proc, RTS_Call);
8966 end Register_Handler;
8968 -------------------------------
8969 -- Replace_Access_Definition --
8970 -------------------------------
8972 procedure Replace_Access_Definition (Comp : Node_Id) is
8973 Loc : constant Source_Ptr := Sloc (Comp);
8974 Inc_T : Node_Id;
8975 Inc_D : Node_Id;
8976 Acc_Def : Node_Id;
8977 Acc_D : Node_Id;
8979 begin
8980 if No (Acc_T) then
8981 Inc_T := Make_Defining_Identifier (Loc, Chars (Rec_Id));
8982 Inc_D := Make_Incomplete_Type_Declaration (Loc, Inc_T);
8983 Acc_T := Make_Temporary (Loc, 'S');
8984 Acc_Def :=
8985 Make_Access_To_Object_Definition (Loc,
8986 Subtype_Indication => New_Occurrence_Of (Inc_T, Loc));
8987 Acc_D :=
8988 Make_Full_Type_Declaration (Loc,
8989 Defining_Identifier => Acc_T,
8990 Type_Definition => Acc_Def);
8992 Insert_Before (Rec_Decl, Inc_D);
8993 Analyze (Inc_D);
8995 Insert_Before (Rec_Decl, Acc_D);
8996 Analyze (Acc_D);
8997 end if;
8999 Set_Access_Definition (Comp, Empty);
9000 Set_Subtype_Indication (Comp, New_Occurrence_Of (Acc_T, Loc));
9001 end Replace_Access_Definition;
9003 -- Local variables
9005 Body_Arr : Node_Id;
9006 Body_Id : Entity_Id;
9007 Cdecls : List_Id;
9008 Comp : Node_Id;
9009 Expr : Node_Id;
9010 New_Priv : Node_Id;
9011 Obj_Def : Node_Id;
9012 Object_Comp : Node_Id;
9013 Priv : Node_Id;
9014 Sub : Node_Id;
9016 -- Start of processing for Expand_N_Protected_Type_Declaration
9018 begin
9019 if Present (Corresponding_Record_Type (Prot_Typ)) then
9020 return;
9021 else
9022 Rec_Decl := Build_Corresponding_Record (N, Prot_Typ, Loc);
9023 Rec_Id := Defining_Identifier (Rec_Decl);
9024 end if;
9026 Cdecls := Component_Items (Component_List (Type_Definition (Rec_Decl)));
9028 Qualify_Entity_Names (N);
9030 -- If the type has discriminants, their occurrences in the declaration
9031 -- have been replaced by the corresponding discriminals. For components
9032 -- that are constrained by discriminants, their homologues in the
9033 -- corresponding record type must refer to the discriminants of that
9034 -- record, so we must apply a new renaming to subtypes_indications:
9036 -- protected discriminant => discriminal => record discriminant
9038 -- This replacement is not applied to default expressions, for which
9039 -- the discriminal is correct.
9041 if Has_Discriminants (Prot_Typ) then
9042 declare
9043 Disc : Entity_Id;
9044 Decl : Node_Id;
9046 begin
9047 Disc := First_Discriminant (Prot_Typ);
9048 Decl := First (Discriminant_Specifications (Rec_Decl));
9049 while Present (Disc) loop
9050 Append_Elmt (Discriminal (Disc), Discr_Map);
9051 Append_Elmt (Defining_Identifier (Decl), Discr_Map);
9052 Next_Discriminant (Disc);
9053 Next (Decl);
9054 end loop;
9055 end;
9056 end if;
9058 -- Fill in the component declarations
9060 -- Add components for entry families. For each entry family, create an
9061 -- anonymous type declaration with the same size, and analyze the type.
9063 Collect_Entry_Families (Loc, Cdecls, Current_Node, Prot_Typ);
9065 pragma Assert (Present (Pdef));
9067 Insert_After (Current_Node, Rec_Decl);
9068 Current_Node := Rec_Decl;
9070 -- Add private field components
9072 Priv := First (Private_Declarations (Pdef));
9073 while Present (Priv) loop
9074 if Nkind (Priv) = N_Component_Declaration then
9075 if not Static_Component_Size (Defining_Identifier (Priv)) then
9077 -- When compiling for a restricted profile, the private
9078 -- components must have a static size. If not, this is an error
9079 -- for a single protected declaration, and rates a warning on a
9080 -- protected type declaration.
9082 if not Comes_From_Source (Prot_Typ) then
9084 -- It's ok to be checking this restriction at expansion
9085 -- time, because this is only for the restricted profile,
9086 -- which is not subject to strict RM conformance, so it
9087 -- is OK to miss this check in -gnatc mode.
9089 Check_Restriction (No_Implicit_Heap_Allocations, Priv);
9090 Check_Restriction
9091 (No_Implicit_Protected_Object_Allocations, Priv);
9093 elsif Restriction_Active (No_Implicit_Heap_Allocations) then
9094 if not Discriminated_Size (Defining_Identifier (Priv))
9095 then
9096 -- Any object of the type will be non-static
9098 Error_Msg_N ("component has non-static size??", Priv);
9099 Error_Msg_NE
9100 ("\creation of protected object of type& will "
9101 & "violate restriction "
9102 & "No_Implicit_Heap_Allocations??", Priv, Prot_Typ);
9103 else
9104 -- Object will be non-static if discriminants are
9106 Error_Msg_NE
9107 ("creation of protected object of type& with "
9108 & "non-static discriminants will violate "
9109 & "restriction No_Implicit_Heap_Allocations??",
9110 Priv, Prot_Typ);
9111 end if;
9113 -- Likewise for No_Implicit_Protected_Object_Allocations
9115 elsif Restriction_Active
9116 (No_Implicit_Protected_Object_Allocations)
9117 then
9118 if not Discriminated_Size (Defining_Identifier (Priv)) then
9119 -- Any object of the type will be non-static
9121 Error_Msg_N ("component has non-static size??", Priv);
9122 Error_Msg_NE
9123 ("\creation of protected object of type& will violate "
9124 & "restriction "
9125 & "No_Implicit_Protected_Object_Allocations??",
9126 Priv, Prot_Typ);
9127 else
9128 -- Object will be non-static if discriminants are
9130 Error_Msg_NE
9131 ("creation of protected object of type& with "
9132 & "non-static discriminants will violate restriction "
9133 & "No_Implicit_Protected_Object_Allocations??",
9134 Priv, Prot_Typ);
9135 end if;
9136 end if;
9137 end if;
9139 -- The component definition consists of a subtype indication, or
9140 -- (in Ada 2005) an access definition. Make a copy of the proper
9141 -- definition.
9143 declare
9144 Old_Comp : constant Node_Id := Component_Definition (Priv);
9145 Oent : constant Entity_Id := Defining_Identifier (Priv);
9146 Nent : constant Entity_Id :=
9147 Make_Defining_Identifier (Sloc (Oent),
9148 Chars => Chars (Oent));
9149 New_Comp : Node_Id;
9151 begin
9152 if Present (Subtype_Indication (Old_Comp)) then
9153 New_Comp :=
9154 Make_Component_Definition (Sloc (Oent),
9155 Aliased_Present => False,
9156 Subtype_Indication =>
9157 New_Copy_Tree
9158 (Subtype_Indication (Old_Comp), Discr_Map));
9159 else
9160 New_Comp :=
9161 Make_Component_Definition (Sloc (Oent),
9162 Aliased_Present => False,
9163 Access_Definition =>
9164 New_Copy_Tree
9165 (Access_Definition (Old_Comp), Discr_Map));
9167 -- A self-reference in the private part becomes a
9168 -- self-reference to the corresponding record.
9170 if Entity (Subtype_Mark (Access_Definition (New_Comp)))
9171 = Prot_Typ
9172 then
9173 Replace_Access_Definition (New_Comp);
9174 end if;
9175 end if;
9177 New_Priv :=
9178 Make_Component_Declaration (Loc,
9179 Defining_Identifier => Nent,
9180 Component_Definition => New_Comp,
9181 Expression => Expression (Priv));
9183 Set_Has_Per_Object_Constraint (Nent,
9184 Has_Per_Object_Constraint (Oent));
9186 Append_To (Cdecls, New_Priv);
9187 end;
9189 elsif Nkind (Priv) = N_Subprogram_Declaration then
9191 -- Make the unprotected version of the subprogram available for
9192 -- expansion of intra object calls. There is need for a protected
9193 -- version only if the subprogram is an interrupt handler,
9194 -- otherwise this operation can only be called from within the
9195 -- body.
9197 Sub :=
9198 Make_Subprogram_Declaration (Loc,
9199 Specification =>
9200 Build_Protected_Sub_Specification
9201 (Priv, Prot_Typ, Unprotected_Mode));
9203 Insert_After (Current_Node, Sub);
9204 Analyze (Sub);
9206 Set_Protected_Body_Subprogram
9207 (Defining_Unit_Name (Specification (Priv)),
9208 Defining_Unit_Name (Specification (Sub)));
9209 Check_Inlining (Defining_Unit_Name (Specification (Priv)));
9210 Current_Node := Sub;
9212 Sub :=
9213 Make_Subprogram_Declaration (Loc,
9214 Specification =>
9215 Build_Protected_Sub_Specification
9216 (Priv, Prot_Typ, Protected_Mode));
9218 Insert_After (Current_Node, Sub);
9219 Analyze (Sub);
9220 Current_Node := Sub;
9222 if Is_Interrupt_Handler
9223 (Defining_Unit_Name (Specification (Priv)))
9224 then
9225 if not Restricted_Profile then
9226 Register_Handler;
9227 end if;
9228 end if;
9229 end if;
9231 Next (Priv);
9232 end loop;
9234 -- Except for the lock-free implementation, append the _Object field
9235 -- with the right type to the component list. We need to compute the
9236 -- number of entries, and in some cases the number of Attach_Handler
9237 -- pragmas.
9239 if not Lock_Free_Active then
9240 declare
9241 Entry_Count_Expr : constant Node_Id :=
9242 Build_Entry_Count_Expression
9243 (Prot_Typ, Loc);
9244 Num_Attach_Handler : Nat := 0;
9245 Protection_Subtype : Node_Id;
9246 Ritem : Node_Id;
9248 begin
9249 if Has_Attach_Handler (Prot_Typ) then
9250 Ritem := First_Rep_Item (Prot_Typ);
9251 while Present (Ritem) loop
9252 if Nkind (Ritem) = N_Pragma
9253 and then Pragma_Name (Ritem) = Name_Attach_Handler
9254 then
9255 Num_Attach_Handler := Num_Attach_Handler + 1;
9256 end if;
9258 Next_Rep_Item (Ritem);
9259 end loop;
9260 end if;
9262 -- Determine the proper protection type. There are two special
9263 -- cases: 1) when the protected type has dynamic interrupt
9264 -- handlers, and 2) when it has static handlers and we use a
9265 -- restricted profile.
9267 if Has_Attach_Handler (Prot_Typ)
9268 and then not Restricted_Profile
9269 then
9270 Protection_Subtype :=
9271 Make_Subtype_Indication (Loc,
9272 Subtype_Mark =>
9273 New_Occurrence_Of
9274 (RTE (RE_Static_Interrupt_Protection), Loc),
9275 Constraint =>
9276 Make_Index_Or_Discriminant_Constraint (Loc,
9277 Constraints => New_List (
9278 Entry_Count_Expr,
9279 Make_Integer_Literal (Loc, Num_Attach_Handler))));
9281 elsif Has_Interrupt_Handler (Prot_Typ)
9282 and then not Restriction_Active (No_Dynamic_Attachment)
9283 then
9284 Protection_Subtype :=
9285 Make_Subtype_Indication (Loc,
9286 Subtype_Mark =>
9287 New_Occurrence_Of
9288 (RTE (RE_Dynamic_Interrupt_Protection), Loc),
9289 Constraint =>
9290 Make_Index_Or_Discriminant_Constraint (Loc,
9291 Constraints => New_List (Entry_Count_Expr)));
9293 else
9294 case Corresponding_Runtime_Package (Prot_Typ) is
9295 when System_Tasking_Protected_Objects_Entries =>
9296 Protection_Subtype :=
9297 Make_Subtype_Indication (Loc,
9298 Subtype_Mark =>
9299 New_Occurrence_Of
9300 (RTE (RE_Protection_Entries), Loc),
9301 Constraint =>
9302 Make_Index_Or_Discriminant_Constraint (Loc,
9303 Constraints => New_List (Entry_Count_Expr)));
9305 when System_Tasking_Protected_Objects_Single_Entry =>
9306 Protection_Subtype :=
9307 New_Occurrence_Of (RTE (RE_Protection_Entry), Loc);
9309 when System_Tasking_Protected_Objects =>
9310 Protection_Subtype :=
9311 New_Occurrence_Of (RTE (RE_Protection), Loc);
9313 when others =>
9314 raise Program_Error;
9315 end case;
9316 end if;
9318 Object_Comp :=
9319 Make_Component_Declaration (Loc,
9320 Defining_Identifier =>
9321 Make_Defining_Identifier (Loc, Name_uObject),
9322 Component_Definition =>
9323 Make_Component_Definition (Loc,
9324 Aliased_Present => True,
9325 Subtype_Indication => Protection_Subtype));
9326 end;
9328 -- Put the _Object component after the private component so that it
9329 -- be finalized early as required by 9.4 (20)
9331 Append_To (Cdecls, Object_Comp);
9332 end if;
9334 -- Analyze the record declaration immediately after construction,
9335 -- because the initialization procedure is needed for single object
9336 -- declarations before the next entity is analyzed (the freeze call
9337 -- that generates this initialization procedure is found below).
9339 Analyze (Rec_Decl, Suppress => All_Checks);
9341 -- Ada 2005 (AI-345): Construct the primitive entry wrappers before
9342 -- the corresponding record is frozen. If any wrappers are generated,
9343 -- Current_Node is updated accordingly.
9345 if Ada_Version >= Ada_2005 then
9346 Build_Wrapper_Specs (Loc, Prot_Typ, Current_Node);
9347 end if;
9349 -- Collect pointers to entry bodies and their barriers, to be placed
9350 -- in the Entry_Bodies_Array for the type. For each entry/family we
9351 -- add an expression to the aggregate which is the initial value of
9352 -- this array. The array is declared after all protected subprograms.
9354 if Has_Entries (Prot_Typ) then
9355 Entries_Aggr := Make_Aggregate (Loc, Expressions => New_List);
9356 else
9357 Entries_Aggr := Empty;
9358 end if;
9360 -- Build two new procedure specifications for each protected subprogram;
9361 -- one to call from outside the object and one to call from inside.
9362 -- Build a barrier function and an entry body action procedure
9363 -- specification for each protected entry. Initialize the entry body
9364 -- array. If subprogram is flagged as eliminated, do not generate any
9365 -- internal operations.
9367 E_Count := 0;
9368 Comp := First (Visible_Declarations (Pdef));
9369 while Present (Comp) loop
9370 if Nkind (Comp) = N_Subprogram_Declaration then
9371 Sub :=
9372 Make_Subprogram_Declaration (Loc,
9373 Specification =>
9374 Build_Protected_Sub_Specification
9375 (Comp, Prot_Typ, Unprotected_Mode));
9377 Insert_After (Current_Node, Sub);
9378 Analyze (Sub);
9380 Set_Protected_Body_Subprogram
9381 (Defining_Unit_Name (Specification (Comp)),
9382 Defining_Unit_Name (Specification (Sub)));
9383 Check_Inlining (Defining_Unit_Name (Specification (Comp)));
9385 -- Make the protected version of the subprogram available for
9386 -- expansion of external calls.
9388 Current_Node := Sub;
9390 Sub :=
9391 Make_Subprogram_Declaration (Loc,
9392 Specification =>
9393 Build_Protected_Sub_Specification
9394 (Comp, Prot_Typ, Protected_Mode));
9396 Insert_After (Current_Node, Sub);
9397 Analyze (Sub);
9399 Current_Node := Sub;
9401 -- Generate an overriding primitive operation specification for
9402 -- this subprogram if the protected type implements an interface
9403 -- and Build_Wrapper_Spec did not generate its wrapper.
9405 if Ada_Version >= Ada_2005
9406 and then
9407 Present (Interfaces (Corresponding_Record_Type (Prot_Typ)))
9408 then
9409 declare
9410 Found : Boolean := False;
9411 Prim_Elmt : Elmt_Id;
9412 Prim_Op : Node_Id;
9414 begin
9415 Prim_Elmt :=
9416 First_Elmt
9417 (Primitive_Operations
9418 (Corresponding_Record_Type (Prot_Typ)));
9420 while Present (Prim_Elmt) loop
9421 Prim_Op := Node (Prim_Elmt);
9423 if Is_Primitive_Wrapper (Prim_Op)
9424 and then Wrapped_Entity (Prim_Op) =
9425 Defining_Entity (Specification (Comp))
9426 then
9427 Found := True;
9428 exit;
9429 end if;
9431 Next_Elmt (Prim_Elmt);
9432 end loop;
9434 if not Found then
9435 Sub :=
9436 Make_Subprogram_Declaration (Loc,
9437 Specification =>
9438 Build_Protected_Sub_Specification
9439 (Comp, Prot_Typ, Dispatching_Mode));
9441 Insert_After (Current_Node, Sub);
9442 Analyze (Sub);
9444 Current_Node := Sub;
9445 end if;
9446 end;
9447 end if;
9449 -- If a pragma Interrupt_Handler applies, build and add a call to
9450 -- Register_Interrupt_Handler to the freezing actions of the
9451 -- protected version (Current_Node) of the subprogram:
9453 -- system.interrupts.register_interrupt_handler
9454 -- (prot_procP'address);
9456 if not Restricted_Profile
9457 and then Is_Interrupt_Handler
9458 (Defining_Unit_Name (Specification (Comp)))
9459 then
9460 Register_Handler;
9461 end if;
9463 elsif Nkind (Comp) = N_Entry_Declaration then
9464 Expand_Entry_Declaration (Comp);
9465 end if;
9467 Next (Comp);
9468 end loop;
9470 -- If there are some private entry declarations, expand it as if they
9471 -- were visible entries.
9473 Comp := First (Private_Declarations (Pdef));
9474 while Present (Comp) loop
9475 if Nkind (Comp) = N_Entry_Declaration then
9476 Expand_Entry_Declaration (Comp);
9477 end if;
9479 Next (Comp);
9480 end loop;
9482 -- Create the declaration of an array object which contains the values
9483 -- of aspect/pragma Max_Queue_Length for all entries of the protected
9484 -- type. This object is later passed to the appropriate protected object
9485 -- initialization routine.
9487 if Has_Entries (Prot_Typ)
9488 and then Corresponding_Runtime_Package (Prot_Typ) =
9489 System_Tasking_Protected_Objects_Entries
9490 then
9491 declare
9492 Count : Int;
9493 Item : Entity_Id;
9494 Max_Vals : Node_Id;
9495 Maxes : List_Id;
9496 Maxes_Id : Entity_Id;
9497 Need_Array : Boolean := False;
9499 begin
9500 -- First check if there is any Max_Queue_Length pragma
9502 Item := First_Entity (Prot_Typ);
9503 while Present (Item) loop
9504 if Is_Entry (Item) and then Has_Max_Queue_Length (Item) then
9505 Need_Array := True;
9506 exit;
9507 end if;
9509 Next_Entity (Item);
9510 end loop;
9512 -- Gather the Max_Queue_Length values of all entries in a list. A
9513 -- value of zero indicates that the entry has no limitation on its
9514 -- queue length.
9516 if Need_Array then
9517 Count := 0;
9518 Item := First_Entity (Prot_Typ);
9519 Maxes := New_List;
9520 while Present (Item) loop
9521 if Is_Entry (Item) then
9522 Count := Count + 1;
9523 Append_To (Maxes,
9524 Make_Integer_Literal
9525 (Loc, Get_Max_Queue_Length (Item)));
9526 end if;
9528 Next_Entity (Item);
9529 end loop;
9531 -- Create the declaration of the array object. Generate:
9533 -- Maxes_Id : aliased constant
9534 -- Protected_Entry_Queue_Max_Array
9535 -- (1 .. Count) := (..., ...);
9537 Maxes_Id :=
9538 Make_Defining_Identifier (Loc,
9539 Chars => New_External_Name (Chars (Prot_Typ), 'B'));
9541 Max_Vals :=
9542 Make_Object_Declaration (Loc,
9543 Defining_Identifier => Maxes_Id,
9544 Aliased_Present => True,
9545 Constant_Present => True,
9546 Object_Definition =>
9547 Make_Subtype_Indication (Loc,
9548 Subtype_Mark =>
9549 New_Occurrence_Of
9550 (RTE (RE_Protected_Entry_Queue_Max_Array), Loc),
9551 Constraint =>
9552 Make_Index_Or_Discriminant_Constraint (Loc,
9553 Constraints => New_List (
9554 Make_Range (Loc,
9555 Make_Integer_Literal (Loc, 1),
9556 Make_Integer_Literal (Loc, Count))))),
9557 Expression => Make_Aggregate (Loc, Maxes));
9559 -- A pointer to this array will be placed in the corresponding
9560 -- record by its initialization procedure so this needs to be
9561 -- analyzed here.
9563 Insert_After (Current_Node, Max_Vals);
9564 Current_Node := Max_Vals;
9565 Analyze (Max_Vals);
9567 Set_Entry_Max_Queue_Lengths_Array (Prot_Typ, Maxes_Id);
9568 end if;
9569 end;
9570 end if;
9572 -- Emit declaration for Entry_Bodies_Array, now that the addresses of
9573 -- all protected subprograms have been collected.
9575 if Has_Entries (Prot_Typ) then
9576 Body_Id :=
9577 Make_Defining_Identifier (Sloc (Prot_Typ),
9578 Chars => New_External_Name (Chars (Prot_Typ), 'A'));
9580 case Corresponding_Runtime_Package (Prot_Typ) is
9581 when System_Tasking_Protected_Objects_Entries =>
9582 Expr := Entries_Aggr;
9583 Obj_Def :=
9584 Make_Subtype_Indication (Loc,
9585 Subtype_Mark =>
9586 New_Occurrence_Of
9587 (RTE (RE_Protected_Entry_Body_Array), Loc),
9588 Constraint =>
9589 Make_Index_Or_Discriminant_Constraint (Loc,
9590 Constraints => New_List (
9591 Make_Range (Loc,
9592 Make_Integer_Literal (Loc, 1),
9593 Make_Integer_Literal (Loc, E_Count)))));
9595 when System_Tasking_Protected_Objects_Single_Entry =>
9596 Expr := Remove_Head (Expressions (Entries_Aggr));
9597 Obj_Def := New_Occurrence_Of (RTE (RE_Entry_Body), Loc);
9599 when others =>
9600 raise Program_Error;
9601 end case;
9603 Body_Arr :=
9604 Make_Object_Declaration (Loc,
9605 Defining_Identifier => Body_Id,
9606 Aliased_Present => True,
9607 Constant_Present => True,
9608 Object_Definition => Obj_Def,
9609 Expression => Expr);
9611 -- A pointer to this array will be placed in the corresponding record
9612 -- by its initialization procedure so this needs to be analyzed here.
9614 Insert_After (Current_Node, Body_Arr);
9615 Current_Node := Body_Arr;
9616 Analyze (Body_Arr);
9618 Set_Entry_Bodies_Array (Prot_Typ, Body_Id);
9620 -- Finally, build the function that maps an entry index into the
9621 -- corresponding body. A pointer to this function is placed in each
9622 -- object of the type. Except for a ravenscar-like profile (no abort,
9623 -- no entry queue, 1 entry)
9625 if Corresponding_Runtime_Package (Prot_Typ) =
9626 System_Tasking_Protected_Objects_Entries
9627 then
9628 Sub :=
9629 Make_Subprogram_Declaration (Loc,
9630 Specification => Build_Find_Body_Index_Spec (Prot_Typ));
9632 Insert_After (Current_Node, Sub);
9633 Analyze (Sub);
9634 end if;
9635 end if;
9636 end Expand_N_Protected_Type_Declaration;
9638 --------------------------------
9639 -- Expand_N_Requeue_Statement --
9640 --------------------------------
9642 -- A nondispatching requeue statement is expanded into one of four GNARLI
9643 -- operations, depending on the source and destination (task or protected
9644 -- object). A dispatching requeue statement is expanded into a call to the
9645 -- predefined primitive _Disp_Requeue. In addition, code is generated to
9646 -- jump around the remainder of processing for the original entry and, if
9647 -- the destination is (different) protected object, to attempt to service
9648 -- it. The following illustrates the various cases:
9650 -- procedure entE
9651 -- (O : System.Address;
9652 -- P : System.Address;
9653 -- E : Protected_Entry_Index)
9654 -- is
9655 -- <discriminant renamings>
9656 -- <private object renamings>
9657 -- type poVP is access poV;
9658 -- _object : ptVP := ptVP!(O);
9660 -- begin
9661 -- begin
9662 -- <start of statement sequence for entry>
9664 -- -- Requeue from one protected entry body to another protected
9665 -- -- entry.
9667 -- Requeue_Protected_Entry (
9668 -- _object._object'Access,
9669 -- new._object'Access,
9670 -- E,
9671 -- Abort_Present);
9672 -- return;
9674 -- <some more of the statement sequence for entry>
9676 -- -- Requeue from an entry body to a task entry
9678 -- Requeue_Protected_To_Task_Entry (
9679 -- New._task_id,
9680 -- E,
9681 -- Abort_Present);
9682 -- return;
9684 -- <rest of statement sequence for entry>
9685 -- Complete_Entry_Body (_object._object);
9687 -- exception
9688 -- when all others =>
9689 -- Exceptional_Complete_Entry_Body (
9690 -- _object._object, Get_GNAT_Exception);
9691 -- end;
9692 -- end entE;
9694 -- Requeue of a task entry call to a task entry
9696 -- Accept_Call (E, Ann);
9697 -- <start of statement sequence for accept statement>
9698 -- Requeue_Task_Entry (New._task_id, E, Abort_Present);
9699 -- goto Lnn;
9700 -- <rest of statement sequence for accept statement>
9701 -- <<Lnn>>
9702 -- Complete_Rendezvous;
9704 -- exception
9705 -- when all others =>
9706 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9708 -- Requeue of a task entry call to a protected entry
9710 -- Accept_Call (E, Ann);
9711 -- <start of statement sequence for accept statement>
9712 -- Requeue_Task_To_Protected_Entry (
9713 -- new._object'Access,
9714 -- E,
9715 -- Abort_Present);
9716 -- newS (new, Pnn);
9717 -- goto Lnn;
9718 -- <rest of statement sequence for accept statement>
9719 -- <<Lnn>>
9720 -- Complete_Rendezvous;
9722 -- exception
9723 -- when all others =>
9724 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9726 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9727 -- marked by pragma Implemented (XXX, By_Entry).
9729 -- The requeue is inside a protected entry:
9731 -- procedure entE
9732 -- (O : System.Address;
9733 -- P : System.Address;
9734 -- E : Protected_Entry_Index)
9735 -- is
9736 -- <discriminant renamings>
9737 -- <private object renamings>
9738 -- type poVP is access poV;
9739 -- _object : ptVP := ptVP!(O);
9741 -- begin
9742 -- begin
9743 -- <start of statement sequence for entry>
9745 -- _Disp_Requeue
9746 -- (<interface class-wide object>,
9747 -- True,
9748 -- _object'Address,
9749 -- Ada.Tags.Get_Offset_Index
9750 -- (Tag (_object),
9751 -- <interface dispatch table index of target entry>),
9752 -- Abort_Present);
9753 -- return;
9755 -- <rest of statement sequence for entry>
9756 -- Complete_Entry_Body (_object._object);
9758 -- exception
9759 -- when all others =>
9760 -- Exceptional_Complete_Entry_Body (
9761 -- _object._object, Get_GNAT_Exception);
9762 -- end;
9763 -- end entE;
9765 -- The requeue is inside a task entry:
9767 -- Accept_Call (E, Ann);
9768 -- <start of statement sequence for accept statement>
9769 -- _Disp_Requeue
9770 -- (<interface class-wide object>,
9771 -- False,
9772 -- null,
9773 -- Ada.Tags.Get_Offset_Index
9774 -- (Tag (_object),
9775 -- <interface dispatch table index of target entrt>),
9776 -- Abort_Present);
9777 -- newS (new, Pnn);
9778 -- goto Lnn;
9779 -- <rest of statement sequence for accept statement>
9780 -- <<Lnn>>
9781 -- Complete_Rendezvous;
9783 -- exception
9784 -- when all others =>
9785 -- Exceptional_Complete_Rendezvous (Get_GNAT_Exception);
9787 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9788 -- marked by pragma Implemented (XXX, By_Protected_Procedure). The requeue
9789 -- statement is replaced by a dispatching call with actual parameters taken
9790 -- from the inner-most accept statement or entry body.
9792 -- Target.Primitive (Param1, ..., ParamN);
9794 -- Ada 2012 (AI05-0030): Dispatching requeue to an interface primitive
9795 -- marked by pragma Implemented (XXX, By_Any | Optional) or not marked
9796 -- at all.
9798 -- declare
9799 -- S : constant Offset_Index :=
9800 -- Get_Offset_Index (Tag (Concval), DT_Position (Ename));
9801 -- C : constant Prim_Op_Kind := Get_Prim_Op_Kind (Tag (Concval), S);
9803 -- begin
9804 -- if C = POK_Protected_Entry
9805 -- or else C = POK_Task_Entry
9806 -- then
9807 -- <statements for dispatching requeue>
9809 -- elsif C = POK_Protected_Procedure then
9810 -- <dispatching call equivalent>
9812 -- else
9813 -- raise Program_Error;
9814 -- end if;
9815 -- end;
9817 procedure Expand_N_Requeue_Statement (N : Node_Id) is
9818 Loc : constant Source_Ptr := Sloc (N);
9819 Conc_Typ : Entity_Id;
9820 Concval : Node_Id;
9821 Ename : Node_Id;
9822 Enc_Subp : Entity_Id;
9823 Index : Node_Id;
9824 Old_Typ : Entity_Id;
9826 function Build_Dispatching_Call_Equivalent return Node_Id;
9827 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9828 -- the form Concval.Ename. It is statically known that Ename is allowed
9829 -- to be implemented by a protected procedure. Create a dispatching call
9830 -- equivalent of Concval.Ename taking the actual parameters from the
9831 -- inner-most accept statement or entry body.
9833 function Build_Dispatching_Requeue return Node_Id;
9834 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9835 -- the form Concval.Ename. It is statically known that Ename is allowed
9836 -- to be implemented by a protected or a task entry. Create a call to
9837 -- primitive _Disp_Requeue which handles the low-level actions.
9839 function Build_Dispatching_Requeue_To_Any return Node_Id;
9840 -- Ada 2012 (AI05-0030): N denotes a dispatching requeue statement of
9841 -- the form Concval.Ename. Ename is either marked by pragma Implemented
9842 -- (XXX, By_Any | Optional) or not marked at all. Create a block which
9843 -- determines at runtime whether Ename denotes an entry or a procedure
9844 -- and perform the appropriate kind of dispatching select.
9846 function Build_Normal_Requeue return Node_Id;
9847 -- N denotes a nondispatching requeue statement to either a task or a
9848 -- protected entry. Build the appropriate runtime call to perform the
9849 -- action.
9851 function Build_Skip_Statement (Search : Node_Id) return Node_Id;
9852 -- For a protected entry, create a return statement to skip the rest of
9853 -- the entry body. Otherwise, create a goto statement to skip the rest
9854 -- of a task accept statement. The lookup for the enclosing entry body
9855 -- or accept statement starts from Search.
9857 ---------------------------------------
9858 -- Build_Dispatching_Call_Equivalent --
9859 ---------------------------------------
9861 function Build_Dispatching_Call_Equivalent return Node_Id is
9862 Call_Ent : constant Entity_Id := Entity (Ename);
9863 Obj : constant Node_Id := Original_Node (Concval);
9864 Acc_Ent : Node_Id;
9865 Actuals : List_Id;
9866 Formal : Node_Id;
9867 Formals : List_Id;
9869 begin
9870 -- Climb the parent chain looking for the inner-most entry body or
9871 -- accept statement.
9873 Acc_Ent := N;
9874 while Present (Acc_Ent)
9875 and then Nkind (Acc_Ent) not in N_Accept_Statement | N_Entry_Body
9876 loop
9877 Acc_Ent := Parent (Acc_Ent);
9878 end loop;
9880 -- A requeue statement should be housed inside an entry body or an
9881 -- accept statement at some level. If this is not the case, then the
9882 -- tree is malformed.
9884 pragma Assert (Present (Acc_Ent));
9886 -- Recover the list of formal parameters
9888 if Nkind (Acc_Ent) = N_Entry_Body then
9889 Acc_Ent := Entry_Body_Formal_Part (Acc_Ent);
9890 end if;
9892 Formals := Parameter_Specifications (Acc_Ent);
9894 -- Create the actual parameters for the dispatching call. These are
9895 -- simply copies of the entry body or accept statement formals in the
9896 -- same order as they appear.
9898 Actuals := No_List;
9900 if Present (Formals) then
9901 Actuals := New_List;
9902 Formal := First (Formals);
9903 while Present (Formal) loop
9904 Append_To (Actuals,
9905 Make_Identifier (Loc, Chars (Defining_Identifier (Formal))));
9906 Next (Formal);
9907 end loop;
9908 end if;
9910 -- Generate:
9911 -- Obj.Call_Ent (Actuals);
9913 return
9914 Make_Procedure_Call_Statement (Loc,
9915 Name =>
9916 Make_Selected_Component (Loc,
9917 Prefix => Make_Identifier (Loc, Chars (Obj)),
9918 Selector_Name => Make_Identifier (Loc, Chars (Call_Ent))),
9920 Parameter_Associations => Actuals);
9921 end Build_Dispatching_Call_Equivalent;
9923 -------------------------------
9924 -- Build_Dispatching_Requeue --
9925 -------------------------------
9927 function Build_Dispatching_Requeue return Node_Id is
9928 Params : constant List_Id := New_List;
9930 begin
9931 -- Process the "with abort" parameter
9933 Prepend_To (Params,
9934 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
9936 -- Process the entry wrapper's position in the primary dispatch
9937 -- table parameter. Generate:
9939 -- Ada.Tags.Get_Entry_Index
9940 -- (T => To_Tag_Ptr (Obj'Address).all,
9941 -- Position =>
9942 -- Ada.Tags.Get_Offset_Index
9943 -- (Ada.Tags.Tag (Concval),
9944 -- <interface dispatch table position of Ename>));
9946 -- Note that Obj'Address is recursively expanded into a call to
9947 -- Base_Address (Obj).
9949 if Tagged_Type_Expansion then
9950 Prepend_To (Params,
9951 Make_Function_Call (Loc,
9952 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9953 Parameter_Associations => New_List (
9955 Make_Explicit_Dereference (Loc,
9956 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
9957 Make_Attribute_Reference (Loc,
9958 Prefix => New_Copy_Tree (Concval),
9959 Attribute_Name => Name_Address))),
9961 Make_Function_Call (Loc,
9962 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9963 Parameter_Associations => New_List (
9964 Unchecked_Convert_To (RTE (RE_Tag), Concval),
9965 Make_Integer_Literal (Loc,
9966 DT_Position (Entity (Ename))))))));
9968 -- VM targets
9970 else
9971 Prepend_To (Params,
9972 Make_Function_Call (Loc,
9973 Name => New_Occurrence_Of (RTE (RE_Get_Entry_Index), Loc),
9974 Parameter_Associations => New_List (
9976 Make_Attribute_Reference (Loc,
9977 Prefix => Concval,
9978 Attribute_Name => Name_Tag),
9980 Make_Function_Call (Loc,
9981 Name => New_Occurrence_Of (RTE (RE_Get_Offset_Index), Loc),
9983 Parameter_Associations => New_List (
9985 -- Obj_Tag
9987 Make_Attribute_Reference (Loc,
9988 Prefix => Concval,
9989 Attribute_Name => Name_Tag),
9991 -- Tag_Typ
9993 Make_Attribute_Reference (Loc,
9994 Prefix => New_Occurrence_Of (Etype (Concval), Loc),
9995 Attribute_Name => Name_Tag),
9997 -- Position
9999 Make_Integer_Literal (Loc,
10000 DT_Position (Entity (Ename))))))));
10001 end if;
10003 -- Specific actuals for protected to XXX requeue
10005 if Is_Protected_Type (Old_Typ) then
10006 Prepend_To (Params,
10007 Make_Attribute_Reference (Loc, -- _object'Address
10008 Prefix =>
10009 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10010 Attribute_Name => Name_Address));
10012 Prepend_To (Params, -- True
10013 New_Occurrence_Of (Standard_True, Loc));
10015 -- Specific actuals for task to XXX requeue
10017 else
10018 pragma Assert (Is_Task_Type (Old_Typ));
10020 Prepend_To (Params, -- null
10021 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
10023 Prepend_To (Params, -- False
10024 New_Occurrence_Of (Standard_False, Loc));
10025 end if;
10027 -- Add the object parameter
10029 Prepend_To (Params, New_Copy_Tree (Concval));
10031 -- Generate:
10032 -- _Disp_Requeue (<Params>);
10034 -- Find entity for Disp_Requeue operation, which belongs to
10035 -- the type and may not be directly visible.
10037 declare
10038 Elmt : Elmt_Id;
10039 Op : Entity_Id := Empty;
10041 begin
10042 Elmt := First_Elmt (Primitive_Operations (Etype (Conc_Typ)));
10043 while Present (Elmt) loop
10044 Op := Node (Elmt);
10045 exit when Chars (Op) = Name_uDisp_Requeue;
10046 Next_Elmt (Elmt);
10047 end loop;
10049 pragma Assert (Present (Op));
10051 return
10052 Make_Procedure_Call_Statement (Loc,
10053 Name => New_Occurrence_Of (Op, Loc),
10054 Parameter_Associations => Params);
10055 end;
10056 end Build_Dispatching_Requeue;
10058 --------------------------------------
10059 -- Build_Dispatching_Requeue_To_Any --
10060 --------------------------------------
10062 function Build_Dispatching_Requeue_To_Any return Node_Id is
10063 Call_Ent : constant Entity_Id := Entity (Ename);
10064 Obj : constant Node_Id := Original_Node (Concval);
10065 Skip : constant Node_Id := Build_Skip_Statement (N);
10066 C : Entity_Id;
10067 Decls : List_Id;
10068 S : Entity_Id;
10069 Stmts : List_Id;
10071 begin
10072 Decls := New_List;
10073 Stmts := New_List;
10075 -- Dispatch table slot processing, generate:
10076 -- S : Integer;
10078 S := Build_S (Loc, Decls);
10080 -- Call kind processing, generate:
10081 -- C : Ada.Tags.Prim_Op_Kind;
10083 C := Build_C (Loc, Decls);
10085 -- Generate:
10086 -- S := Ada.Tags.Get_Offset_Index
10087 -- (Ada.Tags.Tag (Obj), DT_Position (Call_Ent));
10089 Append_To (Stmts, Build_S_Assignment (Loc, S, Obj, Call_Ent));
10091 -- Generate:
10092 -- _Disp_Get_Prim_Op_Kind (Obj, S, C);
10094 Append_To (Stmts,
10095 Make_Procedure_Call_Statement (Loc,
10096 Name =>
10097 New_Occurrence_Of (
10098 Find_Prim_Op (Etype (Etype (Obj)),
10099 Name_uDisp_Get_Prim_Op_Kind),
10100 Loc),
10101 Parameter_Associations => New_List (
10102 New_Copy_Tree (Obj),
10103 New_Occurrence_Of (S, Loc),
10104 New_Occurrence_Of (C, Loc))));
10106 Append_To (Stmts,
10108 -- if C = POK_Protected_Entry
10109 -- or else C = POK_Task_Entry
10110 -- then
10112 Make_Implicit_If_Statement (N,
10113 Condition =>
10114 Make_Op_Or (Loc,
10115 Left_Opnd =>
10116 Make_Op_Eq (Loc,
10117 Left_Opnd =>
10118 New_Occurrence_Of (C, Loc),
10119 Right_Opnd =>
10120 New_Occurrence_Of (RTE (RE_POK_Protected_Entry), Loc)),
10122 Right_Opnd =>
10123 Make_Op_Eq (Loc,
10124 Left_Opnd =>
10125 New_Occurrence_Of (C, Loc),
10126 Right_Opnd =>
10127 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
10129 -- Dispatching requeue equivalent
10131 Then_Statements => New_List (
10132 Build_Dispatching_Requeue,
10133 Skip),
10135 -- elsif C = POK_Protected_Procedure then
10137 Elsif_Parts => New_List (
10138 Make_Elsif_Part (Loc,
10139 Condition =>
10140 Make_Op_Eq (Loc,
10141 Left_Opnd =>
10142 New_Occurrence_Of (C, Loc),
10143 Right_Opnd =>
10144 New_Occurrence_Of (
10145 RTE (RE_POK_Protected_Procedure), Loc)),
10147 -- Dispatching call equivalent
10149 Then_Statements => New_List (
10150 Build_Dispatching_Call_Equivalent))),
10152 -- else
10153 -- raise Program_Error;
10154 -- end if;
10156 Else_Statements => New_List (
10157 Make_Raise_Program_Error (Loc,
10158 Reason => PE_Explicit_Raise))));
10160 -- Wrap everything into a block
10162 return
10163 Make_Block_Statement (Loc,
10164 Declarations => Decls,
10165 Handled_Statement_Sequence =>
10166 Make_Handled_Sequence_Of_Statements (Loc,
10167 Statements => Stmts));
10168 end Build_Dispatching_Requeue_To_Any;
10170 --------------------------
10171 -- Build_Normal_Requeue --
10172 --------------------------
10174 function Build_Normal_Requeue return Node_Id is
10175 Params : constant List_Id := New_List;
10176 Param : Node_Id;
10177 RT_Call : Node_Id;
10179 begin
10180 -- Process the "with abort" parameter
10182 Prepend_To (Params,
10183 New_Occurrence_Of (Boolean_Literals (Abort_Present (N)), Loc));
10185 -- Add the index expression to the parameters. It is common among all
10186 -- four cases.
10188 Prepend_To (Params,
10189 Entry_Index_Expression (Loc, Entity (Ename), Index, Conc_Typ));
10191 if Is_Protected_Type (Old_Typ) then
10192 declare
10193 Self_Param : Node_Id;
10195 begin
10196 Self_Param :=
10197 Make_Attribute_Reference (Loc,
10198 Prefix =>
10199 Concurrent_Ref (New_Occurrence_Of (Old_Typ, Loc)),
10200 Attribute_Name =>
10201 Name_Unchecked_Access);
10203 -- Protected to protected requeue
10205 if Is_Protected_Type (Conc_Typ) then
10206 RT_Call :=
10207 New_Occurrence_Of (
10208 RTE (RE_Requeue_Protected_Entry), Loc);
10210 Param :=
10211 Make_Attribute_Reference (Loc,
10212 Prefix =>
10213 Concurrent_Ref (Concval),
10214 Attribute_Name =>
10215 Name_Unchecked_Access);
10217 -- Protected to task requeue
10219 else pragma Assert (Is_Task_Type (Conc_Typ));
10220 RT_Call :=
10221 New_Occurrence_Of (
10222 RTE (RE_Requeue_Protected_To_Task_Entry), Loc);
10224 Param := Concurrent_Ref (Concval);
10225 end if;
10227 Prepend_To (Params, Param);
10228 Prepend_To (Params, Self_Param);
10229 end;
10231 else pragma Assert (Is_Task_Type (Old_Typ));
10233 -- Task to protected requeue
10235 if Is_Protected_Type (Conc_Typ) then
10236 RT_Call :=
10237 New_Occurrence_Of (
10238 RTE (RE_Requeue_Task_To_Protected_Entry), Loc);
10240 Param :=
10241 Make_Attribute_Reference (Loc,
10242 Prefix =>
10243 Concurrent_Ref (Concval),
10244 Attribute_Name =>
10245 Name_Unchecked_Access);
10247 -- Task to task requeue
10249 else pragma Assert (Is_Task_Type (Conc_Typ));
10250 RT_Call :=
10251 New_Occurrence_Of (RTE (RE_Requeue_Task_Entry), Loc);
10253 Param := Concurrent_Ref (Concval);
10254 end if;
10256 Prepend_To (Params, Param);
10257 end if;
10259 return
10260 Make_Procedure_Call_Statement (Loc,
10261 Name => RT_Call,
10262 Parameter_Associations => Params);
10263 end Build_Normal_Requeue;
10265 --------------------------
10266 -- Build_Skip_Statement --
10267 --------------------------
10269 function Build_Skip_Statement (Search : Node_Id) return Node_Id is
10270 Skip_Stmt : Node_Id;
10272 begin
10273 -- Build a return statement to skip the rest of the entire body
10275 if Is_Protected_Type (Old_Typ) then
10276 Skip_Stmt := Make_Simple_Return_Statement (Loc);
10278 -- If the requeue is within a task, find the end label of the
10279 -- enclosing accept statement and create a goto statement to it.
10281 else
10282 declare
10283 Acc : Node_Id;
10284 Label : Node_Id;
10286 begin
10287 -- Climb the parent chain looking for the enclosing accept
10288 -- statement.
10290 Acc := Parent (Search);
10291 while Present (Acc)
10292 and then Nkind (Acc) /= N_Accept_Statement
10293 loop
10294 Acc := Parent (Acc);
10295 end loop;
10297 -- The last statement is the second label used for completing
10298 -- the rendezvous the usual way. The label we are looking for
10299 -- is right before it.
10301 Label :=
10302 Prev (Last (Statements (Handled_Statement_Sequence (Acc))));
10304 pragma Assert (Nkind (Label) = N_Label);
10306 -- Generate a goto statement to skip the rest of the accept
10308 Skip_Stmt :=
10309 Make_Goto_Statement (Loc,
10310 Name =>
10311 New_Occurrence_Of (Entity (Identifier (Label)), Loc));
10312 end;
10313 end if;
10315 Set_Analyzed (Skip_Stmt);
10317 return Skip_Stmt;
10318 end Build_Skip_Statement;
10320 -- Start of processing for Expand_N_Requeue_Statement
10322 begin
10323 -- Extract the components of the entry call
10325 Extract_Entry (N, Concval, Ename, Index);
10326 Conc_Typ := Etype (Concval);
10328 -- Examine the scope stack in order to find nearest enclosing concurrent
10329 -- type. This will constitute our invocation source.
10331 Old_Typ := Current_Scope;
10332 while Present (Old_Typ)
10333 and then not Is_Concurrent_Type (Old_Typ)
10334 loop
10335 Old_Typ := Scope (Old_Typ);
10336 end loop;
10338 -- Obtain the innermost enclosing callable construct for use in
10339 -- generating a dynamic accessibility check.
10341 Enc_Subp := Current_Scope;
10343 if Ekind (Enc_Subp) not in Entry_Kind | Subprogram_Kind then
10344 Enc_Subp := Enclosing_Subprogram (Enc_Subp);
10345 end if;
10347 -- Generate a dynamic accessibility check on the target object
10349 Insert_Before_And_Analyze (N,
10350 Make_Raise_Program_Error (Loc,
10351 Condition =>
10352 Make_Op_Gt (Loc,
10353 Left_Opnd => Accessibility_Level (Name (N), Dynamic_Level),
10354 Right_Opnd => Make_Integer_Literal (Loc,
10355 Scope_Depth (Enc_Subp))),
10356 Reason => PE_Accessibility_Check_Failed));
10358 -- Ada 2012 (AI05-0030): We have a dispatching requeue of the form
10359 -- Concval.Ename where the type of Concval is class-wide concurrent
10360 -- interface.
10362 if Ada_Version >= Ada_2012
10363 and then Present (Concval)
10364 and then Is_Class_Wide_Type (Conc_Typ)
10365 and then Is_Concurrent_Interface (Conc_Typ)
10366 then
10367 declare
10368 Has_Impl : Boolean := False;
10369 Impl_Kind : Name_Id := No_Name;
10371 begin
10372 -- Check whether the Ename is flagged by pragma Implemented
10374 if Has_Rep_Pragma (Entity (Ename), Name_Implemented) then
10375 Has_Impl := True;
10376 Impl_Kind := Implementation_Kind (Entity (Ename));
10377 end if;
10379 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10380 -- an entry. Create a call to predefined primitive _Disp_Requeue.
10382 if Has_Impl and then Impl_Kind = Name_By_Entry then
10383 Rewrite (N, Build_Dispatching_Requeue);
10384 Analyze (N);
10385 Insert_After (N, Build_Skip_Statement (N));
10387 -- The procedure_or_entry_NAME is guaranteed to be overridden by
10388 -- a protected procedure. In this case the requeue is transformed
10389 -- into a dispatching call.
10391 elsif Has_Impl
10392 and then Impl_Kind = Name_By_Protected_Procedure
10393 then
10394 Rewrite (N, Build_Dispatching_Call_Equivalent);
10395 Analyze (N);
10397 -- The procedure_or_entry_NAME's implementation kind is either
10398 -- By_Any, Optional, or pragma Implemented was not applied at all.
10399 -- In this case a runtime test determines whether Ename denotes an
10400 -- entry or a protected procedure and performs the appropriate
10401 -- call.
10403 else
10404 Rewrite (N, Build_Dispatching_Requeue_To_Any);
10405 Analyze (N);
10406 end if;
10407 end;
10409 -- Processing for regular (nondispatching) requeues
10411 else
10412 Rewrite (N, Build_Normal_Requeue);
10413 Analyze (N);
10414 Insert_After (N, Build_Skip_Statement (N));
10415 end if;
10416 end Expand_N_Requeue_Statement;
10418 -------------------------------
10419 -- Expand_N_Selective_Accept --
10420 -------------------------------
10422 procedure Expand_N_Selective_Accept (N : Node_Id) is
10423 Loc : constant Source_Ptr := Sloc (N);
10424 Alts : constant List_Id := Select_Alternatives (N);
10426 -- Note: in the below declarations a lot of new lists are allocated
10427 -- unconditionally which may well not end up being used. That's not
10428 -- a good idea since it wastes space gratuitously ???
10430 Accept_Case : List_Id;
10431 Accept_List : constant List_Id := New_List;
10433 Alt : Node_Id;
10434 Alt_List : constant List_Id := New_List;
10435 Alt_Stats : List_Id;
10436 Ann : Entity_Id := Empty;
10438 Check_Guard : Boolean := True;
10440 Decls : constant List_Id := New_List;
10441 Stats : constant List_Id := New_List;
10442 Body_List : constant List_Id := New_List;
10443 Trailing_List : constant List_Id := New_List;
10445 Choices : List_Id;
10446 Else_Present : Boolean := False;
10447 Terminate_Alt : Node_Id := Empty;
10448 Select_Mode : Node_Id;
10450 Delay_Case : List_Id;
10451 Delay_Count : Integer := 0;
10452 Delay_Val : Entity_Id;
10453 Delay_Index : Entity_Id;
10454 Delay_Min : Entity_Id;
10455 Delay_Num : Pos := 1;
10456 Delay_Alt_List : List_Id := New_List;
10457 Delay_List : constant List_Id := New_List;
10458 D : Entity_Id;
10459 M : Entity_Id;
10461 First_Delay : Boolean := True;
10462 Guard_Open : Entity_Id;
10464 End_Lab : Node_Id;
10465 Index : Pos := 1;
10466 Lab : Node_Id;
10467 Num_Alts : Nat;
10468 Num_Accept : Nat := 0;
10469 Proc : Node_Id;
10470 Time_Type : Entity_Id := Empty;
10471 Select_Call : Node_Id;
10473 Qnam : constant Entity_Id :=
10474 Make_Defining_Identifier (Loc, New_External_Name ('S', 0));
10476 Xnam : constant Entity_Id :=
10477 Make_Defining_Identifier (Loc, New_External_Name ('J', 1));
10479 -----------------------
10480 -- Local subprograms --
10481 -----------------------
10483 function Accept_Or_Raise return List_Id;
10484 -- For the rare case where delay alternatives all have guards, and
10485 -- all of them are closed, it is still possible that there were open
10486 -- accept alternatives with no callers. We must reexamine the
10487 -- Accept_List, and execute a selective wait with no else if some
10488 -- accept is open. If none, we raise program_error.
10490 procedure Add_Accept (Alt : Node_Id);
10491 -- Process a single accept statement in a select alternative. Build
10492 -- procedure for body of accept, and add entry to dispatch table with
10493 -- expression for guard, in preparation for call to run time select.
10495 function Make_And_Declare_Label (Num : Int) return Node_Id;
10496 -- Manufacture a label using Num as a serial number and declare it.
10497 -- The declaration is appended to Decls. The label marks the trailing
10498 -- statements of an accept or delay alternative.
10500 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id;
10501 -- Build call to Selective_Wait runtime routine
10503 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int);
10504 -- Add code to compare value of delay with previous values, and
10505 -- generate case entry for trailing statements.
10507 procedure Process_Accept_Alternative
10508 (Alt : Node_Id;
10509 Index : Int;
10510 Proc : Node_Id);
10511 -- Add code to call corresponding procedure, and branch to
10512 -- trailing statements, if any.
10514 ---------------------
10515 -- Accept_Or_Raise --
10516 ---------------------
10518 function Accept_Or_Raise return List_Id is
10519 Cond : Node_Id;
10520 Stats : List_Id;
10521 J : constant Entity_Id := Make_Temporary (Loc, 'J');
10523 begin
10524 -- We generate the following:
10526 -- for J in q'range loop
10527 -- if q(J).S /=null_task_entry then
10528 -- selective_wait (simple_mode,...);
10529 -- done := True;
10530 -- exit;
10531 -- end if;
10532 -- end loop;
10534 -- if no rendez_vous then
10535 -- raise program_error;
10536 -- end if;
10538 -- Note that the code needs to know that the selector name
10539 -- in an Accept_Alternative is named S.
10541 Cond := Make_Op_Ne (Loc,
10542 Left_Opnd =>
10543 Make_Selected_Component (Loc,
10544 Prefix =>
10545 Make_Indexed_Component (Loc,
10546 Prefix => New_Occurrence_Of (Qnam, Loc),
10547 Expressions => New_List (New_Occurrence_Of (J, Loc))),
10548 Selector_Name => Make_Identifier (Loc, Name_S)),
10549 Right_Opnd =>
10550 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Loc));
10552 Stats := New_List (
10553 Make_Implicit_Loop_Statement (N,
10554 Iteration_Scheme =>
10555 Make_Iteration_Scheme (Loc,
10556 Loop_Parameter_Specification =>
10557 Make_Loop_Parameter_Specification (Loc,
10558 Defining_Identifier => J,
10559 Discrete_Subtype_Definition =>
10560 Make_Attribute_Reference (Loc,
10561 Prefix => New_Occurrence_Of (Qnam, Loc),
10562 Attribute_Name => Name_Range,
10563 Expressions => New_List (
10564 Make_Integer_Literal (Loc, 1))))),
10566 Statements => New_List (
10567 Make_Implicit_If_Statement (N,
10568 Condition => Cond,
10569 Then_Statements => New_List (
10570 Make_Select_Call (
10571 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)),
10572 Make_Exit_Statement (Loc))))));
10574 Append_To (Stats,
10575 Make_Raise_Program_Error (Loc,
10576 Condition => Make_Op_Eq (Loc,
10577 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
10578 Right_Opnd =>
10579 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
10580 Reason => PE_All_Guards_Closed));
10582 return Stats;
10583 end Accept_Or_Raise;
10585 ----------------
10586 -- Add_Accept --
10587 ----------------
10589 procedure Add_Accept (Alt : Node_Id) is
10590 Acc_Stm : constant Node_Id := Accept_Statement (Alt);
10591 Ename : constant Node_Id := Entry_Direct_Name (Acc_Stm);
10592 Eloc : constant Source_Ptr := Sloc (Ename);
10593 Eent : constant Entity_Id := Entity (Ename);
10594 Index : constant Node_Id := Entry_Index (Acc_Stm);
10596 Call : Node_Id;
10597 Expr : Node_Id;
10598 Null_Body : Node_Id;
10599 PB_Ent : Entity_Id;
10600 Proc_Body : Node_Id;
10602 -- Start of processing for Add_Accept
10604 begin
10605 if No (Ann) then
10606 Ann := Node (Last_Elmt (Accept_Address (Eent)));
10607 end if;
10609 if Present (Condition (Alt)) then
10610 Expr :=
10611 Make_If_Expression (Eloc, New_List (
10612 Condition (Alt),
10613 Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent)),
10614 New_Occurrence_Of (RTE (RE_Null_Task_Entry), Eloc)));
10615 else
10616 Expr := Entry_Index_Expression (Eloc, Eent, Index, Scope (Eent));
10617 end if;
10619 if Present (Handled_Statement_Sequence (Accept_Statement (Alt))) then
10620 Null_Body := New_Occurrence_Of (Standard_False, Eloc);
10622 -- Always add call to Abort_Undefer when generating code, since
10623 -- this is what the runtime expects (abort deferred in
10624 -- Selective_Wait). In CodePeer mode this only confuses the
10625 -- analysis with unknown calls, so don't do it.
10627 if not CodePeer_Mode then
10628 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
10629 Insert_Before
10630 (First (Statements (Handled_Statement_Sequence
10631 (Accept_Statement (Alt)))),
10632 Call);
10633 Analyze (Call);
10634 end if;
10636 PB_Ent :=
10637 Make_Defining_Identifier (Eloc,
10638 New_External_Name (Chars (Ename), 'A', Num_Accept));
10640 -- Link the acceptor to the original receiving entry
10642 Mutate_Ekind (PB_Ent, E_Procedure);
10643 Set_Receiving_Entry (PB_Ent, Eent);
10645 if Comes_From_Source (Alt) then
10646 Set_Debug_Info_Needed (PB_Ent);
10647 end if;
10649 Proc_Body :=
10650 Make_Subprogram_Body (Eloc,
10651 Specification =>
10652 Make_Procedure_Specification (Eloc,
10653 Defining_Unit_Name => PB_Ent),
10654 Declarations => Declarations (Acc_Stm),
10655 Handled_Statement_Sequence =>
10656 Build_Accept_Body (Accept_Statement (Alt)));
10658 Reset_Scopes_To (Proc_Body, PB_Ent);
10660 -- During the analysis of the body of the accept statement, any
10661 -- zero cost exception handler records were collected in the
10662 -- Accept_Handler_Records field of the N_Accept_Alternative node.
10663 -- This is where we move them to where they belong, namely the
10664 -- newly created procedure.
10666 Set_Handler_Records (PB_Ent, Accept_Handler_Records (Alt));
10667 Append (Proc_Body, Body_List);
10669 else
10670 Null_Body := New_Occurrence_Of (Standard_True, Eloc);
10672 -- if accept statement has declarations, insert above, given that
10673 -- we are not creating a body for the accept.
10675 if Present (Declarations (Acc_Stm)) then
10676 Insert_Actions (N, Declarations (Acc_Stm));
10677 end if;
10678 end if;
10680 Append_To (Accept_List,
10681 Make_Aggregate (Eloc, Expressions => New_List (Null_Body, Expr)));
10683 Num_Accept := Num_Accept + 1;
10684 end Add_Accept;
10686 ----------------------------
10687 -- Make_And_Declare_Label --
10688 ----------------------------
10690 function Make_And_Declare_Label (Num : Int) return Node_Id is
10691 Lab_Id : Node_Id;
10693 begin
10694 Lab_Id := Make_Identifier (Loc, New_External_Name ('L', Num));
10695 Lab :=
10696 Make_Label (Loc, Lab_Id);
10698 Append_To (Decls,
10699 Make_Implicit_Label_Declaration (Loc,
10700 Defining_Identifier =>
10701 Make_Defining_Identifier (Loc, Chars (Lab_Id)),
10702 Label_Construct => Lab));
10704 return Lab;
10705 end Make_And_Declare_Label;
10707 ----------------------
10708 -- Make_Select_Call --
10709 ----------------------
10711 function Make_Select_Call (Select_Mode : Entity_Id) return Node_Id is
10712 Params : constant List_Id := New_List;
10714 begin
10715 Append_To (Params,
10716 Make_Attribute_Reference (Loc,
10717 Prefix => New_Occurrence_Of (Qnam, Loc),
10718 Attribute_Name => Name_Unchecked_Access));
10719 Append_To (Params, Select_Mode);
10720 Append_To (Params, New_Occurrence_Of (Ann, Loc));
10721 Append_To (Params, New_Occurrence_Of (Xnam, Loc));
10723 return
10724 Make_Procedure_Call_Statement (Loc,
10725 Name => New_Occurrence_Of (RTE (RE_Selective_Wait), Loc),
10726 Parameter_Associations => Params);
10727 end Make_Select_Call;
10729 --------------------------------
10730 -- Process_Accept_Alternative --
10731 --------------------------------
10733 procedure Process_Accept_Alternative
10734 (Alt : Node_Id;
10735 Index : Int;
10736 Proc : Node_Id)
10738 Astmt : constant Node_Id := Accept_Statement (Alt);
10739 Alt_Stats : List_Id;
10741 begin
10742 Adjust_Condition (Condition (Alt));
10744 -- Accept with body
10746 if Present (Handled_Statement_Sequence (Astmt)) then
10747 Alt_Stats :=
10748 New_List (
10749 Make_Procedure_Call_Statement (Sloc (Proc),
10750 Name =>
10751 New_Occurrence_Of
10752 (Defining_Unit_Name (Specification (Proc)),
10753 Sloc (Proc))));
10755 -- Accept with no body (followed by trailing statements)
10757 else
10758 declare
10759 Entry_Id : constant Entity_Id :=
10760 Entity (Entry_Direct_Name (Accept_Statement (Alt)));
10761 begin
10762 -- Ada 2022 (AI12-0279)
10764 if Has_Yield_Aspect (Entry_Id)
10765 and then RTE_Available (RE_Yield)
10766 then
10767 Alt_Stats :=
10768 New_List (
10769 Make_Procedure_Call_Statement (Sloc (Proc),
10770 New_Occurrence_Of (RTE (RE_Yield), Sloc (Proc))));
10771 else
10772 Alt_Stats := Empty_List;
10773 end if;
10774 end;
10775 end if;
10777 Ensure_Statement_Present (Sloc (Astmt), Alt);
10779 -- After the call, if any, branch to trailing statements, if any.
10780 -- We create a label for each, as well as the corresponding label
10781 -- declaration.
10783 if not Is_Empty_List (Statements (Alt)) then
10784 Lab := Make_And_Declare_Label (Index);
10785 Append (Lab, Trailing_List);
10786 Append_List (Statements (Alt), Trailing_List);
10787 Append_To (Trailing_List,
10788 Make_Goto_Statement (Loc,
10789 Name => New_Copy (Identifier (End_Lab))));
10791 else
10792 Lab := End_Lab;
10793 end if;
10795 Append_To (Alt_Stats,
10796 Make_Goto_Statement (Loc, Name => New_Copy (Identifier (Lab))));
10798 Append_To (Alt_List,
10799 Make_Case_Statement_Alternative (Loc,
10800 Discrete_Choices => New_List (Make_Integer_Literal (Loc, Index)),
10801 Statements => Alt_Stats));
10802 end Process_Accept_Alternative;
10804 -------------------------------
10805 -- Process_Delay_Alternative --
10806 -------------------------------
10808 procedure Process_Delay_Alternative (Alt : Node_Id; Index : Int) is
10809 Dloc : constant Source_Ptr := Sloc (Delay_Statement (Alt));
10810 Cond : Node_Id;
10811 Delay_Alt : List_Id;
10813 begin
10814 -- Deal with C/Fortran boolean as delay condition
10816 Adjust_Condition (Condition (Alt));
10818 -- Determine the smallest specified delay
10820 -- for each delay alternative generate:
10822 -- if guard-expression then
10823 -- Delay_Val := delay-expression;
10824 -- Guard_Open := True;
10825 -- if Delay_Val < Delay_Min then
10826 -- Delay_Min := Delay_Val;
10827 -- Delay_Index := Index;
10828 -- end if;
10829 -- end if;
10831 -- The enclosing if-statement is omitted if there is no guard
10833 if Delay_Count = 1 or else First_Delay then
10834 First_Delay := False;
10836 Delay_Alt := New_List (
10837 Make_Assignment_Statement (Loc,
10838 Name => New_Occurrence_Of (Delay_Min, Loc),
10839 Expression => Expression (Delay_Statement (Alt))));
10841 if Delay_Count > 1 then
10842 Append_To (Delay_Alt,
10843 Make_Assignment_Statement (Loc,
10844 Name => New_Occurrence_Of (Delay_Index, Loc),
10845 Expression => Make_Integer_Literal (Loc, Index)));
10846 end if;
10848 else
10849 Delay_Alt := New_List (
10850 Make_Assignment_Statement (Loc,
10851 Name => New_Occurrence_Of (Delay_Val, Loc),
10852 Expression => Expression (Delay_Statement (Alt))));
10854 if Time_Type = Standard_Duration then
10855 Cond :=
10856 Make_Op_Lt (Loc,
10857 Left_Opnd => New_Occurrence_Of (Delay_Val, Loc),
10858 Right_Opnd => New_Occurrence_Of (Delay_Min, Loc));
10860 else
10861 -- The scope of the time type must define a comparison
10862 -- operator. The scope itself may not be visible, so we
10863 -- construct a node with entity information to insure that
10864 -- semantic analysis can find the proper operator.
10866 Cond :=
10867 Make_Function_Call (Loc,
10868 Name => Make_Selected_Component (Loc,
10869 Prefix =>
10870 New_Occurrence_Of (Scope (Time_Type), Loc),
10871 Selector_Name =>
10872 Make_Operator_Symbol (Loc,
10873 Chars => Name_Op_Lt,
10874 Strval => No_String)),
10875 Parameter_Associations =>
10876 New_List (
10877 New_Occurrence_Of (Delay_Val, Loc),
10878 New_Occurrence_Of (Delay_Min, Loc)));
10880 Set_Entity (Prefix (Name (Cond)), Scope (Time_Type));
10881 end if;
10883 Append_To (Delay_Alt,
10884 Make_Implicit_If_Statement (N,
10885 Condition => Cond,
10886 Then_Statements => New_List (
10887 Make_Assignment_Statement (Loc,
10888 Name => New_Occurrence_Of (Delay_Min, Loc),
10889 Expression => New_Occurrence_Of (Delay_Val, Loc)),
10891 Make_Assignment_Statement (Loc,
10892 Name => New_Occurrence_Of (Delay_Index, Loc),
10893 Expression => Make_Integer_Literal (Loc, Index)))));
10894 end if;
10896 if Check_Guard then
10897 Append_To (Delay_Alt,
10898 Make_Assignment_Statement (Loc,
10899 Name => New_Occurrence_Of (Guard_Open, Loc),
10900 Expression => New_Occurrence_Of (Standard_True, Loc)));
10901 end if;
10903 if Present (Condition (Alt)) then
10904 Delay_Alt := New_List (
10905 Make_Implicit_If_Statement (N,
10906 Condition => Condition (Alt),
10907 Then_Statements => Delay_Alt));
10908 end if;
10910 Append_List (Delay_Alt, Delay_List);
10912 Ensure_Statement_Present (Dloc, Alt);
10914 -- If the delay alternative has a statement part, add choice to the
10915 -- case statements for delays.
10917 if not Is_Empty_List (Statements (Alt)) then
10919 if Delay_Count = 1 then
10920 Append_List (Statements (Alt), Delay_Alt_List);
10922 else
10923 Append_To (Delay_Alt_List,
10924 Make_Case_Statement_Alternative (Loc,
10925 Discrete_Choices => New_List (
10926 Make_Integer_Literal (Loc, Index)),
10927 Statements => Statements (Alt)));
10928 end if;
10930 elsif Delay_Count = 1 then
10932 -- If the single delay has no trailing statements, add a branch
10933 -- to the exit label to the selective wait.
10935 Delay_Alt_List := New_List (
10936 Make_Goto_Statement (Loc,
10937 Name => New_Copy (Identifier (End_Lab))));
10939 end if;
10940 end Process_Delay_Alternative;
10942 -- Start of processing for Expand_N_Selective_Accept
10944 begin
10945 Process_Statements_For_Controlled_Objects (N);
10947 -- First insert some declarations before the select. The first is:
10949 -- Ann : Address
10951 -- This variable holds the parameters passed to the accept body. This
10952 -- declaration has already been inserted by the time we get here by
10953 -- a call to Expand_Accept_Declarations made from the semantics when
10954 -- processing the first accept statement contained in the select. We
10955 -- can find this entity as Accept_Address (E), where E is any of the
10956 -- entries references by contained accept statements.
10958 -- The first step is to scan the list of Selective_Accept_Statements
10959 -- to find this entity, and also count the number of accepts, and
10960 -- determine if terminated, delay or else is present:
10962 Num_Alts := 0;
10964 Alt := First (Alts);
10965 while Present (Alt) loop
10966 Process_Statements_For_Controlled_Objects (Alt);
10968 if Nkind (Alt) = N_Accept_Alternative then
10969 Add_Accept (Alt);
10971 elsif Nkind (Alt) = N_Delay_Alternative then
10972 Delay_Count := Delay_Count + 1;
10974 -- If the delays are relative delays, the delay expressions have
10975 -- type Standard_Duration. Otherwise they must have some time type
10976 -- recognized by GNAT.
10978 if Nkind (Delay_Statement (Alt)) = N_Delay_Relative_Statement then
10979 Time_Type := Standard_Duration;
10980 else
10981 Time_Type := Etype (Expression (Delay_Statement (Alt)));
10983 if Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time)
10984 or else Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time)
10985 then
10986 null;
10987 else
10988 -- Move this check to sem???
10989 Error_Msg_NE (
10990 "& is not a time type (RM 9.6(6))",
10991 Expression (Delay_Statement (Alt)), Time_Type);
10992 Time_Type := Standard_Duration;
10993 Set_Etype (Expression (Delay_Statement (Alt)), Any_Type);
10994 end if;
10995 end if;
10997 if No (Condition (Alt)) then
10999 -- This guard will always be open
11001 Check_Guard := False;
11002 end if;
11004 elsif Nkind (Alt) = N_Terminate_Alternative then
11005 Adjust_Condition (Condition (Alt));
11006 Terminate_Alt := Alt;
11007 end if;
11009 Num_Alts := Num_Alts + 1;
11010 Next (Alt);
11011 end loop;
11013 Else_Present := Present (Else_Statements (N));
11015 -- At the same time (see procedure Add_Accept) we build the accept list:
11017 -- Qnn : Accept_List (1 .. num-select) := (
11018 -- (null-body, entry-index),
11019 -- (null-body, entry-index),
11020 -- ..
11021 -- (null_body, entry-index));
11023 -- In the above declaration, null-body is True if the corresponding
11024 -- accept has no body, and false otherwise. The entry is either the
11025 -- entry index expression if there is no guard, or if a guard is
11026 -- present, then an if expression of the form:
11028 -- (if guard then entry-index else Null_Task_Entry)
11030 -- If a guard is statically known to be false, the entry can simply
11031 -- be omitted from the accept list.
11033 Append_To (Decls,
11034 Make_Object_Declaration (Loc,
11035 Defining_Identifier => Qnam,
11036 Object_Definition => New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11037 Aliased_Present => True,
11038 Expression =>
11039 Make_Qualified_Expression (Loc,
11040 Subtype_Mark =>
11041 New_Occurrence_Of (RTE (RE_Accept_List), Loc),
11042 Expression =>
11043 Make_Aggregate (Loc, Expressions => Accept_List))));
11045 -- Then we declare the variable that holds the index for the accept
11046 -- that will be selected for service:
11048 -- Xnn : Select_Index;
11050 Append_To (Decls,
11051 Make_Object_Declaration (Loc,
11052 Defining_Identifier => Xnam,
11053 Object_Definition =>
11054 New_Occurrence_Of (RTE (RE_Select_Index), Loc),
11055 Expression =>
11056 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)));
11058 -- After this follow procedure declarations for each accept body
11060 -- procedure Pnn is
11061 -- begin
11062 -- ...
11063 -- end;
11065 -- where the ... are statements from the corresponding procedure body.
11066 -- No parameters are involved, since the parameters are passed via Ann
11067 -- and the parameter references have already been expanded to be direct
11068 -- references to Ann (see Exp_Ch2.Expand_Entry_Parameter). Furthermore,
11069 -- any embedded tasking statements (which would normally be illegal in
11070 -- procedures), have been converted to calls to the tasking runtime so
11071 -- there is no problem in putting them into procedures.
11073 -- The original accept statement has been expanded into a block in
11074 -- the same fashion as for simple accepts (see Build_Accept_Body).
11076 -- Note: we don't really need to build these procedures for the case
11077 -- where no delay statement is present, but it is just as easy to
11078 -- build them unconditionally, and not significantly inefficient,
11079 -- since if they are short they will be inlined anyway.
11081 -- The procedure declarations have been assembled in Body_List
11083 -- If delays are present, we must compute the required delay.
11084 -- We first generate the declarations:
11086 -- Delay_Index : Boolean := 0;
11087 -- Delay_Min : Some_Time_Type.Time;
11088 -- Delay_Val : Some_Time_Type.Time;
11090 -- Delay_Index will be set to the index of the minimum delay, i.e. the
11091 -- active delay that is actually chosen as the basis for the possible
11092 -- delay if an immediate rendez-vous is not possible.
11094 -- In the most common case there is a single delay statement, and this
11095 -- is handled specially.
11097 if Delay_Count > 0 then
11099 -- Generate the required declarations
11101 Delay_Val :=
11102 Make_Defining_Identifier (Loc, New_External_Name ('D', 1));
11103 Delay_Index :=
11104 Make_Defining_Identifier (Loc, New_External_Name ('D', 2));
11105 Delay_Min :=
11106 Make_Defining_Identifier (Loc, New_External_Name ('D', 3));
11108 pragma Assert (Present (Time_Type));
11110 Append_To (Decls,
11111 Make_Object_Declaration (Loc,
11112 Defining_Identifier => Delay_Val,
11113 Object_Definition => New_Occurrence_Of (Time_Type, Loc)));
11115 Append_To (Decls,
11116 Make_Object_Declaration (Loc,
11117 Defining_Identifier => Delay_Index,
11118 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
11119 Expression => Make_Integer_Literal (Loc, 0)));
11121 Append_To (Decls,
11122 Make_Object_Declaration (Loc,
11123 Defining_Identifier => Delay_Min,
11124 Object_Definition => New_Occurrence_Of (Time_Type, Loc),
11125 Expression =>
11126 Unchecked_Convert_To (Time_Type,
11127 Make_Attribute_Reference (Loc,
11128 Prefix =>
11129 New_Occurrence_Of (Underlying_Type (Time_Type), Loc),
11130 Attribute_Name => Name_Last))));
11132 -- Create Duration and Delay_Mode objects used for passing a delay
11133 -- value to RTS
11135 D := Make_Temporary (Loc, 'D');
11136 M := Make_Temporary (Loc, 'M');
11138 declare
11139 Discr : Entity_Id;
11141 begin
11142 -- Note that these values are defined in s-osprim.ads and must
11143 -- be kept in sync:
11145 -- Relative : constant := 0;
11146 -- Absolute_Calendar : constant := 1;
11147 -- Absolute_RT : constant := 2;
11149 if Time_Type = Standard_Duration then
11150 Discr := Make_Integer_Literal (Loc, 0);
11152 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11153 Discr := Make_Integer_Literal (Loc, 1);
11155 else
11156 pragma Assert
11157 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11158 Discr := Make_Integer_Literal (Loc, 2);
11159 end if;
11161 Append_To (Decls,
11162 Make_Object_Declaration (Loc,
11163 Defining_Identifier => D,
11164 Object_Definition =>
11165 New_Occurrence_Of (Standard_Duration, Loc)));
11167 Append_To (Decls,
11168 Make_Object_Declaration (Loc,
11169 Defining_Identifier => M,
11170 Object_Definition =>
11171 New_Occurrence_Of (Standard_Integer, Loc),
11172 Expression => Discr));
11173 end;
11175 if Check_Guard then
11176 Guard_Open :=
11177 Make_Defining_Identifier (Loc, New_External_Name ('G', 1));
11179 Append_To (Decls,
11180 Make_Object_Declaration (Loc,
11181 Defining_Identifier => Guard_Open,
11182 Object_Definition =>
11183 New_Occurrence_Of (Standard_Boolean, Loc),
11184 Expression =>
11185 New_Occurrence_Of (Standard_False, Loc)));
11186 end if;
11188 -- Delay_Count is zero, don't need M and D set (suppress warning)
11190 else
11191 M := Empty;
11192 D := Empty;
11193 end if;
11195 if Present (Terminate_Alt) then
11197 -- If the terminate alternative guard is False, use
11198 -- Simple_Mode; otherwise use Terminate_Mode.
11200 if Present (Condition (Terminate_Alt)) then
11201 Select_Mode := Make_If_Expression (Loc,
11202 New_List (Condition (Terminate_Alt),
11203 New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc),
11204 New_Occurrence_Of (RTE (RE_Simple_Mode), Loc)));
11205 else
11206 Select_Mode := New_Occurrence_Of (RTE (RE_Terminate_Mode), Loc);
11207 end if;
11209 elsif Else_Present or Delay_Count > 0 then
11210 Select_Mode := New_Occurrence_Of (RTE (RE_Else_Mode), Loc);
11212 else
11213 Select_Mode := New_Occurrence_Of (RTE (RE_Simple_Mode), Loc);
11214 end if;
11216 Select_Call := Make_Select_Call (Select_Mode);
11217 Append (Select_Call, Stats);
11219 -- Now generate code to act on the result. There is an entry
11220 -- in this case for each accept statement with a non-null body,
11221 -- followed by a branch to the statements that follow the Accept.
11222 -- In the absence of delay alternatives, we generate:
11224 -- case X is
11225 -- when No_Rendezvous => -- omitted if simple mode
11226 -- goto Lab0;
11228 -- when 1 =>
11229 -- P1n;
11230 -- goto Lab1;
11232 -- when 2 =>
11233 -- P2n;
11234 -- goto Lab2;
11236 -- when others =>
11237 -- goto Exit;
11238 -- end case;
11240 -- Lab0: Else_Statements;
11241 -- goto exit;
11243 -- Lab1: Trailing_Statements1;
11244 -- goto Exit;
11246 -- Lab2: Trailing_Statements2;
11247 -- goto Exit;
11248 -- ...
11249 -- Exit:
11251 -- Generate label for common exit
11253 End_Lab := Make_And_Declare_Label (Num_Alts + 1);
11255 -- First entry is the default case, when no rendezvous is possible
11257 Choices := New_List (New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc));
11259 if Else_Present then
11261 -- If no rendezvous is possible, the else part is executed
11263 Lab := Make_And_Declare_Label (0);
11264 Alt_Stats := New_List (
11265 Make_Goto_Statement (Loc,
11266 Name => New_Copy (Identifier (Lab))));
11268 Append (Lab, Trailing_List);
11269 Append_List (Else_Statements (N), Trailing_List);
11270 Append_To (Trailing_List,
11271 Make_Goto_Statement (Loc,
11272 Name => New_Copy (Identifier (End_Lab))));
11273 else
11274 Alt_Stats := New_List (
11275 Make_Goto_Statement (Loc,
11276 Name => New_Copy (Identifier (End_Lab))));
11277 end if;
11279 Append_To (Alt_List,
11280 Make_Case_Statement_Alternative (Loc,
11281 Discrete_Choices => Choices,
11282 Statements => Alt_Stats));
11284 -- We make use of the fact that Accept_Index is an integer type, and
11285 -- generate successive literals for entries for each accept. Only those
11286 -- for which there is a body or trailing statements get a case entry.
11288 Alt := First (Select_Alternatives (N));
11289 Proc := First (Body_List);
11290 while Present (Alt) loop
11292 if Nkind (Alt) = N_Accept_Alternative then
11293 Process_Accept_Alternative (Alt, Index, Proc);
11294 Index := Index + 1;
11296 if Present
11297 (Handled_Statement_Sequence (Accept_Statement (Alt)))
11298 then
11299 Next (Proc);
11300 end if;
11302 elsif Nkind (Alt) = N_Delay_Alternative then
11303 Process_Delay_Alternative (Alt, Delay_Num);
11304 Delay_Num := Delay_Num + 1;
11305 end if;
11307 Next (Alt);
11308 end loop;
11310 -- An others choice is always added to the main case, as well
11311 -- as the delay case (to satisfy the compiler).
11313 Append_To (Alt_List,
11314 Make_Case_Statement_Alternative (Loc,
11315 Discrete_Choices =>
11316 New_List (Make_Others_Choice (Loc)),
11317 Statements =>
11318 New_List (Make_Goto_Statement (Loc,
11319 Name => New_Copy (Identifier (End_Lab))))));
11321 Accept_Case := New_List (
11322 Make_Case_Statement (Loc,
11323 Expression => New_Occurrence_Of (Xnam, Loc),
11324 Alternatives => Alt_List));
11326 Append_List (Trailing_List, Accept_Case);
11327 Append_List (Body_List, Decls);
11329 -- Construct case statement for trailing statements of delay
11330 -- alternatives, if there are several of them.
11332 if Delay_Count > 1 then
11333 Append_To (Delay_Alt_List,
11334 Make_Case_Statement_Alternative (Loc,
11335 Discrete_Choices =>
11336 New_List (Make_Others_Choice (Loc)),
11337 Statements =>
11338 New_List (Make_Null_Statement (Loc))));
11340 Delay_Case := New_List (
11341 Make_Case_Statement (Loc,
11342 Expression => New_Occurrence_Of (Delay_Index, Loc),
11343 Alternatives => Delay_Alt_List));
11344 else
11345 Delay_Case := Delay_Alt_List;
11346 end if;
11348 -- If there are no delay alternatives, we append the case statement
11349 -- to the statement list.
11351 if Delay_Count = 0 then
11352 Append_List (Accept_Case, Stats);
11354 -- Delay alternatives present
11356 else
11357 -- If delay alternatives are present we generate:
11359 -- find minimum delay.
11360 -- DX := minimum delay;
11361 -- M := <delay mode>;
11362 -- Timed_Selective_Wait (Q'Unchecked_Access, Delay_Mode, P,
11363 -- DX, MX, X);
11365 -- if X = No_Rendezvous then
11366 -- case statement for delay statements.
11367 -- else
11368 -- case statement for accept alternatives.
11369 -- end if;
11371 declare
11372 Cases : Node_Id;
11373 Stmt : Node_Id;
11374 Parms : List_Id;
11375 Parm : Node_Id;
11376 Conv : Node_Id;
11378 begin
11379 -- The type of the delay expression is known to be legal
11381 if Time_Type = Standard_Duration then
11382 Conv := New_Occurrence_Of (Delay_Min, Loc);
11384 elsif Is_RTE (Base_Type (Etype (Time_Type)), RO_CA_Time) then
11385 Conv := Make_Function_Call (Loc,
11386 New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
11387 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11389 else
11390 pragma Assert
11391 (Is_RTE (Base_Type (Etype (Time_Type)), RO_RT_Time));
11393 Conv := Make_Function_Call (Loc,
11394 New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
11395 New_List (New_Occurrence_Of (Delay_Min, Loc)));
11396 end if;
11398 Stmt := Make_Assignment_Statement (Loc,
11399 Name => New_Occurrence_Of (D, Loc),
11400 Expression => Conv);
11402 -- Change the value for Accept_Modes. (Else_Mode -> Delay_Mode)
11404 Parms := Parameter_Associations (Select_Call);
11406 Parm := First (Parms);
11407 while Present (Parm) and then Parm /= Select_Mode loop
11408 Next (Parm);
11409 end loop;
11411 pragma Assert (Present (Parm));
11412 Rewrite (Parm, New_Occurrence_Of (RTE (RE_Delay_Mode), Loc));
11413 Analyze (Parm);
11415 -- Prepare two new parameters of Duration and Delay_Mode type
11416 -- which represent the value and the mode of the minimum delay.
11418 Next (Parm);
11419 Insert_After (Parm, New_Occurrence_Of (M, Loc));
11420 Insert_After (Parm, New_Occurrence_Of (D, Loc));
11422 -- Create a call to RTS
11424 Rewrite (Select_Call,
11425 Make_Procedure_Call_Statement (Loc,
11426 Name => New_Occurrence_Of (RTE (RE_Timed_Selective_Wait), Loc),
11427 Parameter_Associations => Parms));
11429 -- This new call should follow the calculation of the minimum
11430 -- delay.
11432 Insert_List_Before (Select_Call, Delay_List);
11434 if Check_Guard then
11435 Stmt :=
11436 Make_Implicit_If_Statement (N,
11437 Condition => New_Occurrence_Of (Guard_Open, Loc),
11438 Then_Statements => New_List (
11439 New_Copy_Tree (Stmt),
11440 New_Copy_Tree (Select_Call)),
11441 Else_Statements => Accept_Or_Raise);
11442 Rewrite (Select_Call, Stmt);
11443 else
11444 Insert_Before (Select_Call, Stmt);
11445 end if;
11447 Cases :=
11448 Make_Implicit_If_Statement (N,
11449 Condition => Make_Op_Eq (Loc,
11450 Left_Opnd => New_Occurrence_Of (Xnam, Loc),
11451 Right_Opnd =>
11452 New_Occurrence_Of (RTE (RE_No_Rendezvous), Loc)),
11454 Then_Statements => Delay_Case,
11455 Else_Statements => Accept_Case);
11457 Append (Cases, Stats);
11458 end;
11459 end if;
11461 Append (End_Lab, Stats);
11463 -- Replace accept statement with appropriate block
11465 Rewrite (N,
11466 Make_Block_Statement (Loc,
11467 Declarations => Decls,
11468 Handled_Statement_Sequence =>
11469 Make_Handled_Sequence_Of_Statements (Loc, Statements => Stats)));
11470 Analyze (N);
11472 -- Note: have to worry more about abort deferral in above code ???
11474 -- Final step is to unstack the Accept_Address entries for all accept
11475 -- statements appearing in accept alternatives in the select statement
11477 Alt := First (Alts);
11478 while Present (Alt) loop
11479 if Nkind (Alt) = N_Accept_Alternative then
11480 Remove_Last_Elmt (Accept_Address
11481 (Entity (Entry_Direct_Name (Accept_Statement (Alt)))));
11482 end if;
11484 Next (Alt);
11485 end loop;
11486 end Expand_N_Selective_Accept;
11488 -------------------------------------------
11489 -- Expand_N_Single_Protected_Declaration --
11490 -------------------------------------------
11492 -- A single protected declaration should never be present after semantic
11493 -- analysis because it is transformed into a protected type declaration
11494 -- and an accompanying anonymous object. This routine ensures that the
11495 -- transformation takes place.
11497 procedure Expand_N_Single_Protected_Declaration (N : Node_Id) is
11498 begin
11499 raise Program_Error;
11500 end Expand_N_Single_Protected_Declaration;
11502 --------------------------------------
11503 -- Expand_N_Single_Task_Declaration --
11504 --------------------------------------
11506 -- A single task declaration should never be present after semantic
11507 -- analysis because it is transformed into a task type declaration and
11508 -- an accompanying anonymous object. This routine ensures that the
11509 -- transformation takes place.
11511 procedure Expand_N_Single_Task_Declaration (N : Node_Id) is
11512 begin
11513 raise Program_Error;
11514 end Expand_N_Single_Task_Declaration;
11516 ------------------------
11517 -- Expand_N_Task_Body --
11518 ------------------------
11520 -- Given a task body
11522 -- task body tname is
11523 -- <declarations>
11524 -- begin
11525 -- <statements>
11526 -- end x;
11528 -- This expansion routine converts it into a procedure and sets the
11529 -- elaboration flag for the procedure to true, to represent the fact
11530 -- that the task body is now elaborated:
11532 -- procedure tnameB (_Task : access tnameV) is
11533 -- discriminal : dtype renames _Task.discriminant;
11535 -- procedure _clean is
11536 -- begin
11537 -- Abort_Defer.all;
11538 -- Complete_Task;
11539 -- Abort_Undefer.all;
11540 -- return;
11541 -- end _clean;
11543 -- begin
11544 -- Abort_Undefer.all;
11545 -- <declarations>
11546 -- System.Task_Stages.Complete_Activation;
11547 -- <statements>
11548 -- at end
11549 -- _clean;
11550 -- end tnameB;
11552 -- tnameE := True;
11554 -- In addition, if the task body is an activator, then a call to activate
11555 -- tasks is added at the start of the statements, before the call to
11556 -- Complete_Activation, and if in addition the task is a master then it
11557 -- must be established as a master. These calls are inserted and analyzed
11558 -- in Expand_Cleanup_Actions, when the Handled_Sequence_Of_Statements is
11559 -- expanded.
11561 -- There is one discriminal declaration line generated for each
11562 -- discriminant that is present to provide an easy reference point for
11563 -- discriminant references inside the body (see Exp_Ch2.Expand_Name).
11565 -- Note on relationship to GNARLI definition. In the GNARLI definition,
11566 -- task body procedures have a profile (Arg : System.Address). That is
11567 -- needed because GNARLI has to use the same access-to-subprogram type
11568 -- for all task types. We depend here on knowing that in GNAT, passing
11569 -- an address argument by value is identical to passing a record value
11570 -- by access (in either case a single pointer is passed), so even though
11571 -- this procedure has the wrong profile. In fact it's all OK, since the
11572 -- callings sequence is identical.
11574 procedure Expand_N_Task_Body (N : Node_Id) is
11575 Loc : constant Source_Ptr := Sloc (N);
11576 Ttyp : constant Entity_Id := Corresponding_Spec (N);
11577 Call : Node_Id;
11578 New_N : Node_Id;
11580 Insert_Nod : Node_Id;
11581 -- Used to determine the proper location of wrapper body insertions
11583 begin
11584 -- if no task body procedure, means we had an error in configurable
11585 -- run-time mode, and there is no point in proceeding further.
11587 if No (Task_Body_Procedure (Ttyp)) then
11588 return;
11589 end if;
11591 -- Add renaming declarations for discriminals and a declaration for the
11592 -- entry family index (if applicable).
11594 Install_Private_Data_Declarations
11595 (Loc, Task_Body_Procedure (Ttyp), Ttyp, N, Declarations (N));
11597 -- Add a call to Abort_Undefer at the very beginning of the task
11598 -- body since this body is called with abort still deferred.
11600 if Abort_Allowed then
11601 Call := Build_Runtime_Call (Loc, RE_Abort_Undefer);
11602 Prepend (Call, Declarations (N));
11603 Analyze (Call);
11604 end if;
11606 -- Place call to Complete_Activation at the head of the statement list.
11608 if Restricted_Profile then
11609 Call := Build_Runtime_Call (Loc, RE_Complete_Restricted_Activation);
11610 else
11611 Call := Build_Runtime_Call (Loc, RE_Complete_Activation);
11612 end if;
11614 Insert_Before
11615 (First (Statements (Handled_Statement_Sequence (N))), Call);
11616 Analyze (Call);
11618 New_N :=
11619 Make_Subprogram_Body (Loc,
11620 Specification => Build_Task_Proc_Specification (Ttyp),
11621 Declarations => Declarations (N),
11622 Handled_Statement_Sequence => Handled_Statement_Sequence (N));
11623 Set_Is_Task_Body_Procedure (New_N);
11624 Set_At_End_Proc (New_N, At_End_Proc (N));
11626 -- If the task contains generic instantiations, cleanup actions are
11627 -- delayed until after instantiation. Transfer the activation chain to
11628 -- the subprogram, to insure that the activation call is properly
11629 -- generated. It the task body contains inner tasks, indicate that the
11630 -- subprogram is a task master.
11632 if Delay_Cleanups (Ttyp) then
11633 Set_Activation_Chain_Entity (New_N, Activation_Chain_Entity (N));
11634 Set_Is_Task_Master (New_N, Is_Task_Master (N));
11635 end if;
11637 Rewrite (N, New_N);
11638 Analyze (N);
11640 -- Set elaboration flag immediately after task body. If the body is a
11641 -- subunit, the flag is set in the declarative part containing the stub.
11643 if Nkind (Parent (N)) /= N_Subunit then
11644 Insert_After (N,
11645 Make_Assignment_Statement (Loc,
11646 Name =>
11647 Make_Identifier (Loc, New_External_Name (Chars (Ttyp), 'E')),
11648 Expression => New_Occurrence_Of (Standard_True, Loc)));
11649 end if;
11651 -- Ada 2005 (AI-345): Construct the primitive entry wrapper bodies after
11652 -- the task body. At this point all wrapper specs have been created,
11653 -- frozen and included in the dispatch table for the task type.
11655 if Ada_Version >= Ada_2005 then
11656 if Nkind (Parent (N)) = N_Subunit then
11657 Insert_Nod := Corresponding_Stub (Parent (N));
11658 else
11659 Insert_Nod := N;
11660 end if;
11662 Build_Wrapper_Bodies (Loc, Ttyp, Insert_Nod);
11663 end if;
11664 end Expand_N_Task_Body;
11666 ------------------------------------
11667 -- Expand_N_Task_Type_Declaration --
11668 ------------------------------------
11670 -- We have several things to do. First we must create a Boolean flag used
11671 -- to mark if the body is elaborated yet. This variable gets set to True
11672 -- when the body of the task is elaborated (we can't rely on the normal
11673 -- ABE mechanism for the task body, since we need to pass an access to
11674 -- this elaboration boolean to the runtime routines).
11676 -- taskE : aliased Boolean := False;
11678 -- Next a variable is declared to hold the task stack size (either the
11679 -- default : Unspecified_Size, or a value that is set by a pragma
11680 -- Storage_Size). If the value of the pragma Storage_Size is static, then
11681 -- the variable is initialized with this value:
11683 -- taskZ : Size_Type := Unspecified_Size;
11684 -- or
11685 -- taskZ : Size_Type := Size_Type (size_expression);
11687 -- Note: No variable is needed to hold the task relative deadline since
11688 -- its value would never be static because the parameter is of a private
11689 -- type (Ada.Real_Time.Time_Span).
11691 -- Next we create a corresponding record type declaration used to represent
11692 -- values of this task. The general form of this type declaration is
11694 -- type taskV (discriminants) is record
11695 -- _Task_Id : Task_Id;
11696 -- entry_family : array (bounds) of Void;
11697 -- _Priority : Integer := priority_expression;
11698 -- _Size : Size_Type := size_expression;
11699 -- _Secondary_Stack_Size : Size_Type := size_expression;
11700 -- _Task_Info : Task_Info_Type := task_info_expression;
11701 -- _CPU : Integer := cpu_range_expression;
11702 -- _Relative_Deadline : Time_Span := time_span_expression;
11703 -- _Domain : Dispatching_Domain := dd_expression;
11704 -- end record;
11706 -- The discriminants are present only if the corresponding task type has
11707 -- discriminants, and they exactly mirror the task type discriminants.
11709 -- The Id field is always present. It contains the Task_Id value, as set by
11710 -- the call to Create_Task. Note that although the task is limited, the
11711 -- task value record type is not limited, so there is no problem in passing
11712 -- this field as an out parameter to Create_Task.
11714 -- One entry_family component is present for each entry family in the task
11715 -- definition. The bounds correspond to the bounds of the entry family
11716 -- (which may depend on discriminants). The element type is void, since we
11717 -- only need the bounds information for determining the entry index. Note
11718 -- that the use of an anonymous array would normally be illegal in this
11719 -- context, but this is a parser check, and the semantics is quite prepared
11720 -- to handle such a case.
11722 -- The _Size field is present only if a Storage_Size pragma appears in the
11723 -- task definition. The expression captures the argument that was present
11724 -- in the pragma, and is used to override the task stack size otherwise
11725 -- associated with the task type.
11727 -- The _Secondary_Stack_Size field is present only the task entity has a
11728 -- Secondary_Stack_Size rep item. It will be filled at the freeze point,
11729 -- when the record init proc is built, to capture the expression of the
11730 -- rep item (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot
11731 -- be filled here since aspect evaluations are delayed till the freeze
11732 -- point.
11734 -- The _Priority field is present only if the task entity has a Priority or
11735 -- Interrupt_Priority rep item (pragma, aspect specification or attribute
11736 -- definition clause). It will be filled at the freeze point, when the
11737 -- record init proc is built, to capture the expression of the rep item
11738 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11739 -- here since aspect evaluations are delayed till the freeze point.
11741 -- The _Task_Info field is present only if a Task_Info pragma appears in
11742 -- the task definition. The expression captures the argument that was
11743 -- present in the pragma, and is used to provide the Task_Image parameter
11744 -- to the call to Create_Task.
11746 -- The _CPU field is present only if the task entity has a CPU rep item
11747 -- (pragma, aspect specification or attribute definition clause). It will
11748 -- be filled at the freeze point, when the record init proc is built, to
11749 -- capture the expression of the rep item (see Build_Record_Init_Proc in
11750 -- Exp_Ch3). Note that it cannot be filled here since aspect evaluations
11751 -- are delayed till the freeze point.
11753 -- The _Relative_Deadline field is present only if a Relative_Deadline
11754 -- pragma appears in the task definition. The expression captures the
11755 -- argument that was present in the pragma, and is used to provide the
11756 -- Relative_Deadline parameter to the call to Create_Task.
11758 -- The _Domain field is present only if the task entity has a
11759 -- Dispatching_Domain rep item (pragma, aspect specification or attribute
11760 -- definition clause). It will be filled at the freeze point, when the
11761 -- record init proc is built, to capture the expression of the rep item
11762 -- (see Build_Record_Init_Proc in Exp_Ch3). Note that it cannot be filled
11763 -- here since aspect evaluations are delayed till the freeze point.
11765 -- When a task is declared, an instance of the task value record is
11766 -- created. The elaboration of this declaration creates the correct bounds
11767 -- for the entry families, and also evaluates the size, priority, and
11768 -- task_Info expressions if needed. The initialization routine for the task
11769 -- type itself then calls Create_Task with appropriate parameters to
11770 -- initialize the value of the Task_Id field.
11772 -- Note: the address of this record is passed as the "Discriminants"
11773 -- parameter for Create_Task. Since Create_Task merely passes this onto the
11774 -- body procedure, it does not matter that it does not quite match the
11775 -- GNARLI model of what is being passed (the record contains more than just
11776 -- the discriminants, but the discriminants can be found from the record
11777 -- value).
11779 -- The Entity_Id for this created record type is placed in the
11780 -- Corresponding_Record_Type field of the associated task type entity.
11782 -- Next we create a procedure specification for the task body procedure:
11784 -- procedure taskB (_Task : access taskV);
11786 -- Note that this must come after the record type declaration, since
11787 -- the spec refers to this type. It turns out that the initialization
11788 -- procedure for the value type references the task body spec, but that's
11789 -- fine, since it won't be generated till the freeze point for the type,
11790 -- which is certainly after the task body spec declaration.
11792 -- Finally, we set the task index value field of the entry attribute in
11793 -- the case of a simple entry.
11795 procedure Expand_N_Task_Type_Declaration (N : Node_Id) is
11796 Loc : constant Source_Ptr := Sloc (N);
11797 TaskId : constant Entity_Id := Defining_Identifier (N);
11798 Tasktyp : constant Entity_Id := Etype (Defining_Identifier (N));
11799 Tasknm : constant Name_Id := Chars (Tasktyp);
11800 Taskdef : constant Node_Id := Task_Definition (N);
11802 Body_Decl : Node_Id;
11803 Cdecls : List_Id;
11804 Decl_Stack : Node_Id;
11805 Decl_SS : Node_Id;
11806 Elab_Decl : Node_Id;
11807 Ent_Stack : Entity_Id;
11808 Proc_Spec : Node_Id;
11809 Rec_Decl : Node_Id;
11810 Rec_Ent : Entity_Id;
11811 Size_Decl : Entity_Id;
11812 Task_Size : Node_Id;
11814 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id;
11815 -- Searches the task definition T for the first occurrence of the pragma
11816 -- Relative Deadline. The caller has ensured that the pragma is present
11817 -- in the task definition. Note that this routine cannot be implemented
11818 -- with the Rep Item chain mechanism since Relative_Deadline pragmas are
11819 -- not chained because their expansion into a procedure call statement
11820 -- would cause a break in the chain.
11822 ----------------------------------
11823 -- Get_Relative_Deadline_Pragma --
11824 ----------------------------------
11826 function Get_Relative_Deadline_Pragma (T : Node_Id) return Node_Id is
11827 N : Node_Id;
11829 begin
11830 N := First (Visible_Declarations (T));
11831 while Present (N) loop
11832 if Nkind (N) = N_Pragma
11833 and then Pragma_Name (N) = Name_Relative_Deadline
11834 then
11835 return N;
11836 end if;
11838 Next (N);
11839 end loop;
11841 N := First (Private_Declarations (T));
11842 while Present (N) loop
11843 if Nkind (N) = N_Pragma
11844 and then Pragma_Name (N) = Name_Relative_Deadline
11845 then
11846 return N;
11847 end if;
11849 Next (N);
11850 end loop;
11852 raise Program_Error;
11853 end Get_Relative_Deadline_Pragma;
11855 -- Start of processing for Expand_N_Task_Type_Declaration
11857 begin
11858 -- If already expanded, nothing to do
11860 if Present (Corresponding_Record_Type (Tasktyp)) then
11861 return;
11862 end if;
11864 -- Here we will do the expansion
11866 Rec_Decl := Build_Corresponding_Record (N, Tasktyp, Loc);
11868 Rec_Ent := Defining_Identifier (Rec_Decl);
11869 Cdecls := Component_Items (Component_List
11870 (Type_Definition (Rec_Decl)));
11872 Qualify_Entity_Names (N);
11874 -- First create the elaboration variable
11876 Elab_Decl :=
11877 Make_Object_Declaration (Loc,
11878 Defining_Identifier =>
11879 Make_Defining_Identifier (Sloc (Tasktyp),
11880 Chars => New_External_Name (Tasknm, 'E')),
11881 Aliased_Present => True,
11882 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
11883 Expression => New_Occurrence_Of (Standard_False, Loc));
11885 Insert_After (N, Elab_Decl);
11887 -- Next create the declaration of the size variable (tasknmZ)
11889 Set_Storage_Size_Variable (Tasktyp,
11890 Make_Defining_Identifier (Sloc (Tasktyp),
11891 Chars => New_External_Name (Tasknm, 'Z')));
11893 if Present (Taskdef)
11894 and then Has_Storage_Size_Pragma (Taskdef)
11895 and then
11896 Is_OK_Static_Expression
11897 (Expression
11898 (First (Pragma_Argument_Associations
11899 (Get_Rep_Pragma (TaskId, Name_Storage_Size)))))
11900 then
11901 Size_Decl :=
11902 Make_Object_Declaration (Loc,
11903 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11904 Object_Definition =>
11905 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11906 Expression =>
11907 Convert_To (RTE (RE_Size_Type),
11908 Relocate_Node
11909 (Expression (First (Pragma_Argument_Associations
11910 (Get_Rep_Pragma
11911 (TaskId, Name_Storage_Size)))))));
11913 else
11914 Size_Decl :=
11915 Make_Object_Declaration (Loc,
11916 Defining_Identifier => Storage_Size_Variable (Tasktyp),
11917 Object_Definition =>
11918 New_Occurrence_Of (RTE (RE_Size_Type), Loc),
11919 Expression =>
11920 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
11921 end if;
11923 Insert_After (Elab_Decl, Size_Decl);
11925 -- Next build the rest of the corresponding record declaration. This is
11926 -- done last, since the corresponding record initialization procedure
11927 -- will reference the previously created entities.
11929 -- Fill in the component declarations -- first the _Task_Id field
11931 Append_To (Cdecls,
11932 Make_Component_Declaration (Loc,
11933 Defining_Identifier =>
11934 Make_Defining_Identifier (Loc, Name_uTask_Id),
11935 Component_Definition =>
11936 Make_Component_Definition (Loc,
11937 Aliased_Present => False,
11938 Subtype_Indication => New_Occurrence_Of (RTE (RO_ST_Task_Id),
11939 Loc))));
11941 -- Declare static ATCB (that is, created by the expander) if we are
11942 -- using the Restricted run time.
11944 if Restricted_Profile then
11945 Append_To (Cdecls,
11946 Make_Component_Declaration (Loc,
11947 Defining_Identifier =>
11948 Make_Defining_Identifier (Loc, Name_uATCB),
11950 Component_Definition =>
11951 Make_Component_Definition (Loc,
11952 Aliased_Present => True,
11953 Subtype_Indication => Make_Subtype_Indication (Loc,
11954 Subtype_Mark =>
11955 New_Occurrence_Of (RTE (RE_Ada_Task_Control_Block), Loc),
11957 Constraint =>
11958 Make_Index_Or_Discriminant_Constraint (Loc,
11959 Constraints =>
11960 New_List (Make_Integer_Literal (Loc, 0)))))));
11962 end if;
11964 -- Declare static stack (that is, created by the expander) if we are
11965 -- using the Restricted run time on a bare board configuration.
11967 if Restricted_Profile and then Preallocated_Stacks_On_Target then
11969 -- First we need to extract the appropriate stack size
11971 Ent_Stack := Make_Defining_Identifier (Loc, Name_uStack);
11973 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
11974 declare
11975 Expr_N : constant Node_Id :=
11976 Expression (First (
11977 Pragma_Argument_Associations (
11978 Get_Rep_Pragma (TaskId, Name_Storage_Size))));
11979 Etyp : constant Entity_Id := Etype (Expr_N);
11980 P : constant Node_Id := Parent (Expr_N);
11982 begin
11983 -- The stack is defined inside the corresponding record.
11984 -- Therefore if the size of the stack is set by means of
11985 -- a discriminant, we must reference the discriminant of the
11986 -- corresponding record type.
11988 if Nkind (Expr_N) in N_Has_Entity
11989 and then Present (Discriminal_Link (Entity (Expr_N)))
11990 then
11991 Task_Size :=
11992 New_Occurrence_Of
11993 (CR_Discriminant (Discriminal_Link (Entity (Expr_N))),
11994 Loc);
11995 Set_Parent (Task_Size, P);
11996 Set_Etype (Task_Size, Etyp);
11997 Set_Analyzed (Task_Size);
11999 else
12000 Task_Size := New_Copy_Tree (Expr_N);
12001 end if;
12002 end;
12004 else
12005 Task_Size :=
12006 New_Occurrence_Of (RTE (RE_Default_Stack_Size), Loc);
12007 end if;
12009 Decl_Stack := Make_Component_Declaration (Loc,
12010 Defining_Identifier => Ent_Stack,
12012 Component_Definition =>
12013 Make_Component_Definition (Loc,
12014 Aliased_Present => True,
12015 Subtype_Indication => Make_Subtype_Indication (Loc,
12016 Subtype_Mark =>
12017 New_Occurrence_Of (RTE (RE_Storage_Array), Loc),
12019 Constraint =>
12020 Make_Index_Or_Discriminant_Constraint (Loc,
12021 Constraints => New_List (Make_Range (Loc,
12022 Low_Bound => Make_Integer_Literal (Loc, 1),
12023 High_Bound => Convert_To (RTE (RE_Storage_Offset),
12024 Task_Size)))))));
12026 Append_To (Cdecls, Decl_Stack);
12028 -- The appropriate alignment for the stack is ensured by the run-time
12029 -- code in charge of task creation.
12031 end if;
12033 -- Declare a static secondary stack if the conditions for a statically
12034 -- generated stack are met.
12036 if Create_Secondary_Stack_For_Task (TaskId) then
12037 declare
12038 Size_Expr : constant Node_Id :=
12039 Expression (First (
12040 Pragma_Argument_Associations (
12041 Get_Rep_Pragma (TaskId,
12042 Name_Secondary_Stack_Size))));
12044 Stack_Size : Node_Id;
12046 begin
12047 -- The secondary stack is defined inside the corresponding
12048 -- record. Therefore if the size of the stack is set by means
12049 -- of a discriminant, we must reference the discriminant of the
12050 -- corresponding record type.
12052 if Nkind (Size_Expr) in N_Has_Entity
12053 and then Present (Discriminal_Link (Entity (Size_Expr)))
12054 then
12055 Stack_Size :=
12056 New_Occurrence_Of
12057 (CR_Discriminant (Discriminal_Link (Entity (Size_Expr))),
12058 Loc);
12059 Set_Parent (Stack_Size, Parent (Size_Expr));
12060 Set_Etype (Stack_Size, Etype (Size_Expr));
12061 Set_Analyzed (Stack_Size);
12063 else
12064 Stack_Size := New_Copy_Tree (Size_Expr);
12065 end if;
12067 -- Create the secondary stack for the task
12069 Decl_SS :=
12070 Make_Component_Declaration (Loc,
12071 Defining_Identifier =>
12072 Make_Defining_Identifier (Loc, Name_uSecondary_Stack),
12073 Component_Definition =>
12074 Make_Component_Definition (Loc,
12075 Aliased_Present => True,
12076 Subtype_Indication =>
12077 Make_Subtype_Indication (Loc,
12078 Subtype_Mark =>
12079 New_Occurrence_Of (RTE (RE_SS_Stack), Loc),
12080 Constraint =>
12081 Make_Index_Or_Discriminant_Constraint (Loc,
12082 Constraints => New_List (
12083 Convert_To (RTE (RE_Size_Type),
12084 Stack_Size))))));
12086 Append_To (Cdecls, Decl_SS);
12087 end;
12088 end if;
12090 -- Add components for entry families
12092 Collect_Entry_Families (Loc, Cdecls, Size_Decl, Tasktyp);
12094 -- Add the _Priority component if a Interrupt_Priority or Priority rep
12095 -- item is present.
12097 if Has_Rep_Item (TaskId, Name_Priority, Check_Parents => False) then
12098 Append_To (Cdecls,
12099 Make_Component_Declaration (Loc,
12100 Defining_Identifier =>
12101 Make_Defining_Identifier (Loc, Name_uPriority),
12102 Component_Definition =>
12103 Make_Component_Definition (Loc,
12104 Aliased_Present => False,
12105 Subtype_Indication =>
12106 New_Occurrence_Of (Standard_Integer, Loc))));
12107 end if;
12109 -- Add the _Size component if a Storage_Size pragma is present
12111 if Present (Taskdef) and then Has_Storage_Size_Pragma (Taskdef) then
12112 Append_To (Cdecls,
12113 Make_Component_Declaration (Loc,
12114 Defining_Identifier =>
12115 Make_Defining_Identifier (Loc, Name_uSize),
12117 Component_Definition =>
12118 Make_Component_Definition (Loc,
12119 Aliased_Present => False,
12120 Subtype_Indication =>
12121 New_Occurrence_Of (RTE (RE_Size_Type), Loc)),
12123 Expression =>
12124 Convert_To (RTE (RE_Size_Type),
12125 New_Copy_Tree (
12126 Expression (First (
12127 Pragma_Argument_Associations (
12128 Get_Rep_Pragma (TaskId, Name_Storage_Size))))))));
12129 end if;
12131 -- Add the _Secondary_Stack_Size component if a Secondary_Stack_Size
12132 -- pragma is present.
12134 if Has_Rep_Pragma
12135 (TaskId, Name_Secondary_Stack_Size, Check_Parents => False)
12136 then
12137 Append_To (Cdecls,
12138 Make_Component_Declaration (Loc,
12139 Defining_Identifier =>
12140 Make_Defining_Identifier (Loc, Name_uSecondary_Stack_Size),
12142 Component_Definition =>
12143 Make_Component_Definition (Loc,
12144 Aliased_Present => False,
12145 Subtype_Indication =>
12146 New_Occurrence_Of (RTE (RE_Size_Type), Loc))));
12147 end if;
12149 -- Add the _Task_Info component if a Task_Info pragma is present
12151 if Has_Rep_Pragma (TaskId, Name_Task_Info, Check_Parents => False) then
12152 Append_To (Cdecls,
12153 Make_Component_Declaration (Loc,
12154 Defining_Identifier =>
12155 Make_Defining_Identifier (Loc, Name_uTask_Info),
12157 Component_Definition =>
12158 Make_Component_Definition (Loc,
12159 Aliased_Present => False,
12160 Subtype_Indication =>
12161 New_Occurrence_Of (RTE (RE_Task_Info_Type), Loc)),
12163 Expression => New_Copy (
12164 Expression (First (
12165 Pragma_Argument_Associations (
12166 Get_Rep_Pragma
12167 (TaskId, Name_Task_Info, Check_Parents => False)))))));
12168 end if;
12170 -- Add the _CPU component if a CPU rep item is present
12172 if Has_Rep_Item (TaskId, Name_CPU, Check_Parents => False) then
12173 Append_To (Cdecls,
12174 Make_Component_Declaration (Loc,
12175 Defining_Identifier =>
12176 Make_Defining_Identifier (Loc, Name_uCPU),
12178 Component_Definition =>
12179 Make_Component_Definition (Loc,
12180 Aliased_Present => False,
12181 Subtype_Indication =>
12182 New_Occurrence_Of (RTE (RE_CPU_Range), Loc))));
12183 end if;
12185 -- Add the _Relative_Deadline component if a Relative_Deadline pragma is
12186 -- present. If we are using a restricted run time this component will
12187 -- not be added (deadlines are not allowed by the Ravenscar profile),
12188 -- unless the task dispatching policy is EDF (for GNAT_Ravenscar_EDF
12189 -- profile).
12191 if (not Restricted_Profile or else Task_Dispatching_Policy = 'E')
12192 and then Present (Taskdef)
12193 and then Has_Relative_Deadline_Pragma (Taskdef)
12194 then
12195 Append_To (Cdecls,
12196 Make_Component_Declaration (Loc,
12197 Defining_Identifier =>
12198 Make_Defining_Identifier (Loc, Name_uRelative_Deadline),
12200 Component_Definition =>
12201 Make_Component_Definition (Loc,
12202 Aliased_Present => False,
12203 Subtype_Indication =>
12204 New_Occurrence_Of (RTE (RE_Time_Span), Loc)),
12206 Expression =>
12207 Convert_To (RTE (RE_Time_Span),
12208 New_Copy_Tree (
12209 Expression (First (
12210 Pragma_Argument_Associations (
12211 Get_Relative_Deadline_Pragma (Taskdef))))))));
12212 end if;
12214 -- Add the _Dispatching_Domain component if a Dispatching_Domain rep
12215 -- item is present. If we are using a restricted run time this component
12216 -- will not be added (dispatching domains are not allowed by the
12217 -- Ravenscar profile).
12219 if not Restricted_Profile
12220 and then
12221 Has_Rep_Item
12222 (TaskId, Name_Dispatching_Domain, Check_Parents => False)
12223 then
12224 Append_To (Cdecls,
12225 Make_Component_Declaration (Loc,
12226 Defining_Identifier =>
12227 Make_Defining_Identifier (Loc, Name_uDispatching_Domain),
12229 Component_Definition =>
12230 Make_Component_Definition (Loc,
12231 Aliased_Present => False,
12232 Subtype_Indication =>
12233 New_Occurrence_Of
12234 (RTE (RE_Dispatching_Domain_Access), Loc))));
12235 end if;
12237 Insert_After (Size_Decl, Rec_Decl);
12239 -- Analyze the record declaration immediately after construction,
12240 -- because the initialization procedure is needed for single task
12241 -- declarations before the next entity is analyzed.
12243 Analyze (Rec_Decl);
12245 -- Create the declaration of the task body procedure
12247 Proc_Spec := Build_Task_Proc_Specification (Tasktyp);
12248 Body_Decl :=
12249 Make_Subprogram_Declaration (Loc,
12250 Specification => Proc_Spec);
12251 Set_Is_Task_Body_Procedure (Body_Decl);
12253 Insert_After (Rec_Decl, Body_Decl);
12255 -- The subprogram does not comes from source, so we have to indicate the
12256 -- need for debugging information explicitly.
12258 if Comes_From_Source (Original_Node (N)) then
12259 Set_Debug_Info_Needed (Defining_Entity (Proc_Spec));
12260 end if;
12262 -- Ada 2005 (AI-345): Construct the primitive entry wrapper specs before
12263 -- the corresponding record has been frozen.
12265 if Ada_Version >= Ada_2005 then
12266 Build_Wrapper_Specs (Loc, Tasktyp, Rec_Decl);
12267 end if;
12269 -- Ada 2005 (AI-345): We must defer freezing to allow further
12270 -- declaration of primitive subprograms covering task interfaces
12272 if Ada_Version <= Ada_95 then
12274 -- Now we can freeze the corresponding record. This needs manually
12275 -- freezing, since it is really part of the task type, and the task
12276 -- type is frozen at this stage. We of course need the initialization
12277 -- procedure for this corresponding record type and we won't get it
12278 -- in time if we don't freeze now.
12280 Insert_List_After (Body_Decl, List => Freeze_Entity (Rec_Ent, N));
12281 end if;
12283 -- Complete the expansion of access types to the current task type, if
12284 -- any were declared.
12286 Expand_Previous_Access_Type (Tasktyp);
12288 -- Create wrappers for entries that have contract cases, preconditions
12289 -- and postconditions.
12291 declare
12292 Ent : Entity_Id;
12294 begin
12295 Ent := First_Entity (Tasktyp);
12296 while Present (Ent) loop
12297 if Ekind (Ent) in E_Entry | E_Entry_Family then
12298 Build_Entry_Contract_Wrapper (Ent, N);
12299 end if;
12301 Next_Entity (Ent);
12302 end loop;
12303 end;
12304 end Expand_N_Task_Type_Declaration;
12306 -------------------------------
12307 -- Expand_N_Timed_Entry_Call --
12308 -------------------------------
12310 -- A timed entry call in normal case is not implemented using ATC mechanism
12311 -- anymore for efficiency reason.
12313 -- select
12314 -- T.E;
12315 -- S1;
12316 -- or
12317 -- delay D;
12318 -- S2;
12319 -- end select;
12321 -- is expanded as follows:
12323 -- 1) When T.E is a task entry_call;
12325 -- declare
12326 -- B : Boolean;
12327 -- X : Task_Entry_Index := <entry index>;
12328 -- DX : Duration := To_Duration (D);
12329 -- M : Delay_Mode := <discriminant>;
12330 -- P : parms := (parm, parm, parm);
12332 -- begin
12333 -- Timed_Protected_Entry_Call
12334 -- (<acceptor-task>, X, P'Address, DX, M, B);
12335 -- if B then
12336 -- S1;
12337 -- else
12338 -- S2;
12339 -- end if;
12340 -- end;
12342 -- 2) When T.E is a protected entry_call;
12344 -- declare
12345 -- B : Boolean;
12346 -- X : Protected_Entry_Index := <entry index>;
12347 -- DX : Duration := To_Duration (D);
12348 -- M : Delay_Mode := <discriminant>;
12349 -- P : parms := (parm, parm, parm);
12351 -- begin
12352 -- Timed_Protected_Entry_Call
12353 -- (<object>'unchecked_access, X, P'Address, DX, M, B);
12354 -- if B then
12355 -- S1;
12356 -- else
12357 -- S2;
12358 -- end if;
12359 -- end;
12361 -- 3) Ada 2005 (AI-345): When T.E is a dispatching procedure call, there
12362 -- is no delay and the triggering statements are executed. We first
12363 -- determine the kind of the triggering call and then execute a
12364 -- synchronized operation or a direct call.
12366 -- declare
12367 -- B : Boolean := False;
12368 -- C : Ada.Tags.Prim_Op_Kind;
12369 -- DX : Duration := To_Duration (D)
12370 -- K : Ada.Tags.Tagged_Kind :=
12371 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag (<object>));
12372 -- M : Integer :=...;
12373 -- P : Parameters := (Param1 .. ParamN);
12374 -- S : Integer;
12376 -- begin
12377 -- if K = Ada.Tags.TK_Limited_Tagged
12378 -- or else K = Ada.Tags.TK_Tagged
12379 -- then
12380 -- <dispatching-call>;
12381 -- B := True;
12383 -- else
12384 -- S :=
12385 -- Ada.Tags.Get_Offset_Index
12386 -- (Ada.Tags.Tag (<object>), DT_Position (<dispatching-call>));
12388 -- _Disp_Timed_Select (<object>, S, P'Address, DX, M, C, B);
12390 -- if C = POK_Protected_Entry
12391 -- or else C = POK_Task_Entry
12392 -- then
12393 -- Param1 := P.Param1;
12394 -- ...
12395 -- ParamN := P.ParamN;
12396 -- end if;
12398 -- if B then
12399 -- if C = POK_Procedure
12400 -- or else C = POK_Protected_Procedure
12401 -- or else C = POK_Task_Procedure
12402 -- then
12403 -- <dispatching-call>;
12404 -- end if;
12405 -- end if;
12406 -- end if;
12408 -- if B then
12409 -- <triggering-statements>
12410 -- else
12411 -- <timed-statements>
12412 -- end if;
12413 -- end;
12415 -- The triggering statement and the sequence of timed statements have not
12416 -- been analyzed yet (see Analyzed_Timed_Entry_Call), but they may contain
12417 -- global references if within an instantiation.
12419 procedure Expand_N_Timed_Entry_Call (N : Node_Id) is
12420 Actuals : List_Id;
12421 Blk_Typ : Entity_Id;
12422 Call : Node_Id;
12423 Call_Ent : Entity_Id;
12424 Conc_Typ_Stmts : List_Id;
12425 Concval : Node_Id := Empty; -- init to avoid warning
12426 D_Alt : constant Node_Id := Delay_Alternative (N);
12427 D_Conv : Node_Id;
12428 D_Disc : Node_Id;
12429 D_Stat : Node_Id := Delay_Statement (D_Alt);
12430 D_Stats : List_Id;
12431 D_Type : Entity_Id;
12432 Decls : List_Id;
12433 Dummy : Node_Id;
12434 E_Alt : constant Node_Id := Entry_Call_Alternative (N);
12435 E_Call : Node_Id := Entry_Call_Statement (E_Alt);
12436 E_Stats : List_Id;
12437 Ename : Node_Id;
12438 Formals : List_Id;
12439 Index : Node_Id;
12440 Is_Disp_Select : Boolean;
12441 Lim_Typ_Stmts : List_Id;
12442 Loc : constant Source_Ptr := Sloc (D_Stat);
12443 N_Stats : List_Id;
12444 Obj : Entity_Id;
12445 Param : Node_Id;
12446 Params : List_Id;
12447 Stmt : Node_Id;
12448 Stmts : List_Id;
12449 Unpack : List_Id;
12451 B : Entity_Id; -- Call status flag
12452 C : Entity_Id; -- Call kind
12453 D : Entity_Id; -- Delay
12454 K : Entity_Id; -- Tagged kind
12455 M : Entity_Id; -- Delay mode
12456 P : Entity_Id; -- Parameter block
12457 S : Entity_Id; -- Primitive operation slot
12459 -- Start of processing for Expand_N_Timed_Entry_Call
12461 begin
12462 -- Under the Ravenscar profile, timed entry calls are excluded. An error
12463 -- was already reported on spec, so do not attempt to expand the call.
12465 if Restriction_Active (No_Select_Statements) then
12466 return;
12467 end if;
12469 Process_Statements_For_Controlled_Objects (E_Alt);
12470 Process_Statements_For_Controlled_Objects (D_Alt);
12472 Ensure_Statement_Present (Sloc (D_Stat), D_Alt);
12474 -- Retrieve E_Stats and D_Stats now because the finalization machinery
12475 -- may wrap them in blocks.
12477 E_Stats := Statements (E_Alt);
12478 D_Stats := Statements (D_Alt);
12480 -- The arguments in the call may require dynamic allocation, and the
12481 -- call statement may have been transformed into a block. The block
12482 -- may contain additional declarations for internal entities, and the
12483 -- original call is found by sequential search.
12485 if Nkind (E_Call) = N_Block_Statement then
12486 E_Call := First (Statements (Handled_Statement_Sequence (E_Call)));
12487 while Nkind (E_Call) not in
12488 N_Procedure_Call_Statement | N_Entry_Call_Statement
12489 loop
12490 Next (E_Call);
12491 end loop;
12492 end if;
12494 Is_Disp_Select :=
12495 Ada_Version >= Ada_2005
12496 and then Nkind (E_Call) = N_Procedure_Call_Statement;
12498 if Is_Disp_Select then
12499 Extract_Dispatching_Call (E_Call, Call_Ent, Obj, Actuals, Formals);
12500 Decls := New_List;
12502 Stmts := New_List;
12504 -- Generate:
12505 -- B : Boolean := False;
12507 B := Build_B (Loc, Decls);
12509 -- Generate:
12510 -- C : Ada.Tags.Prim_Op_Kind;
12512 C := Build_C (Loc, Decls);
12514 -- Because the analysis of all statements was disabled, manually
12515 -- analyze the delay statement.
12517 Analyze (D_Stat);
12518 D_Stat := Original_Node (D_Stat);
12520 else
12521 -- Build an entry call using Simple_Entry_Call
12523 Extract_Entry (E_Call, Concval, Ename, Index);
12524 Build_Simple_Entry_Call (E_Call, Concval, Ename, Index);
12526 Decls := Declarations (E_Call);
12527 Stmts := Statements (Handled_Statement_Sequence (E_Call));
12529 if No (Decls) then
12530 Decls := New_List;
12531 end if;
12533 -- Generate:
12534 -- B : Boolean;
12536 B := Make_Defining_Identifier (Loc, Name_uB);
12538 Prepend_To (Decls,
12539 Make_Object_Declaration (Loc,
12540 Defining_Identifier => B,
12541 Object_Definition =>
12542 New_Occurrence_Of (Standard_Boolean, Loc)));
12543 end if;
12545 -- Duration and mode processing
12547 D_Type := Base_Type (Etype (Expression (D_Stat)));
12549 -- Use the type of the delay expression (Calendar or Real_Time) to
12550 -- generate the appropriate conversion.
12552 if Nkind (D_Stat) = N_Delay_Relative_Statement then
12553 D_Disc := Make_Integer_Literal (Loc, 0);
12554 D_Conv := Relocate_Node (Expression (D_Stat));
12556 elsif Is_RTE (D_Type, RO_CA_Time) then
12557 D_Disc := Make_Integer_Literal (Loc, 1);
12558 D_Conv :=
12559 Make_Function_Call (Loc,
12560 Name => New_Occurrence_Of (RTE (RO_CA_To_Duration), Loc),
12561 Parameter_Associations =>
12562 New_List (New_Copy (Expression (D_Stat))));
12564 else pragma Assert (Is_RTE (D_Type, RO_RT_Time));
12565 D_Disc := Make_Integer_Literal (Loc, 2);
12566 D_Conv :=
12567 Make_Function_Call (Loc,
12568 Name => New_Occurrence_Of (RTE (RO_RT_To_Duration), Loc),
12569 Parameter_Associations =>
12570 New_List (New_Copy (Expression (D_Stat))));
12571 end if;
12573 D := Make_Temporary (Loc, 'D');
12575 -- Generate:
12576 -- D : Duration;
12578 Append_To (Decls,
12579 Make_Object_Declaration (Loc,
12580 Defining_Identifier => D,
12581 Object_Definition => New_Occurrence_Of (Standard_Duration, Loc)));
12583 M := Make_Temporary (Loc, 'M');
12585 -- Generate:
12586 -- M : Integer := (0 | 1 | 2);
12588 Append_To (Decls,
12589 Make_Object_Declaration (Loc,
12590 Defining_Identifier => M,
12591 Object_Definition => New_Occurrence_Of (Standard_Integer, Loc),
12592 Expression => D_Disc));
12594 -- Parameter block processing
12596 -- Manually create the parameter block for dispatching calls. In the
12597 -- case of entries, the block has already been created during the call
12598 -- to Build_Simple_Entry_Call.
12600 if Is_Disp_Select then
12602 -- Compute the delay at this stage because the evaluation of its
12603 -- expression must not occur earlier (see ACVC C97302A).
12605 Append_To (Stmts,
12606 Make_Assignment_Statement (Loc,
12607 Name => New_Occurrence_Of (D, Loc),
12608 Expression => D_Conv));
12610 -- Tagged kind processing, generate:
12611 -- K : Ada.Tags.Tagged_Kind :=
12612 -- Ada.Tags.Get_Tagged_Kind (Ada.Tags.Tag <object>));
12614 K := Build_K (Loc, Decls, Obj);
12616 Blk_Typ := Build_Parameter_Block (Loc, Actuals, Formals, Decls);
12617 P :=
12618 Parameter_Block_Pack (Loc, Blk_Typ, Actuals, Formals, Decls, Stmts);
12620 -- Dispatch table slot processing, generate:
12621 -- S : Integer;
12623 S := Build_S (Loc, Decls);
12625 -- Generate:
12626 -- S := Ada.Tags.Get_Offset_Index
12627 -- (Ada.Tags.Tag (<object>), DT_Position (Call_Ent));
12629 Conc_Typ_Stmts :=
12630 New_List (Build_S_Assignment (Loc, S, Obj, Call_Ent));
12632 -- Generate:
12633 -- _Disp_Timed_Select (<object>, S, P'Address, D, M, C, B);
12635 -- where Obj is the controlling formal parameter, S is the dispatch
12636 -- table slot number of the dispatching operation, P is the wrapped
12637 -- parameter block, D is the duration, M is the duration mode, C is
12638 -- the call kind and B is the call status.
12640 Params := New_List;
12642 Append_To (Params, New_Copy_Tree (Obj));
12643 Append_To (Params, New_Occurrence_Of (S, Loc));
12644 Append_To (Params,
12645 Make_Attribute_Reference (Loc,
12646 Prefix => New_Occurrence_Of (P, Loc),
12647 Attribute_Name => Name_Address));
12648 Append_To (Params, New_Occurrence_Of (D, Loc));
12649 Append_To (Params, New_Occurrence_Of (M, Loc));
12650 Append_To (Params, New_Occurrence_Of (C, Loc));
12651 Append_To (Params, New_Occurrence_Of (B, Loc));
12653 Append_To (Conc_Typ_Stmts,
12654 Make_Procedure_Call_Statement (Loc,
12655 Name =>
12656 New_Occurrence_Of
12657 (Find_Prim_Op
12658 (Etype (Etype (Obj)), Name_uDisp_Timed_Select), Loc),
12659 Parameter_Associations => Params));
12661 -- Generate:
12662 -- if C = POK_Protected_Entry
12663 -- or else C = POK_Task_Entry
12664 -- then
12665 -- Param1 := P.Param1;
12666 -- ...
12667 -- ParamN := P.ParamN;
12668 -- end if;
12670 Unpack := Parameter_Block_Unpack (Loc, P, Actuals, Formals);
12672 -- Generate the if statement only when the packed parameters need
12673 -- explicit assignments to their corresponding actuals.
12675 if Present (Unpack) then
12676 Append_To (Conc_Typ_Stmts,
12677 Make_Implicit_If_Statement (N,
12679 Condition =>
12680 Make_Or_Else (Loc,
12681 Left_Opnd =>
12682 Make_Op_Eq (Loc,
12683 Left_Opnd => New_Occurrence_Of (C, Loc),
12684 Right_Opnd =>
12685 New_Occurrence_Of
12686 (RTE (RE_POK_Protected_Entry), Loc)),
12688 Right_Opnd =>
12689 Make_Op_Eq (Loc,
12690 Left_Opnd => New_Occurrence_Of (C, Loc),
12691 Right_Opnd =>
12692 New_Occurrence_Of (RTE (RE_POK_Task_Entry), Loc))),
12694 Then_Statements => Unpack));
12695 end if;
12697 -- Generate:
12699 -- if B then
12700 -- if C = POK_Procedure
12701 -- or else C = POK_Protected_Procedure
12702 -- or else C = POK_Task_Procedure
12703 -- then
12704 -- <dispatching-call>
12705 -- end if;
12706 -- end if;
12708 N_Stats := New_List (
12709 Make_Implicit_If_Statement (N,
12710 Condition =>
12711 Make_Or_Else (Loc,
12712 Left_Opnd =>
12713 Make_Op_Eq (Loc,
12714 Left_Opnd => New_Occurrence_Of (C, Loc),
12715 Right_Opnd =>
12716 New_Occurrence_Of (RTE (RE_POK_Procedure), Loc)),
12718 Right_Opnd =>
12719 Make_Or_Else (Loc,
12720 Left_Opnd =>
12721 Make_Op_Eq (Loc,
12722 Left_Opnd => New_Occurrence_Of (C, Loc),
12723 Right_Opnd =>
12724 New_Occurrence_Of (RTE (
12725 RE_POK_Protected_Procedure), Loc)),
12726 Right_Opnd =>
12727 Make_Op_Eq (Loc,
12728 Left_Opnd => New_Occurrence_Of (C, Loc),
12729 Right_Opnd =>
12730 New_Occurrence_Of
12731 (RTE (RE_POK_Task_Procedure), Loc)))),
12733 Then_Statements => New_List (E_Call)));
12735 Append_To (Conc_Typ_Stmts,
12736 Make_Implicit_If_Statement (N,
12737 Condition => New_Occurrence_Of (B, Loc),
12738 Then_Statements => N_Stats));
12740 -- Generate:
12741 -- <dispatching-call>;
12742 -- B := True;
12744 Lim_Typ_Stmts :=
12745 New_List (New_Copy_Tree (E_Call),
12746 Make_Assignment_Statement (Loc,
12747 Name => New_Occurrence_Of (B, Loc),
12748 Expression => New_Occurrence_Of (Standard_True, Loc)));
12750 -- Generate:
12751 -- if K = Ada.Tags.TK_Limited_Tagged
12752 -- or else K = Ada.Tags.TK_Tagged
12753 -- then
12754 -- Lim_Typ_Stmts
12755 -- else
12756 -- Conc_Typ_Stmts
12757 -- end if;
12759 Append_To (Stmts,
12760 Make_Implicit_If_Statement (N,
12761 Condition => Build_Dispatching_Tag_Check (K, N),
12762 Then_Statements => Lim_Typ_Stmts,
12763 Else_Statements => Conc_Typ_Stmts));
12765 -- Generate:
12767 -- if B then
12768 -- <triggering-statements>
12769 -- else
12770 -- <timed-statements>
12771 -- end if;
12773 Append_To (Stmts,
12774 Make_Implicit_If_Statement (N,
12775 Condition => New_Occurrence_Of (B, Loc),
12776 Then_Statements => E_Stats,
12777 Else_Statements => D_Stats));
12779 else
12780 -- Simple case of a nondispatching trigger. Skip assignments to
12781 -- temporaries created for in-out parameters.
12783 -- This makes unwarranted assumptions about the shape of the expanded
12784 -- tree for the call, and should be cleaned up ???
12786 Stmt := First (Stmts);
12787 while Nkind (Stmt) /= N_Procedure_Call_Statement loop
12788 Next (Stmt);
12789 end loop;
12791 -- Compute the delay at this stage because the evaluation of
12792 -- its expression must not occur earlier (see ACVC C97302A).
12794 Insert_Before (Stmt,
12795 Make_Assignment_Statement (Loc,
12796 Name => New_Occurrence_Of (D, Loc),
12797 Expression => D_Conv));
12799 Call := Stmt;
12800 Params := Parameter_Associations (Call);
12802 -- For a protected type, we build a Timed_Protected_Entry_Call
12804 if Is_Protected_Type (Etype (Concval)) then
12806 -- Create a new call statement
12808 Param := First (Params);
12809 while Present (Param)
12810 and then not Is_RTE (Etype (Param), RE_Call_Modes)
12811 loop
12812 Next (Param);
12813 end loop;
12815 Dummy := Remove_Next (Next (Param));
12817 -- Remove garbage is following the Cancel_Param if present
12819 Dummy := Next (Param);
12821 -- Remove the mode of the Protected_Entry_Call call, then remove
12822 -- the Communication_Block of the Protected_Entry_Call call, and
12823 -- finally add Duration and a Delay_Mode parameter
12825 pragma Assert (Present (Param));
12826 Rewrite (Param, New_Occurrence_Of (D, Loc));
12828 Rewrite (Dummy, New_Occurrence_Of (M, Loc));
12830 -- Add a Boolean flag for successful entry call
12832 Append_To (Params, New_Occurrence_Of (B, Loc));
12834 case Corresponding_Runtime_Package (Etype (Concval)) is
12835 when System_Tasking_Protected_Objects_Entries =>
12836 Rewrite (Call,
12837 Make_Procedure_Call_Statement (Loc,
12838 Name =>
12839 New_Occurrence_Of
12840 (RTE (RE_Timed_Protected_Entry_Call), Loc),
12841 Parameter_Associations => Params));
12843 when others =>
12844 raise Program_Error;
12845 end case;
12847 -- For the task case, build a Timed_Task_Entry_Call
12849 else
12850 -- Create a new call statement
12852 Append_To (Params, New_Occurrence_Of (D, Loc));
12853 Append_To (Params, New_Occurrence_Of (M, Loc));
12854 Append_To (Params, New_Occurrence_Of (B, Loc));
12856 Rewrite (Call,
12857 Make_Procedure_Call_Statement (Loc,
12858 Name =>
12859 New_Occurrence_Of (RTE (RE_Timed_Task_Entry_Call), Loc),
12860 Parameter_Associations => Params));
12861 end if;
12863 Append_To (Stmts,
12864 Make_Implicit_If_Statement (N,
12865 Condition => New_Occurrence_Of (B, Loc),
12866 Then_Statements => E_Stats,
12867 Else_Statements => D_Stats));
12868 end if;
12870 Rewrite (N,
12871 Make_Block_Statement (Loc,
12872 Declarations => Decls,
12873 Handled_Statement_Sequence =>
12874 Make_Handled_Sequence_Of_Statements (Loc, Stmts)));
12876 Analyze (N);
12878 -- Some items in Decls used to be in the N_Block in E_Call that is
12879 -- constructed in Expand_Entry_Call, and are now in the new Block
12880 -- into which N has been rewritten. Adjust their scopes to reflect that.
12882 if Nkind (E_Call) = N_Block_Statement then
12883 Obj := First_Entity (Entity (Identifier (E_Call)));
12884 while Present (Obj) loop
12885 Set_Scope (Obj, Entity (Identifier (N)));
12886 Next_Entity (Obj);
12887 end loop;
12888 end if;
12890 Reset_Scopes_To (N, Entity (Identifier (N)));
12891 end Expand_N_Timed_Entry_Call;
12893 ----------------------------------------
12894 -- Expand_Protected_Body_Declarations --
12895 ----------------------------------------
12897 procedure Expand_Protected_Body_Declarations
12898 (N : Node_Id;
12899 Spec_Id : Entity_Id)
12901 begin
12902 if No_Run_Time_Mode then
12903 Error_Msg_CRT ("protected body", N);
12904 return;
12906 elsif Expander_Active then
12908 -- Associate discriminals with the first subprogram or entry body to
12909 -- be expanded.
12911 if Present (First_Protected_Operation (Declarations (N))) then
12912 Set_Discriminals (Parent (Spec_Id));
12913 end if;
12914 end if;
12915 end Expand_Protected_Body_Declarations;
12917 -------------------------
12918 -- External_Subprogram --
12919 -------------------------
12921 function External_Subprogram (E : Entity_Id) return Entity_Id is
12922 Subp : constant Entity_Id := Protected_Body_Subprogram (E);
12924 begin
12925 -- The internal and external subprograms follow each other on the entity
12926 -- chain. Note that previously private operations had no separate
12927 -- external subprogram. We now create one in all cases, because a
12928 -- private operation may actually appear in an external call, through
12929 -- a 'Access reference used for a callback.
12931 -- If the operation is a function that returns an anonymous access type,
12932 -- the corresponding itype appears before the operation, and must be
12933 -- skipped.
12935 -- This mechanism is fragile, there should be a real link between the
12936 -- two versions of the operation, but there is no place to put it ???
12938 if Is_Access_Type (Next_Entity (Subp)) then
12939 return Next_Entity (Next_Entity (Subp));
12940 else
12941 return Next_Entity (Subp);
12942 end if;
12943 end External_Subprogram;
12945 ------------------------------
12946 -- Extract_Dispatching_Call --
12947 ------------------------------
12949 procedure Extract_Dispatching_Call
12950 (N : Node_Id;
12951 Call_Ent : out Entity_Id;
12952 Object : out Entity_Id;
12953 Actuals : out List_Id;
12954 Formals : out List_Id)
12956 Call_Nam : Node_Id;
12958 begin
12959 pragma Assert (Nkind (N) = N_Procedure_Call_Statement);
12961 if Present (Original_Node (N)) then
12962 Call_Nam := Name (Original_Node (N));
12963 else
12964 Call_Nam := Name (N);
12965 end if;
12967 -- Retrieve the name of the dispatching procedure. It contains the
12968 -- dispatch table slot number.
12970 loop
12971 case Nkind (Call_Nam) is
12972 when N_Identifier =>
12973 exit;
12975 when N_Selected_Component =>
12976 Call_Nam := Selector_Name (Call_Nam);
12978 when others =>
12979 raise Program_Error;
12980 end case;
12981 end loop;
12983 Actuals := Parameter_Associations (N);
12984 Call_Ent := Entity (Call_Nam);
12985 Formals := Parameter_Specifications (Parent (Call_Ent));
12986 Object := First (Actuals);
12988 if Present (Original_Node (Object)) then
12989 Object := Original_Node (Object);
12990 end if;
12992 -- If the type of the dispatching object is an access type then return
12993 -- an explicit dereference of a copy of the object, and note that this
12994 -- is the controlling actual of the call.
12996 if Is_Access_Type (Etype (Object)) then
12997 Object :=
12998 Make_Explicit_Dereference (Sloc (N), New_Copy_Tree (Object));
12999 Analyze (Object);
13000 Set_Is_Controlling_Actual (Object);
13001 end if;
13002 end Extract_Dispatching_Call;
13004 -------------------
13005 -- Extract_Entry --
13006 -------------------
13008 procedure Extract_Entry
13009 (N : Node_Id;
13010 Concval : out Node_Id;
13011 Ename : out Node_Id;
13012 Index : out Node_Id)
13014 Nam : constant Node_Id := Name (N);
13016 begin
13017 -- For a simple entry, the name is a selected component, with the
13018 -- prefix being the task value, and the selector being the entry.
13020 if Nkind (Nam) = N_Selected_Component then
13021 Concval := Prefix (Nam);
13022 Ename := Selector_Name (Nam);
13023 Index := Empty;
13025 -- For a member of an entry family, the name is an indexed component
13026 -- where the prefix is a selected component, whose prefix in turn is
13027 -- the task value, and whose selector is the entry family. The single
13028 -- expression in the expressions list of the indexed component is the
13029 -- subscript for the family.
13031 else pragma Assert (Nkind (Nam) = N_Indexed_Component);
13032 Concval := Prefix (Prefix (Nam));
13033 Ename := Selector_Name (Prefix (Nam));
13034 Index := First (Expressions (Nam));
13035 end if;
13037 -- Through indirection, the type may actually be a limited view of a
13038 -- concurrent type. When compiling a call, the non-limited view of the
13039 -- type is visible.
13041 if From_Limited_With (Etype (Concval)) then
13042 Set_Etype (Concval, Non_Limited_View (Etype (Concval)));
13043 end if;
13044 end Extract_Entry;
13046 -------------------
13047 -- Family_Offset --
13048 -------------------
13050 function Family_Offset
13051 (Loc : Source_Ptr;
13052 Hi : Node_Id;
13053 Lo : Node_Id;
13054 Ttyp : Entity_Id;
13055 Cap : Boolean) return Node_Id
13057 Ityp : Entity_Id;
13058 Real_Hi : Node_Id;
13059 Real_Lo : Node_Id;
13061 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id;
13062 -- If one of the bounds is a reference to a discriminant, replace with
13063 -- corresponding discriminal of type. Within the body of a task retrieve
13064 -- the renamed discriminant by simple visibility, using its generated
13065 -- name. Within a protected object, find the original discriminant and
13066 -- replace it with the discriminal of the current protected operation.
13068 ------------------------------
13069 -- Convert_Discriminant_Ref --
13070 ------------------------------
13072 function Convert_Discriminant_Ref (Bound : Node_Id) return Node_Id is
13073 Loc : constant Source_Ptr := Sloc (Bound);
13074 B : Node_Id;
13075 D : Entity_Id;
13077 begin
13078 if Is_Entity_Name (Bound)
13079 and then Ekind (Entity (Bound)) = E_Discriminant
13080 then
13081 if Is_Task_Type (Ttyp) and then Has_Completion (Ttyp) then
13082 B := Make_Identifier (Loc, Chars (Entity (Bound)));
13083 Find_Direct_Name (B);
13085 elsif Is_Protected_Type (Ttyp) then
13086 D := First_Discriminant (Ttyp);
13087 while Chars (D) /= Chars (Entity (Bound)) loop
13088 Next_Discriminant (D);
13089 end loop;
13091 B := New_Occurrence_Of (Discriminal (D), Loc);
13093 else
13094 B := New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
13095 end if;
13097 elsif Nkind (Bound) = N_Attribute_Reference then
13098 return Bound;
13100 else
13101 B := New_Copy_Tree (Bound);
13102 end if;
13104 return
13105 Make_Attribute_Reference (Loc,
13106 Attribute_Name => Name_Pos,
13107 Prefix => New_Occurrence_Of (Etype (Bound), Loc),
13108 Expressions => New_List (B));
13109 end Convert_Discriminant_Ref;
13111 -- Start of processing for Family_Offset
13113 begin
13114 Real_Hi := Convert_Discriminant_Ref (Hi);
13115 Real_Lo := Convert_Discriminant_Ref (Lo);
13117 if Cap then
13118 if Is_Task_Type (Ttyp) then
13119 Ityp := RTE (RE_Task_Entry_Index);
13120 else
13121 Ityp := RTE (RE_Protected_Entry_Index);
13122 end if;
13124 Real_Hi :=
13125 Make_Attribute_Reference (Loc,
13126 Prefix => New_Occurrence_Of (Ityp, Loc),
13127 Attribute_Name => Name_Min,
13128 Expressions => New_List (
13129 Real_Hi,
13130 Make_Integer_Literal (Loc, Entry_Family_Bound - 1)));
13132 Real_Lo :=
13133 Make_Attribute_Reference (Loc,
13134 Prefix => New_Occurrence_Of (Ityp, Loc),
13135 Attribute_Name => Name_Max,
13136 Expressions => New_List (
13137 Real_Lo,
13138 Make_Integer_Literal (Loc, -Entry_Family_Bound)));
13139 end if;
13141 return Make_Op_Subtract (Loc, Real_Hi, Real_Lo);
13142 end Family_Offset;
13144 -----------------
13145 -- Family_Size --
13146 -----------------
13148 function Family_Size
13149 (Loc : Source_Ptr;
13150 Hi : Node_Id;
13151 Lo : Node_Id;
13152 Ttyp : Entity_Id;
13153 Cap : Boolean) return Node_Id
13155 Ityp : Entity_Id;
13157 begin
13158 if Is_Task_Type (Ttyp) then
13159 Ityp := RTE (RE_Task_Entry_Index);
13160 else
13161 Ityp := RTE (RE_Protected_Entry_Index);
13162 end if;
13164 return
13165 Make_Attribute_Reference (Loc,
13166 Prefix => New_Occurrence_Of (Ityp, Loc),
13167 Attribute_Name => Name_Max,
13168 Expressions => New_List (
13169 Make_Op_Add (Loc,
13170 Left_Opnd => Family_Offset (Loc, Hi, Lo, Ttyp, Cap),
13171 Right_Opnd => Make_Integer_Literal (Loc, 1)),
13172 Make_Integer_Literal (Loc, 0)));
13173 end Family_Size;
13175 ----------------------------
13176 -- Find_Enclosing_Context --
13177 ----------------------------
13179 procedure Find_Enclosing_Context
13180 (N : Node_Id;
13181 Context : out Node_Id;
13182 Context_Id : out Entity_Id;
13183 Context_Decls : out List_Id)
13185 begin
13186 -- Traverse the parent chain looking for an enclosing body, block,
13187 -- package or return statement.
13189 Context := Parent (N);
13190 while Present (Context) loop
13191 if Nkind (Context) in N_Entry_Body
13192 | N_Extended_Return_Statement
13193 | N_Package_Body
13194 | N_Package_Declaration
13195 | N_Subprogram_Body
13196 | N_Task_Body
13197 then
13198 exit;
13200 -- Do not consider block created to protect a list of statements with
13201 -- an Abort_Defer / Abort_Undefer_Direct pair.
13203 elsif Nkind (Context) = N_Block_Statement
13204 and then not Is_Abort_Block (Context)
13205 then
13206 exit;
13207 end if;
13209 Context := Parent (Context);
13210 end loop;
13212 pragma Assert (Present (Context));
13214 -- Extract the constituents of the context
13216 if Nkind (Context) = N_Extended_Return_Statement then
13217 Context_Decls := Return_Object_Declarations (Context);
13218 Context_Id := Return_Statement_Entity (Context);
13220 -- Package declarations and bodies use a common library-level activation
13221 -- chain or task master, therefore return the package declaration as the
13222 -- proper carrier for the appropriate flag.
13224 elsif Nkind (Context) = N_Package_Body then
13225 Context_Decls := Declarations (Context);
13226 Context_Id := Corresponding_Spec (Context);
13227 Context := Parent (Context_Id);
13229 if Nkind (Context) = N_Defining_Program_Unit_Name then
13230 Context := Parent (Parent (Context));
13231 else
13232 Context := Parent (Context);
13233 end if;
13235 elsif Nkind (Context) = N_Package_Declaration then
13236 Context_Decls := Visible_Declarations (Specification (Context));
13237 Context_Id := Defining_Unit_Name (Specification (Context));
13239 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13240 Context_Id := Defining_Identifier (Context_Id);
13241 end if;
13243 else
13244 if Nkind (Context) = N_Block_Statement then
13245 Context_Id := Entity (Identifier (Context));
13247 if No (Declarations (Context)) then
13248 Set_Declarations (Context, New_List);
13249 end if;
13251 elsif Nkind (Context) = N_Entry_Body then
13252 Context_Id := Defining_Identifier (Context);
13254 elsif Nkind (Context) = N_Subprogram_Body then
13255 if Present (Corresponding_Spec (Context)) then
13256 Context_Id := Corresponding_Spec (Context);
13257 else
13258 Context_Id := Defining_Unit_Name (Specification (Context));
13260 if Nkind (Context_Id) = N_Defining_Program_Unit_Name then
13261 Context_Id := Defining_Identifier (Context_Id);
13262 end if;
13263 end if;
13265 elsif Nkind (Context) = N_Task_Body then
13266 Context_Id := Corresponding_Spec (Context);
13268 else
13269 raise Program_Error;
13270 end if;
13272 Context_Decls := Declarations (Context);
13273 end if;
13275 pragma Assert (Present (Context_Id));
13276 pragma Assert (Present (Context_Decls));
13277 end Find_Enclosing_Context;
13279 -----------------------
13280 -- Find_Master_Scope --
13281 -----------------------
13283 function Find_Master_Scope (E : Entity_Id) return Entity_Id is
13284 S : Entity_Id;
13286 begin
13287 -- In Ada 2005, the master is the innermost enclosing scope that is not
13288 -- transient. If the enclosing block is the rewriting of a call or the
13289 -- scope is an extended return statement this is valid master. The
13290 -- master in an extended return is only used within the return, and is
13291 -- subsequently overwritten in Move_Activation_Chain, but it must exist
13292 -- now before that overwriting occurs.
13294 S := Scope (E);
13296 if Ada_Version >= Ada_2005 then
13297 while Is_Internal (S) loop
13298 if Nkind (Parent (S)) = N_Block_Statement
13299 and then Has_Master_Entity (S)
13300 then
13301 exit;
13303 elsif Ekind (S) = E_Return_Statement then
13304 exit;
13306 else
13307 S := Scope (S);
13308 end if;
13309 end loop;
13310 end if;
13312 return S;
13313 end Find_Master_Scope;
13315 -------------------------------
13316 -- First_Protected_Operation --
13317 -------------------------------
13319 function First_Protected_Operation (D : List_Id) return Node_Id is
13320 First_Op : Node_Id;
13322 begin
13323 First_Op := First (D);
13324 while Present (First_Op)
13325 and then Nkind (First_Op) not in N_Subprogram_Body | N_Entry_Body
13326 loop
13327 Next (First_Op);
13328 end loop;
13330 return First_Op;
13331 end First_Protected_Operation;
13333 ---------------------------------------
13334 -- Install_Private_Data_Declarations --
13335 ---------------------------------------
13337 procedure Install_Private_Data_Declarations
13338 (Loc : Source_Ptr;
13339 Spec_Id : Entity_Id;
13340 Conc_Typ : Entity_Id;
13341 Body_Nod : Node_Id;
13342 Decls : List_Id;
13343 Barrier : Boolean := False;
13344 Family : Boolean := False)
13346 Is_Protected : constant Boolean := Is_Protected_Type (Conc_Typ);
13347 Decl : Node_Id;
13348 Def : Node_Id;
13349 Insert_Node : Node_Id := Empty;
13350 Obj_Ent : Entity_Id;
13352 procedure Add (Decl : Node_Id);
13353 -- Add a single declaration after Insert_Node. If this is the first
13354 -- addition, Decl is added to the front of Decls and it becomes the
13355 -- insertion node.
13357 function Replace_Bound (Bound : Node_Id) return Node_Id;
13358 -- The bounds of an entry index may depend on discriminants, create a
13359 -- reference to the corresponding prival. Otherwise return a duplicate
13360 -- of the original bound.
13362 ---------
13363 -- Add --
13364 ---------
13366 procedure Add (Decl : Node_Id) is
13367 begin
13368 if No (Insert_Node) then
13369 Prepend_To (Decls, Decl);
13370 else
13371 Insert_After (Insert_Node, Decl);
13372 end if;
13374 Insert_Node := Decl;
13375 end Add;
13377 -------------------
13378 -- Replace_Bound --
13379 -------------------
13381 function Replace_Bound (Bound : Node_Id) return Node_Id is
13382 begin
13383 if Nkind (Bound) = N_Identifier
13384 and then Is_Discriminal (Entity (Bound))
13385 then
13386 return Make_Identifier (Loc, Chars (Entity (Bound)));
13387 else
13388 return Duplicate_Subexpr (Bound);
13389 end if;
13390 end Replace_Bound;
13392 -- Start of processing for Install_Private_Data_Declarations
13394 begin
13395 -- Step 1: Retrieve the concurrent object entity. Obj_Ent can denote
13396 -- formal parameter _O, _object or _task depending on the context.
13398 Obj_Ent := Concurrent_Object (Spec_Id, Conc_Typ);
13400 -- Special processing of _O for barrier functions, protected entries
13401 -- and families.
13403 if Barrier
13404 or else
13405 (Is_Protected
13406 and then
13407 (Ekind (Spec_Id) = E_Entry
13408 or else Ekind (Spec_Id) = E_Entry_Family))
13409 then
13410 declare
13411 Conc_Rec : constant Entity_Id :=
13412 Corresponding_Record_Type (Conc_Typ);
13413 Typ_Id : constant Entity_Id :=
13414 Make_Defining_Identifier (Loc,
13415 New_External_Name (Chars (Conc_Rec), 'P'));
13416 begin
13417 -- Generate:
13418 -- type prot_typVP is access prot_typV;
13420 Decl :=
13421 Make_Full_Type_Declaration (Loc,
13422 Defining_Identifier => Typ_Id,
13423 Type_Definition =>
13424 Make_Access_To_Object_Definition (Loc,
13425 Subtype_Indication =>
13426 New_Occurrence_Of (Conc_Rec, Loc)));
13427 Add (Decl);
13429 -- Generate:
13430 -- _object : prot_typVP := prot_typV (_O);
13432 Decl :=
13433 Make_Object_Declaration (Loc,
13434 Defining_Identifier =>
13435 Make_Defining_Identifier (Loc, Name_uObject),
13436 Object_Definition => New_Occurrence_Of (Typ_Id, Loc),
13437 Expression =>
13438 Unchecked_Convert_To (Typ_Id,
13439 New_Occurrence_Of (Obj_Ent, Loc)));
13440 Add (Decl);
13442 -- Set the reference to the concurrent object
13444 Obj_Ent := Defining_Identifier (Decl);
13445 end;
13446 end if;
13448 -- Step 2: Create the Protection object and build its declaration for
13449 -- any protected entry (family) of subprogram. Note for the lock-free
13450 -- implementation, the Protection object is not needed anymore.
13452 if Is_Protected and then not Uses_Lock_Free (Conc_Typ) then
13453 declare
13454 Prot_Ent : constant Entity_Id := Make_Temporary (Loc, 'R');
13455 Prot_Typ : RE_Id;
13457 begin
13458 Set_Protection_Object (Spec_Id, Prot_Ent);
13460 -- Determine the proper protection type
13462 if Has_Attach_Handler (Conc_Typ)
13463 and then not Restricted_Profile
13464 then
13465 Prot_Typ := RE_Static_Interrupt_Protection;
13467 elsif Has_Interrupt_Handler (Conc_Typ)
13468 and then not Restriction_Active (No_Dynamic_Attachment)
13469 then
13470 Prot_Typ := RE_Dynamic_Interrupt_Protection;
13472 else
13473 case Corresponding_Runtime_Package (Conc_Typ) is
13474 when System_Tasking_Protected_Objects_Entries =>
13475 Prot_Typ := RE_Protection_Entries;
13477 when System_Tasking_Protected_Objects_Single_Entry =>
13478 Prot_Typ := RE_Protection_Entry;
13480 when System_Tasking_Protected_Objects =>
13481 Prot_Typ := RE_Protection;
13483 when others =>
13484 raise Program_Error;
13485 end case;
13486 end if;
13488 -- Generate:
13489 -- conc_typR : protection_typ renames _object._object;
13491 Decl :=
13492 Make_Object_Renaming_Declaration (Loc,
13493 Defining_Identifier => Prot_Ent,
13494 Subtype_Mark =>
13495 New_Occurrence_Of (RTE (Prot_Typ), Loc),
13496 Name =>
13497 Make_Selected_Component (Loc,
13498 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13499 Selector_Name => Make_Identifier (Loc, Name_uObject)));
13501 Add (Decl);
13502 end;
13503 end if;
13505 -- Step 3: Add discriminant renamings (if any)
13507 if Has_Discriminants (Conc_Typ) then
13508 declare
13509 D : Entity_Id;
13511 begin
13512 D := First_Discriminant (Conc_Typ);
13513 while Present (D) loop
13515 -- Adjust the source location
13517 Set_Sloc (Discriminal (D), Loc);
13519 -- Generate:
13520 -- discr_name : discr_typ renames _object.discr_name;
13521 -- or
13522 -- discr_name : discr_typ renames _task.discr_name;
13524 Decl :=
13525 Make_Object_Renaming_Declaration (Loc,
13526 Defining_Identifier => Discriminal (D),
13527 Subtype_Mark => New_Occurrence_Of (Etype (D), Loc),
13528 Name =>
13529 Make_Selected_Component (Loc,
13530 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13531 Selector_Name => Make_Identifier (Loc, Chars (D))));
13533 Add (Decl);
13535 -- Set debug info needed on this renaming declaration even
13536 -- though it does not come from source, so that the debugger
13537 -- will get the right information for these generated names.
13539 Set_Debug_Info_Needed (Discriminal (D));
13541 Next_Discriminant (D);
13542 end loop;
13543 end;
13544 end if;
13546 -- Step 4: Add private component renamings (if any)
13548 if Is_Protected then
13549 Def := Protected_Definition (Parent (Conc_Typ));
13551 if Present (Private_Declarations (Def)) then
13552 declare
13553 Comp : Node_Id;
13554 Comp_Id : Entity_Id;
13555 Decl_Id : Entity_Id;
13556 Nam : Name_Id;
13558 begin
13559 Comp := First (Private_Declarations (Def));
13560 while Present (Comp) loop
13561 if Nkind (Comp) = N_Component_Declaration then
13562 Comp_Id := Defining_Identifier (Comp);
13563 Nam := Chars (Comp_Id);
13564 Decl_Id := Make_Defining_Identifier (Sloc (Comp_Id), Nam);
13566 -- Minimal decoration
13568 if Ekind (Spec_Id) = E_Function then
13569 Mutate_Ekind (Decl_Id, E_Constant);
13570 else
13571 Mutate_Ekind (Decl_Id, E_Variable);
13572 end if;
13574 Set_Prival (Comp_Id, Decl_Id);
13575 Set_Prival_Link (Decl_Id, Comp_Id);
13576 Set_Is_Aliased (Decl_Id, Is_Aliased (Comp_Id));
13577 Set_Is_Independent (Decl_Id, Is_Independent (Comp_Id));
13579 -- Copy the Comes_From_Source flag of the component, as
13580 -- the renaming may be the only entity directly seen by
13581 -- the user in the context, but do not warn for it.
13583 Set_Comes_From_Source
13584 (Decl_Id, Comes_From_Source (Comp_Id));
13585 Set_Warnings_Off (Decl_Id);
13587 -- Generate:
13588 -- comp_name : comp_typ renames _object.comp_name;
13590 Decl :=
13591 Make_Object_Renaming_Declaration (Loc,
13592 Defining_Identifier => Decl_Id,
13593 Subtype_Mark =>
13594 New_Occurrence_Of (Etype (Comp_Id), Loc),
13595 Name =>
13596 Make_Selected_Component (Loc,
13597 Prefix => New_Occurrence_Of (Obj_Ent, Loc),
13598 Selector_Name => Make_Identifier (Loc, Nam)));
13600 Add (Decl);
13601 end if;
13603 Next (Comp);
13604 end loop;
13605 end;
13606 end if;
13607 end if;
13609 -- Step 5: Add the declaration of the entry index and the associated
13610 -- type for barrier functions and entry families.
13612 if (Barrier and Family) or else Ekind (Spec_Id) = E_Entry_Family then
13613 declare
13614 E : constant Entity_Id := Index_Object (Spec_Id);
13615 Index : constant Entity_Id :=
13616 Defining_Identifier
13617 (Entry_Index_Specification
13618 (Entry_Body_Formal_Part (Body_Nod)));
13619 Index_Con : constant Entity_Id :=
13620 Make_Defining_Identifier (Loc, Chars (Index));
13621 High : Node_Id;
13622 Index_Typ : Entity_Id;
13623 Low : Node_Id;
13625 begin
13626 -- Minimal decoration
13628 Mutate_Ekind (Index_Con, E_Constant);
13629 Set_Entry_Index_Constant (Index, Index_Con);
13630 Set_Discriminal_Link (Index_Con, Index);
13632 -- Retrieve the bounds of the entry family
13634 High := Type_High_Bound (Etype (Index));
13635 Low := Type_Low_Bound (Etype (Index));
13637 -- In the simple case the entry family is given by a subtype mark
13638 -- and the index constant has the same type.
13640 if Is_Entity_Name (Original_Node (
13641 Discrete_Subtype_Definition (Parent (Index))))
13642 then
13643 Index_Typ := Etype (Index);
13645 -- Otherwise a new subtype declaration is required
13647 else
13648 High := Replace_Bound (High);
13649 Low := Replace_Bound (Low);
13651 Index_Typ := Make_Temporary (Loc, 'J');
13653 -- Generate:
13654 -- subtype Jnn is <Etype of Index> range Low .. High;
13656 Decl :=
13657 Make_Subtype_Declaration (Loc,
13658 Defining_Identifier => Index_Typ,
13659 Subtype_Indication =>
13660 Make_Subtype_Indication (Loc,
13661 Subtype_Mark =>
13662 New_Occurrence_Of (Base_Type (Etype (Index)), Loc),
13663 Constraint =>
13664 Make_Range_Constraint (Loc,
13665 Range_Expression =>
13666 Make_Range (Loc, Low, High))));
13667 Add (Decl);
13668 end if;
13670 Set_Etype (Index_Con, Index_Typ);
13672 -- Create the object which designates the index:
13673 -- J : constant Jnn :=
13674 -- Jnn'Val (_E - <index expr> + Jnn'Pos (Jnn'First));
13676 -- where Jnn is the subtype created above or the original type of
13677 -- the index, _E is a formal of the protected body subprogram and
13678 -- <index expr> is the index of the first family member.
13680 Decl :=
13681 Make_Object_Declaration (Loc,
13682 Defining_Identifier => Index_Con,
13683 Constant_Present => True,
13684 Object_Definition =>
13685 New_Occurrence_Of (Index_Typ, Loc),
13687 Expression =>
13688 Make_Attribute_Reference (Loc,
13689 Prefix =>
13690 New_Occurrence_Of (Index_Typ, Loc),
13691 Attribute_Name => Name_Val,
13693 Expressions => New_List (
13695 Make_Op_Add (Loc,
13696 Left_Opnd =>
13697 Make_Op_Subtract (Loc,
13698 Left_Opnd => New_Occurrence_Of (E, Loc),
13699 Right_Opnd =>
13700 Entry_Index_Expression (Loc,
13701 Defining_Identifier (Body_Nod),
13702 Empty, Conc_Typ)),
13704 Right_Opnd =>
13705 Make_Attribute_Reference (Loc,
13706 Prefix =>
13707 New_Occurrence_Of (Index_Typ, Loc),
13708 Attribute_Name => Name_Pos,
13709 Expressions => New_List (
13710 Make_Attribute_Reference (Loc,
13711 Prefix =>
13712 New_Occurrence_Of (Index_Typ, Loc),
13713 Attribute_Name => Name_First)))))));
13714 Add (Decl);
13715 end;
13716 end if;
13717 end Install_Private_Data_Declarations;
13719 ---------------------------------
13720 -- Is_Potentially_Large_Family --
13721 ---------------------------------
13723 function Is_Potentially_Large_Family
13724 (Base_Index : Entity_Id;
13725 Conctyp : Entity_Id;
13726 Lo : Node_Id;
13727 Hi : Node_Id) return Boolean
13729 begin
13730 return Scope (Base_Index) = Standard_Standard
13731 and then Base_Index = Base_Type (Standard_Integer)
13732 and then Has_Defaulted_Discriminants (Conctyp)
13733 and then
13734 (Denotes_Discriminant (Lo, True)
13735 or else
13736 Denotes_Discriminant (Hi, True));
13737 end Is_Potentially_Large_Family;
13739 -------------------------------------
13740 -- Is_Private_Primitive_Subprogram --
13741 -------------------------------------
13743 function Is_Private_Primitive_Subprogram (Id : Entity_Id) return Boolean is
13744 begin
13745 return
13746 (Ekind (Id) = E_Function or else Ekind (Id) = E_Procedure)
13747 and then Is_Private_Primitive (Id);
13748 end Is_Private_Primitive_Subprogram;
13750 ------------------
13751 -- Index_Object --
13752 ------------------
13754 function Index_Object (Spec_Id : Entity_Id) return Entity_Id is
13755 Bod_Subp : constant Entity_Id := Protected_Body_Subprogram (Spec_Id);
13756 Formal : Entity_Id;
13758 begin
13759 Formal := First_Formal (Bod_Subp);
13760 while Present (Formal) loop
13762 -- Look for formal parameter _E
13764 if Chars (Formal) = Name_uE then
13765 return Formal;
13766 end if;
13768 Next_Formal (Formal);
13769 end loop;
13771 -- A protected body subprogram should always have the parameter in
13772 -- question.
13774 raise Program_Error;
13775 end Index_Object;
13777 --------------------------------
13778 -- Make_Initialize_Protection --
13779 --------------------------------
13781 function Make_Initialize_Protection
13782 (Protect_Rec : Entity_Id) return List_Id
13784 Loc : constant Source_Ptr := Sloc (Protect_Rec);
13785 P_Arr : Entity_Id;
13786 Pdec : Node_Id;
13787 Ptyp : constant Node_Id :=
13788 Corresponding_Concurrent_Type (Protect_Rec);
13789 Args : List_Id;
13790 L : constant List_Id := New_List;
13791 Has_Entry : constant Boolean := Has_Entries (Ptyp);
13792 Prio_Type : Entity_Id;
13793 Prio_Var : Entity_Id := Empty;
13794 Restricted : constant Boolean := Restricted_Profile;
13796 begin
13797 -- We may need two calls to properly initialize the object, one to
13798 -- Initialize_Protection, and possibly one to Install_Handlers if we
13799 -- have a pragma Attach_Handler.
13801 -- Get protected declaration. In the case of a task type declaration,
13802 -- this is simply the parent of the protected type entity. In the single
13803 -- protected object declaration, this parent will be the implicit type,
13804 -- and we can find the corresponding single protected object declaration
13805 -- by searching forward in the declaration list in the tree.
13807 -- Is the test for N_Single_Protected_Declaration needed here??? Nodes
13808 -- of this type should have been removed during semantic analysis.
13810 Pdec := Parent (Ptyp);
13811 while Nkind (Pdec) not in
13812 N_Protected_Type_Declaration | N_Single_Protected_Declaration
13813 loop
13814 Next (Pdec);
13815 end loop;
13817 -- Build the parameter list for the call. Note that _Init is the name
13818 -- of the formal for the object to be initialized, which is the task
13819 -- value record itself.
13821 Args := New_List;
13823 -- For lock-free implementation, skip initializations of the Protection
13824 -- object.
13826 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
13828 -- Object parameter. This is a pointer to the object of type
13829 -- Protection used by the GNARL to control the protected object.
13831 Append_To (Args,
13832 Make_Attribute_Reference (Loc,
13833 Prefix =>
13834 Make_Selected_Component (Loc,
13835 Prefix => Make_Identifier (Loc, Name_uInit),
13836 Selector_Name => Make_Identifier (Loc, Name_uObject)),
13837 Attribute_Name => Name_Unchecked_Access));
13839 -- Priority parameter. Set to Unspecified_Priority unless there is a
13840 -- Priority rep item, in which case we take the value from the pragma
13841 -- or attribute definition clause, or there is an Interrupt_Priority
13842 -- rep item and no Priority rep item, and we set the ceiling to
13843 -- Interrupt_Priority'Last, an implementation-defined value, see
13844 -- (RM D.3(10)).
13846 if Has_Rep_Item (Ptyp, Name_Priority, Check_Parents => False) then
13847 declare
13848 Prio_Clause : constant Node_Id :=
13849 Get_Rep_Item
13850 (Ptyp, Name_Priority, Check_Parents => False);
13852 Prio : Node_Id;
13854 begin
13855 -- Pragma Priority
13857 if Nkind (Prio_Clause) = N_Pragma then
13858 Prio :=
13859 Expression
13860 (First (Pragma_Argument_Associations (Prio_Clause)));
13862 -- Get_Rep_Item returns either priority pragma
13864 if Pragma_Name (Prio_Clause) = Name_Priority then
13865 Prio_Type := RTE (RE_Any_Priority);
13866 else
13867 Prio_Type := RTE (RE_Interrupt_Priority);
13868 end if;
13870 -- Attribute definition clause Priority
13872 else
13873 if Chars (Prio_Clause) = Name_Priority then
13874 Prio_Type := RTE (RE_Any_Priority);
13875 else
13876 Prio_Type := RTE (RE_Interrupt_Priority);
13877 end if;
13879 Prio := Expression (Prio_Clause);
13880 end if;
13882 -- Always create a locale variable to capture the priority.
13883 -- The priority is also passed to Install_Restriced_Handlers.
13884 -- Note that it is really necessary to create this variable
13885 -- explicitly. It might be thought that removing side effects
13886 -- would the appropriate approach, but that could generate
13887 -- declarations improperly placed in the enclosing scope.
13889 Prio_Var := Make_Temporary (Loc, 'R', Prio);
13890 Append_To (L,
13891 Make_Object_Declaration (Loc,
13892 Defining_Identifier => Prio_Var,
13893 Object_Definition => New_Occurrence_Of (Prio_Type, Loc),
13894 Expression => Relocate_Node (Prio)));
13896 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
13897 end;
13899 -- When no priority is specified but an xx_Handler pragma is, we
13900 -- default to System.Interrupts.Default_Interrupt_Priority, see
13901 -- D.3(10).
13903 elsif Has_Attach_Handler (Ptyp)
13904 or else Has_Interrupt_Handler (Ptyp)
13905 then
13906 Append_To (Args,
13907 New_Occurrence_Of (RTE (RE_Default_Interrupt_Priority), Loc));
13909 -- Normal case, no priority or xx_Handler specified, default priority
13911 else
13912 Append_To (Args,
13913 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
13914 end if;
13916 -- Deadline_Floor parameter for GNAT_Ravenscar_EDF runtimes
13918 if Restricted_Profile and Task_Dispatching_Policy = 'E' then
13919 Deadline_Floor : declare
13920 Item : constant Node_Id :=
13921 Get_Rep_Item
13922 (Ptyp, Name_Deadline_Floor, Check_Parents => False);
13924 Deadline : Node_Id;
13926 begin
13927 if Present (Item) then
13929 -- Pragma Deadline_Floor
13931 if Nkind (Item) = N_Pragma then
13932 Deadline :=
13933 Expression
13934 (First (Pragma_Argument_Associations (Item)));
13936 -- Attribute definition clause Deadline_Floor
13938 else
13939 pragma Assert
13940 (Nkind (Item) = N_Attribute_Definition_Clause);
13942 Deadline := Expression (Item);
13943 end if;
13945 Append_To (Args, Deadline);
13947 -- Unusual case: default deadline
13949 else
13950 Append_To (Args,
13951 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
13952 end if;
13953 end Deadline_Floor;
13954 end if;
13956 -- Test for Compiler_Info parameter. This parameter allows entry body
13957 -- procedures and barrier functions to be called from the runtime. It
13958 -- is a pointer to the record generated by the compiler to represent
13959 -- the protected object.
13961 -- A protected type without entries that covers an interface and
13962 -- overrides the abstract routines with protected procedures is
13963 -- considered equivalent to a protected type with entries in the
13964 -- context of dispatching select statements.
13966 -- Protected types with interrupt handlers (when not using a
13967 -- restricted profile) are also considered equivalent to protected
13968 -- types with entries.
13970 -- The types which are used (Static_Interrupt_Protection and
13971 -- Dynamic_Interrupt_Protection) are derived from Protection_Entries.
13973 declare
13974 Pkg_Id : constant RTU_Id := Corresponding_Runtime_Package (Ptyp);
13976 Called_Subp : RE_Id;
13978 begin
13979 case Pkg_Id is
13980 when System_Tasking_Protected_Objects_Entries =>
13981 Called_Subp := RE_Initialize_Protection_Entries;
13983 -- Argument Compiler_Info
13985 Append_To (Args,
13986 Make_Attribute_Reference (Loc,
13987 Prefix => Make_Identifier (Loc, Name_uInit),
13988 Attribute_Name => Name_Address));
13990 when System_Tasking_Protected_Objects_Single_Entry =>
13991 Called_Subp := RE_Initialize_Protection_Entry;
13993 -- Argument Compiler_Info
13995 Append_To (Args,
13996 Make_Attribute_Reference (Loc,
13997 Prefix => Make_Identifier (Loc, Name_uInit),
13998 Attribute_Name => Name_Address));
14000 when System_Tasking_Protected_Objects =>
14001 Called_Subp := RE_Initialize_Protection;
14003 when others =>
14004 raise Program_Error;
14005 end case;
14007 -- Entry_Queue_Maxes parameter. This is an access to an array of
14008 -- naturals representing the entry queue maximums for each entry
14009 -- in the protected type. Zero represents no max. The access is
14010 -- null if there is no limit for all entries (usual case).
14012 if Has_Entry
14013 and then Pkg_Id = System_Tasking_Protected_Objects_Entries
14014 then
14015 if Present (Entry_Max_Queue_Lengths_Array (Ptyp)) then
14016 Append_To (Args,
14017 Make_Attribute_Reference (Loc,
14018 Prefix =>
14019 New_Occurrence_Of
14020 (Entry_Max_Queue_Lengths_Array (Ptyp), Loc),
14021 Attribute_Name => Name_Unrestricted_Access));
14022 else
14023 Append_To (Args, Make_Null (Loc));
14024 end if;
14026 -- Edge cases exist where entry initialization functions are
14027 -- called, but no entries exist, so null is appended.
14029 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14030 Append_To (Args, Make_Null (Loc));
14031 end if;
14033 -- Entry_Bodies parameter. This is a pointer to an array of
14034 -- pointers to the entry body procedures and barrier functions of
14035 -- the object. If the protected type has no entries this object
14036 -- will not exist, in this case, pass a null (it can happen when
14037 -- there are protected interrupt handlers or interfaces).
14039 if Has_Entry then
14040 P_Arr := Entry_Bodies_Array (Ptyp);
14042 -- Argument Entry_Body (for single entry) or Entry_Bodies (for
14043 -- multiple entries).
14045 Append_To (Args,
14046 Make_Attribute_Reference (Loc,
14047 Prefix => New_Occurrence_Of (P_Arr, Loc),
14048 Attribute_Name => Name_Unrestricted_Access));
14050 if Pkg_Id = System_Tasking_Protected_Objects_Entries then
14052 -- Find index mapping function (clumsy but ok for now)
14054 while Ekind (P_Arr) /= E_Function loop
14055 Next_Entity (P_Arr);
14056 end loop;
14058 Append_To (Args,
14059 Make_Attribute_Reference (Loc,
14060 Prefix => New_Occurrence_Of (P_Arr, Loc),
14061 Attribute_Name => Name_Unrestricted_Access));
14062 end if;
14064 elsif Pkg_Id = System_Tasking_Protected_Objects_Single_Entry then
14066 -- This is the case where we have a protected object with
14067 -- interfaces and no entries, and the single entry restriction
14068 -- is in effect. We pass a null pointer for the entry
14069 -- parameter because there is no actual entry.
14071 Append_To (Args, Make_Null (Loc));
14073 elsif Pkg_Id = System_Tasking_Protected_Objects_Entries then
14075 -- This is the case where we have a protected object with no
14076 -- entries and:
14077 -- - either interrupt handlers with non restricted profile,
14078 -- - or interfaces
14079 -- Note that the types which are used for interrupt handlers
14080 -- (Static/Dynamic_Interrupt_Protection) are derived from
14081 -- Protection_Entries. We pass two null pointers because there
14082 -- is no actual entry, and the initialization procedure needs
14083 -- both Entry_Bodies and Find_Body_Index.
14085 Append_To (Args, Make_Null (Loc));
14086 Append_To (Args, Make_Null (Loc));
14087 end if;
14089 Append_To (L,
14090 Make_Procedure_Call_Statement (Loc,
14091 Name =>
14092 New_Occurrence_Of (RTE (Called_Subp), Loc),
14093 Parameter_Associations => Args));
14094 end;
14095 end if;
14097 if Has_Attach_Handler (Ptyp) then
14099 -- We have a list of N Attach_Handler (ProcI, ExprI), and we have to
14100 -- make the following call:
14102 -- Install_Handlers (_object,
14103 -- ((Expr1, Proc1'access), ...., (ExprN, ProcN'access));
14105 -- or, in the case of Ravenscar:
14107 -- Install_Restricted_Handlers
14108 -- (Prio, ((Expr1, Proc1'access), ...., (ExprN, ProcN'access)));
14110 declare
14111 Args : constant List_Id := New_List;
14112 Table : constant List_Id := New_List;
14113 Ritem : Node_Id := First_Rep_Item (Ptyp);
14115 begin
14116 -- Build the Priority parameter (only for ravenscar)
14118 if Restricted then
14120 -- Priority comes from a pragma
14122 if Present (Prio_Var) then
14123 Append_To (Args, New_Occurrence_Of (Prio_Var, Loc));
14125 -- Priority is the default one
14127 else
14128 Append_To (Args,
14129 New_Occurrence_Of
14130 (RTE (RE_Default_Interrupt_Priority), Loc));
14131 end if;
14132 end if;
14134 -- Build the Attach_Handler table argument
14136 while Present (Ritem) loop
14137 if Nkind (Ritem) = N_Pragma
14138 and then Pragma_Name (Ritem) = Name_Attach_Handler
14139 then
14140 declare
14141 Handler : constant Node_Id :=
14142 First (Pragma_Argument_Associations (Ritem));
14144 Interrupt : constant Node_Id := Next (Handler);
14145 Expr : constant Node_Id := Expression (Interrupt);
14147 begin
14148 Append_To (Table,
14149 Make_Aggregate (Loc, Expressions => New_List (
14150 Unchecked_Convert_To
14151 (RTE (RE_System_Interrupt_Id), Expr),
14152 Make_Attribute_Reference (Loc,
14153 Prefix =>
14154 Make_Selected_Component (Loc,
14155 Prefix =>
14156 Make_Identifier (Loc, Name_uInit),
14157 Selector_Name =>
14158 Duplicate_Subexpr_No_Checks
14159 (Expression (Handler))),
14160 Attribute_Name => Name_Access))));
14161 end;
14162 end if;
14164 Next_Rep_Item (Ritem);
14165 end loop;
14167 -- Append the table argument we just built
14169 Append_To (Args, Make_Aggregate (Loc, Table));
14171 -- Append the Install_Handlers (or Install_Restricted_Handlers)
14172 -- call to the statements.
14174 if Restricted then
14175 -- Call a simplified version of Install_Handlers to be used
14176 -- when the Ravenscar restrictions are in effect
14177 -- (Install_Restricted_Handlers).
14179 Append_To (L,
14180 Make_Procedure_Call_Statement (Loc,
14181 Name =>
14182 New_Occurrence_Of
14183 (RTE (RE_Install_Restricted_Handlers), Loc),
14184 Parameter_Associations => Args));
14186 else
14187 if not Uses_Lock_Free (Defining_Identifier (Pdec)) then
14189 -- First, prepends the _object argument
14191 Prepend_To (Args,
14192 Make_Attribute_Reference (Loc,
14193 Prefix =>
14194 Make_Selected_Component (Loc,
14195 Prefix => Make_Identifier (Loc, Name_uInit),
14196 Selector_Name =>
14197 Make_Identifier (Loc, Name_uObject)),
14198 Attribute_Name => Name_Unchecked_Access));
14199 end if;
14201 -- Then, insert call to Install_Handlers
14203 Append_To (L,
14204 Make_Procedure_Call_Statement (Loc,
14205 Name =>
14206 New_Occurrence_Of (RTE (RE_Install_Handlers), Loc),
14207 Parameter_Associations => Args));
14208 end if;
14209 end;
14210 end if;
14212 return L;
14213 end Make_Initialize_Protection;
14215 ---------------------------
14216 -- Make_Task_Create_Call --
14217 ---------------------------
14219 function Make_Task_Create_Call (Task_Rec : Entity_Id) return Node_Id is
14220 Loc : constant Source_Ptr := Sloc (Task_Rec);
14221 Args : List_Id;
14222 Ecount : Node_Id;
14223 Name : Node_Id;
14224 Tdec : Node_Id;
14225 Tdef : Node_Id;
14226 Tnam : Name_Id;
14227 Ttyp : Entity_Id;
14229 begin
14230 Ttyp := Corresponding_Concurrent_Type (Task_Rec);
14231 Tnam := Chars (Ttyp);
14233 -- Get task declaration. In the case of a task type declaration, this is
14234 -- simply the parent of the task type entity. In the single task
14235 -- declaration, this parent will be the implicit type, and we can find
14236 -- the corresponding single task declaration by searching forward in the
14237 -- declaration list in the tree.
14239 -- Is the test for N_Single_Task_Declaration needed here??? Nodes of
14240 -- this type should have been removed during semantic analysis.
14242 Tdec := Parent (Ttyp);
14243 while Nkind (Tdec) not in
14244 N_Task_Type_Declaration | N_Single_Task_Declaration
14245 loop
14246 Next (Tdec);
14247 end loop;
14249 -- Now we can find the task definition from this declaration
14251 Tdef := Task_Definition (Tdec);
14253 -- Build the parameter list for the call. Note that _Init is the name
14254 -- of the formal for the object to be initialized, which is the task
14255 -- value record itself.
14257 Args := New_List;
14259 -- Priority parameter. Set to Unspecified_Priority unless there is a
14260 -- Priority rep item, in which case we take the value from the rep item.
14261 -- Not used on Ravenscar_EDF profile.
14263 if not (Restricted_Profile and then Task_Dispatching_Policy = 'E') then
14264 if Has_Rep_Item (Ttyp, Name_Priority, Check_Parents => False) then
14265 Append_To (Args,
14266 Make_Selected_Component (Loc,
14267 Prefix => Make_Identifier (Loc, Name_uInit),
14268 Selector_Name => Make_Identifier (Loc, Name_uPriority)));
14269 else
14270 Append_To (Args,
14271 New_Occurrence_Of (RTE (RE_Unspecified_Priority), Loc));
14272 end if;
14273 end if;
14275 -- Optional Stack parameter
14277 if Restricted_Profile then
14279 -- If the stack has been preallocated by the expander then
14280 -- pass its address. Otherwise, pass a null address.
14282 if Preallocated_Stacks_On_Target then
14283 Append_To (Args,
14284 Make_Attribute_Reference (Loc,
14285 Prefix =>
14286 Make_Selected_Component (Loc,
14287 Prefix => Make_Identifier (Loc, Name_uInit),
14288 Selector_Name => Make_Identifier (Loc, Name_uStack)),
14289 Attribute_Name => Name_Address));
14291 else
14292 Append_To (Args,
14293 New_Occurrence_Of (RTE (RE_Null_Address), Loc));
14294 end if;
14295 end if;
14297 -- Size parameter. If no Storage_Size pragma is present, then
14298 -- the size is taken from the taskZ variable for the type, which
14299 -- is either Unspecified_Size, or has been reset by the use of
14300 -- a Storage_Size attribute definition clause. If a pragma is
14301 -- present, then the size is taken from the _Size field of the
14302 -- task value record, which was set from the pragma value.
14304 if Present (Tdef) and then Has_Storage_Size_Pragma (Tdef) then
14305 Append_To (Args,
14306 Make_Selected_Component (Loc,
14307 Prefix => Make_Identifier (Loc, Name_uInit),
14308 Selector_Name => Make_Identifier (Loc, Name_uSize)));
14310 else
14311 Append_To (Args,
14312 New_Occurrence_Of (Storage_Size_Variable (Ttyp), Loc));
14313 end if;
14315 -- Secondary_Stack parameter used for restricted profiles
14317 if Restricted_Profile then
14319 -- If the secondary stack has been allocated by the expander then
14320 -- pass its access pointer. Otherwise, pass null.
14322 if Create_Secondary_Stack_For_Task (Ttyp) then
14323 Append_To (Args,
14324 Make_Attribute_Reference (Loc,
14325 Prefix =>
14326 Make_Selected_Component (Loc,
14327 Prefix => Make_Identifier (Loc, Name_uInit),
14328 Selector_Name =>
14329 Make_Identifier (Loc, Name_uSecondary_Stack)),
14330 Attribute_Name => Name_Unrestricted_Access));
14332 else
14333 Append_To (Args, Make_Null (Loc));
14334 end if;
14335 end if;
14337 -- Secondary_Stack_Size parameter. Set RE_Unspecified_Size unless there
14338 -- is a Secondary_Stack_Size pragma, in which case take the value from
14339 -- the pragma. If the restriction No_Secondary_Stack is active then a
14340 -- size of 0 is passed regardless to prevent the allocation of the
14341 -- unused stack.
14343 if Restriction_Active (No_Secondary_Stack) then
14344 Append_To (Args, Make_Integer_Literal (Loc, 0));
14346 elsif Has_Rep_Pragma
14347 (Ttyp, Name_Secondary_Stack_Size, Check_Parents => False)
14348 then
14349 Append_To (Args,
14350 Make_Selected_Component (Loc,
14351 Prefix => Make_Identifier (Loc, Name_uInit),
14352 Selector_Name =>
14353 Make_Identifier (Loc, Name_uSecondary_Stack_Size)));
14355 else
14356 Append_To (Args,
14357 New_Occurrence_Of (RTE (RE_Unspecified_Size), Loc));
14358 end if;
14360 -- Task_Info parameter. Set to Unspecified_Task_Info unless there is a
14361 -- Task_Info pragma, in which case we take the value from the pragma.
14363 if Has_Rep_Pragma (Ttyp, Name_Task_Info, Check_Parents => False) then
14364 Append_To (Args,
14365 Make_Selected_Component (Loc,
14366 Prefix => Make_Identifier (Loc, Name_uInit),
14367 Selector_Name => Make_Identifier (Loc, Name_uTask_Info)));
14369 else
14370 Append_To (Args,
14371 New_Occurrence_Of (RTE (RE_Unspecified_Task_Info), Loc));
14372 end if;
14374 -- CPU parameter. Set to Unspecified_CPU unless there is a CPU rep item,
14375 -- in which case we take the value from the rep item. The parameter is
14376 -- passed as an Integer because in the case of unspecified CPU the
14377 -- value is not in the range of CPU_Range.
14379 if Has_Rep_Item (Ttyp, Name_CPU, Check_Parents => False) then
14380 Append_To (Args,
14381 Convert_To (Standard_Integer,
14382 Make_Selected_Component (Loc,
14383 Prefix => Make_Identifier (Loc, Name_uInit),
14384 Selector_Name => Make_Identifier (Loc, Name_uCPU))));
14385 else
14386 Append_To (Args,
14387 New_Occurrence_Of (RTE (RE_Unspecified_CPU), Loc));
14388 end if;
14390 if not Restricted_Profile or else Task_Dispatching_Policy = 'E' then
14392 -- Deadline parameter. If no Relative_Deadline pragma is present,
14393 -- then the deadline is Time_Span_Zero. If a pragma is present, then
14394 -- the deadline is taken from the _Relative_Deadline field of the
14395 -- task value record, which was set from the pragma value. Note that
14396 -- this parameter must not be generated for the restricted profiles
14397 -- since Ravenscar does not allow deadlines.
14399 -- Case where pragma Relative_Deadline applies: use given value
14401 if Present (Tdef) and then Has_Relative_Deadline_Pragma (Tdef) then
14402 Append_To (Args,
14403 Make_Selected_Component (Loc,
14404 Prefix => Make_Identifier (Loc, Name_uInit),
14405 Selector_Name =>
14406 Make_Identifier (Loc, Name_uRelative_Deadline)));
14408 -- No pragma Relative_Deadline apply to the task
14410 else
14411 Append_To (Args,
14412 New_Occurrence_Of (RTE (RE_Time_Span_Zero), Loc));
14413 end if;
14414 end if;
14416 if not Restricted_Profile then
14418 -- Dispatching_Domain parameter. If no Dispatching_Domain rep item is
14419 -- present, then the dispatching domain is null. If a rep item is
14420 -- present, then the dispatching domain is taken from the
14421 -- _Dispatching_Domain field of the task value record, which was set
14422 -- from the rep item value.
14424 -- Case where Dispatching_Domain rep item applies: use given value
14426 if Has_Rep_Item
14427 (Ttyp, Name_Dispatching_Domain, Check_Parents => False)
14428 then
14429 Append_To (Args,
14430 Make_Selected_Component (Loc,
14431 Prefix =>
14432 Make_Identifier (Loc, Name_uInit),
14433 Selector_Name =>
14434 Make_Identifier (Loc, Name_uDispatching_Domain)));
14436 -- No pragma or aspect Dispatching_Domain applies to the task
14438 else
14439 Append_To (Args, Make_Null (Loc));
14440 end if;
14442 -- Number of entries. This is an expression of the form:
14444 -- n + _Init.a'Length + _Init.a'B'Length + ...
14446 -- where a,b... are the entry family names for the task definition
14448 Ecount := Build_Entry_Count_Expression (Ttyp, Loc);
14449 Append_To (Args, Ecount);
14451 -- Master parameter. This is a reference to the _Master parameter of
14452 -- the initialization procedure, except in the case of the pragma
14453 -- Restrictions (No_Task_Hierarchy) where the value is fixed to
14454 -- System.Tasking.Library_Task_Level.
14456 if Restriction_Active (No_Task_Hierarchy) = False then
14457 Append_To (Args, Make_Identifier (Loc, Name_uMaster));
14458 else
14459 Append_To (Args, Make_Integer_Literal (Loc, Library_Task_Level));
14460 end if;
14461 end if;
14463 -- State parameter. This is a pointer to the task body procedure. The
14464 -- required value is obtained by taking 'Unrestricted_Access of the task
14465 -- body procedure and converting it (with an unchecked conversion) to
14466 -- the type required by the task kernel. For further details, see the
14467 -- description of Expand_N_Task_Body. We use 'Unrestricted_Access rather
14468 -- than 'Address in order to avoid creating trampolines.
14470 declare
14471 Body_Proc : constant Node_Id := Get_Task_Body_Procedure (Ttyp);
14472 Subp_Ptr_Typ : constant Node_Id :=
14473 Create_Itype (E_Access_Subprogram_Type, Tdec);
14474 Ref : constant Node_Id := Make_Itype_Reference (Loc);
14476 begin
14477 Set_Directly_Designated_Type (Subp_Ptr_Typ, Body_Proc);
14478 Set_Etype (Subp_Ptr_Typ, Subp_Ptr_Typ);
14480 -- Be sure to freeze a reference to the access-to-subprogram type,
14481 -- otherwise gigi will complain that it's in the wrong scope, because
14482 -- it's actually inside the init procedure for the record type that
14483 -- corresponds to the task type.
14485 Set_Itype (Ref, Subp_Ptr_Typ);
14486 Append_Freeze_Action (Task_Rec, Ref);
14488 Append_To (Args,
14489 Unchecked_Convert_To (RTE (RE_Task_Procedure_Access),
14490 Make_Qualified_Expression (Loc,
14491 Subtype_Mark => New_Occurrence_Of (Subp_Ptr_Typ, Loc),
14492 Expression =>
14493 Make_Attribute_Reference (Loc,
14494 Prefix => New_Occurrence_Of (Body_Proc, Loc),
14495 Attribute_Name => Name_Unrestricted_Access))));
14496 end;
14498 -- Discriminants parameter. This is just the address of the task
14499 -- value record itself (which contains the discriminant values
14501 Append_To (Args,
14502 Make_Attribute_Reference (Loc,
14503 Prefix => Make_Identifier (Loc, Name_uInit),
14504 Attribute_Name => Name_Address));
14506 -- Elaborated parameter. This is an access to the elaboration Boolean
14508 Append_To (Args,
14509 Make_Attribute_Reference (Loc,
14510 Prefix => Make_Identifier (Loc, New_External_Name (Tnam, 'E')),
14511 Attribute_Name => Name_Unchecked_Access));
14513 -- Add Chain parameter (not done for sequential elaboration policy, see
14514 -- comment for Create_Restricted_Task_Sequential in s-tarest.ads).
14516 if Partition_Elaboration_Policy /= 'S' then
14517 Append_To (Args, Make_Identifier (Loc, Name_uChain));
14518 end if;
14520 -- Task name parameter. Take this from the _Task_Id parameter to the
14521 -- init call unless there is a Task_Name pragma, in which case we take
14522 -- the value from the pragma.
14524 if Has_Rep_Pragma (Ttyp, Name_Task_Name, Check_Parents => False) then
14525 -- Copy expression in full, because it may be dynamic and have
14526 -- side effects.
14528 Append_To (Args,
14529 New_Copy_Tree
14530 (Expression
14531 (First
14532 (Pragma_Argument_Associations
14533 (Get_Rep_Pragma
14534 (Ttyp, Name_Task_Name, Check_Parents => False))))));
14536 else
14537 Append_To (Args, Make_Identifier (Loc, Name_uTask_Name));
14538 end if;
14540 -- Created_Task parameter. This is the _Task_Id field of the task
14541 -- record value
14543 Append_To (Args,
14544 Make_Selected_Component (Loc,
14545 Prefix => Make_Identifier (Loc, Name_uInit),
14546 Selector_Name => Make_Identifier (Loc, Name_uTask_Id)));
14548 declare
14549 Create_RE : RE_Id;
14551 begin
14552 if Restricted_Profile then
14553 if Partition_Elaboration_Policy = 'S' then
14554 Create_RE := RE_Create_Restricted_Task_Sequential;
14555 else
14556 Create_RE := RE_Create_Restricted_Task;
14557 end if;
14558 else
14559 Create_RE := RE_Create_Task;
14560 end if;
14562 Name := New_Occurrence_Of (RTE (Create_RE), Loc);
14563 end;
14565 return
14566 Make_Procedure_Call_Statement (Loc,
14567 Name => Name,
14568 Parameter_Associations => Args);
14569 end Make_Task_Create_Call;
14571 ------------------------------
14572 -- Next_Protected_Operation --
14573 ------------------------------
14575 function Next_Protected_Operation (N : Node_Id) return Node_Id is
14576 Next_Op : Node_Id;
14578 begin
14579 -- Check whether there is a subsequent body for a protected operation
14580 -- in the current protected body. In Ada2012 that includes expression
14581 -- functions that are completions.
14583 Next_Op := Next (N);
14584 while Present (Next_Op)
14585 and then Nkind (Next_Op) not in
14586 N_Subprogram_Body | N_Entry_Body | N_Expression_Function
14587 loop
14588 Next (Next_Op);
14589 end loop;
14591 return Next_Op;
14592 end Next_Protected_Operation;
14594 ---------------------
14595 -- Null_Statements --
14596 ---------------------
14598 function Null_Statements (Stats : List_Id) return Boolean is
14599 Stmt : Node_Id;
14601 begin
14602 Stmt := First (Stats);
14603 while Nkind (Stmt) /= N_Empty
14604 and then (Nkind (Stmt) in N_Null_Statement | N_Label
14605 or else
14606 (Nkind (Stmt) = N_Pragma
14607 and then
14608 Pragma_Name_Unmapped (Stmt) in Name_Unreferenced
14609 | Name_Unmodified
14610 | Name_Warnings))
14611 loop
14612 Next (Stmt);
14613 end loop;
14615 return Nkind (Stmt) = N_Empty;
14616 end Null_Statements;
14618 --------------------------
14619 -- Parameter_Block_Pack --
14620 --------------------------
14622 function Parameter_Block_Pack
14623 (Loc : Source_Ptr;
14624 Blk_Typ : Entity_Id;
14625 Actuals : List_Id;
14626 Formals : List_Id;
14627 Decls : List_Id;
14628 Stmts : List_Id) return Entity_Id
14630 Actual : Entity_Id;
14631 Expr : Node_Id := Empty;
14632 Formal : Entity_Id;
14633 Has_Param : Boolean := False;
14634 P : Entity_Id;
14635 Params : List_Id;
14636 Temp_Asn : Node_Id;
14637 Temp_Nam : Node_Id;
14639 begin
14640 Actual := First (Actuals);
14641 Formal := Defining_Identifier (First (Formals));
14642 Params := New_List;
14643 while Present (Actual) loop
14644 if Is_By_Copy_Type (Etype (Actual)) then
14645 -- Generate:
14646 -- Jnn : aliased <formal-type>
14648 Temp_Nam := Make_Temporary (Loc, 'J');
14650 Append_To (Decls,
14651 Make_Object_Declaration (Loc,
14652 Aliased_Present => True,
14653 Defining_Identifier => Temp_Nam,
14654 Object_Definition =>
14655 New_Occurrence_Of (Etype (Formal), Loc)));
14657 -- The object is initialized with an explicit assignment
14658 -- later. Indicate that it does not need an initialization
14659 -- to prevent spurious warnings if the type excludes null.
14661 Set_No_Initialization (Last (Decls));
14663 if Ekind (Formal) /= E_Out_Parameter then
14665 -- Generate:
14666 -- Jnn := <actual>
14668 Temp_Asn :=
14669 New_Occurrence_Of (Temp_Nam, Loc);
14671 Set_Assignment_OK (Temp_Asn);
14673 Append_To (Stmts,
14674 Make_Assignment_Statement (Loc,
14675 Name => Temp_Asn,
14676 Expression => New_Copy_Tree (Actual)));
14677 end if;
14679 -- If the actual is not controlling, generate:
14681 -- Jnn'unchecked_access
14683 -- and add it to aggegate for access to formals. Note that the
14684 -- actual may be by-copy but still be a controlling actual if it
14685 -- is an access to class-wide interface.
14687 if not Is_Controlling_Actual (Actual) then
14688 Append_To (Params,
14689 Make_Attribute_Reference (Loc,
14690 Attribute_Name => Name_Unchecked_Access,
14691 Prefix => New_Occurrence_Of (Temp_Nam, Loc)));
14693 Has_Param := True;
14694 end if;
14696 -- The controlling parameter is omitted
14698 else
14699 if not Is_Controlling_Actual (Actual) then
14700 Append_To (Params,
14701 Make_Reference (Loc, New_Copy_Tree (Actual)));
14703 Has_Param := True;
14704 end if;
14705 end if;
14707 Next_Actual (Actual);
14708 Next_Formal_With_Extras (Formal);
14709 end loop;
14711 if Has_Param then
14712 Expr := Make_Aggregate (Loc, Params);
14713 end if;
14715 -- Generate:
14716 -- P : Ann := (
14717 -- J1'unchecked_access;
14718 -- <actual2>'reference;
14719 -- ...);
14721 P := Make_Temporary (Loc, 'P');
14723 Append_To (Decls,
14724 Make_Object_Declaration (Loc,
14725 Defining_Identifier => P,
14726 Object_Definition => New_Occurrence_Of (Blk_Typ, Loc),
14727 Expression => Expr));
14729 return P;
14730 end Parameter_Block_Pack;
14732 ----------------------------
14733 -- Parameter_Block_Unpack --
14734 ----------------------------
14736 function Parameter_Block_Unpack
14737 (Loc : Source_Ptr;
14738 P : Entity_Id;
14739 Actuals : List_Id;
14740 Formals : List_Id) return List_Id
14742 Actual : Entity_Id;
14743 Asnmt : Node_Id;
14744 Formal : Entity_Id;
14745 Has_Asnmt : Boolean := False;
14746 Result : constant List_Id := New_List;
14748 begin
14749 Actual := First (Actuals);
14750 Formal := Defining_Identifier (First (Formals));
14751 while Present (Actual) loop
14752 if Is_By_Copy_Type (Etype (Actual))
14753 and then Ekind (Formal) /= E_In_Parameter
14754 then
14755 -- Generate:
14756 -- <actual> := P.<formal>;
14758 Asnmt :=
14759 Make_Assignment_Statement (Loc,
14760 Name =>
14761 New_Copy (Actual),
14762 Expression =>
14763 Make_Explicit_Dereference (Loc,
14764 Make_Selected_Component (Loc,
14765 Prefix =>
14766 New_Occurrence_Of (P, Loc),
14767 Selector_Name =>
14768 Make_Identifier (Loc, Chars (Formal)))));
14770 Set_Assignment_OK (Name (Asnmt));
14771 Append_To (Result, Asnmt);
14773 Has_Asnmt := True;
14774 end if;
14776 Next_Actual (Actual);
14777 Next_Formal_With_Extras (Formal);
14778 end loop;
14780 if Has_Asnmt then
14781 return Result;
14782 else
14783 return New_List (Make_Null_Statement (Loc));
14784 end if;
14785 end Parameter_Block_Unpack;
14787 ---------------------
14788 -- Reset_Scopes_To --
14789 ---------------------
14791 procedure Reset_Scopes_To (Bod : Node_Id; E : Entity_Id) is
14792 function Reset_Scope (N : Node_Id) return Traverse_Result;
14793 -- Temporaries may have been declared during expansion of the procedure
14794 -- created for an entry body or an accept alternative. Indicate that
14795 -- their scope is the new body, to ensure proper generation of uplevel
14796 -- references where needed during unnesting.
14798 procedure Reset_Scopes is new Traverse_Proc (Reset_Scope);
14800 -----------------
14801 -- Reset_Scope --
14802 -----------------
14804 function Reset_Scope (N : Node_Id) return Traverse_Result is
14805 Decl : Node_Id;
14807 begin
14808 -- If this is a block statement with an Identifier, it forms a scope,
14809 -- so we want to reset its scope but not look inside.
14811 if N /= Bod
14812 and then Nkind (N) = N_Block_Statement
14813 and then Present (Identifier (N))
14814 then
14815 Set_Scope (Entity (Identifier (N)), E);
14816 return Skip;
14818 -- Ditto for a package declaration or a full type declaration, etc.
14820 elsif (Nkind (N) = N_Package_Declaration
14821 and then N /= Specification (N))
14822 or else Nkind (N) in N_Declaration
14823 or else Nkind (N) in N_Renaming_Declaration
14824 then
14825 Set_Scope (Defining_Entity (N), E);
14826 return Skip;
14828 elsif N = Bod then
14830 -- Scan declarations in new body. Declarations in the statement
14831 -- part will be handled during later traversal.
14833 Decl := First (Declarations (N));
14834 while Present (Decl) loop
14835 Reset_Scopes (Decl);
14836 Next (Decl);
14837 end loop;
14839 elsif Nkind (N) = N_Freeze_Entity then
14841 -- Scan the actions associated with a freeze node, which may
14842 -- actually be declarations with entities that need to have
14843 -- their scopes reset.
14845 Decl := First (Actions (N));
14846 while Present (Decl) loop
14847 Reset_Scopes (Decl);
14848 Next (Decl);
14849 end loop;
14851 elsif N /= Bod and then Nkind (N) in N_Proper_Body then
14853 -- A subprogram without a separate declaration may be encountered,
14854 -- and we need to reset the subprogram's entity's scope.
14856 if Nkind (N) = N_Subprogram_Body then
14857 Set_Scope (Defining_Entity (Specification (N)), E);
14858 end if;
14860 return Skip;
14861 end if;
14863 return OK;
14864 end Reset_Scope;
14866 -- Start of processing for Reset_Scopes_To
14868 begin
14869 Reset_Scopes (Bod);
14870 end Reset_Scopes_To;
14872 ----------------------
14873 -- Set_Discriminals --
14874 ----------------------
14876 procedure Set_Discriminals (Dec : Node_Id) is
14877 D : Entity_Id;
14878 Pdef : Entity_Id;
14879 D_Minal : Entity_Id;
14881 begin
14882 pragma Assert (Nkind (Dec) = N_Protected_Type_Declaration);
14883 Pdef := Defining_Identifier (Dec);
14885 if Has_Discriminants (Pdef) then
14886 D := First_Discriminant (Pdef);
14887 while Present (D) loop
14888 D_Minal :=
14889 Make_Defining_Identifier (Sloc (D),
14890 Chars => New_External_Name (Chars (D), 'D'));
14892 Mutate_Ekind (D_Minal, E_Constant);
14893 Set_Etype (D_Minal, Etype (D));
14894 Set_Scope (D_Minal, Pdef);
14895 Set_Discriminal (D, D_Minal);
14896 Set_Discriminal_Link (D_Minal, D);
14898 Next_Discriminant (D);
14899 end loop;
14900 end if;
14901 end Set_Discriminals;
14903 -----------------------
14904 -- Trivial_Accept_OK --
14905 -----------------------
14907 function Trivial_Accept_OK return Boolean is
14908 begin
14909 case Opt.Task_Dispatching_Policy is
14911 -- If we have the default task dispatching policy in effect, we can
14912 -- definitely do the optimization (one way of looking at this is to
14913 -- think of the formal definition of the default policy being allowed
14914 -- to run any task it likes after a rendezvous, so even if notionally
14915 -- a full rescheduling occurs, we can say that our dispatching policy
14916 -- (i.e. the default dispatching policy) reorders the queue to be the
14917 -- same as just before the call.
14919 when ' ' =>
14920 return True;
14922 -- FIFO_Within_Priorities certainly does not permit this
14923 -- optimization since the Rendezvous is a scheduling action that may
14924 -- require some other task to be run.
14926 when 'F' =>
14927 return False;
14929 -- For now, disallow the optimization for all other policies. This
14930 -- may be over-conservative, but it is certainly not incorrect.
14932 when others =>
14933 return False;
14934 end case;
14935 end Trivial_Accept_OK;
14937 end Exp_Ch9;